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/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkSurface.h"
22 #include "include/core/SkTileMode.h"
23 #include "include/core/SkTypes.h"
24 #include "include/effects/SkGradientShader.h"
25 #include "include/effects/SkImageFilters.h"
26 #include "include/effects/SkRuntimeEffect.h"
27 #include "include/gpu/GpuTypes.h"
28 #include "include/gpu/ganesh/GrDirectContext.h"
29 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
30 #include "tests/CtsEnforcement.h"
31 #include "tests/Test.h"
32 #include "tools/EncodeUtils.h"
33 #include "tools/ToolUtils.h"
34
35 #include <vector>
36
37 struct GrContextOptions;
38
test_unscaled(skiatest::Reporter * reporter)39 static void test_unscaled(skiatest::Reporter* reporter) {
40 static const int kWidth = 10;
41 static const int kHeight = 10;
42
43 SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
44
45 SkBitmap filterResult, paintResult;
46
47 filterResult.allocN32Pixels(kWidth, kHeight);
48 SkCanvas canvasFilter(filterResult);
49 canvasFilter.clear(0x00000000);
50
51 paintResult.allocN32Pixels(kWidth, kHeight);
52 SkCanvas canvasPaint(paintResult);
53 canvasPaint.clear(0x00000000);
54
55 SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
56 SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
57 SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
58 SkScalar radius = SkIntToScalar(5);
59
60 sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
61 center, radius, colors, pos, std::size(colors), SkTileMode::kClamp);
62
63 // Test using the image filter
64 {
65 SkPaint paint;
66 paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
67 canvasFilter.drawRect(SkRect::Make(ir), paint);
68 }
69
70 // Test using the paint directly
71 {
72 SkPaint paint;
73 paint.setShader(gradient);
74 canvasPaint.drawRect(SkRect::Make(ir), paint);
75 }
76
77 // Assert that both paths yielded the same result
78 for (int y = 0; y < kHeight; ++y) {
79 const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
80 const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
81 for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
82 REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
83 }
84 }
85 }
86
test_scaled(skiatest::Reporter * reporter)87 static void test_scaled(skiatest::Reporter* reporter) {
88 static const int kWidth = 10;
89 static const int kHeight = 10;
90
91 SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
92
93 SkBitmap filterResult, paintResult;
94
95 filterResult.allocN32Pixels(kWidth, kHeight);
96 SkCanvas canvasFilter(filterResult);
97 canvasFilter.clear(0x00000000);
98
99 paintResult.allocN32Pixels(kWidth, kHeight);
100 SkCanvas canvasPaint(paintResult);
101 canvasPaint.clear(0x00000000);
102
103 SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
104 SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
105 SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
106 SkScalar radius = SkIntToScalar(5);
107
108 sk_sp<SkShader> gradient = SkGradientShader::MakeRadial(
109 center, radius, colors, pos, std::size(colors), SkTileMode::kClamp);
110
111 // Test using the image filter
112 {
113 SkPaint paint;
114 paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
115 canvasFilter.scale(SkIntToScalar(2), SkIntToScalar(2));
116 canvasFilter.drawRect(SkRect::Make(ir), paint);
117 }
118
119 // Test using the paint directly
120 {
121 SkPaint paint;
122 paint.setShader(gradient);
123 canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
124 canvasPaint.drawRect(SkRect::Make(ir), paint);
125 }
126
127 // Assert that both paths yielded the same result
128 if (!ToolUtils::equal_pixels(filterResult, paintResult)) {
129 SkString encoded;
130 SkString errString("Image filter doesn't match paint reference");
131 errString.append("\nExpected: ");
132 if (ToolUtils::BitmapToBase64DataURI(paintResult, &encoded)) {
133 errString.append(encoded);
134 } else {
135 errString.append("failed to encode");
136 }
137
138 errString.append("\nActual: ");
139 if (ToolUtils::BitmapToBase64DataURI(filterResult, &encoded)) {
140 errString.append(encoded);
141 } else {
142 errString.append("failed to encode");
143 }
144
145 ERRORF(reporter, "%s\n", errString.c_str());
146 }
147 }
148
DEF_TEST(ShaderImageFilter,reporter)149 DEF_TEST(ShaderImageFilter, reporter) {
150 test_unscaled(reporter);
151 test_scaled(reporter);
152 }
153
test_runtime_shader(skiatest::Reporter * r,SkSurface * surface)154 static void test_runtime_shader(skiatest::Reporter* r, SkSurface* surface) {
155 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(R"(
156 uniform shader child;
157 vec4 main(vec2 coord) {
158 return child.eval(coord) * 0.5;
159 }
160 )"))
161 .effect;
162 SkRuntimeShaderBuilder builder(effect);
163
164 // create a red image filter to feed as input into the SkImageFilters::RuntimeShader
165 sk_sp<SkImageFilter> input = SkImageFilters::Shader(SkShaders::Color(SK_ColorRED));
166
167 // Create the different variations of SkImageFilters::RuntimeShader
168 // All variations should produce the same pixel output
169 std::vector<sk_sp<SkImageFilter>> filters = {
170 SkImageFilters::RuntimeShader(builder, /*childShaderName=*/"", input),
171 SkImageFilters::RuntimeShader(builder, /*childShaderName=*/"child", input)};
172
173 for (auto&& filter : filters) {
174 auto canvas = surface->getCanvas();
175
176 // clear to transparent
177 SkPaint paint;
178 paint.setColor(SK_ColorTRANSPARENT);
179 paint.setBlendMode(SkBlendMode::kSrc);
180 canvas->drawPaint(paint);
181
182 SkPaint filterPaint;
183 // the green color will be ignored by the filter within the runtime shader
184 filterPaint.setColor(SK_ColorGREEN);
185 filterPaint.setImageFilter(filter);
186 canvas->saveLayer(nullptr, &filterPaint);
187 // the blue color will be ignored by the filter because the input to the image filter is not
188 // null
189 canvas->drawColor(SK_ColorBLUE);
190 canvas->restore();
191
192 // This is expected to read back the half transparent red pixel produced by the image filter
193 SkBitmap bitmap;
194 REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
195 REPORTER_ASSERT(r,
196 surface->readPixels(bitmap.info(),
197 bitmap.getPixels(),
198 bitmap.rowBytes(),
199 /*srcX=*/0,
200 /*srcY=*/0));
201 SkColor color = bitmap.getColor(/*x=*/0, /*y=*/0);
202
203 // check alpha with a small tolerance
204 SkAlpha alpha = SkColorGetA(color);
205 REPORTER_ASSERT(r, alpha >= 127 && alpha <= 129, "Expected: %d Actual: %d", 128, alpha);
206
207 // check each color channel
208 color = SkColorSetA(color, 255);
209 REPORTER_ASSERT(r, SK_ColorRED == color, "Expected: %08x Actual: %08x", SK_ColorRED, color);
210 }
211 }
212
DEF_TEST(SkRuntimeShaderImageFilter_CPU,r)213 DEF_TEST(SkRuntimeShaderImageFilter_CPU, r) {
214 const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
215 sk_sp<SkSurface> surface(SkSurfaces::Raster(info));
216 test_runtime_shader(r, surface.get());
217 }
218
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkRuntimeShaderImageFilter_GPU,r,ctxInfo,CtsEnforcement::kApiLevel_T)219 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkRuntimeShaderImageFilter_GPU,
220 r,
221 ctxInfo,
222 CtsEnforcement::kApiLevel_T) {
223 const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
224 sk_sp<SkSurface> surface(
225 SkSurfaces::RenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kNo, info));
226 test_runtime_shader(r, surface.get());
227 }
228