blob: 867f766d17935978d60fd101b13dbef25e887a7c [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>
bsalomon@google.comc6980972011-11-02 19:57:21 +00009#include "SkCanvas.h"
Cary Clarka4083c92017-09-15 11:59:23 -040010#include "SkColorData.h"
Matt Sarett8572d852017-02-14 11:21:02 -050011#include "SkHalf.h"
12#include "SkImageInfoPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000013#include "SkMathPriv.h"
reed52d9ac62014-06-30 09:05:34 -070014#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000015#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000016
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080018#include "GrContext.h"
Robert Phillipseb4f1862017-06-08 16:38:25 -040019#include "GrContextFactory.h"
Robert Phillipse78b7252017-04-06 07:59:41 -040020#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050021#include "GrProxyProvider.h"
Brian Salomon58389b92018-03-07 13:01:25 -050022#include "ProxyUtils.h"
bsalomone8d21e82015-07-16 08:23:13 -070023#include "SkGr.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000024#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000025
kkinnunen15302832015-12-01 04:35:26 -080026
bsalomon@google.comc6980972011-11-02 19:57:21 +000027static const int DEV_W = 100, DEV_H = 100;
28static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000029static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000030 DEV_H * SK_Scalar1);
31
bsalomone8d21e82015-07-16 08:23:13 -070032static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000033 SkASSERT(x >= 0 && x < DEV_W);
34 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000035
36 U8CPU r = x;
37 U8CPU g = y;
38 U8CPU b = 0xc;
39
40 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000041 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000042 case 0:
43 a = 0xff;
44 break;
45 case 1:
46 a = 0x80;
47 break;
48 case 2:
49 a = 0xCC;
50 break;
51 case 4:
52 a = 0x01;
53 break;
54 case 3:
55 a = 0x00;
56 break;
57 }
58 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000059}
halcanary9d524f22016-03-29 09:03:52 -070060
bsalomone8d21e82015-07-16 08:23:13 -070061static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000062 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000063
bsalomon@google.comc6980972011-11-02 19:57:21 +000064 U8CPU b = n & 0xff;
65 U8CPU g = (n >> 8) & 0xff;
66 U8CPU r = (n >> 16) & 0xff;
67 return SkPackARGB32(0xff, r, g , b);
68}
69
Brian Salomon5fba7ad2018-03-22 10:01:16 -040070// TODO: Make this consider both ATs
bsalomone8d21e82015-07-16 08:23:13 -070071static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
72 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000073 *doUnpremul = (kUnpremul_SkAlphaType == at);
74
75 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000076 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000077 switch (ct) {
78 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000079 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000080 g = static_cast<U8CPU>(c[1]);
81 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000082 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000083 break;
Brian Salomon5fba7ad2018-03-22 10:01:16 -040084 case kRGB_888x_SkColorType: // fallthrough
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000085 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000086 r = static_cast<U8CPU>(c[0]);
87 g = static_cast<U8CPU>(c[1]);
88 b = static_cast<U8CPU>(c[2]);
Brian Salomon5fba7ad2018-03-22 10:01:16 -040089 // 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 +000090 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000091 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000092 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000093 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000094 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000095 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000096
97 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000098 r = SkMulDiv255Ceiling(r, a);
99 g = SkMulDiv255Ceiling(g, a);
100 b = SkMulDiv255Ceiling(b, a);
101 }
102 return SkPackARGB32(a, r, g, b);
103}
104
bsalomone8d21e82015-07-16 08:23:13 -0700105static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000106 static SkBitmap bmp;
107 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -0700108 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000109 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
110 for (int y = 0; y < DEV_H; ++y) {
111 for (int x = 0; x < DEV_W; ++x) {
112 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700113 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000114 }
115 }
116 }
bsalomone8d21e82015-07-16 08:23:13 -0700117 return bmp;
118}
119
120static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000121 canvas->save();
122 canvas->setMatrix(SkMatrix::I());
Mike Reedc1f77742016-12-09 09:00:50 -0500123 canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000124 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700125 paint.setBlendMode(SkBlendMode::kSrc);
bsalomone8d21e82015-07-16 08:23:13 -0700126 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000127 canvas->restore();
128}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129
bsalomone8d21e82015-07-16 08:23:13 -0700130static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000131 int w = bitmap->width();
132 int h = bitmap->height();
133 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
134 for (int y = 0; y < h; ++y) {
135 for (int x = 0; x < w; ++x) {
lsalzmana2415ac2016-10-11 14:29:12 -0700136 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
137 if (kAlpha_8_SkColorType == bitmap->colorType()) {
138 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
139 *alpha = SkGetPackedA32(initColor);
140 } else {
141 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
142 *pixel = initColor;
143 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000144 }
145 }
146}
147
bsalomone8d21e82015-07-16 08:23:13 -0700148static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000149 if (!didPremulConversion) {
150 return a == b;
151 }
152 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
153 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
154 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
155 int32_t aB = SkGetPackedB32(a);
156
157 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
158 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
159 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
160 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
161
162 return aA == bA &&
163 SkAbs32(aR - bR) <= 1 &&
164 SkAbs32(aG - bG) <= 1 &&
165 SkAbs32(aB - bB) <= 1;
166}
167
bsalomon@google.comc6980972011-11-02 19:57:21 +0000168// checks the bitmap contains correct pixels after the readPixels
169// if the bitmap was prefilled with pixels it checks that these weren't
170// overwritten in the area outside the readPixels.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400171static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
172 bool checkSurfacePixels, bool checkBitmapPixels,
173 SkAlphaType surfaceAlphaType) {
174 SkAlphaType bmpAT = bitmap.alphaType();
175 SkColorType bmpCT = bitmap.colorType();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000176 SkASSERT(!bitmap.isNull());
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400177 SkASSERT(checkSurfacePixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178
bsalomon@google.comc6980972011-11-02 19:57:21 +0000179 int bw = bitmap.width();
180 int bh = bitmap.height();
181
182 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
183 SkIRect clippedSrcRect = DEV_RECT;
184 if (!clippedSrcRect.intersect(srcRect)) {
185 clippedSrcRect.setEmpty();
186 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400187 if (kAlpha_8_SkColorType == bmpCT) {
lsalzmana2415ac2016-10-11 14:29:12 -0700188 for (int by = 0; by < bh; ++by) {
189 for (int bx = 0; bx < bw; ++bx) {
190 int devx = bx + srcRect.fLeft;
191 int devy = by + srcRect.fTop;
192 const uint8_t* alpha = bitmap.getAddr8(bx, by);
193
194 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400195 if (checkSurfacePixels) {
196 uint8_t surfaceAlpha = (surfaceAlphaType == kOpaque_SkAlphaType)
197 ? 0xFF
198 : SkGetPackedA32(get_src_color(devx, devy));
199 if (surfaceAlpha != *alpha) {
200 ERRORF(reporter,
201 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
202 bx, by, surfaceAlpha, *alpha);
lsalzmana2415ac2016-10-11 14:29:12 -0700203 return false;
204 }
205 }
206 } else if (checkBitmapPixels) {
207 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
208 if (origDstAlpha != *alpha) {
209 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
210 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
211 return false;
212 }
213 }
214 }
215 }
216 return true;
217 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000218 for (int by = 0; by < bh; ++by) {
219 for (int bx = 0; bx < bw; ++bx) {
220 int devx = bx + srcRect.fLeft;
221 int devy = by + srcRect.fTop;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000222
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000223 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000224
225 if (clippedSrcRect.contains(devx, devy)) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400226 if (checkSurfacePixels) {
227 SkPMColor surfacePMColor = get_src_color(devx, devy);
228 if (kOpaque_SkAlphaType == surfaceAlphaType || kOpaque_SkAlphaType == bmpAT) {
229 surfacePMColor |= 0xFF000000;
230 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000231 bool didPremul;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400232 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
233 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
234 ERRORF(reporter,
235 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
236 "Readback was unpremul: %d",
237 bx, by, surfacePMColor, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000238 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000239 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000240 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000241 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700242 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
243 if (origDstPixel != *pixel) {
244 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
245 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000246 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000247 }
248 }
249 }
250 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000251 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000252}
253
254enum BitmapInit {
255 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000256
Mike Reed12e946b2017-04-17 10:53:29 -0400257 kTight_BitmapInit = kFirstBitmapInit,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000258 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800259 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000260
bsalomon9d02b262016-02-01 12:49:30 -0800261 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500262
263#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800264 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500265#else
266 kLast_BitmapInit = kLastAligned_BitmapInit
267#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000268};
269
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000270static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000271 int x = bmi;
272 return static_cast<BitmapInit>(++x);
273}
274
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000275static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
276 SkAlphaType at) {
277 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000278 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000279 switch (init) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000280 case kTight_BitmapInit:
281 break;
282 case kRowBytes_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700283 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000284 break;
bsalomon9d02b262016-02-01 12:49:30 -0800285 case kRowBytesOdd_BitmapInit:
lsalzmana2415ac2016-10-11 14:29:12 -0700286 rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
bsalomon9d02b262016-02-01 12:49:30 -0800287 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000288 default:
289 SkASSERT(0);
290 break;
291 }
Mike Reed12e946b2017-04-17 10:53:29 -0400292 bitmap->allocPixels(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000293}
294
kkinnunen15302832015-12-01 04:35:26 -0800295static const struct {
296 SkColorType fColorType;
297 SkAlphaType fAlphaType;
298} gReadPixelsConfigs[] = {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400299 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
300 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
301 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
302 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
303 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
304 {kAlpha_8_SkColorType, kPremul_SkAlphaType},
kkinnunen15302832015-12-01 04:35:26 -0800305};
306const SkIRect gReadPixelsTestRects[] = {
307 // entire thing
308 DEV_RECT,
309 // larger on all sides
310 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
311 // fully contained
312 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
313 // outside top left
314 SkIRect::MakeLTRB(-10, -10, -1, -1),
315 // touching top left corner
316 SkIRect::MakeLTRB(-10, -10, 0, 0),
317 // overlapping top left corner
318 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
319 // overlapping top left and top right corners
320 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
321 // touching entire top edge
322 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
323 // overlapping top right corner
324 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
325 // contained in x, overlapping top edge
326 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
327 // outside top right corner
328 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
329 // touching top right corner
330 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
331 // overlapping top left and bottom left corners
332 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
333 // touching entire left edge
334 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
335 // overlapping bottom left corner
336 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
337 // contained in y, overlapping left edge
338 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
339 // outside bottom left corner
340 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
341 // touching bottom left corner
342 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
343 // overlapping bottom left and bottom right corners
344 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
345 // touching entire left edge
346 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
347 // overlapping bottom right corner
348 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
349 // overlapping top right and bottom right corners
350 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
351};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000352
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400353enum class ReadSuccessExpectation {
354 kNo,
355 kMaybe,
356 kYes,
357};
358
359bool check_success_expectation(ReadSuccessExpectation expectation, bool actualSuccess) {
360 switch (expectation) {
361 case ReadSuccessExpectation::kMaybe:
362 return true;
363 case ReadSuccessExpectation::kNo:
364 return !actualSuccess;
365 case ReadSuccessExpectation::kYes:
366 return actualSuccess;
367 }
368 return false;
369}
370
371ReadSuccessExpectation read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
372 const SkImageInfo& srcInfo, bool isGPU) {
373 if (!SkIRect::Intersects(srcRect, DEV_RECT)) {
374 return ReadSuccessExpectation::kNo;
375 }
376 if (!SkImageInfoValidConversion(dstInfo, srcInfo)) {
377 return ReadSuccessExpectation::kNo;
378 }
379 if (!isGPU) {
380 return ReadSuccessExpectation::kYes;
381 }
382 // This serves more as documentation of what currently works on the GPU rather than desired
383 // expectations. Once we make GrSurfaceContext color/alpha type aware and clean up some read
384 // pixels code we will make more scenarios work.
385
386 // The GPU code current only does the premul->unpremul conversion, not the reverse.
387 if (srcInfo.alphaType() == kUnpremul_SkAlphaType &&
388 dstInfo.alphaType() == kPremul_SkAlphaType) {
389 return ReadSuccessExpectation::kNo;
390 }
391 // We don't currently require reading alpha-only surfaces to succeed because of some pessimistic
392 // caps decisions and alpha/red complexity in GL.
393 if (SkColorTypeIsAlphaOnly(srcInfo.colorType())) {
394 return ReadSuccessExpectation::kMaybe;
395 }
396 if (!SkColorTypeIsAlwaysOpaque(srcInfo.colorType()) &&
397 SkColorTypeIsAlwaysOpaque(dstInfo.colorType())) {
398 return ReadSuccessExpectation::kNo;
399 }
400 return ReadSuccessExpectation::kYes;
401}
402
reede8f30622016-03-23 18:59:25 -0700403static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400404 const SkImageInfo& surfaceInfo, BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800405 SkCanvas* canvas = surface->getCanvas();
406 fill_src_canvas(canvas);
407 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
408 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800409 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800410 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
411 SkBitmap bmp;
412 init_bitmap(&bmp, srcRect, bmi,
413 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700414
kkinnunen15302832015-12-01 04:35:26 -0800415 // if the bitmap has pixels allocated before the readPixels,
416 // note that and fill them with pattern
417 bool startsWithPixels = !bmp.isNull();
418 if (startsWithPixels) {
419 fill_dst_bmp_with_init_data(&bmp);
420 }
421 uint32_t idBefore = surface->generationID();
Mike Reedf1942192017-07-21 14:24:29 -0400422 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
kkinnunen15302832015-12-01 04:35:26 -0800423 uint32_t idAfter = surface->generationID();
424
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400425 // we expect to succeed when the read isn't fully clipped out and the infos are
426 // compatible.
427 bool isGPU = SkToBool(surface->getCanvas()->getGrContext());
428 auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo, isGPU);
kkinnunen15302832015-12-01 04:35:26 -0800429 // determine whether we expected the read to succeed.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400430 REPORTER_ASSERT(reporter, check_success_expectation(expectSuccess, success),
431 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
432 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
433 bmp.info().colorType(), bmp.info().alphaType());
kkinnunen15302832015-12-01 04:35:26 -0800434 // read pixels should never change the gen id
435 REPORTER_ASSERT(reporter, idBefore == idAfter);
436
437 if (success || startsWithPixels) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400438 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
439 startsWithPixels, surfaceInfo.alphaType());
kkinnunen15302832015-12-01 04:35:26 -0800440 } else {
441 // if we had no pixels beforehand and the readPixels
442 // failed then our bitmap should still not have pixels
443 REPORTER_ASSERT(reporter, bmp.isNull());
444 }
445 }
kkinnunen15302832015-12-01 04:35:26 -0800446 }
447 }
448}
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400449
kkinnunen15302832015-12-01 04:35:26 -0800450DEF_TEST(ReadPixels, reporter) {
451 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700452 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800453 // SW readback fails a premul check when reading back to an unaligned rowbytes.
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400454 test_readpixels(reporter, surface, info, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800455}
bsalomone8d21e82015-07-16 08:23:13 -0700456#if SK_SUPPORT_GPU
egdaniel88e8aef2016-06-27 14:34:55 -0700457DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
Robert Phillipseb4f1862017-06-08 16:38:25 -0400458 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
459 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType ||
460 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
461 // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs
462 return;
463 }
464
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400465 static const SkImageInfo kImageInfos[] = {
466 SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
467 SkImageInfo::Make(DEV_W, DEV_H, kBGRA_8888_SkColorType, kPremul_SkAlphaType),
468 SkImageInfo::Make(DEV_W, DEV_H, kRGB_888x_SkColorType, kOpaque_SkAlphaType),
469 SkImageInfo::Make(DEV_W, DEV_H, kAlpha_8_SkColorType, kPremul_SkAlphaType),
470 };
471 for (const auto& ii : kImageInfos) {
472 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
473 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(
474 ctxInfo.grContext(), SkBudgeted::kNo, ii, 0, origin, nullptr));
475 if (!surface) {
476 continue;
477 }
478 test_readpixels(reporter, surface, ii, kLast_BitmapInit);
479 }
kkinnunen15302832015-12-01 04:35:26 -0800480 }
481}
bsalomone8d21e82015-07-16 08:23:13 -0700482#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000483
bsalomone8d21e82015-07-16 08:23:13 -0700484#if SK_SUPPORT_GPU
Robert Phillipse78b7252017-04-06 07:59:41 -0400485static void test_readpixels_texture(skiatest::Reporter* reporter,
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400486 sk_sp<GrSurfaceContext> sContext,
487 const SkImageInfo& surfaceInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800488 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
489 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800490 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800491 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
492 SkBitmap bmp;
493 init_bitmap(&bmp, srcRect, bmi,
494 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
495
496 // if the bitmap has pixels allocated before the readPixels,
497 // note that and fill them with pattern
498 bool startsWithPixels = !bmp.isNull();
499 // Try doing the read directly from a non-renderable texture
500 if (startsWithPixels) {
501 fill_dst_bmp_with_init_data(&bmp);
kkinnunen15302832015-12-01 04:35:26 -0800502 uint32_t flags = 0;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400503 // TODO: These two hacks can go away when the surface context knows the alpha
504 // type.
505 // Tell the read to perform an unpremul step since it doesn't know alpha type.
kkinnunen15302832015-12-01 04:35:26 -0800506 if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400507 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000508 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400509 // The surface context doesn't know that the src is opaque. We don't support
510 // converting non-opaque data to opaque during a read.
511 if (bmp.alphaType() == kOpaque_SkAlphaType &&
512 surfaceInfo.alphaType() != kOpaque_SkAlphaType) {
513 continue;
514 }
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400515 bool success = sContext->readPixels(bmp.info(), bmp.getPixels(),
516 bmp.rowBytes(),
517 srcRect.fLeft, srcRect.fTop, flags);
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400518 auto expectSuccess =
519 read_should_succeed(srcRect, bmp.info(), surfaceInfo, true);
520 REPORTER_ASSERT(
521 reporter, check_success_expectation(expectSuccess, success),
522 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
523 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
524 bmp.info().colorType(), bmp.info().alphaType());
525 if (success) {
526 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true,
527 surfaceInfo.alphaType());
528 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000529 }
530 }
531 }
532 }
533}
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400534
egdaniel88e8aef2016-06-27 14:34:55 -0700535DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
Robert Phillipseb4f1862017-06-08 16:38:25 -0400536 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
537 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType ||
538 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
539 // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs
540 return;
541 }
542
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400543 GrContext* context = ctxInfo.grContext();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400544 SkBitmap bmp = make_src_bitmap();
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400545
kkinnunen15302832015-12-01 04:35:26 -0800546 // On the GPU we will also try reading back from a non-renderable texture.
Brian Salomon8b1fb742016-11-03 15:21:06 -0400547 for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
Brian Salomon58389b92018-03-07 13:01:25 -0500548 for (auto isRT : {false, true}) {
549 sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData(
550 context, isRT, DEV_W, DEV_H, bmp.colorType(), origin, bmp.getPixels(),
551 bmp.rowBytes());
Robert Phillipsbab2dbb2017-04-17 07:43:27 -0400552 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500553 std::move(proxy));
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400554 auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
555 test_readpixels_texture(reporter, std::move(sContext), info);
Brian Salomon8b1fb742016-11-03 15:21:06 -0400556 }
kkinnunen15302832015-12-01 04:35:26 -0800557 }
558}
559#endif
Matt Sarett8572d852017-02-14 11:21:02 -0500560
561///////////////////////////////////////////////////////////////////////////////////////////////////
562
563static const uint32_t kNumPixels = 5;
564
565// The five reference pixels are: red, green, blue, white, black.
566// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
567// plus a tail pixel.
568static const uint32_t rgba[kNumPixels] = {
569 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
570};
571static const uint32_t bgra[kNumPixels] = {
572 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
573};
574static const uint16_t rgb565[kNumPixels] = {
575 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
576};
577
578static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
579
580static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
581static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
582static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
583static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
584static const uint64_t f16[kNumPixels] = {
585 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
586};
587
Matt Sarett8572d852017-02-14 11:21:02 -0500588static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
589static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
590
591static const void* five_reference_pixels(SkColorType colorType) {
592 switch (colorType) {
593 case kUnknown_SkColorType:
594 return nullptr;
595 case kAlpha_8_SkColorType:
596 return alpha8;
597 case kRGB_565_SkColorType:
598 return rgb565;
599 case kARGB_4444_SkColorType:
600 return rgba4444;
601 case kRGBA_8888_SkColorType:
602 return rgba;
603 case kBGRA_8888_SkColorType:
604 return bgra;
Matt Sarett8572d852017-02-14 11:21:02 -0500605 case kGray_8_SkColorType:
606 return gray8;
607 case kRGBA_F16_SkColorType:
608 return f16;
Mike Reed304a07c2017-07-12 15:10:28 -0400609 default:
610 return nullptr; // remove me when kIndex_8 is removed from the enum
Matt Sarett8572d852017-02-14 11:21:02 -0500611 }
612
613 SkASSERT(false);
614 return nullptr;
615}
616
617static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
618 const SkImageInfo& srcInfo) {
Matt Sarett26b44df2017-05-02 16:04:56 -0400619 if (!SkImageInfoIsValidRenderingCS(srcInfo)) {
Matt Sarett8572d852017-02-14 11:21:02 -0500620 return;
621 }
622
Matt Sarett8572d852017-02-14 11:21:02 -0500623 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
Mike Reed304a07c2017-07-12 15:10:28 -0400624 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500625 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
626 REPORTER_ASSERT(r, src);
627
628 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
629 uint64_t dstPixels[kNumPixels];
Mike Reed304a07c2017-07-12 15:10:28 -0400630 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
Matt Sarett8572d852017-02-14 11:21:02 -0500631 bool success = src->readPixels(dstPixmap, 0, 0);
632 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
633
634 if (success) {
635 if (kGray_8_SkColorType == srcInfo.colorType() &&
636 kGray_8_SkColorType != dstInfo.colorType())
637 {
638 // This conversion is legal, but we won't get the "reference" pixels since we cannot
639 // represent colors in kGray8.
640 return;
641 }
642
643 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
644 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
645
646 }
647}
648
649DEF_TEST(ReadPixels_ValidConversion, reporter) {
650 const SkColorType kColorTypes[] = {
651 kUnknown_SkColorType,
652 kAlpha_8_SkColorType,
653 kRGB_565_SkColorType,
654 kARGB_4444_SkColorType,
655 kRGBA_8888_SkColorType,
656 kBGRA_8888_SkColorType,
Matt Sarett8572d852017-02-14 11:21:02 -0500657 kGray_8_SkColorType,
658 kRGBA_F16_SkColorType,
659 };
660
661 const SkAlphaType kAlphaTypes[] = {
662 kUnknown_SkAlphaType,
663 kOpaque_SkAlphaType,
664 kPremul_SkAlphaType,
665 kUnpremul_SkAlphaType,
666 };
667
668 const sk_sp<SkColorSpace> kColorSpaces[] = {
669 nullptr,
670 SkColorSpace::MakeSRGB(),
671 };
672
673 for (SkColorType dstCT : kColorTypes) {
674 for (SkAlphaType dstAT: kAlphaTypes) {
675 for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
676 for (SkColorType srcCT : kColorTypes) {
677 for (SkAlphaType srcAT: kAlphaTypes) {
678 for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
679 if (kRGBA_F16_SkColorType == dstCT && dstCS) {
Brian Osman36703d92017-12-12 14:09:31 -0500680 dstCS = dstCS->makeLinearGamma();
Matt Sarett8572d852017-02-14 11:21:02 -0500681 }
682
683 if (kRGBA_F16_SkColorType == srcCT && srcCS) {
Brian Osman36703d92017-12-12 14:09:31 -0500684 srcCS = srcCS->makeLinearGamma();
Matt Sarett8572d852017-02-14 11:21:02 -0500685 }
686
687 test_conversion(reporter,
688 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
689 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
690 }
691 }
692 }
693 }
694 }
695 }
696}