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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "tests/Test.h" |
| 13 | #include "tools/ToolUtils.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 15 | static uint32_t pack_unpremul_rgba(SkColor c) { |
| 16 | uint32_t packed; |
| 17 | uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); |
| 18 | byte[0] = SkColorGetR(c); |
| 19 | byte[1] = SkColorGetG(c); |
| 20 | byte[2] = SkColorGetB(c); |
| 21 | byte[3] = SkColorGetA(c); |
| 22 | return packed; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 23 | } |
| 24 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 25 | static uint32_t pack_unpremul_bgra(SkColor c) { |
| 26 | uint32_t packed; |
| 27 | uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); |
| 28 | byte[0] = SkColorGetB(c); |
| 29 | byte[1] = SkColorGetG(c); |
| 30 | byte[2] = SkColorGetR(c); |
| 31 | byte[3] = SkColorGetA(c); |
| 32 | return packed; |
| 33 | } |
| 34 | |
| 35 | typedef uint32_t (*PackUnpremulProc)(SkColor); |
| 36 | |
| 37 | const struct { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 38 | SkColorType fColorType; |
| 39 | PackUnpremulProc fPackProc; |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 40 | } gUnpremul[] = { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 41 | { kRGBA_8888_SkColorType, pack_unpremul_rgba }, |
| 42 | { kBGRA_8888_SkColorType, pack_unpremul_bgra }, |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 45 | static void fill_surface(SkSurface* surf, SkColorType colorType, PackUnpremulProc proc) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 46 | // Don't strictly need a bitmap, but its a handy way to allocate the pixels |
| 47 | SkBitmap bmp; |
| 48 | bmp.allocN32Pixels(256, 256); |
| 49 | |
| 50 | for (int a = 0; a < 256; ++a) { |
| 51 | uint32_t* pixels = bmp.getAddr32(0, a); |
| 52 | for (int r = 0; r < 256; ++r) { |
| 53 | pixels[r] = proc(SkColorSetARGB(a, r, 0, 0)); |
| 54 | } |
| 55 | } |
| 56 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 57 | const SkImageInfo info = SkImageInfo::Make(bmp.dimensions(), colorType, kUnpremul_SkAlphaType); |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 58 | surf->writePixels({info, bmp.getPixels(), bmp.rowBytes()}, 0, 0); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 61 | static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface* surf) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 62 | for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) { |
Mike Reed | 4c790bd | 2018-02-08 14:10:40 -0500 | [diff] [blame] | 63 | fill_surface(surf, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 64 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 65 | const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType, |
| 66 | kUnpremul_SkAlphaType); |
| 67 | SkBitmap readBmp1; |
| 68 | readBmp1.allocPixels(info); |
| 69 | SkBitmap readBmp2; |
| 70 | readBmp2.allocPixels(info); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 71 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 72 | readBmp1.eraseColor(0); |
| 73 | readBmp2.eraseColor(0); |
| 74 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 75 | surf->readPixels(readBmp1, 0, 0); |
Brian Osman | b27667a | 2019-04-01 14:31:38 -0400 | [diff] [blame] | 76 | surf->writePixels(readBmp1, 0, 0); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 77 | surf->readPixels(readBmp2, 0, 0); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 78 | |
| 79 | bool success = true; |
| 80 | for (int y = 0; y < 256 && success; ++y) { |
| 81 | const uint32_t* pixels1 = readBmp1.getAddr32(0, y); |
| 82 | const uint32_t* pixels2 = readBmp2.getAddr32(0, y); |
| 83 | for (int x = 0; x < 256 && success; ++x) { |
| 84 | // We see sporadic failures here. May help to see where it goes wrong. |
| 85 | if (pixels1[x] != pixels2[x]) { |
| 86 | 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] | 87 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 88 | REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 93 | |
| 94 | DEF_TEST(PremulAlphaRoundTrip, reporter) { |
| 95 | const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 96 | |
| 97 | sk_sp<SkSurface> surf(SkSurface::MakeRaster(info)); |
| 98 | |
| 99 | test_premul_alpha_roundtrip(reporter, surf.get()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 100 | } |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 101 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo) { |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 102 | const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 103 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 104 | sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.directContext(), |
| 105 | SkBudgeted::kNo, info)); |
robertphillips | 7715e06 | 2016-04-22 10:57:16 -0700 | [diff] [blame] | 106 | test_premul_alpha_roundtrip(reporter, surf.get()); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 107 | } |