Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
TestUtils.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 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
8#include "tests/TestUtils.h"
9
15#include "include/core/SkSize.h"
30#include "src/gpu/ganesh/SkGr.h"
33#include "tests/Test.h"
34
35#include <cmath>
36#include <cstdlib>
37#include <utility>
38
40 GrDirectContext* dContext,
42 uint32_t expectedPixelValues[],
43 const char* testName) {
44 int pixelCnt = srcContext->width() * srcContext->height();
45 SkImageInfo ii = SkImageInfo::Make(srcContext->dimensions(),
49 pm.alloc(ii);
51
52 bool read = srcContext->readPixels(dContext, pm, {0, 0});
53 if (!read) {
54 ERRORF(reporter, "%s: Error reading from texture.", testName);
55 }
56
57 for (int i = 0; i < pixelCnt; ++i) {
58 if (pm.addr32()[i] != expectedPixelValues[i]) {
59 ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
60 testName, i, expectedPixelValues[i], pm.addr32()[i]);
61 break;
62 }
63 }
64}
65
67 GrDirectContext* dContext,
69 bool expectedToWork,
70 const char* testName) {
71 SkImageInfo ii = SkImageInfo::Make(dstContext->dimensions(),
75 pm.alloc(ii);
76 for (int y = 0; y < dstContext->height(); ++y) {
77 for (int x = 0; x < dstContext->width(); ++x) {
79 }
80 }
81
82 bool write = dstContext->writePixels(dContext, pm, {0, 0});
83 if (!write) {
84 if (expectedToWork) {
85 ERRORF(reporter, "%s: Error writing to texture.", testName);
86 }
87 return;
88 }
89
90 if (write && !expectedToWork) {
91 ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
92 return;
93 }
94
95 TestReadPixels(reporter, dContext, dstContext, pm.writable_addr32(0, 0), testName);
96}
97
99 GrDirectContext* dContext,
101 GrSurfaceOrigin origin,
103 uint32_t expectedPixelValues[],
104 const char* testName) {
105 auto copy = GrSurfaceProxy::Copy(dContext,
106 std::move(proxy),
107 origin,
108 skgpu::Mipmapped::kNo,
111 /*label=*/"CopyFromSurface_Test");
112 SkASSERT(copy && copy->asTextureProxy());
113 auto swizzle = dContext->priv().caps()->getReadSwizzle(copy->backendFormat(), colorType);
114 GrSurfaceProxyView view(std::move(copy), origin, swizzle);
115 auto dstContext = dContext->priv().makeSC(std::move(view),
116 {colorType, kPremul_SkAlphaType, nullptr});
117 SkASSERT(dstContext);
118
119 TestReadPixels(reporter, dContext, dstContext.get(), expectedPixelValues, testName);
120}
121
122static bool compare_colors(int x, int y,
123 const float rgbaA[],
124 const float rgbaB[],
125 const float tolRGBA[4],
126 std::function<ComparePixmapsErrorReporter>& error) {
127 float diffs[4];
128 bool bad = false;
129 for (int i = 0; i < 4; ++i) {
130 diffs[i] = rgbaB[i] - rgbaA[i];
131 if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
132 bad = true;
133 }
134 }
135 if (bad) {
136 error(x, y, diffs);
137 return false;
138 }
139 return true;
140}
141
143 const GrCPixmap& b,
144 const float tolRGBA[4],
145 std::function<ComparePixmapsErrorReporter>& error) {
146 if (a.dimensions() != b.dimensions()) {
147 static constexpr float kEmptyDiffs[4] = {};
148 error(-1, -1, kEmptyDiffs);
149 return false;
150 }
151
152 SkAlphaType floatAlphaType = a.alphaType();
153 // If one is premul and the other is unpremul we do the comparison in premul space.
154 if ((a.alphaType() == kPremul_SkAlphaType || b.alphaType() == kPremul_SkAlphaType) &&
155 (a.alphaType() == kUnpremul_SkAlphaType || b.alphaType() == kUnpremul_SkAlphaType)) {
156 floatAlphaType = kPremul_SkAlphaType;
157 }
158 sk_sp<SkColorSpace> floatCS;
159 if (SkColorSpace::Equals(a.colorSpace(), b.colorSpace())) {
160 floatCS = a.refColorSpace();
161 } else {
163 }
165 floatAlphaType,
166 std::move(floatCS),
167 a.dimensions());
168
169 GrPixmap floatA = GrPixmap::Allocate(floatInfo);
170 GrPixmap floatB = GrPixmap::Allocate(floatInfo);
173
174 SkASSERT(floatA.rowBytes() == floatB.rowBytes());
175 auto at = [rb = floatA.rowBytes()](const void* base, int x, int y) {
176 return SkTAddOffset<const float>(base, y*rb + x*sizeof(float)*4);
177 };
178
179 for (int y = 0; y < floatA.height(); ++y) {
180 for (int x = 0; x < floatA.width(); ++x) {
181 const float* rgbaA = at(floatA.addr(), x, y);
182 const float* rgbaB = at(floatB.addr(), x, y);
183 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
184 return false;
185 }
186 }
187 }
188 return true;
189}
190
192 const SkPixmap& pixmap,
193 const float tolRGBA[4],
194 std::function<ComparePixmapsErrorReporter>& error) {
196
197 // First convert 'col' to be compatible with 'pixmap'
198 GrPixmap colorPixmap;
199 {
203 std::move(srcCS),
204 {1, 1});
205 GrCPixmap srcPixmap(srcInfo, col.vec(), floatBpp);
206 GrImageInfo dstInfo =
207 srcInfo.makeAlphaType(pixmap.alphaType()).makeColorSpace(pixmap.refColorSpace());
208 colorPixmap = GrPixmap::Allocate(dstInfo);
209 SkAssertResult(GrConvertPixels(colorPixmap, srcPixmap));
210 }
211
212 size_t floatRowBytes = floatBpp * pixmap.width();
213 std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]);
214 // Then convert 'pixmap' to RGBA_F32
216 SkAssertResult(GrConvertPixels(f32Pixmap, pixmap));
217
218 for (int y = 0; y < f32Pixmap.height(); ++y) {
219 for (int x = 0; x < f32Pixmap.width(); ++x) {
220 auto rgbaA = SkTAddOffset<const float>(f32Pixmap.addr(),
221 f32Pixmap.rowBytes()*y + floatBpp*x);
222 auto rgbaB = static_cast<const float*>(colorPixmap.addr());
223 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
224 return false;
225 }
226 }
227 }
228 return true;
229}
230
232 GrSurfaceProxy* proxy,
233 int32_t expectedProxyRefs,
234 int32_t expectedBackingRefs) {
235 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
236
237 REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) &&
238 !proxy->refCntGreaterThan(expectedProxyRefs));
239 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
240}
241
242std::unique_ptr<skgpu::ganesh::SurfaceContext> CreateSurfaceContext(GrRecordingContext* rContext,
243 const GrImageInfo& info,
244 SkBackingFit fit,
245 GrSurfaceOrigin origin,
246 GrRenderable renderable,
247 int sampleCount,
248 skgpu::Mipmapped mipmapped,
249 GrProtected isProtected,
250 skgpu::Budgeted budgeted) {
251 GrBackendFormat format = rContext->priv().caps()->getDefaultBackendFormat(info.colorType(),
252 renderable);
253 return rContext->priv().makeSC(info,
254 format,
255 /*label=*/{},
256 fit,
257 origin,
258 renderable,
259 sampleCount,
260 mipmapped,
261 isProtected,
262 budgeted);
263}
264
265static SkGlyphID hash_to_glyph(uint32_t value) {
266 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
267}
268
269namespace {
270class UnicharGen {
271 SkUnichar fU;
272 const int fStep;
273public:
274 UnicharGen(int step) : fU(0), fStep(step) {}
275
276 SkUnichar next() {
277 fU += fStep;
278 return fU;
279 }
280};
281} // namespace
282
283DEF_TEST(chartoglyph_cache, reporter) {
284 SkCharToGlyphCache cache;
285 const int step = 3;
286
287 UnicharGen gen(step);
288 for (int i = 0; i < 500; ++i) {
289 SkUnichar c = gen.next();
290 SkGlyphID glyph = hash_to_glyph(c);
291
292 int index = cache.findGlyphIndex(c);
293 if (index >= 0) {
294 index = cache.findGlyphIndex(c);
295 }
296 REPORTER_ASSERT(reporter, index < 0);
297 cache.insertCharAndGlyph(~index, c, glyph);
298
299 UnicharGen gen2(step);
300 for (int j = 0; j <= i; ++j) {
301 c = gen2.next();
302 glyph = hash_to_glyph(c);
303 index = cache.findGlyphIndex(c);
304 if ((unsigned)index != glyph) {
305 index = cache.findGlyphIndex(c);
306 }
307 REPORTER_ASSERT(reporter, (unsigned)index == glyph);
308 }
309 }
310}
static int step(int x, SkScalar min, SkScalar max)
Definition BlurTest.cpp:215
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
reporter
bool GrConvertPixels(const GrPixmap &dst, const GrCPixmap &src, bool flipY)
static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct)
GrColorType
GrSurfaceOrigin
Definition GrTypes.h:147
static float next(float f)
kUnpremul_SkAlphaType
SkAlphaType
Definition SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define SkASSERT(cond)
Definition SkAssert.h:116
SkBackingFit
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition SkColorType.h:40
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
static bool read(SkStream *stream, void *buffer, size_t amount)
static GrColor SkColorToPremulGrColor(SkColor c)
Definition SkGr.h:51
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
constexpr uint16_t SkToU16(S x)
Definition SkTo.h:24
int32_t SkUnichar
Definition SkTypes.h:175
uint16_t SkGlyphID
Definition SkTypes.h:179
std::unique_ptr< skgpu::ganesh::SurfaceContext > CreateSurfaceContext(GrRecordingContext *rContext, const GrImageInfo &info, SkBackingFit fit, GrSurfaceOrigin origin, GrRenderable renderable, int sampleCount, skgpu::Mipmapped mipmapped, GrProtected isProtected, skgpu::Budgeted budgeted)
static bool compare_colors(int x, int y, const float rgbaA[], const float rgbaB[], const float tolRGBA[4], std::function< ComparePixmapsErrorReporter > &error)
void CheckSingleThreadedProxyRefs(skiatest::Reporter *reporter, GrSurfaceProxy *proxy, int32_t expectedProxyRefs, int32_t expectedBackingRefs)
void TestReadPixels(skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *srcContext, uint32_t expectedPixelValues[], const char *testName)
Definition TestUtils.cpp:39
static SkGlyphID hash_to_glyph(uint32_t value)
void TestWritePixels(skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *dstContext, bool expectedToWork, const char *testName)
Definition TestUtils.cpp:66
void TestCopyFromSurface(skiatest::Reporter *reporter, GrDirectContext *dContext, sk_sp< GrSurfaceProxy > proxy, GrSurfaceOrigin origin, GrColorType colorType, uint32_t expectedPixelValues[], const char *testName)
Definition TestUtils.cpp:98
bool ComparePixels(const GrCPixmap &a, const GrCPixmap &b, const float tolRGBA[4], std::function< ComparePixmapsErrorReporter > &error)
bool CheckSolidPixels(const SkColor4f &col, const SkPixmap &pixmap, const float tolRGBA[4], std::function< ComparePixmapsErrorReporter > &error)
#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
const GrCaps * caps() const
GrBackendFormat getDefaultBackendFormat(GrColorType, GrRenderable) const
Definition GrCaps.cpp:400
skgpu::Swizzle getReadSwizzle(const GrBackendFormat &format, GrColorType colorType) const
Definition GrCaps.cpp:443
GrDirectContextPriv priv()
GrImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
GrImageInfo makeAlphaType(SkAlphaType at) const
int width() const
Definition GrPixmap.h:27
size_t rowBytes() const
Definition GrPixmap.h:21
int height() const
Definition GrPixmap.h:28
T * addr() const
Definition GrPixmap.h:20
static GrPixmap Allocate(const GrImageInfo &info)
Definition GrPixmap.h:101
std::unique_ptr< skgpu::ganesh::SurfaceContext > makeSC(GrSurfaceProxyView readView, const GrColorInfo &)
GrRecordingContextPriv priv()
static sk_sp< GrSurfaceProxy > Copy(GrRecordingContext *, sk_sp< GrSurfaceProxy > src, GrSurfaceOrigin, skgpu::Mipmapped, SkIRect srcRect, SkBackingFit, skgpu::Budgeted, std::string_view label, RectsMustMatch=RectsMustMatch::kNo, sk_sp< GrRenderTask > *outTask=nullptr)
void alloc(const SkImageInfo &)
static bool Equals(const SkColorSpace *, const SkColorSpace *)
static sk_sp< SkColorSpace > MakeSRGBLinear()
bool refCntGreaterThan(int32_t threadIsolatedTestCnt) const
Definition SkRefCnt.h:191
const uint32_t * addr32() const
Definition SkPixmap.h:352
bool erase(SkColor color, const SkIRect &subset) const
Definition SkPixmap.cpp:742
int width() const
Definition SkPixmap.h:160
const SkImageInfo & info() const
Definition SkPixmap.h:135
uint32_t * writable_addr32(int x, int y) const
Definition SkPixmap.h:537
sk_sp< SkColorSpace > refColorSpace() const
Definition SkPixmap.cpp:63
int height() const
Definition SkPixmap.h:166
SkAlphaType alphaType() const
Definition SkPixmap.h:175
bool readPixels(GrDirectContext *dContext, GrPixmap dst, SkIPoint srcPt)
bool writePixels(GrDirectContext *dContext, GrCPixmap src, SkIPoint dstPt)
static bool b
struct MyStruct a[10]
const uint8_t uint32_t uint32_t GError ** error
uint8_t value
uint32_t uint32_t * format
double y
double x
Definition copy.py:1
Definition gen.py:1
Budgeted
Definition GpuTypes.h:35
Renderable
Definition GpuTypes.h:69
Mipmapped
Definition GpuTypes.h:53
Protected
Definition GpuTypes.h:61
void write(SkWStream *wStream, const T &text)
Definition skqp.cpp:188
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkImageInfo makeColorType(SkColorType newColorType) const