Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
ShaderImageFilterTest.cpp File Reference
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"
#include "include/effects/SkRuntimeEffect.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include "tools/EncodeUtils.h"
#include "tools/ToolUtils.h"
#include <vector>

Go to the source code of this file.

Functions

static void test_unscaled (skiatest::Reporter *reporter)
 
static void test_scaled (skiatest::Reporter *reporter)
 
 DEF_TEST (ShaderImageFilter, reporter)
 
static void test_runtime_shader (skiatest::Reporter *r, SkSurface *surface)
 
 DEF_TEST (SkRuntimeShaderImageFilter_CPU, r)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (SkRuntimeShaderImageFilter_GPU, r, ctxInfo, CtsEnforcement::kApiLevel_T)
 

Function Documentation

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS()

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( SkRuntimeShaderImageFilter_GPU  ,
,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 219 of file ShaderImageFilterTest.cpp.

222 {
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
static void test_runtime_shader(skiatest::Reporter *r, SkSurface *surface)
VkSurfaceKHR surface
Definition main.cc:49
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)
static SkImageInfo MakeN32Premul(int width, int height)

◆ DEF_TEST() [1/2]

DEF_TEST ( ShaderImageFilter  ,
reporter   
)

Definition at line 149 of file ShaderImageFilterTest.cpp.

149 {
152}
reporter
static void test_scaled(skiatest::Reporter *reporter)
static void test_unscaled(skiatest::Reporter *reporter)

◆ DEF_TEST() [2/2]

DEF_TEST ( SkRuntimeShaderImageFilter_CPU  ,
 
)

Definition at line 213 of file ShaderImageFilterTest.cpp.

213 {
214 const SkImageInfo info = SkImageInfo::MakeN32Premul(/*width=*/1, /*height=*/1);
217}
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)

◆ test_runtime_shader()

static void test_runtime_shader ( skiatest::Reporter r,
SkSurface surface 
)
static

Definition at line 154 of file ShaderImageFilterTest.cpp.

154 {
156 uniform shader child;
157 vec4 main(vec2 coord) {
158 return child.eval(coord) * 0.5;
159 }
160 )"))
161 .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}
SkColor4f color
uint32_t SkColor
Definition SkColor.h:37
uint8_t SkAlpha
Definition SkColor.h:26
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
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 REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
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 &)
const Paint & paint
sk_sp< SkRuntimeEffect > effect

◆ test_scaled()

static void test_scaled ( skiatest::Reporter reporter)
static

Definition at line 87 of file ShaderImageFilterTest.cpp.

87 {
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}
SkPoint pos
#define SK_Scalar1
Definition SkScalar.h:18
#define SK_ScalarHalf
Definition SkScalar.h:19
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define ERRORF(r,...)
Definition Test.h:293
static SkScalar center(float pos0, float pos1)
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
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)
float SkScalar
Definition extension.cpp:12
PODArray< SkColor > colors
Definition SkRecords.h:276
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 constexpr SkPoint Make(float x, float y)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
constexpr size_t kHeight
constexpr size_t kWidth

◆ test_unscaled()

static void test_unscaled ( skiatest::Reporter reporter)
static

Definition at line 39 of file ShaderImageFilterTest.cpp.

39 {
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}
uint32_t SkPMColor
Definition SkColor.h:205
uint32_t * getAddr32(int x, int y) const
Definition SkBitmap.h:1260
double y
double x