blob: 99e6d11dda94fe401593151856e9a3fb5faf0c07 [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.comd58a1cd2011-11-10 20:57:43 +000016#include "SkGpuDevice.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000017#include "GrContextFactory.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()) {
139 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H);
humper@google.com0e515772013-01-07 19:54:40 +0000140 SkDEBUGCODE(bool alloc = ) bmp.allocPixels();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000141 SkASSERT(alloc);
142 SkAutoLockPixels alp(bmp);
143 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
144 for (int y = 0; y < DEV_H; ++y) {
145 for (int x = 0; x < DEV_W; ++x) {
146 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
147 *pixel = getCanvasColor(x, y);
148 }
149 }
150 }
151 canvas->save();
152 canvas->setMatrix(SkMatrix::I());
153 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
154 SkPaint paint;
155 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
156 canvas->drawBitmap(bmp, 0, 0, &paint);
157 canvas->restore();
158}
159
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000160static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
161 uint32_t color,
162 bool* premul) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000163 const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
164 U8CPU a,r,g,b;
165 *premul = false;
166 switch (config8888) {
167 case SkCanvas::kNative_Premul_Config8888:
168 return color;
169 case SkCanvas::kNative_Unpremul_Config8888:
170 *premul = true;
171 a = SkGetPackedA32(color);
172 r = SkGetPackedR32(color);
173 g = SkGetPackedG32(color);
174 b = SkGetPackedB32(color);
175 break;
176 case SkCanvas::kBGRA_Unpremul_Config8888:
177 *premul = true; // fallthru
178 case SkCanvas::kBGRA_Premul_Config8888:
179 a = static_cast<U8CPU>(c[3]);
180 r = static_cast<U8CPU>(c[2]);
181 g = static_cast<U8CPU>(c[1]);
182 b = static_cast<U8CPU>(c[0]);
183 break;
184 case SkCanvas::kRGBA_Unpremul_Config8888:
185 *premul = true; // fallthru
186 case SkCanvas::kRGBA_Premul_Config8888:
187 a = static_cast<U8CPU>(c[3]);
188 r = static_cast<U8CPU>(c[0]);
189 g = static_cast<U8CPU>(c[1]);
190 b = static_cast<U8CPU>(c[2]);
191 break;
192 default:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000193 SkDEBUGFAIL("Unexpected Config8888");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000194 return 0;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000195 }
196 if (*premul) {
197 r = SkMulDiv255Ceiling(r, a);
198 g = SkMulDiv255Ceiling(g, a);
199 b = SkMulDiv255Ceiling(b, a);
200 }
201 return SkPackARGB32(a, r, g, b);
202}
203
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000204static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000205 if (!didPremulConversion) {
206 return a == b;
207 }
208 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
209 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
210 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
211 int32_t aB = SkGetPackedB32(a);
212
213 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
214 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
215 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
216 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
217
218 return aA == bA &&
219 SkAbs32(aR - bR) <= 1 &&
220 SkAbs32(aG - bG) <= 1 &&
221 SkAbs32(aB - bB) <= 1;
222}
223
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000224static bool checkWrite(skiatest::Reporter* reporter,
225 SkCanvas* canvas,
226 const SkBitmap& bitmap,
227 int writeX, int writeY,
228 SkCanvas::Config8888 config8888) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000229 SkBaseDevice* dev = canvas->getDevice();
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000230 if (!dev) {
231 return false;
232 }
233 SkBitmap devBmp = dev->accessBitmap(false);
234 if (devBmp.width() != DEV_W ||
235 devBmp.height() != DEV_H ||
236 devBmp.config() != SkBitmap::kARGB_8888_Config ||
237 devBmp.isNull()) {
238 return false;
239 }
240 SkAutoLockPixels alp(devBmp);
241
242 intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels());
243 size_t canvasRowBytes = devBmp.rowBytes();
244 SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000245 for (int cy = 0; cy < DEV_H; ++cy) {
246 const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels);
247 for (int cx = 0; cx < DEV_W; ++cx) {
248 SkPMColor canvasPixel = canvasRow[cx];
249 if (writeRect.contains(cx, cy)) {
250 int bx = cx - writeX;
251 int by = cy - writeY;
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000252 uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), config8888);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000253 bool mul;
254 SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul);
255 bool check;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000256 REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul));
257 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000258 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000259 }
260 } else {
261 bool check;
262 SkPMColor testColor = getCanvasColor(cx, cy);
263 REPORTER_ASSERT(reporter, check = (canvasPixel == testColor));
264 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000265 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000266 }
267 }
268 }
269 if (cy != DEV_H -1) {
270 const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W);
271 for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
272 bool check;
273 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
274 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000275 return false;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000276 }
277 }
278 }
279 canvasPixels += canvasRowBytes;
280 }
281
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000282 return true;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000283}
284
285enum DevType {
286 kRaster_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000287#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000288 kGpu_BottomLeft_DevType,
289 kGpu_TopLeft_DevType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000290#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000291};
292
293struct CanvasConfig {
294 DevType fDevType;
295 bool fTightRowBytes;
296};
297
298static const CanvasConfig gCanvasConfigs[] = {
299 {kRaster_DevType, true},
300 {kRaster_DevType, false},
reed@google.com8f4d2302013-12-17 16:44:46 +0000301#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000302 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices
303 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices
bsalomon@google.combbce8b22011-11-10 21:20:37 +0000304#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000305};
306
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000307static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000308 switch (c.fDevType) {
309 case kRaster_DevType: {
310 SkBitmap bmp;
311 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100;
312 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, rowBytes);
313 if (!bmp.allocPixels()) {
reed@google.com44a42ea2012-10-01 17:54:05 +0000314 sk_throw();
315 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000316 }
317 // if rowBytes isn't tight then set the padding to a known value
318 if (rowBytes) {
319 SkAutoLockPixels alp(bmp);
320 memset(bmp.getPixels(), DEV_PAD, bmp.getSafeSize());
321 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000322 return new SkBitmapDevice(bmp);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000323 }
324#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000325 case kGpu_BottomLeft_DevType:
326 case kGpu_TopLeft_DevType:
327 GrTextureDesc desc;
328 desc.fFlags = kRenderTarget_GrTextureFlagBit;
329 desc.fWidth = DEV_W;
330 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000331 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000332 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ?
333 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
334 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMatch);
335 SkAutoTUnref<GrTexture> tex(ast.detach());
336 return new SkGpuDevice(grCtx, tex);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000337#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 }
reed@google.com44a42ea2012-10-01 17:54:05 +0000339 return NULL;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000340}
341
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000342static bool setupBitmap(SkBitmap* bitmap,
343 SkCanvas::Config8888 config8888,
344 int w, int h,
345 bool tightRowBytes) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000346 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60;
347 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes);
348 if (!bitmap->allocPixels()) {
349 return false;
350 }
351 SkAutoLockPixels alp(*bitmap);
352 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
353 for (int y = 0; y < h; ++y) {
354 for (int x = 0; x < w; ++x) {
355 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000356 *pixel = getBitmapColor(x, y, w, config8888);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000357 }
358 }
359 return true;
360}
361
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000362DEF_GPUTEST(WritePixels, reporter, factory) {
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000363 SkCanvas canvas;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000364
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000365 const SkIRect testRects[] = {
366 // entire thing
367 DEV_RECT,
368 // larger on all sides
369 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
370 // fully contained
371 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
372 // outside top left
373 SkIRect::MakeLTRB(-10, -10, -1, -1),
374 // touching top left corner
375 SkIRect::MakeLTRB(-10, -10, 0, 0),
376 // overlapping top left corner
377 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
378 // overlapping top left and top right corners
379 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
380 // touching entire top edge
381 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
382 // overlapping top right corner
383 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
384 // contained in x, overlapping top edge
385 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
386 // outside top right corner
387 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
388 // touching top right corner
389 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
390 // overlapping top left and bottom left corners
391 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
392 // touching entire left edge
393 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
394 // overlapping bottom left corner
395 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
396 // contained in y, overlapping left edge
397 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
398 // outside bottom left corner
399 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
400 // touching bottom left corner
401 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
402 // overlapping bottom left and bottom right corners
403 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
404 // touching entire left edge
405 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
406 // overlapping bottom right corner
407 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
408 // overlapping top right and bottom right corners
409 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
410 };
411
412 for (size_t i = 0; i < SK_ARRAY_COUNT(gCanvasConfigs); ++i) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000413 int glCtxTypeCnt = 1;
414#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000415 bool isGPUDevice = kGpu_TopLeft_DevType == gCanvasConfigs[i].fDevType ||
416 kGpu_BottomLeft_DevType == gCanvasConfigs[i].fDevType;
417 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000418 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
419 }
420#endif
421 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
422 GrContext* context = NULL;
423#if SK_SUPPORT_GPU
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000424 if (isGPUDevice) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000425 GrContextFactory::GLContextType type =
426 static_cast<GrContextFactory::GLContextType>(glCtxType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000427 if (!GrContextFactory::IsRenderingGLContext(type)) {
428 continue;
429 }
430 context = factory->get(type);
431 if (NULL == context) {
432 continue;
433 }
434 }
435#endif
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000436
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000437 SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000438 SkCanvas canvas(device);
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000439
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000440 static const SkCanvas::Config8888 gSrcConfigs[] = {
441 SkCanvas::kNative_Premul_Config8888,
442 SkCanvas::kNative_Unpremul_Config8888,
443 SkCanvas::kBGRA_Premul_Config8888,
444 SkCanvas::kBGRA_Unpremul_Config8888,
445 SkCanvas::kRGBA_Premul_Config8888,
446 SkCanvas::kRGBA_Unpremul_Config8888,
447 };
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) {
452 fillCanvas(&canvas);
453 SkCanvas::Config8888 config8888 = gSrcConfigs[c];
454 SkBitmap bmp;
455 REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp)));
456 uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
457 canvas.writePixels(bmp, rect.fLeft, rect.fTop, config8888);
458 uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
459 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888));
460
461 // we should change the genID iff pixels were actually written.
462 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
463 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
464 bmp.width(), bmp.height());
465 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
466 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
467 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000468 }
469 }
470 }
471 }
472}