Flutter Engine
The Flutter Engine
Functions
TestUtils.cpp File Reference
#include "tests/TestUtils.h"
#include "include/core/SkAlphaType.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkColorType.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkSize.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrRecordingContext.h"
#include "include/private/base/SkTemplates.h"
#include "include/private/base/SkTo.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
#include "src/core/SkAutoPixmapStorage.h"
#include "src/gpu/ganesh/GrCaps.h"
#include "src/gpu/ganesh/GrDataUtils.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrImageInfo.h"
#include "src/gpu/ganesh/GrRecordingContextPriv.h"
#include "src/gpu/ganesh/GrSurfaceProxy.h"
#include "src/gpu/ganesh/GrSurfaceProxyView.h"
#include "src/gpu/ganesh/SkGr.h"
#include "src/gpu/ganesh/SurfaceContext.h"
#include "src/utils/SkCharToGlyphCache.h"
#include "tests/Test.h"
#include <cmath>
#include <cstdlib>
#include <utility>

Go to the source code of this file.

Functions

void TestReadPixels (skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *srcContext, uint32_t expectedPixelValues[], const char *testName)
 
void TestWritePixels (skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *dstContext, bool expectedToWork, const char *testName)
 
void TestCopyFromSurface (skiatest::Reporter *reporter, GrDirectContext *dContext, sk_sp< GrSurfaceProxy > proxy, GrSurfaceOrigin origin, GrColorType colorType, uint32_t expectedPixelValues[], const char *testName)
 
static bool compare_colors (int x, int y, const float rgbaA[], const float rgbaB[], const float tolRGBA[4], std::function< ComparePixmapsErrorReporter > &error)
 
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)
 
void CheckSingleThreadedProxyRefs (skiatest::Reporter *reporter, GrSurfaceProxy *proxy, int32_t expectedProxyRefs, int32_t expectedBackingRefs)
 
std::unique_ptr< skgpu::ganesh::SurfaceContextCreateSurfaceContext (GrRecordingContext *rContext, const GrImageInfo &info, SkBackingFit fit, GrSurfaceOrigin origin, GrRenderable renderable, int sampleCount, skgpu::Mipmapped mipmapped, GrProtected isProtected, skgpu::Budgeted budgeted)
 
static SkGlyphID hash_to_glyph (uint32_t value)
 
 DEF_TEST (chartoglyph_cache, reporter)
 

Function Documentation

◆ CheckSingleThreadedProxyRefs()

void CheckSingleThreadedProxyRefs ( skiatest::Reporter reporter,
GrSurfaceProxy proxy,
int32_t  expectedProxyRefs,
int32_t  expectedBackingRefs 
)

Checks the ref cnt on a proxy and its backing store. This is only valid if the proxy and the resource are both used on a single thread.

Definition at line 231 of file TestUtils.cpp.

234 {
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}
reporter
Definition: FontMgrTest.cpp:39
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
bool refCntGreaterThan(int32_t threadIsolatedTestCnt) const
Definition: SkRefCnt.h:191

◆ CheckSolidPixels()

bool CheckSolidPixels ( const SkColor4f col,
const SkPixmap pixmap,
const float  tolRGBA[4],
std::function< ComparePixmapsErrorReporter > &  error 
)

Convenience version that checks that 'pixmap' is a solid field of 'col'

Definition at line 191 of file TestUtils.cpp.

194 {
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}
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
bool GrConvertPixels(const GrPixmap &dst, const GrCPixmap &src, bool flipY)
static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct)
Definition: GrTypesPriv.h:896
kUnpremul_SkAlphaType
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
static bool compare_colors(int x, int y, const float rgbaA[], const float rgbaB[], const float tolRGBA[4], std::function< ComparePixmapsErrorReporter > &error)
Definition: TestUtils.cpp:122
GrImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
Definition: GrImageInfo.cpp:46
GrImageInfo makeAlphaType(SkAlphaType at) const
Definition: GrImageInfo.cpp:42
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
static sk_sp< SkColorSpace > MakeSRGBLinear()
int width() const
Definition: SkPixmap.h:160
const SkImageInfo & info() const
Definition: SkPixmap.h:135
sk_sp< SkColorSpace > refColorSpace() const
Definition: SkPixmap.cpp:63
int height() const
Definition: SkPixmap.h:166
SkAlphaType alphaType() const
Definition: SkPixmap.h:175
const uint8_t uint32_t uint32_t GError ** error
double y
double x
SkImageInfo makeColorType(SkColorType newColorType) const
Definition: SkImageInfo.h:475

◆ compare_colors()

static bool compare_colors ( int  x,
int  y,
const float  rgbaA[],
const float  rgbaB[],
const float  tolRGBA[4],
std::function< ComparePixmapsErrorReporter > &  error 
)
static

Definition at line 122 of file TestUtils.cpp.

126 {
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}
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707

◆ ComparePixels()

bool ComparePixels ( const GrCPixmap a,
const GrCPixmap b,
const float  tolRGBA[4],
std::function< ComparePixmapsErrorReporter > &  error 
)

Compares pixels pointed to by 'a' to pixels pointed to by 'b'.

If the pixmaps have different dimensions error is called with negative coordinate values and zero diffs and no comparisons are made.

Before comparison pixels are converted to a common color type, alpha type, and color space. The color type is always 32 bit float. The alpha type is premul if one of the pixmaps is premul and the other is unpremul. The color space is linear sRGB if the pixmaps have different colorspaces, otherwise their common color space is used.

'tolRGBA' expresses the allowed difference between pixels in the comparison space per channel. If pixel components differ more than by 'tolRGBA' in absolute value in any channel then 'error' is called with the coordinate and difference in the comparison space (B - A).

The function quits after a single error is reported and returns false if 'error' was called and true otherwise.

Definition at line 142 of file TestUtils.cpp.

145 {
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}
SkAlphaType
Definition: SkAlphaType.h:26
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
static bool Equals(const SkColorSpace *, const SkColorSpace *)
static bool b
struct MyStruct a[10]

◆ CreateSurfaceContext()

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 
)

Definition at line 242 of file TestUtils.cpp.

250 {
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}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
const GrCaps * caps() const
GrBackendFormat getDefaultBackendFormat(GrColorType, GrRenderable) const
Definition: GrCaps.cpp:400
std::unique_ptr< skgpu::ganesh::SurfaceContext > makeSC(GrSurfaceProxyView readView, const GrColorInfo &)
GrRecordingContextPriv priv()
uint32_t uint32_t * format

◆ DEF_TEST()

DEF_TEST ( chartoglyph_cache  ,
reporter   
)

Definition at line 283 of file TestUtils.cpp.

283 {
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
int32_t SkUnichar
Definition: SkTypes.h:175
uint16_t SkGlyphID
Definition: SkTypes.h:179
static SkGlyphID hash_to_glyph(uint32_t value)
Definition: TestUtils.cpp:265
def gen()
Definition: dom.py:77
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition: switches.h:191
Definition: gen.py:1

◆ hash_to_glyph()

static SkGlyphID hash_to_glyph ( uint32_t  value)
static

Definition at line 265 of file TestUtils.cpp.

265 {
266 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
267}
constexpr uint16_t SkToU16(S x)
Definition: SkTo.h:24
uint8_t value

◆ TestCopyFromSurface()

void TestCopyFromSurface ( skiatest::Reporter reporter,
GrDirectContext dContext,
sk_sp< GrSurfaceProxy proxy,
GrSurfaceOrigin  origin,
GrColorType  colorType,
uint32_t  expectedPixelValues[],
const char *  testName 
)

Definition at line 98 of file TestUtils.cpp.

104 {
105 auto copy = GrSurfaceProxy::Copy(dContext,
106 std::move(proxy),
107 origin,
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}
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
void TestReadPixels(skiatest::Reporter *reporter, GrDirectContext *dContext, skgpu::ganesh::SurfaceContext *srcContext, uint32_t expectedPixelValues[], const char *testName)
Definition: TestUtils.cpp:39
skgpu::Swizzle getReadSwizzle(const GrBackendFormat &format, GrColorType colorType) const
Definition: GrCaps.cpp:443
GrDirectContextPriv 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)
Definition: copy.py:1

◆ TestReadPixels()

void TestReadPixels ( skiatest::Reporter reporter,
GrDirectContext dContext,
skgpu::ganesh::SurfaceContext srcContext,
uint32_t  expectedPixelValues[],
const char *  testName 
)

Definition at line 39 of file TestUtils.cpp.

43 {
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}
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
static bool read(SkStream *stream, void *buffer, size_t amount)
#define ERRORF(r,...)
Definition: Test.h:293
void alloc(const SkImageInfo &)
const uint32_t * addr32() const
Definition: SkPixmap.h:352
bool erase(SkColor color, const SkIRect &subset) const
Definition: SkPixmap.cpp:742
bool readPixels(GrDirectContext *dContext, GrPixmap dst, SkIPoint srcPt)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ TestWritePixels()

void TestWritePixels ( skiatest::Reporter reporter,
GrDirectContext dContext,
skgpu::ganesh::SurfaceContext dstContext,
bool  expectedToWork,
const char *  testName 
)

Definition at line 66 of file TestUtils.cpp.

70 {
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}
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
static GrColor SkColorToPremulGrColor(SkColor c)
Definition: SkGr.h:51
uint32_t * writable_addr32(int x, int y) const
Definition: SkPixmap.h:537
bool writePixels(GrDirectContext *dContext, GrCPixmap src, SkIPoint dstPt)
void write(SkWStream *wStream, const T &text)
Definition: skqp.cpp:188