blob: b94be0eba88f939e6f0f3bcf2ab3596b6d3c7d2b [file] [log] [blame]
bsalomon@google.comc6980972011-11-02 19:57:21 +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
bsalomon@google.comc6980972011-11-02 19:57:21 +00008#include "SkCanvas.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009#include "SkColorPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000010#include "SkMathPriv.h"
bsalomon@google.comc6980972011-11-02 19:57:21 +000011#include "SkRegion.h"
reed52d9ac62014-06-30 09:05:34 -070012#include "SkSurface.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.com67b915d2013-02-04 16:13:32 +000016#include "GrContextFactory.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "SkGpuDevice.h"
bsalomone8d21e82015-07-16 08:23:13 -070018#include "SkGr.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000019#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000020
bsalomon@google.comc6980972011-11-02 19:57:21 +000021static 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
bsalomone8d21e82015-07-16 08:23:13 -070026static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000027 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}
bsalomone8d21e82015-07-16 08:23:13 -070054
55static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000056 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
bsalomone8d21e82015-07-16 08:23:13 -070064static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
65 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000066 *doUnpremul = (kUnpremul_SkAlphaType == at);
67
68 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000069 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000070 switch (ct) {
71 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000072 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000073 g = static_cast<U8CPU>(c[1]);
74 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000075 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000076 break;
77 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000078 r = static_cast<U8CPU>(c[0]);
79 g = static_cast<U8CPU>(c[1]);
80 b = static_cast<U8CPU>(c[2]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000081 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000082 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000083 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000084 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000085 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000086 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000087
88 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000089 r = SkMulDiv255Ceiling(r, a);
90 g = SkMulDiv255Ceiling(g, a);
91 b = SkMulDiv255Ceiling(b, a);
92 }
93 return SkPackARGB32(a, r, g, b);
94}
95
bsalomone8d21e82015-07-16 08:23:13 -070096static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +000097 static SkBitmap bmp;
98 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -070099 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000100 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
101 for (int y = 0; y < DEV_H; ++y) {
102 for (int x = 0; x < DEV_W; ++x) {
103 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700104 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000105 }
106 }
107 }
bsalomone8d21e82015-07-16 08:23:13 -0700108 return bmp;
109}
110
111static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000112 canvas->save();
113 canvas->setMatrix(SkMatrix::I());
114 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
115 SkPaint paint;
116 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
bsalomone8d21e82015-07-16 08:23:13 -0700117 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000118 canvas->restore();
119}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
bsalomone8d21e82015-07-16 08:23:13 -0700121#if SK_SUPPORT_GPU
122static void fill_src_texture(GrTexture* texture) {
123 SkBitmap bmp = make_src_bitmap();
124 bmp.lockPixels();
125 texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(),
126 bmp.rowBytes());
127 bmp.unlockPixels();
128}
129#endif
130
131static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000132 SkASSERT(bitmap->lockPixelsAreWritable());
133 SkAutoLockPixels alp(*bitmap);
134 int w = bitmap->width();
135 int h = bitmap->height();
136 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
137 for (int y = 0; y < h; ++y) {
138 for (int x = 0; x < w; ++x) {
139 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700140 *pixel = get_dst_bmp_init_color(x, y, w);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000141 }
142 }
143}
144
bsalomone8d21e82015-07-16 08:23:13 -0700145static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000146 if (!didPremulConversion) {
147 return a == b;
148 }
149 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
150 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
151 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
152 int32_t aB = SkGetPackedB32(a);
153
154 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
155 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
156 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
157 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
158
159 return aA == bA &&
160 SkAbs32(aR - bR) <= 1 &&
161 SkAbs32(aG - bG) <= 1 &&
162 SkAbs32(aB - bB) <= 1;
163}
164
bsalomon@google.comc6980972011-11-02 19:57:21 +0000165// checks the bitmap contains correct pixels after the readPixels
166// if the bitmap was prefilled with pixels it checks that these weren't
167// overwritten in the area outside the readPixels.
bsalomone8d21e82015-07-16 08:23:13 -0700168static bool check_read(skiatest::Reporter* reporter,
169 const SkBitmap& bitmap,
170 int x, int y,
171 bool checkCanvasPixels,
172 bool checkBitmapPixels) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000173 SkASSERT(4 == bitmap.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000174 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000175 SkASSERT(checkCanvasPixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000176
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000177 const SkColorType ct = bitmap.colorType();
178 const SkAlphaType at = bitmap.alphaType();
179
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);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000189 for (int by = 0; by < bh; ++by) {
190 for (int bx = 0; bx < bw; ++bx) {
191 int devx = bx + srcRect.fLeft;
192 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000194 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000195
196 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000197 if (checkCanvasPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700198 SkPMColor canvasPixel = get_src_color(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000199 bool didPremul;
bsalomone8d21e82015-07-16 08:23:13 -0700200 SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
bsalomon39826022015-07-23 08:07:21 -0700201 if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
202 ERRORF(reporter, "Expected readback pixel value 0x%08x, got 0x%08x. "
203 "Readback was unpremul: %d", canvasPixel, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000204 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000205 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000206 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000207 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700208 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
209 if (origDstPixel != *pixel) {
210 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
211 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000212 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000213 }
214 }
215 }
216 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000217 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000218}
219
220enum BitmapInit {
221 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000222
bsalomon@google.comc6980972011-11-02 19:57:21 +0000223 kNoPixels_BitmapInit = kFirstBitmapInit,
224 kTight_BitmapInit,
225 kRowBytes_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226
bsalomon@google.comc6980972011-11-02 19:57:21 +0000227 kBitmapInitCnt
228};
229
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000230static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000231 int x = bmi;
232 return static_cast<BitmapInit>(++x);
233}
234
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000235static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
236 SkAlphaType at) {
237 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000238 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000239 bool alloc = true;
240 switch (init) {
241 case kNoPixels_BitmapInit:
242 alloc = false;
243 case kTight_BitmapInit:
244 break;
245 case kRowBytes_BitmapInit:
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000246 rowBytes = (info.width() + 16) * sizeof(SkPMColor);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000247 break;
248 default:
249 SkASSERT(0);
250 break;
251 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000252
bsalomon@google.comc6980972011-11-02 19:57:21 +0000253 if (alloc) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000254 bitmap->allocPixels(info);
255 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000256 bitmap->setInfo(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000257 }
258}
259
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000260DEF_GPUTEST(ReadPixels, reporter, factory) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000261 const SkIRect testRects[] = {
262 // entire thing
263 DEV_RECT,
264 // larger on all sides
265 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
266 // fully contained
267 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
268 // outside top left
269 SkIRect::MakeLTRB(-10, -10, -1, -1),
270 // touching top left corner
271 SkIRect::MakeLTRB(-10, -10, 0, 0),
272 // overlapping top left corner
273 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
274 // overlapping top left and top right corners
275 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
276 // touching entire top edge
277 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
278 // overlapping top right corner
279 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
280 // contained in x, overlapping top edge
281 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
282 // outside top right corner
283 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
284 // touching top right corner
285 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
286 // overlapping top left and bottom left corners
287 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
288 // touching entire left edge
289 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
290 // overlapping bottom left corner
291 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
292 // contained in y, overlapping left edge
293 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
294 // outside bottom left corner
295 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
296 // touching bottom left corner
297 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
298 // overlapping bottom left and bottom right corners
299 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
300 // touching entire left edge
301 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
302 // overlapping bottom right corner
303 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
304 // overlapping top right and bottom right corners
305 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
306 };
307
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000308 for (int dtype = 0; dtype < 3; ++dtype) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000309 int glCtxTypeCnt = 1;
310#if SK_SUPPORT_GPU
bsalomone8d21e82015-07-16 08:23:13 -0700311 // On the GPU we will also try reading back from a non-renderable texture.
312 SkAutoTUnref<GrTexture> texture;
313
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000314 if (0 != dtype) {
315 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000316 }
djsollen@google.com8688e5b2012-01-09 13:02:20 +0000317#endif
reed52d9ac62014-06-30 09:05:34 -0700318 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000319 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
reed52d9ac62014-06-30 09:05:34 -0700320 SkAutoTUnref<SkSurface> surface;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000321 if (0 == dtype) {
reed52d9ac62014-06-30 09:05:34 -0700322 surface.reset(SkSurface::NewRaster(info));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000323 } else {
324#if SK_SUPPORT_GPU
325 GrContextFactory::GLContextType type =
326 static_cast<GrContextFactory::GLContextType>(glCtxType);
327 if (!GrContextFactory::IsRenderingGLContext(type)) {
328 continue;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000329 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000330 GrContext* context = factory->get(type);
331 if (NULL == context) {
332 continue;
333 }
bsalomonf2703d82014-10-28 14:33:06 -0700334 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800335 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000336 desc.fWidth = DEV_W;
337 desc.fHeight = DEV_H;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000338 desc.fConfig = kSkia8888_GrPixelConfig;
reed52d9ac62014-06-30 09:05:34 -0700339 desc.fOrigin = 1 == dtype ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
bsalomone8d21e82015-07-16 08:23:13 -0700340 SkAutoTUnref<GrTexture> surfaceTexture(
bsalomond309e7a2015-04-30 14:18:54 -0700341 context->textureProvider()->createTexture(desc, false));
bsalomone8d21e82015-07-16 08:23:13 -0700342 surface.reset(SkSurface::NewRenderTargetDirect(surfaceTexture->asRenderTarget()));
343 desc.fFlags = kNone_GrSurfaceFlags;
344
345 texture.reset(context->textureProvider()->createTexture(desc, false));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000346#else
347 continue;
348#endif
349 }
reed52d9ac62014-06-30 09:05:34 -0700350 SkCanvas& canvas = *surface->getCanvas();
bsalomone8d21e82015-07-16 08:23:13 -0700351 fill_src_canvas(&canvas);
352
353#if SK_SUPPORT_GPU
354 if (texture) {
355 fill_src_texture(texture);
356 }
357#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000358
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000359 static const struct {
360 SkColorType fColorType;
361 SkAlphaType fAlphaType;
362 } gReadConfigs[] = {
363 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
364 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
365 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
366 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000367 };
368 for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
369 const SkIRect& srcRect = testRects[rect];
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000370 for (BitmapInit bmi = kFirstBitmapInit; bmi < kBitmapInitCnt; bmi = nextBMI(bmi)) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000371 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000372 SkBitmap bmp;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000373 init_bitmap(&bmp, srcRect, bmi,
374 gReadConfigs[c].fColorType, gReadConfigs[c].fAlphaType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000375
376 // if the bitmap has pixels allocated before the readPixels,
377 // note that and fill them with pattern
378 bool startsWithPixels = !bmp.isNull();
379 if (startsWithPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700380 fill_dst_bmp_with_init_data(&bmp);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000381 }
reed52d9ac62014-06-30 09:05:34 -0700382 uint32_t idBefore = surface->generationID();
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000383 bool success = canvas.readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
reed52d9ac62014-06-30 09:05:34 -0700384 uint32_t idAfter = surface->generationID();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000385
386 // we expect to succeed when the read isn't fully clipped
387 // out.
388 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
389 // determine whether we expected the read to succeed.
390 REPORTER_ASSERT(reporter, success == expectSuccess);
391 // read pixels should never change the gen id
392 REPORTER_ASSERT(reporter, idBefore == idAfter);
393
394 if (success || startsWithPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700395 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
396 success, startsWithPixels);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000397 } else {
398 // if we had no pixels beforehand and the readPixels
399 // failed then our bitmap should still not have pixels
400 REPORTER_ASSERT(reporter, bmp.isNull());
401 }
bsalomone8d21e82015-07-16 08:23:13 -0700402#if SK_SUPPORT_GPU
403 // Try doing the read directly from a non-renderable texture
404 if (texture && startsWithPixels) {
405 fill_dst_bmp_with_init_data(&bmp);
406 GrPixelConfig dstConfig =
407 SkImageInfo2GrPixelConfig(gReadConfigs[c].fColorType,
408 gReadConfigs[c].fAlphaType,
409 kLinear_SkColorProfileType);
410 uint32_t flags = 0;
411 if (gReadConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
412 flags = GrContext::kUnpremul_PixelOpsFlag;
413 }
414 bmp.lockPixels();
415 success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(),
416 bmp.height(), dstConfig, bmp.getPixels(),
417 bmp.rowBytes(), flags);
418 bmp.unlockPixels();
419 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
420 success, true);
421 }
422#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000423 }
424 // check the old webkit version of readPixels that clips the
425 // bitmap size
426 SkBitmap wkbmp;
427 bool success = canvas.readPixels(srcRect, &wkbmp);
428 SkIRect clippedRect = DEV_RECT;
429 if (clippedRect.intersect(srcRect)) {
430 REPORTER_ASSERT(reporter, success);
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000431 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000432 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
bsalomone8d21e82015-07-16 08:23:13 -0700433 check_read(reporter, wkbmp, clippedRect.fLeft,
434 clippedRect.fTop, true, false);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000435 } else {
436 REPORTER_ASSERT(reporter, !success);
437 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000438 }
439 }
440 }
441 }
442}