blob: a06130e36b70a240a97eff1c812cc023d71e4c60 [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"
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000011#include "SkRegion.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
bsalomon@google.com67b915d2013-02-04 16:13:32 +000017#include "GrContextFactory.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000018#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000019#else
20class GrContext;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000021class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000022#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000023
24static const int DEV_W = 100, DEV_H = 100;
25static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000026static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000027 DEV_H * SK_Scalar1);
28static const U8CPU DEV_PAD = 0xee;
29
bsalomonf0674512015-07-28 13:26:15 -070030static SkPMColor get_canvas_color(int x, int y) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000031 SkASSERT(x >= 0 && x < DEV_W);
32 SkASSERT(y >= 0 && y < DEV_H);
33
34 U8CPU r = x;
35 U8CPU g = y;
36 U8CPU b = 0xc;
37
bsalomon@google.com31648eb2011-11-23 15:01:08 +000038 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000039 switch ((x+y) % 5) {
40 case 0:
41 a = 0xff;
42 break;
43 case 1:
44 a = 0x80;
45 break;
46 case 2:
47 a = 0xCC;
48 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000049 case 3:
50 a = 0x00;
51 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000052 case 4:
53 a = 0x01;
54 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000055 }
56 return SkPremultiplyARGBInline(a, r, g, b);
57}
58
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000059// assumes any premu/.unpremul has been applied
bsalomonf0674512015-07-28 13:26:15 -070060static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000061 uint32_t r32;
62 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
reed@google.com7111d462014-03-25 16:20:24 +000063 switch (ct) {
64 case kBGRA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000065 result[0] = b;
66 result[1] = g;
67 result[2] = r;
68 result[3] = a;
69 break;
reed@google.com7111d462014-03-25 16:20:24 +000070 case kRGBA_8888_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
bsalomonf0674512015-07-28 13:26:15 -0700114static void fill_canvas(SkCanvas* canvas) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000115 SkBitmap bmp;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000116 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700117 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000118 for (int y = 0; y < DEV_H; ++y) {
119 for (int x = 0; x < DEV_W; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700120 *bmp.getAddr32(x, y) = get_canvas_color(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000121 }
122 }
123 }
124 canvas->save();
125 canvas->setMatrix(SkMatrix::I());
126 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
127 SkPaint paint;
128 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
129 canvas->drawBitmap(bmp, 0, 0, &paint);
130 canvas->restore();
131}
132
reed@google.com7111d462014-03-25 16:20:24 +0000133/**
134 * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
135 * Thus this routine doesn't need to know the exact colortype
136 */
137static uint32_t premul(uint32_t color) {
138 unsigned a = SkGetPackedA32(color);
139 // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
140 unsigned c0 = SkGetPackedR32(color);
141 unsigned c1 = SkGetPackedG32(color);
142 unsigned c2 = SkGetPackedB32(color);
143 c0 = SkMulDiv255Ceiling(c0, a);
144 c1 = SkMulDiv255Ceiling(c1, a);
145 c2 = SkMulDiv255Ceiling(c2, a);
146 return SkPackARGB32NoCheck(a, c0, c1, c2);
147}
148
149static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
150 if (kUnpremul_SkAlphaType == at) {
151 color = premul(color);
152 }
153 switch (ct) {
154 case kRGBA_8888_SkColorType:
155 color = SkSwizzle_RGBA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000156 break;
reed@google.com7111d462014-03-25 16:20:24 +0000157 case kBGRA_8888_SkColorType:
158 color = SkSwizzle_BGRA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000159 break;
160 default:
reed@google.com7111d462014-03-25 16:20:24 +0000161 SkASSERT(0);
162 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000163 }
reed@google.com7111d462014-03-25 16:20:24 +0000164 return color;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000165}
166
bsalomonf0674512015-07-28 13:26:15 -0700167static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000168 if (!didPremulConversion) {
169 return a == b;
170 }
171 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
172 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
173 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
174 int32_t aB = SkGetPackedB32(a);
175
176 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
177 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
178 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
179 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
180
181 return aA == bA &&
182 SkAbs32(aR - bR) <= 1 &&
183 SkAbs32(aG - bG) <= 1 &&
184 SkAbs32(aB - bB) <= 1;
185}
186
reed52d9ac62014-06-30 09:05:34 -0700187static bool check_write(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap,
reed@google.com7111d462014-03-25 16:20:24 +0000188 int writeX, int writeY) {
reed52d9ac62014-06-30 09:05:34 -0700189 const SkImageInfo canvasInfo = canvas->imageInfo();
reed@google.com7111d462014-03-25 16:20:24 +0000190 size_t canvasRowBytes;
191 const uint32_t* canvasPixels;
reed@google.com11211702014-03-25 12:00:30 +0000192
reed@google.com7111d462014-03-25 16:20:24 +0000193 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
194 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
195 // readPixels for the client.
196 SkBitmap secretDevBitmap;
halcanaryf622a6c2014-10-24 12:54:53 -0700197 canvas->readPixels(canvasInfo.bounds(), &secretDevBitmap);
reed52d9ac62014-06-30 09:05:34 -0700198
reed@google.com7111d462014-03-25 16:20:24 +0000199 SkAutoLockPixels alp(secretDevBitmap);
reed@google.com7111d462014-03-25 16:20:24 +0000200 canvasRowBytes = secretDevBitmap.rowBytes();
201 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
202
203 if (NULL == canvasPixels) {
204 return false;
205 }
206
207 if (canvasInfo.width() != DEV_W ||
208 canvasInfo.height() != DEV_H ||
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000209 canvasInfo.colorType() != kN32_SkColorType) {
reed@google.com7111d462014-03-25 16:20:24 +0000210 return false;
211 }
212
213 const SkImageInfo bmInfo = bitmap.info();
214
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000215 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000216 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000217 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000218 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000219 if (writeRect.contains(cx, cy)) {
220 int bx = cx - writeX;
221 int by = cy - writeY;
bsalomonf0674512015-07-28 13:26:15 -0700222 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
reed@google.com7111d462014-03-25 16:20:24 +0000223 bmInfo.colorType(), bmInfo.alphaType());
224 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
225 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
226 bmpColor8888);
bsalomonf0674512015-07-28 13:26:15 -0700227 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
228 ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
229 "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000230 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000231 }
232 } else {
bsalomonf0674512015-07-28 13:26:15 -0700233 SkPMColor testColor = get_canvas_color(cx, cy);
234 if (canvasPixel != testColor) {
235 ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
236 " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000237 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000238 }
239 }
240 }
241 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000242 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000243 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
244 bool check;
245 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
246 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000247 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000248 }
249 }
250 }
reed@google.com7111d462014-03-25 16:20:24 +0000251 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000252 }
253
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000254 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000255}
256
257enum DevType {
258 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000259#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000260 kGpu_BottomLeft_DevType,
261 kGpu_TopLeft_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000262#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000263};
264
265struct CanvasConfig {
266 DevType fDevType;
267 bool fTightRowBytes;
268};
269
270static const CanvasConfig gCanvasConfigs[] = {
271 {kRaster_DevType, true},
272 {kRaster_DevType, false},
reed@google.com8f4d2302013-12-17 16:44:46 +0000273#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000274 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices
275 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000276#endif
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 }
289 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL);
290 bm->setPixelRef(pr)->unref();
291 return true;
292}
293
reed52d9ac62014-06-30 09:05:34 -0700294static void free_pixels(void* pixels, void* ctx) {
295 sk_free(pixels);
296}
297
298static SkSurface* create_surface(const CanvasConfig& c, GrContext* grCtx) {
299 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000300 switch (c.fDevType) {
301 case kRaster_DevType: {
reed52d9ac62014-06-30 09:05:34 -0700302 const size_t rowBytes = c.fTightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
303 const size_t size = info.getSafeSize(rowBytes);
304 void* pixels = sk_malloc_throw(size);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000305 // if rowBytes isn't tight then set the padding to a known value
reed52d9ac62014-06-30 09:05:34 -0700306 if (!c.fTightRowBytes) {
307 memset(pixels, DEV_PAD, size);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000308 }
reed52d9ac62014-06-30 09:05:34 -0700309 return SkSurface::NewRasterDirectReleaseProc(info, pixels, rowBytes, free_pixels, NULL);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000310 }
311#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000312 case kGpu_BottomLeft_DevType:
313 case kGpu_TopLeft_DevType:
bsalomonf2703d82014-10-28 14:33:06 -0700314 GrSurfaceDesc desc;
315 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000316 desc.fWidth = DEV_W;
317 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000318 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000319 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ?
320 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
bsalomond309e7a2015-04-30 14:18:54 -0700321 SkAutoTUnref<GrTexture> texture(grCtx->textureProvider()->createTexture(desc, false));
bsalomone3059732014-10-14 11:47:22 -0700322 return SkSurface::NewRenderTargetDirect(texture->asRenderTarget());
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000323#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000324 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000325 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000326}
327
reed52d9ac62014-06-30 09:05:34 -0700328static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
reed@google.com7111d462014-03-25 16:20:24 +0000329 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
330 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
bsalomonf0674512015-07-28 13:26:15 -0700331 if (!alloc_row_bytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000332 return false;
333 }
reed@google.com7111d462014-03-25 16:20:24 +0000334 SkAutoLockPixels alp(*bm);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335 for (int y = 0; y < h; ++y) {
336 for (int x = 0; x < w; ++x) {
bsalomonf0674512015-07-28 13:26:15 -0700337 *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 }
339 }
340 return true;
341}
342
reed4af35f32014-06-27 17:47:49 -0700343static void call_writepixels(SkCanvas* canvas) {
344 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
345 SkPMColor pixel = 0;
346 canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0);
347}
348
349static void test_surface_genid(skiatest::Reporter* reporter) {
350 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
351 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
352 uint32_t genID1 = surface->generationID();
353 call_writepixels(surface->getCanvas());
354 uint32_t genID2 = surface->generationID();
355 REPORTER_ASSERT(reporter, genID1 != genID2);
356}
357
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000358DEF_GPUTEST(WritePixels, reporter, factory) {
reed4af35f32014-06-27 17:47:49 -0700359 test_surface_genid(reporter);
360
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000361 SkCanvas canvas;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000362
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000363 const SkIRect testRects[] = {
364 // entire thing
365 DEV_RECT,
366 // larger on all sides
367 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
368 // fully contained
369 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
370 // outside top left
371 SkIRect::MakeLTRB(-10, -10, -1, -1),
372 // touching top left corner
373 SkIRect::MakeLTRB(-10, -10, 0, 0),
374 // overlapping top left corner
375 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
376 // overlapping top left and top right corners
377 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
378 // touching entire top edge
379 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
380 // overlapping top right corner
381 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
382 // contained in x, overlapping top edge
383 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
384 // outside top right corner
385 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
386 // touching top right corner
387 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
388 // overlapping top left and bottom left corners
389 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
390 // touching entire left edge
391 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
392 // overlapping bottom left corner
393 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
394 // contained in y, overlapping left edge
395 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
396 // outside bottom left corner
397 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
398 // touching bottom left corner
399 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
400 // overlapping bottom left and bottom right corners
401 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
402 // touching entire left edge
403 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
404 // overlapping bottom right corner
405 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
406 // overlapping top right and bottom right corners
407 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
408 };
409
410 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000411 int glCtxTypeCnt = 1;
412#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000413 bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType ||
414 kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType;
415 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000416 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
417 }
418#endif
419 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
420 GrContext* context = NULL;
421#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000422 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000423 GrContextFactory::GLContextType type =
424 static_cast<GrContextFactory::GLContextType>(glCtxType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000425 if (!GrContextFactory::IsRenderingGLContext(type)) {
426 continue;
427 }
428 context = factory->get(type);
429 if (NULL == context) {
430 continue;
431 }
432 }
433#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000434
reed52d9ac62014-06-30 09:05:34 -0700435 SkAutoTUnref<SkSurface> surface(create_surface(gCanvasConfigs[i], context));
436 SkCanvas& canvas = *surface->getCanvas();
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000437
reed@google.com7111d462014-03-25 16:20:24 +0000438 static const struct {
439 SkColorType fColorType;
440 SkAlphaType fAlphaType;
441 } gSrcConfigs[] = {
442 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
443 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
444 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
445 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000446 };
447 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
448 const SkIRect& rect = testRects[r];
449 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
450 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
reed@google.com7111d462014-03-25 16:20:24 +0000451 const SkColorType ct = gSrcConfigs[c].fColorType;
452 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
453
bsalomonf0674512015-07-28 13:26:15 -0700454 fill_canvas(&canvas);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000455 SkBitmap bmp;
reed52d9ac62014-06-30 09:05:34 -0700456 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
457 rect.height(), SkToBool(tightBmp)));
458 uint32_t idBefore = surface->generationID();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000459
reed@google.com7111d462014-03-25 16:20:24 +0000460 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
461 canvas.writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000462
reed52d9ac62014-06-30 09:05:34 -0700463 uint32_t idAfter = surface->generationID();
464 REPORTER_ASSERT(reporter, check_write(reporter, &canvas, bmp,
465 rect.fLeft, rect.fTop));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000466
467 // we should change the genID iff pixels were actually written.
468 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
469 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
470 bmp.width(), bmp.height());
471 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
472 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
473 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000474 }
475 }
476 }
477 }
478}