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