• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkCanvas.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkSurface.h"
11 #include "include/effects/SkRuntimeEffect.h"
12 #include "include/gpu/GrContext.h"
13 #include "tests/Test.h"
14 
15 #include <algorithm>
16 
DEF_TEST(SkRuntimeEffectInvalidInputs,r)17 DEF_TEST(SkRuntimeEffectInvalidInputs, r) {
18     auto test = [r](const char* hdr, const char* expected) {
19         SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) {}", hdr);
20         auto[effect, errorText] = SkRuntimeEffect::Make(src);
21         REPORTER_ASSERT(r, !effect);
22         REPORTER_ASSERT(r, errorText.contains(expected),
23                         "Expected error message to contain \"%s\". Actual message: \"%s\"",
24                         expected, errorText.c_str());
25     };
26 
27     // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked).
28     // Ensure that these fail, and the error messages contain the relevant keyword.
29     test("layout(key) in bool Input;", "key");
30     test("in uniform float Input;", "in uniform");
31     test("layout(ctype=SkRect) float4 Input;", "ctype");
32     test("in bool Flag; layout(when=Flag) uniform float Input;", "when");
33     test("layout(tracked) uniform float Input;", "tracked");
34 
35     // Runtime SkSL supports a limited set of uniform types. No samplers, for example:
36     test("uniform sampler2D s;", "sampler2D");
37 
38     // 'in' variables can't be arrays
39     test("in int Input[2];", "array");
40 
41     // Type specific restrictions:
42 
43     // 'bool', 'int' can't be 'uniform'
44     test("uniform bool Input;", "'uniform'");
45     test("uniform int Input;", "'uniform'");
46 
47     // vector and matrix types can't be 'in'
48     test("in float2 Input;", "'in'");
49     test("in half3x3 Input;", "'in'");
50 }
51 
52 // Our packing rules and unit test code here relies on this:
53 static_assert(sizeof(bool) == 1);
54 
55 class TestEffect {
56 public:
TestEffect(skiatest::Reporter * r,const char * hdr,const char * body)57     TestEffect(skiatest::Reporter* r, const char* hdr, const char* body) {
58         SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
59                                       hdr, body);
60         auto[effect, errorText] = SkRuntimeEffect::Make(src);
61         if (!effect) {
62             REPORT_FAILURE(r, "effect",
63                            SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
64             return;
65         }
66 
67         fEffect = std::move(effect);
68         fInputs = SkData::MakeUninitialized(fEffect->inputSize());
69     }
70 
71     struct InputVar {
operator =TestEffect::InputVar72         template <typename T> InputVar& operator=(const T& val) {
73             SkASSERT(sizeof(T) == fVar.sizeInBytes());
74             memcpy(SkTAddOffset<void>(fOwner->fInputs->writable_data(), fVar.fOffset), &val,
75                    sizeof(T));
76             return *this;
77         }
78         TestEffect* fOwner;
79         const SkRuntimeEffect::Variable& fVar;
80     };
81 
operator [](const char * name)82     InputVar operator[](const char* name) {
83         auto input = std::find_if(fEffect->inputs().begin(), fEffect->inputs().end(),
84                                   [name](const auto& v) { return v.fName.equals(name); });
85         SkASSERT(input != fEffect->inputs().end());
86         return {this, *input};
87     }
88 
test(skiatest::Reporter * r,sk_sp<SkSurface> surface,uint32_t TL,uint32_t TR,uint32_t BL,uint32_t BR)89     void test(skiatest::Reporter* r, sk_sp<SkSurface> surface,
90               uint32_t TL, uint32_t TR, uint32_t BL, uint32_t BR) {
91         if (!fEffect) { return; }
92 
93         auto shader = fEffect->makeShader(fInputs, nullptr, 0, nullptr, false);
94         if (!shader) {
95             REPORT_FAILURE(r, "shader", SkString("Effect didn't produce a shader"));
96             return;
97         }
98 
99         SkPaint paint;
100         paint.setShader(std::move(shader));
101         paint.setBlendMode(SkBlendMode::kSrc);
102         surface->getCanvas()->drawPaint(paint);
103 
104         uint32_t actual[4];
105         SkImageInfo info = surface->imageInfo();
106         if (!surface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
107             REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
108             return;
109         }
110 
111         uint32_t expected[4] = {TL, TR, BL, BR};
112         if (memcmp(actual, expected, sizeof(actual)) != 0) {
113             REPORT_FAILURE(r, "Runtime effect didn't match expectations",
114                            SkStringPrintf("\n"
115                                           "Expected: [ %08x %08x %08x %08x ]\n"
116                                           "Got     : [ %08x %08x %08x %08x ]\n"
117                                           "SkSL:\n%s\n",
118                                           TL, TR, BL, BR, actual[0], actual[1], actual[2],
119                                           actual[3], fEffect->source().c_str()));
120         }
121     }
122 
test(skiatest::Reporter * r,sk_sp<SkSurface> surface,uint32_t expected)123     void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, uint32_t expected) {
124         this->test(r, surface, expected, expected, expected, expected);
125     }
126 
127 private:
128     sk_sp<SkRuntimeEffect> fEffect;
129     sk_sp<SkData> fInputs;
130 };
131 
test_RuntimeEffect_Shaders(skiatest::Reporter * r,GrContext * context)132 static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) {
133     SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
134     sk_sp<SkSurface> surface;
135     if (context) {
136         surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
137     } else {
138         surface = SkSurface::MakeRaster(info);
139     }
140     REPORTER_ASSERT(r, surface);
141 
142     TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);");
143     xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
144 
145     using float4 = std::array<float, 4>;
146 
147     // NOTE: For now, we always emit valid premul colors, until CPU and GPU agree on clamping
148     TestEffect uniformColor(r, "uniform float4 gColor;", "color = half4(gColor);");
149 
150     uniformColor["gColor"] = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
151     uniformColor.test(r, surface, 0xFFBF4000);
152 
153     uniformColor["gColor"] = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
154     uniformColor.test(r, surface, 0xFF0040BF);
155 
156     TestEffect pickColor(r, "in int flag; uniform half4 gColors[2];", "color = gColors[flag];");
157     pickColor["gColors"] =
158             std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f}, float4{0.0f, 1.0f, 0.0f, 1.0f}};
159     pickColor["flag"] = 0;
160     pickColor.test(r, surface, 0x7F00007F);  // Tests that we clamp to valid premul
161     pickColor["flag"] = 1;
162     pickColor.test(r, surface, 0xFF00FF00);
163 }
164 
DEF_TEST(SkRuntimeEffectSimple,r)165 DEF_TEST(SkRuntimeEffectSimple, r) {
166     test_RuntimeEffect_Shaders(r, nullptr);
167 }
168 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU,r,ctxInfo)169 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
170     test_RuntimeEffect_Shaders(r, ctxInfo.grContext());
171 }
172