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