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