blob: 5e8e8bf1dd42843b8db5d9905b4812e1e4106f72 [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) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000039 SkAutoTUnref<SkBaseDevice> device;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000040 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000041
42 int glCtxTypeCnt = 1;
43#if SK_SUPPORT_GPU
44 if (0 != dtype) {
45 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000046 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000047#endif
48 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
49 if (0 == dtype) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000050 device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
51 256,
52 256,
53 false));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000054 } else {
55#if SK_SUPPORT_GPU
56 GrContextFactory::GLContextType type =
57 static_cast<GrContextFactory::GLContextType>(glCtxType);
58 if (!GrContextFactory::IsRenderingGLContext(type)) {
59 continue;
60 }
61 GrContext* context = factory->get(type);
62 if (NULL == context) {
63 continue;
64 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +000065
bsalomon@google.com67b915d2013-02-04 16:13:32 +000066 device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256));
67#else
68 continue;
69#endif
70 }
71 SkCanvas canvas(device);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000072
bsalomon@google.com67b915d2013-02-04 16:13:32 +000073 SkBitmap readBmp1;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000074 readBmp1.allocN32Pixels(256, 256);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000075 SkBitmap readBmp2;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000076 readBmp2.allocN32Pixels(256, 256);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000077
78 for (size_t upmaIdx = 0;
79 upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
80 ++upmaIdx) {
81 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
82 {
83 SkAutoLockPixels alp1(readBmp1);
84 SkAutoLockPixels alp2(readBmp2);
85 sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
86 sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
87 }
88
89 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
90 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
91 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
92
bsalomon@google.coma91e9232012-02-23 15:39:54 +000093 SkAutoLockPixels alp1(readBmp1);
94 SkAutoLockPixels alp2(readBmp2);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000095 uint32_t* pixels1 =
96 reinterpret_cast<uint32_t*>(readBmp1.getPixels());
97 uint32_t* pixels2 =
98 reinterpret_cast<uint32_t*>(readBmp2.getPixels());
99 bool success = true;
100 for (int y = 0; y < 256 && success; ++y) {
101 for (int x = 0; x < 256 && success; ++x) {
102 int i = y * 256 + x;
103 REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]);
104 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000105 }
106 }
107 }
108 }
109}