• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkCanvas.h"
9 #include "SkColorPriv.h"
10 #include "SkColorSpace_Base.h"
11 #include "SkHalf.h"
12 #include "SkImageInfoPriv.h"
13 #include "SkMathPriv.h"
14 #include "SkSurface.h"
15 #include "Test.h"
16 
17 #if SK_SUPPORT_GPU
18 #include "GrContext.h"
19 #include "GrResourceProvider.h"
20 #include "SkGr.h"
21 #endif
22 
23 #include <initializer_list>
24 
25 static const int DEV_W = 100, DEV_H = 100;
26 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
27 static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
28                                                 DEV_H * SK_Scalar1);
29 
get_src_color(int x,int y)30 static SkPMColor get_src_color(int x, int y) {
31     SkASSERT(x >= 0 && x < DEV_W);
32     SkASSERT(y >= 0 && y < DEV_H);
33 
34     U8CPU r = x;
35     U8CPU g = y;
36     U8CPU b = 0xc;
37 
38     U8CPU a = 0xff;
39     switch ((x+y) % 5) {
40         case 0:
41             a = 0xff;
42             break;
43         case 1:
44             a = 0x80;
45             break;
46         case 2:
47             a = 0xCC;
48             break;
49         case 4:
50             a = 0x01;
51             break;
52         case 3:
53             a = 0x00;
54             break;
55     }
56     return SkPremultiplyARGBInline(a, r, g, b);
57 }
58 
get_dst_bmp_init_color(int x,int y,int w)59 static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
60     int n = y * w + x;
61 
62     U8CPU b = n & 0xff;
63     U8CPU g = (n >> 8) & 0xff;
64     U8CPU r = (n >> 16) & 0xff;
65     return SkPackARGB32(0xff, r, g , b);
66 }
67 
convert_to_pmcolor(SkColorType ct,SkAlphaType at,const uint32_t * addr,bool * doUnpremul)68 static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
69                                     bool* doUnpremul) {
70     *doUnpremul = (kUnpremul_SkAlphaType == at);
71 
72     const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
73     U8CPU a,r,g,b;
74     switch (ct) {
75         case kBGRA_8888_SkColorType:
76             b = static_cast<U8CPU>(c[0]);
77             g = static_cast<U8CPU>(c[1]);
78             r = static_cast<U8CPU>(c[2]);
79             a = static_cast<U8CPU>(c[3]);
80             break;
81         case kRGBA_8888_SkColorType:
82             r = static_cast<U8CPU>(c[0]);
83             g = static_cast<U8CPU>(c[1]);
84             b = static_cast<U8CPU>(c[2]);
85             a = static_cast<U8CPU>(c[3]);
86             break;
87         default:
88             SkDEBUGFAIL("Unexpected colortype");
89             return 0;
90     }
91 
92     if (*doUnpremul) {
93         r = SkMulDiv255Ceiling(r, a);
94         g = SkMulDiv255Ceiling(g, a);
95         b = SkMulDiv255Ceiling(b, a);
96     }
97     return SkPackARGB32(a, r, g, b);
98 }
99 
make_src_bitmap()100 static SkBitmap make_src_bitmap() {
101     static SkBitmap bmp;
102     if (bmp.isNull()) {
103         bmp.allocN32Pixels(DEV_W, DEV_H);
104         intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
105         for (int y = 0; y < DEV_H; ++y) {
106             for (int x = 0; x < DEV_W; ++x) {
107                 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
108                 *pixel = get_src_color(x, y);
109             }
110         }
111     }
112     return bmp;
113 }
114 
fill_src_canvas(SkCanvas * canvas)115 static void fill_src_canvas(SkCanvas* canvas) {
116     canvas->save();
117     canvas->setMatrix(SkMatrix::I());
118     canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
119     SkPaint paint;
120     paint.setBlendMode(SkBlendMode::kSrc);
121     canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
122     canvas->restore();
123 }
124 
125 #if SK_SUPPORT_GPU
fill_src_texture(GrTexture * texture)126 static void fill_src_texture(GrTexture* texture) {
127     SkBitmap bmp = make_src_bitmap();
128     bmp.lockPixels();
129     texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(),
130                          bmp.rowBytes());
131     bmp.unlockPixels();
132 }
133 #endif
134 
fill_dst_bmp_with_init_data(SkBitmap * bitmap)135 static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
136     SkAutoLockPixels alp(*bitmap);
137     int w = bitmap->width();
138     int h = bitmap->height();
139     intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
140     for (int y = 0; y < h; ++y) {
141         for (int x = 0; x < w; ++x) {
142             SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
143             if (kAlpha_8_SkColorType == bitmap->colorType()) {
144                 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
145                 *alpha = SkGetPackedA32(initColor);
146             } else {
147                 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
148                 *pixel = initColor;
149             }
150         }
151     }
152 }
153 
check_read_pixel(SkPMColor a,SkPMColor b,bool didPremulConversion)154 static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
155     if (!didPremulConversion) {
156         return a == b;
157     }
158     int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
159     int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
160     int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
161     int32_t aB = SkGetPackedB32(a);
162 
163     int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
164     int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
165     int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
166     int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
167 
168     return aA == bA &&
169            SkAbs32(aR - bR) <= 1 &&
170            SkAbs32(aG - bG) <= 1 &&
171            SkAbs32(aB - bB) <= 1;
172 }
173 
174 // checks the bitmap contains correct pixels after the readPixels
175 // if the bitmap was prefilled with pixels it checks that these weren't
176 // overwritten in the area outside the readPixels.
check_read(skiatest::Reporter * reporter,const SkBitmap & bitmap,int x,int y,bool checkCanvasPixels,bool checkBitmapPixels,SkColorType ct,SkAlphaType at)177 static bool check_read(skiatest::Reporter* reporter,
178                        const SkBitmap& bitmap,
179                        int x, int y,
180                        bool checkCanvasPixels,
181                        bool checkBitmapPixels,
182                        SkColorType ct,
183                        SkAlphaType at) {
184     SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType());
185     SkASSERT(!bitmap.isNull());
186     SkASSERT(checkCanvasPixels || checkBitmapPixels);
187 
188     int bw = bitmap.width();
189     int bh = bitmap.height();
190 
191     SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
192     SkIRect clippedSrcRect = DEV_RECT;
193     if (!clippedSrcRect.intersect(srcRect)) {
194         clippedSrcRect.setEmpty();
195     }
196     SkAutoLockPixels alp(bitmap);
197     if (kAlpha_8_SkColorType == ct) {
198         for (int by = 0; by < bh; ++by) {
199             for (int bx = 0; bx < bw; ++bx) {
200                 int devx = bx + srcRect.fLeft;
201                 int devy = by + srcRect.fTop;
202                 const uint8_t* alpha = bitmap.getAddr8(bx, by);
203 
204                 if (clippedSrcRect.contains(devx, devy)) {
205                     if (checkCanvasPixels) {
206                         uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy));
207                         if (canvasAlpha != *alpha) {
208                             ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
209                                    bx, by, canvasAlpha, *alpha);
210                             return false;
211                         }
212                     }
213                 } else if (checkBitmapPixels) {
214                     uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
215                     if (origDstAlpha != *alpha) {
216                         ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
217                             "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
218                         return false;
219                     }
220                 }
221             }
222         }
223         return true;
224     }
225     for (int by = 0; by < bh; ++by) {
226         for (int bx = 0; bx < bw; ++bx) {
227             int devx = bx + srcRect.fLeft;
228             int devy = by + srcRect.fTop;
229 
230             const uint32_t* pixel = bitmap.getAddr32(bx, by);
231 
232             if (clippedSrcRect.contains(devx, devy)) {
233                 if (checkCanvasPixels) {
234                     SkPMColor canvasPixel = get_src_color(devx, devy);
235                     bool didPremul;
236                     SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
237                     if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
238                         ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
239                                "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul);
240                         return false;
241                     }
242                 }
243             } else if (checkBitmapPixels) {
244                 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
245                 if (origDstPixel != *pixel) {
246                     ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
247                            "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
248                     return false;
249                 }
250             }
251         }
252     }
253     return true;
254 }
255 
256 enum BitmapInit {
257     kFirstBitmapInit = 0,
258 
259     kNoPixels_BitmapInit = kFirstBitmapInit,
260     kTight_BitmapInit,
261     kRowBytes_BitmapInit,
262     kRowBytesOdd_BitmapInit,
263 
264     kLastAligned_BitmapInit = kRowBytes_BitmapInit,
265 
266 #if 0  // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
267     kLast_BitmapInit = kRowBytesOdd_BitmapInit
268 #else
269     kLast_BitmapInit = kLastAligned_BitmapInit
270 #endif
271 };
272 
nextBMI(BitmapInit bmi)273 static BitmapInit nextBMI(BitmapInit bmi) {
274     int x = bmi;
275     return static_cast<BitmapInit>(++x);
276 }
277 
init_bitmap(SkBitmap * bitmap,const SkIRect & rect,BitmapInit init,SkColorType ct,SkAlphaType at)278 static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
279                         SkAlphaType at) {
280     SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
281     size_t rowBytes = 0;
282     bool alloc = true;
283     switch (init) {
284         case kNoPixels_BitmapInit:
285             alloc = false;
286         case kTight_BitmapInit:
287             break;
288         case kRowBytes_BitmapInit:
289             rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
290             break;
291         case kRowBytesOdd_BitmapInit:
292             rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
293             break;
294         default:
295             SkASSERT(0);
296             break;
297     }
298 
299     if (alloc) {
300         bitmap->allocPixels(info, rowBytes);
301     } else {
302         bitmap->setInfo(info, rowBytes);
303     }
304 }
305 
306 static const struct {
307     SkColorType fColorType;
308     SkAlphaType fAlphaType;
309 } gReadPixelsConfigs[] = {
310     { kRGBA_8888_SkColorType,   kPremul_SkAlphaType },
311     { kRGBA_8888_SkColorType,   kUnpremul_SkAlphaType },
312     { kBGRA_8888_SkColorType,   kPremul_SkAlphaType },
313     { kBGRA_8888_SkColorType,   kUnpremul_SkAlphaType },
314     { kAlpha_8_SkColorType,     kPremul_SkAlphaType },
315 };
316 const SkIRect gReadPixelsTestRects[] = {
317     // entire thing
318     DEV_RECT,
319     // larger on all sides
320     SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
321     // fully contained
322     SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
323     // outside top left
324     SkIRect::MakeLTRB(-10, -10, -1, -1),
325     // touching top left corner
326     SkIRect::MakeLTRB(-10, -10, 0, 0),
327     // overlapping top left corner
328     SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
329     // overlapping top left and top right corners
330     SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, DEV_H / 4),
331     // touching entire top edge
332     SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, 0),
333     // overlapping top right corner
334     SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W  + 10, DEV_H / 4),
335     // contained in x, overlapping top edge
336     SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W  / 4, DEV_H / 4),
337     // outside top right corner
338     SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
339     // touching top right corner
340     SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
341     // overlapping top left and bottom left corners
342     SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
343     // touching entire left edge
344     SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
345     // overlapping bottom left corner
346     SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
347     // contained in y, overlapping left edge
348     SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
349     // outside bottom left corner
350     SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
351     // touching bottom left corner
352     SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
353     // overlapping bottom left and bottom right corners
354     SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
355     // touching entire left edge
356     SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
357     // overlapping bottom right corner
358     SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
359     // overlapping top right and bottom right corners
360     SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
361 };
362 
test_readpixels(skiatest::Reporter * reporter,const sk_sp<SkSurface> & surface,BitmapInit lastBitmapInit)363 static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
364                             BitmapInit lastBitmapInit) {
365     SkCanvas* canvas = surface->getCanvas();
366     fill_src_canvas(canvas);
367     for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
368         const SkIRect& srcRect = gReadPixelsTestRects[rect];
369         for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
370             for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
371                 SkBitmap bmp;
372                 init_bitmap(&bmp, srcRect, bmi,
373                             gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
374 
375                 // if the bitmap has pixels allocated before the readPixels,
376                 // note that and fill them with pattern
377                 bool startsWithPixels = !bmp.isNull();
378                 if (startsWithPixels) {
379                     fill_dst_bmp_with_init_data(&bmp);
380                 }
381                 uint32_t idBefore = surface->generationID();
382                 bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
383                 uint32_t idAfter = surface->generationID();
384 
385                 // we expect to succeed when the read isn't fully clipped
386                 // out.
387                 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
388                 // determine whether we expected the read to succeed.
389                 REPORTER_ASSERT(reporter, success == expectSuccess);
390                 // read pixels should never change the gen id
391                 REPORTER_ASSERT(reporter, idBefore == idAfter);
392 
393                 if (success || startsWithPixels) {
394                     check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
395                                success, startsWithPixels,
396                                gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
397                 } else {
398                     // if we had no pixels beforehand and the readPixels
399                     // failed then our bitmap should still not have pixels
400                     REPORTER_ASSERT(reporter, bmp.isNull());
401                 }
402             }
403             // check the old webkit version of readPixels that clips the
404             // bitmap size
405             SkBitmap wkbmp;
406             bool success = canvas->readPixels(srcRect, &wkbmp);
407             SkIRect clippedRect = DEV_RECT;
408             if (clippedRect.intersect(srcRect)) {
409                 REPORTER_ASSERT(reporter, success);
410                 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
411                 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
412                 check_read(reporter, wkbmp, clippedRect.fLeft,
413                            clippedRect.fTop, true, false,
414                            kN32_SkColorType, kPremul_SkAlphaType);
415             } else {
416                 REPORTER_ASSERT(reporter, !success);
417             }
418         }
419     }
420 }
DEF_TEST(ReadPixels,reporter)421 DEF_TEST(ReadPixels, reporter) {
422     const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
423     auto surface(SkSurface::MakeRaster(info));
424     // SW readback fails a premul check when reading back to an unaligned rowbytes.
425     test_readpixels(reporter, surface, kLastAligned_BitmapInit);
426 }
427 #if SK_SUPPORT_GPU
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu,reporter,ctxInfo)428 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
429     const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
430     for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
431         sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
432                                                              ii, 0, origin, nullptr));
433         test_readpixels(reporter, surface, kLast_BitmapInit);
434     }
435 }
436 #endif
437 
438 #if SK_SUPPORT_GPU
test_readpixels_texture(skiatest::Reporter * reporter,GrTexture * texture)439 static void test_readpixels_texture(skiatest::Reporter* reporter, GrTexture* texture) {
440     fill_src_texture(texture);
441     for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
442         const SkIRect& srcRect = gReadPixelsTestRects[rect];
443         for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
444             for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
445                 SkBitmap bmp;
446                 init_bitmap(&bmp, srcRect, bmi,
447                             gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
448 
449                 // if the bitmap has pixels allocated before the readPixels,
450                 // note that and fill them with pattern
451                 bool startsWithPixels = !bmp.isNull();
452                 // Try doing the read directly from a non-renderable texture
453                 if (startsWithPixels) {
454                     fill_dst_bmp_with_init_data(&bmp);
455                     GrPixelConfig dstConfig =
456                             SkImageInfo2GrPixelConfig(bmp.info(), *texture->getContext()->caps());
457                     uint32_t flags = 0;
458                     if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
459                         flags = GrContext::kUnpremul_PixelOpsFlag;
460                     }
461                     bmp.lockPixels();
462                     bool success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(),
463                                                        bmp.height(), dstConfig, bmp.getPixels(),
464                                                        bmp.rowBytes(), flags);
465                     bmp.unlockPixels();
466                     check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
467                                success, true,
468                                gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
469                 }
470             }
471         }
472     }
473 }
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture,reporter,ctxInfo)474 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
475     // On the GPU we will also try reading back from a non-renderable texture.
476     for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
477         for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
478             GrSurfaceDesc desc;
479             desc.fFlags = flags;
480             desc.fWidth = DEV_W;
481             desc.fHeight = DEV_H;
482             desc.fConfig = kSkia8888_GrPixelConfig;
483             desc.fOrigin = origin;
484             sk_sp<GrTexture> texture(ctxInfo.grContext()->resourceProvider()->createTexture(desc,
485                 SkBudgeted::kNo));
486             test_readpixels_texture(reporter, texture.get());
487         }
488     }
489 }
490 #endif
491 
492 ///////////////////////////////////////////////////////////////////////////////////////////////////
493 
494 static const uint32_t kNumPixels = 5;
495 
496 // The five reference pixels are: red, green, blue, white, black.
497 // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
498 // plus a tail pixel.
499 static const uint32_t rgba[kNumPixels] = {
500         0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
501 };
502 static const uint32_t bgra[kNumPixels] = {
503         0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
504 };
505 static const uint16_t rgb565[kNumPixels] = {
506         SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
507 };
508 
509 static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
510 
511 static const uint64_t kRed      = (uint64_t) SK_Half1 <<  0;
512 static const uint64_t kGreen    = (uint64_t) SK_Half1 << 16;
513 static const uint64_t kBlue     = (uint64_t) SK_Half1 << 32;
514 static const uint64_t kAlpha    = (uint64_t) SK_Half1 << 48;
515 static const uint64_t f16[kNumPixels] = {
516         kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
517 };
518 
519 #ifdef SK_PMCOLOR_IS_RGBA
520 static const SkPMColor index8colors[kNumPixels] = {
521         0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
522 };
523 #else
524 static const SkPMColor index8colors[kNumPixels] = {
525         0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
526 };
527 #endif
528 static const uint8_t index8[kNumPixels] = { 0, 1, 2, 3, 4 };
529 static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
530 static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
531 
five_reference_pixels(SkColorType colorType)532 static const void* five_reference_pixels(SkColorType colorType) {
533     switch (colorType) {
534         case kUnknown_SkColorType:
535             return nullptr;
536         case kAlpha_8_SkColorType:
537             return alpha8;
538         case kRGB_565_SkColorType:
539             return rgb565;
540         case kARGB_4444_SkColorType:
541             return rgba4444;
542         case kRGBA_8888_SkColorType:
543             return rgba;
544         case kBGRA_8888_SkColorType:
545             return bgra;
546         case kIndex_8_SkColorType:
547             return index8;
548         case kGray_8_SkColorType:
549             return gray8;
550         case kRGBA_F16_SkColorType:
551             return f16;
552     }
553 
554     SkASSERT(false);
555     return nullptr;
556 }
557 
test_conversion(skiatest::Reporter * r,const SkImageInfo & dstInfo,const SkImageInfo & srcInfo)558 static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
559                             const SkImageInfo& srcInfo) {
560     if (!SkImageInfoIsValid(srcInfo)) {
561         return;
562     }
563 
564     sk_sp<SkColorTable> srcColorTable = (kIndex_8_SkColorType == srcInfo.colorType())
565             ? sk_make_sp<SkColorTable>(index8colors, 5)
566             : nullptr;
567     sk_sp<SkColorTable> dstColorTable = (kIndex_8_SkColorType == dstInfo.colorType())
568             ? sk_make_sp<SkColorTable>(index8colors, 5)
569             : nullptr;
570 
571     const void* srcPixels = five_reference_pixels(srcInfo.colorType());
572     SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes(), srcColorTable.get());
573     sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
574     REPORTER_ASSERT(r, src);
575 
576     // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
577     uint64_t dstPixels[kNumPixels];
578     SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes(), dstColorTable.get());
579     bool success = src->readPixels(dstPixmap, 0, 0);
580     REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
581 
582     if (success) {
583         if (kGray_8_SkColorType == srcInfo.colorType() &&
584             kGray_8_SkColorType != dstInfo.colorType())
585         {
586             // This conversion is legal, but we won't get the "reference" pixels since we cannot
587             // represent colors in kGray8.
588             return;
589         }
590 
591         REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
592                                        kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
593 
594     }
595 }
596 
DEF_TEST(ReadPixels_ValidConversion,reporter)597 DEF_TEST(ReadPixels_ValidConversion, reporter) {
598     const SkColorType kColorTypes[] = {
599             kUnknown_SkColorType,
600             kAlpha_8_SkColorType,
601             kRGB_565_SkColorType,
602             kARGB_4444_SkColorType,
603             kRGBA_8888_SkColorType,
604             kBGRA_8888_SkColorType,
605             kIndex_8_SkColorType,
606             kGray_8_SkColorType,
607             kRGBA_F16_SkColorType,
608     };
609 
610     const SkAlphaType kAlphaTypes[] = {
611             kUnknown_SkAlphaType,
612             kOpaque_SkAlphaType,
613             kPremul_SkAlphaType,
614             kUnpremul_SkAlphaType,
615     };
616 
617     const sk_sp<SkColorSpace> kColorSpaces[] = {
618             nullptr,
619             SkColorSpace::MakeSRGB(),
620     };
621 
622     for (SkColorType dstCT : kColorTypes) {
623         for (SkAlphaType dstAT: kAlphaTypes) {
624             for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
625                 for (SkColorType srcCT : kColorTypes) {
626                     for (SkAlphaType srcAT: kAlphaTypes) {
627                         for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
628                             if (kRGBA_F16_SkColorType == dstCT && dstCS) {
629                                 dstCS = as_CSB(dstCS)->makeLinearGamma();
630                             }
631 
632                             if (kRGBA_F16_SkColorType == srcCT && srcCS) {
633                                 srcCS = as_CSB(srcCS)->makeLinearGamma();
634                             }
635 
636                             test_conversion(reporter,
637                                             SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
638                                             SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
639                         }
640                     }
641                 }
642             }
643         }
644     }
645 }
646