• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkShader.h"
11 #include "include/core/SkSurface.h"
12 #include "include/effects/SkGradientShader.h"
13 #include "include/effects/SkImageFilters.h"
14 #include "include/effects/SkRuntimeEffect.h"
15 #include "src/effects/imagefilters/SkRuntimeImageFilter.h"
16 #include "tests/Test.h"
17 
test_unscaled(skiatest::Reporter * reporter)18 static void test_unscaled(skiatest::Reporter* reporter) {
19     static const int kWidth = 10;
20     static const int kHeight = 10;
21 
22     SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
23 
24     SkBitmap filterResult, paintResult;
25 
26     filterResult.allocN32Pixels(kWidth, kHeight);
27     SkCanvas canvasFilter(filterResult);
28     canvasFilter.clear(0x00000000);
29 
30     paintResult.allocN32Pixels(kWidth, kHeight);
31     SkCanvas canvasPaint(paintResult);
32     canvasPaint.clear(0x00000000);
33 
34     SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
35     SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
36     SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
37     SkScalar radius = SkIntToScalar(5);
38 
39     sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
40             center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
41 
42     // Test using the image filter
43     {
44         SkPaint paint;
45         paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
46         canvasFilter.drawRect(SkRect::Make(ir), paint);
47     }
48 
49     // Test using the paint directly
50     {
51         SkPaint paint;
52         paint.setShader(gradient);
53         canvasPaint.drawRect(SkRect::Make(ir), paint);
54     }
55 
56     // Assert that both paths yielded the same result
57     for (int y = 0; y < kHeight; ++y) {
58         const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
59         const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
60         for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
61             REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
62         }
63     }
64 }
65 
test_scaled(skiatest::Reporter * reporter)66 static void test_scaled(skiatest::Reporter* reporter) {
67     static const int kWidth = 10;
68     static const int kHeight = 10;
69 
70     SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
71 
72     SkBitmap filterResult, paintResult;
73 
74     filterResult.allocN32Pixels(kWidth, kHeight);
75     SkCanvas canvasFilter(filterResult);
76     canvasFilter.clear(0x00000000);
77 
78     paintResult.allocN32Pixels(kWidth, kHeight);
79     SkCanvas canvasPaint(paintResult);
80     canvasPaint.clear(0x00000000);
81 
82     SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
83     SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
84     SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
85     SkScalar radius = SkIntToScalar(5);
86 
87     sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
88         center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
89 
90     // Test using the image filter
91     {
92         SkPaint paint;
93         paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
94         canvasFilter.scale(SkIntToScalar(2), SkIntToScalar(2));
95         canvasFilter.drawRect(SkRect::Make(ir), paint);
96     }
97 
98     // Test using the paint directly
99     {
100         SkPaint paint;
101         paint.setShader(gradient);
102         canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
103         canvasPaint.drawRect(SkRect::Make(ir), paint);
104     }
105 
106     // Assert that both paths yielded the same result
107     for (int y = 0; y < kHeight; ++y) {
108         const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
109         const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
110         for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
111             REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
112         }
113     }
114 }
115 
DEF_TEST(PaintImageFilter,reporter)116 DEF_TEST(PaintImageFilter, reporter) {
117     test_unscaled(reporter);
118     test_scaled(reporter);
119 }
120 
test_runtime_shader(skiatest::Reporter * r,SkSurface * surface)121 static void test_runtime_shader(skiatest::Reporter* r, SkSurface* surface) {
122     sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(R"(
123         uniform shader child;
124         vec4 main(vec2 coord) {
125             return child.eval(coord) * 0.5;
126         }
127     )"))
128                                             .effect;
129     SkRuntimeShaderBuilder builder(effect);
130 
131     // create a red image filter to feed as input into the SkImageFilters::RuntimeShader
132     SkPaint redPaint;
133     redPaint.setColor(SK_ColorRED);
134     sk_sp<SkImageFilter> input = SkImageFilters::Paint(redPaint);
135 
136     // Create the different variations of SkImageFilters::RuntimeShader
137     // All 3 variations should produce the same pixel output
138     std::vector<sk_sp<SkImageFilter>> filters = {
139             SkMakeRuntimeImageFilter(effect, /*uniforms=*/nullptr, input),
140             SkImageFilters::RuntimeShader(builder, /*childShaderName=*/nullptr, input),
141             SkImageFilters::RuntimeShader(builder, /*childShaderName=*/"child", input)};
142 
143     for (auto&& filter : filters) {
144         auto canvas = surface->getCanvas();
145 
146         // clear to transparent
147         SkPaint paint;
148         paint.setColor(SK_ColorTRANSPARENT);
149         paint.setBlendMode(SkBlendMode::kSrc);
150         canvas->drawPaint(paint);
151 
152         SkPaint filterPaint;
153         // the green color will be ignored by the filter within the runtime shader
154         filterPaint.setColor(SK_ColorGREEN);
155         filterPaint.setImageFilter(filter);
156         canvas->saveLayer(nullptr, &filterPaint);
157         // the blue color will be ignored by the filter because the input to the image filter is not
158         // null
159         canvas->drawColor(SK_ColorBLUE);
160         canvas->restore();
161 
162         // This is expected to read back the half transparent red pixel produced by the image filter
163         SkBitmap bitmap;
164         REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
165         REPORTER_ASSERT(r,
166                         surface->readPixels(bitmap.info(),
167                                             bitmap.getPixels(),
168                                             bitmap.rowBytes(),
169                                             /*srcX=*/0,
170                                             /*srcY=*/0));
171         SkColor color = bitmap.getColor(/*x=*/0, /*y=*/0);
172 
173         // check alpha with a small tolerance
174         SkAlpha alpha = SkColorGetA(color);
175         REPORTER_ASSERT(r, alpha >= 127 && alpha <= 129, "Expected: %d Actual: %d", 128, alpha);
176 
177         // check each color channel
178         color = SkColorSetA(color, 255);
179         REPORTER_ASSERT(r, SK_ColorRED == color, "Expected: %08x Actual: %08x", SK_ColorRED, color);
180     }
181 }
182 
DEF_TEST(SkRuntimeShaderImageFilter_CPU,r)183 DEF_TEST(SkRuntimeShaderImageFilter_CPU, r) {
184     const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
185     sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
186     test_runtime_shader(r, surface.get());
187 }
188 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeShaderImageFilter_GPU,r,ctxInfo)189 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeShaderImageFilter_GPU, r, ctxInfo) {
190     const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
191     sk_sp<SkSurface> surface(
192             SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
193     test_runtime_shader(r, surface.get());
194 }
195