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 | |
robertphillips@google.com | 6323ca5 | 2013-08-29 12:53:23 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +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 | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 12 | #include "SkRegion.h" |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 13 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 14 | #include "Test.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 15 | #include "sk_tool_utils.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 18 | #include "GrContextFactory.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 19 | #include "SkGpuDevice.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 20 | #else |
| 21 | class GrContext; |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 22 | class GrContextFactory; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 23 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 24 | |
| 25 | static const int DEV_W = 100, DEV_H = 100; |
| 26 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 27 | 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] | 28 | DEV_H * SK_Scalar1); |
| 29 | static const U8CPU DEV_PAD = 0xee; |
| 30 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 31 | static SkPMColor getCanvasColor(int x, int y) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 32 | SkASSERT(x >= 0 && x < DEV_W); |
| 33 | SkASSERT(y >= 0 && y < DEV_H); |
| 34 | |
| 35 | U8CPU r = x; |
| 36 | U8CPU g = y; |
| 37 | U8CPU b = 0xc; |
| 38 | |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 39 | U8CPU a = 0x0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 40 | switch ((x+y) % 5) { |
| 41 | case 0: |
| 42 | a = 0xff; |
| 43 | break; |
| 44 | case 1: |
| 45 | a = 0x80; |
| 46 | break; |
| 47 | case 2: |
| 48 | a = 0xCC; |
| 49 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 50 | case 3: |
| 51 | a = 0x00; |
| 52 | break; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 53 | case 4: |
| 54 | a = 0x01; |
| 55 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 56 | } |
| 57 | return SkPremultiplyARGBInline(a, r, g, b); |
| 58 | } |
| 59 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 60 | // assumes any premu/.unpremul has been applied |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 61 | static uint32_t packColorType(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 62 | uint32_t r32; |
| 63 | uint8_t* result = reinterpret_cast<uint8_t*>(&r32); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 64 | switch (ct) { |
| 65 | case kBGRA_8888_SkColorType: |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 66 | result[0] = b; |
| 67 | result[1] = g; |
| 68 | result[2] = r; |
| 69 | result[3] = a; |
| 70 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 71 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 72 | result[0] = r; |
| 73 | result[1] = g; |
| 74 | result[2] = b; |
| 75 | result[3] = a; |
| 76 | break; |
| 77 | default: |
| 78 | SkASSERT(0); |
| 79 | return 0; |
| 80 | } |
| 81 | return r32; |
| 82 | } |
| 83 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 84 | static uint32_t getBitmapColor(int x, int y, int w, SkColorType ct, SkAlphaType at) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 85 | int n = y * w + x; |
| 86 | U8CPU b = n & 0xff; |
| 87 | U8CPU g = (n >> 8) & 0xff; |
| 88 | U8CPU r = (n >> 16) & 0xff; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 89 | U8CPU a = 0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 90 | switch ((x+y) % 5) { |
| 91 | case 4: |
| 92 | a = 0xff; |
| 93 | break; |
| 94 | case 3: |
| 95 | a = 0x80; |
| 96 | break; |
| 97 | case 2: |
| 98 | a = 0xCC; |
| 99 | break; |
| 100 | case 1: |
| 101 | a = 0x01; |
| 102 | break; |
| 103 | case 0: |
| 104 | a = 0x00; |
| 105 | break; |
| 106 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 107 | if (kPremul_SkAlphaType == at) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 108 | r = SkMulDiv255Ceiling(r, a); |
| 109 | g = SkMulDiv255Ceiling(g, a); |
| 110 | b = SkMulDiv255Ceiling(b, a); |
| 111 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 112 | return packColorType(ct, a, r, g , b); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 115 | static void fillCanvas(SkCanvas* canvas) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 116 | SkBitmap bmp; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 117 | if (bmp.isNull()) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 118 | SkDEBUGCODE(bool alloc = ) bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 119 | SkASSERT(alloc); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 120 | for (int y = 0; y < DEV_H; ++y) { |
| 121 | for (int x = 0; x < DEV_W; ++x) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 122 | *bmp.getAddr32(x, y) = getCanvasColor(x, y); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | canvas->save(); |
| 127 | canvas->setMatrix(SkMatrix::I()); |
| 128 | canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op); |
| 129 | SkPaint paint; |
| 130 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 131 | canvas->drawBitmap(bmp, 0, 0, &paint); |
| 132 | canvas->restore(); |
| 133 | } |
| 134 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 135 | /** |
| 136 | * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA. |
| 137 | * Thus this routine doesn't need to know the exact colortype |
| 138 | */ |
| 139 | static uint32_t premul(uint32_t color) { |
| 140 | unsigned a = SkGetPackedA32(color); |
| 141 | // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order. |
| 142 | unsigned c0 = SkGetPackedR32(color); |
| 143 | unsigned c1 = SkGetPackedG32(color); |
| 144 | unsigned c2 = SkGetPackedB32(color); |
| 145 | c0 = SkMulDiv255Ceiling(c0, a); |
| 146 | c1 = SkMulDiv255Ceiling(c1, a); |
| 147 | c2 = SkMulDiv255Ceiling(c2, a); |
| 148 | return SkPackARGB32NoCheck(a, c0, c1, c2); |
| 149 | } |
| 150 | |
| 151 | static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) { |
| 152 | if (kUnpremul_SkAlphaType == at) { |
| 153 | color = premul(color); |
| 154 | } |
| 155 | switch (ct) { |
| 156 | case kRGBA_8888_SkColorType: |
| 157 | color = SkSwizzle_RGBA_to_PMColor(color); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 158 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 159 | case kBGRA_8888_SkColorType: |
| 160 | color = SkSwizzle_BGRA_to_PMColor(color); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 161 | break; |
| 162 | default: |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 163 | SkASSERT(0); |
| 164 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 165 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 166 | return color; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 167 | } |
| 168 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 169 | static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 170 | if (!didPremulConversion) { |
| 171 | return a == b; |
| 172 | } |
| 173 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 174 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 175 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 176 | int32_t aB = SkGetPackedB32(a); |
| 177 | |
| 178 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 179 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 180 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 181 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 182 | |
| 183 | return aA == bA && |
| 184 | SkAbs32(aR - bR) <= 1 && |
| 185 | SkAbs32(aG - bG) <= 1 && |
| 186 | SkAbs32(aB - bB) <= 1; |
| 187 | } |
| 188 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 189 | static bool check_write(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap, |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 190 | int writeX, int writeY) { |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 191 | const SkImageInfo canvasInfo = canvas->imageInfo(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 192 | size_t canvasRowBytes; |
| 193 | const uint32_t* canvasPixels; |
reed@google.com | 1121170 | 2014-03-25 12:00:30 +0000 | [diff] [blame] | 194 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 195 | // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well. |
| 196 | // At some point this will be unsupported, as we won't allow accessBitmap() to magically call |
| 197 | // readPixels for the client. |
| 198 | SkBitmap secretDevBitmap; |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 199 | canvas->readPixels(SkIRect::MakeWH(canvasInfo.width(), canvasInfo.height()), &secretDevBitmap); |
| 200 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 201 | SkAutoLockPixels alp(secretDevBitmap); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 202 | canvasRowBytes = secretDevBitmap.rowBytes(); |
| 203 | canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); |
| 204 | |
| 205 | if (NULL == canvasPixels) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if (canvasInfo.width() != DEV_W || |
| 210 | canvasInfo.height() != DEV_H || |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 211 | canvasInfo.colorType() != kN32_SkColorType) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
| 215 | const SkImageInfo bmInfo = bitmap.info(); |
| 216 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 217 | SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height()); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 218 | for (int cy = 0; cy < DEV_H; ++cy) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 219 | for (int cx = 0; cx < DEV_W; ++cx) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 220 | SkPMColor canvasPixel = canvasPixels[cx]; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 221 | if (writeRect.contains(cx, cy)) { |
| 222 | int bx = cx - writeX; |
| 223 | int by = cy - writeY; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 224 | uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), |
| 225 | bmInfo.colorType(), bmInfo.alphaType()); |
| 226 | bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType()); |
| 227 | SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(), |
| 228 | bmpColor8888); |
| 229 | bool check = checkPixel(bmpPMColor, canvasPixel, mul); |
| 230 | REPORTER_ASSERT(reporter, check); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 231 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 232 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 233 | } |
| 234 | } else { |
| 235 | bool check; |
| 236 | SkPMColor testColor = getCanvasColor(cx, cy); |
| 237 | REPORTER_ASSERT(reporter, check = (canvasPixel == testColor)); |
| 238 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 239 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | if (cy != DEV_H -1) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 244 | const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 245 | for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) { |
| 246 | bool check; |
| 247 | REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD))); |
| 248 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 249 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 253 | canvasPixels += canvasRowBytes/4; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 254 | } |
| 255 | |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 256 | return true; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | enum DevType { |
| 260 | kRaster_DevType, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 261 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 262 | kGpu_BottomLeft_DevType, |
| 263 | kGpu_TopLeft_DevType, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 264 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | struct CanvasConfig { |
| 268 | DevType fDevType; |
| 269 | bool fTightRowBytes; |
| 270 | }; |
| 271 | |
| 272 | static const CanvasConfig gCanvasConfigs[] = { |
| 273 | {kRaster_DevType, true}, |
| 274 | {kRaster_DevType, false}, |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 275 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 276 | {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices |
| 277 | {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] | 278 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 281 | #include "SkMallocPixelRef.h" |
| 282 | |
| 283 | // This is a tricky pattern, because we have to setConfig+rowBytes AND specify |
| 284 | // a custom pixelRef (which also has to specify its rowBytes), so we have to be |
| 285 | // sure that the two rowBytes match (and the infos match). |
| 286 | // |
| 287 | static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 288 | if (!bm->setInfo(info, rowBytes)) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL); |
| 292 | bm->setPixelRef(pr)->unref(); |
| 293 | return true; |
| 294 | } |
| 295 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 296 | static void free_pixels(void* pixels, void* ctx) { |
| 297 | sk_free(pixels); |
| 298 | } |
| 299 | |
| 300 | static SkSurface* create_surface(const CanvasConfig& c, GrContext* grCtx) { |
| 301 | SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 302 | switch (c.fDevType) { |
| 303 | case kRaster_DevType: { |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 304 | const size_t rowBytes = c.fTightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100; |
| 305 | const size_t size = info.getSafeSize(rowBytes); |
| 306 | void* pixels = sk_malloc_throw(size); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 307 | // if rowBytes isn't tight then set the padding to a known value |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 308 | if (!c.fTightRowBytes) { |
| 309 | memset(pixels, DEV_PAD, size); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 310 | } |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 311 | return SkSurface::NewRasterDirectReleaseProc(info, pixels, rowBytes, free_pixels, NULL); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 312 | } |
| 313 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 314 | case kGpu_BottomLeft_DevType: |
| 315 | case kGpu_TopLeft_DevType: |
| 316 | GrTextureDesc desc; |
| 317 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 318 | desc.fWidth = DEV_W; |
| 319 | desc.fHeight = DEV_H; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 320 | desc.fConfig = kSkia8888_GrPixelConfig; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 321 | desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ? |
| 322 | kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
| 323 | GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMatch); |
| 324 | SkAutoTUnref<GrTexture> tex(ast.detach()); |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 325 | return SkSurface::NewRenderTargetDirect(tex->asRenderTarget()); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 326 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 327 | } |
reed@google.com | 44a42ea | 2012-10-01 17:54:05 +0000 | [diff] [blame] | 328 | return NULL; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 329 | } |
| 330 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 331 | static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 332 | size_t rowBytes = tightRB ? 0 : 4 * w + 60; |
| 333 | SkImageInfo info = SkImageInfo::Make(w, h, ct, at); |
| 334 | if (!allocRowBytes(bm, info, rowBytes)) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 335 | return false; |
| 336 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 337 | SkAutoLockPixels alp(*bm); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 338 | for (int y = 0; y < h; ++y) { |
| 339 | for (int x = 0; x < w; ++x) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 340 | *bm->getAddr32(x, y) = getBitmapColor(x, y, w, ct, at); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | return true; |
| 344 | } |
| 345 | |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 346 | static void call_writepixels(SkCanvas* canvas) { |
| 347 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 348 | SkPMColor pixel = 0; |
| 349 | canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0); |
| 350 | } |
| 351 | |
| 352 | static void test_surface_genid(skiatest::Reporter* reporter) { |
| 353 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 354 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 355 | uint32_t genID1 = surface->generationID(); |
| 356 | call_writepixels(surface->getCanvas()); |
| 357 | uint32_t genID2 = surface->generationID(); |
| 358 | REPORTER_ASSERT(reporter, genID1 != genID2); |
| 359 | } |
| 360 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 361 | DEF_GPUTEST(WritePixels, reporter, factory) { |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 362 | test_surface_genid(reporter); |
| 363 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 364 | SkCanvas canvas; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 366 | const SkIRect testRects[] = { |
| 367 | // entire thing |
| 368 | DEV_RECT, |
| 369 | // larger on all sides |
| 370 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 371 | // fully contained |
| 372 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 373 | // outside top left |
| 374 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 375 | // touching top left corner |
| 376 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 377 | // overlapping top left corner |
| 378 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 379 | // overlapping top left and top right corners |
| 380 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 381 | // touching entire top edge |
| 382 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 383 | // overlapping top right corner |
| 384 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 385 | // contained in x, overlapping top edge |
| 386 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 387 | // outside top right corner |
| 388 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 389 | // touching top right corner |
| 390 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 391 | // overlapping top left and bottom left corners |
| 392 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 393 | // touching entire left edge |
| 394 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 395 | // overlapping bottom left corner |
| 396 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 397 | // contained in y, overlapping left edge |
| 398 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 399 | // outside bottom left corner |
| 400 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 401 | // touching bottom left corner |
| 402 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 403 | // overlapping bottom left and bottom right corners |
| 404 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 405 | // touching entire left edge |
| 406 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 407 | // overlapping bottom right corner |
| 408 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 409 | // overlapping top right and bottom right corners |
| 410 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 411 | }; |
| 412 | |
| 413 | for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 414 | int glCtxTypeCnt = 1; |
| 415 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 416 | bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType || |
| 417 | kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType; |
| 418 | if (isGPUDevice) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 419 | glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; |
| 420 | } |
| 421 | #endif |
| 422 | for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { |
| 423 | GrContext* context = NULL; |
| 424 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 425 | if (isGPUDevice) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 426 | GrContextFactory::GLContextType type = |
| 427 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 428 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 429 | continue; |
| 430 | } |
| 431 | context = factory->get(type); |
| 432 | if (NULL == context) { |
| 433 | continue; |
| 434 | } |
| 435 | } |
| 436 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 437 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 438 | SkAutoTUnref<SkSurface> surface(create_surface(gCanvasConfigs[i], context)); |
| 439 | SkCanvas& canvas = *surface->getCanvas(); |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 440 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 441 | static const struct { |
| 442 | SkColorType fColorType; |
| 443 | SkAlphaType fAlphaType; |
| 444 | } gSrcConfigs[] = { |
| 445 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
| 446 | { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 447 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
| 448 | { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 449 | }; |
| 450 | for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { |
| 451 | const SkIRect& rect = testRects[r]; |
| 452 | for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { |
| 453 | for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 454 | const SkColorType ct = gSrcConfigs[c].fColorType; |
| 455 | const SkAlphaType at = gSrcConfigs[c].fAlphaType; |
| 456 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 457 | fillCanvas(&canvas); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 458 | SkBitmap bmp; |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 459 | REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(), |
| 460 | rect.height(), SkToBool(tightBmp))); |
| 461 | uint32_t idBefore = surface->generationID(); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 462 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 463 | // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); |
| 464 | canvas.writePixels(bmp, rect.fLeft, rect.fTop); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 465 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 466 | uint32_t idAfter = surface->generationID(); |
| 467 | REPORTER_ASSERT(reporter, check_write(reporter, &canvas, bmp, |
| 468 | rect.fLeft, rect.fTop)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 469 | |
| 470 | // we should change the genID iff pixels were actually written. |
| 471 | SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize()); |
| 472 | SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop, |
| 473 | bmp.width(), bmp.height()); |
| 474 | bool intersects = SkIRect::Intersects(canvasRect, writeRect) ; |
| 475 | REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter)); |
| 476 | } |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |