blob: 64894a91ef143f9c97859f04a9a7b0595a314abe [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 Salomon5392c942021-03-30 16:14:37 -04009#include "include/core/SkColorPriv.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040010#include "include/core/SkImage.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkSurface.h"
Brian Salomon5392c942021-03-30 16:14:37 -040012#include "include/private/SkColorData.h"
13#include "include/private/SkHalf.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkImageInfoPriv.h"
Brian Salomon5dd77462019-09-26 16:57:09 -040015#include "include/utils/SkNWayCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkMathPriv.h"
Brian Salomonab32f652019-05-10 14:24:50 -040017#include "tests/Test.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040018
bsalomon@google.comc6980972011-11-02 19:57:21 +000019static const int DEV_W = 100, DEV_H = 100;
20static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000021static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000022 DEV_H * SK_Scalar1);
23
bsalomone8d21e82015-07-16 08:23:13 -070024static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000025 SkASSERT(x >= 0 && x < DEV_W);
26 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000027
28 U8CPU r = x;
29 U8CPU g = y;
30 U8CPU b = 0xc;
31
32 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000033 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000034 case 0:
35 a = 0xff;
36 break;
37 case 1:
38 a = 0x80;
39 break;
40 case 2:
41 a = 0xCC;
42 break;
43 case 4:
44 a = 0x01;
45 break;
46 case 3:
47 a = 0x00;
48 break;
49 }
50 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000051}
halcanary9d524f22016-03-29 09:03:52 -070052
bsalomone8d21e82015-07-16 08:23:13 -070053static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000054 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000055
bsalomon@google.comc6980972011-11-02 19:57:21 +000056 U8CPU b = n & 0xff;
57 U8CPU g = (n >> 8) & 0xff;
58 U8CPU r = (n >> 16) & 0xff;
59 return SkPackARGB32(0xff, r, g , b);
60}
61
Brian Salomon5fba7ad2018-03-22 10:01:16 -040062// TODO: Make this consider both ATs
bsalomone8d21e82015-07-16 08:23:13 -070063static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
64 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000065 *doUnpremul = (kUnpremul_SkAlphaType == at);
66
67 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000068 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000069 switch (ct) {
70 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000071 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000072 g = static_cast<U8CPU>(c[1]);
73 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000074 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000075 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040076 case kRGB_888x_SkColorType: // fallthrough
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000077 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000078 r = static_cast<U8CPU>(c[0]);
79 g = static_cast<U8CPU>(c[1]);
80 b = static_cast<U8CPU>(c[2]);
Brian Salomon5fba7ad2018-03-22 10:01:16 -040081 // 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 +000082 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000083 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000084 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000085 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000086 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000087 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000088
89 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000090 r = SkMulDiv255Ceiling(r, a);
91 g = SkMulDiv255Ceiling(g, a);
92 b = SkMulDiv255Ceiling(b, a);
93 }
94 return SkPackARGB32(a, r, g, b);
95}
96
Mike Reed34a0c972021-01-25 17:49:32 -050097static sk_sp<SkImage> make_src_image() {
bsalomon@google.comc6980972011-11-02 19:57:21 +000098 static SkBitmap bmp;
99 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700100 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000101 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
102 for (int y = 0; y < DEV_H; ++y) {
103 for (int x = 0; x < DEV_W; ++x) {
104 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700105 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000106 }
107 }
Mike Reed34a0c972021-01-25 17:49:32 -0500108 bmp.setImmutable();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000109 }
Mike Reed34a0c972021-01-25 17:49:32 -0500110 return bmp.asImage();
bsalomone8d21e82015-07-16 08:23:13 -0700111}
112
113static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000114 canvas->save();
115 canvas->setMatrix(SkMatrix::I());
Michael Ludwige4b79692020-09-16 13:55:05 -0400116 canvas->clipRect(DEV_RECT_S, SkClipOp::kIntersect);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000117 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700118 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reed34a0c972021-01-25 17:49:32 -0500119 canvas->drawImage(make_src_image(), 0, 0, SkSamplingOptions(), &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000120 canvas->restore();
121}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
bsalomone8d21e82015-07-16 08:23:13 -0700123static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000124 int w = bitmap->width();
125 int h = bitmap->height();
126 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
127 for (int y = 0; y < h; ++y) {
128 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700129 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
130 if (kAlpha_8_SkColorType == bitmap->colorType()) {
131 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
132 *alpha = SkGetPackedA32(initColor);
133 } else {
134 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
135 *pixel = initColor;
136 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000137 }
138 }
139}
140
bsalomone8d21e82015-07-16 08:23:13 -0700141static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000142 if (!didPremulConversion) {
143 return a == b;
144 }
145 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
146 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
147 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
148 int32_t aB = SkGetPackedB32(a);
149
150 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
151 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
152 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
153 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
154
155 return aA == bA &&
156 SkAbs32(aR - bR) <= 1 &&
157 SkAbs32(aG - bG) <= 1 &&
158 SkAbs32(aB - bB) <= 1;
159}
160
bsalomon@google.comc6980972011-11-02 19:57:21 +0000161// checks the bitmap contains correct pixels after the readPixels
162// if the bitmap was prefilled with pixels it checks that these weren't
163// overwritten in the area outside the readPixels.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400164static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
165 bool checkSurfacePixels, bool checkBitmapPixels,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400166 SkImageInfo surfaceInfo) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400167 SkAlphaType bmpAT = bitmap.alphaType();
168 SkColorType bmpCT = bitmap.colorType();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000169 SkASSERT(!bitmap.isNull());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400170 SkASSERT(checkSurfacePixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000171
bsalomon@google.comc6980972011-11-02 19:57:21 +0000172 int bw = bitmap.width();
173 int bh = bitmap.height();
174
175 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
176 SkIRect clippedSrcRect = DEV_RECT;
177 if (!clippedSrcRect.intersect(srcRect)) {
178 clippedSrcRect.setEmpty();
179 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400180 if (kAlpha_8_SkColorType == bmpCT) {
lsalzmana2415ac2016-10-11 14:29:12 -0700181 for (int by = 0; by < bh; ++by) {
182 for (int bx = 0; bx < bw; ++bx) {
183 int devx = bx + srcRect.fLeft;
184 int devy = by + srcRect.fTop;
185 const uint8_t* alpha = bitmap.getAddr8(bx, by);
186
187 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400188 if (checkSurfacePixels) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400189 uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400190 ? 0xFF
191 : SkGetPackedA32(get_src_color(devx, devy));
192 if (surfaceAlpha != *alpha) {
193 ERRORF(reporter,
194 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
195 bx, by, surfaceAlpha, *alpha);
lsalzmana2415ac2016-10-11 14:29:12 -0700196 return false;
197 }
198 }
199 } else if (checkBitmapPixels) {
200 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
201 if (origDstAlpha != *alpha) {
202 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
203 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
204 return false;
205 }
206 }
207 }
208 }
209 return true;
210 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000211 for (int by = 0; by < bh; ++by) {
212 for (int bx = 0; bx < bw; ++bx) {
213 int devx = bx + srcRect.fLeft;
214 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000215
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000216 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000217
218 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400219 if (checkSurfacePixels) {
220 SkPMColor surfacePMColor = get_src_color(devx, devy);
Mike Klein12d4b6e2018-08-17 14:09:55 -0400221 if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
222 surfacePMColor &= 0xFF000000;
223 }
224 if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400225 surfacePMColor |= 0xFF000000;
226 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000227 bool didPremul;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400228 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
229 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
230 ERRORF(reporter,
231 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
232 "Readback was unpremul: %d",
233 bx, by, surfacePMColor, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000234 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000235 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000236 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000237 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700238 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
239 if (origDstPixel != *pixel) {
240 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
241 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000242 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000243 }
244 }
245 }
246 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000247 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248}
249
Brian Salomon5918e832019-10-23 13:34:36 -0400250enum class TightRowBytes : bool { kNo, kYes };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000251
Brian Salomon5918e832019-10-23 13:34:36 -0400252static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB,
253 SkColorType ct, SkAlphaType at) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400254 SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000255 size_t rowBytes = 0;
Brian Salomon5918e832019-10-23 13:34:36 -0400256 if (tightRB == TightRowBytes::kNo) {
257 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000258 }
Mike Reed12e946b2017-04-17 10:53:29 -0400259 bitmap->allocPixels(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000260}
261
kkinnunen15302832015-12-01 04:35:26 -0800262static const struct {
263 SkColorType fColorType;
264 SkAlphaType fAlphaType;
265} gReadPixelsConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400266 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
267 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
268 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
269 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
270 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
271 {kAlpha_8_SkColorType, kPremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800272};
273const SkIRect gReadPixelsTestRects[] = {
274 // entire thing
275 DEV_RECT,
276 // larger on all sides
277 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
278 // fully contained
279 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
280 // outside top left
281 SkIRect::MakeLTRB(-10, -10, -1, -1),
282 // touching top left corner
283 SkIRect::MakeLTRB(-10, -10, 0, 0),
284 // overlapping top left corner
285 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
286 // overlapping top left and top right corners
287 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
288 // touching entire top edge
289 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
290 // overlapping top right corner
291 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
292 // contained in x, overlapping top edge
293 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
294 // outside top right corner
295 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
296 // touching top right corner
297 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
298 // overlapping top left and bottom left corners
299 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
300 // touching entire left edge
301 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
302 // overlapping bottom left corner
303 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
304 // contained in y, overlapping left edge
305 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
306 // outside bottom left corner
307 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
308 // touching bottom left corner
309 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
310 // overlapping bottom left and bottom right corners
311 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
312 // touching entire left edge
313 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
314 // overlapping bottom right corner
315 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
316 // overlapping top right and bottom right corners
317 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
318};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000319
Brian Salomon1d435302019-07-01 13:05:28 -0400320bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
321 const SkImageInfo& srcInfo) {
322 return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400323}
324
reede8f30622016-03-23 18:59:25 -0700325static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
Brian Salomon5918e832019-10-23 13:34:36 -0400326 const SkImageInfo& surfaceInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800327 SkCanvas* canvas = surface->getCanvas();
328 fill_src_canvas(canvas);
329 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
330 const SkIRect& srcRect = gReadPixelsTestRects[rect];
Brian Salomon5918e832019-10-23 13:34:36 -0400331 for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
kkinnunen15302832015-12-01 04:35:26 -0800332 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
333 SkBitmap bmp;
Brian Salomon5918e832019-10-23 13:34:36 -0400334 init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
335 gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700336
kkinnunen15302832015-12-01 04:35:26 -0800337 // if the bitmap has pixels allocated before the readPixels,
338 // note that and fill them with pattern
339 bool startsWithPixels = !bmp.isNull();
340 if (startsWithPixels) {
341 fill_dst_bmp_with_init_data(&bmp);
342 }
343 uint32_t idBefore = surface->generationID();
Mike Reedf1942192017-07-21 14:24:29 -0400344 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
kkinnunen15302832015-12-01 04:35:26 -0800345 uint32_t idAfter = surface->generationID();
346
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400347 // we expect to succeed when the read isn't fully clipped out and the infos are
348 // compatible.
Brian Salomon1d435302019-07-01 13:05:28 -0400349 bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800350 // determine whether we expected the read to succeed.
Brian Salomon1d435302019-07-01 13:05:28 -0400351 REPORTER_ASSERT(reporter, expectSuccess == success,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400352 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
353 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
354 bmp.info().colorType(), bmp.info().alphaType());
kkinnunen15302832015-12-01 04:35:26 -0800355 // read pixels should never change the gen id
356 REPORTER_ASSERT(reporter, idBefore == idAfter);
357
358 if (success || startsWithPixels) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400359 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400360 startsWithPixels, surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800361 } else {
362 // if we had no pixels beforehand and the readPixels
363 // failed then our bitmap should still not have pixels
364 REPORTER_ASSERT(reporter, bmp.isNull());
365 }
366 }
kkinnunen15302832015-12-01 04:35:26 -0800367 }
368 }
369}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400370
kkinnunen15302832015-12-01 04:35:26 -0800371DEF_TEST(ReadPixels, reporter) {
372 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700373 auto surface(SkSurface::MakeRaster(info));
Brian Salomon5918e832019-10-23 13:34:36 -0400374 test_readpixels(reporter, surface, info);
kkinnunen15302832015-12-01 04:35:26 -0800375}
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000376
Matt Sarett8572d852017-02-14 11:21:02 -0500377///////////////////////////////////////////////////////////////////////////////////////////////////
378
379static const uint32_t kNumPixels = 5;
380
381// The five reference pixels are: red, green, blue, white, black.
382// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
383// plus a tail pixel.
384static const uint32_t rgba[kNumPixels] = {
385 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
386};
387static const uint32_t bgra[kNumPixels] = {
388 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
389};
390static const uint16_t rgb565[kNumPixels] = {
391 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
392};
393
394static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
395
396static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
397static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
398static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
399static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
400static const uint64_t f16[kNumPixels] = {
401 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
402};
403
Matt Sarett8572d852017-02-14 11:21:02 -0500404static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
405static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
406
407static const void* five_reference_pixels(SkColorType colorType) {
408 switch (colorType) {
409 case kUnknown_SkColorType:
410 return nullptr;
411 case kAlpha_8_SkColorType:
412 return alpha8;
413 case kRGB_565_SkColorType:
414 return rgb565;
415 case kARGB_4444_SkColorType:
416 return rgba4444;
417 case kRGBA_8888_SkColorType:
418 return rgba;
419 case kBGRA_8888_SkColorType:
420 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500421 case kGray_8_SkColorType:
422 return gray8;
423 case kRGBA_F16_SkColorType:
424 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400425 default:
Leon Scroggins IIId6832d02018-09-04 11:10:04 -0400426 return nullptr;
Matt Sarett8572d852017-02-14 11:21:02 -0500427 }
428
429 SkASSERT(false);
430 return nullptr;
431}
432
433static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
434 const SkImageInfo& srcInfo) {
Brian Osmane1adc3a2018-06-04 09:21:17 -0400435 if (!SkImageInfoIsValid(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500436 return;
437 }
438
Matt Sarett8572d852017-02-14 11:21:02 -0500439 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400440 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500441 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
442 REPORTER_ASSERT(r, src);
443
444 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
445 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400446 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400447 bool success = src->readPixels(nullptr, dstPixmap, 0, 0);
Matt Sarett8572d852017-02-14 11:21:02 -0500448 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
449
450 if (success) {
451 if (kGray_8_SkColorType == srcInfo.colorType() &&
Mike Klein12d4b6e2018-08-17 14:09:55 -0400452 kGray_8_SkColorType != dstInfo.colorType()) {
453 // TODO: test (r,g,b) == (gray,gray,gray)?
454 return;
455 }
456
457 if (kGray_8_SkColorType == dstInfo.colorType() &&
458 kGray_8_SkColorType != srcInfo.colorType()) {
459 // TODO: test gray = luminance?
460 return;
461 }
462
463 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
464 kAlpha_8_SkColorType != dstInfo.colorType()) {
465 // TODO: test output = black with this alpha?
Matt Sarett8572d852017-02-14 11:21:02 -0500466 return;
467 }
468
469 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
470 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
Matt Sarett8572d852017-02-14 11:21:02 -0500471 }
472}
473
474DEF_TEST(ReadPixels_ValidConversion, reporter) {
475 const SkColorType kColorTypes[] = {
476 kUnknown_SkColorType,
477 kAlpha_8_SkColorType,
478 kRGB_565_SkColorType,
479 kARGB_4444_SkColorType,
480 kRGBA_8888_SkColorType,
481 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500482 kGray_8_SkColorType,
483 kRGBA_F16_SkColorType,
484 };
485
486 const SkAlphaType kAlphaTypes[] = {
487 kUnknown_SkAlphaType,
488 kOpaque_SkAlphaType,
489 kPremul_SkAlphaType,
490 kUnpremul_SkAlphaType,
491 };
492
493 const sk_sp<SkColorSpace> kColorSpaces[] = {
494 nullptr,
495 SkColorSpace::MakeSRGB(),
496 };
497
498 for (SkColorType dstCT : kColorTypes) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400499 for (SkAlphaType dstAT : kAlphaTypes) {
500 for (const sk_sp<SkColorSpace>& dstCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500501 for (SkColorType srcCT : kColorTypes) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400502 for (SkAlphaType srcAT : kAlphaTypes) {
503 for (const sk_sp<SkColorSpace>& srcCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500504 test_conversion(reporter,
505 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
506 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
507 }
508 }
509 }
510 }
511 }
512 }
513}
Brian Salomonc24c8ef2021-02-01 13:32:30 -0500514
515DEF_TEST(ReadPixels_InvalidRowBytes, reporter) {
516 auto srcII = SkImageInfo::Make({10, 10}, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
517 auto surf = SkSurface::MakeRaster(srcII);
518 for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) {
519 auto colorType = static_cast<SkColorType>(ct);
520 size_t bpp = SkColorTypeBytesPerPixel(colorType);
521 if (bpp <= 1) {
522 continue;
523 }
524 auto dstII = srcII.makeColorType(colorType);
525 size_t badRowBytes = (surf->width() + 1)*bpp - 1;
526 auto storage = std::make_unique<char[]>(badRowBytes*surf->height());
527 REPORTER_ASSERT(reporter, !surf->readPixels(dstII, storage.get(), badRowBytes, 0, 0));
528 }
529}