Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
ShaderTest.cpp File Reference
#include "include/core/SkAlphaType.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkBlender.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorType.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTileMode.h"
#include "include/effects/SkPerlinNoiseShader.h"
#include "include/effects/SkRuntimeEffect.h"
#include "include/private/base/SkAssert.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include <cmath>
#include <vector>

Go to the source code of this file.

Functions

static void check_isaimage (skiatest::Reporter *reporter, SkShader *shader, int expectedW, int expectedH, SkTileMode expectedX, SkTileMode expectedY, const SkMatrix &expectedM)
 
 DEF_TEST (Shader_isAImage, reporter)
 
 DEF_TEST (ComposeShaderSingle, reporter)
 
static void test_nested_blends (skiatest::Reporter *reporter, SkSurface *surface)
 
 DEF_TEST (ShaderTestNestedBlendsCpu, reporter)
 

Function Documentation

◆ check_isaimage()

static void check_isaimage ( skiatest::Reporter reporter,
SkShader shader,
int  expectedW,
int  expectedH,
SkTileMode  expectedX,
SkTileMode  expectedY,
const SkMatrix expectedM 
)
static

Definition at line 54 of file ShaderTest.cpp.

57 {
58 SkTileMode tileModes[2];
59 SkMatrix localM;
60
61 // wack these so we don't get a false positive
62 localM.setScale(9999, -9999);
63 tileModes[0] = tileModes[1] = (SkTileMode)99;
64
65 SkImage* image = shader->isAImage(&localM, tileModes);
67 REPORTER_ASSERT(reporter, image->width() == expectedW);
68 REPORTER_ASSERT(reporter, image->height() == expectedH);
69 REPORTER_ASSERT(reporter, localM == expectedM);
70 REPORTER_ASSERT(reporter, tileModes[0] == expectedX);
71 REPORTER_ASSERT(reporter, tileModes[1] == expectedY);
72}
reporter
SkTileMode
Definition SkTileMode.h:13
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
int width() const
Definition SkImage.h:285
int height() const
Definition SkImage.h:291
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition SkMatrix.cpp:296
SkImage * isAImage(SkMatrix *localMatrix, SkTileMode xy[2]) const
Definition SkShader.cpp:22
sk_sp< SkImage > image
Definition examples.cpp:29

◆ DEF_TEST() [1/3]

DEF_TEST ( ComposeShaderSingle  ,
reporter   
)

Definition at line 92 of file ShaderTest.cpp.

92 {
93 SkBitmap srcBitmap;
94 srcBitmap.allocN32Pixels(10, 10);
95 srcBitmap.eraseColor(SK_ColorRED);
96 SkCanvas canvas(srcBitmap);
97 SkPaint p;
98 p.setShader(SkShaders::Blend(SkBlendMode::kClear,
99 SkShaders::Empty(),
100 SkShaders::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f)));
101 SkRRect rr;
102 SkVector rd[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
103 rr.setRectRadii({0, 0, 0, 0}, rd);
104 canvas.drawRRect(rr, p);
105}
@ kClear
r = 0
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
void setRectRadii(const SkRect &rect, const SkVector radii[4])
Definition SkRRect.cpp:189
SK_API sk_sp< SkShader > MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, int numOctaves, SkScalar seed, const SkISize *tileSize=nullptr)

◆ DEF_TEST() [2/3]

DEF_TEST ( Shader_isAImage  ,
reporter   
)

Definition at line 74 of file ShaderTest.cpp.

74 {
75 const int W = 100;
76 const int H = 100;
77 SkBitmap bm;
78 bm.allocN32Pixels(W, H);
79 auto img = bm.asImage();
80 const SkMatrix localM = SkMatrix::Scale(2, 3);
83
84 auto shader0 = bm.makeShader(tmx, tmy, SkSamplingOptions(), localM);
85 auto shader1 = bm.asImage()->makeShader(tmx, tmy, SkSamplingOptions(), localM);
86
87 check_isaimage(reporter, shader0.get(), W, H, tmx, tmy, localM);
88 check_isaimage(reporter, shader1.get(), W, H, tmx, tmy, localM);
89}
static void check_isaimage(skiatest::Reporter *reporter, SkShader *shader, int expectedW, int expectedH, SkTileMode expectedX, SkTileMode expectedY, const SkMatrix &expectedM)
#define W
Definition aaa.cpp:17
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkBitmap.cpp:669
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
SkTileMode tmy
SkTileMode tmx
Definition SkMD5.cpp:130

◆ DEF_TEST() [3/3]

DEF_TEST ( ShaderTestNestedBlendsCpu  ,
reporter   
)

Definition at line 177 of file ShaderTest.cpp.

177 {
183}
static void test_nested_blends(skiatest::Reporter *reporter, SkSurface *surface)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
VkSurfaceKHR surface
Definition main.cc:49
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ test_nested_blends()

static void test_nested_blends ( skiatest::Reporter reporter,
SkSurface surface 
)
static

Definition at line 108 of file ShaderTest.cpp.

108 {
109 auto [redEffect, redError] = SkRuntimeEffect::MakeForShader(SkString(R"(
110 half4 main(float2 coord) {
111 return half4(1, 0, 0, 1);
112 }
113 )"));
114
115 auto [greenEffect, greenError] = SkRuntimeEffect::MakeForShader(SkString(R"(
116 half4 main(float2 coord) {
117 return half4(0, 1, 0, 1);
118 }
119 )"));
120
121 auto [blendEffect, blenderError] = SkRuntimeEffect::MakeForBlender(SkString(R"(
122 half4 main(half4 src, half4 dst) {
123 return (src + dst) * 0.5;
124 }
125 )"));
126
127 auto [nestedBlendEffect, nestedBlenderError] = SkRuntimeEffect::MakeForBlender(SkString(R"(
128 uniform blender child_blender;
129 half4 main(half4 src, half4 dst) {
130 return (child_blender.eval(src, dst) + dst) * 0.5;
131 }
132 )"));
133
134 sk_sp<SkShader> redShader = redEffect->makeShader(nullptr, {});
135 sk_sp<SkShader> greenShader = greenEffect->makeShader(nullptr, {});
136 sk_sp<SkBlender> blender = blendEffect->makeBlender(nullptr);
137 std::vector<SkRuntimeEffect::ChildPtr> children = {SkRuntimeEffect::ChildPtr(blender)};
138 sk_sp<SkBlender> nestedBlender = nestedBlendEffect->makeBlender(nullptr, children);
139
141 paint.setShader(SkShaders::Blend(nestedBlender, greenShader, redShader));
142 paint.setBlender(blender);
143
144 // Do the drawing.
145 SkCanvas* canvas = surface->getCanvas();
146 canvas->drawPaint(paint);
147
148 // Read pixels.
150 SkPixmap pixmap;
151 bitmap.allocPixels(surface->imageInfo());
152 SkAssertResult(bitmap.peekPixels(&pixmap));
153 if (!surface->readPixels(pixmap, 0, 0)) {
154 ERRORF(reporter, "readPixels failed");
155 return;
156 }
157
158 // Check the resulting blended color.
159 // First, in the paint's shader, red and green are averaged in the child blender to get
160 // (0.5, 0.5, 0, 1), which is then averaged with green in the parent blender to get
161 // (0.25, 0.75, 0, 1). Then, in the paint's blender this is averaged with a transparent
162 // background to get (0.125, 0.375, 0, 0.5) and then unpremuled to get (0.25, 0.75, 0, 0.5).
163 constexpr SkColor4f kExpected = {0.25f, 0.75f, 0.0f, 0.5f};
164 constexpr float kTolerance[4] = {0.01f, 0.01f, 0.0f, 0.01f};
165 SkColor4f color = pixmap.getColor4f(0, 0);
166 for (int i = 0; i < 4; ++i) {
167 if (std::abs(color[i] - kExpected[i]) > kTolerance[i]) {
169 "Wrong color, expected (%.2f %.2f %.2f %.2f), actual (%.2f, %.2f, %.2f, %.2f)",
170 kExpected.fR, kExpected.fG, kExpected.fB, kExpected.fA,
171 color.fR, color.fG, color.fB, color.fA);
172 break;
173 }
174 }
175}
static constexpr float kTolerance
SkColor4f color
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define ERRORF(r,...)
Definition Test.h:293
void drawPaint(const SkPaint &paint)
SkColor4f getColor4f(int x, int y) const
Definition SkPixmap.cpp:388
static Result MakeForBlender(SkString sksl, const Options &)
static Result MakeForShader(SkString sksl, const Options &)
const Paint & paint