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