Flutter Engine
The Flutter Engine
ShaderTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
15#include "include/core/SkData.h"
26#include "include/core/SkSize.h"
34#include "tests/Test.h"
35
36#include <cmath>
37#include <vector>
38
39#if defined(SK_GANESH) || defined(SK_GRAPHITE)
41#endif
42
43#if defined(SK_GANESH)
46struct GrContextOptions;
47#endif
48
49#if defined(SK_GRAPHITE)
52#endif
53
55 int expectedW, int expectedH,
56 SkTileMode expectedX, SkTileMode expectedY,
57 const SkMatrix& expectedM) {
58 SkTileMode tileModes[2];
59 SkMatrix localM;
60
61 // wack these so we don't get a false positive
62 localM.setScale(9999, -9999);
63 tileModes[0] = tileModes[1] = (SkTileMode)99;
64
65 SkImage* image = shader->isAImage(&localM, tileModes);
67 REPORTER_ASSERT(reporter, image->width() == expectedW);
68 REPORTER_ASSERT(reporter, image->height() == expectedH);
69 REPORTER_ASSERT(reporter, localM == expectedM);
70 REPORTER_ASSERT(reporter, tileModes[0] == expectedX);
71 REPORTER_ASSERT(reporter, tileModes[1] == expectedY);
72}
73
74DEF_TEST(Shader_isAImage, reporter) {
75 const int W = 100;
76 const int H = 100;
77 SkBitmap bm;
78 bm.allocN32Pixels(W, H);
79 auto img = bm.asImage();
80 const SkMatrix localM = SkMatrix::Scale(2, 3);
83
84 auto shader0 = bm.makeShader(tmx, tmy, SkSamplingOptions(), localM);
85 auto shader1 = bm.asImage()->makeShader(tmx, tmy, SkSamplingOptions(), localM);
86
87 check_isaimage(reporter, shader0.get(), W, H, tmx, tmy, localM);
88 check_isaimage(reporter, shader1.get(), W, H, tmx, tmy, localM);
89}
90
91// Make sure things are ok with just a single leg.
92DEF_TEST(ComposeShaderSingle, reporter) {
93 SkBitmap srcBitmap;
94 srcBitmap.allocN32Pixels(10, 10);
95 srcBitmap.eraseColor(SK_ColorRED);
96 SkCanvas canvas(srcBitmap);
97 SkPaint p;
100 SkShaders::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f)));
101 SkRRect rr;
102 SkVector rd[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
103 rr.setRectRadii({0, 0, 0, 0}, rd);
104 canvas.drawRRect(rr, p);
105}
106
107// Tests that nested blending will render as expected.
109 auto [redEffect, redError] = SkRuntimeEffect::MakeForShader(SkString(R"(
110 half4 main(float2 coord) {
111 return half4(1, 0, 0, 1);
112 }
113 )"));
114
115 auto [greenEffect, greenError] = SkRuntimeEffect::MakeForShader(SkString(R"(
116 half4 main(float2 coord) {
117 return half4(0, 1, 0, 1);
118 }
119 )"));
120
121 auto [blendEffect, blenderError] = SkRuntimeEffect::MakeForBlender(SkString(R"(
122 half4 main(half4 src, half4 dst) {
123 return (src + dst) * 0.5;
124 }
125 )"));
126
127 auto [nestedBlendEffect, nestedBlenderError] = SkRuntimeEffect::MakeForBlender(SkString(R"(
128 uniform blender child_blender;
129 half4 main(half4 src, half4 dst) {
130 return (child_blender.eval(src, dst) + dst) * 0.5;
131 }
132 )"));
133
134 sk_sp<SkShader> redShader = redEffect->makeShader(nullptr, {});
135 sk_sp<SkShader> greenShader = greenEffect->makeShader(nullptr, {});
136 sk_sp<SkBlender> blender = blendEffect->makeBlender(nullptr);
137 std::vector<SkRuntimeEffect::ChildPtr> children = {SkRuntimeEffect::ChildPtr(blender)};
138 sk_sp<SkBlender> nestedBlender = nestedBlendEffect->makeBlender(nullptr, children);
139
141 paint.setShader(SkShaders::Blend(nestedBlender, greenShader, redShader));
142 paint.setBlender(blender);
143
144 // Do the drawing.
145 SkCanvas* canvas = surface->getCanvas();
146 canvas->drawPaint(paint);
147
148 // Read pixels.
150 SkPixmap pixmap;
151 bitmap.allocPixels(surface->imageInfo());
152 SkAssertResult(bitmap.peekPixels(&pixmap));
153 if (!surface->readPixels(pixmap, 0, 0)) {
154 ERRORF(reporter, "readPixels failed");
155 return;
156 }
157
158 // Check the resulting blended color.
159 // First, in the paint's shader, red and green are averaged in the child blender to get
160 // (0.5, 0.5, 0, 1), which is then averaged with green in the parent blender to get
161 // (0.25, 0.75, 0, 1). Then, in the paint's blender this is averaged with a transparent
162 // background to get (0.125, 0.375, 0, 0.5) and then unpremuled to get (0.25, 0.75, 0, 0.5).
163 constexpr SkColor4f kExpected = {0.25f, 0.75f, 0.0f, 0.5f};
164 constexpr float kTolerance[4] = {0.01f, 0.01f, 0.0f, 0.01f};
165 SkColor4f color = pixmap.getColor4f(0, 0);
166 for (int i = 0; i < 4; ++i) {
167 if (std::abs(color[i] - kExpected[i]) > kTolerance[i]) {
169 "Wrong color, expected (%.2f %.2f %.2f %.2f), actual (%.2f, %.2f, %.2f, %.2f)",
170 kExpected.fR, kExpected.fG, kExpected.fB, kExpected.fA,
171 color.fR, color.fG, color.fB, color.fA);
172 break;
173 }
174 }
175}
176
177DEF_TEST(ShaderTestNestedBlendsCpu, reporter) {
183}
184
185#if defined(SK_GANESH)
186DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ShaderTestNestedBlendsGanesh,
187 reporter,
188 contextInfo,
193 GrDirectContext* context = contextInfo.directContext();
196}
197#endif
198
199#if defined(SK_GRAPHITE)
200DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(ShaderTestNestedBlendsGraphite, reporter, context,
202 using namespace skgpu::graphite;
203
207 std::unique_ptr<Recorder> recorder = context->makeRecorder();
210}
211#endif
reporter
Definition: FontMgrTest.cpp:39
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
static constexpr float kTolerance
Definition: GrQuadUtils.cpp:29
static void check_isaimage(skiatest::Reporter *reporter, SkShader *shader, int expectedW, int expectedH, SkTileMode expectedX, SkTileMode expectedY, const SkMatrix &expectedM)
Definition: ShaderTest.cpp:54
static void test_nested_blends(skiatest::Reporter *reporter, SkSurface *surface)
Definition: ShaderTest.cpp:108
DEF_TEST(Shader_isAImage, reporter)
Definition: ShaderTest.cpp:74
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kClear
r = 0
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
SkTileMode
Definition: SkTileMode.h:13
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(name, reporter, graphite_context, ctsEnforcement)
Definition: Test.h:377
#define ERRORF(r,...)
Definition: Test.h:293
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition: Test.h:434
#define W
Definition: aaa.cpp:17
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
Definition: SkCanvas.cpp:1705
int width() const
Definition: SkImage.h:285
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
int height() const
Definition: SkImage.h:291
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:296
SkColor4f getColor4f(int x, int y) const
Definition: SkPixmap.cpp:388
void setRectRadii(const SkRect &rect, const SkVector radii[4])
Definition: SkRRect.cpp:189
static Result MakeForBlender(SkString sksl, const Options &)
static Result MakeForShader(SkString sksl, const Options &)
SkImage * isAImage(SkMatrix *localMatrix, SkTileMode xy[2]) const
Definition: SkShader.cpp:22
const Paint & paint
Definition: color_source.cc:38
DlColor color
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkShader > Blend(SkBlendMode mode, sk_sp< SkShader > dst, sk_sp< SkShader > src)
SK_API sk_sp< SkShader > MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, int numOctaves, SkScalar seed, const SkISize *tileSize=nullptr)
SK_API sk_sp< SkShader > Empty()
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
Definition: bitmap.py:1
SkSamplingOptions(SkFilterMode::kLinear))
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707
SkTileMode tmy
SkTileMode tmx
Definition: SkMD5.cpp:130
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)