blob: eb7c663254af2962b13cb9bd90c92f1eaf893de4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +00007
reed@android.com42263962009-05-01 04:00:01 +00008#include "SkBitmap.h"
reed@android.com311c82d2009-05-05 23:13:23 +00009#include "SkRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
reed@android.com42263962009-05-01 04:00:01 +000011
12static const char* boolStr(bool value) {
13 return value ? "true" : "false";
14}
15
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000016// these are in the same order as the SkColorType enum
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000017static const char* gColorTypeName[] = {
18 "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8"
reed@android.com42263962009-05-01 04:00:01 +000019};
20
reed@android.comcafc9f92009-08-22 03:44:57 +000021static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
22 const SkBitmap& dst) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000023 ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d",
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000024 gColorTypeName[src.colorType()], src.isOpaque(),
25 gColorTypeName[dst.colorType()], dst.isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +000026}
27
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000028static bool canHaveAlpha(SkColorType ct) {
29 return kRGB_565_SkColorType != ct;
reed@android.comcafc9f92009-08-22 03:44:57 +000030}
31
32// copyTo() should preserve isOpaque when it makes sense
reed@google.com0a6151d2013-10-10 14:44:56 +000033static void test_isOpaque(skiatest::Reporter* reporter,
34 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000035 SkColorType dstColorType) {
reed@android.comcafc9f92009-08-22 03:44:57 +000036 SkBitmap dst;
37
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000038 if (canHaveAlpha(srcPremul.colorType()) && canHaveAlpha(dstColorType)) {
39 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstColorType));
40 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
reed@google.com0a6151d2013-10-10 14:44:56 +000041 if (srcPremul.isOpaque() != dst.isOpaque()) {
42 report_opaqueness(reporter, srcPremul, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000043 }
44 }
45
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000046 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstColorType));
47 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
reed@google.com0a6151d2013-10-10 14:44:56 +000048 if (srcOpaque.isOpaque() != dst.isOpaque()) {
49 report_opaqueness(reporter, srcOpaque, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000050 }
51}
52
reed@google.com0a6151d2013-10-10 14:44:56 +000053static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000054 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000055 if (bitmap.getPixels()) {
reed@google.com0a6151d2013-10-10 14:44:56 +000056 if (bitmap.getColorTable()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000057 sk_bzero(bitmap.getPixels(), bitmap.getSize());
58 } else {
59 bitmap.eraseColor(SK_ColorWHITE);
60 }
reed@android.com42263962009-05-01 04:00:01 +000061 }
62}
63
reedc5e15a12014-09-29 12:10:27 -070064static SkColorTable* init_ctable() {
reed@android.com42263962009-05-01 04:00:01 +000065 static const SkColor colors[] = {
66 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
67 };
reedc5e15a12014-09-29 12:10:27 -070068 return new SkColorTable(colors, SK_ARRAY_COUNT(colors));
reed@android.com42263962009-05-01 04:00:01 +000069}
70
71struct Pair {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000072 SkColorType fColorType;
73 const char* fValid;
reed@android.com42263962009-05-01 04:00:01 +000074};
75
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000076// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
77// getPixel()
78// setPixel()
79// getSkConfigName()
80// struct Coordinates
81// reportCopyVerification()
82// writeCoordPixels()
83
84// Utility function to read the value of a given pixel in bm. All
85// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +000086static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000087 uint32_t val = 0;
88 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +000089 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000090 SkAutoLockPixels lock(bm);
91 const void* rawAddr = bm.getAddr(x,y);
92
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000093 switch (bm.bytesPerPixel()) {
94 case 4:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000095 memcpy(&val, rawAddr, sizeof(uint32_t));
96 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000097 case 2:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000098 memcpy(&val16, rawAddr, sizeof(uint16_t));
99 val = val16;
100 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000101 case 1:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000102 memcpy(&val8, rawAddr, sizeof(uint8_t));
103 val = val8;
104 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000105 default:
106 break;
107 }
108 return val;
109}
110
111// Utility function to set value of any pixel in bm.
112// bm.getConfig() specifies what format 'val' must be
113// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000114static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000115 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000116 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000117 SkAutoLockPixels lock(bm);
118 void* rawAddr = bm.getAddr(x,y);
119
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000120 switch (bm.bytesPerPixel()) {
121 case 4:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000122 memcpy(rawAddr, &val, sizeof(uint32_t));
123 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000124 case 2:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000125 val16 = val & 0xFFFF;
126 memcpy(rawAddr, &val16, sizeof(uint16_t));
127 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000128 case 1:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000129 val8 = val & 0xFF;
130 memcpy(rawAddr, &val8, sizeof(uint8_t));
131 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000132 default:
133 // Ignore.
134 break;
135 }
136}
137
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000138// Helper struct to contain pixel locations, while avoiding need for STL.
139struct Coordinates {
140
141 const int length;
142 SkIPoint* const data;
143
144 explicit Coordinates(int _length): length(_length)
145 , data(new SkIPoint[length]) { }
146
147 ~Coordinates(){
148 delete [] data;
149 }
150
151 SkIPoint* operator[](int i) const {
152 // Use with care, no bounds checking.
153 return data + i;
154 }
155};
156
157// A function to verify that two bitmaps contain the same pixel values
158// at all coordinates indicated by coords. Simplifies verification of
159// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000160static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000161 Coordinates& coords,
162 const char* msg,
163 skiatest::Reporter* reporter){
164 bool success = true;
165
166 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000167 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000168 success = success &&
169 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
170 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000171 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000172
173 if (!success) {
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000174 ERRORF(reporter, "%s [colortype = %s]", msg,
175 gColorTypeName[bm1.colorType()]);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000176 }
177}
178
179// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000180static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000181 for (int i = 0; i < coords.length; ++i)
182 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
183}
184
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000185static const Pair gPairs[] = {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000186 { kUnknown_SkColorType, "000000" },
187 { kAlpha_8_SkColorType, "010101" },
reedb184f7f2014-07-13 04:32:32 -0700188 { kIndex_8_SkColorType, "011111" },
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000189 { kRGB_565_SkColorType, "010101" },
190 { kARGB_4444_SkColorType, "010111" },
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000191 { kN32_SkColorType, "010111" },
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000192};
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000193
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000194static const int W = 20;
195static const int H = 33;
196
197static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000198 SkColorType ct) {
reedc5e15a12014-09-29 12:10:27 -0700199 SkColorTable* ctable = NULL;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000200 if (kIndex_8_SkColorType == ct) {
reedc5e15a12014-09-29 12:10:27 -0700201 ctable = init_ctable();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000202 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000203
204 srcOpaque->allocPixels(SkImageInfo::Make(W, H, ct, kOpaque_SkAlphaType),
reedc5e15a12014-09-29 12:10:27 -0700205 NULL, ctable);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000206 srcPremul->allocPixels(SkImageInfo::Make(W, H, ct, kPremul_SkAlphaType),
reedc5e15a12014-09-29 12:10:27 -0700207 NULL, ctable);
208 SkSafeUnref(ctable);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000209 init_src(*srcOpaque);
210 init_src(*srcPremul);
211}
212
213DEF_TEST(BitmapCopy_extractSubset, reporter) {
214 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
215 SkBitmap srcOpaque, srcPremul;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000216 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000217
218 SkBitmap bitmap(srcOpaque);
219 SkBitmap subset;
220 SkIRect r;
221 // Extract a subset which has the same width as the original. This
222 // catches a bug where we cloned the genID incorrectly.
223 r.set(0, 1, W, 3);
224 bitmap.setIsVolatile(true);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000225 // Relies on old behavior of extractSubset failing if colortype is unknown
226 if (kUnknown_SkColorType != bitmap.colorType() && bitmap.extractSubset(&subset, r)) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000227 REPORTER_ASSERT(reporter, subset.width() == W);
228 REPORTER_ASSERT(reporter, subset.height() == 2);
229 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
230 REPORTER_ASSERT(reporter, subset.isVolatile() == true);
231
232 // Test copying an extracted subset.
233 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
234 SkBitmap copy;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000235 bool success = subset.copyTo(&copy, gPairs[j].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000236 if (!success) {
237 // Skip checking that success matches fValid, which is redundant
238 // with the code below.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000239 REPORTER_ASSERT(reporter, gPairs[i].fColorType != gPairs[j].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000240 continue;
241 }
242
243 // When performing a copy of an extracted subset, the gen id should
244 // change.
245 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID());
246
247 REPORTER_ASSERT(reporter, copy.width() == W);
248 REPORTER_ASSERT(reporter, copy.height() == 2);
249
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000250 if (gPairs[i].fColorType == gPairs[j].fColorType) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000251 SkAutoLockPixels alp0(subset);
252 SkAutoLockPixels alp1(copy);
253 // they should both have, or both not-have, a colortable
254 bool hasCT = subset.getColorTable() != NULL;
255 REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT);
256 }
257 }
258 }
259
260 bitmap = srcPremul;
261 bitmap.setIsVolatile(false);
262 if (bitmap.extractSubset(&subset, r)) {
263 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
264 REPORTER_ASSERT(reporter, subset.isVolatile() == false);
265 }
266 }
267}
268
269DEF_TEST(BitmapCopy, reporter) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000270 static const bool isExtracted[] = {
271 false, true
272 };
273
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000274 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000275 SkBitmap srcOpaque, srcPremul;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000276 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000277
reed@android.com42263962009-05-01 04:00:01 +0000278 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000279 SkBitmap dst;
reed@android.com42263962009-05-01 04:00:01 +0000280
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000281 bool success = srcPremul.copyTo(&dst, gPairs[j].fColorType);
reed@android.com42263962009-05-01 04:00:01 +0000282 bool expected = gPairs[i].fValid[j] != '0';
283 if (success != expected) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000284 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000285 "returned %s", gColorTypeName[i], gColorTypeName[j],
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000286 boolStr(expected), boolStr(success));
reed@android.com42263962009-05-01 04:00:01 +0000287 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000288
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000289 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fColorType);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000290 if (success != canSucceed) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000291 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s "
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000292 "canCopyTo %s", gColorTypeName[i], gColorTypeName[j],
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000293 boolStr(success), boolStr(canSucceed));
reed@android.comfbaa88d2009-05-06 17:44:34 +0000294 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000295
reed@android.com42263962009-05-01 04:00:01 +0000296 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000297 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
298 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000299 REPORTER_ASSERT(reporter, dst.colorType() == gPairs[j].fColorType);
300 test_isOpaque(reporter, srcOpaque, srcPremul, dst.colorType());
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000301 if (srcPremul.colorType() == dst.colorType()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000302 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000303 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000304 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000305 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000306 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000307 const char* dstP = (const char*)dst.getAddr(0, 0);
308 REPORTER_ASSERT(reporter, srcP != dstP);
309 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000310 srcPremul.getSize()));
311 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000312 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000313 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000314 }
reed@android.com42263962009-05-01 04:00:01 +0000315 } else {
316 // dst should be unchanged from its initial state
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000317 REPORTER_ASSERT(reporter, dst.colorType() == kUnknown_SkColorType);
reed@android.com42263962009-05-01 04:00:01 +0000318 REPORTER_ASSERT(reporter, dst.width() == 0);
319 REPORTER_ASSERT(reporter, dst.height() == 0);
320 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000321 } // for (size_t j = ...
322
323 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
324 // copyPixelsFrom().
325 //
326 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
327 ++copyCase) {
328 // Test copying to/from external buffer.
329 // Note: the tests below have hard-coded values ---
330 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000331
reed@google.com2cb14802013-06-26 14:35:02 +0000332 // Tests for getSafeSize64().
333 // Test with a very large configuration without pixel buffer
334 // attached.
335 SkBitmap tstSafeSize;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000336 tstSafeSize.setInfo(SkImageInfo::Make(100000000U, 100000000U,
337 gPairs[i].fColorType, kPremul_SkAlphaType));
reed@google.com57212f92013-12-30 14:40:38 +0000338 int64_t safeSize = tstSafeSize.computeSafeSize64();
339 if (safeSize < 0) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000340 ERRORF(reporter, "getSafeSize64() negative: %s",
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000341 gColorTypeName[tstSafeSize.colorType()]);
reed@google.com2cb14802013-06-26 14:35:02 +0000342 }
343 bool sizeFail = false;
344 // Compare against hand-computed values.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000345 switch (gPairs[i].fColorType) {
346 case kUnknown_SkColorType:
reed@google.com2cb14802013-06-26 14:35:02 +0000347 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000348
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000349 case kAlpha_8_SkColorType:
350 case kIndex_8_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000351 if (safeSize != 0x2386F26FC10000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000352 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000353 }
reed@google.com2cb14802013-06-26 14:35:02 +0000354 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000355
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000356 case kRGB_565_SkColorType:
357 case kARGB_4444_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000358 if (safeSize != 0x470DE4DF820000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000359 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000360 }
reed@google.com2cb14802013-06-26 14:35:02 +0000361 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000362
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000363 case kN32_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000364 if (safeSize != 0x8E1BC9BF040000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000365 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000366 }
reed@google.com2cb14802013-06-26 14:35:02 +0000367 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000368
reed@google.com2cb14802013-06-26 14:35:02 +0000369 default:
370 break;
371 }
372 if (sizeFail) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000373 ERRORF(reporter, "computeSafeSize64() wrong size: %s",
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000374 gColorTypeName[tstSafeSize.colorType()]);
reed@google.com2cb14802013-06-26 14:35:02 +0000375 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000376
rmistry@google.comd6bab022013-12-02 13:50:38 +0000377 int subW = 2;
378 int subH = 2;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000379
reed@google.com2cb14802013-06-26 14:35:02 +0000380 // Create bitmap to act as source for copies and subsets.
381 SkBitmap src, subset;
382 SkColorTable* ct = NULL;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000383 if (kIndex_8_SkColorType == src.colorType()) {
reedc5e15a12014-09-29 12:10:27 -0700384 ct = init_ctable();
reed@google.com2cb14802013-06-26 14:35:02 +0000385 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000386
reed84825042014-09-02 12:50:45 -0700387 int localSubW;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000388 if (isExtracted[copyCase]) { // A larger image to extract from.
reed84825042014-09-02 12:50:45 -0700389 localSubW = 2 * subW + 1;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000390 } else { // Tests expect a 2x2 bitmap, so make smaller.
reed84825042014-09-02 12:50:45 -0700391 localSubW = subW;
392 }
393 // could fail if we pass kIndex_8 for the colortype
394 if (src.tryAllocPixels(SkImageInfo::Make(localSubW, subH, gPairs[i].fColorType,
395 kPremul_SkAlphaType))) {
396 // failure is fine, as we will notice later on
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000397 }
reed@google.com2cb14802013-06-26 14:35:02 +0000398 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000399
reed@google.com2cb14802013-06-26 14:35:02 +0000400 // Either copy src or extract into 'subset', which is used
401 // for subsequent calls to copyPixelsTo/From.
402 bool srcReady = false;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000403 // Test relies on older behavior that extractSubset will fail on
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000404 // kUnknown_SkColorType
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000405 if (kUnknown_SkColorType != src.colorType() &&
406 isExtracted[copyCase]) {
reed@google.com2cb14802013-06-26 14:35:02 +0000407 // The extractedSubset() test case allows us to test copy-
408 // ing when src and dst mave possibly different strides.
409 SkIRect r;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000410 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000411
reed@google.com2cb14802013-06-26 14:35:02 +0000412 srcReady = src.extractSubset(&subset, r);
413 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000414 srcReady = src.copyTo(&subset);
reed@google.com2cb14802013-06-26 14:35:02 +0000415 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000416
reed@google.com2cb14802013-06-26 14:35:02 +0000417 // Not all configurations will generate a valid 'subset'.
418 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000419
reed@google.com2cb14802013-06-26 14:35:02 +0000420 // Allocate our target buffer 'buf' for all copies.
421 // To simplify verifying correctness of copies attach
422 // buf to a SkBitmap, but copies are done using the
423 // raw buffer pointer.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000424 const size_t bufSize = subH *
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000425 SkColorTypeMinRowBytes(src.colorType(), subW) * 2;
reed@google.com2cb14802013-06-26 14:35:02 +0000426 SkAutoMalloc autoBuf (bufSize);
427 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000428
reed@google.com2cb14802013-06-26 14:35:02 +0000429 SkBitmap bufBm; // Attach buf to this bitmap.
430 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000431
reed@google.com2cb14802013-06-26 14:35:02 +0000432 // Set up values for each pixel being copied.
433 Coordinates coords(subW * subH);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000434 for (int x = 0; x < subW; ++x)
435 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000436 {
437 int index = y * subW + x;
438 SkASSERT(index < coords.length);
439 coords[index]->fX = x;
440 coords[index]->fY = y;
441 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000442
reed@google.com2cb14802013-06-26 14:35:02 +0000443 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000444
reed@google.com2cb14802013-06-26 14:35:02 +0000445 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000446
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000447 const SkImageInfo info = SkImageInfo::Make(subW, subH,
448 gPairs[i].fColorType,
449 kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000450 // Before/after comparisons easier if we attach buf
451 // to an appropriately configured SkBitmap.
452 memset(buf, 0xFF, bufSize);
453 // Config with stride greater than src but that fits in buf.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000454 bufBm.installPixels(info, buf, info.minRowBytes() * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000455 successExpected = false;
456 // Then attempt to copy with a stride that is too large
457 // to fit in the buffer.
458 REPORTER_ASSERT(reporter,
459 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
460 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000461
reed@google.com2cb14802013-06-26 14:35:02 +0000462 if (successExpected)
463 reportCopyVerification(subset, bufBm, coords,
464 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000465 reporter);
466
reed@google.com2cb14802013-06-26 14:35:02 +0000467 // Test #2 ////////////////////////////////////////////
468 // This test should always succeed, but in the case
469 // of extracted bitmaps only because we handle the
470 // issue of getSafeSize(). Without getSafeSize()
471 // buffer overrun/read would occur.
472 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000473 bufBm.installPixels(info, buf, subset.rowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000474 successExpected = subset.getSafeSize() <= bufSize;
475 REPORTER_ASSERT(reporter,
476 subset.copyPixelsTo(buf, bufSize) ==
477 successExpected);
478 if (successExpected)
479 reportCopyVerification(subset, bufBm, coords,
480 "copyPixelsTo(buf, bufSize)", reporter);
481
482 // Test #3 ////////////////////////////////////////////
483 // Copy with different stride between src and dst.
484 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000485 bufBm.installPixels(info, buf, subset.rowBytes()+1);
reed@google.com2cb14802013-06-26 14:35:02 +0000486 successExpected = true; // Should always work.
487 REPORTER_ASSERT(reporter,
488 subset.copyPixelsTo(buf, bufSize,
489 subset.rowBytes()+1) == successExpected);
490 if (successExpected)
491 reportCopyVerification(subset, bufBm, coords,
492 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
493
494 // Test #4 ////////////////////////////////////////////
495 // Test copy with stride too small.
496 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000497 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000498 successExpected = false;
499 // Request copy with stride too small.
500 REPORTER_ASSERT(reporter,
501 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
502 == successExpected);
503 if (successExpected)
504 reportCopyVerification(subset, bufBm, coords,
505 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
506
507#if 0 // copyPixelsFrom is gone
508 // Test #5 ////////////////////////////////////////////
509 // Tests the case where the source stride is too small
510 // for the source configuration.
511 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000512 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000513 writeCoordPixels(bufBm, coords);
514 REPORTER_ASSERT(reporter,
515 subset.copyPixelsFrom(buf, bufSize, 1) == false);
516
517 // Test #6 ///////////////////////////////////////////
518 // Tests basic copy from an external buffer to the bitmap.
519 // If the bitmap is "extracted", this also tests the case
520 // where the source stride is different from the dest.
521 // stride.
522 // We've made the buffer large enough to always succeed.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000523 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000524 writeCoordPixels(bufBm, coords);
525 REPORTER_ASSERT(reporter,
526 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
527 true);
528 reportCopyVerification(bufBm, subset, coords,
529 "copyPixelsFrom(buf, bufSize)",
530 reporter);
531
532 // Test #7 ////////////////////////////////////////////
533 // Tests the case where the source buffer is too small
534 // for the transfer.
535 REPORTER_ASSERT(reporter,
536 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
537 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000538
reed@google.comab77aaf2011-11-01 16:03:35 +0000539#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000540 }
541 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000542 }
543}
reedb184f7f2014-07-13 04:32:32 -0700544
545#include "SkColorPriv.h"
546#include "SkUtils.h"
547
548/**
549 * Construct 4x4 pixels where we can look at a color and determine where it should be in the grid.
550 * alpha = 0xFF, blue = 0x80, red = x, green = y
551 */
552static void fill_4x4_pixels(SkPMColor colors[16]) {
553 for (int y = 0; y < 4; ++y) {
554 for (int x = 0; x < 4; ++x) {
555 colors[y*4+x] = SkPackARGB32(0xFF, x, y, 0x80);
556 }
557 }
558}
559
560static bool check_4x4_pixel(SkPMColor color, unsigned x, unsigned y) {
561 SkASSERT(x < 4 && y < 4);
562 return 0xFF == SkGetPackedA32(color) &&
563 x == SkGetPackedR32(color) &&
564 y == SkGetPackedG32(color) &&
565 0x80 == SkGetPackedB32(color);
566}
567
568/**
569 * Fill with all zeros, which will never match any value from fill_4x4_pixels
570 */
571static void clear_4x4_pixels(SkPMColor colors[16]) {
572 sk_memset32(colors, 0, 16);
573}
574
575// Much of readPixels is exercised by copyTo testing, since readPixels is the backend for that
576// method. Here we explicitly test subset copies.
577//
578DEF_TEST(BitmapReadPixels, reporter) {
579 const int W = 4;
580 const int H = 4;
581 const size_t rowBytes = W * sizeof(SkPMColor);
582 const SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(W, H);
583 SkPMColor srcPixels[16];
584 fill_4x4_pixels(srcPixels);
585 SkBitmap srcBM;
586 srcBM.installPixels(srcInfo, srcPixels, rowBytes);
587
588 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(W, H);
589 SkPMColor dstPixels[16];
590
591 const struct {
592 bool fExpectedSuccess;
593 SkIPoint fRequestedSrcLoc;
594 SkISize fRequestedDstSize;
595 // If fExpectedSuccess, check these, otherwise ignore
596 SkIPoint fExpectedDstLoc;
597 SkIRect fExpectedSrcR;
598 } gRec[] = {
599 { true, { 0, 0 }, { 4, 4 }, { 0, 0 }, { 0, 0, 4, 4 } },
600 { true, { 1, 1 }, { 2, 2 }, { 0, 0 }, { 1, 1, 3, 3 } },
601 { true, { 2, 2 }, { 4, 4 }, { 0, 0 }, { 2, 2, 4, 4 } },
602 { true, {-1,-1 }, { 2, 2 }, { 1, 1 }, { 0, 0, 1, 1 } },
603 { false, {-1,-1 }, { 1, 1 }, { 0, 0 }, { 0, 0, 0, 0 } },
604 };
605
606 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
607 clear_4x4_pixels(dstPixels);
608
reede5ea5002014-09-03 11:54:58 -0700609 dstInfo = dstInfo.makeWH(gRec[i].fRequestedDstSize.width(),
610 gRec[i].fRequestedDstSize.height());
reedb184f7f2014-07-13 04:32:32 -0700611 bool success = srcBM.readPixels(dstInfo, dstPixels, rowBytes,
612 gRec[i].fRequestedSrcLoc.x(), gRec[i].fRequestedSrcLoc.y());
613
614 REPORTER_ASSERT(reporter, gRec[i].fExpectedSuccess == success);
615 if (success) {
616 const SkIRect srcR = gRec[i].fExpectedSrcR;
617 const int dstX = gRec[i].fExpectedDstLoc.x();
618 const int dstY = gRec[i].fExpectedDstLoc.y();
619 // Walk the dst pixels, and check if we got what we expected
620 for (int y = 0; y < H; ++y) {
621 for (int x = 0; x < W; ++x) {
622 SkPMColor dstC = dstPixels[y*4+x];
623 // get into src coordinates
624 int sx = x - dstX + srcR.x();
625 int sy = y - dstY + srcR.y();
626 if (srcR.contains(sx, sy)) {
627 REPORTER_ASSERT(reporter, check_4x4_pixel(dstC, sx, sy));
628 } else {
629 REPORTER_ASSERT(reporter, 0 == dstC);
630 }
631 }
632 }
633 }
634 }
635}
636