diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-19 21:20:24 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-19 21:20:24 +0000 |
commit | e14792d99fc7a1a314ef5e2ca5b269239468355a (patch) | |
tree | 8415a8086f37d363ac8ebf4107c98258d2671420 /tests | |
parent | d8a57af725e8fa8905207df3cf7465be50598752 (diff) |
hide Config8888 entirely
BUG=skia:
R=bsalomon@google.com
Author: reed@google.com
Review URL: https://codereview.chromium.org/203993002
git-svn-id: http://skia.googlecode.com/svn/trunk@13865 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests')
-rw-r--r-- | tests/DeferredCanvasTest.cpp | 27 | ||||
-rw-r--r-- | tests/WritePixelsTest.cpp | 213 |
2 files changed, 102 insertions, 138 deletions
diff --git a/tests/DeferredCanvasTest.cpp b/tests/DeferredCanvasTest.cpp index aa360be401..0af0c51fed 100644 --- a/tests/DeferredCanvasTest.cpp +++ b/tests/DeferredCanvasTest.cpp @@ -26,21 +26,6 @@ class GrContextFactory; static const int gWidth = 2; static const int gHeight = 2; -static void callWritePixels(SkCanvas* canvas, const SkBitmap& src, int x, int y, - SkCanvas::Config8888 config) { - SkBitmap bm(src); - bm.lockPixels(); - - SkImageInfo info = bm.info(); - sk_tool_utils::config8888_to_imagetypes(config, &info.fColorType, &info.fAlphaType); - - if (src.isOpaque()) { - info.fAlphaType = kOpaque_SkAlphaType; - } - - canvas->writePixels(info, bm.getPixels(), bm.rowBytes(), x, y); -} - static void create(SkBitmap* bm, SkColor color) { bm->allocN32Pixels(gWidth, gHeight); bm->eraseColor(color); @@ -122,7 +107,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get())); SkBitmap srcBitmap; - srcBitmap.allocN32Pixels(10, 10); + srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType)); srcBitmap.eraseColor(SK_ColorGREEN); // Tests below depend on this bitmap being recognized as opaque @@ -174,7 +159,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888); + canvas->writePixels(srcBitmap, 0, 0); REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); @@ -191,7 +176,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); + canvas->writePixels(srcBitmap, 5, 0); REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); @@ -213,7 +198,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888); + canvas->writePixels(srcBitmap, 0, 0); REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); @@ -235,7 +220,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); + canvas->writePixels(srcBitmap, 5, 0); REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); @@ -259,7 +244,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); + canvas->writePixels(srcBitmap, 5, 0); REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); diff --git a/tests/WritePixelsTest.cpp b/tests/WritePixelsTest.cpp index 7adcf0295d..087012739c 100644 --- a/tests/WritePixelsTest.cpp +++ b/tests/WritePixelsTest.cpp @@ -56,41 +56,18 @@ static SkPMColor getCanvasColor(int x, int y) { return SkPremultiplyARGBInline(a, r, g, b); } -static bool config8888IsPremul(SkCanvas::Config8888 config8888) { - switch (config8888) { - case SkCanvas::kNative_Premul_Config8888: - case SkCanvas::kBGRA_Premul_Config8888: - case SkCanvas::kRGBA_Premul_Config8888: - return true; - case SkCanvas::kNative_Unpremul_Config8888: - case SkCanvas::kBGRA_Unpremul_Config8888: - case SkCanvas::kRGBA_Unpremul_Config8888: - return false; - default: - SkASSERT(0); - return false; - } -} - // assumes any premu/.unpremul has been applied -static uint32_t packConfig8888(SkCanvas::Config8888 config8888, - U8CPU a, U8CPU r, U8CPU g, U8CPU b) { +static uint32_t packColorType(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) { uint32_t r32; uint8_t* result = reinterpret_cast<uint8_t*>(&r32); - switch (config8888) { - case SkCanvas::kNative_Premul_Config8888: - case SkCanvas::kNative_Unpremul_Config8888: - r32 = SkPackARGB32NoCheck(a, r, g, b); - break; - case SkCanvas::kBGRA_Premul_Config8888: - case SkCanvas::kBGRA_Unpremul_Config8888: + switch (ct) { + case kBGRA_8888_SkColorType: result[0] = b; result[1] = g; result[2] = r; result[3] = a; break; - case SkCanvas::kRGBA_Premul_Config8888: - case SkCanvas::kRGBA_Unpremul_Config8888: + case kRGBA_8888_SkColorType: result[0] = r; result[1] = g; result[2] = b; @@ -103,7 +80,7 @@ static uint32_t packConfig8888(SkCanvas::Config8888 config8888, return r32; } -static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8888) { +static uint32_t getBitmapColor(int x, int y, int w, SkColorType ct, SkAlphaType at) { int n = y * w + x; U8CPU b = n & 0xff; U8CPU g = (n >> 8) & 0xff; @@ -126,12 +103,12 @@ static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8 a = 0x00; break; } - if (config8888IsPremul(config8888)) { + if (kPremul_SkAlphaType == at) { r = SkMulDiv255Ceiling(r, a); g = SkMulDiv255Ceiling(g, a); b = SkMulDiv255Ceiling(b, a); } - return packConfig8888(config8888, a, r, g , b); + return packColorType(ct, a, r, g , b); } static void fillCanvas(SkCanvas* canvas) { @@ -154,48 +131,38 @@ static void fillCanvas(SkCanvas* canvas) { canvas->restore(); } -static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888, - uint32_t color, - bool* premul) { - const uint8_t* c = reinterpret_cast<uint8_t*>(&color); - U8CPU a,r,g,b; - *premul = false; - switch (config8888) { - case SkCanvas::kNative_Premul_Config8888: - return color; - case SkCanvas::kNative_Unpremul_Config8888: - *premul = true; - a = SkGetPackedA32(color); - r = SkGetPackedR32(color); - g = SkGetPackedG32(color); - b = SkGetPackedB32(color); - break; - case SkCanvas::kBGRA_Unpremul_Config8888: - *premul = true; // fallthru - case SkCanvas::kBGRA_Premul_Config8888: - a = static_cast<U8CPU>(c[3]); - r = static_cast<U8CPU>(c[2]); - g = static_cast<U8CPU>(c[1]); - b = static_cast<U8CPU>(c[0]); +/** + * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA. + * Thus this routine doesn't need to know the exact colortype + */ +static uint32_t premul(uint32_t color) { + unsigned a = SkGetPackedA32(color); + // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order. + unsigned c0 = SkGetPackedR32(color); + unsigned c1 = SkGetPackedG32(color); + unsigned c2 = SkGetPackedB32(color); + c0 = SkMulDiv255Ceiling(c0, a); + c1 = SkMulDiv255Ceiling(c1, a); + c2 = SkMulDiv255Ceiling(c2, a); + return SkPackARGB32NoCheck(a, c0, c1, c2); +} + +static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) { + if (kUnpremul_SkAlphaType == at) { + color = premul(color); + } + switch (ct) { + case kRGBA_8888_SkColorType: + color = SkSwizzle_RGBA_to_PMColor(color); break; - case SkCanvas::kRGBA_Unpremul_Config8888: - *premul = true; // fallthru - case SkCanvas::kRGBA_Premul_Config8888: - a = static_cast<U8CPU>(c[3]); - r = static_cast<U8CPU>(c[0]); - g = static_cast<U8CPU>(c[1]); - b = static_cast<U8CPU>(c[2]); + case kBGRA_8888_SkColorType: + color = SkSwizzle_BGRA_to_PMColor(color); break; default: - SkDEBUGFAIL("Unexpected Config8888"); - return 0; - } - if (*premul) { - r = SkMulDiv255Ceiling(r, a); - g = SkMulDiv255Ceiling(g, a); - b = SkMulDiv255Ceiling(b, a); + SkASSERT(0); + break; } - return SkPackARGB32(a, r, g, b); + return color; } static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { @@ -218,39 +185,54 @@ static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { SkAbs32(aB - bB) <= 1; } -static bool checkWrite(skiatest::Reporter* reporter, - SkCanvas* canvas, - const SkBitmap& bitmap, - int writeX, int writeY, - SkCanvas::Config8888 config8888) { - SkBaseDevice* dev = canvas->getDevice(); - if (!dev) { +static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap, + int writeX, int writeY) { + SkImageInfo canvasInfo; + size_t canvasRowBytes; + const uint32_t* canvasPixels; + + // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well. + // At some point this will be unsupported, as we won't allow accessBitmap() to magically call + // readPixels for the client. + SkBitmap secretDevBitmap; + { + SkBaseDevice* dev = canvas->getDevice(); + if (!dev) { + return false; + } + secretDevBitmap = dev->accessBitmap(false); + } + SkAutoLockPixels alp(secretDevBitmap); + canvasInfo = secretDevBitmap.info(); + canvasRowBytes = secretDevBitmap.rowBytes(); + canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); + + if (NULL == canvasPixels) { return false; } - SkBitmap devBmp = dev->accessBitmap(false); - if (devBmp.width() != DEV_W || - devBmp.height() != DEV_H || - devBmp.config() != SkBitmap::kARGB_8888_Config || - devBmp.isNull()) { + + if (canvasInfo.width() != DEV_W || + canvasInfo.height() != DEV_H || + canvasInfo.colorType() != kPMColor_SkColorType) { return false; } - SkAutoLockPixels alp(devBmp); - intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels()); - size_t canvasRowBytes = devBmp.rowBytes(); + const SkImageInfo bmInfo = bitmap.info(); + SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height()); for (int cy = 0; cy < DEV_H; ++cy) { - const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels); for (int cx = 0; cx < DEV_W; ++cx) { - SkPMColor canvasPixel = canvasRow[cx]; + SkPMColor canvasPixel = canvasPixels[cx]; if (writeRect.contains(cx, cy)) { int bx = cx - writeX; int by = cy - writeY; - uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), config8888); - bool mul; - SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul); - bool check; - REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul)); + uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), + bmInfo.colorType(), bmInfo.alphaType()); + bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType()); + SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(), + bmpColor8888); + bool check = checkPixel(bmpPMColor, canvasPixel, mul); + REPORTER_ASSERT(reporter, check); if (!check) { return false; } @@ -264,7 +246,7 @@ static bool checkWrite(skiatest::Reporter* reporter, } } if (cy != DEV_H -1) { - const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W); + const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W); for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) { bool check; REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD))); @@ -273,7 +255,7 @@ static bool checkWrite(skiatest::Reporter* reporter, } } } - canvasPixels += canvasRowBytes; + canvasPixels += canvasRowBytes/4; } return true; @@ -351,21 +333,16 @@ static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { return NULL; } -static bool setupBitmap(SkBitmap* bitmap, - SkCanvas::Config8888 config8888, - int w, int h, - bool tightRowBytes) { - size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60; - SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); - if (!allocRowBytes(bitmap, info, rowBytes)) { +static bool setupBitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) { + size_t rowBytes = tightRB ? 0 : 4 * w + 60; + SkImageInfo info = SkImageInfo::Make(w, h, ct, at); + if (!allocRowBytes(bm, info, rowBytes)) { return false; } - SkAutoLockPixels alp(*bitmap); - intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); + SkAutoLockPixels alp(*bm); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { - uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4); - *pixel = getBitmapColor(x, y, w, config8888); + *bm->getAddr32(x, y) = getBitmapColor(x, y, w, ct, at); } } return true; @@ -449,31 +426,33 @@ DEF_GPUTEST(WritePixels, reporter, factory) { SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context)); SkCanvas canvas(device); - static const SkCanvas::Config8888 gSrcConfigs[] = { - SkCanvas::kNative_Premul_Config8888, - SkCanvas::kNative_Unpremul_Config8888, - SkCanvas::kBGRA_Premul_Config8888, - SkCanvas::kBGRA_Unpremul_Config8888, - SkCanvas::kRGBA_Premul_Config8888, - SkCanvas::kRGBA_Unpremul_Config8888, + static const struct { + SkColorType fColorType; + SkAlphaType fAlphaType; + } gSrcConfigs[] = { + { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, + { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, + { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, + { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, }; for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { const SkIRect& rect = testRects[r]; for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { + const SkColorType ct = gSrcConfigs[c].fColorType; + const SkAlphaType at = gSrcConfigs[c].fAlphaType; + fillCanvas(&canvas); - SkCanvas::Config8888 config8888 = gSrcConfigs[c]; SkBitmap bmp; - REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp))); + REPORTER_ASSERT(reporter, setupBitmap(&bmp, ct, at, rect.width(), + rect.height(), SkToBool(tightBmp))); uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID(); - SkColorType ct; - SkAlphaType at; - sk_tool_utils::config8888_to_imagetypes(config8888, &ct, &at); - sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); + // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); + canvas.writePixels(bmp, rect.fLeft, rect.fTop); uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID(); - REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888)); + REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop)); // we should change the genID iff pixels were actually written. SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize()); |