blob: 2c3cb005b56dad935055e36166b96007b8be1130 [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"
robertphillips7715e062016-04-22 10:57:16 -07009#include "SkSurface.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000010#include "Test.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000011#include "sk_tool_utils.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012
13#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080014#include "GrContext.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +000016
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000017static uint32_t pack_unpremul_rgba(SkColor c) {
18 uint32_t packed;
19 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
20 byte[0] = SkColorGetR(c);
21 byte[1] = SkColorGetG(c);
22 byte[2] = SkColorGetB(c);
23 byte[3] = SkColorGetA(c);
24 return packed;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000025}
26
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000027static uint32_t pack_unpremul_bgra(SkColor c) {
28 uint32_t packed;
29 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
30 byte[0] = SkColorGetB(c);
31 byte[1] = SkColorGetG(c);
32 byte[2] = SkColorGetR(c);
33 byte[3] = SkColorGetA(c);
34 return packed;
35}
36
37typedef uint32_t (*PackUnpremulProc)(SkColor);
38
39const struct {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000040 SkColorType fColorType;
41 PackUnpremulProc fPackProc;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000042} gUnpremul[] = {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000043 { kRGBA_8888_SkColorType, pack_unpremul_rgba },
44 { kBGRA_8888_SkColorType, pack_unpremul_bgra },
bsalomon@google.coma91e9232012-02-23 15:39:54 +000045};
46
robertphillips7715e062016-04-22 10:57:16 -070047static void fill_canvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulProc proc) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000048 // Don't strictly need a bitmap, but its a handy way to allocate the pixels
49 SkBitmap bmp;
50 bmp.allocN32Pixels(256, 256);
51
52 for (int a = 0; a < 256; ++a) {
53 uint32_t* pixels = bmp.getAddr32(0, a);
54 for (int r = 0; r < 256; ++r) {
55 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0));
56 }
57 }
58
reede5ea5002014-09-03 11:54:58 -070059 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(),
60 colorType, kUnpremul_SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000061 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0);
62}
63
robertphillips7715e062016-04-22 10:57:16 -070064static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface* surf) {
65 SkCanvas* canvas = surf->getCanvas();
kkinnunen15302832015-12-01 04:35:26 -080066 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
robertphillips7715e062016-04-22 10:57:16 -070067 fill_canvas(canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000068
kkinnunen15302832015-12-01 04:35:26 -080069 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
70 kUnpremul_SkAlphaType);
71 SkBitmap readBmp1;
72 readBmp1.allocPixels(info);
73 SkBitmap readBmp2;
74 readBmp2.allocPixels(info);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000075
kkinnunen15302832015-12-01 04:35:26 -080076 readBmp1.eraseColor(0);
77 readBmp2.eraseColor(0);
78
Mike Reedf1942192017-07-21 14:24:29 -040079 surf->readPixels(readBmp1, 0, 0);
robertphillips7715e062016-04-22 10:57:16 -070080 sk_tool_utils::write_pixels(canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].fColorType,
kkinnunen15302832015-12-01 04:35:26 -080081 kUnpremul_SkAlphaType);
Mike Reedf1942192017-07-21 14:24:29 -040082 surf->readPixels(readBmp2, 0, 0);
kkinnunen15302832015-12-01 04:35:26 -080083
84 bool success = true;
85 for (int y = 0; y < 256 && success; ++y) {
86 const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
87 const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
88 for (int x = 0; x < 256 && success; ++x) {
89 // We see sporadic failures here. May help to see where it goes wrong.
90 if (pixels1[x] != pixels2[x]) {
91 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000092 }
kkinnunen15302832015-12-01 04:35:26 -080093 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000094 }
95 }
96 }
97}
kkinnunen15302832015-12-01 04:35:26 -080098
99DEF_TEST(PremulAlphaRoundTrip, reporter) {
100 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
robertphillips7715e062016-04-22 10:57:16 -0700101
102 sk_sp<SkSurface> surf(SkSurface::MakeRaster(info));
103
104 test_premul_alpha_roundtrip(reporter, surf.get());
kkinnunen15302832015-12-01 04:35:26 -0800105}
106#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700107DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo) {
kkinnunen15302832015-12-01 04:35:26 -0800108 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
robertphillips7715e062016-04-22 10:57:16 -0700109
bsalomon8b7451a2016-05-11 06:33:06 -0700110 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.grContext(),
robertphillips7715e062016-04-22 10:57:16 -0700111 SkBudgeted::kNo,
112 info));
113 test_premul_alpha_roundtrip(reporter, surf.get());
kkinnunen15302832015-12-01 04:35:26 -0800114}
115#endif