blob: 575e7763779d3813e6fc11b7a94ce6af0eb14010 [file] [log] [blame]
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001/*
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
bsalomon@google.coma91e9232012-02-23 15:39:54 +00008#include "SkCanvas.h"
9#include "SkConfig8888.h"
robertphillips7715e062016-04-22 10:57:16 -070010#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000011#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000012#include "sk_tool_utils.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000013
14#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080015#include "GrContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000016#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +000017
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000018static uint32_t pack_unpremul_rgba(SkColor c) {
19 uint32_t packed;
20 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
21 byte[0] = SkColorGetR(c);
22 byte[1] = SkColorGetG(c);
23 byte[2] = SkColorGetB(c);
24 byte[3] = SkColorGetA(c);
25 return packed;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000026}
27
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000028static uint32_t pack_unpremul_bgra(SkColor c) {
29 uint32_t packed;
30 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
31 byte[0] = SkColorGetB(c);
32 byte[1] = SkColorGetG(c);
33 byte[2] = SkColorGetR(c);
34 byte[3] = SkColorGetA(c);
35 return packed;
36}
37
38typedef uint32_t (*PackUnpremulProc)(SkColor);
39
40const struct {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000041 SkColorType fColorType;
42 PackUnpremulProc fPackProc;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000043} gUnpremul[] = {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000044 { kRGBA_8888_SkColorType, pack_unpremul_rgba },
45 { kBGRA_8888_SkColorType, pack_unpremul_bgra },
bsalomon@google.coma91e9232012-02-23 15:39:54 +000046};
47
robertphillips7715e062016-04-22 10:57:16 -070048static void fill_canvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulProc proc) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000049 // Don't strictly need a bitmap, but its a handy way to allocate the pixels
50 SkBitmap bmp;
51 bmp.allocN32Pixels(256, 256);
52
53 for (int a = 0; a < 256; ++a) {
54 uint32_t* pixels = bmp.getAddr32(0, a);
55 for (int r = 0; r < 256; ++r) {
56 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0));
57 }
58 }
59
reede5ea5002014-09-03 11:54:58 -070060 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(),
61 colorType, kUnpremul_SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000062 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0);
63}
64
robertphillips7715e062016-04-22 10:57:16 -070065static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface* surf) {
66 SkCanvas* canvas = surf->getCanvas();
kkinnunen15302832015-12-01 04:35:26 -080067 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
robertphillips7715e062016-04-22 10:57:16 -070068 fill_canvas(canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000069
kkinnunen15302832015-12-01 04:35:26 -080070 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
71 kUnpremul_SkAlphaType);
72 SkBitmap readBmp1;
73 readBmp1.allocPixels(info);
74 SkBitmap readBmp2;
75 readBmp2.allocPixels(info);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000076
kkinnunen15302832015-12-01 04:35:26 -080077 readBmp1.eraseColor(0);
78 readBmp2.eraseColor(0);
79
robertphillips7715e062016-04-22 10:57:16 -070080 canvas->readPixels(&readBmp1, 0, 0);
81 sk_tool_utils::write_pixels(canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].fColorType,
kkinnunen15302832015-12-01 04:35:26 -080082 kUnpremul_SkAlphaType);
robertphillips7715e062016-04-22 10:57:16 -070083 canvas->readPixels(&readBmp2, 0, 0);
kkinnunen15302832015-12-01 04:35:26 -080084
85 bool success = true;
86 for (int y = 0; y < 256 && success; ++y) {
87 const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
88 const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
89 for (int x = 0; x < 256 && success; ++x) {
90 // We see sporadic failures here. May help to see where it goes wrong.
91 if (pixels1[x] != pixels2[x]) {
92 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000093 }
kkinnunen15302832015-12-01 04:35:26 -080094 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000095 }
96 }
97 }
98}
kkinnunen15302832015-12-01 04:35:26 -080099
100DEF_TEST(PremulAlphaRoundTrip, reporter) {
101 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
robertphillips7715e062016-04-22 10:57:16 -0700102
103 sk_sp<SkSurface> surf(SkSurface::MakeRaster(info));
104
105 test_premul_alpha_roundtrip(reporter, surf.get());
kkinnunen15302832015-12-01 04:35:26 -0800106}
107#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700108DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800109 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
robertphillips7715e062016-04-22 10:57:16 -0700110
bsalomon8b7451a2016-05-11 06:33:06 -0700111 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.grContext(),
robertphillips7715e062016-04-22 10:57:16 -0700112 SkBudgeted::kNo,
113 info));
114 test_premul_alpha_roundtrip(reporter, surf.get());
kkinnunen15302832015-12-01 04:35:26 -0800115}
116#endif