blob: 730be051773d2e547097e56cae3e34bde984dd9d [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"
bsalomon@google.comc6980972011-11-02 19:57:21 +000013#include "SkRegion.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000014#if SK_SUPPORT_GPU
bsalomon@google.comc6980972011-11-02 19:57:21 +000015#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000016#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000017
18
19static const int DEV_W = 100, DEV_H = 100;
20static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
21static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
22 DEV_H * SK_Scalar1);
23
24namespace {
25SkPMColor getCanvasColor(int x, int y) {
26 SkASSERT(x >= 0 && x < DEV_W);
27 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000028
29 U8CPU r = x;
30 U8CPU g = y;
31 U8CPU b = 0xc;
32
33 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000034 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000035 case 0:
36 a = 0xff;
37 break;
38 case 1:
39 a = 0x80;
40 break;
41 case 2:
42 a = 0xCC;
43 break;
44 case 4:
45 a = 0x01;
46 break;
47 case 3:
48 a = 0x00;
49 break;
50 }
51 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000052}
53
54SkPMColor getBitmapColor(int x, int y, int w, int h) {
55 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000056
bsalomon@google.comc6980972011-11-02 19:57:21 +000057 U8CPU b = n & 0xff;
58 U8CPU g = (n >> 8) & 0xff;
59 U8CPU r = (n >> 16) & 0xff;
60 return SkPackARGB32(0xff, r, g , b);
61}
62
bsalomon@google.com6850eab2011-11-03 20:29:47 +000063SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
bsalomon@google.comc4364992011-11-07 15:54:49 +000064 uint32_t color,
65 bool* premul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000066 const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
67 U8CPU a,r,g,b;
bsalomon@google.comc4364992011-11-07 15:54:49 +000068 *premul = false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000069 switch (config8888) {
70 case SkCanvas::kNative_Premul_Config8888:
71 return color;
72 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000073 *premul = true;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000074 a = SkGetPackedA32(color);
75 r = SkGetPackedR32(color);
76 g = SkGetPackedG32(color);
77 b = SkGetPackedB32(color);
78 break;
79 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000080 *premul = true; // fallthru
bsalomon@google.com6850eab2011-11-03 20:29:47 +000081 case SkCanvas::kBGRA_Premul_Config8888:
82 a = static_cast<U8CPU>(c[3]);
83 r = static_cast<U8CPU>(c[2]);
84 g = static_cast<U8CPU>(c[1]);
85 b = static_cast<U8CPU>(c[0]);
86 break;
87 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.comc4364992011-11-07 15:54:49 +000088 *premul = true; // fallthru
bsalomon@google.com6850eab2011-11-03 20:29:47 +000089 case SkCanvas::kRGBA_Premul_Config8888:
90 a = static_cast<U8CPU>(c[3]);
91 r = static_cast<U8CPU>(c[0]);
92 g = static_cast<U8CPU>(c[1]);
93 b = static_cast<U8CPU>(c[2]);
94 break;
95 }
bsalomon@google.comc4364992011-11-07 15:54:49 +000096 if (*premul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000097 r = SkMulDiv255Ceiling(r, a);
98 g = SkMulDiv255Ceiling(g, a);
99 b = SkMulDiv255Ceiling(b, a);
100 }
101 return SkPackARGB32(a, r, g, b);
102}
103
bsalomon@google.comc6980972011-11-02 19:57:21 +0000104void fillCanvas(SkCanvas* canvas) {
105 static SkBitmap bmp;
106 if (bmp.isNull()) {
107 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H);
108 bool alloc = bmp.allocPixels();
109 SkASSERT(alloc);
110 SkAutoLockPixels alp(bmp);
111 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
112 for (int y = 0; y < DEV_H; ++y) {
113 for (int x = 0; x < DEV_W; ++x) {
114 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
115 *pixel = getCanvasColor(x, y);
116 }
117 }
118 }
119 canvas->save();
120 canvas->setMatrix(SkMatrix::I());
121 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
122 SkPaint paint;
123 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
124 canvas->drawBitmap(bmp, 0, 0, &paint);
125 canvas->restore();
126}
127
128void fillBitmap(SkBitmap* bitmap) {
129 SkASSERT(bitmap->lockPixelsAreWritable());
130 SkAutoLockPixels alp(*bitmap);
131 int w = bitmap->width();
132 int h = bitmap->height();
133 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
134 for (int y = 0; y < h; ++y) {
135 for (int x = 0; x < w; ++x) {
136 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
137 *pixel = getBitmapColor(x, y, w, h);
138 }
139 }
140}
141
bsalomon@google.comc4364992011-11-07 15:54:49 +0000142bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
143 if (!didPremulConversion) {
144 return a == b;
145 }
146 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
147 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
148 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
149 int32_t aB = SkGetPackedB32(a);
150
151 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
152 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
153 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
154 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
155
156 return aA == bA &&
157 SkAbs32(aR - bR) <= 1 &&
158 SkAbs32(aG - bG) <= 1 &&
159 SkAbs32(aB - bB) <= 1;
160}
161
bsalomon@google.comc6980972011-11-02 19:57:21 +0000162// checks the bitmap contains correct pixels after the readPixels
163// if the bitmap was prefilled with pixels it checks that these weren't
164// overwritten in the area outside the readPixels.
165bool checkRead(skiatest::Reporter* reporter,
166 const SkBitmap& bitmap,
167 int x, int y,
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000168 bool checkCanvasPixels,
169 bool checkBitmapPixels,
170 SkCanvas::Config8888 config8888) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000171 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
172 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000173 SkASSERT(checkCanvasPixels || checkBitmapPixels);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000174
175 int bw = bitmap.width();
176 int bh = bitmap.height();
177
178 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
179 SkIRect clippedSrcRect = DEV_RECT;
180 if (!clippedSrcRect.intersect(srcRect)) {
181 clippedSrcRect.setEmpty();
182 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000183 bool failed = false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000184 SkAutoLockPixels alp(bitmap);
185 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap.getPixels());
186 for (int by = 0; by < bh; ++by) {
187 for (int bx = 0; bx < bw; ++bx) {
188 int devx = bx + srcRect.fLeft;
189 int devy = by + srcRect.fTop;
190
bsalomon@google.comc4364992011-11-07 15:54:49 +0000191 uint32_t pixel = *reinterpret_cast<SkPMColor*>(pixels + by * bitmap.rowBytes() + bx * bitmap.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000192
193 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000194 if (checkCanvasPixels) {
195 SkPMColor canvasPixel = getCanvasColor(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000196 bool didPremul;
197 SkPMColor pmPixel = convertConfig8888ToPMColor(config8888, pixel, &didPremul);
198 bool check;
199 REPORTER_ASSERT(reporter, check = checkPixel(pmPixel, canvasPixel, didPremul));
200 if (!check) {
201 failed = true;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000202 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000203 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000204 } else if (checkBitmapPixels) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000205 REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw, bh) == pixel);
206 if (getBitmapColor(bx, by, bw, bh) != pixel) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000207 failed = true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000208 }
209 }
210 }
211 }
bsalomon@google.coma2092aa2011-11-10 15:10:24 +0000212 return !failed;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000213}
214
215enum BitmapInit {
216 kFirstBitmapInit = 0,
217
218 kNoPixels_BitmapInit = kFirstBitmapInit,
219 kTight_BitmapInit,
220 kRowBytes_BitmapInit,
221
222 kBitmapInitCnt
223};
224
225BitmapInit nextBMI(BitmapInit bmi) {
226 int x = bmi;
227 return static_cast<BitmapInit>(++x);
228}
229
230
231void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init) {
232 int w = rect.width();
233 int h = rect.height();
234 int rowBytes = 0;
235 bool alloc = true;
236 switch (init) {
237 case kNoPixels_BitmapInit:
238 alloc = false;
239 case kTight_BitmapInit:
240 break;
241 case kRowBytes_BitmapInit:
242 rowBytes = w * sizeof(SkPMColor) + 16 * sizeof(SkPMColor);
243 break;
244 default:
245 SkASSERT(0);
246 break;
247 }
248 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes);
249 if (alloc) {
250 bitmap->allocPixels();
251 }
252}
253
254void ReadPixelsTest(skiatest::Reporter* reporter, GrContext* context) {
255 SkCanvas canvas;
256
257 const SkIRect testRects[] = {
258 // entire thing
259 DEV_RECT,
260 // larger on all sides
261 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
262 // fully contained
263 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
264 // outside top left
265 SkIRect::MakeLTRB(-10, -10, -1, -1),
266 // touching top left corner
267 SkIRect::MakeLTRB(-10, -10, 0, 0),
268 // overlapping top left corner
269 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
270 // overlapping top left and top right corners
271 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
272 // touching entire top edge
273 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
274 // overlapping top right corner
275 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
276 // contained in x, overlapping top edge
277 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
278 // outside top right corner
279 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
280 // touching top right corner
281 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
282 // overlapping top left and bottom left corners
283 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
284 // touching entire left edge
285 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
286 // overlapping bottom left corner
287 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
288 // contained in y, overlapping left edge
289 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
290 // outside bottom left corner
291 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
292 // touching bottom left corner
293 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
294 // overlapping bottom left and bottom right corners
295 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
296 // touching entire left edge
297 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
298 // overlapping bottom right corner
299 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
300 // overlapping top right and bottom right corners
301 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
302 };
303
bsalomon@google.comb9f026f2012-02-09 13:53:48 +0000304 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000305
306 if (0 == dtype) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000307 canvas.setDevice(new SkDevice(SkBitmap::kARGB_8888_Config,
308 DEV_W,
309 DEV_H,
310 false))->unref();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000311 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000312// GPU device known not to work in the fixed pt build.
313#if defined(SK_SCALAR_IS_FIXED) || !SK_SUPPORT_GPU
bsalomon@google.comc6980972011-11-02 19:57:21 +0000314 continue;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000315#else
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000316 canvas.setDevice(new SkGpuDevice(context,
317 SkBitmap::kARGB_8888_Config,
318 DEV_W,
319 DEV_H))->unref();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000320#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000321 }
322 fillCanvas(&canvas);
323
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000324 static const SkCanvas::Config8888 gReadConfigs[] = {
325 SkCanvas::kNative_Premul_Config8888,
326 SkCanvas::kNative_Unpremul_Config8888,
djsollen@google.com8688e5b2012-01-09 13:02:20 +0000327/**
328 * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438)
329 * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to
330 * fail. This should be removed as soon as the issue above is resolved.
331 */
332#if !defined(SK_BUILD_FOR_ANDROID)
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000333 SkCanvas::kBGRA_Premul_Config8888,
334 SkCanvas::kBGRA_Unpremul_Config8888,
djsollen@google.com8688e5b2012-01-09 13:02:20 +0000335#endif
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000336 SkCanvas::kRGBA_Premul_Config8888,
337 SkCanvas::kRGBA_Unpremul_Config8888,
338 };
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000339 for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000340 const SkIRect& srcRect = testRects[rect];
341 for (BitmapInit bmi = kFirstBitmapInit;
342 bmi < kBitmapInitCnt;
343 bmi = nextBMI(bmi)) {
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000344 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000345 SkCanvas::Config8888 config8888 = gReadConfigs[c];
346 SkBitmap bmp;
347 init_bitmap(&bmp, srcRect, bmi);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000348
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000349 // if the bitmap has pixels allocated before the readPixels,
350 // note that and fill them with pattern
351 bool startsWithPixels = !bmp.isNull();
352 if (startsWithPixels) {
353 fillBitmap(&bmp);
354 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000355
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000356 bool success =
357 canvas.readPixels(&bmp, srcRect.fLeft,
358 srcRect.fTop, config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000359
bsalomon@google.comc4364992011-11-07 15:54:49 +0000360 // we expect to succeed when the read isn't fully clipped
361 // out.
362 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000363 // determine whether we expected the read to succeed.
364 REPORTER_ASSERT(reporter, success == expectSuccess);
365
366 if (success || startsWithPixels) {
367 checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop,
368 success, startsWithPixels, config8888);
369 } else {
370 // if we had no pixels beforehand and the readPixels
371 // failed then our bitmap should still not have pixels
372 REPORTER_ASSERT(reporter, bmp.isNull());
373 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000374 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000375 // check the old webkit version of readPixels that clips the
376 // bitmap size
bsalomon@google.comc6980972011-11-02 19:57:21 +0000377 SkBitmap wkbmp;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000378 bool success = canvas.readPixels(srcRect, &wkbmp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000379 SkIRect clippedRect = DEV_RECT;
380 if (clippedRect.intersect(srcRect)) {
381 REPORTER_ASSERT(reporter, success);
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000382 checkRead(reporter, wkbmp, clippedRect.fLeft,
383 clippedRect.fTop, true, false,
384 SkCanvas::kNative_Premul_Config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000385 } else {
386 REPORTER_ASSERT(reporter, !success);
387 }
388 }
389 }
390 }
391}
392}
393
394#include "TestClassDef.h"
395DEFINE_GPUTESTCLASS("ReadPixels", ReadPixelsTestClass, ReadPixelsTest)
396