blob: c67e62b0909b6be4cd61bb36e13609e980799bde [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"
Robert Phillipse78b7252017-04-06 07:59:41 -040019#include "GrContextPriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050020#include "GrResourceProvider.h"
bsalomone8d21e82015-07-16 08:23:13 -070021#include "SkGr.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000022#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000023
kkinnunen15302832015-12-01 04:35:26 -080024#include <initializer_list>
25
bsalomon@google.comc6980972011-11-02 19:57:21 +000026static const int DEV_W = 100, DEV_H = 100;
27static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000028static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000029 DEV_H * SK_Scalar1);
30
bsalomone8d21e82015-07-16 08:23:13 -070031static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000032 SkASSERT(x >= 0 && x < DEV_W);
33 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000034
35 U8CPU r = x;
36 U8CPU g = y;
37 U8CPU b = 0xc;
38
39 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000040 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000041 case 0:
42 a = 0xff;
43 break;
44 case 1:
45 a = 0x80;
46 break;
47 case 2:
48 a = 0xCC;
49 break;
50 case 4:
51 a = 0x01;
52 break;
53 case 3:
54 a = 0x00;
55 break;
56 }
57 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000058}
halcanary9d524f22016-03-29 09:03:52 -070059
bsalomone8d21e82015-07-16 08:23:13 -070060static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000061 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000062
bsalomon@google.comc6980972011-11-02 19:57:21 +000063 U8CPU b = n & 0xff;
64 U8CPU g = (n >> 8) & 0xff;
65 U8CPU r = (n >> 16) & 0xff;
66 return SkPackARGB32(0xff, r, g , b);
67}
68
bsalomone8d21e82015-07-16 08:23:13 -070069static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
70 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000071 *doUnpremul = (kUnpremul_SkAlphaType == at);
72
73 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000074 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000075 switch (ct) {
76 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000077 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000078 g = static_cast<U8CPU>(c[1]);
79 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000080 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000081 break;
82 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000083 r = static_cast<U8CPU>(c[0]);
84 g = static_cast<U8CPU>(c[1]);
85 b = static_cast<U8CPU>(c[2]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000086 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000087 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000088 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000089 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000090 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000091 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000092
93 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000094 r = SkMulDiv255Ceiling(r, a);
95 g = SkMulDiv255Ceiling(g, a);
96 b = SkMulDiv255Ceiling(b, a);
97 }
98 return SkPackARGB32(a, r, g, b);
99}
100
bsalomone8d21e82015-07-16 08:23:13 -0700101static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000102 static SkBitmap bmp;
103 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700104 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000105 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
106 for (int y = 0; y < DEV_H; ++y) {
107 for (int x = 0; x < DEV_W; ++x) {
108 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700109 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000110 }
111 }
112 }
bsalomone8d21e82015-07-16 08:23:13 -0700113 return bmp;
114}
115
116static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000117 canvas->save();
118 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500119 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000120 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700121 paint.setBlendMode(SkBlendMode::kSrc);
bsalomone8d21e82015-07-16 08:23:13 -0700122 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000123 canvas->restore();
124}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125
bsalomone8d21e82015-07-16 08:23:13 -0700126#if SK_SUPPORT_GPU
Robert Phillipse78b7252017-04-06 07:59:41 -0400127static void fill_src_texture(GrContext* context, GrTextureProxy* proxy) {
bsalomone8d21e82015-07-16 08:23:13 -0700128 SkBitmap bmp = make_src_bitmap();
129 bmp.lockPixels();
Robert Phillipse78b7252017-04-06 07:59:41 -0400130
131 SkDEBUGCODE(bool result =) context->contextPriv().writeSurfacePixels(
132 proxy, nullptr,
133 0, 0, DEV_W, DEV_H,
134 kSkia8888_GrPixelConfig, nullptr,
135 bmp.getPixels(), bmp.rowBytes());
136 SkASSERT(result);
bsalomone8d21e82015-07-16 08:23:13 -0700137 bmp.unlockPixels();
138}
139#endif
140
141static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000142 SkAutoLockPixels alp(*bitmap);
143 int w = bitmap->width();
144 int h = bitmap->height();
145 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
146 for (int y = 0; y < h; ++y) {
147 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700148 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
149 if (kAlpha_8_SkColorType == bitmap->colorType()) {
150 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
151 *alpha = SkGetPackedA32(initColor);
152 } else {
153 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
154 *pixel = initColor;
155 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000156 }
157 }
158}
159
bsalomone8d21e82015-07-16 08:23:13 -0700160static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000161 if (!didPremulConversion) {
162 return a == b;
163 }
164 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
165 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
166 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
167 int32_t aB = SkGetPackedB32(a);
168
169 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
170 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
171 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
172 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
173
174 return aA == bA &&
175 SkAbs32(aR - bR) <= 1 &&
176 SkAbs32(aG - bG) <= 1 &&
177 SkAbs32(aB - bB) <= 1;
178}
179
bsalomon@google.comc6980972011-11-02 19:57:21 +0000180// checks the bitmap contains correct pixels after the readPixels
181// if the bitmap was prefilled with pixels it checks that these weren't
182// overwritten in the area outside the readPixels.
bsalomone8d21e82015-07-16 08:23:13 -0700183static bool check_read(skiatest::Reporter* reporter,
184 const SkBitmap& bitmap,
185 int x, int y,
186 bool checkCanvasPixels,
lsalzmana2415ac2016-10-11 14:29:12 -0700187 bool checkBitmapPixels,
188 SkColorType ct,
189 SkAlphaType at) {
190 SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000191 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000192 SkASSERT(checkCanvasPixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193
bsalomon@google.comc6980972011-11-02 19:57:21 +0000194 int bw = bitmap.width();
195 int bh = bitmap.height();
196
197 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
198 SkIRect clippedSrcRect = DEV_RECT;
199 if (!clippedSrcRect.intersect(srcRect)) {
200 clippedSrcRect.setEmpty();
201 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000202 SkAutoLockPixels alp(bitmap);
lsalzmana2415ac2016-10-11 14:29:12 -0700203 if (kAlpha_8_SkColorType == ct) {
204 for (int by = 0; by < bh; ++by) {
205 for (int bx = 0; bx < bw; ++bx) {
206 int devx = bx + srcRect.fLeft;
207 int devy = by + srcRect.fTop;
208 const uint8_t* alpha = bitmap.getAddr8(bx, by);
209
210 if (clippedSrcRect.contains(devx, devy)) {
211 if (checkCanvasPixels) {
212 uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy));
213 if (canvasAlpha != *alpha) {
214 ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
215 bx, by, canvasAlpha, *alpha);
216 return false;
217 }
218 }
219 } else if (checkBitmapPixels) {
220 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
221 if (origDstAlpha != *alpha) {
222 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
223 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
224 return false;
225 }
226 }
227 }
228 }
229 return true;
230 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000231 for (int by = 0; by < bh; ++by) {
232 for (int bx = 0; bx < bw; ++bx) {
233 int devx = bx + srcRect.fLeft;
234 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000236 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000237
238 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000239 if (checkCanvasPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700240 SkPMColor canvasPixel = get_src_color(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000241 bool didPremul;
bsalomone8d21e82015-07-16 08:23:13 -0700242 SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
bsalomon39826022015-07-23 08:07:21 -0700243 if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
egdaniel88e8aef2016-06-27 14:34:55 -0700244 ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
245 "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000246 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000247 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000249 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700250 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
251 if (origDstPixel != *pixel) {
252 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
253 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000254 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000255 }
256 }
257 }
258 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000259 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000260}
261
262enum BitmapInit {
263 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264
bsalomon@google.comc6980972011-11-02 19:57:21 +0000265 kNoPixels_BitmapInit = kFirstBitmapInit,
266 kTight_BitmapInit,
267 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800268 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000269
bsalomon9d02b262016-02-01 12:49:30 -0800270 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500271
272#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800273 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500274#else
275 kLast_BitmapInit = kLastAligned_BitmapInit
276#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000277};
278
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000279static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000280 int x = bmi;
281 return static_cast<BitmapInit>(++x);
282}
283
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000284static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
285 SkAlphaType at) {
286 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000287 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000288 bool alloc = true;
289 switch (init) {
290 case kNoPixels_BitmapInit:
291 alloc = false;
292 case kTight_BitmapInit:
293 break;
294 case kRowBytes_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700295 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000296 break;
bsalomon9d02b262016-02-01 12:49:30 -0800297 case kRowBytesOdd_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700298 rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
bsalomon9d02b262016-02-01 12:49:30 -0800299 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000300 default:
301 SkASSERT(0);
302 break;
303 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000304
bsalomon@google.comc6980972011-11-02 19:57:21 +0000305 if (alloc) {
bsalomon9d02b262016-02-01 12:49:30 -0800306 bitmap->allocPixels(info, rowBytes);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000307 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000308 bitmap->setInfo(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000309 }
310}
311
kkinnunen15302832015-12-01 04:35:26 -0800312static const struct {
313 SkColorType fColorType;
314 SkAlphaType fAlphaType;
315} gReadPixelsConfigs[] = {
316 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
317 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
318 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
319 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
lsalzmana2415ac2016-10-11 14:29:12 -0700320 { kAlpha_8_SkColorType, kPremul_SkAlphaType },
kkinnunen15302832015-12-01 04:35:26 -0800321};
322const SkIRect gReadPixelsTestRects[] = {
323 // entire thing
324 DEV_RECT,
325 // larger on all sides
326 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
327 // fully contained
328 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
329 // outside top left
330 SkIRect::MakeLTRB(-10, -10, -1, -1),
331 // touching top left corner
332 SkIRect::MakeLTRB(-10, -10, 0, 0),
333 // overlapping top left corner
334 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
335 // overlapping top left and top right corners
336 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
337 // touching entire top edge
338 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
339 // overlapping top right corner
340 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
341 // contained in x, overlapping top edge
342 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
343 // outside top right corner
344 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
345 // touching top right corner
346 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
347 // overlapping top left and bottom left corners
348 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
349 // touching entire left edge
350 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
351 // overlapping bottom left corner
352 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
353 // contained in y, overlapping left edge
354 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
355 // outside bottom left corner
356 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
357 // touching bottom left corner
358 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
359 // overlapping bottom left and bottom right corners
360 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
361 // touching entire left edge
362 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
363 // overlapping bottom right corner
364 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
365 // overlapping top right and bottom right corners
366 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
367};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000368
reede8f30622016-03-23 18:59:25 -0700369static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
bsalomon9d02b262016-02-01 12:49:30 -0800370 BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800371 SkCanvas* canvas = surface->getCanvas();
372 fill_src_canvas(canvas);
373 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
374 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800375 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800376 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
377 SkBitmap bmp;
378 init_bitmap(&bmp, srcRect, bmi,
379 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700380
kkinnunen15302832015-12-01 04:35:26 -0800381 // if the bitmap has pixels allocated before the readPixels,
382 // note that and fill them with pattern
383 bool startsWithPixels = !bmp.isNull();
384 if (startsWithPixels) {
385 fill_dst_bmp_with_init_data(&bmp);
386 }
387 uint32_t idBefore = surface->generationID();
388 bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
389 uint32_t idAfter = surface->generationID();
390
391 // we expect to succeed when the read isn't fully clipped
392 // out.
393 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
394 // determine whether we expected the read to succeed.
395 REPORTER_ASSERT(reporter, success == expectSuccess);
396 // read pixels should never change the gen id
397 REPORTER_ASSERT(reporter, idBefore == idAfter);
398
399 if (success || startsWithPixels) {
400 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
lsalzmana2415ac2016-10-11 14:29:12 -0700401 success, startsWithPixels,
402 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
kkinnunen15302832015-12-01 04:35:26 -0800403 } else {
404 // if we had no pixels beforehand and the readPixels
405 // failed then our bitmap should still not have pixels
406 REPORTER_ASSERT(reporter, bmp.isNull());
407 }
408 }
409 // check the old webkit version of readPixels that clips the
410 // bitmap size
411 SkBitmap wkbmp;
412 bool success = canvas->readPixels(srcRect, &wkbmp);
413 SkIRect clippedRect = DEV_RECT;
414 if (clippedRect.intersect(srcRect)) {
415 REPORTER_ASSERT(reporter, success);
416 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
417 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
418 check_read(reporter, wkbmp, clippedRect.fLeft,
lsalzmana2415ac2016-10-11 14:29:12 -0700419 clippedRect.fTop, true, false,
420 kN32_SkColorType, kPremul_SkAlphaType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000421 } else {
kkinnunen15302832015-12-01 04:35:26 -0800422 REPORTER_ASSERT(reporter, !success);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000423 }
kkinnunen15302832015-12-01 04:35:26 -0800424 }
425 }
426}
427DEF_TEST(ReadPixels, reporter) {
428 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700429 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800430 // SW readback fails a premul check when reading back to an unaligned rowbytes.
431 test_readpixels(reporter, surface, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800432}
bsalomone8d21e82015-07-16 08:23:13 -0700433#if SK_SUPPORT_GPU
egdaniel88e8aef2016-06-27 14:34:55 -0700434DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
robertphillips7e922762016-07-26 11:38:17 -0700435 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
kkinnunen15302832015-12-01 04:35:26 -0800436 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
robertphillips7e922762016-07-26 11:38:17 -0700437 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
438 ii, 0, origin, nullptr));
bsalomon9d02b262016-02-01 12:49:30 -0800439 test_readpixels(reporter, surface, kLast_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800440 }
441}
bsalomone8d21e82015-07-16 08:23:13 -0700442#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000443
bsalomone8d21e82015-07-16 08:23:13 -0700444#if SK_SUPPORT_GPU
Robert Phillipse78b7252017-04-06 07:59:41 -0400445static void test_readpixels_texture(skiatest::Reporter* reporter,
446 GrContext* context, sk_sp<GrTextureProxy> proxy) {
447 fill_src_texture(context, proxy.get());
kkinnunen15302832015-12-01 04:35:26 -0800448 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
449 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800450 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800451 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
452 SkBitmap bmp;
453 init_bitmap(&bmp, srcRect, bmi,
454 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
455
456 // if the bitmap has pixels allocated before the readPixels,
457 // note that and fill them with pattern
458 bool startsWithPixels = !bmp.isNull();
459 // Try doing the read directly from a non-renderable texture
460 if (startsWithPixels) {
461 fill_dst_bmp_with_init_data(&bmp);
Robert Phillipse78b7252017-04-06 07:59:41 -0400462 GrPixelConfig dstConfig = SkImageInfo2GrPixelConfig(bmp.info(),
463 *context->caps());
kkinnunen15302832015-12-01 04:35:26 -0800464 uint32_t flags = 0;
465 if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400466 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000467 }
kkinnunen15302832015-12-01 04:35:26 -0800468 bmp.lockPixels();
Robert Phillipse78b7252017-04-06 07:59:41 -0400469 bool success = context->contextPriv().readSurfacePixels(
470 proxy.get(), nullptr,
471 srcRect.fLeft, srcRect.fTop, bmp.width(),
472 bmp.height(), dstConfig, nullptr,
473 bmp.getPixels(), bmp.rowBytes(), flags);
kkinnunen15302832015-12-01 04:35:26 -0800474 bmp.unlockPixels();
475 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
lsalzmana2415ac2016-10-11 14:29:12 -0700476 success, true,
477 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000478 }
479 }
480 }
481 }
482}
egdaniel88e8aef2016-06-27 14:34:55 -0700483DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800484 // On the GPU we will also try reading back from a non-renderable texture.
Brian Salomon8b1fb742016-11-03 15:21:06 -0400485 for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
486 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
487 GrSurfaceDesc desc;
488 desc.fFlags = flags;
489 desc.fWidth = DEV_W;
490 desc.fHeight = DEV_H;
491 desc.fConfig = kSkia8888_GrPixelConfig;
492 desc.fOrigin = origin;
Robert Phillipse78b7252017-04-06 07:59:41 -0400493 sk_sp<GrTexture> texture =
494 ctxInfo.grContext()->resourceProvider()->createTexture(desc, SkBudgeted::kNo);
495 test_readpixels_texture(reporter, ctxInfo.grContext(),
496 GrSurfaceProxy::MakeWrapped(std::move(texture)));
Brian Salomon8b1fb742016-11-03 15:21:06 -0400497 }
kkinnunen15302832015-12-01 04:35:26 -0800498 }
499}
500#endif
Matt Sarett8572d852017-02-14 11:21:02 -0500501
502///////////////////////////////////////////////////////////////////////////////////////////////////
503
504static const uint32_t kNumPixels = 5;
505
506// The five reference pixels are: red, green, blue, white, black.
507// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
508// plus a tail pixel.
509static const uint32_t rgba[kNumPixels] = {
510 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
511};
512static const uint32_t bgra[kNumPixels] = {
513 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
514};
515static const uint16_t rgb565[kNumPixels] = {
516 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
517};
518
519static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
520
521static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
522static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
523static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
524static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
525static const uint64_t f16[kNumPixels] = {
526 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
527};
528
529#ifdef SK_PMCOLOR_IS_RGBA
530static const SkPMColor index8colors[kNumPixels] = {
531 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
532};
533#else
534static const SkPMColor index8colors[kNumPixels] = {
535 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
536};
537#endif
538static const uint8_t index8[kNumPixels] = { 0, 1, 2, 3, 4 };
539static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
540static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
541
542static const void* five_reference_pixels(SkColorType colorType) {
543 switch (colorType) {
544 case kUnknown_SkColorType:
545 return nullptr;
546 case kAlpha_8_SkColorType:
547 return alpha8;
548 case kRGB_565_SkColorType:
549 return rgb565;
550 case kARGB_4444_SkColorType:
551 return rgba4444;
552 case kRGBA_8888_SkColorType:
553 return rgba;
554 case kBGRA_8888_SkColorType:
555 return bgra;
556 case kIndex_8_SkColorType:
557 return index8;
558 case kGray_8_SkColorType:
559 return gray8;
560 case kRGBA_F16_SkColorType:
561 return f16;
562 }
563
564 SkASSERT(false);
565 return nullptr;
566}
567
568static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
569 const SkImageInfo& srcInfo) {
570 if (!SkImageInfoIsValid(srcInfo)) {
571 return;
572 }
573
574 sk_sp<SkColorTable> srcColorTable = (kIndex_8_SkColorType == srcInfo.colorType())
575 ? sk_make_sp<SkColorTable>(index8colors, 5)
576 : nullptr;
577 sk_sp<SkColorTable> dstColorTable = (kIndex_8_SkColorType == dstInfo.colorType())
578 ? sk_make_sp<SkColorTable>(index8colors, 5)
579 : nullptr;
580
581 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
582 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes(), srcColorTable.get());
583 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
584 REPORTER_ASSERT(r, src);
585
586 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
587 uint64_t dstPixels[kNumPixels];
588 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes(), dstColorTable.get());
589 bool success = src->readPixels(dstPixmap, 0, 0);
590 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
591
592 if (success) {
593 if (kGray_8_SkColorType == srcInfo.colorType() &&
594 kGray_8_SkColorType != dstInfo.colorType())
595 {
596 // This conversion is legal, but we won't get the "reference" pixels since we cannot
597 // represent colors in kGray8.
598 return;
599 }
600
601 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
602 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
603
604 }
605}
606
607DEF_TEST(ReadPixels_ValidConversion, reporter) {
608 const SkColorType kColorTypes[] = {
609 kUnknown_SkColorType,
610 kAlpha_8_SkColorType,
611 kRGB_565_SkColorType,
612 kARGB_4444_SkColorType,
613 kRGBA_8888_SkColorType,
614 kBGRA_8888_SkColorType,
615 kIndex_8_SkColorType,
616 kGray_8_SkColorType,
617 kRGBA_F16_SkColorType,
618 };
619
620 const SkAlphaType kAlphaTypes[] = {
621 kUnknown_SkAlphaType,
622 kOpaque_SkAlphaType,
623 kPremul_SkAlphaType,
624 kUnpremul_SkAlphaType,
625 };
626
627 const sk_sp<SkColorSpace> kColorSpaces[] = {
628 nullptr,
629 SkColorSpace::MakeSRGB(),
630 };
631
632 for (SkColorType dstCT : kColorTypes) {
633 for (SkAlphaType dstAT: kAlphaTypes) {
634 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
635 for (SkColorType srcCT : kColorTypes) {
636 for (SkAlphaType srcAT: kAlphaTypes) {
637 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
638 if (kRGBA_F16_SkColorType == dstCT && dstCS) {
639 dstCS = as_CSB(dstCS)->makeLinearGamma();
640 }
641
642 if (kRGBA_F16_SkColorType == srcCT && srcCS) {
643 srcCS = as_CSB(srcCS)->makeLinearGamma();
644 }
645
646 test_conversion(reporter,
647 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
648 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
649 }
650 }
651 }
652 }
653 }
654 }
655}