blob: c6cd4765ae8fa6af9ea860f445c7ba51fdf0a6ac [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 Salomon4eda7102019-10-21 15:04:52 -0400474 context, renderable, origin, bmp.info(), bmp.getPixels(), bmp.rowBytes());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400475 auto sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400476 std::move(proxy), SkColorTypeToGrColorType(bmp.colorType()),
477 kPremul_SkAlphaType);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400478 auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
479 test_readpixels_texture(reporter, std::move(sContext), info);
Brian Salomon8b1fb742016-11-03 15:21:06 -0400480 }
kkinnunen15302832015-12-01 04:35:26 -0800481 }
482}
Matt Sarett8572d852017-02-14 11:21:02 -0500483
484///////////////////////////////////////////////////////////////////////////////////////////////////
485
486static const uint32_t kNumPixels = 5;
487
488// The five reference pixels are: red, green, blue, white, black.
489// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
490// plus a tail pixel.
491static const uint32_t rgba[kNumPixels] = {
492 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
493};
494static const uint32_t bgra[kNumPixels] = {
495 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
496};
497static const uint16_t rgb565[kNumPixels] = {
498 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
499};
500
501static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
502
503static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
504static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
505static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
506static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
507static const uint64_t f16[kNumPixels] = {
508 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
509};
510
Matt Sarett8572d852017-02-14 11:21:02 -0500511static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
512static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
513
514static const void* five_reference_pixels(SkColorType colorType) {
515 switch (colorType) {
516 case kUnknown_SkColorType:
517 return nullptr;
518 case kAlpha_8_SkColorType:
519 return alpha8;
520 case kRGB_565_SkColorType:
521 return rgb565;
522 case kARGB_4444_SkColorType:
523 return rgba4444;
524 case kRGBA_8888_SkColorType:
525 return rgba;
526 case kBGRA_8888_SkColorType:
527 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500528 case kGray_8_SkColorType:
529 return gray8;
530 case kRGBA_F16_SkColorType:
531 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400532 default:
Leon Scroggins IIId6832d02018-09-04 11:10:04 -0400533 return nullptr;
Matt Sarett8572d852017-02-14 11:21:02 -0500534 }
535
536 SkASSERT(false);
537 return nullptr;
538}
539
540static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
541 const SkImageInfo& srcInfo) {
Brian Osmane1adc3a2018-06-04 09:21:17 -0400542 if (!SkImageInfoIsValid(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500543 return;
544 }
545
Matt Sarett8572d852017-02-14 11:21:02 -0500546 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400547 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500548 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
549 REPORTER_ASSERT(r, src);
550
551 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
552 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400553 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500554 bool success = src->readPixels(dstPixmap, 0, 0);
555 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
556
557 if (success) {
558 if (kGray_8_SkColorType == srcInfo.colorType() &&
Mike Klein12d4b6e2018-08-17 14:09:55 -0400559 kGray_8_SkColorType != dstInfo.colorType()) {
560 // TODO: test (r,g,b) == (gray,gray,gray)?
561 return;
562 }
563
564 if (kGray_8_SkColorType == dstInfo.colorType() &&
565 kGray_8_SkColorType != srcInfo.colorType()) {
566 // TODO: test gray = luminance?
567 return;
568 }
569
570 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
571 kAlpha_8_SkColorType != dstInfo.colorType()) {
572 // TODO: test output = black with this alpha?
Matt Sarett8572d852017-02-14 11:21:02 -0500573 return;
574 }
575
576 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
577 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
Matt Sarett8572d852017-02-14 11:21:02 -0500578 }
579}
580
581DEF_TEST(ReadPixels_ValidConversion, reporter) {
582 const SkColorType kColorTypes[] = {
583 kUnknown_SkColorType,
584 kAlpha_8_SkColorType,
585 kRGB_565_SkColorType,
586 kARGB_4444_SkColorType,
587 kRGBA_8888_SkColorType,
588 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500589 kGray_8_SkColorType,
590 kRGBA_F16_SkColorType,
591 };
592
593 const SkAlphaType kAlphaTypes[] = {
594 kUnknown_SkAlphaType,
595 kOpaque_SkAlphaType,
596 kPremul_SkAlphaType,
597 kUnpremul_SkAlphaType,
598 };
599
600 const sk_sp<SkColorSpace> kColorSpaces[] = {
601 nullptr,
602 SkColorSpace::MakeSRGB(),
603 };
604
605 for (SkColorType dstCT : kColorTypes) {
606 for (SkAlphaType dstAT: kAlphaTypes) {
607 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
608 for (SkColorType srcCT : kColorTypes) {
609 for (SkAlphaType srcAT: kAlphaTypes) {
610 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500611 test_conversion(reporter,
612 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
613 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
614 }
615 }
616 }
617 }
618 }
619 }
620}
Brian Salomonab32f652019-05-10 14:24:50 -0400621
Brian Salomoncd734f62019-05-10 16:32:54 -0400622static int min_rgb_channel_bits(SkColorType ct) {
623 switch (ct) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400624 case kUnknown_SkColorType: return 0;
625 case kAlpha_8_SkColorType: return 8;
626 case kA16_unorm_SkColorType: return 16;
627 case kA16_float_SkColorType: return 16;
628 case kRGB_565_SkColorType: return 5;
629 case kARGB_4444_SkColorType: return 4;
630 case kR8G8_unorm_SkColorType: return 8;
631 case kR16G16_unorm_SkColorType: return 16;
632 case kR16G16_float_SkColorType: return 16;
633 case kRGBA_8888_SkColorType: return 8;
634 case kRGB_888x_SkColorType: return 8;
635 case kBGRA_8888_SkColorType: return 8;
636 case kRGBA_1010102_SkColorType: return 10;
637 case kRGB_101010x_SkColorType: return 10;
638 case kGray_8_SkColorType: return 8; // counting gray as "rgb"
639 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
640 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
641 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
642 case kR16G16B16A16_unorm_SkColorType: return 16;
Brian Salomoncd734f62019-05-10 16:32:54 -0400643 }
644 SK_ABORT("Unexpected color type.");
Brian Salomoncd734f62019-05-10 16:32:54 -0400645}
646
Brian Salomon9241a6d2019-10-03 13:26:54 -0400647namespace {
648struct AsyncContext {
649 bool fCalled = false;
650 std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
651};
652} // anonymous namespace
653
654// Making this a lambda in the test functions caused:
655// "error: cannot compile this forwarded non-trivially copyable parameter yet"
656// on x86/Win/Clang bot, referring to 'result'.
657static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
658 auto context = static_cast<AsyncContext*>(c);
659 context->fResult = std::move(result);
660 context->fCalled = true;
661};
662
Brian Salomonab32f652019-05-10 14:24:50 -0400663DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400664 struct LegacyContext {
Brian Salomonab32f652019-05-10 14:24:50 -0400665 SkPixmap* fPixmap = nullptr;
666 bool fSuceeded = false;
667 bool fCalled = false;
668 };
Brian Salomon9241a6d2019-10-03 13:26:54 -0400669 auto legacy_callback = [](SkSurface::ReleaseContext context, const void* data,
670 size_t rowBytes) {
671 auto* pm = static_cast<LegacyContext*>(context)->fPixmap;
672 static_cast<LegacyContext*>(context)->fCalled = true;
673 if ((static_cast<LegacyContext*>(context)->fSuceeded = SkToBool(data))) {
Brian Salomonab32f652019-05-10 14:24:50 -0400674 auto dst = static_cast<char*>(pm->writable_addr());
675 const auto* src = static_cast<const char*>(data);
676 for (int y = 0; y < pm->height(); ++y, src += rowBytes, dst += pm->rowBytes()) {
677 memcpy(dst, src, pm->width() * SkColorTypeBytesPerPixel(pm->colorType()));
678 }
679 }
680 };
681 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
682 static constexpr int kW = 16;
683 static constexpr int kH = 16;
Brian Salomoncd734f62019-05-10 16:32:54 -0400684 for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
685 auto surfCT = static_cast<SkColorType>(sct);
Brian Salomon5dd77462019-09-26 16:57:09 -0400686 auto info = SkImageInfo::Make(kW, kH, surfCT, kPremul_SkAlphaType,
687 SkColorSpace::MakeSRGB());
Brian Salomonab32f652019-05-10 14:24:50 -0400688 auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info, 1,
689 origin, nullptr);
690 if (!surf) {
691 continue;
692 }
Brian Salomon5dd77462019-09-26 16:57:09 -0400693 auto refSurf = SkSurface::MakeRaster(info);
694 SkNWayCanvas nway(info.width(), info.height());
695 nway.addCanvas(surf->getCanvas());
696 nway.addCanvas(refSurf->getCanvas());
697
Brian Salomonab32f652019-05-10 14:24:50 -0400698 float d = std::sqrt((float)surf->width() * surf->width() +
699 (float)surf->height() * surf->height());
700 for (int j = 0; j < surf->height(); ++j) {
701 for (int i = 0; i < surf->width(); ++i) {
702 float r = i / (float)surf->width();
703 float g = 1.f - i / (float)surf->height();
704 float b = std::sqrt((float)i * i + (float)j * j) / d;
705 SkPaint paint;
706 paint.setColor4f(SkColor4f{r, g, b, 1.f}, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -0400707 nway.drawRect(SkRect::MakeXYWH(i, j, 1, 1), paint);
Brian Salomonab32f652019-05-10 14:24:50 -0400708 }
709 }
Brian Salomon5dd77462019-09-26 16:57:09 -0400710 for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
711 auto readCT = static_cast<SkColorType>(rct);
712
713 for (const sk_sp<SkColorSpace>& readCS :
714 {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) {
715 auto refImg = refSurf->makeImageSnapshot()->makeColorTypeAndColorSpace(readCT,
716 readCS);
717 // Test full size, partial, empty, and too wide rects.
718 for (const auto& rect : {SkIRect::MakeWH(kW, kH),
719 SkIRect::MakeLTRB(1, 2, kW - 3, kH - 4),
720 SkIRect::MakeXYWH(1, 1, 0, 0),
721 SkIRect::MakeWH(kW + 1, kH / 2)}) {
Brian Salomon9241a6d2019-10-03 13:26:54 -0400722 for (bool legacy : {false, true}) {
723 SkPixmap result;
724 std::unique_ptr<char[]> tempPixels;
725 info = SkImageInfo::Make(rect.size(), readCT, kPremul_SkAlphaType,
726 readCS);
727 // Rescale quality and linearity don't matter since we're doing a non-
728 // scaling readback.
729 static constexpr auto kQuality = kNone_SkFilterQuality;
730 static constexpr auto kGamma = SkSurface::RescaleGamma::kSrc;
731 bool succeeded = false;
732 // This holds the pixel results and so must live until comparisons are
733 // finished.
734 AsyncContext asyncContext;
735 if (legacy) {
736 LegacyContext context;
737 tempPixels.reset(new char[info.computeMinByteSize()]);
738 result.reset(info, tempPixels.get(), info.minRowBytes());
739 memset(result.writable_addr(), 0xAB, info.computeMinByteSize());
740 context.fPixmap = &result;
741 surf->asyncRescaleAndReadPixels(info, rect, kGamma, kQuality,
742 legacy_callback, &context);
743 while (!context.fCalled) {
744 ctxInfo.grContext()->checkAsyncWorkCompletion();
745 }
746 succeeded = context.fSuceeded;
747 } else {
748 surf->asyncRescaleAndReadPixels(info, rect, kGamma, kQuality,
749 async_callback, &asyncContext);
750 while (!asyncContext.fCalled) {
751 ctxInfo.grContext()->checkAsyncWorkCompletion();
752 }
753 if (asyncContext.fResult) {
754 int count = asyncContext.fResult->count();
755 if (count == 1) {
756 succeeded = true;
757 result.reset(info,
758 asyncContext.fResult->data(0),
759 asyncContext.fResult->rowBytes(0));
760 } else {
761 ERRORF(reporter, "Unexpected AsyncResult::count(): %d",
762 count);
763 continue;
764 }
765 }
Ravi Mistrycb550102019-10-03 09:06:25 +0000766 }
Ravi Mistrycb550102019-10-03 09:06:25 +0000767
Brian Salomon9241a6d2019-10-03 13:26:54 -0400768 if (rect.isEmpty() || !SkIRect::MakeWH(kW, kH).contains(rect)) {
769 REPORTER_ASSERT(reporter, !succeeded);
770 }
771
772 bool didCSConversion = !SkColorSpace::Equals(
773 readCS.get(), surf->imageInfo().colorSpace());
774
775 if (succeeded) {
776 REPORTER_ASSERT(reporter,
777 readCT != kUnknown_SkColorType && !rect.isEmpty());
778 } else {
779 // TODO: Support reading to kGray, support kRGB_101010x at all in
780 // GPU.
781 auto surfBounds = SkIRect::MakeWH(surf->width(), surf->height());
782 if (readCT != kUnknown_SkColorType &&
783 readCT != kGray_8_SkColorType &&
784 readCT != kRGB_101010x_SkColorType && !rect.isEmpty() &&
785 surfBounds.contains(rect)) {
Ravi Mistrycb550102019-10-03 09:06:25 +0000786 ERRORF(reporter,
Brian Salomon9241a6d2019-10-03 13:26:54 -0400787 "Async read failed. Surf Color Type: %s, Read CT: %s,"
788 "Rect [%d, %d, %d, %d], origin: %d, CS conversion: %d\n",
Ravi Mistrycb550102019-10-03 09:06:25 +0000789 ToolUtils::colortype_name(surfCT),
Brian Salomon9241a6d2019-10-03 13:26:54 -0400790 ToolUtils::colortype_name(readCT), rect.fLeft, rect.fTop,
791 rect.fRight, rect.fBottom, origin, didCSConversion);
792 }
793 continue;
794 }
795 SkPixmap ref;
796 refImg->peekPixels(&ref);
797 SkAssertResult(ref.extractSubset(&ref, rect));
798
799 // A CS conversion allows a 3 value difference and otherwise a 2 value
800 // difference. Note that sometimes read back on GPU can be lossy even
801 // when there no conversion at all because GPU->CPU read may go to a
802 // lower bit depth format and then be promoted back to the original
803 // type. For example, GL ES cannot read to 1010102, so we go through
804 // 8888.
805 float numer = didCSConversion ? 3.f : 2.f;
806 int rgbBits = std::min({min_rgb_channel_bits(readCT),
807 min_rgb_channel_bits(surfCT), 8});
808 float tol = numer / (1 << rgbBits);
809 const float tols[4] = {tol, tol, tol, 0};
810 auto error = std::function<
811 ComparePixmapsErrorReporter>([&](int x, int y,
812 const float diffs[4]) {
813 SkASSERT(x >= 0 && y >= 0);
814 ERRORF(reporter,
815 "Surf Color Type: %s, Read CT: %s, Rect [%d, %d, %d, %d]"
816 ", origin: %d, CS conversion: %d\n"
817 "Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
818 ToolUtils::colortype_name(surfCT),
819 ToolUtils::colortype_name(readCT), rect.fLeft, rect.fTop,
820 rect.fRight, rect.fBottom, origin, didCSConversion, x, y,
821 diffs[0], diffs[1], diffs[2], diffs[3]);
822 });
823 compare_pixels(ref, result, tols, error);
824 }
Ravi Mistrycb550102019-10-03 09:06:25 +0000825 }
Brian Salomon6fc04f82019-10-02 19:11:55 -0400826 }
827 }
828 }
829 }
830}
Brian Salomon9241a6d2019-10-03 13:26:54 -0400831
832DEF_GPUTEST(AsyncReadPixelsContextShutdown, reporter, options) {
833 const auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
834 SkColorSpace::MakeSRGB());
835 enum class ShutdownSequence {
836 kFreeResult_DestroyContext,
837 kDestroyContext_FreeResult,
838 kFreeResult_ReleaseAndAbandon_DestroyContext,
839 kFreeResult_Abandon_DestroyContext,
840 kReleaseAndAbandon_FreeResult_DestroyContext,
841 kAbandon_FreeResult_DestroyContext,
842 kReleaseAndAbandon_DestroyContext_FreeResult,
843 kAbandon_DestroyContext_FreeResult,
844 };
845 for (int t = 0; t < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++t) {
846 auto type = static_cast<sk_gpu_test::GrContextFactory::ContextType>(t);
847 for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext,
848 ShutdownSequence::kDestroyContext_FreeResult,
849 ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext,
850 ShutdownSequence::kFreeResult_Abandon_DestroyContext,
851 ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext,
852 ShutdownSequence::kAbandon_FreeResult_DestroyContext,
853 ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult,
854 ShutdownSequence::kAbandon_DestroyContext_FreeResult}) {
855 // Vulkan context abandoning without resource release has issues outside of the scope of
856 // this test.
857 if (type == sk_gpu_test::GrContextFactory::kVulkan_ContextType &&
858 (sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext ||
859 sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult ||
860 sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext)) {
861 continue;
862 }
863 for (bool yuv : {false, true}) {
864 sk_gpu_test::GrContextFactory factory(options);
865 auto context = factory.get(type);
866 if (!context) {
867 continue;
868 }
869 // This test is only meaningful for contexts that support transfer buffers.
870 if (!context->priv().caps()->transferBufferSupport()) {
871 continue;
872 }
873 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 1, nullptr);
874 if (!surf) {
875 continue;
876 }
877 AsyncContext cbContext;
878 if (yuv) {
879 surf->asyncRescaleAndReadPixelsYUV420(
880 kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(), ii.bounds(),
881 ii.dimensions(), SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality,
882 &async_callback, &cbContext);
883 } else {
884 surf->asyncRescaleAndReadPixels(ii, ii.bounds(), SkSurface::RescaleGamma::kSrc,
885 kNone_SkFilterQuality, &async_callback,
886 &cbContext);
887 }
888 while (!cbContext.fCalled) {
889 context->checkAsyncWorkCompletion();
890 }
891 if (!cbContext.fResult) {
892 ERRORF(reporter, "Callback failed on %s. is YUV: %d",
893 sk_gpu_test::GrContextFactory::ContextTypeName(type), yuv);
894 continue;
895 }
896 // The real test is that we don't crash, get Vulkan validation errors, etc, during
897 // this shutdown sequence.
898 switch (sequence) {
899 case ShutdownSequence::kFreeResult_DestroyContext:
900 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
901 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
902 break;
903 case ShutdownSequence::kDestroyContext_FreeResult:
904 factory.destroyContexts();
905 break;
906 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
907 factory.releaseResourcesAndAbandonContexts();
908 break;
909 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
910 factory.abandonContexts();
911 break;
912 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
913 factory.releaseResourcesAndAbandonContexts();
914 factory.destroyContexts();
915 break;
916 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
917 factory.abandonContexts();
918 factory.destroyContexts();
919 break;
920 }
921 cbContext.fResult.reset();
922 switch (sequence) {
923 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
924 factory.releaseResourcesAndAbandonContexts();
925 break;
926 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
927 factory.abandonContexts();
928 break;
929 case ShutdownSequence::kFreeResult_DestroyContext:
930 case ShutdownSequence::kDestroyContext_FreeResult:
931 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
932 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
933 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
934 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
935 break;
936 }
937 }
938 }
939 }
940}