Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
BlendTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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
17#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
24#include "include/gpu/GrTypes.h"
28#include "tests/Test.h"
30
31#include <initializer_list>
32#include <vector>
33
34struct GrContextOptions;
36
37static bool acceptable(const Results& r) {
38#if 0
39 SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
41#endif
42 return r.diffs_by_1 == r.diffs // never off by more than 1
43 && r.diffs_0x00 == 0 // transparent must stay transparent
44 && r.diffs_0xff == 0; // opaque must stay opaque
45}
46
47template <typename Fn>
48static Results test(Fn&& multiply) {
49 Results r = { 0,0,0,0 };
50 for (int x = 0; x < 256; x++) {
51 for (int y = 0; y < 256; y++) {
52 int p = multiply(x, y),
53 ideal = (x*y+127)/255;
54 if (p != ideal) {
55 r.diffs++;
56 if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
57 if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
58 if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
59 }
60 }}
61 return r;
62}
63
64DEF_TEST(Blend_byte_multiply, r) {
65 // These are all temptingly close but fundamentally broken.
66 int (*broken[])(int, int) = {
67 [](int x, int y) { return (x*y)>>8; },
68 [](int x, int y) { return (x*y+128)>>8; },
69 [](int x, int y) { y += y>>7; return (x*y)>>8; },
70 };
71 for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
72
73 // These are fine to use, but not perfect.
74 int (*fine[])(int, int) = {
75 [](int x, int y) { return (x*y+x)>>8; },
76 [](int x, int y) { return (x*y+y)>>8; },
77 [](int x, int y) { return (x*y+255)>>8; },
78 [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
79 };
80 for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
81
82 // These are pefect.
83 int (*perfect[])(int, int) = {
84 [](int x, int y) { return (x*y+127)/255; }, // Duh.
85 [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
86 [](int x, int y) { return ((x*y+128)*257)>>16; },
87 };
88 for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
89}
90
91// Tests blending to a surface with no texture available.
92DEF_GANESH_TEST_FOR_GL_CONTEXT(ES2BlendWithNoTexture,
94 ctxInfo,
96 auto context = ctxInfo.directContext();
97 static constexpr SkISize kDimensions{10, 10};
99
100 // Build our test cases:
101 struct RectAndSamplePoint {
102 SkRect rect;
103 SkIPoint outPoint;
104 SkIPoint inPoint;
105 } allRectsAndPoints[3] = {
106 {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
107 {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
108 {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
109 };
110
111 struct TestCase {
112 RectAndSamplePoint fRectAndPoints;
113 SkRect fClip;
114 int fSampleCnt;
115 GrSurfaceOrigin fOrigin;
116 };
117 std::vector<TestCase> testCases;
118
120 for (int sampleCnt : {1, 4}) {
121 for (auto rectAndPoints : allRectsAndPoints) {
122 for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
123 testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
124 }
125 }
126 }
127 }
128
129 // Run each test case:
130 for (auto testCase : testCases) {
131 int sampleCnt = testCase.fSampleCnt;
132 SkRect paintRect = testCase.fRectAndPoints.rect;
133 SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
134 SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
135 GrSurfaceOrigin origin = testCase.fOrigin;
136
137 // BGRA forces a framebuffer blit on ES2.
139 kDimensions,
140 origin,
141 sampleCnt,
142 kColorType);
143
144 if (!surface && sampleCnt > 1) {
145 // Some platforms don't support MSAA.
146 continue;
147 }
149
150 // Fill our canvas with 0xFFFF80
151 SkCanvas* canvas = surface->getCanvas();
152 canvas->clipRect(testCase.fClip, false);
153 SkPaint black_paint;
154 black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
155 canvas->drawRect(SkRect::Make(kDimensions), black_paint);
156
157 // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
158 // a copy of the destination.
159 SkPaint white_paint;
160 white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
162 canvas->drawRect(paintRect, white_paint);
163
164 // Read the result into a bitmap.
166 REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(kDimensions, kColorType,
169 reporter,
170 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
171
172 // Check the in/out pixels.
173 REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
174 SkColorSetRGB(0xFF, 0xFF, 0x80));
175 REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
176 SkColorSetRGB(0x80, 0xFF, 0x80));
177 }
178}
179
180// Test that dst reads when large coordinates read the correct pixels.
181// When we use half-width floats for dst read coordinates, we can end up reading the wrong pixel
182// from dst and consequently writing the wrong blended color (skbug.com/14347).
183DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(BlendRequiringDstReadWithLargeCoordinates,
184 reporter,
185 contextInfo,
188
189 GrDirectContext* context = contextInfo.directContext();
190 SkImageInfo imageInfo =
193 SkCanvas* canvas = surface->getCanvas();
194 canvas->clear(SK_ColorBLACK);
195
196 // Draw a red rectangle at x=1100.
198 paint.setColor(SK_ColorRED);
199 canvas->drawIRect(SkIRect::MakeXYWH(1100, 0, 5, 1), paint);
200
201 // Draw a rectangle with a blend mode that requires a dst read, over the drawn red rectangle.
202 SkPaint blendPaint;
204 canvas->drawIRect(SkIRect::MakeXYWH(1090, 0, 20, 1), blendPaint);
205
206 // Check the pixels at the edge of the left intersection between the two rectangles.
209 bitmap.tryAllocPixels(SkImageInfo::Make(
212 reporter,
213 surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 1099, 0));
214
215 REPORTER_ASSERT(reporter, bitmap.getColor(0, 0) == SK_ColorBLACK);
216 REPORTER_ASSERT(reporter, bitmap.getColor(1, 0) == SK_ColorRED);
217}
static bool acceptable(const Results &r)
Definition BlendTest.cpp:37
#define test(name)
reporter
GrSurfaceOrigin
Definition GrTypes.h:147
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kMultiply
r = s*(1-da) + d*(1-sa) + s*d
@ kSoftLight
lighten or darken, depending on source
SkColorType
Definition SkColorType.h:19
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
#define SkColorSetRGB(r, g, b)
Definition SkColor.h:57
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
static T SkTAbs(T value)
Definition SkTemplates.h:43
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define DEF_GANESH_TEST_FOR_GL_CONTEXT(name, reporter, context_info, ctsEnforcement)
Definition Test.h:442
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
static constexpr auto kColorType
Type::kYUV Type::kRGBA() int(0.7 *637)
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void drawIRect(const SkIRect &rect, const SkPaint &paint)
Definition SkCanvas.h:1358
void clear(SkColor color)
Definition SkCanvas.h:1199
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
double y
double x
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)
sk_sp< SkSurface > MakeBackendRenderTargetSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, GrProtected isProtected, const SkSurfaceProps *props)
int diffs_by_1
Definition BlendTest.cpp:35
int diffs
Definition BlendTest.cpp:35
int diffs_0x00
Definition BlendTest.cpp:35
int diffs_0xff
Definition BlendTest.cpp:35
constexpr int32_t y() const
static constexpr SkIPoint Make(int32_t x, int32_t y)
constexpr int32_t x() const
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
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)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659