blob: b3da67bb96ba5a63b803bebe7fc1fc80ab280e58 [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"
reed@google.com4b163ed2012-08-07 21:35:13 +000010#include "SkMathPriv.h"
reed4af35f32014-06-27 17:47:49 -070011#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000013#include "sk_tool_utils.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000014
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015#if SK_SUPPORT_GPU
Brian Salomond17f6582017-07-19 18:28:58 -040016#include "GrBackendSurface.h"
kkinnunen15302832015-12-01 04:35:26 -080017#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050018#include "GrContextPriv.h"
Brian Osman33910292017-04-18 14:38:53 -040019#include "GrGpu.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050020#include "GrProxyProvider.h"
Brian Salomond17f6582017-07-19 18:28:58 -040021#include "GrTest.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000022#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000023
kkinnunen15302832015-12-01 04:35:26 -080024#include <initializer_list>
25
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000026static const int DEV_W = 100, DEV_H = 100;
27static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000028static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000029 DEV_H * SK_Scalar1);
30static const U8CPU DEV_PAD = 0xee;
31
bsalomonf0674512015-07-28 13:26:15 -070032static SkPMColor get_canvas_color(int x, int y) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000033 SkASSERT(x >= 0 && x < DEV_W);
34 SkASSERT(y >= 0 && y < DEV_H);
35
36 U8CPU r = x;
37 U8CPU g = y;
38 U8CPU b = 0xc;
39
bsalomon@google.com31648eb2011-11-23 15:01:08 +000040 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000041 switch ((x+y) % 5) {
42 case 0:
43 a = 0xff;
44 break;
45 case 1:
46 a = 0x80;
47 break;
48 case 2:
49 a = 0xCC;
50 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000051 case 3:
52 a = 0x00;
53 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000054 case 4:
55 a = 0x01;
56 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000057 }
58 return SkPremultiplyARGBInline(a, r, g, b);
59}
60
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000061// assumes any premu/.unpremul has been applied
bsalomonf0674512015-07-28 13:26:15 -070062static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000063 uint32_t r32;
64 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
reed@google.com7111d462014-03-25 16:20:24 +000065 switch (ct) {
66 case kBGRA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000067 result[0] = b;
68 result[1] = g;
69 result[2] = r;
70 result[3] = a;
71 break;
reed@google.com7111d462014-03-25 16:20:24 +000072 case kRGBA_8888_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
bsalomonf0674512015-07-28 13:26:15 -0700116static void fill_canvas(SkCanvas* canvas) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000117 SkBitmap bmp;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000118 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700119 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000120 for (int y = 0; y < DEV_H; ++y) {
121 for (int x = 0; x < DEV_W; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700122 *bmp.getAddr32(x, y) = get_canvas_color(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000123 }
124 }
125 }
126 canvas->save();
127 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500128 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000129 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700130 paint.setBlendMode(SkBlendMode::kSrc);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000131 canvas->drawBitmap(bmp, 0, 0, &paint);
132 canvas->restore();
133}
134
reed@google.com7111d462014-03-25 16:20:24 +0000135/**
136 * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
137 * Thus this routine doesn't need to know the exact colortype
138 */
139static uint32_t premul(uint32_t color) {
140 unsigned a = SkGetPackedA32(color);
141 // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
142 unsigned c0 = SkGetPackedR32(color);
143 unsigned c1 = SkGetPackedG32(color);
144 unsigned c2 = SkGetPackedB32(color);
145 c0 = SkMulDiv255Ceiling(c0, a);
146 c1 = SkMulDiv255Ceiling(c1, a);
147 c2 = SkMulDiv255Ceiling(c2, a);
148 return SkPackARGB32NoCheck(a, c0, c1, c2);
149}
150
151static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
152 if (kUnpremul_SkAlphaType == at) {
153 color = premul(color);
154 }
155 switch (ct) {
156 case kRGBA_8888_SkColorType:
157 color = SkSwizzle_RGBA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000158 break;
reed@google.com7111d462014-03-25 16:20:24 +0000159 case kBGRA_8888_SkColorType:
160 color = SkSwizzle_BGRA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000161 break;
162 default:
reed@google.com7111d462014-03-25 16:20:24 +0000163 SkASSERT(0);
164 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000165 }
reed@google.com7111d462014-03-25 16:20:24 +0000166 return color;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000167}
168
bsalomonf0674512015-07-28 13:26:15 -0700169static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000170 if (!didPremulConversion) {
171 return a == b;
172 }
173 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
174 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
175 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
176 int32_t aB = SkGetPackedB32(a);
177
178 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
179 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
180 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
181 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
182
183 return aA == bA &&
184 SkAbs32(aR - bR) <= 1 &&
185 SkAbs32(aG - bG) <= 1 &&
186 SkAbs32(aB - bB) <= 1;
187}
188
Mike Reedf1942192017-07-21 14:24:29 -0400189static bool check_write(skiatest::Reporter* reporter, SkSurface* surf, const SkBitmap& bitmap,
reed@google.com7111d462014-03-25 16:20:24 +0000190 int writeX, int writeY) {
reed@google.com7111d462014-03-25 16:20:24 +0000191 size_t canvasRowBytes;
192 const uint32_t* canvasPixels;
reed@google.com11211702014-03-25 12:00:30 +0000193
reed@google.com7111d462014-03-25 16:20:24 +0000194 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
195 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
196 // readPixels for the client.
197 SkBitmap secretDevBitmap;
Mike Reedf1942192017-07-21 14:24:29 -0400198 secretDevBitmap.allocN32Pixels(surf->width(), surf->height());
199 if (!surf->readPixels(secretDevBitmap, 0, 0)) {
Brian Salomon71d9d842016-11-03 13:42:00 -0400200 return false;
201 }
reed52d9ac62014-06-30 09:05:34 -0700202
reed@google.com7111d462014-03-25 16:20:24 +0000203 canvasRowBytes = secretDevBitmap.rowBytes();
204 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
205
halcanary96fcdcc2015-08-27 07:41:13 -0700206 if (nullptr == canvasPixels) {
reed@google.com7111d462014-03-25 16:20:24 +0000207 return false;
208 }
209
Mike Reedf1942192017-07-21 14:24:29 -0400210 if (surf->width() != DEV_W || surf->height() != DEV_H) {
reed@google.com7111d462014-03-25 16:20:24 +0000211 return false;
212 }
213
214 const SkImageInfo bmInfo = bitmap.info();
215
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000216 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000217 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000218 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000219 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000220 if (writeRect.contains(cx, cy)) {
221 int bx = cx - writeX;
222 int by = cy - writeY;
bsalomonf0674512015-07-28 13:26:15 -0700223 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
reed@google.com7111d462014-03-25 16:20:24 +0000224 bmInfo.colorType(), bmInfo.alphaType());
225 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
226 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
227 bmpColor8888);
bsalomonf0674512015-07-28 13:26:15 -0700228 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
229 ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
230 "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000231 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000232 }
233 } else {
bsalomonf0674512015-07-28 13:26:15 -0700234 SkPMColor testColor = get_canvas_color(cx, cy);
235 if (canvasPixel != testColor) {
236 ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
237 " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000238 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000239 }
240 }
241 }
242 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000243 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000244 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
245 bool check;
246 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
247 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000248 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000249 }
250 }
251 }
reed@google.com7111d462014-03-25 16:20:24 +0000252 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000253 }
254
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000255 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000256}
257
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000258#include "SkMallocPixelRef.h"
259
260// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
261// a custom pixelRef (which also has to specify its rowBytes), so we have to be
262// sure that the two rowBytes match (and the infos match).
263//
bsalomonf0674512015-07-28 13:26:15 -0700264static bool alloc_row_bytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000265 if (!bm->setInfo(info, rowBytes)) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000266 return false;
267 }
Mike Reed086a4272017-07-18 10:53:11 -0400268 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
Hal Canary1b3387b2016-12-12 13:48:12 -0500269 bm->setPixelRef(std::move(pr), 0, 0);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000270 return true;
271}
272
reed52d9ac62014-06-30 09:05:34 -0700273static void free_pixels(void* pixels, void* ctx) {
274 sk_free(pixels);
275}
276
reed52d9ac62014-06-30 09:05:34 -0700277static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
reed@google.com7111d462014-03-25 16:20:24 +0000278 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
279 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
bsalomonf0674512015-07-28 13:26:15 -0700280 if (!alloc_row_bytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000281 return false;
282 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000283 for (int y = 0; y < h; ++y) {
284 for (int x = 0; x < w; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700285 *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000286 }
287 }
288 return true;
289}
290
reed4af35f32014-06-27 17:47:49 -0700291static void call_writepixels(SkCanvas* canvas) {
292 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
293 SkPMColor pixel = 0;
294 canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0);
295}
296
kkinnunen15302832015-12-01 04:35:26 -0800297DEF_TEST(WritePixelsSurfaceGenID, reporter) {
reed4af35f32014-06-27 17:47:49 -0700298 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -0700299 auto surface(SkSurface::MakeRaster(info));
reed4af35f32014-06-27 17:47:49 -0700300 uint32_t genID1 = surface->generationID();
301 call_writepixels(surface->getCanvas());
302 uint32_t genID2 = surface->generationID();
303 REPORTER_ASSERT(reporter, genID1 != genID2);
304}
305
kkinnunen15302832015-12-01 04:35:26 -0800306static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000307 const SkIRect testRects[] = {
308 // entire thing
309 DEV_RECT,
310 // larger on all sides
311 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
312 // fully contained
313 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
314 // outside top left
315 SkIRect::MakeLTRB(-10, -10, -1, -1),
316 // touching top left corner
317 SkIRect::MakeLTRB(-10, -10, 0, 0),
318 // overlapping top left corner
319 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
320 // overlapping top left and top right corners
321 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
322 // touching entire top edge
323 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
324 // overlapping top right corner
325 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
326 // contained in x, overlapping top edge
327 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
328 // outside top right corner
329 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
330 // touching top right corner
331 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
332 // overlapping top left and bottom left corners
333 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
334 // touching entire left edge
335 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
336 // overlapping bottom left corner
337 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
338 // contained in y, overlapping left edge
339 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
340 // outside bottom left corner
341 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
342 // touching bottom left corner
343 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
344 // overlapping bottom left and bottom right corners
345 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
346 // touching entire left edge
347 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
348 // overlapping bottom right corner
349 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
350 // overlapping top right and bottom right corners
351 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
352 };
353
Mike Reedf1942192017-07-21 14:24:29 -0400354 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000355
kkinnunen15302832015-12-01 04:35:26 -0800356 static const struct {
357 SkColorType fColorType;
358 SkAlphaType fAlphaType;
359 } gSrcConfigs[] = {
360 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
361 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
362 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
363 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
364 };
365 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
366 const SkIRect& rect = testRects[r];
367 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
368 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
369 const SkColorType ct = gSrcConfigs[c].fColorType;
370 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000371
Mike Reedf1942192017-07-21 14:24:29 -0400372 fill_canvas(canvas);
kkinnunen15302832015-12-01 04:35:26 -0800373 SkBitmap bmp;
374 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
375 rect.height(), SkToBool(tightBmp)));
376 uint32_t idBefore = surface->generationID();
reed@google.com7111d462014-03-25 16:20:24 +0000377
kkinnunen15302832015-12-01 04:35:26 -0800378 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
Mike Reedf1942192017-07-21 14:24:29 -0400379 canvas->writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000380
kkinnunen15302832015-12-01 04:35:26 -0800381 uint32_t idAfter = surface->generationID();
Mike Reedf1942192017-07-21 14:24:29 -0400382 REPORTER_ASSERT(reporter, check_write(reporter, surface, bmp,
kkinnunen15302832015-12-01 04:35:26 -0800383 rect.fLeft, rect.fTop));
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000384
kkinnunen15302832015-12-01 04:35:26 -0800385 // we should change the genID iff pixels were actually written.
Mike Reedf1942192017-07-21 14:24:29 -0400386 SkIRect canvasRect = SkIRect::MakeSize(canvas->getBaseLayerSize());
kkinnunen15302832015-12-01 04:35:26 -0800387 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
388 bmp.width(), bmp.height());
389 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
390 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000391 }
392 }
393 }
394}
kkinnunen15302832015-12-01 04:35:26 -0800395DEF_TEST(WritePixels, reporter) {
396 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
397 for (auto& tightRowBytes : { true, false }) {
398 const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
Mike Reedf0ffb892017-10-03 14:47:21 -0400399 const size_t size = info.computeByteSize(rowBytes);
kkinnunen15302832015-12-01 04:35:26 -0800400 void* pixels = sk_malloc_throw(size);
401 // if rowBytes isn't tight then set the padding to a known value
402 if (!tightRowBytes) {
403 memset(pixels, DEV_PAD, size);
404 }
reede8f30622016-03-23 18:59:25 -0700405 auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
406 free_pixels, nullptr));
407 test_write_pixels(reporter, surface.get());
kkinnunen15302832015-12-01 04:35:26 -0800408 }
409}
410#if SK_SUPPORT_GPU
egdaniel4583ec52016-06-27 12:57:00 -0700411DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) {
robertphillips7e922762016-07-26 11:38:17 -0700412 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
413
kkinnunen15302832015-12-01 04:35:26 -0800414 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
Brian Osman33910292017-04-18 14:38:53 -0400415 for (int sampleCnt : {0, 4}) {
416 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(),
417 SkBudgeted::kNo, ii, sampleCnt,
418 origin, nullptr));
419 if (!surface && sampleCnt > 0) {
420 // Some platforms don't support MSAA
421 continue;
422 }
423 test_write_pixels(reporter, surface.get());
424 }
425 }
426}
427
428DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo) {
429 GrContext* context = ctxInfo.grContext();
430
431 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
432 for (int sampleCnt : {0, 4}) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500433 GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
434 nullptr, DEV_W, DEV_H, kSkia8888_GrPixelConfig, true, GrMipMapped::kNo);
Greg Danielfaa095e2017-12-19 13:15:02 -0500435 SkColorType colorType;
436 if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig) {
437 colorType = kRGBA_8888_SkColorType;
438 } else {
439 colorType = kBGRA_8888_SkColorType;
440 }
Brian Salomond17f6582017-07-19 18:28:58 -0400441 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTextureAsRenderTarget(
Greg Danielfaa095e2017-12-19 13:15:02 -0500442 context, backendTex, origin, sampleCnt, colorType, nullptr, nullptr));
Brian Osman33910292017-04-18 14:38:53 -0400443 if (!surface) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500444 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Brian Osman33910292017-04-18 14:38:53 -0400445 continue;
446 }
447
448 test_write_pixels(reporter, surface.get());
449
450 surface.reset();
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500451 context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
Brian Osman33910292017-04-18 14:38:53 -0400452 }
kkinnunen15302832015-12-01 04:35:26 -0800453 }
454}
Robert Phillips7bbbf622017-10-17 07:36:59 -0400455
456static sk_sp<SkSurface> create_surf(GrContext* context, int width, int height) {
457 const SkImageInfo ii = SkImageInfo::Make(width, height,
458 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
459
460 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
461 surf->flush();
462 return surf;
463}
464
465static sk_sp<SkImage> upload(const sk_sp<SkSurface>& surf, SkColor color) {
466 const SkImageInfo smII = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
467 SkBitmap bm;
468 bm.allocPixels(smII);
469 bm.eraseColor(color);
470
471 surf->getCanvas()->writePixels(bm, 0, 0);
472
473 return surf->makeImageSnapshot();
474}
475
476// This is tests whether the first writePixels is completed before the
477// second writePixels takes effect (i.e., that writePixels correctly flushes
478// in between uses of the shared backing resource).
479DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsPendingIO, reporter, ctxInfo) {
480 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500481 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400482
483 static const int kFullSize = 62;
484 static const int kHalfSize = 31;
485
486 static const uint32_t kLeftColor = 0xFF222222;
487 static const uint32_t kRightColor = 0xFFAAAAAA;
488
489 const SkImageInfo fullII = SkImageInfo::Make(kFullSize, kFullSize,
490 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
491 const SkImageInfo halfII = SkImageInfo::Make(kHalfSize, kFullSize,
492 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
493
494 sk_sp<SkSurface> dest = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, fullII);
495
496 {
497 // Seed the resource cached with a scratch texture that will be
498 // reused by writeSurfacePixels
499 GrSurfaceDesc desc;
500 desc.fFlags = kNone_GrSurfaceFlags;
501 desc.fWidth = 32;
502 desc.fHeight = 64;
503 desc.fConfig = kRGBA_8888_GrPixelConfig;
504
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500505 sk_sp<GrTextureProxy> temp = proxyProvider->createProxy(desc, SkBackingFit::kApprox,
506 SkBudgeted::kYes);
Robert Phillips7bbbf622017-10-17 07:36:59 -0400507 temp->instantiate(context->resourceProvider());
508 }
509
510 // Create the surfaces and flush them to ensure there is no lingering pendingIO
511 sk_sp<SkSurface> leftSurf = create_surf(context, kHalfSize, kFullSize);
512 sk_sp<SkSurface> rightSurf = create_surf(context, kHalfSize, kFullSize);
513
514 sk_sp<SkImage> leftImg = upload(std::move(leftSurf), kLeftColor);
515 dest->getCanvas()->drawImage(std::move(leftImg), 0, 0);
516
517 sk_sp<SkImage> rightImg = upload(std::move(rightSurf), kRightColor);
518 dest->getCanvas()->drawImage(std::move(rightImg), kHalfSize, 0);
519
520 SkBitmap bm;
521 bm.allocPixels(fullII);
522 SkAssertResult(dest->readPixels(bm, 0, 0));
523
524 bool isCorrect = true;
525 for (int y = 0; isCorrect && y < 16; ++y) {
526 const uint32_t* sl = bm.getAddr32(0, y);
527
528 for (int x = 0; x < 16; ++x) {
529 if (kLeftColor != sl[x]) {
530 isCorrect = false;
531 break;
532 }
533 }
534 for (int x = kHalfSize; x < kHalfSize+16; ++x) {
535 if (kRightColor != sl[x]) {
536 isCorrect = false;
537 break;
538 }
539 }
540 }
541
542 REPORTER_ASSERT(reporter, isCorrect);
543}
544
545
kkinnunen15302832015-12-01 04:35:26 -0800546#endif