2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkBitmapDevice.h"
10 #include "SkConfig8888.h"
12 #include "sk_tool_utils.h"
15 #include "GrContextFactory.h"
16 #include "SkGpuDevice.h"
19 static uint32_t pack_unpremul_rgba(SkColor c) {
21 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
22 byte[0] = SkColorGetR(c);
23 byte[1] = SkColorGetG(c);
24 byte[2] = SkColorGetB(c);
25 byte[3] = SkColorGetA(c);
29 static uint32_t pack_unpremul_bgra(SkColor c) {
31 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
32 byte[0] = SkColorGetB(c);
33 byte[1] = SkColorGetG(c);
34 byte[2] = SkColorGetR(c);
35 byte[3] = SkColorGetA(c);
39 typedef uint32_t (*PackUnpremulProc)(SkColor);
42 SkColorType fColorType;
43 PackUnpremulProc fPackProc;
45 { kRGBA_8888_SkColorType, pack_unpremul_rgba },
46 { kBGRA_8888_SkColorType, pack_unpremul_bgra },
49 static void fillCanvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulProc proc) {
50 // Don't strictly need a bitmap, but its a handy way to allocate the pixels
52 bmp.allocN32Pixels(256, 256);
54 for (int a = 0; a < 256; ++a) {
55 uint32_t* pixels = bmp.getAddr32(0, a);
56 for (int r = 0; r < 256; ++r) {
57 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0));
61 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(),
62 colorType, kUnpremul_SkAlphaType);
63 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0);
66 DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) {
67 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
69 for (int dtype = 0; dtype < 2; ++dtype) {
74 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
77 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
78 SkAutoTUnref<SkBaseDevice> device;
80 device.reset(SkBitmapDevice::Create(info));
83 GrContextFactory::GLContextType type =
84 static_cast<GrContextFactory::GLContextType>(glCtxType);
85 if (!GrContextFactory::IsRenderingGLContext(type)) {
88 GrContext* context = factory->get(type);
89 if (NULL == context) {
93 device.reset(SkGpuDevice::Create(context, info,
94 SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType), 0));
99 SkCanvas canvas(device);
101 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
102 fillCanvas(&canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
104 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
105 kUnpremul_SkAlphaType);
107 readBmp1.allocPixels(info);
109 readBmp2.allocPixels(info);
111 readBmp1.eraseColor(0);
112 readBmp2.eraseColor(0);
114 canvas.readPixels(&readBmp1, 0, 0);
115 sk_tool_utils::write_pixels(&canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].fColorType,
116 kUnpremul_SkAlphaType);
117 canvas.readPixels(&readBmp2, 0, 0);
120 for (int y = 0; y < 256 && success; ++y) {
121 const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
122 const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
123 for (int x = 0; x < 256 && success; ++x) {
124 // We see sporadic failures here. May help to see where it goes wrong.
125 if (pixels1[x] != pixels2[x]) {
126 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
128 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);