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 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 8 | #include <initializer_list> |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 10 | #include "SkColorData.h" |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 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 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 17 | #include "GrContext.h" |
Robert Phillips | eb4f186 | 2017-06-08 16:38:25 -0400 | [diff] [blame] | 18 | #include "GrContextFactory.h" |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 19 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 20 | #include "GrProxyProvider.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 21 | #include "ProxyUtils.h" |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 22 | #include "SkGr.h" |
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 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 25 | static const int DEV_W = 100, DEV_H = 100; |
| 26 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 27 | 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] | 28 | DEV_H * SK_Scalar1); |
| 29 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 30 | static SkPMColor get_src_color(int x, int y) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 31 | SkASSERT(x >= 0 && x < DEV_W); |
| 32 | SkASSERT(y >= 0 && y < DEV_H); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 33 | |
| 34 | U8CPU r = x; |
| 35 | U8CPU g = y; |
| 36 | U8CPU b = 0xc; |
| 37 | |
| 38 | U8CPU a = 0xff; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 39 | switch ((x+y) % 5) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 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); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 57 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 58 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 59 | 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] | 60 | int n = y * w + x; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 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 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 68 | // TODO: Make this consider both ATs |
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; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 82 | case kRGB_888x_SkColorType: // fallthrough |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 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]); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 87 | // We set this even when for kRGB_888x because our caller will validate that it is 0xff. |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 88 | a = static_cast<U8CPU>(c[3]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 89 | break; |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 90 | default: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 91 | SkDEBUGFAIL("Unexpected colortype"); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 92 | return 0; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 93 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 94 | |
| 95 | if (*doUnpremul) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 96 | r = SkMulDiv255Ceiling(r, a); |
| 97 | g = SkMulDiv255Ceiling(g, a); |
| 98 | b = SkMulDiv255Ceiling(b, a); |
| 99 | } |
| 100 | return SkPackARGB32(a, r, g, b); |
| 101 | } |
| 102 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 103 | static SkBitmap make_src_bitmap() { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 104 | static SkBitmap bmp; |
| 105 | if (bmp.isNull()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 106 | bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 107 | intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
| 108 | for (int y = 0; y < DEV_H; ++y) { |
| 109 | for (int x = 0; x < DEV_W; ++x) { |
| 110 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 111 | *pixel = get_src_color(x, y); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 115 | return bmp; |
| 116 | } |
| 117 | |
| 118 | static void fill_src_canvas(SkCanvas* canvas) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 119 | canvas->save(); |
| 120 | canvas->setMatrix(SkMatrix::I()); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 121 | canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 122 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 123 | paint.setBlendMode(SkBlendMode::kSrc); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 124 | canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 125 | canvas->restore(); |
| 126 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 128 | static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 129 | int w = bitmap->width(); |
| 130 | int h = bitmap->height(); |
| 131 | intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
| 132 | for (int y = 0; y < h; ++y) { |
| 133 | for (int x = 0; x < w; ++x) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 134 | SkPMColor initColor = get_dst_bmp_init_color(x, y, w); |
| 135 | if (kAlpha_8_SkColorType == bitmap->colorType()) { |
| 136 | uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x); |
| 137 | *alpha = SkGetPackedA32(initColor); |
| 138 | } else { |
| 139 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); |
| 140 | *pixel = initColor; |
| 141 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 146 | static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 147 | if (!didPremulConversion) { |
| 148 | return a == b; |
| 149 | } |
| 150 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 151 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 152 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 153 | int32_t aB = SkGetPackedB32(a); |
| 154 | |
| 155 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 156 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 157 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 158 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 159 | |
| 160 | return aA == bA && |
| 161 | SkAbs32(aR - bR) <= 1 && |
| 162 | SkAbs32(aG - bG) <= 1 && |
| 163 | SkAbs32(aB - bB) <= 1; |
| 164 | } |
| 165 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 166 | // checks the bitmap contains correct pixels after the readPixels |
| 167 | // if the bitmap was prefilled with pixels it checks that these weren't |
| 168 | // overwritten in the area outside the readPixels. |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 169 | static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y, |
| 170 | bool checkSurfacePixels, bool checkBitmapPixels, |
| 171 | SkAlphaType surfaceAlphaType) { |
| 172 | SkAlphaType bmpAT = bitmap.alphaType(); |
| 173 | SkColorType bmpCT = bitmap.colorType(); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 174 | SkASSERT(!bitmap.isNull()); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 175 | SkASSERT(checkSurfacePixels || checkBitmapPixels); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 176 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 177 | int bw = bitmap.width(); |
| 178 | int bh = bitmap.height(); |
| 179 | |
| 180 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); |
| 181 | SkIRect clippedSrcRect = DEV_RECT; |
| 182 | if (!clippedSrcRect.intersect(srcRect)) { |
| 183 | clippedSrcRect.setEmpty(); |
| 184 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 185 | if (kAlpha_8_SkColorType == bmpCT) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 186 | for (int by = 0; by < bh; ++by) { |
| 187 | for (int bx = 0; bx < bw; ++bx) { |
| 188 | int devx = bx + srcRect.fLeft; |
| 189 | int devy = by + srcRect.fTop; |
| 190 | const uint8_t* alpha = bitmap.getAddr8(bx, by); |
| 191 | |
| 192 | if (clippedSrcRect.contains(devx, devy)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 193 | if (checkSurfacePixels) { |
| 194 | uint8_t surfaceAlpha = (surfaceAlphaType == kOpaque_SkAlphaType) |
| 195 | ? 0xFF |
| 196 | : SkGetPackedA32(get_src_color(devx, devy)); |
| 197 | if (surfaceAlpha != *alpha) { |
| 198 | ERRORF(reporter, |
| 199 | "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ", |
| 200 | bx, by, surfaceAlpha, *alpha); |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | } else if (checkBitmapPixels) { |
| 205 | uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw)); |
| 206 | if (origDstAlpha != *alpha) { |
| 207 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 208 | "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha); |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | return true; |
| 215 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 216 | for (int by = 0; by < bh; ++by) { |
| 217 | for (int bx = 0; bx < bw; ++bx) { |
| 218 | int devx = bx + srcRect.fLeft; |
| 219 | int devy = by + srcRect.fTop; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 220 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 221 | const uint32_t* pixel = bitmap.getAddr32(bx, by); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 222 | |
| 223 | if (clippedSrcRect.contains(devx, devy)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 224 | if (checkSurfacePixels) { |
| 225 | SkPMColor surfacePMColor = get_src_color(devx, devy); |
| 226 | if (kOpaque_SkAlphaType == surfaceAlphaType || kOpaque_SkAlphaType == bmpAT) { |
| 227 | surfacePMColor |= 0xFF000000; |
| 228 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 229 | bool didPremul; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 230 | SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul); |
| 231 | if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) { |
| 232 | ERRORF(reporter, |
| 233 | "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. " |
| 234 | "Readback was unpremul: %d", |
| 235 | bx, by, surfacePMColor, pmPixel, didPremul); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 236 | return false; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 237 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 238 | } |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 239 | } else if (checkBitmapPixels) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 240 | uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw); |
| 241 | if (origDstPixel != *pixel) { |
| 242 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 243 | "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 244 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 249 | return true; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | enum BitmapInit { |
| 253 | kFirstBitmapInit = 0, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 254 | |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 255 | kTight_BitmapInit = kFirstBitmapInit, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 256 | kRowBytes_BitmapInit, |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 257 | kRowBytesOdd_BitmapInit, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 258 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 259 | kLastAligned_BitmapInit = kRowBytes_BitmapInit, |
Brian Salomon | a64afd6 | 2016-02-01 16:44:22 -0500 | [diff] [blame] | 260 | |
| 261 | #if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 262 | kLast_BitmapInit = kRowBytesOdd_BitmapInit |
Brian Salomon | a64afd6 | 2016-02-01 16:44:22 -0500 | [diff] [blame] | 263 | #else |
| 264 | kLast_BitmapInit = kLastAligned_BitmapInit |
| 265 | #endif |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 268 | static BitmapInit nextBMI(BitmapInit bmi) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 269 | int x = bmi; |
| 270 | return static_cast<BitmapInit>(++x); |
| 271 | } |
| 272 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 273 | static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct, |
| 274 | SkAlphaType at) { |
| 275 | SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 276 | size_t rowBytes = 0; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 277 | switch (init) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 278 | case kTight_BitmapInit: |
| 279 | break; |
| 280 | case kRowBytes_BitmapInit: |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 281 | rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 282 | break; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 283 | case kRowBytesOdd_BitmapInit: |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 284 | rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 285 | break; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 286 | default: |
| 287 | SkASSERT(0); |
| 288 | break; |
| 289 | } |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 290 | bitmap->allocPixels(info, rowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 291 | } |
| 292 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 293 | static const struct { |
| 294 | SkColorType fColorType; |
| 295 | SkAlphaType fAlphaType; |
| 296 | } gReadPixelsConfigs[] = { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 297 | {kRGBA_8888_SkColorType, kPremul_SkAlphaType}, |
| 298 | {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 299 | {kRGB_888x_SkColorType, kOpaque_SkAlphaType}, |
| 300 | {kBGRA_8888_SkColorType, kPremul_SkAlphaType}, |
| 301 | {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 302 | {kAlpha_8_SkColorType, kPremul_SkAlphaType}, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 303 | }; |
| 304 | const SkIRect gReadPixelsTestRects[] = { |
| 305 | // entire thing |
| 306 | DEV_RECT, |
| 307 | // larger on all sides |
| 308 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 309 | // fully contained |
| 310 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 311 | // outside top left |
| 312 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 313 | // touching top left corner |
| 314 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 315 | // overlapping top left corner |
| 316 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 317 | // overlapping top left and top right corners |
| 318 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 319 | // touching entire top edge |
| 320 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 321 | // overlapping top right corner |
| 322 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 323 | // contained in x, overlapping top edge |
| 324 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 325 | // outside top right corner |
| 326 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 327 | // touching top right corner |
| 328 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 329 | // overlapping top left and bottom left corners |
| 330 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 331 | // touching entire left edge |
| 332 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 333 | // overlapping bottom left corner |
| 334 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 335 | // contained in y, overlapping left edge |
| 336 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 337 | // outside bottom left corner |
| 338 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 339 | // touching bottom left corner |
| 340 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 341 | // overlapping bottom left and bottom right corners |
| 342 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 343 | // touching entire left edge |
| 344 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 345 | // overlapping bottom right corner |
| 346 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 347 | // overlapping top right and bottom right corners |
| 348 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 349 | }; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 350 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 351 | enum class ReadSuccessExpectation { |
| 352 | kNo, |
| 353 | kMaybe, |
| 354 | kYes, |
| 355 | }; |
| 356 | |
| 357 | bool check_success_expectation(ReadSuccessExpectation expectation, bool actualSuccess) { |
| 358 | switch (expectation) { |
| 359 | case ReadSuccessExpectation::kMaybe: |
| 360 | return true; |
| 361 | case ReadSuccessExpectation::kNo: |
| 362 | return !actualSuccess; |
| 363 | case ReadSuccessExpectation::kYes: |
| 364 | return actualSuccess; |
| 365 | } |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | ReadSuccessExpectation read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo, |
| 370 | const SkImageInfo& srcInfo, bool isGPU) { |
| 371 | if (!SkIRect::Intersects(srcRect, DEV_RECT)) { |
| 372 | return ReadSuccessExpectation::kNo; |
| 373 | } |
| 374 | if (!SkImageInfoValidConversion(dstInfo, srcInfo)) { |
| 375 | return ReadSuccessExpectation::kNo; |
| 376 | } |
| 377 | if (!isGPU) { |
| 378 | return ReadSuccessExpectation::kYes; |
| 379 | } |
| 380 | // This serves more as documentation of what currently works on the GPU rather than desired |
| 381 | // expectations. Once we make GrSurfaceContext color/alpha type aware and clean up some read |
| 382 | // pixels code we will make more scenarios work. |
| 383 | |
| 384 | // The GPU code current only does the premul->unpremul conversion, not the reverse. |
| 385 | if (srcInfo.alphaType() == kUnpremul_SkAlphaType && |
| 386 | dstInfo.alphaType() == kPremul_SkAlphaType) { |
| 387 | return ReadSuccessExpectation::kNo; |
| 388 | } |
| 389 | // We don't currently require reading alpha-only surfaces to succeed because of some pessimistic |
| 390 | // caps decisions and alpha/red complexity in GL. |
| 391 | if (SkColorTypeIsAlphaOnly(srcInfo.colorType())) { |
| 392 | return ReadSuccessExpectation::kMaybe; |
| 393 | } |
| 394 | if (!SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) && |
| 395 | SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) { |
| 396 | return ReadSuccessExpectation::kNo; |
| 397 | } |
| 398 | return ReadSuccessExpectation::kYes; |
| 399 | } |
| 400 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 401 | static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 402 | const SkImageInfo& surfaceInfo, BitmapInit lastBitmapInit) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 403 | SkCanvas* canvas = surface->getCanvas(); |
| 404 | fill_src_canvas(canvas); |
| 405 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 406 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 407 | for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 408 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 409 | SkBitmap bmp; |
| 410 | init_bitmap(&bmp, srcRect, bmi, |
| 411 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 412 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 413 | // if the bitmap has pixels allocated before the readPixels, |
| 414 | // note that and fill them with pattern |
| 415 | bool startsWithPixels = !bmp.isNull(); |
| 416 | if (startsWithPixels) { |
| 417 | fill_dst_bmp_with_init_data(&bmp); |
| 418 | } |
| 419 | uint32_t idBefore = surface->generationID(); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 420 | bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 421 | uint32_t idAfter = surface->generationID(); |
| 422 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 423 | // we expect to succeed when the read isn't fully clipped out and the infos are |
| 424 | // compatible. |
| 425 | bool isGPU = SkToBool(surface->getCanvas()->getGrContext()); |
| 426 | auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo, isGPU); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 427 | // determine whether we expected the read to succeed. |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 428 | REPORTER_ASSERT(reporter, check_success_expectation(expectSuccess, success), |
| 429 | "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", |
| 430 | success, surfaceInfo.colorType(), surfaceInfo.alphaType(), |
| 431 | bmp.info().colorType(), bmp.info().alphaType()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 432 | // read pixels should never change the gen id |
| 433 | REPORTER_ASSERT(reporter, idBefore == idAfter); |
| 434 | |
| 435 | if (success || startsWithPixels) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 436 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, |
| 437 | startsWithPixels, surfaceInfo.alphaType()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 438 | } else { |
| 439 | // if we had no pixels beforehand and the readPixels |
| 440 | // failed then our bitmap should still not have pixels |
| 441 | REPORTER_ASSERT(reporter, bmp.isNull()); |
| 442 | } |
| 443 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 447 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 448 | DEF_TEST(ReadPixels, reporter) { |
| 449 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 450 | auto surface(SkSurface::MakeRaster(info)); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 451 | // SW readback fails a premul check when reading back to an unaligned rowbytes. |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 452 | test_readpixels(reporter, surface, info, kLastAligned_BitmapInit); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 453 | } |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 454 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) { |
Robert Phillips | eb4f186 | 2017-06-08 16:38:25 -0400 | [diff] [blame] | 455 | if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType || |
| 456 | ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType || |
| 457 | ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) { |
| 458 | // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs |
| 459 | return; |
| 460 | } |
| 461 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 462 | static const SkImageInfo kImageInfos[] = { |
| 463 | SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
| 464 | SkImageInfo::Make(DEV_W, DEV_H, kBGRA_8888_SkColorType, kPremul_SkAlphaType), |
| 465 | SkImageInfo::Make(DEV_W, DEV_H, kRGB_888x_SkColorType, kOpaque_SkAlphaType), |
| 466 | SkImageInfo::Make(DEV_W, DEV_H, kAlpha_8_SkColorType, kPremul_SkAlphaType), |
| 467 | }; |
| 468 | for (const auto& ii : kImageInfos) { |
| 469 | for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
| 470 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget( |
| 471 | ctxInfo.grContext(), SkBudgeted::kNo, ii, 0, origin, nullptr)); |
| 472 | if (!surface) { |
| 473 | continue; |
| 474 | } |
| 475 | test_readpixels(reporter, surface, ii, kLast_BitmapInit); |
| 476 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 479 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 480 | static void test_readpixels_texture(skiatest::Reporter* reporter, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 481 | sk_sp<GrSurfaceContext> sContext, |
| 482 | const SkImageInfo& surfaceInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 483 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 484 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 485 | for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 486 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 487 | SkBitmap bmp; |
| 488 | init_bitmap(&bmp, srcRect, bmi, |
| 489 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
| 490 | |
| 491 | // if the bitmap has pixels allocated before the readPixels, |
| 492 | // note that and fill them with pattern |
| 493 | bool startsWithPixels = !bmp.isNull(); |
| 494 | // Try doing the read directly from a non-renderable texture |
| 495 | if (startsWithPixels) { |
| 496 | fill_dst_bmp_with_init_data(&bmp); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 497 | uint32_t flags = 0; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 498 | // TODO: These two hacks can go away when the surface context knows the alpha |
| 499 | // type. |
| 500 | // Tell the read to perform an unpremul step since it doesn't know alpha type. |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 501 | if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 502 | flags = GrContextPriv::kUnpremul_PixelOpsFlag; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 503 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 504 | // The surface context doesn't know that the src is opaque. We don't support |
| 505 | // converting non-opaque data to opaque during a read. |
| 506 | if (bmp.alphaType() == kOpaque_SkAlphaType && |
| 507 | surfaceInfo.alphaType() != kOpaque_SkAlphaType) { |
| 508 | continue; |
| 509 | } |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 510 | bool success = sContext->readPixels(bmp.info(), bmp.getPixels(), |
| 511 | bmp.rowBytes(), |
| 512 | srcRect.fLeft, srcRect.fTop, flags); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 513 | auto expectSuccess = |
| 514 | read_should_succeed(srcRect, bmp.info(), surfaceInfo, true); |
| 515 | REPORTER_ASSERT( |
| 516 | reporter, check_success_expectation(expectSuccess, success), |
| 517 | "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", |
| 518 | success, surfaceInfo.colorType(), surfaceInfo.alphaType(), |
| 519 | bmp.info().colorType(), bmp.info().alphaType()); |
| 520 | if (success) { |
| 521 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true, |
| 522 | surfaceInfo.alphaType()); |
| 523 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 529 | |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 530 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) { |
Robert Phillips | eb4f186 | 2017-06-08 16:38:25 -0400 | [diff] [blame] | 531 | if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType || |
| 532 | ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType || |
| 533 | ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) { |
| 534 | // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs |
| 535 | return; |
| 536 | } |
| 537 | |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 538 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 539 | SkBitmap bmp = make_src_bitmap(); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 540 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 541 | // 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] | 542 | for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 543 | for (auto isRT : {false, true}) { |
| 544 | sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData( |
| 545 | context, isRT, DEV_W, DEV_H, bmp.colorType(), origin, bmp.getPixels(), |
| 546 | bmp.rowBytes()); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 547 | sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext( |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 548 | std::move(proxy)); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 549 | auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType); |
| 550 | test_readpixels_texture(reporter, std::move(sContext), info); |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 551 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 554 | |
| 555 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 556 | |
| 557 | static const uint32_t kNumPixels = 5; |
| 558 | |
| 559 | // The five reference pixels are: red, green, blue, white, black. |
| 560 | // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector |
| 561 | // plus a tail pixel. |
| 562 | static const uint32_t rgba[kNumPixels] = { |
| 563 | 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000 |
| 564 | }; |
| 565 | static const uint32_t bgra[kNumPixels] = { |
| 566 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000 |
| 567 | }; |
| 568 | static const uint16_t rgb565[kNumPixels] = { |
| 569 | SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0 |
| 570 | }; |
| 571 | |
| 572 | static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F }; |
| 573 | |
| 574 | static const uint64_t kRed = (uint64_t) SK_Half1 << 0; |
| 575 | static const uint64_t kGreen = (uint64_t) SK_Half1 << 16; |
| 576 | static const uint64_t kBlue = (uint64_t) SK_Half1 << 32; |
| 577 | static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48; |
| 578 | static const uint64_t f16[kNumPixels] = { |
| 579 | kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha |
| 580 | }; |
| 581 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 582 | static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 583 | static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 584 | |
| 585 | static const void* five_reference_pixels(SkColorType colorType) { |
| 586 | switch (colorType) { |
| 587 | case kUnknown_SkColorType: |
| 588 | return nullptr; |
| 589 | case kAlpha_8_SkColorType: |
| 590 | return alpha8; |
| 591 | case kRGB_565_SkColorType: |
| 592 | return rgb565; |
| 593 | case kARGB_4444_SkColorType: |
| 594 | return rgba4444; |
| 595 | case kRGBA_8888_SkColorType: |
| 596 | return rgba; |
| 597 | case kBGRA_8888_SkColorType: |
| 598 | return bgra; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 599 | case kGray_8_SkColorType: |
| 600 | return gray8; |
| 601 | case kRGBA_F16_SkColorType: |
| 602 | return f16; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 603 | default: |
| 604 | return nullptr; // remove me when kIndex_8 is removed from the enum |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | SkASSERT(false); |
| 608 | return nullptr; |
| 609 | } |
| 610 | |
| 611 | static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo, |
| 612 | const SkImageInfo& srcInfo) { |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame^] | 613 | if (!SkImageInfoIsValid(srcInfo)) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 617 | const void* srcPixels = five_reference_pixels(srcInfo.colorType()); |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 618 | SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 619 | sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr); |
| 620 | REPORTER_ASSERT(r, src); |
| 621 | |
| 622 | // Enough space for 5 pixels when color type is F16, more than enough space in other cases. |
| 623 | uint64_t dstPixels[kNumPixels]; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 624 | SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 625 | bool success = src->readPixels(dstPixmap, 0, 0); |
| 626 | REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo)); |
| 627 | |
| 628 | if (success) { |
| 629 | if (kGray_8_SkColorType == srcInfo.colorType() && |
| 630 | kGray_8_SkColorType != dstInfo.colorType()) |
| 631 | { |
| 632 | // This conversion is legal, but we won't get the "reference" pixels since we cannot |
| 633 | // represent colors in kGray8. |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()), |
| 638 | kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType()))); |
| 639 | |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | DEF_TEST(ReadPixels_ValidConversion, reporter) { |
| 644 | const SkColorType kColorTypes[] = { |
| 645 | kUnknown_SkColorType, |
| 646 | kAlpha_8_SkColorType, |
| 647 | kRGB_565_SkColorType, |
| 648 | kARGB_4444_SkColorType, |
| 649 | kRGBA_8888_SkColorType, |
| 650 | kBGRA_8888_SkColorType, |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 651 | kGray_8_SkColorType, |
| 652 | kRGBA_F16_SkColorType, |
| 653 | }; |
| 654 | |
| 655 | const SkAlphaType kAlphaTypes[] = { |
| 656 | kUnknown_SkAlphaType, |
| 657 | kOpaque_SkAlphaType, |
| 658 | kPremul_SkAlphaType, |
| 659 | kUnpremul_SkAlphaType, |
| 660 | }; |
| 661 | |
| 662 | const sk_sp<SkColorSpace> kColorSpaces[] = { |
| 663 | nullptr, |
| 664 | SkColorSpace::MakeSRGB(), |
| 665 | }; |
| 666 | |
| 667 | for (SkColorType dstCT : kColorTypes) { |
| 668 | for (SkAlphaType dstAT: kAlphaTypes) { |
| 669 | for (sk_sp<SkColorSpace> dstCS : kColorSpaces) { |
| 670 | for (SkColorType srcCT : kColorTypes) { |
| 671 | for (SkAlphaType srcAT: kAlphaTypes) { |
| 672 | for (sk_sp<SkColorSpace> srcCS : kColorSpaces) { |
| 673 | if (kRGBA_F16_SkColorType == dstCT && dstCS) { |
Brian Osman | 36703d9 | 2017-12-12 14:09:31 -0500 | [diff] [blame] | 674 | dstCS = dstCS->makeLinearGamma(); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if (kRGBA_F16_SkColorType == srcCT && srcCS) { |
Brian Osman | 36703d9 | 2017-12-12 14:09:31 -0500 | [diff] [blame] | 678 | srcCS = srcCS->makeLinearGamma(); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | test_conversion(reporter, |
| 682 | SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS), |
| 683 | SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS)); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |