• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkCanvas.h"
9 #include "include/core/SkSurface.h"
10 #include "tests/Test.h"
11 #include "tools/ToolUtils.h"
12 
13 #include "include/gpu/GrContext.h"
14 
pack_unpremul_rgba(SkColor c)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;
23 }
24 
pack_unpremul_bgra(SkColor c)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 {
38     SkColorType         fColorType;
39     PackUnpremulProc    fPackProc;
40 } gUnpremul[] = {
41     { kRGBA_8888_SkColorType, pack_unpremul_rgba },
42     { kBGRA_8888_SkColorType, pack_unpremul_bgra },
43 };
44 
fill_surface(SkSurface * surf,SkColorType colorType,PackUnpremulProc proc)45 static void fill_surface(SkSurface* surf, SkColorType colorType, PackUnpremulProc proc) {
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 
57     const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(),
58                                                colorType, kUnpremul_SkAlphaType);
59     surf->writePixels({info, bmp.getPixels(), bmp.rowBytes()}, 0, 0);
60 }
61 
test_premul_alpha_roundtrip(skiatest::Reporter * reporter,SkSurface * surf)62 static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface* surf) {
63     for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
64         fill_surface(surf, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
65 
66         const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
67                                                    kUnpremul_SkAlphaType);
68         SkBitmap readBmp1;
69         readBmp1.allocPixels(info);
70         SkBitmap readBmp2;
71         readBmp2.allocPixels(info);
72 
73         readBmp1.eraseColor(0);
74         readBmp2.eraseColor(0);
75 
76         surf->readPixels(readBmp1, 0, 0);
77         surf->writePixels(readBmp1, 0, 0);
78         surf->readPixels(readBmp2, 0, 0);
79 
80         bool success = true;
81         for (int y = 0; y < 256 && success; ++y) {
82             const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
83             const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
84             for (int x = 0; x < 256 && success; ++x) {
85                 // We see sporadic failures here. May help to see where it goes wrong.
86                 if (pixels1[x] != pixels2[x]) {
87                     SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
88                 }
89                 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
90             }
91         }
92     }
93 }
94 
DEF_TEST(PremulAlphaRoundTrip,reporter)95 DEF_TEST(PremulAlphaRoundTrip, reporter) {
96     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
97 
98     sk_sp<SkSurface> surf(SkSurface::MakeRaster(info));
99 
100     test_premul_alpha_roundtrip(reporter, surf.get());
101 }
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu,reporter,ctxInfo)102 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo) {
103     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
104 
105     sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.grContext(),
106                                                       SkBudgeted::kNo,
107                                                       info));
108     test_premul_alpha_roundtrip(reporter, surf.get());
109 }
110