Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions | Variables
WritePixelsTest.cpp File Reference
#include "include/core/SkAlphaType.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorPriv.h"
#include "include/core/SkColorType.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMallocPixelRef.h"
#include "include/core/SkPixelRef.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/GrBackendSurface.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrTypes.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/private/SkColorData.h"
#include "include/private/base/SkCPUTypes.h"
#include "include/private/base/SkMalloc.h"
#include "include/private/base/SkSafe32.h"
#include "include/private/base/SkTo.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
#include "src/base/SkMathPriv.h"
#include "src/core/SkImageInfoPriv.h"
#include "src/gpu/SkBackingFit.h"
#include "src/gpu/ganesh/GrCaps.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrProxyProvider.h"
#include "src/gpu/ganesh/GrTextureProxy.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include "tools/gpu/BackendSurfaceFactory.h"
#include <array>
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <memory>
#include <utility>

Go to the source code of this file.

Functions

static SkPMColor get_canvas_color (int x, int y)
 
static uint32_t pack_color_type (SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b)
 
static uint32_t get_bitmap_color (int x, int y, int w, SkColorType ct, SkAlphaType at)
 
static void fill_surface (SkSurface *surface)
 
static uint32_t premul (uint32_t color)
 
static SkPMColor convert_to_PMColor (SkColorType ct, SkAlphaType at, uint32_t color)
 
static bool check_pixel (SkPMColor a, SkPMColor b, bool didPremulConversion)
 
bool write_should_succeed (const SkImageInfo &dstInfo, const SkImageInfo &srcInfo, bool isGPU)
 
static bool check_write (skiatest::Reporter *reporter, SkSurface *surf, SkAlphaType surfaceAlphaType, const SkBitmap &bitmap, int writeX, int writeY)
 
static bool alloc_row_bytes (SkBitmap *bm, const SkImageInfo &info, size_t rowBytes)
 
static bool setup_bitmap (SkBitmap *bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB)
 
static void call_writepixels (SkSurface *surface)
 
 DEF_TEST (WritePixelsSurfaceGenID, reporter)
 
static void test_write_pixels (skiatest::Reporter *reporter, SkSurface *surface, const SkImageInfo &surfaceInfo)
 
static void free_pixels (void *pixels, void *ctx)
 
 DEF_TEST (WritePixels, reporter)
 
static void test_write_pixels (skiatest::Reporter *reporter, GrRecordingContext *rContext, int sampleCnt)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (WritePixels_Gpu, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (WritePixelsMSAA_Gpu, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
static void test_write_pixels_non_texture (skiatest::Reporter *reporter, GrDirectContext *dContext, int sampleCnt)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (WritePixelsNonTexture_Gpu, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (WritePixelsNonTextureMSAA_Gpu, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
static sk_sp< SkSurfacecreate_surf (GrRecordingContext *rContext, int width, int height)
 
static sk_sp< SkImageupload (const sk_sp< SkSurface > &surf, SkColor color)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (WritePixelsPendingIO, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
 
 DEF_TEST (WritePixels_InvalidRowBytes, reporter)
 

Variables

static const int DEV_W = 100
 
static const int DEV_H = 100
 
static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H)
 
static const U8CPU DEV_PAD = 0xee
 

Function Documentation

◆ alloc_row_bytes()

static bool alloc_row_bytes ( SkBitmap bm,
const SkImageInfo info,
size_t  rowBytes 
)
static

Definition at line 315 of file WritePixelsTest.cpp.

315 {
316 if (!bm->setInfo(info, rowBytes)) {
317 return false;
318 }
320 bm->setPixelRef(std::move(pr), 0, 0);
321 return true;
322}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
bool setInfo(const SkImageInfo &imageInfo, size_t rowBytes=0)
Definition SkBitmap.cpp:114
void setPixelRef(sk_sp< SkPixelRef > pixelRef, int dx, int dy)
Definition SkBitmap.cpp:182
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)

◆ call_writepixels()

static void call_writepixels ( SkSurface surface)
static

Definition at line 338 of file WritePixelsTest.cpp.

338 {
340 SkPMColor pixel = 0;
341 surface->writePixels({info, &pixel, sizeof(SkPMColor)}, 0, 0);
342}
uint32_t SkPMColor
Definition SkColor.h:205
VkSurfaceKHR surface
Definition main.cc:49
static SkImageInfo MakeN32Premul(int width, int height)

◆ check_pixel()

static bool check_pixel ( SkPMColor  a,
SkPMColor  b,
bool  didPremulConversion 
)
static

Definition at line 194 of file WritePixelsTest.cpp.

194 {
195 if (!didPremulConversion) {
196 return a == b;
197 }
198 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
199 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
200 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
201 int32_t aB = SkGetPackedB32(a);
202
203 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
204 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
205 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
206 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
207
208 return aA == bA &&
209 SkAbs32(aR - bR) <= 1 &&
210 SkAbs32(aG - bG) <= 1 &&
211 SkAbs32(aB - bB) <= 1;
212}
#define SkGetPackedB32(packed)
Definition SkColorPriv.h:95
#define SkGetPackedR32(packed)
Definition SkColorPriv.h:93
#define SkGetPackedA32(packed)
Definition SkColorPriv.h:92
#define SkGetPackedG32(packed)
Definition SkColorPriv.h:94
static int32_t SkAbs32(int32_t value)
Definition SkSafe32.h:41
static bool b
struct MyStruct a[10]

◆ check_write()

static bool check_write ( skiatest::Reporter reporter,
SkSurface surf,
SkAlphaType  surfaceAlphaType,
const SkBitmap bitmap,
int  writeX,
int  writeY 
)
static

Definition at line 238 of file WritePixelsTest.cpp.

239 {
240 size_t canvasRowBytes;
241 const uint32_t* canvasPixels;
242
243 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
244 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
245 // readPixels for the client.
246 SkBitmap secretDevBitmap;
247 secretDevBitmap.allocN32Pixels(surf->width(), surf->height());
248 if (!surf->readPixels(secretDevBitmap, 0, 0)) {
249 return false;
250 }
251
252 canvasRowBytes = secretDevBitmap.rowBytes();
253 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
254
255 if (nullptr == canvasPixels) {
256 return false;
257 }
258
259 if (surf->width() != DEV_W || surf->height() != DEV_H) {
260 return false;
261 }
262
263 const SkImageInfo& bmInfo = bitmap.info();
264
265 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
266 for (int cy = 0; cy < DEV_H; ++cy) {
267 for (int cx = 0; cx < DEV_W; ++cx) {
268 SkPMColor canvasPixel = canvasPixels[cx];
269 if (writeRect.contains(cx, cy)) {
270 int bx = cx - writeX;
271 int by = cy - writeY;
272 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
273 bmInfo.colorType(), bmInfo.alphaType());
274 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
275 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
276 bmpColor8888);
277 if (bmInfo.alphaType() == kOpaque_SkAlphaType ||
278 surfaceAlphaType == kOpaque_SkAlphaType) {
279 bmpPMColor |= 0xFF000000;
280 }
281 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
282 ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
283 "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
284 return false;
285 }
286 } else {
287 SkPMColor testColor = get_canvas_color(cx, cy);
288 if (canvasPixel != testColor) {
289 ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
290 " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
291 return false;
292 }
293 }
294 }
295 if (cy != DEV_H -1) {
296 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
297 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
298 bool check;
299 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
300 if (!check) {
301 return false;
302 }
303 }
304 }
305 canvasPixels += canvasRowBytes/4;
306 }
307
308 return true;
309}
reporter
kUnpremul_SkAlphaType
#define check(reporter, ref, unref, make, kill)
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define ERRORF(r,...)
Definition Test.h:293
static const int DEV_H
static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion)
static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color)
static uint32_t get_bitmap_color(int x, int y, int w, SkColorType ct, SkAlphaType at)
static const U8CPU DEV_PAD
static SkPMColor get_canvas_color(int x, int y)
static const int DEV_W
size_t rowBytes() const
Definition SkBitmap.h:238
void * getPixels() const
Definition SkBitmap.h:283
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
int width() const
Definition SkSurface.h:178
int height() const
Definition SkSurface.h:184
bool readPixels(const SkPixmap &dst, int srcX, int srcY)
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
bool contains(int32_t x, int32_t y) const
Definition SkRect.h:463
SkAlphaType alphaType() const
SkColorType colorType() const

◆ convert_to_PMColor()

static SkPMColor convert_to_PMColor ( SkColorType  ct,
SkAlphaType  at,
uint32_t  color 
)
static

Definition at line 175 of file WritePixelsTest.cpp.

175 {
176 if (kUnpremul_SkAlphaType == at) {
177 color = premul(color);
178 }
179 switch (ct) {
180 case kRGBA_8888_SkColorType: // fallthrough
183 break;
186 break;
187 default:
188 SkASSERT(0);
189 break;
190 }
191 return color;
192}
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
static SkPMColor SkSwizzle_RGBA_to_PMColor(uint32_t c)
Definition SkColorData.h:83
static SkPMColor SkSwizzle_BGRA_to_PMColor(uint32_t c)
Definition SkColorData.h:91
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:25
static uint32_t premul(uint32_t color)

◆ create_surf()

static sk_sp< SkSurface > create_surf ( GrRecordingContext rContext,
int  width,
int  height 
)
static

Definition at line 542 of file WritePixelsTest.cpp.

542 {
545
548 return surf;
549}
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
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_API void FlushAndSubmit(sk_sp< SkSurface >)
int32_t height
int32_t width
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [1/5]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( WritePixels_Gpu  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 477 of file WritePixelsTest.cpp.

480 {
481 test_write_pixels(reporter, ctxInfo.directContext(), 1);
482}
static void test_write_pixels(skiatest::Reporter *reporter, SkSurface *surface, const SkImageInfo &surfaceInfo)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [2/5]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( WritePixelsMSAA_Gpu  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 484 of file WritePixelsTest.cpp.

487 {
488 test_write_pixels(reporter, ctxInfo.directContext(), 1);
489}

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [3/5]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( WritePixelsNonTexture_Gpu  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 528 of file WritePixelsTest.cpp.

531 {
532 test_write_pixels_non_texture(reporter, ctxInfo.directContext(), 1);
533}
static void test_write_pixels_non_texture(skiatest::Reporter *reporter, GrDirectContext *dContext, int sampleCnt)

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [4/5]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( WritePixelsNonTextureMSAA_Gpu  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 535 of file WritePixelsTest.cpp.

538 {
539 test_write_pixels_non_texture(reporter, ctxInfo.directContext(), 4);
540}

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS() [5/5]

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( WritePixelsPendingIO  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kApiLevel_T   
)

Definition at line 568 of file WritePixelsTest.cpp.

571 {
572 auto context = ctxInfo.directContext();
573 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
574 const GrCaps* caps = context->priv().caps();
575
576 static const int kFullSize = 62;
577 static const int kHalfSize = 31;
578
579 static const uint32_t kLeftColor = 0xFF222222;
580 static const uint32_t kRightColor = 0xFFAAAAAA;
581
584 const SkImageInfo halfII = SkImageInfo::Make(kHalfSize, kFullSize,
586
588
589 {
590 // Seed the resource cached with a scratch texture that will be reused by writePixels
591 static constexpr SkISize kDims = {32, 64};
592
594 GrRenderable::kNo);
595
596 sk_sp<GrTextureProxy> temp = proxyProvider->createProxy(format,
597 kDims,
598 GrRenderable::kNo,
599 1,
600 skgpu::Mipmapped::kNo,
603 GrProtected::kNo,
604 /*label=*/"WritePixelsTest");
605 temp->instantiate(context->priv().resourceProvider());
606 }
607
608 // Create the surfaces and flush them to ensure there is no lingering pendingIO
609 sk_sp<SkSurface> leftSurf = create_surf(context, kHalfSize, kFullSize);
610 sk_sp<SkSurface> rightSurf = create_surf(context, kHalfSize, kFullSize);
611
612 sk_sp<SkImage> leftImg = upload(std::move(leftSurf), kLeftColor);
613 dest->getCanvas()->drawImage(std::move(leftImg), 0, 0);
614
615 sk_sp<SkImage> rightImg = upload(std::move(rightSurf), kRightColor);
616 dest->getCanvas()->drawImage(std::move(rightImg), kHalfSize, 0);
617
618 SkBitmap bm;
619 bm.allocPixels(fullII);
620 SkAssertResult(dest->readPixels(bm, 0, 0));
621
622 bool isCorrect = true;
623 for (int y = 0; isCorrect && y < 16; ++y) {
624 const uint32_t* sl = bm.getAddr32(0, y);
625
626 for (int x = 0; x < 16; ++x) {
627 if (kLeftColor != sl[x]) {
628 isCorrect = false;
629 break;
630 }
631 }
632 for (int x = kHalfSize; x < kHalfSize+16; ++x) {
633 if (kRightColor != sl[x]) {
634 isCorrect = false;
635 break;
636 }
637 }
638 }
639
640 REPORTER_ASSERT(reporter, isCorrect);
641}
static const int kFullSize
#define SkAssertResult(cond)
Definition SkAssert.h:123
static sk_sp< SkSurface > create_surf(GrRecordingContext *rContext, int width, int height)
GrBackendFormat getDefaultBackendFormat(GrColorType, GrRenderable) const
Definition GrCaps.cpp:400
sk_sp< GrTextureProxy > createProxy(const GrBackendFormat &, SkISize dimensions, GrRenderable, int renderTargetSampleCnt, skgpu::Mipmapped, SkBackingFit, skgpu::Budgeted, GrProtected, std::string_view label, GrInternalSurfaceFlags=GrInternalSurfaceFlags::kNone, UseAllocator useAllocator=UseAllocator::kYes)
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
uint32_t * getAddr32(int x, int y) const
Definition SkBitmap.h:1260
uint32_t uint32_t * format
double y
double x
dest
Definition zip.py:79

◆ DEF_TEST() [1/3]

DEF_TEST ( WritePixels  ,
reporter   
)

Definition at line 449 of file WritePixelsTest.cpp.

449 {
451 for (auto& tightRowBytes : { true, false }) {
452 const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
453 const size_t size = info.computeByteSize(rowBytes);
454 void* pixels = sk_malloc_throw(size);
455 // if rowBytes isn't tight then set the padding to a known value
456 if (!tightRowBytes) {
457 memset(pixels, DEV_PAD, size);
458 }
459 auto surface(SkSurfaces::WrapPixels(info, pixels, rowBytes, free_pixels, nullptr));
461 }
462}
static void * sk_malloc_throw(size_t size)
Definition SkMalloc.h:67
static void free_pixels(void *pixels, void *ctx)
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259

◆ DEF_TEST() [2/3]

DEF_TEST ( WritePixels_InvalidRowBytes  ,
reporter   
)

Definition at line 643 of file WritePixelsTest.cpp.

643 {
645 auto surf = SkSurfaces::Raster(dstII);
646 for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) {
647 auto colorType = static_cast<SkColorType>(ct);
648
650 if (bpp <= 1) {
651 continue;
652 }
653 auto srcII = dstII.makeColorType(colorType);
654 size_t badRowBytes = (surf->width() + 1)*bpp - 1;
655 auto storage = std::make_unique<char[]>(badRowBytes*surf->height());
656 memset(storage.get(), 0, badRowBytes * surf->height());
657 // SkSurface::writePixels doesn't report bool, SkCanvas's does.
659 !surf->getCanvas()->writePixels(srcII, storage.get(), badRowBytes, 0, 0));
660 }
661}
SkColorType
Definition SkColorType.h:19
@ kLastEnum_SkColorType
last valid value
Definition SkColorType.h:56
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)

◆ DEF_TEST() [3/3]

DEF_TEST ( WritePixelsSurfaceGenID  ,
reporter   
)

Definition at line 344 of file WritePixelsTest.cpp.

344 {
347 uint32_t genID1 = surface->generationID();
349 uint32_t genID2 = surface->generationID();
350 REPORTER_ASSERT(reporter, genID1 != genID2);
351}
static void call_writepixels(SkSurface *surface)

◆ fill_surface()

static void fill_surface ( SkSurface surface)
static

Definition at line 148 of file WritePixelsTest.cpp.

148 {
149 SkBitmap bmp;
151 for (int y = 0; y < DEV_H; ++y) {
152 for (int x = 0; x < DEV_W; ++x) {
153 *bmp.getAddr32(x, y) = get_canvas_color(x, y);
154 }
155 }
156 surface->writePixels(bmp, 0, 0);
157}

◆ free_pixels()

static void free_pixels ( void *  pixels,
void *  ctx 
)
static

Definition at line 447 of file WritePixelsTest.cpp.

447{ sk_free(pixels); }
SK_API void sk_free(void *)

◆ get_bitmap_color()

static uint32_t get_bitmap_color ( int  x,
int  y,
int  w,
SkColorType  ct,
SkAlphaType  at 
)
static

Definition at line 117 of file WritePixelsTest.cpp.

117 {
118 int n = y * w + x;
119 U8CPU b = n & 0xff;
120 U8CPU g = (n >> 8) & 0xff;
121 U8CPU r = (n >> 16) & 0xff;
122 U8CPU a = 0;
123 switch ((x+y) % 5) {
124 case 4:
125 a = 0xff;
126 break;
127 case 3:
128 a = 0x80;
129 break;
130 case 2:
131 a = 0xCC;
132 break;
133 case 1:
134 a = 0x01;
135 break;
136 case 0:
137 a = 0x00;
138 break;
139 }
140 if (kPremul_SkAlphaType == at) {
141 r = SkMulDiv255Ceiling(r, a);
142 g = SkMulDiv255Ceiling(g, a);
144 }
145 return pack_color_type(ct, a, r, g , b);
146}
unsigned U8CPU
Definition SkCPUTypes.h:18
static U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b)
Definition SkMathPriv.h:102
static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b)
SkScalar w

◆ get_canvas_color()

static SkPMColor get_canvas_color ( int  x,
int  y 
)
static

Definition at line 63 of file WritePixelsTest.cpp.

63 {
64 SkASSERT(x >= 0 && x < DEV_W);
65 SkASSERT(y >= 0 && y < DEV_H);
66
67 U8CPU r = x;
68 U8CPU g = y;
69 U8CPU b = 0xc;
70
71 U8CPU a = 0x0;
72 switch ((x+y) % 5) {
73 case 0:
74 a = 0xff;
75 break;
76 case 1:
77 a = 0x80;
78 break;
79 case 2:
80 a = 0xCC;
81 break;
82 case 3:
83 a = 0x00;
84 break;
85 case 4:
86 a = 0x01;
87 break;
88 }
89 return SkPremultiplyARGBInline(a, r, g, b);
90}
static SkPMColor SkPremultiplyARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)

◆ pack_color_type()

static uint32_t pack_color_type ( SkColorType  ct,
U8CPU  a,
U8CPU  r,
U8CPU  g,
U8CPU  b 
)
static

Definition at line 93 of file WritePixelsTest.cpp.

93 {
94 uint32_t r32;
95 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
96 switch (ct) {
98 result[0] = b;
99 result[1] = g;
100 result[2] = r;
101 result[3] = a;
102 break;
103 case kRGBA_8888_SkColorType: // fallthrough
105 result[0] = r;
106 result[1] = g;
107 result[2] = b;
108 result[3] = a;
109 break;
110 default:
111 SkASSERT(0);
112 return 0;
113 }
114 return r32;
115}
GAsyncResult * result

◆ premul()

static uint32_t premul ( uint32_t  color)
static

Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA. Thus this routine doesn't need to know the exact colortype

Definition at line 163 of file WritePixelsTest.cpp.

163 {
164 unsigned a = SkGetPackedA32(color);
165 // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
166 unsigned c0 = SkGetPackedR32(color);
167 unsigned c1 = SkGetPackedG32(color);
168 unsigned c2 = SkGetPackedB32(color);
169 c0 = SkMulDiv255Ceiling(c0, a);
170 c1 = SkMulDiv255Ceiling(c1, a);
171 c2 = SkMulDiv255Ceiling(c2, a);
172 return SkPackARGB32NoCheck(a, c0, c1, c2);
173}
static SkPMColor SkPackARGB32NoCheck(U8CPU a, U8CPU r, U8CPU g, U8CPU b)

◆ setup_bitmap()

static bool setup_bitmap ( SkBitmap bm,
SkColorType  ct,
SkAlphaType  at,
int  w,
int  h,
int  tightRB 
)
static

Definition at line 324 of file WritePixelsTest.cpp.

324 {
325 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
327 if (!alloc_row_bytes(bm, info, rowBytes)) {
328 return false;
329 }
330 for (int y = 0; y < h; ++y) {
331 for (int x = 0; x < w; ++x) {
332 *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
333 }
334 }
335 return true;
336}
static bool alloc_row_bytes(SkBitmap *bm, const SkImageInfo &info, size_t rowBytes)
SkScalar h

◆ test_write_pixels() [1/2]

static void test_write_pixels ( skiatest::Reporter reporter,
GrRecordingContext rContext,
int  sampleCnt 
)
static

Definition at line 464 of file WritePixelsTest.cpp.

466 {
468 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
470 rContext, skgpu::Budgeted::kNo, ii, sampleCnt, origin, nullptr));
471 if (surface) {
473 }
474 }
475}
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148

◆ test_write_pixels() [2/2]

static void test_write_pixels ( skiatest::Reporter reporter,
SkSurface surface,
const SkImageInfo surfaceInfo 
)
static

Definition at line 353 of file WritePixelsTest.cpp.

354 {
355 const SkIRect testRects[] = {
356 // entire thing
357 DEV_RECT,
358 // larger on all sides
359 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
360 // fully contained
361 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
362 // outside top left
363 SkIRect::MakeLTRB(-10, -10, -1, -1),
364 // touching top left corner
365 SkIRect::MakeLTRB(-10, -10, 0, 0),
366 // overlapping top left corner
367 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
368 // overlapping top left and top right corners
369 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
370 // touching entire top edge
371 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
372 // overlapping top right corner
373 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
374 // contained in x, overlapping top edge
375 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
376 // outside top right corner
377 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
378 // touching top right corner
379 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
380 // overlapping top left and bottom left corners
381 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
382 // touching entire left edge
383 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
384 // overlapping bottom left corner
385 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
386 // contained in y, overlapping left edge
387 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
388 // outside bottom left corner
389 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
390 // touching bottom left corner
391 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
392 // overlapping bottom left and bottom right corners
393 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
394 // touching entire left edge
396 // overlapping bottom right corner
397 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
398 // overlapping top right and bottom right corners
399 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
400 };
401
402 SkCanvas* canvas = surface->getCanvas();
403
404 static const struct {
406 SkAlphaType fAlphaType;
407 } gSrcConfigs[] = {
413 };
414 for (size_t r = 0; r < std::size(testRects); ++r) {
415 const SkIRect& rect = testRects[r];
416 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
417 for (size_t c = 0; c < std::size(gSrcConfigs); ++c) {
418 const SkColorType ct = gSrcConfigs[c].fColorType;
419 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
420
421 bool isGPU = SkToBool(surface->getCanvas()->recordingContext()) ||
422 SkToBool(surface->getCanvas()->recorder());
424 SkBitmap bmp;
425 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
426 rect.height(), SkToBool(tightBmp)));
427 uint32_t idBefore = surface->generationID();
428
429 surface->writePixels(bmp, rect.fLeft, rect.fTop);
430
431 uint32_t idAfter = surface->generationID();
433 bmp, rect.fLeft, rect.fTop));
434
435 // we should change the genID iff pixels were actually written.
436 SkIRect canvasRect = SkIRect::MakeSize(canvas->getBaseLayerSize());
437 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
438 bmp.width(), bmp.height());
439 bool expectSuccess = SkIRect::Intersects(canvasRect, writeRect) &&
440 write_should_succeed(surfaceInfo, bmp.info(), isGPU);
441 REPORTER_ASSERT(reporter, expectSuccess == (idBefore != idAfter));
442 }
443 }
444 }
445}
SkColorType fColorType
SkAlphaType
Definition SkAlphaType.h:26
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
static void fill_surface(SkSurface *surface)
static bool check_write(skiatest::Reporter *reporter, SkSurface *surf, SkAlphaType surfaceAlphaType, const SkBitmap &bitmap, int writeX, int writeY)
bool write_should_succeed(const SkImageInfo &dstInfo, const SkImageInfo &srcInfo, bool isGPU)
static const SkIRect DEV_RECT
static bool setup_bitmap(SkBitmap *bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB)
int width() const
Definition SkBitmap.h:149
const SkImageInfo & info() const
Definition SkBitmap.h:139
int height() const
Definition SkBitmap.h:158
virtual SkISize getBaseLayerSize() const
Definition SkCanvas.cpp:373
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
static bool Intersects(const SkIRect &a, const SkIRect &b)
Definition SkRect.h:535
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition SkRect.h:91
static constexpr SkIRect MakeSize(const SkISize &size)
Definition SkRect.h:66

◆ test_write_pixels_non_texture()

static void test_write_pixels_non_texture ( skiatest::Reporter reporter,
GrDirectContext dContext,
int  sampleCnt 
)
static

Definition at line 511 of file WritePixelsTest.cpp.

513 {
515 SkColorType colorType = kN32_SkColorType;
517 {DEV_W, DEV_H},
518 origin,
519 sampleCnt,
520 colorType);
521 if (surface) {
524 }
525 }
526}
sk_sp< SkSurface > MakeBackendRenderTargetSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, GrProtected isProtected, const SkSurfaceProps *props)

◆ upload()

static sk_sp< SkImage > upload ( const sk_sp< SkSurface > &  surf,
SkColor  color 
)
static

Definition at line 551 of file WritePixelsTest.cpp.

551 {
553 SkBitmap bm;
554 bm.allocPixels(smII);
555 bm.eraseColor(color);
556
557 surf->writePixels(bm, 0, 0);
558
559 return surf->makeImageSnapshot();
560}
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
sk_sp< SkImage > makeImageSnapshot()
Definition SkSurface.cpp:90
void writePixels(const SkPixmap &src, int dstX, int dstY)

◆ write_should_succeed()

bool write_should_succeed ( const SkImageInfo dstInfo,
const SkImageInfo srcInfo,
bool  isGPU 
)

Definition at line 214 of file WritePixelsTest.cpp.

214 {
215 if (!SkImageInfoValidConversion(dstInfo, srcInfo)) {
216 return false;
217 }
218 if (!isGPU) {
219 return true;
220 }
221 // The GPU backend supports writing unpremul data to a premul dst but not vice versa.
222 if (srcInfo.alphaType() == kPremul_SkAlphaType &&
223 dstInfo.alphaType() == kUnpremul_SkAlphaType) {
224 return false;
225 }
226 if (!SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
228 return false;
229 }
230 // The source has no alpha value and the dst is only alpha
231 if (SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
233 return false;
234 }
235 return true;
236}
static bool SkColorTypeIsAlphaOnly(SkColorType ct)
static bool SkImageInfoValidConversion(const SkImageInfo &dst, const SkImageInfo &src)
SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct)

Variable Documentation

◆ DEV_H

const int DEV_H = 100
static

Definition at line 59 of file WritePixelsTest.cpp.

◆ DEV_PAD

const U8CPU DEV_PAD = 0xee
static

Definition at line 61 of file WritePixelsTest.cpp.

◆ DEV_RECT

const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H)
static

Definition at line 60 of file WritePixelsTest.cpp.

◆ DEV_W

const int DEV_W = 100
static

Definition at line 59 of file WritePixelsTest.cpp.