Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Namespaces | Typedefs | Functions
TestUtils.h File Reference
#include "include/core/SkColor.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/GrTypes.h"
#include "src/gpu/SkBackingFit.h"
#include "src/gpu/ganesh/GrImageInfo.h"
#include "src/gpu/ganesh/GrPixmap.h"
#include <cstdint>
#include <functional>
#include <memory>

Go to the source code of this file.

Namespaces

namespace  skiatest
 
namespace  skgpu
 
namespace  skgpu::ganesh
 

Typedefs

typedef uint32_t GrColor
 
using ComparePixmapsErrorReporter = void(int x, int y, const float diffs[4])
 

Functions

void TestReadPixels (skiatest::Reporter *, GrDirectContext *, skgpu::ganesh::SurfaceContext *, uint32_t expectedPixelValues[], const char *testName)
 
void TestWritePixels (skiatest::Reporter *, GrDirectContext *, skgpu::ganesh::SurfaceContext *, bool expectedToWork, const char *testName)
 
void TestCopyFromSurface (skiatest::Reporter *, GrDirectContext *, sk_sp< GrSurfaceProxy > proxy, GrSurfaceOrigin origin, GrColorType colorType, uint32_t expectedPixelValues[], const char *testName)
 
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 *, const GrImageInfo &, SkBackingFit=SkBackingFit::kExact, GrSurfaceOrigin=kTopLeft_GrSurfaceOrigin, GrRenderable=GrRenderable::kNo, int sampleCount=1, skgpu::Mipmapped=skgpu::Mipmapped::kNo, GrProtected=GrProtected::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
 

Typedef Documentation

◆ ComparePixmapsErrorReporter

using ComparePixmapsErrorReporter = void(int x, int y, const float diffs[4])

Used by compare_pixels.

Definition at line 61 of file TestUtils.h.

◆ GrColor

typedef uint32_t GrColor

Definition at line 33 of file TestUtils.h.

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
#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}
bool GrConvertPixels(const GrPixmap &dst, const GrCPixmap &src, bool flipY)
static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct)
kUnpremul_SkAlphaType
#define SkAssertResult(cond)
Definition SkAssert.h:123
@ 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)
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
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

◆ 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 = SkBackingFit::kExact,
GrSurfaceOrigin  origin = kTopLeft_GrSurfaceOrigin,
GrRenderable  renderable = GrRenderable::kNo,
int  sampleCount = 1,
skgpu::Mipmapped  mipmapped = skgpu::Mipmapped::kNo,
GrProtected  isProtected = GrProtected::kNo,
skgpu::Budgeted  budgeted = skgpu::Budgeted::kYes 
)

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

◆ 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,
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}
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