blob: 774f0af06aa53e4f053cd52e288df09294deb5df [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"
Brian Osman32342f02017-03-04 08:12:46 -050019#include "GrResourceProvider.h"
bsalomone8d21e82015-07-16 08:23:13 -070020#include "SkGr.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000021#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000022
kkinnunen15302832015-12-01 04:35:26 -080023#include <initializer_list>
24
bsalomon@google.comc6980972011-11-02 19:57:21 +000025static const int DEV_W = 100, DEV_H = 100;
26static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000027static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000028 DEV_H * SK_Scalar1);
29
bsalomone8d21e82015-07-16 08:23:13 -070030static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000031 SkASSERT(x >= 0 && x < DEV_W);
32 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000033
34 U8CPU r = x;
35 U8CPU g = y;
36 U8CPU b = 0xc;
37
38 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000039 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000040 case 0:
41 a = 0xff;
42 break;
43 case 1:
44 a = 0x80;
45 break;
46 case 2:
47 a = 0xCC;
48 break;
49 case 4:
50 a = 0x01;
51 break;
52 case 3:
53 a = 0x00;
54 break;
55 }
56 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000057}
halcanary9d524f22016-03-29 09:03:52 -070058
bsalomone8d21e82015-07-16 08:23:13 -070059static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000060 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000061
bsalomon@google.comc6980972011-11-02 19:57:21 +000062 U8CPU b = n & 0xff;
63 U8CPU g = (n >> 8) & 0xff;
64 U8CPU r = (n >> 16) & 0xff;
65 return SkPackARGB32(0xff, r, g , b);
66}
67
bsalomone8d21e82015-07-16 08:23:13 -070068static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
69 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000070 *doUnpremul = (kUnpremul_SkAlphaType == at);
71
72 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000073 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000074 switch (ct) {
75 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000076 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000077 g = static_cast<U8CPU>(c[1]);
78 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000079 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000080 break;
81 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000082 r = static_cast<U8CPU>(c[0]);
83 g = static_cast<U8CPU>(c[1]);
84 b = static_cast<U8CPU>(c[2]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000085 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000086 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000087 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000088 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000089 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000090 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000091
92 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000093 r = SkMulDiv255Ceiling(r, a);
94 g = SkMulDiv255Ceiling(g, a);
95 b = SkMulDiv255Ceiling(b, a);
96 }
97 return SkPackARGB32(a, r, g, b);
98}
99
bsalomone8d21e82015-07-16 08:23:13 -0700100static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000101 static SkBitmap bmp;
102 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700103 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000104 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
105 for (int y = 0; y < DEV_H; ++y) {
106 for (int x = 0; x < DEV_W; ++x) {
107 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700108 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000109 }
110 }
111 }
bsalomone8d21e82015-07-16 08:23:13 -0700112 return bmp;
113}
114
115static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000116 canvas->save();
117 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500118 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000119 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700120 paint.setBlendMode(SkBlendMode::kSrc);
bsalomone8d21e82015-07-16 08:23:13 -0700121 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000122 canvas->restore();
123}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124
bsalomone8d21e82015-07-16 08:23:13 -0700125#if SK_SUPPORT_GPU
Robert Phillips1b352562017-04-05 18:56:21 +0000126static void fill_src_texture(GrTexture* texture) {
bsalomone8d21e82015-07-16 08:23:13 -0700127 SkBitmap bmp = make_src_bitmap();
128 bmp.lockPixels();
Robert Phillips1b352562017-04-05 18:56:21 +0000129 texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(),
130 bmp.rowBytes());
bsalomone8d21e82015-07-16 08:23:13 -0700131 bmp.unlockPixels();
132}
133#endif
134
135static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000136 SkAutoLockPixels alp(*bitmap);
137 int w = bitmap->width();
138 int h = bitmap->height();
139 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
140 for (int y = 0; y < h; ++y) {
141 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700142 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
143 if (kAlpha_8_SkColorType == bitmap->colorType()) {
144 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
145 *alpha = SkGetPackedA32(initColor);
146 } else {
147 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
148 *pixel = initColor;
149 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000150 }
151 }
152}
153
bsalomone8d21e82015-07-16 08:23:13 -0700154static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000155 if (!didPremulConversion) {
156 return a == b;
157 }
158 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
159 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
160 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
161 int32_t aB = SkGetPackedB32(a);
162
163 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
164 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
165 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
166 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
167
168 return aA == bA &&
169 SkAbs32(aR - bR) <= 1 &&
170 SkAbs32(aG - bG) <= 1 &&
171 SkAbs32(aB - bB) <= 1;
172}
173
bsalomon@google.comc6980972011-11-02 19:57:21 +0000174// checks the bitmap contains correct pixels after the readPixels
175// if the bitmap was prefilled with pixels it checks that these weren't
176// overwritten in the area outside the readPixels.
bsalomone8d21e82015-07-16 08:23:13 -0700177static bool check_read(skiatest::Reporter* reporter,
178 const SkBitmap& bitmap,
179 int x, int y,
180 bool checkCanvasPixels,
lsalzmana2415ac2016-10-11 14:29:12 -0700181 bool checkBitmapPixels,
182 SkColorType ct,
183 SkAlphaType at) {
184 SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000185 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000186 SkASSERT(checkCanvasPixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
bsalomon@google.comc6980972011-11-02 19:57:21 +0000188 int bw = bitmap.width();
189 int bh = bitmap.height();
190
191 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
192 SkIRect clippedSrcRect = DEV_RECT;
193 if (!clippedSrcRect.intersect(srcRect)) {
194 clippedSrcRect.setEmpty();
195 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000196 SkAutoLockPixels alp(bitmap);
lsalzmana2415ac2016-10-11 14:29:12 -0700197 if (kAlpha_8_SkColorType == ct) {
198 for (int by = 0; by < bh; ++by) {
199 for (int bx = 0; bx < bw; ++bx) {
200 int devx = bx + srcRect.fLeft;
201 int devy = by + srcRect.fTop;
202 const uint8_t* alpha = bitmap.getAddr8(bx, by);
203
204 if (clippedSrcRect.contains(devx, devy)) {
205 if (checkCanvasPixels) {
206 uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy));
207 if (canvasAlpha != *alpha) {
208 ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
209 bx, by, canvasAlpha, *alpha);
210 return false;
211 }
212 }
213 } else if (checkBitmapPixels) {
214 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
215 if (origDstAlpha != *alpha) {
216 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
217 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
218 return false;
219 }
220 }
221 }
222 }
223 return true;
224 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000225 for (int by = 0; by < bh; ++by) {
226 for (int bx = 0; bx < bw; ++bx) {
227 int devx = bx + srcRect.fLeft;
228 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000229
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000230 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000231
232 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000233 if (checkCanvasPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700234 SkPMColor canvasPixel = get_src_color(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000235 bool didPremul;
bsalomone8d21e82015-07-16 08:23:13 -0700236 SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
bsalomon39826022015-07-23 08:07:21 -0700237 if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
egdaniel88e8aef2016-06-27 14:34:55 -0700238 ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
239 "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000240 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000241 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000242 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000243 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700244 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
245 if (origDstPixel != *pixel) {
246 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
247 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000248 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000249 }
250 }
251 }
252 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000253 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000254}
255
256enum BitmapInit {
257 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258
bsalomon@google.comc6980972011-11-02 19:57:21 +0000259 kNoPixels_BitmapInit = kFirstBitmapInit,
260 kTight_BitmapInit,
261 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800262 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263
bsalomon9d02b262016-02-01 12:49:30 -0800264 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500265
266#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800267 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500268#else
269 kLast_BitmapInit = kLastAligned_BitmapInit
270#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000271};
272
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000273static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000274 int x = bmi;
275 return static_cast<BitmapInit>(++x);
276}
277
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000278static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
279 SkAlphaType at) {
280 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000281 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000282 bool alloc = true;
283 switch (init) {
284 case kNoPixels_BitmapInit:
285 alloc = false;
286 case kTight_BitmapInit:
287 break;
288 case kRowBytes_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700289 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000290 break;
bsalomon9d02b262016-02-01 12:49:30 -0800291 case kRowBytesOdd_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700292 rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
bsalomon9d02b262016-02-01 12:49:30 -0800293 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000294 default:
295 SkASSERT(0);
296 break;
297 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000298
bsalomon@google.comc6980972011-11-02 19:57:21 +0000299 if (alloc) {
bsalomon9d02b262016-02-01 12:49:30 -0800300 bitmap->allocPixels(info, rowBytes);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000301 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000302 bitmap->setInfo(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000303 }
304}
305
kkinnunen15302832015-12-01 04:35:26 -0800306static const struct {
307 SkColorType fColorType;
308 SkAlphaType fAlphaType;
309} gReadPixelsConfigs[] = {
310 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
311 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
312 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
313 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
lsalzmana2415ac2016-10-11 14:29:12 -0700314 { kAlpha_8_SkColorType, kPremul_SkAlphaType },
kkinnunen15302832015-12-01 04:35:26 -0800315};
316const SkIRect gReadPixelsTestRects[] = {
317 // entire thing
318 DEV_RECT,
319 // larger on all sides
320 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
321 // fully contained
322 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
323 // outside top left
324 SkIRect::MakeLTRB(-10, -10, -1, -1),
325 // touching top left corner
326 SkIRect::MakeLTRB(-10, -10, 0, 0),
327 // overlapping top left corner
328 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
329 // overlapping top left and top right corners
330 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
331 // touching entire top edge
332 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
333 // overlapping top right corner
334 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
335 // contained in x, overlapping top edge
336 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
337 // outside top right corner
338 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
339 // touching top right corner
340 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
341 // overlapping top left and bottom left corners
342 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
343 // touching entire left edge
344 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
345 // overlapping bottom left corner
346 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
347 // contained in y, overlapping left edge
348 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
349 // outside bottom left corner
350 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
351 // touching bottom left corner
352 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
353 // overlapping bottom left and bottom right corners
354 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
355 // touching entire left edge
356 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
357 // overlapping bottom right corner
358 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
359 // overlapping top right and bottom right corners
360 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
361};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000362
reede8f30622016-03-23 18:59:25 -0700363static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
bsalomon9d02b262016-02-01 12:49:30 -0800364 BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800365 SkCanvas* canvas = surface->getCanvas();
366 fill_src_canvas(canvas);
367 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
368 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800369 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800370 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
371 SkBitmap bmp;
372 init_bitmap(&bmp, srcRect, bmi,
373 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700374
kkinnunen15302832015-12-01 04:35:26 -0800375 // if the bitmap has pixels allocated before the readPixels,
376 // note that and fill them with pattern
377 bool startsWithPixels = !bmp.isNull();
378 if (startsWithPixels) {
379 fill_dst_bmp_with_init_data(&bmp);
380 }
381 uint32_t idBefore = surface->generationID();
382 bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
383 uint32_t idAfter = surface->generationID();
384
385 // we expect to succeed when the read isn't fully clipped
386 // out.
387 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
388 // determine whether we expected the read to succeed.
389 REPORTER_ASSERT(reporter, success == expectSuccess);
390 // read pixels should never change the gen id
391 REPORTER_ASSERT(reporter, idBefore == idAfter);
392
393 if (success || startsWithPixels) {
394 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
lsalzmana2415ac2016-10-11 14:29:12 -0700395 success, startsWithPixels,
396 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
kkinnunen15302832015-12-01 04:35:26 -0800397 } 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 }
402 }
403 // check the old webkit version of readPixels that clips the
404 // bitmap size
405 SkBitmap wkbmp;
406 bool success = canvas->readPixels(srcRect, &wkbmp);
407 SkIRect clippedRect = DEV_RECT;
408 if (clippedRect.intersect(srcRect)) {
409 REPORTER_ASSERT(reporter, success);
410 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
411 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
412 check_read(reporter, wkbmp, clippedRect.fLeft,
lsalzmana2415ac2016-10-11 14:29:12 -0700413 clippedRect.fTop, true, false,
414 kN32_SkColorType, kPremul_SkAlphaType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000415 } else {
kkinnunen15302832015-12-01 04:35:26 -0800416 REPORTER_ASSERT(reporter, !success);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000417 }
kkinnunen15302832015-12-01 04:35:26 -0800418 }
419 }
420}
421DEF_TEST(ReadPixels, reporter) {
422 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700423 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800424 // SW readback fails a premul check when reading back to an unaligned rowbytes.
425 test_readpixels(reporter, surface, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800426}
bsalomone8d21e82015-07-16 08:23:13 -0700427#if SK_SUPPORT_GPU
egdaniel88e8aef2016-06-27 14:34:55 -0700428DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
robertphillips7e922762016-07-26 11:38:17 -0700429 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
kkinnunen15302832015-12-01 04:35:26 -0800430 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
robertphillips7e922762016-07-26 11:38:17 -0700431 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
432 ii, 0, origin, nullptr));
bsalomon9d02b262016-02-01 12:49:30 -0800433 test_readpixels(reporter, surface, kLast_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800434 }
435}
bsalomone8d21e82015-07-16 08:23:13 -0700436#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000437
bsalomone8d21e82015-07-16 08:23:13 -0700438#if SK_SUPPORT_GPU
Robert Phillips1b352562017-04-05 18:56:21 +0000439static void test_readpixels_texture(skiatest::Reporter* reporter, GrTexture* texture) {
440 fill_src_texture(texture);
kkinnunen15302832015-12-01 04:35:26 -0800441 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
442 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800443 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800444 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
445 SkBitmap bmp;
446 init_bitmap(&bmp, srcRect, bmi,
447 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
448
449 // if the bitmap has pixels allocated before the readPixels,
450 // note that and fill them with pattern
451 bool startsWithPixels = !bmp.isNull();
452 // Try doing the read directly from a non-renderable texture
453 if (startsWithPixels) {
454 fill_dst_bmp_with_init_data(&bmp);
Robert Phillips1b352562017-04-05 18:56:21 +0000455 GrPixelConfig dstConfig =
456 SkImageInfo2GrPixelConfig(bmp.info(), *texture->getContext()->caps());
kkinnunen15302832015-12-01 04:35:26 -0800457 uint32_t flags = 0;
458 if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
Robert Phillips1b352562017-04-05 18:56:21 +0000459 flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000460 }
kkinnunen15302832015-12-01 04:35:26 -0800461 bmp.lockPixels();
Robert Phillips1b352562017-04-05 18:56:21 +0000462 bool success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(),
463 bmp.height(), dstConfig, bmp.getPixels(),
464 bmp.rowBytes(), flags);
kkinnunen15302832015-12-01 04:35:26 -0800465 bmp.unlockPixels();
466 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
lsalzmana2415ac2016-10-11 14:29:12 -0700467 success, true,
468 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000469 }
470 }
471 }
472 }
473}
egdaniel88e8aef2016-06-27 14:34:55 -0700474DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800475 // On the GPU we will also try reading back from a non-renderable texture.
Brian Salomon8b1fb742016-11-03 15:21:06 -0400476 for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
477 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
478 GrSurfaceDesc desc;
479 desc.fFlags = flags;
480 desc.fWidth = DEV_W;
481 desc.fHeight = DEV_H;
482 desc.fConfig = kSkia8888_GrPixelConfig;
483 desc.fOrigin = origin;
Robert Phillips1b352562017-04-05 18:56:21 +0000484 sk_sp<GrTexture> texture(ctxInfo.grContext()->resourceProvider()->createTexture(desc,
485 SkBudgeted::kNo));
486 test_readpixels_texture(reporter, texture.get());
Brian Salomon8b1fb742016-11-03 15:21:06 -0400487 }
kkinnunen15302832015-12-01 04:35:26 -0800488 }
489}
490#endif
Matt Sarett8572d852017-02-14 11:21:02 -0500491
492///////////////////////////////////////////////////////////////////////////////////////////////////
493
494static const uint32_t kNumPixels = 5;
495
496// The five reference pixels are: red, green, blue, white, black.
497// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
498// plus a tail pixel.
499static const uint32_t rgba[kNumPixels] = {
500 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
501};
502static const uint32_t bgra[kNumPixels] = {
503 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
504};
505static const uint16_t rgb565[kNumPixels] = {
506 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
507};
508
509static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
510
511static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
512static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
513static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
514static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
515static const uint64_t f16[kNumPixels] = {
516 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
517};
518
519#ifdef SK_PMCOLOR_IS_RGBA
520static const SkPMColor index8colors[kNumPixels] = {
521 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
522};
523#else
524static const SkPMColor index8colors[kNumPixels] = {
525 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
526};
527#endif
528static const uint8_t index8[kNumPixels] = { 0, 1, 2, 3, 4 };
529static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
530static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
531
532static const void* five_reference_pixels(SkColorType colorType) {
533 switch (colorType) {
534 case kUnknown_SkColorType:
535 return nullptr;
536 case kAlpha_8_SkColorType:
537 return alpha8;
538 case kRGB_565_SkColorType:
539 return rgb565;
540 case kARGB_4444_SkColorType:
541 return rgba4444;
542 case kRGBA_8888_SkColorType:
543 return rgba;
544 case kBGRA_8888_SkColorType:
545 return bgra;
546 case kIndex_8_SkColorType:
547 return index8;
548 case kGray_8_SkColorType:
549 return gray8;
550 case kRGBA_F16_SkColorType:
551 return f16;
552 }
553
554 SkASSERT(false);
555 return nullptr;
556}
557
558static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
559 const SkImageInfo& srcInfo) {
560 if (!SkImageInfoIsValid(srcInfo)) {
561 return;
562 }
563
564 sk_sp<SkColorTable> srcColorTable = (kIndex_8_SkColorType == srcInfo.colorType())
565 ? sk_make_sp<SkColorTable>(index8colors, 5)
566 : nullptr;
567 sk_sp<SkColorTable> dstColorTable = (kIndex_8_SkColorType == dstInfo.colorType())
568 ? sk_make_sp<SkColorTable>(index8colors, 5)
569 : nullptr;
570
571 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
572 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes(), srcColorTable.get());
573 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
574 REPORTER_ASSERT(r, src);
575
576 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
577 uint64_t dstPixels[kNumPixels];
578 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes(), dstColorTable.get());
579 bool success = src->readPixels(dstPixmap, 0, 0);
580 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
581
582 if (success) {
583 if (kGray_8_SkColorType == srcInfo.colorType() &&
584 kGray_8_SkColorType != dstInfo.colorType())
585 {
586 // This conversion is legal, but we won't get the "reference" pixels since we cannot
587 // represent colors in kGray8.
588 return;
589 }
590
591 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
592 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
593
594 }
595}
596
597DEF_TEST(ReadPixels_ValidConversion, reporter) {
598 const SkColorType kColorTypes[] = {
599 kUnknown_SkColorType,
600 kAlpha_8_SkColorType,
601 kRGB_565_SkColorType,
602 kARGB_4444_SkColorType,
603 kRGBA_8888_SkColorType,
604 kBGRA_8888_SkColorType,
605 kIndex_8_SkColorType,
606 kGray_8_SkColorType,
607 kRGBA_F16_SkColorType,
608 };
609
610 const SkAlphaType kAlphaTypes[] = {
611 kUnknown_SkAlphaType,
612 kOpaque_SkAlphaType,
613 kPremul_SkAlphaType,
614 kUnpremul_SkAlphaType,
615 };
616
617 const sk_sp<SkColorSpace> kColorSpaces[] = {
618 nullptr,
619 SkColorSpace::MakeSRGB(),
620 };
621
622 for (SkColorType dstCT : kColorTypes) {
623 for (SkAlphaType dstAT: kAlphaTypes) {
624 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
625 for (SkColorType srcCT : kColorTypes) {
626 for (SkAlphaType srcAT: kAlphaTypes) {
627 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
628 if (kRGBA_F16_SkColorType == dstCT && dstCS) {
629 dstCS = as_CSB(dstCS)->makeLinearGamma();
630 }
631
632 if (kRGBA_F16_SkColorType == srcCT && srcCS) {
633 srcCS = as_CSB(srcCS)->makeLinearGamma();
634 }
635
636 test_conversion(reporter,
637 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
638 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
639 }
640 }
641 }
642 }
643 }
644 }
645}