Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ShaderImageFilterTest.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
16#include "include/core/SkRect.h"
31#include "tests/Test.h"
32#include "tools/EncodeUtils.h"
33#include "tools/ToolUtils.h"
34
35#include <vector>
36
37struct GrContextOptions;
38
40 static const int kWidth = 10;
41 static const int kHeight = 10;
42
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
58 SkScalar radius = SkIntToScalar(5);
59
61 center, radius, colors, pos, std::size(colors), SkTileMode::kClamp);
62
63 // Test using the image filter
64 {
66 paint.setImageFilter(SkImageFilters::Shader(gradient, &ir));
67 canvasFilter.drawRect(SkRect::Make(ir), paint);
68 }
69
70 // Test using the paint directly
71 {
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
88 static const int kWidth = 10;
89 static const int kHeight = 10;
90
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
106 SkScalar radius = SkIntToScalar(5);
107
109 center, radius, colors, pos, std::size(colors), SkTileMode::kClamp);
110
111 // Test using the image filter
112 {
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 {
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
149DEF_TEST(ShaderImageFilter, reporter) {
152}
153
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
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
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
194 REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
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
213DEF_TEST(SkRuntimeShaderImageFilter_CPU, r) {
214 const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
217}
218
219DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkRuntimeShaderImageFilter_GPU,
220 r,
221 ctxInfo,
223 const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
225 SkSurfaces::RenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kNo, info));
227}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
reporter
SkPoint pos
SkColor4f color
static void test_scaled(skiatest::Reporter *reporter)
static void test_unscaled(skiatest::Reporter *reporter)
static void test_runtime_shader(skiatest::Reporter *r, SkSurface *surface)
uint32_t SkColor
Definition SkColor.h:37
uint8_t SkAlpha
Definition SkColor.h:26
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
uint32_t SkPMColor
Definition SkColor.h:205
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Definition SkColor.h:82
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
#define SkColorGetA(color)
Definition SkColor.h:61
#define SK_Scalar1
Definition SkScalar.h:18
#define SK_ScalarHalf
Definition SkScalar.h:19
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define ERRORF(r,...)
Definition Test.h:293
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
static SkScalar center(float pos0, float pos1)
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
uint32_t * getAddr32(int x, int y) const
Definition SkBitmap.h:1260
void drawRect(const SkRect &rect, const SkPaint &paint)
void clear(SkColor color)
Definition SkCanvas.h:1199
void scale(SkScalar sx, SkScalar sy)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > RuntimeShader(const SkRuntimeShaderBuilder &builder, std::string_view childShaderName, sk_sp< SkImageFilter > input)
static sk_sp< SkImageFilter > Shader(sk_sp< SkShader > shader, const CropRect &cropRect={})
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
static Result MakeForShader(SkString sksl, const Options &)
void append(const char text[])
Definition SkString.h:203
const char * c_str() const
Definition SkString.h:133
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
double y
double x
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)
bool BitmapToBase64DataURI(const SkBitmap &bitmap, SkString *dst)
bool equal_pixels(const SkPixmap &a, const SkPixmap &b)
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkPoint Make(float x, float y)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
sk_sp< SkRuntimeEffect > effect
constexpr size_t kHeight
constexpr size_t kWidth