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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkSurface.h" |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrBackendSurface.h" |
| 12 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/private/SkColorData.h" |
| 14 | #include "include/private/SkImageInfoPriv.h" |
| 15 | #include "src/core/SkMathPriv.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrGpu.h" |
| 18 | #include "src/gpu/GrProxyProvider.h" |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 19 | #include "tests/Test.h" |
| 20 | #include "tools/gpu/BackendSurfaceFactory.h" |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 21 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 22 | #include <initializer_list> |
| 23 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 24 | static const int DEV_W = 100, DEV_H = 100; |
| 25 | static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 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; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 68 | case kRGBA_8888_SkColorType: // fallthrough |
| 69 | case kRGB_888x_SkColorType: |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 70 | result[0] = r; |
| 71 | result[1] = g; |
| 72 | result[2] = b; |
| 73 | result[3] = a; |
| 74 | break; |
| 75 | default: |
| 76 | SkASSERT(0); |
| 77 | return 0; |
| 78 | } |
| 79 | return r32; |
| 80 | } |
| 81 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 82 | 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] | 83 | int n = y * w + x; |
| 84 | U8CPU b = n & 0xff; |
| 85 | U8CPU g = (n >> 8) & 0xff; |
| 86 | U8CPU r = (n >> 16) & 0xff; |
bsalomon@google.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 87 | U8CPU a = 0; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 88 | switch ((x+y) % 5) { |
| 89 | case 4: |
| 90 | a = 0xff; |
| 91 | break; |
| 92 | case 3: |
| 93 | a = 0x80; |
| 94 | break; |
| 95 | case 2: |
| 96 | a = 0xCC; |
| 97 | break; |
| 98 | case 1: |
| 99 | a = 0x01; |
| 100 | break; |
| 101 | case 0: |
| 102 | a = 0x00; |
| 103 | break; |
| 104 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 105 | if (kPremul_SkAlphaType == at) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 106 | r = SkMulDiv255Ceiling(r, a); |
| 107 | g = SkMulDiv255Ceiling(g, a); |
| 108 | b = SkMulDiv255Ceiling(b, a); |
| 109 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 110 | return pack_color_type(ct, a, r, g , b); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 113 | static void fill_surface(SkSurface* surface) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 114 | SkBitmap bmp; |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 115 | bmp.allocN32Pixels(DEV_W, DEV_H); |
| 116 | for (int y = 0; y < DEV_H; ++y) { |
| 117 | for (int x = 0; x < DEV_W; ++x) { |
| 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 | } |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 121 | surface->writePixels(bmp, 0, 0); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 122 | } |
| 123 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA. |
| 126 | * Thus this routine doesn't need to know the exact colortype |
| 127 | */ |
| 128 | static uint32_t premul(uint32_t color) { |
| 129 | unsigned a = SkGetPackedA32(color); |
| 130 | // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order. |
| 131 | unsigned c0 = SkGetPackedR32(color); |
| 132 | unsigned c1 = SkGetPackedG32(color); |
| 133 | unsigned c2 = SkGetPackedB32(color); |
| 134 | c0 = SkMulDiv255Ceiling(c0, a); |
| 135 | c1 = SkMulDiv255Ceiling(c1, a); |
| 136 | c2 = SkMulDiv255Ceiling(c2, a); |
| 137 | return SkPackARGB32NoCheck(a, c0, c1, c2); |
| 138 | } |
| 139 | |
| 140 | static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) { |
| 141 | if (kUnpremul_SkAlphaType == at) { |
| 142 | color = premul(color); |
| 143 | } |
| 144 | switch (ct) { |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 145 | case kRGBA_8888_SkColorType: // fallthrough |
| 146 | case kRGB_888x_SkColorType: |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 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 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 179 | bool write_should_succeed(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo, bool isGPU) { |
| 180 | if (!SkImageInfoValidConversion(dstInfo, srcInfo)) { |
| 181 | return false; |
| 182 | } |
| 183 | if (!isGPU) { |
| 184 | return true; |
| 185 | } |
| 186 | // The GPU backend supports writing unpremul data to a premul dst but not vice versa. |
| 187 | if (srcInfo.alphaType() == kPremul_SkAlphaType && |
| 188 | dstInfo.alphaType() == kUnpremul_SkAlphaType) { |
| 189 | return false; |
| 190 | } |
| 191 | if (!SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) && |
| 192 | SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) { |
| 193 | return false; |
| 194 | } |
| 195 | // The source has no alpha value and the dst is only alpha |
| 196 | if (SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) && |
| 197 | SkColorTypeIsAlphaOnly(dstInfo.colorType())) { |
| 198 | return false; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | static bool check_write(skiatest::Reporter* reporter, SkSurface* surf, SkAlphaType surfaceAlphaType, |
| 204 | const SkBitmap& bitmap, int writeX, int writeY) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 205 | size_t canvasRowBytes; |
| 206 | const uint32_t* canvasPixels; |
reed@google.com | 1121170 | 2014-03-25 12:00:30 +0000 | [diff] [blame] | 207 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 208 | // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well. |
| 209 | // At some point this will be unsupported, as we won't allow accessBitmap() to magically call |
| 210 | // readPixels for the client. |
| 211 | SkBitmap secretDevBitmap; |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 212 | secretDevBitmap.allocN32Pixels(surf->width(), surf->height()); |
| 213 | if (!surf->readPixels(secretDevBitmap, 0, 0)) { |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 214 | return false; |
| 215 | } |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 216 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 217 | canvasRowBytes = secretDevBitmap.rowBytes(); |
| 218 | canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); |
| 219 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 220 | if (nullptr == canvasPixels) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 224 | if (surf->width() != DEV_W || surf->height() != DEV_H) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
John Stiles | 31954bf | 2020-08-07 17:35:54 -0400 | [diff] [blame] | 228 | const SkImageInfo& bmInfo = bitmap.info(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 229 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 230 | SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height()); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 231 | for (int cy = 0; cy < DEV_H; ++cy) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 232 | for (int cx = 0; cx < DEV_W; ++cx) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 233 | SkPMColor canvasPixel = canvasPixels[cx]; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 234 | if (writeRect.contains(cx, cy)) { |
| 235 | int bx = cx - writeX; |
| 236 | int by = cy - writeY; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 237 | uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(), |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 238 | bmInfo.colorType(), bmInfo.alphaType()); |
| 239 | bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType()); |
| 240 | SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(), |
| 241 | bmpColor8888); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 242 | if (bmInfo.alphaType() == kOpaque_SkAlphaType || |
| 243 | surfaceAlphaType == kOpaque_SkAlphaType) { |
| 244 | bmpPMColor |= 0xFF000000; |
| 245 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 246 | if (!check_pixel(bmpPMColor, canvasPixel, mul)) { |
| 247 | ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. " |
| 248 | "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul); |
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 | } else { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 252 | SkPMColor testColor = get_canvas_color(cx, cy); |
| 253 | if (canvasPixel != testColor) { |
| 254 | ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed." |
| 255 | " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel); |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 256 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | if (cy != DEV_H -1) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 261 | const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 262 | for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) { |
| 263 | bool check; |
| 264 | REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD))); |
| 265 | if (!check) { |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 266 | return false; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 270 | canvasPixels += canvasRowBytes/4; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 271 | } |
| 272 | |
bsalomon@google.com | 72f3dca | 2012-08-17 13:32:06 +0000 | [diff] [blame] | 273 | return true; |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 276 | #include "include/core/SkMallocPixelRef.h" |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 277 | |
| 278 | // This is a tricky pattern, because we have to setConfig+rowBytes AND specify |
| 279 | // a custom pixelRef (which also has to specify its rowBytes), so we have to be |
| 280 | // sure that the two rowBytes match (and the infos match). |
| 281 | // |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 282 | 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] | 283 | if (!bm->setInfo(info, rowBytes)) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 284 | return false; |
| 285 | } |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 286 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 287 | bm->setPixelRef(std::move(pr), 0, 0); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 288 | return true; |
| 289 | } |
| 290 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 291 | static void free_pixels(void* pixels, void* ctx) { |
| 292 | sk_free(pixels); |
| 293 | } |
| 294 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 295 | 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] | 296 | size_t rowBytes = tightRB ? 0 : 4 * w + 60; |
| 297 | SkImageInfo info = SkImageInfo::Make(w, h, ct, at); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 298 | if (!alloc_row_bytes(bm, info, rowBytes)) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 299 | return false; |
| 300 | } |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 301 | for (int y = 0; y < h; ++y) { |
| 302 | for (int x = 0; x < w; ++x) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 303 | *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] | 304 | } |
| 305 | } |
| 306 | return true; |
| 307 | } |
| 308 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 309 | static void call_writepixels(SkSurface* surface) { |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 310 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 311 | SkPMColor pixel = 0; |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 312 | surface->writePixels({info, &pixel, sizeof(SkPMColor)}, 0, 0); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 313 | } |
| 314 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 315 | DEF_TEST(WritePixelsSurfaceGenID, reporter) { |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 316 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 317 | auto surface(SkSurface::MakeRaster(info)); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 318 | uint32_t genID1 = surface->generationID(); |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 319 | call_writepixels(surface.get()); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 320 | uint32_t genID2 = surface->generationID(); |
| 321 | REPORTER_ASSERT(reporter, genID1 != genID2); |
| 322 | } |
| 323 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 324 | static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface, |
| 325 | const SkImageInfo& surfaceInfo) { |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 326 | const SkIRect testRects[] = { |
| 327 | // entire thing |
| 328 | DEV_RECT, |
| 329 | // larger on all sides |
| 330 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), |
| 331 | // fully contained |
| 332 | SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), |
| 333 | // outside top left |
| 334 | SkIRect::MakeLTRB(-10, -10, -1, -1), |
| 335 | // touching top left corner |
| 336 | SkIRect::MakeLTRB(-10, -10, 0, 0), |
| 337 | // overlapping top left corner |
| 338 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), |
| 339 | // overlapping top left and top right corners |
| 340 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), |
| 341 | // touching entire top edge |
| 342 | SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), |
| 343 | // overlapping top right corner |
| 344 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), |
| 345 | // contained in x, overlapping top edge |
| 346 | SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), |
| 347 | // outside top right corner |
| 348 | SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), |
| 349 | // touching top right corner |
| 350 | SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), |
| 351 | // overlapping top left and bottom left corners |
| 352 | SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), |
| 353 | // touching entire left edge |
| 354 | SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), |
| 355 | // overlapping bottom left corner |
| 356 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), |
| 357 | // contained in y, overlapping left edge |
| 358 | SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), |
| 359 | // outside bottom left corner |
| 360 | SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), |
| 361 | // touching bottom left corner |
| 362 | SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), |
| 363 | // overlapping bottom left and bottom right corners |
| 364 | SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 365 | // touching entire left edge |
| 366 | SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), |
| 367 | // overlapping bottom right corner |
| 368 | SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), |
| 369 | // overlapping top right and bottom right corners |
| 370 | SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), |
| 371 | }; |
| 372 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 373 | SkCanvas* canvas = surface->getCanvas(); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 374 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 375 | static const struct { |
| 376 | SkColorType fColorType; |
| 377 | SkAlphaType fAlphaType; |
| 378 | } gSrcConfigs[] = { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 379 | {kRGBA_8888_SkColorType, kPremul_SkAlphaType}, |
| 380 | {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType}, |
| 381 | {kRGB_888x_SkColorType, kOpaque_SkAlphaType}, |
| 382 | {kBGRA_8888_SkColorType, kPremul_SkAlphaType}, |
| 383 | {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType}, |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 384 | }; |
| 385 | for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { |
| 386 | const SkIRect& rect = testRects[r]; |
| 387 | for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { |
| 388 | for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { |
| 389 | const SkColorType ct = gSrcConfigs[c].fColorType; |
| 390 | const SkAlphaType at = gSrcConfigs[c].fAlphaType; |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 391 | |
Robert Phillips | b27b38b | 2020-07-10 16:23:47 -0400 | [diff] [blame] | 392 | bool isGPU = SkToBool(surface->getCanvas()->recordingContext()); |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 393 | fill_surface(surface); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 394 | SkBitmap bmp; |
| 395 | REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(), |
| 396 | rect.height(), SkToBool(tightBmp))); |
| 397 | uint32_t idBefore = surface->generationID(); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 398 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 399 | surface->writePixels(bmp, rect.fLeft, rect.fTop); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 400 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 401 | uint32_t idAfter = surface->generationID(); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 402 | REPORTER_ASSERT(reporter, check_write(reporter, surface, surfaceInfo.alphaType(), |
| 403 | bmp, rect.fLeft, rect.fTop)); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 404 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 405 | // we should change the genID iff pixels were actually written. |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 406 | SkIRect canvasRect = SkIRect::MakeSize(canvas->getBaseLayerSize()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 407 | SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop, |
| 408 | bmp.width(), bmp.height()); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 409 | bool expectSuccess = SkIRect::Intersects(canvasRect, writeRect) && |
| 410 | write_should_succeed(surfaceInfo, bmp.info(), isGPU); |
| 411 | REPORTER_ASSERT(reporter, expectSuccess == (idBefore != idAfter)); |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 416 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 417 | DEF_TEST(WritePixels, reporter) { |
| 418 | const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
| 419 | for (auto& tightRowBytes : { true, false }) { |
| 420 | const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100; |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 421 | const size_t size = info.computeByteSize(rowBytes); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 422 | void* pixels = sk_malloc_throw(size); |
| 423 | // if rowBytes isn't tight then set the padding to a known value |
| 424 | if (!tightRowBytes) { |
| 425 | memset(pixels, DEV_PAD, size); |
| 426 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 427 | auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes, |
| 428 | free_pixels, nullptr)); |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 429 | test_write_pixels(reporter, surface.get(), info); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 432 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 433 | static void test_write_pixels(skiatest::Reporter* reporter, |
| 434 | GrRecordingContext* rContext, |
| 435 | int sampleCnt) { |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 436 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 437 | for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 438 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(rContext, |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 439 | SkBudgeted::kNo, ii, sampleCnt, |
| 440 | origin, nullptr)); |
| 441 | if (surface) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 442 | test_write_pixels(reporter, surface.get(), ii); |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 443 | } |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 448 | test_write_pixels(reporter, ctxInfo.directContext(), 1); |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsMSAA_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 452 | test_write_pixels(reporter, ctxInfo.directContext(), 1); |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 453 | } |
| 454 | |
Robert Phillips | 9b16f81 | 2019-05-17 10:01:21 -0400 | [diff] [blame] | 455 | static void test_write_pixels_non_texture(skiatest::Reporter* reporter, |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 456 | GrDirectContext* dContext, |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 457 | int sampleCnt) { |
Stephen White | af6cf63 | 2020-06-05 16:19:45 -0400 | [diff] [blame] | 458 | // Dawn currently doesn't support writePixels to a texture-as-render-target. |
| 459 | // See http://skbug.com/10336. |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 460 | if (GrBackendApi::kDawn == dContext->backend()) { |
Stephen White | af6cf63 | 2020-06-05 16:19:45 -0400 | [diff] [blame] | 461 | return; |
| 462 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 463 | for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) { |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 464 | SkColorType colorType = kN32_SkColorType; |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame] | 465 | auto surface = sk_gpu_test::MakeBackendRenderTargetSurface(dContext, |
| 466 | {DEV_W, DEV_H}, |
| 467 | origin, |
| 468 | sampleCnt, |
| 469 | colorType); |
Stephen White | af6cf63 | 2020-06-05 16:19:45 -0400 | [diff] [blame] | 470 | if (surface) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 471 | auto ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
| 472 | test_write_pixels(reporter, surface.get(), ii); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 478 | test_write_pixels_non_texture(reporter, ctxInfo.directContext(), 1); |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 479 | } |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 480 | |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 481 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTextureMSAA_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 482 | test_write_pixels_non_texture(reporter, ctxInfo.directContext(), 4); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 483 | } |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 484 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 485 | static sk_sp<SkSurface> create_surf(GrRecordingContext* rContext, int width, int height) { |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 486 | const SkImageInfo ii = SkImageInfo::Make(width, height, |
| 487 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 488 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 489 | sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 490 | surf->flushAndSubmit(); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 491 | return surf; |
| 492 | } |
| 493 | |
| 494 | static sk_sp<SkImage> upload(const sk_sp<SkSurface>& surf, SkColor color) { |
| 495 | const SkImageInfo smII = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 496 | SkBitmap bm; |
| 497 | bm.allocPixels(smII); |
| 498 | bm.eraseColor(color); |
| 499 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 500 | surf->writePixels(bm, 0, 0); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 501 | |
| 502 | return surf->makeImageSnapshot(); |
| 503 | } |
| 504 | |
| 505 | // This is tests whether the first writePixels is completed before the |
| 506 | // second writePixels takes effect (i.e., that writePixels correctly flushes |
| 507 | // in between uses of the shared backing resource). |
Brian Salomon | b3479ce | 2021-04-06 09:05:02 -0400 | [diff] [blame] | 508 | // The unit test fails on Nexus 6P/Android M with driver 129.0 without the |
| 509 | // "DisallowTexSubImageForUnormConfigTexturesEverBoundToFBO" workaround enabled. |
| 510 | // skbug.com/11834 |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 511 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsPendingIO, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 512 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 513 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 514 | const GrCaps* caps = context->priv().caps(); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 515 | |
| 516 | static const int kFullSize = 62; |
| 517 | static const int kHalfSize = 31; |
| 518 | |
| 519 | static const uint32_t kLeftColor = 0xFF222222; |
| 520 | static const uint32_t kRightColor = 0xFFAAAAAA; |
| 521 | |
| 522 | const SkImageInfo fullII = SkImageInfo::Make(kFullSize, kFullSize, |
| 523 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 524 | const SkImageInfo halfII = SkImageInfo::Make(kHalfSize, kFullSize, |
| 525 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 526 | |
| 527 | sk_sp<SkSurface> dest = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, fullII); |
| 528 | |
| 529 | { |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 530 | // Seed the resource cached with a scratch texture that will be reused by writePixels |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 531 | static constexpr SkISize kDims = {32, 64}; |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 532 | |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 533 | const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 534 | GrRenderable::kNo); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 535 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 536 | sk_sp<GrTextureProxy> temp = proxyProvider->createProxy( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 537 | format, kDims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kApprox, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 538 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 539 | temp->instantiate(context->priv().resourceProvider()); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // Create the surfaces and flush them to ensure there is no lingering pendingIO |
| 543 | sk_sp<SkSurface> leftSurf = create_surf(context, kHalfSize, kFullSize); |
| 544 | sk_sp<SkSurface> rightSurf = create_surf(context, kHalfSize, kFullSize); |
| 545 | |
| 546 | sk_sp<SkImage> leftImg = upload(std::move(leftSurf), kLeftColor); |
| 547 | dest->getCanvas()->drawImage(std::move(leftImg), 0, 0); |
| 548 | |
| 549 | sk_sp<SkImage> rightImg = upload(std::move(rightSurf), kRightColor); |
| 550 | dest->getCanvas()->drawImage(std::move(rightImg), kHalfSize, 0); |
| 551 | |
| 552 | SkBitmap bm; |
| 553 | bm.allocPixels(fullII); |
| 554 | SkAssertResult(dest->readPixels(bm, 0, 0)); |
| 555 | |
| 556 | bool isCorrect = true; |
| 557 | for (int y = 0; isCorrect && y < 16; ++y) { |
| 558 | const uint32_t* sl = bm.getAddr32(0, y); |
| 559 | |
| 560 | for (int x = 0; x < 16; ++x) { |
| 561 | if (kLeftColor != sl[x]) { |
| 562 | isCorrect = false; |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | for (int x = kHalfSize; x < kHalfSize+16; ++x) { |
| 567 | if (kRightColor != sl[x]) { |
| 568 | isCorrect = false; |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | REPORTER_ASSERT(reporter, isCorrect); |
| 575 | } |
Michael Ludwig | a5c9058 | 2021-03-09 15:35:32 -0500 | [diff] [blame] | 576 | |
| 577 | DEF_TEST(WritePixels_InvalidRowBytes, reporter) { |
| 578 | auto dstII = SkImageInfo::Make({10, 10}, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 579 | auto surf = SkSurface::MakeRaster(dstII); |
| 580 | for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) { |
| 581 | auto colorType = static_cast<SkColorType>(ct); |
| 582 | |
| 583 | size_t bpp = SkColorTypeBytesPerPixel(colorType); |
| 584 | if (bpp <= 1) { |
| 585 | continue; |
| 586 | } |
| 587 | auto srcII = dstII.makeColorType(colorType); |
| 588 | size_t badRowBytes = (surf->width() + 1)*bpp - 1; |
| 589 | auto storage = std::make_unique<char[]>(badRowBytes*surf->height()); |
| 590 | memset(storage.get(), 0, badRowBytes * surf->height()); |
| 591 | // SkSurface::writePixels doesn't report bool, SkCanvas's does. |
| 592 | REPORTER_ASSERT(reporter, |
| 593 | !surf->getCanvas()->writePixels(srcII, storage.get(), badRowBytes, 0, 0)); |
| 594 | } |
| 595 | } |