blob: 79b32f91cbebac4bca83546e1efc3e39a68703ea [file] [log] [blame]
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "Test.h"
10#include "SkCanvas.h"
11#include "SkConfig8888.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012#include "SkDevice.h"
13
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
19
20namespace {
21
22void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) {
23 SkBitmap bmp;
24 bmp.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
25 bmp.allocPixels();
26 SkAutoLockPixels alp(bmp);
27 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
28
29 for (int a = 0; a < 256; ++a) {
30 for (int r = 0; r < 256; ++r) {
31 pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0);
32 }
33 }
34 canvas->writePixels(bmp, 0, 0, unpremulConfig);
35}
36
37static const SkCanvas::Config8888 gUnpremulConfigs[] = {
38 SkCanvas::kNative_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000039 SkCanvas::kBGRA_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000040 SkCanvas::kRGBA_Unpremul_Config8888,
41};
42
bsalomon@google.com67b915d2013-02-04 16:13:32 +000043void PremulAlphaRoundTripTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000044 SkAutoTUnref<SkBaseDevice> device;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000045 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000046
47 int glCtxTypeCnt = 1;
48#if SK_SUPPORT_GPU
49 if (0 != dtype) {
50 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000051 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000052#endif
53 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
54 if (0 == dtype) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000055 device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
56 256,
57 256,
58 false));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000059 } else {
60#if SK_SUPPORT_GPU
61 GrContextFactory::GLContextType type =
62 static_cast<GrContextFactory::GLContextType>(glCtxType);
63 if (!GrContextFactory::IsRenderingGLContext(type)) {
64 continue;
65 }
66 GrContext* context = factory->get(type);
67 if (NULL == context) {
68 continue;
69 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +000070
bsalomon@google.com67b915d2013-02-04 16:13:32 +000071 device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256));
72#else
73 continue;
74#endif
75 }
76 SkCanvas canvas(device);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000077
bsalomon@google.com67b915d2013-02-04 16:13:32 +000078 SkBitmap readBmp1;
79 readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
80 readBmp1.allocPixels();
81 SkBitmap readBmp2;
82 readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
83 readBmp2.allocPixels();
84
85 for (size_t upmaIdx = 0;
86 upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
87 ++upmaIdx) {
88 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
89 {
90 SkAutoLockPixels alp1(readBmp1);
91 SkAutoLockPixels alp2(readBmp2);
92 sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
93 sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
94 }
95
96 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
97 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
98 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
99
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000100 SkAutoLockPixels alp1(readBmp1);
101 SkAutoLockPixels alp2(readBmp2);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000102 uint32_t* pixels1 =
103 reinterpret_cast<uint32_t*>(readBmp1.getPixels());
104 uint32_t* pixels2 =
105 reinterpret_cast<uint32_t*>(readBmp2.getPixels());
106 bool success = true;
107 for (int y = 0; y < 256 && success; ++y) {
108 for (int x = 0; x < 256 && success; ++x) {
109 int i = y * 256 + x;
110 REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]);
111 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000112 }
113 }
114 }
115 }
116}
117}
118
119#include "TestClassDef.h"
120DEFINE_GPUTESTCLASS("PremulAlphaRoundTripTest", PremulAlphaRoundTripTestClass, PremulAlphaRoundTripTest)