blob: 5fbaf9bf94c2272289c599f687ab3197f2832511 [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
bsalomon@google.comc6980972011-11-02 19:57:21 +00008#include "SkCanvas.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009#include "SkColorPriv.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000010#include "SkMathPriv.h"
reed52d9ac62014-06-30 09:05:34 -070011#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000013
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000014#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080015#include "GrContext.h"
bsalomone8d21e82015-07-16 08:23:13 -070016#include "SkGr.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +000018
kkinnunen15302832015-12-01 04:35:26 -080019#include <initializer_list>
20
bsalomon@google.comc6980972011-11-02 19:57:21 +000021static const int DEV_W = 100, DEV_H = 100;
22static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
rmistry@google.comd6176b02012-08-23 18:14:13 +000023static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
bsalomon@google.comc6980972011-11-02 19:57:21 +000024 DEV_H * SK_Scalar1);
25
bsalomone8d21e82015-07-16 08:23:13 -070026static SkPMColor get_src_color(int x, int y) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000027 SkASSERT(x >= 0 && x < DEV_W);
28 SkASSERT(y >= 0 && y < DEV_H);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000029
30 U8CPU r = x;
31 U8CPU g = y;
32 U8CPU b = 0xc;
33
34 U8CPU a = 0xff;
bsalomon@google.comc4364992011-11-07 15:54:49 +000035 switch ((x+y) % 5) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000036 case 0:
37 a = 0xff;
38 break;
39 case 1:
40 a = 0x80;
41 break;
42 case 2:
43 a = 0xCC;
44 break;
45 case 4:
46 a = 0x01;
47 break;
48 case 3:
49 a = 0x00;
50 break;
51 }
52 return SkPremultiplyARGBInline(a, r, g, b);
bsalomon@google.comc6980972011-11-02 19:57:21 +000053}
halcanary9d524f22016-03-29 09:03:52 -070054
bsalomone8d21e82015-07-16 08:23:13 -070055static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
bsalomon@google.comc6980972011-11-02 19:57:21 +000056 int n = y * w + x;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000057
bsalomon@google.comc6980972011-11-02 19:57:21 +000058 U8CPU b = n & 0xff;
59 U8CPU g = (n >> 8) & 0xff;
60 U8CPU r = (n >> 16) & 0xff;
61 return SkPackARGB32(0xff, r, g , b);
62}
63
bsalomone8d21e82015-07-16 08:23:13 -070064static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
65 bool* doUnpremul) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000066 *doUnpremul = (kUnpremul_SkAlphaType == at);
67
68 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000069 U8CPU a,r,g,b;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000070 switch (ct) {
71 case kBGRA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000072 b = static_cast<U8CPU>(c[0]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000073 g = static_cast<U8CPU>(c[1]);
74 r = static_cast<U8CPU>(c[2]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000075 a = static_cast<U8CPU>(c[3]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000076 break;
77 case kRGBA_8888_SkColorType:
bsalomon@google.com6850eab2011-11-03 20:29:47 +000078 r = static_cast<U8CPU>(c[0]);
79 g = static_cast<U8CPU>(c[1]);
80 b = static_cast<U8CPU>(c[2]);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000081 a = static_cast<U8CPU>(c[3]);
bsalomon@google.com6850eab2011-11-03 20:29:47 +000082 break;
bsalomon@google.comccaa0022012-09-25 19:55:07 +000083 default:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000084 SkDEBUGFAIL("Unexpected colortype");
bsalomon@google.comccaa0022012-09-25 19:55:07 +000085 return 0;
bsalomon@google.com6850eab2011-11-03 20:29:47 +000086 }
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000087
88 if (*doUnpremul) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +000089 r = SkMulDiv255Ceiling(r, a);
90 g = SkMulDiv255Ceiling(g, a);
91 b = SkMulDiv255Ceiling(b, a);
92 }
93 return SkPackARGB32(a, r, g, b);
94}
95
bsalomone8d21e82015-07-16 08:23:13 -070096static SkBitmap make_src_bitmap() {
bsalomon@google.comc6980972011-11-02 19:57:21 +000097 static SkBitmap bmp;
98 if (bmp.isNull()) {
reed84825042014-09-02 12:50:45 -070099 bmp.allocN32Pixels(DEV_W, DEV_H);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000100 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
101 for (int y = 0; y < DEV_H; ++y) {
102 for (int x = 0; x < DEV_W; ++x) {
103 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700104 *pixel = get_src_color(x, y);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000105 }
106 }
107 }
bsalomone8d21e82015-07-16 08:23:13 -0700108 return bmp;
109}
110
111static void fill_src_canvas(SkCanvas* canvas) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000112 canvas->save();
113 canvas->setMatrix(SkMatrix::I());
reed73603f32016-09-20 08:42:38 -0700114 canvas->clipRect(DEV_RECT_S, SkCanvas::kReplace_Op);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000115 SkPaint paint;
116 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
bsalomone8d21e82015-07-16 08:23:13 -0700117 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000118 canvas->restore();
119}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
bsalomone8d21e82015-07-16 08:23:13 -0700121#if SK_SUPPORT_GPU
122static void fill_src_texture(GrTexture* texture) {
123 SkBitmap bmp = make_src_bitmap();
124 bmp.lockPixels();
125 texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(),
126 bmp.rowBytes());
127 bmp.unlockPixels();
128}
129#endif
130
131static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000132 SkAutoLockPixels alp(*bitmap);
133 int w = bitmap->width();
134 int h = bitmap->height();
135 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
136 for (int y = 0; y < h; ++y) {
137 for (int x = 0; x < w; ++x) {
138 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
bsalomone8d21e82015-07-16 08:23:13 -0700139 *pixel = get_dst_bmp_init_color(x, y, w);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000140 }
141 }
142}
143
bsalomone8d21e82015-07-16 08:23:13 -0700144static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000145 if (!didPremulConversion) {
146 return a == b;
147 }
148 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
149 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
150 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
151 int32_t aB = SkGetPackedB32(a);
152
153 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
154 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
155 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
156 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
157
158 return aA == bA &&
159 SkAbs32(aR - bR) <= 1 &&
160 SkAbs32(aG - bG) <= 1 &&
161 SkAbs32(aB - bB) <= 1;
162}
163
bsalomon@google.comc6980972011-11-02 19:57:21 +0000164// checks the bitmap contains correct pixels after the readPixels
165// if the bitmap was prefilled with pixels it checks that these weren't
166// overwritten in the area outside the readPixels.
bsalomone8d21e82015-07-16 08:23:13 -0700167static bool check_read(skiatest::Reporter* reporter,
168 const SkBitmap& bitmap,
169 int x, int y,
170 bool checkCanvasPixels,
171 bool checkBitmapPixels) {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000172 SkASSERT(4 == bitmap.bytesPerPixel());
bsalomon@google.comc6980972011-11-02 19:57:21 +0000173 SkASSERT(!bitmap.isNull());
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000174 SkASSERT(checkCanvasPixels || checkBitmapPixels);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000176 const SkColorType ct = bitmap.colorType();
177 const SkAlphaType at = bitmap.alphaType();
178
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 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000187 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000188 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;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000193 const uint32_t* pixel = bitmap.getAddr32(bx, by);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000194
195 if (clippedSrcRect.contains(devx, devy)) {
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000196 if (checkCanvasPixels) {
bsalomone8d21e82015-07-16 08:23:13 -0700197 SkPMColor canvasPixel = get_src_color(devx, devy);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000198 bool didPremul;
bsalomone8d21e82015-07-16 08:23:13 -0700199 SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
bsalomon39826022015-07-23 08:07:21 -0700200 if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
egdaniel88e8aef2016-06-27 14:34:55 -0700201 ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
202 "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000203 return false;
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000204 }
bsalomon@google.comc6980972011-11-02 19:57:21 +0000205 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000206 } else if (checkBitmapPixels) {
bsalomon39826022015-07-23 08:07:21 -0700207 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
208 if (origDstPixel != *pixel) {
209 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
210 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000211 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000212 }
213 }
214 }
215 }
bsalomon@google.com72f3dca2012-08-17 13:32:06 +0000216 return true;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000217}
218
219enum BitmapInit {
220 kFirstBitmapInit = 0,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
bsalomon@google.comc6980972011-11-02 19:57:21 +0000222 kNoPixels_BitmapInit = kFirstBitmapInit,
223 kTight_BitmapInit,
224 kRowBytes_BitmapInit,
bsalomon9d02b262016-02-01 12:49:30 -0800225 kRowBytesOdd_BitmapInit,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226
bsalomon9d02b262016-02-01 12:49:30 -0800227 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
Brian Salomona64afd62016-02-01 16:44:22 -0500228
229#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
bsalomon9d02b262016-02-01 12:49:30 -0800230 kLast_BitmapInit = kRowBytesOdd_BitmapInit
Brian Salomona64afd62016-02-01 16:44:22 -0500231#else
232 kLast_BitmapInit = kLastAligned_BitmapInit
233#endif
bsalomon@google.comc6980972011-11-02 19:57:21 +0000234};
235
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000236static BitmapInit nextBMI(BitmapInit bmi) {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000237 int x = bmi;
238 return static_cast<BitmapInit>(++x);
239}
240
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000241static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
242 SkAlphaType at) {
243 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000244 size_t rowBytes = 0;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000245 bool alloc = true;
246 switch (init) {
247 case kNoPixels_BitmapInit:
248 alloc = false;
249 case kTight_BitmapInit:
250 break;
251 case kRowBytes_BitmapInit:
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000252 rowBytes = (info.width() + 16) * sizeof(SkPMColor);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000253 break;
bsalomon9d02b262016-02-01 12:49:30 -0800254 case kRowBytesOdd_BitmapInit:
255 rowBytes = (info.width() * sizeof(SkPMColor)) + 3;
256 break;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000257 default:
258 SkASSERT(0);
259 break;
260 }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000261
bsalomon@google.comc6980972011-11-02 19:57:21 +0000262 if (alloc) {
bsalomon9d02b262016-02-01 12:49:30 -0800263 bitmap->allocPixels(info, rowBytes);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000264 } else {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000265 bitmap->setInfo(info, rowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000266 }
267}
268
kkinnunen15302832015-12-01 04:35:26 -0800269static const struct {
270 SkColorType fColorType;
271 SkAlphaType fAlphaType;
272} gReadPixelsConfigs[] = {
273 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
274 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
275 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
276 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
277};
278const SkIRect gReadPixelsTestRects[] = {
279 // entire thing
280 DEV_RECT,
281 // larger on all sides
282 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
283 // fully contained
284 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
285 // outside top left
286 SkIRect::MakeLTRB(-10, -10, -1, -1),
287 // touching top left corner
288 SkIRect::MakeLTRB(-10, -10, 0, 0),
289 // overlapping top left corner
290 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
291 // overlapping top left and top right corners
292 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
293 // touching entire top edge
294 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
295 // overlapping top right corner
296 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
297 // contained in x, overlapping top edge
298 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
299 // outside top right corner
300 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
301 // touching top right corner
302 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
303 // overlapping top left and bottom left corners
304 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
305 // touching entire left edge
306 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
307 // overlapping bottom left corner
308 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
309 // contained in y, overlapping left edge
310 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
311 // outside bottom left corner
312 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
313 // touching bottom left corner
314 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
315 // overlapping bottom left and bottom right corners
316 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
317 // touching entire left edge
318 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
319 // overlapping bottom right corner
320 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
321 // overlapping top right and bottom right corners
322 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
323};
bsalomon@google.comc6980972011-11-02 19:57:21 +0000324
reede8f30622016-03-23 18:59:25 -0700325static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
bsalomon9d02b262016-02-01 12:49:30 -0800326 BitmapInit lastBitmapInit) {
kkinnunen15302832015-12-01 04:35:26 -0800327 SkCanvas* canvas = surface->getCanvas();
328 fill_src_canvas(canvas);
329 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
330 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800331 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800332 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
333 SkBitmap bmp;
334 init_bitmap(&bmp, srcRect, bmi,
335 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
bsalomone8d21e82015-07-16 08:23:13 -0700336
kkinnunen15302832015-12-01 04:35:26 -0800337 // if the bitmap has pixels allocated before the readPixels,
338 // note that and fill them with pattern
339 bool startsWithPixels = !bmp.isNull();
340 if (startsWithPixels) {
341 fill_dst_bmp_with_init_data(&bmp);
342 }
343 uint32_t idBefore = surface->generationID();
344 bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
345 uint32_t idAfter = surface->generationID();
346
347 // we expect to succeed when the read isn't fully clipped
348 // out.
349 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
350 // determine whether we expected the read to succeed.
351 REPORTER_ASSERT(reporter, success == expectSuccess);
352 // read pixels should never change the gen id
353 REPORTER_ASSERT(reporter, idBefore == idAfter);
354
355 if (success || startsWithPixels) {
356 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
357 success, startsWithPixels);
358 } else {
359 // if we had no pixels beforehand and the readPixels
360 // failed then our bitmap should still not have pixels
361 REPORTER_ASSERT(reporter, bmp.isNull());
362 }
363 }
364 // check the old webkit version of readPixels that clips the
365 // bitmap size
366 SkBitmap wkbmp;
367 bool success = canvas->readPixels(srcRect, &wkbmp);
368 SkIRect clippedRect = DEV_RECT;
369 if (clippedRect.intersect(srcRect)) {
370 REPORTER_ASSERT(reporter, success);
371 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
372 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
373 check_read(reporter, wkbmp, clippedRect.fLeft,
374 clippedRect.fTop, true, false);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000375 } else {
kkinnunen15302832015-12-01 04:35:26 -0800376 REPORTER_ASSERT(reporter, !success);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000377 }
kkinnunen15302832015-12-01 04:35:26 -0800378 }
379 }
380}
381DEF_TEST(ReadPixels, reporter) {
382 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
reede8f30622016-03-23 18:59:25 -0700383 auto surface(SkSurface::MakeRaster(info));
bsalomon9d02b262016-02-01 12:49:30 -0800384 // SW readback fails a premul check when reading back to an unaligned rowbytes.
385 test_readpixels(reporter, surface, kLastAligned_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800386}
bsalomone8d21e82015-07-16 08:23:13 -0700387#if SK_SUPPORT_GPU
egdaniel88e8aef2016-06-27 14:34:55 -0700388DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
robertphillips7e922762016-07-26 11:38:17 -0700389 const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
kkinnunen15302832015-12-01 04:35:26 -0800390 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
robertphillips7e922762016-07-26 11:38:17 -0700391 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
392 ii, 0, origin, nullptr));
bsalomon9d02b262016-02-01 12:49:30 -0800393 test_readpixels(reporter, surface, kLast_BitmapInit);
kkinnunen15302832015-12-01 04:35:26 -0800394 }
395}
bsalomone8d21e82015-07-16 08:23:13 -0700396#endif
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000397
bsalomone8d21e82015-07-16 08:23:13 -0700398#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -0800399static void test_readpixels_texture(skiatest::Reporter* reporter, GrTexture* texture) {
400 fill_src_texture(texture);
401 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
402 const SkIRect& srcRect = gReadPixelsTestRects[rect];
bsalomon9d02b262016-02-01 12:49:30 -0800403 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
kkinnunen15302832015-12-01 04:35:26 -0800404 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
405 SkBitmap bmp;
406 init_bitmap(&bmp, srcRect, bmi,
407 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
408
409 // if the bitmap has pixels allocated before the readPixels,
410 // note that and fill them with pattern
411 bool startsWithPixels = !bmp.isNull();
412 // Try doing the read directly from a non-renderable texture
413 if (startsWithPixels) {
414 fill_dst_bmp_with_init_data(&bmp);
415 GrPixelConfig dstConfig =
416 SkImageInfo2GrPixelConfig(gReadPixelsConfigs[c].fColorType,
417 gReadPixelsConfigs[c].fAlphaType,
brianosmanb109b8c2016-06-16 13:03:24 -0700418 nullptr,
brianosmana6359362016-03-21 06:55:37 -0700419 *texture->getContext()->caps());
kkinnunen15302832015-12-01 04:35:26 -0800420 uint32_t flags = 0;
421 if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
422 flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000423 }
kkinnunen15302832015-12-01 04:35:26 -0800424 bmp.lockPixels();
425 bool success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(),
426 bmp.height(), dstConfig, bmp.getPixels(),
427 bmp.rowBytes(), flags);
428 bmp.unlockPixels();
429 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
430 success, true);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000431 }
432 }
433 }
434 }
435}
egdaniel88e8aef2016-06-27 14:34:55 -0700436DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800437 // On the GPU we will also try reading back from a non-renderable texture.
438 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
439 SkAutoTUnref<GrTexture> texture;
440 GrSurfaceDesc desc;
441 desc.fFlags = kRenderTarget_GrSurfaceFlag;
442 desc.fWidth = DEV_W;
443 desc.fHeight = DEV_H;
444 desc.fConfig = kSkia8888_GrPixelConfig;
445 desc.fOrigin = origin;
446 desc.fFlags = kNone_GrSurfaceFlags;
bsalomon8b7451a2016-05-11 06:33:06 -0700447 texture.reset(ctxInfo.grContext()->textureProvider()->createTexture(desc,
448 SkBudgeted::kNo));
kkinnunen15302832015-12-01 04:35:26 -0800449 test_readpixels_texture(reporter, texture);
450 }
451}
452#endif