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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 9 | #include "include/core/SkImage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/SkImageInfoPriv.h" |
Brian Salomon | 5dd7746 | 2019-09-26 16:57:09 -0400 | [diff] [blame] | 12 | #include "include/utils/SkNWayCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkMathPriv.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 14 | #include "tests/Test.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 15 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 16 | static const int DEV_W = 100, DEV_H = 100; |
| 17 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 18 | 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] | 19 | DEV_H * SK_Scalar1); |
| 20 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 21 | static SkPMColor get_src_color(int x, int y) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 22 | SkASSERT(x >= 0 && x < DEV_W); |
| 23 | SkASSERT(y >= 0 && y < DEV_H); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 24 | |
| 25 | U8CPU r = x; |
| 26 | U8CPU g = y; |
| 27 | U8CPU b = 0xc; |
| 28 | |
| 29 | U8CPU a = 0xff; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 30 | switch ((x+y) % 5) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 31 | case 0: |
| 32 | a = 0xff; |
| 33 | break; |
| 34 | case 1: |
| 35 | a = 0x80; |
| 36 | break; |
| 37 | case 2: |
| 38 | a = 0xCC; |
| 39 | break; |
| 40 | case 4: |
| 41 | a = 0x01; |
| 42 | break; |
| 43 | case 3: |
| 44 | a = 0x00; |
| 45 | break; |
| 46 | } |
| 47 | return SkPremultiplyARGBInline(a, r, g, b); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 48 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 49 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 50 | 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] | 51 | int n = y * w + x; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 52 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 53 | U8CPU b = n & 0xff; |
| 54 | U8CPU g = (n >> 8) & 0xff; |
| 55 | U8CPU r = (n >> 16) & 0xff; |
| 56 | return SkPackARGB32(0xff, r, g , b); |
| 57 | } |
| 58 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 59 | // TODO: Make this consider both ATs |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 60 | static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr, |
| 61 | bool* doUnpremul) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 62 | *doUnpremul = (kUnpremul_SkAlphaType == at); |
| 63 | |
| 64 | const uint8_t* c = reinterpret_cast<const uint8_t*>(addr); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 65 | U8CPU a,r,g,b; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 66 | switch (ct) { |
| 67 | case kBGRA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 68 | b = static_cast<U8CPU>(c[0]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 69 | g = static_cast<U8CPU>(c[1]); |
| 70 | r = static_cast<U8CPU>(c[2]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 71 | a = static_cast<U8CPU>(c[3]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 72 | break; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 73 | case kRGB_888x_SkColorType: // fallthrough |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 74 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 75 | r = static_cast<U8CPU>(c[0]); |
| 76 | g = static_cast<U8CPU>(c[1]); |
| 77 | b = static_cast<U8CPU>(c[2]); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 78 | // 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] | 79 | a = static_cast<U8CPU>(c[3]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 80 | break; |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 81 | default: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 82 | SkDEBUGFAIL("Unexpected colortype"); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 83 | return 0; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 84 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 85 | |
| 86 | if (*doUnpremul) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 87 | r = SkMulDiv255Ceiling(r, a); |
| 88 | g = SkMulDiv255Ceiling(g, a); |
| 89 | b = SkMulDiv255Ceiling(b, a); |
| 90 | } |
| 91 | return SkPackARGB32(a, r, g, b); |
| 92 | } |
| 93 | |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 94 | static sk_sp<SkImage> make_src_image() { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 95 | static SkBitmap bmp; |
| 96 | if (bmp.isNull()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 97 | bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 98 | intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
| 99 | for (int y = 0; y < DEV_H; ++y) { |
| 100 | for (int x = 0; x < DEV_W; ++x) { |
| 101 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 102 | *pixel = get_src_color(x, y); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 105 | bmp.setImmutable(); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 106 | } |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 107 | return bmp.asImage(); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void fill_src_canvas(SkCanvas* canvas) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 111 | canvas->save(); |
| 112 | canvas->setMatrix(SkMatrix::I()); |
Michael Ludwig | e4b7969 | 2020-09-16 13:55:05 -0400 | [diff] [blame] | 113 | canvas->clipRect(DEV_RECT_S, SkClipOp::kIntersect); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 114 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 115 | paint.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 116 | canvas->drawImage(make_src_image(), 0, 0, SkSamplingOptions(), &paint); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 117 | canvas->restore(); |
| 118 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 120 | static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 121 | int w = bitmap->width(); |
| 122 | int h = bitmap->height(); |
| 123 | intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
| 124 | for (int y = 0; y < h; ++y) { |
| 125 | for (int x = 0; x < w; ++x) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 126 | SkPMColor initColor = get_dst_bmp_init_color(x, y, w); |
| 127 | if (kAlpha_8_SkColorType == bitmap->colorType()) { |
| 128 | uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x); |
| 129 | *alpha = SkGetPackedA32(initColor); |
| 130 | } else { |
| 131 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); |
| 132 | *pixel = initColor; |
| 133 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 138 | static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 139 | if (!didPremulConversion) { |
| 140 | return a == b; |
| 141 | } |
| 142 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 143 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 144 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 145 | int32_t aB = SkGetPackedB32(a); |
| 146 | |
| 147 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 148 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 149 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 150 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 151 | |
| 152 | return aA == bA && |
| 153 | SkAbs32(aR - bR) <= 1 && |
| 154 | SkAbs32(aG - bG) <= 1 && |
| 155 | SkAbs32(aB - bB) <= 1; |
| 156 | } |
| 157 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 158 | // checks the bitmap contains correct pixels after the readPixels |
| 159 | // if the bitmap was prefilled with pixels it checks that these weren't |
| 160 | // overwritten in the area outside the readPixels. |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 161 | static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y, |
| 162 | bool checkSurfacePixels, bool checkBitmapPixels, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 163 | SkImageInfo surfaceInfo) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 164 | SkAlphaType bmpAT = bitmap.alphaType(); |
| 165 | SkColorType bmpCT = bitmap.colorType(); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 166 | SkASSERT(!bitmap.isNull()); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 167 | SkASSERT(checkSurfacePixels || checkBitmapPixels); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 168 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 169 | int bw = bitmap.width(); |
| 170 | int bh = bitmap.height(); |
| 171 | |
| 172 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); |
| 173 | SkIRect clippedSrcRect = DEV_RECT; |
| 174 | if (!clippedSrcRect.intersect(srcRect)) { |
| 175 | clippedSrcRect.setEmpty(); |
| 176 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 177 | if (kAlpha_8_SkColorType == bmpCT) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 178 | for (int by = 0; by < bh; ++by) { |
| 179 | for (int bx = 0; bx < bw; ++bx) { |
| 180 | int devx = bx + srcRect.fLeft; |
| 181 | int devy = by + srcRect.fTop; |
| 182 | const uint8_t* alpha = bitmap.getAddr8(bx, by); |
| 183 | |
| 184 | if (clippedSrcRect.contains(devx, devy)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 185 | if (checkSurfacePixels) { |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 186 | uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType) |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 187 | ? 0xFF |
| 188 | : SkGetPackedA32(get_src_color(devx, devy)); |
| 189 | if (surfaceAlpha != *alpha) { |
| 190 | ERRORF(reporter, |
| 191 | "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ", |
| 192 | bx, by, surfaceAlpha, *alpha); |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | } |
| 196 | } else if (checkBitmapPixels) { |
| 197 | uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw)); |
| 198 | if (origDstAlpha != *alpha) { |
| 199 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 200 | "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha); |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | return true; |
| 207 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 208 | for (int by = 0; by < bh; ++by) { |
| 209 | for (int bx = 0; bx < bw; ++bx) { |
| 210 | int devx = bx + srcRect.fLeft; |
| 211 | int devy = by + srcRect.fTop; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 212 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 213 | const uint32_t* pixel = bitmap.getAddr32(bx, by); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 214 | |
| 215 | if (clippedSrcRect.contains(devx, devy)) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 216 | if (checkSurfacePixels) { |
| 217 | SkPMColor surfacePMColor = get_src_color(devx, devy); |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 218 | if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) { |
| 219 | surfacePMColor &= 0xFF000000; |
| 220 | } |
| 221 | if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 222 | surfacePMColor |= 0xFF000000; |
| 223 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 224 | bool didPremul; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 225 | SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul); |
| 226 | if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) { |
| 227 | ERRORF(reporter, |
| 228 | "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. " |
| 229 | "Readback was unpremul: %d", |
| 230 | bx, by, surfacePMColor, pmPixel, didPremul); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 231 | return false; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 232 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 233 | } |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 234 | } else if (checkBitmapPixels) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 235 | uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw); |
| 236 | if (origDstPixel != *pixel) { |
| 237 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 238 | "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 239 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 244 | return true; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 247 | enum class TightRowBytes : bool { kNo, kYes }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 248 | |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 249 | static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB, |
| 250 | SkColorType ct, SkAlphaType at) { |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 251 | SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 252 | size_t rowBytes = 0; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 253 | if (tightRB == TightRowBytes::kNo) { |
| 254 | rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 255 | } |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 256 | bitmap->allocPixels(info, rowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 257 | } |
| 258 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 259 | static const struct { |
| 260 | SkColorType fColorType; |
| 261 | SkAlphaType fAlphaType; |
| 262 | } gReadPixelsConfigs[] = { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 263 | {kRGBA_8888_SkColorType, kPremul_SkAlphaType}, |
| 264 | {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 265 | {kRGB_888x_SkColorType, kOpaque_SkAlphaType}, |
| 266 | {kBGRA_8888_SkColorType, kPremul_SkAlphaType}, |
| 267 | {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 268 | {kAlpha_8_SkColorType, kPremul_SkAlphaType}, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 269 | }; |
| 270 | const SkIRect gReadPixelsTestRects[] = { |
| 271 | // entire thing |
| 272 | DEV_RECT, |
| 273 | // larger on all sides |
| 274 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 275 | // fully contained |
| 276 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 277 | // outside top left |
| 278 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 279 | // touching top left corner |
| 280 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 281 | // overlapping top left corner |
| 282 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 283 | // overlapping top left and top right corners |
| 284 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 285 | // touching entire top edge |
| 286 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 287 | // overlapping top right corner |
| 288 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 289 | // contained in x, overlapping top edge |
| 290 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 291 | // outside top right corner |
| 292 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 293 | // touching top right corner |
| 294 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 295 | // overlapping top left and bottom left corners |
| 296 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 297 | // touching entire left edge |
| 298 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 299 | // overlapping bottom left corner |
| 300 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 301 | // contained in y, overlapping left edge |
| 302 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 303 | // outside bottom left corner |
| 304 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 305 | // touching bottom left corner |
| 306 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 307 | // overlapping bottom left and bottom right corners |
| 308 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 309 | // touching entire left edge |
| 310 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 311 | // overlapping bottom right corner |
| 312 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 313 | // overlapping top right and bottom right corners |
| 314 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 315 | }; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 316 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 317 | bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo, |
| 318 | const SkImageInfo& srcInfo) { |
| 319 | return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 320 | } |
| 321 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 322 | static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 323 | const SkImageInfo& surfaceInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 324 | SkCanvas* canvas = surface->getCanvas(); |
| 325 | fill_src_canvas(canvas); |
| 326 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 327 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 328 | for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 329 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 330 | SkBitmap bmp; |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 331 | init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType, |
| 332 | gReadPixelsConfigs[c].fAlphaType); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 333 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 334 | // if the bitmap has pixels allocated before the readPixels, |
| 335 | // note that and fill them with pattern |
| 336 | bool startsWithPixels = !bmp.isNull(); |
| 337 | if (startsWithPixels) { |
| 338 | fill_dst_bmp_with_init_data(&bmp); |
| 339 | } |
| 340 | uint32_t idBefore = surface->generationID(); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 341 | bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 342 | uint32_t idAfter = surface->generationID(); |
| 343 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 344 | // we expect to succeed when the read isn't fully clipped out and the infos are |
| 345 | // compatible. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 346 | bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 347 | // determine whether we expected the read to succeed. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 348 | REPORTER_ASSERT(reporter, expectSuccess == success, |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 349 | "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", |
| 350 | success, surfaceInfo.colorType(), surfaceInfo.alphaType(), |
| 351 | bmp.info().colorType(), bmp.info().alphaType()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 352 | // read pixels should never change the gen id |
| 353 | REPORTER_ASSERT(reporter, idBefore == idAfter); |
| 354 | |
| 355 | if (success || startsWithPixels) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 356 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 357 | startsWithPixels, surfaceInfo); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 358 | } else { |
| 359 | // if we had no pixels beforehand and the readPixels |
| 360 | // failed then our bitmap should still not have pixels |
| 361 | REPORTER_ASSERT(reporter, bmp.isNull()); |
| 362 | } |
| 363 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 367 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 368 | DEF_TEST(ReadPixels, reporter) { |
| 369 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 370 | auto surface(SkSurface::MakeRaster(info)); |
Brian Salomon | 5918e83 | 2019-10-23 13:34:36 -0400 | [diff] [blame] | 371 | test_readpixels(reporter, surface, info); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 372 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 373 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 374 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 375 | |
| 376 | static const uint32_t kNumPixels = 5; |
| 377 | |
| 378 | // The five reference pixels are: red, green, blue, white, black. |
| 379 | // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector |
| 380 | // plus a tail pixel. |
| 381 | static const uint32_t rgba[kNumPixels] = { |
| 382 | 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000 |
| 383 | }; |
| 384 | static const uint32_t bgra[kNumPixels] = { |
| 385 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000 |
| 386 | }; |
| 387 | static const uint16_t rgb565[kNumPixels] = { |
| 388 | SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0 |
| 389 | }; |
| 390 | |
| 391 | static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F }; |
| 392 | |
| 393 | static const uint64_t kRed = (uint64_t) SK_Half1 << 0; |
| 394 | static const uint64_t kGreen = (uint64_t) SK_Half1 << 16; |
| 395 | static const uint64_t kBlue = (uint64_t) SK_Half1 << 32; |
| 396 | static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48; |
| 397 | static const uint64_t f16[kNumPixels] = { |
| 398 | kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha |
| 399 | }; |
| 400 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 401 | static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 402 | static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
| 403 | |
| 404 | static const void* five_reference_pixels(SkColorType colorType) { |
| 405 | switch (colorType) { |
| 406 | case kUnknown_SkColorType: |
| 407 | return nullptr; |
| 408 | case kAlpha_8_SkColorType: |
| 409 | return alpha8; |
| 410 | case kRGB_565_SkColorType: |
| 411 | return rgb565; |
| 412 | case kARGB_4444_SkColorType: |
| 413 | return rgba4444; |
| 414 | case kRGBA_8888_SkColorType: |
| 415 | return rgba; |
| 416 | case kBGRA_8888_SkColorType: |
| 417 | return bgra; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 418 | case kGray_8_SkColorType: |
| 419 | return gray8; |
| 420 | case kRGBA_F16_SkColorType: |
| 421 | return f16; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 422 | default: |
Leon Scroggins III | d6832d0 | 2018-09-04 11:10:04 -0400 | [diff] [blame] | 423 | return nullptr; |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | SkASSERT(false); |
| 427 | return nullptr; |
| 428 | } |
| 429 | |
| 430 | static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo, |
| 431 | const SkImageInfo& srcInfo) { |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 432 | if (!SkImageInfoIsValid(srcInfo)) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 436 | const void* srcPixels = five_reference_pixels(srcInfo.colorType()); |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 437 | SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 438 | sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr); |
| 439 | REPORTER_ASSERT(r, src); |
| 440 | |
| 441 | // Enough space for 5 pixels when color type is F16, more than enough space in other cases. |
| 442 | uint64_t dstPixels[kNumPixels]; |
Mike Reed | 304a07c | 2017-07-12 15:10:28 -0400 | [diff] [blame] | 443 | SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes()); |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 444 | bool success = src->readPixels(nullptr, dstPixmap, 0, 0); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 445 | REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo)); |
| 446 | |
| 447 | if (success) { |
| 448 | if (kGray_8_SkColorType == srcInfo.colorType() && |
Mike Klein | 12d4b6e | 2018-08-17 14:09:55 -0400 | [diff] [blame] | 449 | kGray_8_SkColorType != dstInfo.colorType()) { |
| 450 | // TODO: test (r,g,b) == (gray,gray,gray)? |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | if (kGray_8_SkColorType == dstInfo.colorType() && |
| 455 | kGray_8_SkColorType != srcInfo.colorType()) { |
| 456 | // TODO: test gray = luminance? |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | if (kAlpha_8_SkColorType == srcInfo.colorType() && |
| 461 | kAlpha_8_SkColorType != dstInfo.colorType()) { |
| 462 | // TODO: test output = black with this alpha? |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
| 466 | REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()), |
| 467 | kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType()))); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | DEF_TEST(ReadPixels_ValidConversion, reporter) { |
| 472 | const SkColorType kColorTypes[] = { |
| 473 | kUnknown_SkColorType, |
| 474 | kAlpha_8_SkColorType, |
| 475 | kRGB_565_SkColorType, |
| 476 | kARGB_4444_SkColorType, |
| 477 | kRGBA_8888_SkColorType, |
| 478 | kBGRA_8888_SkColorType, |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 479 | kGray_8_SkColorType, |
| 480 | kRGBA_F16_SkColorType, |
| 481 | }; |
| 482 | |
| 483 | const SkAlphaType kAlphaTypes[] = { |
| 484 | kUnknown_SkAlphaType, |
| 485 | kOpaque_SkAlphaType, |
| 486 | kPremul_SkAlphaType, |
| 487 | kUnpremul_SkAlphaType, |
| 488 | }; |
| 489 | |
| 490 | const sk_sp<SkColorSpace> kColorSpaces[] = { |
| 491 | nullptr, |
| 492 | SkColorSpace::MakeSRGB(), |
| 493 | }; |
| 494 | |
| 495 | for (SkColorType dstCT : kColorTypes) { |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 496 | for (SkAlphaType dstAT : kAlphaTypes) { |
| 497 | for (const sk_sp<SkColorSpace>& dstCS : kColorSpaces) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 498 | for (SkColorType srcCT : kColorTypes) { |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 499 | for (SkAlphaType srcAT : kAlphaTypes) { |
| 500 | for (const sk_sp<SkColorSpace>& srcCS : kColorSpaces) { |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 501 | test_conversion(reporter, |
| 502 | SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS), |
| 503 | SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS)); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
Brian Salomon | c24c8ef | 2021-02-01 13:32:30 -0500 | [diff] [blame] | 511 | |
| 512 | DEF_TEST(ReadPixels_InvalidRowBytes, reporter) { |
| 513 | auto srcII = SkImageInfo::Make({10, 10}, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 514 | auto surf = SkSurface::MakeRaster(srcII); |
| 515 | for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) { |
| 516 | auto colorType = static_cast<SkColorType>(ct); |
| 517 | size_t bpp = SkColorTypeBytesPerPixel(colorType); |
| 518 | if (bpp <= 1) { |
| 519 | continue; |
| 520 | } |
| 521 | auto dstII = srcII.makeColorType(colorType); |
| 522 | size_t badRowBytes = (surf->width() + 1)*bpp - 1; |
| 523 | auto storage = std::make_unique<char[]>(badRowBytes*surf->height()); |
| 524 | REPORTER_ASSERT(reporter, !surf->readPixels(dstII, storage.get(), badRowBytes, 0, 0)); |
| 525 | } |
| 526 | } |