Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions
BlendTest.cpp File Reference
#include "include/core/SkAlphaType.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorType.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/SkSize.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrTypes.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/private/base/SkTemplates.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include "tools/gpu/BackendSurfaceFactory.h"
#include <initializer_list>
#include <vector>

Go to the source code of this file.

Classes

struct  Results
 

Functions

static bool acceptable (const Results &r)
 
template<typename Fn >
static Results test (Fn &&multiply)
 
 DEF_TEST (Blend_byte_multiply, r)
 
 DEF_GANESH_TEST_FOR_GL_CONTEXT (ES2BlendWithNoTexture, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (BlendRequiringDstReadWithLargeCoordinates, reporter, contextInfo, CtsEnforcement::kNextRelease)
 

Function Documentation

◆ acceptable()

static bool acceptable ( const Results r)
static

Definition at line 37 of file BlendTest.cpp.

37 {
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}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
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

◆ DEF_GANESH_TEST_FOR_GL_CONTEXT()

DEF_GANESH_TEST_FOR_GL_CONTEXT ( ES2BlendWithNoTexture  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 92 of file BlendTest.cpp.

95 {
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}
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
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
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static constexpr auto kColorType
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
sk_sp< SkSurface > MakeBackendRenderTargetSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, GrProtected isProtected, const SkSurfaceProps *props)
constexpr int32_t y() const
static constexpr SkIPoint Make(int32_t x, int32_t y)
constexpr int32_t x() const
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

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS()

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( BlendRequiringDstReadWithLargeCoordinates  ,
reporter  ,
contextInfo  ,
CtsEnforcement::kNextRelease   
)

Definition at line 183 of file BlendTest.cpp.

186 {
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}
@ kSoftLight
lighten or darken, depending on source
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
void drawIRect(const SkIRect &rect, const SkPaint &paint)
Definition SkCanvas.h:1358
void clear(SkColor color)
Definition SkCanvas.h:1199
const Paint & paint
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 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

◆ DEF_TEST()

DEF_TEST ( Blend_byte_multiply  ,
 
)

Definition at line 64 of file BlendTest.cpp.

64 {
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}
static bool acceptable(const Results &r)
Definition BlendTest.cpp:37
#define test(name)
Type::kYUV Type::kRGBA() int(0.7 *637)
double y
double x

◆ test()

template<typename Fn >
static Results test ( Fn &&  multiply)
static

Definition at line 48 of file BlendTest.cpp.

48 {
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}
static T SkTAbs(T value)
Definition SkTemplates.h:43
Int96 multiply(int64_t a, int32_t b)
Definition Int96.cpp:41