blob: aa98e919df4acaff486d2b423f07a50be1ebc0c7 [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"
Matt Sarett8572d852017-02-14 11:21:02 -050010#include "SkColorSpace_Base.h"
11#include "SkHalf.h"
12#include "SkImageInfoPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000013#include "SkMathPriv.h"
reed52d9ac62014-06-30 09:05:34 -070014#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000015#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000016
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080018#include "GrContext.h"
bsalomone8d21e82015-07-16 08:23:13 -070019#include "SkGr.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000020#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000021
kkinnunen15302832015-12-01 04:35:26 -080022#include <initializer_list>
23
bsalomon@google.comc6980972011-11-02 19:57:21 +000024static const int DEV_W = 100, DEV_H = 100;
25static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000026static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000027 DEV_H * SK_Scalar1);
28
bsalomone8d21e82015-07-16 08:23:13 -070029static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000030 SkASSERT(x >= 0 && x < DEV_W);
31 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000032
33 U8CPU r = x;
34 U8CPU g = y;
35 U8CPU b = 0xc;
36
37 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000038 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000039 case 0:
40 a = 0xff;
41 break;
42 case 1:
43 a = 0x80;
44 break;
45 case 2:
46 a = 0xCC;
47 break;
48 case 4:
49 a = 0x01;
50 break;
51 case 3:
52 a = 0x00;
53 break;
54 }
55 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000056}
halcanary9d524f22016-03-29 09:03:52 -070057
bsalomone8d21e82015-07-16 08:23:13 -070058static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000059 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000060
bsalomon@google.comc6980972011-11-02 19:57:21 +000061 U8CPU b = n & 0xff;
62 U8CPU g = (n >> 8) & 0xff;
63 U8CPU r = (n >> 16) & 0xff;
64 return SkPackARGB32(0xff, r, g , b);
65}
66
bsalomone8d21e82015-07-16 08:23:13 -070067static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
68 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000069 *doUnpremul = (kUnpremul_SkAlphaType == at);
70
71 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000072 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000073 switch (ct) {
74 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000075 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000076 g = static_cast<U8CPU>(c[1]);
77 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000078 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000079 break;
80 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000081 r = static_cast<U8CPU>(c[0]);
82 g = static_cast<U8CPU>(c[1]);
83 b = static_cast<U8CPU>(c[2]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000084 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000085 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000086 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000087 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000088 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000089 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000090
91 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000092 r = SkMulDiv255Ceiling(r, a);
93 g = SkMulDiv255Ceiling(g, a);
94 b = SkMulDiv255Ceiling(b, a);
95 }
96 return SkPackARGB32(a, r, g, b);
97}
98
bsalomone8d21e82015-07-16 08:23:13 -070099static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000100 static SkBitmap bmp;
101 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700102 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000103 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
104 for (int y = 0; y < DEV_H; ++y) {
105 for (int x = 0; x < DEV_W; ++x) {
106 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700107 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000108 }
109 }
110 }
bsalomone8d21e82015-07-16 08:23:13 -0700111 return bmp;
112}
113
114static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000115 canvas->save();
116 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500117 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000118 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700119 paint.setBlendMode(SkBlendMode::kSrc);
bsalomone8d21e82015-07-16 08:23:13 -0700120 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000121 canvas->restore();
122}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123
bsalomone8d21e82015-07-16 08:23:13 -0700124#if SK_SUPPORT_GPU
125static void fill_src_texture(GrTexture* texture) {
126 SkBitmap bmp = make_src_bitmap();
127 bmp.lockPixels();
128 texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(),
129 bmp.rowBytes());
130 bmp.unlockPixels();
131}
132#endif
133
134static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000135 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) {
lsalzmana2415ac2016-10-11 14:29:12 -0700141 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
142 if (kAlpha_8_SkColorType == bitmap->colorType()) {
143 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
144 *alpha = SkGetPackedA32(initColor);
145 } else {
146 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
147 *pixel = initColor;
148 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000149 }
150 }
151}
152
bsalomone8d21e82015-07-16 08:23:13 -0700153static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000154 if (!didPremulConversion) {
155 return a == b;
156 }
157 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
158 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
159 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
160 int32_t aB = SkGetPackedB32(a);
161
162 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
163 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
164 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
165 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
166
167 return aA == bA &&
168 SkAbs32(aR - bR) <= 1 &&
169 SkAbs32(aG - bG) <= 1 &&
170 SkAbs32(aB - bB) <= 1;
171}
172
bsalomon@google.comc6980972011-11-02 19:57:21 +0000173// checks the bitmap contains correct pixels after the readPixels
174// if the bitmap was prefilled with pixels it checks that these weren't
175// overwritten in the area outside the readPixels.
bsalomone8d21e82015-07-16 08:23:13 -0700176static bool check_read(skiatest::Reporter* reporter,
177 const SkBitmap& bitmap,
178 int x, int y,
179 bool checkCanvasPixels,
lsalzmana2415ac2016-10-11 14:29:12 -0700180 bool checkBitmapPixels,
181 SkColorType ct,
182 SkAlphaType at) {
183 SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000184 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000185 SkASSERT(checkCanvasPixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186
bsalomon@google.comc6980972011-11-02 19:57:21 +0000187 int bw = bitmap.width();
188 int bh = bitmap.height();
189
190 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
191 SkIRect clippedSrcRect = DEV_RECT;
192 if (!clippedSrcRect.intersect(srcRect)) {
193 clippedSrcRect.setEmpty();
194 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000195 SkAutoLockPixels alp(bitmap);
lsalzmana2415ac2016-10-11 14:29:12 -0700196 if (kAlpha_8_SkColorType == ct) {
197 for (int by = 0; by < bh; ++by) {
198 for (int bx = 0; bx < bw; ++bx) {
199 int devx = bx + srcRect.fLeft;
200 int devy = by + srcRect.fTop;
201 const uint8_t* alpha = bitmap.getAddr8(bx, by);
202
203 if (clippedSrcRect.contains(devx, devy)) {
204 if (checkCanvasPixels) {
205 uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy));
206 if (canvasAlpha != *alpha) {
207 ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
208 bx, by, canvasAlpha, *alpha);
209 return false;
210 }
211 }
212 } else if (checkBitmapPixels) {
213 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
214 if (origDstAlpha != *alpha) {
215 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
216 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
217 return false;
218 }
219 }
220 }
221 }
222 return true;
223 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000224 for (int by = 0; by < bh; ++by) {
225 for (int bx = 0; bx < bw; ++bx) {
226 int devx = bx + srcRect.fLeft;
227 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000229 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000230
231 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000232 if (checkCanvasPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700233 SkPMColor canvasPixel = get_src_color(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000234 bool didPremul;
bsalomone8d21e82015-07-16 08:23:13 -0700235 SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
bsalomon39826022015-07-23 08:07:21 -0700236 if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
egdaniel88e8aef2016-06-27 14:34:55 -0700237 ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
238 "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000239 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000240 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000241 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000242 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700243 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
244 if (origDstPixel != *pixel) {
245 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
246 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000247 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248 }
249 }
250 }
251 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000252 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000253}
254
255enum BitmapInit {
256 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000257
bsalomon@google.comc6980972011-11-02 19:57:21 +0000258 kNoPixels_BitmapInit = kFirstBitmapInit,
259 kTight_BitmapInit,
260 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800261 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262
bsalomon9d02b262016-02-01 12:49:30 -0800263 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500264
265#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800266 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500267#else
268 kLast_BitmapInit = kLastAligned_BitmapInit
269#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000270};
271
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000272static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000273 int x = bmi;
274 return static_cast<BitmapInit>(++x);
275}
276
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000277static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
278 SkAlphaType at) {
279 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000280 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000281 bool alloc = true;
282 switch (init) {
283 case kNoPixels_BitmapInit:
284 alloc = false;
285 case kTight_BitmapInit:
286 break;
287 case kRowBytes_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700288 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000289 break;
bsalomon9d02b262016-02-01 12:49:30 -0800290 case kRowBytesOdd_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700291 rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
bsalomon9d02b262016-02-01 12:49:30 -0800292 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000293 default:
294 SkASSERT(0);
295 break;
296 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000297
bsalomon@google.comc6980972011-11-02 19:57:21 +0000298 if (alloc) {
bsalomon9d02b262016-02-01 12:49:30 -0800299 bitmap->allocPixels(info, rowBytes);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000300 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000301 bitmap->setInfo(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000302 }
303}
304
kkinnunen15302832015-12-01 04:35:26 -0800305static const struct {
306 SkColorType fColorType;
307 SkAlphaType fAlphaType;
308} gReadPixelsConfigs[] = {
309 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
310 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
311 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
312 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
lsalzmana2415ac2016-10-11 14:29:12 -0700313 { kAlpha_8_SkColorType, kPremul_SkAlphaType },
kkinnunen15302832015-12-01 04:35:26 -0800314};
315const SkIRect gReadPixelsTestRects[] = {
316 // entire thing
317 DEV_RECT,
318 // larger on all sides
319 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
320 // fully contained
321 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
322 // outside top left
323 SkIRect::MakeLTRB(-10, -10, -1, -1),
324 // touching top left corner
325 SkIRect::MakeLTRB(-10, -10, 0, 0),
326 // overlapping top left corner
327 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
328 // overlapping top left and top right corners
329 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
330 // touching entire top edge
331 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
332 // overlapping top right corner
333 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
334 // contained in x, overlapping top edge
335 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
336 // outside top right corner
337 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
338 // touching top right corner
339 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
340 // overlapping top left and bottom left corners
341 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
342 // touching entire left edge
343 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
344 // overlapping bottom left corner
345 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
346 // contained in y, overlapping left edge
347 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
348 // outside bottom left corner
349 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
350 // touching bottom left corner
351 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
352 // overlapping bottom left and bottom right corners
353 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
354 // touching entire left edge
355 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
356 // overlapping bottom right corner
357 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
358 // overlapping top right and bottom right corners
359 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
360};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000361
reede8f30622016-03-23 18:59:25 -0700362static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
bsalomon9d02b262016-02-01 12:49:30 -0800363 BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800364 SkCanvas* canvas = surface->getCanvas();
365 fill_src_canvas(canvas);
366 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
367 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800368 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800369 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
370 SkBitmap bmp;
371 init_bitmap(&bmp, srcRect, bmi,
372 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700373
kkinnunen15302832015-12-01 04:35:26 -0800374 // if the bitmap has pixels allocated before the readPixels,
375 // note that and fill them with pattern
376 bool startsWithPixels = !bmp.isNull();
377 if (startsWithPixels) {
378 fill_dst_bmp_with_init_data(&bmp);
379 }
380 uint32_t idBefore = surface->generationID();
381 bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
382 uint32_t idAfter = surface->generationID();
383
384 // we expect to succeed when the read isn't fully clipped
385 // out.
386 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
387 // determine whether we expected the read to succeed.
388 REPORTER_ASSERT(reporter, success == expectSuccess);
389 // read pixels should never change the gen id
390 REPORTER_ASSERT(reporter, idBefore == idAfter);
391
392 if (success || startsWithPixels) {
393 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
lsalzmana2415ac2016-10-11 14:29:12 -0700394 success, startsWithPixels,
395 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
kkinnunen15302832015-12-01 04:35:26 -0800396 } else {
397 // if we had no pixels beforehand and the readPixels
398 // failed then our bitmap should still not have pixels
399 REPORTER_ASSERT(reporter, bmp.isNull());
400 }
401 }
402 // check the old webkit version of readPixels that clips the
403 // bitmap size
404 SkBitmap wkbmp;
405 bool success = canvas->readPixels(srcRect, &wkbmp);
406 SkIRect clippedRect = DEV_RECT;
407 if (clippedRect.intersect(srcRect)) {
408 REPORTER_ASSERT(reporter, success);
409 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
410 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
411 check_read(reporter, wkbmp, clippedRect.fLeft,
lsalzmana2415ac2016-10-11 14:29:12 -0700412 clippedRect.fTop, true, false,
413 kN32_SkColorType, kPremul_SkAlphaType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000414 } else {
kkinnunen15302832015-12-01 04:35:26 -0800415 REPORTER_ASSERT(reporter, !success);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000416 }
kkinnunen15302832015-12-01 04:35:26 -0800417 }
418 }
419}
420DEF_TEST(ReadPixels, reporter) {
421 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700422 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800423 // SW readback fails a premul check when reading back to an unaligned rowbytes.
424 test_readpixels(reporter, surface, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800425}
bsalomone8d21e82015-07-16 08:23:13 -0700426#if SK_SUPPORT_GPU
egdaniel88e8aef2016-06-27 14:34:55 -0700427DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
robertphillips7e922762016-07-26 11:38:17 -0700428 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
kkinnunen15302832015-12-01 04:35:26 -0800429 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
robertphillips7e922762016-07-26 11:38:17 -0700430 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
431 ii, 0, origin, nullptr));
bsalomon9d02b262016-02-01 12:49:30 -0800432 test_readpixels(reporter, surface, kLast_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800433 }
434}
bsalomone8d21e82015-07-16 08:23:13 -0700435#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000436
bsalomone8d21e82015-07-16 08:23:13 -0700437#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -0800438static void test_readpixels_texture(skiatest::Reporter* reporter, GrTexture* texture) {
439 fill_src_texture(texture);
440 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
441 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800442 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800443 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
444 SkBitmap bmp;
445 init_bitmap(&bmp, srcRect, bmi,
446 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
447
448 // if the bitmap has pixels allocated before the readPixels,
449 // note that and fill them with pattern
450 bool startsWithPixels = !bmp.isNull();
451 // Try doing the read directly from a non-renderable texture
452 if (startsWithPixels) {
453 fill_dst_bmp_with_init_data(&bmp);
454 GrPixelConfig dstConfig =
Brian Osman0c2997b2017-01-11 16:58:42 -0500455 SkImageInfo2GrPixelConfig(bmp.info(), *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
Matt Sarett8572d852017-02-14 11:21:02 -0500490
491///////////////////////////////////////////////////////////////////////////////////////////////////
492
493static const uint32_t kNumPixels = 5;
494
495// The five reference pixels are: red, green, blue, white, black.
496// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
497// plus a tail pixel.
498static const uint32_t rgba[kNumPixels] = {
499 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
500};
501static const uint32_t bgra[kNumPixels] = {
502 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
503};
504static const uint16_t rgb565[kNumPixels] = {
505 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
506};
507
508static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
509
510static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
511static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
512static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
513static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
514static const uint64_t f16[kNumPixels] = {
515 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
516};
517
518#ifdef SK_PMCOLOR_IS_RGBA
519static const SkPMColor index8colors[kNumPixels] = {
520 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
521};
522#else
523static const SkPMColor index8colors[kNumPixels] = {
524 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
525};
526#endif
527static const uint8_t index8[kNumPixels] = { 0, 1, 2, 3, 4 };
528static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
529static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
530
531static const void* five_reference_pixels(SkColorType colorType) {
532 switch (colorType) {
533 case kUnknown_SkColorType:
534 return nullptr;
535 case kAlpha_8_SkColorType:
536 return alpha8;
537 case kRGB_565_SkColorType:
538 return rgb565;
539 case kARGB_4444_SkColorType:
540 return rgba4444;
541 case kRGBA_8888_SkColorType:
542 return rgba;
543 case kBGRA_8888_SkColorType:
544 return bgra;
545 case kIndex_8_SkColorType:
546 return index8;
547 case kGray_8_SkColorType:
548 return gray8;
549 case kRGBA_F16_SkColorType:
550 return f16;
551 }
552
553 SkASSERT(false);
554 return nullptr;
555}
556
557static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
558 const SkImageInfo& srcInfo) {
559 if (!SkImageInfoIsValid(srcInfo)) {
560 return;
561 }
562
563 sk_sp<SkColorTable> srcColorTable = (kIndex_8_SkColorType == srcInfo.colorType())
564 ? sk_make_sp<SkColorTable>(index8colors, 5)
565 : nullptr;
566 sk_sp<SkColorTable> dstColorTable = (kIndex_8_SkColorType == dstInfo.colorType())
567 ? sk_make_sp<SkColorTable>(index8colors, 5)
568 : nullptr;
569
570 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
571 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes(), srcColorTable.get());
572 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
573 REPORTER_ASSERT(r, src);
574
575 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
576 uint64_t dstPixels[kNumPixels];
577 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes(), dstColorTable.get());
578 bool success = src->readPixels(dstPixmap, 0, 0);
579 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
580
581 if (success) {
582 if (kGray_8_SkColorType == srcInfo.colorType() &&
583 kGray_8_SkColorType != dstInfo.colorType())
584 {
585 // This conversion is legal, but we won't get the "reference" pixels since we cannot
586 // represent colors in kGray8.
587 return;
588 }
589
590 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
591 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
592
593 }
594}
595
596DEF_TEST(ReadPixels_ValidConversion, reporter) {
597 const SkColorType kColorTypes[] = {
598 kUnknown_SkColorType,
599 kAlpha_8_SkColorType,
600 kRGB_565_SkColorType,
601 kARGB_4444_SkColorType,
602 kRGBA_8888_SkColorType,
603 kBGRA_8888_SkColorType,
604 kIndex_8_SkColorType,
605 kGray_8_SkColorType,
606 kRGBA_F16_SkColorType,
607 };
608
609 const SkAlphaType kAlphaTypes[] = {
610 kUnknown_SkAlphaType,
611 kOpaque_SkAlphaType,
612 kPremul_SkAlphaType,
613 kUnpremul_SkAlphaType,
614 };
615
616 const sk_sp<SkColorSpace> kColorSpaces[] = {
617 nullptr,
618 SkColorSpace::MakeSRGB(),
619 };
620
621 for (SkColorType dstCT : kColorTypes) {
622 for (SkAlphaType dstAT: kAlphaTypes) {
623 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
624 for (SkColorType srcCT : kColorTypes) {
625 for (SkAlphaType srcAT: kAlphaTypes) {
626 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
627 if (kRGBA_F16_SkColorType == dstCT && dstCS) {
628 dstCS = as_CSB(dstCS)->makeLinearGamma();
629 }
630
631 if (kRGBA_F16_SkColorType == srcCT && srcCS) {
632 srcCS = as_CSB(srcCS)->makeLinearGamma();
633 }
634
635 test_conversion(reporter,
636 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
637 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
638 }
639 }
640 }
641 }
642 }
643 }
644}