blob: a22ff5ae13436d6bebfc47c2fd3b12c04ff9b05d [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.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
18
19namespace {
20
21void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) {
22 SkBitmap bmp;
23 bmp.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
24 bmp.allocPixels();
25 SkAutoLockPixels alp(bmp);
26 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
27
28 for (int a = 0; a < 256; ++a) {
29 for (int r = 0; r < 256; ++r) {
30 pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0);
31 }
32 }
33 canvas->writePixels(bmp, 0, 0, unpremulConfig);
34}
35
36static const SkCanvas::Config8888 gUnpremulConfigs[] = {
37 SkCanvas::kNative_Unpremul_Config8888,
38/**
39 * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438)
40 * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to
41 * fail. This should be removed as soon as the issue above is resolved.
42 */
43#if !defined(SK_BUILD_FOR_ANDROID)
44 SkCanvas::kBGRA_Unpremul_Config8888,
45#endif
46 SkCanvas::kRGBA_Unpremul_Config8888,
47};
48
49void PremulAlphaRoundTripTest(skiatest::Reporter* reporter,
50 GrContext* context) {
51 SkCanvas canvas;
52 for (int dtype = 0; dtype < 2; ++dtype) {
53 if (0 == dtype) {
54 canvas.setDevice(new SkDevice(SkBitmap::kARGB_8888_Config,
55 256,
56 256,
57 false))->unref();
58 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000059#if !SK_SUPPORT_GPU || defined(SK_SCALAR_IS_FIXED)
bsalomon@google.coma91e9232012-02-23 15:39:54 +000060 // GPU device known not to work in the fixed pt build.
61 continue;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000062#else
bsalomon@google.coma91e9232012-02-23 15:39:54 +000063 canvas.setDevice(new SkGpuDevice(context,
64 SkBitmap::kARGB_8888_Config,
65 256,
66 256))->unref();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000067#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +000068 }
69
70 SkBitmap readBmp1;
71 readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
72 readBmp1.allocPixels();
73 SkBitmap readBmp2;
74 readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
75 readBmp2.allocPixels();
76
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
92 SkAutoLockPixels alp1(readBmp1);
93 SkAutoLockPixels alp2(readBmp2);
94 uint32_t* pixels1 =
95 reinterpret_cast<uint32_t*>(readBmp1.getPixels());
96 uint32_t* pixels2 =
97 reinterpret_cast<uint32_t*>(readBmp2.getPixels());
98 for (int y = 0; y < 256; ++y) {
99 for (int x = 0; x < 256; ++x) {
100 int i = y * 256 + x;
101 REPORTER_ASSERT(reporter, pixels1[i] == pixels2[i]);
102 }
103 }
104 }
105 }
106}
107}
108
109#include "TestClassDef.h"
110DEFINE_GPUTESTCLASS("PremulAlphaRoundTripTest", PremulAlphaRoundTripTestClass, PremulAlphaRoundTripTest)
111