blob: a0eee04e5a1a241983f8e6473a8e16a1aadac911 [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.comcf8fb1f2012-08-02 14:03:32 +000017#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000018
19
20static const int DEV_W = 100, DEV_H = 100;
21static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
22static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
23 DEV_H * SK_Scalar1);
24
25namespace {
26SkPMColor getCanvasColor(int x, int y) {
27 SkASSERT(x >= 0 && x < DEV_W);
28 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000029
30 U8CPU r = x;
31 U8CPU g = y;
32 U8CPU b = 0xc;
33
34 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000035 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000036 case 0:
37 a = 0xff;
38 break;
39 case 1:
40 a = 0x80;
41 break;
42 case 2:
43 a = 0xCC;
44 break;
45 case 4:
46 a = 0x01;
47 break;
48 case 3:
49 a = 0x00;
50 break;
51 }
52 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000053}
54
55SkPMColor getBitmapColor(int x, int y, int w, int h) {
56 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000057
bsalomon@google.comc6980972011-11-02 19:57:21 +000058 U8CPU b = n & 0xff;
59 U8CPU g = (n >> 8) & 0xff;
60 U8CPU r = (n >> 16) & 0xff;
61 return SkPackARGB32(0xff, r, g , b);
62}
63
bsalomon@google.com6850eab2011-11-03 20:29:47 +000064SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
bsalomon@google.comc4364992011-11-07 15:54:49 +000065 uint32_t color,
66 bool* premul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000067 const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
68 U8CPU a,r,g,b;
bsalomon@google.comc4364992011-11-07 15:54:49 +000069 *premul = false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000070 switch (config8888) {
71 case SkCanvas::kNative_Premul_Config8888:
72 return color;
73 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000074 *premul = true;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000075 a = SkGetPackedA32(color);
76 r = SkGetPackedR32(color);
77 g = SkGetPackedG32(color);
78 b = SkGetPackedB32(color);
79 break;
80 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000081 *premul = true; // fallthru
bsalomon@google.com6850eab2011-11-03 20:29:47 +000082 case SkCanvas::kBGRA_Premul_Config8888:
83 a = static_cast<U8CPU>(c[3]);
84 r = static_cast<U8CPU>(c[2]);
85 g = static_cast<U8CPU>(c[1]);
86 b = static_cast<U8CPU>(c[0]);
87 break;
88 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000089 *premul = true; // fallthru
bsalomon@google.com6850eab2011-11-03 20:29:47 +000090 case SkCanvas::kRGBA_Premul_Config8888:
91 a = static_cast<U8CPU>(c[3]);
92 r = static_cast<U8CPU>(c[0]);
93 g = static_cast<U8CPU>(c[1]);
94 b = static_cast<U8CPU>(c[2]);
95 break;
96 }
bsalomon@google.comc4364992011-11-07 15:54:49 +000097 if (*premul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000098 r = SkMulDiv255Ceiling(r, a);
99 g = SkMulDiv255Ceiling(g, a);
100 b = SkMulDiv255Ceiling(b, a);
101 }
102 return SkPackARGB32(a, r, g, b);
103}
104
bsalomon@google.comc6980972011-11-02 19:57:21 +0000105void fillCanvas(SkCanvas* canvas) {
106 static SkBitmap bmp;
107 if (bmp.isNull()) {
108 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H);
109 bool alloc = bmp.allocPixels();
110 SkASSERT(alloc);
111 SkAutoLockPixels alp(bmp);
112 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
113 for (int y = 0; y < DEV_H; ++y) {
114 for (int x = 0; x < DEV_W; ++x) {
115 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
116 *pixel = getCanvasColor(x, y);
117 }
118 }
119 }
120 canvas->save();
121 canvas->setMatrix(SkMatrix::I());
122 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
123 SkPaint paint;
124 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
125 canvas->drawBitmap(bmp, 0, 0, &paint);
126 canvas->restore();
127}
128
129void fillBitmap(SkBitmap* bitmap) {
130 SkASSERT(bitmap->lockPixelsAreWritable());
131 SkAutoLockPixels alp(*bitmap);
132 int w = bitmap->width();
133 int h = bitmap->height();
134 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
135 for (int y = 0; y < h; ++y) {
136 for (int x = 0; x < w; ++x) {
137 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
138 *pixel = getBitmapColor(x, y, w, h);
139 }
140 }
141}
142
bsalomon@google.comc4364992011-11-07 15:54:49 +0000143bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
144 if (!didPremulConversion) {
145 return a == b;
146 }
147 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
148 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
149 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
150 int32_t aB = SkGetPackedB32(a);
151
152 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
153 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
154 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
155 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
156
157 return aA == bA &&
158 SkAbs32(aR - bR) <= 1 &&
159 SkAbs32(aG - bG) <= 1 &&
160 SkAbs32(aB - bB) <= 1;
161}
162
bsalomon@google.comc6980972011-11-02 19:57:21 +0000163// checks the bitmap contains correct pixels after the readPixels
164// if the bitmap was prefilled with pixels it checks that these weren't
165// overwritten in the area outside the readPixels.
166bool checkRead(skiatest::Reporter* reporter,
167 const SkBitmap& bitmap,
168 int x, int y,
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000169 bool checkCanvasPixels,
170 bool checkBitmapPixels,
171 SkCanvas::Config8888 config8888) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000172 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
173 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000174 SkASSERT(checkCanvasPixels || checkBitmapPixels);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000175
176 int bw = bitmap.width();
177 int bh = bitmap.height();
178
179 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
180 SkIRect clippedSrcRect = DEV_RECT;
181 if (!clippedSrcRect.intersect(srcRect)) {
182 clippedSrcRect.setEmpty();
183 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000184 bool failed = false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000185 SkAutoLockPixels alp(bitmap);
186 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap.getPixels());
187 for (int by = 0; by < bh; ++by) {
188 for (int bx = 0; bx < bw; ++bx) {
189 int devx = bx + srcRect.fLeft;
190 int devy = by + srcRect.fTop;
191
bsalomon@google.comc4364992011-11-07 15:54:49 +0000192 uint32_t pixel = *reinterpret_cast<SkPMColor*>(pixels + by * bitmap.rowBytes() + bx * bitmap.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000193
194 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000195 if (checkCanvasPixels) {
196 SkPMColor canvasPixel = getCanvasColor(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000197 bool didPremul;
198 SkPMColor pmPixel = convertConfig8888ToPMColor(config8888, pixel, &didPremul);
199 bool check;
200 REPORTER_ASSERT(reporter, check = checkPixel(pmPixel, canvasPixel, didPremul));
201 if (!check) {
202 failed = true;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000203 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000204 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000205 } else if (checkBitmapPixels) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000206 REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw, bh) == pixel);
207 if (getBitmapColor(bx, by, bw, bh) != pixel) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000208 failed = true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000209 }
210 }
211 }
212 }
bsalomon@google.coma2092aa2011-11-10 15:10:24 +0000213 return !failed;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000214}
215
216enum BitmapInit {
217 kFirstBitmapInit = 0,
218
219 kNoPixels_BitmapInit = kFirstBitmapInit,
220 kTight_BitmapInit,
221 kRowBytes_BitmapInit,
222
223 kBitmapInitCnt
224};
225
226BitmapInit nextBMI(BitmapInit bmi) {
227 int x = bmi;
228 return static_cast<BitmapInit>(++x);
229}
230
231
232void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init) {
233 int w = rect.width();
234 int h = rect.height();
235 int rowBytes = 0;
236 bool alloc = true;
237 switch (init) {
238 case kNoPixels_BitmapInit:
239 alloc = false;
240 case kTight_BitmapInit:
241 break;
242 case kRowBytes_BitmapInit:
243 rowBytes = w * sizeof(SkPMColor) + 16 * sizeof(SkPMColor);
244 break;
245 default:
246 SkASSERT(0);
247 break;
248 }
249 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes);
250 if (alloc) {
251 bitmap->allocPixels();
252 }
253}
254
255void ReadPixelsTest(skiatest::Reporter* reporter, GrContext* context) {
256 SkCanvas canvas;
257
258 const SkIRect testRects[] = {
259 // entire thing
260 DEV_RECT,
261 // larger on all sides
262 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
263 // fully contained
264 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
265 // outside top left
266 SkIRect::MakeLTRB(-10, -10, -1, -1),
267 // touching top left corner
268 SkIRect::MakeLTRB(-10, -10, 0, 0),
269 // overlapping top left corner
270 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
271 // overlapping top left and top right corners
272 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
273 // touching entire top edge
274 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
275 // overlapping top right corner
276 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
277 // contained in x, overlapping top edge
278 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
279 // outside top right corner
280 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
281 // touching top right corner
282 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
283 // overlapping top left and bottom left corners
284 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
285 // touching entire left edge
286 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
287 // overlapping bottom left corner
288 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
289 // contained in y, overlapping left edge
290 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
291 // outside bottom left corner
292 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
293 // touching bottom left corner
294 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
295 // overlapping bottom left and bottom right corners
296 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
297 // touching entire left edge
298 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
299 // overlapping bottom right corner
300 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
301 // overlapping top right and bottom right corners
302 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
303 };
304
bsalomon@google.comb9f026f2012-02-09 13:53:48 +0000305 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000306
307 if (0 == dtype) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000308 canvas.setDevice(new SkDevice(SkBitmap::kARGB_8888_Config,
309 DEV_W,
310 DEV_H,
311 false))->unref();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000312 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000313// GPU device known not to work in the fixed pt build.
314#if defined(SK_SCALAR_IS_FIXED) || !SK_SUPPORT_GPU
bsalomon@google.comc6980972011-11-02 19:57:21 +0000315 continue;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000316#else
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000317 canvas.setDevice(new SkGpuDevice(context,
318 SkBitmap::kARGB_8888_Config,
319 DEV_W,
320 DEV_H))->unref();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000321#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000322 }
323 fillCanvas(&canvas);
324
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000325 static const SkCanvas::Config8888 gReadConfigs[] = {
326 SkCanvas::kNative_Premul_Config8888,
327 SkCanvas::kNative_Unpremul_Config8888,
djsollen@google.com8688e5b2012-01-09 13:02:20 +0000328/**
329 * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438)
330 * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to
331 * fail. This should be removed as soon as the issue above is resolved.
332 */
333#if !defined(SK_BUILD_FOR_ANDROID)
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000334 SkCanvas::kBGRA_Premul_Config8888,
335 SkCanvas::kBGRA_Unpremul_Config8888,
djsollen@google.com8688e5b2012-01-09 13:02:20 +0000336#endif
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000337 SkCanvas::kRGBA_Premul_Config8888,
338 SkCanvas::kRGBA_Unpremul_Config8888,
339 };
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000340 for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000341 const SkIRect& srcRect = testRects[rect];
342 for (BitmapInit bmi = kFirstBitmapInit;
343 bmi < kBitmapInitCnt;
344 bmi = nextBMI(bmi)) {
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000345 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000346 SkCanvas::Config8888 config8888 = gReadConfigs[c];
347 SkBitmap bmp;
348 init_bitmap(&bmp, srcRect, bmi);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000349
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000350 // if the bitmap has pixels allocated before the readPixels,
351 // note that and fill them with pattern
352 bool startsWithPixels = !bmp.isNull();
353 if (startsWithPixels) {
354 fillBitmap(&bmp);
355 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000356
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000357 bool success =
358 canvas.readPixels(&bmp, srcRect.fLeft,
359 srcRect.fTop, config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000360
bsalomon@google.comc4364992011-11-07 15:54:49 +0000361 // we expect to succeed when the read isn't fully clipped
362 // out.
363 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000364 // determine whether we expected the read to succeed.
365 REPORTER_ASSERT(reporter, success == expectSuccess);
366
367 if (success || startsWithPixels) {
368 checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop,
369 success, startsWithPixels, config8888);
370 } else {
371 // if we had no pixels beforehand and the readPixels
372 // failed then our bitmap should still not have pixels
373 REPORTER_ASSERT(reporter, bmp.isNull());
374 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000375 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000376 // check the old webkit version of readPixels that clips the
377 // bitmap size
bsalomon@google.comc6980972011-11-02 19:57:21 +0000378 SkBitmap wkbmp;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000379 bool success = canvas.readPixels(srcRect, &wkbmp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000380 SkIRect clippedRect = DEV_RECT;
381 if (clippedRect.intersect(srcRect)) {
382 REPORTER_ASSERT(reporter, success);
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000383 checkRead(reporter, wkbmp, clippedRect.fLeft,
384 clippedRect.fTop, true, false,
385 SkCanvas::kNative_Premul_Config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000386 } else {
387 REPORTER_ASSERT(reporter, !success);
388 }
389 }
390 }
391 }
392}
393}
394
395#include "TestClassDef.h"
396DEFINE_GPUTESTCLASS("ReadPixels", ReadPixelsTestClass, ReadPixelsTest)
397