bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkConfig8888.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 11 | #include "Test.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 12 | #include "sk_tool_utils.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 13 | |
| 14 | #if SK_SUPPORT_GPU |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 15 | #include "GrContext.h" |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 16 | #include "SkGpuDevice.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 17 | #endif |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 18 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 19 | static uint32_t pack_unpremul_rgba(SkColor c) { |
| 20 | uint32_t packed; |
| 21 | uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); |
| 22 | byte[0] = SkColorGetR(c); |
| 23 | byte[1] = SkColorGetG(c); |
| 24 | byte[2] = SkColorGetB(c); |
| 25 | byte[3] = SkColorGetA(c); |
| 26 | return packed; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 27 | } |
| 28 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 29 | static uint32_t pack_unpremul_bgra(SkColor c) { |
| 30 | uint32_t packed; |
| 31 | uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); |
| 32 | byte[0] = SkColorGetB(c); |
| 33 | byte[1] = SkColorGetG(c); |
| 34 | byte[2] = SkColorGetR(c); |
| 35 | byte[3] = SkColorGetA(c); |
| 36 | return packed; |
| 37 | } |
| 38 | |
| 39 | typedef uint32_t (*PackUnpremulProc)(SkColor); |
| 40 | |
| 41 | const struct { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 42 | SkColorType fColorType; |
| 43 | PackUnpremulProc fPackProc; |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 44 | } gUnpremul[] = { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 45 | { kRGBA_8888_SkColorType, pack_unpremul_rgba }, |
| 46 | { kBGRA_8888_SkColorType, pack_unpremul_bgra }, |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 49 | static void fillCanvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulProc proc) { |
| 50 | // Don't strictly need a bitmap, but its a handy way to allocate the pixels |
| 51 | SkBitmap bmp; |
| 52 | bmp.allocN32Pixels(256, 256); |
| 53 | |
| 54 | for (int a = 0; a < 256; ++a) { |
| 55 | uint32_t* pixels = bmp.getAddr32(0, a); |
| 56 | for (int r = 0; r < 256; ++r) { |
| 57 | pixels[r] = proc(SkColorSetARGB(a, r, 0, 0)); |
| 58 | } |
| 59 | } |
| 60 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 61 | const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(), |
| 62 | colorType, kUnpremul_SkAlphaType); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 63 | canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0); |
| 64 | } |
| 65 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 66 | static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkBaseDevice* device) { |
| 67 | SkCanvas canvas(device); |
| 68 | for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) { |
| 69 | fillCanvas(&canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 70 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 71 | const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType, |
| 72 | kUnpremul_SkAlphaType); |
| 73 | SkBitmap readBmp1; |
| 74 | readBmp1.allocPixels(info); |
| 75 | SkBitmap readBmp2; |
| 76 | readBmp2.allocPixels(info); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 77 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 78 | readBmp1.eraseColor(0); |
| 79 | readBmp2.eraseColor(0); |
| 80 | |
| 81 | canvas.readPixels(&readBmp1, 0, 0); |
| 82 | sk_tool_utils::write_pixels(&canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].fColorType, |
| 83 | kUnpremul_SkAlphaType); |
| 84 | canvas.readPixels(&readBmp2, 0, 0); |
| 85 | |
| 86 | bool success = true; |
| 87 | for (int y = 0; y < 256 && success; ++y) { |
| 88 | const uint32_t* pixels1 = readBmp1.getAddr32(0, y); |
| 89 | const uint32_t* pixels2 = readBmp2.getAddr32(0, y); |
| 90 | for (int x = 0; x < 256 && success; ++x) { |
| 91 | // We see sporadic failures here. May help to see where it goes wrong. |
| 92 | if (pixels1[x] != pixels2[x]) { |
| 93 | SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 94 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 100 | |
| 101 | DEF_TEST(PremulAlphaRoundTrip, reporter) { |
| 102 | const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
| 103 | SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 104 | SkAutoTUnref<SkBaseDevice> device(SkBitmapDevice::Create(info, props)); |
| 105 | test_premul_alpha_roundtrip(reporter, device); |
| 106 | } |
| 107 | #if SK_SUPPORT_GPU |
| 108 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, context) { |
| 109 | const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
| 110 | SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 111 | SkAutoTUnref<SkBaseDevice> device( |
| 112 | SkGpuDevice::Create(context, SkSurface::kNo_Budgeted, info, 0, &props, |
| 113 | SkGpuDevice::kUninit_InitContents)); |
| 114 | test_premul_alpha_roundtrip(reporter, device); |
| 115 | } |
| 116 | #endif |
| 117 | |