blob: 77f62dcecd960432919cb256f7304304a3ed0e78 [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;
20 bmp.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
21 bmp.allocPixels();
22 SkAutoLockPixels alp(bmp);
23 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
24
25 for (int a = 0; a < 256; ++a) {
26 for (int r = 0; r < 256; ++r) {
27 pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0);
28 }
29 }
30 canvas->writePixels(bmp, 0, 0, unpremulConfig);
31}
32
33static const SkCanvas::Config8888 gUnpremulConfigs[] = {
34 SkCanvas::kNative_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000035 SkCanvas::kBGRA_Unpremul_Config8888,
bsalomon@google.coma91e9232012-02-23 15:39:54 +000036 SkCanvas::kRGBA_Unpremul_Config8888,
37};
38
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000039DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000040 SkAutoTUnref<SkBaseDevice> device;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000041 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000042
43 int glCtxTypeCnt = 1;
44#if SK_SUPPORT_GPU
45 if (0 != dtype) {
46 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000047 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000048#endif
49 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
50 if (0 == dtype) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000051 device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
52 256,
53 256,
54 false));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000055 } else {
56#if SK_SUPPORT_GPU
57 GrContextFactory::GLContextType type =
58 static_cast<GrContextFactory::GLContextType>(glCtxType);
59 if (!GrContextFactory::IsRenderingGLContext(type)) {
60 continue;
61 }
62 GrContext* context = factory->get(type);
63 if (NULL == context) {
64 continue;
65 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +000066
bsalomon@google.com67b915d2013-02-04 16:13:32 +000067 device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256));
68#else
69 continue;
70#endif
71 }
72 SkCanvas canvas(device);
bsalomon@google.coma91e9232012-02-23 15:39:54 +000073
bsalomon@google.com67b915d2013-02-04 16:13:32 +000074 SkBitmap readBmp1;
75 readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
76 readBmp1.allocPixels();
77 SkBitmap readBmp2;
78 readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
79 readBmp2.allocPixels();
80
81 for (size_t upmaIdx = 0;
82 upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
83 ++upmaIdx) {
84 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
85 {
86 SkAutoLockPixels alp1(readBmp1);
87 SkAutoLockPixels alp2(readBmp2);
88 sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
89 sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
90 }
91
92 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
93 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
94 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
95
bsalomon@google.coma91e9232012-02-23 15:39:54 +000096 SkAutoLockPixels alp1(readBmp1);
97 SkAutoLockPixels alp2(readBmp2);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000098 uint32_t* pixels1 =
99 reinterpret_cast<uint32_t*>(readBmp1.getPixels());
100 uint32_t* pixels2 =
101 reinterpret_cast<uint32_t*>(readBmp2.getPixels());
102 bool success = true;
103 for (int y = 0; y < 256 && success; ++y) {
104 for (int x = 0; x < 256 && success; ++x) {
105 int i = y * 256 + x;
106 REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]);
107 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000108 }
109 }
110 }
111 }
112}