1 /*
2 * Copyright 2019 Google LLC
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
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkBlender.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkSurface.h"
15 #include "include/effects/SkBlenders.h"
16 #include "include/effects/SkRuntimeEffect.h"
17 #include "include/gpu/GrDirectContext.h"
18 #include "src/core/SkColorSpacePriv.h"
19 #include "src/core/SkRuntimeEffectPriv.h"
20 #include "src/core/SkTLazy.h"
21 #include "src/gpu/GrCaps.h"
22 #include "src/gpu/GrColor.h"
23 #include "src/gpu/GrDirectContextPriv.h"
24 #include "src/gpu/GrFragmentProcessor.h"
25 #include "src/gpu/GrImageInfo.h"
26 #include "src/gpu/SurfaceFillContext.h"
27 #include "src/gpu/effects/GrSkSLFP.h"
28 #include "tests/Test.h"
29
30 #include <algorithm>
31 #include <thread>
32
test_invalid_effect(skiatest::Reporter * r,const char * src,const char * expected)33 void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) {
34 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
35 REPORTER_ASSERT(r, !effect);
36 REPORTER_ASSERT(r, errorText.contains(expected),
37 "Expected error message to contain \"%s\". Actual message: \"%s\"",
38 expected, errorText.c_str());
39 };
40
41 #define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }"
42
DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes,r)43 DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) {
44 // Runtime SkSL supports a limited set of uniform types. No bool, for example:
45 test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform");
46 }
47
DEF_TEST(SkRuntimeEffectInvalid_NoInVariables,r)48 DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) {
49 // 'in' variables aren't allowed at all:
50 test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'");
51 test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'");
52 test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'");
53 test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'");
54 }
55
DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction,r)56 DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) {
57 test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }",
58 "function 'half4 missing()' is not defined");
59 }
60
DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain,r)61 DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) {
62 // Shouldn't be possible to create an SkRuntimeEffect without "main"
63 test_invalid_effect(r, "", "main");
64 }
65
DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed,r)66 DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) {
67 // sk_Caps is an internal system. It should not be visible to runtime effects
68 test_invalid_effect(
69 r,
70 "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }",
71 "unknown identifier 'sk_Caps'");
72 }
73
DEF_TEST(SkRuntimeEffect_DeadCodeEliminationStackOverflow,r)74 DEF_TEST(SkRuntimeEffect_DeadCodeEliminationStackOverflow, r) {
75 // Verify that a deeply-nested loop does not cause stack overflow during SkVM dead-code
76 // elimination.
77 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(R"(
78 half4 main(half4 color) {
79 half value = color.r;
80
81 for (int a=0; a<10; ++a) { // 10
82 for (int b=0; b<10; ++b) { // 100
83 for (int c=0; c<10; ++c) { // 1000
84 for (int d=0; d<10; ++d) { // 10000
85 ++value;
86 }}}}
87
88 return value.xxxx;
89 }
90 )"));
91 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
92 }
93
DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions,r)94 DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) {
95 auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) {
96 SkRuntimeEffect::Options opt = SkRuntimeEffectPriv::ES3Options();
97 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt);
98 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
99 };
100
101 test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type");
102 test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN);
103 }
104
DEF_TEST(SkRuntimeEffectForColorFilter,r)105 DEF_TEST(SkRuntimeEffectForColorFilter, r) {
106 // Tests that the color filter factory rejects or accepts certain SkSL constructs
107 auto test_valid = [r](const char* sksl) {
108 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
109 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
110 };
111
112 auto test_invalid = [r](const char* sksl, const char* expected) {
113 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
114 REPORTER_ASSERT(r, !effect);
115 REPORTER_ASSERT(r,
116 errorText.contains(expected),
117 "Expected error message to contain \"%s\". Actual message: \"%s\"",
118 expected,
119 errorText.c_str());
120 };
121
122 // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4
123 test_valid("half4 main(half4 c) { return c; }");
124 test_valid("float4 main(half4 c) { return c; }");
125 test_valid("half4 main(float4 c) { return c; }");
126 test_valid("float4 main(float4 c) { return c; }");
127 test_valid("vec4 main(half4 c) { return c; }");
128 test_valid("half4 main(vec4 c) { return c; }");
129 test_valid("vec4 main(vec4 c) { return c; }");
130
131 // Invalid return types
132 test_invalid("void main(half4 c) {}", "'main' must return");
133 test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return");
134
135 // Invalid argument types (some are valid as shaders, but not color filters)
136 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
137 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
138 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
139
140 // sk_FragCoord should not be available
141 test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier");
142
143 // Sampling a child shader requires that we pass explicit coords
144 test_valid("uniform shader child;"
145 "half4 main(half4 c) { return child.eval(c.rg); }");
146
147 // Sampling a colorFilter requires a color
148 test_valid("uniform colorFilter child;"
149 "half4 main(half4 c) { return child.eval(c); }");
150
151 // Sampling a blender requires two colors
152 test_valid("uniform blender child;"
153 "half4 main(half4 c) { return child.eval(c, c); }");
154 }
155
DEF_TEST(SkRuntimeEffectForBlender,r)156 DEF_TEST(SkRuntimeEffectForBlender, r) {
157 // Tests that the blender factory rejects or accepts certain SkSL constructs
158 auto test_valid = [r](const char* sksl) {
159 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
160 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
161 };
162
163 auto test_invalid = [r](const char* sksl, const char* expected) {
164 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
165 REPORTER_ASSERT(r, !effect);
166 REPORTER_ASSERT(r,
167 errorText.contains(expected),
168 "Expected error message to contain \"%s\". Actual message: \"%s\"",
169 expected,
170 errorText.c_str());
171 };
172
173 // Blenders must use the 'half4 main(half4, half4)' signature. Any mixture of float4/vec4/half4
174 // is allowed.
175 test_valid("half4 main(half4 s, half4 d) { return s; }");
176 test_valid("float4 main(float4 s, float4 d) { return d; }");
177 test_valid("float4 main(half4 s, float4 d) { return s; }");
178 test_valid("half4 main(float4 s, half4 d) { return d; }");
179 test_valid("vec4 main(half4 s, half4 d) { return s; }");
180 test_valid("half4 main(vec4 s, vec4 d) { return d; }");
181 test_valid("vec4 main(vec4 s, vec4 d) { return s; }");
182
183 // Invalid return types
184 test_invalid("void main(half4 s, half4 d) {}", "'main' must return");
185 test_invalid("half3 main(half4 s, half4 d) { return s.rgb; }", "'main' must return");
186
187 // Invalid argument types (some are valid as shaders/color filters)
188 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
189 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
190 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
191 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
192 test_invalid("half4 main(float2 p, half4 a, half4 b) { return a; }", "'main' parameter");
193 test_invalid("half4 main(half4 a, half4 b, half4 c) { return a; }", "'main' parameter");
194
195 // sk_FragCoord should not be available
196 test_invalid("half4 main(half4 s, half4 d) { return sk_FragCoord.xy01; }",
197 "unknown identifier");
198
199 // Sampling a child shader requires that we pass explicit coords
200 test_valid("uniform shader child;"
201 "half4 main(half4 s, half4 d) { return child.eval(s.rg); }");
202
203 // Sampling a colorFilter requires a color
204 test_valid("uniform colorFilter child;"
205 "half4 main(half4 s, half4 d) { return child.eval(d); }");
206
207 // Sampling a blender requires two colors
208 test_valid("uniform blender child;"
209 "half4 main(half4 s, half4 d) { return child.eval(s, d); }");
210 }
211
DEF_TEST(SkRuntimeEffectForShader,r)212 DEF_TEST(SkRuntimeEffectForShader, r) {
213 // Tests that the shader factory rejects or accepts certain SkSL constructs
214 auto test_valid = [r](const char* sksl, SkRuntimeEffect::Options options = {}) {
215 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), options);
216 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
217 };
218
219 auto test_invalid = [r](const char* sksl,
220 const char* expected,
221 SkRuntimeEffect::Options options = {}) {
222 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
223 REPORTER_ASSERT(r, !effect);
224 REPORTER_ASSERT(r,
225 errorText.contains(expected),
226 "Expected error message to contain \"%s\". Actual message: \"%s\"",
227 expected,
228 errorText.c_str());
229 };
230
231 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
232 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
233 test_valid("half4 main(float2 p) { return p.xyxy; }");
234 test_valid("float4 main(float2 p) { return p.xyxy; }");
235 test_valid("vec4 main(float2 p) { return p.xyxy; }");
236 test_valid("half4 main(vec2 p) { return p.xyxy; }");
237 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
238 test_valid("half4 main(float2 p, half4 c) { return c; }");
239 test_valid("half4 main(float2 p, float4 c) { return c; }");
240 test_valid("half4 main(float2 p, vec4 c) { return c; }");
241 test_valid("float4 main(float2 p, half4 c) { return c; }");
242 test_valid("vec4 main(float2 p, half4 c) { return c; }");
243 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
244
245 // Invalid return types
246 test_invalid("void main(float2 p) {}", "'main' must return");
247 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
248
249 // Invalid argument types (some are valid as color filters, but not shaders)
250 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
251 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
252
253 // sk_FragCoord should be available, but only if we've enabled it via Options
254 test_invalid("half4 main(float2 p) { return sk_FragCoord.xy01; }",
255 "unknown identifier 'sk_FragCoord'");
256
257 SkRuntimeEffect::Options optionsWithFragCoord;
258 SkRuntimeEffectPriv::EnableFragCoord(&optionsWithFragCoord);
259 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }", optionsWithFragCoord);
260
261 // Sampling a child shader requires that we pass explicit coords
262 test_valid("uniform shader child;"
263 "half4 main(float2 p) { return child.eval(p); }");
264
265 // Sampling a colorFilter requires a color
266 test_valid("uniform colorFilter child;"
267 "half4 main(float2 p, half4 c) { return child.eval(c); }");
268
269 // Sampling a blender requires two colors
270 test_valid("uniform blender child;"
271 "half4 main(float2 p, half4 c) { return child.eval(c, c); }");
272 }
273
274 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
275
paint_canvas(SkCanvas * canvas,SkPaint * paint,const PreTestFn & preTestCallback)276 void paint_canvas(SkCanvas* canvas, SkPaint* paint, const PreTestFn& preTestCallback) {
277 canvas->save();
278 if (preTestCallback) {
279 preTestCallback(canvas, paint);
280 }
281 canvas->drawPaint(*paint);
282 canvas->restore();
283 }
284
verify_2x2_surface_results(skiatest::Reporter * r,const SkRuntimeEffect * effect,SkSurface * surface,std::array<GrColor,4> expected)285 static void verify_2x2_surface_results(skiatest::Reporter* r,
286 const SkRuntimeEffect* effect,
287 SkSurface* surface,
288 std::array<GrColor, 4> expected) {
289 std::array<GrColor, 4> actual;
290 SkImageInfo info = surface->imageInfo();
291 if (!surface->readPixels(info, actual.data(), info.minRowBytes(), /*srcX=*/0, /*srcY=*/0)) {
292 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
293 return;
294 }
295
296 if (actual != expected) {
297 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
298 SkStringPrintf("\n"
299 "Expected: [ %08x %08x %08x %08x ]\n"
300 "Got : [ %08x %08x %08x %08x ]\n"
301 "SkSL:\n%s\n",
302 expected[0], expected[1], expected[2], expected[3],
303 actual[0], actual[1], actual[2], actual[3],
304 effect->source().c_str()));
305 }
306 }
307
308 class TestEffect {
309 public:
TestEffect(skiatest::Reporter * r,sk_sp<SkSurface> surface)310 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
311 : fReporter(r), fSurface(std::move(surface)) {}
312
build(const char * src)313 void build(const char* src) {
314 SkRuntimeEffect::Options options;
315 SkRuntimeEffectPriv::EnableFragCoord(&options);
316 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src), options);
317 if (!effect) {
318 REPORT_FAILURE(fReporter, "effect",
319 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
320 return;
321 }
322 fBuilder.init(std::move(effect));
323 }
324
uniform(const char * name)325 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
326 return fBuilder->uniform(name);
327 }
328
child(const char * name)329 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
330 return fBuilder->child(name);
331 }
332
test(std::array<GrColor,4> expected,PreTestFn preTestCallback=nullptr)333 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
334 auto shader = fBuilder->makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
335 if (!shader) {
336 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
337 return;
338 }
339
340 SkCanvas* canvas = fSurface->getCanvas();
341 SkPaint paint;
342 paint.setShader(std::move(shader));
343 paint.setBlendMode(SkBlendMode::kSrc);
344
345 paint_canvas(canvas, &paint, preTestCallback);
346
347 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
348 }
349
test(GrColor expected,PreTestFn preTestCallback=nullptr)350 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
351 this->test({expected, expected, expected, expected}, preTestCallback);
352 }
353
354 private:
355 skiatest::Reporter* fReporter;
356 sk_sp<SkSurface> fSurface;
357 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
358 };
359
360 class TestBlend {
361 public:
TestBlend(skiatest::Reporter * r,sk_sp<SkSurface> surface)362 TestBlend(skiatest::Reporter* r, sk_sp<SkSurface> surface)
363 : fReporter(r), fSurface(std::move(surface)) {}
364
build(const char * src)365 void build(const char* src) {
366 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(src));
367 if (!effect) {
368 REPORT_FAILURE(fReporter, "effect",
369 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
370 return;
371 }
372 fBuilder.init(std::move(effect));
373 }
374
uniform(const char * name)375 SkRuntimeBlendBuilder::BuilderUniform uniform(const char* name) {
376 return fBuilder->uniform(name);
377 }
378
child(const char * name)379 SkRuntimeBlendBuilder::BuilderChild child(const char* name) {
380 return fBuilder->child(name);
381 }
382
test(std::array<GrColor,4> expected,PreTestFn preTestCallback=nullptr)383 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
384 auto blender = fBuilder->makeBlender();
385 if (!blender) {
386 REPORT_FAILURE(fReporter, "blender", SkString("Effect didn't produce a blender"));
387 return;
388 }
389
390 SkCanvas* canvas = fSurface->getCanvas();
391 SkPaint paint;
392 paint.setBlender(std::move(blender));
393 paint.setColor(SK_ColorGRAY);
394
395 paint_canvas(canvas, &paint, preTestCallback);
396
397 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
398 }
399
test(GrColor expected,PreTestFn preTestCallback=nullptr)400 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
401 this->test({expected, expected, expected, expected}, preTestCallback);
402 }
403
404 private:
405 skiatest::Reporter* fReporter;
406 sk_sp<SkSurface> fSurface;
407 SkTLazy<SkRuntimeBlendBuilder> fBuilder;
408 };
409
410 // Produces a 2x2 bitmap shader, with opaque colors:
411 // [ Red, Green ]
412 // [ Blue, White ]
make_RGBW_shader()413 static sk_sp<SkShader> make_RGBW_shader() {
414 SkBitmap bmp;
415 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
416 SkIRect topLeft = SkIRect::MakeWH(1, 1);
417 bmp.pixmap().erase(SK_ColorRED, topLeft);
418 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
419 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
420 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
421 return bmp.makeShader(SkSamplingOptions());
422 }
423
test_RuntimeEffect_Shaders(skiatest::Reporter * r,GrRecordingContext * rContext)424 static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
425 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
426 sk_sp<SkSurface> surface = rContext
427 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
428 : SkSurface::MakeRaster(info);
429 REPORTER_ASSERT(r, surface);
430 TestEffect effect(r, surface);
431
432 using float4 = std::array<float, 4>;
433 using int4 = std::array<int, 4>;
434
435 // Local coords
436 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
437 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
438
439 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
440 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
441 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
442 effect.test(0xFFBF4000);
443 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
444 effect.test(0x7F0000FF); // Tests that we don't clamp to valid premul
445
446 // Same, with integer uniforms
447 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
448 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
449 effect.test(0xFFBF4000);
450 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
451 effect.test(0x7F0000FF); // Tests that we don't clamp to valid premul
452
453 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
454 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
455 // make sure we're not saturating unexpectedly.
456 effect.build(
457 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
458 effect.test({0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F},
459 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
460
461 // Runtime effects should use relaxed precision rules by default
462 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
463 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
464
465 // ... and support *returning* float4 (aka vec4), not just half4
466 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
467 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
468 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
469 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
470
471 // Mutating coords should work. (skbug.com/10918)
472 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
473 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
474 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
475 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
476 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
477
478 //
479 // Sampling children
480 //
481
482 // Sampling a null child should return the paint color
483 effect.build("uniform shader child;"
484 "half4 main(float2 p) { return child.eval(p); }");
485 effect.child("child") = nullptr;
486 effect.test(0xFF00FFFF,
487 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
488
489 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
490
491 // Sampling a simple child at our coordinates
492 effect.build("uniform shader child;"
493 "half4 main(float2 p) { return child.eval(p); }");
494 effect.child("child") = rgbwShader;
495 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
496
497 // Sampling with explicit coordinates (reflecting about the diagonal)
498 effect.build("uniform shader child;"
499 "half4 main(float2 p) { return child.eval(p.yx); }");
500 effect.child("child") = rgbwShader;
501 effect.test({0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF});
502
503 // Bind an image shader, but don't use it - ensure that we don't assert or generate bad shaders.
504 // (skbug.com/12429)
505 effect.build("uniform shader child;"
506 "half4 main(float2 p) { return half4(0, 1, 0, 1); }");
507 effect.child("child") = rgbwShader;
508 effect.test(0xFF00FF00);
509
510 //
511 // Helper functions
512 //
513
514 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
515 effect.build("float2 helper(float2 x) { return x + 1; }"
516 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
517 effect.test(0xFF00FFFF);
518 }
519
DEF_TEST(SkRuntimeEffectSimple,r)520 DEF_TEST(SkRuntimeEffectSimple, r) {
521 test_RuntimeEffect_Shaders(r, nullptr);
522 }
523
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU,r,ctxInfo)524 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
525 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
526 }
527
test_RuntimeEffect_Blenders(skiatest::Reporter * r,GrRecordingContext * rContext)528 static void test_RuntimeEffect_Blenders(skiatest::Reporter* r, GrRecordingContext* rContext) {
529 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
530 sk_sp<SkSurface> surface = rContext
531 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
532 : SkSurface::MakeRaster(info);
533 REPORTER_ASSERT(r, surface);
534 TestBlend effect(r, surface);
535
536 using float2 = std::array<float, 2>;
537 using float4 = std::array<float, 4>;
538 using int4 = std::array<int, 4>;
539
540 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
541 effect.build("uniform float4 gColor; half4 main(half4 s, half4 d) { return half4(gColor); }");
542 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
543 effect.test(0xFFBF4000);
544 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
545 effect.test(0x7F0000FF); // We don't clamp here either
546
547 // Same, with integer uniforms
548 effect.build("uniform int4 gColor;"
549 "half4 main(half4 s, half4 d) { return half4(gColor) / 255.0; }");
550 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
551 effect.test(0xFFBF4000);
552 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
553 effect.test(0x7F0000FF); // We don't clamp here either
554
555 // Verify that mutating the source and destination colors is allowed
556 effect.build("half4 main(half4 s, half4 d) { s += d; d += s; return half4(1); }");
557 effect.test(0xFFFFFFFF);
558
559 // Verify that we can write out the source color (ignoring the dest color)
560 // This is equivalent to the kSrc blend mode.
561 effect.build("half4 main(half4 s, half4 d) { return s; }");
562 effect.test(0xFF888888);
563
564 // Fill the destination with a variety of colors (using the RGBW shader)
565 SkPaint rgbwPaint;
566 rgbwPaint.setShader(make_RGBW_shader());
567 rgbwPaint.setBlendMode(SkBlendMode::kSrc);
568 surface->getCanvas()->drawPaint(rgbwPaint);
569
570 // Verify that we can read back the dest color exactly as-is (ignoring the source color)
571 // This is equivalent to the kDst blend mode.
572 effect.build("half4 main(half4 s, half4 d) { return d; }");
573 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
574
575 // Verify that we can invert the destination color (including the alpha channel).
576 // The expected outputs are the exact inverse of the previous test.
577 effect.build("half4 main(half4 s, half4 d) { return half4(1) - d; }");
578 effect.test({0x00FFFF00, 0x00FF00FF, 0x0000FFFF, 0x00000000});
579
580 // Verify that color values are clamped to 0 and 1.
581 effect.build("half4 main(half4 s, half4 d) { return half4(-1); }");
582 effect.test(0x00000000);
583 effect.build("half4 main(half4 s, half4 d) { return half4(2); }");
584 effect.test(0xFFFFFFFF);
585
586 //
587 // Sampling children
588 //
589
590 // Sampling a null shader/color filter should return the paint color.
591 effect.build("uniform shader child;"
592 "half4 main(half4 s, half4 d) { return child.eval(s.rg); }");
593 effect.child("child") = nullptr;
594 effect.test(0xFF00FFFF,
595 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
596
597 effect.build("uniform colorFilter child;"
598 "half4 main(half4 s, half4 d) { return child.eval(s); }");
599 effect.child("child") = nullptr;
600 effect.test(0xFF00FFFF,
601 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
602
603 // Sampling a null blender should do a src-over blend. Draw 50% black over RGBW to verify this.
604 surface->getCanvas()->drawPaint(rgbwPaint);
605 effect.build("uniform blender child;"
606 "half4 main(half4 s, half4 d) { return child.eval(s, d); }");
607 effect.child("child") = nullptr;
608 effect.test({0xFF000080, 0xFF008000, 0xFF800000, 0xFF808080},
609 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({0.0f, 0.0f, 0.0f, 0.497f}); });
610
611 // Sampling a shader at various coordinates
612 effect.build("uniform shader child;"
613 "uniform half2 pos;"
614 "half4 main(half4 s, half4 d) { return child.eval(pos); }");
615 effect.child("child") = make_RGBW_shader();
616 effect.uniform("pos") = float2{0, 0};
617 effect.test(0xFF0000FF);
618
619 effect.uniform("pos") = float2{1, 0};
620 effect.test(0xFF00FF00);
621
622 effect.uniform("pos") = float2{0, 1};
623 effect.test(0xFFFF0000);
624
625 effect.uniform("pos") = float2{1, 1};
626 effect.test(0xFFFFFFFF);
627
628 // Sampling a color filter
629 effect.build("uniform colorFilter child;"
630 "half4 main(half4 s, half4 d) { return child.eval(half4(1)); }");
631 effect.child("child") = SkColorFilters::Blend(0xFF012345, SkBlendMode::kSrc);
632 effect.test(0xFF452301);
633
634 // Sampling a built-in blender
635 surface->getCanvas()->drawPaint(rgbwPaint);
636 effect.build("uniform blender child;"
637 "half4 main(half4 s, half4 d) { return child.eval(s, d); }");
638 effect.child("child") = SkBlender::Mode(SkBlendMode::kPlus);
639 effect.test({0xFF4523FF, 0xFF45FF01, 0xFFFF2301, 0xFFFFFFFF},
640 [](SkCanvas*, SkPaint* paint) { paint->setColor(0xFF012345); });
641
642 // Sampling a runtime-effect blender
643 surface->getCanvas()->drawPaint(rgbwPaint);
644 effect.build("uniform blender child;"
645 "half4 main(half4 s, half4 d) { return child.eval(s, d); }");
646 effect.child("child") = SkBlenders::Arithmetic(0, 1, 1, 0, /*enforcePremul=*/false);
647 effect.test({0xFF4523FF, 0xFF45FF01, 0xFFFF2301, 0xFFFFFFFF},
648 [](SkCanvas*, SkPaint* paint) { paint->setColor(0xFF012345); });
649 }
650
DEF_TEST(SkRuntimeEffect_Blender_CPU,r)651 DEF_TEST(SkRuntimeEffect_Blender_CPU, r) {
652 test_RuntimeEffect_Blenders(r, /*rContext=*/nullptr);
653 }
654
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffect_Blender_GPU,r,ctxInfo)655 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffect_Blender_GPU, r, ctxInfo) {
656 test_RuntimeEffect_Blenders(r, ctxInfo.directContext());
657 }
658
DEF_TEST(SkRuntimeShaderBuilderReuse,r)659 DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
660 const char* kSource = R"(
661 uniform half x;
662 half4 main(float2 p) { return half4(x); }
663 )";
664
665 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
666 REPORTER_ASSERT(r, effect);
667
668 // Test passes if this sequence doesn't assert. skbug.com/10667
669 SkRuntimeShaderBuilder b(std::move(effect));
670 b.uniform("x") = 0.0f;
671 auto shader_0 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
672
673 b.uniform("x") = 1.0f;
674 auto shader_1 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
675 }
676
DEF_TEST(SkRuntimeBlendBuilderReuse,r)677 DEF_TEST(SkRuntimeBlendBuilderReuse, r) {
678 const char* kSource = R"(
679 uniform half x;
680 half4 main(half4 s, half4 d) { return half4(x); }
681 )";
682
683 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForBlender(SkString(kSource)).effect;
684 REPORTER_ASSERT(r, effect);
685
686 // We should be able to construct multiple SkBlenders in a row without asserting.
687 SkRuntimeBlendBuilder b(std::move(effect));
688 for (float x = 0.0f; x <= 2.0f; x += 2.0f) {
689 b.uniform("x") = x;
690 sk_sp<SkBlender> blender = b.makeBlender();
691 }
692 }
693
DEF_TEST(SkRuntimeShaderBuilderSetUniforms,r)694 DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
695 const char* kSource = R"(
696 uniform half x;
697 uniform vec2 offset;
698 half4 main(float2 p) { return half4(x); }
699 )";
700
701 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
702 REPORTER_ASSERT(r, effect);
703
704 SkRuntimeShaderBuilder b(std::move(effect));
705
706 // Test passes if this sequence doesn't assert.
707 float x = 1.0f;
708 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
709
710 // add extra value to ensure that set doesn't try to use sizeof(array)
711 float origin[] = { 2.0f, 3.0f, 4.0f };
712 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
713
714 #ifndef SK_DEBUG
715 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
716 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
717 #endif
718
719 auto shader = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
720 }
721
DEF_TEST(SkRuntimeEffectThreaded,r)722 DEF_TEST(SkRuntimeEffectThreaded, r) {
723 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
724 // This tests that we can safely use it from more than one thread, and also
725 // that programs don't refer to shared structures owned by the compiler.
726 // skbug.com/10589
727 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
728
729 std::thread threads[16];
730 for (auto& thread : threads) {
731 thread = std::thread([r]() {
732 SkRuntimeEffect::Options options;
733 SkRuntimeEffectPriv::EnableFragCoord(&options);
734 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource), options);
735 REPORTER_ASSERT(r, effect);
736 });
737 }
738
739 for (auto& thread : threads) {
740 thread.join();
741 }
742 }
743
DEF_TEST(SkRuntimeColorFilterSingleColor,r)744 DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
745 // Test runtime colorfilters support filterColor4f().
746 auto [effect, err] =
747 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
748 REPORTER_ASSERT(r, effect);
749 REPORTER_ASSERT(r, err.isEmpty());
750
751 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
752 REPORTER_ASSERT(r, cf);
753
754 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
755 sk_srgb_singleton(), sk_srgb_singleton());
756 REPORTER_ASSERT(r, c.fR == 0.0625f);
757 REPORTER_ASSERT(r, c.fG == 0.25f);
758 REPORTER_ASSERT(r, c.fB == 0.5625f);
759 REPORTER_ASSERT(r, c.fA == 1.0f);
760 }
761
test_RuntimeEffectStructNameReuse(skiatest::Reporter * r,GrRecordingContext * rContext)762 static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
763 // Test that two different runtime effects can reuse struct names in a single paint operation
764 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
765 "uniform shader paint;"
766 "struct S { half4 rgba; };"
767 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
768 "half4 main(float2 p) { S s; s.rgba = paint.eval(p); process(s); return s.rgba; }"
769 ));
770 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
771 sk_sp<SkShader> nullChild = nullptr;
772 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
773 /*childCount=*/1, /*localMatrix=*/nullptr,
774 /*isOpaque=*/false);
775
776 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
777 sk_sp<SkSurface> surface = rContext
778 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
779 : SkSurface::MakeRaster(info);
780 REPORTER_ASSERT(r, surface);
781
782 TestEffect effect(r, surface);
783 effect.build(
784 "uniform shader child;"
785 "struct S { float2 coord; };"
786 "void process(inout S s) { s.coord = s.coord.yx; }"
787 "half4 main(float2 p) { S s; s.coord = p; process(s); return child.eval(s.coord); "
788 "}");
789 effect.child("child") = child;
790 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
791 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
792 });
793 }
794
DEF_TEST(SkRuntimeStructNameReuse,r)795 DEF_TEST(SkRuntimeStructNameReuse, r) {
796 test_RuntimeEffectStructNameReuse(r, nullptr);
797 }
798
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU,r,ctxInfo)799 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
800 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
801 }
802
DEF_TEST(SkRuntimeColorFilterFlags,r)803 DEF_TEST(SkRuntimeColorFilterFlags, r) {
804 { // Here's a non-trivial filter that doesn't change alpha.
805 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
806 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
807 REPORTER_ASSERT(r, effect && err.isEmpty());
808 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
809 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
810 }
811
812 { // Here's one that definitely changes alpha.
813 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
814 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
815 REPORTER_ASSERT(r, effect && err.isEmpty());
816 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
817 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
818 }
819 }
820
DEF_TEST(SkRuntimeShaderSampleCoords,r)821 DEF_TEST(SkRuntimeShaderSampleCoords, r) {
822 // This test verifies that we detect calls to sample where the coords are the same as those
823 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
824 // sampling. This optimization is implemented very conservatively.
825 //
826 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
827 // FP, depending on how the coords parameter to main is used.
828
829 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
830 auto [effect, err] =
831 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
832 REPORTER_ASSERT(r, effect);
833
834 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
835 auto fp = GrSkSLFP::Make(effect, "test_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
836 "child", std::move(child));
837 REPORTER_ASSERT(r, fp);
838
839 REPORTER_ASSERT(r, fp->childProcessor(0)->sampleUsage().isExplicit() == expectExplicit);
840 REPORTER_ASSERT(r, fp->usesSampleCoords() == expectReferencesSampleCoords);
841 };
842
843 // Cases where our optimization is valid, and works:
844
845 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
846 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
847 test("half4 main(float2 xy) { return child.eval(xy); }", false, false);
848 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
849 test("half4 main(float2 xy) { return child.eval(xy) + sin(xy.x); }", false, true);
850
851 // Cases where our optimization is not valid, and does not happen:
852
853 // Sampling with values completely unrelated to passed-in coords
854 test("half4 main(float2 xy) { return child.eval(float2(0, 0)); }", true, false);
855 // Use of expression involving passed in coords
856 test("half4 main(float2 xy) { return child.eval(xy * 0.5); }", true, true);
857 // Use of coords after modification
858 test("half4 main(float2 xy) { xy *= 2; return child.eval(xy); }", true, true);
859 // Use of coords after modification via out-param call
860 test("void adjust(inout float2 xy) { xy *= 2; }"
861 "half4 main(float2 xy) { adjust(xy); return child.eval(xy); }", true, true);
862
863 // There should (must) not be any false-positive cases. There are false-negatives.
864 // In all of these cases, our optimization would be valid, but does not happen:
865
866 // Direct use of passed-in coords, modified after use
867 test("half4 main(float2 xy) { half4 c = child.eval(xy); xy *= 2; return c; }", true, true);
868 // Passed-in coords copied to a temp variable
869 test("half4 main(float2 xy) { float2 p = xy; return child.eval(p); }", true, true);
870 // Use of coords passed to helper function
871 test("half4 helper(float2 xy) { return child.eval(xy); }"
872 "half4 main(float2 xy) { return helper(xy); }", true, true);
873 }
874
DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized,r,ctxInfo)875 DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) {
876 struct FpAndKey {
877 std::unique_ptr<GrFragmentProcessor> fp;
878 SkTArray<uint32_t, true> key;
879 };
880
881 // Constant color, but with an 'specialize' option that decides if the color is inserted in the
882 // SkSL as a literal, or left as a uniform
883 auto make_color_fp = [&](SkPMColor4f color, bool specialize) {
884 auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
885 uniform half4 color;
886 half4 main(float2 xy) { return color; }
887 )");
888 FpAndKey result;
889 result.fp = GrSkSLFP::Make(std::move(effect), "color_fp", /*inputFP=*/nullptr,
890 GrSkSLFP::OptFlags::kNone,
891 "color", GrSkSLFP::SpecializeIf(specialize, color));
892 GrProcessorKeyBuilder builder(&result.key);
893 result.fp->addToKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(), &builder);
894 builder.flush();
895 return result;
896 };
897
898 FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false),
899 uGreen = make_color_fp({0, 1, 0, 1}, false),
900 sRed = make_color_fp({1, 0, 0, 1}, true),
901 sGreen = make_color_fp({0, 1, 0, 1}, true);
902
903 // uRed and uGreen should have the same key - they just have different uniforms
904 SkASSERT(uRed.key == uGreen.key);
905 // sRed and sGreen should have keys that are different from the uniform case, and each other
906 SkASSERT(sRed.key != uRed.key);
907 SkASSERT(sGreen.key != uRed.key);
908 SkASSERT(sRed.key != sGreen.key);
909 }
910
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrSkSLFP_UniformArray,r,ctxInfo)911 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrSkSLFP_UniformArray, r, ctxInfo) {
912 // Make a fill-context to draw into.
913 GrDirectContext* directContext = ctxInfo.directContext();
914 SkImageInfo info = SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
915 std::unique_ptr<skgpu::SurfaceFillContext> testCtx =
916 directContext->priv().makeSFC(info, SkBackingFit::kExact);
917
918 // Make an effect that takes a uniform array as input.
919 static constexpr std::array<float, 4> kRed {1.0f, 0.0f, 0.0f, 1.0f};
920 static constexpr std::array<float, 4> kGreen{0.0f, 1.0f, 0.0f, 1.0f};
921 static constexpr std::array<float, 4> kBlue {0.0f, 0.0f, 1.0f, 1.0f};
922 static constexpr std::array<float, 4> kGray {0.499f, 0.499f, 0.499f, 1.0f};
923
924 for (const auto& colorArray : {kRed, kGreen, kBlue, kGray}) {
925 // Compile our runtime effect.
926 auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
927 uniform half color[4];
928 half4 main(float2 xy) { return half4(color[0], color[1], color[2], color[3]); }
929 )");
930 // Render our shader into the fill-context with our various input colors.
931 testCtx->fillWithFP(GrSkSLFP::Make(std::move(effect), "test_fp",
932 /*inputFP=*/nullptr,
933 GrSkSLFP::OptFlags::kNone,
934 "color", SkMakeSpan(colorArray)));
935 // Read our color back and ensure it matches.
936 GrColor actual;
937 GrPixmap pixmap(info, &actual, sizeof(GrColor));
938 if (!testCtx->readPixels(directContext, pixmap, /*srcPt=*/{0, 0})) {
939 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
940 break;
941 }
942 if (actual != GrColorPackRGBA(255 * colorArray[0], 255 * colorArray[1],
943 255 * colorArray[2], 255 * colorArray[3])) {
944 REPORT_FAILURE(r, "Uniform array didn't match expectations",
945 SkStringPrintf("\n"
946 "Expected: [ %g %g %g %g ]\n"
947 "Got : [ %08x ]\n",
948 colorArray[0], colorArray[1],
949 colorArray[2], colorArray[3],
950 actual));
951 break;
952 }
953 }
954 }
955