blob: 9e287758cbd4cb14e36ebc42db569fe963d88a5a [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"
robertphillips@google.com73672252013-08-29 12:40:26 +000010#include "SkBitmapDevice.h"
bsalomon@google.comc6980972011-11-02 19:57:21 +000011#include "SkCanvas.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012#include "SkColorPriv.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
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000056SkPMColor getBitmapColor(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000057 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());
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000142 *pixel = getBitmapColor(x, y, w);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000143 }
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) {
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000209 REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw) == pixel);
210 if (getBitmapColor(bx, by, bw) != 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
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000306 for (int dtype = 0; dtype < 3; ++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) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000314 SkAutoTUnref<SkBaseDevice> device;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000315 if (0 == dtype) {
skia.committer@gmail.com772c4e62013-08-30 07:01:34 +0000316 device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000317 DEV_W, DEV_H, false));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000318 } else {
319#if SK_SUPPORT_GPU
320 GrContextFactory::GLContextType type =
321 static_cast<GrContextFactory::GLContextType>(glCtxType);
322 if (!GrContextFactory::IsRenderingGLContext(type)) {
323 continue;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000324 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000325 GrContext* context = factory->get(type);
326 if (NULL == context) {
327 continue;
328 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000329 GrTextureDesc desc;
330 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
331 desc.fWidth = DEV_W;
332 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000333 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000334 desc.fOrigin = 1 == dtype ? kBottomLeft_GrSurfaceOrigin
335 : kTopLeft_GrSurfaceOrigin;
336 GrAutoScratchTexture ast(context, desc, GrContext::kExact_ScratchTexMatch);
337 SkAutoTUnref<GrTexture> tex(ast.detach());
338 device.reset(new SkGpuDevice(context, tex));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000339#else
340 continue;
341#endif
342 }
343 SkCanvas canvas(device);
344 fillCanvas(&canvas);
345
346 static const SkCanvas::Config8888 gReadConfigs[] = {
347 SkCanvas::kNative_Premul_Config8888,
348 SkCanvas::kNative_Unpremul_Config8888,
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000349
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000350 SkCanvas::kBGRA_Premul_Config8888,
351 SkCanvas::kBGRA_Unpremul_Config8888,
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000352
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000353 SkCanvas::kRGBA_Premul_Config8888,
354 SkCanvas::kRGBA_Unpremul_Config8888,
355 };
356 for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
357 const SkIRect& srcRect = testRects[rect];
358 for (BitmapInit bmi = kFirstBitmapInit;
359 bmi < kBitmapInitCnt;
360 bmi = nextBMI(bmi)) {
361 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
362 SkCanvas::Config8888 config8888 = gReadConfigs[c];
363 SkBitmap bmp;
364 init_bitmap(&bmp, srcRect, bmi);
365
366 // if the bitmap has pixels allocated before the readPixels,
367 // note that and fill them with pattern
368 bool startsWithPixels = !bmp.isNull();
369 if (startsWithPixels) {
370 fillBitmap(&bmp);
371 }
372 uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
373 bool success =
374 canvas.readPixels(&bmp, srcRect.fLeft,
375 srcRect.fTop, config8888);
376 uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
377
378 // we expect to succeed when the read isn't fully clipped
379 // out.
380 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
381 // determine whether we expected the read to succeed.
382 REPORTER_ASSERT(reporter, success == expectSuccess);
383 // read pixels should never change the gen id
384 REPORTER_ASSERT(reporter, idBefore == idAfter);
385
386 if (success || startsWithPixels) {
387 checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop,
388 success, startsWithPixels, config8888);
389 } else {
390 // if we had no pixels beforehand and the readPixels
391 // failed then our bitmap should still not have pixels
392 REPORTER_ASSERT(reporter, bmp.isNull());
393 }
394 }
395 // check the old webkit version of readPixels that clips the
396 // bitmap size
397 SkBitmap wkbmp;
398 bool success = canvas.readPixels(srcRect, &wkbmp);
399 SkIRect clippedRect = DEV_RECT;
400 if (clippedRect.intersect(srcRect)) {
401 REPORTER_ASSERT(reporter, success);
402 checkRead(reporter, wkbmp, clippedRect.fLeft,
403 clippedRect.fTop, true, false,
404 SkCanvas::kNative_Premul_Config8888);
405 } else {
406 REPORTER_ASSERT(reporter, !success);
407 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000408 }
409 }
410 }
411 }
412}
413}
414
415#include "TestClassDef.h"
416DEFINE_GPUTESTCLASS("ReadPixels", ReadPixelsTestClass, ReadPixelsTest)