blob: 39866d7e18e47f73b8ee8ade0ebc918f636e1765 [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
Brian Salomon58389b92018-03-07 13:01:25 -05008#include <initializer_list>
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "include/core/SkSurface.h"
Brian Salomonab32f652019-05-10 14:24:50 -040011#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkColorData.h"
13#include "include/private/SkHalf.h"
14#include "include/private/SkImageInfoPriv.h"
Brian Salomonab32f652019-05-10 14:24:50 -040015#include "src/core/SkAutoPixmapStorage.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040016#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrContextPriv.h"
19#include "src/gpu/GrProxyProvider.h"
20#include "src/gpu/SkGr.h"
Brian Salomonab32f652019-05-10 14:24:50 -040021#include "tests/Test.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/gpu/GrContextFactory.h"
23#include "tools/gpu/ProxyUtils.h"
bsalomon@google.comc6980972011-11-02 19:57:21 +000024
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
Brian Salomon5fba7ad2018-03-22 10:01:16 -040068// TODO: Make this consider both ATs
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;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040082 case kRGB_888x_SkColorType: // fallthrough
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000083 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000084 r = static_cast<U8CPU>(c[0]);
85 g = static_cast<U8CPU>(c[1]);
86 b = static_cast<U8CPU>(c[2]);
Brian Salomon5fba7ad2018-03-22 10:01:16 -040087 // 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 +000088 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000089 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000090 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000091 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000092 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000093 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000094
95 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000096 r = SkMulDiv255Ceiling(r, a);
97 g = SkMulDiv255Ceiling(g, a);
98 b = SkMulDiv255Ceiling(b, a);
99 }
100 return SkPackARGB32(a, r, g, b);
101}
102
bsalomone8d21e82015-07-16 08:23:13 -0700103static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000104 static SkBitmap bmp;
105 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700106 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000107 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
108 for (int y = 0; y < DEV_H; ++y) {
109 for (int x = 0; x < DEV_W; ++x) {
110 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700111 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000112 }
113 }
114 }
bsalomone8d21e82015-07-16 08:23:13 -0700115 return bmp;
116}
117
118static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000119 canvas->save();
120 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500121 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000122 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700123 paint.setBlendMode(SkBlendMode::kSrc);
bsalomone8d21e82015-07-16 08:23:13 -0700124 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000125 canvas->restore();
126}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
bsalomone8d21e82015-07-16 08:23:13 -0700128static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000129 int w = bitmap->width();
130 int h = bitmap->height();
131 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
132 for (int y = 0; y < h; ++y) {
133 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700134 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
135 if (kAlpha_8_SkColorType == bitmap->colorType()) {
136 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
137 *alpha = SkGetPackedA32(initColor);
138 } else {
139 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
140 *pixel = initColor;
141 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000142 }
143 }
144}
145
bsalomone8d21e82015-07-16 08:23:13 -0700146static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000147 if (!didPremulConversion) {
148 return a == b;
149 }
150 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
151 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
152 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
153 int32_t aB = SkGetPackedB32(a);
154
155 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
156 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
157 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
158 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
159
160 return aA == bA &&
161 SkAbs32(aR - bR) <= 1 &&
162 SkAbs32(aG - bG) <= 1 &&
163 SkAbs32(aB - bB) <= 1;
164}
165
bsalomon@google.comc6980972011-11-02 19:57:21 +0000166// checks the bitmap contains correct pixels after the readPixels
167// if the bitmap was prefilled with pixels it checks that these weren't
168// overwritten in the area outside the readPixels.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400169static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
170 bool checkSurfacePixels, bool checkBitmapPixels,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400171 SkImageInfo surfaceInfo) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400172 SkAlphaType bmpAT = bitmap.alphaType();
173 SkColorType bmpCT = bitmap.colorType();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000174 SkASSERT(!bitmap.isNull());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400175 SkASSERT(checkSurfacePixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000176
bsalomon@google.comc6980972011-11-02 19:57:21 +0000177 int bw = bitmap.width();
178 int bh = bitmap.height();
179
180 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
181 SkIRect clippedSrcRect = DEV_RECT;
182 if (!clippedSrcRect.intersect(srcRect)) {
183 clippedSrcRect.setEmpty();
184 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400185 if (kAlpha_8_SkColorType == bmpCT) {
lsalzmana2415ac2016-10-11 14:29:12 -0700186 for (int by = 0; by < bh; ++by) {
187 for (int bx = 0; bx < bw; ++bx) {
188 int devx = bx + srcRect.fLeft;
189 int devy = by + srcRect.fTop;
190 const uint8_t* alpha = bitmap.getAddr8(bx, by);
191
192 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400193 if (checkSurfacePixels) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400194 uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400195 ? 0xFF
196 : SkGetPackedA32(get_src_color(devx, devy));
197 if (surfaceAlpha != *alpha) {
198 ERRORF(reporter,
199 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
200 bx, by, surfaceAlpha, *alpha);
lsalzmana2415ac2016-10-11 14:29:12 -0700201 return false;
202 }
203 }
204 } else if (checkBitmapPixels) {
205 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
206 if (origDstAlpha != *alpha) {
207 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
208 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
209 return false;
210 }
211 }
212 }
213 }
214 return true;
215 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000216 for (int by = 0; by < bh; ++by) {
217 for (int bx = 0; bx < bw; ++bx) {
218 int devx = bx + srcRect.fLeft;
219 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000220
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000221 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000222
223 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400224 if (checkSurfacePixels) {
225 SkPMColor surfacePMColor = get_src_color(devx, devy);
Mike Klein12d4b6e2018-08-17 14:09:55 -0400226 if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
227 surfacePMColor &= 0xFF000000;
228 }
229 if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400230 surfacePMColor |= 0xFF000000;
231 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000232 bool didPremul;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400233 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
234 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
235 ERRORF(reporter,
236 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
237 "Readback was unpremul: %d",
238 bx, by, surfacePMColor, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000239 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000240 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000241 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000242 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700243 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
244 if (origDstPixel != *pixel) {
245 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
246 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000247 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248 }
249 }
250 }
251 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000252 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000253}
254
255enum BitmapInit {
256 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000257
Mike Reed12e946b2017-04-17 10:53:29 -0400258 kTight_BitmapInit = kFirstBitmapInit,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000259 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800260 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000261
bsalomon9d02b262016-02-01 12:49:30 -0800262 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500263
264#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800265 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500266#else
267 kLast_BitmapInit = kLastAligned_BitmapInit
268#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000269};
270
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000271static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000272 int x = bmi;
273 return static_cast<BitmapInit>(++x);
274}
275
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000276static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
277 SkAlphaType at) {
278 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000279 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000280 switch (init) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000281 case kTight_BitmapInit:
282 break;
283 case kRowBytes_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700284 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000285 break;
bsalomon9d02b262016-02-01 12:49:30 -0800286 case kRowBytesOdd_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700287 rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
bsalomon9d02b262016-02-01 12:49:30 -0800288 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000289 default:
290 SkASSERT(0);
291 break;
292 }
Mike Reed12e946b2017-04-17 10:53:29 -0400293 bitmap->allocPixels(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000294}
295
kkinnunen15302832015-12-01 04:35:26 -0800296static const struct {
297 SkColorType fColorType;
298 SkAlphaType fAlphaType;
299} gReadPixelsConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400300 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
301 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
302 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
303 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
304 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
305 {kAlpha_8_SkColorType, kPremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800306};
307const SkIRect gReadPixelsTestRects[] = {
308 // entire thing
309 DEV_RECT,
310 // larger on all sides
311 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
312 // fully contained
313 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
314 // outside top left
315 SkIRect::MakeLTRB(-10, -10, -1, -1),
316 // touching top left corner
317 SkIRect::MakeLTRB(-10, -10, 0, 0),
318 // overlapping top left corner
319 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
320 // overlapping top left and top right corners
321 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
322 // touching entire top edge
323 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
324 // overlapping top right corner
325 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
326 // contained in x, overlapping top edge
327 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
328 // outside top right corner
329 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
330 // touching top right corner
331 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
332 // overlapping top left and bottom left corners
333 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
334 // touching entire left edge
335 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
336 // overlapping bottom left corner
337 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
338 // contained in y, overlapping left edge
339 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
340 // outside bottom left corner
341 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
342 // touching bottom left corner
343 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
344 // overlapping bottom left and bottom right corners
345 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
346 // touching entire left edge
347 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
348 // overlapping bottom right corner
349 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
350 // overlapping top right and bottom right corners
351 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
352};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000353
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400354enum class ReadSuccessExpectation {
355 kNo,
356 kMaybe,
357 kYes,
358};
359
360bool check_success_expectation(ReadSuccessExpectation expectation, bool actualSuccess) {
361 switch (expectation) {
362 case ReadSuccessExpectation::kMaybe:
363 return true;
364 case ReadSuccessExpectation::kNo:
365 return !actualSuccess;
366 case ReadSuccessExpectation::kYes:
367 return actualSuccess;
368 }
369 return false;
370}
371
372ReadSuccessExpectation read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
373 const SkImageInfo& srcInfo, bool isGPU) {
374 if (!SkIRect::Intersects(srcRect, DEV_RECT)) {
375 return ReadSuccessExpectation::kNo;
376 }
377 if (!SkImageInfoValidConversion(dstInfo, srcInfo)) {
378 return ReadSuccessExpectation::kNo;
379 }
380 if (!isGPU) {
381 return ReadSuccessExpectation::kYes;
382 }
383 // This serves more as documentation of what currently works on the GPU rather than desired
384 // expectations. Once we make GrSurfaceContext color/alpha type aware and clean up some read
385 // pixels code we will make more scenarios work.
386
387 // The GPU code current only does the premul->unpremul conversion, not the reverse.
388 if (srcInfo.alphaType() == kUnpremul_SkAlphaType &&
389 dstInfo.alphaType() == kPremul_SkAlphaType) {
390 return ReadSuccessExpectation::kNo;
391 }
392 // We don't currently require reading alpha-only surfaces to succeed because of some pessimistic
393 // caps decisions and alpha/red complexity in GL.
394 if (SkColorTypeIsAlphaOnly(srcInfo.colorType())) {
395 return ReadSuccessExpectation::kMaybe;
396 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400397 return ReadSuccessExpectation::kYes;
398}
399
reede8f30622016-03-23 18:59:25 -0700400static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400401 const SkImageInfo& surfaceInfo, BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800402 SkCanvas* canvas = surface->getCanvas();
403 fill_src_canvas(canvas);
404 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
405 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800406 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800407 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
408 SkBitmap bmp;
409 init_bitmap(&bmp, srcRect, bmi,
410 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700411
kkinnunen15302832015-12-01 04:35:26 -0800412 // if the bitmap has pixels allocated before the readPixels,
413 // note that and fill them with pattern
414 bool startsWithPixels = !bmp.isNull();
415 if (startsWithPixels) {
416 fill_dst_bmp_with_init_data(&bmp);
417 }
418 uint32_t idBefore = surface->generationID();
Mike Reedf1942192017-07-21 14:24:29 -0400419 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
kkinnunen15302832015-12-01 04:35:26 -0800420 uint32_t idAfter = surface->generationID();
421
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400422 // we expect to succeed when the read isn't fully clipped out and the infos are
423 // compatible.
424 bool isGPU = SkToBool(surface->getCanvas()->getGrContext());
425 auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo, isGPU);
kkinnunen15302832015-12-01 04:35:26 -0800426 // determine whether we expected the read to succeed.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400427 REPORTER_ASSERT(reporter, check_success_expectation(expectSuccess, success),
428 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
429 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
430 bmp.info().colorType(), bmp.info().alphaType());
kkinnunen15302832015-12-01 04:35:26 -0800431 // read pixels should never change the gen id
432 REPORTER_ASSERT(reporter, idBefore == idAfter);
433
434 if (success || startsWithPixels) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400435 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400436 startsWithPixels, surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800437 } else {
438 // if we had no pixels beforehand and the readPixels
439 // failed then our bitmap should still not have pixels
440 REPORTER_ASSERT(reporter, bmp.isNull());
441 }
442 }
kkinnunen15302832015-12-01 04:35:26 -0800443 }
444 }
445}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400446
kkinnunen15302832015-12-01 04:35:26 -0800447DEF_TEST(ReadPixels, reporter) {
448 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700449 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800450 // SW readback fails a premul check when reading back to an unaligned rowbytes.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400451 test_readpixels(reporter, surface, info, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800452}
egdaniel88e8aef2016-06-27 14:34:55 -0700453DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
Robert Phillipseb4f1862017-06-08 16:38:25 -0400454 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
455 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType ||
456 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
457 // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs
458 return;
459 }
460
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400461 static const SkImageInfo kImageInfos[] = {
462 SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
463 SkImageInfo::Make(DEV_W, DEV_H, kBGRA_8888_SkColorType, kPremul_SkAlphaType),
464 SkImageInfo::Make(DEV_W, DEV_H, kRGB_888x_SkColorType, kOpaque_SkAlphaType),
465 SkImageInfo::Make(DEV_W, DEV_H, kAlpha_8_SkColorType, kPremul_SkAlphaType),
466 };
467 for (const auto& ii : kImageInfos) {
468 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
469 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(
470 ctxInfo.grContext(), SkBudgeted::kNo, ii, 0, origin, nullptr));
471 if (!surface) {
472 continue;
473 }
474 test_readpixels(reporter, surface, ii, kLast_BitmapInit);
475 }
kkinnunen15302832015-12-01 04:35:26 -0800476 }
477}
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000478
Robert Phillipse78b7252017-04-06 07:59:41 -0400479static void test_readpixels_texture(skiatest::Reporter* reporter,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400480 sk_sp<GrSurfaceContext> sContext,
481 const SkImageInfo& surfaceInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800482 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
483 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800484 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800485 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
486 SkBitmap bmp;
487 init_bitmap(&bmp, srcRect, bmi,
488 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
489
490 // if the bitmap has pixels allocated before the readPixels,
491 // note that and fill them with pattern
492 bool startsWithPixels = !bmp.isNull();
493 // Try doing the read directly from a non-renderable texture
494 if (startsWithPixels) {
495 fill_dst_bmp_with_init_data(&bmp);
kkinnunen15302832015-12-01 04:35:26 -0800496 uint32_t flags = 0;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400497 // TODO: These two hacks can go away when the surface context knows the alpha
498 // type.
499 // Tell the read to perform an unpremul step since it doesn't know alpha type.
kkinnunen15302832015-12-01 04:35:26 -0800500 if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400501 flags = GrSurfaceContext::kUnpremul_PixelOpsFlag;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000502 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400503 // The surface context doesn't know that the src is opaque. We don't support
504 // converting non-opaque data to opaque during a read.
505 if (bmp.alphaType() == kOpaque_SkAlphaType &&
506 surfaceInfo.alphaType() != kOpaque_SkAlphaType) {
507 continue;
508 }
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400509 bool success = sContext->readPixels(bmp.info(), bmp.getPixels(),
510 bmp.rowBytes(),
511 srcRect.fLeft, srcRect.fTop, flags);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400512 auto expectSuccess =
513 read_should_succeed(srcRect, bmp.info(), surfaceInfo, true);
514 REPORTER_ASSERT(
515 reporter, check_success_expectation(expectSuccess, success),
516 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
517 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
518 bmp.info().colorType(), bmp.info().alphaType());
519 if (success) {
520 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400521 surfaceInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400522 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000523 }
524 }
525 }
526 }
527}
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400528
egdaniel88e8aef2016-06-27 14:34:55 -0700529DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
Robert Phillipseb4f1862017-06-08 16:38:25 -0400530 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
531 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType ||
532 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
533 // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs
534 return;
535 }
536
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400537 GrContext* context = ctxInfo.grContext();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400538 SkBitmap bmp = make_src_bitmap();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400539
kkinnunen15302832015-12-01 04:35:26 -0800540 // On the GPU we will also try reading back from a non-renderable texture.
Brian Salomon8b1fb742016-11-03 15:21:06 -0400541 for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400542 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Brian Salomon58389b92018-03-07 13:01:25 -0500543 sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData(
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400544 context, renderable, DEV_W, DEV_H, bmp.colorType(), origin, bmp.getPixels(),
Brian Salomon58389b92018-03-07 13:01:25 -0500545 bmp.rowBytes());
Robert Phillips9da87e02019-02-04 13:26:26 -0500546 sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500547 std::move(proxy));
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400548 auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
549 test_readpixels_texture(reporter, std::move(sContext), info);
Brian Salomon8b1fb742016-11-03 15:21:06 -0400550 }
kkinnunen15302832015-12-01 04:35:26 -0800551 }
552}
Matt Sarett8572d852017-02-14 11:21:02 -0500553
554///////////////////////////////////////////////////////////////////////////////////////////////////
555
556static const uint32_t kNumPixels = 5;
557
558// The five reference pixels are: red, green, blue, white, black.
559// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
560// plus a tail pixel.
561static const uint32_t rgba[kNumPixels] = {
562 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
563};
564static const uint32_t bgra[kNumPixels] = {
565 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
566};
567static const uint16_t rgb565[kNumPixels] = {
568 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
569};
570
571static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
572
573static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
574static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
575static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
576static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
577static const uint64_t f16[kNumPixels] = {
578 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
579};
580
Matt Sarett8572d852017-02-14 11:21:02 -0500581static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
582static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
583
584static const void* five_reference_pixels(SkColorType colorType) {
585 switch (colorType) {
586 case kUnknown_SkColorType:
587 return nullptr;
588 case kAlpha_8_SkColorType:
589 return alpha8;
590 case kRGB_565_SkColorType:
591 return rgb565;
592 case kARGB_4444_SkColorType:
593 return rgba4444;
594 case kRGBA_8888_SkColorType:
595 return rgba;
596 case kBGRA_8888_SkColorType:
597 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500598 case kGray_8_SkColorType:
599 return gray8;
600 case kRGBA_F16_SkColorType:
601 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400602 default:
Leon Scroggins IIId6832d02018-09-04 11:10:04 -0400603 return nullptr;
Matt Sarett8572d852017-02-14 11:21:02 -0500604 }
605
606 SkASSERT(false);
607 return nullptr;
608}
609
610static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
611 const SkImageInfo& srcInfo) {
Brian Osmane1adc3a2018-06-04 09:21:17 -0400612 if (!SkImageInfoIsValid(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500613 return;
614 }
615
Matt Sarett8572d852017-02-14 11:21:02 -0500616 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400617 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500618 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
619 REPORTER_ASSERT(r, src);
620
621 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
622 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400623 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500624 bool success = src->readPixels(dstPixmap, 0, 0);
625 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
626
627 if (success) {
628 if (kGray_8_SkColorType == srcInfo.colorType() &&
Mike Klein12d4b6e2018-08-17 14:09:55 -0400629 kGray_8_SkColorType != dstInfo.colorType()) {
630 // TODO: test (r,g,b) == (gray,gray,gray)?
631 return;
632 }
633
634 if (kGray_8_SkColorType == dstInfo.colorType() &&
635 kGray_8_SkColorType != srcInfo.colorType()) {
636 // TODO: test gray = luminance?
637 return;
638 }
639
640 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
641 kAlpha_8_SkColorType != dstInfo.colorType()) {
642 // TODO: test output = black with this alpha?
Matt Sarett8572d852017-02-14 11:21:02 -0500643 return;
644 }
645
646 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
647 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
Matt Sarett8572d852017-02-14 11:21:02 -0500648 }
649}
650
651DEF_TEST(ReadPixels_ValidConversion, reporter) {
652 const SkColorType kColorTypes[] = {
653 kUnknown_SkColorType,
654 kAlpha_8_SkColorType,
655 kRGB_565_SkColorType,
656 kARGB_4444_SkColorType,
657 kRGBA_8888_SkColorType,
658 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500659 kGray_8_SkColorType,
660 kRGBA_F16_SkColorType,
661 };
662
663 const SkAlphaType kAlphaTypes[] = {
664 kUnknown_SkAlphaType,
665 kOpaque_SkAlphaType,
666 kPremul_SkAlphaType,
667 kUnpremul_SkAlphaType,
668 };
669
670 const sk_sp<SkColorSpace> kColorSpaces[] = {
671 nullptr,
672 SkColorSpace::MakeSRGB(),
673 };
674
675 for (SkColorType dstCT : kColorTypes) {
676 for (SkAlphaType dstAT: kAlphaTypes) {
677 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
678 for (SkColorType srcCT : kColorTypes) {
679 for (SkAlphaType srcAT: kAlphaTypes) {
680 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500681 test_conversion(reporter,
682 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
683 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
684 }
685 }
686 }
687 }
688 }
689 }
690}
Brian Salomonab32f652019-05-10 14:24:50 -0400691
Brian Salomoncd734f62019-05-10 16:32:54 -0400692namespace {
693using ComparePixmapsErrorReporter = void(int x, int y, const float diffs[4]);
694} // anonymous namespace
695
696static void compare_pixmaps(const SkPixmap& a, const SkPixmap& b, const float tol[4],
697 std::function<ComparePixmapsErrorReporter>& error) {
698 if (a.width() != b.width() || a.height() != b.height()) {
699 static constexpr float kDummyDiffs[4] = {};
700 error(-1, -1, kDummyDiffs);
701 return;
702 }
703 SkAutoPixmapStorage afloat;
704 SkAutoPixmapStorage bfloat;
705 afloat.alloc(a.info().makeColorType(kRGBA_F32_SkColorType));
706 bfloat.alloc(b.info().makeColorType(kRGBA_F32_SkColorType));
707 SkConvertPixels(afloat.info(), afloat.writable_addr(), afloat.rowBytes(), a.info(), a.addr(),
708 a.rowBytes());
709 SkConvertPixels(bfloat.info(), bfloat.writable_addr(), bfloat.rowBytes(), b.info(), b.addr(),
710 b.rowBytes());
711 for (int y = 0; y < a.height(); ++y) {
712 for (int x = 0; x < a.width(); ++x) {
713 const float* rgbaA = static_cast<const float*>(afloat.addr(x, y));
714 const float* rgbaB = static_cast<const float*>(bfloat.addr(x, y));
715 float diffs[4];
716 bool bad = false;
717 for (int i = 0; i < 4; ++i) {
718 diffs[i] = rgbaB[i] - rgbaA[i];
719 if (std::abs(diffs[i]) > tol[i]) {
720 bad = true;
721 }
722 }
723 if (bad) {
724 error(x, y, diffs);
725 return;
726 }
727 }
728 }
729}
730
731static int min_rgb_channel_bits(SkColorType ct) {
732 switch (ct) {
733 case kUnknown_SkColorType: return 0;
734 case kAlpha_8_SkColorType: return 8;
735 case kRGB_565_SkColorType: return 5;
736 case kARGB_4444_SkColorType: return 4;
737 case kRGBA_8888_SkColorType: return 8;
738 case kRGB_888x_SkColorType: return 8;
739 case kBGRA_8888_SkColorType: return 8;
740 case kRGBA_1010102_SkColorType: return 10;
741 case kRGB_101010x_SkColorType: return 10;
742 case kGray_8_SkColorType: return 8; // counting gray as "rgb"
743 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
744 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
745 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
746 }
747 SK_ABORT("Unexpected color type.");
748 return 0;
749}
750
Brian Salomonab32f652019-05-10 14:24:50 -0400751DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) {
752 struct Context {
753 SkPixmap* fPixmap = nullptr;
754 bool fSuceeded = false;
755 bool fCalled = false;
756 };
757 auto callback = [](SkSurface::ReleaseContext context, const void* data, size_t rowBytes) {
758 auto* pm = static_cast<Context*>(context)->fPixmap;
759 static_cast<Context*>(context)->fCalled = true;
760 if ((static_cast<Context*>(context)->fSuceeded = SkToBool(data))) {
761 auto dst = static_cast<char*>(pm->writable_addr());
762 const auto* src = static_cast<const char*>(data);
763 for (int y = 0; y < pm->height(); ++y, src += rowBytes, dst += pm->rowBytes()) {
764 memcpy(dst, src, pm->width() * SkColorTypeBytesPerPixel(pm->colorType()));
765 }
766 }
767 };
768 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
769 static constexpr int kW = 16;
770 static constexpr int kH = 16;
Brian Salomoncd734f62019-05-10 16:32:54 -0400771 for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
772 auto surfCT = static_cast<SkColorType>(sct);
773 auto info = SkImageInfo::Make(kW, kH, surfCT, kPremul_SkAlphaType, nullptr);
Brian Salomonab32f652019-05-10 14:24:50 -0400774 auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info, 1,
775 origin, nullptr);
776 if (!surf) {
777 continue;
778 }
779 float d = std::sqrt((float)surf->width() * surf->width() +
780 (float)surf->height() * surf->height());
781 for (int j = 0; j < surf->height(); ++j) {
782 for (int i = 0; i < surf->width(); ++i) {
783 float r = i / (float)surf->width();
784 float g = 1.f - i / (float)surf->height();
785 float b = std::sqrt((float)i * i + (float)j * j) / d;
786 SkPaint paint;
787 paint.setColor4f(SkColor4f{r, g, b, 1.f}, nullptr);
788 surf->getCanvas()->drawRect(SkRect::MakeXYWH(i, j, 1, 1), paint);
789 }
790 }
791 for (const auto& rect : {SkIRect::MakeWH(kW, kH), // full size
792 SkIRect::MakeLTRB(1, 2, kW - 3, kH - 4), // partial
Brian Salomoncd734f62019-05-10 16:32:54 -0400793 SkIRect::MakeXYWH(1, 1, 0, 0), // empty: fail
794 SkIRect::MakeWH(kW + 1, kH / 2)}) { // too wide: fail
795 for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
796 auto readCT = static_cast<SkColorType>(rct);
797 for (const sk_sp<SkColorSpace>& readCS :
798 {sk_sp<SkColorSpace>(), SkColorSpace::MakeSRGBLinear()}) {
799 SkAutoPixmapStorage result;
800 Context context;
801 context.fPixmap = &result;
802 info = SkImageInfo::Make(rect.width(), rect.height(), readCT,
803 kPremul_SkAlphaType, readCS);
804 result.alloc(info);
805 memset(result.writable_addr(), 0xAB, result.computeByteSize());
Brian Salomon201700f2019-05-17 12:05:44 -0400806 // Rescale quality and linearity don't matter since we're doing a non-
807 // scaling readback.
808 surf->asyncRescaleAndReadPixels(info, rect, SkSurface::RescaleGamma::kSrc,
809 kNone_SkFilterQuality, callback, &context);
Brian Salomoncd734f62019-05-10 16:32:54 -0400810 while (!context.fCalled) {
811 ctxInfo.grContext()->checkAsyncWorkCompletion();
Brian Salomonab32f652019-05-10 14:24:50 -0400812 }
Brian Salomoncd734f62019-05-10 16:32:54 -0400813 if (rect.isEmpty() || !SkIRect::MakeWH(kW, kH).contains(rect)) {
814 REPORTER_ASSERT(reporter, !context.fSuceeded);
815 }
816 if (!context.fSuceeded) {
817 continue;
818 }
819 // We use a synchronous read as the source of truth.
820 SkAutoPixmapStorage ref;
821 ref.alloc(info);
822 memset(ref.writable_addr(), 0xCD, ref.computeByteSize());
823 if (!surf->readPixels(ref, rect.fLeft, rect.fTop)) {
824 continue;
825 }
826 // When there is no conversion, don't allow a difference.
827 float tol = 0.f;
828 if (readCS || readCT != surfCT) {
829 // When there is a conversion allow a diff of two values when no
830 // color space conversion and three otherwise. Except for alpha where
831 // we allow no difference. Allow intermediate truncation to an 8 bit per
832 // channel format.
833 int rgbBits = std::min({min_rgb_channel_bits(readCT),
834 min_rgb_channel_bits(surfCT), 8});
835
836 tol = (readCS ? 3.f : 2.f) / (1 << rgbBits);
837 }
838 const float tols[4] = {tol, tol, tol, 0};
839 auto error = std::function<ComparePixmapsErrorReporter>(
840 [&](int x, int y, const float diffs[4]) {
841 ERRORF(reporter,
842 "Surf Color Type: %d, Read CT: %d, Rect [%d, %d, %d, %d]"
843 ", origin: %d, CS conversion: %d\n"
844 "Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
845 surfCT, readCT, rect.fLeft, rect.fTop, rect.fRight,
846 rect.fBottom, origin, (bool)readCS, x, y, diffs[0],
847 diffs[1], diffs[2], diffs[3]);
848 });
849 compare_pixmaps(ref, result, tols, error);
Brian Salomonab32f652019-05-10 14:24:50 -0400850 }
851 }
852 }
853 }
854 }
855}