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 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +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 | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 11 | #include "SkRegion.h" |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 12 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 13 | #include "Test.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 14 | #include "sk_tool_utils.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 16 | #if SK_SUPPORT_GPU |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 17 | #include "GrContext.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 18 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 19 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 20 | #include <initializer_list> |
| 21 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 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 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 28 | static SkPMColor get_canvas_color(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 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 57 | // assumes any premu/.unpremul has been applied |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 58 | static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 59 | uint32_t r32; |
| 60 | uint8_t* result = reinterpret_cast<uint8_t*>(&r32); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 61 | switch (ct) { |
| 62 | case kBGRA_8888_SkColorType: |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 63 | result[0] = b; |
| 64 | result[1] = g; |
| 65 | result[2] = r; |
| 66 | result[3] = a; |
| 67 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 68 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 69 | result[0] = r; |
| 70 | result[1] = g; |
| 71 | result[2] = b; |
| 72 | result[3] = a; |
| 73 | break; |
| 74 | default: |
| 75 | SkASSERT(0); |
| 76 | return 0; |
| 77 | } |
| 78 | return r32; |
| 79 | } |
| 80 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 81 | static uint32_t get_bitmap_color(int x, int y, int w, SkColorType ct, SkAlphaType at) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 82 | int n = y * w + x; |
| 83 | U8CPU b = n & 0xff; |
| 84 | U8CPU g = (n >> 8) & 0xff; |
| 85 | U8CPU r = (n >> 16) & 0xff; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 86 | U8CPU a = 0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 87 | switch ((x+y) % 5) { |
| 88 | case 4: |
| 89 | a = 0xff; |
| 90 | break; |
| 91 | case 3: |
| 92 | a = 0x80; |
| 93 | break; |
| 94 | case 2: |
| 95 | a = 0xCC; |
| 96 | break; |
| 97 | case 1: |
| 98 | a = 0x01; |
| 99 | break; |
| 100 | case 0: |
| 101 | a = 0x00; |
| 102 | break; |
| 103 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 104 | if (kPremul_SkAlphaType == at) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 105 | r = SkMulDiv255Ceiling(r, a); |
| 106 | g = SkMulDiv255Ceiling(g, a); |
| 107 | b = SkMulDiv255Ceiling(b, a); |
| 108 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 109 | return pack_color_type(ct, a, r, g , b); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 110 | } |
| 111 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 112 | static void fill_canvas(SkCanvas* canvas) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 113 | SkBitmap bmp; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 114 | if (bmp.isNull()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 115 | bmp.allocN32Pixels(DEV_W, DEV_H); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 116 | for (int y = 0; y < DEV_H; ++y) { |
| 117 | for (int x = 0; x < DEV_W; ++x) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 118 | *bmp.getAddr32(x, y) = get_canvas_color(x, y); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 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 | } |
| 130 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 131 | /** |
| 132 | * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA. |
| 133 | * Thus this routine doesn't need to know the exact colortype |
| 134 | */ |
| 135 | static uint32_t premul(uint32_t color) { |
| 136 | unsigned a = SkGetPackedA32(color); |
| 137 | // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order. |
| 138 | unsigned c0 = SkGetPackedR32(color); |
| 139 | unsigned c1 = SkGetPackedG32(color); |
| 140 | unsigned c2 = SkGetPackedB32(color); |
| 141 | c0 = SkMulDiv255Ceiling(c0, a); |
| 142 | c1 = SkMulDiv255Ceiling(c1, a); |
| 143 | c2 = SkMulDiv255Ceiling(c2, a); |
| 144 | return SkPackARGB32NoCheck(a, c0, c1, c2); |
| 145 | } |
| 146 | |
| 147 | static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) { |
| 148 | if (kUnpremul_SkAlphaType == at) { |
| 149 | color = premul(color); |
| 150 | } |
| 151 | switch (ct) { |
| 152 | case kRGBA_8888_SkColorType: |
| 153 | color = SkSwizzle_RGBA_to_PMColor(color); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 154 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 155 | case kBGRA_8888_SkColorType: |
| 156 | color = SkSwizzle_BGRA_to_PMColor(color); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 157 | break; |
| 158 | default: |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 159 | SkASSERT(0); |
| 160 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 161 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 162 | return color; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 163 | } |
| 164 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 165 | static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 166 | if (!didPremulConversion) { |
| 167 | return a == b; |
| 168 | } |
| 169 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 170 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 171 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 172 | int32_t aB = SkGetPackedB32(a); |
| 173 | |
| 174 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 175 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 176 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 177 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 178 | |
| 179 | return aA == bA && |
| 180 | SkAbs32(aR - bR) <= 1 && |
| 181 | SkAbs32(aG - bG) <= 1 && |
| 182 | SkAbs32(aB - bB) <= 1; |
| 183 | } |
| 184 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 185 | 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] | 186 | int writeX, int writeY) { |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 187 | const SkImageInfo canvasInfo = canvas->imageInfo(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 188 | size_t canvasRowBytes; |
| 189 | const uint32_t* canvasPixels; |
reed@google.com | 1121170 | 2014-03-25 12:00:30 +0000 | [diff] [blame] | 190 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 191 | // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well. |
| 192 | // At some point this will be unsupported, as we won't allow accessBitmap() to magically call |
| 193 | // readPixels for the client. |
| 194 | SkBitmap secretDevBitmap; |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 195 | canvas->readPixels(canvasInfo.bounds(), &secretDevBitmap); |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 196 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 197 | SkAutoLockPixels alp(secretDevBitmap); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 198 | canvasRowBytes = secretDevBitmap.rowBytes(); |
| 199 | canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); |
| 200 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | if (nullptr == canvasPixels) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | if (canvasInfo.width() != DEV_W || |
| 206 | canvasInfo.height() != DEV_H || |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 207 | canvasInfo.colorType() != kN32_SkColorType) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | |
| 211 | const SkImageInfo bmInfo = bitmap.info(); |
| 212 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 213 | SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height()); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 214 | for (int cy = 0; cy < DEV_H; ++cy) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 215 | for (int cx = 0; cx < DEV_W; ++cx) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 216 | SkPMColor canvasPixel = canvasPixels[cx]; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 217 | if (writeRect.contains(cx, cy)) { |
| 218 | int bx = cx - writeX; |
| 219 | int by = cy - writeY; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 220 | uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(), |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 221 | bmInfo.colorType(), bmInfo.alphaType()); |
| 222 | bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType()); |
| 223 | SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(), |
| 224 | bmpColor8888); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 225 | if (!check_pixel(bmpPMColor, canvasPixel, mul)) { |
| 226 | ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. " |
| 227 | "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 228 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 229 | } |
| 230 | } else { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 231 | SkPMColor testColor = get_canvas_color(cx, cy); |
| 232 | if (canvasPixel != testColor) { |
| 233 | ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed." |
| 234 | " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 235 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | if (cy != DEV_H -1) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 240 | const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 241 | for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) { |
| 242 | bool check; |
| 243 | REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD))); |
| 244 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 245 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 249 | canvasPixels += canvasRowBytes/4; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 250 | } |
| 251 | |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 252 | return true; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 253 | } |
| 254 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 255 | #include "SkMallocPixelRef.h" |
| 256 | |
| 257 | // This is a tricky pattern, because we have to setConfig+rowBytes AND specify |
| 258 | // a custom pixelRef (which also has to specify its rowBytes), so we have to be |
| 259 | // sure that the two rowBytes match (and the infos match). |
| 260 | // |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 261 | static bool alloc_row_bytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 262 | if (!bm->setInfo(info, rowBytes)) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 263 | return false; |
| 264 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 265 | SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, nullptr); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 266 | bm->setPixelRef(pr)->unref(); |
| 267 | return true; |
| 268 | } |
| 269 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 270 | static void free_pixels(void* pixels, void* ctx) { |
| 271 | sk_free(pixels); |
| 272 | } |
| 273 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 274 | 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] | 275 | size_t rowBytes = tightRB ? 0 : 4 * w + 60; |
| 276 | SkImageInfo info = SkImageInfo::Make(w, h, ct, at); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 277 | if (!alloc_row_bytes(bm, info, rowBytes)) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 280 | SkAutoLockPixels alp(*bm); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 281 | for (int y = 0; y < h; ++y) { |
| 282 | for (int x = 0; x < w; ++x) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 283 | *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 289 | static void call_writepixels(SkCanvas* canvas) { |
| 290 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 291 | SkPMColor pixel = 0; |
| 292 | canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0); |
| 293 | } |
| 294 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 295 | DEF_TEST(WritePixelsSurfaceGenID, reporter) { |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 296 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 297 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 298 | uint32_t genID1 = surface->generationID(); |
| 299 | call_writepixels(surface->getCanvas()); |
| 300 | uint32_t genID2 = surface->generationID(); |
| 301 | REPORTER_ASSERT(reporter, genID1 != genID2); |
| 302 | } |
| 303 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 304 | static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 305 | const SkIRect testRects[] = { |
| 306 | // entire thing |
| 307 | DEV_RECT, |
| 308 | // larger on all sides |
| 309 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 310 | // fully contained |
| 311 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 312 | // outside top left |
| 313 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 314 | // touching top left corner |
| 315 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 316 | // overlapping top left corner |
| 317 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 318 | // overlapping top left and top right corners |
| 319 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 320 | // touching entire top edge |
| 321 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 322 | // overlapping top right corner |
| 323 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 324 | // contained in x, overlapping top edge |
| 325 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 326 | // outside top right corner |
| 327 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 328 | // touching top right corner |
| 329 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 330 | // overlapping top left and bottom left corners |
| 331 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 332 | // touching entire left edge |
| 333 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 334 | // overlapping bottom left corner |
| 335 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 336 | // contained in y, overlapping left edge |
| 337 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 338 | // outside bottom left corner |
| 339 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 340 | // touching bottom left corner |
| 341 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 342 | // overlapping bottom left and bottom right corners |
| 343 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 344 | // touching entire left edge |
| 345 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 346 | // overlapping bottom right corner |
| 347 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 348 | // overlapping top right and bottom right corners |
| 349 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 350 | }; |
| 351 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 352 | SkCanvas& canvas = *surface->getCanvas(); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 353 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 354 | static const struct { |
| 355 | SkColorType fColorType; |
| 356 | SkAlphaType fAlphaType; |
| 357 | } gSrcConfigs[] = { |
| 358 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
| 359 | { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 360 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
| 361 | { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 362 | }; |
| 363 | for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { |
| 364 | const SkIRect& rect = testRects[r]; |
| 365 | for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { |
| 366 | for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { |
| 367 | const SkColorType ct = gSrcConfigs[c].fColorType; |
| 368 | const SkAlphaType at = gSrcConfigs[c].fAlphaType; |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 369 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 370 | fill_canvas(&canvas); |
| 371 | SkBitmap bmp; |
| 372 | REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(), |
| 373 | rect.height(), SkToBool(tightBmp))); |
| 374 | uint32_t idBefore = surface->generationID(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 375 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 376 | // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); |
| 377 | canvas.writePixels(bmp, rect.fLeft, rect.fTop); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 378 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 379 | uint32_t idAfter = surface->generationID(); |
| 380 | REPORTER_ASSERT(reporter, check_write(reporter, &canvas, bmp, |
| 381 | rect.fLeft, rect.fTop)); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 382 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 383 | // we should change the genID iff pixels were actually written. |
| 384 | SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize()); |
| 385 | SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop, |
| 386 | bmp.width(), bmp.height()); |
| 387 | bool intersects = SkIRect::Intersects(canvasRect, writeRect) ; |
| 388 | REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter)); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 393 | DEF_TEST(WritePixels, reporter) { |
| 394 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
| 395 | for (auto& tightRowBytes : { true, false }) { |
| 396 | const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100; |
| 397 | const size_t size = info.getSafeSize(rowBytes); |
| 398 | void* pixels = sk_malloc_throw(size); |
| 399 | // if rowBytes isn't tight then set the padding to a known value |
| 400 | if (!tightRowBytes) { |
| 401 | memset(pixels, DEV_PAD, size); |
| 402 | } |
| 403 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterDirectReleaseProc(info, pixels, rowBytes, free_pixels, nullptr)); |
| 404 | test_write_pixels(reporter, surface); |
| 405 | } |
| 406 | } |
| 407 | #if SK_SUPPORT_GPU |
| 408 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, context) { |
| 409 | for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
| 410 | GrSurfaceDesc desc; |
| 411 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 412 | desc.fWidth = DEV_W; |
| 413 | desc.fHeight = DEV_H; |
| 414 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 415 | desc.fOrigin = origin; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 416 | SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, |
| 417 | SkBudgeted::kNo)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 418 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(texture->asRenderTarget())); |
| 419 | test_write_pixels(reporter, surface); |
| 420 | } |
| 421 | } |
| 422 | #endif |