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 | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 474 | context, renderable, DEV_W, DEV_H, bmp.colorType(), bmp.alphaType(), origin, |
| 475 | bmp.getPixels(), bmp.rowBytes()); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 476 | auto sContext = context->priv().makeWrappedSurfaceContext( |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 477 | std::move(proxy), SkColorTypeToGrColorType(bmp.colorType()), |
| 478 | kPremul_SkAlphaType); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 479 | auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType); |
| 480 | test_readpixels_texture(reporter, std::move(sContext), info); |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 481 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 482 | } |
| 483 | } |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 484 | |
| 485 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 486 | |
| 487 | static const uint32_t kNumPixels = 5; |
| 488 | |
| 489 | // The five reference pixels are: red, green, blue, white, black. |
| 490 | // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector |
| 491 | // plus a tail pixel. |
| 492 | static const uint32_t rgba[kNumPixels] = { |
| 493 | 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000 |
| 494 | }; |
| 495 | static const uint32_t bgra[kNumPixels] = { |
| 496 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000 |
| 497 | }; |
| 498 | static const uint16_t rgb565[kNumPixels] = { |
| 499 | SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0 |
| 500 | }; |
| 501 | |
| 502 | static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F }; |
| 503 | |
| 504 | static const uint64_t kRed = (uint64_t) SK_Half1 << 0; |
| 505 | static const uint64_t kGreen = (uint64_t) SK_Half1 << 16; |
| 506 | static const uint64_t kBlue = (uint64_t) SK_Half1 << 32; |
| 507 | static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48; |
| 508 | static const uint64_t f16[kNumPixels] = { |
| 509 | kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha |
| 510 | }; |
| 511 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 512 | static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 513 | static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 514 | |
| 515 | static const void* five_reference_pixels(SkColorType colorType) { |
| 516 | switch (colorType) { |
| 517 | case kUnknown_SkColorType: |
| 518 | return nullptr; |
| 519 | case kAlpha_8_SkColorType: |
| 520 | return alpha8; |
| 521 | case kRGB_565_SkColorType: |
| 522 | return rgb565; |
| 523 | case kARGB_4444_SkColorType: |
| 524 | return rgba4444; |
| 525 | case kRGBA_8888_SkColorType: |
| 526 | return rgba; |
| 527 | case kBGRA_8888_SkColorType: |
| 528 | return bgra; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 529 | case kGray_8_SkColorType: |
| 530 | return gray8; |
| 531 | case kRGBA_F16_SkColorType: |
| 532 | return f16; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 533 | default: |
Leon Scroggins III | d6832d0 | 2018-09-04 11:10:04 -0400 | [diff] [blame] | 534 | return nullptr; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | SkASSERT(false); |
| 538 | return nullptr; |
| 539 | } |
| 540 | |
| 541 | static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo, |
| 542 | const SkImageInfo& srcInfo) { |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 543 | if (!SkImageInfoIsValid(srcInfo)) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 547 | const void* srcPixels = five_reference_pixels(srcInfo.colorType()); |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 548 | SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 549 | sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr); |
| 550 | REPORTER_ASSERT(r, src); |
| 551 | |
| 552 | // Enough space for 5 pixels when color type is F16, more than enough space in other cases. |
| 553 | uint64_t dstPixels[kNumPixels]; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 554 | SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 555 | bool success = src->readPixels(dstPixmap, 0, 0); |
| 556 | REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo)); |
| 557 | |
| 558 | if (success) { |
| 559 | if (kGray_8_SkColorType == srcInfo.colorType() && |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 560 | kGray_8_SkColorType != dstInfo.colorType()) { |
| 561 | // TODO: test (r,g,b) == (gray,gray,gray)? |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | if (kGray_8_SkColorType == dstInfo.colorType() && |
| 566 | kGray_8_SkColorType != srcInfo.colorType()) { |
| 567 | // TODO: test gray = luminance? |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | if (kAlpha_8_SkColorType == srcInfo.colorType() && |
| 572 | kAlpha_8_SkColorType != dstInfo.colorType()) { |
| 573 | // TODO: test output = black with this alpha? |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
| 577 | REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()), |
| 578 | kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType()))); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | DEF_TEST(ReadPixels_ValidConversion, reporter) { |
| 583 | const SkColorType kColorTypes[] = { |
| 584 | kUnknown_SkColorType, |
| 585 | kAlpha_8_SkColorType, |
| 586 | kRGB_565_SkColorType, |
| 587 | kARGB_4444_SkColorType, |
| 588 | kRGBA_8888_SkColorType, |
| 589 | kBGRA_8888_SkColorType, |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 590 | kGray_8_SkColorType, |
| 591 | kRGBA_F16_SkColorType, |
| 592 | }; |
| 593 | |
| 594 | const SkAlphaType kAlphaTypes[] = { |
| 595 | kUnknown_SkAlphaType, |
| 596 | kOpaque_SkAlphaType, |
| 597 | kPremul_SkAlphaType, |
| 598 | kUnpremul_SkAlphaType, |
| 599 | }; |
| 600 | |
| 601 | const sk_sp<SkColorSpace> kColorSpaces[] = { |
| 602 | nullptr, |
| 603 | SkColorSpace::MakeSRGB(), |
| 604 | }; |
| 605 | |
| 606 | for (SkColorType dstCT : kColorTypes) { |
| 607 | for (SkAlphaType dstAT: kAlphaTypes) { |
| 608 | for (sk_sp<SkColorSpace> dstCS : kColorSpaces) { |
| 609 | for (SkColorType srcCT : kColorTypes) { |
| 610 | for (SkAlphaType srcAT: kAlphaTypes) { |
| 611 | for (sk_sp<SkColorSpace> srcCS : kColorSpaces) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 612 | test_conversion(reporter, |
| 613 | SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS), |
| 614 | SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS)); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 622 | |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 623 | static int min_rgb_channel_bits(SkColorType ct) { |
| 624 | switch (ct) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 625 | case kUnknown_SkColorType: return 0; |
| 626 | case kAlpha_8_SkColorType: return 8; |
| 627 | case kA16_unorm_SkColorType: return 16; |
| 628 | case kA16_float_SkColorType: return 16; |
| 629 | case kRGB_565_SkColorType: return 5; |
| 630 | case kARGB_4444_SkColorType: return 4; |
| 631 | case kR8G8_unorm_SkColorType: return 8; |
| 632 | case kR16G16_unorm_SkColorType: return 16; |
| 633 | case kR16G16_float_SkColorType: return 16; |
| 634 | case kRGBA_8888_SkColorType: return 8; |
| 635 | case kRGB_888x_SkColorType: return 8; |
| 636 | case kBGRA_8888_SkColorType: return 8; |
| 637 | case kRGBA_1010102_SkColorType: return 10; |
| 638 | case kRGB_101010x_SkColorType: return 10; |
| 639 | case kGray_8_SkColorType: return 8; // counting gray as "rgb" |
| 640 | case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa |
| 641 | case kRGBA_F16_SkColorType: return 10; // just counting the mantissa |
| 642 | case kRGBA_F32_SkColorType: return 23; // just counting the mantissa |
| 643 | case kR16G16B16A16_unorm_SkColorType: return 16; |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 644 | } |
| 645 | SK_ABORT("Unexpected color type."); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 648 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) { |
| 649 | struct Context { |
| 650 | SkPixmap* fPixmap = nullptr; |
| 651 | bool fSuceeded = false; |
| 652 | bool fCalled = false; |
| 653 | }; |
| 654 | auto callback = [](SkSurface::ReleaseContext context, const void* data, size_t rowBytes) { |
| 655 | auto* pm = static_cast<Context*>(context)->fPixmap; |
| 656 | static_cast<Context*>(context)->fCalled = true; |
| 657 | if ((static_cast<Context*>(context)->fSuceeded = SkToBool(data))) { |
| 658 | auto dst = static_cast<char*>(pm->writable_addr()); |
| 659 | const auto* src = static_cast<const char*>(data); |
| 660 | for (int y = 0; y < pm->height(); ++y, src += rowBytes, dst += pm->rowBytes()) { |
| 661 | memcpy(dst, src, pm->width() * SkColorTypeBytesPerPixel(pm->colorType())); |
| 662 | } |
| 663 | } |
| 664 | }; |
| 665 | for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 666 | static constexpr int kW = 16; |
| 667 | static constexpr int kH = 16; |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 668 | for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) { |
| 669 | auto surfCT = static_cast<SkColorType>(sct); |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 670 | auto info = SkImageInfo::Make(kW, kH, surfCT, kPremul_SkAlphaType, |
| 671 | SkColorSpace::MakeSRGB()); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 672 | auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info, 1, |
| 673 | origin, nullptr); |
| 674 | if (!surf) { |
| 675 | continue; |
| 676 | } |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 677 | auto refSurf = SkSurface::MakeRaster(info); |
| 678 | SkNWayCanvas nway(info.width(), info.height()); |
| 679 | nway.addCanvas(surf->getCanvas()); |
| 680 | nway.addCanvas(refSurf->getCanvas()); |
| 681 | |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 682 | float d = std::sqrt((float)surf->width() * surf->width() + |
| 683 | (float)surf->height() * surf->height()); |
| 684 | for (int j = 0; j < surf->height(); ++j) { |
| 685 | for (int i = 0; i < surf->width(); ++i) { |
| 686 | float r = i / (float)surf->width(); |
| 687 | float g = 1.f - i / (float)surf->height(); |
| 688 | float b = std::sqrt((float)i * i + (float)j * j) / d; |
| 689 | SkPaint paint; |
| 690 | paint.setColor4f(SkColor4f{r, g, b, 1.f}, nullptr); |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 691 | nway.drawRect(SkRect::MakeXYWH(i, j, 1, 1), paint); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 692 | } |
| 693 | } |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 694 | for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) { |
| 695 | auto readCT = static_cast<SkColorType>(rct); |
| 696 | |
| 697 | for (const sk_sp<SkColorSpace>& readCS : |
| 698 | {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) { |
| 699 | auto refImg = refSurf->makeImageSnapshot()->makeColorTypeAndColorSpace(readCT, |
| 700 | readCS); |
| 701 | // Test full size, partial, empty, and too wide rects. |
| 702 | for (const auto& rect : {SkIRect::MakeWH(kW, kH), |
| 703 | SkIRect::MakeLTRB(1, 2, kW - 3, kH - 4), |
| 704 | SkIRect::MakeXYWH(1, 1, 0, 0), |
| 705 | SkIRect::MakeWH(kW + 1, kH / 2)}) { |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 706 | SkAutoPixmapStorage result; |
| 707 | Context context; |
| 708 | context.fPixmap = &result; |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 709 | info = SkImageInfo::Make(rect.size(), readCT, kPremul_SkAlphaType, readCS); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 710 | result.alloc(info); |
| 711 | memset(result.writable_addr(), 0xAB, result.computeByteSize()); |
Brian Salomon | 201700f | 2019-05-17 12:05:44 -0400 | [diff] [blame] | 712 | // Rescale quality and linearity don't matter since we're doing a non- |
| 713 | // scaling readback. |
| 714 | surf->asyncRescaleAndReadPixels(info, rect, SkSurface::RescaleGamma::kSrc, |
| 715 | kNone_SkFilterQuality, callback, &context); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 716 | while (!context.fCalled) { |
| 717 | ctxInfo.grContext()->checkAsyncWorkCompletion(); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 718 | } |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 719 | if (rect.isEmpty() || !SkIRect::MakeWH(kW, kH).contains(rect)) { |
| 720 | REPORTER_ASSERT(reporter, !context.fSuceeded); |
| 721 | } |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 722 | |
| 723 | bool didCSConversion = |
| 724 | !SkColorSpace::Equals(readCS.get(), surf->imageInfo().colorSpace()); |
| 725 | |
Brian Salomon | 624f906 | 2019-07-02 14:23:00 -0400 | [diff] [blame] | 726 | if (context.fSuceeded) { |
| 727 | REPORTER_ASSERT(reporter, readCT != kUnknown_SkColorType && |
| 728 | !rect.isEmpty()); |
| 729 | } else { |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 730 | // TODO: Support reading to kGray, support kRGB_101010x at all in GPU. |
Brian Salomon | 624f906 | 2019-07-02 14:23:00 -0400 | [diff] [blame] | 731 | auto surfBounds = SkIRect::MakeWH(surf->width(), surf->height()); |
| 732 | if (readCT != kUnknown_SkColorType && readCT != kGray_8_SkColorType && |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 733 | readCT != kRGB_101010x_SkColorType && !rect.isEmpty() && |
| 734 | surfBounds.contains(rect)) { |
Brian Salomon | 624f906 | 2019-07-02 14:23:00 -0400 | [diff] [blame] | 735 | ERRORF(reporter, |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 736 | "Async read failed. Surf Color Type: %s, Read CT: %s," |
Brian Salomon | 624f906 | 2019-07-02 14:23:00 -0400 | [diff] [blame] | 737 | "Rect [%d, %d, %d, %d], origin: %d, CS conversion: %d\n", |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 738 | ToolUtils::colortype_name(surfCT), |
| 739 | ToolUtils::colortype_name(readCT), |
| 740 | rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, origin, |
| 741 | didCSConversion); |
Brian Salomon | 624f906 | 2019-07-02 14:23:00 -0400 | [diff] [blame] | 742 | } |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 743 | continue; |
| 744 | } |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 745 | SkPixmap ref; |
| 746 | refImg->peekPixels(&ref); |
| 747 | SkAssertResult(ref.extractSubset(&ref, rect)); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 748 | |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 749 | // A CS conversion allows a 3 value difference and otherwise a 2 value |
| 750 | // difference. Note that sometimes read back on GPU can be lossy even when |
| 751 | // there no conversion at all because GPU->CPU read may go to a a lower bit |
| 752 | // depth format and then be promoted back to the original type. For example, |
| 753 | // GL ES cannot read to 1010102, so we go through 8888. |
| 754 | float numer = didCSConversion ? 3.f : 2.f; |
| 755 | int rgbBits = std::min({min_rgb_channel_bits(readCT), |
| 756 | min_rgb_channel_bits(surfCT), 8}); |
| 757 | float tol = numer / (1 << rgbBits); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 758 | const float tols[4] = {tol, tol, tol, 0}; |
| 759 | auto error = std::function<ComparePixmapsErrorReporter>( |
| 760 | [&](int x, int y, const float diffs[4]) { |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 761 | SkASSERT(x >= 0 && y >= 0); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 762 | ERRORF(reporter, |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 763 | "Surf Color Type: %s, Read CT: %s, Rect [%d, %d, %d, %d]" |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 764 | ", origin: %d, CS conversion: %d\n" |
| 765 | "Error at %d, %d. Diff in floats: (%f, %f, %f %f)", |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 766 | ToolUtils::colortype_name(surfCT), |
| 767 | ToolUtils::colortype_name(readCT), |
| 768 | rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, origin, |
| 769 | didCSConversion, x, y, diffs[0], diffs[1], diffs[2], |
| 770 | diffs[3]); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 771 | }); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 772 | compare_pixels(ref, result, tols, error); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | } |