blob: e2e4a0c802952f9e6f09cfe6ec772c387e645f7c [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"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000014
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +000016#include "GrContextFactory.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000018#else
19class GrContext;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000020class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000021#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000022
23static const int DEV_W = 100, DEV_H = 100;
24static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000025static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000026 DEV_H * SK_Scalar1);
27static const U8CPU DEV_PAD = 0xee;
28
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000029static SkPMColor getCanvasColor(int x, int y) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000030 SkASSERT(x >= 0 && x < DEV_W);
31 SkASSERT(y >= 0 && y < DEV_H);
32
33 U8CPU r = x;
34 U8CPU g = y;
35 U8CPU b = 0xc;
36
bsalomon@google.com31648eb2011-11-23 15:01:08 +000037 U8CPU a = 0x0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000038 switch ((x+y) % 5) {
39 case 0:
40 a = 0xff;
41 break;
42 case 1:
43 a = 0x80;
44 break;
45 case 2:
46 a = 0xCC;
47 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000048 case 3:
49 a = 0x00;
50 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000051 case 4:
52 a = 0x01;
53 break;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000054 }
55 return SkPremultiplyARGBInline(a, r, g, b);
56}
57
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000058static bool config8888IsPremul(SkCanvas::Config8888 config8888) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000059 switch (config8888) {
60 case SkCanvas::kNative_Premul_Config8888:
61 case SkCanvas::kBGRA_Premul_Config8888:
62 case SkCanvas::kRGBA_Premul_Config8888:
63 return true;
64 case SkCanvas::kNative_Unpremul_Config8888:
65 case SkCanvas::kBGRA_Unpremul_Config8888:
66 case SkCanvas::kRGBA_Unpremul_Config8888:
67 return false;
68 default:
69 SkASSERT(0);
70 return false;
71 }
72}
73
74// assumes any premu/.unpremul has been applied
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000075static uint32_t packConfig8888(SkCanvas::Config8888 config8888,
76 U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +000077 uint32_t r32;
78 uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
79 switch (config8888) {
80 case SkCanvas::kNative_Premul_Config8888:
81 case SkCanvas::kNative_Unpremul_Config8888:
82 r32 = SkPackARGB32NoCheck(a, r, g, b);
83 break;
84 case SkCanvas::kBGRA_Premul_Config8888:
85 case SkCanvas::kBGRA_Unpremul_Config8888:
86 result[0] = b;
87 result[1] = g;
88 result[2] = r;
89 result[3] = a;
90 break;
91 case SkCanvas::kRGBA_Premul_Config8888:
92 case SkCanvas::kRGBA_Unpremul_Config8888:
93 result[0] = r;
94 result[1] = g;
95 result[2] = b;
96 result[3] = a;
97 break;
98 default:
99 SkASSERT(0);
100 return 0;
101 }
102 return r32;
103}
104
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000105static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8888) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000106 int n = y * w + x;
107 U8CPU b = n & 0xff;
108 U8CPU g = (n >> 8) & 0xff;
109 U8CPU r = (n >> 16) & 0xff;
bsalomon@google.com31648eb2011-11-23 15:01:08 +0000110 U8CPU a = 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000111 switch ((x+y) % 5) {
112 case 4:
113 a = 0xff;
114 break;
115 case 3:
116 a = 0x80;
117 break;
118 case 2:
119 a = 0xCC;
120 break;
121 case 1:
122 a = 0x01;
123 break;
124 case 0:
125 a = 0x00;
126 break;
127 }
128 if (config8888IsPremul(config8888)) {
129 r = SkMulDiv255Ceiling(r, a);
130 g = SkMulDiv255Ceiling(g, a);
131 b = SkMulDiv255Ceiling(b, a);
132 }
133 return packConfig8888(config8888, a, r, g , b);
134}
135
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000136static void fillCanvas(SkCanvas* canvas) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000137 static SkBitmap bmp;
138 if (bmp.isNull()) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000139 SkDEBUGCODE(bool alloc = ) bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000140 SkASSERT(alloc);
141 SkAutoLockPixels alp(bmp);
142 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
143 for (int y = 0; y < DEV_H; ++y) {
144 for (int x = 0; x < DEV_W; ++x) {
145 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
146 *pixel = getCanvasColor(x, y);
147 }
148 }
149 }
150 canvas->save();
151 canvas->setMatrix(SkMatrix::I());
152 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
153 SkPaint paint;
154 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
155 canvas->drawBitmap(bmp, 0, 0, &paint);
156 canvas->restore();
157}
158
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000159static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
160 uint32_t color,
161 bool* premul) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000162 const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
163 U8CPU a,r,g,b;
164 *premul = false;
165 switch (config8888) {
166 case SkCanvas::kNative_Premul_Config8888:
167 return color;
168 case SkCanvas::kNative_Unpremul_Config8888:
169 *premul = true;
170 a = SkGetPackedA32(color);
171 r = SkGetPackedR32(color);
172 g = SkGetPackedG32(color);
173 b = SkGetPackedB32(color);
174 break;
175 case SkCanvas::kBGRA_Unpremul_Config8888:
176 *premul = true; // fallthru
177 case SkCanvas::kBGRA_Premul_Config8888:
178 a = static_cast<U8CPU>(c[3]);
179 r = static_cast<U8CPU>(c[2]);
180 g = static_cast<U8CPU>(c[1]);
181 b = static_cast<U8CPU>(c[0]);
182 break;
183 case SkCanvas::kRGBA_Unpremul_Config8888:
184 *premul = true; // fallthru
185 case SkCanvas::kRGBA_Premul_Config8888:
186 a = static_cast<U8CPU>(c[3]);
187 r = static_cast<U8CPU>(c[0]);
188 g = static_cast<U8CPU>(c[1]);
189 b = static_cast<U8CPU>(c[2]);
190 break;
191 default:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000192 SkDEBUGFAIL("Unexpected Config8888");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000193 return 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000194 }
195 if (*premul) {
196 r = SkMulDiv255Ceiling(r, a);
197 g = SkMulDiv255Ceiling(g, a);
198 b = SkMulDiv255Ceiling(b, a);
199 }
200 return SkPackARGB32(a, r, g, b);
201}
202
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000203static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000204 if (!didPremulConversion) {
205 return a == b;
206 }
207 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
208 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
209 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
210 int32_t aB = SkGetPackedB32(a);
211
212 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
213 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
214 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
215 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
216
217 return aA == bA &&
218 SkAbs32(aR - bR) <= 1 &&
219 SkAbs32(aG - bG) <= 1 &&
220 SkAbs32(aB - bB) <= 1;
221}
222
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000223static bool checkWrite(skiatest::Reporter* reporter,
224 SkCanvas* canvas,
225 const SkBitmap& bitmap,
226 int writeX, int writeY,
227 SkCanvas::Config8888 config8888) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000228 SkBaseDevice* dev = canvas->getDevice();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000229 if (!dev) {
230 return false;
231 }
232 SkBitmap devBmp = dev->accessBitmap(false);
233 if (devBmp.width() != DEV_W ||
234 devBmp.height() != DEV_H ||
235 devBmp.config() != SkBitmap::kARGB_8888_Config ||
236 devBmp.isNull()) {
237 return false;
238 }
239 SkAutoLockPixels alp(devBmp);
240
241 intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels());
242 size_t canvasRowBytes = devBmp.rowBytes();
243 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000244 for (int cy = 0; cy < DEV_H; ++cy) {
245 const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels);
246 for (int cx = 0; cx < DEV_W; ++cx) {
247 SkPMColor canvasPixel = canvasRow[cx];
248 if (writeRect.contains(cx, cy)) {
249 int bx = cx - writeX;
250 int by = cy - writeY;
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000251 uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), config8888);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000252 bool mul;
253 SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul);
254 bool check;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000255 REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul));
256 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000257 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000258 }
259 } else {
260 bool check;
261 SkPMColor testColor = getCanvasColor(cx, cy);
262 REPORTER_ASSERT(reporter, check = (canvasPixel == testColor));
263 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000264 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000265 }
266 }
267 }
268 if (cy != DEV_H -1) {
269 const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W);
270 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
271 bool check;
272 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
273 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000274 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000275 }
276 }
277 }
278 canvasPixels += canvasRowBytes;
279 }
280
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000281 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000282}
283
284enum DevType {
285 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000286#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000287 kGpu_BottomLeft_DevType,
288 kGpu_TopLeft_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000289#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000290};
291
292struct CanvasConfig {
293 DevType fDevType;
294 bool fTightRowBytes;
295};
296
297static const CanvasConfig gCanvasConfigs[] = {
298 {kRaster_DevType, true},
299 {kRaster_DevType, false},
reed@google.com8f4d2302013-12-17 16:44:46 +0000300#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000301 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices
302 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000303#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000304};
305
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000306#include "SkMallocPixelRef.h"
307
308// This is a tricky pattern, because we have to setConfig+rowBytes AND specify
309// a custom pixelRef (which also has to specify its rowBytes), so we have to be
310// sure that the two rowBytes match (and the infos match).
311//
312static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
313 if (!bm->setConfig(info, rowBytes)) {
314 return false;
315 }
316 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL);
317 bm->setPixelRef(pr)->unref();
318 return true;
319}
320
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000321static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000322 switch (c.fDevType) {
323 case kRaster_DevType: {
324 SkBitmap bmp;
325 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000326 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
327 if (!allocRowBytes(&bmp, info, rowBytes)) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000328 sk_throw();
329 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000330 }
331 // if rowBytes isn't tight then set the padding to a known value
332 if (rowBytes) {
333 SkAutoLockPixels alp(bmp);
334 memset(bmp.getPixels(), DEV_PAD, bmp.getSafeSize());
335 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000336 return new SkBitmapDevice(bmp);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000337 }
338#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000339 case kGpu_BottomLeft_DevType:
340 case kGpu_TopLeft_DevType:
341 GrTextureDesc desc;
342 desc.fFlags = kRenderTarget_GrTextureFlagBit;
343 desc.fWidth = DEV_W;
344 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000345 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000346 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ?
347 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
348 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMatch);
349 SkAutoTUnref<GrTexture> tex(ast.detach());
350 return new SkGpuDevice(grCtx, tex);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000351#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000352 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000353 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000354}
355
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000356static bool setupBitmap(SkBitmap* bitmap,
357 SkCanvas::Config8888 config8888,
358 int w, int h,
359 bool tightRowBytes) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000360 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000361 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
362 if (!allocRowBytes(bitmap, info, rowBytes)) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000363 return false;
364 }
365 SkAutoLockPixels alp(*bitmap);
366 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
367 for (int y = 0; y < h; ++y) {
368 for (int x = 0; x < w; ++x) {
369 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000370 *pixel = getBitmapColor(x, y, w, config8888);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000371 }
372 }
373 return true;
374}
375
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000376DEF_GPUTEST(WritePixels, reporter, factory) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000377 SkCanvas canvas;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000378
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000379 const SkIRect testRects[] = {
380 // entire thing
381 DEV_RECT,
382 // larger on all sides
383 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
384 // fully contained
385 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
386 // outside top left
387 SkIRect::MakeLTRB(-10, -10, -1, -1),
388 // touching top left corner
389 SkIRect::MakeLTRB(-10, -10, 0, 0),
390 // overlapping top left corner
391 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
392 // overlapping top left and top right corners
393 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
394 // touching entire top edge
395 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
396 // overlapping top right corner
397 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
398 // contained in x, overlapping top edge
399 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
400 // outside top right corner
401 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
402 // touching top right corner
403 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
404 // overlapping top left and bottom left corners
405 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
406 // touching entire left edge
407 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
408 // overlapping bottom left corner
409 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
410 // contained in y, overlapping left edge
411 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
412 // outside bottom left corner
413 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
414 // touching bottom left corner
415 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
416 // overlapping bottom left and bottom right corners
417 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
418 // touching entire left edge
419 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
420 // overlapping bottom right corner
421 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
422 // overlapping top right and bottom right corners
423 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
424 };
425
426 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000427 int glCtxTypeCnt = 1;
428#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000429 bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType ||
430 kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType;
431 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000432 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
433 }
434#endif
435 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
436 GrContext* context = NULL;
437#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000438 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000439 GrContextFactory::GLContextType type =
440 static_cast<GrContextFactory::GLContextType>(glCtxType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000441 if (!GrContextFactory::IsRenderingGLContext(type)) {
442 continue;
443 }
444 context = factory->get(type);
445 if (NULL == context) {
446 continue;
447 }
448 }
449#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000450
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000451 SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000452 SkCanvas canvas(device);
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000453
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000454 static const SkCanvas::Config8888 gSrcConfigs[] = {
455 SkCanvas::kNative_Premul_Config8888,
456 SkCanvas::kNative_Unpremul_Config8888,
457 SkCanvas::kBGRA_Premul_Config8888,
458 SkCanvas::kBGRA_Unpremul_Config8888,
459 SkCanvas::kRGBA_Premul_Config8888,
460 SkCanvas::kRGBA_Unpremul_Config8888,
461 };
462 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
463 const SkIRect& rect = testRects[r];
464 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
465 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
466 fillCanvas(&canvas);
467 SkCanvas::Config8888 config8888 = gSrcConfigs[c];
468 SkBitmap bmp;
469 REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp)));
470 uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
471 canvas.writePixels(bmp, rect.fLeft, rect.fTop, config8888);
472 uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
473 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888));
474
475 // we should change the genID iff pixels were actually written.
476 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
477 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
478 bmp.width(), bmp.height());
479 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
480 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
481 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000482 }
483 }
484 }
485 }
486}