blob: 1550c6d8dd8c8066624190649d5008a87fccae54 [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"
reed52d9ac62014-06-30 09:05:34 -070011#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000013
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000014#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080015#include "GrContext.h"
bsalomone8d21e82015-07-16 08:23:13 -070016#include "SkGr.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000018
kkinnunen15302832015-12-01 04:35:26 -080019#include <initializer_list>
20
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}
halcanary9d524f22016-03-29 09:03:52 -070054
bsalomone8d21e82015-07-16 08:23:13 -070055static 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());
reed73603f32016-09-20 08:42:38 -0700114 canvas->clipRect(DEV_RECT_S, SkCanvas::kReplace_Op);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000115 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700116 paint.setBlendMode(SkBlendMode::kSrc);
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 SkAutoLockPixels alp(*bitmap);
133 int w = bitmap->width();
134 int h = bitmap->height();
135 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
136 for (int y = 0; y < h; ++y) {
137 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700138 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
139 if (kAlpha_8_SkColorType == bitmap->colorType()) {
140 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
141 *alpha = SkGetPackedA32(initColor);
142 } else {
143 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
144 *pixel = initColor;
145 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000146 }
147 }
148}
149
bsalomone8d21e82015-07-16 08:23:13 -0700150static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000151 if (!didPremulConversion) {
152 return a == b;
153 }
154 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
155 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
156 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
157 int32_t aB = SkGetPackedB32(a);
158
159 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
160 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
161 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
162 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
163
164 return aA == bA &&
165 SkAbs32(aR - bR) <= 1 &&
166 SkAbs32(aG - bG) <= 1 &&
167 SkAbs32(aB - bB) <= 1;
168}
169
bsalomon@google.comc6980972011-11-02 19:57:21 +0000170// checks the bitmap contains correct pixels after the readPixels
171// if the bitmap was prefilled with pixels it checks that these weren't
172// overwritten in the area outside the readPixels.
bsalomone8d21e82015-07-16 08:23:13 -0700173static bool check_read(skiatest::Reporter* reporter,
174 const SkBitmap& bitmap,
175 int x, int y,
176 bool checkCanvasPixels,
lsalzmana2415ac2016-10-11 14:29:12 -0700177 bool checkBitmapPixels,
178 SkColorType ct,
179 SkAlphaType at) {
180 SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000181 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000182 SkASSERT(checkCanvasPixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183
bsalomon@google.comc6980972011-11-02 19:57:21 +0000184 int bw = bitmap.width();
185 int bh = bitmap.height();
186
187 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
188 SkIRect clippedSrcRect = DEV_RECT;
189 if (!clippedSrcRect.intersect(srcRect)) {
190 clippedSrcRect.setEmpty();
191 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000192 SkAutoLockPixels alp(bitmap);
lsalzmana2415ac2016-10-11 14:29:12 -0700193 if (kAlpha_8_SkColorType == ct) {
194 for (int by = 0; by < bh; ++by) {
195 for (int bx = 0; bx < bw; ++bx) {
196 int devx = bx + srcRect.fLeft;
197 int devy = by + srcRect.fTop;
198 const uint8_t* alpha = bitmap.getAddr8(bx, by);
199
200 if (clippedSrcRect.contains(devx, devy)) {
201 if (checkCanvasPixels) {
202 uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy));
203 if (canvasAlpha != *alpha) {
204 ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
205 bx, by, canvasAlpha, *alpha);
206 return false;
207 }
208 }
209 } else if (checkBitmapPixels) {
210 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
211 if (origDstAlpha != *alpha) {
212 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
213 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
214 return false;
215 }
216 }
217 }
218 }
219 return true;
220 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000221 for (int by = 0; by < bh; ++by) {
222 for (int bx = 0; bx < bw; ++bx) {
223 int devx = bx + srcRect.fLeft;
224 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000226 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000227
228 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000229 if (checkCanvasPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700230 SkPMColor canvasPixel = get_src_color(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000231 bool didPremul;
bsalomone8d21e82015-07-16 08:23:13 -0700232 SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
bsalomon39826022015-07-23 08:07:21 -0700233 if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
egdaniel88e8aef2016-06-27 14:34:55 -0700234 ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
235 "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000236 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000237 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000238 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000239 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700240 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
241 if (origDstPixel != *pixel) {
242 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
243 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000244 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000245 }
246 }
247 }
248 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000249 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000250}
251
252enum BitmapInit {
253 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000254
bsalomon@google.comc6980972011-11-02 19:57:21 +0000255 kNoPixels_BitmapInit = kFirstBitmapInit,
256 kTight_BitmapInit,
257 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800258 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000259
bsalomon9d02b262016-02-01 12:49:30 -0800260 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500261
262#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800263 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500264#else
265 kLast_BitmapInit = kLastAligned_BitmapInit
266#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000267};
268
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000269static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000270 int x = bmi;
271 return static_cast<BitmapInit>(++x);
272}
273
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000274static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
275 SkAlphaType at) {
276 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000277 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000278 bool alloc = true;
279 switch (init) {
280 case kNoPixels_BitmapInit:
281 alloc = false;
282 case kTight_BitmapInit:
283 break;
284 case kRowBytes_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700285 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000286 break;
bsalomon9d02b262016-02-01 12:49:30 -0800287 case kRowBytesOdd_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700288 rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
bsalomon9d02b262016-02-01 12:49:30 -0800289 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000290 default:
291 SkASSERT(0);
292 break;
293 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000294
bsalomon@google.comc6980972011-11-02 19:57:21 +0000295 if (alloc) {
bsalomon9d02b262016-02-01 12:49:30 -0800296 bitmap->allocPixels(info, rowBytes);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000297 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000298 bitmap->setInfo(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000299 }
300}
301
kkinnunen15302832015-12-01 04:35:26 -0800302static const struct {
303 SkColorType fColorType;
304 SkAlphaType fAlphaType;
305} gReadPixelsConfigs[] = {
306 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
307 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
308 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
309 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
lsalzmana2415ac2016-10-11 14:29:12 -0700310 { kAlpha_8_SkColorType, kPremul_SkAlphaType },
kkinnunen15302832015-12-01 04:35:26 -0800311};
312const SkIRect gReadPixelsTestRects[] = {
313 // entire thing
314 DEV_RECT,
315 // larger on all sides
316 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
317 // fully contained
318 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
319 // outside top left
320 SkIRect::MakeLTRB(-10, -10, -1, -1),
321 // touching top left corner
322 SkIRect::MakeLTRB(-10, -10, 0, 0),
323 // overlapping top left corner
324 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
325 // overlapping top left and top right corners
326 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
327 // touching entire top edge
328 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
329 // overlapping top right corner
330 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
331 // contained in x, overlapping top edge
332 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
333 // outside top right corner
334 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
335 // touching top right corner
336 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
337 // overlapping top left and bottom left corners
338 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
339 // touching entire left edge
340 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
341 // overlapping bottom left corner
342 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
343 // contained in y, overlapping left edge
344 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
345 // outside bottom left corner
346 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
347 // touching bottom left corner
348 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
349 // overlapping bottom left and bottom right corners
350 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
351 // touching entire left edge
352 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
353 // overlapping bottom right corner
354 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
355 // overlapping top right and bottom right corners
356 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
357};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000358
reede8f30622016-03-23 18:59:25 -0700359static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
bsalomon9d02b262016-02-01 12:49:30 -0800360 BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800361 SkCanvas* canvas = surface->getCanvas();
362 fill_src_canvas(canvas);
363 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
364 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800365 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800366 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
367 SkBitmap bmp;
368 init_bitmap(&bmp, srcRect, bmi,
369 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700370
kkinnunen15302832015-12-01 04:35:26 -0800371 // if the bitmap has pixels allocated before the readPixels,
372 // note that and fill them with pattern
373 bool startsWithPixels = !bmp.isNull();
374 if (startsWithPixels) {
375 fill_dst_bmp_with_init_data(&bmp);
376 }
377 uint32_t idBefore = surface->generationID();
378 bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
379 uint32_t idAfter = surface->generationID();
380
381 // we expect to succeed when the read isn't fully clipped
382 // out.
383 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
384 // determine whether we expected the read to succeed.
385 REPORTER_ASSERT(reporter, success == expectSuccess);
386 // read pixels should never change the gen id
387 REPORTER_ASSERT(reporter, idBefore == idAfter);
388
389 if (success || startsWithPixels) {
390 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
lsalzmana2415ac2016-10-11 14:29:12 -0700391 success, startsWithPixels,
392 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
kkinnunen15302832015-12-01 04:35:26 -0800393 } else {
394 // if we had no pixels beforehand and the readPixels
395 // failed then our bitmap should still not have pixels
396 REPORTER_ASSERT(reporter, bmp.isNull());
397 }
398 }
399 // check the old webkit version of readPixels that clips the
400 // bitmap size
401 SkBitmap wkbmp;
402 bool success = canvas->readPixels(srcRect, &wkbmp);
403 SkIRect clippedRect = DEV_RECT;
404 if (clippedRect.intersect(srcRect)) {
405 REPORTER_ASSERT(reporter, success);
406 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
407 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
408 check_read(reporter, wkbmp, clippedRect.fLeft,
lsalzmana2415ac2016-10-11 14:29:12 -0700409 clippedRect.fTop, true, false,
410 kN32_SkColorType, kPremul_SkAlphaType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000411 } else {
kkinnunen15302832015-12-01 04:35:26 -0800412 REPORTER_ASSERT(reporter, !success);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000413 }
kkinnunen15302832015-12-01 04:35:26 -0800414 }
415 }
416}
417DEF_TEST(ReadPixels, reporter) {
418 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700419 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800420 // SW readback fails a premul check when reading back to an unaligned rowbytes.
421 test_readpixels(reporter, surface, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800422}
bsalomone8d21e82015-07-16 08:23:13 -0700423#if SK_SUPPORT_GPU
egdaniel88e8aef2016-06-27 14:34:55 -0700424DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
robertphillips7e922762016-07-26 11:38:17 -0700425 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
kkinnunen15302832015-12-01 04:35:26 -0800426 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
robertphillips7e922762016-07-26 11:38:17 -0700427 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
428 ii, 0, origin, nullptr));
bsalomon9d02b262016-02-01 12:49:30 -0800429 test_readpixels(reporter, surface, kLast_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800430 }
431}
bsalomone8d21e82015-07-16 08:23:13 -0700432#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000433
bsalomone8d21e82015-07-16 08:23:13 -0700434#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -0800435static void test_readpixels_texture(skiatest::Reporter* reporter, GrTexture* texture) {
436 fill_src_texture(texture);
437 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
438 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800439 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800440 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
441 SkBitmap bmp;
442 init_bitmap(&bmp, srcRect, bmi,
443 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
444
445 // if the bitmap has pixels allocated before the readPixels,
446 // note that and fill them with pattern
447 bool startsWithPixels = !bmp.isNull();
448 // Try doing the read directly from a non-renderable texture
449 if (startsWithPixels) {
450 fill_dst_bmp_with_init_data(&bmp);
451 GrPixelConfig dstConfig =
452 SkImageInfo2GrPixelConfig(gReadPixelsConfigs[c].fColorType,
453 gReadPixelsConfigs[c].fAlphaType,
brianosmanb109b8c2016-06-16 13:03:24 -0700454 nullptr,
brianosmana6359362016-03-21 06:55:37 -0700455 *texture->getContext()->caps());
kkinnunen15302832015-12-01 04:35:26 -0800456 uint32_t flags = 0;
457 if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
458 flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000459 }
kkinnunen15302832015-12-01 04:35:26 -0800460 bmp.lockPixels();
461 bool success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(),
462 bmp.height(), dstConfig, bmp.getPixels(),
463 bmp.rowBytes(), flags);
464 bmp.unlockPixels();
465 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
lsalzmana2415ac2016-10-11 14:29:12 -0700466 success, true,
467 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000468 }
469 }
470 }
471 }
472}
egdaniel88e8aef2016-06-27 14:34:55 -0700473DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800474 // On the GPU we will also try reading back from a non-renderable texture.
Brian Salomon8b1fb742016-11-03 15:21:06 -0400475 for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
476 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
477 GrSurfaceDesc desc;
478 desc.fFlags = flags;
479 desc.fWidth = DEV_W;
480 desc.fHeight = DEV_H;
481 desc.fConfig = kSkia8888_GrPixelConfig;
482 desc.fOrigin = origin;
483 sk_sp<GrTexture> texture(ctxInfo.grContext()->textureProvider()->createTexture(desc,
484 SkBudgeted::kNo));
485 test_readpixels_texture(reporter, texture.get());
486 }
kkinnunen15302832015-12-01 04:35:26 -0800487 }
488}
489#endif