blob: 81fde73fb130c9254e7b48c3b0358b6c0f18e79e [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());
robertphillips@google.com9a282be2013-10-10 22:01:02 +000060 } else if (SkBitmap::kA1_Config == bitmap.config()) {
61 // The A1 config can have uninitialized bits at the
62 // end of each row if eraseColor is used
63 memset(bitmap.getPixels(), 0xff, bitmap.getSafeSize());
reed@google.com9ce6e752011-01-10 14:04:07 +000064 } else {
65 bitmap.eraseColor(SK_ColorWHITE);
66 }
reed@android.com42263962009-05-01 04:00:01 +000067 }
68}
69
reed@google.com0a6151d2013-10-10 14:44:56 +000070static SkColorTable* init_ctable(SkAlphaType alphaType) {
reed@android.com42263962009-05-01 04:00:01 +000071 static const SkColor colors[] = {
72 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
73 };
reed@google.com0a6151d2013-10-10 14:44:56 +000074 return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType);
reed@android.com42263962009-05-01 04:00:01 +000075}
76
77struct Pair {
78 SkBitmap::Config fConfig;
79 const char* fValid;
80};
81
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000082// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
83// getPixel()
84// setPixel()
85// getSkConfigName()
86// struct Coordinates
87// reportCopyVerification()
88// writeCoordPixels()
89
90// Utility function to read the value of a given pixel in bm. All
91// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +000092static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000093 uint32_t val = 0;
94 uint16_t val16;
95 uint8_t val8, shift;
96 SkAutoLockPixels lock(bm);
97 const void* rawAddr = bm.getAddr(x,y);
98
99 switch (bm.getConfig()) {
100 case SkBitmap::kARGB_8888_Config:
101 memcpy(&val, rawAddr, sizeof(uint32_t));
102 break;
103 case SkBitmap::kARGB_4444_Config:
104 case SkBitmap::kRGB_565_Config:
105 memcpy(&val16, rawAddr, sizeof(uint16_t));
106 val = val16;
107 break;
108 case SkBitmap::kA8_Config:
109 case SkBitmap::kIndex8_Config:
110 memcpy(&val8, rawAddr, sizeof(uint8_t));
111 val = val8;
112 break;
113 case SkBitmap::kA1_Config:
114 memcpy(&val8, rawAddr, sizeof(uint8_t));
115 shift = x % 8;
116 val = (val8 >> shift) & 0x1 ;
117 break;
118 default:
119 break;
120 }
121 return val;
122}
123
124// Utility function to set value of any pixel in bm.
125// bm.getConfig() specifies what format 'val' must be
126// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000127static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000128 uint16_t val16;
129 uint8_t val8, shift;
130 SkAutoLockPixels lock(bm);
131 void* rawAddr = bm.getAddr(x,y);
132
133 switch (bm.getConfig()) {
134 case SkBitmap::kARGB_8888_Config:
135 memcpy(rawAddr, &val, sizeof(uint32_t));
136 break;
137 case SkBitmap::kARGB_4444_Config:
138 case SkBitmap::kRGB_565_Config:
139 val16 = val & 0xFFFF;
140 memcpy(rawAddr, &val16, sizeof(uint16_t));
141 break;
142 case SkBitmap::kA8_Config:
143 case SkBitmap::kIndex8_Config:
144 val8 = val & 0xFF;
145 memcpy(rawAddr, &val8, sizeof(uint8_t));
146 break;
147 case SkBitmap::kA1_Config:
148 shift = x % 8; // We assume we're in the right byte.
149 memcpy(&val8, rawAddr, sizeof(uint8_t));
150 if (val & 0x1) // Turn bit on.
151 val8 |= (0x1 << shift);
152 else // Turn bit off.
153 val8 &= ~(0x1 << shift);
154 memcpy(rawAddr, &val8, sizeof(uint8_t));
155 break;
156 default:
157 // Ignore.
158 break;
159 }
160}
161
162// Utility to return string containing name of each format, to
163// simplify diagnostic output.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000164static const char* getSkConfigName(const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000165 switch (bm.getConfig()) {
166 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
167 case SkBitmap::kA1_Config: return "SkBitmap::kA1_Config";
168 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
169 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
170 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
171 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
172 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000173 default: return "Unknown SkBitmap configuration.";
174 }
175}
176
177// Helper struct to contain pixel locations, while avoiding need for STL.
178struct Coordinates {
179
180 const int length;
181 SkIPoint* const data;
182
183 explicit Coordinates(int _length): length(_length)
184 , data(new SkIPoint[length]) { }
185
186 ~Coordinates(){
187 delete [] data;
188 }
189
190 SkIPoint* operator[](int i) const {
191 // Use with care, no bounds checking.
192 return data + i;
193 }
194};
195
196// A function to verify that two bitmaps contain the same pixel values
197// at all coordinates indicated by coords. Simplifies verification of
198// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000199static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000200 Coordinates& coords,
201 const char* msg,
202 skiatest::Reporter* reporter){
203 bool success = true;
204
205 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000206 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000207 success = success &&
208 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
209 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000210 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000211
212 if (!success) {
213 SkString str;
214 str.printf("%s [config = %s]",
215 msg, getSkConfigName(bm1));
216 reporter->reportFailed(str);
217 }
218}
219
220// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000221static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000222 for (int i = 0; i < coords.length; ++i)
223 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
224}
225
reed@android.com42263962009-05-01 04:00:01 +0000226static void TestBitmapCopy(skiatest::Reporter* reporter) {
227 static const Pair gPairs[] = {
228 { SkBitmap::kNo_Config, "00000000" },
weita@google.comf9ab99a2009-05-03 18:23:30 +0000229 { SkBitmap::kA1_Config, "01000000" },
reed@google.com6ba45722013-06-21 18:30:53 +0000230 { SkBitmap::kA8_Config, "00101010" },
231 { SkBitmap::kIndex8_Config, "00111010" },
232 { SkBitmap::kRGB_565_Config, "00101010" },
reed@android.com42263962009-05-01 04:00:01 +0000233 { SkBitmap::kARGB_4444_Config, "00101110" },
scroggo@google.com7bb36ab2013-08-07 19:39:56 +0000234 { SkBitmap::kARGB_8888_Config, "00101110" },
reed@android.com42263962009-05-01 04:00:01 +0000235 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000236
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000237 static const bool isExtracted[] = {
238 false, true
239 };
240
reed@android.com42263962009-05-01 04:00:01 +0000241 const int W = 20;
242 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000243
reed@android.com42263962009-05-01 04:00:01 +0000244 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
245 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000246 SkBitmap srcOpaque, srcPremul, dst;
skia.committer@gmail.comd33115d2013-10-11 07:01:39 +0000247
reed@google.com0a6151d2013-10-10 14:44:56 +0000248 {
249 SkColorTable* ctOpaque = NULL;
250 SkColorTable* ctPremul = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000251
reed@google.com0a6151d2013-10-10 14:44:56 +0000252 srcOpaque.setConfig(gPairs[i].fConfig, W, H);
253 srcPremul.setConfig(gPairs[i].fConfig, W, H);
254 if (SkBitmap::kIndex8_Config == gPairs[i].fConfig) {
255 ctOpaque = init_ctable(kOpaque_SkAlphaType);
256 ctPremul = init_ctable(kPremul_SkAlphaType);
257 }
258 srcOpaque.allocPixels(ctOpaque);
259 srcPremul.allocPixels(ctPremul);
260 SkSafeUnref(ctOpaque);
261 SkSafeUnref(ctPremul);
skia.committer@gmail.comd33115d2013-10-11 07:01:39 +0000262
reed@google.com0a6151d2013-10-10 14:44:56 +0000263 srcOpaque.setIsOpaque(true);
264 srcPremul.setIsOpaque(false);
reed@android.com42263962009-05-01 04:00:01 +0000265 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000266 init_src(srcOpaque);
267 init_src(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000268
reed@google.com0a6151d2013-10-10 14:44:56 +0000269 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +0000270 bool expected = gPairs[i].fValid[j] != '0';
271 if (success != expected) {
272 SkString str;
273 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
274 gConfigName[i], gConfigName[j], boolStr(expected),
275 boolStr(success));
276 reporter->reportFailed(str);
277 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000278
reed@google.com0a6151d2013-10-10 14:44:56 +0000279 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000280 if (success != canSucceed) {
281 SkString str;
282 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
283 gConfigName[i], gConfigName[j], boolStr(success),
284 boolStr(canSucceed));
285 reporter->reportFailed(str);
286 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000287
reed@android.com42263962009-05-01 04:00:01 +0000288 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000289 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
290 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000291 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +0000292 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
293 if (srcPremul.config() == dst.config()) {
294 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000295 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000296 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000297 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000298 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000299 const char* dstP = (const char*)dst.getAddr(0, 0);
300 REPORTER_ASSERT(reporter, srcP != dstP);
301 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000302 srcPremul.getSize()));
303 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000304 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000305 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000306 }
reed@android.com311c82d2009-05-05 23:13:23 +0000307 // test extractSubset
308 {
reed@google.com0a6151d2013-10-10 14:44:56 +0000309 SkBitmap bitmap(srcOpaque);
reed@android.com311c82d2009-05-05 23:13:23 +0000310 SkBitmap subset;
311 SkIRect r;
312 r.set(1, 1, 2, 2);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000313 bitmap.setIsVolatile(true);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000314 if (bitmap.extractSubset(&subset, r)) {
reed@android.com311c82d2009-05-05 23:13:23 +0000315 REPORTER_ASSERT(reporter, subset.width() == 1);
316 REPORTER_ASSERT(reporter, subset.height() == 1);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000317 REPORTER_ASSERT(reporter,
bsalomon@google.com37688532012-10-12 13:42:36 +0000318 subset.isOpaque() == bitmap.isOpaque());
319 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000320 subset.isVolatile() == true);
reed@android.com311c82d2009-05-05 23:13:23 +0000321
322 SkBitmap copy;
323 REPORTER_ASSERT(reporter,
324 subset.copyTo(&copy, subset.config()));
325 REPORTER_ASSERT(reporter, copy.width() == 1);
326 REPORTER_ASSERT(reporter, copy.height() == 1);
327 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000328
reed@android.com311c82d2009-05-05 23:13:23 +0000329 SkAutoLockPixels alp0(subset);
330 SkAutoLockPixels alp1(copy);
331 // they should both have, or both not-have, a colortable
332 bool hasCT = subset.getColorTable() != NULL;
333 REPORTER_ASSERT(reporter,
334 (copy.getColorTable() != NULL) == hasCT);
335 }
skia.committer@gmail.comd33115d2013-10-11 07:01:39 +0000336
reed@google.com0a6151d2013-10-10 14:44:56 +0000337 bitmap = srcPremul;
skyostil@google.com0eb75762012-01-16 10:45:53 +0000338 bitmap.setIsVolatile(false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000339 if (bitmap.extractSubset(&subset, r)) {
340 REPORTER_ASSERT(reporter,
bsalomon@google.com37688532012-10-12 13:42:36 +0000341 subset.isOpaque() == bitmap.isOpaque());
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.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000362
reed@google.com2cb14802013-06-26 14:35:02 +0000363 // Tests for getSafeSize64().
364 // Test with a very large configuration without pixel buffer
365 // attached.
366 SkBitmap tstSafeSize;
367 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
368 100000000U);
369 Sk64 safeSize = tstSafeSize.getSafeSize64();
370 if (safeSize.isNeg()) {
371 SkString str;
372 str.printf("getSafeSize64() negative: %s",
373 getSkConfigName(tstSafeSize));
374 reporter->reportFailed(str);
375 }
376 bool sizeFail = false;
377 // Compare against hand-computed values.
378 switch (gPairs[i].fConfig) {
379 case SkBitmap::kNo_Config:
380 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000381
reed@google.com2cb14802013-06-26 14:35:02 +0000382 case SkBitmap::kA1_Config:
383 if (safeSize.fHi != 0x470DE ||
384 safeSize.fLo != 0x4DF82000)
385 sizeFail = true;
386 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000387
reed@google.com2cb14802013-06-26 14:35:02 +0000388 case SkBitmap::kA8_Config:
389 case SkBitmap::kIndex8_Config:
390 if (safeSize.fHi != 0x2386F2 ||
391 safeSize.fLo != 0x6FC10000)
392 sizeFail = true;
393 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000394
reed@google.com2cb14802013-06-26 14:35:02 +0000395 case SkBitmap::kRGB_565_Config:
396 case SkBitmap::kARGB_4444_Config:
397 if (safeSize.fHi != 0x470DE4 ||
398 safeSize.fLo != 0xDF820000)
399 sizeFail = true;
400 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000401
reed@google.com2cb14802013-06-26 14:35:02 +0000402 case SkBitmap::kARGB_8888_Config:
403 if (safeSize.fHi != 0x8E1BC9 ||
404 safeSize.fLo != 0xBF040000)
405 sizeFail = true;
406 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000407
reed@google.com2cb14802013-06-26 14:35:02 +0000408 default:
409 break;
410 }
411 if (sizeFail) {
412 SkString str;
413 str.printf("getSafeSize64() wrong size: %s",
414 getSkConfigName(tstSafeSize));
415 reporter->reportFailed(str);
416 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000417
reed@google.com2cb14802013-06-26 14:35:02 +0000418 size_t subW, subH;
419 // Set sizes to be height = 2 to force the last row of the
420 // source to be used, thus verifying correct operation if
421 // the bitmap is an extracted subset.
422 if (gPairs[i].fConfig == SkBitmap::kA1_Config) {
423 // If one-bit per pixel, use 9 pixels to force more than
424 // one byte per row.
425 subW = 9;
426 subH = 2;
427 } else {
428 // All other configurations are at least one byte per pixel,
429 // and different configs will test copying different numbers
430 // of bytes.
431 subW = subH = 2;
432 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000433
reed@google.com2cb14802013-06-26 14:35:02 +0000434 // Create bitmap to act as source for copies and subsets.
435 SkBitmap src, subset;
436 SkColorTable* ct = NULL;
437 if (isExtracted[copyCase]) { // A larger image to extract from.
438 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
439 } else { // Tests expect a 2x2 bitmap, so make smaller.
440 src.setConfig(gPairs[i].fConfig, subW, subH);
441 }
442 if (SkBitmap::kIndex8_Config == src.config()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000443 ct = init_ctable(kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000444 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000445
reed@google.com2cb14802013-06-26 14:35:02 +0000446 src.allocPixels(ct);
447 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000448
reed@google.com2cb14802013-06-26 14:35:02 +0000449 // Either copy src or extract into 'subset', which is used
450 // for subsequent calls to copyPixelsTo/From.
451 bool srcReady = false;
452 if (isExtracted[copyCase]) {
453 // The extractedSubset() test case allows us to test copy-
454 // ing when src and dst mave possibly different strides.
455 SkIRect r;
456 if (gPairs[i].fConfig == SkBitmap::kA1_Config)
457 // This config seems to need byte-alignment of
458 // extracted subset bits.
459 r.set(0, 0, subW, subH);
460 else
461 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000462
reed@google.com2cb14802013-06-26 14:35:02 +0000463 srcReady = src.extractSubset(&subset, r);
464 } else {
465 srcReady = src.copyTo(&subset, src.getConfig());
466 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000467
reed@google.com2cb14802013-06-26 14:35:02 +0000468 // Not all configurations will generate a valid 'subset'.
469 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000470
reed@google.com2cb14802013-06-26 14:35:02 +0000471 // Allocate our target buffer 'buf' for all copies.
472 // To simplify verifying correctness of copies attach
473 // buf to a SkBitmap, but copies are done using the
474 // raw buffer pointer.
475 const uint32_t bufSize = subH *
476 SkBitmap::ComputeRowBytes(src.getConfig(), subW) * 2;
477 SkAutoMalloc autoBuf (bufSize);
478 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000479
reed@google.com2cb14802013-06-26 14:35:02 +0000480 SkBitmap bufBm; // Attach buf to this bitmap.
481 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000482
reed@google.com2cb14802013-06-26 14:35:02 +0000483 // Set up values for each pixel being copied.
484 Coordinates coords(subW * subH);
485 for (size_t x = 0; x < subW; ++x)
486 for (size_t y = 0; y < subH; ++y)
487 {
488 int index = y * subW + x;
489 SkASSERT(index < coords.length);
490 coords[index]->fX = x;
491 coords[index]->fY = y;
492 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000493
reed@google.com2cb14802013-06-26 14:35:02 +0000494 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000495
reed@google.com2cb14802013-06-26 14:35:02 +0000496 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000497
reed@google.com2cb14802013-06-26 14:35:02 +0000498 // Before/after comparisons easier if we attach buf
499 // to an appropriately configured SkBitmap.
500 memset(buf, 0xFF, bufSize);
501 // Config with stride greater than src but that fits in buf.
502 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
503 SkBitmap::ComputeRowBytes(subset.getConfig(), subW)
504 * 2);
505 bufBm.setPixels(buf);
506 successExpected = false;
507 // Then attempt to copy with a stride that is too large
508 // to fit in the buffer.
509 REPORTER_ASSERT(reporter,
510 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
511 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000512
reed@google.com2cb14802013-06-26 14:35:02 +0000513 if (successExpected)
514 reportCopyVerification(subset, bufBm, coords,
515 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000516 reporter);
517
reed@google.com2cb14802013-06-26 14:35:02 +0000518 // Test #2 ////////////////////////////////////////////
519 // This test should always succeed, but in the case
520 // of extracted bitmaps only because we handle the
521 // issue of getSafeSize(). Without getSafeSize()
522 // buffer overrun/read would occur.
523 memset(buf, 0xFF, bufSize);
524 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
525 subset.rowBytes());
526 bufBm.setPixels(buf);
527 successExpected = subset.getSafeSize() <= bufSize;
528 REPORTER_ASSERT(reporter,
529 subset.copyPixelsTo(buf, bufSize) ==
530 successExpected);
531 if (successExpected)
532 reportCopyVerification(subset, bufBm, coords,
533 "copyPixelsTo(buf, bufSize)", reporter);
534
535 // Test #3 ////////////////////////////////////////////
536 // Copy with different stride between src and dst.
537 memset(buf, 0xFF, bufSize);
538 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
539 subset.rowBytes()+1);
540 bufBm.setPixels(buf);
541 successExpected = true; // Should always work.
542 REPORTER_ASSERT(reporter,
543 subset.copyPixelsTo(buf, bufSize,
544 subset.rowBytes()+1) == successExpected);
545 if (successExpected)
546 reportCopyVerification(subset, bufBm, coords,
547 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
548
549 // Test #4 ////////////////////////////////////////////
550 // Test copy with stride too small.
551 memset(buf, 0xFF, bufSize);
552 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
553 bufBm.setPixels(buf);
554 successExpected = false;
555 // Request copy with stride too small.
556 REPORTER_ASSERT(reporter,
557 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
558 == successExpected);
559 if (successExpected)
560 reportCopyVerification(subset, bufBm, coords,
561 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
562
563#if 0 // copyPixelsFrom is gone
564 // Test #5 ////////////////////////////////////////////
565 // Tests the case where the source stride is too small
566 // for the source configuration.
567 memset(buf, 0xFF, bufSize);
568 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
569 bufBm.setPixels(buf);
570 writeCoordPixels(bufBm, coords);
571 REPORTER_ASSERT(reporter,
572 subset.copyPixelsFrom(buf, bufSize, 1) == false);
573
574 // Test #6 ///////////////////////////////////////////
575 // Tests basic copy from an external buffer to the bitmap.
576 // If the bitmap is "extracted", this also tests the case
577 // where the source stride is different from the dest.
578 // stride.
579 // We've made the buffer large enough to always succeed.
580 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
581 bufBm.setPixels(buf);
582 writeCoordPixels(bufBm, coords);
583 REPORTER_ASSERT(reporter,
584 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
585 true);
586 reportCopyVerification(bufBm, subset, coords,
587 "copyPixelsFrom(buf, bufSize)",
588 reporter);
589
590 // Test #7 ////////////////////////////////////////////
591 // Tests the case where the source buffer is too small
592 // for the transfer.
593 REPORTER_ASSERT(reporter,
594 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
595 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000596
reed@google.comab77aaf2011-11-01 16:03:35 +0000597#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000598 }
599 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000600 }
601}
602
603#include "TestClassDef.h"
604DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)