blob: 38453c89b6a1cd79a0103ecab2228173d422dd8b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
reed@android.com42263962009-05-01 04:00:01 +00008#include "Test.h"
9#include "SkBitmap.h"
reed@android.com311c82d2009-05-05 23:13:23 +000010#include "SkRect.h"
reed@android.com42263962009-05-01 04:00:01 +000011
12static const char* boolStr(bool value) {
13 return value ? "true" : "false";
14}
15
16// these are in the same order as the SkBitmap::Config enum
17static const char* gConfigName[] = {
18 "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8"
19};
20
reed@android.comcafc9f92009-08-22 03:44:57 +000021static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
22 const SkBitmap& dst) {
23 SkString str;
24 str.printf("src %s opaque:%d, dst %s opaque:%d",
25 gConfigName[src.config()], src.isOpaque(),
26 gConfigName[dst.config()], dst.isOpaque());
27 reporter->reportFailed(str);
28}
29
30static bool canHaveAlpha(SkBitmap::Config config) {
31 return config != SkBitmap::kRGB_565_Config;
32}
33
34// copyTo() should preserve isOpaque when it makes sense
reed@google.com0a6151d2013-10-10 14:44:56 +000035static void test_isOpaque(skiatest::Reporter* reporter,
36 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
reed@android.comcafc9f92009-08-22 03:44:57 +000037 SkBitmap::Config dstConfig) {
reed@android.comcafc9f92009-08-22 03:44:57 +000038 SkBitmap dst;
39
reed@google.com0a6151d2013-10-10 14:44:56 +000040 if (canHaveAlpha(srcPremul.config()) && canHaveAlpha(dstConfig)) {
41 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000042 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000043 if (srcPremul.isOpaque() != dst.isOpaque()) {
44 report_opaqueness(reporter, srcPremul, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000045 }
46 }
47
reed@google.com0a6151d2013-10-10 14:44:56 +000048 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000049 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000050 if (srcOpaque.isOpaque() != dst.isOpaque()) {
51 report_opaqueness(reporter, srcOpaque, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000052 }
53}
54
reed@google.com0a6151d2013-10-10 14:44:56 +000055static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000056 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000057 if (bitmap.getPixels()) {
reed@google.com0a6151d2013-10-10 14:44:56 +000058 if (bitmap.getColorTable()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000059 sk_bzero(bitmap.getPixels(), bitmap.getSize());
60 } else {
61 bitmap.eraseColor(SK_ColorWHITE);
62 }
reed@android.com42263962009-05-01 04:00:01 +000063 }
64}
65
reed@google.com0a6151d2013-10-10 14:44:56 +000066static SkColorTable* init_ctable(SkAlphaType alphaType) {
reed@android.com42263962009-05-01 04:00:01 +000067 static const SkColor colors[] = {
68 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
69 };
reed@google.com0a6151d2013-10-10 14:44:56 +000070 return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType);
reed@android.com42263962009-05-01 04:00:01 +000071}
72
73struct Pair {
74 SkBitmap::Config fConfig;
75 const char* fValid;
76};
77
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000078// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
79// getPixel()
80// setPixel()
81// getSkConfigName()
82// struct Coordinates
83// reportCopyVerification()
84// writeCoordPixels()
85
86// Utility function to read the value of a given pixel in bm. All
87// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +000088static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000089 uint32_t val = 0;
90 uint16_t val16;
91 uint8_t val8, shift;
92 SkAutoLockPixels lock(bm);
93 const void* rawAddr = bm.getAddr(x,y);
94
95 switch (bm.getConfig()) {
96 case SkBitmap::kARGB_8888_Config:
97 memcpy(&val, rawAddr, sizeof(uint32_t));
98 break;
99 case SkBitmap::kARGB_4444_Config:
100 case SkBitmap::kRGB_565_Config:
101 memcpy(&val16, rawAddr, sizeof(uint16_t));
102 val = val16;
103 break;
104 case SkBitmap::kA8_Config:
105 case SkBitmap::kIndex8_Config:
106 memcpy(&val8, rawAddr, sizeof(uint8_t));
107 val = val8;
108 break;
109 case SkBitmap::kA1_Config:
110 memcpy(&val8, rawAddr, sizeof(uint8_t));
111 shift = x % 8;
112 val = (val8 >> shift) & 0x1 ;
113 break;
114 default:
115 break;
116 }
117 return val;
118}
119
120// Utility function to set value of any pixel in bm.
121// bm.getConfig() specifies what format 'val' must be
122// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000123static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000124 uint16_t val16;
125 uint8_t val8, shift;
126 SkAutoLockPixels lock(bm);
127 void* rawAddr = bm.getAddr(x,y);
128
129 switch (bm.getConfig()) {
130 case SkBitmap::kARGB_8888_Config:
131 memcpy(rawAddr, &val, sizeof(uint32_t));
132 break;
133 case SkBitmap::kARGB_4444_Config:
134 case SkBitmap::kRGB_565_Config:
135 val16 = val & 0xFFFF;
136 memcpy(rawAddr, &val16, sizeof(uint16_t));
137 break;
138 case SkBitmap::kA8_Config:
139 case SkBitmap::kIndex8_Config:
140 val8 = val & 0xFF;
141 memcpy(rawAddr, &val8, sizeof(uint8_t));
142 break;
143 case SkBitmap::kA1_Config:
144 shift = x % 8; // We assume we're in the right byte.
145 memcpy(&val8, rawAddr, sizeof(uint8_t));
146 if (val & 0x1) // Turn bit on.
147 val8 |= (0x1 << shift);
148 else // Turn bit off.
149 val8 &= ~(0x1 << shift);
150 memcpy(rawAddr, &val8, sizeof(uint8_t));
151 break;
152 default:
153 // Ignore.
154 break;
155 }
156}
157
158// Utility to return string containing name of each format, to
159// simplify diagnostic output.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000160static const char* getSkConfigName(const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000161 switch (bm.getConfig()) {
162 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
163 case SkBitmap::kA1_Config: return "SkBitmap::kA1_Config";
164 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
165 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
166 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
167 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
168 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000169 default: return "Unknown SkBitmap configuration.";
170 }
171}
172
173// Helper struct to contain pixel locations, while avoiding need for STL.
174struct Coordinates {
175
176 const int length;
177 SkIPoint* const data;
178
179 explicit Coordinates(int _length): length(_length)
180 , data(new SkIPoint[length]) { }
181
182 ~Coordinates(){
183 delete [] data;
184 }
185
186 SkIPoint* operator[](int i) const {
187 // Use with care, no bounds checking.
188 return data + i;
189 }
190};
191
192// A function to verify that two bitmaps contain the same pixel values
193// at all coordinates indicated by coords. Simplifies verification of
194// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000195static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000196 Coordinates& coords,
197 const char* msg,
198 skiatest::Reporter* reporter){
199 bool success = true;
200
201 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000202 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000203 success = success &&
204 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
205 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000206 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000207
208 if (!success) {
209 SkString str;
210 str.printf("%s [config = %s]",
211 msg, getSkConfigName(bm1));
212 reporter->reportFailed(str);
213 }
214}
215
216// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000217static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000218 for (int i = 0; i < coords.length; ++i)
219 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
220}
221
reed@android.com42263962009-05-01 04:00:01 +0000222static void TestBitmapCopy(skiatest::Reporter* reporter) {
223 static const Pair gPairs[] = {
224 { SkBitmap::kNo_Config, "00000000" },
weita@google.comf9ab99a2009-05-03 18:23:30 +0000225 { SkBitmap::kA1_Config, "01000000" },
reed@google.com6ba45722013-06-21 18:30:53 +0000226 { SkBitmap::kA8_Config, "00101010" },
227 { SkBitmap::kIndex8_Config, "00111010" },
228 { SkBitmap::kRGB_565_Config, "00101010" },
reed@android.com42263962009-05-01 04:00:01 +0000229 { SkBitmap::kARGB_4444_Config, "00101110" },
scroggo@google.com7bb36ab2013-08-07 19:39:56 +0000230 { SkBitmap::kARGB_8888_Config, "00101110" },
reed@android.com42263962009-05-01 04:00:01 +0000231 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000232
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000233 static const bool isExtracted[] = {
234 false, true
235 };
236
reed@android.com42263962009-05-01 04:00:01 +0000237 const int W = 20;
238 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000239
reed@android.com42263962009-05-01 04:00:01 +0000240 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
241 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000242 SkBitmap srcOpaque, srcPremul, dst;
243
244 {
245 SkColorTable* ctOpaque = NULL;
246 SkColorTable* ctPremul = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000247
reed@google.com0a6151d2013-10-10 14:44:56 +0000248 srcOpaque.setConfig(gPairs[i].fConfig, W, H);
249 srcPremul.setConfig(gPairs[i].fConfig, W, H);
250 if (SkBitmap::kIndex8_Config == gPairs[i].fConfig) {
251 ctOpaque = init_ctable(kOpaque_SkAlphaType);
252 ctPremul = init_ctable(kPremul_SkAlphaType);
253 }
254 srcOpaque.allocPixels(ctOpaque);
255 srcPremul.allocPixels(ctPremul);
256 SkSafeUnref(ctOpaque);
257 SkSafeUnref(ctPremul);
258
259 srcOpaque.setIsOpaque(true);
260 srcPremul.setIsOpaque(false);
reed@android.com42263962009-05-01 04:00:01 +0000261 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000262 init_src(srcOpaque);
263 init_src(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000264
reed@google.com0a6151d2013-10-10 14:44:56 +0000265 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +0000266 bool expected = gPairs[i].fValid[j] != '0';
267 if (success != expected) {
268 SkString str;
269 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
270 gConfigName[i], gConfigName[j], boolStr(expected),
271 boolStr(success));
272 reporter->reportFailed(str);
273 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000274
reed@google.com0a6151d2013-10-10 14:44:56 +0000275 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000276 if (success != canSucceed) {
277 SkString str;
278 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
279 gConfigName[i], gConfigName[j], boolStr(success),
280 boolStr(canSucceed));
281 reporter->reportFailed(str);
282 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000283
reed@android.com42263962009-05-01 04:00:01 +0000284 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000285 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
286 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000287 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +0000288 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
289 if (srcPremul.config() == dst.config()) {
290 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000291 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000292 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000293 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000294 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000295 const char* dstP = (const char*)dst.getAddr(0, 0);
296 REPORTER_ASSERT(reporter, srcP != dstP);
297 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000298 srcPremul.getSize()));
299 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000300 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000301 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000302 }
reed@android.com311c82d2009-05-05 23:13:23 +0000303 // test extractSubset
304 {
reed@google.com0a6151d2013-10-10 14:44:56 +0000305 SkBitmap bitmap(srcOpaque);
reed@android.com311c82d2009-05-05 23:13:23 +0000306 SkBitmap subset;
307 SkIRect r;
308 r.set(1, 1, 2, 2);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000309 bitmap.setIsVolatile(true);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000310 if (bitmap.extractSubset(&subset, r)) {
reed@android.com311c82d2009-05-05 23:13:23 +0000311 REPORTER_ASSERT(reporter, subset.width() == 1);
312 REPORTER_ASSERT(reporter, subset.height() == 1);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000313 REPORTER_ASSERT(reporter,
bsalomon@google.com37688532012-10-12 13:42:36 +0000314 subset.isOpaque() == bitmap.isOpaque());
315 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000316 subset.isVolatile() == true);
reed@android.com311c82d2009-05-05 23:13:23 +0000317
318 SkBitmap copy;
319 REPORTER_ASSERT(reporter,
320 subset.copyTo(&copy, subset.config()));
321 REPORTER_ASSERT(reporter, copy.width() == 1);
322 REPORTER_ASSERT(reporter, copy.height() == 1);
323 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000324
reed@android.com311c82d2009-05-05 23:13:23 +0000325 SkAutoLockPixels alp0(subset);
326 SkAutoLockPixels alp1(copy);
327 // they should both have, or both not-have, a colortable
328 bool hasCT = subset.getColorTable() != NULL;
329 REPORTER_ASSERT(reporter,
330 (copy.getColorTable() != NULL) == hasCT);
331 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000332
333 bitmap = srcPremul;
skyostil@google.com0eb75762012-01-16 10:45:53 +0000334 bitmap.setIsVolatile(false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000335 if (bitmap.extractSubset(&subset, r)) {
336 REPORTER_ASSERT(reporter,
bsalomon@google.com37688532012-10-12 13:42:36 +0000337 subset.isOpaque() == bitmap.isOpaque());
338 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000339 subset.isVolatile() == false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000340 }
reed@android.com311c82d2009-05-05 23:13:23 +0000341 }
reed@android.com42263962009-05-01 04:00:01 +0000342 } else {
343 // dst should be unchanged from its initial state
344 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
345 REPORTER_ASSERT(reporter, dst.width() == 0);
346 REPORTER_ASSERT(reporter, dst.height() == 0);
347 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000348 } // for (size_t j = ...
349
350 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
351 // copyPixelsFrom().
352 //
353 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
354 ++copyCase) {
355 // Test copying to/from external buffer.
356 // Note: the tests below have hard-coded values ---
357 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000358
reed@google.com2cb14802013-06-26 14:35:02 +0000359 // Tests for getSafeSize64().
360 // Test with a very large configuration without pixel buffer
361 // attached.
362 SkBitmap tstSafeSize;
363 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
364 100000000U);
365 Sk64 safeSize = tstSafeSize.getSafeSize64();
366 if (safeSize.isNeg()) {
367 SkString str;
368 str.printf("getSafeSize64() negative: %s",
369 getSkConfigName(tstSafeSize));
370 reporter->reportFailed(str);
371 }
372 bool sizeFail = false;
373 // Compare against hand-computed values.
374 switch (gPairs[i].fConfig) {
375 case SkBitmap::kNo_Config:
376 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000377
reed@google.com2cb14802013-06-26 14:35:02 +0000378 case SkBitmap::kA1_Config:
379 if (safeSize.fHi != 0x470DE ||
380 safeSize.fLo != 0x4DF82000)
381 sizeFail = true;
382 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000383
reed@google.com2cb14802013-06-26 14:35:02 +0000384 case SkBitmap::kA8_Config:
385 case SkBitmap::kIndex8_Config:
386 if (safeSize.fHi != 0x2386F2 ||
387 safeSize.fLo != 0x6FC10000)
388 sizeFail = true;
389 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000390
reed@google.com2cb14802013-06-26 14:35:02 +0000391 case SkBitmap::kRGB_565_Config:
392 case SkBitmap::kARGB_4444_Config:
393 if (safeSize.fHi != 0x470DE4 ||
394 safeSize.fLo != 0xDF820000)
395 sizeFail = true;
396 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000397
reed@google.com2cb14802013-06-26 14:35:02 +0000398 case SkBitmap::kARGB_8888_Config:
399 if (safeSize.fHi != 0x8E1BC9 ||
400 safeSize.fLo != 0xBF040000)
401 sizeFail = true;
402 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000403
reed@google.com2cb14802013-06-26 14:35:02 +0000404 default:
405 break;
406 }
407 if (sizeFail) {
408 SkString str;
409 str.printf("getSafeSize64() wrong size: %s",
410 getSkConfigName(tstSafeSize));
411 reporter->reportFailed(str);
412 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000413
reed@google.com2cb14802013-06-26 14:35:02 +0000414 size_t subW, subH;
415 // Set sizes to be height = 2 to force the last row of the
416 // source to be used, thus verifying correct operation if
417 // the bitmap is an extracted subset.
418 if (gPairs[i].fConfig == SkBitmap::kA1_Config) {
419 // If one-bit per pixel, use 9 pixels to force more than
420 // one byte per row.
421 subW = 9;
422 subH = 2;
423 } else {
424 // All other configurations are at least one byte per pixel,
425 // and different configs will test copying different numbers
426 // of bytes.
427 subW = subH = 2;
428 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000429
reed@google.com2cb14802013-06-26 14:35:02 +0000430 // Create bitmap to act as source for copies and subsets.
431 SkBitmap src, subset;
432 SkColorTable* ct = NULL;
433 if (isExtracted[copyCase]) { // A larger image to extract from.
434 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
435 } else { // Tests expect a 2x2 bitmap, so make smaller.
436 src.setConfig(gPairs[i].fConfig, subW, subH);
437 }
438 if (SkBitmap::kIndex8_Config == src.config()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000439 ct = init_ctable(kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000440 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000441
reed@google.com2cb14802013-06-26 14:35:02 +0000442 src.allocPixels(ct);
443 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000444
reed@google.com2cb14802013-06-26 14:35:02 +0000445 // Either copy src or extract into 'subset', which is used
446 // for subsequent calls to copyPixelsTo/From.
447 bool srcReady = false;
448 if (isExtracted[copyCase]) {
449 // The extractedSubset() test case allows us to test copy-
450 // ing when src and dst mave possibly different strides.
451 SkIRect r;
452 if (gPairs[i].fConfig == SkBitmap::kA1_Config)
453 // This config seems to need byte-alignment of
454 // extracted subset bits.
455 r.set(0, 0, subW, subH);
456 else
457 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000458
reed@google.com2cb14802013-06-26 14:35:02 +0000459 srcReady = src.extractSubset(&subset, r);
460 } else {
461 srcReady = src.copyTo(&subset, src.getConfig());
462 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000463
reed@google.com2cb14802013-06-26 14:35:02 +0000464 // Not all configurations will generate a valid 'subset'.
465 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000466
reed@google.com2cb14802013-06-26 14:35:02 +0000467 // Allocate our target buffer 'buf' for all copies.
468 // To simplify verifying correctness of copies attach
469 // buf to a SkBitmap, but copies are done using the
470 // raw buffer pointer.
471 const uint32_t bufSize = subH *
472 SkBitmap::ComputeRowBytes(src.getConfig(), subW) * 2;
473 SkAutoMalloc autoBuf (bufSize);
474 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000475
reed@google.com2cb14802013-06-26 14:35:02 +0000476 SkBitmap bufBm; // Attach buf to this bitmap.
477 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000478
reed@google.com2cb14802013-06-26 14:35:02 +0000479 // Set up values for each pixel being copied.
480 Coordinates coords(subW * subH);
481 for (size_t x = 0; x < subW; ++x)
482 for (size_t y = 0; y < subH; ++y)
483 {
484 int index = y * subW + x;
485 SkASSERT(index < coords.length);
486 coords[index]->fX = x;
487 coords[index]->fY = y;
488 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000489
reed@google.com2cb14802013-06-26 14:35:02 +0000490 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000491
reed@google.com2cb14802013-06-26 14:35:02 +0000492 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000493
reed@google.com2cb14802013-06-26 14:35:02 +0000494 // Before/after comparisons easier if we attach buf
495 // to an appropriately configured SkBitmap.
496 memset(buf, 0xFF, bufSize);
497 // Config with stride greater than src but that fits in buf.
498 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
499 SkBitmap::ComputeRowBytes(subset.getConfig(), subW)
500 * 2);
501 bufBm.setPixels(buf);
502 successExpected = false;
503 // Then attempt to copy with a stride that is too large
504 // to fit in the buffer.
505 REPORTER_ASSERT(reporter,
506 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
507 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000508
reed@google.com2cb14802013-06-26 14:35:02 +0000509 if (successExpected)
510 reportCopyVerification(subset, bufBm, coords,
511 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000512 reporter);
513
reed@google.com2cb14802013-06-26 14:35:02 +0000514 // Test #2 ////////////////////////////////////////////
515 // This test should always succeed, but in the case
516 // of extracted bitmaps only because we handle the
517 // issue of getSafeSize(). Without getSafeSize()
518 // buffer overrun/read would occur.
519 memset(buf, 0xFF, bufSize);
520 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
521 subset.rowBytes());
522 bufBm.setPixels(buf);
523 successExpected = subset.getSafeSize() <= bufSize;
524 REPORTER_ASSERT(reporter,
525 subset.copyPixelsTo(buf, bufSize) ==
526 successExpected);
527 if (successExpected)
528 reportCopyVerification(subset, bufBm, coords,
529 "copyPixelsTo(buf, bufSize)", reporter);
530
531 // Test #3 ////////////////////////////////////////////
532 // Copy with different stride between src and dst.
533 memset(buf, 0xFF, bufSize);
534 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
535 subset.rowBytes()+1);
536 bufBm.setPixels(buf);
537 successExpected = true; // Should always work.
538 REPORTER_ASSERT(reporter,
539 subset.copyPixelsTo(buf, bufSize,
540 subset.rowBytes()+1) == successExpected);
541 if (successExpected)
542 reportCopyVerification(subset, bufBm, coords,
543 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
544
545 // Test #4 ////////////////////////////////////////////
546 // Test copy with stride too small.
547 memset(buf, 0xFF, bufSize);
548 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
549 bufBm.setPixels(buf);
550 successExpected = false;
551 // Request copy with stride too small.
552 REPORTER_ASSERT(reporter,
553 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
554 == successExpected);
555 if (successExpected)
556 reportCopyVerification(subset, bufBm, coords,
557 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
558
559#if 0 // copyPixelsFrom is gone
560 // Test #5 ////////////////////////////////////////////
561 // Tests the case where the source stride is too small
562 // for the source configuration.
563 memset(buf, 0xFF, bufSize);
564 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
565 bufBm.setPixels(buf);
566 writeCoordPixels(bufBm, coords);
567 REPORTER_ASSERT(reporter,
568 subset.copyPixelsFrom(buf, bufSize, 1) == false);
569
570 // Test #6 ///////////////////////////////////////////
571 // Tests basic copy from an external buffer to the bitmap.
572 // If the bitmap is "extracted", this also tests the case
573 // where the source stride is different from the dest.
574 // stride.
575 // We've made the buffer large enough to always succeed.
576 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
577 bufBm.setPixels(buf);
578 writeCoordPixels(bufBm, coords);
579 REPORTER_ASSERT(reporter,
580 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
581 true);
582 reportCopyVerification(bufBm, subset, coords,
583 "copyPixelsFrom(buf, bufSize)",
584 reporter);
585
586 // Test #7 ////////////////////////////////////////////
587 // Tests the case where the source buffer is too small
588 // for the transfer.
589 REPORTER_ASSERT(reporter,
590 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
591 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000592
reed@google.comab77aaf2011-11-01 16:03:35 +0000593#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000594 }
595 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000596 }
597}
598
599#include "TestClassDef.h"
600DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)