blob: 1f5758184f0fcabfd22f0841400ba7d90027969b [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
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +00008#include "SkCanvas.h"
Cary Clarka4083c92017-09-15 11:59:23 -04009#include "SkColorData.h"
Brian Salomon5fba7ad2018-03-22 10:01:16 -040010#include "SkImageInfoPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000011#include "SkMathPriv.h"
reed4af35f32014-06-27 17:47:49 -070012#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000013#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000014#include "sk_tool_utils.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000015
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000016#if SK_SUPPORT_GPU
Brian Salomond17f6582017-07-19 18:28:58 -040017#include "GrBackendSurface.h"
kkinnunen15302832015-12-01 04:35:26 -080018#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050019#include "GrContextPriv.h"
Brian Osman33910292017-04-18 14:38:53 -040020#include "GrGpu.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050021#include "GrProxyProvider.h"
Brian Salomond17f6582017-07-19 18:28:58 -040022#include "GrTest.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000023#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000024
kkinnunen15302832015-12-01 04:35:26 -080025#include <initializer_list>
26
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000027static const int DEV_W = 100, DEV_H = 100;
28static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000029static const U8CPU DEV_PAD = 0xee;
30
bsalomonf0674512015-07-28 13:26:15 -070031static SkPMColor get_canvas_color(int x, int y) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000032 SkASSERT(x >= 0 && x < DEV_W);
33 SkASSERT(y >= 0 && y < DEV_H);
34
35 U8CPU r = x;
36 U8CPU g = y;
37 U8CPU b = 0xc;
38
bsalomon@google.com31648eb2011-11-23 15:01:08 +000039 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000040 switch ((x+y) % 5) {
41 case 0:
42 a = 0xff;
43 break;
44 case 1:
45 a = 0x80;
46 break;
47 case 2:
48 a = 0xCC;
49 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000050 case 3:
51 a = 0x00;
52 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000053 case 4:
54 a = 0x01;
55 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000056 }
57 return SkPremultiplyARGBInline(a, r, g, b);
58}
59
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000060// assumes any premu/.unpremul has been applied
bsalomonf0674512015-07-28 13:26:15 -070061static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000062 uint32_t r32;
63 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
reed@google.com7111d462014-03-25 16:20:24 +000064 switch (ct) {
65 case kBGRA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000066 result[0] = b;
67 result[1] = g;
68 result[2] = r;
69 result[3] = a;
70 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040071 case kRGBA_8888_SkColorType: // fallthrough
72 case kRGB_888x_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000073 result[0] = r;
74 result[1] = g;
75 result[2] = b;
76 result[3] = a;
77 break;
78 default:
79 SkASSERT(0);
80 return 0;
81 }
82 return r32;
83}
84
bsalomonf0674512015-07-28 13:26:15 -070085static uint32_t get_bitmap_color(int x, int y, int w, SkColorType ct, SkAlphaType at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000086 int n = y * w + x;
87 U8CPU b = n & 0xff;
88 U8CPU g = (n >> 8) & 0xff;
89 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000090 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000091 switch ((x+y) % 5) {
92 case 4:
93 a = 0xff;
94 break;
95 case 3:
96 a = 0x80;
97 break;
98 case 2:
99 a = 0xCC;
100 break;
101 case 1:
102 a = 0x01;
103 break;
104 case 0:
105 a = 0x00;
106 break;
107 }
reed@google.com7111d462014-03-25 16:20:24 +0000108 if (kPremul_SkAlphaType == at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000109 r = SkMulDiv255Ceiling(r, a);
110 g = SkMulDiv255Ceiling(g, a);
111 b = SkMulDiv255Ceiling(b, a);
112 }
bsalomonf0674512015-07-28 13:26:15 -0700113 return pack_color_type(ct, a, r, g , b);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000114}
115
Mike Reed4c790bd2018-02-08 14:10:40 -0500116static void fill_surface(SkSurface* surface) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000117 SkBitmap bmp;
Mike Reed4c790bd2018-02-08 14:10:40 -0500118 bmp.allocN32Pixels(DEV_W, DEV_H);
119 for (int y = 0; y < DEV_H; ++y) {
120 for (int x = 0; x < DEV_W; ++x) {
121 *bmp.getAddr32(x, y) = get_canvas_color(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000122 }
123 }
Mike Reed4c790bd2018-02-08 14:10:40 -0500124 surface->writePixels(bmp, 0, 0);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000125}
126
reed@google.com7111d462014-03-25 16:20:24 +0000127/**
128 * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
129 * Thus this routine doesn't need to know the exact colortype
130 */
131static uint32_t premul(uint32_t color) {
132 unsigned a = SkGetPackedA32(color);
133 // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
134 unsigned c0 = SkGetPackedR32(color);
135 unsigned c1 = SkGetPackedG32(color);
136 unsigned c2 = SkGetPackedB32(color);
137 c0 = SkMulDiv255Ceiling(c0, a);
138 c1 = SkMulDiv255Ceiling(c1, a);
139 c2 = SkMulDiv255Ceiling(c2, a);
140 return SkPackARGB32NoCheck(a, c0, c1, c2);
141}
142
143static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
144 if (kUnpremul_SkAlphaType == at) {
145 color = premul(color);
146 }
147 switch (ct) {
148 case kRGBA_8888_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400149 case kRGB_888x_SkColorType: // fallthrough
reed@google.com7111d462014-03-25 16:20:24 +0000150 color = SkSwizzle_RGBA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000151 break;
reed@google.com7111d462014-03-25 16:20:24 +0000152 case kBGRA_8888_SkColorType:
153 color = SkSwizzle_BGRA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000154 break;
155 default:
reed@google.com7111d462014-03-25 16:20:24 +0000156 SkASSERT(0);
157 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000158 }
reed@google.com7111d462014-03-25 16:20:24 +0000159 return color;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000160}
161
bsalomonf0674512015-07-28 13:26:15 -0700162static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000163 if (!didPremulConversion) {
164 return a == b;
165 }
166 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
167 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
168 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
169 int32_t aB = SkGetPackedB32(a);
170
171 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
172 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
173 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
174 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
175
176 return aA == bA &&
177 SkAbs32(aR - bR) <= 1 &&
178 SkAbs32(aG - bG) <= 1 &&
179 SkAbs32(aB - bB) <= 1;
180}
181
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400182bool write_should_succeed(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo, bool isGPU) {
183 if (!SkImageInfoValidConversion(dstInfo, srcInfo)) {
184 return false;
185 }
186 if (!isGPU) {
187 return true;
188 }
189 // The GPU backend supports writing unpremul data to a premul dst but not vice versa.
190 if (srcInfo.alphaType() == kPremul_SkAlphaType &&
191 dstInfo.alphaType() == kUnpremul_SkAlphaType) {
192 return false;
193 }
194 if (!SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
195 SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
196 return false;
197 }
198 // The source has no alpha value and the dst is only alpha
199 if (SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
200 SkColorTypeIsAlphaOnly(dstInfo.colorType())) {
201 return false;
202 }
203 return true;
204}
205
206static bool check_write(skiatest::Reporter* reporter, SkSurface* surf, SkAlphaType surfaceAlphaType,
207 const SkBitmap& bitmap, int writeX, int writeY) {
reed@google.com7111d462014-03-25 16:20:24 +0000208 size_t canvasRowBytes;
209 const uint32_t* canvasPixels;
reed@google.com11211702014-03-25 12:00:30 +0000210
reed@google.com7111d462014-03-25 16:20:24 +0000211 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
212 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
213 // readPixels for the client.
214 SkBitmap secretDevBitmap;
Mike Reedf1942192017-07-21 14:24:29 -0400215 secretDevBitmap.allocN32Pixels(surf->width(), surf->height());
216 if (!surf->readPixels(secretDevBitmap, 0, 0)) {
Brian Salomon71d9d842016-11-03 13:42:00 -0400217 return false;
218 }
reed52d9ac62014-06-30 09:05:34 -0700219
reed@google.com7111d462014-03-25 16:20:24 +0000220 canvasRowBytes = secretDevBitmap.rowBytes();
221 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
222
halcanary96fcdcc2015-08-27 07:41:13 -0700223 if (nullptr == canvasPixels) {
reed@google.com7111d462014-03-25 16:20:24 +0000224 return false;
225 }
226
Mike Reedf1942192017-07-21 14:24:29 -0400227 if (surf->width() != DEV_W || surf->height() != DEV_H) {
reed@google.com7111d462014-03-25 16:20:24 +0000228 return false;
229 }
230
231 const SkImageInfo bmInfo = bitmap.info();
232
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000233 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000234 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000235 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000236 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000237 if (writeRect.contains(cx, cy)) {
238 int bx = cx - writeX;
239 int by = cy - writeY;
bsalomonf0674512015-07-28 13:26:15 -0700240 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
reed@google.com7111d462014-03-25 16:20:24 +0000241 bmInfo.colorType(), bmInfo.alphaType());
242 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
243 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
244 bmpColor8888);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400245 if (bmInfo.alphaType() == kOpaque_SkAlphaType ||
246 surfaceAlphaType == kOpaque_SkAlphaType) {
247 bmpPMColor |= 0xFF000000;
248 }
bsalomonf0674512015-07-28 13:26:15 -0700249 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
250 ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
251 "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000252 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000253 }
254 } else {
bsalomonf0674512015-07-28 13:26:15 -0700255 SkPMColor testColor = get_canvas_color(cx, cy);
256 if (canvasPixel != testColor) {
257 ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
258 " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000259 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000260 }
261 }
262 }
263 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000264 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000265 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
266 bool check;
267 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
268 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000269 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000270 }
271 }
272 }
reed@google.com7111d462014-03-25 16:20:24 +0000273 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000274 }
275
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000276 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000277}
278
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000279#include "SkMallocPixelRef.h"
280
281// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
282// a custom pixelRef (which also has to specify its rowBytes), so we have to be
283// sure that the two rowBytes match (and the infos match).
284//
bsalomonf0674512015-07-28 13:26:15 -0700285static bool alloc_row_bytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000286 if (!bm->setInfo(info, rowBytes)) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000287 return false;
288 }
Mike Reed086a4272017-07-18 10:53:11 -0400289 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
Hal Canary1b3387b2016-12-12 13:48:12 -0500290 bm->setPixelRef(std::move(pr), 0, 0);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000291 return true;
292}
293
reed52d9ac62014-06-30 09:05:34 -0700294static void free_pixels(void* pixels, void* ctx) {
295 sk_free(pixels);
296}
297
reed52d9ac62014-06-30 09:05:34 -0700298static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
reed@google.com7111d462014-03-25 16:20:24 +0000299 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
300 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
bsalomonf0674512015-07-28 13:26:15 -0700301 if (!alloc_row_bytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302 return false;
303 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000304 for (int y = 0; y < h; ++y) {
305 for (int x = 0; x < w; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700306 *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000307 }
308 }
309 return true;
310}
311
Mike Reed4c790bd2018-02-08 14:10:40 -0500312static void call_writepixels(SkSurface* surface) {
reed4af35f32014-06-27 17:47:49 -0700313 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
314 SkPMColor pixel = 0;
Mike Reed4c790bd2018-02-08 14:10:40 -0500315 surface->writePixels({info, &pixel, sizeof(SkPMColor)}, 0, 0);
reed4af35f32014-06-27 17:47:49 -0700316}
317
kkinnunen15302832015-12-01 04:35:26 -0800318DEF_TEST(WritePixelsSurfaceGenID, reporter) {
reed4af35f32014-06-27 17:47:49 -0700319 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -0700320 auto surface(SkSurface::MakeRaster(info));
reed4af35f32014-06-27 17:47:49 -0700321 uint32_t genID1 = surface->generationID();
Mike Reed4c790bd2018-02-08 14:10:40 -0500322 call_writepixels(surface.get());
reed4af35f32014-06-27 17:47:49 -0700323 uint32_t genID2 = surface->generationID();
324 REPORTER_ASSERT(reporter, genID1 != genID2);
325}
326
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400327static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface,
328 const SkImageInfo& surfaceInfo) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000329 const SkIRect testRects[] = {
330 // entire thing
331 DEV_RECT,
332 // larger on all sides
333 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
334 // fully contained
335 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
336 // outside top left
337 SkIRect::MakeLTRB(-10, -10, -1, -1),
338 // touching top left corner
339 SkIRect::MakeLTRB(-10, -10, 0, 0),
340 // overlapping top left corner
341 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
342 // overlapping top left and top right corners
343 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
344 // touching entire top edge
345 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
346 // overlapping top right corner
347 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
348 // contained in x, overlapping top edge
349 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
350 // outside top right corner
351 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
352 // touching top right corner
353 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
354 // overlapping top left and bottom left corners
355 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
356 // touching entire left edge
357 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
358 // overlapping bottom left corner
359 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
360 // contained in y, overlapping left edge
361 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
362 // outside bottom left corner
363 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
364 // touching bottom left corner
365 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
366 // overlapping bottom left and bottom right corners
367 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
368 // touching entire left edge
369 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
370 // overlapping bottom right corner
371 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
372 // overlapping top right and bottom right corners
373 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
374 };
375
Mike Reedf1942192017-07-21 14:24:29 -0400376 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000377
kkinnunen15302832015-12-01 04:35:26 -0800378 static const struct {
379 SkColorType fColorType;
380 SkAlphaType fAlphaType;
381 } gSrcConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400382 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
383 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
384 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
385 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
386 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800387 };
388 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
389 const SkIRect& rect = testRects[r];
390 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
391 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
392 const SkColorType ct = gSrcConfigs[c].fColorType;
393 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000394
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400395 bool isGPU = SkToBool(surface->getCanvas()->getGrContext());
Mike Reed4c790bd2018-02-08 14:10:40 -0500396 fill_surface(surface);
kkinnunen15302832015-12-01 04:35:26 -0800397 SkBitmap bmp;
398 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
399 rect.height(), SkToBool(tightBmp)));
400 uint32_t idBefore = surface->generationID();
reed@google.com7111d462014-03-25 16:20:24 +0000401
kkinnunen15302832015-12-01 04:35:26 -0800402 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
Mike Reed4c790bd2018-02-08 14:10:40 -0500403 surface->writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000404
kkinnunen15302832015-12-01 04:35:26 -0800405 uint32_t idAfter = surface->generationID();
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400406 REPORTER_ASSERT(reporter, check_write(reporter, surface, surfaceInfo.alphaType(),
407 bmp, rect.fLeft, rect.fTop));
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000408
kkinnunen15302832015-12-01 04:35:26 -0800409 // we should change the genID iff pixels were actually written.
Mike Reedf1942192017-07-21 14:24:29 -0400410 SkIRect canvasRect = SkIRect::MakeSize(canvas->getBaseLayerSize());
kkinnunen15302832015-12-01 04:35:26 -0800411 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
412 bmp.width(), bmp.height());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400413 bool expectSuccess = SkIRect::Intersects(canvasRect, writeRect) &&
414 write_should_succeed(surfaceInfo, bmp.info(), isGPU);
415 REPORTER_ASSERT(reporter, expectSuccess == (idBefore != idAfter));
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000416 }
417 }
418 }
419}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400420
kkinnunen15302832015-12-01 04:35:26 -0800421DEF_TEST(WritePixels, reporter) {
422 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
423 for (auto& tightRowBytes : { true, false }) {
424 const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
Mike Reedf0ffb892017-10-03 14:47:21 -0400425 const size_t size = info.computeByteSize(rowBytes);
kkinnunen15302832015-12-01 04:35:26 -0800426 void* pixels = sk_malloc_throw(size);
427 // if rowBytes isn't tight then set the padding to a known value
428 if (!tightRowBytes) {
429 memset(pixels, DEV_PAD, size);
430 }
reede8f30622016-03-23 18:59:25 -0700431 auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
432 free_pixels, nullptr));
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400433 test_write_pixels(reporter, surface.get(), info);
kkinnunen15302832015-12-01 04:35:26 -0800434 }
435}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400436
kkinnunen15302832015-12-01 04:35:26 -0800437#if SK_SUPPORT_GPU
Brian Salomon3d86a192018-02-27 16:46:11 -0500438static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context, int sampleCnt) {
robertphillips7e922762016-07-26 11:38:17 -0700439 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
Brian Salomon3d86a192018-02-27 16:46:11 -0500440 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
441 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(context,
442 SkBudgeted::kNo, ii, sampleCnt,
443 origin, nullptr));
444 if (surface) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400445 test_write_pixels(reporter, surface.get(), ii);
Brian Salomon3d86a192018-02-27 16:46:11 -0500446 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500447 }
448}
449
450DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) {
451 test_write_pixels(reporter, ctxInfo.grContext(), 1);
452}
453
454DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsMSAA_Gpu, reporter, ctxInfo) {
455 test_write_pixels(reporter, ctxInfo.grContext(), 1);
456}
457
458static void test_write_pixels_non_texture(skiatest::Reporter* reporter, GrContext* context,
459 int sampleCnt) {
460 GrGpu* gpu = context->contextPriv().getGpu();
robertphillips7e922762016-07-26 11:38:17 -0700461
kkinnunen15302832015-12-01 04:35:26 -0800462 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Brian Salomon3d86a192018-02-27 16:46:11 -0500463 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
464 nullptr, DEV_W, DEV_H, kSkia8888_GrPixelConfig, true, GrMipMapped::kNo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400465 if (!backendTex.isValid()) {
466 continue;
467 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500468 SkColorType colorType = kN32_SkColorType;
469 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTextureAsRenderTarget(
470 context, backendTex, origin, sampleCnt, colorType, nullptr, nullptr));
471 if (surface) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400472 auto ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
473 test_write_pixels(reporter, surface.get(), ii);
Brian Osman33910292017-04-18 14:38:53 -0400474 }
Brian Salomon26102cb2018-03-09 09:33:19 -0500475 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Osman33910292017-04-18 14:38:53 -0400476 }
477}
478
479DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo) {
Brian Salomon3d86a192018-02-27 16:46:11 -0500480 test_write_pixels_non_texture(reporter, ctxInfo.grContext(), 1);
481}
Brian Osman33910292017-04-18 14:38:53 -0400482
Brian Salomon3d86a192018-02-27 16:46:11 -0500483DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTextureMSAA_Gpu, reporter, ctxInfo) {
484 test_write_pixels_non_texture(reporter, ctxInfo.grContext(), 4);
kkinnunen15302832015-12-01 04:35:26 -0800485}
Robert Phillips7bbbf622017-10-17 07:36:59 -0400486
487static sk_sp<SkSurface> create_surf(GrContext* context, int width, int height) {
488 const SkImageInfo ii = SkImageInfo::Make(width, height,
489 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
490
491 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
492 surf->flush();
493 return surf;
494}
495
496static sk_sp<SkImage> upload(const sk_sp<SkSurface>& surf, SkColor color) {
497 const SkImageInfo smII = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
498 SkBitmap bm;
499 bm.allocPixels(smII);
500 bm.eraseColor(color);
501
Mike Reed4c790bd2018-02-08 14:10:40 -0500502 surf->writePixels(bm, 0, 0);
Robert Phillips7bbbf622017-10-17 07:36:59 -0400503
504 return surf->makeImageSnapshot();
505}
506
507// This is tests whether the first writePixels is completed before the
508// second writePixels takes effect (i.e., that writePixels correctly flushes
509// in between uses of the shared backing resource).
510DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsPendingIO, reporter, ctxInfo) {
511 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500512 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400513
514 static const int kFullSize = 62;
515 static const int kHalfSize = 31;
516
517 static const uint32_t kLeftColor = 0xFF222222;
518 static const uint32_t kRightColor = 0xFFAAAAAA;
519
520 const SkImageInfo fullII = SkImageInfo::Make(kFullSize, kFullSize,
521 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
522 const SkImageInfo halfII = SkImageInfo::Make(kHalfSize, kFullSize,
523 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
524
525 sk_sp<SkSurface> dest = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, fullII);
526
527 {
528 // Seed the resource cached with a scratch texture that will be
529 // reused by writeSurfacePixels
530 GrSurfaceDesc desc;
531 desc.fFlags = kNone_GrSurfaceFlags;
532 desc.fWidth = 32;
533 desc.fHeight = 64;
534 desc.fConfig = kRGBA_8888_GrPixelConfig;
535
Brian Salomon2a4f9832018-03-03 22:43:43 -0500536 sk_sp<GrTextureProxy> temp = proxyProvider->createProxy(
537 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Robert Phillips6be756b2018-01-16 15:07:54 -0500538 temp->instantiate(context->contextPriv().resourceProvider());
Robert Phillips7bbbf622017-10-17 07:36:59 -0400539 }
540
541 // Create the surfaces and flush them to ensure there is no lingering pendingIO
542 sk_sp<SkSurface> leftSurf = create_surf(context, kHalfSize, kFullSize);
543 sk_sp<SkSurface> rightSurf = create_surf(context, kHalfSize, kFullSize);
544
545 sk_sp<SkImage> leftImg = upload(std::move(leftSurf), kLeftColor);
546 dest->getCanvas()->drawImage(std::move(leftImg), 0, 0);
547
548 sk_sp<SkImage> rightImg = upload(std::move(rightSurf), kRightColor);
549 dest->getCanvas()->drawImage(std::move(rightImg), kHalfSize, 0);
550
551 SkBitmap bm;
552 bm.allocPixels(fullII);
553 SkAssertResult(dest->readPixels(bm, 0, 0));
554
555 bool isCorrect = true;
556 for (int y = 0; isCorrect && y < 16; ++y) {
557 const uint32_t* sl = bm.getAddr32(0, y);
558
559 for (int x = 0; x < 16; ++x) {
560 if (kLeftColor != sl[x]) {
561 isCorrect = false;
562 break;
563 }
564 }
565 for (int x = kHalfSize; x < kHalfSize+16; ++x) {
566 if (kRightColor != sl[x]) {
567 isCorrect = false;
568 break;
569 }
570 }
571 }
572
573 REPORTER_ASSERT(reporter, isCorrect);
574}
575
576
kkinnunen15302832015-12-01 04:35:26 -0800577#endif