blob: 41ecccff353f5b952da1f2b7497b7a8ab5fb246a [file] [log] [blame]
bsalomon@google.comc6980972011-11-02 19:57:21 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "Test.h"
10#include "SkCanvas.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000011#include "SkColorPriv.h"
12#include "SkDevice.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000013#include "SkMathPriv.h"
bsalomon@google.comc6980972011-11-02 19:57:21 +000014#include "SkRegion.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015#if SK_SUPPORT_GPU
bsalomon@google.comc6980972011-11-02 19:57:21 +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#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000019
20
21static const int DEV_W = 100, DEV_H = 100;
22static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000023static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000024 DEV_H * SK_Scalar1);
25
26namespace {
27SkPMColor getCanvasColor(int x, int y) {
28 SkASSERT(x >= 0 && x < DEV_W);
29 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000030
31 U8CPU r = x;
32 U8CPU g = y;
33 U8CPU b = 0xc;
34
35 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000036 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000037 case 0:
38 a = 0xff;
39 break;
40 case 1:
41 a = 0x80;
42 break;
43 case 2:
44 a = 0xCC;
45 break;
46 case 4:
47 a = 0x01;
48 break;
49 case 3:
50 a = 0x00;
51 break;
52 }
53 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000054}
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
bsalomon@google.comc6980972011-11-02 19:57:21 +000056SkPMColor getBitmapColor(int x, int y, int w, int h) {
57 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000058
bsalomon@google.comc6980972011-11-02 19:57:21 +000059 U8CPU b = n & 0xff;
60 U8CPU g = (n >> 8) & 0xff;
61 U8CPU r = (n >> 16) & 0xff;
62 return SkPackARGB32(0xff, r, g , b);
63}
64
bsalomon@google.com6850eab2011-11-03 20:29:47 +000065SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
bsalomon@google.comc4364992011-11-07 15:54:49 +000066 uint32_t color,
67 bool* premul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000068 const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
69 U8CPU a,r,g,b;
bsalomon@google.comc4364992011-11-07 15:54:49 +000070 *premul = false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000071 switch (config8888) {
72 case SkCanvas::kNative_Premul_Config8888:
73 return color;
74 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000075 *premul = true;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000076 a = SkGetPackedA32(color);
77 r = SkGetPackedR32(color);
78 g = SkGetPackedG32(color);
79 b = SkGetPackedB32(color);
80 break;
81 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000082 *premul = true; // fallthru
bsalomon@google.com6850eab2011-11-03 20:29:47 +000083 case SkCanvas::kBGRA_Premul_Config8888:
84 a = static_cast<U8CPU>(c[3]);
85 r = static_cast<U8CPU>(c[2]);
86 g = static_cast<U8CPU>(c[1]);
87 b = static_cast<U8CPU>(c[0]);
88 break;
89 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000090 *premul = true; // fallthru
bsalomon@google.com6850eab2011-11-03 20:29:47 +000091 case SkCanvas::kRGBA_Premul_Config8888:
92 a = static_cast<U8CPU>(c[3]);
93 r = static_cast<U8CPU>(c[0]);
94 g = static_cast<U8CPU>(c[1]);
95 b = static_cast<U8CPU>(c[2]);
96 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000097 default:
98 SkDEBUGFAIL("Unexpected Config8888");
99 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000100 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000101 if (*premul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000102 r = SkMulDiv255Ceiling(r, a);
103 g = SkMulDiv255Ceiling(g, a);
104 b = SkMulDiv255Ceiling(b, a);
105 }
106 return SkPackARGB32(a, r, g, b);
107}
108
bsalomon@google.comc6980972011-11-02 19:57:21 +0000109void fillCanvas(SkCanvas* canvas) {
110 static SkBitmap bmp;
111 if (bmp.isNull()) {
112 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H);
reed@google.com44a42ea2012-10-01 17:54:05 +0000113 SkDEBUGCODE(bool alloc =) bmp.allocPixels();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000114 SkASSERT(alloc);
115 SkAutoLockPixels alp(bmp);
116 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
117 for (int y = 0; y < DEV_H; ++y) {
118 for (int x = 0; x < DEV_W; ++x) {
119 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
120 *pixel = getCanvasColor(x, y);
121 }
122 }
123 }
124 canvas->save();
125 canvas->setMatrix(SkMatrix::I());
126 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
127 SkPaint paint;
128 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
129 canvas->drawBitmap(bmp, 0, 0, &paint);
130 canvas->restore();
131}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
bsalomon@google.comc6980972011-11-02 19:57:21 +0000133void fillBitmap(SkBitmap* bitmap) {
134 SkASSERT(bitmap->lockPixelsAreWritable());
135 SkAutoLockPixels alp(*bitmap);
136 int w = bitmap->width();
137 int h = bitmap->height();
138 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
139 for (int y = 0; y < h; ++y) {
140 for (int x = 0; x < w; ++x) {
141 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
142 *pixel = getBitmapColor(x, y, w, h);
143 }
144 }
145}
146
bsalomon@google.comc4364992011-11-07 15:54:49 +0000147bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
148 if (!didPremulConversion) {
149 return a == b;
150 }
151 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
152 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
153 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
154 int32_t aB = SkGetPackedB32(a);
155
156 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
157 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
158 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
159 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
160
161 return aA == bA &&
162 SkAbs32(aR - bR) <= 1 &&
163 SkAbs32(aG - bG) <= 1 &&
164 SkAbs32(aB - bB) <= 1;
165}
166
bsalomon@google.comc6980972011-11-02 19:57:21 +0000167// checks the bitmap contains correct pixels after the readPixels
168// if the bitmap was prefilled with pixels it checks that these weren't
169// overwritten in the area outside the readPixels.
170bool checkRead(skiatest::Reporter* reporter,
171 const SkBitmap& bitmap,
172 int x, int y,
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000173 bool checkCanvasPixels,
174 bool checkBitmapPixels,
175 SkCanvas::Config8888 config8888) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000176 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
177 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000178 SkASSERT(checkCanvasPixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000179
bsalomon@google.comc6980972011-11-02 19:57:21 +0000180 int bw = bitmap.width();
181 int bh = bitmap.height();
182
183 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
184 SkIRect clippedSrcRect = DEV_RECT;
185 if (!clippedSrcRect.intersect(srcRect)) {
186 clippedSrcRect.setEmpty();
187 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000188 SkAutoLockPixels alp(bitmap);
189 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap.getPixels());
190 for (int by = 0; by < bh; ++by) {
191 for (int bx = 0; bx < bw; ++bx) {
192 int devx = bx + srcRect.fLeft;
193 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
bsalomon@google.comc4364992011-11-07 15:54:49 +0000195 uint32_t pixel = *reinterpret_cast<SkPMColor*>(pixels + by * bitmap.rowBytes() + bx * bitmap.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000196
197 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000198 if (checkCanvasPixels) {
199 SkPMColor canvasPixel = getCanvasColor(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000200 bool didPremul;
201 SkPMColor pmPixel = convertConfig8888ToPMColor(config8888, pixel, &didPremul);
202 bool check;
203 REPORTER_ASSERT(reporter, check = checkPixel(pmPixel, canvasPixel, didPremul));
204 if (!check) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000205 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000206 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000207 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000208 } else if (checkBitmapPixels) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000209 REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw, bh) == pixel);
210 if (getBitmapColor(bx, by, bw, bh) != pixel) {
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000211 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000212 }
213 }
214 }
215 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000216 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000217}
218
219enum BitmapInit {
220 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
bsalomon@google.comc6980972011-11-02 19:57:21 +0000222 kNoPixels_BitmapInit = kFirstBitmapInit,
223 kTight_BitmapInit,
224 kRowBytes_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
bsalomon@google.comc6980972011-11-02 19:57:21 +0000226 kBitmapInitCnt
227};
228
229BitmapInit nextBMI(BitmapInit bmi) {
230 int x = bmi;
231 return static_cast<BitmapInit>(++x);
232}
233
234
235void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init) {
236 int w = rect.width();
237 int h = rect.height();
238 int rowBytes = 0;
239 bool alloc = true;
240 switch (init) {
241 case kNoPixels_BitmapInit:
242 alloc = false;
243 case kTight_BitmapInit:
244 break;
245 case kRowBytes_BitmapInit:
246 rowBytes = w * sizeof(SkPMColor) + 16 * sizeof(SkPMColor);
247 break;
248 default:
249 SkASSERT(0);
250 break;
251 }
252 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes);
253 if (alloc) {
254 bitmap->allocPixels();
255 }
256}
257
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000258void ReadPixelsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000259 const SkIRect testRects[] = {
260 // entire thing
261 DEV_RECT,
262 // larger on all sides
263 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
264 // fully contained
265 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
266 // outside top left
267 SkIRect::MakeLTRB(-10, -10, -1, -1),
268 // touching top left corner
269 SkIRect::MakeLTRB(-10, -10, 0, 0),
270 // overlapping top left corner
271 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
272 // overlapping top left and top right corners
273 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
274 // touching entire top edge
275 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
276 // overlapping top right corner
277 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
278 // contained in x, overlapping top edge
279 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
280 // outside top right corner
281 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
282 // touching top right corner
283 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
284 // overlapping top left and bottom left corners
285 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
286 // touching entire left edge
287 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
288 // overlapping bottom left corner
289 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
290 // contained in y, overlapping left edge
291 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
292 // outside bottom left corner
293 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
294 // touching bottom left corner
295 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
296 // overlapping bottom left and bottom right corners
297 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
298 // touching entire left edge
299 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
300 // overlapping bottom right corner
301 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
302 // overlapping top right and bottom right corners
303 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
304 };
305
bsalomon@google.comb9f026f2012-02-09 13:53:48 +0000306 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000307 int glCtxTypeCnt = 1;
308#if SK_SUPPORT_GPU
309 if (0 != dtype) {
310 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000311 }
djsollen@google.com8688e5b2012-01-09 13:02:20 +0000312#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000313 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
314 SkAutoTUnref<SkDevice> device;
315 if (0 == dtype) {
316 device.reset(new SkDevice(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, false));
317 } else {
318#if SK_SUPPORT_GPU
319 GrContextFactory::GLContextType type =
320 static_cast<GrContextFactory::GLContextType>(glCtxType);
321 if (!GrContextFactory::IsRenderingGLContext(type)) {
322 continue;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000323 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000324 GrContext* context = factory->get(type);
325 if (NULL == context) {
326 continue;
327 }
328 device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, DEV_W, DEV_H));
329#else
330 continue;
331#endif
332 }
333 SkCanvas canvas(device);
334 fillCanvas(&canvas);
335
336 static const SkCanvas::Config8888 gReadConfigs[] = {
337 SkCanvas::kNative_Premul_Config8888,
338 SkCanvas::kNative_Unpremul_Config8888,
339/**
340 * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438)
341 * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to
342 * fail. This should be removed as soon as the issue above is resolved.
343 */
344#if !defined(SK_BUILD_FOR_ANDROID)
345 SkCanvas::kBGRA_Premul_Config8888,
346 SkCanvas::kBGRA_Unpremul_Config8888,
347#endif
348 SkCanvas::kRGBA_Premul_Config8888,
349 SkCanvas::kRGBA_Unpremul_Config8888,
350 };
351 for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
352 const SkIRect& srcRect = testRects[rect];
353 for (BitmapInit bmi = kFirstBitmapInit;
354 bmi < kBitmapInitCnt;
355 bmi = nextBMI(bmi)) {
356 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
357 SkCanvas::Config8888 config8888 = gReadConfigs[c];
358 SkBitmap bmp;
359 init_bitmap(&bmp, srcRect, bmi);
360
361 // if the bitmap has pixels allocated before the readPixels,
362 // note that and fill them with pattern
363 bool startsWithPixels = !bmp.isNull();
364 if (startsWithPixels) {
365 fillBitmap(&bmp);
366 }
367 uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
368 bool success =
369 canvas.readPixels(&bmp, srcRect.fLeft,
370 srcRect.fTop, config8888);
371 uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
372
373 // we expect to succeed when the read isn't fully clipped
374 // out.
375 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
376 // determine whether we expected the read to succeed.
377 REPORTER_ASSERT(reporter, success == expectSuccess);
378 // read pixels should never change the gen id
379 REPORTER_ASSERT(reporter, idBefore == idAfter);
380
381 if (success || startsWithPixels) {
382 checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop,
383 success, startsWithPixels, config8888);
384 } else {
385 // if we had no pixels beforehand and the readPixels
386 // failed then our bitmap should still not have pixels
387 REPORTER_ASSERT(reporter, bmp.isNull());
388 }
389 }
390 // check the old webkit version of readPixels that clips the
391 // bitmap size
392 SkBitmap wkbmp;
393 bool success = canvas.readPixels(srcRect, &wkbmp);
394 SkIRect clippedRect = DEV_RECT;
395 if (clippedRect.intersect(srcRect)) {
396 REPORTER_ASSERT(reporter, success);
397 checkRead(reporter, wkbmp, clippedRect.fLeft,
398 clippedRect.fTop, true, false,
399 SkCanvas::kNative_Premul_Config8888);
400 } else {
401 REPORTER_ASSERT(reporter, !success);
402 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000403 }
404 }
405 }
406 }
407}
408}
409
410#include "TestClassDef.h"
411DEFINE_GPUTESTCLASS("ReadPixels", ReadPixelsTestClass, ReadPixelsTest)