blob: 57ac95009f79621da9501ed742b15be0b4fe5e30 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com42263962009-05-01 04:00:01 +00007#include "SkBitmap.h"
reed@android.com311c82d2009-05-05 23:13:23 +00008#include "SkRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "Test.h"
reed@android.com42263962009-05-01 04:00:01 +000010
11static const char* boolStr(bool value) {
12 return value ? "true" : "false";
13}
14
15// these are in the same order as the SkBitmap::Config enum
16static const char* gConfigName[] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +000017 "None", "A8", "Index8", "565", "4444", "8888"
reed@android.com42263962009-05-01 04:00:01 +000018};
19
reed@android.comcafc9f92009-08-22 03:44:57 +000020static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
21 const SkBitmap& dst) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000022 ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d",
23 gConfigName[src.config()], src.isOpaque(),
24 gConfigName[dst.config()], dst.isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +000025}
26
27static bool canHaveAlpha(SkBitmap::Config config) {
28 return config != SkBitmap::kRGB_565_Config;
29}
30
31// copyTo() should preserve isOpaque when it makes sense
reed@google.com0a6151d2013-10-10 14:44:56 +000032static void test_isOpaque(skiatest::Reporter* reporter,
33 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
reed@android.comcafc9f92009-08-22 03:44:57 +000034 SkBitmap::Config dstConfig) {
reed@android.comcafc9f92009-08-22 03:44:57 +000035 SkBitmap dst;
36
reed@google.com0a6151d2013-10-10 14:44:56 +000037 if (canHaveAlpha(srcPremul.config()) && canHaveAlpha(dstConfig)) {
38 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000039 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000040 if (srcPremul.isOpaque() != dst.isOpaque()) {
41 report_opaqueness(reporter, srcPremul, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000042 }
43 }
44
reed@google.com0a6151d2013-10-10 14:44:56 +000045 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000046 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000047 if (srcOpaque.isOpaque() != dst.isOpaque()) {
48 report_opaqueness(reporter, srcOpaque, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000049 }
50}
51
reed@google.com0a6151d2013-10-10 14:44:56 +000052static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000053 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000054 if (bitmap.getPixels()) {
reed@google.com0a6151d2013-10-10 14:44:56 +000055 if (bitmap.getColorTable()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000056 sk_bzero(bitmap.getPixels(), bitmap.getSize());
57 } else {
58 bitmap.eraseColor(SK_ColorWHITE);
59 }
reed@android.com42263962009-05-01 04:00:01 +000060 }
61}
62
reed@google.com0a6151d2013-10-10 14:44:56 +000063static SkColorTable* init_ctable(SkAlphaType alphaType) {
reed@android.com42263962009-05-01 04:00:01 +000064 static const SkColor colors[] = {
65 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
66 };
reed@google.com0a6151d2013-10-10 14:44:56 +000067 return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType);
reed@android.com42263962009-05-01 04:00:01 +000068}
69
70struct Pair {
71 SkBitmap::Config fConfig;
72 const char* fValid;
73};
74
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000075// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
76// getPixel()
77// setPixel()
78// getSkConfigName()
79// struct Coordinates
80// reportCopyVerification()
81// writeCoordPixels()
82
83// Utility function to read the value of a given pixel in bm. All
84// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +000085static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000086 uint32_t val = 0;
87 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +000088 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000089 SkAutoLockPixels lock(bm);
90 const void* rawAddr = bm.getAddr(x,y);
91
reed@google.com44699382013-10-31 17:28:30 +000092 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000093 case SkBitmap::kARGB_8888_Config:
94 memcpy(&val, rawAddr, sizeof(uint32_t));
95 break;
96 case SkBitmap::kARGB_4444_Config:
97 case SkBitmap::kRGB_565_Config:
98 memcpy(&val16, rawAddr, sizeof(uint16_t));
99 val = val16;
100 break;
101 case SkBitmap::kA8_Config:
102 case SkBitmap::kIndex8_Config:
103 memcpy(&val8, rawAddr, sizeof(uint8_t));
104 val = val8;
105 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000106 default:
107 break;
108 }
109 return val;
110}
111
112// Utility function to set value of any pixel in bm.
113// bm.getConfig() specifies what format 'val' must be
114// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000115static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000116 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000117 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000118 SkAutoLockPixels lock(bm);
119 void* rawAddr = bm.getAddr(x,y);
120
reed@google.com44699382013-10-31 17:28:30 +0000121 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000122 case SkBitmap::kARGB_8888_Config:
123 memcpy(rawAddr, &val, sizeof(uint32_t));
124 break;
125 case SkBitmap::kARGB_4444_Config:
126 case SkBitmap::kRGB_565_Config:
127 val16 = val & 0xFFFF;
128 memcpy(rawAddr, &val16, sizeof(uint16_t));
129 break;
130 case SkBitmap::kA8_Config:
131 case SkBitmap::kIndex8_Config:
132 val8 = val & 0xFF;
133 memcpy(rawAddr, &val8, sizeof(uint8_t));
134 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000135 default:
136 // Ignore.
137 break;
138 }
139}
140
141// Utility to return string containing name of each format, to
142// simplify diagnostic output.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000143static const char* getSkConfigName(const SkBitmap& bm) {
reed@google.com44699382013-10-31 17:28:30 +0000144 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000145 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000146 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
147 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
148 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
149 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
150 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000151 default: return "Unknown SkBitmap configuration.";
152 }
153}
154
155// Helper struct to contain pixel locations, while avoiding need for STL.
156struct Coordinates {
157
158 const int length;
159 SkIPoint* const data;
160
161 explicit Coordinates(int _length): length(_length)
162 , data(new SkIPoint[length]) { }
163
164 ~Coordinates(){
165 delete [] data;
166 }
167
168 SkIPoint* operator[](int i) const {
169 // Use with care, no bounds checking.
170 return data + i;
171 }
172};
173
174// A function to verify that two bitmaps contain the same pixel values
175// at all coordinates indicated by coords. Simplifies verification of
176// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000177static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000178 Coordinates& coords,
179 const char* msg,
180 skiatest::Reporter* reporter){
181 bool success = true;
182
183 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000184 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000185 success = success &&
186 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
187 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000188 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000189
190 if (!success) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000191 ERRORF(reporter, "%s [config = %s]", msg, getSkConfigName(bm1));
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000192 }
193}
194
195// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000196static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000197 for (int i = 0; i < coords.length; ++i)
198 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
199}
200
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000201static const Pair gPairs[] = {
202 { SkBitmap::kNo_Config, "0000000" },
203 { SkBitmap::kA8_Config, "0101010" },
204 { SkBitmap::kIndex8_Config, "0111010" },
205 { SkBitmap::kRGB_565_Config, "0101010" },
206 { SkBitmap::kARGB_4444_Config, "0101110" },
207 { SkBitmap::kARGB_8888_Config, "0101110" },
208};
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000209
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000210static const int W = 20;
211static const int H = 33;
212
213static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
214 SkBitmap::Config config) {
215 SkColorTable* ctOpaque = NULL;
216 SkColorTable* ctPremul = NULL;
217
218 srcOpaque->setConfig(config, W, H, 0, kOpaque_SkAlphaType);
219 srcPremul->setConfig(config, W, H, 0, kPremul_SkAlphaType);
220 if (SkBitmap::kIndex8_Config == config) {
221 ctOpaque = init_ctable(kOpaque_SkAlphaType);
222 ctPremul = init_ctable(kPremul_SkAlphaType);
223 }
224 srcOpaque->allocPixels(ctOpaque);
225 srcPremul->allocPixels(ctPremul);
226 SkSafeUnref(ctOpaque);
227 SkSafeUnref(ctPremul);
228 init_src(*srcOpaque);
229 init_src(*srcPremul);
230}
231
232DEF_TEST(BitmapCopy_extractSubset, reporter) {
233 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
234 SkBitmap srcOpaque, srcPremul;
235 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
236
237 SkBitmap bitmap(srcOpaque);
238 SkBitmap subset;
239 SkIRect r;
240 // Extract a subset which has the same width as the original. This
241 // catches a bug where we cloned the genID incorrectly.
242 r.set(0, 1, W, 3);
243 bitmap.setIsVolatile(true);
244 if (bitmap.extractSubset(&subset, r)) {
245 REPORTER_ASSERT(reporter, subset.width() == W);
246 REPORTER_ASSERT(reporter, subset.height() == 2);
247 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
248 REPORTER_ASSERT(reporter, subset.isVolatile() == true);
249
250 // Test copying an extracted subset.
251 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
252 SkBitmap copy;
253 bool success = subset.copyTo(&copy, gPairs[j].fConfig);
254 if (!success) {
255 // Skip checking that success matches fValid, which is redundant
256 // with the code below.
257 REPORTER_ASSERT(reporter, gPairs[i].fConfig != gPairs[j].fConfig);
258 continue;
259 }
260
261 // When performing a copy of an extracted subset, the gen id should
262 // change.
263 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID());
264
265 REPORTER_ASSERT(reporter, copy.width() == W);
266 REPORTER_ASSERT(reporter, copy.height() == 2);
267
268 if (gPairs[i].fConfig == gPairs[j].fConfig) {
269 SkAutoLockPixels alp0(subset);
270 SkAutoLockPixels alp1(copy);
271 // they should both have, or both not-have, a colortable
272 bool hasCT = subset.getColorTable() != NULL;
273 REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT);
274 }
275 }
276 }
277
278 bitmap = srcPremul;
279 bitmap.setIsVolatile(false);
280 if (bitmap.extractSubset(&subset, r)) {
281 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
282 REPORTER_ASSERT(reporter, subset.isVolatile() == false);
283 }
284 }
285}
286
287DEF_TEST(BitmapCopy, reporter) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000288 static const bool isExtracted[] = {
289 false, true
290 };
291
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000292 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000293 SkBitmap srcOpaque, srcPremul;
294 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
295
reed@android.com42263962009-05-01 04:00:01 +0000296 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000297 SkBitmap dst;
reed@android.com42263962009-05-01 04:00:01 +0000298
reed@google.com0a6151d2013-10-10 14:44:56 +0000299 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +0000300 bool expected = gPairs[i].fValid[j] != '0';
301 if (success != expected) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000302 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
303 "returned %s", gConfigName[i], gConfigName[j],
304 boolStr(expected), boolStr(success));
reed@android.com42263962009-05-01 04:00:01 +0000305 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000306
reed@google.com0a6151d2013-10-10 14:44:56 +0000307 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000308 if (success != canSucceed) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000309 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s "
310 "canCopyTo %s", gConfigName[i], gConfigName[j],
311 boolStr(success), boolStr(canSucceed));
reed@android.comfbaa88d2009-05-06 17:44:34 +0000312 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000313
reed@android.com42263962009-05-01 04:00:01 +0000314 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000315 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
316 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000317 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +0000318 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
319 if (srcPremul.config() == dst.config()) {
320 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000321 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000322 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000323 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000324 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000325 const char* dstP = (const char*)dst.getAddr(0, 0);
326 REPORTER_ASSERT(reporter, srcP != dstP);
327 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000328 srcPremul.getSize()));
329 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000330 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000331 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000332 }
reed@android.com42263962009-05-01 04:00:01 +0000333 } else {
334 // dst should be unchanged from its initial state
335 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
336 REPORTER_ASSERT(reporter, dst.width() == 0);
337 REPORTER_ASSERT(reporter, dst.height() == 0);
338 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000339 } // for (size_t j = ...
340
341 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
342 // copyPixelsFrom().
343 //
344 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
345 ++copyCase) {
346 // Test copying to/from external buffer.
347 // Note: the tests below have hard-coded values ---
348 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000349
reed@google.com2cb14802013-06-26 14:35:02 +0000350 // Tests for getSafeSize64().
351 // Test with a very large configuration without pixel buffer
352 // attached.
353 SkBitmap tstSafeSize;
354 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
355 100000000U);
reed@google.com57212f92013-12-30 14:40:38 +0000356 int64_t safeSize = tstSafeSize.computeSafeSize64();
357 if (safeSize < 0) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000358 ERRORF(reporter, "getSafeSize64() negative: %s",
359 getSkConfigName(tstSafeSize));
reed@google.com2cb14802013-06-26 14:35:02 +0000360 }
361 bool sizeFail = false;
362 // Compare against hand-computed values.
363 switch (gPairs[i].fConfig) {
364 case SkBitmap::kNo_Config:
365 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000366
reed@google.com2cb14802013-06-26 14:35:02 +0000367 case SkBitmap::kA8_Config:
368 case SkBitmap::kIndex8_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000369 if (safeSize != 0x2386F26FC10000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000370 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000371 }
reed@google.com2cb14802013-06-26 14:35:02 +0000372 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000373
reed@google.com2cb14802013-06-26 14:35:02 +0000374 case SkBitmap::kRGB_565_Config:
375 case SkBitmap::kARGB_4444_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000376 if (safeSize != 0x470DE4DF820000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000377 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000378 }
reed@google.com2cb14802013-06-26 14:35:02 +0000379 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000380
reed@google.com2cb14802013-06-26 14:35:02 +0000381 case SkBitmap::kARGB_8888_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000382 if (safeSize != 0x8E1BC9BF040000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000383 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000384 }
reed@google.com2cb14802013-06-26 14:35:02 +0000385 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000386
reed@google.com2cb14802013-06-26 14:35:02 +0000387 default:
388 break;
389 }
390 if (sizeFail) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000391 ERRORF(reporter, "computeSafeSize64() wrong size: %s",
392 getSkConfigName(tstSafeSize));
reed@google.com2cb14802013-06-26 14:35:02 +0000393 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000394
rmistry@google.comd6bab022013-12-02 13:50:38 +0000395 int subW = 2;
396 int subH = 2;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000397
reed@google.com2cb14802013-06-26 14:35:02 +0000398 // Create bitmap to act as source for copies and subsets.
399 SkBitmap src, subset;
400 SkColorTable* ct = NULL;
401 if (isExtracted[copyCase]) { // A larger image to extract from.
402 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
403 } else { // Tests expect a 2x2 bitmap, so make smaller.
404 src.setConfig(gPairs[i].fConfig, subW, subH);
405 }
406 if (SkBitmap::kIndex8_Config == src.config()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000407 ct = init_ctable(kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000408 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000409
reed@google.com2cb14802013-06-26 14:35:02 +0000410 src.allocPixels(ct);
411 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000412
reed@google.com2cb14802013-06-26 14:35:02 +0000413 // Either copy src or extract into 'subset', which is used
414 // for subsequent calls to copyPixelsTo/From.
415 bool srcReady = false;
416 if (isExtracted[copyCase]) {
417 // The extractedSubset() test case allows us to test copy-
418 // ing when src and dst mave possibly different strides.
419 SkIRect r;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000420 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000421
reed@google.com2cb14802013-06-26 14:35:02 +0000422 srcReady = src.extractSubset(&subset, r);
423 } else {
reed@google.com44699382013-10-31 17:28:30 +0000424 srcReady = src.copyTo(&subset, src.config());
reed@google.com2cb14802013-06-26 14:35:02 +0000425 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000426
reed@google.com2cb14802013-06-26 14:35:02 +0000427 // Not all configurations will generate a valid 'subset'.
428 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000429
reed@google.com2cb14802013-06-26 14:35:02 +0000430 // Allocate our target buffer 'buf' for all copies.
431 // To simplify verifying correctness of copies attach
432 // buf to a SkBitmap, but copies are done using the
433 // raw buffer pointer.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000434 const size_t bufSize = subH *
reed@google.com44699382013-10-31 17:28:30 +0000435 SkBitmap::ComputeRowBytes(src.config(), subW) * 2;
reed@google.com2cb14802013-06-26 14:35:02 +0000436 SkAutoMalloc autoBuf (bufSize);
437 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000438
reed@google.com2cb14802013-06-26 14:35:02 +0000439 SkBitmap bufBm; // Attach buf to this bitmap.
440 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000441
reed@google.com2cb14802013-06-26 14:35:02 +0000442 // Set up values for each pixel being copied.
443 Coordinates coords(subW * subH);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000444 for (int x = 0; x < subW; ++x)
445 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000446 {
447 int index = y * subW + x;
448 SkASSERT(index < coords.length);
449 coords[index]->fX = x;
450 coords[index]->fY = y;
451 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000452
reed@google.com2cb14802013-06-26 14:35:02 +0000453 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000454
reed@google.com2cb14802013-06-26 14:35:02 +0000455 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000456
reed@google.com2cb14802013-06-26 14:35:02 +0000457 // Before/after comparisons easier if we attach buf
458 // to an appropriately configured SkBitmap.
459 memset(buf, 0xFF, bufSize);
460 // Config with stride greater than src but that fits in buf.
461 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
reed@google.com44699382013-10-31 17:28:30 +0000462 SkBitmap::ComputeRowBytes(subset.config(), subW) * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000463 bufBm.setPixels(buf);
464 successExpected = false;
465 // Then attempt to copy with a stride that is too large
466 // to fit in the buffer.
467 REPORTER_ASSERT(reporter,
468 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
469 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000470
reed@google.com2cb14802013-06-26 14:35:02 +0000471 if (successExpected)
472 reportCopyVerification(subset, bufBm, coords,
473 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000474 reporter);
475
reed@google.com2cb14802013-06-26 14:35:02 +0000476 // Test #2 ////////////////////////////////////////////
477 // This test should always succeed, but in the case
478 // of extracted bitmaps only because we handle the
479 // issue of getSafeSize(). Without getSafeSize()
480 // buffer overrun/read would occur.
481 memset(buf, 0xFF, bufSize);
482 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
483 subset.rowBytes());
484 bufBm.setPixels(buf);
485 successExpected = subset.getSafeSize() <= bufSize;
486 REPORTER_ASSERT(reporter,
487 subset.copyPixelsTo(buf, bufSize) ==
488 successExpected);
489 if (successExpected)
490 reportCopyVerification(subset, bufBm, coords,
491 "copyPixelsTo(buf, bufSize)", reporter);
492
493 // Test #3 ////////////////////////////////////////////
494 // Copy with different stride between src and dst.
495 memset(buf, 0xFF, bufSize);
496 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
497 subset.rowBytes()+1);
498 bufBm.setPixels(buf);
499 successExpected = true; // Should always work.
500 REPORTER_ASSERT(reporter,
501 subset.copyPixelsTo(buf, bufSize,
502 subset.rowBytes()+1) == successExpected);
503 if (successExpected)
504 reportCopyVerification(subset, bufBm, coords,
505 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
506
507 // Test #4 ////////////////////////////////////////////
508 // Test copy with stride too small.
509 memset(buf, 0xFF, bufSize);
510 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
511 bufBm.setPixels(buf);
512 successExpected = false;
513 // Request copy with stride too small.
514 REPORTER_ASSERT(reporter,
515 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
516 == successExpected);
517 if (successExpected)
518 reportCopyVerification(subset, bufBm, coords,
519 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
520
521#if 0 // copyPixelsFrom is gone
522 // Test #5 ////////////////////////////////////////////
523 // Tests the case where the source stride is too small
524 // for the source configuration.
525 memset(buf, 0xFF, bufSize);
526 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
527 bufBm.setPixels(buf);
528 writeCoordPixels(bufBm, coords);
529 REPORTER_ASSERT(reporter,
530 subset.copyPixelsFrom(buf, bufSize, 1) == false);
531
532 // Test #6 ///////////////////////////////////////////
533 // Tests basic copy from an external buffer to the bitmap.
534 // If the bitmap is "extracted", this also tests the case
535 // where the source stride is different from the dest.
536 // stride.
537 // We've made the buffer large enough to always succeed.
538 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
539 bufBm.setPixels(buf);
540 writeCoordPixels(bufBm, coords);
541 REPORTER_ASSERT(reporter,
542 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
543 true);
544 reportCopyVerification(bufBm, subset, coords,
545 "copyPixelsFrom(buf, bufSize)",
546 reporter);
547
548 // Test #7 ////////////////////////////////////////////
549 // Tests the case where the source buffer is too small
550 // for the transfer.
551 REPORTER_ASSERT(reporter,
552 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
553 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000554
reed@google.comab77aaf2011-11-01 16:03:35 +0000555#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000556 }
557 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000558 }
559}