blob: 675ce44a9da12b3bbcefe88bc392a4080a1ab2b4 [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 Salomon5918e832019-10-23 13:34:36 -040011#include "include/effects/SkGradientShader.h"
Brian Salomonab32f652019-05-10 14:24:50 -040012#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkColorData.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkImageInfoPriv.h"
Brian Salomon5dd77462019-09-26 16:57:09 -040016#include "include/utils/SkNWayCanvas.h"
Brian Salomonab32f652019-05-10 14:24:50 -040017#include "src/core/SkAutoPixmapStorage.h"
Brian Salomon5918e832019-10-23 13:34:36 -040018#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040021#include "src/gpu/GrImageInfo.h"
Brian Salomonab32f652019-05-10 14:24:50 -040022#include "tests/Test.h"
Brian Salomon85aeccf2019-07-15 12:30:44 -040023#include "tests/TestUtils.h"
Brian Salomon5dd77462019-09-26 16:57:09 -040024#include "tools/ToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/gpu/GrContextFactory.h"
26#include "tools/gpu/ProxyUtils.h"
bsalomon@google.comc6980972011-11-02 19:57:21 +000027
bsalomon@google.comc6980972011-11-02 19:57:21 +000028static const int DEV_W = 100, DEV_H = 100;
29static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000030static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000031 DEV_H * SK_Scalar1);
32
bsalomone8d21e82015-07-16 08:23:13 -070033static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000034 SkASSERT(x >= 0 && x < DEV_W);
35 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000036
37 U8CPU r = x;
38 U8CPU g = y;
39 U8CPU b = 0xc;
40
41 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000042 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000043 case 0:
44 a = 0xff;
45 break;
46 case 1:
47 a = 0x80;
48 break;
49 case 2:
50 a = 0xCC;
51 break;
52 case 4:
53 a = 0x01;
54 break;
55 case 3:
56 a = 0x00;
57 break;
58 }
59 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000060}
halcanary9d524f22016-03-29 09:03:52 -070061
bsalomone8d21e82015-07-16 08:23:13 -070062static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000063 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000064
bsalomon@google.comc6980972011-11-02 19:57:21 +000065 U8CPU b = n & 0xff;
66 U8CPU g = (n >> 8) & 0xff;
67 U8CPU r = (n >> 16) & 0xff;
68 return SkPackARGB32(0xff, r, g , b);
69}
70
Brian Salomon5fba7ad2018-03-22 10:01:16 -040071// TODO: Make this consider both ATs
bsalomone8d21e82015-07-16 08:23:13 -070072static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
73 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000074 *doUnpremul = (kUnpremul_SkAlphaType == at);
75
76 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000077 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000078 switch (ct) {
79 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000080 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000081 g = static_cast<U8CPU>(c[1]);
82 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000083 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000084 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040085 case kRGB_888x_SkColorType: // fallthrough
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000086 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000087 r = static_cast<U8CPU>(c[0]);
88 g = static_cast<U8CPU>(c[1]);
89 b = static_cast<U8CPU>(c[2]);
Brian Salomon5fba7ad2018-03-22 10:01:16 -040090 // 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 +000091 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000092 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000093 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000094 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000095 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000096 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000097
98 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000099 r = SkMulDiv255Ceiling(r, a);
100 g = SkMulDiv255Ceiling(g, a);
101 b = SkMulDiv255Ceiling(b, a);
102 }
103 return SkPackARGB32(a, r, g, b);
104}
105
bsalomone8d21e82015-07-16 08:23:13 -0700106static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000107 static SkBitmap bmp;
108 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700109 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000110 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
111 for (int y = 0; y < DEV_H; ++y) {
112 for (int x = 0; x < DEV_W; ++x) {
113 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700114 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000115 }
116 }
117 }
bsalomone8d21e82015-07-16 08:23:13 -0700118 return bmp;
119}
120
121static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000122 canvas->save();
123 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500124 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000125 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700126 paint.setBlendMode(SkBlendMode::kSrc);
bsalomone8d21e82015-07-16 08:23:13 -0700127 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000128 canvas->restore();
129}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130
bsalomone8d21e82015-07-16 08:23:13 -0700131static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000132 int w = bitmap->width();
133 int h = bitmap->height();
134 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
135 for (int y = 0; y < h; ++y) {
136 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700137 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
138 if (kAlpha_8_SkColorType == bitmap->colorType()) {
139 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
140 *alpha = SkGetPackedA32(initColor);
141 } else {
142 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
143 *pixel = initColor;
144 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000145 }
146 }
147}
148
bsalomone8d21e82015-07-16 08:23:13 -0700149static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000150 if (!didPremulConversion) {
151 return a == b;
152 }
153 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
154 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
155 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
156 int32_t aB = SkGetPackedB32(a);
157
158 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
159 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
160 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
161 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
162
163 return aA == bA &&
164 SkAbs32(aR - bR) <= 1 &&
165 SkAbs32(aG - bG) <= 1 &&
166 SkAbs32(aB - bB) <= 1;
167}
168
bsalomon@google.comc6980972011-11-02 19:57:21 +0000169// checks the bitmap contains correct pixels after the readPixels
170// if the bitmap was prefilled with pixels it checks that these weren't
171// overwritten in the area outside the readPixels.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400172static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
173 bool checkSurfacePixels, bool checkBitmapPixels,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400174 SkImageInfo surfaceInfo) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400175 SkAlphaType bmpAT = bitmap.alphaType();
176 SkColorType bmpCT = bitmap.colorType();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000177 SkASSERT(!bitmap.isNull());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400178 SkASSERT(checkSurfacePixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000179
bsalomon@google.comc6980972011-11-02 19:57:21 +0000180 int bw = bitmap.width();
181 int bh = bitmap.height();
182
183 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
184 SkIRect clippedSrcRect = DEV_RECT;
185 if (!clippedSrcRect.intersect(srcRect)) {
186 clippedSrcRect.setEmpty();
187 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400188 if (kAlpha_8_SkColorType == bmpCT) {
lsalzmana2415ac2016-10-11 14:29:12 -0700189 for (int by = 0; by < bh; ++by) {
190 for (int bx = 0; bx < bw; ++bx) {
191 int devx = bx + srcRect.fLeft;
192 int devy = by + srcRect.fTop;
193 const uint8_t* alpha = bitmap.getAddr8(bx, by);
194
195 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400196 if (checkSurfacePixels) {
Mike Klein12d4b6e2018-08-17 14:09:55 -0400197 uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400198 ? 0xFF
199 : SkGetPackedA32(get_src_color(devx, devy));
200 if (surfaceAlpha != *alpha) {
201 ERRORF(reporter,
202 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
203 bx, by, surfaceAlpha, *alpha);
lsalzmana2415ac2016-10-11 14:29:12 -0700204 return false;
205 }
206 }
207 } else if (checkBitmapPixels) {
208 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
209 if (origDstAlpha != *alpha) {
210 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
211 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
212 return false;
213 }
214 }
215 }
216 }
217 return true;
218 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000219 for (int by = 0; by < bh; ++by) {
220 for (int bx = 0; bx < bw; ++bx) {
221 int devx = bx + srcRect.fLeft;
222 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000223
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000224 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000225
226 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400227 if (checkSurfacePixels) {
228 SkPMColor surfacePMColor = get_src_color(devx, devy);
Mike Klein12d4b6e2018-08-17 14:09:55 -0400229 if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
230 surfacePMColor &= 0xFF000000;
231 }
232 if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400233 surfacePMColor |= 0xFF000000;
234 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000235 bool didPremul;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400236 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
237 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
238 ERRORF(reporter,
239 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
240 "Readback was unpremul: %d",
241 bx, by, surfacePMColor, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000242 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000243 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000244 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000245 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700246 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
247 if (origDstPixel != *pixel) {
248 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
249 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000250 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000251 }
252 }
253 }
254 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000255 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000256}
257
Brian Salomon5918e832019-10-23 13:34:36 -0400258enum class TightRowBytes : bool { kNo, kYes };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000259
Brian Salomon5918e832019-10-23 13:34:36 -0400260static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB,
261 SkColorType ct, SkAlphaType at) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400262 SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000263 size_t rowBytes = 0;
Brian Salomon5918e832019-10-23 13:34:36 -0400264 if (tightRB == TightRowBytes::kNo) {
265 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000266 }
Mike Reed12e946b2017-04-17 10:53:29 -0400267 bitmap->allocPixels(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000268}
269
kkinnunen15302832015-12-01 04:35:26 -0800270static const struct {
271 SkColorType fColorType;
272 SkAlphaType fAlphaType;
273} gReadPixelsConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400274 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
275 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
276 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
277 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
278 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
279 {kAlpha_8_SkColorType, kPremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800280};
281const SkIRect gReadPixelsTestRects[] = {
282 // entire thing
283 DEV_RECT,
284 // larger on all sides
285 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
286 // fully contained
287 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
288 // outside top left
289 SkIRect::MakeLTRB(-10, -10, -1, -1),
290 // touching top left corner
291 SkIRect::MakeLTRB(-10, -10, 0, 0),
292 // overlapping top left corner
293 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
294 // overlapping top left and top right corners
295 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
296 // touching entire top edge
297 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
298 // overlapping top right corner
299 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
300 // contained in x, overlapping top edge
301 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
302 // outside top right corner
303 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
304 // touching top right corner
305 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
306 // overlapping top left and bottom left corners
307 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
308 // touching entire left edge
309 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
310 // overlapping bottom left corner
311 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
312 // contained in y, overlapping left edge
313 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
314 // outside bottom left corner
315 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
316 // touching bottom left corner
317 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
318 // overlapping bottom left and bottom right corners
319 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
320 // touching entire left edge
321 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
322 // overlapping bottom right corner
323 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
324 // overlapping top right and bottom right corners
325 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
326};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000327
Brian Salomon1d435302019-07-01 13:05:28 -0400328bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
329 const SkImageInfo& srcInfo) {
330 return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400331}
332
reede8f30622016-03-23 18:59:25 -0700333static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
Brian Salomon5918e832019-10-23 13:34:36 -0400334 const SkImageInfo& surfaceInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800335 SkCanvas* canvas = surface->getCanvas();
336 fill_src_canvas(canvas);
337 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
338 const SkIRect& srcRect = gReadPixelsTestRects[rect];
Brian Salomon5918e832019-10-23 13:34:36 -0400339 for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
kkinnunen15302832015-12-01 04:35:26 -0800340 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
341 SkBitmap bmp;
Brian Salomon5918e832019-10-23 13:34:36 -0400342 init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
343 gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700344
kkinnunen15302832015-12-01 04:35:26 -0800345 // if the bitmap has pixels allocated before the readPixels,
346 // note that and fill them with pattern
347 bool startsWithPixels = !bmp.isNull();
348 if (startsWithPixels) {
349 fill_dst_bmp_with_init_data(&bmp);
350 }
351 uint32_t idBefore = surface->generationID();
Mike Reedf1942192017-07-21 14:24:29 -0400352 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
kkinnunen15302832015-12-01 04:35:26 -0800353 uint32_t idAfter = surface->generationID();
354
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400355 // we expect to succeed when the read isn't fully clipped out and the infos are
356 // compatible.
Brian Salomon1d435302019-07-01 13:05:28 -0400357 bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800358 // determine whether we expected the read to succeed.
Brian Salomon1d435302019-07-01 13:05:28 -0400359 REPORTER_ASSERT(reporter, expectSuccess == success,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400360 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
361 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
362 bmp.info().colorType(), bmp.info().alphaType());
kkinnunen15302832015-12-01 04:35:26 -0800363 // read pixels should never change the gen id
364 REPORTER_ASSERT(reporter, idBefore == idAfter);
365
366 if (success || startsWithPixels) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400367 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400368 startsWithPixels, surfaceInfo);
kkinnunen15302832015-12-01 04:35:26 -0800369 } else {
370 // if we had no pixels beforehand and the readPixels
371 // failed then our bitmap should still not have pixels
372 REPORTER_ASSERT(reporter, bmp.isNull());
373 }
374 }
kkinnunen15302832015-12-01 04:35:26 -0800375 }
376 }
377}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400378
kkinnunen15302832015-12-01 04:35:26 -0800379DEF_TEST(ReadPixels, reporter) {
380 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700381 auto surface(SkSurface::MakeRaster(info));
Brian Salomon5918e832019-10-23 13:34:36 -0400382 test_readpixels(reporter, surface, info);
kkinnunen15302832015-12-01 04:35:26 -0800383}
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000384
Robert Phillipse78b7252017-04-06 07:59:41 -0400385static void test_readpixels_texture(skiatest::Reporter* reporter,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400386 std::unique_ptr<GrSurfaceContext> sContext,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400387 const SkImageInfo& surfaceInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800388 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
389 const SkIRect& srcRect = gReadPixelsTestRects[rect];
Brian Salomon5918e832019-10-23 13:34:36 -0400390 for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
kkinnunen15302832015-12-01 04:35:26 -0800391 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
392 SkBitmap bmp;
Brian Salomon5918e832019-10-23 13:34:36 -0400393 init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
394 gReadPixelsConfigs[c].fAlphaType);
kkinnunen15302832015-12-01 04:35:26 -0800395
396 // if the bitmap has pixels allocated before the readPixels,
397 // note that and fill them with pattern
398 bool startsWithPixels = !bmp.isNull();
399 // Try doing the read directly from a non-renderable texture
400 if (startsWithPixels) {
401 fill_dst_bmp_with_init_data(&bmp);
Brian Salomon5918e832019-10-23 13:34:36 -0400402 bool success = sContext->readPixels(bmp.info(), bmp.getPixels(), bmp.rowBytes(),
403 {srcRect.fLeft, srcRect.fTop});
Brian Salomon1d435302019-07-01 13:05:28 -0400404 auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400405 REPORTER_ASSERT(
Brian Salomon1d435302019-07-01 13:05:28 -0400406 reporter, expectSuccess == success,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400407 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
408 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
409 bmp.info().colorType(), bmp.info().alphaType());
410 if (success) {
411 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true,
Mike Klein12d4b6e2018-08-17 14:09:55 -0400412 surfaceInfo);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400413 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000414 }
415 }
416 }
417 }
418}
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400419
egdaniel88e8aef2016-06-27 14:34:55 -0700420DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400421 GrContext* context = ctxInfo.grContext();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400422 SkBitmap bmp = make_src_bitmap();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400423
kkinnunen15302832015-12-01 04:35:26 -0800424 // On the GPU we will also try reading back from a non-renderable texture.
Brian Salomon8b1fb742016-11-03 15:21:06 -0400425 for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400426 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
Brian Salomon58389b92018-03-07 13:01:25 -0500427 sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData(
Brian Salomon4eda7102019-10-21 15:04:52 -0400428 context, renderable, origin, bmp.info(), bmp.getPixels(), bmp.rowBytes());
Greg Daniel3912a4b2020-01-14 09:56:04 -0500429 GrColorType grColorType = SkColorTypeToGrColorType(bmp.colorType());
430 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
431 grColorType);
432 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
433 auto sContext = GrSurfaceContext::Make(context, std::move(view),
434 grColorType, kPremul_SkAlphaType, nullptr);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400435 auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
436 test_readpixels_texture(reporter, std::move(sContext), info);
Brian Salomon8b1fb742016-11-03 15:21:06 -0400437 }
kkinnunen15302832015-12-01 04:35:26 -0800438 }
439}
Matt Sarett8572d852017-02-14 11:21:02 -0500440
441///////////////////////////////////////////////////////////////////////////////////////////////////
442
443static const uint32_t kNumPixels = 5;
444
445// The five reference pixels are: red, green, blue, white, black.
446// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
447// plus a tail pixel.
448static const uint32_t rgba[kNumPixels] = {
449 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
450};
451static const uint32_t bgra[kNumPixels] = {
452 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
453};
454static const uint16_t rgb565[kNumPixels] = {
455 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
456};
457
458static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
459
460static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
461static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
462static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
463static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
464static const uint64_t f16[kNumPixels] = {
465 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
466};
467
Matt Sarett8572d852017-02-14 11:21:02 -0500468static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
469static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
470
471static const void* five_reference_pixels(SkColorType colorType) {
472 switch (colorType) {
473 case kUnknown_SkColorType:
474 return nullptr;
475 case kAlpha_8_SkColorType:
476 return alpha8;
477 case kRGB_565_SkColorType:
478 return rgb565;
479 case kARGB_4444_SkColorType:
480 return rgba4444;
481 case kRGBA_8888_SkColorType:
482 return rgba;
483 case kBGRA_8888_SkColorType:
484 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500485 case kGray_8_SkColorType:
486 return gray8;
487 case kRGBA_F16_SkColorType:
488 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400489 default:
Leon Scroggins IIId6832d02018-09-04 11:10:04 -0400490 return nullptr;
Matt Sarett8572d852017-02-14 11:21:02 -0500491 }
492
493 SkASSERT(false);
494 return nullptr;
495}
496
497static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
498 const SkImageInfo& srcInfo) {
Brian Osmane1adc3a2018-06-04 09:21:17 -0400499 if (!SkImageInfoIsValid(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500500 return;
501 }
502
Matt Sarett8572d852017-02-14 11:21:02 -0500503 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400504 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500505 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
506 REPORTER_ASSERT(r, src);
507
508 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
509 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400510 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500511 bool success = src->readPixels(dstPixmap, 0, 0);
512 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
513
514 if (success) {
515 if (kGray_8_SkColorType == srcInfo.colorType() &&
Mike Klein12d4b6e2018-08-17 14:09:55 -0400516 kGray_8_SkColorType != dstInfo.colorType()) {
517 // TODO: test (r,g,b) == (gray,gray,gray)?
518 return;
519 }
520
521 if (kGray_8_SkColorType == dstInfo.colorType() &&
522 kGray_8_SkColorType != srcInfo.colorType()) {
523 // TODO: test gray = luminance?
524 return;
525 }
526
527 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
528 kAlpha_8_SkColorType != dstInfo.colorType()) {
529 // TODO: test output = black with this alpha?
Matt Sarett8572d852017-02-14 11:21:02 -0500530 return;
531 }
532
533 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
534 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
Matt Sarett8572d852017-02-14 11:21:02 -0500535 }
536}
537
538DEF_TEST(ReadPixels_ValidConversion, reporter) {
539 const SkColorType kColorTypes[] = {
540 kUnknown_SkColorType,
541 kAlpha_8_SkColorType,
542 kRGB_565_SkColorType,
543 kARGB_4444_SkColorType,
544 kRGBA_8888_SkColorType,
545 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500546 kGray_8_SkColorType,
547 kRGBA_F16_SkColorType,
548 };
549
550 const SkAlphaType kAlphaTypes[] = {
551 kUnknown_SkAlphaType,
552 kOpaque_SkAlphaType,
553 kPremul_SkAlphaType,
554 kUnpremul_SkAlphaType,
555 };
556
557 const sk_sp<SkColorSpace> kColorSpaces[] = {
558 nullptr,
559 SkColorSpace::MakeSRGB(),
560 };
561
562 for (SkColorType dstCT : kColorTypes) {
563 for (SkAlphaType dstAT: kAlphaTypes) {
564 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
565 for (SkColorType srcCT : kColorTypes) {
566 for (SkAlphaType srcAT: kAlphaTypes) {
567 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500568 test_conversion(reporter,
569 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
570 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
571 }
572 }
573 }
574 }
575 }
576 }
577}
Brian Salomonab32f652019-05-10 14:24:50 -0400578
Brian Salomon5918e832019-10-23 13:34:36 -0400579static constexpr int min_rgb_channel_bits(SkColorType ct) {
Brian Salomoncd734f62019-05-10 16:32:54 -0400580 switch (ct) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400581 case kUnknown_SkColorType: return 0;
Brian Salomon5918e832019-10-23 13:34:36 -0400582 case kAlpha_8_SkColorType: return 0;
583 case kA16_unorm_SkColorType: return 0;
584 case kA16_float_SkColorType: return 0;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400585 case kRGB_565_SkColorType: return 5;
586 case kARGB_4444_SkColorType: return 4;
587 case kR8G8_unorm_SkColorType: return 8;
588 case kR16G16_unorm_SkColorType: return 16;
589 case kR16G16_float_SkColorType: return 16;
590 case kRGBA_8888_SkColorType: return 8;
591 case kRGB_888x_SkColorType: return 8;
592 case kBGRA_8888_SkColorType: return 8;
593 case kRGBA_1010102_SkColorType: return 10;
594 case kRGB_101010x_SkColorType: return 10;
595 case kGray_8_SkColorType: return 8; // counting gray as "rgb"
596 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
597 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
598 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
599 case kR16G16B16A16_unorm_SkColorType: return 16;
Brian Salomoncd734f62019-05-10 16:32:54 -0400600 }
Brian Salomon5918e832019-10-23 13:34:36 -0400601 SkUNREACHABLE;
602}
603
604static constexpr int alpha_channel_bits(SkColorType ct) {
605 switch (ct) {
606 case kUnknown_SkColorType: return 0;
607 case kAlpha_8_SkColorType: return 8;
608 case kA16_unorm_SkColorType: return 16;
609 case kA16_float_SkColorType: return 16;
610 case kRGB_565_SkColorType: return 0;
611 case kARGB_4444_SkColorType: return 4;
612 case kR8G8_unorm_SkColorType: return 0;
613 case kR16G16_unorm_SkColorType: return 0;
614 case kR16G16_float_SkColorType: return 0;
615 case kRGBA_8888_SkColorType: return 8;
616 case kRGB_888x_SkColorType: return 0;
617 case kBGRA_8888_SkColorType: return 8;
618 case kRGBA_1010102_SkColorType: return 2;
619 case kRGB_101010x_SkColorType: return 0;
620 case kGray_8_SkColorType: return 0;
621 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
622 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
623 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
624 case kR16G16B16A16_unorm_SkColorType: return 16;
625 }
626 SkUNREACHABLE;
627}
628
629namespace {
630
631struct GpuReadPixelTestRules {
632 // Test unpremul sources? We could omit this and detect that creating the source of the read
633 // failed but having it lets us skip generating reference color data.
634 bool fAllowUnpremulSrc = true;
635 // Expect read function to succeed for kUnpremul?
636 bool fAllowUnpremulRead = true;
637 // Are reads that are overlapping but not contained by the src bounds expected to succeed?
638 bool fUncontainedRectSucceeds = true;
639};
640
641// Makes a src populated with the pixmap. The src should get its image info (or equivalent) from
642// the pixmap.
643template <typename T> using GpuSrcFactory = T(SkPixmap&);
644
645// Does a read from the T into the pixmap.
646template <typename T> using GpuReadSrcFn = bool(const T&, const SkIVector& offset, const SkPixmap&);
647
648} // anonymous namespace
649
650template <typename T>
651static void gpu_read_pixels_test_driver(skiatest::Reporter* reporter,
652 const GpuReadPixelTestRules& rules,
653 const std::function<GpuSrcFactory<T>>& srcFactory,
654 const std::function<GpuReadSrcFn<T>>& read) {
655 // Separate this out just to give it some line width to breathe. Note 'srcPixels' should have
656 // the same image info as src. We will do a converting readPixels() on it to get the data
657 // to compare with the results of 'read'.
658 auto runTest = [&](const T& src, const SkPixmap& srcPixels, const SkImageInfo& readInfo,
659 const SkIVector& offset) {
660 const bool csConversion =
661 !SkColorSpace::Equals(readInfo.colorSpace(), srcPixels.info().colorSpace());
662 const auto readCT = readInfo.colorType();
663 const auto readAT = readInfo.alphaType();
664 const auto srcCT = srcPixels.info().colorType();
665 const auto srcAT = srcPixels.info().alphaType();
666 const auto rect = SkIRect::MakeWH(readInfo.width(), readInfo.height()).makeOffset(offset);
667 const auto surfBounds = SkIRect::MakeWH(srcPixels.width(), srcPixels.height());
668 const size_t readBpp = SkColorTypeBytesPerPixel(readCT);
669
670 // Make the row bytes in the dst be loose for extra stress.
671 const size_t dstRB = readBpp * readInfo.width() + 10 * readBpp;
672 // This will make the last row tight.
673 const size_t dstSize = readInfo.computeByteSize(dstRB);
674 std::unique_ptr<char[]> dstData(new char[dstSize]);
675 SkPixmap dstPixels(readInfo, dstData.get(), dstRB);
676 // Initialize with an arbitrary value for each byte. Later we will check that only the
677 // correct part of the destination gets overwritten by 'read'.
678 static constexpr auto kInitialByte = static_cast<char>(0x1B);
679 std::fill_n(static_cast<char*>(dstPixels.writable_addr()),
680 dstPixels.computeByteSize(),
681 kInitialByte);
682
683 const bool success = read(src, offset, dstPixels);
684
685 if (!SkIRect::Intersects(rect, surfBounds)) {
686 REPORTER_ASSERT(reporter, !success);
687 } else if (readCT == kUnknown_SkColorType) {
688 REPORTER_ASSERT(reporter, !success);
689 } else if (readAT == kUnknown_SkAlphaType) {
690 REPORTER_ASSERT(reporter, !success);
691 } else if (!rules.fUncontainedRectSucceeds && !surfBounds.contains(rect)) {
692 REPORTER_ASSERT(reporter, !success);
693 } else if (!rules.fAllowUnpremulRead && readAT == kUnpremul_SkAlphaType) {
694 REPORTER_ASSERT(reporter, !success);
695 } else if (!success) {
Brian Salomonb3bd8642019-11-04 16:59:29 -0500696 // TODO: Support kRGB_101010x at all in GPU.
697 if (readCT != kRGB_101010x_SkColorType) {
Brian Salomon5918e832019-10-23 13:34:36 -0400698 ERRORF(reporter,
699 "Read failed. Src CT: %s, Src AT: %s Read CT: %s, Read AT: %s, "
700 "Rect [%d, %d, %d, %d], CS conversion: %d\n",
701 ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
702 ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
703 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion);
704 }
705 return;
706 }
707
708 bool guardOk = true;
709 auto guardCheck = [](char x) { return x == kInitialByte; };
710
711 // Considering the rect we tried to read and the surface bounds figure out which pixels in
712 // both src and dst space should actually have been read and written.
713 SkIRect srcReadRect;
714 if (success && srcReadRect.intersect(surfBounds, rect)) {
715 SkIRect dstWriteRect = srcReadRect.makeOffset(-rect.fLeft, -rect.fTop);
716
Brian Salomon85c3d682019-11-04 15:04:54 -0500717 const bool lumConversion =
718 !(SkColorTypeComponentFlags(srcCT) & kGray_SkColorTypeComponentFlag) &&
719 (SkColorTypeComponentFlags(readCT) & kGray_SkColorTypeComponentFlag);
720 // A CS or luminance conversion allows a 3 value difference and otherwise a 2 value
721 // difference. Note that sometimes read back on GPU can be lossy even when there no
722 // conversion at allbecause GPU->CPU read may go to a lower bit depth format and then be
723 // promoted back to the original type. For example, GL ES cannot read to 1010102, so we
724 // go through 8888.
Brian Salomon67904232019-11-06 10:56:00 -0500725 const float numer = (lumConversion || csConversion) ? 3.f : 2.f;
Brian Salomon5918e832019-10-23 13:34:36 -0400726 int rgbBits = std::min({min_rgb_channel_bits(readCT),
727 min_rgb_channel_bits(srcCT),
728 8});
729 float tol = numer / (1 << rgbBits);
730 float alphaTol = 0;
731 if (readAT != kOpaque_SkAlphaType && srcAT != kOpaque_SkAlphaType) {
732 const int alphaBits = std::min(alpha_channel_bits(readCT),
733 alpha_channel_bits(srcCT));
734 alphaTol = 2.f / (1 << alphaBits);
735 }
736
737 const float tols[4] = {tol, tol, tol, alphaTol};
738 auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
739 const float diffs[4]) {
740 SkASSERT(x >= 0 && y >= 0);
741 ERRORF(reporter,
742 "Src CT: %s, Src AT: %s, Read CT: %s, Read AT: %s, Rect [%d, %d, %d, %d]"
743 ", CS conversion: %d\n"
744 "Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
745 ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
746 ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
747 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion, x, y,
748 diffs[0], diffs[1], diffs[2], diffs[3]);
749 });
750 SkAutoPixmapStorage ref;
751 ref.alloc(readInfo.makeWH(dstWriteRect.width(), dstWriteRect.height()));
752 srcPixels.readPixels(ref, srcReadRect.x(), srcReadRect.y());
753 // This is the part of dstPixels that should have been updated.
754 SkPixmap actual;
755 SkAssertResult(dstPixels.extractSubset(&actual, dstWriteRect));
Brian Salomon28a8f282019-10-24 20:07:39 -0400756 ComparePixels(ref, actual, tols, error);
Brian Salomon5918e832019-10-23 13:34:36 -0400757
758 const auto* v = dstData.get();
759 const auto* end = dstData.get() + dstSize;
760 guardOk = std::all_of(v, v + dstWriteRect.top() * dstPixels.rowBytes(), guardCheck);
761 v += dstWriteRect.top() * dstPixels.rowBytes();
762 for (int y = dstWriteRect.top(); y < dstWriteRect.bottom(); ++y) {
763 guardOk |= std::all_of(v, v + dstWriteRect.left() * readBpp, guardCheck);
764 auto pad = v + dstWriteRect.right() * readBpp;
765 auto rowEnd = std::min(end, v + dstPixels.rowBytes());
766 // min protects against reading past the end of the tight last row.
767 guardOk |= std::all_of(pad, rowEnd, guardCheck);
768 v = rowEnd;
769 }
770 guardOk |= std::all_of(v, end, guardCheck);
771 } else {
772 guardOk = std::all_of(dstData.get(), dstData.get() + dstSize, guardCheck);
773 }
774 if (!guardOk) {
775 ERRORF(reporter,
776 "Result pixels modified result outside read rect [%d, %d, %d, %d]. "
777 "Src CT: %s, Read CT: %s, CS conversion: %d",
778 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
779 ToolUtils::colortype_name(srcCT), ToolUtils::colortype_name(readCT),
780 csConversion);
781 }
782 };
783
784 static constexpr int kW = 16;
785 static constexpr int kH = 16;
786
787 // Makes the reference data that is used to populate the src. Always F32 regardless of srcCT.
788 auto make_ref_f32_data = [](SkAlphaType srcAT, SkColorType srcCT) {
789 // Make src data in F32 with srcAT. We will convert it to each color type we test to
790 // initialize the src.
791 const auto refInfo =
792 SkImageInfo::Make(kW, kH, kRGBA_F32_SkColorType, srcAT, SkColorSpace::MakeSRGB());
793 auto refSurf = SkSurface::MakeRaster(refInfo);
794 static constexpr SkPoint kPts1[] = {{0, 0}, {kW, kH}};
795 static constexpr SkColor kColors1[] = {SK_ColorGREEN, SK_ColorRED};
796 SkPaint paint;
797 paint.setShader(
798 SkGradientShader::MakeLinear(kPts1, kColors1, nullptr, 2, SkTileMode::kClamp));
799 refSurf->getCanvas()->drawPaint(paint);
800 static constexpr SkPoint kPts2[] = {{kW, 0}, {0, kH}};
801 static constexpr SkColor kColors2[] = {SK_ColorBLUE, SK_ColorBLACK};
802 paint.setShader(
803 SkGradientShader::MakeLinear(kPts2, kColors2, nullptr, 2, SkTileMode::kClamp));
804 paint.setBlendMode(SkBlendMode::kPlus);
805 refSurf->getCanvas()->drawPaint(paint);
806 // Keep everything opaque if the src alpha type is opaque. Also, there is an issue with
807 // 1010102 (the only color type where the number of alpha bits is non-zero and not the
808 // same as r, g, and b). Because of the different precisions the draw below can create
809 // data that isn't strictly premul (e.g. alpha is 1/3 but green is .4). SW will clamp
810 // r, g, b to a if the dst is premul and a different color type. GPU doesn't do this.
811 // We could but 1010102 premul is kind of dubious anyway. So for now just keep the data
812 // opaque.
813 if (srcAT != kOpaque_SkAlphaType &&
814 (srcAT == kPremul_SkAlphaType && srcCT != kRGBA_1010102_SkColorType)) {
815 static constexpr SkColor kColors3[] = {SK_ColorWHITE,
816 SK_ColorWHITE,
817 0x60FFFFFF,
818 SK_ColorWHITE,
819 SK_ColorWHITE};
820 static constexpr SkScalar kPos3[] = {0.f, 0.15f, 0.5f, 0.85f, 1.f};
821 paint.setShader(SkGradientShader::MakeRadial({kW / 2.f, kH / 2.f}, (kW + kH) / 10.f,
822 kColors3, kPos3, 5, SkTileMode::kMirror));
823 paint.setBlendMode(SkBlendMode::kDstIn);
824 refSurf->getCanvas()->drawPaint(paint);
825 }
826
827 const auto srcInfo = SkImageInfo::Make(kW, kH, srcCT, srcAT, SkColorSpace::MakeSRGB());
828 SkAutoPixmapStorage srcPixels;
829 srcPixels.alloc(srcInfo);
830 refSurf->readPixels(srcPixels, 0, 0);
831 return std::move(srcPixels);
832 };
833
834 for (int sat = 0; sat < kLastEnum_SkAlphaType; ++sat) {
835 const auto srcAT = static_cast<SkAlphaType>(sat);
836 if (srcAT == kUnknown_SkAlphaType ||
837 (srcAT == kUnpremul_SkAlphaType && !rules.fAllowUnpremulSrc)) {
838 continue;
839 }
840 for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
841 const auto srcCT = static_cast<SkColorType>(sct);
842 // Note that we only currently use srcCT for a 1010102 workaround. If we remove this we
843 // can also but the ref data setup above the srcCT loop.
844 SkAutoPixmapStorage srcPixels = make_ref_f32_data(srcAT, srcCT);
845 auto src = srcFactory(srcPixels);
846 if (!src) {
847 continue;
848 }
849 for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
850 const auto readCT = static_cast<SkColorType>(rct);
851 for (const sk_sp<SkColorSpace>& readCS :
852 {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) {
853 for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
854 const auto readAT = static_cast<SkAlphaType>(at);
855 if (srcAT != kOpaque_SkAlphaType && readAT == kOpaque_SkAlphaType) {
856 // This doesn't make sense.
857 continue;
858 }
859 // Test full size, partial, empty, and too wide rects.
860 for (const auto& rect : {
861 // entire thing
862 SkIRect::MakeWH(kW, kH),
863 // larger on all sides
864 SkIRect::MakeLTRB(-10, -10, kW + 10, kH + 10),
865 // fully contained
866 SkIRect::MakeLTRB(kW / 4, kH / 4, 3 * kW / 4, 3 * kH / 4),
867 // outside top left
868 SkIRect::MakeLTRB(-10, -10, -1, -1),
869 // touching top left corner
870 SkIRect::MakeLTRB(-10, -10, 0, 0),
871 // overlapping top left corner
872 SkIRect::MakeLTRB(-10, -10, kW / 4, kH / 4),
873 // overlapping top left and top right corners
874 SkIRect::MakeLTRB(-10, -10, kW + 10, kH / 4),
875 // touching entire top edge
876 SkIRect::MakeLTRB(-10, -10, kW + 10, 0),
877 // overlapping top right corner
878 SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH / 4),
879 // contained in x, overlapping top edge
880 SkIRect::MakeLTRB(kW / 4, -10, 3 * kW / 4, kH / 4),
881 // outside top right corner
882 SkIRect::MakeLTRB(kW + 1, -10, kW + 10, -1),
883 // touching top right corner
884 SkIRect::MakeLTRB(kW, -10, kW + 10, 0),
885 // overlapping top left and bottom left corners
886 SkIRect::MakeLTRB(-10, -10, kW / 4, kH + 10),
887 // touching entire left edge
888 SkIRect::MakeLTRB(-10, -10, 0, kH + 10),
889 // overlapping bottom left corner
890 SkIRect::MakeLTRB(-10, 3 * kH / 4, kW / 4, kH + 10),
891 // contained in y, overlapping left edge
892 SkIRect::MakeLTRB(-10, kH / 4, kW / 4, 3 * kH / 4),
893 // outside bottom left corner
894 SkIRect::MakeLTRB(-10, kH + 1, -1, kH + 10),
895 // touching bottom left corner
896 SkIRect::MakeLTRB(-10, kH, 0, kH + 10),
897 // overlapping bottom left and bottom right corners
898 SkIRect::MakeLTRB(-10, 3 * kH / 4, kW + 10, kH + 10),
899 // touching entire left edge
900 SkIRect::MakeLTRB(0, kH, kW, kH + 10),
901 // overlapping bottom right corner
902 SkIRect::MakeLTRB(3 * kW / 4, 3 * kH / 4, kW + 10, kH + 10),
903 // overlapping top right and bottom right corners
904 SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH + 10),
905 }) {
906 const auto readInfo = SkImageInfo::Make(rect.width(), rect.height(),
907 readCT, readAT, readCS);
908 const SkIVector offset = rect.topLeft();
909 runTest(src, srcPixels, readInfo, offset);
910 }
911 }
912 }
913 }
914 }
915 }
Brian Salomoncd734f62019-05-10 16:32:54 -0400916}
917
Brian Salomon9241a6d2019-10-03 13:26:54 -0400918namespace {
919struct AsyncContext {
920 bool fCalled = false;
921 std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
922};
923} // anonymous namespace
924
925// Making this a lambda in the test functions caused:
926// "error: cannot compile this forwarded non-trivially copyable parameter yet"
927// on x86/Win/Clang bot, referring to 'result'.
928static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
929 auto context = static_cast<AsyncContext*>(c);
930 context->fResult = std::move(result);
931 context->fCalled = true;
932};
933
Brian Salomonab32f652019-05-10 14:24:50 -0400934DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) {
Brian Salomon5918e832019-10-23 13:34:36 -0400935 using Surface = sk_sp<SkSurface>;
936 auto reader = std::function<GpuReadSrcFn<Surface>>([](const Surface& surface,
937 const SkIVector& offset,
938 const SkPixmap& pixels) {
939 AsyncContext context;
940 auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
941
942 // Rescale quality and linearity don't matter since we're doing a non-scaling readback.
943 surface->asyncRescaleAndReadPixels(pixels.info(), rect, SkSurface::RescaleGamma::kSrc,
944 kNone_SkFilterQuality, async_callback, &context);
945 while (!context.fCalled) {
946 surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
Brian Salomonab32f652019-05-10 14:24:50 -0400947 }
Brian Salomon5918e832019-10-23 13:34:36 -0400948 if (!context.fResult) {
949 return false;
950 }
951 SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), context.fResult->data(0),
952 context.fResult->rowBytes(0), pixels.info().minRowBytes(), pixels.height());
953 return true;
954 });
955 GpuReadPixelTestRules rules;
956 rules.fAllowUnpremulSrc = false;
957 rules.fAllowUnpremulRead = false;
958 rules.fUncontainedRectSucceeds = false;
Brian Salomon5dd77462019-09-26 16:57:09 -0400959
Brian Salomon5918e832019-10-23 13:34:36 -0400960 for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
961 auto factory = std::function<GpuSrcFactory<Surface>>(
962 [context = ctxInfo.grContext(), origin](const SkPixmap& src) {
963 if (src.colorType() == kRGB_888x_SkColorType) {
964 return Surface();
Ravi Mistrycb550102019-10-03 09:06:25 +0000965 }
Brian Salomon5918e832019-10-23 13:34:36 -0400966 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(),
967 0, origin, nullptr);
968 if (surf) {
969 surf->writePixels(src, 0, 0);
970 }
971 return surf;
972 });
973 gpu_read_pixels_test_driver(reporter, rules, factory, reader);
974 }
975}
976
Brian Salomon5918e832019-10-23 13:34:36 -0400977DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
978 using Surface = sk_sp<SkSurface>;
979 auto reader = std::function<GpuReadSrcFn<Surface>>(
980 [](const Surface& surface, const SkIVector& offset, const SkPixmap& pixels) {
981 return surface->readPixels(pixels, offset.fX, offset.fY);
982 });
983 GpuReadPixelTestRules rules;
984 rules.fAllowUnpremulSrc = false;
985 rules.fAllowUnpremulRead = true;
986 rules.fUncontainedRectSucceeds = true;
987
988 for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
989 auto factory = std::function<GpuSrcFactory<Surface>>(
990 [context = ctxInfo.grContext(), origin](const SkPixmap& src) {
991 if (src.colorType() == kRGB_888x_SkColorType) {
992 return Surface();
993 }
994 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(),
995 0, origin, nullptr);
996 if (surf) {
997 surf->writePixels(src, 0, 0);
998 }
999 return surf;
1000 });
1001 gpu_read_pixels_test_driver(reporter, rules, factory, reader);
Brian Salomon6fc04f82019-10-02 19:11:55 -04001002 }
1003}
Brian Salomon9241a6d2019-10-03 13:26:54 -04001004
1005DEF_GPUTEST(AsyncReadPixelsContextShutdown, reporter, options) {
1006 const auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1007 SkColorSpace::MakeSRGB());
1008 enum class ShutdownSequence {
1009 kFreeResult_DestroyContext,
1010 kDestroyContext_FreeResult,
1011 kFreeResult_ReleaseAndAbandon_DestroyContext,
1012 kFreeResult_Abandon_DestroyContext,
1013 kReleaseAndAbandon_FreeResult_DestroyContext,
1014 kAbandon_FreeResult_DestroyContext,
1015 kReleaseAndAbandon_DestroyContext_FreeResult,
1016 kAbandon_DestroyContext_FreeResult,
1017 };
1018 for (int t = 0; t < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++t) {
1019 auto type = static_cast<sk_gpu_test::GrContextFactory::ContextType>(t);
1020 for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext,
1021 ShutdownSequence::kDestroyContext_FreeResult,
1022 ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext,
1023 ShutdownSequence::kFreeResult_Abandon_DestroyContext,
1024 ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext,
1025 ShutdownSequence::kAbandon_FreeResult_DestroyContext,
1026 ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult,
1027 ShutdownSequence::kAbandon_DestroyContext_FreeResult}) {
1028 // Vulkan context abandoning without resource release has issues outside of the scope of
1029 // this test.
1030 if (type == sk_gpu_test::GrContextFactory::kVulkan_ContextType &&
1031 (sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext ||
1032 sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult ||
1033 sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext)) {
1034 continue;
1035 }
1036 for (bool yuv : {false, true}) {
1037 sk_gpu_test::GrContextFactory factory(options);
1038 auto context = factory.get(type);
1039 if (!context) {
1040 continue;
1041 }
Brian Salomonfb28c6f2020-01-10 13:04:45 -05001042 // This test is only meaningful for contexts that support transfer buffers for
1043 // reads.
1044 if (!context->priv().caps()->transferFromSurfaceToBufferSupport()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001045 continue;
1046 }
1047 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 1, nullptr);
1048 if (!surf) {
1049 continue;
1050 }
1051 AsyncContext cbContext;
1052 if (yuv) {
1053 surf->asyncRescaleAndReadPixelsYUV420(
1054 kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(), ii.bounds(),
1055 ii.dimensions(), SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality,
1056 &async_callback, &cbContext);
1057 } else {
1058 surf->asyncRescaleAndReadPixels(ii, ii.bounds(), SkSurface::RescaleGamma::kSrc,
1059 kNone_SkFilterQuality, &async_callback,
1060 &cbContext);
1061 }
1062 while (!cbContext.fCalled) {
1063 context->checkAsyncWorkCompletion();
1064 }
1065 if (!cbContext.fResult) {
1066 ERRORF(reporter, "Callback failed on %s. is YUV: %d",
1067 sk_gpu_test::GrContextFactory::ContextTypeName(type), yuv);
1068 continue;
1069 }
1070 // The real test is that we don't crash, get Vulkan validation errors, etc, during
1071 // this shutdown sequence.
1072 switch (sequence) {
1073 case ShutdownSequence::kFreeResult_DestroyContext:
1074 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
1075 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
1076 break;
1077 case ShutdownSequence::kDestroyContext_FreeResult:
1078 factory.destroyContexts();
1079 break;
1080 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
1081 factory.releaseResourcesAndAbandonContexts();
1082 break;
1083 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
1084 factory.abandonContexts();
1085 break;
1086 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
1087 factory.releaseResourcesAndAbandonContexts();
1088 factory.destroyContexts();
1089 break;
1090 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
1091 factory.abandonContexts();
1092 factory.destroyContexts();
1093 break;
1094 }
1095 cbContext.fResult.reset();
1096 switch (sequence) {
1097 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
1098 factory.releaseResourcesAndAbandonContexts();
1099 break;
1100 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
1101 factory.abandonContexts();
1102 break;
1103 case ShutdownSequence::kFreeResult_DestroyContext:
1104 case ShutdownSequence::kDestroyContext_FreeResult:
1105 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
1106 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
1107 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
1108 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
1109 break;
1110 }
1111 }
1112 }
1113 }
1114}