xref: /aosp_15_r20/external/skia/tests/ReadPixelsTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2011 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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkClipOp.h"
13 #include "include/core/SkColor.h"
14 #include "include/core/SkColorPriv.h"
15 #include "include/core/SkColorSpace.h"
16 #include "include/core/SkColorType.h"
17 #include "include/core/SkImage.h"
18 #include "include/core/SkImageInfo.h"
19 #include "include/core/SkMatrix.h"
20 #include "include/core/SkPaint.h"
21 #include "include/core/SkPixmap.h"
22 #include "include/core/SkRect.h"
23 #include "include/core/SkRefCnt.h"
24 #include "include/core/SkSamplingOptions.h"
25 #include "include/core/SkScalar.h"
26 #include "include/core/SkSurface.h"
27 #include "include/core/SkTypes.h"
28 #include "include/private/SkColorData.h"
29 #include "include/private/base/SkAlign.h"
30 #include "include/private/base/SkCPUTypes.h"
31 #include "include/private/base/SkSafe32.h"
32 #include "src/base/SkHalf.h"
33 #include "src/base/SkMathPriv.h"
34 #include "src/core/SkImageInfoPriv.h"
35 #include "tests/Test.h"
36 
37 #include <cstdint>
38 #include <cstring>
39 #include <initializer_list>
40 #include <memory>
41 #include <string>
42 
43 static const int DEV_W = 100, DEV_H = 100;
44 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
45 static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
46                                                 DEV_H * SK_Scalar1);
47 
get_src_color(int x,int y)48 static SkPMColor get_src_color(int x, int y) {
49     SkASSERT(x >= 0 && x < DEV_W);
50     SkASSERT(y >= 0 && y < DEV_H);
51 
52     U8CPU r = x;
53     U8CPU g = y;
54     U8CPU b = 0xc;
55 
56     U8CPU a = 0xff;
57     switch ((x+y) % 5) {
58         case 0:
59             a = 0xff;
60             break;
61         case 1:
62             a = 0x80;
63             break;
64         case 2:
65             a = 0xCC;
66             break;
67         case 4:
68             a = 0x01;
69             break;
70         case 3:
71             a = 0x00;
72             break;
73     }
74     return SkPremultiplyARGBInline(a, r, g, b);
75 }
76 
get_dst_bmp_init_color(int x,int y,int w)77 static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
78     int n = y * w + x;
79 
80     U8CPU b = n & 0xff;
81     U8CPU g = (n >> 8) & 0xff;
82     U8CPU r = (n >> 16) & 0xff;
83     return SkPackARGB32(0xff, r, g , b);
84 }
85 
86 // TODO: Make this consider both ATs
convert_to_pmcolor(SkColorType ct,SkAlphaType at,const uint32_t * addr,bool * doUnpremul)87 static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
88                                     bool* doUnpremul) {
89     *doUnpremul = (kUnpremul_SkAlphaType == at);
90 
91     const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
92     U8CPU a,r,g,b;
93     switch (ct) {
94         case kBGRA_8888_SkColorType:
95             b = static_cast<U8CPU>(c[0]);
96             g = static_cast<U8CPU>(c[1]);
97             r = static_cast<U8CPU>(c[2]);
98             a = static_cast<U8CPU>(c[3]);
99             break;
100         case kRGB_888x_SkColorType:  // fallthrough
101         case kRGBA_8888_SkColorType:
102             r = static_cast<U8CPU>(c[0]);
103             g = static_cast<U8CPU>(c[1]);
104             b = static_cast<U8CPU>(c[2]);
105             // We set this even for kRGB_888x because our caller will validate that it is 0xff.
106             a = static_cast<U8CPU>(c[3]);
107             break;
108         default:
109             SkDEBUGFAIL("Unexpected colortype");
110             return 0;
111     }
112 
113     if (*doUnpremul) {
114         r = SkMulDiv255Ceiling(r, a);
115         g = SkMulDiv255Ceiling(g, a);
116         b = SkMulDiv255Ceiling(b, a);
117     }
118     return SkPackARGB32(a, r, g, b);
119 }
120 
make_src_image()121 static sk_sp<SkImage> make_src_image() {
122     static SkBitmap bmp;
123     if (bmp.isNull()) {
124         bmp.allocN32Pixels(DEV_W, DEV_H);
125         intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
126         for (int y = 0; y < DEV_H; ++y) {
127             for (int x = 0; x < DEV_W; ++x) {
128                 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
129                 *pixel = get_src_color(x, y);
130             }
131         }
132         bmp.setImmutable();
133     }
134     return bmp.asImage();
135 }
136 
fill_src_canvas(SkCanvas * canvas)137 static void fill_src_canvas(SkCanvas* canvas) {
138     canvas->save();
139     canvas->setMatrix(SkMatrix::I());
140     canvas->clipRect(DEV_RECT_S, SkClipOp::kIntersect);
141     SkPaint paint;
142     paint.setBlendMode(SkBlendMode::kSrc);
143     canvas->drawImage(make_src_image(), 0, 0, SkSamplingOptions(), &paint);
144     canvas->restore();
145 }
146 
fill_dst_bmp_with_init_data(SkBitmap * bitmap)147 static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
148     int w = bitmap->width();
149     int h = bitmap->height();
150     intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
151     for (int y = 0; y < h; ++y) {
152         for (int x = 0; x < w; ++x) {
153             SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
154             if (kAlpha_8_SkColorType == bitmap->colorType()) {
155                 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
156                 *alpha = SkGetPackedA32(initColor);
157             } else {
158                 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
159                 *pixel = initColor;
160             }
161         }
162     }
163 }
164 
check_read_pixel(SkPMColor a,SkPMColor b,bool didPremulConversion)165 static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
166     if (!didPremulConversion) {
167         return a == b;
168     }
169     int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
170     int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
171     int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
172     int32_t aB = SkGetPackedB32(a);
173 
174     int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
175     int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
176     int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
177     int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
178 
179     return aA == bA &&
180            SkAbs32(aR - bR) <= 1 &&
181            SkAbs32(aG - bG) <= 1 &&
182            SkAbs32(aB - bB) <= 1;
183 }
184 
185 // checks the bitmap contains correct pixels after the readPixels
186 // if the bitmap was prefilled with pixels it checks that these weren't
187 // overwritten in the area outside the readPixels.
check_read(skiatest::Reporter * reporter,const SkBitmap & bitmap,int x,int y,bool checkSurfacePixels,bool checkBitmapPixels,const SkImageInfo & surfaceInfo)188 static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
189                        bool checkSurfacePixels, bool checkBitmapPixels,
190                        const SkImageInfo& surfaceInfo) {
191     SkAlphaType bmpAT = bitmap.alphaType();
192     SkColorType bmpCT = bitmap.colorType();
193     SkASSERT(!bitmap.isNull());
194     SkASSERT(checkSurfacePixels || checkBitmapPixels);
195 
196     int bw = bitmap.width();
197     int bh = bitmap.height();
198 
199     SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
200     SkIRect clippedSrcRect = DEV_RECT;
201     if (!clippedSrcRect.intersect(srcRect)) {
202         clippedSrcRect.setEmpty();
203     }
204     if (kAlpha_8_SkColorType == bmpCT) {
205         for (int by = 0; by < bh; ++by) {
206             for (int bx = 0; bx < bw; ++bx) {
207                 int devx = bx + srcRect.fLeft;
208                 int devy = by + srcRect.fTop;
209                 const uint8_t* alpha = bitmap.getAddr8(bx, by);
210 
211                 if (clippedSrcRect.contains(devx, devy)) {
212                     if (checkSurfacePixels) {
213                         uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
214                                                        ? 0xFF
215                                                        : SkGetPackedA32(get_src_color(devx, devy));
216                         if (surfaceAlpha != *alpha) {
217                             ERRORF(reporter,
218                                    "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
219                                    bx, by, surfaceAlpha, *alpha);
220                             return false;
221                         }
222                     }
223                 } else if (checkBitmapPixels) {
224                     uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
225                     if (origDstAlpha != *alpha) {
226                         ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
227                             "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
228                         return false;
229                     }
230                 }
231             }
232         }
233         return true;
234     }
235     for (int by = 0; by < bh; ++by) {
236         for (int bx = 0; bx < bw; ++bx) {
237             int devx = bx + srcRect.fLeft;
238             int devy = by + srcRect.fTop;
239 
240             const uint32_t* pixel = bitmap.getAddr32(bx, by);
241 
242             if (clippedSrcRect.contains(devx, devy)) {
243                 if (checkSurfacePixels) {
244                     SkPMColor surfacePMColor = get_src_color(devx, devy);
245                     if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
246                         surfacePMColor &= 0xFF000000;
247                     }
248                     if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
249                         surfacePMColor |= 0xFF000000;
250                     }
251                     bool didPremul;
252                     SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
253                     if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
254                         ERRORF(reporter,
255                                "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
256                                "Readback was unpremul: %d",
257                                bx, by, surfacePMColor, pmPixel, didPremul);
258                         return false;
259                     }
260                 }
261             } else if (checkBitmapPixels) {
262                 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
263                 if (origDstPixel != *pixel) {
264                     ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
265                            "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
266                     return false;
267                 }
268             }
269         }
270     }
271     return true;
272 }
273 
274 enum class TightRowBytes : bool { kNo, kYes };
275 
init_bitmap(SkBitmap * bitmap,const SkIRect & rect,TightRowBytes tightRB,SkColorType ct,SkAlphaType at)276 static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB,
277                         SkColorType ct, SkAlphaType at) {
278     SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at);
279     size_t rowBytes = 0;
280     if (tightRB == TightRowBytes::kNo) {
281         rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
282     }
283     bitmap->allocPixels(info, rowBytes);
284 }
285 
286 static const struct {
287     SkColorType fColorType;
288     SkAlphaType fAlphaType;
289 } gReadPixelsConfigs[] = {
290         {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
291         {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
292         {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
293         {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
294         {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
295         {kAlpha_8_SkColorType, kPremul_SkAlphaType},
296 };
297 const SkIRect gReadPixelsTestRects[] = {
298     // entire thing
299     DEV_RECT,
300     // larger on all sides
301     SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
302     // fully contained
303     SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
304     // outside top left
305     SkIRect::MakeLTRB(-10, -10, -1, -1),
306     // touching top left corner
307     SkIRect::MakeLTRB(-10, -10, 0, 0),
308     // overlapping top left corner
309     SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
310     // overlapping top left and top right corners
311     SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, DEV_H / 4),
312     // touching entire top edge
313     SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, 0),
314     // overlapping top right corner
315     SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W  + 10, DEV_H / 4),
316     // contained in x, overlapping top edge
317     SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W  / 4, DEV_H / 4),
318     // outside top right corner
319     SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
320     // touching top right corner
321     SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
322     // overlapping top left and bottom left corners
323     SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
324     // touching entire left edge
325     SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
326     // overlapping bottom left corner
327     SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
328     // contained in y, overlapping left edge
329     SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
330     // outside bottom left corner
331     SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
332     // touching bottom left corner
333     SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
334     // overlapping bottom left and bottom right corners
335     SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
336     // touching entire left edge
337     SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
338     // overlapping bottom right corner
339     SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
340     // overlapping top right and bottom right corners
341     SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
342 };
343 
read_should_succeed(const SkIRect & srcRect,const SkImageInfo & dstInfo,const SkImageInfo & srcInfo)344 bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
345                          const SkImageInfo& srcInfo) {
346     return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
347 }
348 
test_readpixels(skiatest::Reporter * reporter,const sk_sp<SkSurface> & surface,const SkImageInfo & surfaceInfo)349 static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
350                             const SkImageInfo& surfaceInfo) {
351     SkCanvas* canvas = surface->getCanvas();
352     fill_src_canvas(canvas);
353     for (size_t rect = 0; rect < std::size(gReadPixelsTestRects); ++rect) {
354         const SkIRect& srcRect = gReadPixelsTestRects[rect];
355         for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
356             for (size_t c = 0; c < std::size(gReadPixelsConfigs); ++c) {
357                 SkBitmap bmp;
358                 init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
359                             gReadPixelsConfigs[c].fAlphaType);
360 
361                 // if the bitmap has pixels allocated before the readPixels,
362                 // note that and fill them with pattern
363                 bool startsWithPixels = !bmp.isNull();
364                 if (startsWithPixels) {
365                     fill_dst_bmp_with_init_data(&bmp);
366                 }
367                 uint32_t idBefore = surface->generationID();
368                 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
369                 uint32_t idAfter = surface->generationID();
370 
371                 // we expect to succeed when the read isn't fully clipped out and the infos are
372                 // compatible.
373                 bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
374                 // determine whether we expected the read to succeed.
375                 REPORTER_ASSERT(reporter, expectSuccess == success,
376                                 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
377                                 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
378                                 bmp.info().colorType(), bmp.info().alphaType());
379                 // read pixels should never change the gen id
380                 REPORTER_ASSERT(reporter, idBefore == idAfter);
381 
382                 if (success || startsWithPixels) {
383                     check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
384                                startsWithPixels, surfaceInfo);
385                 } else {
386                     // if we had no pixels beforehand and the readPixels
387                     // failed then our bitmap should still not have pixels
388                     REPORTER_ASSERT(reporter, bmp.isNull());
389                 }
390             }
391         }
392     }
393 }
394 
DEF_TEST(ReadPixels,reporter)395 DEF_TEST(ReadPixels, reporter) {
396     const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
397     auto surface(SkSurfaces::Raster(info));
398     test_readpixels(reporter, surface, info);
399 }
400 
401 ///////////////////////////////////////////////////////////////////////////////////////////////////
402 
403 static const uint32_t kNumPixels = 5;
404 
405 // The five reference pixels are: red, green, blue, white, black.
406 // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
407 // plus a tail pixel.
408 static const uint32_t rgba[kNumPixels] = {
409         0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
410 };
411 static const uint32_t bgra[kNumPixels] = {
412         0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
413 };
414 static const uint16_t rgb565[kNumPixels] = {
415         SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
416 };
417 
418 static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
419 
420 static const uint64_t kRed      = (uint64_t) SK_Half1 <<  0;
421 static const uint64_t kGreen    = (uint64_t) SK_Half1 << 16;
422 static const uint64_t kBlue     = (uint64_t) SK_Half1 << 32;
423 static const uint64_t kAlpha    = (uint64_t) SK_Half1 << 48;
424 static const uint64_t f16[kNumPixels] = {
425         kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
426 };
427 
428 static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
429 static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
430 
five_reference_pixels(SkColorType colorType)431 static const void* five_reference_pixels(SkColorType colorType) {
432     switch (colorType) {
433         case kUnknown_SkColorType:
434             return nullptr;
435         case kAlpha_8_SkColorType:
436             return alpha8;
437         case kRGB_565_SkColorType:
438             return rgb565;
439         case kARGB_4444_SkColorType:
440             return rgba4444;
441         case kRGBA_8888_SkColorType:
442             return rgba;
443         case kBGRA_8888_SkColorType:
444             return bgra;
445         case kGray_8_SkColorType:
446             return gray8;
447         case kRGBA_F16_SkColorType:
448             return f16;
449         default:
450             return nullptr;
451     }
452 
453     SkASSERT(false);
454     return nullptr;
455 }
456 
test_conversion(skiatest::Reporter * r,const SkImageInfo & dstInfo,const SkImageInfo & srcInfo)457 static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
458                             const SkImageInfo& srcInfo) {
459     if (!SkImageInfoIsValid(srcInfo)) {
460         return;
461     }
462 
463     const void* srcPixels = five_reference_pixels(srcInfo.colorType());
464     SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
465     sk_sp<SkImage> src = SkImages::RasterFromPixmap(srcPixmap, nullptr, nullptr);
466     REPORTER_ASSERT(r, src);
467 
468     // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
469     uint64_t dstPixels[kNumPixels];
470     SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
471     bool success = src->readPixels(nullptr, dstPixmap, 0, 0);
472     REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
473 
474     if (success) {
475         if (kGray_8_SkColorType == srcInfo.colorType() &&
476             kGray_8_SkColorType != dstInfo.colorType()) {
477             // TODO: test (r,g,b) == (gray,gray,gray)?
478             return;
479         }
480 
481         if (kGray_8_SkColorType == dstInfo.colorType() &&
482             kGray_8_SkColorType != srcInfo.colorType()) {
483             // TODO: test gray = luminance?
484             return;
485         }
486 
487         if (kAlpha_8_SkColorType == srcInfo.colorType() &&
488             kAlpha_8_SkColorType != dstInfo.colorType()) {
489             // TODO: test output = black with this alpha?
490             return;
491         }
492 
493         REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
494                                        kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
495     }
496 }
497 
DEF_TEST(ReadPixels_ValidConversion,reporter)498 DEF_TEST(ReadPixels_ValidConversion, reporter) {
499     const SkColorType kColorTypes[] = {
500             kUnknown_SkColorType,
501             kAlpha_8_SkColorType,
502             kRGB_565_SkColorType,
503             kARGB_4444_SkColorType,
504             kRGBA_8888_SkColorType,
505             kBGRA_8888_SkColorType,
506             kGray_8_SkColorType,
507             kRGBA_F16_SkColorType,
508     };
509 
510     const SkAlphaType kAlphaTypes[] = {
511             kUnknown_SkAlphaType,
512             kOpaque_SkAlphaType,
513             kPremul_SkAlphaType,
514             kUnpremul_SkAlphaType,
515     };
516 
517     const sk_sp<SkColorSpace> kColorSpaces[] = {
518             nullptr,
519             SkColorSpace::MakeSRGB(),
520     };
521 
522     for (SkColorType dstCT : kColorTypes) {
523         for (SkAlphaType dstAT : kAlphaTypes) {
524             for (const sk_sp<SkColorSpace>& dstCS : kColorSpaces) {
525                 for (SkColorType srcCT : kColorTypes) {
526                     for (SkAlphaType srcAT : kAlphaTypes) {
527                         for (const sk_sp<SkColorSpace>& srcCS : kColorSpaces) {
528                             test_conversion(reporter,
529                                             SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
530                                             SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
531                         }
532                     }
533                 }
534             }
535         }
536     }
537 }
538 
DEF_TEST(ReadPixels_InvalidRowBytes,reporter)539 DEF_TEST(ReadPixels_InvalidRowBytes, reporter) {
540     auto srcII = SkImageInfo::Make({10, 10}, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
541     auto surf = SkSurfaces::Raster(srcII);
542     for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) {
543         auto colorType = static_cast<SkColorType>(ct);
544         size_t bpp = SkColorTypeBytesPerPixel(colorType);
545         if (bpp <= 1) {
546             continue;
547         }
548         auto dstII = srcII.makeColorType(colorType);
549         size_t badRowBytes = (surf->width() + 1)*bpp - 1;
550         auto storage = std::make_unique<char[]>(badRowBytes*surf->height());
551         REPORTER_ASSERT(reporter, !surf->readPixels(dstII, storage.get(), badRowBytes, 0, 0));
552     }
553 }
554