OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapDevice.h" | |
9 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
10 #include "SkConfig8888.h" | 9 #include "SkConfig8888.h" |
| 10 #include "SkSurface.h" |
11 #include "Test.h" | 11 #include "Test.h" |
12 #include "sk_tool_utils.h" | 12 #include "sk_tool_utils.h" |
13 | 13 |
14 #if SK_SUPPORT_GPU | 14 #if SK_SUPPORT_GPU |
15 #include "GrContext.h" | 15 #include "GrContext.h" |
16 #include "SkGpuDevice.h" | |
17 #endif | 16 #endif |
18 | 17 |
19 static uint32_t pack_unpremul_rgba(SkColor c) { | 18 static uint32_t pack_unpremul_rgba(SkColor c) { |
20 uint32_t packed; | 19 uint32_t packed; |
21 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); | 20 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); |
22 byte[0] = SkColorGetR(c); | 21 byte[0] = SkColorGetR(c); |
23 byte[1] = SkColorGetG(c); | 22 byte[1] = SkColorGetG(c); |
24 byte[2] = SkColorGetB(c); | 23 byte[2] = SkColorGetB(c); |
25 byte[3] = SkColorGetA(c); | 24 byte[3] = SkColorGetA(c); |
26 return packed; | 25 return packed; |
(...skipping 12 matching lines...) Expand all Loading... |
39 typedef uint32_t (*PackUnpremulProc)(SkColor); | 38 typedef uint32_t (*PackUnpremulProc)(SkColor); |
40 | 39 |
41 const struct { | 40 const struct { |
42 SkColorType fColorType; | 41 SkColorType fColorType; |
43 PackUnpremulProc fPackProc; | 42 PackUnpremulProc fPackProc; |
44 } gUnpremul[] = { | 43 } gUnpremul[] = { |
45 { kRGBA_8888_SkColorType, pack_unpremul_rgba }, | 44 { kRGBA_8888_SkColorType, pack_unpremul_rgba }, |
46 { kBGRA_8888_SkColorType, pack_unpremul_bgra }, | 45 { kBGRA_8888_SkColorType, pack_unpremul_bgra }, |
47 }; | 46 }; |
48 | 47 |
49 static void fillCanvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulProc
proc) { | 48 static void fill_canvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulPro
c proc) { |
50 // Don't strictly need a bitmap, but its a handy way to allocate the pixels | 49 // Don't strictly need a bitmap, but its a handy way to allocate the pixels |
51 SkBitmap bmp; | 50 SkBitmap bmp; |
52 bmp.allocN32Pixels(256, 256); | 51 bmp.allocN32Pixels(256, 256); |
53 | 52 |
54 for (int a = 0; a < 256; ++a) { | 53 for (int a = 0; a < 256; ++a) { |
55 uint32_t* pixels = bmp.getAddr32(0, a); | 54 uint32_t* pixels = bmp.getAddr32(0, a); |
56 for (int r = 0; r < 256; ++r) { | 55 for (int r = 0; r < 256; ++r) { |
57 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0)); | 56 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0)); |
58 } | 57 } |
59 } | 58 } |
60 | 59 |
61 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(), | 60 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(), |
62 colorType, kUnpremul_SkAlphaType)
; | 61 colorType, kUnpremul_SkAlphaType)
; |
63 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0); | 62 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0); |
64 } | 63 } |
65 | 64 |
66 static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkBaseDevi
ce* device) { | 65 static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface*
surf) { |
67 SkCanvas canvas(device); | 66 SkCanvas* canvas = surf->getCanvas(); |
68 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) { | 67 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) { |
69 fillCanvas(&canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fP
ackProc); | 68 fill_canvas(canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fP
ackProc); |
70 | 69 |
71 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].
fColorType, | 70 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].
fColorType, |
72 kUnpremul_SkAlphaType); | 71 kUnpremul_SkAlphaType); |
73 SkBitmap readBmp1; | 72 SkBitmap readBmp1; |
74 readBmp1.allocPixels(info); | 73 readBmp1.allocPixels(info); |
75 SkBitmap readBmp2; | 74 SkBitmap readBmp2; |
76 readBmp2.allocPixels(info); | 75 readBmp2.allocPixels(info); |
77 | 76 |
78 readBmp1.eraseColor(0); | 77 readBmp1.eraseColor(0); |
79 readBmp2.eraseColor(0); | 78 readBmp2.eraseColor(0); |
80 | 79 |
81 canvas.readPixels(&readBmp1, 0, 0); | 80 canvas->readPixels(&readBmp1, 0, 0); |
82 sk_tool_utils::write_pixels(&canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].
fColorType, | 81 sk_tool_utils::write_pixels(canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].f
ColorType, |
83 kUnpremul_SkAlphaType); | 82 kUnpremul_SkAlphaType); |
84 canvas.readPixels(&readBmp2, 0, 0); | 83 canvas->readPixels(&readBmp2, 0, 0); |
85 | 84 |
86 bool success = true; | 85 bool success = true; |
87 for (int y = 0; y < 256 && success; ++y) { | 86 for (int y = 0; y < 256 && success; ++y) { |
88 const uint32_t* pixels1 = readBmp1.getAddr32(0, y); | 87 const uint32_t* pixels1 = readBmp1.getAddr32(0, y); |
89 const uint32_t* pixels2 = readBmp2.getAddr32(0, y); | 88 const uint32_t* pixels2 = readBmp2.getAddr32(0, y); |
90 for (int x = 0; x < 256 && success; ++x) { | 89 for (int x = 0; x < 256 && success; ++x) { |
91 // We see sporadic failures here. May help to see where it goes
wrong. | 90 // We see sporadic failures here. May help to see where it goes
wrong. |
92 if (pixels1[x] != pixels2[x]) { | 91 if (pixels1[x] != pixels2[x]) { |
93 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x
], x, y); | 92 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x
], x, y); |
94 } | 93 } |
95 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]); | 94 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]); |
96 } | 95 } |
97 } | 96 } |
98 } | 97 } |
99 } | 98 } |
100 | 99 |
101 DEF_TEST(PremulAlphaRoundTrip, reporter) { | 100 DEF_TEST(PremulAlphaRoundTrip, reporter) { |
102 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); | 101 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
103 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | 102 |
104 SkAutoTUnref<SkBaseDevice> device(SkBitmapDevice::Create(info, props)); | 103 sk_sp<SkSurface> surf(SkSurface::MakeRaster(info)); |
105 test_premul_alpha_roundtrip(reporter, device); | 104 |
| 105 test_premul_alpha_roundtrip(reporter, surf.get()); |
106 } | 106 } |
107 #if SK_SUPPORT_GPU | 107 #if SK_SUPPORT_GPU |
108 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo)
{ | 108 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo)
{ |
109 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); | 109 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
110 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | 110 |
111 SkAutoTUnref<SkBaseDevice> device( | 111 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.fGrContext, |
112 SkGpuDevice::Create(ctxInfo.fGrContext, SkBudgeted::kNo, info, 0, &props
, | 112 SkBudgeted::kNo, |
113 SkGpuDevice::kUninit_InitContents)); | 113 info)); |
114 test_premul_alpha_roundtrip(reporter, device); | 114 test_premul_alpha_roundtrip(reporter, surf.get()); |
115 } | 115 } |
116 #endif | 116 #endif |
OLD | NEW |