blob: 93367bfe2dd913a67b0c7231f98c8b0b5573f740 [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 "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "TestClassDef.h"
reed@android.com42263962009-05-01 04:00:01 +00009#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) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000023 ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d",
24 gConfigName[src.config()], src.isOpaque(),
25 gConfigName[dst.config()], dst.isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +000026}
27
28static bool canHaveAlpha(SkBitmap::Config config) {
29 return config != SkBitmap::kRGB_565_Config;
30}
31
32// copyTo() should preserve isOpaque when it makes sense
reed@google.com0a6151d2013-10-10 14:44:56 +000033static void test_isOpaque(skiatest::Reporter* reporter,
34 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
reed@android.comcafc9f92009-08-22 03:44:57 +000035 SkBitmap::Config dstConfig) {
reed@android.comcafc9f92009-08-22 03:44:57 +000036 SkBitmap dst;
37
reed@google.com0a6151d2013-10-10 14:44:56 +000038 if (canHaveAlpha(srcPremul.config()) && canHaveAlpha(dstConfig)) {
39 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000040 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000041 if (srcPremul.isOpaque() != dst.isOpaque()) {
42 report_opaqueness(reporter, srcPremul, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000043 }
44 }
45
reed@google.com0a6151d2013-10-10 14:44:56 +000046 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000047 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000048 if (srcOpaque.isOpaque() != dst.isOpaque()) {
49 report_opaqueness(reporter, srcOpaque, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000050 }
51}
52
reed@google.com0a6151d2013-10-10 14:44:56 +000053static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000054 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000055 if (bitmap.getPixels()) {
reed@google.com0a6151d2013-10-10 14:44:56 +000056 if (bitmap.getColorTable()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000057 sk_bzero(bitmap.getPixels(), bitmap.getSize());
58 } else {
59 bitmap.eraseColor(SK_ColorWHITE);
60 }
reed@android.com42263962009-05-01 04:00:01 +000061 }
62}
63
reed@google.com0a6151d2013-10-10 14:44:56 +000064static SkColorTable* init_ctable(SkAlphaType alphaType) {
reed@android.com42263962009-05-01 04:00:01 +000065 static const SkColor colors[] = {
66 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
67 };
reed@google.com0a6151d2013-10-10 14:44:56 +000068 return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType);
reed@android.com42263962009-05-01 04:00:01 +000069}
70
71struct Pair {
72 SkBitmap::Config fConfig;
73 const char* fValid;
74};
75
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000076// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
77// getPixel()
78// setPixel()
79// getSkConfigName()
80// struct Coordinates
81// reportCopyVerification()
82// writeCoordPixels()
83
84// Utility function to read the value of a given pixel in bm. All
85// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +000086static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000087 uint32_t val = 0;
88 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +000089 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000090 SkAutoLockPixels lock(bm);
91 const void* rawAddr = bm.getAddr(x,y);
92
reed@google.com44699382013-10-31 17:28:30 +000093 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000094 case SkBitmap::kARGB_8888_Config:
95 memcpy(&val, rawAddr, sizeof(uint32_t));
96 break;
97 case SkBitmap::kARGB_4444_Config:
98 case SkBitmap::kRGB_565_Config:
99 memcpy(&val16, rawAddr, sizeof(uint16_t));
100 val = val16;
101 break;
102 case SkBitmap::kA8_Config:
103 case SkBitmap::kIndex8_Config:
104 memcpy(&val8, rawAddr, sizeof(uint8_t));
105 val = val8;
106 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000107 default:
108 break;
109 }
110 return val;
111}
112
113// Utility function to set value of any pixel in bm.
114// bm.getConfig() specifies what format 'val' must be
115// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000116static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000117 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000118 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000119 SkAutoLockPixels lock(bm);
120 void* rawAddr = bm.getAddr(x,y);
121
reed@google.com44699382013-10-31 17:28:30 +0000122 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000123 case SkBitmap::kARGB_8888_Config:
124 memcpy(rawAddr, &val, sizeof(uint32_t));
125 break;
126 case SkBitmap::kARGB_4444_Config:
127 case SkBitmap::kRGB_565_Config:
128 val16 = val & 0xFFFF;
129 memcpy(rawAddr, &val16, sizeof(uint16_t));
130 break;
131 case SkBitmap::kA8_Config:
132 case SkBitmap::kIndex8_Config:
133 val8 = val & 0xFF;
134 memcpy(rawAddr, &val8, sizeof(uint8_t));
135 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000136 default:
137 // Ignore.
138 break;
139 }
140}
141
142// Utility to return string containing name of each format, to
143// simplify diagnostic output.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000144static const char* getSkConfigName(const SkBitmap& bm) {
reed@google.com44699382013-10-31 17:28:30 +0000145 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000146 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000147 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
148 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
149 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
150 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
151 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000152 default: return "Unknown SkBitmap configuration.";
153 }
154}
155
156// Helper struct to contain pixel locations, while avoiding need for STL.
157struct Coordinates {
158
159 const int length;
160 SkIPoint* const data;
161
162 explicit Coordinates(int _length): length(_length)
163 , data(new SkIPoint[length]) { }
164
165 ~Coordinates(){
166 delete [] data;
167 }
168
169 SkIPoint* operator[](int i) const {
170 // Use with care, no bounds checking.
171 return data + i;
172 }
173};
174
175// A function to verify that two bitmaps contain the same pixel values
176// at all coordinates indicated by coords. Simplifies verification of
177// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000178static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000179 Coordinates& coords,
180 const char* msg,
181 skiatest::Reporter* reporter){
182 bool success = true;
183
184 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000185 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000186 success = success &&
187 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
188 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000189 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000190
191 if (!success) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000192 ERRORF(reporter, "%s [config = %s]", msg, getSkConfigName(bm1));
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000193 }
194}
195
196// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000197static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000198 for (int i = 0; i < coords.length; ++i)
199 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
200}
201
commit-bot@chromium.orgf2595e42014-01-10 17:22:01 +0000202static const Pair gPairs[] = {
203 { SkBitmap::kNo_Config, "0000000" },
204 { SkBitmap::kA8_Config, "0101010" },
205 { SkBitmap::kIndex8_Config, "0111010" },
206 { SkBitmap::kRGB_565_Config, "0101010" },
207 { SkBitmap::kARGB_4444_Config, "0101110" },
208 { SkBitmap::kARGB_8888_Config, "0101110" },
209};
weita@google.comf9ab99a2009-05-03 18:23:30 +0000210
commit-bot@chromium.orgf2595e42014-01-10 17:22:01 +0000211static const int W = 20;
212static const int H = 33;
213
214static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
215 SkBitmap::Config config) {
216 SkColorTable* ctOpaque = NULL;
217 SkColorTable* ctPremul = NULL;
218
219 srcOpaque->setConfig(config, W, H, 0, kOpaque_SkAlphaType);
220 srcPremul->setConfig(config, W, H, 0, kPremul_SkAlphaType);
221 if (SkBitmap::kIndex8_Config == config) {
222 ctOpaque = init_ctable(kOpaque_SkAlphaType);
223 ctPremul = init_ctable(kPremul_SkAlphaType);
224 }
225 srcOpaque->allocPixels(ctOpaque);
226 srcPremul->allocPixels(ctPremul);
227 SkSafeUnref(ctOpaque);
228 SkSafeUnref(ctPremul);
229 init_src(*srcOpaque);
230 init_src(*srcPremul);
231}
232
233DEF_TEST(BitmapCopy_extractSubset, reporter) {
234 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
235 SkBitmap srcOpaque, srcPremul;
236 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
237
238 SkBitmap bitmap(srcOpaque);
239 SkBitmap subset;
240 SkIRect r;
241 // Extract a subset which has the same width as the original. This
242 // catches a bug where we cloned the genID incorrectly.
243 r.set(0, 1, W, 3);
244 bitmap.setIsVolatile(true);
245 if (bitmap.extractSubset(&subset, r)) {
246 REPORTER_ASSERT(reporter, subset.width() == W);
247 REPORTER_ASSERT(reporter, subset.height() == 2);
248 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
249 REPORTER_ASSERT(reporter, subset.isVolatile() == true);
250
251 // Test copying an extracted subset.
252 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
253 SkBitmap copy;
254 bool success = subset.copyTo(&copy, gPairs[j].fConfig);
255 if (!success) {
256 // Skip checking that success matches fValid, which is redundant
257 // with the code below.
258 REPORTER_ASSERT(reporter, gPairs[i].fConfig != gPairs[j].fConfig);
259 continue;
260 }
261
262 // When performing a copy of an extracted subset, the gen id should
263 // change.
264 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID());
265
266 REPORTER_ASSERT(reporter, copy.width() == W);
267 REPORTER_ASSERT(reporter, copy.height() == 2);
268
269 if (gPairs[i].fConfig == gPairs[j].fConfig) {
270 SkAutoLockPixels alp0(subset);
271 SkAutoLockPixels alp1(copy);
272 // they should both have, or both not-have, a colortable
273 bool hasCT = subset.getColorTable() != NULL;
274 REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT);
275 }
276 }
277 }
278
279 bitmap = srcPremul;
280 bitmap.setIsVolatile(false);
281 if (bitmap.extractSubset(&subset, r)) {
282 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
283 REPORTER_ASSERT(reporter, subset.isVolatile() == false);
284 }
285 }
286}
287
288DEF_TEST(BitmapCopy, reporter) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000289 static const bool isExtracted[] = {
290 false, true
291 };
292
reed@android.com42263962009-05-01 04:00:01 +0000293 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
commit-bot@chromium.orgf2595e42014-01-10 17:22:01 +0000294 SkBitmap srcOpaque, srcPremul;
295 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
296
reed@android.com42263962009-05-01 04:00:01 +0000297 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
commit-bot@chromium.orgf2595e42014-01-10 17:22:01 +0000298 SkBitmap dst;
reed@android.com42263962009-05-01 04:00:01 +0000299
reed@google.com0a6151d2013-10-10 14:44:56 +0000300 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +0000301 bool expected = gPairs[i].fValid[j] != '0';
302 if (success != expected) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000303 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
304 "returned %s", gConfigName[i], gConfigName[j],
305 boolStr(expected), boolStr(success));
reed@android.com42263962009-05-01 04:00:01 +0000306 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000307
reed@google.com0a6151d2013-10-10 14:44:56 +0000308 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000309 if (success != canSucceed) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000310 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s "
311 "canCopyTo %s", gConfigName[i], gConfigName[j],
312 boolStr(success), boolStr(canSucceed));
reed@android.comfbaa88d2009-05-06 17:44:34 +0000313 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000314
reed@android.com42263962009-05-01 04:00:01 +0000315 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000316 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
317 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000318 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +0000319 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
320 if (srcPremul.config() == dst.config()) {
321 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000322 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000323 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000324 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000325 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000326 const char* dstP = (const char*)dst.getAddr(0, 0);
327 REPORTER_ASSERT(reporter, srcP != dstP);
328 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000329 srcPremul.getSize()));
330 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000331 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000332 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000333 }
reed@android.com42263962009-05-01 04:00:01 +0000334 } else {
335 // dst should be unchanged from its initial state
336 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
337 REPORTER_ASSERT(reporter, dst.width() == 0);
338 REPORTER_ASSERT(reporter, dst.height() == 0);
339 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000340 } // for (size_t j = ...
341
342 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
343 // copyPixelsFrom().
344 //
345 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
346 ++copyCase) {
347 // Test copying to/from external buffer.
348 // Note: the tests below have hard-coded values ---
349 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000350
reed@google.com2cb14802013-06-26 14:35:02 +0000351 // Tests for getSafeSize64().
352 // Test with a very large configuration without pixel buffer
353 // attached.
354 SkBitmap tstSafeSize;
355 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
356 100000000U);
reed@google.com57212f92013-12-30 14:40:38 +0000357 int64_t safeSize = tstSafeSize.computeSafeSize64();
358 if (safeSize < 0) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000359 ERRORF(reporter, "getSafeSize64() negative: %s",
360 getSkConfigName(tstSafeSize));
reed@google.com2cb14802013-06-26 14:35:02 +0000361 }
362 bool sizeFail = false;
363 // Compare against hand-computed values.
364 switch (gPairs[i].fConfig) {
365 case SkBitmap::kNo_Config:
366 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000367
reed@google.com2cb14802013-06-26 14:35:02 +0000368 case SkBitmap::kA8_Config:
369 case SkBitmap::kIndex8_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000370 if (safeSize != 0x2386F26FC10000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000371 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000372 }
reed@google.com2cb14802013-06-26 14:35:02 +0000373 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000374
reed@google.com2cb14802013-06-26 14:35:02 +0000375 case SkBitmap::kRGB_565_Config:
376 case SkBitmap::kARGB_4444_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000377 if (safeSize != 0x470DE4DF820000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000378 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000379 }
reed@google.com2cb14802013-06-26 14:35:02 +0000380 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000381
reed@google.com2cb14802013-06-26 14:35:02 +0000382 case SkBitmap::kARGB_8888_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000383 if (safeSize != 0x8E1BC9BF040000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000384 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000385 }
reed@google.com2cb14802013-06-26 14:35:02 +0000386 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000387
reed@google.com2cb14802013-06-26 14:35:02 +0000388 default:
389 break;
390 }
391 if (sizeFail) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000392 ERRORF(reporter, "computeSafeSize64() wrong size: %s",
393 getSkConfigName(tstSafeSize));
reed@google.com2cb14802013-06-26 14:35:02 +0000394 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000395
rmistry@google.comd6bab022013-12-02 13:50:38 +0000396 int subW = 2;
397 int subH = 2;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000398
reed@google.com2cb14802013-06-26 14:35:02 +0000399 // Create bitmap to act as source for copies and subsets.
400 SkBitmap src, subset;
401 SkColorTable* ct = NULL;
402 if (isExtracted[copyCase]) { // A larger image to extract from.
403 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
404 } else { // Tests expect a 2x2 bitmap, so make smaller.
405 src.setConfig(gPairs[i].fConfig, subW, subH);
406 }
407 if (SkBitmap::kIndex8_Config == src.config()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000408 ct = init_ctable(kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000409 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000410
reed@google.com2cb14802013-06-26 14:35:02 +0000411 src.allocPixels(ct);
412 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000413
reed@google.com2cb14802013-06-26 14:35:02 +0000414 // Either copy src or extract into 'subset', which is used
415 // for subsequent calls to copyPixelsTo/From.
416 bool srcReady = false;
417 if (isExtracted[copyCase]) {
418 // The extractedSubset() test case allows us to test copy-
419 // ing when src and dst mave possibly different strides.
420 SkIRect r;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000421 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000422
reed@google.com2cb14802013-06-26 14:35:02 +0000423 srcReady = src.extractSubset(&subset, r);
424 } else {
reed@google.com44699382013-10-31 17:28:30 +0000425 srcReady = src.copyTo(&subset, src.config());
reed@google.com2cb14802013-06-26 14:35:02 +0000426 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000427
reed@google.com2cb14802013-06-26 14:35:02 +0000428 // Not all configurations will generate a valid 'subset'.
429 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000430
reed@google.com2cb14802013-06-26 14:35:02 +0000431 // Allocate our target buffer 'buf' for all copies.
432 // To simplify verifying correctness of copies attach
433 // buf to a SkBitmap, but copies are done using the
434 // raw buffer pointer.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000435 const size_t bufSize = subH *
reed@google.com44699382013-10-31 17:28:30 +0000436 SkBitmap::ComputeRowBytes(src.config(), subW) * 2;
reed@google.com2cb14802013-06-26 14:35:02 +0000437 SkAutoMalloc autoBuf (bufSize);
438 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000439
reed@google.com2cb14802013-06-26 14:35:02 +0000440 SkBitmap bufBm; // Attach buf to this bitmap.
441 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000442
reed@google.com2cb14802013-06-26 14:35:02 +0000443 // Set up values for each pixel being copied.
444 Coordinates coords(subW * subH);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000445 for (int x = 0; x < subW; ++x)
446 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000447 {
448 int index = y * subW + x;
449 SkASSERT(index < coords.length);
450 coords[index]->fX = x;
451 coords[index]->fY = y;
452 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000453
reed@google.com2cb14802013-06-26 14:35:02 +0000454 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000455
reed@google.com2cb14802013-06-26 14:35:02 +0000456 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000457
reed@google.com2cb14802013-06-26 14:35:02 +0000458 // Before/after comparisons easier if we attach buf
459 // to an appropriately configured SkBitmap.
460 memset(buf, 0xFF, bufSize);
461 // Config with stride greater than src but that fits in buf.
462 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
reed@google.com44699382013-10-31 17:28:30 +0000463 SkBitmap::ComputeRowBytes(subset.config(), subW) * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000464 bufBm.setPixels(buf);
465 successExpected = false;
466 // Then attempt to copy with a stride that is too large
467 // to fit in the buffer.
468 REPORTER_ASSERT(reporter,
469 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
470 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000471
reed@google.com2cb14802013-06-26 14:35:02 +0000472 if (successExpected)
473 reportCopyVerification(subset, bufBm, coords,
474 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000475 reporter);
476
reed@google.com2cb14802013-06-26 14:35:02 +0000477 // Test #2 ////////////////////////////////////////////
478 // This test should always succeed, but in the case
479 // of extracted bitmaps only because we handle the
480 // issue of getSafeSize(). Without getSafeSize()
481 // buffer overrun/read would occur.
482 memset(buf, 0xFF, bufSize);
483 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
484 subset.rowBytes());
485 bufBm.setPixels(buf);
486 successExpected = subset.getSafeSize() <= bufSize;
487 REPORTER_ASSERT(reporter,
488 subset.copyPixelsTo(buf, bufSize) ==
489 successExpected);
490 if (successExpected)
491 reportCopyVerification(subset, bufBm, coords,
492 "copyPixelsTo(buf, bufSize)", reporter);
493
494 // Test #3 ////////////////////////////////////////////
495 // Copy with different stride between src and dst.
496 memset(buf, 0xFF, bufSize);
497 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
498 subset.rowBytes()+1);
499 bufBm.setPixels(buf);
500 successExpected = true; // Should always work.
501 REPORTER_ASSERT(reporter,
502 subset.copyPixelsTo(buf, bufSize,
503 subset.rowBytes()+1) == successExpected);
504 if (successExpected)
505 reportCopyVerification(subset, bufBm, coords,
506 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
507
508 // Test #4 ////////////////////////////////////////////
509 // Test copy with stride too small.
510 memset(buf, 0xFF, bufSize);
511 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
512 bufBm.setPixels(buf);
513 successExpected = false;
514 // Request copy with stride too small.
515 REPORTER_ASSERT(reporter,
516 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
517 == successExpected);
518 if (successExpected)
519 reportCopyVerification(subset, bufBm, coords,
520 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
521
522#if 0 // copyPixelsFrom is gone
523 // Test #5 ////////////////////////////////////////////
524 // Tests the case where the source stride is too small
525 // for the source configuration.
526 memset(buf, 0xFF, bufSize);
527 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
528 bufBm.setPixels(buf);
529 writeCoordPixels(bufBm, coords);
530 REPORTER_ASSERT(reporter,
531 subset.copyPixelsFrom(buf, bufSize, 1) == false);
532
533 // Test #6 ///////////////////////////////////////////
534 // Tests basic copy from an external buffer to the bitmap.
535 // If the bitmap is "extracted", this also tests the case
536 // where the source stride is different from the dest.
537 // stride.
538 // We've made the buffer large enough to always succeed.
539 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
540 bufBm.setPixels(buf);
541 writeCoordPixels(bufBm, coords);
542 REPORTER_ASSERT(reporter,
543 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
544 true);
545 reportCopyVerification(bufBm, subset, coords,
546 "copyPixelsFrom(buf, bufSize)",
547 reporter);
548
549 // Test #7 ////////////////////////////////////////////
550 // Tests the case where the source buffer is too small
551 // for the transfer.
552 REPORTER_ASSERT(reporter,
553 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
554 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000555
reed@google.comab77aaf2011-11-01 16:03:35 +0000556#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000557 }
558 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000559 }
560}