blob: 0366068426da1f9b0613a038bf08b8dbeb29ae7e [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
35static void test_isOpaque(skiatest::Reporter* reporter, const SkBitmap& src,
36 SkBitmap::Config dstConfig) {
37 SkBitmap bitmap(src);
38 SkBitmap dst;
39
40 // we need the lock so that we get a valid colorTable (when available)
41 SkAutoLockPixels alp(bitmap);
42 SkColorTable* ctable = bitmap.getColorTable();
43 unsigned ctableFlags = ctable ? ctable->getFlags() : 0;
44
45 if (canHaveAlpha(bitmap.config()) && canHaveAlpha(dstConfig)) {
46 bitmap.setIsOpaque(false);
47 if (ctable) {
48 ctable->setFlags(ctableFlags & ~SkColorTable::kColorsAreOpaque_Flag);
49 }
50 REPORTER_ASSERT(reporter, bitmap.copyTo(&dst, dstConfig));
51 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
52 if (bitmap.isOpaque() != dst.isOpaque()) {
53 report_opaqueness(reporter, bitmap, dst);
54 }
55 }
56
57 bitmap.setIsOpaque(true);
58 if (ctable) {
59 ctable->setFlags(ctableFlags | SkColorTable::kColorsAreOpaque_Flag);
60 }
61 REPORTER_ASSERT(reporter, bitmap.copyTo(&dst, dstConfig));
62 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
63 if (bitmap.isOpaque() != dst.isOpaque()) {
64 report_opaqueness(reporter, bitmap, dst);
65 }
66
67 if (ctable) {
68 ctable->setFlags(ctableFlags);
69 }
70}
71
reed@google.com9ce6e752011-01-10 14:04:07 +000072static void init_src(const SkBitmap& bitmap, const SkColorTable* ct) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000073 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000074 if (bitmap.getPixels()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000075 if (ct) {
76 sk_bzero(bitmap.getPixels(), bitmap.getSize());
77 } else {
78 bitmap.eraseColor(SK_ColorWHITE);
79 }
reed@android.com42263962009-05-01 04:00:01 +000080 }
81}
82
caryclark@google.com42639cd2012-06-06 12:03:39 +000083static SkColorTable* init_ctable() {
reed@android.com42263962009-05-01 04:00:01 +000084 static const SkColor colors[] = {
85 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
86 };
87 return new SkColorTable(colors, SK_ARRAY_COUNT(colors));
88}
89
90struct Pair {
91 SkBitmap::Config fConfig;
92 const char* fValid;
93};
94
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000095// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
96// getPixel()
97// setPixel()
98// getSkConfigName()
99// struct Coordinates
100// reportCopyVerification()
101// writeCoordPixels()
102
103// Utility function to read the value of a given pixel in bm. All
104// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000105static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000106 uint32_t val = 0;
107 uint16_t val16;
108 uint8_t val8, shift;
109 SkAutoLockPixels lock(bm);
110 const void* rawAddr = bm.getAddr(x,y);
111
112 switch (bm.getConfig()) {
113 case SkBitmap::kARGB_8888_Config:
114 memcpy(&val, rawAddr, sizeof(uint32_t));
115 break;
116 case SkBitmap::kARGB_4444_Config:
117 case SkBitmap::kRGB_565_Config:
118 memcpy(&val16, rawAddr, sizeof(uint16_t));
119 val = val16;
120 break;
121 case SkBitmap::kA8_Config:
122 case SkBitmap::kIndex8_Config:
123 memcpy(&val8, rawAddr, sizeof(uint8_t));
124 val = val8;
125 break;
126 case SkBitmap::kA1_Config:
127 memcpy(&val8, rawAddr, sizeof(uint8_t));
128 shift = x % 8;
129 val = (val8 >> shift) & 0x1 ;
130 break;
131 default:
132 break;
133 }
134 return val;
135}
136
137// Utility function to set value of any pixel in bm.
138// bm.getConfig() specifies what format 'val' must be
139// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000140static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000141 uint16_t val16;
142 uint8_t val8, shift;
143 SkAutoLockPixels lock(bm);
144 void* rawAddr = bm.getAddr(x,y);
145
146 switch (bm.getConfig()) {
147 case SkBitmap::kARGB_8888_Config:
148 memcpy(rawAddr, &val, sizeof(uint32_t));
149 break;
150 case SkBitmap::kARGB_4444_Config:
151 case SkBitmap::kRGB_565_Config:
152 val16 = val & 0xFFFF;
153 memcpy(rawAddr, &val16, sizeof(uint16_t));
154 break;
155 case SkBitmap::kA8_Config:
156 case SkBitmap::kIndex8_Config:
157 val8 = val & 0xFF;
158 memcpy(rawAddr, &val8, sizeof(uint8_t));
159 break;
160 case SkBitmap::kA1_Config:
161 shift = x % 8; // We assume we're in the right byte.
162 memcpy(&val8, rawAddr, sizeof(uint8_t));
163 if (val & 0x1) // Turn bit on.
164 val8 |= (0x1 << shift);
165 else // Turn bit off.
166 val8 &= ~(0x1 << shift);
167 memcpy(rawAddr, &val8, sizeof(uint8_t));
168 break;
169 default:
170 // Ignore.
171 break;
172 }
173}
174
175// Utility to return string containing name of each format, to
176// simplify diagnostic output.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000177static const char* getSkConfigName(const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000178 switch (bm.getConfig()) {
179 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
180 case SkBitmap::kA1_Config: return "SkBitmap::kA1_Config";
181 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
182 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
183 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
184 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
185 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
186 case SkBitmap::kRLE_Index8_Config:
187 return "SkBitmap::kRLE_Index8_Config,";
188 default: return "Unknown SkBitmap configuration.";
189 }
190}
191
192// Helper struct to contain pixel locations, while avoiding need for STL.
193struct Coordinates {
194
195 const int length;
196 SkIPoint* const data;
197
198 explicit Coordinates(int _length): length(_length)
199 , data(new SkIPoint[length]) { }
200
201 ~Coordinates(){
202 delete [] data;
203 }
204
205 SkIPoint* operator[](int i) const {
206 // Use with care, no bounds checking.
207 return data + i;
208 }
209};
210
211// A function to verify that two bitmaps contain the same pixel values
212// at all coordinates indicated by coords. Simplifies verification of
213// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000214static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000215 Coordinates& coords,
216 const char* msg,
217 skiatest::Reporter* reporter){
218 bool success = true;
219
220 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000221 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000222 success = success &&
223 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
224 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000225 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000226
227 if (!success) {
228 SkString str;
229 str.printf("%s [config = %s]",
230 msg, getSkConfigName(bm1));
231 reporter->reportFailed(str);
232 }
233}
234
235// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000236static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000237 for (int i = 0; i < coords.length; ++i)
238 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
239}
240
reed@android.com42263962009-05-01 04:00:01 +0000241static void TestBitmapCopy(skiatest::Reporter* reporter) {
242 static const Pair gPairs[] = {
243 { SkBitmap::kNo_Config, "00000000" },
weita@google.comf9ab99a2009-05-03 18:23:30 +0000244 { SkBitmap::kA1_Config, "01000000" },
reed@android.com42263962009-05-01 04:00:01 +0000245 { SkBitmap::kA8_Config, "00101110" },
246 { SkBitmap::kIndex8_Config, "00111110" },
247 { SkBitmap::kRGB_565_Config, "00101110" },
248 { SkBitmap::kARGB_4444_Config, "00101110" },
249 { SkBitmap::kARGB_8888_Config, "00101110" },
reed@android.comfbaa88d2009-05-06 17:44:34 +0000250// TODO: create valid RLE bitmap to test with
251 // { SkBitmap::kRLE_Index8_Config, "00101111" }
reed@android.com42263962009-05-01 04:00:01 +0000252 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000253
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000254 static const bool isExtracted[] = {
255 false, true
256 };
257
reed@android.com42263962009-05-01 04:00:01 +0000258 const int W = 20;
259 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000260
reed@android.com42263962009-05-01 04:00:01 +0000261 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
262 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
263 SkBitmap src, dst;
264 SkColorTable* ct = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000265
reed@android.com42263962009-05-01 04:00:01 +0000266 src.setConfig(gPairs[i].fConfig, W, H);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000267 if (SkBitmap::kIndex8_Config == src.config() ||
268 SkBitmap::kRLE_Index8_Config == src.config()) {
reed@android.com42263962009-05-01 04:00:01 +0000269 ct = init_ctable();
270 }
271 src.allocPixels(ct);
reed@android.comd0a529d2010-01-08 14:01:41 +0000272 SkSafeUnref(ct);
reed@android.com42263962009-05-01 04:00:01 +0000273
reed@google.com9ce6e752011-01-10 14:04:07 +0000274 init_src(src, ct);
reed@android.com42263962009-05-01 04:00:01 +0000275 bool success = src.copyTo(&dst, gPairs[j].fConfig);
276 bool expected = gPairs[i].fValid[j] != '0';
277 if (success != expected) {
278 SkString str;
279 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
280 gConfigName[i], gConfigName[j], boolStr(expected),
281 boolStr(success));
282 reporter->reportFailed(str);
283 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000284
reed@android.comfbaa88d2009-05-06 17:44:34 +0000285 bool canSucceed = src.canCopyTo(gPairs[j].fConfig);
286 if (success != canSucceed) {
287 SkString str;
288 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
289 gConfigName[i], gConfigName[j], boolStr(success),
290 boolStr(canSucceed));
291 reporter->reportFailed(str);
292 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000293
reed@android.com42263962009-05-01 04:00:01 +0000294 if (success) {
295 REPORTER_ASSERT(reporter, src.width() == dst.width());
296 REPORTER_ASSERT(reporter, src.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000297 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@android.comcafc9f92009-08-22 03:44:57 +0000298 test_isOpaque(reporter, src, dst.config());
reed@android.com42263962009-05-01 04:00:01 +0000299 if (src.config() == dst.config()) {
weita@google.comf9ab99a2009-05-03 18:23:30 +0000300 SkAutoLockPixels srcLock(src);
reed@android.com42263962009-05-01 04:00:01 +0000301 SkAutoLockPixels dstLock(dst);
302 REPORTER_ASSERT(reporter, src.readyToDraw());
303 REPORTER_ASSERT(reporter, dst.readyToDraw());
304 const char* srcP = (const char*)src.getAddr(0, 0);
305 const char* dstP = (const char*)dst.getAddr(0, 0);
306 REPORTER_ASSERT(reporter, srcP != dstP);
307 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
308 src.getSize()));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000309 REPORTER_ASSERT(reporter, src.getGenerationID() == dst.getGenerationID());
310 } else {
311 REPORTER_ASSERT(reporter, src.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000312 }
reed@android.com311c82d2009-05-05 23:13:23 +0000313 // test extractSubset
314 {
skyostil@google.comce7adb52012-01-13 14:56:51 +0000315 SkBitmap bitmap(src);
reed@android.com311c82d2009-05-05 23:13:23 +0000316 SkBitmap subset;
317 SkIRect r;
318 r.set(1, 1, 2, 2);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000319 bitmap.setIsVolatile(true);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000320 if (bitmap.extractSubset(&subset, r)) {
reed@android.com311c82d2009-05-05 23:13:23 +0000321 REPORTER_ASSERT(reporter, subset.width() == 1);
322 REPORTER_ASSERT(reporter, subset.height() == 1);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000323 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000324 subset.isVolatile() == true);
reed@android.com311c82d2009-05-05 23:13:23 +0000325
326 SkBitmap copy;
327 REPORTER_ASSERT(reporter,
328 subset.copyTo(&copy, subset.config()));
329 REPORTER_ASSERT(reporter, copy.width() == 1);
330 REPORTER_ASSERT(reporter, copy.height() == 1);
331 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000332
reed@android.com311c82d2009-05-05 23:13:23 +0000333 SkAutoLockPixels alp0(subset);
334 SkAutoLockPixels alp1(copy);
335 // they should both have, or both not-have, a colortable
336 bool hasCT = subset.getColorTable() != NULL;
337 REPORTER_ASSERT(reporter,
338 (copy.getColorTable() != NULL) == hasCT);
339 }
skyostil@google.com0eb75762012-01-16 10:45:53 +0000340 bitmap.setIsVolatile(false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000341 if (bitmap.extractSubset(&subset, r)) {
342 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000343 subset.isVolatile() == false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000344 }
reed@android.com311c82d2009-05-05 23:13:23 +0000345 }
reed@android.com42263962009-05-01 04:00:01 +0000346 } else {
347 // dst should be unchanged from its initial state
348 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
349 REPORTER_ASSERT(reporter, dst.width() == 0);
350 REPORTER_ASSERT(reporter, dst.height() == 0);
351 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000352 } // for (size_t j = ...
353
354 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
355 // copyPixelsFrom().
356 //
357 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
358 ++copyCase) {
359 // Test copying to/from external buffer.
360 // Note: the tests below have hard-coded values ---
361 // Please take care if modifying.
362 if (gPairs[i].fConfig != SkBitmap::kRLE_Index8_Config) {
363
364 // Tests for getSafeSize64().
365 // Test with a very large configuration without pixel buffer
366 // attached.
367 SkBitmap tstSafeSize;
368 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
369 100000000U);
370 Sk64 safeSize = tstSafeSize.getSafeSize64();
371 if (safeSize.isNeg()) {
372 SkString str;
373 str.printf("getSafeSize64() negative: %s",
374 getSkConfigName(tstSafeSize));
375 reporter->reportFailed(str);
376 }
377 bool sizeFail = false;
378 // Compare against hand-computed values.
379 switch (gPairs[i].fConfig) {
380 case SkBitmap::kNo_Config:
381 break;
382
383 case SkBitmap::kA1_Config:
384 if (safeSize.fHi != 0x470DE ||
385 safeSize.fLo != 0x4DF82000)
386 sizeFail = true;
387 break;
388
389 case SkBitmap::kA8_Config:
390 case SkBitmap::kIndex8_Config:
391 if (safeSize.fHi != 0x2386F2 ||
392 safeSize.fLo != 0x6FC10000)
393 sizeFail = true;
394 break;
395
396 case SkBitmap::kRGB_565_Config:
397 case SkBitmap::kARGB_4444_Config:
398 if (safeSize.fHi != 0x470DE4 ||
399 safeSize.fLo != 0xDF820000)
400 sizeFail = true;
401 break;
402
403 case SkBitmap::kARGB_8888_Config:
404 if (safeSize.fHi != 0x8E1BC9 ||
405 safeSize.fLo != 0xBF040000)
406 sizeFail = true;
407 break;
408
409 case SkBitmap::kRLE_Index8_Config:
410 break;
411
412 default:
413 break;
414 }
415 if (sizeFail) {
416 SkString str;
417 str.printf("getSafeSize64() wrong size: %s",
418 getSkConfigName(tstSafeSize));
419 reporter->reportFailed(str);
420 }
421
422 size_t subW, subH;
423 // Set sizes to be height = 2 to force the last row of the
424 // source to be used, thus verifying correct operation if
425 // the bitmap is an extracted subset.
426 if (gPairs[i].fConfig == SkBitmap::kA1_Config) {
427 // If one-bit per pixel, use 9 pixels to force more than
428 // one byte per row.
429 subW = 9;
430 subH = 2;
431 } else {
432 // All other configurations are at least one byte per pixel,
433 // and different configs will test copying different numbers
434 // of bytes.
435 subW = subH = 2;
436 }
437
438 // Create bitmap to act as source for copies and subsets.
439 SkBitmap src, subset;
440 SkColorTable* ct = NULL;
441 if (isExtracted[copyCase]) { // A larger image to extract from.
442 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
443 } else // Tests expect a 2x2 bitmap, so make smaller.
444 src.setConfig(gPairs[i].fConfig, subW, subH);
445 if (SkBitmap::kIndex8_Config == src.config() ||
446 SkBitmap::kRLE_Index8_Config == src.config()) {
447 ct = init_ctable();
448 }
449
450 src.allocPixels(ct);
451 SkSafeUnref(ct);
452
453 // Either copy src or extract into 'subset', which is used
454 // for subsequent calls to copyPixelsTo/From.
455 bool srcReady = false;
456 if (isExtracted[copyCase]) {
457 // The extractedSubset() test case allows us to test copy-
458 // ing when src and dst mave possibly different strides.
459 SkIRect r;
460 if (gPairs[i].fConfig == SkBitmap::kA1_Config)
461 // This config seems to need byte-alignment of
462 // extracted subset bits.
463 r.set(0, 0, subW, subH);
464 else
465 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
466
467 srcReady = src.extractSubset(&subset, r);
468 } else {
469 srcReady = src.copyTo(&subset, src.getConfig());
470 }
471
472 // Not all configurations will generate a valid 'subset'.
473 if (srcReady) {
474
475 // Allocate our target buffer 'buf' for all copies.
476 // To simplify verifying correctness of copies attach
477 // buf to a SkBitmap, but copies are done using the
478 // raw buffer pointer.
479 const uint32_t bufSize = subH *
480 SkBitmap::ComputeRowBytes(src.getConfig(), subW) * 2;
tomhudson@google.com0435f162012-03-15 18:16:39 +0000481 SkAutoMalloc autoBuf (bufSize);
482 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000483
484 SkBitmap bufBm; // Attach buf to this bitmap.
485 bool successExpected;
486
487 // Set up values for each pixel being copied.
488 Coordinates coords(subW * subH);
489 for (size_t x = 0; x < subW; ++x)
490 for (size_t y = 0; y < subH; ++y)
491 {
reed@google.com1fcd51e2011-01-05 15:50:27 +0000492 int index = y * subW + x;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000493 SkASSERT(index < coords.length);
494 coords[index]->fX = x;
495 coords[index]->fY = y;
496 }
497
498 writeCoordPixels(subset, coords);
499
500 // Test #1 ////////////////////////////////////////////
501
502 // Before/after comparisons easier if we attach buf
503 // to an appropriately configured SkBitmap.
504 memset(buf, 0xFF, bufSize);
505 // Config with stride greater than src but that fits in buf.
506 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
507 SkBitmap::ComputeRowBytes(subset.getConfig(), subW)
508 * 2);
509 bufBm.setPixels(buf);
510 successExpected = false;
511 // Then attempt to copy with a stride that is too large
512 // to fit in the buffer.
513 REPORTER_ASSERT(reporter,
514 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
515 == successExpected);
516
517 if (successExpected)
518 reportCopyVerification(subset, bufBm, coords,
519 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
520 reporter);
521
522 // Test #2 ////////////////////////////////////////////
523 // This test should always succeed, but in the case
524 // of extracted bitmaps only because we handle the
525 // issue of getSafeSize(). Without getSafeSize()
526 // buffer overrun/read would occur.
527 memset(buf, 0xFF, bufSize);
528 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
529 subset.rowBytes());
530 bufBm.setPixels(buf);
531 successExpected = subset.getSafeSize() <= bufSize;
532 REPORTER_ASSERT(reporter,
533 subset.copyPixelsTo(buf, bufSize) ==
534 successExpected);
535 if (successExpected)
536 reportCopyVerification(subset, bufBm, coords,
537 "copyPixelsTo(buf, bufSize)", reporter);
538
539 // Test #3 ////////////////////////////////////////////
540 // Copy with different stride between src and dst.
541 memset(buf, 0xFF, bufSize);
542 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
543 subset.rowBytes()+1);
544 bufBm.setPixels(buf);
545 successExpected = true; // Should always work.
546 REPORTER_ASSERT(reporter,
547 subset.copyPixelsTo(buf, bufSize,
548 subset.rowBytes()+1) == successExpected);
549 if (successExpected)
550 reportCopyVerification(subset, bufBm, coords,
551 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
552
553 // Test #4 ////////////////////////////////////////////
554 // Test copy with stride too small.
555 memset(buf, 0xFF, bufSize);
556 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
557 bufBm.setPixels(buf);
558 successExpected = false;
559 // Request copy with stride too small.
560 REPORTER_ASSERT(reporter,
561 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
562 == successExpected);
563 if (successExpected)
564 reportCopyVerification(subset, bufBm, coords,
565 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
566
reed@google.comab77aaf2011-11-01 16:03:35 +0000567#if 0 // copyPixelsFrom is gone
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000568 // Test #5 ////////////////////////////////////////////
569 // Tests the case where the source stride is too small
570 // for the source configuration.
571 memset(buf, 0xFF, bufSize);
572 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
573 bufBm.setPixels(buf);
574 writeCoordPixels(bufBm, coords);
575 REPORTER_ASSERT(reporter,
576 subset.copyPixelsFrom(buf, bufSize, 1) == false);
577
epoger@google.com69731aa2011-04-26 19:31:33 +0000578 // Test #6 ///////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000579 // Tests basic copy from an external buffer to the bitmap.
580 // If the bitmap is "extracted", this also tests the case
581 // where the source stride is different from the dest.
582 // stride.
583 // We've made the buffer large enough to always succeed.
584 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
585 bufBm.setPixels(buf);
586 writeCoordPixels(bufBm, coords);
587 REPORTER_ASSERT(reporter,
588 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
589 true);
590 reportCopyVerification(bufBm, subset, coords,
591 "copyPixelsFrom(buf, bufSize)",
592 reporter);
593
594 // Test #7 ////////////////////////////////////////////
595 // Tests the case where the source buffer is too small
596 // for the transfer.
597 REPORTER_ASSERT(reporter,
598 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
599 false);
600
reed@google.comab77aaf2011-11-01 16:03:35 +0000601#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000602 }
603 }
604 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000605 }
606}
607
608#include "TestClassDef.h"
609DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)