blob: f80748d9a1365f50a317fab441b5d4d9a1ce675c [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 Salomon5dd77462019-09-26 16:57:09 -040015#include "include/utils/SkNWayCanvas.h"
Brian Salomonab32f652019-05-10 14:24:50 -040016#include "src/core/SkAutoPixmapStorage.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"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040019#include "src/gpu/GrImageInfo.h"
Brian Salomonab32f652019-05-10 14:24:50 -040020#include "tests/Test.h"
Brian Salomon85aeccf2019-07-15 12:30:44 -040021#include "tests/TestUtils.h"
Brian Salomon5dd77462019-09-26 16:57:09 -040022#include "tools/ToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/gpu/GrContextFactory.h"
24#include "tools/gpu/ProxyUtils.h"
bsalomon@google.comc6980972011-11-02 19:57:21 +000025
bsalomon@google.comc6980972011-11-02 19:57:21 +000026static const int DEV_W = 100, DEV_H = 100;
27static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000028static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000029 DEV_H * SK_Scalar1);
30
bsalomone8d21e82015-07-16 08:23:13 -070031static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000032 SkASSERT(x >= 0 && x < DEV_W);
33 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000034
35 U8CPU r = x;
36 U8CPU g = y;
37 U8CPU b = 0xc;
38
39 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000040 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000041 case 0:
42 a = 0xff;
43 break;
44 case 1:
45 a = 0x80;
46 break;
47 case 2:
48 a = 0xCC;
49 break;
50 case 4:
51 a = 0x01;
52 break;
53 case 3:
54 a = 0x00;
55 break;
56 }
57 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000058}
halcanary9d524f22016-03-29 09:03:52 -070059
bsalomone8d21e82015-07-16 08:23:13 -070060static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000061 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000062
bsalomon@google.comc6980972011-11-02 19:57:21 +000063 U8CPU b = n & 0xff;
64 U8CPU g = (n >> 8) & 0xff;
65 U8CPU r = (n >> 16) & 0xff;
66 return SkPackARGB32(0xff, r, g , b);
67}
68
Brian Salomon5fba7ad2018-03-22 10:01:16 -040069// TODO: Make this consider both ATs
bsalomone8d21e82015-07-16 08:23:13 -070070static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
71 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000072 *doUnpremul = (kUnpremul_SkAlphaType == at);
73
74 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000075 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000076 switch (ct) {
77 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000078 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000079 g = static_cast<U8CPU>(c[1]);
80 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000081 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000082 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040083 case kRGB_888x_SkColorType: // fallthrough
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000084 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000085 r = static_cast<U8CPU>(c[0]);
86 g = static_cast<U8CPU>(c[1]);
87 b = static_cast<U8CPU>(c[2]);
Brian Salomon5fba7ad2018-03-22 10:01:16 -040088 // 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 +000089 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000090 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000091 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000092 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000093 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000094 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000095
96 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000097 r = SkMulDiv255Ceiling(r, a);
98 g = SkMulDiv255Ceiling(g, a);
99 b = SkMulDiv255Ceiling(b, a);
100 }
101 return SkPackARGB32(a, r, g, b);
102}
103
bsalomone8d21e82015-07-16 08:23:13 -0700104static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000105 static SkBitmap bmp;
106 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700107 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000108 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
109 for (int y = 0; y < DEV_H; ++y) {
110 for (int x = 0; x < DEV_W; ++x) {
111 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700112 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000113 }
114 }
115 }
bsalomone8d21e82015-07-16 08:23:13 -0700116 return bmp;
117}
118
119static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000120 canvas->save();
121 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500122 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000123 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700124 paint.setBlendMode(SkBlendMode::kSrc);
bsalomone8d21e82015-07-16 08:23:13 -0700125 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000126 canvas->restore();
127}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128
bsalomone8d21e82015-07-16 08:23:13 -0700129static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000130 int w = bitmap->width();
131 int h = bitmap->height();
132 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
133 for (int y = 0; y < h; ++y) {
134 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700135 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
136 if (kAlpha_8_SkColorType == bitmap->colorType()) {
137 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
138 *alpha = SkGetPackedA32(initColor);
139 } else {
140 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
141 *pixel = initColor;
142 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000143 }
144 }
145}
146
bsalomone8d21e82015-07-16 08:23:13 -0700147static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000148 if (!didPremulConversion) {
149 return a == b;
150 }
151 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
152 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
153 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
154 int32_t aB = SkGetPackedB32(a);
155
156 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
157 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
158 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
159 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
160
161 return aA == bA &&
162 SkAbs32(aR - bR) <= 1 &&
163 SkAbs32(aG - bG) <= 1 &&
164 SkAbs32(aB - bB) <= 1;
165}
166
bsalomon@google.comc6980972011-11-02 19:57:21 +0000167// checks the bitmap contains correct pixels after the readPixels
168// if the bitmap was prefilled with pixels it checks that these weren't
169// overwritten in the area outside the readPixels.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400170static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
171 bool checkSurfacePixels, bool checkBitmapPixels,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400172 SkImageInfo surfaceInfo) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400173 SkAlphaType bmpAT = bitmap.alphaType();
174 SkColorType bmpCT = bitmap.colorType();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000175 SkASSERT(!bitmap.isNull());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400176 SkASSERT(checkSurfacePixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000177
bsalomon@google.comc6980972011-11-02 19:57:21 +0000178 int bw = bitmap.width();
179 int bh = bitmap.height();
180
181 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
182 SkIRect clippedSrcRect = DEV_RECT;
183 if (!clippedSrcRect.intersect(srcRect)) {
184 clippedSrcRect.setEmpty();
185 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400186 if (kAlpha_8_SkColorType == bmpCT) {
lsalzmana2415ac2016-10-11 14:29:12 -0700187 for (int by = 0; by < bh; ++by) {
188 for (int bx = 0; bx < bw; ++bx) {
189 int devx = bx + srcRect.fLeft;
190 int devy = by + srcRect.fTop;
191 const uint8_t* alpha = bitmap.getAddr8(bx, by);
192
193 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400194 if (checkSurfacePixels) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400195 uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400196 ? 0xFF
197 : SkGetPackedA32(get_src_color(devx, devy));
198 if (surfaceAlpha != *alpha) {
199 ERRORF(reporter,
200 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
201 bx, by, surfaceAlpha, *alpha);
lsalzmana2415ac2016-10-11 14:29:12 -0700202 return false;
203 }
204 }
205 } else if (checkBitmapPixels) {
206 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
207 if (origDstAlpha != *alpha) {
208 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
209 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
210 return false;
211 }
212 }
213 }
214 }
215 return true;
216 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000217 for (int by = 0; by < bh; ++by) {
218 for (int bx = 0; bx < bw; ++bx) {
219 int devx = bx + srcRect.fLeft;
220 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000222 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000223
224 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400225 if (checkSurfacePixels) {
226 SkPMColor surfacePMColor = get_src_color(devx, devy);
Mike Klein12d4b6e2018-08-17 14:09:55 -0400227 if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
228 surfacePMColor &= 0xFF000000;
229 }
230 if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400231 surfacePMColor |= 0xFF000000;
232 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000233 bool didPremul;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400234 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
235 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
236 ERRORF(reporter,
237 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
238 "Readback was unpremul: %d",
239 bx, by, surfacePMColor, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000240 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000241 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000242 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000243 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700244 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
245 if (origDstPixel != *pixel) {
246 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
247 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000248 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000249 }
250 }
251 }
252 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000253 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000254}
255
256enum BitmapInit {
257 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258
Mike Reed12e946b2017-04-17 10:53:29 -0400259 kTight_BitmapInit = kFirstBitmapInit,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000260 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800261 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262
bsalomon9d02b262016-02-01 12:49:30 -0800263 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500264
265#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800266 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500267#else
268 kLast_BitmapInit = kLastAligned_BitmapInit
269#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000270};
271
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000272static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000273 int x = bmi;
274 return static_cast<BitmapInit>(++x);
275}
276
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000277static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
278 SkAlphaType at) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400279 SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000280 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000281 switch (init) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000282 case kTight_BitmapInit:
283 break;
284 case kRowBytes_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700285 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000286 break;
bsalomon9d02b262016-02-01 12:49:30 -0800287 case kRowBytesOdd_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700288 rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
bsalomon9d02b262016-02-01 12:49:30 -0800289 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000290 default:
291 SkASSERT(0);
292 break;
293 }
Mike Reed12e946b2017-04-17 10:53:29 -0400294 bitmap->allocPixels(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000295}
296
kkinnunen15302832015-12-01 04:35:26 -0800297static const struct {
298 SkColorType fColorType;
299 SkAlphaType fAlphaType;
300} gReadPixelsConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400301 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
302 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
303 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
304 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
305 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
306 {kAlpha_8_SkColorType, kPremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800307};
308const SkIRect gReadPixelsTestRects[] = {
309 // entire thing
310 DEV_RECT,
311 // larger on all sides
312 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
313 // fully contained
314 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
315 // outside top left
316 SkIRect::MakeLTRB(-10, -10, -1, -1),
317 // touching top left corner
318 SkIRect::MakeLTRB(-10, -10, 0, 0),
319 // overlapping top left corner
320 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
321 // overlapping top left and top right corners
322 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
323 // touching entire top edge
324 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
325 // overlapping top right corner
326 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
327 // contained in x, overlapping top edge
328 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
329 // outside top right corner
330 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
331 // touching top right corner
332 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
333 // overlapping top left and bottom left corners
334 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
335 // touching entire left edge
336 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
337 // overlapping bottom left corner
338 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
339 // contained in y, overlapping left edge
340 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
341 // outside bottom left corner
342 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
343 // touching bottom left corner
344 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
345 // overlapping bottom left and bottom right corners
346 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
347 // touching entire left edge
348 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
349 // overlapping bottom right corner
350 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
351 // overlapping top right and bottom right corners
352 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
353};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000354
Brian Salomon1d435302019-07-01 13:05:28 -0400355bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
356 const SkImageInfo& srcInfo) {
357 return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400358}
359
reede8f30622016-03-23 18:59:25 -0700360static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400361 const SkImageInfo& surfaceInfo, BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800362 SkCanvas* canvas = surface->getCanvas();
363 fill_src_canvas(canvas);
364 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
365 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800366 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800367 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
368 SkBitmap bmp;
369 init_bitmap(&bmp, srcRect, bmi,
370 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700371
kkinnunen15302832015-12-01 04:35:26 -0800372 // if the bitmap has pixels allocated before the readPixels,
373 // note that and fill them with pattern
374 bool startsWithPixels = !bmp.isNull();
375 if (startsWithPixels) {
376 fill_dst_bmp_with_init_data(&bmp);
377 }
378 uint32_t idBefore = surface->generationID();
Mike Reedf1942192017-07-21 14:24:29 -0400379 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
kkinnunen15302832015-12-01 04:35:26 -0800380 uint32_t idAfter = surface->generationID();
381
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400382 // we expect to succeed when the read isn't fully clipped out and the infos are
383 // compatible.
Brian Salomon1d435302019-07-01 13:05:28 -0400384 bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800385 // determine whether we expected the read to succeed.
Brian Salomon1d435302019-07-01 13:05:28 -0400386 REPORTER_ASSERT(reporter, expectSuccess == success,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400387 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
388 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
389 bmp.info().colorType(), bmp.info().alphaType());
kkinnunen15302832015-12-01 04:35:26 -0800390 // read pixels should never change the gen id
391 REPORTER_ASSERT(reporter, idBefore == idAfter);
392
393 if (success || startsWithPixels) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400394 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400395 startsWithPixels, surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800396 } else {
397 // if we had no pixels beforehand and the readPixels
398 // failed then our bitmap should still not have pixels
399 REPORTER_ASSERT(reporter, bmp.isNull());
400 }
401 }
kkinnunen15302832015-12-01 04:35:26 -0800402 }
403 }
404}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400405
kkinnunen15302832015-12-01 04:35:26 -0800406DEF_TEST(ReadPixels, reporter) {
407 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700408 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800409 // SW readback fails a premul check when reading back to an unaligned rowbytes.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400410 test_readpixels(reporter, surface, info, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800411}
egdaniel88e8aef2016-06-27 14:34:55 -0700412DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400413 static const SkImageInfo kImageInfos[] = {
414 SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
415 SkImageInfo::Make(DEV_W, DEV_H, kBGRA_8888_SkColorType, kPremul_SkAlphaType),
416 SkImageInfo::Make(DEV_W, DEV_H, kRGB_888x_SkColorType, kOpaque_SkAlphaType),
417 SkImageInfo::Make(DEV_W, DEV_H, kAlpha_8_SkColorType, kPremul_SkAlphaType),
418 };
419 for (const auto& ii : kImageInfos) {
420 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
421 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(
422 ctxInfo.grContext(), SkBudgeted::kNo, ii, 0, origin, nullptr));
423 if (!surface) {
424 continue;
425 }
426 test_readpixels(reporter, surface, ii, kLast_BitmapInit);
427 }
kkinnunen15302832015-12-01 04:35:26 -0800428 }
429}
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000430
Robert Phillipse78b7252017-04-06 07:59:41 -0400431static void test_readpixels_texture(skiatest::Reporter* reporter,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400432 std::unique_ptr<GrSurfaceContext> sContext,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400433 const SkImageInfo& surfaceInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800434 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
435 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800436 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800437 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
438 SkBitmap bmp;
439 init_bitmap(&bmp, srcRect, bmi,
440 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
441
442 // if the bitmap has pixels allocated before the readPixels,
443 // note that and fill them with pattern
444 bool startsWithPixels = !bmp.isNull();
445 // Try doing the read directly from a non-renderable texture
446 if (startsWithPixels) {
447 fill_dst_bmp_with_init_data(&bmp);
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400448 bool success = sContext->readPixels(bmp.info(), bmp.getPixels(),
Brian Salomon1d435302019-07-01 13:05:28 -0400449 bmp.rowBytes(), {srcRect.fLeft, srcRect.fTop});
450 auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400451 REPORTER_ASSERT(
Brian Salomon1d435302019-07-01 13:05:28 -0400452 reporter, expectSuccess == success,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400453 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
454 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
455 bmp.info().colorType(), bmp.info().alphaType());
456 if (success) {
457 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400458 surfaceInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400459 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000460 }
461 }
462 }
463 }
464}
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400465
egdaniel88e8aef2016-06-27 14:34:55 -0700466DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400467 GrContext* context = ctxInfo.grContext();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400468 SkBitmap bmp = make_src_bitmap();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400469
kkinnunen15302832015-12-01 04:35:26 -0800470 // On the GPU we will also try reading back from a non-renderable texture.
Brian Salomon8b1fb742016-11-03 15:21:06 -0400471 for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400472 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Brian Salomon58389b92018-03-07 13:01:25 -0500473 sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomone7499c72019-06-24 12:12:36 -0400474 context, renderable, DEV_W, DEV_H, bmp.colorType(), bmp.alphaType(), origin,
475 bmp.getPixels(), bmp.rowBytes());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400476 auto sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400477 std::move(proxy), SkColorTypeToGrColorType(bmp.colorType()),
478 kPremul_SkAlphaType);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400479 auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
480 test_readpixels_texture(reporter, std::move(sContext), info);
Brian Salomon8b1fb742016-11-03 15:21:06 -0400481 }
kkinnunen15302832015-12-01 04:35:26 -0800482 }
483}
Matt Sarett8572d852017-02-14 11:21:02 -0500484
485///////////////////////////////////////////////////////////////////////////////////////////////////
486
487static const uint32_t kNumPixels = 5;
488
489// The five reference pixels are: red, green, blue, white, black.
490// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
491// plus a tail pixel.
492static const uint32_t rgba[kNumPixels] = {
493 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
494};
495static const uint32_t bgra[kNumPixels] = {
496 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
497};
498static const uint16_t rgb565[kNumPixels] = {
499 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
500};
501
502static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
503
504static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
505static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
506static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
507static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
508static const uint64_t f16[kNumPixels] = {
509 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
510};
511
Matt Sarett8572d852017-02-14 11:21:02 -0500512static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
513static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
514
515static const void* five_reference_pixels(SkColorType colorType) {
516 switch (colorType) {
517 case kUnknown_SkColorType:
518 return nullptr;
519 case kAlpha_8_SkColorType:
520 return alpha8;
521 case kRGB_565_SkColorType:
522 return rgb565;
523 case kARGB_4444_SkColorType:
524 return rgba4444;
525 case kRGBA_8888_SkColorType:
526 return rgba;
527 case kBGRA_8888_SkColorType:
528 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500529 case kGray_8_SkColorType:
530 return gray8;
531 case kRGBA_F16_SkColorType:
532 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400533 default:
Leon Scroggins IIId6832d02018-09-04 11:10:04 -0400534 return nullptr;
Matt Sarett8572d852017-02-14 11:21:02 -0500535 }
536
537 SkASSERT(false);
538 return nullptr;
539}
540
541static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
542 const SkImageInfo& srcInfo) {
Brian Osmane1adc3a2018-06-04 09:21:17 -0400543 if (!SkImageInfoIsValid(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500544 return;
545 }
546
Matt Sarett8572d852017-02-14 11:21:02 -0500547 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400548 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500549 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
550 REPORTER_ASSERT(r, src);
551
552 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
553 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400554 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500555 bool success = src->readPixels(dstPixmap, 0, 0);
556 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
557
558 if (success) {
559 if (kGray_8_SkColorType == srcInfo.colorType() &&
Mike Klein12d4b6e2018-08-17 14:09:55 -0400560 kGray_8_SkColorType != dstInfo.colorType()) {
561 // TODO: test (r,g,b) == (gray,gray,gray)?
562 return;
563 }
564
565 if (kGray_8_SkColorType == dstInfo.colorType() &&
566 kGray_8_SkColorType != srcInfo.colorType()) {
567 // TODO: test gray = luminance?
568 return;
569 }
570
571 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
572 kAlpha_8_SkColorType != dstInfo.colorType()) {
573 // TODO: test output = black with this alpha?
Matt Sarett8572d852017-02-14 11:21:02 -0500574 return;
575 }
576
577 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
578 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
Matt Sarett8572d852017-02-14 11:21:02 -0500579 }
580}
581
582DEF_TEST(ReadPixels_ValidConversion, reporter) {
583 const SkColorType kColorTypes[] = {
584 kUnknown_SkColorType,
585 kAlpha_8_SkColorType,
586 kRGB_565_SkColorType,
587 kARGB_4444_SkColorType,
588 kRGBA_8888_SkColorType,
589 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500590 kGray_8_SkColorType,
591 kRGBA_F16_SkColorType,
592 };
593
594 const SkAlphaType kAlphaTypes[] = {
595 kUnknown_SkAlphaType,
596 kOpaque_SkAlphaType,
597 kPremul_SkAlphaType,
598 kUnpremul_SkAlphaType,
599 };
600
601 const sk_sp<SkColorSpace> kColorSpaces[] = {
602 nullptr,
603 SkColorSpace::MakeSRGB(),
604 };
605
606 for (SkColorType dstCT : kColorTypes) {
607 for (SkAlphaType dstAT: kAlphaTypes) {
608 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
609 for (SkColorType srcCT : kColorTypes) {
610 for (SkAlphaType srcAT: kAlphaTypes) {
611 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500612 test_conversion(reporter,
613 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
614 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
615 }
616 }
617 }
618 }
619 }
620 }
621}
Brian Salomonab32f652019-05-10 14:24:50 -0400622
Brian Salomoncd734f62019-05-10 16:32:54 -0400623static int min_rgb_channel_bits(SkColorType ct) {
624 switch (ct) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400625 case kUnknown_SkColorType: return 0;
626 case kAlpha_8_SkColorType: return 8;
627 case kA16_unorm_SkColorType: return 16;
628 case kA16_float_SkColorType: return 16;
629 case kRGB_565_SkColorType: return 5;
630 case kARGB_4444_SkColorType: return 4;
631 case kR8G8_unorm_SkColorType: return 8;
632 case kR16G16_unorm_SkColorType: return 16;
633 case kR16G16_float_SkColorType: return 16;
634 case kRGBA_8888_SkColorType: return 8;
635 case kRGB_888x_SkColorType: return 8;
636 case kBGRA_8888_SkColorType: return 8;
637 case kRGBA_1010102_SkColorType: return 10;
638 case kRGB_101010x_SkColorType: return 10;
639 case kGray_8_SkColorType: return 8; // counting gray as "rgb"
640 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
641 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
642 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
643 case kR16G16B16A16_unorm_SkColorType: return 16;
Brian Salomoncd734f62019-05-10 16:32:54 -0400644 }
645 SK_ABORT("Unexpected color type.");
Brian Salomoncd734f62019-05-10 16:32:54 -0400646}
647
Brian Salomonab32f652019-05-10 14:24:50 -0400648DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) {
649 struct Context {
650 SkPixmap* fPixmap = nullptr;
651 bool fSuceeded = false;
652 bool fCalled = false;
653 };
654 auto callback = [](SkSurface::ReleaseContext context, const void* data, size_t rowBytes) {
655 auto* pm = static_cast<Context*>(context)->fPixmap;
656 static_cast<Context*>(context)->fCalled = true;
657 if ((static_cast<Context*>(context)->fSuceeded = SkToBool(data))) {
658 auto dst = static_cast<char*>(pm->writable_addr());
659 const auto* src = static_cast<const char*>(data);
660 for (int y = 0; y < pm->height(); ++y, src += rowBytes, dst += pm->rowBytes()) {
661 memcpy(dst, src, pm->width() * SkColorTypeBytesPerPixel(pm->colorType()));
662 }
663 }
664 };
665 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
666 static constexpr int kW = 16;
667 static constexpr int kH = 16;
Brian Salomoncd734f62019-05-10 16:32:54 -0400668 for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
669 auto surfCT = static_cast<SkColorType>(sct);
Brian Salomon5dd77462019-09-26 16:57:09 -0400670 auto info = SkImageInfo::Make(kW, kH, surfCT, kPremul_SkAlphaType,
671 SkColorSpace::MakeSRGB());
Brian Salomonab32f652019-05-10 14:24:50 -0400672 auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info, 1,
673 origin, nullptr);
674 if (!surf) {
675 continue;
676 }
Brian Salomon5dd77462019-09-26 16:57:09 -0400677 auto refSurf = SkSurface::MakeRaster(info);
678 SkNWayCanvas nway(info.width(), info.height());
679 nway.addCanvas(surf->getCanvas());
680 nway.addCanvas(refSurf->getCanvas());
681
Brian Salomonab32f652019-05-10 14:24:50 -0400682 float d = std::sqrt((float)surf->width() * surf->width() +
683 (float)surf->height() * surf->height());
684 for (int j = 0; j < surf->height(); ++j) {
685 for (int i = 0; i < surf->width(); ++i) {
686 float r = i / (float)surf->width();
687 float g = 1.f - i / (float)surf->height();
688 float b = std::sqrt((float)i * i + (float)j * j) / d;
689 SkPaint paint;
690 paint.setColor4f(SkColor4f{r, g, b, 1.f}, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -0400691 nway.drawRect(SkRect::MakeXYWH(i, j, 1, 1), paint);
Brian Salomonab32f652019-05-10 14:24:50 -0400692 }
693 }
Brian Salomon5dd77462019-09-26 16:57:09 -0400694 for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
695 auto readCT = static_cast<SkColorType>(rct);
696
697 for (const sk_sp<SkColorSpace>& readCS :
698 {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) {
699 auto refImg = refSurf->makeImageSnapshot()->makeColorTypeAndColorSpace(readCT,
700 readCS);
701 // Test full size, partial, empty, and too wide rects.
702 for (const auto& rect : {SkIRect::MakeWH(kW, kH),
703 SkIRect::MakeLTRB(1, 2, kW - 3, kH - 4),
704 SkIRect::MakeXYWH(1, 1, 0, 0),
705 SkIRect::MakeWH(kW + 1, kH / 2)}) {
Brian Salomoncd734f62019-05-10 16:32:54 -0400706 SkAutoPixmapStorage result;
707 Context context;
708 context.fPixmap = &result;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400709 info = SkImageInfo::Make(rect.size(), readCT, kPremul_SkAlphaType, readCS);
Brian Salomoncd734f62019-05-10 16:32:54 -0400710 result.alloc(info);
711 memset(result.writable_addr(), 0xAB, result.computeByteSize());
Brian Salomon201700f2019-05-17 12:05:44 -0400712 // Rescale quality and linearity don't matter since we're doing a non-
713 // scaling readback.
714 surf->asyncRescaleAndReadPixels(info, rect, SkSurface::RescaleGamma::kSrc,
715 kNone_SkFilterQuality, callback, &context);
Brian Salomoncd734f62019-05-10 16:32:54 -0400716 while (!context.fCalled) {
717 ctxInfo.grContext()->checkAsyncWorkCompletion();
Brian Salomonab32f652019-05-10 14:24:50 -0400718 }
Brian Salomoncd734f62019-05-10 16:32:54 -0400719 if (rect.isEmpty() || !SkIRect::MakeWH(kW, kH).contains(rect)) {
720 REPORTER_ASSERT(reporter, !context.fSuceeded);
721 }
Brian Salomon5dd77462019-09-26 16:57:09 -0400722
723 bool didCSConversion =
724 !SkColorSpace::Equals(readCS.get(), surf->imageInfo().colorSpace());
725
Brian Salomon624f9062019-07-02 14:23:00 -0400726 if (context.fSuceeded) {
727 REPORTER_ASSERT(reporter, readCT != kUnknown_SkColorType &&
728 !rect.isEmpty());
729 } else {
Brian Salomon5dd77462019-09-26 16:57:09 -0400730 // TODO: Support reading to kGray, support kRGB_101010x at all in GPU.
Brian Salomon624f9062019-07-02 14:23:00 -0400731 auto surfBounds = SkIRect::MakeWH(surf->width(), surf->height());
732 if (readCT != kUnknown_SkColorType && readCT != kGray_8_SkColorType &&
Brian Salomon5dd77462019-09-26 16:57:09 -0400733 readCT != kRGB_101010x_SkColorType && !rect.isEmpty() &&
734 surfBounds.contains(rect)) {
Brian Salomon624f9062019-07-02 14:23:00 -0400735 ERRORF(reporter,
Brian Salomon5dd77462019-09-26 16:57:09 -0400736 "Async read failed. Surf Color Type: %s, Read CT: %s,"
Brian Salomon624f9062019-07-02 14:23:00 -0400737 "Rect [%d, %d, %d, %d], origin: %d, CS conversion: %d\n",
Brian Salomon5dd77462019-09-26 16:57:09 -0400738 ToolUtils::colortype_name(surfCT),
739 ToolUtils::colortype_name(readCT),
740 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, origin,
741 didCSConversion);
Brian Salomon624f9062019-07-02 14:23:00 -0400742 }
Brian Salomoncd734f62019-05-10 16:32:54 -0400743 continue;
744 }
Brian Salomon5dd77462019-09-26 16:57:09 -0400745 SkPixmap ref;
746 refImg->peekPixels(&ref);
747 SkAssertResult(ref.extractSubset(&ref, rect));
Brian Salomoncd734f62019-05-10 16:32:54 -0400748
Brian Salomon5dd77462019-09-26 16:57:09 -0400749 // A CS conversion allows a 3 value difference and otherwise a 2 value
750 // difference. Note that sometimes read back on GPU can be lossy even when
751 // there no conversion at all because GPU->CPU read may go to a a lower bit
752 // depth format and then be promoted back to the original type. For example,
753 // GL ES cannot read to 1010102, so we go through 8888.
754 float numer = didCSConversion ? 3.f : 2.f;
755 int rgbBits = std::min({min_rgb_channel_bits(readCT),
756 min_rgb_channel_bits(surfCT), 8});
757 float tol = numer / (1 << rgbBits);
Brian Salomoncd734f62019-05-10 16:32:54 -0400758 const float tols[4] = {tol, tol, tol, 0};
759 auto error = std::function<ComparePixmapsErrorReporter>(
760 [&](int x, int y, const float diffs[4]) {
Brian Salomon85aeccf2019-07-15 12:30:44 -0400761 SkASSERT(x >= 0 && y >= 0);
Brian Salomoncd734f62019-05-10 16:32:54 -0400762 ERRORF(reporter,
Brian Salomon5dd77462019-09-26 16:57:09 -0400763 "Surf Color Type: %s, Read CT: %s, Rect [%d, %d, %d, %d]"
Brian Salomoncd734f62019-05-10 16:32:54 -0400764 ", origin: %d, CS conversion: %d\n"
765 "Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
Brian Salomon5dd77462019-09-26 16:57:09 -0400766 ToolUtils::colortype_name(surfCT),
767 ToolUtils::colortype_name(readCT),
768 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, origin,
769 didCSConversion, x, y, diffs[0], diffs[1], diffs[2],
770 diffs[3]);
Brian Salomoncd734f62019-05-10 16:32:54 -0400771 });
Brian Salomon85aeccf2019-07-15 12:30:44 -0400772 compare_pixels(ref, result, tols, error);
Brian Salomonab32f652019-05-10 14:24:50 -0400773 }
774 }
775 }
776 }
777 }
778}