1c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com/* 2c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com * Copyright 2011 Google Inc. 3c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com * 4c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com * Use of this source code is governed by a BSD-style license that can be 5c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com * found in the LICENSE file. 6c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com */ 7c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 8c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com#include "SkCanvas.h" 9a4083c97d48e8a4f88e2797d7363f141e3d42553Cary Clark#include "SkColorData.h" 108572d853514e3c73077540341edbf62a3f486605Matt Sarett#include "SkHalf.h" 118572d853514e3c73077540341edbf62a3f486605Matt Sarett#include "SkImageInfoPriv.h" 124b163ed2c22facbe8891616874ae07ba7827d9c9reed@google.com#include "SkMathPriv.h" 1352d9ac6c92ddf33b3b05eb77ba9509a7aa441657reed#include "SkSurface.h" 144ee16bfaedb14aff8cf102f1f0722ff2529a9699tfarina@chromium.org#include "Test.h" 154ee16bfaedb14aff8cf102f1f0722ff2529a9699tfarina@chromium.org 16cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU 171530283c483cb88aa725bce50a6d193dd00ee570kkinnunen#include "GrContext.h" 18eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips#include "GrContextFactory.h" 19e78b7259c3d5cbed77b4390150cfb699b0b59cd4Robert Phillips#include "GrContextPriv.h" 200bd24dc41f2351d84f5fce32130668a5792d134bRobert Phillips#include "GrProxyProvider.h" 21e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon#include "SkGr.h" 22cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif 23c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 241530283c483cb88aa725bce50a6d193dd00ee570kkinnunen#include <initializer_list> 251530283c483cb88aa725bce50a6d193dd00ee570kkinnunen 26c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.comstatic const int DEV_W = 100, DEV_H = 100; 27c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.comstatic const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H); 28d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.comstatic const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1, 29c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com DEV_H * SK_Scalar1); 30c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 31e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic SkPMColor get_src_color(int x, int y) { 32c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkASSERT(x >= 0 && x < DEV_W); 33c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkASSERT(y >= 0 && y < DEV_H); 346850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com 356850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com U8CPU r = x; 366850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com U8CPU g = y; 376850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com U8CPU b = 0xc; 386850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com 396850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com U8CPU a = 0xff; 40c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com switch ((x+y) % 5) { 416850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com case 0: 426850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com a = 0xff; 436850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com break; 446850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com case 1: 456850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com a = 0x80; 466850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com break; 476850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com case 2: 486850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com a = 0xCC; 496850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com break; 506850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com case 4: 516850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com a = 0x01; 526850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com break; 536850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com case 3: 546850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com a = 0x00; 556850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com break; 566850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com } 576850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com return SkPremultiplyARGBInline(a, r, g, b); 58c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 599d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary 60e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic SkPMColor get_dst_bmp_init_color(int x, int y, int w) { 61c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int n = y * w + x; 626850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com 63c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com U8CPU b = n & 0xff; 64c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com U8CPU g = (n >> 8) & 0xff; 65c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com U8CPU r = (n >> 16) & 0xff; 66c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com return SkPackARGB32(0xff, r, g , b); 67c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 68c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 69e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr, 70e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon bool* doUnpremul) { 71a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org *doUnpremul = (kUnpremul_SkAlphaType == at); 72a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org 73a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org const uint8_t* c = reinterpret_cast<const uint8_t*>(addr); 746850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com U8CPU a,r,g,b; 75a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org switch (ct) { 76a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org case kBGRA_8888_SkColorType: 776850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com b = static_cast<U8CPU>(c[0]); 78a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org g = static_cast<U8CPU>(c[1]); 79a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org r = static_cast<U8CPU>(c[2]); 806850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com a = static_cast<U8CPU>(c[3]); 81a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org break; 82a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org case kRGBA_8888_SkColorType: 836850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com r = static_cast<U8CPU>(c[0]); 846850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com g = static_cast<U8CPU>(c[1]); 856850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com b = static_cast<U8CPU>(c[2]); 86a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org a = static_cast<U8CPU>(c[3]); 876850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com break; 88ccaa002dd81a6a8bd5acb7a2fa69a2437873c1fdbsalomon@google.com default: 89a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org SkDEBUGFAIL("Unexpected colortype"); 90ccaa002dd81a6a8bd5acb7a2fa69a2437873c1fdbsalomon@google.com return 0; 916850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com } 92a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org 93a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org if (*doUnpremul) { 946850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com r = SkMulDiv255Ceiling(r, a); 956850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com g = SkMulDiv255Ceiling(g, a); 966850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com b = SkMulDiv255Ceiling(b, a); 976850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com } 986850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com return SkPackARGB32(a, r, g, b); 996850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com} 1006850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com 101e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic SkBitmap make_src_bitmap() { 102c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com static SkBitmap bmp; 103c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com if (bmp.isNull()) { 104848250415eddc54075f7eb8795e8db79e749c6abreed bmp.allocN32Pixels(DEV_W, DEV_H); 105c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); 106c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com for (int y = 0; y < DEV_H; ++y) { 107c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com for (int x = 0; x < DEV_W; ++x) { 108c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel()); 109e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon *pixel = get_src_color(x, y); 110c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 111c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 112c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 113e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon return bmp; 114e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon} 115e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon 116e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic void fill_src_canvas(SkCanvas* canvas) { 117c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com canvas->save(); 118c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com canvas->setMatrix(SkMatrix::I()); 119c1f7774e8d327e3c98b4094c9c01d26e27013f71Mike Reed canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp); 120c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkPaint paint; 121374772bd61951f01bf84fe17bf53d8867681c9aereed paint.setBlendMode(SkBlendMode::kSrc); 122e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint); 123c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com canvas->restore(); 124c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 125d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com 126e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic void fill_dst_bmp_with_init_data(SkBitmap* bitmap) { 127c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int w = bitmap->width(); 128c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int h = bitmap->height(); 129c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); 130c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com for (int y = 0; y < h; ++y) { 131c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com for (int x = 0; x < w; ++x) { 132a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman SkPMColor initColor = get_dst_bmp_init_color(x, y, w); 133a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman if (kAlpha_8_SkColorType == bitmap->colorType()) { 134a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x); 135a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman *alpha = SkGetPackedA32(initColor); 136a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } else { 137a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel()); 138a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman *pixel = initColor; 139a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 140c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 141c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 142c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 143c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 144e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { 145c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com if (!didPremulConversion) { 146c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com return a == b; 147c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com } 148c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t aA = static_cast<int32_t>(SkGetPackedA32(a)); 149c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t aR = static_cast<int32_t>(SkGetPackedR32(a)); 150c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t aG = static_cast<int32_t>(SkGetPackedG32(a)); 151c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t aB = SkGetPackedB32(a); 152c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com 153c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); 154c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); 155c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); 156c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); 157c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com 158c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com return aA == bA && 159c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com SkAbs32(aR - bR) <= 1 && 160c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com SkAbs32(aG - bG) <= 1 && 161c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com SkAbs32(aB - bB) <= 1; 162c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com} 163c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com 164c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com// checks the bitmap contains correct pixels after the readPixels 165c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com// if the bitmap was prefilled with pixels it checks that these weren't 166c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com// overwritten in the area outside the readPixels. 167e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomonstatic bool check_read(skiatest::Reporter* reporter, 168e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon const SkBitmap& bitmap, 169e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon int x, int y, 170e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon bool checkCanvasPixels, 171a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman bool checkBitmapPixels, 172a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman SkColorType ct, 173a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman SkAlphaType at) { 174a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType()); 175c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkASSERT(!bitmap.isNull()); 1766850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com SkASSERT(checkCanvasPixels || checkBitmapPixels); 177d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com 178c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int bw = bitmap.width(); 179c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int bh = bitmap.height(); 180c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 181c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh); 182c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkIRect clippedSrcRect = DEV_RECT; 183c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com if (!clippedSrcRect.intersect(srcRect)) { 184c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com clippedSrcRect.setEmpty(); 185c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 186a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman if (kAlpha_8_SkColorType == ct) { 187a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman for (int by = 0; by < bh; ++by) { 188a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman for (int bx = 0; bx < bw; ++bx) { 189a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman int devx = bx + srcRect.fLeft; 190a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman int devy = by + srcRect.fTop; 191a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman const uint8_t* alpha = bitmap.getAddr8(bx, by); 192a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman 193a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman if (clippedSrcRect.contains(devx, devy)) { 194a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman if (checkCanvasPixels) { 195a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy)); 196a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman if (canvasAlpha != *alpha) { 197a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ", 198a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman bx, by, canvasAlpha, *alpha); 199a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman return false; 200a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 201a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 202a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } else if (checkBitmapPixels) { 203a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw)); 204a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman if (origDstAlpha != *alpha) { 205a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " 206a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha); 207a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman return false; 208a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 209a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 210a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 211a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 212a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman return true; 213a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman } 214c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com for (int by = 0; by < bh; ++by) { 215c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com for (int bx = 0; bx < bw; ++bx) { 216c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int devx = bx + srcRect.fLeft; 217c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int devy = by + srcRect.fTop; 218d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com 219a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org const uint32_t* pixel = bitmap.getAddr32(bx, by); 220c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 221c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com if (clippedSrcRect.contains(devx, devy)) { 2226850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com if (checkCanvasPixels) { 223e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon SkPMColor canvasPixel = get_src_color(devx, devy); 224c43649962221c348d656d425a3fa9b29c78231d4bsalomon@google.com bool didPremul; 225e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul); 226398260262f8508687614ec496a23b8152bcdc967bsalomon if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) { 22788e8aef3916454e5f6916cc8b3420345b1cf0584egdaniel ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. " 22888e8aef3916454e5f6916cc8b3420345b1cf0584egdaniel "Readback was unpremul: %d", bx, by, canvasPixel, pmPixel, didPremul); 22972f3dca451f4739e20be0b4b198813677339205cbsalomon@google.com return false; 2306850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com } 231c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 2326850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com } else if (checkBitmapPixels) { 233398260262f8508687614ec496a23b8152bcdc967bsalomon uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw); 234398260262f8508687614ec496a23b8152bcdc967bsalomon if (origDstPixel != *pixel) { 235398260262f8508687614ec496a23b8152bcdc967bsalomon ERRORF(reporter, "Expected clipped out area of readback to be unchanged. " 236398260262f8508687614ec496a23b8152bcdc967bsalomon "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel); 23772f3dca451f4739e20be0b4b198813677339205cbsalomon@google.com return false; 238c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 239c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 240c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 241c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 24272f3dca451f4739e20be0b4b198813677339205cbsalomon@google.com return true; 243c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 244c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 245c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.comenum BitmapInit { 246c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com kFirstBitmapInit = 0, 247d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com 24812e946b4bfdf598bffb276776ea6e25439e25265Mike Reed kTight_BitmapInit = kFirstBitmapInit, 249c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com kRowBytes_BitmapInit, 2509d02b264b72be64702e43cb797b7899a8a6926cabsalomon kRowBytesOdd_BitmapInit, 251d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com 2529d02b264b72be64702e43cb797b7899a8a6926cabsalomon kLastAligned_BitmapInit = kRowBytes_BitmapInit, 253a64afd6680b493dce3537fe56b249907d41caf36Brian Salomon 254a64afd6680b493dce3537fe56b249907d41caf36Brian Salomon#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES 2559d02b264b72be64702e43cb797b7899a8a6926cabsalomon kLast_BitmapInit = kRowBytesOdd_BitmapInit 256a64afd6680b493dce3537fe56b249907d41caf36Brian Salomon#else 257a64afd6680b493dce3537fe56b249907d41caf36Brian Salomon kLast_BitmapInit = kLastAligned_BitmapInit 258a64afd6680b493dce3537fe56b249907d41caf36Brian Salomon#endif 259c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com}; 260c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 261ddf94cf108ae430877f009bd67b9070341426947commit-bot@chromium.orgstatic BitmapInit nextBMI(BitmapInit bmi) { 262c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com int x = bmi; 263c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com return static_cast<BitmapInit>(++x); 264c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 265c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 266a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.orgstatic void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct, 267a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org SkAlphaType at) { 268a713f9c6f6a06d216d53e268b9c691941053dabfcommit-bot@chromium.org SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at); 269fa9e5fa42a555712fb7a29d08d2ae2bdef0ed68ecommit-bot@chromium.org size_t rowBytes = 0; 270c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com switch (init) { 271c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com case kTight_BitmapInit: 272c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com break; 273c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com case kRowBytes_BitmapInit: 274a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel()); 275c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com break; 2769d02b264b72be64702e43cb797b7899a8a6926cabsalomon case kRowBytesOdd_BitmapInit: 277a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3; 2789d02b264b72be64702e43cb797b7899a8a6926cabsalomon break; 279c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com default: 280c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com SkASSERT(0); 281c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com break; 282c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 28312e946b4bfdf598bffb276776ea6e25439e25265Mike Reed bitmap->allocPixels(info, rowBytes); 284c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 285c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 2861530283c483cb88aa725bce50a6d193dd00ee570kkinnunenstatic const struct { 2871530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkColorType fColorType; 2881530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkAlphaType fAlphaType; 2891530283c483cb88aa725bce50a6d193dd00ee570kkinnunen} gReadPixelsConfigs[] = { 2901530283c483cb88aa725bce50a6d193dd00ee570kkinnunen { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, 2911530283c483cb88aa725bce50a6d193dd00ee570kkinnunen { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, 2921530283c483cb88aa725bce50a6d193dd00ee570kkinnunen { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, 2931530283c483cb88aa725bce50a6d193dd00ee570kkinnunen { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, 294a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman { kAlpha_8_SkColorType, kPremul_SkAlphaType }, 2951530283c483cb88aa725bce50a6d193dd00ee570kkinnunen}; 2961530283c483cb88aa725bce50a6d193dd00ee570kkinnunenconst SkIRect gReadPixelsTestRects[] = { 2971530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // entire thing 2981530283c483cb88aa725bce50a6d193dd00ee570kkinnunen DEV_RECT, 2991530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // larger on all sides 3001530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10), 3011530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // fully contained 3021530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4), 3031530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // outside top left 3041530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, -1, -1), 3051530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // touching top left corner 3061530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, 0, 0), 3071530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping top left corner 3081530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4), 3091530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping top left and top right corners 3101530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4), 3111530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // touching entire top edge 3121530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0), 3131530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping top right corner 3141530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4), 3151530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // contained in x, overlapping top edge 3161530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4), 3171530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // outside top right corner 3181530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1), 3191530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // touching top right corner 3201530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0), 3211530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping top left and bottom left corners 3221530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10), 3231530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // touching entire left edge 3241530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10), 3251530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping bottom left corner 3261530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10), 3271530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // contained in y, overlapping left edge 3281530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4), 3291530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // outside bottom left corner 3301530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10), 3311530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // touching bottom left corner 3321530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10), 3331530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping bottom left and bottom right corners 3341530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), 3351530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // touching entire left edge 3361530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10), 3371530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping bottom right corner 3381530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10), 3391530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // overlapping top right and bottom right corners 3401530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10), 3411530283c483cb88aa725bce50a6d193dd00ee570kkinnunen}; 342e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon 343e8f3062a36d3682f4019309a32b5b84dc9eddf8creedstatic void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, 3449d02b264b72be64702e43cb797b7899a8a6926cabsalomon BitmapInit lastBitmapInit) { 3451530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkCanvas* canvas = surface->getCanvas(); 3461530283c483cb88aa725bce50a6d193dd00ee570kkinnunen fill_src_canvas(canvas); 3471530283c483cb88aa725bce50a6d193dd00ee570kkinnunen for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { 3481530283c483cb88aa725bce50a6d193dd00ee570kkinnunen const SkIRect& srcRect = gReadPixelsTestRects[rect]; 3499d02b264b72be64702e43cb797b7899a8a6926cabsalomon for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) { 3501530283c483cb88aa725bce50a6d193dd00ee570kkinnunen for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { 3511530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkBitmap bmp; 3521530283c483cb88aa725bce50a6d193dd00ee570kkinnunen init_bitmap(&bmp, srcRect, bmi, 3531530283c483cb88aa725bce50a6d193dd00ee570kkinnunen gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); 3541530283c483cb88aa725bce50a6d193dd00ee570kkinnunen 3551530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // if the bitmap has pixels allocated before the readPixels, 3561530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // note that and fill them with pattern 3571530283c483cb88aa725bce50a6d193dd00ee570kkinnunen bool startsWithPixels = !bmp.isNull(); 3581530283c483cb88aa725bce50a6d193dd00ee570kkinnunen if (startsWithPixels) { 3591530283c483cb88aa725bce50a6d193dd00ee570kkinnunen fill_dst_bmp_with_init_data(&bmp); 36067b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com } 3611530283c483cb88aa725bce50a6d193dd00ee570kkinnunen uint32_t idBefore = surface->generationID(); 362f19421961c59ceabfb88a5c5cdf7c7e31a95b015Mike Reed bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop); 3631530283c483cb88aa725bce50a6d193dd00ee570kkinnunen uint32_t idAfter = surface->generationID(); 3641530283c483cb88aa725bce50a6d193dd00ee570kkinnunen 3651530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // we expect to succeed when the read isn't fully clipped 3661530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // out. 3671530283c483cb88aa725bce50a6d193dd00ee570kkinnunen bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT); 3681530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // determine whether we expected the read to succeed. 3691530283c483cb88aa725bce50a6d193dd00ee570kkinnunen REPORTER_ASSERT(reporter, success == expectSuccess); 3701530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // read pixels should never change the gen id 3711530283c483cb88aa725bce50a6d193dd00ee570kkinnunen REPORTER_ASSERT(reporter, idBefore == idAfter); 3721530283c483cb88aa725bce50a6d193dd00ee570kkinnunen 3731530283c483cb88aa725bce50a6d193dd00ee570kkinnunen if (success || startsWithPixels) { 3741530283c483cb88aa725bce50a6d193dd00ee570kkinnunen check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, 375a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman success, startsWithPixels, 376a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); 3771530283c483cb88aa725bce50a6d193dd00ee570kkinnunen } else { 3781530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // if we had no pixels beforehand and the readPixels 3791530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // failed then our bitmap should still not have pixels 3801530283c483cb88aa725bce50a6d193dd00ee570kkinnunen REPORTER_ASSERT(reporter, bmp.isNull()); 38167b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com } 38267b915de99e6b89d476907930ac8c27afb64d10ebsalomon@google.com } 3831530283c483cb88aa725bce50a6d193dd00ee570kkinnunen } 3841530283c483cb88aa725bce50a6d193dd00ee570kkinnunen } 3851530283c483cb88aa725bce50a6d193dd00ee570kkinnunen} 3861530283c483cb88aa725bce50a6d193dd00ee570kkinnunenDEF_TEST(ReadPixels, reporter) { 3871530283c483cb88aa725bce50a6d193dd00ee570kkinnunen const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); 388e8f3062a36d3682f4019309a32b5b84dc9eddf8creed auto surface(SkSurface::MakeRaster(info)); 3899d02b264b72be64702e43cb797b7899a8a6926cabsalomon // SW readback fails a premul check when reading back to an unaligned rowbytes. 3909d02b264b72be64702e43cb797b7899a8a6926cabsalomon test_readpixels(reporter, surface, kLastAligned_BitmapInit); 3911530283c483cb88aa725bce50a6d193dd00ee570kkinnunen} 3921530283c483cb88aa725bce50a6d193dd00ee570kkinnunen#if SK_SUPPORT_GPU 39388e8aef3916454e5f6916cc8b3420345b1cf0584egdanielDEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) { 394eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType || 395eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType || 396eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) { 397eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs 398eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips return; 399eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips } 400eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips 4017e922765545f42ce691e4f3d5fbbd4e44ba47ff1robertphillips const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); 4021530283c483cb88aa725bce50a6d193dd00ee570kkinnunen for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { 4037e922765545f42ce691e4f3d5fbbd4e44ba47ff1robertphillips sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, 4047e922765545f42ce691e4f3d5fbbd4e44ba47ff1robertphillips ii, 0, origin, nullptr)); 4059d02b264b72be64702e43cb797b7899a8a6926cabsalomon test_readpixels(reporter, surface, kLast_BitmapInit); 4061530283c483cb88aa725bce50a6d193dd00ee570kkinnunen } 4071530283c483cb88aa725bce50a6d193dd00ee570kkinnunen} 408e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon#endif 409c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com 410e8d21e8f24aa676765d0ff8f433228665c75cdc2bsalomon#if SK_SUPPORT_GPU 411e78b7259c3d5cbed77b4390150cfb699b0b59cd4Robert Phillipsstatic void test_readpixels_texture(skiatest::Reporter* reporter, 412bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips sk_sp<GrSurfaceContext> sContext) { 4131530283c483cb88aa725bce50a6d193dd00ee570kkinnunen for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) { 4141530283c483cb88aa725bce50a6d193dd00ee570kkinnunen const SkIRect& srcRect = gReadPixelsTestRects[rect]; 4159d02b264b72be64702e43cb797b7899a8a6926cabsalomon for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) { 4161530283c483cb88aa725bce50a6d193dd00ee570kkinnunen for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) { 4171530283c483cb88aa725bce50a6d193dd00ee570kkinnunen SkBitmap bmp; 4181530283c483cb88aa725bce50a6d193dd00ee570kkinnunen init_bitmap(&bmp, srcRect, bmi, 4191530283c483cb88aa725bce50a6d193dd00ee570kkinnunen gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); 4201530283c483cb88aa725bce50a6d193dd00ee570kkinnunen 4211530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // if the bitmap has pixels allocated before the readPixels, 4221530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // note that and fill them with pattern 4231530283c483cb88aa725bce50a6d193dd00ee570kkinnunen bool startsWithPixels = !bmp.isNull(); 4241530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // Try doing the read directly from a non-renderable texture 4251530283c483cb88aa725bce50a6d193dd00ee570kkinnunen if (startsWithPixels) { 4261530283c483cb88aa725bce50a6d193dd00ee570kkinnunen fill_dst_bmp_with_init_data(&bmp); 4271530283c483cb88aa725bce50a6d193dd00ee570kkinnunen uint32_t flags = 0; 4281530283c483cb88aa725bce50a6d193dd00ee570kkinnunen if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) { 429e78b7259c3d5cbed77b4390150cfb699b0b59cd4Robert Phillips flags = GrContextPriv::kUnpremul_PixelOpsFlag; 4306850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com } 431bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips bool success = sContext->readPixels(bmp.info(), bmp.getPixels(), 432bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips bmp.rowBytes(), 433bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips srcRect.fLeft, srcRect.fTop, flags); 4341530283c483cb88aa725bce50a6d193dd00ee570kkinnunen check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, 435a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman success, true, 436a2415accf1f184c4df6cffc2ccdd38f8d61031f6lsalzman gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType); 4376850eab42ba4c2a7033a99824b02a2846ce0ef2absalomon@google.com } 438c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 439c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 440c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com } 441c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.com} 442bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips 44388e8aef3916454e5f6916cc8b3420345b1cf0584egdanielDEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) { 444eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType || 445eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType || 446eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) { 447eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips // skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs 448eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips return; 449eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips } 450eb4f186779cf2f4fd778ba35e63cad539cd0c2aeRobert Phillips 451bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips GrContext* context = ctxInfo.grContext(); 4521afd4cdb0800e2e395b465da24eb71e0e834dafaRobert Phillips GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); 453bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips 454bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips SkBitmap bmp = make_src_bitmap(); 455bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips 4561530283c483cb88aa725bce50a6d193dd00ee570kkinnunen // On the GPU we will also try reading back from a non-renderable texture. 4578b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) { 4588b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) { 4598b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon GrSurfaceDesc desc; 4608b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon desc.fFlags = flags; 4618b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon desc.fWidth = DEV_W; 4628b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon desc.fHeight = DEV_H; 4638b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon desc.fConfig = kSkia8888_GrPixelConfig; 4648b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon desc.fOrigin = origin; 465bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips 4660bd24dc41f2351d84f5fce32130668a5792d134bRobert Phillips sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo, 4670bd24dc41f2351d84f5fce32130668a5792d134bRobert Phillips bmp.getPixels(), 4680bd24dc41f2351d84f5fce32130668a5792d134bRobert Phillips bmp.rowBytes()); 469bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips 470bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext( 471d5f9cdd4b3a6ab0e49bb1a56f2e52e2f40edd0faRobert Phillips std::move(proxy)); 472bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips 473bab2dbb5fa50da8e8365abbaa9f1490e659f436cRobert Phillips test_readpixels_texture(reporter, std::move(sContext)); 4748b1fb741f196b78479b9d5bb2b33875aaa9709bbBrian Salomon } 4751530283c483cb88aa725bce50a6d193dd00ee570kkinnunen } 4761530283c483cb88aa725bce50a6d193dd00ee570kkinnunen} 4771530283c483cb88aa725bce50a6d193dd00ee570kkinnunen#endif 4788572d853514e3c73077540341edbf62a3f486605Matt Sarett 4798572d853514e3c73077540341edbf62a3f486605Matt Sarett/////////////////////////////////////////////////////////////////////////////////////////////////// 4808572d853514e3c73077540341edbf62a3f486605Matt Sarett 4818572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint32_t kNumPixels = 5; 4828572d853514e3c73077540341edbf62a3f486605Matt Sarett 4838572d853514e3c73077540341edbf62a3f486605Matt Sarett// The five reference pixels are: red, green, blue, white, black. 4848572d853514e3c73077540341edbf62a3f486605Matt Sarett// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector 4858572d853514e3c73077540341edbf62a3f486605Matt Sarett// plus a tail pixel. 4868572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint32_t rgba[kNumPixels] = { 4878572d853514e3c73077540341edbf62a3f486605Matt Sarett 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000 4888572d853514e3c73077540341edbf62a3f486605Matt Sarett}; 4898572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint32_t bgra[kNumPixels] = { 4908572d853514e3c73077540341edbf62a3f486605Matt Sarett 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000 4918572d853514e3c73077540341edbf62a3f486605Matt Sarett}; 4928572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint16_t rgb565[kNumPixels] = { 4938572d853514e3c73077540341edbf62a3f486605Matt Sarett SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0 4948572d853514e3c73077540341edbf62a3f486605Matt Sarett}; 4958572d853514e3c73077540341edbf62a3f486605Matt Sarett 4968572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F }; 4978572d853514e3c73077540341edbf62a3f486605Matt Sarett 4988572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint64_t kRed = (uint64_t) SK_Half1 << 0; 4998572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint64_t kGreen = (uint64_t) SK_Half1 << 16; 5008572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint64_t kBlue = (uint64_t) SK_Half1 << 32; 5018572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint64_t kAlpha = (uint64_t) SK_Half1 << 48; 5028572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint64_t f16[kNumPixels] = { 5038572d853514e3c73077540341edbf62a3f486605Matt Sarett kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha 5048572d853514e3c73077540341edbf62a3f486605Matt Sarett}; 5058572d853514e3c73077540341edbf62a3f486605Matt Sarett 5068572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 5078572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 5088572d853514e3c73077540341edbf62a3f486605Matt Sarett 5098572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic const void* five_reference_pixels(SkColorType colorType) { 5108572d853514e3c73077540341edbf62a3f486605Matt Sarett switch (colorType) { 5118572d853514e3c73077540341edbf62a3f486605Matt Sarett case kUnknown_SkColorType: 5128572d853514e3c73077540341edbf62a3f486605Matt Sarett return nullptr; 5138572d853514e3c73077540341edbf62a3f486605Matt Sarett case kAlpha_8_SkColorType: 5148572d853514e3c73077540341edbf62a3f486605Matt Sarett return alpha8; 5158572d853514e3c73077540341edbf62a3f486605Matt Sarett case kRGB_565_SkColorType: 5168572d853514e3c73077540341edbf62a3f486605Matt Sarett return rgb565; 5178572d853514e3c73077540341edbf62a3f486605Matt Sarett case kARGB_4444_SkColorType: 5188572d853514e3c73077540341edbf62a3f486605Matt Sarett return rgba4444; 5198572d853514e3c73077540341edbf62a3f486605Matt Sarett case kRGBA_8888_SkColorType: 5208572d853514e3c73077540341edbf62a3f486605Matt Sarett return rgba; 5218572d853514e3c73077540341edbf62a3f486605Matt Sarett case kBGRA_8888_SkColorType: 5228572d853514e3c73077540341edbf62a3f486605Matt Sarett return bgra; 5238572d853514e3c73077540341edbf62a3f486605Matt Sarett case kGray_8_SkColorType: 5248572d853514e3c73077540341edbf62a3f486605Matt Sarett return gray8; 5258572d853514e3c73077540341edbf62a3f486605Matt Sarett case kRGBA_F16_SkColorType: 5268572d853514e3c73077540341edbf62a3f486605Matt Sarett return f16; 527304a07c6c93298c3481bc0d4852d08c22a539504Mike Reed default: 528304a07c6c93298c3481bc0d4852d08c22a539504Mike Reed return nullptr; // remove me when kIndex_8 is removed from the enum 5298572d853514e3c73077540341edbf62a3f486605Matt Sarett } 5308572d853514e3c73077540341edbf62a3f486605Matt Sarett 5318572d853514e3c73077540341edbf62a3f486605Matt Sarett SkASSERT(false); 5328572d853514e3c73077540341edbf62a3f486605Matt Sarett return nullptr; 5338572d853514e3c73077540341edbf62a3f486605Matt Sarett} 5348572d853514e3c73077540341edbf62a3f486605Matt Sarett 5358572d853514e3c73077540341edbf62a3f486605Matt Sarettstatic void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo, 5368572d853514e3c73077540341edbf62a3f486605Matt Sarett const SkImageInfo& srcInfo) { 53726b44df2333d5e8cec8aef11ab598d63b4ee05c7Matt Sarett if (!SkImageInfoIsValidRenderingCS(srcInfo)) { 5388572d853514e3c73077540341edbf62a3f486605Matt Sarett return; 5398572d853514e3c73077540341edbf62a3f486605Matt Sarett } 5408572d853514e3c73077540341edbf62a3f486605Matt Sarett 5418572d853514e3c73077540341edbf62a3f486605Matt Sarett const void* srcPixels = five_reference_pixels(srcInfo.colorType()); 542304a07c6c93298c3481bc0d4852d08c22a539504Mike Reed SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes()); 5438572d853514e3c73077540341edbf62a3f486605Matt Sarett sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr); 5448572d853514e3c73077540341edbf62a3f486605Matt Sarett REPORTER_ASSERT(r, src); 5458572d853514e3c73077540341edbf62a3f486605Matt Sarett 5468572d853514e3c73077540341edbf62a3f486605Matt Sarett // Enough space for 5 pixels when color type is F16, more than enough space in other cases. 5478572d853514e3c73077540341edbf62a3f486605Matt Sarett uint64_t dstPixels[kNumPixels]; 548304a07c6c93298c3481bc0d4852d08c22a539504Mike Reed SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes()); 5498572d853514e3c73077540341edbf62a3f486605Matt Sarett bool success = src->readPixels(dstPixmap, 0, 0); 5508572d853514e3c73077540341edbf62a3f486605Matt Sarett REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo)); 5518572d853514e3c73077540341edbf62a3f486605Matt Sarett 5528572d853514e3c73077540341edbf62a3f486605Matt Sarett if (success) { 5538572d853514e3c73077540341edbf62a3f486605Matt Sarett if (kGray_8_SkColorType == srcInfo.colorType() && 5548572d853514e3c73077540341edbf62a3f486605Matt Sarett kGray_8_SkColorType != dstInfo.colorType()) 5558572d853514e3c73077540341edbf62a3f486605Matt Sarett { 5568572d853514e3c73077540341edbf62a3f486605Matt Sarett // This conversion is legal, but we won't get the "reference" pixels since we cannot 5578572d853514e3c73077540341edbf62a3f486605Matt Sarett // represent colors in kGray8. 5588572d853514e3c73077540341edbf62a3f486605Matt Sarett return; 5598572d853514e3c73077540341edbf62a3f486605Matt Sarett } 5608572d853514e3c73077540341edbf62a3f486605Matt Sarett 5618572d853514e3c73077540341edbf62a3f486605Matt Sarett REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()), 5628572d853514e3c73077540341edbf62a3f486605Matt Sarett kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType()))); 5638572d853514e3c73077540341edbf62a3f486605Matt Sarett 5648572d853514e3c73077540341edbf62a3f486605Matt Sarett } 5658572d853514e3c73077540341edbf62a3f486605Matt Sarett} 5668572d853514e3c73077540341edbf62a3f486605Matt Sarett 5678572d853514e3c73077540341edbf62a3f486605Matt SarettDEF_TEST(ReadPixels_ValidConversion, reporter) { 5688572d853514e3c73077540341edbf62a3f486605Matt Sarett const SkColorType kColorTypes[] = { 5698572d853514e3c73077540341edbf62a3f486605Matt Sarett kUnknown_SkColorType, 5708572d853514e3c73077540341edbf62a3f486605Matt Sarett kAlpha_8_SkColorType, 5718572d853514e3c73077540341edbf62a3f486605Matt Sarett kRGB_565_SkColorType, 5728572d853514e3c73077540341edbf62a3f486605Matt Sarett kARGB_4444_SkColorType, 5738572d853514e3c73077540341edbf62a3f486605Matt Sarett kRGBA_8888_SkColorType, 5748572d853514e3c73077540341edbf62a3f486605Matt Sarett kBGRA_8888_SkColorType, 5758572d853514e3c73077540341edbf62a3f486605Matt Sarett kGray_8_SkColorType, 5768572d853514e3c73077540341edbf62a3f486605Matt Sarett kRGBA_F16_SkColorType, 5778572d853514e3c73077540341edbf62a3f486605Matt Sarett }; 5788572d853514e3c73077540341edbf62a3f486605Matt Sarett 5798572d853514e3c73077540341edbf62a3f486605Matt Sarett const SkAlphaType kAlphaTypes[] = { 5808572d853514e3c73077540341edbf62a3f486605Matt Sarett kUnknown_SkAlphaType, 5818572d853514e3c73077540341edbf62a3f486605Matt Sarett kOpaque_SkAlphaType, 5828572d853514e3c73077540341edbf62a3f486605Matt Sarett kPremul_SkAlphaType, 5838572d853514e3c73077540341edbf62a3f486605Matt Sarett kUnpremul_SkAlphaType, 5848572d853514e3c73077540341edbf62a3f486605Matt Sarett }; 5858572d853514e3c73077540341edbf62a3f486605Matt Sarett 5868572d853514e3c73077540341edbf62a3f486605Matt Sarett const sk_sp<SkColorSpace> kColorSpaces[] = { 5878572d853514e3c73077540341edbf62a3f486605Matt Sarett nullptr, 5888572d853514e3c73077540341edbf62a3f486605Matt Sarett SkColorSpace::MakeSRGB(), 5898572d853514e3c73077540341edbf62a3f486605Matt Sarett }; 5908572d853514e3c73077540341edbf62a3f486605Matt Sarett 5918572d853514e3c73077540341edbf62a3f486605Matt Sarett for (SkColorType dstCT : kColorTypes) { 5928572d853514e3c73077540341edbf62a3f486605Matt Sarett for (SkAlphaType dstAT: kAlphaTypes) { 5938572d853514e3c73077540341edbf62a3f486605Matt Sarett for (sk_sp<SkColorSpace> dstCS : kColorSpaces) { 5948572d853514e3c73077540341edbf62a3f486605Matt Sarett for (SkColorType srcCT : kColorTypes) { 5958572d853514e3c73077540341edbf62a3f486605Matt Sarett for (SkAlphaType srcAT: kAlphaTypes) { 5968572d853514e3c73077540341edbf62a3f486605Matt Sarett for (sk_sp<SkColorSpace> srcCS : kColorSpaces) { 5978572d853514e3c73077540341edbf62a3f486605Matt Sarett if (kRGBA_F16_SkColorType == dstCT && dstCS) { 59836703d9d368050a20764b5336534bd718fd00a6eBrian Osman dstCS = dstCS->makeLinearGamma(); 5998572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6008572d853514e3c73077540341edbf62a3f486605Matt Sarett 6018572d853514e3c73077540341edbf62a3f486605Matt Sarett if (kRGBA_F16_SkColorType == srcCT && srcCS) { 60236703d9d368050a20764b5336534bd718fd00a6eBrian Osman srcCS = srcCS->makeLinearGamma(); 6038572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6048572d853514e3c73077540341edbf62a3f486605Matt Sarett 6058572d853514e3c73077540341edbf62a3f486605Matt Sarett test_conversion(reporter, 6068572d853514e3c73077540341edbf62a3f486605Matt Sarett SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS), 6078572d853514e3c73077540341edbf62a3f486605Matt Sarett SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS)); 6088572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6098572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6108572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6118572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6128572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6138572d853514e3c73077540341edbf62a3f486605Matt Sarett } 6148572d853514e3c73077540341edbf62a3f486605Matt Sarett} 615