blob: 7adcf0295dfd0c2a81f44001524d70790bfd7427 [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
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000059static bool config8888IsPremul(SkCanvas::Config8888 config8888) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000060 switch (config8888) {
61 case SkCanvas::kNative_Premul_Config8888:
62 case SkCanvas::kBGRA_Premul_Config8888:
63 case SkCanvas::kRGBA_Premul_Config8888:
64 return true;
65 case SkCanvas::kNative_Unpremul_Config8888:
66 case SkCanvas::kBGRA_Unpremul_Config8888:
67 case SkCanvas::kRGBA_Unpremul_Config8888:
68 return false;
69 default:
70 SkASSERT(0);
71 return false;
72 }
73}
74
75// assumes any premu/.unpremul has been applied
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000076static uint32_t packConfig8888(SkCanvas::Config8888 config8888,
77 U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000078 uint32_t r32;
79 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
80 switch (config8888) {
81 case SkCanvas::kNative_Premul_Config8888:
82 case SkCanvas::kNative_Unpremul_Config8888:
83 r32 = SkPackARGB32NoCheck(a, r, g, b);
84 break;
85 case SkCanvas::kBGRA_Premul_Config8888:
86 case SkCanvas::kBGRA_Unpremul_Config8888:
87 result[0] = b;
88 result[1] = g;
89 result[2] = r;
90 result[3] = a;
91 break;
92 case SkCanvas::kRGBA_Premul_Config8888:
93 case SkCanvas::kRGBA_Unpremul_Config8888:
94 result[0] = r;
95 result[1] = g;
96 result[2] = b;
97 result[3] = a;
98 break;
99 default:
100 SkASSERT(0);
101 return 0;
102 }
103 return r32;
104}
105
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000106static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8888) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000107 int n = y * w + x;
108 U8CPU b = n & 0xff;
109 U8CPU g = (n >> 8) & 0xff;
110 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +0000111 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000112 switch ((x+y) % 5) {
113 case 4:
114 a = 0xff;
115 break;
116 case 3:
117 a = 0x80;
118 break;
119 case 2:
120 a = 0xCC;
121 break;
122 case 1:
123 a = 0x01;
124 break;
125 case 0:
126 a = 0x00;
127 break;
128 }
129 if (config8888IsPremul(config8888)) {
130 r = SkMulDiv255Ceiling(r, a);
131 g = SkMulDiv255Ceiling(g, a);
132 b = SkMulDiv255Ceiling(b, a);
133 }
134 return packConfig8888(config8888, a, r, g , b);
135}
136
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000137static void fillCanvas(SkCanvas* canvas) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000138 SkBitmap bmp;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000139 if (bmp.isNull()) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000140 SkDEBUGCODE(bool alloc = ) bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000141 SkASSERT(alloc);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000142 for (int y = 0; y < DEV_H; ++y) {
143 for (int x = 0; x < DEV_W; ++x) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000144 *bmp.getAddr32(x, y) = getCanvasColor(x, y);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000145 }
146 }
147 }
148 canvas->save();
149 canvas->setMatrix(SkMatrix::I());
150 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
151 SkPaint paint;
152 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
153 canvas->drawBitmap(bmp, 0, 0, &paint);
154 canvas->restore();
155}
156
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000157static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
158 uint32_t color,
159 bool* premul) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000160 const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
161 U8CPU a,r,g,b;
162 *premul = false;
163 switch (config8888) {
164 case SkCanvas::kNative_Premul_Config8888:
165 return color;
166 case SkCanvas::kNative_Unpremul_Config8888:
167 *premul = true;
168 a = SkGetPackedA32(color);
169 r = SkGetPackedR32(color);
170 g = SkGetPackedG32(color);
171 b = SkGetPackedB32(color);
172 break;
173 case SkCanvas::kBGRA_Unpremul_Config8888:
174 *premul = true; // fallthru
175 case SkCanvas::kBGRA_Premul_Config8888:
176 a = static_cast<U8CPU>(c[3]);
177 r = static_cast<U8CPU>(c[2]);
178 g = static_cast<U8CPU>(c[1]);
179 b = static_cast<U8CPU>(c[0]);
180 break;
181 case SkCanvas::kRGBA_Unpremul_Config8888:
182 *premul = true; // fallthru
183 case SkCanvas::kRGBA_Premul_Config8888:
184 a = static_cast<U8CPU>(c[3]);
185 r = static_cast<U8CPU>(c[0]);
186 g = static_cast<U8CPU>(c[1]);
187 b = static_cast<U8CPU>(c[2]);
188 break;
189 default:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000190 SkDEBUGFAIL("Unexpected Config8888");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000191 return 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000192 }
193 if (*premul) {
194 r = SkMulDiv255Ceiling(r, a);
195 g = SkMulDiv255Ceiling(g, a);
196 b = SkMulDiv255Ceiling(b, a);
197 }
198 return SkPackARGB32(a, r, g, b);
199}
200
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000201static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000202 if (!didPremulConversion) {
203 return a == b;
204 }
205 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
206 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
207 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
208 int32_t aB = SkGetPackedB32(a);
209
210 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
211 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
212 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
213 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
214
215 return aA == bA &&
216 SkAbs32(aR - bR) <= 1 &&
217 SkAbs32(aG - bG) <= 1 &&
218 SkAbs32(aB - bB) <= 1;
219}
220
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000221static bool checkWrite(skiatest::Reporter* reporter,
222 SkCanvas* canvas,
223 const SkBitmap& bitmap,
224 int writeX, int writeY,
225 SkCanvas::Config8888 config8888) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000226 SkBaseDevice* dev = canvas->getDevice();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000227 if (!dev) {
228 return false;
229 }
230 SkBitmap devBmp = dev->accessBitmap(false);
231 if (devBmp.width() != DEV_W ||
232 devBmp.height() != DEV_H ||
233 devBmp.config() != SkBitmap::kARGB_8888_Config ||
234 devBmp.isNull()) {
235 return false;
236 }
237 SkAutoLockPixels alp(devBmp);
238
239 intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels());
240 size_t canvasRowBytes = devBmp.rowBytes();
241 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000242 for (int cy = 0; cy < DEV_H; ++cy) {
243 const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels);
244 for (int cx = 0; cx < DEV_W; ++cx) {
245 SkPMColor canvasPixel = canvasRow[cx];
246 if (writeRect.contains(cx, cy)) {
247 int bx = cx - writeX;
248 int by = cy - writeY;
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000249 uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), config8888);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000250 bool mul;
251 SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul);
252 bool check;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000253 REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul));
254 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000255 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000256 }
257 } else {
258 bool check;
259 SkPMColor testColor = getCanvasColor(cx, cy);
260 REPORTER_ASSERT(reporter, check = (canvasPixel == testColor));
261 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000262 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000263 }
264 }
265 }
266 if (cy != DEV_H -1) {
267 const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W);
268 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
269 bool check;
270 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
271 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000272 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000273 }
274 }
275 }
276 canvasPixels += canvasRowBytes;
277 }
278
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000279 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000280}
281
282enum DevType {
283 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000284#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000285 kGpu_BottomLeft_DevType,
286 kGpu_TopLeft_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000287#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000288};
289
290struct CanvasConfig {
291 DevType fDevType;
292 bool fTightRowBytes;
293};
294
295static const CanvasConfig gCanvasConfigs[] = {
296 {kRaster_DevType, true},
297 {kRaster_DevType, false},
reed@google.com8f4d2302013-12-17 16:44:46 +0000298#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000299 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices
300 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000301#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302};
303
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000304#include "SkMallocPixelRef.h"
305
306// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
307// a custom pixelRef (which also has to specify its rowBytes), so we have to be
308// sure that the two rowBytes match (and the infos match).
309//
310static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
311 if (!bm->setConfig(info, rowBytes)) {
312 return false;
313 }
314 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL);
315 bm->setPixelRef(pr)->unref();
316 return true;
317}
318
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000319static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000320 switch (c.fDevType) {
321 case kRaster_DevType: {
322 SkBitmap bmp;
323 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000324 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
325 if (!allocRowBytes(&bmp, info, rowBytes)) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000326 sk_throw();
327 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000328 }
329 // if rowBytes isn't tight then set the padding to a known value
330 if (rowBytes) {
331 SkAutoLockPixels alp(bmp);
332 memset(bmp.getPixels(), DEV_PAD, bmp.getSafeSize());
333 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000334 return new SkBitmapDevice(bmp);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000335 }
336#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000337 case kGpu_BottomLeft_DevType:
338 case kGpu_TopLeft_DevType:
339 GrTextureDesc desc;
340 desc.fFlags = kRenderTarget_GrTextureFlagBit;
341 desc.fWidth = DEV_W;
342 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000343 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000344 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ?
345 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
346 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMatch);
347 SkAutoTUnref<GrTexture> tex(ast.detach());
348 return new SkGpuDevice(grCtx, tex);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000349#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000350 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000351 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000352}
353
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000354static bool setupBitmap(SkBitmap* bitmap,
355 SkCanvas::Config8888 config8888,
356 int w, int h,
357 bool tightRowBytes) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000358 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000359 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
360 if (!allocRowBytes(bitmap, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000361 return false;
362 }
363 SkAutoLockPixels alp(*bitmap);
364 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
365 for (int y = 0; y < h; ++y) {
366 for (int x = 0; x < w; ++x) {
367 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000368 *pixel = getBitmapColor(x, y, w, config8888);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000369 }
370 }
371 return true;
372}
373
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000374DEF_GPUTEST(WritePixels, reporter, factory) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000375 SkCanvas canvas;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000376
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000377 const SkIRect testRects[] = {
378 // entire thing
379 DEV_RECT,
380 // larger on all sides
381 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
382 // fully contained
383 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
384 // outside top left
385 SkIRect::MakeLTRB(-10, -10, -1, -1),
386 // touching top left corner
387 SkIRect::MakeLTRB(-10, -10, 0, 0),
388 // overlapping top left corner
389 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
390 // overlapping top left and top right corners
391 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
392 // touching entire top edge
393 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
394 // overlapping top right corner
395 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
396 // contained in x, overlapping top edge
397 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
398 // outside top right corner
399 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
400 // touching top right corner
401 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
402 // overlapping top left and bottom left corners
403 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
404 // touching entire left edge
405 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
406 // overlapping bottom left corner
407 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
408 // contained in y, overlapping left edge
409 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
410 // outside bottom left corner
411 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
412 // touching bottom left corner
413 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
414 // overlapping bottom left and bottom right corners
415 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
416 // touching entire left edge
417 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
418 // overlapping bottom right corner
419 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
420 // overlapping top right and bottom right corners
421 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
422 };
423
424 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000425 int glCtxTypeCnt = 1;
426#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000427 bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType ||
428 kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType;
429 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000430 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
431 }
432#endif
433 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
434 GrContext* context = NULL;
435#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000436 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000437 GrContextFactory::GLContextType type =
438 static_cast<GrContextFactory::GLContextType>(glCtxType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000439 if (!GrContextFactory::IsRenderingGLContext(type)) {
440 continue;
441 }
442 context = factory->get(type);
443 if (NULL == context) {
444 continue;
445 }
446 }
447#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000448
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000449 SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000450 SkCanvas canvas(device);
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000451
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000452 static const SkCanvas::Config8888 gSrcConfigs[] = {
453 SkCanvas::kNative_Premul_Config8888,
454 SkCanvas::kNative_Unpremul_Config8888,
455 SkCanvas::kBGRA_Premul_Config8888,
456 SkCanvas::kBGRA_Unpremul_Config8888,
457 SkCanvas::kRGBA_Premul_Config8888,
458 SkCanvas::kRGBA_Unpremul_Config8888,
459 };
460 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
461 const SkIRect& rect = testRects[r];
462 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
463 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
464 fillCanvas(&canvas);
465 SkCanvas::Config8888 config8888 = gSrcConfigs[c];
466 SkBitmap bmp;
467 REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp)));
468 uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000469
470 SkColorType ct;
471 SkAlphaType at;
472 sk_tool_utils::config8888_to_imagetypes(config8888, &ct, &at);
473 sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
474
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000475 uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
476 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888));
477
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}