| 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 | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 63 | static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888, | 
 | 64 |                                             uint32_t color, | 
 | 65 |                                             bool* premul) { | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 66 |     const uint8_t* c = reinterpret_cast<uint8_t*>(&color); | 
 | 67 |     U8CPU a,r,g,b; | 
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 68 |     *premul = false; | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 69 |     switch (config8888) { | 
 | 70 |         case SkCanvas::kNative_Premul_Config8888: | 
 | 71 |             return color; | 
 | 72 |         case SkCanvas::kNative_Unpremul_Config8888: | 
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 73 |             *premul = true; | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 74 |             a = SkGetPackedA32(color); | 
 | 75 |             r = SkGetPackedR32(color); | 
 | 76 |             g = SkGetPackedG32(color); | 
 | 77 |             b = SkGetPackedB32(color); | 
 | 78 |             break; | 
 | 79 |         case SkCanvas::kBGRA_Unpremul_Config8888: | 
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 80 |             *premul = true; // fallthru | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 81 |         case SkCanvas::kBGRA_Premul_Config8888: | 
 | 82 |             a = static_cast<U8CPU>(c[3]); | 
 | 83 |             r = static_cast<U8CPU>(c[2]); | 
 | 84 |             g = static_cast<U8CPU>(c[1]); | 
 | 85 |             b = static_cast<U8CPU>(c[0]); | 
 | 86 |             break; | 
 | 87 |         case SkCanvas::kRGBA_Unpremul_Config8888: | 
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 88 |             *premul = true; // fallthru | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 89 |         case SkCanvas::kRGBA_Premul_Config8888: | 
 | 90 |             a = static_cast<U8CPU>(c[3]); | 
 | 91 |             r = static_cast<U8CPU>(c[0]); | 
 | 92 |             g = static_cast<U8CPU>(c[1]); | 
 | 93 |             b = static_cast<U8CPU>(c[2]); | 
 | 94 |             break; | 
| bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 95 |         default: | 
 | 96 |             SkDEBUGFAIL("Unexpected Config8888"); | 
 | 97 |             return 0; | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 98 |     } | 
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 99 |     if (*premul) { | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 100 |         r = SkMulDiv255Ceiling(r, a); | 
 | 101 |         g = SkMulDiv255Ceiling(g, a); | 
 | 102 |         b = SkMulDiv255Ceiling(b, a); | 
 | 103 |     } | 
 | 104 |     return SkPackARGB32(a, r, g, b); | 
 | 105 | } | 
 | 106 |  | 
| commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 107 | static void fillCanvas(SkCanvas* canvas) { | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 108 |     static SkBitmap bmp; | 
 | 109 |     if (bmp.isNull()) { | 
 | 110 |         bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H); | 
| reed@google.com | 44a42ea | 2012-10-01 17:54:05 +0000 | [diff] [blame] | 111 |         SkDEBUGCODE(bool alloc =) bmp.allocPixels(); | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 112 |         SkASSERT(alloc); | 
 | 113 |         SkAutoLockPixels alp(bmp); | 
 | 114 |         intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); | 
 | 115 |         for (int y = 0; y < DEV_H; ++y) { | 
 | 116 |             for (int x = 0; x < DEV_W; ++x) { | 
 | 117 |                 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); | 
 | 118 |                 *pixel = getCanvasColor(x, y); | 
 | 119 |             } | 
 | 120 |         } | 
 | 121 |     } | 
 | 122 |     canvas->save(); | 
 | 123 |     canvas->setMatrix(SkMatrix::I()); | 
 | 124 |     canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op); | 
 | 125 |     SkPaint paint; | 
 | 126 |     paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 
 | 127 |     canvas->drawBitmap(bmp, 0, 0, &paint); | 
 | 128 |     canvas->restore(); | 
 | 129 | } | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 130 |  | 
| commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 131 | static void fillBitmap(SkBitmap* bitmap) { | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 132 |     SkASSERT(bitmap->lockPixelsAreWritable()); | 
 | 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()); | 
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 140 |             *pixel = getBitmapColor(x, y, w); | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 141 |         } | 
 | 142 |     } | 
 | 143 | } | 
 | 144 |  | 
| commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 145 | static bool checkPixel(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. | 
| commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 168 | static bool checkRead(skiatest::Reporter* reporter, | 
 | 169 |                       const SkBitmap& bitmap, | 
 | 170 |                       int x, int y, | 
 | 171 |                       bool checkCanvasPixels, | 
 | 172 |                       bool checkBitmapPixels, | 
 | 173 |                       SkCanvas::Config8888 config8888) { | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 174 |     SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config()); | 
 | 175 |     SkASSERT(!bitmap.isNull()); | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 176 |     SkASSERT(checkCanvasPixels || checkBitmapPixels); | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 177 |  | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 178 |     int bw = bitmap.width(); | 
 | 179 |     int bh = bitmap.height(); | 
 | 180 |  | 
 | 181 |     SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); | 
 | 182 |     SkIRect clippedSrcRect = DEV_RECT; | 
 | 183 |     if (!clippedSrcRect.intersect(srcRect)) { | 
 | 184 |         clippedSrcRect.setEmpty(); | 
 | 185 |     } | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 186 |     SkAutoLockPixels alp(bitmap); | 
 | 187 |     intptr_t pixels = reinterpret_cast<intptr_t>(bitmap.getPixels()); | 
 | 188 |     for (int by = 0; by < bh; ++by) { | 
 | 189 |         for (int bx = 0; bx < bw; ++bx) { | 
 | 190 |             int devx = bx + srcRect.fLeft; | 
 | 191 |             int devy = by + srcRect.fTop; | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 192 |  | 
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 193 |             uint32_t pixel = *reinterpret_cast<SkPMColor*>(pixels + by * bitmap.rowBytes() + bx * bitmap.bytesPerPixel()); | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 194 |  | 
 | 195 |             if (clippedSrcRect.contains(devx, devy)) { | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 196 |                 if (checkCanvasPixels) { | 
 | 197 |                     SkPMColor canvasPixel = getCanvasColor(devx, devy); | 
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 198 |                     bool didPremul; | 
 | 199 |                     SkPMColor pmPixel = convertConfig8888ToPMColor(config8888, pixel, &didPremul); | 
 | 200 |                     bool check; | 
 | 201 |                     REPORTER_ASSERT(reporter, check = checkPixel(pmPixel, canvasPixel, didPremul)); | 
 | 202 |                     if (!check) { | 
| bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 203 |                         return false; | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 204 |                     } | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 205 |                 } | 
| bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 206 |             } else if (checkBitmapPixels) { | 
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 207 |                 REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw) == pixel); | 
 | 208 |                 if (getBitmapColor(bx, by, bw) != pixel) { | 
| bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 209 |                     return false; | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 210 |                 } | 
 | 211 |             } | 
 | 212 |         } | 
 | 213 |     } | 
| bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 214 |     return true; | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 215 | } | 
 | 216 |  | 
 | 217 | enum BitmapInit { | 
 | 218 |     kFirstBitmapInit = 0, | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 219 |  | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 220 |     kNoPixels_BitmapInit = kFirstBitmapInit, | 
 | 221 |     kTight_BitmapInit, | 
 | 222 |     kRowBytes_BitmapInit, | 
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 223 |  | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 224 |     kBitmapInitCnt | 
 | 225 | }; | 
 | 226 |  | 
| commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 227 | static BitmapInit nextBMI(BitmapInit bmi) { | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 228 |     int x = bmi; | 
 | 229 |     return static_cast<BitmapInit>(++x); | 
 | 230 | } | 
 | 231 |  | 
| commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 232 | static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init) { | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 233 |     int w = rect.width(); | 
 | 234 |     int h = rect.height(); | 
 | 235 |     int rowBytes = 0; | 
 | 236 |     bool alloc = true; | 
 | 237 |     switch (init) { | 
 | 238 |         case kNoPixels_BitmapInit: | 
 | 239 |             alloc = false; | 
 | 240 |         case kTight_BitmapInit: | 
 | 241 |             break; | 
 | 242 |         case kRowBytes_BitmapInit: | 
 | 243 |             rowBytes = w * sizeof(SkPMColor) + 16 * sizeof(SkPMColor); | 
 | 244 |             break; | 
 | 245 |         default: | 
 | 246 |             SkASSERT(0); | 
 | 247 |             break; | 
 | 248 |     } | 
 | 249 |     bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes); | 
 | 250 |     if (alloc) { | 
 | 251 |         bitmap->allocPixels(); | 
 | 252 |     } | 
 | 253 | } | 
 | 254 |  | 
| tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 255 | DEF_GPUTEST(ReadPixels, reporter, factory) { | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 256 |     const SkIRect testRects[] = { | 
 | 257 |         // entire thing | 
 | 258 |         DEV_RECT, | 
 | 259 |         // larger on all sides | 
 | 260 |         SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), | 
 | 261 |         // fully contained | 
 | 262 |         SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), | 
 | 263 |         // outside top left | 
 | 264 |         SkIRect::MakeLTRB(-10, -10, -1, -1), | 
 | 265 |         // touching top left corner | 
 | 266 |         SkIRect::MakeLTRB(-10, -10, 0, 0), | 
 | 267 |         // overlapping top left corner | 
 | 268 |         SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), | 
 | 269 |         // overlapping top left and top right corners | 
 | 270 |         SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, DEV_H / 4), | 
 | 271 |         // touching entire top edge | 
 | 272 |         SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, 0), | 
 | 273 |         // overlapping top right corner | 
 | 274 |         SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W  + 10, DEV_H / 4), | 
 | 275 |         // contained in x, overlapping top edge | 
 | 276 |         SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W  / 4, DEV_H / 4), | 
 | 277 |         // outside top right corner | 
 | 278 |         SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), | 
 | 279 |         // touching top right corner | 
 | 280 |         SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), | 
 | 281 |         // overlapping top left and bottom left corners | 
 | 282 |         SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), | 
 | 283 |         // touching entire left edge | 
 | 284 |         SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), | 
 | 285 |         // overlapping bottom left corner | 
 | 286 |         SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), | 
 | 287 |         // contained in y, overlapping left edge | 
 | 288 |         SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), | 
 | 289 |         // outside bottom left corner | 
 | 290 |         SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), | 
 | 291 |         // touching bottom left corner | 
 | 292 |         SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), | 
 | 293 |         // overlapping bottom left and bottom right corners | 
 | 294 |         SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), | 
 | 295 |         // touching entire left edge | 
 | 296 |         SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), | 
 | 297 |         // overlapping bottom right corner | 
 | 298 |         SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), | 
 | 299 |         // overlapping top right and bottom right corners | 
 | 300 |         SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), | 
 | 301 |     }; | 
 | 302 |  | 
| senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 303 |     for (int dtype = 0; dtype < 3; ++dtype) { | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 304 |         int glCtxTypeCnt = 1; | 
 | 305 | #if SK_SUPPORT_GPU | 
 | 306 |         if (0 != dtype)  { | 
 | 307 |             glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 308 |         } | 
| djsollen@google.com | 8688e5b | 2012-01-09 13:02:20 +0000 | [diff] [blame] | 309 | #endif | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 310 |         for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { | 
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 311 |             SkAutoTUnref<SkBaseDevice> device; | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 312 |             if (0 == dtype) { | 
| skia.committer@gmail.com | 772c4e6 | 2013-08-30 07:01:34 +0000 | [diff] [blame] | 313 |                 device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config, | 
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 314 |                                                 DEV_W, DEV_H, false)); | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 315 |             } else { | 
 | 316 | #if SK_SUPPORT_GPU | 
 | 317 |                 GrContextFactory::GLContextType type = | 
 | 318 |                     static_cast<GrContextFactory::GLContextType>(glCtxType); | 
 | 319 |                 if (!GrContextFactory::IsRenderingGLContext(type)) { | 
 | 320 |                     continue; | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 321 |                 } | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 322 |                 GrContext* context = factory->get(type); | 
 | 323 |                 if (NULL == context) { | 
 | 324 |                     continue; | 
 | 325 |                 } | 
| senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 326 |                 GrTextureDesc desc; | 
 | 327 |                 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; | 
 | 328 |                 desc.fWidth = DEV_W; | 
 | 329 |                 desc.fHeight = DEV_H; | 
| bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 330 |                 desc.fConfig = kSkia8888_GrPixelConfig; | 
| senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 331 |                 desc.fOrigin = 1 == dtype ? kBottomLeft_GrSurfaceOrigin | 
 | 332 |                                           : kTopLeft_GrSurfaceOrigin; | 
 | 333 |                 GrAutoScratchTexture ast(context, desc, GrContext::kExact_ScratchTexMatch); | 
 | 334 |                 SkAutoTUnref<GrTexture> tex(ast.detach()); | 
 | 335 |                 device.reset(new SkGpuDevice(context, tex)); | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 336 | #else | 
 | 337 |                 continue; | 
 | 338 | #endif | 
 | 339 |             } | 
 | 340 |             SkCanvas canvas(device); | 
 | 341 |             fillCanvas(&canvas); | 
 | 342 |  | 
 | 343 |             static const SkCanvas::Config8888 gReadConfigs[] = { | 
 | 344 |                 SkCanvas::kNative_Premul_Config8888, | 
 | 345 |                 SkCanvas::kNative_Unpremul_Config8888, | 
| commit-bot@chromium.org | 2862151 | 2013-08-07 19:43:45 +0000 | [diff] [blame] | 346 |  | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 347 |                 SkCanvas::kBGRA_Premul_Config8888, | 
 | 348 |                 SkCanvas::kBGRA_Unpremul_Config8888, | 
| commit-bot@chromium.org | 2862151 | 2013-08-07 19:43:45 +0000 | [diff] [blame] | 349 |  | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 350 |                 SkCanvas::kRGBA_Premul_Config8888, | 
 | 351 |                 SkCanvas::kRGBA_Unpremul_Config8888, | 
 | 352 |             }; | 
 | 353 |             for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) { | 
 | 354 |                 const SkIRect& srcRect = testRects[rect]; | 
 | 355 |                 for (BitmapInit bmi = kFirstBitmapInit; | 
 | 356 |                      bmi < kBitmapInitCnt; | 
 | 357 |                      bmi = nextBMI(bmi)) { | 
 | 358 |                     for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) { | 
 | 359 |                         SkCanvas::Config8888 config8888 = gReadConfigs[c]; | 
 | 360 |                         SkBitmap bmp; | 
 | 361 |                         init_bitmap(&bmp, srcRect, bmi); | 
 | 362 |  | 
 | 363 |                         // if the bitmap has pixels allocated before the readPixels, | 
 | 364 |                         // note that and fill them with pattern | 
 | 365 |                         bool startsWithPixels = !bmp.isNull(); | 
 | 366 |                         if (startsWithPixels) { | 
 | 367 |                             fillBitmap(&bmp); | 
 | 368 |                         } | 
 | 369 |                         uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID(); | 
 | 370 |                         bool success = | 
 | 371 |                             canvas.readPixels(&bmp, srcRect.fLeft, | 
 | 372 |                                               srcRect.fTop, config8888); | 
 | 373 |                         uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID(); | 
 | 374 |  | 
 | 375 |                         // we expect to succeed when the read isn't fully clipped | 
 | 376 |                         // out. | 
 | 377 |                         bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT); | 
 | 378 |                         // determine whether we expected the read to succeed. | 
 | 379 |                         REPORTER_ASSERT(reporter, success == expectSuccess); | 
 | 380 |                         // read pixels should never change the gen id | 
 | 381 |                         REPORTER_ASSERT(reporter, idBefore == idAfter); | 
 | 382 |  | 
 | 383 |                         if (success || startsWithPixels) { | 
 | 384 |                             checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop, | 
 | 385 |                                       success, startsWithPixels, config8888); | 
 | 386 |                         } else { | 
 | 387 |                             // if we had no pixels beforehand and the readPixels | 
 | 388 |                             // failed then our bitmap should still not have pixels | 
 | 389 |                             REPORTER_ASSERT(reporter, bmp.isNull()); | 
 | 390 |                         } | 
 | 391 |                     } | 
 | 392 |                     // check the old webkit version of readPixels that clips the | 
 | 393 |                     // bitmap size | 
 | 394 |                     SkBitmap wkbmp; | 
 | 395 |                     bool success = canvas.readPixels(srcRect, &wkbmp); | 
 | 396 |                     SkIRect clippedRect = DEV_RECT; | 
 | 397 |                     if (clippedRect.intersect(srcRect)) { | 
 | 398 |                         REPORTER_ASSERT(reporter, success); | 
 | 399 |                         checkRead(reporter, wkbmp, clippedRect.fLeft, | 
 | 400 |                                   clippedRect.fTop, true, false, | 
 | 401 |                                   SkCanvas::kNative_Premul_Config8888); | 
 | 402 |                     } else { | 
 | 403 |                         REPORTER_ASSERT(reporter, !success); | 
 | 404 |                     } | 
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 405 |                 } | 
 | 406 |             } | 
 | 407 |         } | 
 | 408 |     } | 
 | 409 | } |