2012-02-23 15:39:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-01-10 22:08:27 +00:00
|
|
|
#include "SkBitmapDevice.h"
|
2012-02-23 15:39:54 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkConfig8888.h"
|
2014-01-10 22:08:27 +00:00
|
|
|
#include "Test.h"
|
2014-03-07 03:25:16 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2012-08-02 14:03:32 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2013-02-04 16:13:32 +00:00
|
|
|
#include "GrContextFactory.h"
|
2012-02-23 15:39:54 +00:00
|
|
|
#include "SkGpuDevice.h"
|
2012-08-02 14:03:32 +00:00
|
|
|
#endif
|
2012-02-23 15:39:54 +00:00
|
|
|
|
2014-03-07 03:25:16 +00:00
|
|
|
static uint32_t pack_unpremul_rgba(SkColor c) {
|
|
|
|
uint32_t packed;
|
|
|
|
uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
|
|
|
|
byte[0] = SkColorGetR(c);
|
|
|
|
byte[1] = SkColorGetG(c);
|
|
|
|
byte[2] = SkColorGetB(c);
|
|
|
|
byte[3] = SkColorGetA(c);
|
|
|
|
return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t pack_unpremul_bgra(SkColor c) {
|
|
|
|
uint32_t packed;
|
|
|
|
uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
|
|
|
|
byte[0] = SkColorGetB(c);
|
|
|
|
byte[1] = SkColorGetG(c);
|
|
|
|
byte[2] = SkColorGetR(c);
|
|
|
|
byte[3] = SkColorGetA(c);
|
|
|
|
return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef uint32_t (*PackUnpremulProc)(SkColor);
|
|
|
|
|
|
|
|
const struct {
|
2014-03-17 21:31:26 +00:00
|
|
|
SkColorType fColorType;
|
|
|
|
PackUnpremulProc fPackProc;
|
2014-03-07 03:25:16 +00:00
|
|
|
} gUnpremul[] = {
|
2014-03-17 21:31:26 +00:00
|
|
|
{ kRGBA_8888_SkColorType, pack_unpremul_rgba },
|
|
|
|
{ kBGRA_8888_SkColorType, pack_unpremul_bgra },
|
2014-03-07 03:25:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void fillCanvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulProc proc) {
|
|
|
|
// Don't strictly need a bitmap, but its a handy way to allocate the pixels
|
2012-02-23 15:39:54 +00:00
|
|
|
SkBitmap bmp;
|
2014-02-13 14:41:43 +00:00
|
|
|
bmp.allocN32Pixels(256, 256);
|
2012-02-23 15:39:54 +00:00
|
|
|
|
|
|
|
for (int a = 0; a < 256; ++a) {
|
2014-03-07 03:25:16 +00:00
|
|
|
uint32_t* pixels = bmp.getAddr32(0, a);
|
2012-02-23 15:39:54 +00:00
|
|
|
for (int r = 0; r < 256; ++r) {
|
2014-03-07 03:25:16 +00:00
|
|
|
pixels[r] = proc(SkColorSetARGB(a, r, 0, 0));
|
2012-02-23 15:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:54:58 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(),
|
|
|
|
colorType, kUnpremul_SkAlphaType);
|
2014-03-07 03:25:16 +00:00
|
|
|
canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0);
|
|
|
|
}
|
2012-02-23 15:39:54 +00:00
|
|
|
|
2014-01-10 22:08:27 +00:00
|
|
|
DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) {
|
2014-02-16 00:59:25 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
|
|
|
|
|
2012-02-23 15:39:54 +00:00
|
|
|
for (int dtype = 0; dtype < 2; ++dtype) {
|
2013-02-04 16:13:32 +00:00
|
|
|
|
|
|
|
int glCtxTypeCnt = 1;
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (0 != dtype) {
|
|
|
|
glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-22 16:46:59 +00:00
|
|
|
SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
|
2013-02-04 16:13:32 +00:00
|
|
|
for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
|
2014-07-15 21:38:53 +00:00
|
|
|
SkAutoTUnref<SkBaseDevice> device;
|
2013-02-04 16:13:32 +00:00
|
|
|
if (0 == dtype) {
|
2015-06-22 16:46:59 +00:00
|
|
|
device.reset(SkBitmapDevice::Create(info, props));
|
2013-02-04 16:13:32 +00:00
|
|
|
} else {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
GrContextFactory::GLContextType type =
|
|
|
|
static_cast<GrContextFactory::GLContextType>(glCtxType);
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(type)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-16 15:32:33 +00:00
|
|
|
GrContext* ctx = factory->get(type);
|
|
|
|
if (NULL == ctx) {
|
2013-02-04 16:13:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-16 15:32:33 +00:00
|
|
|
device.reset(SkGpuDevice::Create(ctx, SkSurface::kNo_Budgeted, info, 0, &props));
|
2012-08-02 14:03:32 +00:00
|
|
|
#else
|
2013-02-04 16:13:32 +00:00
|
|
|
continue;
|
2012-08-02 14:03:32 +00:00
|
|
|
#endif
|
2012-02-23 15:39:54 +00:00
|
|
|
}
|
2013-02-04 16:13:32 +00:00
|
|
|
SkCanvas canvas(device);
|
|
|
|
|
2014-03-07 03:25:16 +00:00
|
|
|
for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
|
|
|
|
fillCanvas(&canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
|
2014-03-08 03:02:06 +00:00
|
|
|
|
2014-03-17 21:31:26 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
|
|
|
|
kUnpremul_SkAlphaType);
|
|
|
|
SkBitmap readBmp1;
|
|
|
|
readBmp1.allocPixels(info);
|
|
|
|
SkBitmap readBmp2;
|
|
|
|
readBmp2.allocPixels(info);
|
|
|
|
|
2014-03-07 03:25:16 +00:00
|
|
|
readBmp1.eraseColor(0);
|
|
|
|
readBmp2.eraseColor(0);
|
2013-02-04 16:13:32 +00:00
|
|
|
|
2014-03-17 21:31:26 +00:00
|
|
|
canvas.readPixels(&readBmp1, 0, 0);
|
2014-03-07 03:25:16 +00:00
|
|
|
sk_tool_utils::write_pixels(&canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].fColorType,
|
|
|
|
kUnpremul_SkAlphaType);
|
2014-03-17 21:31:26 +00:00
|
|
|
canvas.readPixels(&readBmp2, 0, 0);
|
2012-02-23 15:39:54 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
bool success = true;
|
|
|
|
for (int y = 0; y < 256 && success; ++y) {
|
2014-03-07 03:25:16 +00:00
|
|
|
const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
|
|
|
|
const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
|
2013-02-04 16:13:32 +00:00
|
|
|
for (int x = 0; x < 256 && success; ++x) {
|
2014-07-30 19:37:19 +00:00
|
|
|
// We see sporadic failures here. May help to see where it goes wrong.
|
|
|
|
if (pixels1[x] != pixels2[x]) {
|
|
|
|
SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
|
|
|
|
}
|
2014-03-07 03:25:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
|
2013-02-04 16:13:32 +00:00
|
|
|
}
|
2012-02-23 15:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|