1/* 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 8#include "include/core/SkCanvas.h" 9#include "include/core/SkColorPriv.h" 10#include "include/core/SkImage.h" 11#include "include/core/SkSurface.h" 12#include "include/private/SkColorData.h" 13#include "include/private/SkHalf.h" 14#include "include/private/SkImageInfoPriv.h" 15#include "include/utils/SkNWayCanvas.h" 16#include "src/core/SkMathPriv.h" 17#include "tests/Test.h" 18 19static const int DEV_W = 100, DEV_H = 100; 20static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); 21static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1, 22 DEV_H * SK_Scalar1); 23 24static SkPMColor get_src_color(int x, int y) { 25 SkASSERT(x >= 0 && x < DEV_W); 26 SkASSERT(y >= 0 && y < DEV_H); 27 28 U8CPU r = x; 29 U8CPU g = y; 30 U8CPU b = 0xc; 31 32 U8CPU a = 0xff; 33 switch ((x+y) % 5) { 34 case 0: 35 a = 0xff; 36 break; 37 case 1: 38 a = 0x80; 39 break; 40 case 2: 41 a = 0xCC; 42 break; 43 case 4: 44 a = 0x01; 45 break; 46 case 3: 47 a = 0x00; 48 break; 49 } 50 return SkPremultiplyARGBInline(a, r, g, b); 51} 52 53static SkPMColor get_dst_bmp_init_color(int x, int y, int w) { 54 int n = y * w + x; 55 56 U8CPU b = n & 0xff; 57 U8CPU g = (n >> 8) & 0xff; 58 U8CPU r = (n >> 16) & 0xff; 59 return SkPackARGB32(0xff, r, g , b); 60} 61 62// TODO: Make this consider both ATs 63static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr, 64 bool* doUnpremul) { 65 *doUnpremul = (kUnpremul_SkAlphaType == at); 66 67 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr); 68 U8CPU a,r,g,b; 69 switch (ct) { 70 case kBGRA_8888_SkColorType: 71 b = static_cast<U8CPU>(c[0]); 72 g = static_cast<U8CPU>(c[1]); 73 r = static_cast<U8CPU>(c[2]); 74 a = static_cast<U8CPU>(c[3]); 75 break; 76 case kRGB_888x_SkColorType: // fallthrough 77 case kRGBA_8888_SkColorType: 78 r = static_cast<U8CPU>(c[0]); 79 g = static_cast<U8CPU>(c[1]); 80 b = static_cast<U8CPU>(c[2]); 81 // We set this even when for kRGB_888x because our caller will validate that it is 0xff. 82 a = static_cast<U8CPU>(c[3]); 83 break; 84 default: 85 SkDEBUGFAIL("Unexpected colortype"); 86 return 0; 87 } 88 89 if (*doUnpremul) { 90 r = SkMulDiv255Ceiling(r, a); 91 g = SkMulDiv255Ceiling(g, a); 92 b = SkMulDiv255Ceiling(b, a); 93 } 94 return SkPackARGB32(a, r, g, b); 95} 96 97static sk_sp<SkImage> make_src_image() { 98 static SkBitmap bmp; 99 if (bmp.isNull()) { 100 bmp.allocN32Pixels(DEV_W, DEV_H); 101 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); 102 for (int y = 0; y < DEV_H; ++y) { 103 for (int x = 0; x < DEV_W; ++x) { 104 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); 105 *pixel = get_src_color(x, y); 106 } 107 } 108 bmp.setImmutable(); 109 } 110 return bmp.asImage(); 111} 112 113static void fill_src_canvas(SkCanvas* canvas) { 114 canvas->save(); 115 canvas->setMatrix(SkMatrix::I()); 116 canvas->clipRect(DEV_RECT_S, SkClipOp::kIntersect); 117 SkPaint paint; 118 paint.setBlendMode(SkBlendMode::kSrc); 119 canvas->drawImage(make_src_image(), 0, 0, SkSamplingOptions(), &paint); 120 canvas->restore(); 121} 122 123static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) { 124 int w = bitmap->width(); 125 int h = bitmap->height(); 126 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); 127 for (int y = 0; y < h; ++y) { 128 for (int x = 0; x < w; ++x) { 129 SkPMColor initColor = get_dst_bmp_init_color(x, y, w); 130 if (kAlpha_8_SkColorType == bitmap->colorType()) { 131 uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x); 132 *alpha = SkGetPackedA32(initColor); 133 } else { 134 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); 135 *pixel = initColor; 136 } 137 } 138 } 139} 140 141static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { 142 if (!didPremulConversion) { 143 return a == b; 144 } 145 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); 146 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); 147 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); 148 int32_t aB = SkGetPackedB32(a); 149 150 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); 151 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); 152 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); 153 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); 154 155 return aA == bA && 156 SkAbs32(aR - bR) <= 1 && 157 SkAbs32(aG - bG) <= 1 && 158 SkAbs32(aB - bB) <= 1; 159} 160 161// checks the bitmap contains correct pixels after the readPixels 162// if the bitmap was prefilled with pixels it checks that these weren't 163// overwritten in the area outside the readPixels. 164static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y, 165 bool checkSurfacePixels, bool checkBitmapPixels, 166 SkImageInfo surfaceInfo) { 167 SkAlphaType bmpAT = bitmap.alphaType(); 168 SkColorType bmpCT = bitmap.colorType(); 169 SkASSERT(!bitmap.isNull()); 170 SkASSERT(checkSurfacePixels || checkBitmapPixels); 171 172 int bw = bitmap.width(); 173 int bh = bitmap.height(); 174 175 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); 176 SkIRect clippedSrcRect = DEV_RECT; 177 if (!clippedSrcRect.intersect(srcRect)) { 178 clippedSrcRect.setEmpty(); 179 } 180 if (kAlpha_8_SkColorType == bmpCT) { 181 for (int by = 0; by < bh; ++by) { 182 for (int bx = 0; bx < bw; ++bx) { 183 int devx = bx + srcRect.fLeft; 184 int devy = by + srcRect.fTop; 185 const uint8_t* alpha = bitmap.getAddr8(bx, by); 186 187 if (clippedSrcRect.contains(devx, devy)) { 188 if (checkSurfacePixels) { 189 uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType) 190 ? 0xFF 191 : SkGetPackedA32(get_src_color(devx, devy)); 192 if (surfaceAlpha != *alpha) { 193 ERRORF(reporter, 194 "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ", 195 bx, by, surfaceAlpha, *alpha); 196 return false; 197 } 198 } 199 } else if (checkBitmapPixels) { 200 uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw)); 201 if (origDstAlpha != *alpha) { 202 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " 203 "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha); 204 return false; 205 } 206 } 207 } 208 } 209 return true; 210 } 211 for (int by = 0; by < bh; ++by) { 212 for (int bx = 0; bx < bw; ++bx) { 213 int devx = bx + srcRect.fLeft; 214 int devy = by + srcRect.fTop; 215 216 const uint32_t* pixel = bitmap.getAddr32(bx, by); 217 218 if (clippedSrcRect.contains(devx, devy)) { 219 if (checkSurfacePixels) { 220 SkPMColor surfacePMColor = get_src_color(devx, devy); 221 if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) { 222 surfacePMColor &= 0xFF000000; 223 } 224 if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) { 225 surfacePMColor |= 0xFF000000; 226 } 227 bool didPremul; 228 SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul); 229 if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) { 230 ERRORF(reporter, 231 "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. " 232 "Readback was unpremul: %d", 233 bx, by, surfacePMColor, pmPixel, didPremul); 234 return false; 235 } 236 } 237 } else if (checkBitmapPixels) { 238 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw); 239 if (origDstPixel != *pixel) { 240 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " 241 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel); 242 return false; 243 } 244 } 245 } 246 } 247 return true; 248} 249 250enum class TightRowBytes : bool { kNo, kYes }; 251 252static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB, 253 SkColorType ct, SkAlphaType at) { 254 SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at); 255 size_t rowBytes = 0; 256 if (tightRB == TightRowBytes::kNo) { 257 rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel()); 258 } 259 bitmap->allocPixels(info, rowBytes); 260} 261 262static const struct { 263 SkColorType fColorType; 264 SkAlphaType fAlphaType; 265} gReadPixelsConfigs[] = { 266 {kRGBA_8888_SkColorType, kPremul_SkAlphaType}, 267 {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType}, 268 {kRGB_888x_SkColorType, kOpaque_SkAlphaType}, 269 {kBGRA_8888_SkColorType, kPremul_SkAlphaType}, 270 {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType}, 271 {kAlpha_8_SkColorType, kPremul_SkAlphaType}, 272}; 273const SkIRect gReadPixelsTestRects[] = { 274 // entire thing 275 DEV_RECT, 276 // larger on all sides 277 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), 278 // fully contained 279 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), 280 // outside top left 281 SkIRect::MakeLTRB(-10, -10, -1, -1), 282 // touching top left corner 283 SkIRect::MakeLTRB(-10, -10, 0, 0), 284 // overlapping top left corner 285 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), 286 // overlapping top left and top right corners 287 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), 288 // touching entire top edge 289 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), 290 // overlapping top right corner 291 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), 292 // contained in x, overlapping top edge 293 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), 294 // outside top right corner 295 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), 296 // touching top right corner 297 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), 298 // overlapping top left and bottom left corners 299 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), 300 // touching entire left edge 301 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), 302 // overlapping bottom left corner 303 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), 304 // contained in y, overlapping left edge 305 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), 306 // outside bottom left corner 307 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), 308 // touching bottom left corner 309 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), 310 // overlapping bottom left and bottom right corners 311 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), 312 // touching entire left edge 313 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), 314 // overlapping bottom right corner 315 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), 316 // overlapping top right and bottom right corners 317 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), 318}; 319 320bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo, 321 const SkImageInfo& srcInfo) { 322 return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo); 323} 324 325static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, 326 const SkImageInfo& surfaceInfo) { 327 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]; 331 for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) { 332 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { 333 SkBitmap bmp; 334 init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType, 335 gReadPixelsConfigs[c].fAlphaType); 336 337 // 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 = surface->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 out and the infos are 348 // compatible. 349 bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo); 350 // determine whether we expected the read to succeed. 351 REPORTER_ASSERT(reporter, expectSuccess == success, 352 "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d", 353 success, surfaceInfo.colorType(), surfaceInfo.alphaType(), 354 bmp.info().colorType(), bmp.info().alphaType()); 355 // read pixels should never change the gen id 356 REPORTER_ASSERT(reporter, idBefore == idAfter); 357 358 if (success || startsWithPixels) { 359 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, 360 startsWithPixels, surfaceInfo); 361 } else { 362 // if we had no pixels beforehand and the readPixels 363 // failed then our bitmap should still not have pixels 364 REPORTER_ASSERT(reporter, bmp.isNull()); 365 } 366 } 367 } 368 } 369} 370 371DEF_TEST(ReadPixels, reporter) { 372 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); 373 auto surface(SkSurface::MakeRaster(info)); 374 test_readpixels(reporter, surface, info); 375} 376 377/////////////////////////////////////////////////////////////////////////////////////////////////// 378 379static const uint32_t kNumPixels = 5; 380 381// The five reference pixels are: red, green, blue, white, black. 382// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector 383// plus a tail pixel. 384static const uint32_t rgba[kNumPixels] = { 385 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000 386}; 387static const uint32_t bgra[kNumPixels] = { 388 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000 389}; 390static const uint16_t rgb565[kNumPixels] = { 391 SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0 392}; 393 394static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F }; 395 396static const uint64_t kRed = (uint64_t) SK_Half1 << 0; 397static const uint64_t kGreen = (uint64_t) SK_Half1 << 16; 398static const uint64_t kBlue = (uint64_t) SK_Half1 << 32; 399static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48; 400static const uint64_t f16[kNumPixels] = { 401 kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha 402}; 403 404static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 405static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 406 407static const void* five_reference_pixels(SkColorType colorType) { 408 switch (colorType) { 409 case kUnknown_SkColorType: 410 return nullptr; 411 case kAlpha_8_SkColorType: 412 return alpha8; 413 case kRGB_565_SkColorType: 414 return rgb565; 415 case kARGB_4444_SkColorType: 416 return rgba4444; 417 case kRGBA_8888_SkColorType: 418 return rgba; 419 case kBGRA_8888_SkColorType: 420 return bgra; 421 case kGray_8_SkColorType: 422 return gray8; 423 case kRGBA_F16_SkColorType: 424 return f16; 425 default: 426 return nullptr; 427 } 428 429 SkASSERT(false); 430 return nullptr; 431} 432 433static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo, 434 const SkImageInfo& srcInfo) { 435 if (!SkImageInfoIsValid(srcInfo)) { 436 return; 437 } 438 439 const void* srcPixels = five_reference_pixels(srcInfo.colorType()); 440 SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes()); 441 sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr); 442 REPORTER_ASSERT(r, src); 443 444 // Enough space for 5 pixels when color type is F16, more than enough space in other cases. 445 uint64_t dstPixels[kNumPixels]; 446 SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes()); 447 bool success = src->readPixels(nullptr, dstPixmap, 0, 0); 448 REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo)); 449 450 if (success) { 451 if (kGray_8_SkColorType == srcInfo.colorType() && 452 kGray_8_SkColorType != dstInfo.colorType()) { 453 // TODO: test (r,g,b) == (gray,gray,gray)? 454 return; 455 } 456 457 if (kGray_8_SkColorType == dstInfo.colorType() && 458 kGray_8_SkColorType != srcInfo.colorType()) { 459 // TODO: test gray = luminance? 460 return; 461 } 462 463 if (kAlpha_8_SkColorType == srcInfo.colorType() && 464 kAlpha_8_SkColorType != dstInfo.colorType()) { 465 // TODO: test output = black with this alpha? 466 return; 467 } 468 469 REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()), 470 kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType()))); 471 } 472} 473 474DEF_TEST(ReadPixels_ValidConversion, reporter) { 475 const SkColorType kColorTypes[] = { 476 kUnknown_SkColorType, 477 kAlpha_8_SkColorType, 478 kRGB_565_SkColorType, 479 kARGB_4444_SkColorType, 480 kRGBA_8888_SkColorType, 481 kBGRA_8888_SkColorType, 482 kGray_8_SkColorType, 483 kRGBA_F16_SkColorType, 484 }; 485 486 const SkAlphaType kAlphaTypes[] = { 487 kUnknown_SkAlphaType, 488 kOpaque_SkAlphaType, 489 kPremul_SkAlphaType, 490 kUnpremul_SkAlphaType, 491 }; 492 493 const sk_sp<SkColorSpace> kColorSpaces[] = { 494 nullptr, 495 SkColorSpace::MakeSRGB(), 496 }; 497 498 for (SkColorType dstCT : kColorTypes) { 499 for (SkAlphaType dstAT : kAlphaTypes) { 500 for (const sk_sp<SkColorSpace>& dstCS : kColorSpaces) { 501 for (SkColorType srcCT : kColorTypes) { 502 for (SkAlphaType srcAT : kAlphaTypes) { 503 for (const sk_sp<SkColorSpace>& srcCS : kColorSpaces) { 504 test_conversion(reporter, 505 SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS), 506 SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS)); 507 } 508 } 509 } 510 } 511 } 512 } 513} 514 515DEF_TEST(ReadPixels_InvalidRowBytes, reporter) { 516 auto srcII = SkImageInfo::Make({10, 10}, kRGBA_8888_SkColorType, kPremul_SkAlphaType); 517 auto surf = SkSurface::MakeRaster(srcII); 518 for (int ct = 0; ct < kLastEnum_SkColorType + 1; ++ct) { 519 auto colorType = static_cast<SkColorType>(ct); 520 size_t bpp = SkColorTypeBytesPerPixel(colorType); 521 if (bpp <= 1) { 522 continue; 523 } 524 auto dstII = srcII.makeColorType(colorType); 525 size_t badRowBytes = (surf->width() + 1)*bpp - 1; 526 auto storage = std::make_unique<char[]>(badRowBytes*surf->height()); 527 REPORTER_ASSERT(reporter, !surf->readPixels(dstII, storage.get(), badRowBytes, 0, 0)); 528 } 529} 530