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> |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkSurface.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/private/SkColorData.h" |
| 13 | #include "include/private/SkHalf.h" |
| 14 | #include "include/private/SkImageInfoPriv.h" |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 15 | #include "include/utils/SkNWayCanvas.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 16 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkMathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrImageInfo.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 20 | #include "tests/Test.h" |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 21 | #include "tests/TestUtils.h" |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/gpu/GrContextFactory.h" |
| 24 | #include "tools/gpu/ProxyUtils.h" |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 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 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 69 | // TODO: Make this consider both ATs |
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; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 83 | case kRGB_888x_SkColorType: // fallthrough |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 84 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 85 | r = static_cast<U8CPU>(c[0]); |
| 86 | g = static_cast<U8CPU>(c[1]); |
| 87 | b = static_cast<U8CPU>(c[2]); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 88 | // 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] | 89 | a = static_cast<U8CPU>(c[3]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 90 | break; |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 91 | default: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 92 | SkDEBUGFAIL("Unexpected colortype"); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 93 | return 0; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 94 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 95 | |
| 96 | if (*doUnpremul) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 97 | r = SkMulDiv255Ceiling(r, a); |
| 98 | g = SkMulDiv255Ceiling(g, a); |
| 99 | b = SkMulDiv255Ceiling(b, a); |
| 100 | } |
| 101 | return SkPackARGB32(a, r, g, b); |
| 102 | } |
| 103 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 104 | static SkBitmap make_src_bitmap() { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 105 | static SkBitmap bmp; |
| 106 | if (bmp.isNull()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 107 | bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 108 | intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
| 109 | for (int y = 0; y < DEV_H; ++y) { |
| 110 | for (int x = 0; x < DEV_W; ++x) { |
| 111 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 112 | *pixel = get_src_color(x, y); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 116 | return bmp; |
| 117 | } |
| 118 | |
| 119 | static void fill_src_canvas(SkCanvas* canvas) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 120 | canvas->save(); |
| 121 | canvas->setMatrix(SkMatrix::I()); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 122 | canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 123 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 124 | paint.setBlendMode(SkBlendMode::kSrc); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 125 | canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 126 | canvas->restore(); |
| 127 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 129 | static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 130 | int w = bitmap->width(); |
| 131 | int h = bitmap->height(); |
| 132 | intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
| 133 | for (int y = 0; y < h; ++y) { |
| 134 | for (int x = 0; x < w; ++x) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 135 | SkPMColor initColor = get_dst_bmp_init_color(x, y, w); |
| 136 | if (kAlpha_8_SkColorType == bitmap->colorType()) { |
| 137 | uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x); |
| 138 | *alpha = SkGetPackedA32(initColor); |
| 139 | } else { |
| 140 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); |
| 141 | *pixel = initColor; |
| 142 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 147 | static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 148 | if (!didPremulConversion) { |
| 149 | return a == b; |
| 150 | } |
| 151 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 152 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 153 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 154 | int32_t aB = SkGetPackedB32(a); |
| 155 | |
| 156 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 157 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 158 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 159 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 160 | |
| 161 | return aA == bA && |
| 162 | SkAbs32(aR - bR) <= 1 && |
| 163 | SkAbs32(aG - bG) <= 1 && |
| 164 | SkAbs32(aB - bB) <= 1; |
| 165 | } |
| 166 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 167 | // checks the bitmap contains correct pixels after the readPixels |
| 168 | // if the bitmap was prefilled with pixels it checks that these weren't |
| 169 | // overwritten in the area outside the readPixels. |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 170 | static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y, |
| 171 | bool checkSurfacePixels, bool checkBitmapPixels, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 172 | SkImageInfo surfaceInfo) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 173 | SkAlphaType bmpAT = bitmap.alphaType(); |
| 174 | SkColorType bmpCT = bitmap.colorType(); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 175 | SkASSERT(!bitmap.isNull()); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 176 | SkASSERT(checkSurfacePixels || 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 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 186 | if (kAlpha_8_SkColorType == bmpCT) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 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)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 194 | if (checkSurfacePixels) { |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 195 | uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType) |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 196 | ? 0xFF |
| 197 | : SkGetPackedA32(get_src_color(devx, devy)); |
| 198 | if (surfaceAlpha != *alpha) { |
| 199 | ERRORF(reporter, |
| 200 | "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ", |
| 201 | bx, by, surfaceAlpha, *alpha); |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | } |
| 205 | } else if (checkBitmapPixels) { |
| 206 | uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw)); |
| 207 | if (origDstAlpha != *alpha) { |
| 208 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 209 | "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha); |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return true; |
| 216 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 217 | for (int by = 0; by < bh; ++by) { |
| 218 | for (int bx = 0; bx < bw; ++bx) { |
| 219 | int devx = bx + srcRect.fLeft; |
| 220 | int devy = by + srcRect.fTop; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 221 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 222 | const uint32_t* pixel = bitmap.getAddr32(bx, by); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 223 | |
| 224 | if (clippedSrcRect.contains(devx, devy)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 225 | if (checkSurfacePixels) { |
| 226 | SkPMColor surfacePMColor = get_src_color(devx, devy); |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 227 | if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) { |
| 228 | surfacePMColor &= 0xFF000000; |
| 229 | } |
| 230 | if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 231 | surfacePMColor |= 0xFF000000; |
| 232 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 233 | bool didPremul; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 234 | SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul); |
| 235 | if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) { |
| 236 | ERRORF(reporter, |
| 237 | "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. " |
| 238 | "Readback was unpremul: %d", |
| 239 | bx, by, surfacePMColor, pmPixel, didPremul); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 240 | return false; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 241 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 242 | } |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 243 | } else if (checkBitmapPixels) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 244 | uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw); |
| 245 | if (origDstPixel != *pixel) { |
| 246 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 247 | "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 248 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 253 | return true; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | enum BitmapInit { |
| 257 | kFirstBitmapInit = 0, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 258 | |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 259 | kTight_BitmapInit = kFirstBitmapInit, |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 260 | kRowBytes_BitmapInit, |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 261 | kRowBytesOdd_BitmapInit, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 262 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 263 | kLastAligned_BitmapInit = kRowBytes_BitmapInit, |
Brian Salomon | a64afd6 | 2016-02-01 16:44:22 -0500 | [diff] [blame] | 264 | |
| 265 | #if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 266 | kLast_BitmapInit = kRowBytesOdd_BitmapInit |
Brian Salomon | a64afd6 | 2016-02-01 16:44:22 -0500 | [diff] [blame] | 267 | #else |
| 268 | kLast_BitmapInit = kLastAligned_BitmapInit |
| 269 | #endif |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 272 | static BitmapInit nextBMI(BitmapInit bmi) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 273 | int x = bmi; |
| 274 | return static_cast<BitmapInit>(++x); |
| 275 | } |
| 276 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 277 | static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct, |
| 278 | SkAlphaType at) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 279 | SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 280 | size_t rowBytes = 0; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 281 | switch (init) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 282 | case kTight_BitmapInit: |
| 283 | break; |
| 284 | case kRowBytes_BitmapInit: |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 285 | rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 286 | break; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 287 | case kRowBytesOdd_BitmapInit: |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 288 | rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 289 | break; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 290 | default: |
| 291 | SkASSERT(0); |
| 292 | break; |
| 293 | } |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 294 | bitmap->allocPixels(info, rowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 295 | } |
| 296 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 297 | static const struct { |
| 298 | SkColorType fColorType; |
| 299 | SkAlphaType fAlphaType; |
| 300 | } gReadPixelsConfigs[] = { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 301 | {kRGBA_8888_SkColorType, kPremul_SkAlphaType}, |
| 302 | {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 303 | {kRGB_888x_SkColorType, kOpaque_SkAlphaType}, |
| 304 | {kBGRA_8888_SkColorType, kPremul_SkAlphaType}, |
| 305 | {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 306 | {kAlpha_8_SkColorType, kPremul_SkAlphaType}, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 307 | }; |
| 308 | const SkIRect gReadPixelsTestRects[] = { |
| 309 | // entire thing |
| 310 | DEV_RECT, |
| 311 | // larger on all sides |
| 312 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 313 | // fully contained |
| 314 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 315 | // outside top left |
| 316 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 317 | // touching top left corner |
| 318 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 319 | // overlapping top left corner |
| 320 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 321 | // overlapping top left and top right corners |
| 322 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 323 | // touching entire top edge |
| 324 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 325 | // overlapping top right corner |
| 326 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 327 | // contained in x, overlapping top edge |
| 328 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 329 | // outside top right corner |
| 330 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 331 | // touching top right corner |
| 332 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 333 | // overlapping top left and bottom left corners |
| 334 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 335 | // touching entire left edge |
| 336 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 337 | // overlapping bottom left corner |
| 338 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 339 | // contained in y, overlapping left edge |
| 340 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 341 | // outside bottom left corner |
| 342 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 343 | // touching bottom left corner |
| 344 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 345 | // overlapping bottom left and bottom right corners |
| 346 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 347 | // touching entire left edge |
| 348 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 349 | // overlapping bottom right corner |
| 350 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 351 | // overlapping top right and bottom right corners |
| 352 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 353 | }; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 354 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 355 | bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo, |
| 356 | const SkImageInfo& srcInfo) { |
| 357 | return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 358 | } |
| 359 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 360 | static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 361 | const SkImageInfo& surfaceInfo, BitmapInit lastBitmapInit) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 362 | SkCanvas* canvas = surface->getCanvas(); |
| 363 | fill_src_canvas(canvas); |
| 364 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 365 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 366 | for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 367 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 368 | SkBitmap bmp; |
| 369 | init_bitmap(&bmp, srcRect, bmi, |
| 370 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 371 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 372 | // if the bitmap has pixels allocated before the readPixels, |
| 373 | // note that and fill them with pattern |
| 374 | bool startsWithPixels = !bmp.isNull(); |
| 375 | if (startsWithPixels) { |
| 376 | fill_dst_bmp_with_init_data(&bmp); |
| 377 | } |
| 378 | uint32_t idBefore = surface->generationID(); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 379 | bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 380 | uint32_t idAfter = surface->generationID(); |
| 381 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 382 | // we expect to succeed when the read isn't fully clipped out and the infos are |
| 383 | // compatible. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 384 | bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 385 | // determine whether we expected the read to succeed. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 386 | REPORTER_ASSERT(reporter, expectSuccess == success, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 387 | "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", |
| 388 | success, surfaceInfo.colorType(), surfaceInfo.alphaType(), |
| 389 | bmp.info().colorType(), bmp.info().alphaType()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 390 | // read pixels should never change the gen id |
| 391 | REPORTER_ASSERT(reporter, idBefore == idAfter); |
| 392 | |
| 393 | if (success || startsWithPixels) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 394 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 395 | startsWithPixels, surfaceInfo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 396 | } else { |
| 397 | // if we had no pixels beforehand and the readPixels |
| 398 | // failed then our bitmap should still not have pixels |
| 399 | REPORTER_ASSERT(reporter, bmp.isNull()); |
| 400 | } |
| 401 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 405 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 406 | DEF_TEST(ReadPixels, reporter) { |
| 407 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 408 | auto surface(SkSurface::MakeRaster(info)); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 409 | // 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] | 410 | test_readpixels(reporter, surface, info, kLastAligned_BitmapInit); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 411 | } |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 412 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 413 | static const SkImageInfo kImageInfos[] = { |
| 414 | SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
| 415 | SkImageInfo::Make(DEV_W, DEV_H, kBGRA_8888_SkColorType, kPremul_SkAlphaType), |
| 416 | SkImageInfo::Make(DEV_W, DEV_H, kRGB_888x_SkColorType, kOpaque_SkAlphaType), |
| 417 | SkImageInfo::Make(DEV_W, DEV_H, kAlpha_8_SkColorType, kPremul_SkAlphaType), |
| 418 | }; |
| 419 | for (const auto& ii : kImageInfos) { |
| 420 | for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
| 421 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget( |
| 422 | ctxInfo.grContext(), SkBudgeted::kNo, ii, 0, origin, nullptr)); |
| 423 | if (!surface) { |
| 424 | continue; |
| 425 | } |
| 426 | test_readpixels(reporter, surface, ii, kLast_BitmapInit); |
| 427 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 428 | } |
| 429 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 430 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 431 | static void test_readpixels_texture(skiatest::Reporter* reporter, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 432 | std::unique_ptr<GrSurfaceContext> sContext, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 433 | const SkImageInfo& surfaceInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 434 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 435 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 436 | for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 437 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 438 | SkBitmap bmp; |
| 439 | init_bitmap(&bmp, srcRect, bmi, |
| 440 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
| 441 | |
| 442 | // if the bitmap has pixels allocated before the readPixels, |
| 443 | // note that and fill them with pattern |
| 444 | bool startsWithPixels = !bmp.isNull(); |
| 445 | // Try doing the read directly from a non-renderable texture |
| 446 | if (startsWithPixels) { |
| 447 | fill_dst_bmp_with_init_data(&bmp); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 448 | bool success = sContext->readPixels(bmp.info(), bmp.getPixels(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 449 | bmp.rowBytes(), {srcRect.fLeft, srcRect.fTop}); |
| 450 | auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 451 | REPORTER_ASSERT( |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 452 | reporter, expectSuccess == success, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 453 | "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", |
| 454 | success, surfaceInfo.colorType(), surfaceInfo.alphaType(), |
| 455 | bmp.info().colorType(), bmp.info().alphaType()); |
| 456 | if (success) { |
| 457 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 458 | surfaceInfo); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 459 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 465 | |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 466 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 467 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 468 | SkBitmap bmp = make_src_bitmap(); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 469 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 470 | // 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] | 471 | for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 472 | for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 473 | sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData( |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 474 | context, renderable, origin, bmp.info(), bmp.getPixels(), bmp.rowBytes()); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 475 | auto sContext = context->priv().makeWrappedSurfaceContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 476 | std::move(proxy), SkColorTypeToGrColorType(bmp.colorType()), |
| 477 | kPremul_SkAlphaType); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 478 | auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType); |
| 479 | test_readpixels_texture(reporter, std::move(sContext), info); |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 480 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 481 | } |
| 482 | } |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 483 | |
| 484 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 485 | |
| 486 | static const uint32_t kNumPixels = 5; |
| 487 | |
| 488 | // The five reference pixels are: red, green, blue, white, black. |
| 489 | // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector |
| 490 | // plus a tail pixel. |
| 491 | static const uint32_t rgba[kNumPixels] = { |
| 492 | 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000 |
| 493 | }; |
| 494 | static const uint32_t bgra[kNumPixels] = { |
| 495 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000 |
| 496 | }; |
| 497 | static const uint16_t rgb565[kNumPixels] = { |
| 498 | SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0 |
| 499 | }; |
| 500 | |
| 501 | static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F }; |
| 502 | |
| 503 | static const uint64_t kRed = (uint64_t) SK_Half1 << 0; |
| 504 | static const uint64_t kGreen = (uint64_t) SK_Half1 << 16; |
| 505 | static const uint64_t kBlue = (uint64_t) SK_Half1 << 32; |
| 506 | static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48; |
| 507 | static const uint64_t f16[kNumPixels] = { |
| 508 | kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha |
| 509 | }; |
| 510 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 511 | static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 512 | static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 513 | |
| 514 | static const void* five_reference_pixels(SkColorType colorType) { |
| 515 | switch (colorType) { |
| 516 | case kUnknown_SkColorType: |
| 517 | return nullptr; |
| 518 | case kAlpha_8_SkColorType: |
| 519 | return alpha8; |
| 520 | case kRGB_565_SkColorType: |
| 521 | return rgb565; |
| 522 | case kARGB_4444_SkColorType: |
| 523 | return rgba4444; |
| 524 | case kRGBA_8888_SkColorType: |
| 525 | return rgba; |
| 526 | case kBGRA_8888_SkColorType: |
| 527 | return bgra; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 528 | case kGray_8_SkColorType: |
| 529 | return gray8; |
| 530 | case kRGBA_F16_SkColorType: |
| 531 | return f16; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 532 | default: |
Leon Scroggins III | d6832d0 | 2018-09-04 11:10:04 -0400 | [diff] [blame] | 533 | return nullptr; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | SkASSERT(false); |
| 537 | return nullptr; |
| 538 | } |
| 539 | |
| 540 | static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo, |
| 541 | const SkImageInfo& srcInfo) { |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 542 | if (!SkImageInfoIsValid(srcInfo)) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 546 | const void* srcPixels = five_reference_pixels(srcInfo.colorType()); |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 547 | SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 548 | sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr); |
| 549 | REPORTER_ASSERT(r, src); |
| 550 | |
| 551 | // Enough space for 5 pixels when color type is F16, more than enough space in other cases. |
| 552 | uint64_t dstPixels[kNumPixels]; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 553 | SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 554 | bool success = src->readPixels(dstPixmap, 0, 0); |
| 555 | REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo)); |
| 556 | |
| 557 | if (success) { |
| 558 | if (kGray_8_SkColorType == srcInfo.colorType() && |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 559 | kGray_8_SkColorType != dstInfo.colorType()) { |
| 560 | // TODO: test (r,g,b) == (gray,gray,gray)? |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | if (kGray_8_SkColorType == dstInfo.colorType() && |
| 565 | kGray_8_SkColorType != srcInfo.colorType()) { |
| 566 | // TODO: test gray = luminance? |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | if (kAlpha_8_SkColorType == srcInfo.colorType() && |
| 571 | kAlpha_8_SkColorType != dstInfo.colorType()) { |
| 572 | // TODO: test output = black with this alpha? |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 573 | return; |
| 574 | } |
| 575 | |
| 576 | REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()), |
| 577 | kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType()))); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | DEF_TEST(ReadPixels_ValidConversion, reporter) { |
| 582 | const SkColorType kColorTypes[] = { |
| 583 | kUnknown_SkColorType, |
| 584 | kAlpha_8_SkColorType, |
| 585 | kRGB_565_SkColorType, |
| 586 | kARGB_4444_SkColorType, |
| 587 | kRGBA_8888_SkColorType, |
| 588 | kBGRA_8888_SkColorType, |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 589 | kGray_8_SkColorType, |
| 590 | kRGBA_F16_SkColorType, |
| 591 | }; |
| 592 | |
| 593 | const SkAlphaType kAlphaTypes[] = { |
| 594 | kUnknown_SkAlphaType, |
| 595 | kOpaque_SkAlphaType, |
| 596 | kPremul_SkAlphaType, |
| 597 | kUnpremul_SkAlphaType, |
| 598 | }; |
| 599 | |
| 600 | const sk_sp<SkColorSpace> kColorSpaces[] = { |
| 601 | nullptr, |
| 602 | SkColorSpace::MakeSRGB(), |
| 603 | }; |
| 604 | |
| 605 | for (SkColorType dstCT : kColorTypes) { |
| 606 | for (SkAlphaType dstAT: kAlphaTypes) { |
| 607 | for (sk_sp<SkColorSpace> dstCS : kColorSpaces) { |
| 608 | for (SkColorType srcCT : kColorTypes) { |
| 609 | for (SkAlphaType srcAT: kAlphaTypes) { |
| 610 | for (sk_sp<SkColorSpace> srcCS : kColorSpaces) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 611 | test_conversion(reporter, |
| 612 | SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS), |
| 613 | SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS)); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 621 | |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 622 | static int min_rgb_channel_bits(SkColorType ct) { |
| 623 | switch (ct) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 624 | case kUnknown_SkColorType: return 0; |
| 625 | case kAlpha_8_SkColorType: return 8; |
| 626 | case kA16_unorm_SkColorType: return 16; |
| 627 | case kA16_float_SkColorType: return 16; |
| 628 | case kRGB_565_SkColorType: return 5; |
| 629 | case kARGB_4444_SkColorType: return 4; |
| 630 | case kR8G8_unorm_SkColorType: return 8; |
| 631 | case kR16G16_unorm_SkColorType: return 16; |
| 632 | case kR16G16_float_SkColorType: return 16; |
| 633 | case kRGBA_8888_SkColorType: return 8; |
| 634 | case kRGB_888x_SkColorType: return 8; |
| 635 | case kBGRA_8888_SkColorType: return 8; |
| 636 | case kRGBA_1010102_SkColorType: return 10; |
| 637 | case kRGB_101010x_SkColorType: return 10; |
| 638 | case kGray_8_SkColorType: return 8; // counting gray as "rgb" |
| 639 | case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa |
| 640 | case kRGBA_F16_SkColorType: return 10; // just counting the mantissa |
| 641 | case kRGBA_F32_SkColorType: return 23; // just counting the mantissa |
| 642 | case kR16G16B16A16_unorm_SkColorType: return 16; |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 643 | } |
| 644 | SK_ABORT("Unexpected color type."); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 645 | } |
| 646 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 647 | namespace { |
| 648 | struct AsyncContext { |
| 649 | bool fCalled = false; |
| 650 | std::unique_ptr<const SkSurface::AsyncReadResult> fResult; |
| 651 | }; |
| 652 | } // anonymous namespace |
| 653 | |
| 654 | // Making this a lambda in the test functions caused: |
| 655 | // "error: cannot compile this forwarded non-trivially copyable parameter yet" |
| 656 | // on x86/Win/Clang bot, referring to 'result'. |
| 657 | static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) { |
| 658 | auto context = static_cast<AsyncContext*>(c); |
| 659 | context->fResult = std::move(result); |
| 660 | context->fCalled = true; |
| 661 | }; |
| 662 | |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 663 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) { |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 664 | struct LegacyContext { |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 665 | SkPixmap* fPixmap = nullptr; |
| 666 | bool fSuceeded = false; |
| 667 | bool fCalled = false; |
| 668 | }; |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 669 | auto legacy_callback = [](SkSurface::ReleaseContext context, const void* data, |
| 670 | size_t rowBytes) { |
| 671 | auto* pm = static_cast<LegacyContext*>(context)->fPixmap; |
| 672 | static_cast<LegacyContext*>(context)->fCalled = true; |
| 673 | if ((static_cast<LegacyContext*>(context)->fSuceeded = SkToBool(data))) { |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 674 | auto dst = static_cast<char*>(pm->writable_addr()); |
| 675 | const auto* src = static_cast<const char*>(data); |
| 676 | for (int y = 0; y < pm->height(); ++y, src += rowBytes, dst += pm->rowBytes()) { |
| 677 | memcpy(dst, src, pm->width() * SkColorTypeBytesPerPixel(pm->colorType())); |
| 678 | } |
| 679 | } |
| 680 | }; |
| 681 | for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 682 | static constexpr int kW = 16; |
| 683 | static constexpr int kH = 16; |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 684 | for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) { |
| 685 | auto surfCT = static_cast<SkColorType>(sct); |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 686 | auto info = SkImageInfo::Make(kW, kH, surfCT, kPremul_SkAlphaType, |
| 687 | SkColorSpace::MakeSRGB()); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 688 | auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info, 1, |
| 689 | origin, nullptr); |
| 690 | if (!surf) { |
| 691 | continue; |
| 692 | } |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 693 | auto refSurf = SkSurface::MakeRaster(info); |
| 694 | SkNWayCanvas nway(info.width(), info.height()); |
| 695 | nway.addCanvas(surf->getCanvas()); |
| 696 | nway.addCanvas(refSurf->getCanvas()); |
| 697 | |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 698 | float d = std::sqrt((float)surf->width() * surf->width() + |
| 699 | (float)surf->height() * surf->height()); |
| 700 | for (int j = 0; j < surf->height(); ++j) { |
| 701 | for (int i = 0; i < surf->width(); ++i) { |
| 702 | float r = i / (float)surf->width(); |
| 703 | float g = 1.f - i / (float)surf->height(); |
| 704 | float b = std::sqrt((float)i * i + (float)j * j) / d; |
| 705 | SkPaint paint; |
| 706 | paint.setColor4f(SkColor4f{r, g, b, 1.f}, nullptr); |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 707 | nway.drawRect(SkRect::MakeXYWH(i, j, 1, 1), paint); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 708 | } |
| 709 | } |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 710 | for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) { |
| 711 | auto readCT = static_cast<SkColorType>(rct); |
| 712 | |
| 713 | for (const sk_sp<SkColorSpace>& readCS : |
| 714 | {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) { |
| 715 | auto refImg = refSurf->makeImageSnapshot()->makeColorTypeAndColorSpace(readCT, |
| 716 | readCS); |
| 717 | // Test full size, partial, empty, and too wide rects. |
| 718 | for (const auto& rect : {SkIRect::MakeWH(kW, kH), |
| 719 | SkIRect::MakeLTRB(1, 2, kW - 3, kH - 4), |
| 720 | SkIRect::MakeXYWH(1, 1, 0, 0), |
| 721 | SkIRect::MakeWH(kW + 1, kH / 2)}) { |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 722 | for (bool legacy : {false, true}) { |
| 723 | SkPixmap result; |
| 724 | std::unique_ptr<char[]> tempPixels; |
| 725 | info = SkImageInfo::Make(rect.size(), readCT, kPremul_SkAlphaType, |
| 726 | readCS); |
| 727 | // Rescale quality and linearity don't matter since we're doing a non- |
| 728 | // scaling readback. |
| 729 | static constexpr auto kQuality = kNone_SkFilterQuality; |
| 730 | static constexpr auto kGamma = SkSurface::RescaleGamma::kSrc; |
| 731 | bool succeeded = false; |
| 732 | // This holds the pixel results and so must live until comparisons are |
| 733 | // finished. |
| 734 | AsyncContext asyncContext; |
| 735 | if (legacy) { |
| 736 | LegacyContext context; |
| 737 | tempPixels.reset(new char[info.computeMinByteSize()]); |
| 738 | result.reset(info, tempPixels.get(), info.minRowBytes()); |
| 739 | memset(result.writable_addr(), 0xAB, info.computeMinByteSize()); |
| 740 | context.fPixmap = &result; |
| 741 | surf->asyncRescaleAndReadPixels(info, rect, kGamma, kQuality, |
| 742 | legacy_callback, &context); |
| 743 | while (!context.fCalled) { |
| 744 | ctxInfo.grContext()->checkAsyncWorkCompletion(); |
| 745 | } |
| 746 | succeeded = context.fSuceeded; |
| 747 | } else { |
| 748 | surf->asyncRescaleAndReadPixels(info, rect, kGamma, kQuality, |
| 749 | async_callback, &asyncContext); |
| 750 | while (!asyncContext.fCalled) { |
| 751 | ctxInfo.grContext()->checkAsyncWorkCompletion(); |
| 752 | } |
| 753 | if (asyncContext.fResult) { |
| 754 | int count = asyncContext.fResult->count(); |
| 755 | if (count == 1) { |
| 756 | succeeded = true; |
| 757 | result.reset(info, |
| 758 | asyncContext.fResult->data(0), |
| 759 | asyncContext.fResult->rowBytes(0)); |
| 760 | } else { |
| 761 | ERRORF(reporter, "Unexpected AsyncResult::count(): %d", |
| 762 | count); |
| 763 | continue; |
| 764 | } |
| 765 | } |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 766 | } |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 767 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 768 | if (rect.isEmpty() || !SkIRect::MakeWH(kW, kH).contains(rect)) { |
| 769 | REPORTER_ASSERT(reporter, !succeeded); |
| 770 | } |
| 771 | |
| 772 | bool didCSConversion = !SkColorSpace::Equals( |
| 773 | readCS.get(), surf->imageInfo().colorSpace()); |
| 774 | |
| 775 | if (succeeded) { |
| 776 | REPORTER_ASSERT(reporter, |
| 777 | readCT != kUnknown_SkColorType && !rect.isEmpty()); |
| 778 | } else { |
| 779 | // TODO: Support reading to kGray, support kRGB_101010x at all in |
| 780 | // GPU. |
| 781 | auto surfBounds = SkIRect::MakeWH(surf->width(), surf->height()); |
| 782 | if (readCT != kUnknown_SkColorType && |
| 783 | readCT != kGray_8_SkColorType && |
| 784 | readCT != kRGB_101010x_SkColorType && !rect.isEmpty() && |
| 785 | surfBounds.contains(rect)) { |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 786 | ERRORF(reporter, |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 787 | "Async read failed. Surf Color Type: %s, Read CT: %s," |
| 788 | "Rect [%d, %d, %d, %d], origin: %d, CS conversion: %d\n", |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 789 | ToolUtils::colortype_name(surfCT), |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 790 | ToolUtils::colortype_name(readCT), rect.fLeft, rect.fTop, |
| 791 | rect.fRight, rect.fBottom, origin, didCSConversion); |
| 792 | } |
| 793 | continue; |
| 794 | } |
| 795 | SkPixmap ref; |
| 796 | refImg->peekPixels(&ref); |
| 797 | SkAssertResult(ref.extractSubset(&ref, rect)); |
| 798 | |
| 799 | // A CS conversion allows a 3 value difference and otherwise a 2 value |
| 800 | // difference. Note that sometimes read back on GPU can be lossy even |
| 801 | // when there no conversion at all because GPU->CPU read may go to a |
| 802 | // lower bit depth format and then be promoted back to the original |
| 803 | // type. For example, GL ES cannot read to 1010102, so we go through |
| 804 | // 8888. |
| 805 | float numer = didCSConversion ? 3.f : 2.f; |
| 806 | int rgbBits = std::min({min_rgb_channel_bits(readCT), |
| 807 | min_rgb_channel_bits(surfCT), 8}); |
| 808 | float tol = numer / (1 << rgbBits); |
| 809 | const float tols[4] = {tol, tol, tol, 0}; |
| 810 | auto error = std::function< |
| 811 | ComparePixmapsErrorReporter>([&](int x, int y, |
| 812 | const float diffs[4]) { |
| 813 | SkASSERT(x >= 0 && y >= 0); |
| 814 | ERRORF(reporter, |
| 815 | "Surf Color Type: %s, Read CT: %s, Rect [%d, %d, %d, %d]" |
| 816 | ", origin: %d, CS conversion: %d\n" |
| 817 | "Error at %d, %d. Diff in floats: (%f, %f, %f %f)", |
| 818 | ToolUtils::colortype_name(surfCT), |
| 819 | ToolUtils::colortype_name(readCT), rect.fLeft, rect.fTop, |
| 820 | rect.fRight, rect.fBottom, origin, didCSConversion, x, y, |
| 821 | diffs[0], diffs[1], diffs[2], diffs[3]); |
| 822 | }); |
| 823 | compare_pixels(ref, result, tols, error); |
| 824 | } |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 825 | } |
Brian Salomon | 6fc04f8 | 2019-10-02 19:11:55 -0400 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | } |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 831 | |
| 832 | DEF_GPUTEST(AsyncReadPixelsContextShutdown, reporter, options) { |
| 833 | const auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, |
| 834 | SkColorSpace::MakeSRGB()); |
| 835 | enum class ShutdownSequence { |
| 836 | kFreeResult_DestroyContext, |
| 837 | kDestroyContext_FreeResult, |
| 838 | kFreeResult_ReleaseAndAbandon_DestroyContext, |
| 839 | kFreeResult_Abandon_DestroyContext, |
| 840 | kReleaseAndAbandon_FreeResult_DestroyContext, |
| 841 | kAbandon_FreeResult_DestroyContext, |
| 842 | kReleaseAndAbandon_DestroyContext_FreeResult, |
| 843 | kAbandon_DestroyContext_FreeResult, |
| 844 | }; |
| 845 | for (int t = 0; t < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++t) { |
| 846 | auto type = static_cast<sk_gpu_test::GrContextFactory::ContextType>(t); |
| 847 | for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext, |
| 848 | ShutdownSequence::kDestroyContext_FreeResult, |
| 849 | ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext, |
| 850 | ShutdownSequence::kFreeResult_Abandon_DestroyContext, |
| 851 | ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext, |
| 852 | ShutdownSequence::kAbandon_FreeResult_DestroyContext, |
| 853 | ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult, |
| 854 | ShutdownSequence::kAbandon_DestroyContext_FreeResult}) { |
| 855 | // Vulkan context abandoning without resource release has issues outside of the scope of |
| 856 | // this test. |
| 857 | if (type == sk_gpu_test::GrContextFactory::kVulkan_ContextType && |
| 858 | (sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext || |
| 859 | sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult || |
| 860 | sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext)) { |
| 861 | continue; |
| 862 | } |
| 863 | for (bool yuv : {false, true}) { |
| 864 | sk_gpu_test::GrContextFactory factory(options); |
| 865 | auto context = factory.get(type); |
| 866 | if (!context) { |
| 867 | continue; |
| 868 | } |
| 869 | // This test is only meaningful for contexts that support transfer buffers. |
| 870 | if (!context->priv().caps()->transferBufferSupport()) { |
| 871 | continue; |
| 872 | } |
| 873 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 1, nullptr); |
| 874 | if (!surf) { |
| 875 | continue; |
| 876 | } |
| 877 | AsyncContext cbContext; |
| 878 | if (yuv) { |
| 879 | surf->asyncRescaleAndReadPixelsYUV420( |
| 880 | kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(), ii.bounds(), |
| 881 | ii.dimensions(), SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality, |
| 882 | &async_callback, &cbContext); |
| 883 | } else { |
| 884 | surf->asyncRescaleAndReadPixels(ii, ii.bounds(), SkSurface::RescaleGamma::kSrc, |
| 885 | kNone_SkFilterQuality, &async_callback, |
| 886 | &cbContext); |
| 887 | } |
| 888 | while (!cbContext.fCalled) { |
| 889 | context->checkAsyncWorkCompletion(); |
| 890 | } |
| 891 | if (!cbContext.fResult) { |
| 892 | ERRORF(reporter, "Callback failed on %s. is YUV: %d", |
| 893 | sk_gpu_test::GrContextFactory::ContextTypeName(type), yuv); |
| 894 | continue; |
| 895 | } |
| 896 | // The real test is that we don't crash, get Vulkan validation errors, etc, during |
| 897 | // this shutdown sequence. |
| 898 | switch (sequence) { |
| 899 | case ShutdownSequence::kFreeResult_DestroyContext: |
| 900 | case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext: |
| 901 | case ShutdownSequence::kFreeResult_Abandon_DestroyContext: |
| 902 | break; |
| 903 | case ShutdownSequence::kDestroyContext_FreeResult: |
| 904 | factory.destroyContexts(); |
| 905 | break; |
| 906 | case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext: |
| 907 | factory.releaseResourcesAndAbandonContexts(); |
| 908 | break; |
| 909 | case ShutdownSequence::kAbandon_FreeResult_DestroyContext: |
| 910 | factory.abandonContexts(); |
| 911 | break; |
| 912 | case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult: |
| 913 | factory.releaseResourcesAndAbandonContexts(); |
| 914 | factory.destroyContexts(); |
| 915 | break; |
| 916 | case ShutdownSequence::kAbandon_DestroyContext_FreeResult: |
| 917 | factory.abandonContexts(); |
| 918 | factory.destroyContexts(); |
| 919 | break; |
| 920 | } |
| 921 | cbContext.fResult.reset(); |
| 922 | switch (sequence) { |
| 923 | case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext: |
| 924 | factory.releaseResourcesAndAbandonContexts(); |
| 925 | break; |
| 926 | case ShutdownSequence::kFreeResult_Abandon_DestroyContext: |
| 927 | factory.abandonContexts(); |
| 928 | break; |
| 929 | case ShutdownSequence::kFreeResult_DestroyContext: |
| 930 | case ShutdownSequence::kDestroyContext_FreeResult: |
| 931 | case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext: |
| 932 | case ShutdownSequence::kAbandon_FreeResult_DestroyContext: |
| 933 | case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult: |
| 934 | case ShutdownSequence::kAbandon_DestroyContext_FreeResult: |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |