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