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/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSurface.h"
15 #include "include/core/SkTypes.h"
16 #include "include/gpu/GpuTypes.h"
17 #include "include/gpu/GrDirectContext.h"
18 #include "include/private/base/SkDebug.h"
19 #include "src/core/SkConvertPixels.h"
20 #include "src/gpu/ganesh/GrDataUtils.h"
21 #include "src/gpu/ganesh/GrPixmap.h"
22 #include "tests/CtsEnforcement.h"
23 #include "tests/Test.h"
24 
25 #include <array>
26 #include <cstddef>
27 #include <cstdint>
28 
29 struct GrContextOptions;
30 
pack_unpremul_rgba(SkColor c)31 static uint32_t pack_unpremul_rgba(SkColor c) {
32     uint32_t packed;
33     uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
34     byte[0] = SkColorGetR(c);
35     byte[1] = SkColorGetG(c);
36     byte[2] = SkColorGetB(c);
37     byte[3] = SkColorGetA(c);
38     return packed;
39 }
40 
pack_unpremul_bgra(SkColor c)41 static uint32_t pack_unpremul_bgra(SkColor c) {
42     uint32_t packed;
43     uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
44     byte[0] = SkColorGetB(c);
45     byte[1] = SkColorGetG(c);
46     byte[2] = SkColorGetR(c);
47     byte[3] = SkColorGetA(c);
48     return packed;
49 }
50 
51 typedef uint32_t (*PackUnpremulProc)(SkColor);
52 
53 const struct {
54     SkColorType         fColorType;
55     PackUnpremulProc    fPackProc;
56 } gUnpremul[] = {
57     { kRGBA_8888_SkColorType, pack_unpremul_rgba },
58     { kBGRA_8888_SkColorType, pack_unpremul_bgra },
59 };
60 
fill_surface(SkSurface * surf,SkColorType colorType,PackUnpremulProc proc)61 static void fill_surface(SkSurface* surf, SkColorType colorType, PackUnpremulProc proc) {
62     // Don't strictly need a bitmap, but its a handy way to allocate the pixels
63     SkBitmap bmp;
64     bmp.allocN32Pixels(256, 256);
65 
66     for (int a = 0; a < 256; ++a) {
67         uint32_t* pixels = bmp.getAddr32(0, a);
68         for (int r = 0; r < 256; ++r) {
69             pixels[r] = proc(SkColorSetARGB(a, r, 0, 0));
70         }
71     }
72 
73     const SkImageInfo info = SkImageInfo::Make(bmp.dimensions(), colorType, kUnpremul_SkAlphaType);
74     surf->writePixels({info, bmp.getPixels(), bmp.rowBytes()}, 0, 0);
75 }
76 
test_premul_alpha_roundtrip(skiatest::Reporter * reporter,SkSurface * surf)77 static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface* surf) {
78     for (size_t upmaIdx = 0; upmaIdx < std::size(gUnpremul); ++upmaIdx) {
79         fill_surface(surf, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
80 
81         const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
82                                                    kUnpremul_SkAlphaType);
83         SkBitmap readBmp1;
84         readBmp1.allocPixels(info);
85         SkBitmap readBmp2;
86         readBmp2.allocPixels(info);
87 
88         readBmp1.eraseColor(0);
89         readBmp2.eraseColor(0);
90 
91         surf->readPixels(readBmp1, 0, 0);
92         surf->writePixels(readBmp1, 0, 0);
93         surf->readPixels(readBmp2, 0, 0);
94 
95         bool success = true;
96         for (int y = 0; y < 256 && success; ++y) {
97             const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
98             const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
99             for (int x = 0; x < 256 && success; ++x) {
100                 // We see sporadic failures here. May help to see where it goes wrong.
101                 if (pixels1[x] != pixels2[x]) {
102                     SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
103                 }
104                 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
105             }
106         }
107     }
108 }
109 
DEF_TEST(PremulAlphaRoundTrip,reporter)110 DEF_TEST(PremulAlphaRoundTrip, reporter) {
111     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
112 
113     sk_sp<SkSurface> surf(SkSurface::MakeRaster(info));
114 
115     test_premul_alpha_roundtrip(reporter, surf.get());
116 }
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)117 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu,
118                                        reporter,
119                                        ctxInfo,
120                                        CtsEnforcement::kApiLevel_T) {
121     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
122 
123     sk_sp<SkSurface> surf(
124             SkSurface::MakeRenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kNo, info));
125     test_premul_alpha_roundtrip(reporter, surf.get());
126 }
127 
DEF_TEST(PremulAlphaRoundTripGrConvertPixels,reporter)128 DEF_TEST(PremulAlphaRoundTripGrConvertPixels, reporter) {
129     // Code that does the same thing as above, but using GrConvertPixels. This simulates what
130     // happens if you run the above on a machine with a GPU that doesn't have a valid PM/UPM
131     // conversion pair of FPs.
132     const SkImageInfo upmInfo =
133             SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);
134     const SkImageInfo pmInfo =
135             SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
136 
137     GrPixmap src = GrPixmap::Allocate(upmInfo);
138     uint32_t* srcPixels = (uint32_t*)src.addr();
139     for (int y = 0; y < 256; ++y) {
140         for (int x = 0; x < 256; ++x) {
141             srcPixels[y * 256 + x] = pack_unpremul_rgba(SkColorSetARGB(y, x, x, x));
142         }
143     }
144 
145     GrPixmap surf = GrPixmap::Allocate(pmInfo);
146     GrConvertPixels(surf, src);
147 
148     GrPixmap read1 = GrPixmap::Allocate(upmInfo);
149     GrConvertPixels(read1, surf);
150 
151     GrPixmap surf2 = GrPixmap::Allocate(pmInfo);
152     GrConvertPixels(surf2, read1);
153 
154     GrPixmap read2 = GrPixmap::Allocate(upmInfo);
155     GrConvertPixels(read2, surf2);
156 
157     auto get_pixel = [](const GrPixmap& pm, int x, int y) {
158         const uint32_t* addr = (const uint32_t*)pm.addr();
159         return addr[y * 256 + x];
160     };
161     auto dump_pixel_history = [&](int x, int y) {
162         SkDebugf("Pixel history for (%d, %d):\n", x, y);
163         SkDebugf("Src : %08x\n", get_pixel(src, x, y));
164         SkDebugf(" -> : %08x\n", get_pixel(surf, x, y));
165         SkDebugf(" <- : %08x\n", get_pixel(read1, x, y));
166         SkDebugf(" -> : %08x\n", get_pixel(surf2, x, y));
167         SkDebugf(" <- : %08x\n", get_pixel(read2, x, y));
168     };
169 
170     bool success = true;
171     for (int y = 0; y < 256 && success; ++y) {
172         const uint32_t* pixels1 = (const uint32_t*) read1.addr();
173         const uint32_t* pixels2 = (const uint32_t*) read2.addr();
174         for (int x = 0; x < 256 && success; ++x) {
175             uint32_t c1 = pixels1[y * 256 + x],
176                      c2 = pixels2[y * 256 + x];
177             // If this ever fails, it's helpful to see where it goes wrong.
178             if (c1 != c2) {
179                 dump_pixel_history(x, y);
180             }
181             REPORTER_ASSERT(reporter, success = c1 == c2);
182         }
183     }
184 }
185 
DEF_TEST(PremulAlphaRoundTripSkConvertPixels,reporter)186 DEF_TEST(PremulAlphaRoundTripSkConvertPixels, reporter) {
187     // ... and now using SkConvertPixels, just for completeness
188     const SkImageInfo upmInfo =
189             SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType);
190     const SkImageInfo pmInfo =
191             SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
192 
193     SkBitmap src; src.allocPixels(upmInfo);
194     uint32_t* srcPixels = src.getAddr32(0, 0);
195     for (int y = 0; y < 256; ++y) {
196         for (int x = 0; x < 256; ++x) {
197             srcPixels[y * 256 + x] = pack_unpremul_rgba(SkColorSetARGB(y, x, x, x));
198         }
199     }
200 
201     auto convert = [](const SkBitmap& dst, const SkBitmap& src){
202         SkAssertResult(SkConvertPixels(dst.info(), dst.getAddr(0, 0), dst.rowBytes(),
203                                        src.info(), src.getAddr(0, 0), src.rowBytes()));
204     };
205 
206     SkBitmap surf; surf.allocPixels(pmInfo);
207     convert(surf, src);
208 
209     SkBitmap read1; read1.allocPixels(upmInfo);
210     convert(read1, surf);
211 
212     SkBitmap surf2; surf2.allocPixels(pmInfo);
213     convert(surf2, read1);
214 
215     SkBitmap read2; read2.allocPixels(upmInfo);
216     convert(read2, surf2);
217 
218     auto dump_pixel_history = [&](int x, int y) {
219         SkDebugf("Pixel history for (%d, %d):\n", x, y);
220         SkDebugf("Src : %08x\n", *src.getAddr32(x, y));
221         SkDebugf(" -> : %08x\n", *surf.getAddr32(x, y));
222         SkDebugf(" <- : %08x\n", *read1.getAddr32(x, y));
223         SkDebugf(" -> : %08x\n", *surf2.getAddr32(x, y));
224         SkDebugf(" <- : %08x\n", *read2.getAddr32(x, y));
225     };
226 
227     bool success = true;
228     for (int y = 0; y < 256 && success; ++y) {
229         const uint32_t* pixels1 = read1.getAddr32(0, 0);
230         const uint32_t* pixels2 = read2.getAddr32(0, 0);
231         for (int x = 0; x < 256 && success; ++x) {
232             uint32_t c1 = pixels1[y * 256 + x],
233                      c2 = pixels2[y * 256 + x];
234             // If this ever fails, it's helpful to see where it goes wrong.
235             if (c1 != c2) {
236                 dump_pixel_history(x, y);
237             }
238             REPORTER_ASSERT(reporter, success = c1 == c2);
239         }
240     }
241 }
242