blob: a0e305168036404436572749d8cae45e30f99fc2 [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"
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
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000030static SkPMColor getCanvasColor(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
reed@google.com7111d462014-03-25 16:20:24 +000060static uint32_t packColorType(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
reed@google.com7111d462014-03-25 16:20:24 +000083static uint32_t getBitmapColor(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 }
reed@google.com7111d462014-03-25 16:20:24 +0000111 return packColorType(ct, a, r, g , b);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000112}
113
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000114static void fillCanvas(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()) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000117 SkDEBUGCODE(bool alloc = ) bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000118 SkASSERT(alloc);
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
reed@google.com7111d462014-03-25 16:20:24 +0000188static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap,
189 int writeX, int writeY) {
190 SkImageInfo canvasInfo;
191 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;
198 {
199 SkBaseDevice* dev = canvas->getDevice();
200 if (!dev) {
201 return false;
202 }
203 secretDevBitmap = dev->accessBitmap(false);
204 }
205 SkAutoLockPixels alp(secretDevBitmap);
206 canvasInfo = secretDevBitmap.info();
207 canvasRowBytes = secretDevBitmap.rowBytes();
208 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
209
210 if (NULL == canvasPixels) {
211 return false;
212 }
213
214 if (canvasInfo.width() != DEV_W ||
215 canvasInfo.height() != DEV_H ||
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000216 canvasInfo.colorType() != kN32_SkColorType) {
reed@google.com7111d462014-03-25 16:20:24 +0000217 return false;
218 }
219
220 const SkImageInfo bmInfo = bitmap.info();
221
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000222 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000223 for (int cy = 0; cy < DEV_H; ++cy) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000224 for (int cx = 0; cx < DEV_W; ++cx) {
reed@google.com7111d462014-03-25 16:20:24 +0000225 SkPMColor canvasPixel = canvasPixels[cx];
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000226 if (writeRect.contains(cx, cy)) {
227 int bx = cx - writeX;
228 int by = cy - writeY;
reed@google.com7111d462014-03-25 16:20:24 +0000229 uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(),
230 bmInfo.colorType(), bmInfo.alphaType());
231 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
232 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
233 bmpColor8888);
234 bool check = checkPixel(bmpPMColor, canvasPixel, mul);
235 REPORTER_ASSERT(reporter, check);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000236 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000237 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000238 }
239 } else {
240 bool check;
241 SkPMColor testColor = getCanvasColor(cx, cy);
242 REPORTER_ASSERT(reporter, check = (canvasPixel == testColor));
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 }
248 if (cy != DEV_H -1) {
reed@google.com7111d462014-03-25 16:20:24 +0000249 const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000250 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
251 bool check;
252 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
253 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000254 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000255 }
256 }
257 }
reed@google.com7111d462014-03-25 16:20:24 +0000258 canvasPixels += canvasRowBytes/4;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000259 }
260
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000261 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000262}
263
264enum DevType {
265 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000266#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000267 kGpu_BottomLeft_DevType,
268 kGpu_TopLeft_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000269#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000270};
271
272struct CanvasConfig {
273 DevType fDevType;
274 bool fTightRowBytes;
275};
276
277static const CanvasConfig gCanvasConfigs[] = {
278 {kRaster_DevType, true},
279 {kRaster_DevType, false},
reed@google.com8f4d2302013-12-17 16:44:46 +0000280#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000281 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices
282 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000283#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000284};
285
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000286#include "SkMallocPixelRef.h"
287
288// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
289// a custom pixelRef (which also has to specify its rowBytes), so we have to be
290// sure that the two rowBytes match (and the infos match).
291//
292static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
293 if (!bm->setConfig(info, rowBytes)) {
294 return false;
295 }
296 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL);
297 bm->setPixelRef(pr)->unref();
298 return true;
299}
300
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000301static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302 switch (c.fDevType) {
303 case kRaster_DevType: {
304 SkBitmap bmp;
305 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000306 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
307 if (!allocRowBytes(&bmp, info, rowBytes)) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000308 sk_throw();
309 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000310 }
311 // if rowBytes isn't tight then set the padding to a known value
312 if (rowBytes) {
313 SkAutoLockPixels alp(bmp);
commit-bot@chromium.org61c9b832014-03-24 15:52:33 +0000314 // We'd just use memset here but GCC 4.8.1 throws up a bogus warning when we do.
315 for (size_t i = 0; i < bmp.getSafeSize(); i++) {
316 ((uint8_t*)bmp.getPixels())[i] = DEV_PAD;
317 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000318 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000319 return new SkBitmapDevice(bmp);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000320 }
321#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000322 case kGpu_BottomLeft_DevType:
323 case kGpu_TopLeft_DevType:
324 GrTextureDesc desc;
325 desc.fFlags = kRenderTarget_GrTextureFlagBit;
326 desc.fWidth = DEV_W;
327 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000328 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000329 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ?
330 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
331 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMatch);
332 SkAutoTUnref<GrTexture> tex(ast.detach());
333 return new SkGpuDevice(grCtx, tex);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000334#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000336 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000337}
338
reed@google.com7111d462014-03-25 16:20:24 +0000339static bool setupBitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
340 size_t rowBytes = tightRB ? 0 : 4 * w + 60;
341 SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
342 if (!allocRowBytes(bm, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343 return false;
344 }
reed@google.com7111d462014-03-25 16:20:24 +0000345 SkAutoLockPixels alp(*bm);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000346 for (int y = 0; y < h; ++y) {
347 for (int x = 0; x < w; ++x) {
reed@google.com7111d462014-03-25 16:20:24 +0000348 *bm->getAddr32(x, y) = getBitmapColor(x, y, w, ct, at);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000349 }
350 }
351 return true;
352}
353
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000354DEF_GPUTEST(WritePixels, reporter, factory) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000355 SkCanvas canvas;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000357 const SkIRect testRects[] = {
358 // entire thing
359 DEV_RECT,
360 // larger on all sides
361 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
362 // fully contained
363 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
364 // outside top left
365 SkIRect::MakeLTRB(-10, -10, -1, -1),
366 // touching top left corner
367 SkIRect::MakeLTRB(-10, -10, 0, 0),
368 // overlapping top left corner
369 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
370 // overlapping top left and top right corners
371 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
372 // touching entire top edge
373 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
374 // overlapping top right corner
375 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
376 // contained in x, overlapping top edge
377 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
378 // outside top right corner
379 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
380 // touching top right corner
381 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
382 // overlapping top left and bottom left corners
383 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
384 // touching entire left edge
385 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
386 // overlapping bottom left corner
387 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
388 // contained in y, overlapping left edge
389 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
390 // outside bottom left corner
391 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
392 // touching bottom left corner
393 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
394 // overlapping bottom left and bottom right corners
395 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
396 // touching entire left edge
397 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
398 // overlapping bottom right corner
399 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
400 // overlapping top right and bottom right corners
401 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
402 };
403
404 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000405 int glCtxTypeCnt = 1;
406#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000407 bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType ||
408 kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType;
409 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000410 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
411 }
412#endif
413 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
414 GrContext* context = NULL;
415#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000416 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000417 GrContextFactory::GLContextType type =
418 static_cast<GrContextFactory::GLContextType>(glCtxType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000419 if (!GrContextFactory::IsRenderingGLContext(type)) {
420 continue;
421 }
422 context = factory->get(type);
423 if (NULL == context) {
424 continue;
425 }
426 }
427#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000428
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000429 SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000430 SkCanvas canvas(device);
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000431
reed@google.com7111d462014-03-25 16:20:24 +0000432 static const struct {
433 SkColorType fColorType;
434 SkAlphaType fAlphaType;
435 } gSrcConfigs[] = {
436 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
437 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
438 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
439 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000440 };
441 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
442 const SkIRect& rect = testRects[r];
443 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
444 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
reed@google.com7111d462014-03-25 16:20:24 +0000445 const SkColorType ct = gSrcConfigs[c].fColorType;
446 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
447
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000448 fillCanvas(&canvas);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000449 SkBitmap bmp;
reed@google.com7111d462014-03-25 16:20:24 +0000450 REPORTER_ASSERT(reporter, setupBitmap(&bmp, ct, at, rect.width(),
451 rect.height(), SkToBool(tightBmp)));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000452 uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000453
reed@google.com7111d462014-03-25 16:20:24 +0000454 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
455 canvas.writePixels(bmp, rect.fLeft, rect.fTop);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000456
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000457 uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
reed@google.com7111d462014-03-25 16:20:24 +0000458 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000459
460 // we should change the genID iff pixels were actually written.
461 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
462 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
463 bmp.width(), bmp.height());
464 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
465 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
466 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000467 }
468 }
469 }
470 }
471}