blob: e0a7f78d8b02c82194c5a034d3b5023147da0099 [file] [log] [blame]
halcanary@google.com44287342013-12-13 18:29:51 +00001/*
2 * Copyright 2013 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
8#include "SkBitmap.h"
reed2ff257b2015-01-23 07:51:14 -08009#include "SkMallocPixelRef.h"
Hal Canary02e65df2016-12-07 16:21:53 -050010#include "SkRandom.h"
halcanary@google.com44287342013-12-13 18:29:51 +000011#include "Test.h"
Matt Sarett68b8e3d2017-04-28 11:15:22 -040012#include "sk_tool_utils.h"
halcanary@google.com44287342013-12-13 18:29:51 +000013
reedcb674142015-06-05 06:58:22 -070014static void test_peekpixels(skiatest::Reporter* reporter) {
15 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
16
17 SkPixmap pmap;
18 SkBitmap bm;
19
20 // empty should return false
halcanary96fcdcc2015-08-27 07:41:13 -070021 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
reedcb674142015-06-05 06:58:22 -070022 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
23
24 // no pixels should return false
25 bm.setInfo(SkImageInfo::MakeN32Premul(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -070026 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
reedcb674142015-06-05 06:58:22 -070027 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
28
29 // real pixels should return true
30 bm.allocPixels(info);
halcanary96fcdcc2015-08-27 07:41:13 -070031 REPORTER_ASSERT(reporter, bm.peekPixels(nullptr));
reedcb674142015-06-05 06:58:22 -070032 REPORTER_ASSERT(reporter, bm.peekPixels(&pmap));
33 REPORTER_ASSERT(reporter, pmap.info() == bm.info());
34 REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels());
35 REPORTER_ASSERT(reporter, pmap.rowBytes() == bm.rowBytes());
36 REPORTER_ASSERT(reporter, pmap.ctable() == bm.getColorTable());
37}
38
reed2ff257b2015-01-23 07:51:14 -080039// https://code.google.com/p/chromium/issues/detail?id=446164
40static void test_bigalloc(skiatest::Reporter* reporter) {
41 const int width = 0x40000001;
42 const int height = 0x00000096;
43 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
44
45 SkBitmap bm;
46 REPORTER_ASSERT(reporter, !bm.tryAllocPixels(info));
47
Mike Reed6b3155c2017-04-03 14:41:44 -040048 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes(), nullptr);
reed2ff257b2015-01-23 07:51:14 -080049 REPORTER_ASSERT(reporter, !pr);
50}
51
reedf0aed972014-07-01 12:48:11 -070052static void test_allocpixels(skiatest::Reporter* reporter) {
53 const int width = 10;
54 const int height = 10;
55 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
56 const size_t explicitRowBytes = info.minRowBytes() + 24;
57
58 SkBitmap bm;
59 bm.setInfo(info);
60 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
61 bm.allocPixels();
62 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
63 bm.reset();
64 bm.allocPixels(info);
65 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
66
67 bm.setInfo(info, explicitRowBytes);
68 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
69 bm.allocPixels();
70 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
71 bm.reset();
72 bm.allocPixels(info, explicitRowBytes);
73 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
74
75 bm.reset();
76 bm.setInfo(info, 0);
77 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
78 bm.reset();
79 bm.allocPixels(info, 0);
80 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
81
82 bm.reset();
83 bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit
84 REPORTER_ASSERT(reporter, !success);
85 REPORTER_ASSERT(reporter, bm.isNull());
86}
87
reed@google.com48569642013-12-30 19:21:22 +000088static void test_bigwidth(skiatest::Reporter* reporter) {
89 SkBitmap bm;
90 int width = 1 << 29; // *4 will be the high-bit of 32bit int
91
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000092 SkImageInfo info = SkImageInfo::MakeA8(width, 1);
93 REPORTER_ASSERT(reporter, bm.setInfo(info));
reede5ea5002014-09-03 11:54:58 -070094 REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType)));
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +000095
reed@google.com48569642013-12-30 19:21:22 +000096 // for a 4-byte config, this width will compute a rowbytes of 0x80000000,
97 // which does not fit in a int32_t. setConfig should detect this, and fail.
98
99 // TODO: perhaps skia can relax this, and only require that rowBytes fit
100 // in a uint32_t (or larger), but for now this is the constraint.
101
reede5ea5002014-09-03 11:54:58 -0700102 REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType)));
reed@google.com48569642013-12-30 19:21:22 +0000103}
104
halcanary@google.com44287342013-12-13 18:29:51 +0000105/**
106 * This test contains basic sanity checks concerning bitmaps.
107 */
108DEF_TEST(Bitmap, reporter) {
halcanary@google.com44287342013-12-13 18:29:51 +0000109 // Zero-sized bitmaps are allowed
110 for (int width = 0; width < 2; ++width) {
111 for (int height = 0; height < 2; ++height) {
112 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000113 bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
halcanary@google.com44287342013-12-13 18:29:51 +0000114 REPORTER_ASSERT(reporter, setConf);
115 if (setConf) {
reed84825042014-09-02 12:50:45 -0700116 bm.allocPixels();
halcanary@google.com44287342013-12-13 18:29:51 +0000117 }
halcanary@google.com2af6c122013-12-13 19:25:21 +0000118 REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty());
halcanary@google.com44287342013-12-13 18:29:51 +0000119 }
120 }
skia.committer@gmail.comf5e1f632013-12-31 07:01:36 +0000121
reed@google.com48569642013-12-30 19:21:22 +0000122 test_bigwidth(reporter);
reedf0aed972014-07-01 12:48:11 -0700123 test_allocpixels(reporter);
reed2ff257b2015-01-23 07:51:14 -0800124 test_bigalloc(reporter);
reedcb674142015-06-05 06:58:22 -0700125 test_peekpixels(reporter);
halcanary@google.com44287342013-12-13 18:29:51 +0000126}
halcanaryb260d132015-12-09 10:21:59 -0800127
128/**
129 * This test checks that getColor works for both swizzles.
130 */
131DEF_TEST(Bitmap_getColor_Swizzle, r) {
132 SkBitmap source;
133 source.allocN32Pixels(1,1);
134 source.eraseColor(SK_ColorRED);
135 SkColorType colorTypes[] = {
136 kRGBA_8888_SkColorType,
137 kBGRA_8888_SkColorType,
138 };
139 for (SkColorType ct : colorTypes) {
140 SkBitmap copy;
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400141 if (!sk_tool_utils::copy_to(&copy, ct, source)) {
halcanaryb260d132015-12-09 10:21:59 -0800142 ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
143 continue;
144 }
halcanaryb260d132015-12-09 10:21:59 -0800145 REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
146 }
147}
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800148
149static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
150 SkColor expected) {
151 SkBitmap bm;
152 bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
153 bm.eraseColor(input);
halcanary7d571242016-02-24 17:59:16 -0800154 INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800155 REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
156}
157
158/**
159 * This test checks that eraseColor premultiplies the color correctly.
160 */
161DEF_TEST(Bitmap_eraseColor_Premul, r) {
162 SkColor color = 0x80FF0080;
163 test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
164 test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
165 test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
166 test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
167 test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
168}
Hal Canary4cba3fe2016-12-07 14:59:27 -0500169
170// Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
171DEF_TEST(Bitmap_compute_is_opaque, r) {
172 struct {
173 SkColorType fCT;
174 SkAlphaType fAT;
175 } types[] = {
176 { kGray_8_SkColorType, kOpaque_SkAlphaType },
177 { kAlpha_8_SkColorType, kPremul_SkAlphaType },
178 { kARGB_4444_SkColorType, kPremul_SkAlphaType },
179 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
180 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
181 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
182 { kRGBA_F16_SkColorType, kPremul_SkAlphaType },
183 };
184 for (auto type : types) {
185 SkBitmap bm;
186 REPORTER_ASSERT(r, !SkBitmap::ComputeIsOpaque(bm));
187
188 bm.allocPixels(SkImageInfo::Make(13, 17, type.fCT, type.fAT));
189 bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
190 REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm));
191
192 bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
193 bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
194 bool shouldBeOpaque = (type.fAT == kOpaque_SkAlphaType);
195 REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
196 }
197}
198
199// Test that erase+getColor round trips with RGBA_F16 pixels.
200DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
201 SkRandom random;
202 SkPixmap pm;
203 SkBitmap bm;
204 bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
205 REPORTER_ASSERT(r, bm.peekPixels(&pm));
206 for (unsigned i = 0; i < 0x100; ++i) {
207 // Test all possible values of blue component.
208 SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
209 // Test all possible values of alpha component.
210 SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
211 for (SkColor color : {color1, color2}) {
212 pm.erase(color);
213 if (SkColorGetA(color) != 0) {
214 REPORTER_ASSERT(r, color == pm.getColor(0, 0));
215 } else {
216 REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
217 }
218 }
219 }
220}
221