blob: e230f110259ad09a8fb71378ecc5864deb71a939 [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"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000012#include "sk_tool_utils.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.org4cd9e212014-03-07 03:25:16 +000019static uint32_t pack_unpremul_rgba(SkColor c) {
20 uint32_t packed;
21 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
22 byte[0] = SkColorGetR(c);
23 byte[1] = SkColorGetG(c);
24 byte[2] = SkColorGetB(c);
25 byte[3] = SkColorGetA(c);
26 return packed;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000027}
28
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000029static uint32_t pack_unpremul_bgra(SkColor c) {
30 uint32_t packed;
31 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
32 byte[0] = SkColorGetB(c);
33 byte[1] = SkColorGetG(c);
34 byte[2] = SkColorGetR(c);
35 byte[3] = SkColorGetA(c);
36 return packed;
37}
38
39typedef uint32_t (*PackUnpremulProc)(SkColor);
40
41const struct {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000042 SkColorType fColorType;
43 PackUnpremulProc fPackProc;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000044} gUnpremul[] = {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000045 { kRGBA_8888_SkColorType, pack_unpremul_rgba },
46 { kBGRA_8888_SkColorType, pack_unpremul_bgra },
bsalomon@google.coma91e9232012-02-23 15:39:54 +000047};
48
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000049static void fillCanvas(SkCanvas* canvas, SkColorType colorType, PackUnpremulProc proc) {
50 // Don't strictly need a bitmap, but its a handy way to allocate the pixels
51 SkBitmap bmp;
52 bmp.allocN32Pixels(256, 256);
53
54 for (int a = 0; a < 256; ++a) {
55 uint32_t* pixels = bmp.getAddr32(0, a);
56 for (int r = 0; r < 256; ++r) {
57 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0));
58 }
59 }
60
reede5ea5002014-09-03 11:54:58 -070061 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(),
62 colorType, kUnpremul_SkAlphaType);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000063 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0);
64}
65
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000066DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000067 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
68
bsalomon@google.coma91e9232012-02-23 15:39:54 +000069 for (int dtype = 0; dtype < 2; ++dtype) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000070
71 int glCtxTypeCnt = 1;
72#if SK_SUPPORT_GPU
73 if (0 != dtype) {
74 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
bsalomon@google.coma91e9232012-02-23 15:39:54 +000075 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000076#endif
robertphillips9a53fd72015-06-22 09:46:59 -070077 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000078 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
mtklein184ff372014-07-15 14:38:53 -070079 SkAutoTUnref<SkBaseDevice> device;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000080 if (0 == dtype) {
robertphillips9a53fd72015-06-22 09:46:59 -070081 device.reset(SkBitmapDevice::Create(info, props));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000082 } else {
83#if SK_SUPPORT_GPU
84 GrContextFactory::GLContextType type =
85 static_cast<GrContextFactory::GLContextType>(glCtxType);
86 if (!GrContextFactory::IsRenderingGLContext(type)) {
87 continue;
88 }
bsalomonafe30052015-01-16 07:32:33 -080089 GrContext* ctx = factory->get(type);
90 if (NULL == ctx) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000091 continue;
92 }
bsalomon74f681d2015-06-23 14:38:48 -070093 device.reset(SkGpuDevice::Create(ctx, SkSurface::kNo_Budgeted, info, 0, &props,
94 SkGpuDevice::kUninit_InitContents));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000095#else
96 continue;
97#endif
98 }
99 SkCanvas canvas(device);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000100
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000101 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
102 fillCanvas(&canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000103
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000104 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
105 kUnpremul_SkAlphaType);
106 SkBitmap readBmp1;
107 readBmp1.allocPixels(info);
108 SkBitmap readBmp2;
109 readBmp2.allocPixels(info);
110
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000111 readBmp1.eraseColor(0);
112 readBmp2.eraseColor(0);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000113
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000114 canvas.readPixels(&readBmp1, 0, 0);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000115 sk_tool_utils::write_pixels(&canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].fColorType,
116 kUnpremul_SkAlphaType);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000117 canvas.readPixels(&readBmp2, 0, 0);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000118
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000119 bool success = true;
120 for (int y = 0; y < 256 && success; ++y) {
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000121 const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
122 const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000123 for (int x = 0; x < 256 && success; ++x) {
mtkleine83909f2014-07-30 12:37:19 -0700124 // We see sporadic failures here. May help to see where it goes wrong.
125 if (pixels1[x] != pixels2[x]) {
126 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
127 }
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000128 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000129 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000130 }
131 }
132 }
133 }
134}