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 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 9 | #include "SkColorPriv.h" |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 10 | #include "SkMathPriv.h" |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 11 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 12 | #include "Test.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 15 | #include "GrContext.h" |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 16 | #include "SkGr.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 17 | #endif |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 18 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 19 | #include <initializer_list> |
| 20 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 21 | static const int DEV_W = 100, DEV_H = 100; |
| 22 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 23 | 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] | 24 | DEV_H * SK_Scalar1); |
| 25 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 26 | static SkPMColor get_src_color(int x, int y) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 27 | SkASSERT(x >= 0 && x < DEV_W); |
| 28 | SkASSERT(y >= 0 && y < DEV_H); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 29 | |
| 30 | U8CPU r = x; |
| 31 | U8CPU g = y; |
| 32 | U8CPU b = 0xc; |
| 33 | |
| 34 | U8CPU a = 0xff; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 35 | switch ((x+y) % 5) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 36 | case 0: |
| 37 | a = 0xff; |
| 38 | break; |
| 39 | case 1: |
| 40 | a = 0x80; |
| 41 | break; |
| 42 | case 2: |
| 43 | a = 0xCC; |
| 44 | break; |
| 45 | case 4: |
| 46 | a = 0x01; |
| 47 | break; |
| 48 | case 3: |
| 49 | a = 0x00; |
| 50 | break; |
| 51 | } |
| 52 | return SkPremultiplyARGBInline(a, r, g, b); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 53 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 54 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 55 | 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] | 56 | int n = y * w + x; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 58 | U8CPU b = n & 0xff; |
| 59 | U8CPU g = (n >> 8) & 0xff; |
| 60 | U8CPU r = (n >> 16) & 0xff; |
| 61 | return SkPackARGB32(0xff, r, g , b); |
| 62 | } |
| 63 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 64 | static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr, |
| 65 | bool* doUnpremul) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 66 | *doUnpremul = (kUnpremul_SkAlphaType == at); |
| 67 | |
| 68 | const uint8_t* c = reinterpret_cast<const uint8_t*>(addr); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 69 | U8CPU a,r,g,b; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 70 | switch (ct) { |
| 71 | case kBGRA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 72 | b = static_cast<U8CPU>(c[0]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 73 | g = static_cast<U8CPU>(c[1]); |
| 74 | r = static_cast<U8CPU>(c[2]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 75 | a = static_cast<U8CPU>(c[3]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 76 | break; |
| 77 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 78 | r = static_cast<U8CPU>(c[0]); |
| 79 | g = static_cast<U8CPU>(c[1]); |
| 80 | b = static_cast<U8CPU>(c[2]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 81 | a = static_cast<U8CPU>(c[3]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 82 | break; |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 83 | default: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 84 | SkDEBUGFAIL("Unexpected colortype"); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 85 | return 0; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 86 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 87 | |
| 88 | if (*doUnpremul) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 89 | r = SkMulDiv255Ceiling(r, a); |
| 90 | g = SkMulDiv255Ceiling(g, a); |
| 91 | b = SkMulDiv255Ceiling(b, a); |
| 92 | } |
| 93 | return SkPackARGB32(a, r, g, b); |
| 94 | } |
| 95 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 96 | static SkBitmap make_src_bitmap() { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 97 | static SkBitmap bmp; |
| 98 | if (bmp.isNull()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 99 | bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 100 | intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
| 101 | for (int y = 0; y < DEV_H; ++y) { |
| 102 | for (int x = 0; x < DEV_W; ++x) { |
| 103 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 104 | *pixel = get_src_color(x, y); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 108 | return bmp; |
| 109 | } |
| 110 | |
| 111 | static void fill_src_canvas(SkCanvas* canvas) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 112 | canvas->save(); |
| 113 | canvas->setMatrix(SkMatrix::I()); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 114 | canvas->clipRect(DEV_RECT_S, SkCanvas::kReplace_Op); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 115 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 116 | paint.setBlendMode(SkBlendMode::kSrc); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 117 | canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 118 | canvas->restore(); |
| 119 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 120 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 121 | #if SK_SUPPORT_GPU |
| 122 | static void fill_src_texture(GrTexture* texture) { |
| 123 | SkBitmap bmp = make_src_bitmap(); |
| 124 | bmp.lockPixels(); |
| 125 | texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(), |
| 126 | bmp.rowBytes()); |
| 127 | bmp.unlockPixels(); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 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 | SkAutoLockPixels alp(*bitmap); |
| 133 | int w = bitmap->width(); |
| 134 | int h = bitmap->height(); |
| 135 | intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
| 136 | for (int y = 0; y < h; ++y) { |
| 137 | for (int x = 0; x < w; ++x) { |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 138 | SkPMColor initColor = get_dst_bmp_init_color(x, y, w); |
| 139 | if (kAlpha_8_SkColorType == bitmap->colorType()) { |
| 140 | uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x); |
| 141 | *alpha = SkGetPackedA32(initColor); |
| 142 | } else { |
| 143 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); |
| 144 | *pixel = initColor; |
| 145 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 150 | static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 151 | if (!didPremulConversion) { |
| 152 | return a == b; |
| 153 | } |
| 154 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 155 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 156 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 157 | int32_t aB = SkGetPackedB32(a); |
| 158 | |
| 159 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 160 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 161 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 162 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 163 | |
| 164 | return aA == bA && |
| 165 | SkAbs32(aR - bR) <= 1 && |
| 166 | SkAbs32(aG - bG) <= 1 && |
| 167 | SkAbs32(aB - bB) <= 1; |
| 168 | } |
| 169 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 170 | // checks the bitmap contains correct pixels after the readPixels |
| 171 | // if the bitmap was prefilled with pixels it checks that these weren't |
| 172 | // overwritten in the area outside the readPixels. |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 173 | static bool check_read(skiatest::Reporter* reporter, |
| 174 | const SkBitmap& bitmap, |
| 175 | int x, int y, |
| 176 | bool checkCanvasPixels, |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 177 | bool checkBitmapPixels, |
| 178 | SkColorType ct, |
| 179 | SkAlphaType at) { |
| 180 | SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 181 | SkASSERT(!bitmap.isNull()); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 182 | SkASSERT(checkCanvasPixels || checkBitmapPixels); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 183 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 184 | int bw = bitmap.width(); |
| 185 | int bh = bitmap.height(); |
| 186 | |
| 187 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); |
| 188 | SkIRect clippedSrcRect = DEV_RECT; |
| 189 | if (!clippedSrcRect.intersect(srcRect)) { |
| 190 | clippedSrcRect.setEmpty(); |
| 191 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 192 | SkAutoLockPixels alp(bitmap); |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 193 | if (kAlpha_8_SkColorType == ct) { |
| 194 | for (int by = 0; by < bh; ++by) { |
| 195 | for (int bx = 0; bx < bw; ++bx) { |
| 196 | int devx = bx + srcRect.fLeft; |
| 197 | int devy = by + srcRect.fTop; |
| 198 | const uint8_t* alpha = bitmap.getAddr8(bx, by); |
| 199 | |
| 200 | if (clippedSrcRect.contains(devx, devy)) { |
| 201 | if (checkCanvasPixels) { |
| 202 | uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy)); |
| 203 | if (canvasAlpha != *alpha) { |
| 204 | ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ", |
| 205 | bx, by, canvasAlpha, *alpha); |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | } else if (checkBitmapPixels) { |
| 210 | uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw)); |
| 211 | if (origDstAlpha != *alpha) { |
| 212 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 213 | "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha); |
| 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | return true; |
| 220 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 221 | for (int by = 0; by < bh; ++by) { |
| 222 | for (int bx = 0; bx < bw; ++bx) { |
| 223 | int devx = bx + srcRect.fLeft; |
| 224 | int devy = by + srcRect.fTop; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 225 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 226 | const uint32_t* pixel = bitmap.getAddr32(bx, by); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 227 | |
| 228 | if (clippedSrcRect.contains(devx, devy)) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 229 | if (checkCanvasPixels) { |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 230 | SkPMColor canvasPixel = get_src_color(devx, devy); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 231 | bool didPremul; |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 232 | SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 233 | if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) { |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 234 | ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. " |
| 235 | "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 236 | return false; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 237 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 238 | } |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 239 | } else if (checkBitmapPixels) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 240 | uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw); |
| 241 | if (origDstPixel != *pixel) { |
| 242 | ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " |
| 243 | "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 244 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 249 | return true; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | enum BitmapInit { |
| 253 | kFirstBitmapInit = 0, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 254 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 255 | kNoPixels_BitmapInit = kFirstBitmapInit, |
| 256 | kTight_BitmapInit, |
| 257 | kRowBytes_BitmapInit, |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 258 | kRowBytesOdd_BitmapInit, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 259 | |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 260 | kLastAligned_BitmapInit = kRowBytes_BitmapInit, |
Brian Salomon | a64afd6 | 2016-02-01 16:44:22 -0500 | [diff] [blame] | 261 | |
| 262 | #if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 263 | kLast_BitmapInit = kRowBytesOdd_BitmapInit |
Brian Salomon | a64afd6 | 2016-02-01 16:44:22 -0500 | [diff] [blame] | 264 | #else |
| 265 | kLast_BitmapInit = kLastAligned_BitmapInit |
| 266 | #endif |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 267 | }; |
| 268 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 269 | static BitmapInit nextBMI(BitmapInit bmi) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 270 | int x = bmi; |
| 271 | return static_cast<BitmapInit>(++x); |
| 272 | } |
| 273 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 274 | static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct, |
| 275 | SkAlphaType at) { |
| 276 | SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 277 | size_t rowBytes = 0; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 278 | bool alloc = true; |
| 279 | switch (init) { |
| 280 | case kNoPixels_BitmapInit: |
| 281 | alloc = false; |
| 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 | } |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 294 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 295 | if (alloc) { |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 296 | bitmap->allocPixels(info, rowBytes); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 297 | } else { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 298 | bitmap->setInfo(info, rowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 302 | static const struct { |
| 303 | SkColorType fColorType; |
| 304 | SkAlphaType fAlphaType; |
| 305 | } gReadPixelsConfigs[] = { |
| 306 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
| 307 | { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 308 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
| 309 | { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 310 | { kAlpha_8_SkColorType, kPremul_SkAlphaType }, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 311 | }; |
| 312 | const SkIRect gReadPixelsTestRects[] = { |
| 313 | // entire thing |
| 314 | DEV_RECT, |
| 315 | // larger on all sides |
| 316 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 317 | // fully contained |
| 318 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 319 | // outside top left |
| 320 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 321 | // touching top left corner |
| 322 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 323 | // overlapping top left corner |
| 324 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 325 | // overlapping top left and top right corners |
| 326 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 327 | // touching entire top edge |
| 328 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 329 | // overlapping top right corner |
| 330 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 331 | // contained in x, overlapping top edge |
| 332 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 333 | // outside top right corner |
| 334 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 335 | // touching top right corner |
| 336 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 337 | // overlapping top left and bottom left corners |
| 338 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 339 | // touching entire left edge |
| 340 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 341 | // overlapping bottom left corner |
| 342 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 343 | // contained in y, overlapping left edge |
| 344 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 345 | // outside bottom left corner |
| 346 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 347 | // touching bottom left corner |
| 348 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 349 | // overlapping bottom left and bottom right corners |
| 350 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 351 | // touching entire left edge |
| 352 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 353 | // overlapping bottom right corner |
| 354 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 355 | // overlapping top right and bottom right corners |
| 356 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 357 | }; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 358 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 359 | static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 360 | BitmapInit lastBitmapInit) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 361 | SkCanvas* canvas = surface->getCanvas(); |
| 362 | fill_src_canvas(canvas); |
| 363 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 364 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 365 | for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 366 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 367 | SkBitmap bmp; |
| 368 | init_bitmap(&bmp, srcRect, bmi, |
| 369 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 370 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 371 | // if the bitmap has pixels allocated before the readPixels, |
| 372 | // note that and fill them with pattern |
| 373 | bool startsWithPixels = !bmp.isNull(); |
| 374 | if (startsWithPixels) { |
| 375 | fill_dst_bmp_with_init_data(&bmp); |
| 376 | } |
| 377 | uint32_t idBefore = surface->generationID(); |
| 378 | bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop); |
| 379 | uint32_t idAfter = surface->generationID(); |
| 380 | |
| 381 | // we expect to succeed when the read isn't fully clipped |
| 382 | // out. |
| 383 | bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT); |
| 384 | // determine whether we expected the read to succeed. |
| 385 | REPORTER_ASSERT(reporter, success == expectSuccess); |
| 386 | // read pixels should never change the gen id |
| 387 | REPORTER_ASSERT(reporter, idBefore == idAfter); |
| 388 | |
| 389 | if (success || startsWithPixels) { |
| 390 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 391 | success, startsWithPixels, |
| 392 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 393 | } else { |
| 394 | // if we had no pixels beforehand and the readPixels |
| 395 | // failed then our bitmap should still not have pixels |
| 396 | REPORTER_ASSERT(reporter, bmp.isNull()); |
| 397 | } |
| 398 | } |
| 399 | // check the old webkit version of readPixels that clips the |
| 400 | // bitmap size |
| 401 | SkBitmap wkbmp; |
| 402 | bool success = canvas->readPixels(srcRect, &wkbmp); |
| 403 | SkIRect clippedRect = DEV_RECT; |
| 404 | if (clippedRect.intersect(srcRect)) { |
| 405 | REPORTER_ASSERT(reporter, success); |
| 406 | REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType()); |
| 407 | REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType()); |
| 408 | check_read(reporter, wkbmp, clippedRect.fLeft, |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 409 | clippedRect.fTop, true, false, |
| 410 | kN32_SkColorType, kPremul_SkAlphaType); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 411 | } else { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 412 | REPORTER_ASSERT(reporter, !success); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 413 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | DEF_TEST(ReadPixels, reporter) { |
| 418 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 419 | auto surface(SkSurface::MakeRaster(info)); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 420 | // SW readback fails a premul check when reading back to an unaligned rowbytes. |
| 421 | test_readpixels(reporter, surface, kLastAligned_BitmapInit); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 422 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 423 | #if SK_SUPPORT_GPU |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 424 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) { |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 425 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 426 | for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 427 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, |
| 428 | ii, 0, origin, nullptr)); |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 429 | test_readpixels(reporter, surface, kLast_BitmapInit); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 432 | #endif |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 433 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 434 | #if SK_SUPPORT_GPU |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 435 | static void test_readpixels_texture(skiatest::Reporter* reporter, GrTexture* texture) { |
| 436 | fill_src_texture(texture); |
| 437 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { |
| 438 | const SkIRect& srcRect = gReadPixelsTestRects[rect]; |
bsalomon | 9d02b26 | 2016-02-01 12:49:30 -0800 | [diff] [blame] | 439 | for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 440 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { |
| 441 | SkBitmap bmp; |
| 442 | init_bitmap(&bmp, srcRect, bmi, |
| 443 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
| 444 | |
| 445 | // if the bitmap has pixels allocated before the readPixels, |
| 446 | // note that and fill them with pattern |
| 447 | bool startsWithPixels = !bmp.isNull(); |
| 448 | // Try doing the read directly from a non-renderable texture |
| 449 | if (startsWithPixels) { |
| 450 | fill_dst_bmp_with_init_data(&bmp); |
| 451 | GrPixelConfig dstConfig = |
| 452 | SkImageInfo2GrPixelConfig(gReadPixelsConfigs[c].fColorType, |
| 453 | gReadPixelsConfigs[c].fAlphaType, |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 454 | nullptr, |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 455 | *texture->getContext()->caps()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 456 | uint32_t flags = 0; |
| 457 | if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) { |
| 458 | flags = GrContext::kUnpremul_PixelOpsFlag; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 459 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 460 | bmp.lockPixels(); |
| 461 | bool success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(), |
| 462 | bmp.height(), dstConfig, bmp.getPixels(), |
| 463 | bmp.rowBytes(), flags); |
| 464 | bmp.unlockPixels(); |
| 465 | check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, |
lsalzman | a2415ac | 2016-10-11 14:29:12 -0700 | [diff] [blame] | 466 | success, true, |
| 467 | gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 473 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 474 | // 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^] | 475 | for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { |
| 476 | for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) { |
| 477 | GrSurfaceDesc desc; |
| 478 | desc.fFlags = flags; |
| 479 | desc.fWidth = DEV_W; |
| 480 | desc.fHeight = DEV_H; |
| 481 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 482 | desc.fOrigin = origin; |
| 483 | sk_sp<GrTexture> texture(ctxInfo.grContext()->textureProvider()->createTexture(desc, |
| 484 | SkBudgeted::kNo)); |
| 485 | test_readpixels_texture(reporter, texture.get()); |
| 486 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | #endif |