bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +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 | |
| 8 | #include "Test.h" |
robertphillips@google.com | 6323ca5 | 2013-08-29 12:53:23 +0000 | [diff] [blame] | 9 | #include "SkBitmapDevice.h" |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 12 | #include "SkMathPriv.h" |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 13 | #include "SkRegion.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 15 | #include "SkGpuDevice.h" |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 16 | #include "GrContextFactory.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 17 | #else |
| 18 | class GrContext; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 19 | class GrContextFactory; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 20 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 21 | |
| 22 | static const int DEV_W = 100, DEV_H = 100; |
| 23 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1, |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 25 | DEV_H * SK_Scalar1); |
| 26 | static const U8CPU DEV_PAD = 0xee; |
| 27 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 28 | static SkPMColor getCanvasColor(int x, int y) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 29 | SkASSERT(x >= 0 && x < DEV_W); |
| 30 | SkASSERT(y >= 0 && y < DEV_H); |
| 31 | |
| 32 | U8CPU r = x; |
| 33 | U8CPU g = y; |
| 34 | U8CPU b = 0xc; |
| 35 | |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 36 | U8CPU a = 0x0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 37 | switch ((x+y) % 5) { |
| 38 | case 0: |
| 39 | a = 0xff; |
| 40 | break; |
| 41 | case 1: |
| 42 | a = 0x80; |
| 43 | break; |
| 44 | case 2: |
| 45 | a = 0xCC; |
| 46 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 47 | case 3: |
| 48 | a = 0x00; |
| 49 | break; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 50 | case 4: |
| 51 | a = 0x01; |
| 52 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 53 | } |
| 54 | return SkPremultiplyARGBInline(a, r, g, b); |
| 55 | } |
| 56 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 57 | static bool config8888IsPremul(SkCanvas::Config8888 config8888) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 58 | switch (config8888) { |
| 59 | case SkCanvas::kNative_Premul_Config8888: |
| 60 | case SkCanvas::kBGRA_Premul_Config8888: |
| 61 | case SkCanvas::kRGBA_Premul_Config8888: |
| 62 | return true; |
| 63 | case SkCanvas::kNative_Unpremul_Config8888: |
| 64 | case SkCanvas::kBGRA_Unpremul_Config8888: |
| 65 | case SkCanvas::kRGBA_Unpremul_Config8888: |
| 66 | return false; |
| 67 | default: |
| 68 | SkASSERT(0); |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // assumes any premu/.unpremul has been applied |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 74 | static uint32_t packConfig8888(SkCanvas::Config8888 config8888, |
| 75 | U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 76 | uint32_t r32; |
| 77 | uint8_t* result = reinterpret_cast<uint8_t*>(&r32); |
| 78 | switch (config8888) { |
| 79 | case SkCanvas::kNative_Premul_Config8888: |
| 80 | case SkCanvas::kNative_Unpremul_Config8888: |
| 81 | r32 = SkPackARGB32NoCheck(a, r, g, b); |
| 82 | break; |
| 83 | case SkCanvas::kBGRA_Premul_Config8888: |
| 84 | case SkCanvas::kBGRA_Unpremul_Config8888: |
| 85 | result[0] = b; |
| 86 | result[1] = g; |
| 87 | result[2] = r; |
| 88 | result[3] = a; |
| 89 | break; |
| 90 | case SkCanvas::kRGBA_Premul_Config8888: |
| 91 | case SkCanvas::kRGBA_Unpremul_Config8888: |
| 92 | result[0] = r; |
| 93 | result[1] = g; |
| 94 | result[2] = b; |
| 95 | result[3] = a; |
| 96 | break; |
| 97 | default: |
| 98 | SkASSERT(0); |
| 99 | return 0; |
| 100 | } |
| 101 | return r32; |
| 102 | } |
| 103 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 104 | static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8888) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 105 | int n = y * w + x; |
| 106 | U8CPU b = n & 0xff; |
| 107 | U8CPU g = (n >> 8) & 0xff; |
| 108 | U8CPU r = (n >> 16) & 0xff; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 109 | U8CPU a = 0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 110 | switch ((x+y) % 5) { |
| 111 | case 4: |
| 112 | a = 0xff; |
| 113 | break; |
| 114 | case 3: |
| 115 | a = 0x80; |
| 116 | break; |
| 117 | case 2: |
| 118 | a = 0xCC; |
| 119 | break; |
| 120 | case 1: |
| 121 | a = 0x01; |
| 122 | break; |
| 123 | case 0: |
| 124 | a = 0x00; |
| 125 | break; |
| 126 | } |
| 127 | if (config8888IsPremul(config8888)) { |
| 128 | r = SkMulDiv255Ceiling(r, a); |
| 129 | g = SkMulDiv255Ceiling(g, a); |
| 130 | b = SkMulDiv255Ceiling(b, a); |
| 131 | } |
| 132 | return packConfig8888(config8888, a, r, g , b); |
| 133 | } |
| 134 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 135 | static void fillCanvas(SkCanvas* canvas) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 136 | static SkBitmap bmp; |
| 137 | if (bmp.isNull()) { |
| 138 | bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 139 | SkDEBUGCODE(bool alloc = ) bmp.allocPixels(); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 140 | SkASSERT(alloc); |
| 141 | SkAutoLockPixels alp(bmp); |
| 142 | intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
| 143 | for (int y = 0; y < DEV_H; ++y) { |
| 144 | for (int x = 0; x < DEV_W; ++x) { |
| 145 | SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); |
| 146 | *pixel = getCanvasColor(x, y); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | canvas->save(); |
| 151 | canvas->setMatrix(SkMatrix::I()); |
| 152 | canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op); |
| 153 | SkPaint paint; |
| 154 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 155 | canvas->drawBitmap(bmp, 0, 0, &paint); |
| 156 | canvas->restore(); |
| 157 | } |
| 158 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 159 | static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888, |
| 160 | uint32_t color, |
| 161 | bool* premul) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 162 | const uint8_t* c = reinterpret_cast<uint8_t*>(&color); |
| 163 | U8CPU a,r,g,b; |
| 164 | *premul = false; |
| 165 | switch (config8888) { |
| 166 | case SkCanvas::kNative_Premul_Config8888: |
| 167 | return color; |
| 168 | case SkCanvas::kNative_Unpremul_Config8888: |
| 169 | *premul = true; |
| 170 | a = SkGetPackedA32(color); |
| 171 | r = SkGetPackedR32(color); |
| 172 | g = SkGetPackedG32(color); |
| 173 | b = SkGetPackedB32(color); |
| 174 | break; |
| 175 | case SkCanvas::kBGRA_Unpremul_Config8888: |
| 176 | *premul = true; // fallthru |
| 177 | case SkCanvas::kBGRA_Premul_Config8888: |
| 178 | a = static_cast<U8CPU>(c[3]); |
| 179 | r = static_cast<U8CPU>(c[2]); |
| 180 | g = static_cast<U8CPU>(c[1]); |
| 181 | b = static_cast<U8CPU>(c[0]); |
| 182 | break; |
| 183 | case SkCanvas::kRGBA_Unpremul_Config8888: |
| 184 | *premul = true; // fallthru |
| 185 | case SkCanvas::kRGBA_Premul_Config8888: |
| 186 | a = static_cast<U8CPU>(c[3]); |
| 187 | r = static_cast<U8CPU>(c[0]); |
| 188 | g = static_cast<U8CPU>(c[1]); |
| 189 | b = static_cast<U8CPU>(c[2]); |
| 190 | break; |
| 191 | default: |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 192 | SkDEBUGFAIL("Unexpected Config8888"); |
bsalomon@google.com | ccaa002 | 2012-09-25 19:55:07 +0000 | [diff] [blame] | 193 | return 0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 194 | } |
| 195 | if (*premul) { |
| 196 | r = SkMulDiv255Ceiling(r, a); |
| 197 | g = SkMulDiv255Ceiling(g, a); |
| 198 | b = SkMulDiv255Ceiling(b, a); |
| 199 | } |
| 200 | return SkPackARGB32(a, r, g, b); |
| 201 | } |
| 202 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 203 | static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 204 | if (!didPremulConversion) { |
| 205 | return a == b; |
| 206 | } |
| 207 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 208 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 209 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 210 | int32_t aB = SkGetPackedB32(a); |
| 211 | |
| 212 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 213 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 214 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 215 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 216 | |
| 217 | return aA == bA && |
| 218 | SkAbs32(aR - bR) <= 1 && |
| 219 | SkAbs32(aG - bG) <= 1 && |
| 220 | SkAbs32(aB - bB) <= 1; |
| 221 | } |
| 222 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 223 | static bool checkWrite(skiatest::Reporter* reporter, |
| 224 | SkCanvas* canvas, |
| 225 | const SkBitmap& bitmap, |
| 226 | int writeX, int writeY, |
| 227 | SkCanvas::Config8888 config8888) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 228 | SkBaseDevice* dev = canvas->getDevice(); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 229 | if (!dev) { |
| 230 | return false; |
| 231 | } |
| 232 | SkBitmap devBmp = dev->accessBitmap(false); |
| 233 | if (devBmp.width() != DEV_W || |
| 234 | devBmp.height() != DEV_H || |
| 235 | devBmp.config() != SkBitmap::kARGB_8888_Config || |
| 236 | devBmp.isNull()) { |
| 237 | return false; |
| 238 | } |
| 239 | SkAutoLockPixels alp(devBmp); |
| 240 | |
| 241 | intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels()); |
| 242 | size_t canvasRowBytes = devBmp.rowBytes(); |
| 243 | SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height()); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 244 | for (int cy = 0; cy < DEV_H; ++cy) { |
| 245 | const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels); |
| 246 | for (int cx = 0; cx < DEV_W; ++cx) { |
| 247 | SkPMColor canvasPixel = canvasRow[cx]; |
| 248 | if (writeRect.contains(cx, cy)) { |
| 249 | int bx = cx - writeX; |
| 250 | int by = cy - writeY; |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 251 | uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), config8888); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 252 | bool mul; |
| 253 | SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul); |
| 254 | bool check; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 255 | REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul)); |
| 256 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 257 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 258 | } |
| 259 | } else { |
| 260 | bool check; |
| 261 | SkPMColor testColor = getCanvasColor(cx, cy); |
| 262 | REPORTER_ASSERT(reporter, check = (canvasPixel == testColor)); |
| 263 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 264 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | } |
| 268 | if (cy != DEV_H -1) { |
| 269 | const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W); |
| 270 | for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) { |
| 271 | bool check; |
| 272 | REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD))); |
| 273 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 274 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
| 278 | canvasPixels += canvasRowBytes; |
| 279 | } |
| 280 | |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 281 | return true; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | enum DevType { |
| 285 | kRaster_DevType, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 286 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 287 | kGpu_BottomLeft_DevType, |
| 288 | kGpu_TopLeft_DevType, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 289 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | struct CanvasConfig { |
| 293 | DevType fDevType; |
| 294 | bool fTightRowBytes; |
| 295 | }; |
| 296 | |
| 297 | static const CanvasConfig gCanvasConfigs[] = { |
| 298 | {kRaster_DevType, true}, |
| 299 | {kRaster_DevType, false}, |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame^] | 300 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 301 | {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices |
| 302 | {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices |
bsalomon@google.com | bbce8b2 | 2011-11-10 21:20:37 +0000 | [diff] [blame] | 303 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 306 | static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 307 | switch (c.fDevType) { |
| 308 | case kRaster_DevType: { |
| 309 | SkBitmap bmp; |
| 310 | size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100; |
| 311 | bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, rowBytes); |
| 312 | if (!bmp.allocPixels()) { |
reed@google.com | 44a42ea | 2012-10-01 17:54:05 +0000 | [diff] [blame] | 313 | sk_throw(); |
| 314 | return NULL; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 315 | } |
| 316 | // if rowBytes isn't tight then set the padding to a known value |
| 317 | if (rowBytes) { |
| 318 | SkAutoLockPixels alp(bmp); |
| 319 | memset(bmp.getPixels(), DEV_PAD, bmp.getSafeSize()); |
| 320 | } |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 321 | return new SkBitmapDevice(bmp); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 322 | } |
| 323 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 324 | case kGpu_BottomLeft_DevType: |
| 325 | case kGpu_TopLeft_DevType: |
| 326 | GrTextureDesc desc; |
| 327 | desc.fFlags = kRenderTarget_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 = kGpu_TopLeft_DevType == c.fDevType ? |
| 332 | kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
| 333 | GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMatch); |
| 334 | SkAutoTUnref<GrTexture> tex(ast.detach()); |
| 335 | return new SkGpuDevice(grCtx, tex); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 336 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 337 | } |
reed@google.com | 44a42ea | 2012-10-01 17:54:05 +0000 | [diff] [blame] | 338 | return NULL; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 339 | } |
| 340 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 341 | static bool setupBitmap(SkBitmap* bitmap, |
| 342 | SkCanvas::Config8888 config8888, |
| 343 | int w, int h, |
| 344 | bool tightRowBytes) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 345 | size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60; |
| 346 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes); |
| 347 | if (!bitmap->allocPixels()) { |
| 348 | return false; |
| 349 | } |
| 350 | SkAutoLockPixels alp(*bitmap); |
| 351 | intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
| 352 | for (int y = 0; y < h; ++y) { |
| 353 | for (int x = 0; x < w; ++x) { |
| 354 | uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 355 | *pixel = getBitmapColor(x, y, w, config8888); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | return true; |
| 359 | } |
| 360 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 361 | static void WritePixelsTest(skiatest::Reporter* reporter, GrContextFactory* factory) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 362 | SkCanvas canvas; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 363 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 364 | const SkIRect testRects[] = { |
| 365 | // entire thing |
| 366 | DEV_RECT, |
| 367 | // larger on all sides |
| 368 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 369 | // fully contained |
| 370 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 371 | // outside top left |
| 372 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 373 | // touching top left corner |
| 374 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 375 | // overlapping top left corner |
| 376 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 377 | // overlapping top left and top right corners |
| 378 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 379 | // touching entire top edge |
| 380 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 381 | // overlapping top right corner |
| 382 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 383 | // contained in x, overlapping top edge |
| 384 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 385 | // outside top right corner |
| 386 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 387 | // touching top right corner |
| 388 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 389 | // overlapping top left and bottom left corners |
| 390 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 391 | // touching entire left edge |
| 392 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 393 | // overlapping bottom left corner |
| 394 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 395 | // contained in y, overlapping left edge |
| 396 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 397 | // outside bottom left corner |
| 398 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 399 | // touching bottom left corner |
| 400 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 401 | // overlapping bottom left and bottom right corners |
| 402 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 403 | // touching entire left edge |
| 404 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 405 | // overlapping bottom right corner |
| 406 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 407 | // overlapping top right and bottom right corners |
| 408 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 409 | }; |
| 410 | |
| 411 | for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 412 | int glCtxTypeCnt = 1; |
| 413 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 414 | bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType || |
| 415 | kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType; |
| 416 | if (isGPUDevice) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 417 | glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; |
| 418 | } |
| 419 | #endif |
| 420 | for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { |
| 421 | GrContext* context = NULL; |
| 422 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 423 | if (isGPUDevice) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 424 | GrContextFactory::GLContextType type = |
| 425 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 426 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 427 | continue; |
| 428 | } |
| 429 | context = factory->get(type); |
| 430 | if (NULL == context) { |
| 431 | continue; |
| 432 | } |
| 433 | } |
| 434 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 435 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 436 | SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 437 | SkCanvas canvas(device); |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 438 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 439 | static const SkCanvas::Config8888 gSrcConfigs[] = { |
| 440 | SkCanvas::kNative_Premul_Config8888, |
| 441 | SkCanvas::kNative_Unpremul_Config8888, |
| 442 | SkCanvas::kBGRA_Premul_Config8888, |
| 443 | SkCanvas::kBGRA_Unpremul_Config8888, |
| 444 | SkCanvas::kRGBA_Premul_Config8888, |
| 445 | SkCanvas::kRGBA_Unpremul_Config8888, |
| 446 | }; |
| 447 | for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { |
| 448 | const SkIRect& rect = testRects[r]; |
| 449 | for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { |
| 450 | for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { |
| 451 | fillCanvas(&canvas); |
| 452 | SkCanvas::Config8888 config8888 = gSrcConfigs[c]; |
| 453 | SkBitmap bmp; |
| 454 | REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp))); |
| 455 | uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID(); |
| 456 | canvas.writePixels(bmp, rect.fLeft, rect.fTop, config8888); |
| 457 | uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID(); |
| 458 | REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888)); |
| 459 | |
| 460 | // we should change the genID iff pixels were actually written. |
| 461 | SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize()); |
| 462 | SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop, |
| 463 | bmp.width(), bmp.height()); |
| 464 | bool intersects = SkIRect::Intersects(canvasRect, writeRect) ; |
| 465 | REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter)); |
| 466 | } |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 472 | |
| 473 | #include "TestClassDef.h" |
| 474 | DEFINE_GPUTESTCLASS("WritePixels", WritePixelsTestClass, WritePixelsTest) |