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 | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 11 | #include "include/effects/SkGradientShader.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/private/SkColorData.h" |
| 14 | #include "include/private/SkHalf.h" |
| 15 | #include "include/private/SkImageInfoPriv.h" |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 16 | #include "include/utils/SkNWayCanvas.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 17 | #include "src/core/SkAutoPixmapStorage.h" |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 18 | #include "src/core/SkConvertPixels.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/core/SkMathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrImageInfo.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 22 | #include "tests/Test.h" |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 23 | #include "tests/TestUtils.h" |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 24 | #include "tools/ToolUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/gpu/GrContextFactory.h" |
| 26 | #include "tools/gpu/ProxyUtils.h" |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 28 | static const int DEV_W = 100, DEV_H = 100; |
| 29 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 30 | 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] | 31 | DEV_H * SK_Scalar1); |
| 32 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 33 | static SkPMColor get_src_color(int x, int y) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 34 | SkASSERT(x >= 0 && x < DEV_W); |
| 35 | SkASSERT(y >= 0 && y < DEV_H); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 36 | |
| 37 | U8CPU r = x; |
| 38 | U8CPU g = y; |
| 39 | U8CPU b = 0xc; |
| 40 | |
| 41 | U8CPU a = 0xff; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 42 | switch ((x+y) % 5) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 43 | case 0: |
| 44 | a = 0xff; |
| 45 | break; |
| 46 | case 1: |
| 47 | a = 0x80; |
| 48 | break; |
| 49 | case 2: |
| 50 | a = 0xCC; |
| 51 | break; |
| 52 | case 4: |
| 53 | a = 0x01; |
| 54 | break; |
| 55 | case 3: |
| 56 | a = 0x00; |
| 57 | break; |
| 58 | } |
| 59 | return SkPremultiplyARGBInline(a, r, g, b); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 60 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 61 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 62 | 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] | 63 | int n = y * w + x; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 65 | U8CPU b = n & 0xff; |
| 66 | U8CPU g = (n >> 8) & 0xff; |
| 67 | U8CPU r = (n >> 16) & 0xff; |
| 68 | return SkPackARGB32(0xff, r, g , b); |
| 69 | } |
| 70 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 71 | // TODO: Make this consider both ATs |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 72 | static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr, |
| 73 | bool* doUnpremul) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 74 | *doUnpremul = (kUnpremul_SkAlphaType == at); |
| 75 | |
| 76 | const uint8_t* c = reinterpret_cast<const uint8_t*>(addr); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 77 | U8CPU a,r,g,b; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 78 | switch (ct) { |
| 79 | case kBGRA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 80 | b = static_cast<U8CPU>(c[0]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 81 | g = static_cast<U8CPU>(c[1]); |
| 82 | r = static_cast<U8CPU>(c[2]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 83 | a = static_cast<U8CPU>(c[3]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 84 | break; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 85 | case kRGB_888x_SkColorType: // fallthrough |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 86 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 87 | r = static_cast<U8CPU>(c[0]); |
| 88 | g = static_cast<U8CPU>(c[1]); |
| 89 | b = static_cast<U8CPU>(c[2]); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 90 | // 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] | 91 | a = static_cast<U8CPU>(c[3]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 92 | break; |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 93 | default: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 94 | SkDEBUGFAIL("Unexpected colortype"); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 95 | return 0; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 96 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 97 | |
| 98 | if (*doUnpremul) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 99 | r = SkMulDiv255Ceiling(r, a); |
| 100 | g = SkMulDiv255Ceiling(g, a); |
| 101 | b = SkMulDiv255Ceiling(b, a); |
| 102 | } |
| 103 | return SkPackARGB32(a, r, g, b); |
| 104 | } |
| 105 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 106 | static SkBitmap make_src_bitmap() { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 107 | static SkBitmap bmp; |
| 108 | if (bmp.isNull()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 109 | bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 110 | intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
| 111 | for (int y = 0; y < DEV_H; ++y) { |
| 112 | for (int x = 0; x < DEV_W; ++x) { |
| 113 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 114 | *pixel = get_src_color(x, y); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 118 | return bmp; |
| 119 | } |
| 120 | |
| 121 | static void fill_src_canvas(SkCanvas* canvas) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 122 | canvas->save(); |
| 123 | canvas->setMatrix(SkMatrix::I()); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 124 | canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 125 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 126 | paint.setBlendMode(SkBlendMode::kSrc); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 127 | canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 128 | canvas->restore(); |
| 129 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 130 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 131 | static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 132 | int w = bitmap->width(); |
| 133 | int h = bitmap->height(); |
| 134 | intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
| 135 | for (int y = 0; y < h; ++y) { |
| 136 | for (int x = 0; x < w; ++x) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 137 | SkPMColor initColor = get_dst_bmp_init_color(x, y, w); |
| 138 | if (kAlpha_8_SkColorType == bitmap->colorType()) { |
| 139 | uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x); |
| 140 | *alpha = SkGetPackedA32(initColor); |
| 141 | } else { |
| 142 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); |
| 143 | *pixel = initColor; |
| 144 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 149 | static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 150 | if (!didPremulConversion) { |
| 151 | return a == b; |
| 152 | } |
| 153 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 154 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 155 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 156 | int32_t aB = SkGetPackedB32(a); |
| 157 | |
| 158 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 159 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 160 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 161 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 162 | |
| 163 | return aA == bA && |
| 164 | SkAbs32(aR - bR) <= 1 && |
| 165 | SkAbs32(aG - bG) <= 1 && |
| 166 | SkAbs32(aB - bB) <= 1; |
| 167 | } |
| 168 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 169 | // checks the bitmap contains correct pixels after the readPixels |
| 170 | // if the bitmap was prefilled with pixels it checks that these weren't |
| 171 | // overwritten in the area outside the readPixels. |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 172 | static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y, |
| 173 | bool checkSurfacePixels, bool checkBitmapPixels, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 174 | SkImageInfo surfaceInfo) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 175 | SkAlphaType bmpAT = bitmap.alphaType(); |
| 176 | SkColorType bmpCT = bitmap.colorType(); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 177 | SkASSERT(!bitmap.isNull()); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 178 | SkASSERT(checkSurfacePixels || checkBitmapPixels); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 179 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 180 | int bw = bitmap.width(); |
| 181 | int bh = bitmap.height(); |
| 182 | |
| 183 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); |
| 184 | SkIRect clippedSrcRect = DEV_RECT; |
| 185 | if (!clippedSrcRect.intersect(srcRect)) { |
| 186 | clippedSrcRect.setEmpty(); |
| 187 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 188 | if (kAlpha_8_SkColorType == bmpCT) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 189 | for (int by = 0; by < bh; ++by) { |
| 190 | for (int bx = 0; bx < bw; ++bx) { |
| 191 | int devx = bx + srcRect.fLeft; |
| 192 | int devy = by + srcRect.fTop; |
| 193 | const uint8_t* alpha = bitmap.getAddr8(bx, by); |
| 194 | |
| 195 | if (clippedSrcRect.contains(devx, devy)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 196 | if (checkSurfacePixels) { |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 197 | uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType) |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 198 | ? 0xFF |
| 199 | : SkGetPackedA32(get_src_color(devx, devy)); |
| 200 | if (surfaceAlpha != *alpha) { |
| 201 | ERRORF(reporter, |
| 202 | "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ", |
| 203 | bx, by, surfaceAlpha, *alpha); |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | } else if (checkBitmapPixels) { |
| 208 | uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw)); |
| 209 | if (origDstAlpha != *alpha) { |
| 210 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 211 | "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha); |
| 212 | return false; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return true; |
| 218 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 219 | for (int by = 0; by < bh; ++by) { |
| 220 | for (int bx = 0; bx < bw; ++bx) { |
| 221 | int devx = bx + srcRect.fLeft; |
| 222 | int devy = by + srcRect.fTop; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 223 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 224 | const uint32_t* pixel = bitmap.getAddr32(bx, by); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 225 | |
| 226 | if (clippedSrcRect.contains(devx, devy)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 227 | if (checkSurfacePixels) { |
| 228 | SkPMColor surfacePMColor = get_src_color(devx, devy); |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 229 | if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) { |
| 230 | surfacePMColor &= 0xFF000000; |
| 231 | } |
| 232 | if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 233 | surfacePMColor |= 0xFF000000; |
| 234 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 235 | bool didPremul; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 236 | SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul); |
| 237 | if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) { |
| 238 | ERRORF(reporter, |
| 239 | "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. " |
| 240 | "Readback was unpremul: %d", |
| 241 | bx, by, surfacePMColor, pmPixel, didPremul); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 242 | return false; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 243 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 244 | } |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 245 | } else if (checkBitmapPixels) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 246 | uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw); |
| 247 | if (origDstPixel != *pixel) { |
| 248 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 249 | "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 250 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 255 | return true; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 258 | enum class TightRowBytes : bool { kNo, kYes }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 259 | |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 260 | static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB, |
| 261 | SkColorType ct, SkAlphaType at) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 262 | SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 263 | size_t rowBytes = 0; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 264 | if (tightRB == TightRowBytes::kNo) { |
| 265 | rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 266 | } |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 267 | bitmap->allocPixels(info, rowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 268 | } |
| 269 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 270 | static const struct { |
| 271 | SkColorType fColorType; |
| 272 | SkAlphaType fAlphaType; |
| 273 | } gReadPixelsConfigs[] = { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 274 | {kRGBA_8888_SkColorType, kPremul_SkAlphaType}, |
| 275 | {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 276 | {kRGB_888x_SkColorType, kOpaque_SkAlphaType}, |
| 277 | {kBGRA_8888_SkColorType, kPremul_SkAlphaType}, |
| 278 | {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 279 | {kAlpha_8_SkColorType, kPremul_SkAlphaType}, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 280 | }; |
| 281 | const SkIRect gReadPixelsTestRects[] = { |
| 282 | // entire thing |
| 283 | DEV_RECT, |
| 284 | // larger on all sides |
| 285 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 286 | // fully contained |
| 287 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 288 | // outside top left |
| 289 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 290 | // touching top left corner |
| 291 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 292 | // overlapping top left corner |
| 293 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 294 | // overlapping top left and top right corners |
| 295 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 296 | // touching entire top edge |
| 297 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 298 | // overlapping top right corner |
| 299 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 300 | // contained in x, overlapping top edge |
| 301 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 302 | // outside top right corner |
| 303 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 304 | // touching top right corner |
| 305 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 306 | // overlapping top left and bottom left corners |
| 307 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 308 | // touching entire left edge |
| 309 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 310 | // overlapping bottom left corner |
| 311 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 312 | // contained in y, overlapping left edge |
| 313 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 314 | // outside bottom left corner |
| 315 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 316 | // touching bottom left corner |
| 317 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 318 | // overlapping bottom left and bottom right corners |
| 319 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 320 | // touching entire left edge |
| 321 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 322 | // overlapping bottom right corner |
| 323 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 324 | // overlapping top right and bottom right corners |
| 325 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 326 | }; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 327 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 328 | bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo, |
| 329 | const SkImageInfo& srcInfo) { |
| 330 | return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 331 | } |
| 332 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 333 | static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 334 | const SkImageInfo& surfaceInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 335 | SkCanvas* canvas = surface->getCanvas(); |
| 336 | fill_src_canvas(canvas); |
| 337 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 338 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 339 | for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 340 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 341 | SkBitmap bmp; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 342 | init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType, |
| 343 | gReadPixelsConfigs[c].fAlphaType); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 344 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 345 | // if the bitmap has pixels allocated before the readPixels, |
| 346 | // note that and fill them with pattern |
| 347 | bool startsWithPixels = !bmp.isNull(); |
| 348 | if (startsWithPixels) { |
| 349 | fill_dst_bmp_with_init_data(&bmp); |
| 350 | } |
| 351 | uint32_t idBefore = surface->generationID(); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 352 | bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 353 | uint32_t idAfter = surface->generationID(); |
| 354 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 355 | // we expect to succeed when the read isn't fully clipped out and the infos are |
| 356 | // compatible. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 357 | bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 358 | // determine whether we expected the read to succeed. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 359 | REPORTER_ASSERT(reporter, expectSuccess == success, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 360 | "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", |
| 361 | success, surfaceInfo.colorType(), surfaceInfo.alphaType(), |
| 362 | bmp.info().colorType(), bmp.info().alphaType()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 363 | // read pixels should never change the gen id |
| 364 | REPORTER_ASSERT(reporter, idBefore == idAfter); |
| 365 | |
| 366 | if (success || startsWithPixels) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 367 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 368 | startsWithPixels, surfaceInfo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 369 | } else { |
| 370 | // if we had no pixels beforehand and the readPixels |
| 371 | // failed then our bitmap should still not have pixels |
| 372 | REPORTER_ASSERT(reporter, bmp.isNull()); |
| 373 | } |
| 374 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 378 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 379 | DEF_TEST(ReadPixels, reporter) { |
| 380 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 381 | auto surface(SkSurface::MakeRaster(info)); |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 382 | test_readpixels(reporter, surface, info); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 383 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 384 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 385 | static void test_readpixels_texture(skiatest::Reporter* reporter, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 386 | std::unique_ptr<GrSurfaceContext> sContext, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 387 | const SkImageInfo& surfaceInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 388 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 389 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 390 | for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 391 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 392 | SkBitmap bmp; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 393 | init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType, |
| 394 | gReadPixelsConfigs[c].fAlphaType); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 395 | |
| 396 | // if the bitmap has pixels allocated before the readPixels, |
| 397 | // note that and fill them with pattern |
| 398 | bool startsWithPixels = !bmp.isNull(); |
| 399 | // Try doing the read directly from a non-renderable texture |
| 400 | if (startsWithPixels) { |
| 401 | fill_dst_bmp_with_init_data(&bmp); |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 402 | bool success = sContext->readPixels(bmp.info(), bmp.getPixels(), bmp.rowBytes(), |
| 403 | {srcRect.fLeft, srcRect.fTop}); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 404 | auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 405 | REPORTER_ASSERT( |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 406 | reporter, expectSuccess == success, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 407 | "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", |
| 408 | success, surfaceInfo.colorType(), surfaceInfo.alphaType(), |
| 409 | bmp.info().colorType(), bmp.info().alphaType()); |
| 410 | if (success) { |
| 411 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 412 | surfaceInfo); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 413 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 419 | |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 420 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) { |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 421 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 422 | SkBitmap bmp = make_src_bitmap(); |
Robert Phillips | bab2dbb | 2017-04-17 07:43:27 -0400 | [diff] [blame] | 423 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 424 | // 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] | 425 | for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 426 | for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 427 | sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData( |
Brian Salomon | 4eda710 | 2019-10-21 15:04:52 -0400 | [diff] [blame] | 428 | context, renderable, origin, bmp.info(), bmp.getPixels(), bmp.rowBytes()); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 429 | GrColorType grColorType = SkColorTypeToGrColorType(bmp.colorType()); |
| 430 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(), |
| 431 | grColorType); |
| 432 | GrSurfaceProxyView view(std::move(proxy), origin, swizzle); |
| 433 | auto sContext = GrSurfaceContext::Make(context, std::move(view), |
| 434 | grColorType, kPremul_SkAlphaType, nullptr); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 435 | auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType); |
| 436 | test_readpixels_texture(reporter, std::move(sContext), info); |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 437 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 440 | |
| 441 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 442 | |
| 443 | static const uint32_t kNumPixels = 5; |
| 444 | |
| 445 | // The five reference pixels are: red, green, blue, white, black. |
| 446 | // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector |
| 447 | // plus a tail pixel. |
| 448 | static const uint32_t rgba[kNumPixels] = { |
| 449 | 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000 |
| 450 | }; |
| 451 | static const uint32_t bgra[kNumPixels] = { |
| 452 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000 |
| 453 | }; |
| 454 | static const uint16_t rgb565[kNumPixels] = { |
| 455 | SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0 |
| 456 | }; |
| 457 | |
| 458 | static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F }; |
| 459 | |
| 460 | static const uint64_t kRed = (uint64_t) SK_Half1 << 0; |
| 461 | static const uint64_t kGreen = (uint64_t) SK_Half1 << 16; |
| 462 | static const uint64_t kBlue = (uint64_t) SK_Half1 << 32; |
| 463 | static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48; |
| 464 | static const uint64_t f16[kNumPixels] = { |
| 465 | kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha |
| 466 | }; |
| 467 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 468 | static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 469 | static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 470 | |
| 471 | static const void* five_reference_pixels(SkColorType colorType) { |
| 472 | switch (colorType) { |
| 473 | case kUnknown_SkColorType: |
| 474 | return nullptr; |
| 475 | case kAlpha_8_SkColorType: |
| 476 | return alpha8; |
| 477 | case kRGB_565_SkColorType: |
| 478 | return rgb565; |
| 479 | case kARGB_4444_SkColorType: |
| 480 | return rgba4444; |
| 481 | case kRGBA_8888_SkColorType: |
| 482 | return rgba; |
| 483 | case kBGRA_8888_SkColorType: |
| 484 | return bgra; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 485 | case kGray_8_SkColorType: |
| 486 | return gray8; |
| 487 | case kRGBA_F16_SkColorType: |
| 488 | return f16; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 489 | default: |
Leon Scroggins III | d6832d0 | 2018-09-04 11:10:04 -0400 | [diff] [blame] | 490 | return nullptr; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | SkASSERT(false); |
| 494 | return nullptr; |
| 495 | } |
| 496 | |
| 497 | static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo, |
| 498 | const SkImageInfo& srcInfo) { |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 499 | if (!SkImageInfoIsValid(srcInfo)) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 503 | const void* srcPixels = five_reference_pixels(srcInfo.colorType()); |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 504 | SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 505 | sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr); |
| 506 | REPORTER_ASSERT(r, src); |
| 507 | |
| 508 | // Enough space for 5 pixels when color type is F16, more than enough space in other cases. |
| 509 | uint64_t dstPixels[kNumPixels]; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 510 | SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 511 | bool success = src->readPixels(dstPixmap, 0, 0); |
| 512 | REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo)); |
| 513 | |
| 514 | if (success) { |
| 515 | if (kGray_8_SkColorType == srcInfo.colorType() && |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 516 | kGray_8_SkColorType != dstInfo.colorType()) { |
| 517 | // TODO: test (r,g,b) == (gray,gray,gray)? |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | if (kGray_8_SkColorType == dstInfo.colorType() && |
| 522 | kGray_8_SkColorType != srcInfo.colorType()) { |
| 523 | // TODO: test gray = luminance? |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | if (kAlpha_8_SkColorType == srcInfo.colorType() && |
| 528 | kAlpha_8_SkColorType != dstInfo.colorType()) { |
| 529 | // TODO: test output = black with this alpha? |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | |
| 533 | REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()), |
| 534 | kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType()))); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | DEF_TEST(ReadPixels_ValidConversion, reporter) { |
| 539 | const SkColorType kColorTypes[] = { |
| 540 | kUnknown_SkColorType, |
| 541 | kAlpha_8_SkColorType, |
| 542 | kRGB_565_SkColorType, |
| 543 | kARGB_4444_SkColorType, |
| 544 | kRGBA_8888_SkColorType, |
| 545 | kBGRA_8888_SkColorType, |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 546 | kGray_8_SkColorType, |
| 547 | kRGBA_F16_SkColorType, |
| 548 | }; |
| 549 | |
| 550 | const SkAlphaType kAlphaTypes[] = { |
| 551 | kUnknown_SkAlphaType, |
| 552 | kOpaque_SkAlphaType, |
| 553 | kPremul_SkAlphaType, |
| 554 | kUnpremul_SkAlphaType, |
| 555 | }; |
| 556 | |
| 557 | const sk_sp<SkColorSpace> kColorSpaces[] = { |
| 558 | nullptr, |
| 559 | SkColorSpace::MakeSRGB(), |
| 560 | }; |
| 561 | |
| 562 | for (SkColorType dstCT : kColorTypes) { |
| 563 | for (SkAlphaType dstAT: kAlphaTypes) { |
| 564 | for (sk_sp<SkColorSpace> dstCS : kColorSpaces) { |
| 565 | for (SkColorType srcCT : kColorTypes) { |
| 566 | for (SkAlphaType srcAT: kAlphaTypes) { |
| 567 | for (sk_sp<SkColorSpace> srcCS : kColorSpaces) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 568 | test_conversion(reporter, |
| 569 | SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS), |
| 570 | SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS)); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 578 | |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 579 | static constexpr int min_rgb_channel_bits(SkColorType ct) { |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 580 | switch (ct) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 581 | case kUnknown_SkColorType: return 0; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 582 | case kAlpha_8_SkColorType: return 0; |
| 583 | case kA16_unorm_SkColorType: return 0; |
| 584 | case kA16_float_SkColorType: return 0; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 585 | case kRGB_565_SkColorType: return 5; |
| 586 | case kARGB_4444_SkColorType: return 4; |
| 587 | case kR8G8_unorm_SkColorType: return 8; |
| 588 | case kR16G16_unorm_SkColorType: return 16; |
| 589 | case kR16G16_float_SkColorType: return 16; |
| 590 | case kRGBA_8888_SkColorType: return 8; |
| 591 | case kRGB_888x_SkColorType: return 8; |
| 592 | case kBGRA_8888_SkColorType: return 8; |
| 593 | case kRGBA_1010102_SkColorType: return 10; |
| 594 | case kRGB_101010x_SkColorType: return 10; |
| 595 | case kGray_8_SkColorType: return 8; // counting gray as "rgb" |
| 596 | case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa |
| 597 | case kRGBA_F16_SkColorType: return 10; // just counting the mantissa |
| 598 | case kRGBA_F32_SkColorType: return 23; // just counting the mantissa |
| 599 | case kR16G16B16A16_unorm_SkColorType: return 16; |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 600 | } |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 601 | SkUNREACHABLE; |
| 602 | } |
| 603 | |
| 604 | static constexpr int alpha_channel_bits(SkColorType ct) { |
| 605 | switch (ct) { |
| 606 | case kUnknown_SkColorType: return 0; |
| 607 | case kAlpha_8_SkColorType: return 8; |
| 608 | case kA16_unorm_SkColorType: return 16; |
| 609 | case kA16_float_SkColorType: return 16; |
| 610 | case kRGB_565_SkColorType: return 0; |
| 611 | case kARGB_4444_SkColorType: return 4; |
| 612 | case kR8G8_unorm_SkColorType: return 0; |
| 613 | case kR16G16_unorm_SkColorType: return 0; |
| 614 | case kR16G16_float_SkColorType: return 0; |
| 615 | case kRGBA_8888_SkColorType: return 8; |
| 616 | case kRGB_888x_SkColorType: return 0; |
| 617 | case kBGRA_8888_SkColorType: return 8; |
| 618 | case kRGBA_1010102_SkColorType: return 2; |
| 619 | case kRGB_101010x_SkColorType: return 0; |
| 620 | case kGray_8_SkColorType: return 0; |
| 621 | case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa |
| 622 | case kRGBA_F16_SkColorType: return 10; // just counting the mantissa |
| 623 | case kRGBA_F32_SkColorType: return 23; // just counting the mantissa |
| 624 | case kR16G16B16A16_unorm_SkColorType: return 16; |
| 625 | } |
| 626 | SkUNREACHABLE; |
| 627 | } |
| 628 | |
| 629 | namespace { |
| 630 | |
| 631 | struct GpuReadPixelTestRules { |
| 632 | // Test unpremul sources? We could omit this and detect that creating the source of the read |
| 633 | // failed but having it lets us skip generating reference color data. |
| 634 | bool fAllowUnpremulSrc = true; |
| 635 | // Expect read function to succeed for kUnpremul? |
| 636 | bool fAllowUnpremulRead = true; |
| 637 | // Are reads that are overlapping but not contained by the src bounds expected to succeed? |
| 638 | bool fUncontainedRectSucceeds = true; |
| 639 | }; |
| 640 | |
| 641 | // Makes a src populated with the pixmap. The src should get its image info (or equivalent) from |
| 642 | // the pixmap. |
| 643 | template <typename T> using GpuSrcFactory = T(SkPixmap&); |
| 644 | |
| 645 | // Does a read from the T into the pixmap. |
| 646 | template <typename T> using GpuReadSrcFn = bool(const T&, const SkIVector& offset, const SkPixmap&); |
| 647 | |
| 648 | } // anonymous namespace |
| 649 | |
| 650 | template <typename T> |
| 651 | static void gpu_read_pixels_test_driver(skiatest::Reporter* reporter, |
| 652 | const GpuReadPixelTestRules& rules, |
| 653 | const std::function<GpuSrcFactory<T>>& srcFactory, |
| 654 | const std::function<GpuReadSrcFn<T>>& read) { |
| 655 | // Separate this out just to give it some line width to breathe. Note 'srcPixels' should have |
| 656 | // the same image info as src. We will do a converting readPixels() on it to get the data |
| 657 | // to compare with the results of 'read'. |
| 658 | auto runTest = [&](const T& src, const SkPixmap& srcPixels, const SkImageInfo& readInfo, |
| 659 | const SkIVector& offset) { |
| 660 | const bool csConversion = |
| 661 | !SkColorSpace::Equals(readInfo.colorSpace(), srcPixels.info().colorSpace()); |
| 662 | const auto readCT = readInfo.colorType(); |
| 663 | const auto readAT = readInfo.alphaType(); |
| 664 | const auto srcCT = srcPixels.info().colorType(); |
| 665 | const auto srcAT = srcPixels.info().alphaType(); |
| 666 | const auto rect = SkIRect::MakeWH(readInfo.width(), readInfo.height()).makeOffset(offset); |
| 667 | const auto surfBounds = SkIRect::MakeWH(srcPixels.width(), srcPixels.height()); |
| 668 | const size_t readBpp = SkColorTypeBytesPerPixel(readCT); |
| 669 | |
| 670 | // Make the row bytes in the dst be loose for extra stress. |
| 671 | const size_t dstRB = readBpp * readInfo.width() + 10 * readBpp; |
| 672 | // This will make the last row tight. |
| 673 | const size_t dstSize = readInfo.computeByteSize(dstRB); |
| 674 | std::unique_ptr<char[]> dstData(new char[dstSize]); |
| 675 | SkPixmap dstPixels(readInfo, dstData.get(), dstRB); |
| 676 | // Initialize with an arbitrary value for each byte. Later we will check that only the |
| 677 | // correct part of the destination gets overwritten by 'read'. |
| 678 | static constexpr auto kInitialByte = static_cast<char>(0x1B); |
| 679 | std::fill_n(static_cast<char*>(dstPixels.writable_addr()), |
| 680 | dstPixels.computeByteSize(), |
| 681 | kInitialByte); |
| 682 | |
| 683 | const bool success = read(src, offset, dstPixels); |
| 684 | |
| 685 | if (!SkIRect::Intersects(rect, surfBounds)) { |
| 686 | REPORTER_ASSERT(reporter, !success); |
| 687 | } else if (readCT == kUnknown_SkColorType) { |
| 688 | REPORTER_ASSERT(reporter, !success); |
| 689 | } else if (readAT == kUnknown_SkAlphaType) { |
| 690 | REPORTER_ASSERT(reporter, !success); |
| 691 | } else if (!rules.fUncontainedRectSucceeds && !surfBounds.contains(rect)) { |
| 692 | REPORTER_ASSERT(reporter, !success); |
| 693 | } else if (!rules.fAllowUnpremulRead && readAT == kUnpremul_SkAlphaType) { |
| 694 | REPORTER_ASSERT(reporter, !success); |
| 695 | } else if (!success) { |
Brian Salomon | b3bd864 | 2019-11-04 16:59:29 -0500 | [diff] [blame] | 696 | // TODO: Support kRGB_101010x at all in GPU. |
| 697 | if (readCT != kRGB_101010x_SkColorType) { |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 698 | ERRORF(reporter, |
| 699 | "Read failed. Src CT: %s, Src AT: %s Read CT: %s, Read AT: %s, " |
| 700 | "Rect [%d, %d, %d, %d], CS conversion: %d\n", |
| 701 | ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT), |
| 702 | ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT), |
| 703 | rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion); |
| 704 | } |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | bool guardOk = true; |
| 709 | auto guardCheck = [](char x) { return x == kInitialByte; }; |
| 710 | |
| 711 | // Considering the rect we tried to read and the surface bounds figure out which pixels in |
| 712 | // both src and dst space should actually have been read and written. |
| 713 | SkIRect srcReadRect; |
| 714 | if (success && srcReadRect.intersect(surfBounds, rect)) { |
| 715 | SkIRect dstWriteRect = srcReadRect.makeOffset(-rect.fLeft, -rect.fTop); |
| 716 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 717 | const bool lumConversion = |
| 718 | !(SkColorTypeComponentFlags(srcCT) & kGray_SkColorTypeComponentFlag) && |
| 719 | (SkColorTypeComponentFlags(readCT) & kGray_SkColorTypeComponentFlag); |
| 720 | // A CS or luminance conversion allows a 3 value difference and otherwise a 2 value |
| 721 | // difference. Note that sometimes read back on GPU can be lossy even when there no |
| 722 | // conversion at allbecause GPU->CPU read may go to a lower bit depth format and then be |
| 723 | // promoted back to the original type. For example, GL ES cannot read to 1010102, so we |
| 724 | // go through 8888. |
Brian Salomon | 6790423 | 2019-11-06 10:56:00 -0500 | [diff] [blame] | 725 | const float numer = (lumConversion || csConversion) ? 3.f : 2.f; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 726 | int rgbBits = std::min({min_rgb_channel_bits(readCT), |
| 727 | min_rgb_channel_bits(srcCT), |
| 728 | 8}); |
| 729 | float tol = numer / (1 << rgbBits); |
| 730 | float alphaTol = 0; |
| 731 | if (readAT != kOpaque_SkAlphaType && srcAT != kOpaque_SkAlphaType) { |
| 732 | const int alphaBits = std::min(alpha_channel_bits(readCT), |
| 733 | alpha_channel_bits(srcCT)); |
| 734 | alphaTol = 2.f / (1 << alphaBits); |
| 735 | } |
| 736 | |
| 737 | const float tols[4] = {tol, tol, tol, alphaTol}; |
| 738 | auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y, |
| 739 | const float diffs[4]) { |
| 740 | SkASSERT(x >= 0 && y >= 0); |
| 741 | ERRORF(reporter, |
| 742 | "Src CT: %s, Src AT: %s, Read CT: %s, Read AT: %s, Rect [%d, %d, %d, %d]" |
| 743 | ", CS conversion: %d\n" |
| 744 | "Error at %d, %d. Diff in floats: (%f, %f, %f %f)", |
| 745 | ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT), |
| 746 | ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT), |
| 747 | rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion, x, y, |
| 748 | diffs[0], diffs[1], diffs[2], diffs[3]); |
| 749 | }); |
| 750 | SkAutoPixmapStorage ref; |
| 751 | ref.alloc(readInfo.makeWH(dstWriteRect.width(), dstWriteRect.height())); |
| 752 | srcPixels.readPixels(ref, srcReadRect.x(), srcReadRect.y()); |
| 753 | // This is the part of dstPixels that should have been updated. |
| 754 | SkPixmap actual; |
| 755 | SkAssertResult(dstPixels.extractSubset(&actual, dstWriteRect)); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 756 | ComparePixels(ref, actual, tols, error); |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 757 | |
| 758 | const auto* v = dstData.get(); |
| 759 | const auto* end = dstData.get() + dstSize; |
| 760 | guardOk = std::all_of(v, v + dstWriteRect.top() * dstPixels.rowBytes(), guardCheck); |
| 761 | v += dstWriteRect.top() * dstPixels.rowBytes(); |
| 762 | for (int y = dstWriteRect.top(); y < dstWriteRect.bottom(); ++y) { |
| 763 | guardOk |= std::all_of(v, v + dstWriteRect.left() * readBpp, guardCheck); |
| 764 | auto pad = v + dstWriteRect.right() * readBpp; |
| 765 | auto rowEnd = std::min(end, v + dstPixels.rowBytes()); |
| 766 | // min protects against reading past the end of the tight last row. |
| 767 | guardOk |= std::all_of(pad, rowEnd, guardCheck); |
| 768 | v = rowEnd; |
| 769 | } |
| 770 | guardOk |= std::all_of(v, end, guardCheck); |
| 771 | } else { |
| 772 | guardOk = std::all_of(dstData.get(), dstData.get() + dstSize, guardCheck); |
| 773 | } |
| 774 | if (!guardOk) { |
| 775 | ERRORF(reporter, |
| 776 | "Result pixels modified result outside read rect [%d, %d, %d, %d]. " |
| 777 | "Src CT: %s, Read CT: %s, CS conversion: %d", |
| 778 | rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, |
| 779 | ToolUtils::colortype_name(srcCT), ToolUtils::colortype_name(readCT), |
| 780 | csConversion); |
| 781 | } |
| 782 | }; |
| 783 | |
| 784 | static constexpr int kW = 16; |
| 785 | static constexpr int kH = 16; |
| 786 | |
| 787 | // Makes the reference data that is used to populate the src. Always F32 regardless of srcCT. |
| 788 | auto make_ref_f32_data = [](SkAlphaType srcAT, SkColorType srcCT) { |
| 789 | // Make src data in F32 with srcAT. We will convert it to each color type we test to |
| 790 | // initialize the src. |
| 791 | const auto refInfo = |
| 792 | SkImageInfo::Make(kW, kH, kRGBA_F32_SkColorType, srcAT, SkColorSpace::MakeSRGB()); |
| 793 | auto refSurf = SkSurface::MakeRaster(refInfo); |
| 794 | static constexpr SkPoint kPts1[] = {{0, 0}, {kW, kH}}; |
| 795 | static constexpr SkColor kColors1[] = {SK_ColorGREEN, SK_ColorRED}; |
| 796 | SkPaint paint; |
| 797 | paint.setShader( |
| 798 | SkGradientShader::MakeLinear(kPts1, kColors1, nullptr, 2, SkTileMode::kClamp)); |
| 799 | refSurf->getCanvas()->drawPaint(paint); |
| 800 | static constexpr SkPoint kPts2[] = {{kW, 0}, {0, kH}}; |
| 801 | static constexpr SkColor kColors2[] = {SK_ColorBLUE, SK_ColorBLACK}; |
| 802 | paint.setShader( |
| 803 | SkGradientShader::MakeLinear(kPts2, kColors2, nullptr, 2, SkTileMode::kClamp)); |
| 804 | paint.setBlendMode(SkBlendMode::kPlus); |
| 805 | refSurf->getCanvas()->drawPaint(paint); |
| 806 | // Keep everything opaque if the src alpha type is opaque. Also, there is an issue with |
| 807 | // 1010102 (the only color type where the number of alpha bits is non-zero and not the |
| 808 | // same as r, g, and b). Because of the different precisions the draw below can create |
| 809 | // data that isn't strictly premul (e.g. alpha is 1/3 but green is .4). SW will clamp |
| 810 | // r, g, b to a if the dst is premul and a different color type. GPU doesn't do this. |
| 811 | // We could but 1010102 premul is kind of dubious anyway. So for now just keep the data |
| 812 | // opaque. |
| 813 | if (srcAT != kOpaque_SkAlphaType && |
| 814 | (srcAT == kPremul_SkAlphaType && srcCT != kRGBA_1010102_SkColorType)) { |
| 815 | static constexpr SkColor kColors3[] = {SK_ColorWHITE, |
| 816 | SK_ColorWHITE, |
| 817 | 0x60FFFFFF, |
| 818 | SK_ColorWHITE, |
| 819 | SK_ColorWHITE}; |
| 820 | static constexpr SkScalar kPos3[] = {0.f, 0.15f, 0.5f, 0.85f, 1.f}; |
| 821 | paint.setShader(SkGradientShader::MakeRadial({kW / 2.f, kH / 2.f}, (kW + kH) / 10.f, |
| 822 | kColors3, kPos3, 5, SkTileMode::kMirror)); |
| 823 | paint.setBlendMode(SkBlendMode::kDstIn); |
| 824 | refSurf->getCanvas()->drawPaint(paint); |
| 825 | } |
| 826 | |
| 827 | const auto srcInfo = SkImageInfo::Make(kW, kH, srcCT, srcAT, SkColorSpace::MakeSRGB()); |
| 828 | SkAutoPixmapStorage srcPixels; |
| 829 | srcPixels.alloc(srcInfo); |
| 830 | refSurf->readPixels(srcPixels, 0, 0); |
| 831 | return std::move(srcPixels); |
| 832 | }; |
| 833 | |
| 834 | for (int sat = 0; sat < kLastEnum_SkAlphaType; ++sat) { |
| 835 | const auto srcAT = static_cast<SkAlphaType>(sat); |
| 836 | if (srcAT == kUnknown_SkAlphaType || |
| 837 | (srcAT == kUnpremul_SkAlphaType && !rules.fAllowUnpremulSrc)) { |
| 838 | continue; |
| 839 | } |
| 840 | for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) { |
| 841 | const auto srcCT = static_cast<SkColorType>(sct); |
| 842 | // Note that we only currently use srcCT for a 1010102 workaround. If we remove this we |
| 843 | // can also but the ref data setup above the srcCT loop. |
| 844 | SkAutoPixmapStorage srcPixels = make_ref_f32_data(srcAT, srcCT); |
| 845 | auto src = srcFactory(srcPixels); |
| 846 | if (!src) { |
| 847 | continue; |
| 848 | } |
| 849 | for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) { |
| 850 | const auto readCT = static_cast<SkColorType>(rct); |
| 851 | for (const sk_sp<SkColorSpace>& readCS : |
| 852 | {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) { |
| 853 | for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) { |
| 854 | const auto readAT = static_cast<SkAlphaType>(at); |
| 855 | if (srcAT != kOpaque_SkAlphaType && readAT == kOpaque_SkAlphaType) { |
| 856 | // This doesn't make sense. |
| 857 | continue; |
| 858 | } |
| 859 | // Test full size, partial, empty, and too wide rects. |
| 860 | for (const auto& rect : { |
| 861 | // entire thing |
| 862 | SkIRect::MakeWH(kW, kH), |
| 863 | // larger on all sides |
| 864 | SkIRect::MakeLTRB(-10, -10, kW + 10, kH + 10), |
| 865 | // fully contained |
| 866 | SkIRect::MakeLTRB(kW / 4, kH / 4, 3 * kW / 4, 3 * kH / 4), |
| 867 | // outside top left |
| 868 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 869 | // touching top left corner |
| 870 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 871 | // overlapping top left corner |
| 872 | SkIRect::MakeLTRB(-10, -10, kW / 4, kH / 4), |
| 873 | // overlapping top left and top right corners |
| 874 | SkIRect::MakeLTRB(-10, -10, kW + 10, kH / 4), |
| 875 | // touching entire top edge |
| 876 | SkIRect::MakeLTRB(-10, -10, kW + 10, 0), |
| 877 | // overlapping top right corner |
| 878 | SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH / 4), |
| 879 | // contained in x, overlapping top edge |
| 880 | SkIRect::MakeLTRB(kW / 4, -10, 3 * kW / 4, kH / 4), |
| 881 | // outside top right corner |
| 882 | SkIRect::MakeLTRB(kW + 1, -10, kW + 10, -1), |
| 883 | // touching top right corner |
| 884 | SkIRect::MakeLTRB(kW, -10, kW + 10, 0), |
| 885 | // overlapping top left and bottom left corners |
| 886 | SkIRect::MakeLTRB(-10, -10, kW / 4, kH + 10), |
| 887 | // touching entire left edge |
| 888 | SkIRect::MakeLTRB(-10, -10, 0, kH + 10), |
| 889 | // overlapping bottom left corner |
| 890 | SkIRect::MakeLTRB(-10, 3 * kH / 4, kW / 4, kH + 10), |
| 891 | // contained in y, overlapping left edge |
| 892 | SkIRect::MakeLTRB(-10, kH / 4, kW / 4, 3 * kH / 4), |
| 893 | // outside bottom left corner |
| 894 | SkIRect::MakeLTRB(-10, kH + 1, -1, kH + 10), |
| 895 | // touching bottom left corner |
| 896 | SkIRect::MakeLTRB(-10, kH, 0, kH + 10), |
| 897 | // overlapping bottom left and bottom right corners |
| 898 | SkIRect::MakeLTRB(-10, 3 * kH / 4, kW + 10, kH + 10), |
| 899 | // touching entire left edge |
| 900 | SkIRect::MakeLTRB(0, kH, kW, kH + 10), |
| 901 | // overlapping bottom right corner |
| 902 | SkIRect::MakeLTRB(3 * kW / 4, 3 * kH / 4, kW + 10, kH + 10), |
| 903 | // overlapping top right and bottom right corners |
| 904 | SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH + 10), |
| 905 | }) { |
| 906 | const auto readInfo = SkImageInfo::Make(rect.width(), rect.height(), |
| 907 | readCT, readAT, readCS); |
| 908 | const SkIVector offset = rect.topLeft(); |
| 909 | runTest(src, srcPixels, readInfo, offset); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 916 | } |
| 917 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 918 | namespace { |
| 919 | struct AsyncContext { |
| 920 | bool fCalled = false; |
| 921 | std::unique_ptr<const SkSurface::AsyncReadResult> fResult; |
| 922 | }; |
| 923 | } // anonymous namespace |
| 924 | |
| 925 | // Making this a lambda in the test functions caused: |
| 926 | // "error: cannot compile this forwarded non-trivially copyable parameter yet" |
| 927 | // on x86/Win/Clang bot, referring to 'result'. |
| 928 | static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) { |
| 929 | auto context = static_cast<AsyncContext*>(c); |
| 930 | context->fResult = std::move(result); |
| 931 | context->fCalled = true; |
| 932 | }; |
| 933 | |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 934 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) { |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 935 | using Surface = sk_sp<SkSurface>; |
| 936 | auto reader = std::function<GpuReadSrcFn<Surface>>([](const Surface& surface, |
| 937 | const SkIVector& offset, |
| 938 | const SkPixmap& pixels) { |
| 939 | AsyncContext context; |
| 940 | auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset); |
| 941 | |
| 942 | // Rescale quality and linearity don't matter since we're doing a non-scaling readback. |
| 943 | surface->asyncRescaleAndReadPixels(pixels.info(), rect, SkSurface::RescaleGamma::kSrc, |
| 944 | kNone_SkFilterQuality, async_callback, &context); |
| 945 | while (!context.fCalled) { |
| 946 | surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion(); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 947 | } |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 948 | if (!context.fResult) { |
| 949 | return false; |
| 950 | } |
| 951 | SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), context.fResult->data(0), |
| 952 | context.fResult->rowBytes(0), pixels.info().minRowBytes(), pixels.height()); |
| 953 | return true; |
| 954 | }); |
| 955 | GpuReadPixelTestRules rules; |
| 956 | rules.fAllowUnpremulSrc = false; |
| 957 | rules.fAllowUnpremulRead = false; |
| 958 | rules.fUncontainedRectSucceeds = false; |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 959 | |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 960 | for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 961 | auto factory = std::function<GpuSrcFactory<Surface>>( |
| 962 | [context = ctxInfo.grContext(), origin](const SkPixmap& src) { |
| 963 | if (src.colorType() == kRGB_888x_SkColorType) { |
| 964 | return Surface(); |
Ravi Mistry | cb55010 | 2019-10-03 09:06:25 +0000 | [diff] [blame] | 965 | } |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 966 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(), |
| 967 | 0, origin, nullptr); |
| 968 | if (surf) { |
| 969 | surf->writePixels(src, 0, 0); |
| 970 | } |
| 971 | return surf; |
| 972 | }); |
| 973 | gpu_read_pixels_test_driver(reporter, rules, factory, reader); |
| 974 | } |
| 975 | } |
| 976 | |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 977 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) { |
| 978 | using Surface = sk_sp<SkSurface>; |
| 979 | auto reader = std::function<GpuReadSrcFn<Surface>>( |
| 980 | [](const Surface& surface, const SkIVector& offset, const SkPixmap& pixels) { |
| 981 | return surface->readPixels(pixels, offset.fX, offset.fY); |
| 982 | }); |
| 983 | GpuReadPixelTestRules rules; |
| 984 | rules.fAllowUnpremulSrc = false; |
| 985 | rules.fAllowUnpremulRead = true; |
| 986 | rules.fUncontainedRectSucceeds = true; |
| 987 | |
| 988 | for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 989 | auto factory = std::function<GpuSrcFactory<Surface>>( |
| 990 | [context = ctxInfo.grContext(), origin](const SkPixmap& src) { |
| 991 | if (src.colorType() == kRGB_888x_SkColorType) { |
| 992 | return Surface(); |
| 993 | } |
| 994 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(), |
| 995 | 0, origin, nullptr); |
| 996 | if (surf) { |
| 997 | surf->writePixels(src, 0, 0); |
| 998 | } |
| 999 | return surf; |
| 1000 | }); |
| 1001 | gpu_read_pixels_test_driver(reporter, rules, factory, reader); |
Brian Salomon | 6fc04f8 | 2019-10-02 19:11:55 -0400 | [diff] [blame] | 1002 | } |
| 1003 | } |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 1004 | |
| 1005 | DEF_GPUTEST(AsyncReadPixelsContextShutdown, reporter, options) { |
| 1006 | const auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType, |
| 1007 | SkColorSpace::MakeSRGB()); |
| 1008 | enum class ShutdownSequence { |
| 1009 | kFreeResult_DestroyContext, |
| 1010 | kDestroyContext_FreeResult, |
| 1011 | kFreeResult_ReleaseAndAbandon_DestroyContext, |
| 1012 | kFreeResult_Abandon_DestroyContext, |
| 1013 | kReleaseAndAbandon_FreeResult_DestroyContext, |
| 1014 | kAbandon_FreeResult_DestroyContext, |
| 1015 | kReleaseAndAbandon_DestroyContext_FreeResult, |
| 1016 | kAbandon_DestroyContext_FreeResult, |
| 1017 | }; |
| 1018 | for (int t = 0; t < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++t) { |
| 1019 | auto type = static_cast<sk_gpu_test::GrContextFactory::ContextType>(t); |
| 1020 | for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext, |
| 1021 | ShutdownSequence::kDestroyContext_FreeResult, |
| 1022 | ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext, |
| 1023 | ShutdownSequence::kFreeResult_Abandon_DestroyContext, |
| 1024 | ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext, |
| 1025 | ShutdownSequence::kAbandon_FreeResult_DestroyContext, |
| 1026 | ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult, |
| 1027 | ShutdownSequence::kAbandon_DestroyContext_FreeResult}) { |
| 1028 | // Vulkan context abandoning without resource release has issues outside of the scope of |
| 1029 | // this test. |
| 1030 | if (type == sk_gpu_test::GrContextFactory::kVulkan_ContextType && |
| 1031 | (sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext || |
| 1032 | sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult || |
| 1033 | sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext)) { |
| 1034 | continue; |
| 1035 | } |
| 1036 | for (bool yuv : {false, true}) { |
| 1037 | sk_gpu_test::GrContextFactory factory(options); |
| 1038 | auto context = factory.get(type); |
| 1039 | if (!context) { |
| 1040 | continue; |
| 1041 | } |
Brian Salomon | fb28c6f | 2020-01-10 13:04:45 -0500 | [diff] [blame] | 1042 | // This test is only meaningful for contexts that support transfer buffers for |
| 1043 | // reads. |
| 1044 | if (!context->priv().caps()->transferFromSurfaceToBufferSupport()) { |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 1045 | continue; |
| 1046 | } |
| 1047 | auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 1, nullptr); |
| 1048 | if (!surf) { |
| 1049 | continue; |
| 1050 | } |
| 1051 | AsyncContext cbContext; |
| 1052 | if (yuv) { |
| 1053 | surf->asyncRescaleAndReadPixelsYUV420( |
| 1054 | kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(), ii.bounds(), |
| 1055 | ii.dimensions(), SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality, |
| 1056 | &async_callback, &cbContext); |
| 1057 | } else { |
| 1058 | surf->asyncRescaleAndReadPixels(ii, ii.bounds(), SkSurface::RescaleGamma::kSrc, |
| 1059 | kNone_SkFilterQuality, &async_callback, |
| 1060 | &cbContext); |
| 1061 | } |
| 1062 | while (!cbContext.fCalled) { |
| 1063 | context->checkAsyncWorkCompletion(); |
| 1064 | } |
| 1065 | if (!cbContext.fResult) { |
| 1066 | ERRORF(reporter, "Callback failed on %s. is YUV: %d", |
| 1067 | sk_gpu_test::GrContextFactory::ContextTypeName(type), yuv); |
| 1068 | continue; |
| 1069 | } |
| 1070 | // The real test is that we don't crash, get Vulkan validation errors, etc, during |
| 1071 | // this shutdown sequence. |
| 1072 | switch (sequence) { |
| 1073 | case ShutdownSequence::kFreeResult_DestroyContext: |
| 1074 | case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext: |
| 1075 | case ShutdownSequence::kFreeResult_Abandon_DestroyContext: |
| 1076 | break; |
| 1077 | case ShutdownSequence::kDestroyContext_FreeResult: |
| 1078 | factory.destroyContexts(); |
| 1079 | break; |
| 1080 | case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext: |
| 1081 | factory.releaseResourcesAndAbandonContexts(); |
| 1082 | break; |
| 1083 | case ShutdownSequence::kAbandon_FreeResult_DestroyContext: |
| 1084 | factory.abandonContexts(); |
| 1085 | break; |
| 1086 | case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult: |
| 1087 | factory.releaseResourcesAndAbandonContexts(); |
| 1088 | factory.destroyContexts(); |
| 1089 | break; |
| 1090 | case ShutdownSequence::kAbandon_DestroyContext_FreeResult: |
| 1091 | factory.abandonContexts(); |
| 1092 | factory.destroyContexts(); |
| 1093 | break; |
| 1094 | } |
| 1095 | cbContext.fResult.reset(); |
| 1096 | switch (sequence) { |
| 1097 | case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext: |
| 1098 | factory.releaseResourcesAndAbandonContexts(); |
| 1099 | break; |
| 1100 | case ShutdownSequence::kFreeResult_Abandon_DestroyContext: |
| 1101 | factory.abandonContexts(); |
| 1102 | break; |
| 1103 | case ShutdownSequence::kFreeResult_DestroyContext: |
| 1104 | case ShutdownSequence::kDestroyContext_FreeResult: |
| 1105 | case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext: |
| 1106 | case ShutdownSequence::kAbandon_FreeResult_DestroyContext: |
| 1107 | case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult: |
| 1108 | case ShutdownSequence::kAbandon_DestroyContext_FreeResult: |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | } |