blob: 77038d4c8e556ebb4d5ca3db4f61c0e67610175d [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"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009#include "SkColorPriv.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
kkinnunen15302832015-12-01 04:35:26 -080016#include "GrContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000018
kkinnunen15302832015-12-01 04:35:26 -080019#include <initializer_list>
20
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000021static const int DEV_W = 100, DEV_H = 100;
22static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000023static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000024 DEV_H * SK_Scalar1);
25static const U8CPU DEV_PAD = 0xee;
26
bsalomonf0674512015-07-28 13:26:15 -070027static SkPMColor get_canvas_color(int x, int y) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000028 SkASSERT(x >= 0 && x < DEV_W);
29 SkASSERT(y >= 0 && y < DEV_H);
30
31 U8CPU r = x;
32 U8CPU g = y;
33 U8CPU b = 0xc;
34
bsalomon@google.com31648eb2011-11-23 15:01:08 +000035 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000036 switch ((x+y) % 5) {
37 case 0:
38 a = 0xff;
39 break;
40 case 1:
41 a = 0x80;
42 break;
43 case 2:
44 a = 0xCC;
45 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000046 case 3:
47 a = 0x00;
48 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000049 case 4:
50 a = 0x01;
51 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000052 }
53 return SkPremultiplyARGBInline(a, r, g, b);
54}
55
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000056// assumes any premu/.unpremul has been applied
bsalomonf0674512015-07-28 13:26:15 -070057static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000058 uint32_t r32;
59 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
reed@google.com7111d462014-03-25 16:20:24 +000060 switch (ct) {
61 case kBGRA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000062 result[0] = b;
63 result[1] = g;
64 result[2] = r;
65 result[3] = a;
66 break;
reed@google.com7111d462014-03-25 16:20:24 +000067 case kRGBA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000068 result[0] = r;
69 result[1] = g;
70 result[2] = b;
71 result[3] = a;
72 break;
73 default:
74 SkASSERT(0);
75 return 0;
76 }
77 return r32;
78}
79
bsalomonf0674512015-07-28 13:26:15 -070080static uint32_t get_bitmap_color(int x, int y, int w, SkColorType ct, SkAlphaType at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000081 int n = y * w + x;
82 U8CPU b = n & 0xff;
83 U8CPU g = (n >> 8) & 0xff;
84 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000085 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000086 switch ((x+y) % 5) {
87 case 4:
88 a = 0xff;
89 break;
90 case 3:
91 a = 0x80;
92 break;
93 case 2:
94 a = 0xCC;
95 break;
96 case 1:
97 a = 0x01;
98 break;
99 case 0:
100 a = 0x00;
101 break;
102 }
reed@google.com7111d462014-03-25 16:20:24 +0000103 if (kPremul_SkAlphaType == at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000104 r = SkMulDiv255Ceiling(r, a);
105 g = SkMulDiv255Ceiling(g, a);
106 b = SkMulDiv255Ceiling(b, a);
107 }
bsalomonf0674512015-07-28 13:26:15 -0700108 return pack_color_type(ct, a, r, g , b);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000109}
110
bsalomonf0674512015-07-28 13:26:15 -0700111static void fill_canvas(SkCanvas* canvas) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000112 SkBitmap bmp;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000113 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700114 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000115 for (int y = 0; y < DEV_H; ++y) {
116 for (int x = 0; x < DEV_W; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700117 *bmp.getAddr32(x, y) = get_canvas_color(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000118 }
119 }
120 }
121 canvas->save();
122 canvas->setMatrix(SkMatrix::I());
reed73603f32016-09-20 08:42:38 -0700123 canvas->clipRect(DEV_RECT_S, SkCanvas::kReplace_Op);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000124 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700125 paint.setBlendMode(SkBlendMode::kSrc);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000126 canvas->drawBitmap(bmp, 0, 0, &paint);
127 canvas->restore();
128}
129
reed@google.com7111d462014-03-25 16:20:24 +0000130/**
131 * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
132 * Thus this routine doesn't need to know the exact colortype
133 */
134static uint32_t premul(uint32_t color) {
135 unsigned a = SkGetPackedA32(color);
136 // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
137 unsigned c0 = SkGetPackedR32(color);
138 unsigned c1 = SkGetPackedG32(color);
139 unsigned c2 = SkGetPackedB32(color);
140 c0 = SkMulDiv255Ceiling(c0, a);
141 c1 = SkMulDiv255Ceiling(c1, a);
142 c2 = SkMulDiv255Ceiling(c2, a);
143 return SkPackARGB32NoCheck(a, c0, c1, c2);
144}
145
146static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
147 if (kUnpremul_SkAlphaType == at) {
148 color = premul(color);
149 }
150 switch (ct) {
151 case kRGBA_8888_SkColorType:
152 color = SkSwizzle_RGBA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000153 break;
reed@google.com7111d462014-03-25 16:20:24 +0000154 case kBGRA_8888_SkColorType:
155 color = SkSwizzle_BGRA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000156 break;
157 default:
reed@google.com7111d462014-03-25 16:20:24 +0000158 SkASSERT(0);
159 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000160 }
reed@google.com7111d462014-03-25 16:20:24 +0000161 return color;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000162}
163
bsalomonf0674512015-07-28 13:26:15 -0700164static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000165 if (!didPremulConversion) {
166 return a == b;
167 }
168 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
169 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
170 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
171 int32_t aB = SkGetPackedB32(a);
172
173 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
174 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
175 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
176 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
177
178 return aA == bA &&
179 SkAbs32(aR - bR) <= 1 &&
180 SkAbs32(aG - bG) <= 1 &&
181 SkAbs32(aB - bB) <= 1;
182}
183
reed52d9ac62014-06-30 09:05:34 -0700184static bool check_write(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap,
reed@google.com7111d462014-03-25 16:20:24 +0000185 int writeX, int writeY) {
reed52d9ac62014-06-30 09:05:34 -0700186 const SkImageInfo canvasInfo = canvas->imageInfo();
reed@google.com7111d462014-03-25 16:20:24 +0000187 size_t canvasRowBytes;
188 const uint32_t* canvasPixels;
reed@google.com11211702014-03-25 12:00:30 +0000189
reed@google.com7111d462014-03-25 16:20:24 +0000190 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
191 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
192 // readPixels for the client.
193 SkBitmap secretDevBitmap;
halcanaryf622a6c2014-10-24 12:54:53 -0700194 canvas->readPixels(canvasInfo.bounds(), &secretDevBitmap);
reed52d9ac62014-06-30 09:05:34 -0700195
reed@google.com7111d462014-03-25 16:20:24 +0000196 SkAutoLockPixels alp(secretDevBitmap);
reed@google.com7111d462014-03-25 16:20:24 +0000197 canvasRowBytes = secretDevBitmap.rowBytes();
198 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
199
halcanary96fcdcc2015-08-27 07:41:13 -0700200 if (nullptr == canvasPixels) {
reed@google.com7111d462014-03-25 16:20:24 +0000201 return false;
202 }
203
204 if (canvasInfo.width() != DEV_W ||
205 canvasInfo.height() != DEV_H ||
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000206 canvasInfo.colorType() != kN32_SkColorType) {
reed@google.com7111d462014-03-25 16:20:24 +0000207 return false;
208 }
209
210 const SkImageInfo bmInfo = bitmap.info();
211
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000212 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000213 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000214 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000215 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000216 if (writeRect.contains(cx, cy)) {
217 int bx = cx - writeX;
218 int by = cy - writeY;
bsalomonf0674512015-07-28 13:26:15 -0700219 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
reed@google.com7111d462014-03-25 16:20:24 +0000220 bmInfo.colorType(), bmInfo.alphaType());
221 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
222 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
223 bmpColor8888);
bsalomonf0674512015-07-28 13:26:15 -0700224 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
225 ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
226 "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000227 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000228 }
229 } else {
bsalomonf0674512015-07-28 13:26:15 -0700230 SkPMColor testColor = get_canvas_color(cx, cy);
231 if (canvasPixel != testColor) {
232 ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
233 " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000234 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000235 }
236 }
237 }
238 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000239 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000240 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
241 bool check;
242 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
243 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000244 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000245 }
246 }
247 }
reed@google.com7111d462014-03-25 16:20:24 +0000248 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000249 }
250
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000251 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000252}
253
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000254#include "SkMallocPixelRef.h"
255
256// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
257// a custom pixelRef (which also has to specify its rowBytes), so we have to be
258// sure that the two rowBytes match (and the infos match).
259//
bsalomonf0674512015-07-28 13:26:15 -0700260static bool alloc_row_bytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000261 if (!bm->setInfo(info, rowBytes)) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000262 return false;
263 }
halcanary96fcdcc2015-08-27 07:41:13 -0700264 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, nullptr);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000265 bm->setPixelRef(pr)->unref();
266 return true;
267}
268
reed52d9ac62014-06-30 09:05:34 -0700269static void free_pixels(void* pixels, void* ctx) {
270 sk_free(pixels);
271}
272
reed52d9ac62014-06-30 09:05:34 -0700273static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
reed@google.com7111d462014-03-25 16:20:24 +0000274 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
275 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
bsalomonf0674512015-07-28 13:26:15 -0700276 if (!alloc_row_bytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000277 return false;
278 }
reed@google.com7111d462014-03-25 16:20:24 +0000279 SkAutoLockPixels alp(*bm);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000280 for (int y = 0; y < h; ++y) {
281 for (int x = 0; x < w; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700282 *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000283 }
284 }
285 return true;
286}
287
reed4af35f32014-06-27 17:47:49 -0700288static void call_writepixels(SkCanvas* canvas) {
289 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
290 SkPMColor pixel = 0;
291 canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0);
292}
293
kkinnunen15302832015-12-01 04:35:26 -0800294DEF_TEST(WritePixelsSurfaceGenID, reporter) {
reed4af35f32014-06-27 17:47:49 -0700295 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -0700296 auto surface(SkSurface::MakeRaster(info));
reed4af35f32014-06-27 17:47:49 -0700297 uint32_t genID1 = surface->generationID();
298 call_writepixels(surface->getCanvas());
299 uint32_t genID2 = surface->generationID();
300 REPORTER_ASSERT(reporter, genID1 != genID2);
301}
302
kkinnunen15302832015-12-01 04:35:26 -0800303static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000304 const SkIRect testRects[] = {
305 // entire thing
306 DEV_RECT,
307 // larger on all sides
308 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
309 // fully contained
310 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
311 // outside top left
312 SkIRect::MakeLTRB(-10, -10, -1, -1),
313 // touching top left corner
314 SkIRect::MakeLTRB(-10, -10, 0, 0),
315 // overlapping top left corner
316 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
317 // overlapping top left and top right corners
318 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
319 // touching entire top edge
320 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
321 // overlapping top right corner
322 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
323 // contained in x, overlapping top edge
324 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
325 // outside top right corner
326 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
327 // touching top right corner
328 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
329 // overlapping top left and bottom left corners
330 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
331 // touching entire left edge
332 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
333 // overlapping bottom left corner
334 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
335 // contained in y, overlapping left edge
336 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
337 // outside bottom left corner
338 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
339 // touching bottom left corner
340 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
341 // overlapping bottom left and bottom right corners
342 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
343 // touching entire left edge
344 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
345 // overlapping bottom right corner
346 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
347 // overlapping top right and bottom right corners
348 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
349 };
350
kkinnunen15302832015-12-01 04:35:26 -0800351 SkCanvas& canvas = *surface->getCanvas();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000352
kkinnunen15302832015-12-01 04:35:26 -0800353 static const struct {
354 SkColorType fColorType;
355 SkAlphaType fAlphaType;
356 } gSrcConfigs[] = {
357 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
358 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
359 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
360 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
361 };
362 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
363 const SkIRect& rect = testRects[r];
364 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
365 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
366 const SkColorType ct = gSrcConfigs[c].fColorType;
367 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000368
kkinnunen15302832015-12-01 04:35:26 -0800369 fill_canvas(&canvas);
370 SkBitmap bmp;
371 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
372 rect.height(), SkToBool(tightBmp)));
373 uint32_t idBefore = surface->generationID();
reed@google.com7111d462014-03-25 16:20:24 +0000374
kkinnunen15302832015-12-01 04:35:26 -0800375 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
376 canvas.writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000377
kkinnunen15302832015-12-01 04:35:26 -0800378 uint32_t idAfter = surface->generationID();
379 REPORTER_ASSERT(reporter, check_write(reporter, &canvas, bmp,
380 rect.fLeft, rect.fTop));
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000381
kkinnunen15302832015-12-01 04:35:26 -0800382 // we should change the genID iff pixels were actually written.
383 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
384 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
385 bmp.width(), bmp.height());
386 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
387 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000388 }
389 }
390 }
391}
kkinnunen15302832015-12-01 04:35:26 -0800392DEF_TEST(WritePixels, reporter) {
393 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
394 for (auto& tightRowBytes : { true, false }) {
395 const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
396 const size_t size = info.getSafeSize(rowBytes);
397 void* pixels = sk_malloc_throw(size);
398 // if rowBytes isn't tight then set the padding to a known value
399 if (!tightRowBytes) {
400 memset(pixels, DEV_PAD, size);
401 }
reede8f30622016-03-23 18:59:25 -0700402 auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
403 free_pixels, nullptr));
404 test_write_pixels(reporter, surface.get());
kkinnunen15302832015-12-01 04:35:26 -0800405 }
406}
407#if SK_SUPPORT_GPU
egdaniel4583ec52016-06-27 12:57:00 -0700408DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) {
robertphillips7e922762016-07-26 11:38:17 -0700409 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
410
kkinnunen15302832015-12-01 04:35:26 -0800411 for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
robertphillips7e922762016-07-26 11:38:17 -0700412 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
413 ii, 0, origin, nullptr));
reede8f30622016-03-23 18:59:25 -0700414 test_write_pixels(reporter, surface.get());
kkinnunen15302832015-12-01 04:35:26 -0800415 }
416}
417#endif