blob: 455fc691e3ec004a3c8433d6eb095b37b68559b9 [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@android.com42263962009-05-01 04:00:01 +000010#include "SkBitmap.h"
reed@android.com311c82d2009-05-05 23:13:23 +000011#include "SkRect.h"
reed@android.com42263962009-05-01 04:00:01 +000012
13static const char* boolStr(bool value) {
14 return value ? "true" : "false";
15}
16
17// these are in the same order as the SkBitmap::Config enum
18static const char* gConfigName[] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +000019 "None", "A8", "Index8", "565", "4444", "8888"
reed@android.com42263962009-05-01 04:00:01 +000020};
21
reed@android.comcafc9f92009-08-22 03:44:57 +000022static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
23 const SkBitmap& dst) {
24 SkString str;
25 str.printf("src %s opaque:%d, dst %s opaque:%d",
26 gConfigName[src.config()], src.isOpaque(),
27 gConfigName[dst.config()], dst.isOpaque());
28 reporter->reportFailed(str);
29}
30
31static bool canHaveAlpha(SkBitmap::Config config) {
32 return config != SkBitmap::kRGB_565_Config;
33}
34
35// copyTo() should preserve isOpaque when it makes sense
reed@google.com0a6151d2013-10-10 14:44:56 +000036static void test_isOpaque(skiatest::Reporter* reporter,
37 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
reed@android.comcafc9f92009-08-22 03:44:57 +000038 SkBitmap::Config dstConfig) {
reed@android.comcafc9f92009-08-22 03:44:57 +000039 SkBitmap dst;
40
reed@google.com0a6151d2013-10-10 14:44:56 +000041 if (canHaveAlpha(srcPremul.config()) && canHaveAlpha(dstConfig)) {
42 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000043 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000044 if (srcPremul.isOpaque() != dst.isOpaque()) {
45 report_opaqueness(reporter, srcPremul, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000046 }
47 }
48
reed@google.com0a6151d2013-10-10 14:44:56 +000049 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstConfig));
reed@android.comcafc9f92009-08-22 03:44:57 +000050 REPORTER_ASSERT(reporter, dst.config() == dstConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +000051 if (srcOpaque.isOpaque() != dst.isOpaque()) {
52 report_opaqueness(reporter, srcOpaque, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000053 }
54}
55
reed@google.com0a6151d2013-10-10 14:44:56 +000056static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000057 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000058 if (bitmap.getPixels()) {
reed@google.com0a6151d2013-10-10 14:44:56 +000059 if (bitmap.getColorTable()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000060 sk_bzero(bitmap.getPixels(), bitmap.getSize());
61 } else {
62 bitmap.eraseColor(SK_ColorWHITE);
63 }
reed@android.com42263962009-05-01 04:00:01 +000064 }
65}
66
reed@google.com0a6151d2013-10-10 14:44:56 +000067static SkColorTable* init_ctable(SkAlphaType alphaType) {
reed@android.com42263962009-05-01 04:00:01 +000068 static const SkColor colors[] = {
69 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
70 };
reed@google.com0a6151d2013-10-10 14:44:56 +000071 return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType);
reed@android.com42263962009-05-01 04:00:01 +000072}
73
74struct Pair {
75 SkBitmap::Config fConfig;
76 const char* fValid;
77};
78
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000079// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
80// getPixel()
81// setPixel()
82// getSkConfigName()
83// struct Coordinates
84// reportCopyVerification()
85// writeCoordPixels()
86
87// Utility function to read the value of a given pixel in bm. All
88// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +000089static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000090 uint32_t val = 0;
91 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +000092 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000093 SkAutoLockPixels lock(bm);
94 const void* rawAddr = bm.getAddr(x,y);
95
reed@google.com44699382013-10-31 17:28:30 +000096 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000097 case SkBitmap::kARGB_8888_Config:
98 memcpy(&val, rawAddr, sizeof(uint32_t));
99 break;
100 case SkBitmap::kARGB_4444_Config:
101 case SkBitmap::kRGB_565_Config:
102 memcpy(&val16, rawAddr, sizeof(uint16_t));
103 val = val16;
104 break;
105 case SkBitmap::kA8_Config:
106 case SkBitmap::kIndex8_Config:
107 memcpy(&val8, rawAddr, sizeof(uint8_t));
108 val = val8;
109 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000110 default:
111 break;
112 }
113 return val;
114}
115
116// Utility function to set value of any pixel in bm.
117// bm.getConfig() specifies what format 'val' must be
118// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000119static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000120 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000121 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000122 SkAutoLockPixels lock(bm);
123 void* rawAddr = bm.getAddr(x,y);
124
reed@google.com44699382013-10-31 17:28:30 +0000125 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000126 case SkBitmap::kARGB_8888_Config:
127 memcpy(rawAddr, &val, sizeof(uint32_t));
128 break;
129 case SkBitmap::kARGB_4444_Config:
130 case SkBitmap::kRGB_565_Config:
131 val16 = val & 0xFFFF;
132 memcpy(rawAddr, &val16, sizeof(uint16_t));
133 break;
134 case SkBitmap::kA8_Config:
135 case SkBitmap::kIndex8_Config:
136 val8 = val & 0xFF;
137 memcpy(rawAddr, &val8, sizeof(uint8_t));
138 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000139 default:
140 // Ignore.
141 break;
142 }
143}
144
145// Utility to return string containing name of each format, to
146// simplify diagnostic output.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000147static const char* getSkConfigName(const SkBitmap& bm) {
reed@google.com44699382013-10-31 17:28:30 +0000148 switch (bm.config()) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000149 case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000150 case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
151 case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
152 case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
153 case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
154 case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000155 default: return "Unknown SkBitmap configuration.";
156 }
157}
158
159// Helper struct to contain pixel locations, while avoiding need for STL.
160struct Coordinates {
161
162 const int length;
163 SkIPoint* const data;
164
165 explicit Coordinates(int _length): length(_length)
166 , data(new SkIPoint[length]) { }
167
168 ~Coordinates(){
169 delete [] data;
170 }
171
172 SkIPoint* operator[](int i) const {
173 // Use with care, no bounds checking.
174 return data + i;
175 }
176};
177
178// A function to verify that two bitmaps contain the same pixel values
179// at all coordinates indicated by coords. Simplifies verification of
180// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000181static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000182 Coordinates& coords,
183 const char* msg,
184 skiatest::Reporter* reporter){
185 bool success = true;
186
187 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000188 for (int i = 0; i < coords.length; ++i) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000189 success = success &&
190 (getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
191 getPixel(coords[i]->fX, coords[i]->fY, bm2));
scroggo@google.comd5764e82012-08-22 15:00:05 +0000192 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000193
194 if (!success) {
195 SkString str;
196 str.printf("%s [config = %s]",
197 msg, getSkConfigName(bm1));
198 reporter->reportFailed(str);
199 }
200}
201
202// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000203static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000204 for (int i = 0; i < coords.length; ++i)
205 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
206}
207
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000208DEF_TEST(BitmapCopy, reporter) {
reed@android.com42263962009-05-01 04:00:01 +0000209 static const Pair gPairs[] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +0000210 { SkBitmap::kNo_Config, "0000000" },
211 { SkBitmap::kA8_Config, "0101010" },
212 { SkBitmap::kIndex8_Config, "0111010" },
213 { SkBitmap::kRGB_565_Config, "0101010" },
214 { SkBitmap::kARGB_4444_Config, "0101110" },
215 { SkBitmap::kARGB_8888_Config, "0101110" },
reed@android.com42263962009-05-01 04:00:01 +0000216 };
weita@google.comf9ab99a2009-05-03 18:23:30 +0000217
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000218 static const bool isExtracted[] = {
219 false, true
220 };
221
reed@android.com42263962009-05-01 04:00:01 +0000222 const int W = 20;
223 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000224
reed@android.com42263962009-05-01 04:00:01 +0000225 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
226 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000227 SkBitmap srcOpaque, srcPremul, dst;
skia.committer@gmail.comd33115d2013-10-11 07:01:39 +0000228
reed@google.com0a6151d2013-10-10 14:44:56 +0000229 {
230 SkColorTable* ctOpaque = NULL;
231 SkColorTable* ctPremul = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +0000232
reed@google.com383a6972013-10-21 14:00:07 +0000233 srcOpaque.setConfig(gPairs[i].fConfig, W, H, 0, kOpaque_SkAlphaType);
234 srcPremul.setConfig(gPairs[i].fConfig, W, H, 0, kPremul_SkAlphaType);
reed@google.com0a6151d2013-10-10 14:44:56 +0000235 if (SkBitmap::kIndex8_Config == gPairs[i].fConfig) {
236 ctOpaque = init_ctable(kOpaque_SkAlphaType);
237 ctPremul = init_ctable(kPremul_SkAlphaType);
238 }
239 srcOpaque.allocPixels(ctOpaque);
240 srcPremul.allocPixels(ctPremul);
241 SkSafeUnref(ctOpaque);
242 SkSafeUnref(ctPremul);
reed@android.com42263962009-05-01 04:00:01 +0000243 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000244 init_src(srcOpaque);
245 init_src(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000246
reed@google.com0a6151d2013-10-10 14:44:56 +0000247 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +0000248 bool expected = gPairs[i].fValid[j] != '0';
249 if (success != expected) {
250 SkString str;
251 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
252 gConfigName[i], gConfigName[j], boolStr(expected),
253 boolStr(success));
254 reporter->reportFailed(str);
255 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000256
reed@google.com0a6151d2013-10-10 14:44:56 +0000257 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000258 if (success != canSucceed) {
259 SkString str;
260 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
261 gConfigName[i], gConfigName[j], boolStr(success),
262 boolStr(canSucceed));
263 reporter->reportFailed(str);
264 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000265
reed@android.com42263962009-05-01 04:00:01 +0000266 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000267 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
268 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +0000269 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@google.com0a6151d2013-10-10 14:44:56 +0000270 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
271 if (srcPremul.config() == dst.config()) {
272 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000273 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000274 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000275 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000276 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000277 const char* dstP = (const char*)dst.getAddr(0, 0);
278 REPORTER_ASSERT(reporter, srcP != dstP);
279 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000280 srcPremul.getSize()));
281 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000282 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000283 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000284 }
reed@android.com311c82d2009-05-05 23:13:23 +0000285 // test extractSubset
286 {
reed@google.com0a6151d2013-10-10 14:44:56 +0000287 SkBitmap bitmap(srcOpaque);
reed@android.com311c82d2009-05-05 23:13:23 +0000288 SkBitmap subset;
289 SkIRect r;
290 r.set(1, 1, 2, 2);
skyostil@google.com0eb75762012-01-16 10:45:53 +0000291 bitmap.setIsVolatile(true);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000292 if (bitmap.extractSubset(&subset, r)) {
reed@android.com311c82d2009-05-05 23:13:23 +0000293 REPORTER_ASSERT(reporter, subset.width() == 1);
294 REPORTER_ASSERT(reporter, subset.height() == 1);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000295 REPORTER_ASSERT(reporter,
reed@google.com383a6972013-10-21 14:00:07 +0000296 subset.alphaType() == bitmap.alphaType());
bsalomon@google.com37688532012-10-12 13:42:36 +0000297 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000298 subset.isVolatile() == true);
reed@android.com311c82d2009-05-05 23:13:23 +0000299
300 SkBitmap copy;
301 REPORTER_ASSERT(reporter,
302 subset.copyTo(&copy, subset.config()));
303 REPORTER_ASSERT(reporter, copy.width() == 1);
304 REPORTER_ASSERT(reporter, copy.height() == 1);
305 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000306
reed@android.com311c82d2009-05-05 23:13:23 +0000307 SkAutoLockPixels alp0(subset);
308 SkAutoLockPixels alp1(copy);
309 // they should both have, or both not-have, a colortable
310 bool hasCT = subset.getColorTable() != NULL;
311 REPORTER_ASSERT(reporter,
312 (copy.getColorTable() != NULL) == hasCT);
313 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000314 bitmap = srcPremul;
skyostil@google.com0eb75762012-01-16 10:45:53 +0000315 bitmap.setIsVolatile(false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000316 if (bitmap.extractSubset(&subset, r)) {
317 REPORTER_ASSERT(reporter,
reed@google.com383a6972013-10-21 14:00:07 +0000318 subset.alphaType() == bitmap.alphaType());
bsalomon@google.com37688532012-10-12 13:42:36 +0000319 REPORTER_ASSERT(reporter,
skyostil@google.com0eb75762012-01-16 10:45:53 +0000320 subset.isVolatile() == false);
skyostil@google.comce7adb52012-01-13 14:56:51 +0000321 }
reed@android.com311c82d2009-05-05 23:13:23 +0000322 }
reed@android.com42263962009-05-01 04:00:01 +0000323 } else {
324 // dst should be unchanged from its initial state
325 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
326 REPORTER_ASSERT(reporter, dst.width() == 0);
327 REPORTER_ASSERT(reporter, dst.height() == 0);
328 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000329 } // for (size_t j = ...
330
331 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
332 // copyPixelsFrom().
333 //
334 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
335 ++copyCase) {
336 // Test copying to/from external buffer.
337 // Note: the tests below have hard-coded values ---
338 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000339
reed@google.com2cb14802013-06-26 14:35:02 +0000340 // Tests for getSafeSize64().
341 // Test with a very large configuration without pixel buffer
342 // attached.
343 SkBitmap tstSafeSize;
344 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
345 100000000U);
reed@google.com57212f92013-12-30 14:40:38 +0000346 int64_t safeSize = tstSafeSize.computeSafeSize64();
347 if (safeSize < 0) {
reed@google.com2cb14802013-06-26 14:35:02 +0000348 SkString str;
349 str.printf("getSafeSize64() negative: %s",
350 getSkConfigName(tstSafeSize));
351 reporter->reportFailed(str);
352 }
353 bool sizeFail = false;
354 // Compare against hand-computed values.
355 switch (gPairs[i].fConfig) {
356 case SkBitmap::kNo_Config:
357 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000358
reed@google.com2cb14802013-06-26 14:35:02 +0000359 case SkBitmap::kA8_Config:
360 case SkBitmap::kIndex8_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000361 if (safeSize != 0x2386F26FC10000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000362 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000363 }
reed@google.com2cb14802013-06-26 14:35:02 +0000364 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000365
reed@google.com2cb14802013-06-26 14:35:02 +0000366 case SkBitmap::kRGB_565_Config:
367 case SkBitmap::kARGB_4444_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000368 if (safeSize != 0x470DE4DF820000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000369 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000370 }
reed@google.com2cb14802013-06-26 14:35:02 +0000371 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000372
reed@google.com2cb14802013-06-26 14:35:02 +0000373 case SkBitmap::kARGB_8888_Config:
reed@google.com57212f92013-12-30 14:40:38 +0000374 if (safeSize != 0x8E1BC9BF040000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000375 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000376 }
reed@google.com2cb14802013-06-26 14:35:02 +0000377 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000378
reed@google.com2cb14802013-06-26 14:35:02 +0000379 default:
380 break;
381 }
382 if (sizeFail) {
383 SkString str;
reed@google.com57212f92013-12-30 14:40:38 +0000384 str.printf("computeSafeSize64() wrong size: %s",
reed@google.com2cb14802013-06-26 14:35:02 +0000385 getSkConfigName(tstSafeSize));
386 reporter->reportFailed(str);
387 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000388
rmistry@google.comd6bab022013-12-02 13:50:38 +0000389 int subW = 2;
390 int subH = 2;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000391
reed@google.com2cb14802013-06-26 14:35:02 +0000392 // Create bitmap to act as source for copies and subsets.
393 SkBitmap src, subset;
394 SkColorTable* ct = NULL;
395 if (isExtracted[copyCase]) { // A larger image to extract from.
396 src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
397 } else { // Tests expect a 2x2 bitmap, so make smaller.
398 src.setConfig(gPairs[i].fConfig, subW, subH);
399 }
400 if (SkBitmap::kIndex8_Config == src.config()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000401 ct = init_ctable(kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000402 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000403
reed@google.com2cb14802013-06-26 14:35:02 +0000404 src.allocPixels(ct);
405 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000406
reed@google.com2cb14802013-06-26 14:35:02 +0000407 // Either copy src or extract into 'subset', which is used
408 // for subsequent calls to copyPixelsTo/From.
409 bool srcReady = false;
410 if (isExtracted[copyCase]) {
411 // The extractedSubset() test case allows us to test copy-
412 // ing when src and dst mave possibly different strides.
413 SkIRect r;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000414 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000415
reed@google.com2cb14802013-06-26 14:35:02 +0000416 srcReady = src.extractSubset(&subset, r);
417 } else {
reed@google.com44699382013-10-31 17:28:30 +0000418 srcReady = src.copyTo(&subset, src.config());
reed@google.com2cb14802013-06-26 14:35:02 +0000419 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000420
reed@google.com2cb14802013-06-26 14:35:02 +0000421 // Not all configurations will generate a valid 'subset'.
422 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000423
reed@google.com2cb14802013-06-26 14:35:02 +0000424 // Allocate our target buffer 'buf' for all copies.
425 // To simplify verifying correctness of copies attach
426 // buf to a SkBitmap, but copies are done using the
427 // raw buffer pointer.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000428 const size_t bufSize = subH *
reed@google.com44699382013-10-31 17:28:30 +0000429 SkBitmap::ComputeRowBytes(src.config(), subW) * 2;
reed@google.com2cb14802013-06-26 14:35:02 +0000430 SkAutoMalloc autoBuf (bufSize);
431 uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000432
reed@google.com2cb14802013-06-26 14:35:02 +0000433 SkBitmap bufBm; // Attach buf to this bitmap.
434 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000435
reed@google.com2cb14802013-06-26 14:35:02 +0000436 // Set up values for each pixel being copied.
437 Coordinates coords(subW * subH);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000438 for (int x = 0; x < subW; ++x)
439 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000440 {
441 int index = y * subW + x;
442 SkASSERT(index < coords.length);
443 coords[index]->fX = x;
444 coords[index]->fY = y;
445 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000446
reed@google.com2cb14802013-06-26 14:35:02 +0000447 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000448
reed@google.com2cb14802013-06-26 14:35:02 +0000449 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000450
reed@google.com2cb14802013-06-26 14:35:02 +0000451 // Before/after comparisons easier if we attach buf
452 // to an appropriately configured SkBitmap.
453 memset(buf, 0xFF, bufSize);
454 // Config with stride greater than src but that fits in buf.
455 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
reed@google.com44699382013-10-31 17:28:30 +0000456 SkBitmap::ComputeRowBytes(subset.config(), subW) * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000457 bufBm.setPixels(buf);
458 successExpected = false;
459 // Then attempt to copy with a stride that is too large
460 // to fit in the buffer.
461 REPORTER_ASSERT(reporter,
462 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
463 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000464
reed@google.com2cb14802013-06-26 14:35:02 +0000465 if (successExpected)
466 reportCopyVerification(subset, bufBm, coords,
467 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000468 reporter);
469
reed@google.com2cb14802013-06-26 14:35:02 +0000470 // Test #2 ////////////////////////////////////////////
471 // This test should always succeed, but in the case
472 // of extracted bitmaps only because we handle the
473 // issue of getSafeSize(). Without getSafeSize()
474 // buffer overrun/read would occur.
475 memset(buf, 0xFF, bufSize);
476 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
477 subset.rowBytes());
478 bufBm.setPixels(buf);
479 successExpected = subset.getSafeSize() <= bufSize;
480 REPORTER_ASSERT(reporter,
481 subset.copyPixelsTo(buf, bufSize) ==
482 successExpected);
483 if (successExpected)
484 reportCopyVerification(subset, bufBm, coords,
485 "copyPixelsTo(buf, bufSize)", reporter);
486
487 // Test #3 ////////////////////////////////////////////
488 // Copy with different stride between src and dst.
489 memset(buf, 0xFF, bufSize);
490 bufBm.setConfig(gPairs[i].fConfig, subW, subH,
491 subset.rowBytes()+1);
492 bufBm.setPixels(buf);
493 successExpected = true; // Should always work.
494 REPORTER_ASSERT(reporter,
495 subset.copyPixelsTo(buf, bufSize,
496 subset.rowBytes()+1) == successExpected);
497 if (successExpected)
498 reportCopyVerification(subset, bufBm, coords,
499 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
500
501 // Test #4 ////////////////////////////////////////////
502 // Test copy with stride too small.
503 memset(buf, 0xFF, bufSize);
504 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
505 bufBm.setPixels(buf);
506 successExpected = false;
507 // Request copy with stride too small.
508 REPORTER_ASSERT(reporter,
509 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
510 == successExpected);
511 if (successExpected)
512 reportCopyVerification(subset, bufBm, coords,
513 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
514
515#if 0 // copyPixelsFrom is gone
516 // Test #5 ////////////////////////////////////////////
517 // Tests the case where the source stride is too small
518 // for the source configuration.
519 memset(buf, 0xFF, bufSize);
520 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
521 bufBm.setPixels(buf);
522 writeCoordPixels(bufBm, coords);
523 REPORTER_ASSERT(reporter,
524 subset.copyPixelsFrom(buf, bufSize, 1) == false);
525
526 // Test #6 ///////////////////////////////////////////
527 // Tests basic copy from an external buffer to the bitmap.
528 // If the bitmap is "extracted", this also tests the case
529 // where the source stride is different from the dest.
530 // stride.
531 // We've made the buffer large enough to always succeed.
532 bufBm.setConfig(gPairs[i].fConfig, subW, subH);
533 bufBm.setPixels(buf);
534 writeCoordPixels(bufBm, coords);
535 REPORTER_ASSERT(reporter,
536 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
537 true);
538 reportCopyVerification(bufBm, subset, coords,
539 "copyPixelsFrom(buf, bufSize)",
540 reporter);
541
542 // Test #7 ////////////////////////////////////////////
543 // Tests the case where the source buffer is too small
544 // for the transfer.
545 REPORTER_ASSERT(reporter,
546 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
547 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000548
reed@google.comab77aaf2011-11-01 16:03:35 +0000549#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000550 }
551 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000552 }
553}