blob: f61d55e953ab3df3ac06b9abc1c1f086f944182c [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[] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +000018 "None", "A8", "Index8", "565", "4444", "8888"
reed@android.com42263962009-05-01 04:00:01 +000019};
20
reed@android.comcafc9f92009-08-22 03:44:57 +000021static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
22 const SkBitmap& dst) {
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;
rmistry@google.comd6bab022013-12-02 13:50:38 +000091 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000092 SkAutoLockPixels lock(bm);
93 const void* rawAddr = bm.getAddr(x,y);
94
reed@google.com44699382013-10-31 17:28:30 +000095 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000096 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;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000109 default:
110 break;
111 }
112 return val;
113}
114
115// Utility function to set value of any pixel in bm.
116// bm.getConfig() specifies what format 'val' must be
117// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000118static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000119 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000120 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000121 SkAutoLockPixels lock(bm);
122 void* rawAddr = bm.getAddr(x,y);
123
reed@google.com44699382013-10-31 17:28:30 +0000124 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000125 case SkBitmap::kARGB_8888_Config:
126 memcpy(rawAddr, &val, sizeof(uint32_t));
127 break;
128 case SkBitmap::kARGB_4444_Config:
129 case SkBitmap::kRGB_565_Config:
130 val16 = val & 0xFFFF;
131 memcpy(rawAddr, &val16, sizeof(uint16_t));
132 break;
133 case SkBitmap::kA8_Config:
134 case SkBitmap::kIndex8_Config:
135 val8 = val & 0xFF;
136 memcpy(rawAddr, &val8, sizeof(uint8_t));
137 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000138 default:
139 // Ignore.
140 break;
141 }
142}
143
144// Utility to return string containing name of each format, to
145// simplify diagnostic output.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000146static const char* getSkConfigName(const SkBitmap& bm) {
reed@google.com44699382013-10-31 17:28:30 +0000147 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000148 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000149 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
150 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
151 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
152 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
153 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000154 default: return "Unknown SkBitmap configuration.";
155 }
156}
157
158// Helper struct to contain pixel locations, while avoiding need for STL.
159struct Coordinates {
160
161 const int length;
162 SkIPoint* const data;
163
164 explicit Coordinates(int _length): length(_length)
165 , data(new SkIPoint[length]) { }
166
167 ~Coordinates(){
168 delete [] data;
169 }
170
171 SkIPoint* operator[](int i) const {
172 // Use with care, no bounds checking.
173 return data + i;
174 }
175};
176
177// A function to verify that two bitmaps contain the same pixel values
178// at all coordinates indicated by coords. Simplifies verification of
179// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000180static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000181 Coordinates& coords,
182 const char* msg,
183 skiatest::Reporter* reporter){
184 bool success = true;
185
186 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000187 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000188 success = success &&
189 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
190 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000191 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000192
193 if (!success) {
194 SkString str;
195 str.printf("%s [config = %s]",
196 msg, getSkConfigName(bm1));
197 reporter->reportFailed(str);
198 }
199}
200
201// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000202static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000203 for (int i = 0; i < coords.length; ++i)
204 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
205}
206
reed@android.com42263962009-05-01 04:00:01 +0000207static void TestBitmapCopy(skiatest::Reporter* reporter) {
208 static const Pair gPairs[] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000209 { SkBitmap::kNo_Config, "0000000" },
210 { SkBitmap::kA8_Config, "0101010" },
211 { SkBitmap::kIndex8_Config, "0111010" },
212 { SkBitmap::kRGB_565_Config, "0101010" },
213 { SkBitmap::kARGB_4444_Config, "0101110" },
214 { SkBitmap::kARGB_8888_Config, "0101110" },
reed@android.com42263962009-05-01 04:00:01 +0000215 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000216
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000217 static const bool isExtracted[] = {
218 false, true
219 };
220
reed@android.com42263962009-05-01 04:00:01 +0000221 const int W = 20;
222 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000223
reed@android.com42263962009-05-01 04:00:01 +0000224 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
225 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000226 SkBitmap srcOpaque, srcPremul, dst;
skia.committer@gmail.comd33115d2013-10-11 07:01:39 +0000227
reed@google.com0a6151d2013-10-10 14:44:56 +0000228 {
229 SkColorTable* ctOpaque = NULL;
230 SkColorTable* ctPremul = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000231
reed@google.com383a6972013-10-21 14:00:07 +0000232 srcOpaque.setConfig(gPairs[i].fConfig, W, H, 0, kOpaque_SkAlphaType);
233 srcPremul.setConfig(gPairs[i].fConfig, W, H, 0, kPremul_SkAlphaType);
reed@google.com0a6151d2013-10-10 14:44:56 +0000234 if (SkBitmap::kIndex8_Config == gPairs[i].fConfig) {
235 ctOpaque = init_ctable(kOpaque_SkAlphaType);
236 ctPremul = init_ctable(kPremul_SkAlphaType);
237 }
238 srcOpaque.allocPixels(ctOpaque);
239 srcPremul.allocPixels(ctPremul);
240 SkSafeUnref(ctOpaque);
241 SkSafeUnref(ctPremul);
reed@android.com42263962009-05-01 04:00:01 +0000242 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000243 init_src(srcOpaque);
244 init_src(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000245
reed@google.com0a6151d2013-10-10 14:44:56 +0000246 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +0000247 bool expected = gPairs[i].fValid[j] != '0';
248 if (success != expected) {
249 SkString str;
250 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
251 gConfigName[i], gConfigName[j], boolStr(expected),
252 boolStr(success));
253 reporter->reportFailed(str);
254 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000255
reed@google.com0a6151d2013-10-10 14:44:56 +0000256 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000257 if (success != canSucceed) {
258 SkString str;
259 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
260 gConfigName[i], gConfigName[j], boolStr(success),
261 boolStr(canSucceed));
262 reporter->reportFailed(str);
263 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000264
reed@android.com42263962009-05-01 04:00:01 +0000265 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000266 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
267 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000268 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +0000269 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
270 if (srcPremul.config() == dst.config()) {
271 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000272 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000273 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000274 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000275 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000276 const char* dstP = (const char*)dst.getAddr(0, 0);
277 REPORTER_ASSERT(reporter, srcP != dstP);
278 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000279 srcPremul.getSize()));
280 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000281 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000282 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000283 }
reed@android.com311c82d2009-05-05 23:13:23 +0000284 // test extractSubset
285 {
reed@google.com0a6151d2013-10-10 14:44:56 +0000286 SkBitmap bitmap(srcOpaque);
reed@android.com311c82d2009-05-05 23:13:23 +0000287 SkBitmap subset;
288 SkIRect r;
289 r.set(1, 1, 2, 2);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000290 bitmap.setIsVolatile(true);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000291 if (bitmap.extractSubset(&subset, r)) {
reed@android.com311c82d2009-05-05 23:13:23 +0000292 REPORTER_ASSERT(reporter, subset.width() == 1);
293 REPORTER_ASSERT(reporter, subset.height() == 1);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000294 REPORTER_ASSERT(reporter,
reed@google.com383a6972013-10-21 14:00:07 +0000295 subset.alphaType() == bitmap.alphaType());
bsalomon@google.com37688532012-10-12 13:42:36 +0000296 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000297 subset.isVolatile() == true);
reed@android.com311c82d2009-05-05 23:13:23 +0000298
299 SkBitmap copy;
300 REPORTER_ASSERT(reporter,
301 subset.copyTo(&copy, subset.config()));
302 REPORTER_ASSERT(reporter, copy.width() == 1);
303 REPORTER_ASSERT(reporter, copy.height() == 1);
304 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000305
reed@android.com311c82d2009-05-05 23:13:23 +0000306 SkAutoLockPixels alp0(subset);
307 SkAutoLockPixels alp1(copy);
308 // they should both have, or both not-have, a colortable
309 bool hasCT = subset.getColorTable() != NULL;
310 REPORTER_ASSERT(reporter,
311 (copy.getColorTable() != NULL) == hasCT);
312 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000313 bitmap = srcPremul;
skyostil@google.com0eb75762012-01-16 10:45:53 +0000314 bitmap.setIsVolatile(false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000315 if (bitmap.extractSubset(&subset, r)) {
316 REPORTER_ASSERT(reporter,
reed@google.com383a6972013-10-21 14:00:07 +0000317 subset.alphaType() == bitmap.alphaType());
bsalomon@google.com37688532012-10-12 13:42:36 +0000318 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000319 subset.isVolatile() == false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000320 }
reed@android.com311c82d2009-05-05 23:13:23 +0000321 }
reed@android.com42263962009-05-01 04:00:01 +0000322 } else {
323 // dst should be unchanged from its initial state
324 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
325 REPORTER_ASSERT(reporter, dst.width() == 0);
326 REPORTER_ASSERT(reporter, dst.height() == 0);
327 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000328 } // for (size_t j = ...
329
330 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
331 // copyPixelsFrom().
332 //
333 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
334 ++copyCase) {
335 // Test copying to/from external buffer.
336 // Note: the tests below have hard-coded values ---
337 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000338
reed@google.com2cb14802013-06-26 14:35:02 +0000339 // Tests for getSafeSize64().
340 // Test with a very large configuration without pixel buffer
341 // attached.
342 SkBitmap tstSafeSize;
343 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
344 100000000U);
345 Sk64 safeSize = tstSafeSize.getSafeSize64();
346 if (safeSize.isNeg()) {
347 SkString str;
348 str.printf("getSafeSize64() negative: %s",
349 getSkConfigName(tstSafeSize));
350 reporter->reportFailed(str);
351 }
352 bool sizeFail = false;
353 // Compare against hand-computed values.
354 switch (gPairs[i].fConfig) {
355 case SkBitmap::kNo_Config:
356 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000357
reed@google.com2cb14802013-06-26 14:35:02 +0000358 case SkBitmap::kA8_Config:
359 case SkBitmap::kIndex8_Config:
360 if (safeSize.fHi != 0x2386F2 ||
361 safeSize.fLo != 0x6FC10000)
362 sizeFail = true;
363 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000364
reed@google.com2cb14802013-06-26 14:35:02 +0000365 case SkBitmap::kRGB_565_Config:
366 case SkBitmap::kARGB_4444_Config:
367 if (safeSize.fHi != 0x470DE4 ||
368 safeSize.fLo != 0xDF820000)
369 sizeFail = true;
370 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000371
reed@google.com2cb14802013-06-26 14:35:02 +0000372 case SkBitmap::kARGB_8888_Config:
373 if (safeSize.fHi != 0x8E1BC9 ||
374 safeSize.fLo != 0xBF040000)
375 sizeFail = true;
376 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000377
reed@google.com2cb14802013-06-26 14:35:02 +0000378 default:
379 break;
380 }
381 if (sizeFail) {
382 SkString str;
383 str.printf("getSafeSize64() wrong size: %s",
384 getSkConfigName(tstSafeSize));
385 reporter->reportFailed(str);
386 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000387
rmistry@google.comd6bab022013-12-02 13:50:38 +0000388 int subW = 2;
389 int subH = 2;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000390
reed@google.com2cb14802013-06-26 14:35:02 +0000391 // Create bitmap to act as source for copies and subsets.
392 SkBitmap src, subset;
393 SkColorTable* ct = NULL;
394 if (isExtracted[copyCase]) { // A larger image to extract from.
395 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
396 } else { // Tests expect a 2x2 bitmap, so make smaller.
397 src.setConfig(gPairs[i].fConfig, subW, subH);
398 }
399 if (SkBitmap::kIndex8_Config == src.config()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000400 ct = init_ctable(kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000401 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000402
reed@google.com2cb14802013-06-26 14:35:02 +0000403 src.allocPixels(ct);
404 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000405
reed@google.com2cb14802013-06-26 14:35:02 +0000406 // Either copy src or extract into 'subset', which is used
407 // for subsequent calls to copyPixelsTo/From.
408 bool srcReady = false;
409 if (isExtracted[copyCase]) {
410 // The extractedSubset() test case allows us to test copy-
411 // ing when src and dst mave possibly different strides.
412 SkIRect r;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000413 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000414
reed@google.com2cb14802013-06-26 14:35:02 +0000415 srcReady = src.extractSubset(&subset, r);
416 } else {
reed@google.com44699382013-10-31 17:28:30 +0000417 srcReady = src.copyTo(&subset, src.config());
reed@google.com2cb14802013-06-26 14:35:02 +0000418 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000419
reed@google.com2cb14802013-06-26 14:35:02 +0000420 // Not all configurations will generate a valid 'subset'.
421 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000422
reed@google.com2cb14802013-06-26 14:35:02 +0000423 // Allocate our target buffer 'buf' for all copies.
424 // To simplify verifying correctness of copies attach
425 // buf to a SkBitmap, but copies are done using the
426 // raw buffer pointer.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000427 const size_t bufSize = subH *
reed@google.com44699382013-10-31 17:28:30 +0000428 SkBitmap::ComputeRowBytes(src.config(), subW) * 2;
reed@google.com2cb14802013-06-26 14:35:02 +0000429 SkAutoMalloc autoBuf (bufSize);
430 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000431
reed@google.com2cb14802013-06-26 14:35:02 +0000432 SkBitmap bufBm; // Attach buf to this bitmap.
433 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000434
reed@google.com2cb14802013-06-26 14:35:02 +0000435 // Set up values for each pixel being copied.
436 Coordinates coords(subW * subH);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000437 for (int x = 0; x < subW; ++x)
438 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000439 {
440 int index = y * subW + x;
441 SkASSERT(index < coords.length);
442 coords[index]->fX = x;
443 coords[index]->fY = y;
444 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000445
reed@google.com2cb14802013-06-26 14:35:02 +0000446 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000447
reed@google.com2cb14802013-06-26 14:35:02 +0000448 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000449
reed@google.com2cb14802013-06-26 14:35:02 +0000450 // Before/after comparisons easier if we attach buf
451 // to an appropriately configured SkBitmap.
452 memset(buf, 0xFF, bufSize);
453 // Config with stride greater than src but that fits in buf.
454 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
reed@google.com44699382013-10-31 17:28:30 +0000455 SkBitmap::ComputeRowBytes(subset.config(), subW) * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000456 bufBm.setPixels(buf);
457 successExpected = false;
458 // Then attempt to copy with a stride that is too large
459 // to fit in the buffer.
460 REPORTER_ASSERT(reporter,
461 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
462 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000463
reed@google.com2cb14802013-06-26 14:35:02 +0000464 if (successExpected)
465 reportCopyVerification(subset, bufBm, coords,
466 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000467 reporter);
468
reed@google.com2cb14802013-06-26 14:35:02 +0000469 // Test #2 ////////////////////////////////////////////
470 // This test should always succeed, but in the case
471 // of extracted bitmaps only because we handle the
472 // issue of getSafeSize(). Without getSafeSize()
473 // buffer overrun/read would occur.
474 memset(buf, 0xFF, bufSize);
475 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
476 subset.rowBytes());
477 bufBm.setPixels(buf);
478 successExpected = subset.getSafeSize() <= bufSize;
479 REPORTER_ASSERT(reporter,
480 subset.copyPixelsTo(buf, bufSize) ==
481 successExpected);
482 if (successExpected)
483 reportCopyVerification(subset, bufBm, coords,
484 "copyPixelsTo(buf, bufSize)", reporter);
485
486 // Test #3 ////////////////////////////////////////////
487 // Copy with different stride between src and dst.
488 memset(buf, 0xFF, bufSize);
489 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
490 subset.rowBytes()+1);
491 bufBm.setPixels(buf);
492 successExpected = true; // Should always work.
493 REPORTER_ASSERT(reporter,
494 subset.copyPixelsTo(buf, bufSize,
495 subset.rowBytes()+1) == successExpected);
496 if (successExpected)
497 reportCopyVerification(subset, bufBm, coords,
498 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
499
500 // Test #4 ////////////////////////////////////////////
501 // Test copy with stride too small.
502 memset(buf, 0xFF, bufSize);
503 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
504 bufBm.setPixels(buf);
505 successExpected = false;
506 // Request copy with stride too small.
507 REPORTER_ASSERT(reporter,
508 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
509 == successExpected);
510 if (successExpected)
511 reportCopyVerification(subset, bufBm, coords,
512 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
513
514#if 0 // copyPixelsFrom is gone
515 // Test #5 ////////////////////////////////////////////
516 // Tests the case where the source stride is too small
517 // for the source configuration.
518 memset(buf, 0xFF, bufSize);
519 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
520 bufBm.setPixels(buf);
521 writeCoordPixels(bufBm, coords);
522 REPORTER_ASSERT(reporter,
523 subset.copyPixelsFrom(buf, bufSize, 1) == false);
524
525 // Test #6 ///////////////////////////////////////////
526 // Tests basic copy from an external buffer to the bitmap.
527 // If the bitmap is "extracted", this also tests the case
528 // where the source stride is different from the dest.
529 // stride.
530 // We've made the buffer large enough to always succeed.
531 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
532 bufBm.setPixels(buf);
533 writeCoordPixels(bufBm, coords);
534 REPORTER_ASSERT(reporter,
535 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
536 true);
537 reportCopyVerification(bufBm, subset, coords,
538 "copyPixelsFrom(buf, bufSize)",
539 reporter);
540
541 // Test #7 ////////////////////////////////////////////
542 // Tests the case where the source buffer is too small
543 // for the transfer.
544 REPORTER_ASSERT(reporter,
545 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
546 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000547
reed@google.comab77aaf2011-11-01 16:03:35 +0000548#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000549 }
550 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000551 }
552}
553
554#include "TestClassDef.h"
555DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)