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" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 9 | #include "SkColorData.h" |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 10 | #include "SkMathPriv.h" |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 11 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 12 | #include "Test.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 13 | #include "sk_tool_utils.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 |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 16 | #include "GrBackendSurface.h" |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 17 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 18 | #include "GrContextPriv.h" |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 19 | #include "GrGpu.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 20 | #include "GrProxyProvider.h" |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 21 | #include "GrTest.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 22 | #endif |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 23 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 24 | #include <initializer_list> |
| 25 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 26 | static const int DEV_W = 100, DEV_H = 100; |
| 27 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 28 | static const U8CPU DEV_PAD = 0xee; |
| 29 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 30 | static SkPMColor get_canvas_color(int x, int y) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 31 | SkASSERT(x >= 0 && x < DEV_W); |
| 32 | SkASSERT(y >= 0 && y < DEV_H); |
| 33 | |
| 34 | U8CPU r = x; |
| 35 | U8CPU g = y; |
| 36 | U8CPU b = 0xc; |
| 37 | |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 38 | U8CPU a = 0x0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 39 | switch ((x+y) % 5) { |
| 40 | case 0: |
| 41 | a = 0xff; |
| 42 | break; |
| 43 | case 1: |
| 44 | a = 0x80; |
| 45 | break; |
| 46 | case 2: |
| 47 | a = 0xCC; |
| 48 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 49 | case 3: |
| 50 | a = 0x00; |
| 51 | break; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 52 | case 4: |
| 53 | a = 0x01; |
| 54 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 55 | } |
| 56 | return SkPremultiplyARGBInline(a, r, g, b); |
| 57 | } |
| 58 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 59 | // assumes any premu/.unpremul has been applied |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 60 | 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] | 61 | uint32_t r32; |
| 62 | uint8_t* result = reinterpret_cast<uint8_t*>(&r32); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 63 | switch (ct) { |
| 64 | case kBGRA_8888_SkColorType: |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 65 | result[0] = b; |
| 66 | result[1] = g; |
| 67 | result[2] = r; |
| 68 | result[3] = a; |
| 69 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 70 | case kRGBA_8888_SkColorType: |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 71 | result[0] = r; |
| 72 | result[1] = g; |
| 73 | result[2] = b; |
| 74 | result[3] = a; |
| 75 | break; |
| 76 | default: |
| 77 | SkASSERT(0); |
| 78 | return 0; |
| 79 | } |
| 80 | return r32; |
| 81 | } |
| 82 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 83 | 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] | 84 | int n = y * w + x; |
| 85 | U8CPU b = n & 0xff; |
| 86 | U8CPU g = (n >> 8) & 0xff; |
| 87 | U8CPU r = (n >> 16) & 0xff; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 88 | U8CPU a = 0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 89 | switch ((x+y) % 5) { |
| 90 | case 4: |
| 91 | a = 0xff; |
| 92 | break; |
| 93 | case 3: |
| 94 | a = 0x80; |
| 95 | break; |
| 96 | case 2: |
| 97 | a = 0xCC; |
| 98 | break; |
| 99 | case 1: |
| 100 | a = 0x01; |
| 101 | break; |
| 102 | case 0: |
| 103 | a = 0x00; |
| 104 | break; |
| 105 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 106 | if (kPremul_SkAlphaType == at) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 107 | r = SkMulDiv255Ceiling(r, a); |
| 108 | g = SkMulDiv255Ceiling(g, a); |
| 109 | b = SkMulDiv255Ceiling(b, a); |
| 110 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 111 | return pack_color_type(ct, a, r, g , b); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 114 | static void fill_surface(SkSurface* surface) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 115 | SkBitmap bmp; |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 116 | bmp.allocN32Pixels(DEV_W, DEV_H); |
| 117 | for (int y = 0; y < DEV_H; ++y) { |
| 118 | for (int x = 0; x < DEV_W; ++x) { |
| 119 | *bmp.getAddr32(x, y) = get_canvas_color(x, y); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 122 | surface->writePixels(bmp, 0, 0); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA. |
| 127 | * Thus this routine doesn't need to know the exact colortype |
| 128 | */ |
| 129 | static uint32_t premul(uint32_t color) { |
| 130 | unsigned a = SkGetPackedA32(color); |
| 131 | // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order. |
| 132 | unsigned c0 = SkGetPackedR32(color); |
| 133 | unsigned c1 = SkGetPackedG32(color); |
| 134 | unsigned c2 = SkGetPackedB32(color); |
| 135 | c0 = SkMulDiv255Ceiling(c0, a); |
| 136 | c1 = SkMulDiv255Ceiling(c1, a); |
| 137 | c2 = SkMulDiv255Ceiling(c2, a); |
| 138 | return SkPackARGB32NoCheck(a, c0, c1, c2); |
| 139 | } |
| 140 | |
| 141 | static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) { |
| 142 | if (kUnpremul_SkAlphaType == at) { |
| 143 | color = premul(color); |
| 144 | } |
| 145 | switch (ct) { |
| 146 | case kRGBA_8888_SkColorType: |
| 147 | color = SkSwizzle_RGBA_to_PMColor(color); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 148 | break; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 149 | case kBGRA_8888_SkColorType: |
| 150 | color = SkSwizzle_BGRA_to_PMColor(color); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 151 | break; |
| 152 | default: |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 153 | SkASSERT(0); |
| 154 | break; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 155 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 156 | return color; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 157 | } |
| 158 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 159 | static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 160 | if (!didPremulConversion) { |
| 161 | return a == b; |
| 162 | } |
| 163 | int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); |
| 164 | int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); |
| 165 | int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); |
| 166 | int32_t aB = SkGetPackedB32(a); |
| 167 | |
| 168 | int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
| 169 | int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
| 170 | int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
| 171 | int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
| 172 | |
| 173 | return aA == bA && |
| 174 | SkAbs32(aR - bR) <= 1 && |
| 175 | SkAbs32(aG - bG) <= 1 && |
| 176 | SkAbs32(aB - bB) <= 1; |
| 177 | } |
| 178 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 179 | static bool check_write(skiatest::Reporter* reporter, SkSurface* surf, const SkBitmap& bitmap, |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 180 | int writeX, int writeY) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 181 | size_t canvasRowBytes; |
| 182 | const uint32_t* canvasPixels; |
reed@google.com | 1121170 | 2014-03-25 12:00:30 +0000 | [diff] [blame] | 183 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 184 | // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well. |
| 185 | // At some point this will be unsupported, as we won't allow accessBitmap() to magically call |
| 186 | // readPixels for the client. |
| 187 | SkBitmap secretDevBitmap; |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 188 | secretDevBitmap.allocN32Pixels(surf->width(), surf->height()); |
| 189 | if (!surf->readPixels(secretDevBitmap, 0, 0)) { |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 190 | return false; |
| 191 | } |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 192 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 193 | canvasRowBytes = secretDevBitmap.rowBytes(); |
| 194 | canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); |
| 195 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | if (nullptr == canvasPixels) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 200 | if (surf->width() != DEV_W || surf->height() != DEV_H) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | |
| 204 | const SkImageInfo bmInfo = bitmap.info(); |
| 205 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 206 | SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height()); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 207 | for (int cy = 0; cy < DEV_H; ++cy) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 208 | for (int cx = 0; cx < DEV_W; ++cx) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 209 | SkPMColor canvasPixel = canvasPixels[cx]; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 210 | if (writeRect.contains(cx, cy)) { |
| 211 | int bx = cx - writeX; |
| 212 | int by = cy - writeY; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 213 | uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(), |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 214 | bmInfo.colorType(), bmInfo.alphaType()); |
| 215 | bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType()); |
| 216 | SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(), |
| 217 | bmpColor8888); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 218 | if (!check_pixel(bmpPMColor, canvasPixel, mul)) { |
| 219 | ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. " |
| 220 | "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 221 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 222 | } |
| 223 | } else { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 224 | SkPMColor testColor = get_canvas_color(cx, cy); |
| 225 | if (canvasPixel != testColor) { |
| 226 | ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed." |
| 227 | " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel); |
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 | } |
| 231 | } |
| 232 | if (cy != DEV_H -1) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 233 | const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 234 | for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) { |
| 235 | bool check; |
| 236 | REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD))); |
| 237 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 238 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 242 | canvasPixels += canvasRowBytes/4; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 243 | } |
| 244 | |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 245 | return true; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 246 | } |
| 247 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 248 | #include "SkMallocPixelRef.h" |
| 249 | |
| 250 | // This is a tricky pattern, because we have to setConfig+rowBytes AND specify |
| 251 | // a custom pixelRef (which also has to specify its rowBytes), so we have to be |
| 252 | // sure that the two rowBytes match (and the infos match). |
| 253 | // |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 254 | 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] | 255 | if (!bm->setInfo(info, rowBytes)) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 258 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 259 | bm->setPixelRef(std::move(pr), 0, 0); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 263 | static void free_pixels(void* pixels, void* ctx) { |
| 264 | sk_free(pixels); |
| 265 | } |
| 266 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 267 | 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] | 268 | size_t rowBytes = tightRB ? 0 : 4 * w + 60; |
| 269 | SkImageInfo info = SkImageInfo::Make(w, h, ct, at); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 270 | if (!alloc_row_bytes(bm, info, rowBytes)) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 273 | for (int y = 0; y < h; ++y) { |
| 274 | for (int x = 0; x < w; ++x) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 275 | *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] | 276 | } |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 281 | static void call_writepixels(SkSurface* surface) { |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 282 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 283 | SkPMColor pixel = 0; |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 284 | surface->writePixels({info, &pixel, sizeof(SkPMColor)}, 0, 0); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 285 | } |
| 286 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 287 | DEF_TEST(WritePixelsSurfaceGenID, reporter) { |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 288 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 289 | auto surface(SkSurface::MakeRaster(info)); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 290 | uint32_t genID1 = surface->generationID(); |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 291 | call_writepixels(surface.get()); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 292 | uint32_t genID2 = surface->generationID(); |
| 293 | REPORTER_ASSERT(reporter, genID1 != genID2); |
| 294 | } |
| 295 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 296 | static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 297 | const SkIRect testRects[] = { |
| 298 | // entire thing |
| 299 | DEV_RECT, |
| 300 | // larger on all sides |
| 301 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 302 | // fully contained |
| 303 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 304 | // outside top left |
| 305 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 306 | // touching top left corner |
| 307 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 308 | // overlapping top left corner |
| 309 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 310 | // overlapping top left and top right corners |
| 311 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 312 | // touching entire top edge |
| 313 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 314 | // overlapping top right corner |
| 315 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 316 | // contained in x, overlapping top edge |
| 317 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 318 | // outside top right corner |
| 319 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 320 | // touching top right corner |
| 321 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 322 | // overlapping top left and bottom left corners |
| 323 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 324 | // touching entire left edge |
| 325 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 326 | // overlapping bottom left corner |
| 327 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 328 | // contained in y, overlapping left edge |
| 329 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 330 | // outside bottom left corner |
| 331 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 332 | // touching bottom left corner |
| 333 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 334 | // overlapping bottom left and bottom right corners |
| 335 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 336 | // touching entire left edge |
| 337 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 338 | // overlapping bottom right corner |
| 339 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 340 | // overlapping top right and bottom right corners |
| 341 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 342 | }; |
| 343 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 344 | SkCanvas* canvas = surface->getCanvas(); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 345 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 346 | static const struct { |
| 347 | SkColorType fColorType; |
| 348 | SkAlphaType fAlphaType; |
| 349 | } gSrcConfigs[] = { |
| 350 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
| 351 | { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 352 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
| 353 | { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 354 | }; |
| 355 | for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { |
| 356 | const SkIRect& rect = testRects[r]; |
| 357 | for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { |
| 358 | for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { |
| 359 | const SkColorType ct = gSrcConfigs[c].fColorType; |
| 360 | const SkAlphaType at = gSrcConfigs[c].fAlphaType; |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 361 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 362 | fill_surface(surface); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 363 | SkBitmap bmp; |
| 364 | REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(), |
| 365 | rect.height(), SkToBool(tightBmp))); |
| 366 | uint32_t idBefore = surface->generationID(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 367 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 368 | // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 369 | surface->writePixels(bmp, rect.fLeft, rect.fTop); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 370 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 371 | uint32_t idAfter = surface->generationID(); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 372 | REPORTER_ASSERT(reporter, check_write(reporter, surface, bmp, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 373 | rect.fLeft, rect.fTop)); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 374 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 375 | // we should change the genID iff pixels were actually written. |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 376 | SkIRect canvasRect = SkIRect::MakeSize(canvas->getBaseLayerSize()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 377 | SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop, |
| 378 | bmp.width(), bmp.height()); |
| 379 | bool intersects = SkIRect::Intersects(canvasRect, writeRect) ; |
| 380 | REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter)); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 385 | DEF_TEST(WritePixels, reporter) { |
| 386 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
| 387 | for (auto& tightRowBytes : { true, false }) { |
| 388 | const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100; |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 389 | const size_t size = info.computeByteSize(rowBytes); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 390 | void* pixels = sk_malloc_throw(size); |
| 391 | // if rowBytes isn't tight then set the padding to a known value |
| 392 | if (!tightRowBytes) { |
| 393 | memset(pixels, DEV_PAD, size); |
| 394 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 395 | auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes, |
| 396 | free_pixels, nullptr)); |
| 397 | test_write_pixels(reporter, surface.get()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | #if SK_SUPPORT_GPU |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 401 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) { |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 402 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
| 403 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 404 | for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 405 | for (int sampleCnt : {1, 4}) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 406 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), |
| 407 | SkBudgeted::kNo, ii, sampleCnt, |
| 408 | origin, nullptr)); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 409 | if (!surface && sampleCnt > 1) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 410 | // Some platforms don't support MSAA |
| 411 | continue; |
| 412 | } |
| 413 | test_write_pixels(reporter, surface.get()); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo) { |
| 419 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 420 | GrGpu* gpu = context->contextPriv().getGpu(); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 421 | |
| 422 | for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 423 | for (int sampleCnt : {1, 4}) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 424 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 425 | nullptr, DEV_W, DEV_H, kSkia8888_GrPixelConfig, true, GrMipMapped::kNo); |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 426 | SkColorType colorType; |
| 427 | if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig) { |
| 428 | colorType = kRGBA_8888_SkColorType; |
| 429 | } else { |
| 430 | colorType = kBGRA_8888_SkColorType; |
| 431 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 432 | sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTextureAsRenderTarget( |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 433 | context, backendTex, origin, sampleCnt, colorType, nullptr, nullptr)); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 434 | if (!surface) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 435 | gpu->deleteTestingOnlyBackendTexture(&backendTex); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 436 | continue; |
| 437 | } |
| 438 | |
| 439 | test_write_pixels(reporter, surface.get()); |
| 440 | |
| 441 | surface.reset(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 442 | gpu->deleteTestingOnlyBackendTexture(&backendTex); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 443 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 446 | |
| 447 | static sk_sp<SkSurface> create_surf(GrContext* context, int width, int height) { |
| 448 | const SkImageInfo ii = SkImageInfo::Make(width, height, |
| 449 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 450 | |
| 451 | sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii); |
| 452 | surf->flush(); |
| 453 | return surf; |
| 454 | } |
| 455 | |
| 456 | static sk_sp<SkImage> upload(const sk_sp<SkSurface>& surf, SkColor color) { |
| 457 | const SkImageInfo smII = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 458 | SkBitmap bm; |
| 459 | bm.allocPixels(smII); |
| 460 | bm.eraseColor(color); |
| 461 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame^] | 462 | surf->writePixels(bm, 0, 0); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 463 | |
| 464 | return surf->makeImageSnapshot(); |
| 465 | } |
| 466 | |
| 467 | // This is tests whether the first writePixels is completed before the |
| 468 | // second writePixels takes effect (i.e., that writePixels correctly flushes |
| 469 | // in between uses of the shared backing resource). |
| 470 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsPendingIO, reporter, ctxInfo) { |
| 471 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 472 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 473 | |
| 474 | static const int kFullSize = 62; |
| 475 | static const int kHalfSize = 31; |
| 476 | |
| 477 | static const uint32_t kLeftColor = 0xFF222222; |
| 478 | static const uint32_t kRightColor = 0xFFAAAAAA; |
| 479 | |
| 480 | const SkImageInfo fullII = SkImageInfo::Make(kFullSize, kFullSize, |
| 481 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 482 | const SkImageInfo halfII = SkImageInfo::Make(kHalfSize, kFullSize, |
| 483 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 484 | |
| 485 | sk_sp<SkSurface> dest = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, fullII); |
| 486 | |
| 487 | { |
| 488 | // Seed the resource cached with a scratch texture that will be |
| 489 | // reused by writeSurfacePixels |
| 490 | GrSurfaceDesc desc; |
| 491 | desc.fFlags = kNone_GrSurfaceFlags; |
| 492 | desc.fWidth = 32; |
| 493 | desc.fHeight = 64; |
| 494 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 495 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 496 | sk_sp<GrTextureProxy> temp = proxyProvider->createProxy(desc, SkBackingFit::kApprox, |
| 497 | SkBudgeted::kYes); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 498 | temp->instantiate(context->contextPriv().resourceProvider()); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Create the surfaces and flush them to ensure there is no lingering pendingIO |
| 502 | sk_sp<SkSurface> leftSurf = create_surf(context, kHalfSize, kFullSize); |
| 503 | sk_sp<SkSurface> rightSurf = create_surf(context, kHalfSize, kFullSize); |
| 504 | |
| 505 | sk_sp<SkImage> leftImg = upload(std::move(leftSurf), kLeftColor); |
| 506 | dest->getCanvas()->drawImage(std::move(leftImg), 0, 0); |
| 507 | |
| 508 | sk_sp<SkImage> rightImg = upload(std::move(rightSurf), kRightColor); |
| 509 | dest->getCanvas()->drawImage(std::move(rightImg), kHalfSize, 0); |
| 510 | |
| 511 | SkBitmap bm; |
| 512 | bm.allocPixels(fullII); |
| 513 | SkAssertResult(dest->readPixels(bm, 0, 0)); |
| 514 | |
| 515 | bool isCorrect = true; |
| 516 | for (int y = 0; isCorrect && y < 16; ++y) { |
| 517 | const uint32_t* sl = bm.getAddr32(0, y); |
| 518 | |
| 519 | for (int x = 0; x < 16; ++x) { |
| 520 | if (kLeftColor != sl[x]) { |
| 521 | isCorrect = false; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | for (int x = kHalfSize; x < kHalfSize+16; ++x) { |
| 526 | if (kRightColor != sl[x]) { |
| 527 | isCorrect = false; |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | REPORTER_ASSERT(reporter, isCorrect); |
| 534 | } |
| 535 | |
| 536 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 537 | #endif |