blob: 70b91f227f24e48f13a767ac0ab7027e7af3a3e8 [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()) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000118 SkDEBUGCODE(bool alloc = ) bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000119 SkASSERT(alloc);
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) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000122 *bmp.getAddr32(x, y) = getCanvasColor(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000123 }
124 }
125 }
126 canvas->save();
127 canvas->setMatrix(SkMatrix::I());
128 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
129 SkPaint paint;
130 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
131 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
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000169static bool checkPixel(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
reed@google.com7111d462014-03-25 16:20:24 +0000189static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap,
190 int writeX, int writeY) {
191 SkImageInfo canvasInfo;
192 size_t canvasRowBytes;
193 const uint32_t* canvasPixels;
reed@google.com11211702014-03-25 12:00:30 +0000194
reed@google.com7111d462014-03-25 16:20:24 +0000195 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
196 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
197 // readPixels for the client.
198 SkBitmap secretDevBitmap;
199 {
200 SkBaseDevice* dev = canvas->getDevice();
201 if (!dev) {
202 return false;
203 }
204 secretDevBitmap = dev->accessBitmap(false);
205 }
206 SkAutoLockPixels alp(secretDevBitmap);
207 canvasInfo = secretDevBitmap.info();
208 canvasRowBytes = secretDevBitmap.rowBytes();
209 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
210
211 if (NULL == canvasPixels) {
212 return false;
213 }
214
215 if (canvasInfo.width() != DEV_W ||
216 canvasInfo.height() != DEV_H ||
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000217 canvasInfo.colorType() != kN32_SkColorType) {
reed@google.com7111d462014-03-25 16:20:24 +0000218 return false;
219 }
220
221 const SkImageInfo bmInfo = bitmap.info();
222
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000223 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000224 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000225 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000226 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000227 if (writeRect.contains(cx, cy)) {
228 int bx = cx - writeX;
229 int by = cy - writeY;
reed@google.com7111d462014-03-25 16:20:24 +0000230 uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(),
231 bmInfo.colorType(), bmInfo.alphaType());
232 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
233 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
234 bmpColor8888);
235 bool check = checkPixel(bmpPMColor, canvasPixel, mul);
236 REPORTER_ASSERT(reporter, check);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000237 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000238 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000239 }
240 } else {
241 bool check;
242 SkPMColor testColor = getCanvasColor(cx, cy);
243 REPORTER_ASSERT(reporter, check = (canvasPixel == testColor));
244 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000245 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000246 }
247 }
248 }
249 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000250 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000251 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
252 bool check;
253 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
254 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000255 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000256 }
257 }
258 }
reed@google.com7111d462014-03-25 16:20:24 +0000259 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000260 }
261
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000262 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000263}
264
265enum DevType {
266 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000267#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000268 kGpu_BottomLeft_DevType,
269 kGpu_TopLeft_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000270#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000271};
272
273struct CanvasConfig {
274 DevType fDevType;
275 bool fTightRowBytes;
276};
277
278static const CanvasConfig gCanvasConfigs[] = {
279 {kRaster_DevType, true},
280 {kRaster_DevType, false},
reed@google.com8f4d2302013-12-17 16:44:46 +0000281#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000282 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices
283 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000284#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000285};
286
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000287#include "SkMallocPixelRef.h"
288
289// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
290// a custom pixelRef (which also has to specify its rowBytes), so we have to be
291// sure that the two rowBytes match (and the infos match).
292//
293static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000294 if (!bm->setInfo(info, rowBytes)) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000295 return false;
296 }
297 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL);
298 bm->setPixelRef(pr)->unref();
299 return true;
300}
301
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000302static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000303 switch (c.fDevType) {
304 case kRaster_DevType: {
305 SkBitmap bmp;
306 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000307 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
308 if (!allocRowBytes(&bmp, info, rowBytes)) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000309 sk_throw();
310 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000311 }
312 // if rowBytes isn't tight then set the padding to a known value
313 if (rowBytes) {
314 SkAutoLockPixels alp(bmp);
commit-bot@chromium.org61c9b832014-03-24 15:52:33 +0000315 // We'd just use memset here but GCC 4.8.1 throws up a bogus warning when we do.
316 for (size_t i = 0; i < bmp.getSafeSize(); i++) {
317 ((uint8_t*)bmp.getPixels())[i] = DEV_PAD;
318 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000319 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000320 return new SkBitmapDevice(bmp);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000321 }
322#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000323 case kGpu_BottomLeft_DevType:
324 case kGpu_TopLeft_DevType:
325 GrTextureDesc desc;
326 desc.fFlags = kRenderTarget_GrTextureFlagBit;
327 desc.fWidth = DEV_W;
328 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000329 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000330 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ?
331 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
332 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMatch);
333 SkAutoTUnref<GrTexture> tex(ast.detach());
334 return new SkGpuDevice(grCtx, tex);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000335#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000337 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338}
339
reed@google.com7111d462014-03-25 16:20:24 +0000340static bool setupBitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
341 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
342 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
343 if (!allocRowBytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000344 return false;
345 }
reed@google.com7111d462014-03-25 16:20:24 +0000346 SkAutoLockPixels alp(*bm);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000347 for (int y = 0; y < h; ++y) {
348 for (int x = 0; x < w; ++x) {
reed@google.com7111d462014-03-25 16:20:24 +0000349 *bm->getAddr32(x, y) = getBitmapColor(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000350 }
351 }
352 return true;
353}
354
reed4af35f32014-06-27 17:47:49 -0700355static void call_writepixels(SkCanvas* canvas) {
356 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
357 SkPMColor pixel = 0;
358 canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0);
359}
360
361static void test_surface_genid(skiatest::Reporter* reporter) {
362 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
363 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
364 uint32_t genID1 = surface->generationID();
365 call_writepixels(surface->getCanvas());
366 uint32_t genID2 = surface->generationID();
367 REPORTER_ASSERT(reporter, genID1 != genID2);
368}
369
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000370DEF_GPUTEST(WritePixels, reporter, factory) {
reed4af35f32014-06-27 17:47:49 -0700371 test_surface_genid(reporter);
372
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000373 SkCanvas canvas;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000374
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000375 const SkIRect testRects[] = {
376 // entire thing
377 DEV_RECT,
378 // larger on all sides
379 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
380 // fully contained
381 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
382 // outside top left
383 SkIRect::MakeLTRB(-10, -10, -1, -1),
384 // touching top left corner
385 SkIRect::MakeLTRB(-10, -10, 0, 0),
386 // overlapping top left corner
387 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
388 // overlapping top left and top right corners
389 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
390 // touching entire top edge
391 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
392 // overlapping top right corner
393 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
394 // contained in x, overlapping top edge
395 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
396 // outside top right corner
397 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
398 // touching top right corner
399 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
400 // overlapping top left and bottom left corners
401 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
402 // touching entire left edge
403 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
404 // overlapping bottom left corner
405 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
406 // contained in y, overlapping left edge
407 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
408 // outside bottom left corner
409 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
410 // touching bottom left corner
411 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
412 // overlapping bottom left and bottom right corners
413 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
414 // touching entire left edge
415 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
416 // overlapping bottom right corner
417 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
418 // overlapping top right and bottom right corners
419 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
420 };
421
422 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000423 int glCtxTypeCnt = 1;
424#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000425 bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType ||
426 kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType;
427 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000428 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
429 }
430#endif
431 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
432 GrContext* context = NULL;
433#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000434 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000435 GrContextFactory::GLContextType type =
436 static_cast<GrContextFactory::GLContextType>(glCtxType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000437 if (!GrContextFactory::IsRenderingGLContext(type)) {
438 continue;
439 }
440 context = factory->get(type);
441 if (NULL == context) {
442 continue;
443 }
444 }
445#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000446
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000447 SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000448 SkCanvas canvas(device);
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000449
reed@google.com7111d462014-03-25 16:20:24 +0000450 static const struct {
451 SkColorType fColorType;
452 SkAlphaType fAlphaType;
453 } gSrcConfigs[] = {
454 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
455 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
456 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
457 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000458 };
459 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
460 const SkIRect& rect = testRects[r];
461 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
462 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
reed@google.com7111d462014-03-25 16:20:24 +0000463 const SkColorType ct = gSrcConfigs[c].fColorType;
464 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
465
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000466 fillCanvas(&canvas);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000467 SkBitmap bmp;
reed@google.com7111d462014-03-25 16:20:24 +0000468 REPORTER_ASSERT(reporter, setupBitmap(&bmp, ct, at, rect.width(),
469 rect.height(), SkToBool(tightBmp)));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000470 uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000471
reed@google.com7111d462014-03-25 16:20:24 +0000472 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
473 canvas.writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000474
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000475 uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
reed@google.com7111d462014-03-25 16:20:24 +0000476 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000477
478 // we should change the genID iff pixels were actually written.
479 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
480 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
481 bmp.width(), bmp.height());
482 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
483 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
484 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000485 }
486 }
487 }
488 }
489}