blob: aa16620a1be09ed522a8ef4d7dd633e1b8c483f8 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
Brian Salomon63a0a752020-06-26 13:32:09 -04009#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkImageInfoPriv.h"
Brian Salomon5dd77462019-09-26 16:57:09 -040012#include "include/utils/SkNWayCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkMathPriv.h"
Brian Salomonab32f652019-05-10 14:24:50 -040014#include "tests/Test.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040015
bsalomon@google.comc6980972011-11-02 19:57:21 +000016static const int DEV_W = 100, DEV_H = 100;
17static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000018static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000019 DEV_H * SK_Scalar1);
20
bsalomone8d21e82015-07-16 08:23:13 -070021static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000022 SkASSERT(x >= 0 && x < DEV_W);
23 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000024
25 U8CPU r = x;
26 U8CPU g = y;
27 U8CPU b = 0xc;
28
29 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000030 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000031 case 0:
32 a = 0xff;
33 break;
34 case 1:
35 a = 0x80;
36 break;
37 case 2:
38 a = 0xCC;
39 break;
40 case 4:
41 a = 0x01;
42 break;
43 case 3:
44 a = 0x00;
45 break;
46 }
47 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000048}
halcanary9d524f22016-03-29 09:03:52 -070049
bsalomone8d21e82015-07-16 08:23:13 -070050static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000051 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000052
bsalomon@google.comc6980972011-11-02 19:57:21 +000053 U8CPU b = n & 0xff;
54 U8CPU g = (n >> 8) & 0xff;
55 U8CPU r = (n >> 16) & 0xff;
56 return SkPackARGB32(0xff, r, g , b);
57}
58
Brian Salomon5fba7ad2018-03-22 10:01:16 -040059// TODO: Make this consider both ATs
bsalomone8d21e82015-07-16 08:23:13 -070060static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
61 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000062 *doUnpremul = (kUnpremul_SkAlphaType == at);
63
64 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000065 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000066 switch (ct) {
67 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000068 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000069 g = static_cast<U8CPU>(c[1]);
70 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000071 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000072 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040073 case kRGB_888x_SkColorType: // fallthrough
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000074 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000075 r = static_cast<U8CPU>(c[0]);
76 g = static_cast<U8CPU>(c[1]);
77 b = static_cast<U8CPU>(c[2]);
Brian Salomon5fba7ad2018-03-22 10:01:16 -040078 // We set this even when for kRGB_888x because our caller will validate that it is 0xff.
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000079 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000080 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000081 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000082 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000083 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000084 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000085
86 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000087 r = SkMulDiv255Ceiling(r, a);
88 g = SkMulDiv255Ceiling(g, a);
89 b = SkMulDiv255Ceiling(b, a);
90 }
91 return SkPackARGB32(a, r, g, b);
92}
93
Mike Reed34a0c972021-01-25 17:49:32 -050094static sk_sp<SkImage> make_src_image() {
bsalomon@google.comc6980972011-11-02 19:57:21 +000095 static SkBitmap bmp;
96 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -070097 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +000098 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
99 for (int y = 0; y < DEV_H; ++y) {
100 for (int x = 0; x < DEV_W; ++x) {
101 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700102 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000103 }
104 }
Mike Reed34a0c972021-01-25 17:49:32 -0500105 bmp.setImmutable();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000106 }
Mike Reed34a0c972021-01-25 17:49:32 -0500107 return bmp.asImage();
bsalomone8d21e82015-07-16 08:23:13 -0700108}
109
110static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000111 canvas->save();
112 canvas->setMatrix(SkMatrix::I());
Michael Ludwige4b79692020-09-16 13:55:05 -0400113 canvas->clipRect(DEV_RECT_S, SkClipOp::kIntersect);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000114 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700115 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reed34a0c972021-01-25 17:49:32 -0500116 canvas->drawImage(make_src_image(), 0, 0, SkSamplingOptions(), &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000117 canvas->restore();
118}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
bsalomone8d21e82015-07-16 08:23:13 -0700120static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000121 int w = bitmap->width();
122 int h = bitmap->height();
123 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
124 for (int y = 0; y < h; ++y) {
125 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700126 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
127 if (kAlpha_8_SkColorType == bitmap->colorType()) {
128 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
129 *alpha = SkGetPackedA32(initColor);
130 } else {
131 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
132 *pixel = initColor;
133 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000134 }
135 }
136}
137
bsalomone8d21e82015-07-16 08:23:13 -0700138static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000139 if (!didPremulConversion) {
140 return a == b;
141 }
142 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
143 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
144 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
145 int32_t aB = SkGetPackedB32(a);
146
147 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
148 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
149 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
150 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
151
152 return aA == bA &&
153 SkAbs32(aR - bR) <= 1 &&
154 SkAbs32(aG - bG) <= 1 &&
155 SkAbs32(aB - bB) <= 1;
156}
157
bsalomon@google.comc6980972011-11-02 19:57:21 +0000158// checks the bitmap contains correct pixels after the readPixels
159// if the bitmap was prefilled with pixels it checks that these weren't
160// overwritten in the area outside the readPixels.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400161static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
162 bool checkSurfacePixels, bool checkBitmapPixels,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400163 SkImageInfo surfaceInfo) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400164 SkAlphaType bmpAT = bitmap.alphaType();
165 SkColorType bmpCT = bitmap.colorType();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000166 SkASSERT(!bitmap.isNull());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400167 SkASSERT(checkSurfacePixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000168
bsalomon@google.comc6980972011-11-02 19:57:21 +0000169 int bw = bitmap.width();
170 int bh = bitmap.height();
171
172 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
173 SkIRect clippedSrcRect = DEV_RECT;
174 if (!clippedSrcRect.intersect(srcRect)) {
175 clippedSrcRect.setEmpty();
176 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400177 if (kAlpha_8_SkColorType == bmpCT) {
lsalzmana2415ac2016-10-11 14:29:12 -0700178 for (int by = 0; by < bh; ++by) {
179 for (int bx = 0; bx < bw; ++bx) {
180 int devx = bx + srcRect.fLeft;
181 int devy = by + srcRect.fTop;
182 const uint8_t* alpha = bitmap.getAddr8(bx, by);
183
184 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400185 if (checkSurfacePixels) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400186 uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400187 ? 0xFF
188 : SkGetPackedA32(get_src_color(devx, devy));
189 if (surfaceAlpha != *alpha) {
190 ERRORF(reporter,
191 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
192 bx, by, surfaceAlpha, *alpha);
lsalzmana2415ac2016-10-11 14:29:12 -0700193 return false;
194 }
195 }
196 } else if (checkBitmapPixels) {
197 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
198 if (origDstAlpha != *alpha) {
199 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
200 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
201 return false;
202 }
203 }
204 }
205 }
206 return true;
207 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000208 for (int by = 0; by < bh; ++by) {
209 for (int bx = 0; bx < bw; ++bx) {
210 int devx = bx + srcRect.fLeft;
211 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000212
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000213 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000214
215 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400216 if (checkSurfacePixels) {
217 SkPMColor surfacePMColor = get_src_color(devx, devy);
Mike Klein12d4b6e2018-08-17 14:09:55 -0400218 if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
219 surfacePMColor &= 0xFF000000;
220 }
221 if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400222 surfacePMColor |= 0xFF000000;
223 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000224 bool didPremul;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400225 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
226 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
227 ERRORF(reporter,
228 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
229 "Readback was unpremul: %d",
230 bx, by, surfacePMColor, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000231 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000232 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000233 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000234 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700235 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
236 if (origDstPixel != *pixel) {
237 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
238 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000239 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000240 }
241 }
242 }
243 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000244 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000245}
246
Brian Salomon5918e832019-10-23 13:34:36 -0400247enum class TightRowBytes : bool { kNo, kYes };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
Brian Salomon5918e832019-10-23 13:34:36 -0400249static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB,
250 SkColorType ct, SkAlphaType at) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400251 SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000252 size_t rowBytes = 0;
Brian Salomon5918e832019-10-23 13:34:36 -0400253 if (tightRB == TightRowBytes::kNo) {
254 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000255 }
Mike Reed12e946b2017-04-17 10:53:29 -0400256 bitmap->allocPixels(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000257}
258
kkinnunen15302832015-12-01 04:35:26 -0800259static const struct {
260 SkColorType fColorType;
261 SkAlphaType fAlphaType;
262} gReadPixelsConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400263 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
264 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
265 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
266 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
267 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
268 {kAlpha_8_SkColorType, kPremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800269};
270const SkIRect gReadPixelsTestRects[] = {
271 // entire thing
272 DEV_RECT,
273 // larger on all sides
274 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
275 // fully contained
276 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
277 // outside top left
278 SkIRect::MakeLTRB(-10, -10, -1, -1),
279 // touching top left corner
280 SkIRect::MakeLTRB(-10, -10, 0, 0),
281 // overlapping top left corner
282 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
283 // overlapping top left and top right corners
284 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
285 // touching entire top edge
286 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
287 // overlapping top right corner
288 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
289 // contained in x, overlapping top edge
290 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
291 // outside top right corner
292 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
293 // touching top right corner
294 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
295 // overlapping top left and bottom left corners
296 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
297 // touching entire left edge
298 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
299 // overlapping bottom left corner
300 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
301 // contained in y, overlapping left edge
302 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
303 // outside bottom left corner
304 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
305 // touching bottom left corner
306 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
307 // overlapping bottom left and bottom right corners
308 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
309 // touching entire left edge
310 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
311 // overlapping bottom right corner
312 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
313 // overlapping top right and bottom right corners
314 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
315};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000316
Brian Salomon1d435302019-07-01 13:05:28 -0400317bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
318 const SkImageInfo& srcInfo) {
319 return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400320}
321
reede8f30622016-03-23 18:59:25 -0700322static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
Brian Salomon5918e832019-10-23 13:34:36 -0400323 const SkImageInfo& surfaceInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800324 SkCanvas* canvas = surface->getCanvas();
325 fill_src_canvas(canvas);
326 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
327 const SkIRect& srcRect = gReadPixelsTestRects[rect];
Brian Salomon5918e832019-10-23 13:34:36 -0400328 for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
kkinnunen15302832015-12-01 04:35:26 -0800329 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
330 SkBitmap bmp;
Brian Salomon5918e832019-10-23 13:34:36 -0400331 init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
332 gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700333
kkinnunen15302832015-12-01 04:35:26 -0800334 // if the bitmap has pixels allocated before the readPixels,
335 // note that and fill them with pattern
336 bool startsWithPixels = !bmp.isNull();
337 if (startsWithPixels) {
338 fill_dst_bmp_with_init_data(&bmp);
339 }
340 uint32_t idBefore = surface->generationID();
Mike Reedf1942192017-07-21 14:24:29 -0400341 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
kkinnunen15302832015-12-01 04:35:26 -0800342 uint32_t idAfter = surface->generationID();
343
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400344 // we expect to succeed when the read isn't fully clipped out and the infos are
345 // compatible.
Brian Salomon1d435302019-07-01 13:05:28 -0400346 bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800347 // determine whether we expected the read to succeed.
Brian Salomon1d435302019-07-01 13:05:28 -0400348 REPORTER_ASSERT(reporter, expectSuccess == success,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400349 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
350 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
351 bmp.info().colorType(), bmp.info().alphaType());
kkinnunen15302832015-12-01 04:35:26 -0800352 // read pixels should never change the gen id
353 REPORTER_ASSERT(reporter, idBefore == idAfter);
354
355 if (success || startsWithPixels) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400356 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400357 startsWithPixels, surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800358 } else {
359 // if we had no pixels beforehand and the readPixels
360 // failed then our bitmap should still not have pixels
361 REPORTER_ASSERT(reporter, bmp.isNull());
362 }
363 }
kkinnunen15302832015-12-01 04:35:26 -0800364 }
365 }
366}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400367
kkinnunen15302832015-12-01 04:35:26 -0800368DEF_TEST(ReadPixels, reporter) {
369 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700370 auto surface(SkSurface::MakeRaster(info));
Brian Salomon5918e832019-10-23 13:34:36 -0400371 test_readpixels(reporter, surface, info);
kkinnunen15302832015-12-01 04:35:26 -0800372}
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000373
Matt Sarett8572d852017-02-14 11:21:02 -0500374///////////////////////////////////////////////////////////////////////////////////////////////////
375
376static const uint32_t kNumPixels = 5;
377
378// The five reference pixels are: red, green, blue, white, black.
379// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
380// plus a tail pixel.
381static const uint32_t rgba[kNumPixels] = {
382 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
383};
384static const uint32_t bgra[kNumPixels] = {
385 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
386};
387static const uint16_t rgb565[kNumPixels] = {
388 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
389};
390
391static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
392
393static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
394static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
395static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
396static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
397static const uint64_t f16[kNumPixels] = {
398 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
399};
400
Matt Sarett8572d852017-02-14 11:21:02 -0500401static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
402static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
403
404static const void* five_reference_pixels(SkColorType colorType) {
405 switch (colorType) {
406 case kUnknown_SkColorType:
407 return nullptr;
408 case kAlpha_8_SkColorType:
409 return alpha8;
410 case kRGB_565_SkColorType:
411 return rgb565;
412 case kARGB_4444_SkColorType:
413 return rgba4444;
414 case kRGBA_8888_SkColorType:
415 return rgba;
416 case kBGRA_8888_SkColorType:
417 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500418 case kGray_8_SkColorType:
419 return gray8;
420 case kRGBA_F16_SkColorType:
421 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400422 default:
Leon Scroggins IIId6832d02018-09-04 11:10:04 -0400423 return nullptr;
Matt Sarett8572d852017-02-14 11:21:02 -0500424 }
425
426 SkASSERT(false);
427 return nullptr;
428}
429
430static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
431 const SkImageInfo& srcInfo) {
Brian Osmane1adc3a2018-06-04 09:21:17 -0400432 if (!SkImageInfoIsValid(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500433 return;
434 }
435
Matt Sarett8572d852017-02-14 11:21:02 -0500436 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400437 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500438 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
439 REPORTER_ASSERT(r, src);
440
441 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
442 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400443 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400444 bool success = src->readPixels(nullptr, dstPixmap, 0, 0);
Matt Sarett8572d852017-02-14 11:21:02 -0500445 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
446
447 if (success) {
448 if (kGray_8_SkColorType == srcInfo.colorType() &&
Mike Klein12d4b6e2018-08-17 14:09:55 -0400449 kGray_8_SkColorType != dstInfo.colorType()) {
450 // TODO: test (r,g,b) == (gray,gray,gray)?
451 return;
452 }
453
454 if (kGray_8_SkColorType == dstInfo.colorType() &&
455 kGray_8_SkColorType != srcInfo.colorType()) {
456 // TODO: test gray = luminance?
457 return;
458 }
459
460 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
461 kAlpha_8_SkColorType != dstInfo.colorType()) {
462 // TODO: test output = black with this alpha?
Matt Sarett8572d852017-02-14 11:21:02 -0500463 return;
464 }
465
466 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
467 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
Matt Sarett8572d852017-02-14 11:21:02 -0500468 }
469}
470
471DEF_TEST(ReadPixels_ValidConversion, reporter) {
472 const SkColorType kColorTypes[] = {
473 kUnknown_SkColorType,
474 kAlpha_8_SkColorType,
475 kRGB_565_SkColorType,
476 kARGB_4444_SkColorType,
477 kRGBA_8888_SkColorType,
478 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500479 kGray_8_SkColorType,
480 kRGBA_F16_SkColorType,
481 };
482
483 const SkAlphaType kAlphaTypes[] = {
484 kUnknown_SkAlphaType,
485 kOpaque_SkAlphaType,
486 kPremul_SkAlphaType,
487 kUnpremul_SkAlphaType,
488 };
489
490 const sk_sp<SkColorSpace> kColorSpaces[] = {
491 nullptr,
492 SkColorSpace::MakeSRGB(),
493 };
494
495 for (SkColorType dstCT : kColorTypes) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400496 for (SkAlphaType dstAT : kAlphaTypes) {
497 for (const sk_sp<SkColorSpace>& dstCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500498 for (SkColorType srcCT : kColorTypes) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400499 for (SkAlphaType srcAT : kAlphaTypes) {
500 for (const sk_sp<SkColorSpace>& srcCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500501 test_conversion(reporter,
502 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
503 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
504 }
505 }
506 }
507 }
508 }
509 }
510}
Brian Salomonc24c8ef2021-02-01 13:32:30 -0500511
512DEF_TEST(ReadPixels_InvalidRowBytes, reporter) {
513 auto srcII = SkImageInfo::Make({10, 10}, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
514 auto surf = SkSurface::MakeRaster(srcII);
515 for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) {
516 auto colorType = static_cast<SkColorType>(ct);
517 size_t bpp = SkColorTypeBytesPerPixel(colorType);
518 if (bpp <= 1) {
519 continue;
520 }
521 auto dstII = srcII.makeColorType(colorType);
522 size_t badRowBytes = (surf->width() + 1)*bpp - 1;
523 auto storage = std::make_unique<char[]>(badRowBytes*surf->height());
524 REPORTER_ASSERT(reporter, !surf->readPixels(dstII, storage.get(), badRowBytes, 0, 0));
525 }
526}