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"
|
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
|
|
|
|
2013-10-12 17:25:17 +00:00
|
|
|
static void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) {
|
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
|
|
|
SkAutoLockPixels alp(bmp);
|
|
|
|
uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
|
|
|
|
|
|
|
|
for (int a = 0; a < 256; ++a) {
|
|
|
|
for (int r = 0; r < 256; ++r) {
|
|
|
|
pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->writePixels(bmp, 0, 0, unpremulConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const SkCanvas::Config8888 gUnpremulConfigs[] = {
|
|
|
|
SkCanvas::kNative_Unpremul_Config8888,
|
|
|
|
SkCanvas::kBGRA_Unpremul_Config8888,
|
|
|
|
SkCanvas::kRGBA_Unpremul_Config8888,
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
2013-08-29 11:54:56 +00:00
|
|
|
SkAutoTUnref<SkBaseDevice> device;
|
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
|
|
|
|
for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
|
|
|
|
if (0 == dtype) {
|
2014-02-16 00:59:25 +00:00
|
|
|
device.reset(SkBitmapDevice::Create(info));
|
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;
|
|
|
|
}
|
|
|
|
GrContext* context = factory->get(type);
|
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-16 00:59:25 +00:00
|
|
|
device.reset(SkGpuDevice::Create(context, info, 0));
|
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);
|
|
|
|
|
|
|
|
SkBitmap readBmp1;
|
2014-02-13 14:41:43 +00:00
|
|
|
readBmp1.allocN32Pixels(256, 256);
|
2013-02-04 16:13:32 +00:00
|
|
|
SkBitmap readBmp2;
|
2014-02-13 14:41:43 +00:00
|
|
|
readBmp2.allocN32Pixels(256, 256);
|
2013-02-04 16:13:32 +00:00
|
|
|
|
|
|
|
for (size_t upmaIdx = 0;
|
|
|
|
upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
|
|
|
|
++upmaIdx) {
|
|
|
|
fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
|
|
|
|
{
|
|
|
|
SkAutoLockPixels alp1(readBmp1);
|
|
|
|
SkAutoLockPixels alp2(readBmp2);
|
|
|
|
sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
|
|
|
|
sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
|
|
|
|
canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
|
|
|
|
canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
|
2012-02-23 15:39:54 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
SkAutoLockPixels alp1(readBmp1);
|
|
|
|
SkAutoLockPixels alp2(readBmp2);
|
|
|
|
uint32_t* pixels1 =
|
|
|
|
reinterpret_cast<uint32_t*>(readBmp1.getPixels());
|
|
|
|
uint32_t* pixels2 =
|
|
|
|
reinterpret_cast<uint32_t*>(readBmp2.getPixels());
|
|
|
|
bool success = true;
|
|
|
|
for (int y = 0; y < 256 && success; ++y) {
|
|
|
|
for (int x = 0; x < 256 && success; ++x) {
|
|
|
|
int i = y * 256 + x;
|
|
|
|
REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]);
|
|
|
|
}
|
2012-02-23 15:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|