blob: b6837ef4835d470fa9aee259b22ecdf4a4d45ae8 [file] [log] [blame]
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
10#include "include/private/SkColorData.h"
11#include "include/private/SkImageInfoPriv.h"
12#include "src/core/SkMathPriv.h"
13#include "tests/Test.h"
Greg Danielc1ad77c2020-05-06 11:40:03 -040014#include "tests/TestUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tools/ToolUtils.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000016
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040018#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrContextPriv.h"
20#include "src/gpu/GrGpu.h"
21#include "src/gpu/GrProxyProvider.h"
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000022
kkinnunen15302832015-12-01 04:35:26 -080023#include <initializer_list>
24
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000025static const int DEV_W = 100, DEV_H = 100;
26static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000027static const U8CPU DEV_PAD = 0xee;
28
bsalomonf0674512015-07-28 13:26:15 -070029static SkPMColor get_canvas_color(int x, int y) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000030 SkASSERT(x >= 0 && x < DEV_W);
31 SkASSERT(y >= 0 && y < DEV_H);
32
33 U8CPU r = x;
34 U8CPU g = y;
35 U8CPU b = 0xc;
36
bsalomon@google.com31648eb2011-11-23 15:01:08 +000037 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000038 switch ((x+y) % 5) {
39 case 0:
40 a = 0xff;
41 break;
42 case 1:
43 a = 0x80;
44 break;
45 case 2:
46 a = 0xCC;
47 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000048 case 3:
49 a = 0x00;
50 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000051 case 4:
52 a = 0x01;
53 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000054 }
55 return SkPremultiplyARGBInline(a, r, g, b);
56}
57
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000058// assumes any premu/.unpremul has been applied
bsalomonf0674512015-07-28 13:26:15 -070059static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000060 uint32_t r32;
61 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
reed@google.com7111d462014-03-25 16:20:24 +000062 switch (ct) {
63 case kBGRA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000064 result[0] = b;
65 result[1] = g;
66 result[2] = r;
67 result[3] = a;
68 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040069 case kRGBA_8888_SkColorType: // fallthrough
70 case kRGB_888x_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000071 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
bsalomonf0674512015-07-28 13:26:15 -070083static uint32_t get_bitmap_color(int x, int y, int w, SkColorType ct, SkAlphaType at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000084 int n = y * w + x;
85 U8CPU b = n & 0xff;
86 U8CPU g = (n >> 8) & 0xff;
87 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000088 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000089 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.com7111d462014-03-25 16:20:24 +0000106 if (kPremul_SkAlphaType == at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000107 r = SkMulDiv255Ceiling(r, a);
108 g = SkMulDiv255Ceiling(g, a);
109 b = SkMulDiv255Ceiling(b, a);
110 }
bsalomonf0674512015-07-28 13:26:15 -0700111 return pack_color_type(ct, a, r, g , b);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000112}
113
Mike Reed4c790bd2018-02-08 14:10:40 -0500114static void fill_surface(SkSurface* surface) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000115 SkBitmap bmp;
Mike Reed4c790bd2018-02-08 14:10:40 -0500116 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.comd58a1cd2011-11-10 20:57:43 +0000120 }
121 }
Mike Reed4c790bd2018-02-08 14:10:40 -0500122 surface->writePixels(bmp, 0, 0);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000123}
124
reed@google.com7111d462014-03-25 16:20:24 +0000125/**
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 */
129static 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
141static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
142 if (kUnpremul_SkAlphaType == at) {
143 color = premul(color);
144 }
145 switch (ct) {
John Stiles30212b72020-06-11 17:55:07 -0400146 case kRGBA_8888_SkColorType: // fallthrough
147 case kRGB_888x_SkColorType:
reed@google.com7111d462014-03-25 16:20:24 +0000148 color = SkSwizzle_RGBA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000149 break;
reed@google.com7111d462014-03-25 16:20:24 +0000150 case kBGRA_8888_SkColorType:
151 color = SkSwizzle_BGRA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000152 break;
153 default:
reed@google.com7111d462014-03-25 16:20:24 +0000154 SkASSERT(0);
155 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000156 }
reed@google.com7111d462014-03-25 16:20:24 +0000157 return color;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000158}
159
bsalomonf0674512015-07-28 13:26:15 -0700160static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000161 if (!didPremulConversion) {
162 return a == b;
163 }
164 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
165 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
166 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
167 int32_t aB = SkGetPackedB32(a);
168
169 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
170 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
171 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
172 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
173
174 return aA == bA &&
175 SkAbs32(aR - bR) <= 1 &&
176 SkAbs32(aG - bG) <= 1 &&
177 SkAbs32(aB - bB) <= 1;
178}
179
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400180bool write_should_succeed(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo, bool isGPU) {
181 if (!SkImageInfoValidConversion(dstInfo, srcInfo)) {
182 return false;
183 }
184 if (!isGPU) {
185 return true;
186 }
187 // The GPU backend supports writing unpremul data to a premul dst but not vice versa.
188 if (srcInfo.alphaType() == kPremul_SkAlphaType &&
189 dstInfo.alphaType() == kUnpremul_SkAlphaType) {
190 return false;
191 }
192 if (!SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
193 SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
194 return false;
195 }
196 // The source has no alpha value and the dst is only alpha
197 if (SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
198 SkColorTypeIsAlphaOnly(dstInfo.colorType())) {
199 return false;
200 }
201 return true;
202}
203
204static bool check_write(skiatest::Reporter* reporter, SkSurface* surf, SkAlphaType surfaceAlphaType,
205 const SkBitmap& bitmap, int writeX, int writeY) {
reed@google.com7111d462014-03-25 16:20:24 +0000206 size_t canvasRowBytes;
207 const uint32_t* canvasPixels;
reed@google.com11211702014-03-25 12:00:30 +0000208
reed@google.com7111d462014-03-25 16:20:24 +0000209 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
210 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
211 // readPixels for the client.
212 SkBitmap secretDevBitmap;
Mike Reedf1942192017-07-21 14:24:29 -0400213 secretDevBitmap.allocN32Pixels(surf->width(), surf->height());
214 if (!surf->readPixels(secretDevBitmap, 0, 0)) {
Brian Salomon71d9d842016-11-03 13:42:00 -0400215 return false;
216 }
reed52d9ac62014-06-30 09:05:34 -0700217
reed@google.com7111d462014-03-25 16:20:24 +0000218 canvasRowBytes = secretDevBitmap.rowBytes();
219 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
220
halcanary96fcdcc2015-08-27 07:41:13 -0700221 if (nullptr == canvasPixels) {
reed@google.com7111d462014-03-25 16:20:24 +0000222 return false;
223 }
224
Mike Reedf1942192017-07-21 14:24:29 -0400225 if (surf->width() != DEV_W || surf->height() != DEV_H) {
reed@google.com7111d462014-03-25 16:20:24 +0000226 return false;
227 }
228
229 const SkImageInfo bmInfo = bitmap.info();
230
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000231 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000232 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000233 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000234 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000235 if (writeRect.contains(cx, cy)) {
236 int bx = cx - writeX;
237 int by = cy - writeY;
bsalomonf0674512015-07-28 13:26:15 -0700238 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
reed@google.com7111d462014-03-25 16:20:24 +0000239 bmInfo.colorType(), bmInfo.alphaType());
240 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
241 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
242 bmpColor8888);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400243 if (bmInfo.alphaType() == kOpaque_SkAlphaType ||
244 surfaceAlphaType == kOpaque_SkAlphaType) {
245 bmpPMColor |= 0xFF000000;
246 }
bsalomonf0674512015-07-28 13:26:15 -0700247 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
248 ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
249 "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000250 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000251 }
252 } else {
bsalomonf0674512015-07-28 13:26:15 -0700253 SkPMColor testColor = get_canvas_color(cx, cy);
254 if (canvasPixel != testColor) {
255 ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
256 " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000257 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000258 }
259 }
260 }
261 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000262 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000263 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
264 bool check;
265 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
266 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000267 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000268 }
269 }
270 }
reed@google.com7111d462014-03-25 16:20:24 +0000271 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000272 }
273
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000274 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000275}
276
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500277#include "include/core/SkMallocPixelRef.h"
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000278
279// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
280// a custom pixelRef (which also has to specify its rowBytes), so we have to be
281// sure that the two rowBytes match (and the infos match).
282//
bsalomonf0674512015-07-28 13:26:15 -0700283static bool alloc_row_bytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000284 if (!bm->setInfo(info, rowBytes)) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000285 return false;
286 }
Mike Reed086a4272017-07-18 10:53:11 -0400287 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
Hal Canary1b3387b2016-12-12 13:48:12 -0500288 bm->setPixelRef(std::move(pr), 0, 0);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000289 return true;
290}
291
reed52d9ac62014-06-30 09:05:34 -0700292static void free_pixels(void* pixels, void* ctx) {
293 sk_free(pixels);
294}
295
reed52d9ac62014-06-30 09:05:34 -0700296static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
reed@google.com7111d462014-03-25 16:20:24 +0000297 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
298 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
bsalomonf0674512015-07-28 13:26:15 -0700299 if (!alloc_row_bytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000300 return false;
301 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302 for (int y = 0; y < h; ++y) {
303 for (int x = 0; x < w; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700304 *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000305 }
306 }
307 return true;
308}
309
Mike Reed4c790bd2018-02-08 14:10:40 -0500310static void call_writepixels(SkSurface* surface) {
reed4af35f32014-06-27 17:47:49 -0700311 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
312 SkPMColor pixel = 0;
Mike Reed4c790bd2018-02-08 14:10:40 -0500313 surface->writePixels({info, &pixel, sizeof(SkPMColor)}, 0, 0);
reed4af35f32014-06-27 17:47:49 -0700314}
315
kkinnunen15302832015-12-01 04:35:26 -0800316DEF_TEST(WritePixelsSurfaceGenID, reporter) {
reed4af35f32014-06-27 17:47:49 -0700317 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -0700318 auto surface(SkSurface::MakeRaster(info));
reed4af35f32014-06-27 17:47:49 -0700319 uint32_t genID1 = surface->generationID();
Mike Reed4c790bd2018-02-08 14:10:40 -0500320 call_writepixels(surface.get());
reed4af35f32014-06-27 17:47:49 -0700321 uint32_t genID2 = surface->generationID();
322 REPORTER_ASSERT(reporter, genID1 != genID2);
323}
324
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400325static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface,
326 const SkImageInfo& surfaceInfo) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327 const SkIRect testRects[] = {
328 // entire thing
329 DEV_RECT,
330 // larger on all sides
331 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
332 // fully contained
333 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
334 // outside top left
335 SkIRect::MakeLTRB(-10, -10, -1, -1),
336 // touching top left corner
337 SkIRect::MakeLTRB(-10, -10, 0, 0),
338 // overlapping top left corner
339 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
340 // overlapping top left and top right corners
341 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
342 // touching entire top edge
343 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
344 // overlapping top right corner
345 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
346 // contained in x, overlapping top edge
347 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
348 // outside top right corner
349 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
350 // touching top right corner
351 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
352 // overlapping top left and bottom left corners
353 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
354 // touching entire left edge
355 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
356 // overlapping bottom left corner
357 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
358 // contained in y, overlapping left edge
359 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
360 // outside bottom left corner
361 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
362 // touching bottom left corner
363 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
364 // overlapping bottom left and bottom right corners
365 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
366 // touching entire left edge
367 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
368 // overlapping bottom right corner
369 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
370 // overlapping top right and bottom right corners
371 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
372 };
373
Mike Reedf1942192017-07-21 14:24:29 -0400374 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000375
kkinnunen15302832015-12-01 04:35:26 -0800376 static const struct {
377 SkColorType fColorType;
378 SkAlphaType fAlphaType;
379 } gSrcConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400380 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
381 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
382 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
383 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
384 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800385 };
386 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
387 const SkIRect& rect = testRects[r];
388 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
389 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
390 const SkColorType ct = gSrcConfigs[c].fColorType;
391 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000392
Robert Phillipsb27b38b2020-07-10 16:23:47 -0400393 bool isGPU = SkToBool(surface->getCanvas()->recordingContext());
Mike Reed4c790bd2018-02-08 14:10:40 -0500394 fill_surface(surface);
kkinnunen15302832015-12-01 04:35:26 -0800395 SkBitmap bmp;
396 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
397 rect.height(), SkToBool(tightBmp)));
398 uint32_t idBefore = surface->generationID();
reed@google.com7111d462014-03-25 16:20:24 +0000399
Mike Reed4c790bd2018-02-08 14:10:40 -0500400 surface->writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000401
kkinnunen15302832015-12-01 04:35:26 -0800402 uint32_t idAfter = surface->generationID();
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400403 REPORTER_ASSERT(reporter, check_write(reporter, surface, surfaceInfo.alphaType(),
404 bmp, rect.fLeft, rect.fTop));
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000405
kkinnunen15302832015-12-01 04:35:26 -0800406 // we should change the genID iff pixels were actually written.
Mike Reedf1942192017-07-21 14:24:29 -0400407 SkIRect canvasRect = SkIRect::MakeSize(canvas->getBaseLayerSize());
kkinnunen15302832015-12-01 04:35:26 -0800408 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
409 bmp.width(), bmp.height());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400410 bool expectSuccess = SkIRect::Intersects(canvasRect, writeRect) &&
411 write_should_succeed(surfaceInfo, bmp.info(), isGPU);
412 REPORTER_ASSERT(reporter, expectSuccess == (idBefore != idAfter));
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000413 }
414 }
415 }
416}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400417
kkinnunen15302832015-12-01 04:35:26 -0800418DEF_TEST(WritePixels, reporter) {
419 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
420 for (auto& tightRowBytes : { true, false }) {
421 const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
Mike Reedf0ffb892017-10-03 14:47:21 -0400422 const size_t size = info.computeByteSize(rowBytes);
kkinnunen15302832015-12-01 04:35:26 -0800423 void* pixels = sk_malloc_throw(size);
424 // if rowBytes isn't tight then set the padding to a known value
425 if (!tightRowBytes) {
426 memset(pixels, DEV_PAD, size);
427 }
reede8f30622016-03-23 18:59:25 -0700428 auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
429 free_pixels, nullptr));
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400430 test_write_pixels(reporter, surface.get(), info);
kkinnunen15302832015-12-01 04:35:26 -0800431 }
432}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400433
Robert Phillipseffd13f2020-07-20 15:00:36 -0400434static void test_write_pixels(skiatest::Reporter* reporter,
435 GrRecordingContext* rContext,
436 int sampleCnt) {
robertphillips7e922762016-07-26 11:38:17 -0700437 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
Brian Salomon3d86a192018-02-27 16:46:11 -0500438 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400439 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(rContext,
Brian Salomon3d86a192018-02-27 16:46:11 -0500440 SkBudgeted::kNo, ii, sampleCnt,
441 origin, nullptr));
442 if (surface) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400443 test_write_pixels(reporter, surface.get(), ii);
Brian Salomon3d86a192018-02-27 16:46:11 -0500444 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500445 }
446}
447
448DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400449 test_write_pixels(reporter, ctxInfo.directContext(), 1);
Brian Salomon3d86a192018-02-27 16:46:11 -0500450}
451
452DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsMSAA_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400453 test_write_pixels(reporter, ctxInfo.directContext(), 1);
Brian Salomon3d86a192018-02-27 16:46:11 -0500454}
455
Robert Phillips9b16f812019-05-17 10:01:21 -0400456static void test_write_pixels_non_texture(skiatest::Reporter* reporter,
Robert Phillipseffd13f2020-07-20 15:00:36 -0400457 GrDirectContext* dContext,
Brian Salomon3d86a192018-02-27 16:46:11 -0500458 int sampleCnt) {
Stephen Whiteaf6cf632020-06-05 16:19:45 -0400459 // Dawn currently doesn't support writePixels to a texture-as-render-target.
460 // See http://skbug.com/10336.
Robert Phillipseffd13f2020-07-20 15:00:36 -0400461 if (GrBackendApi::kDawn == dContext->backend()) {
Stephen Whiteaf6cf632020-06-05 16:19:45 -0400462 return;
463 }
kkinnunen15302832015-12-01 04:35:26 -0800464 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400465 GrBackendTexture backendTex;
Robert Phillipseffd13f2020-07-20 15:00:36 -0400466 CreateBackendTexture(dContext, &backendTex, DEV_W, DEV_H, kRGBA_8888_SkColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400467 SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kYes,
Robert Phillipseffd13f2020-07-20 15:00:36 -0400468 GrProtected::kNo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400469 if (!backendTex.isValid()) {
470 continue;
471 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500472 SkColorType colorType = kN32_SkColorType;
473 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTextureAsRenderTarget(
Robert Phillipseffd13f2020-07-20 15:00:36 -0400474 dContext, backendTex, origin, sampleCnt, colorType, nullptr, nullptr));
Stephen Whiteaf6cf632020-06-05 16:19:45 -0400475 if (surface) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400476 auto ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
477 test_write_pixels(reporter, surface.get(), ii);
Brian Osman33910292017-04-18 14:38:53 -0400478 }
Robert Phillipseffd13f2020-07-20 15:00:36 -0400479 dContext->deleteBackendTexture(backendTex);
Brian Osman33910292017-04-18 14:38:53 -0400480 }
481}
482
483DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400484 test_write_pixels_non_texture(reporter, ctxInfo.directContext(), 1);
Brian Salomon3d86a192018-02-27 16:46:11 -0500485}
Brian Osman33910292017-04-18 14:38:53 -0400486
Brian Salomon3d86a192018-02-27 16:46:11 -0500487DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTextureMSAA_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400488 test_write_pixels_non_texture(reporter, ctxInfo.directContext(), 4);
kkinnunen15302832015-12-01 04:35:26 -0800489}
Robert Phillips7bbbf622017-10-17 07:36:59 -0400490
Robert Phillipseffd13f2020-07-20 15:00:36 -0400491static sk_sp<SkSurface> create_surf(GrRecordingContext* rContext, int width, int height) {
Robert Phillips7bbbf622017-10-17 07:36:59 -0400492 const SkImageInfo ii = SkImageInfo::Make(width, height,
493 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
494
Robert Phillipseffd13f2020-07-20 15:00:36 -0400495 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, ii);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400496 surf->flushAndSubmit();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400497 return surf;
498}
499
500static sk_sp<SkImage> upload(const sk_sp<SkSurface>& surf, SkColor color) {
501 const SkImageInfo smII = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
502 SkBitmap bm;
503 bm.allocPixels(smII);
504 bm.eraseColor(color);
505
Mike Reed4c790bd2018-02-08 14:10:40 -0500506 surf->writePixels(bm, 0, 0);
Robert Phillips7bbbf622017-10-17 07:36:59 -0400507
508 return surf->makeImageSnapshot();
509}
510
511// This is tests whether the first writePixels is completed before the
512// second writePixels takes effect (i.e., that writePixels correctly flushes
513// in between uses of the shared backing resource).
514DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsPendingIO, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400515 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500516 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400517 const GrCaps* caps = context->priv().caps();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400518
519 static const int kFullSize = 62;
520 static const int kHalfSize = 31;
521
522 static const uint32_t kLeftColor = 0xFF222222;
523 static const uint32_t kRightColor = 0xFFAAAAAA;
524
525 const SkImageInfo fullII = SkImageInfo::Make(kFullSize, kFullSize,
526 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
527 const SkImageInfo halfII = SkImageInfo::Make(kHalfSize, kFullSize,
528 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
529
530 sk_sp<SkSurface> dest = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, fullII);
531
532 {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400533 // Seed the resource cached with a scratch texture that will be reused by writePixels
Brian Salomona56a7462020-02-07 14:17:25 -0500534 static constexpr SkISize kDims = {32, 64};
Robert Phillips7bbbf622017-10-17 07:36:59 -0400535
Robert Phillips0a15cc62019-07-30 12:49:10 -0400536 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
537 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500538
Brian Salomon2a4f9832018-03-03 22:43:43 -0500539 sk_sp<GrTextureProxy> temp = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400540 format, kDims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kApprox,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400541 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500542 temp->instantiate(context->priv().resourceProvider());
Robert Phillips7bbbf622017-10-17 07:36:59 -0400543 }
544
545 // Create the surfaces and flush them to ensure there is no lingering pendingIO
546 sk_sp<SkSurface> leftSurf = create_surf(context, kHalfSize, kFullSize);
547 sk_sp<SkSurface> rightSurf = create_surf(context, kHalfSize, kFullSize);
548
549 sk_sp<SkImage> leftImg = upload(std::move(leftSurf), kLeftColor);
550 dest->getCanvas()->drawImage(std::move(leftImg), 0, 0);
551
552 sk_sp<SkImage> rightImg = upload(std::move(rightSurf), kRightColor);
553 dest->getCanvas()->drawImage(std::move(rightImg), kHalfSize, 0);
554
555 SkBitmap bm;
556 bm.allocPixels(fullII);
557 SkAssertResult(dest->readPixels(bm, 0, 0));
558
559 bool isCorrect = true;
560 for (int y = 0; isCorrect && y < 16; ++y) {
561 const uint32_t* sl = bm.getAddr32(0, y);
562
563 for (int x = 0; x < 16; ++x) {
564 if (kLeftColor != sl[x]) {
565 isCorrect = false;
566 break;
567 }
568 }
569 for (int x = kHalfSize; x < kHalfSize+16; ++x) {
570 if (kRightColor != sl[x]) {
571 isCorrect = false;
572 break;
573 }
574 }
575 }
576
577 REPORTER_ASSERT(reporter, isCorrect);
578}