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
8#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkSurface.h"
11#include "include/gpu/GrDirectContext.h"
12#include "src/core/SkConvertPixels.h"
13#include "src/gpu/GrPixmap.h"
14#include "tests/Test.h"
15#include "tools/ToolUtils.h"
16
17static 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;
25}
26
27static 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 {
40    SkColorType         fColorType;
41    PackUnpremulProc    fPackProc;
42} gUnpremul[] = {
43    { kRGBA_8888_SkColorType, pack_unpremul_rgba },
44    { kBGRA_8888_SkColorType, pack_unpremul_bgra },
45};
46
47static void fill_surface(SkSurface* surf, SkColorType colorType, PackUnpremulProc proc) {
48    // 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
59    const SkImageInfo info = SkImageInfo::Make(bmp.dimensions(), colorType, kUnpremul_SkAlphaType);
60    surf->writePixels({info, bmp.getPixels(), bmp.rowBytes()}, 0, 0);
61}
62
63static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface* surf) {
64    for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
65        fill_surface(surf, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
66
67        const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
68                                                   kUnpremul_SkAlphaType);
69        SkBitmap readBmp1;
70        readBmp1.allocPixels(info);
71        SkBitmap readBmp2;
72        readBmp2.allocPixels(info);
73
74        readBmp1.eraseColor(0);
75        readBmp2.eraseColor(0);
76
77        surf->readPixels(readBmp1, 0, 0);
78        surf->writePixels(readBmp1, 0, 0);
79        surf->readPixels(readBmp2, 0, 0);
80
81        bool success = true;
82        for (int y = 0; y < 256 && success; ++y) {
83            const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
84            const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
85            for (int x = 0; x < 256 && success; ++x) {
86                // We see sporadic failures here. May help to see where it goes wrong.
87                if (pixels1[x] != pixels2[x]) {
88                    SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
89                }
90                REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
91            }
92        }
93    }
94}
95
96DEF_TEST(PremulAlphaRoundTrip, reporter) {
97    const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
98
99    sk_sp<SkSurface> surf(SkSurface::MakeRaster(info));
100
101    test_premul_alpha_roundtrip(reporter, surf.get());
102}
103DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo) {
104    const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
105
106    sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.directContext(),
107                                                      SkBudgeted::kNo, info));
108    test_premul_alpha_roundtrip(reporter, surf.get());
109}
110
111DEF_TEST(PremulAlphaRoundTripGrConvertPixels, reporter) {
112    // Code that does the same thing as above, but using GrConvertPixels. This simulates what
113    // happens if you run the above on a machine with a GPU that doesn't have a valid PM/UPM
114    // conversion pair of FPs.
115    const SkImageInfo upmInfo =
116            SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);
117    const SkImageInfo pmInfo =
118            SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
119
120    GrPixmap src = GrPixmap::Allocate(upmInfo);
121    uint32_t* srcPixels = (uint32_t*)src.addr();
122    for (int y = 0; y < 256; ++y) {
123        for (int x = 0; x < 256; ++x) {
124            srcPixels[y * 256 + x] = pack_unpremul_rgba(SkColorSetARGB(y, x, x, x));
125        }
126    }
127
128    GrPixmap surf = GrPixmap::Allocate(pmInfo);
129    GrConvertPixels(surf, src);
130
131    GrPixmap read1 = GrPixmap::Allocate(upmInfo);
132    GrConvertPixels(read1, surf);
133
134    GrPixmap surf2 = GrPixmap::Allocate(pmInfo);
135    GrConvertPixels(surf2, read1);
136
137    GrPixmap read2 = GrPixmap::Allocate(upmInfo);
138    GrConvertPixels(read2, surf2);
139
140    auto get_pixel = [](const GrPixmap& pm, int x, int y) {
141        const uint32_t* addr = (const uint32_t*)pm.addr();
142        return addr[y * 256 + x];
143    };
144    auto dump_pixel_history = [&](int x, int y) {
145        SkDebugf("Pixel history for (%d, %d):\n", x, y);
146        SkDebugf("Src : %08x\n", get_pixel(src, x, y));
147        SkDebugf(" -> : %08x\n", get_pixel(surf, x, y));
148        SkDebugf(" <- : %08x\n", get_pixel(read1, x, y));
149        SkDebugf(" -> : %08x\n", get_pixel(surf2, x, y));
150        SkDebugf(" <- : %08x\n", get_pixel(read2, x, y));
151    };
152
153    bool success = true;
154    for (int y = 0; y < 256 && success; ++y) {
155        const uint32_t* pixels1 = (const uint32_t*) read1.addr();
156        const uint32_t* pixels2 = (const uint32_t*) read2.addr();
157        for (int x = 0; x < 256 && success; ++x) {
158            uint32_t c1 = pixels1[y * 256 + x],
159                     c2 = pixels2[y * 256 + x];
160            // If this ever fails, it's helpful to see where it goes wrong.
161            if (c1 != c2) {
162                dump_pixel_history(x, y);
163            }
164            REPORTER_ASSERT(reporter, success = c1 == c2);
165        }
166    }
167}
168
169DEF_TEST(PremulAlphaRoundTripSkConvertPixels, reporter) {
170    // ... and now using SkConvertPixels, just for completeness
171    const SkImageInfo upmInfo =
172            SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);
173    const SkImageInfo pmInfo =
174            SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
175
176    SkBitmap src; src.allocPixels(upmInfo);
177    uint32_t* srcPixels = src.getAddr32(0, 0);
178    for (int y = 0; y < 256; ++y) {
179        for (int x = 0; x < 256; ++x) {
180            srcPixels[y * 256 + x] = pack_unpremul_rgba(SkColorSetARGB(y, x, x, x));
181        }
182    }
183
184    auto convert = [](const SkBitmap& dst, const SkBitmap& src){
185        SkAssertResult(SkConvertPixels(dst.info(), dst.getAddr(0, 0), dst.rowBytes(),
186                                       src.info(), src.getAddr(0, 0), src.rowBytes()));
187    };
188
189    SkBitmap surf; surf.allocPixels(pmInfo);
190    convert(surf, src);
191
192    SkBitmap read1; read1.allocPixels(upmInfo);
193    convert(read1, surf);
194
195    SkBitmap surf2; surf2.allocPixels(pmInfo);
196    convert(surf2, read1);
197
198    SkBitmap read2; read2.allocPixels(upmInfo);
199    convert(read2, surf2);
200
201    auto dump_pixel_history = [&](int x, int y) {
202        SkDebugf("Pixel history for (%d, %d):\n", x, y);
203        SkDebugf("Src : %08x\n", *src.getAddr32(x, y));
204        SkDebugf(" -> : %08x\n", *surf.getAddr32(x, y));
205        SkDebugf(" <- : %08x\n", *read1.getAddr32(x, y));
206        SkDebugf(" -> : %08x\n", *surf2.getAddr32(x, y));
207        SkDebugf(" <- : %08x\n", *read2.getAddr32(x, y));
208    };
209
210    bool success = true;
211    for (int y = 0; y < 256 && success; ++y) {
212        const uint32_t* pixels1 = read1.getAddr32(0, 0);
213        const uint32_t* pixels2 = read2.getAddr32(0, 0);
214        for (int x = 0; x < 256 && success; ++x) {
215            uint32_t c1 = pixels1[y * 256 + x],
216                     c2 = pixels2[y * 256 + x];
217            // If this ever fails, it's helpful to see where it goes wrong.
218            if (c1 != c2) {
219                dump_pixel_history(x, y);
220            }
221            REPORTER_ASSERT(reporter, success = c1 == c2);
222        }
223    }
224}
225