blob: 488d93e19d32d36d067c1e3d1724876da97962e9 [file] [log] [blame]
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001/*
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
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00008#include "SkBitmapDevice.h"
bsalomon@google.coma91e9232012-02-23 15:39:54 +00009#include "SkCanvas.h"
10#include "SkConfig8888.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000011#include "Test.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012
13#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +000014#include "GrContextFactory.h"
bsalomon@google.coma91e9232012-02-23 15:39:54 +000015#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000016#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +000017
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000018static void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +000019 SkBitmap bmp;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000020 bmp.allocN32Pixels(256, 256);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000021 SkAutoLockPixels alp(bmp);
22 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
23
24 for (int a = 0; a < 256; ++a) {
25 for (int r = 0; r < 256; ++r) {
26 pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0);
27 }
28 }
29 canvas->writePixels(bmp, 0, 0, unpremulConfig);
30}
31
32static const SkCanvas::Config8888 gUnpremulConfigs[] = {
33 SkCanvas::kNative_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000034 SkCanvas::kBGRA_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000035 SkCanvas::kRGBA_Unpremul_Config8888,
36};
37
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000038DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000039 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
40
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000041 SkAutoTUnref<SkBaseDevice> device;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000042 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000043
44 int glCtxTypeCnt = 1;
45#if SK_SUPPORT_GPU
46 if (0 != dtype) {
47 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000048 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000049#endif
50 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
51 if (0 == dtype) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000052 device.reset(SkBitmapDevice::Create(info));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000053 } else {
54#if SK_SUPPORT_GPU
55 GrContextFactory::GLContextType type =
56 static_cast<GrContextFactory::GLContextType>(glCtxType);
57 if (!GrContextFactory::IsRenderingGLContext(type)) {
58 continue;
59 }
60 GrContext* context = factory->get(type);
61 if (NULL == context) {
62 continue;
63 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +000064
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000065 device.reset(SkGpuDevice::Create(context, info, 0));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000066#else
67 continue;
68#endif
69 }
70 SkCanvas canvas(device);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000071
bsalomon@google.com67b915d2013-02-04 16:13:32 +000072 SkBitmap readBmp1;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000073 readBmp1.allocN32Pixels(256, 256);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000074 SkBitmap readBmp2;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000075 readBmp2.allocN32Pixels(256, 256);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000076
77 for (size_t upmaIdx = 0;
78 upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
79 ++upmaIdx) {
80 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
81 {
82 SkAutoLockPixels alp1(readBmp1);
83 SkAutoLockPixels alp2(readBmp2);
84 sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
85 sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
86 }
87
88 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
89 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
90 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
91
bsalomon@google.coma91e9232012-02-23 15:39:54 +000092 SkAutoLockPixels alp1(readBmp1);
93 SkAutoLockPixels alp2(readBmp2);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000094 uint32_t* pixels1 =
95 reinterpret_cast<uint32_t*>(readBmp1.getPixels());
96 uint32_t* pixels2 =
97 reinterpret_cast<uint32_t*>(readBmp2.getPixels());
98 bool success = true;
99 for (int y = 0; y < 256 && success; ++y) {
100 for (int x = 0; x < 256 && success; ++x) {
101 int i = y * 256 + x;
102 REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]);
103 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000104 }
105 }
106 }
107 }
108}