blob: 2d24a77f35b7104b8000c1826752b1d3012212a8 [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
robertphillips@google.com6323ca52013-08-29 12:53:23 +00008#include "SkBitmapDevice.h"
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +00009#include "SkCanvas.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010#include "SkColorPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000011#include "SkMathPriv.h"
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000012#include "SkRegion.h"
reed4af35f32014-06-27 17:47:49 -070013#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000014#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000015#include "sk_tool_utils.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000016
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +000018#include "GrContextFactory.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000019#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000020#else
21class GrContext;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000022class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000023#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000024
25static const int DEV_W = 100, DEV_H = 100;
26static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000027static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000028 DEV_H * SK_Scalar1);
29static const U8CPU DEV_PAD = 0xee;
30
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000031static SkPMColor getCanvasColor(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
reed@google.com7111d462014-03-25 16:20:24 +000061static uint32_t packColorType(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;
reed@google.com7111d462014-03-25 16:20:24 +000071 case kRGBA_8888_SkColorType:
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000072 result[0] = r;
73 result[1] = g;
74 result[2] = b;
75 result[3] = a;
76 break;
77 default:
78 SkASSERT(0);
79 return 0;
80 }
81 return r32;
82}
83
reed@google.com7111d462014-03-25 16:20:24 +000084static uint32_t getBitmapColor(int x, int y, int w, SkColorType ct, SkAlphaType at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000085 int n = y * w + x;
86 U8CPU b = n & 0xff;
87 U8CPU g = (n >> 8) & 0xff;
88 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000089 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000090 switch ((x+y) % 5) {
91 case 4:
92 a = 0xff;
93 break;
94 case 3:
95 a = 0x80;
96 break;
97 case 2:
98 a = 0xCC;
99 break;
100 case 1:
101 a = 0x01;
102 break;
103 case 0:
104 a = 0x00;
105 break;
106 }
reed@google.com7111d462014-03-25 16:20:24 +0000107 if (kPremul_SkAlphaType == at) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000108 r = SkMulDiv255Ceiling(r, a);
109 g = SkMulDiv255Ceiling(g, a);
110 b = SkMulDiv255Ceiling(b, a);
111 }
reed@google.com7111d462014-03-25 16:20:24 +0000112 return packColorType(ct, a, r, g , b);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000113}
114
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000115static void fillCanvas(SkCanvas* canvas) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000116 SkBitmap bmp;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000117 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700118 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000119 for (int y = 0; y < DEV_H; ++y) {
120 for (int x = 0; x < DEV_W; ++x) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000121 *bmp.getAddr32(x, y) = getCanvasColor(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000122 }
123 }
124 }
125 canvas->save();
126 canvas->setMatrix(SkMatrix::I());
127 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
128 SkPaint paint;
129 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
130 canvas->drawBitmap(bmp, 0, 0, &paint);
131 canvas->restore();
132}
133
reed@google.com7111d462014-03-25 16:20:24 +0000134/**
135 * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
136 * Thus this routine doesn't need to know the exact colortype
137 */
138static uint32_t premul(uint32_t color) {
139 unsigned a = SkGetPackedA32(color);
140 // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
141 unsigned c0 = SkGetPackedR32(color);
142 unsigned c1 = SkGetPackedG32(color);
143 unsigned c2 = SkGetPackedB32(color);
144 c0 = SkMulDiv255Ceiling(c0, a);
145 c1 = SkMulDiv255Ceiling(c1, a);
146 c2 = SkMulDiv255Ceiling(c2, a);
147 return SkPackARGB32NoCheck(a, c0, c1, c2);
148}
149
150static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
151 if (kUnpremul_SkAlphaType == at) {
152 color = premul(color);
153 }
154 switch (ct) {
155 case kRGBA_8888_SkColorType:
156 color = SkSwizzle_RGBA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000157 break;
reed@google.com7111d462014-03-25 16:20:24 +0000158 case kBGRA_8888_SkColorType:
159 color = SkSwizzle_BGRA_to_PMColor(color);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000160 break;
161 default:
reed@google.com7111d462014-03-25 16:20:24 +0000162 SkASSERT(0);
163 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000164 }
reed@google.com7111d462014-03-25 16:20:24 +0000165 return color;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000166}
167
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000168static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000169 if (!didPremulConversion) {
170 return a == b;
171 }
172 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
173 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
174 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
175 int32_t aB = SkGetPackedB32(a);
176
177 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
178 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
179 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
180 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
181
182 return aA == bA &&
183 SkAbs32(aR - bR) <= 1 &&
184 SkAbs32(aG - bG) <= 1 &&
185 SkAbs32(aB - bB) <= 1;
186}
187
reed52d9ac62014-06-30 09:05:34 -0700188static bool check_write(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap,
reed@google.com7111d462014-03-25 16:20:24 +0000189 int writeX, int writeY) {
reed52d9ac62014-06-30 09:05:34 -0700190 const SkImageInfo canvasInfo = canvas->imageInfo();
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;
halcanaryf622a6c2014-10-24 12:54:53 -0700198 canvas->readPixels(canvasInfo.bounds(), &secretDevBitmap);
reed52d9ac62014-06-30 09:05:34 -0700199
reed@google.com7111d462014-03-25 16:20:24 +0000200 SkAutoLockPixels alp(secretDevBitmap);
reed@google.com7111d462014-03-25 16:20:24 +0000201 canvasRowBytes = secretDevBitmap.rowBytes();
202 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
203
204 if (NULL == canvasPixels) {
205 return false;
206 }
207
208 if (canvasInfo.width() != DEV_W ||
209 canvasInfo.height() != DEV_H ||
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000210 canvasInfo.colorType() != kN32_SkColorType) {
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;
reed@google.com7111d462014-03-25 16:20:24 +0000223 uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(),
224 bmInfo.colorType(), bmInfo.alphaType());
225 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
226 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
227 bmpColor8888);
228 bool check = checkPixel(bmpPMColor, canvasPixel, mul);
229 REPORTER_ASSERT(reporter, check);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000230 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000231 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000232 }
233 } else {
234 bool check;
235 SkPMColor testColor = getCanvasColor(cx, cy);
236 REPORTER_ASSERT(reporter, check = (canvasPixel == testColor));
237 if (!check) {
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
258enum DevType {
259 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000260#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000261 kGpu_BottomLeft_DevType,
262 kGpu_TopLeft_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000263#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000264};
265
266struct CanvasConfig {
267 DevType fDevType;
268 bool fTightRowBytes;
269};
270
271static const CanvasConfig gCanvasConfigs[] = {
272 {kRaster_DevType, true},
273 {kRaster_DevType, false},
reed@google.com8f4d2302013-12-17 16:44:46 +0000274#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000275 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices
276 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000277#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000278};
279
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000280#include "SkMallocPixelRef.h"
281
282// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
283// a custom pixelRef (which also has to specify its rowBytes), so we have to be
284// sure that the two rowBytes match (and the infos match).
285//
286static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000287 if (!bm->setInfo(info, rowBytes)) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000288 return false;
289 }
290 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL);
291 bm->setPixelRef(pr)->unref();
292 return true;
293}
294
reed52d9ac62014-06-30 09:05:34 -0700295static void free_pixels(void* pixels, void* ctx) {
296 sk_free(pixels);
297}
298
299static SkSurface* create_surface(const CanvasConfig& c, GrContext* grCtx) {
300 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000301 switch (c.fDevType) {
302 case kRaster_DevType: {
reed52d9ac62014-06-30 09:05:34 -0700303 const size_t rowBytes = c.fTightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
304 const size_t size = info.getSafeSize(rowBytes);
305 void* pixels = sk_malloc_throw(size);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000306 // if rowBytes isn't tight then set the padding to a known value
reed52d9ac62014-06-30 09:05:34 -0700307 if (!c.fTightRowBytes) {
308 memset(pixels, DEV_PAD, size);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000309 }
reed52d9ac62014-06-30 09:05:34 -0700310 return SkSurface::NewRasterDirectReleaseProc(info, pixels, rowBytes, free_pixels, NULL);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000311 }
312#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000313 case kGpu_BottomLeft_DevType:
314 case kGpu_TopLeft_DevType:
bsalomonf2703d82014-10-28 14:33:06 -0700315 GrSurfaceDesc desc;
316 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000317 desc.fWidth = DEV_W;
318 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000319 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000320 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ?
321 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
bsalomond0423582015-02-06 08:49:24 -0800322 SkAutoTUnref<GrTexture> texture(grCtx->createTexture(desc, false));
bsalomone3059732014-10-14 11:47:22 -0700323 return SkSurface::NewRenderTargetDirect(texture->asRenderTarget());
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000324#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000325 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000326 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327}
328
reed52d9ac62014-06-30 09:05:34 -0700329static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
reed@google.com7111d462014-03-25 16:20:24 +0000330 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
331 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
332 if (!allocRowBytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333 return false;
334 }
reed@google.com7111d462014-03-25 16:20:24 +0000335 SkAutoLockPixels alp(*bm);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336 for (int y = 0; y < h; ++y) {
337 for (int x = 0; x < w; ++x) {
reed@google.com7111d462014-03-25 16:20:24 +0000338 *bm->getAddr32(x, y) = getBitmapColor(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339 }
340 }
341 return true;
342}
343
reed4af35f32014-06-27 17:47:49 -0700344static void call_writepixels(SkCanvas* canvas) {
345 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
346 SkPMColor pixel = 0;
347 canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0);
348}
349
350static void test_surface_genid(skiatest::Reporter* reporter) {
351 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
352 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
353 uint32_t genID1 = surface->generationID();
354 call_writepixels(surface->getCanvas());
355 uint32_t genID2 = surface->generationID();
356 REPORTER_ASSERT(reporter, genID1 != genID2);
357}
358
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000359DEF_GPUTEST(WritePixels, reporter, factory) {
reed4af35f32014-06-27 17:47:49 -0700360 test_surface_genid(reporter);
361
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000362 SkCanvas canvas;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000363
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000364 const SkIRect testRects[] = {
365 // entire thing
366 DEV_RECT,
367 // larger on all sides
368 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
369 // fully contained
370 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
371 // outside top left
372 SkIRect::MakeLTRB(-10, -10, -1, -1),
373 // touching top left corner
374 SkIRect::MakeLTRB(-10, -10, 0, 0),
375 // overlapping top left corner
376 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
377 // overlapping top left and top right corners
378 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
379 // touching entire top edge
380 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
381 // overlapping top right corner
382 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
383 // contained in x, overlapping top edge
384 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
385 // outside top right corner
386 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
387 // touching top right corner
388 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
389 // overlapping top left and bottom left corners
390 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
391 // touching entire left edge
392 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
393 // overlapping bottom left corner
394 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
395 // contained in y, overlapping left edge
396 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
397 // outside bottom left corner
398 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
399 // touching bottom left corner
400 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
401 // overlapping bottom left and bottom right corners
402 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
403 // touching entire left edge
404 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
405 // overlapping bottom right corner
406 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
407 // overlapping top right and bottom right corners
408 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
409 };
410
411 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000412 int glCtxTypeCnt = 1;
413#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000414 bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType ||
415 kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType;
416 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000417 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
418 }
419#endif
420 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
421 GrContext* context = NULL;
422#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000423 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000424 GrContextFactory::GLContextType type =
425 static_cast<GrContextFactory::GLContextType>(glCtxType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000426 if (!GrContextFactory::IsRenderingGLContext(type)) {
427 continue;
428 }
429 context = factory->get(type);
430 if (NULL == context) {
431 continue;
432 }
433 }
434#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000435
reed52d9ac62014-06-30 09:05:34 -0700436 SkAutoTUnref<SkSurface> surface(create_surface(gCanvasConfigs[i], context));
437 SkCanvas& canvas = *surface->getCanvas();
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000438
reed@google.com7111d462014-03-25 16:20:24 +0000439 static const struct {
440 SkColorType fColorType;
441 SkAlphaType fAlphaType;
442 } gSrcConfigs[] = {
443 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
444 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
445 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
446 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000447 };
448 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
449 const SkIRect& rect = testRects[r];
450 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
451 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
reed@google.com7111d462014-03-25 16:20:24 +0000452 const SkColorType ct = gSrcConfigs[c].fColorType;
453 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
454
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000455 fillCanvas(&canvas);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000456 SkBitmap bmp;
reed52d9ac62014-06-30 09:05:34 -0700457 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
458 rect.height(), SkToBool(tightBmp)));
459 uint32_t idBefore = surface->generationID();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000460
reed@google.com7111d462014-03-25 16:20:24 +0000461 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
462 canvas.writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000463
reed52d9ac62014-06-30 09:05:34 -0700464 uint32_t idAfter = surface->generationID();
465 REPORTER_ASSERT(reporter, check_write(reporter, &canvas, bmp,
466 rect.fLeft, rect.fTop));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000467
468 // we should change the genID iff pixels were actually written.
469 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
470 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
471 bmp.width(), bmp.height());
472 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
473 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
474 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000475 }
476 }
477 }
478 }
479}