2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorPriv.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkSurface.h"
14 #include "include/private/SkColorData.h"
15 #include "include/private/SkHalf.h"
16 #include "include/private/SkImageInfoPriv.h"
17 #include "include/utils/SkNWayCanvas.h"
18 #include "src/core/SkMathPriv.h"
19 #include "tests/Test.h"
21 static const int DEV_W = 100, DEV_H = 100;
22 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
23 static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
26 static SkPMColor get_src_color(int x, int y) {
27 SkASSERT(x >= 0 && x < DEV_W);
28 SkASSERT(y >= 0 && y < DEV_H);
52 return SkPremultiplyARGBInline(a, r, g, b);
55 static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
59 U8CPU g = (n >> 8) & 0xff;
60 U8CPU r = (n >> 16) & 0xff;
61 return SkPackARGB32(0xff, r, g , b);
64 // TODO: Make this consider both ATs
65 static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
67 *doUnpremul = (kUnpremul_SkAlphaType == at);
69 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
72 case kBGRA_8888_SkColorType:
73 b = static_cast<U8CPU>(c[0]);
74 g = static_cast<U8CPU>(c[1]);
75 r = static_cast<U8CPU>(c[2]);
76 a = static_cast<U8CPU>(c[3]);
78 case kRGB_888x_SkColorType: // fallthrough
79 case kRGBA_8888_SkColorType:
80 r = static_cast<U8CPU>(c[0]);
81 g = static_cast<U8CPU>(c[1]);
82 b = static_cast<U8CPU>(c[2]);
83 // We set this even when for kRGB_888x because our caller will validate that it is 0xff.
84 a = static_cast<U8CPU>(c[3]);
87 SkDEBUGFAIL("Unexpected colortype");
92 r = SkMulDiv255Ceiling(r, a);
93 g = SkMulDiv255Ceiling(g, a);
94 b = SkMulDiv255Ceiling(b, a);
96 return SkPackARGB32(a, r, g, b);
99 static sk_sp<SkImage> make_src_image() {
102 bmp.allocN32Pixels(DEV_W, DEV_H);
103 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
104 for (int y = 0; y < DEV_H; ++y) {
105 for (int x = 0; x < DEV_W; ++x) {
106 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
107 *pixel = get_src_color(x, y);
112 return bmp.asImage();
115 static void fill_src_canvas(SkCanvas* canvas) {
117 canvas->setMatrix(SkMatrix::I());
118 canvas->clipRect(DEV_RECT_S, SkClipOp::kIntersect);
120 paint.setBlendMode(SkBlendMode::kSrc);
121 canvas->drawImage(make_src_image(), 0, 0, SkSamplingOptions(), &paint);
125 static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
126 int w = bitmap->width();
127 int h = bitmap->height();
128 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
129 for (int y = 0; y < h; ++y) {
130 for (int x = 0; x < w; ++x) {
131 SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
132 if (kAlpha_8_SkColorType == bitmap->colorType()) {
133 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
134 *alpha = SkGetPackedA32(initColor);
136 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
143 static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
144 if (!didPremulConversion) {
147 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
148 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
149 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
150 int32_t aB = SkGetPackedB32(a);
152 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
153 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
154 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
155 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
158 SkAbs32(aR - bR) <= 1 &&
159 SkAbs32(aG - bG) <= 1 &&
160 SkAbs32(aB - bB) <= 1;
163 // checks the bitmap contains correct pixels after the readPixels
164 // if the bitmap was prefilled with pixels it checks that these weren't
165 // overwritten in the area outside the readPixels.
166 static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
167 bool checkSurfacePixels, bool checkBitmapPixels,
168 SkImageInfo surfaceInfo) {
169 SkAlphaType bmpAT = bitmap.alphaType();
170 SkColorType bmpCT = bitmap.colorType();
171 SkASSERT(!bitmap.isNull());
172 SkASSERT(checkSurfacePixels || checkBitmapPixels);
174 int bw = bitmap.width();
175 int bh = bitmap.height();
177 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
178 SkIRect clippedSrcRect = DEV_RECT;
179 if (!clippedSrcRect.intersect(srcRect)) {
180 clippedSrcRect.setEmpty();
182 if (kAlpha_8_SkColorType == bmpCT) {
183 for (int by = 0; by < bh; ++by) {
184 for (int bx = 0; bx < bw; ++bx) {
185 int devx = bx + srcRect.fLeft;
186 int devy = by + srcRect.fTop;
187 const uint8_t* alpha = bitmap.getAddr8(bx, by);
189 if (clippedSrcRect.contains(devx, devy)) {
190 if (checkSurfacePixels) {
191 uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
193 : SkGetPackedA32(get_src_color(devx, devy));
194 if (surfaceAlpha != *alpha) {
196 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
197 bx, by, surfaceAlpha, *alpha);
201 } else if (checkBitmapPixels) {
202 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
203 if (origDstAlpha != *alpha) {
204 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
205 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
213 for (int by = 0; by < bh; ++by) {
214 for (int bx = 0; bx < bw; ++bx) {
215 int devx = bx + srcRect.fLeft;
216 int devy = by + srcRect.fTop;
218 const uint32_t* pixel = bitmap.getAddr32(bx, by);
220 if (clippedSrcRect.contains(devx, devy)) {
221 if (checkSurfacePixels) {
222 SkPMColor surfacePMColor = get_src_color(devx, devy);
223 if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
224 surfacePMColor &= 0xFF000000;
226 if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
227 surfacePMColor |= 0xFF000000;
230 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
231 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
233 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
234 "Readback was unpremul: %d",
235 bx, by, surfacePMColor, pmPixel, didPremul);
239 } else if (checkBitmapPixels) {
240 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
241 if (origDstPixel != *pixel) {
242 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
243 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
252 enum class TightRowBytes : bool { kNo, kYes };
254 static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB,
255 SkColorType ct, SkAlphaType at) {
256 SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at);
258 if (tightRB == TightRowBytes::kNo) {
259 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
261 bitmap->allocPixels(info, rowBytes);
264 static const struct {
265 SkColorType fColorType;
266 SkAlphaType fAlphaType;
267 } gReadPixelsConfigs[] = {
268 {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
269 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
270 {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
271 {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
272 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
273 {kAlpha_8_SkColorType, kPremul_SkAlphaType},
275 const SkIRect gReadPixelsTestRects[] = {
278 // larger on all sides
279 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
281 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
283 SkIRect::MakeLTRB(-10, -10, -1, -1),
284 // touching top left corner
285 SkIRect::MakeLTRB(-10, -10, 0, 0),
286 // overlapping top left corner
287 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
288 // overlapping top left and top right corners
289 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
290 // touching entire top edge
291 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
292 // overlapping top right corner
293 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
294 // contained in x, overlapping top edge
295 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
296 // outside top right corner
297 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
298 // touching top right corner
299 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
300 // overlapping top left and bottom left corners
301 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
302 // touching entire left edge
303 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
304 // overlapping bottom left corner
305 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
306 // contained in y, overlapping left edge
307 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
308 // outside bottom left corner
309 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
310 // touching bottom left corner
311 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
312 // overlapping bottom left and bottom right corners
313 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
314 // touching entire left edge
315 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
316 // overlapping bottom right corner
317 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
318 // overlapping top right and bottom right corners
319 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
322 bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
323 const SkImageInfo& srcInfo) {
324 return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
327 static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
328 const SkImageInfo& surfaceInfo) {
329 SkCanvas* canvas = surface->getCanvas();
330 fill_src_canvas(canvas);
331 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
332 const SkIRect& srcRect = gReadPixelsTestRects[rect];
333 for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
334 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
336 init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
337 gReadPixelsConfigs[c].fAlphaType);
339 // if the bitmap has pixels allocated before the readPixels,
340 // note that and fill them with pattern
341 bool startsWithPixels = !bmp.isNull();
342 if (startsWithPixels) {
343 fill_dst_bmp_with_init_data(&bmp);
345 uint32_t idBefore = surface->generationID();
346 bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
347 uint32_t idAfter = surface->generationID();
349 // we expect to succeed when the read isn't fully clipped out and the infos are
351 bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
352 // determine whether we expected the read to succeed.
353 REPORTER_ASSERT(reporter, expectSuccess == success,
354 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
355 success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
356 bmp.info().colorType(), bmp.info().alphaType());
357 // read pixels should never change the gen id
358 REPORTER_ASSERT(reporter, idBefore == idAfter);
360 if (success || startsWithPixels) {
361 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
362 startsWithPixels, surfaceInfo);
364 // if we had no pixels beforehand and the readPixels
365 // failed then our bitmap should still not have pixels
366 REPORTER_ASSERT(reporter, bmp.isNull());
373 DEF_TEST(ReadPixels, reporter) {
374 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
375 auto surface(SkSurface::MakeRaster(info));
376 test_readpixels(reporter, surface, info);
379 ///////////////////////////////////////////////////////////////////////////////////////////////////
381 static const uint32_t kNumPixels = 5;
383 // The five reference pixels are: red, green, blue, white, black.
384 // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
385 // plus a tail pixel.
386 static const uint32_t rgba[kNumPixels] = {
387 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
389 static const uint32_t bgra[kNumPixels] = {
390 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
392 static const uint16_t rgb565[kNumPixels] = {
393 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
396 static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
398 static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
399 static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
400 static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
401 static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
402 static const uint64_t f16[kNumPixels] = {
403 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
406 static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
407 static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
409 static const void* five_reference_pixels(SkColorType colorType) {
411 case kUnknown_SkColorType:
413 case kAlpha_8_SkColorType:
415 case kRGB_565_SkColorType:
417 case kARGB_4444_SkColorType:
419 case kRGBA_8888_SkColorType:
421 case kBGRA_8888_SkColorType:
423 case kGray_8_SkColorType:
425 case kRGBA_F16_SkColorType:
435 static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
436 const SkImageInfo& srcInfo) {
437 if (!SkImageInfoIsValid(srcInfo)) {
441 const void* srcPixels = five_reference_pixels(srcInfo.colorType());
442 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
443 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
444 REPORTER_ASSERT(r, src);
446 // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
447 uint64_t dstPixels[kNumPixels];
448 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
449 bool success = src->readPixels(nullptr, dstPixmap, 0, 0);
450 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
453 if (kGray_8_SkColorType == srcInfo.colorType() &&
454 kGray_8_SkColorType != dstInfo.colorType()) {
455 // TODO: test (r,g,b) == (gray,gray,gray)?
459 if (kGray_8_SkColorType == dstInfo.colorType() &&
460 kGray_8_SkColorType != srcInfo.colorType()) {
461 // TODO: test gray = luminance?
465 if (kAlpha_8_SkColorType == srcInfo.colorType() &&
466 kAlpha_8_SkColorType != dstInfo.colorType()) {
467 // TODO: test output = black with this alpha?
471 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
472 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
476 DEF_TEST(ReadPixels_ValidConversion, reporter) {
477 const SkColorType kColorTypes[] = {
478 kUnknown_SkColorType,
479 kAlpha_8_SkColorType,
480 kRGB_565_SkColorType,
481 kARGB_4444_SkColorType,
482 kRGBA_8888_SkColorType,
483 kBGRA_8888_SkColorType,
485 kRGBA_F16_SkColorType,
488 const SkAlphaType kAlphaTypes[] = {
489 kUnknown_SkAlphaType,
492 kUnpremul_SkAlphaType,
495 const sk_sp<SkColorSpace> kColorSpaces[] = {
497 SkColorSpace::MakeSRGB(),
500 for (SkColorType dstCT : kColorTypes) {
501 for (SkAlphaType dstAT : kAlphaTypes) {
502 for (const sk_sp<SkColorSpace>& dstCS : kColorSpaces) {
503 for (SkColorType srcCT : kColorTypes) {
504 for (SkAlphaType srcAT : kAlphaTypes) {
505 for (const sk_sp<SkColorSpace>& srcCS : kColorSpaces) {
506 test_conversion(reporter,
507 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
508 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
517 DEF_TEST(ReadPixels_InvalidRowBytes, reporter) {
518 auto srcII = SkImageInfo::Make({10, 10}, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
519 auto surf = SkSurface::MakeRaster(srcII);
520 for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) {
521 auto colorType = static_cast<SkColorType>(ct);
522 size_t bpp = SkColorTypeBytesPerPixel(colorType);
526 auto dstII = srcII.makeColorType(colorType);
527 size_t badRowBytes = (surf->width() + 1)*bpp - 1;
528 auto storage = std::make_unique<char[]>(badRowBytes*surf->height());
529 REPORTER_ASSERT(reporter, !surf->readPixels(dstII, storage.get(), badRowBytes, 0, 0));