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