blob: c4ec6ab90a863ed53c9570e5095adfdee8bf20db [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"
12#include "SkGpuDevice.h"
13
14
15namespace {
16
17void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) {
18 SkBitmap bmp;
19 bmp.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
20 bmp.allocPixels();
21 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,
34/**
35 * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438)
36 * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to
37 * fail. This should be removed as soon as the issue above is resolved.
38 */
39#if !defined(SK_BUILD_FOR_ANDROID)
40 SkCanvas::kBGRA_Unpremul_Config8888,
41#endif
42 SkCanvas::kRGBA_Unpremul_Config8888,
43};
44
45void PremulAlphaRoundTripTest(skiatest::Reporter* reporter,
46 GrContext* context) {
47 SkCanvas canvas;
48 for (int dtype = 0; dtype < 2; ++dtype) {
49 if (0 == dtype) {
50 canvas.setDevice(new SkDevice(SkBitmap::kARGB_8888_Config,
51 256,
52 256,
53 false))->unref();
54 } else {
55#if SK_SCALAR_IS_FIXED
56 // GPU device known not to work in the fixed pt build.
57 continue;
58#endif
59 canvas.setDevice(new SkGpuDevice(context,
60 SkBitmap::kARGB_8888_Config,
61 256,
62 256))->unref();
63 }
64
65 SkBitmap readBmp1;
66 readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
67 readBmp1.allocPixels();
68 SkBitmap readBmp2;
69 readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
70 readBmp2.allocPixels();
71
72 for (size_t upmaIdx = 0;
73 upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
74 ++upmaIdx) {
75 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
76 {
77 SkAutoLockPixels alp1(readBmp1);
78 SkAutoLockPixels alp2(readBmp2);
79 sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
80 sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
81 }
82
83 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
84 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
85 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
86
87 SkAutoLockPixels alp1(readBmp1);
88 SkAutoLockPixels alp2(readBmp2);
89 uint32_t* pixels1 =
90 reinterpret_cast<uint32_t*>(readBmp1.getPixels());
91 uint32_t* pixels2 =
92 reinterpret_cast<uint32_t*>(readBmp2.getPixels());
93 for (int y = 0; y < 256; ++y) {
94 for (int x = 0; x < 256; ++x) {
95 int i = y * 256 + x;
96 REPORTER_ASSERT(reporter, pixels1[i] == pixels2[i]);
97 }
98 }
99 }
100 }
101}
102}
103
104#include "TestClassDef.h"
105DEFINE_GPUTESTCLASS("PremulAlphaRoundTripTest", PremulAlphaRoundTripTestClass, PremulAlphaRoundTripTest)
106