blob: 00e3dd9aeac36dc10f228396cce74522ed625bc6 [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());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400429 auto sContext = context->priv().makeWrappedSurfaceContext(
Brian Salomond6287472019-06-24 15:50:07 -0400430 std::move(proxy), SkColorTypeToGrColorType(bmp.colorType()),
431 kPremul_SkAlphaType);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400432 auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
433 test_readpixels_texture(reporter, std::move(sContext), info);
Brian Salomon8b1fb742016-11-03 15:21:06 -0400434 }
kkinnunen15302832015-12-01 04:35:26 -0800435 }
436}
Matt Sarett8572d852017-02-14 11:21:02 -0500437
438///////////////////////////////////////////////////////////////////////////////////////////////////
439
440static const uint32_t kNumPixels = 5;
441
442// The five reference pixels are: red, green, blue, white, black.
443// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
444// plus a tail pixel.
445static const uint32_t rgba[kNumPixels] = {
446 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
447};
448static const uint32_t bgra[kNumPixels] = {
449 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
450};
451static const uint16_t rgb565[kNumPixels] = {
452 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
453};
454
455static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
456
457static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
458static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
459static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
460static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
461static const uint64_t f16[kNumPixels] = {
462 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
463};
464
Matt Sarett8572d852017-02-14 11:21:02 -0500465static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
466static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
467
468static const void* five_reference_pixels(SkColorType colorType) {
469 switch (colorType) {
470 case kUnknown_SkColorType:
471 return nullptr;
472 case kAlpha_8_SkColorType:
473 return alpha8;
474 case kRGB_565_SkColorType:
475 return rgb565;
476 case kARGB_4444_SkColorType:
477 return rgba4444;
478 case kRGBA_8888_SkColorType:
479 return rgba;
480 case kBGRA_8888_SkColorType:
481 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500482 case kGray_8_SkColorType:
483 return gray8;
484 case kRGBA_F16_SkColorType:
485 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400486 default:
Leon Scroggins IIId6832d02018-09-04 11:10:04 -0400487 return nullptr;
Matt Sarett8572d852017-02-14 11:21:02 -0500488 }
489
490 SkASSERT(false);
491 return nullptr;
492}
493
494static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
495 const SkImageInfo& srcInfo) {
Brian Osmane1adc3a2018-06-04 09:21:17 -0400496 if (!SkImageInfoIsValid(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500497 return;
498 }
499
Matt Sarett8572d852017-02-14 11:21:02 -0500500 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400501 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500502 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
503 REPORTER_ASSERT(r, src);
504
505 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
506 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400507 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500508 bool success = src->readPixels(dstPixmap, 0, 0);
509 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
510
511 if (success) {
512 if (kGray_8_SkColorType == srcInfo.colorType() &&
Mike Klein12d4b6e2018-08-17 14:09:55 -0400513 kGray_8_SkColorType != dstInfo.colorType()) {
514 // TODO: test (r,g,b) == (gray,gray,gray)?
515 return;
516 }
517
518 if (kGray_8_SkColorType == dstInfo.colorType() &&
519 kGray_8_SkColorType != srcInfo.colorType()) {
520 // TODO: test gray = luminance?
521 return;
522 }
523
524 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
525 kAlpha_8_SkColorType != dstInfo.colorType()) {
526 // TODO: test output = black with this alpha?
Matt Sarett8572d852017-02-14 11:21:02 -0500527 return;
528 }
529
530 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
531 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
Matt Sarett8572d852017-02-14 11:21:02 -0500532 }
533}
534
535DEF_TEST(ReadPixels_ValidConversion, reporter) {
536 const SkColorType kColorTypes[] = {
537 kUnknown_SkColorType,
538 kAlpha_8_SkColorType,
539 kRGB_565_SkColorType,
540 kARGB_4444_SkColorType,
541 kRGBA_8888_SkColorType,
542 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500543 kGray_8_SkColorType,
544 kRGBA_F16_SkColorType,
545 };
546
547 const SkAlphaType kAlphaTypes[] = {
548 kUnknown_SkAlphaType,
549 kOpaque_SkAlphaType,
550 kPremul_SkAlphaType,
551 kUnpremul_SkAlphaType,
552 };
553
554 const sk_sp<SkColorSpace> kColorSpaces[] = {
555 nullptr,
556 SkColorSpace::MakeSRGB(),
557 };
558
559 for (SkColorType dstCT : kColorTypes) {
560 for (SkAlphaType dstAT: kAlphaTypes) {
561 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
562 for (SkColorType srcCT : kColorTypes) {
563 for (SkAlphaType srcAT: kAlphaTypes) {
564 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
Matt Sarett8572d852017-02-14 11:21:02 -0500565 test_conversion(reporter,
566 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
567 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
568 }
569 }
570 }
571 }
572 }
573 }
574}
Brian Salomonab32f652019-05-10 14:24:50 -0400575
Brian Salomon5918e832019-10-23 13:34:36 -0400576static constexpr int min_rgb_channel_bits(SkColorType ct) {
Brian Salomoncd734f62019-05-10 16:32:54 -0400577 switch (ct) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400578 case kUnknown_SkColorType: return 0;
Brian Salomon5918e832019-10-23 13:34:36 -0400579 case kAlpha_8_SkColorType: return 0;
580 case kA16_unorm_SkColorType: return 0;
581 case kA16_float_SkColorType: return 0;
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400582 case kRGB_565_SkColorType: return 5;
583 case kARGB_4444_SkColorType: return 4;
584 case kR8G8_unorm_SkColorType: return 8;
585 case kR16G16_unorm_SkColorType: return 16;
586 case kR16G16_float_SkColorType: return 16;
587 case kRGBA_8888_SkColorType: return 8;
588 case kRGB_888x_SkColorType: return 8;
589 case kBGRA_8888_SkColorType: return 8;
590 case kRGBA_1010102_SkColorType: return 10;
591 case kRGB_101010x_SkColorType: return 10;
592 case kGray_8_SkColorType: return 8; // counting gray as "rgb"
593 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
594 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
595 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
596 case kR16G16B16A16_unorm_SkColorType: return 16;
Brian Salomoncd734f62019-05-10 16:32:54 -0400597 }
Brian Salomon5918e832019-10-23 13:34:36 -0400598 SkUNREACHABLE;
599}
600
601static constexpr int alpha_channel_bits(SkColorType ct) {
602 switch (ct) {
603 case kUnknown_SkColorType: return 0;
604 case kAlpha_8_SkColorType: return 8;
605 case kA16_unorm_SkColorType: return 16;
606 case kA16_float_SkColorType: return 16;
607 case kRGB_565_SkColorType: return 0;
608 case kARGB_4444_SkColorType: return 4;
609 case kR8G8_unorm_SkColorType: return 0;
610 case kR16G16_unorm_SkColorType: return 0;
611 case kR16G16_float_SkColorType: return 0;
612 case kRGBA_8888_SkColorType: return 8;
613 case kRGB_888x_SkColorType: return 0;
614 case kBGRA_8888_SkColorType: return 8;
615 case kRGBA_1010102_SkColorType: return 2;
616 case kRGB_101010x_SkColorType: return 0;
617 case kGray_8_SkColorType: return 0;
618 case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
619 case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
620 case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
621 case kR16G16B16A16_unorm_SkColorType: return 16;
622 }
623 SkUNREACHABLE;
624}
625
626namespace {
627
628struct GpuReadPixelTestRules {
629 // Test unpremul sources? We could omit this and detect that creating the source of the read
630 // failed but having it lets us skip generating reference color data.
631 bool fAllowUnpremulSrc = true;
632 // Expect read function to succeed for kUnpremul?
633 bool fAllowUnpremulRead = true;
634 // Are reads that are overlapping but not contained by the src bounds expected to succeed?
635 bool fUncontainedRectSucceeds = true;
636};
637
638// Makes a src populated with the pixmap. The src should get its image info (or equivalent) from
639// the pixmap.
640template <typename T> using GpuSrcFactory = T(SkPixmap&);
641
642// Does a read from the T into the pixmap.
643template <typename T> using GpuReadSrcFn = bool(const T&, const SkIVector& offset, const SkPixmap&);
644
645} // anonymous namespace
646
647template <typename T>
648static void gpu_read_pixels_test_driver(skiatest::Reporter* reporter,
649 const GpuReadPixelTestRules& rules,
650 const std::function<GpuSrcFactory<T>>& srcFactory,
651 const std::function<GpuReadSrcFn<T>>& read) {
652 // Separate this out just to give it some line width to breathe. Note 'srcPixels' should have
653 // the same image info as src. We will do a converting readPixels() on it to get the data
654 // to compare with the results of 'read'.
655 auto runTest = [&](const T& src, const SkPixmap& srcPixels, const SkImageInfo& readInfo,
656 const SkIVector& offset) {
657 const bool csConversion =
658 !SkColorSpace::Equals(readInfo.colorSpace(), srcPixels.info().colorSpace());
659 const auto readCT = readInfo.colorType();
660 const auto readAT = readInfo.alphaType();
661 const auto srcCT = srcPixels.info().colorType();
662 const auto srcAT = srcPixels.info().alphaType();
663 const auto rect = SkIRect::MakeWH(readInfo.width(), readInfo.height()).makeOffset(offset);
664 const auto surfBounds = SkIRect::MakeWH(srcPixels.width(), srcPixels.height());
665 const size_t readBpp = SkColorTypeBytesPerPixel(readCT);
666
667 // Make the row bytes in the dst be loose for extra stress.
668 const size_t dstRB = readBpp * readInfo.width() + 10 * readBpp;
669 // This will make the last row tight.
670 const size_t dstSize = readInfo.computeByteSize(dstRB);
671 std::unique_ptr<char[]> dstData(new char[dstSize]);
672 SkPixmap dstPixels(readInfo, dstData.get(), dstRB);
673 // Initialize with an arbitrary value for each byte. Later we will check that only the
674 // correct part of the destination gets overwritten by 'read'.
675 static constexpr auto kInitialByte = static_cast<char>(0x1B);
676 std::fill_n(static_cast<char*>(dstPixels.writable_addr()),
677 dstPixels.computeByteSize(),
678 kInitialByte);
679
680 const bool success = read(src, offset, dstPixels);
681
682 if (!SkIRect::Intersects(rect, surfBounds)) {
683 REPORTER_ASSERT(reporter, !success);
684 } else if (readCT == kUnknown_SkColorType) {
685 REPORTER_ASSERT(reporter, !success);
686 } else if (readAT == kUnknown_SkAlphaType) {
687 REPORTER_ASSERT(reporter, !success);
688 } else if (!rules.fUncontainedRectSucceeds && !surfBounds.contains(rect)) {
689 REPORTER_ASSERT(reporter, !success);
690 } else if (!rules.fAllowUnpremulRead && readAT == kUnpremul_SkAlphaType) {
691 REPORTER_ASSERT(reporter, !success);
692 } else if (!success) {
Brian Salomonb3bd8642019-11-04 16:59:29 -0500693 // TODO: Support kRGB_101010x at all in GPU.
694 if (readCT != kRGB_101010x_SkColorType) {
Brian Salomon5918e832019-10-23 13:34:36 -0400695 ERRORF(reporter,
696 "Read failed. Src CT: %s, Src AT: %s Read CT: %s, Read AT: %s, "
697 "Rect [%d, %d, %d, %d], CS conversion: %d\n",
698 ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
699 ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
700 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion);
701 }
702 return;
703 }
704
705 bool guardOk = true;
706 auto guardCheck = [](char x) { return x == kInitialByte; };
707
708 // Considering the rect we tried to read and the surface bounds figure out which pixels in
709 // both src and dst space should actually have been read and written.
710 SkIRect srcReadRect;
711 if (success && srcReadRect.intersect(surfBounds, rect)) {
712 SkIRect dstWriteRect = srcReadRect.makeOffset(-rect.fLeft, -rect.fTop);
713
Brian Salomon85c3d682019-11-04 15:04:54 -0500714 const bool lumConversion =
715 !(SkColorTypeComponentFlags(srcCT) & kGray_SkColorTypeComponentFlag) &&
716 (SkColorTypeComponentFlags(readCT) & kGray_SkColorTypeComponentFlag);
717 // A CS or luminance conversion allows a 3 value difference and otherwise a 2 value
718 // difference. Note that sometimes read back on GPU can be lossy even when there no
719 // conversion at allbecause GPU->CPU read may go to a lower bit depth format and then be
720 // promoted back to the original type. For example, GL ES cannot read to 1010102, so we
721 // go through 8888.
Brian Salomon67904232019-11-06 10:56:00 -0500722 const float numer = (lumConversion || csConversion) ? 3.f : 2.f;
Brian Salomon5918e832019-10-23 13:34:36 -0400723 int rgbBits = std::min({min_rgb_channel_bits(readCT),
724 min_rgb_channel_bits(srcCT),
725 8});
726 float tol = numer / (1 << rgbBits);
727 float alphaTol = 0;
728 if (readAT != kOpaque_SkAlphaType && srcAT != kOpaque_SkAlphaType) {
729 const int alphaBits = std::min(alpha_channel_bits(readCT),
730 alpha_channel_bits(srcCT));
731 alphaTol = 2.f / (1 << alphaBits);
732 }
733
734 const float tols[4] = {tol, tol, tol, alphaTol};
735 auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
736 const float diffs[4]) {
737 SkASSERT(x >= 0 && y >= 0);
738 ERRORF(reporter,
739 "Src CT: %s, Src AT: %s, Read CT: %s, Read AT: %s, Rect [%d, %d, %d, %d]"
740 ", CS conversion: %d\n"
741 "Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
742 ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
743 ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
744 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion, x, y,
745 diffs[0], diffs[1], diffs[2], diffs[3]);
746 });
747 SkAutoPixmapStorage ref;
748 ref.alloc(readInfo.makeWH(dstWriteRect.width(), dstWriteRect.height()));
749 srcPixels.readPixels(ref, srcReadRect.x(), srcReadRect.y());
750 // This is the part of dstPixels that should have been updated.
751 SkPixmap actual;
752 SkAssertResult(dstPixels.extractSubset(&actual, dstWriteRect));
Brian Salomon28a8f282019-10-24 20:07:39 -0400753 ComparePixels(ref, actual, tols, error);
Brian Salomon5918e832019-10-23 13:34:36 -0400754
755 const auto* v = dstData.get();
756 const auto* end = dstData.get() + dstSize;
757 guardOk = std::all_of(v, v + dstWriteRect.top() * dstPixels.rowBytes(), guardCheck);
758 v += dstWriteRect.top() * dstPixels.rowBytes();
759 for (int y = dstWriteRect.top(); y < dstWriteRect.bottom(); ++y) {
760 guardOk |= std::all_of(v, v + dstWriteRect.left() * readBpp, guardCheck);
761 auto pad = v + dstWriteRect.right() * readBpp;
762 auto rowEnd = std::min(end, v + dstPixels.rowBytes());
763 // min protects against reading past the end of the tight last row.
764 guardOk |= std::all_of(pad, rowEnd, guardCheck);
765 v = rowEnd;
766 }
767 guardOk |= std::all_of(v, end, guardCheck);
768 } else {
769 guardOk = std::all_of(dstData.get(), dstData.get() + dstSize, guardCheck);
770 }
771 if (!guardOk) {
772 ERRORF(reporter,
773 "Result pixels modified result outside read rect [%d, %d, %d, %d]. "
774 "Src CT: %s, Read CT: %s, CS conversion: %d",
775 rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
776 ToolUtils::colortype_name(srcCT), ToolUtils::colortype_name(readCT),
777 csConversion);
778 }
779 };
780
781 static constexpr int kW = 16;
782 static constexpr int kH = 16;
783
784 // Makes the reference data that is used to populate the src. Always F32 regardless of srcCT.
785 auto make_ref_f32_data = [](SkAlphaType srcAT, SkColorType srcCT) {
786 // Make src data in F32 with srcAT. We will convert it to each color type we test to
787 // initialize the src.
788 const auto refInfo =
789 SkImageInfo::Make(kW, kH, kRGBA_F32_SkColorType, srcAT, SkColorSpace::MakeSRGB());
790 auto refSurf = SkSurface::MakeRaster(refInfo);
791 static constexpr SkPoint kPts1[] = {{0, 0}, {kW, kH}};
792 static constexpr SkColor kColors1[] = {SK_ColorGREEN, SK_ColorRED};
793 SkPaint paint;
794 paint.setShader(
795 SkGradientShader::MakeLinear(kPts1, kColors1, nullptr, 2, SkTileMode::kClamp));
796 refSurf->getCanvas()->drawPaint(paint);
797 static constexpr SkPoint kPts2[] = {{kW, 0}, {0, kH}};
798 static constexpr SkColor kColors2[] = {SK_ColorBLUE, SK_ColorBLACK};
799 paint.setShader(
800 SkGradientShader::MakeLinear(kPts2, kColors2, nullptr, 2, SkTileMode::kClamp));
801 paint.setBlendMode(SkBlendMode::kPlus);
802 refSurf->getCanvas()->drawPaint(paint);
803 // Keep everything opaque if the src alpha type is opaque. Also, there is an issue with
804 // 1010102 (the only color type where the number of alpha bits is non-zero and not the
805 // same as r, g, and b). Because of the different precisions the draw below can create
806 // data that isn't strictly premul (e.g. alpha is 1/3 but green is .4). SW will clamp
807 // r, g, b to a if the dst is premul and a different color type. GPU doesn't do this.
808 // We could but 1010102 premul is kind of dubious anyway. So for now just keep the data
809 // opaque.
810 if (srcAT != kOpaque_SkAlphaType &&
811 (srcAT == kPremul_SkAlphaType && srcCT != kRGBA_1010102_SkColorType)) {
812 static constexpr SkColor kColors3[] = {SK_ColorWHITE,
813 SK_ColorWHITE,
814 0x60FFFFFF,
815 SK_ColorWHITE,
816 SK_ColorWHITE};
817 static constexpr SkScalar kPos3[] = {0.f, 0.15f, 0.5f, 0.85f, 1.f};
818 paint.setShader(SkGradientShader::MakeRadial({kW / 2.f, kH / 2.f}, (kW + kH) / 10.f,
819 kColors3, kPos3, 5, SkTileMode::kMirror));
820 paint.setBlendMode(SkBlendMode::kDstIn);
821 refSurf->getCanvas()->drawPaint(paint);
822 }
823
824 const auto srcInfo = SkImageInfo::Make(kW, kH, srcCT, srcAT, SkColorSpace::MakeSRGB());
825 SkAutoPixmapStorage srcPixels;
826 srcPixels.alloc(srcInfo);
827 refSurf->readPixels(srcPixels, 0, 0);
828 return std::move(srcPixels);
829 };
830
831 for (int sat = 0; sat < kLastEnum_SkAlphaType; ++sat) {
832 const auto srcAT = static_cast<SkAlphaType>(sat);
833 if (srcAT == kUnknown_SkAlphaType ||
834 (srcAT == kUnpremul_SkAlphaType && !rules.fAllowUnpremulSrc)) {
835 continue;
836 }
837 for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
838 const auto srcCT = static_cast<SkColorType>(sct);
839 // Note that we only currently use srcCT for a 1010102 workaround. If we remove this we
840 // can also but the ref data setup above the srcCT loop.
841 SkAutoPixmapStorage srcPixels = make_ref_f32_data(srcAT, srcCT);
842 auto src = srcFactory(srcPixels);
843 if (!src) {
844 continue;
845 }
846 for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
847 const auto readCT = static_cast<SkColorType>(rct);
848 for (const sk_sp<SkColorSpace>& readCS :
849 {SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) {
850 for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
851 const auto readAT = static_cast<SkAlphaType>(at);
852 if (srcAT != kOpaque_SkAlphaType && readAT == kOpaque_SkAlphaType) {
853 // This doesn't make sense.
854 continue;
855 }
856 // Test full size, partial, empty, and too wide rects.
857 for (const auto& rect : {
858 // entire thing
859 SkIRect::MakeWH(kW, kH),
860 // larger on all sides
861 SkIRect::MakeLTRB(-10, -10, kW + 10, kH + 10),
862 // fully contained
863 SkIRect::MakeLTRB(kW / 4, kH / 4, 3 * kW / 4, 3 * kH / 4),
864 // outside top left
865 SkIRect::MakeLTRB(-10, -10, -1, -1),
866 // touching top left corner
867 SkIRect::MakeLTRB(-10, -10, 0, 0),
868 // overlapping top left corner
869 SkIRect::MakeLTRB(-10, -10, kW / 4, kH / 4),
870 // overlapping top left and top right corners
871 SkIRect::MakeLTRB(-10, -10, kW + 10, kH / 4),
872 // touching entire top edge
873 SkIRect::MakeLTRB(-10, -10, kW + 10, 0),
874 // overlapping top right corner
875 SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH / 4),
876 // contained in x, overlapping top edge
877 SkIRect::MakeLTRB(kW / 4, -10, 3 * kW / 4, kH / 4),
878 // outside top right corner
879 SkIRect::MakeLTRB(kW + 1, -10, kW + 10, -1),
880 // touching top right corner
881 SkIRect::MakeLTRB(kW, -10, kW + 10, 0),
882 // overlapping top left and bottom left corners
883 SkIRect::MakeLTRB(-10, -10, kW / 4, kH + 10),
884 // touching entire left edge
885 SkIRect::MakeLTRB(-10, -10, 0, kH + 10),
886 // overlapping bottom left corner
887 SkIRect::MakeLTRB(-10, 3 * kH / 4, kW / 4, kH + 10),
888 // contained in y, overlapping left edge
889 SkIRect::MakeLTRB(-10, kH / 4, kW / 4, 3 * kH / 4),
890 // outside bottom left corner
891 SkIRect::MakeLTRB(-10, kH + 1, -1, kH + 10),
892 // touching bottom left corner
893 SkIRect::MakeLTRB(-10, kH, 0, kH + 10),
894 // overlapping bottom left and bottom right corners
895 SkIRect::MakeLTRB(-10, 3 * kH / 4, kW + 10, kH + 10),
896 // touching entire left edge
897 SkIRect::MakeLTRB(0, kH, kW, kH + 10),
898 // overlapping bottom right corner
899 SkIRect::MakeLTRB(3 * kW / 4, 3 * kH / 4, kW + 10, kH + 10),
900 // overlapping top right and bottom right corners
901 SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH + 10),
902 }) {
903 const auto readInfo = SkImageInfo::Make(rect.width(), rect.height(),
904 readCT, readAT, readCS);
905 const SkIVector offset = rect.topLeft();
906 runTest(src, srcPixels, readInfo, offset);
907 }
908 }
909 }
910 }
911 }
912 }
Brian Salomoncd734f62019-05-10 16:32:54 -0400913}
914
Brian Salomon9241a6d2019-10-03 13:26:54 -0400915namespace {
916struct AsyncContext {
917 bool fCalled = false;
918 std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
919};
920} // anonymous namespace
921
922// Making this a lambda in the test functions caused:
923// "error: cannot compile this forwarded non-trivially copyable parameter yet"
924// on x86/Win/Clang bot, referring to 'result'.
925static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
926 auto context = static_cast<AsyncContext*>(c);
927 context->fResult = std::move(result);
928 context->fCalled = true;
929};
930
Brian Salomonab32f652019-05-10 14:24:50 -0400931DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) {
Brian Salomon5918e832019-10-23 13:34:36 -0400932 using Surface = sk_sp<SkSurface>;
933 auto reader = std::function<GpuReadSrcFn<Surface>>([](const Surface& surface,
934 const SkIVector& offset,
935 const SkPixmap& pixels) {
936 AsyncContext context;
937 auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
938
939 // Rescale quality and linearity don't matter since we're doing a non-scaling readback.
940 surface->asyncRescaleAndReadPixels(pixels.info(), rect, SkSurface::RescaleGamma::kSrc,
941 kNone_SkFilterQuality, async_callback, &context);
942 while (!context.fCalled) {
943 surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
Brian Salomonab32f652019-05-10 14:24:50 -0400944 }
Brian Salomon5918e832019-10-23 13:34:36 -0400945 if (!context.fResult) {
946 return false;
947 }
948 SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), context.fResult->data(0),
949 context.fResult->rowBytes(0), pixels.info().minRowBytes(), pixels.height());
950 return true;
951 });
952 GpuReadPixelTestRules rules;
953 rules.fAllowUnpremulSrc = false;
954 rules.fAllowUnpremulRead = false;
955 rules.fUncontainedRectSucceeds = false;
Brian Salomon5dd77462019-09-26 16:57:09 -0400956
Brian Salomon5918e832019-10-23 13:34:36 -0400957 for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
958 auto factory = std::function<GpuSrcFactory<Surface>>(
959 [context = ctxInfo.grContext(), origin](const SkPixmap& src) {
960 if (src.colorType() == kRGB_888x_SkColorType) {
961 return Surface();
Ravi Mistrycb550102019-10-03 09:06:25 +0000962 }
Brian Salomon5918e832019-10-23 13:34:36 -0400963 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(),
964 0, origin, nullptr);
965 if (surf) {
966 surf->writePixels(src, 0, 0);
967 }
968 return surf;
969 });
970 gpu_read_pixels_test_driver(reporter, rules, factory, reader);
971 }
972}
973
Brian Salomon5918e832019-10-23 13:34:36 -0400974DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
975 using Surface = sk_sp<SkSurface>;
976 auto reader = std::function<GpuReadSrcFn<Surface>>(
977 [](const Surface& surface, const SkIVector& offset, const SkPixmap& pixels) {
978 return surface->readPixels(pixels, offset.fX, offset.fY);
979 });
980 GpuReadPixelTestRules rules;
981 rules.fAllowUnpremulSrc = false;
982 rules.fAllowUnpremulRead = true;
983 rules.fUncontainedRectSucceeds = true;
984
985 for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
986 auto factory = std::function<GpuSrcFactory<Surface>>(
987 [context = ctxInfo.grContext(), origin](const SkPixmap& src) {
988 if (src.colorType() == kRGB_888x_SkColorType) {
989 return Surface();
990 }
991 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(),
992 0, origin, nullptr);
993 if (surf) {
994 surf->writePixels(src, 0, 0);
995 }
996 return surf;
997 });
998 gpu_read_pixels_test_driver(reporter, rules, factory, reader);
Brian Salomon6fc04f82019-10-02 19:11:55 -0400999 }
1000}
Brian Salomon9241a6d2019-10-03 13:26:54 -04001001
1002DEF_GPUTEST(AsyncReadPixelsContextShutdown, reporter, options) {
1003 const auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
1004 SkColorSpace::MakeSRGB());
1005 enum class ShutdownSequence {
1006 kFreeResult_DestroyContext,
1007 kDestroyContext_FreeResult,
1008 kFreeResult_ReleaseAndAbandon_DestroyContext,
1009 kFreeResult_Abandon_DestroyContext,
1010 kReleaseAndAbandon_FreeResult_DestroyContext,
1011 kAbandon_FreeResult_DestroyContext,
1012 kReleaseAndAbandon_DestroyContext_FreeResult,
1013 kAbandon_DestroyContext_FreeResult,
1014 };
1015 for (int t = 0; t < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++t) {
1016 auto type = static_cast<sk_gpu_test::GrContextFactory::ContextType>(t);
1017 for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext,
1018 ShutdownSequence::kDestroyContext_FreeResult,
1019 ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext,
1020 ShutdownSequence::kFreeResult_Abandon_DestroyContext,
1021 ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext,
1022 ShutdownSequence::kAbandon_FreeResult_DestroyContext,
1023 ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult,
1024 ShutdownSequence::kAbandon_DestroyContext_FreeResult}) {
1025 // Vulkan context abandoning without resource release has issues outside of the scope of
1026 // this test.
1027 if (type == sk_gpu_test::GrContextFactory::kVulkan_ContextType &&
1028 (sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext ||
1029 sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult ||
1030 sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext)) {
1031 continue;
1032 }
1033 for (bool yuv : {false, true}) {
1034 sk_gpu_test::GrContextFactory factory(options);
1035 auto context = factory.get(type);
1036 if (!context) {
1037 continue;
1038 }
1039 // This test is only meaningful for contexts that support transfer buffers.
1040 if (!context->priv().caps()->transferBufferSupport()) {
1041 continue;
1042 }
1043 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 1, nullptr);
1044 if (!surf) {
1045 continue;
1046 }
1047 AsyncContext cbContext;
1048 if (yuv) {
1049 surf->asyncRescaleAndReadPixelsYUV420(
1050 kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(), ii.bounds(),
1051 ii.dimensions(), SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality,
1052 &async_callback, &cbContext);
1053 } else {
1054 surf->asyncRescaleAndReadPixels(ii, ii.bounds(), SkSurface::RescaleGamma::kSrc,
1055 kNone_SkFilterQuality, &async_callback,
1056 &cbContext);
1057 }
1058 while (!cbContext.fCalled) {
1059 context->checkAsyncWorkCompletion();
1060 }
1061 if (!cbContext.fResult) {
1062 ERRORF(reporter, "Callback failed on %s. is YUV: %d",
1063 sk_gpu_test::GrContextFactory::ContextTypeName(type), yuv);
1064 continue;
1065 }
1066 // The real test is that we don't crash, get Vulkan validation errors, etc, during
1067 // this shutdown sequence.
1068 switch (sequence) {
1069 case ShutdownSequence::kFreeResult_DestroyContext:
1070 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
1071 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
1072 break;
1073 case ShutdownSequence::kDestroyContext_FreeResult:
1074 factory.destroyContexts();
1075 break;
1076 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
1077 factory.releaseResourcesAndAbandonContexts();
1078 break;
1079 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
1080 factory.abandonContexts();
1081 break;
1082 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
1083 factory.releaseResourcesAndAbandonContexts();
1084 factory.destroyContexts();
1085 break;
1086 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
1087 factory.abandonContexts();
1088 factory.destroyContexts();
1089 break;
1090 }
1091 cbContext.fResult.reset();
1092 switch (sequence) {
1093 case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
1094 factory.releaseResourcesAndAbandonContexts();
1095 break;
1096 case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
1097 factory.abandonContexts();
1098 break;
1099 case ShutdownSequence::kFreeResult_DestroyContext:
1100 case ShutdownSequence::kDestroyContext_FreeResult:
1101 case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
1102 case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
1103 case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
1104 case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
1105 break;
1106 }
1107 }
1108 }
1109 }
1110}