blob: c963e231fbdac6026bf67f40cd964911a6afdda3 [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"
12#include "TestClassDef.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000013
14#if SK_SUPPORT_GPU
bsalomon@google.com67b915d2013-02-04 16:13:32 +000015#include "GrContextFactory.h"
bsalomon@google.coma91e9232012-02-23 15:39:54 +000016#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +000018
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +000019static void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +000020 SkBitmap bmp;
21 bmp.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
22 bmp.allocPixels();
23 SkAutoLockPixels alp(bmp);
24 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
25
26 for (int a = 0; a < 256; ++a) {
27 for (int r = 0; r < 256; ++r) {
28 pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0);
29 }
30 }
31 canvas->writePixels(bmp, 0, 0, unpremulConfig);
32}
33
34static const SkCanvas::Config8888 gUnpremulConfigs[] = {
35 SkCanvas::kNative_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000036 SkCanvas::kBGRA_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000037 SkCanvas::kRGBA_Unpremul_Config8888,
38};
39
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000040DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) {
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) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000052 device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
53 256,
54 256,
55 false));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000056 } else {
57#if SK_SUPPORT_GPU
58 GrContextFactory::GLContextType type =
59 static_cast<GrContextFactory::GLContextType>(glCtxType);
60 if (!GrContextFactory::IsRenderingGLContext(type)) {
61 continue;
62 }
63 GrContext* context = factory->get(type);
64 if (NULL == context) {
65 continue;
66 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +000067
bsalomon@google.com67b915d2013-02-04 16:13:32 +000068 device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256));
69#else
70 continue;
71#endif
72 }
73 SkCanvas canvas(device);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000074
bsalomon@google.com67b915d2013-02-04 16:13:32 +000075 SkBitmap readBmp1;
76 readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
77 readBmp1.allocPixels();
78 SkBitmap readBmp2;
79 readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
80 readBmp2.allocPixels();
81
82 for (size_t upmaIdx = 0;
83 upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
84 ++upmaIdx) {
85 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
86 {
87 SkAutoLockPixels alp1(readBmp1);
88 SkAutoLockPixels alp2(readBmp2);
89 sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
90 sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
91 }
92
93 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
94 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
95 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
96
bsalomon@google.coma91e9232012-02-23 15:39:54 +000097 SkAutoLockPixels alp1(readBmp1);
98 SkAutoLockPixels alp2(readBmp2);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000099 uint32_t* pixels1 =
100 reinterpret_cast<uint32_t*>(readBmp1.getPixels());
101 uint32_t* pixels2 =
102 reinterpret_cast<uint32_t*>(readBmp2.getPixels());
103 bool success = true;
104 for (int y = 0; y < 256 && success; ++y) {
105 for (int x = 0; x < 256 && success; ++x) {
106 int i = y * 256 + x;
107 REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]);
108 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000109 }
110 }
111 }
112 }
113}