blob: 5cef1eb98668ed14a8a9183bc82f622722b59cd6 [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
reed@google.com44699382013-10-31 17:28:30 +000099 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000100 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
reed@google.com44699382013-10-31 17:28:30 +0000133 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000134 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) {
reed@google.com44699382013-10-31 17:28:30 +0000165 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000166 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.com383a6972013-10-21 14:00:07 +0000252 srcOpaque.setConfig(gPairs[i].fConfig, W, H, 0, kOpaque_SkAlphaType);
253 srcPremul.setConfig(gPairs[i].fConfig, W, H, 0, kPremul_SkAlphaType);
reed@google.com0a6151d2013-10-10 14:44:56 +0000254 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);
reed@android.com42263962009-05-01 04:00:01 +0000262 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000263 init_src(srcOpaque);
264 init_src(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000265
reed@google.com0a6151d2013-10-10 14:44:56 +0000266 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +0000267 bool expected = gPairs[i].fValid[j] != '0';
268 if (success != expected) {
269 SkString str;
270 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
271 gConfigName[i], gConfigName[j], boolStr(expected),
272 boolStr(success));
273 reporter->reportFailed(str);
274 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000275
reed@google.com0a6151d2013-10-10 14:44:56 +0000276 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000277 if (success != canSucceed) {
278 SkString str;
279 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
280 gConfigName[i], gConfigName[j], boolStr(success),
281 boolStr(canSucceed));
282 reporter->reportFailed(str);
283 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000284
reed@android.com42263962009-05-01 04:00:01 +0000285 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000286 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
287 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000288 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +0000289 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
290 if (srcPremul.config() == dst.config()) {
291 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000292 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000293 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000294 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000295 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000296 const char* dstP = (const char*)dst.getAddr(0, 0);
297 REPORTER_ASSERT(reporter, srcP != dstP);
298 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000299 srcPremul.getSize()));
300 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000301 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000302 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000303 }
reed@android.com311c82d2009-05-05 23:13:23 +0000304 // test extractSubset
305 {
reed@google.com0a6151d2013-10-10 14:44:56 +0000306 SkBitmap bitmap(srcOpaque);
reed@android.com311c82d2009-05-05 23:13:23 +0000307 SkBitmap subset;
308 SkIRect r;
309 r.set(1, 1, 2, 2);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000310 bitmap.setIsVolatile(true);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000311 if (bitmap.extractSubset(&subset, r)) {
reed@android.com311c82d2009-05-05 23:13:23 +0000312 REPORTER_ASSERT(reporter, subset.width() == 1);
313 REPORTER_ASSERT(reporter, subset.height() == 1);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000314 REPORTER_ASSERT(reporter,
reed@google.com383a6972013-10-21 14:00:07 +0000315 subset.alphaType() == bitmap.alphaType());
bsalomon@google.com37688532012-10-12 13:42:36 +0000316 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000317 subset.isVolatile() == true);
reed@android.com311c82d2009-05-05 23:13:23 +0000318
319 SkBitmap copy;
320 REPORTER_ASSERT(reporter,
321 subset.copyTo(&copy, subset.config()));
322 REPORTER_ASSERT(reporter, copy.width() == 1);
323 REPORTER_ASSERT(reporter, copy.height() == 1);
324 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000325
reed@android.com311c82d2009-05-05 23:13:23 +0000326 SkAutoLockPixels alp0(subset);
327 SkAutoLockPixels alp1(copy);
328 // they should both have, or both not-have, a colortable
329 bool hasCT = subset.getColorTable() != NULL;
330 REPORTER_ASSERT(reporter,
331 (copy.getColorTable() != NULL) == hasCT);
332 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000333 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,
reed@google.com383a6972013-10-21 14:00:07 +0000337 subset.alphaType() == bitmap.alphaType());
bsalomon@google.com37688532012-10-12 13:42:36 +0000338 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
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000414 int subW, subH;
reed@google.com2cb14802013-06-26 14:35:02 +0000415 // 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 {
reed@google.com44699382013-10-31 17:28:30 +0000461 srcReady = src.copyTo(&subset, src.config());
reed@google.com2cb14802013-06-26 14:35:02 +0000462 }
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.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000471 const size_t bufSize = subH *
reed@google.com44699382013-10-31 17:28:30 +0000472 SkBitmap::ComputeRowBytes(src.config(), subW) * 2;
reed@google.com2cb14802013-06-26 14:35:02 +0000473 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);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000481 for (int x = 0; x < subW; ++x)
482 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000483 {
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,
reed@google.com44699382013-10-31 17:28:30 +0000499 SkBitmap::ComputeRowBytes(subset.config(), subW) * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000500 bufBm.setPixels(buf);
501 successExpected = false;
502 // Then attempt to copy with a stride that is too large
503 // to fit in the buffer.
504 REPORTER_ASSERT(reporter,
505 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
506 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000507
reed@google.com2cb14802013-06-26 14:35:02 +0000508 if (successExpected)
509 reportCopyVerification(subset, bufBm, coords,
510 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000511 reporter);
512
reed@google.com2cb14802013-06-26 14:35:02 +0000513 // Test #2 ////////////////////////////////////////////
514 // This test should always succeed, but in the case
515 // of extracted bitmaps only because we handle the
516 // issue of getSafeSize(). Without getSafeSize()
517 // buffer overrun/read would occur.
518 memset(buf, 0xFF, bufSize);
519 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
520 subset.rowBytes());
521 bufBm.setPixels(buf);
522 successExpected = subset.getSafeSize() <= bufSize;
523 REPORTER_ASSERT(reporter,
524 subset.copyPixelsTo(buf, bufSize) ==
525 successExpected);
526 if (successExpected)
527 reportCopyVerification(subset, bufBm, coords,
528 "copyPixelsTo(buf, bufSize)", reporter);
529
530 // Test #3 ////////////////////////////////////////////
531 // Copy with different stride between src and dst.
532 memset(buf, 0xFF, bufSize);
533 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
534 subset.rowBytes()+1);
535 bufBm.setPixels(buf);
536 successExpected = true; // Should always work.
537 REPORTER_ASSERT(reporter,
538 subset.copyPixelsTo(buf, bufSize,
539 subset.rowBytes()+1) == successExpected);
540 if (successExpected)
541 reportCopyVerification(subset, bufBm, coords,
542 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
543
544 // Test #4 ////////////////////////////////////////////
545 // Test copy with stride too small.
546 memset(buf, 0xFF, bufSize);
547 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
548 bufBm.setPixels(buf);
549 successExpected = false;
550 // Request copy with stride too small.
551 REPORTER_ASSERT(reporter,
552 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
553 == successExpected);
554 if (successExpected)
555 reportCopyVerification(subset, bufBm, coords,
556 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
557
558#if 0 // copyPixelsFrom is gone
559 // Test #5 ////////////////////////////////////////////
560 // Tests the case where the source stride is too small
561 // for the source configuration.
562 memset(buf, 0xFF, bufSize);
563 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
564 bufBm.setPixels(buf);
565 writeCoordPixels(bufBm, coords);
566 REPORTER_ASSERT(reporter,
567 subset.copyPixelsFrom(buf, bufSize, 1) == false);
568
569 // Test #6 ///////////////////////////////////////////
570 // Tests basic copy from an external buffer to the bitmap.
571 // If the bitmap is "extracted", this also tests the case
572 // where the source stride is different from the dest.
573 // stride.
574 // We've made the buffer large enough to always succeed.
575 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
576 bufBm.setPixels(buf);
577 writeCoordPixels(bufBm, coords);
578 REPORTER_ASSERT(reporter,
579 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
580 true);
581 reportCopyVerification(bufBm, subset, coords,
582 "copyPixelsFrom(buf, bufSize)",
583 reporter);
584
585 // Test #7 ////////////////////////////////////////////
586 // Tests the case where the source buffer is too small
587 // for the transfer.
588 REPORTER_ASSERT(reporter,
589 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
590 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000591
reed@google.comab77aaf2011-11-01 16:03:35 +0000592#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000593 }
594 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000595 }
596}
597
598#include "TestClassDef.h"
599DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)