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 | |
robertphillips@google.com | 7367225 | 2013-08-29 12:40:26 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 11 | #include "SkMathPriv.h" |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 12 | #include "SkRegion.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 |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 16 | #include "GrContextFactory.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 17 | #include "SkGpuDevice.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 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 20 | static const int DEV_W = 100, DEV_H = 100; |
| 21 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 22 | 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] | 23 | DEV_H * SK_Scalar1); |
| 24 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 25 | static SkPMColor getCanvasColor(int x, int y) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 26 | SkASSERT(x >= 0 && x < DEV_W); |
| 27 | SkASSERT(y >= 0 && y < DEV_H); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 28 | |
| 29 | U8CPU r = x; |
| 30 | U8CPU g = y; |
| 31 | U8CPU b = 0xc; |
| 32 | |
| 33 | U8CPU a = 0xff; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 34 | switch ((x+y) % 5) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 35 | case 0: |
| 36 | a = 0xff; |
| 37 | break; |
| 38 | case 1: |
| 39 | a = 0x80; |
| 40 | break; |
| 41 | case 2: |
| 42 | a = 0xCC; |
| 43 | break; |
| 44 | case 4: |
| 45 | a = 0x01; |
| 46 | break; |
| 47 | case 3: |
| 48 | a = 0x00; |
| 49 | break; |
| 50 | } |
| 51 | return SkPremultiplyARGBInline(a, r, g, b); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 52 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 54 | static SkPMColor getBitmapColor(int x, int y, int w) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 55 | int n = y * w + x; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 57 | U8CPU b = n & 0xff; |
| 58 | U8CPU g = (n >> 8) & 0xff; |
| 59 | U8CPU r = (n >> 16) & 0xff; |
| 60 | return SkPackARGB32(0xff, r, g , b); |
| 61 | } |
| 62 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 63 | static SkPMColor convertToPMColor(SkColorType ct, SkAlphaType at, const uint32_t* addr, |
| 64 | bool* doUnpremul) { |
| 65 | *doUnpremul = (kUnpremul_SkAlphaType == at); |
| 66 | |
| 67 | const uint8_t* c = reinterpret_cast<const uint8_t*>(addr); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 68 | U8CPU a,r,g,b; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 69 | switch (ct) { |
| 70 | case kBGRA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 71 | b = static_cast<U8CPU>(c[0]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 72 | g = static_cast<U8CPU>(c[1]); |
| 73 | r = static_cast<U8CPU>(c[2]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 74 | a = static_cast<U8CPU>(c[3]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 75 | break; |
| 76 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 77 | r = static_cast<U8CPU>(c[0]); |
| 78 | g = static_cast<U8CPU>(c[1]); |
| 79 | b = static_cast<U8CPU>(c[2]); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 80 | a = static_cast<U8CPU>(c[3]); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 81 | break; |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 82 | default: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 83 | SkDEBUGFAIL("Unexpected colortype"); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 84 | return 0; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 85 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 86 | |
| 87 | if (*doUnpremul) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 88 | r = SkMulDiv255Ceiling(r, a); |
| 89 | g = SkMulDiv255Ceiling(g, a); |
| 90 | b = SkMulDiv255Ceiling(b, a); |
| 91 | } |
| 92 | return SkPackARGB32(a, r, g, b); |
| 93 | } |
| 94 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 95 | static void fillCanvas(SkCanvas* canvas) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 96 | static SkBitmap bmp; |
| 97 | if (bmp.isNull()) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 98 | SkDEBUGCODE(bool alloc =) bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 99 | SkASSERT(alloc); |
| 100 | SkAutoLockPixels alp(bmp); |
| 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()); |
| 105 | *pixel = getCanvasColor(x, y); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | canvas->save(); |
| 110 | canvas->setMatrix(SkMatrix::I()); |
| 111 | canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op); |
| 112 | SkPaint paint; |
| 113 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 114 | canvas->drawBitmap(bmp, 0, 0, &paint); |
| 115 | canvas->restore(); |
| 116 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 117 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 118 | static void fillBitmap(SkBitmap* bitmap) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 119 | SkASSERT(bitmap->lockPixelsAreWritable()); |
| 120 | SkAutoLockPixels alp(*bitmap); |
| 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) { |
| 126 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 127 | *pixel = getBitmapColor(x, y, w); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 132 | static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 133 | if (!didPremulConversion) { |
| 134 | return a == b; |
| 135 | } |
| 136 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 137 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 138 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 139 | int32_t aB = SkGetPackedB32(a); |
| 140 | |
| 141 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 142 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 143 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 144 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 145 | |
| 146 | return aA == bA && |
| 147 | SkAbs32(aR - bR) <= 1 && |
| 148 | SkAbs32(aG - bG) <= 1 && |
| 149 | SkAbs32(aB - bB) <= 1; |
| 150 | } |
| 151 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 152 | // checks the bitmap contains correct pixels after the readPixels |
| 153 | // if the bitmap was prefilled with pixels it checks that these weren't |
| 154 | // overwritten in the area outside the readPixels. |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 155 | static bool checkRead(skiatest::Reporter* reporter, |
| 156 | const SkBitmap& bitmap, |
| 157 | int x, int y, |
| 158 | bool checkCanvasPixels, |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 159 | bool checkBitmapPixels) { |
| 160 | SkASSERT(4 == bitmap.bytesPerPixel()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 161 | SkASSERT(!bitmap.isNull()); |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 162 | SkASSERT(checkCanvasPixels || checkBitmapPixels); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 163 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 164 | const SkColorType ct = bitmap.colorType(); |
| 165 | const SkAlphaType at = bitmap.alphaType(); |
| 166 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 167 | int bw = bitmap.width(); |
| 168 | int bh = bitmap.height(); |
| 169 | |
| 170 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); |
| 171 | SkIRect clippedSrcRect = DEV_RECT; |
| 172 | if (!clippedSrcRect.intersect(srcRect)) { |
| 173 | clippedSrcRect.setEmpty(); |
| 174 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 175 | SkAutoLockPixels alp(bitmap); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 176 | for (int by = 0; by < bh; ++by) { |
| 177 | for (int bx = 0; bx < bw; ++bx) { |
| 178 | int devx = bx + srcRect.fLeft; |
| 179 | int devy = by + srcRect.fTop; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 180 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 181 | const uint32_t* pixel = bitmap.getAddr32(bx, by); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 182 | |
| 183 | if (clippedSrcRect.contains(devx, devy)) { |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 184 | if (checkCanvasPixels) { |
| 185 | SkPMColor canvasPixel = getCanvasColor(devx, devy); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 186 | bool didPremul; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 187 | SkPMColor pmPixel = convertToPMColor(ct, at, pixel, &didPremul); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 188 | bool check; |
| 189 | REPORTER_ASSERT(reporter, check = checkPixel(pmPixel, canvasPixel, didPremul)); |
| 190 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 191 | return false; |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 192 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 193 | } |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 194 | } else if (checkBitmapPixels) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 195 | REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw) == *pixel); |
| 196 | if (getBitmapColor(bx, by, bw) != *pixel) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 197 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 202 | return true; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | enum BitmapInit { |
| 206 | kFirstBitmapInit = 0, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 207 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 208 | kNoPixels_BitmapInit = kFirstBitmapInit, |
| 209 | kTight_BitmapInit, |
| 210 | kRowBytes_BitmapInit, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 211 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 212 | kBitmapInitCnt |
| 213 | }; |
| 214 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 215 | static BitmapInit nextBMI(BitmapInit bmi) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 216 | int x = bmi; |
| 217 | return static_cast<BitmapInit>(++x); |
| 218 | } |
| 219 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 220 | static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct, |
| 221 | SkAlphaType at) { |
| 222 | SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 223 | size_t rowBytes = 0; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 224 | bool alloc = true; |
| 225 | switch (init) { |
| 226 | case kNoPixels_BitmapInit: |
| 227 | alloc = false; |
| 228 | case kTight_BitmapInit: |
| 229 | break; |
| 230 | case kRowBytes_BitmapInit: |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 231 | rowBytes = (info.width() + 16) * sizeof(SkPMColor); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 232 | break; |
| 233 | default: |
| 234 | SkASSERT(0); |
| 235 | break; |
| 236 | } |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 237 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 238 | if (alloc) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 239 | bitmap->allocPixels(info); |
| 240 | } else { |
| 241 | bitmap->setConfig(info, rowBytes); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 245 | DEF_GPUTEST(ReadPixels, reporter, factory) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 246 | const SkIRect testRects[] = { |
| 247 | // entire thing |
| 248 | DEV_RECT, |
| 249 | // larger on all sides |
| 250 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 251 | // fully contained |
| 252 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 253 | // outside top left |
| 254 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 255 | // touching top left corner |
| 256 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 257 | // overlapping top left corner |
| 258 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 259 | // overlapping top left and top right corners |
| 260 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 261 | // touching entire top edge |
| 262 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 263 | // overlapping top right corner |
| 264 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 265 | // contained in x, overlapping top edge |
| 266 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 267 | // outside top right corner |
| 268 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 269 | // touching top right corner |
| 270 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 271 | // overlapping top left and bottom left corners |
| 272 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 273 | // touching entire left edge |
| 274 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 275 | // overlapping bottom left corner |
| 276 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 277 | // contained in y, overlapping left edge |
| 278 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 279 | // outside bottom left corner |
| 280 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 281 | // touching bottom left corner |
| 282 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 283 | // overlapping bottom left and bottom right corners |
| 284 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 285 | // touching entire left edge |
| 286 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 287 | // overlapping bottom right corner |
| 288 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 289 | // overlapping top right and bottom right corners |
| 290 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 291 | }; |
| 292 | |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 293 | for (int dtype = 0; dtype < 3; ++dtype) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 294 | int glCtxTypeCnt = 1; |
| 295 | #if SK_SUPPORT_GPU |
| 296 | if (0 != dtype) { |
| 297 | glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 298 | } |
djsollen@google.com | 8688e5b | 2012-01-09 13:02:20 +0000 | [diff] [blame] | 299 | #endif |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 300 | for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 301 | SkAutoTUnref<SkBaseDevice> device; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 302 | if (0 == dtype) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 303 | SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
| 304 | device.reset(SkBitmapDevice::Create(info)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 305 | } else { |
| 306 | #if SK_SUPPORT_GPU |
| 307 | GrContextFactory::GLContextType type = |
| 308 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
| 309 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 310 | continue; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 311 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 312 | GrContext* context = factory->get(type); |
| 313 | if (NULL == context) { |
| 314 | continue; |
| 315 | } |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 316 | GrTextureDesc desc; |
| 317 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 318 | desc.fWidth = DEV_W; |
| 319 | desc.fHeight = DEV_H; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 320 | desc.fConfig = kSkia8888_GrPixelConfig; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 321 | desc.fOrigin = 1 == dtype ? kBottomLeft_GrSurfaceOrigin |
| 322 | : kTopLeft_GrSurfaceOrigin; |
| 323 | GrAutoScratchTexture ast(context, desc, GrContext::kExact_ScratchTexMatch); |
| 324 | SkAutoTUnref<GrTexture> tex(ast.detach()); |
| 325 | device.reset(new SkGpuDevice(context, tex)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 326 | #else |
| 327 | continue; |
| 328 | #endif |
| 329 | } |
| 330 | SkCanvas canvas(device); |
| 331 | fillCanvas(&canvas); |
| 332 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 333 | static const struct { |
| 334 | SkColorType fColorType; |
| 335 | SkAlphaType fAlphaType; |
| 336 | } gReadConfigs[] = { |
| 337 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
| 338 | { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 339 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
| 340 | { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 341 | }; |
| 342 | for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) { |
| 343 | const SkIRect& srcRect = testRects[rect]; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 344 | for (BitmapInit bmi = kFirstBitmapInit; bmi < kBitmapInitCnt; bmi = nextBMI(bmi)) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 345 | for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 346 | SkBitmap bmp; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 347 | init_bitmap(&bmp, srcRect, bmi, |
| 348 | gReadConfigs[c].fColorType, gReadConfigs[c].fAlphaType); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 349 | |
| 350 | // if the bitmap has pixels allocated before the readPixels, |
| 351 | // note that and fill them with pattern |
| 352 | bool startsWithPixels = !bmp.isNull(); |
| 353 | if (startsWithPixels) { |
| 354 | fillBitmap(&bmp); |
| 355 | } |
| 356 | uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID(); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 357 | bool success = canvas.readPixels(&bmp, srcRect.fLeft, srcRect.fTop); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 358 | uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID(); |
| 359 | |
| 360 | // we expect to succeed when the read isn't fully clipped |
| 361 | // out. |
| 362 | bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT); |
| 363 | // determine whether we expected the read to succeed. |
| 364 | REPORTER_ASSERT(reporter, success == expectSuccess); |
| 365 | // read pixels should never change the gen id |
| 366 | REPORTER_ASSERT(reporter, idBefore == idAfter); |
| 367 | |
| 368 | if (success || startsWithPixels) { |
| 369 | checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop, |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 370 | success, startsWithPixels); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 371 | } else { |
| 372 | // if we had no pixels beforehand and the readPixels |
| 373 | // failed then our bitmap should still not have pixels |
| 374 | REPORTER_ASSERT(reporter, bmp.isNull()); |
| 375 | } |
| 376 | } |
| 377 | // check the old webkit version of readPixels that clips the |
| 378 | // bitmap size |
| 379 | SkBitmap wkbmp; |
| 380 | bool success = canvas.readPixels(srcRect, &wkbmp); |
| 381 | SkIRect clippedRect = DEV_RECT; |
| 382 | if (clippedRect.intersect(srcRect)) { |
| 383 | REPORTER_ASSERT(reporter, success); |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 384 | REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType()); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 385 | REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType()); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 386 | checkRead(reporter, wkbmp, clippedRect.fLeft, |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 387 | clippedRect.fTop, true, false); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 388 | } else { |
| 389 | REPORTER_ASSERT(reporter, !success); |
| 390 | } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |