Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: tests/BitmapCopyTest.cpp

Issue 171723007: add new copyTo version to SkBitmap, which takes SkColorType (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/utils/mac/SkCreateCGImageRef.cpp ('k') | tests/GpuBitmapCopyTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 #include "SkBitmap.h" 7 #include "SkBitmap.h"
8 #include "SkRect.h" 8 #include "SkRect.h"
9 #include "Test.h" 9 #include "Test.h"
10 10
11 static const char* boolStr(bool value) { 11 static const char* boolStr(bool value) {
12 return value ? "true" : "false"; 12 return value ? "true" : "false";
13 } 13 }
14 14
15 // these are in the same order as the SkBitmap::Config enum 15 // these are in the same order as the SkBitmap::Config enum
16 static const char* gConfigName[] = { 16 static const char* gColorTypeName[] = {
17 "None", "A8", "Index8", "565", "4444", "8888" 17 "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8"
18 }; 18 };
19 19
20 static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src, 20 static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
21 const SkBitmap& dst) { 21 const SkBitmap& dst) {
22 ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d", 22 ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d",
23 gConfigName[src.config()], src.isOpaque(), 23 gColorTypeName[src.colorType()], src.isOpaque(),
24 gConfigName[dst.config()], dst.isOpaque()); 24 gColorTypeName[dst.colorType()], dst.isOpaque());
25 } 25 }
26 26
27 static bool canHaveAlpha(SkBitmap::Config config) { 27 static bool canHaveAlpha(SkColorType ct) {
28 return config != SkBitmap::kRGB_565_Config; 28 return kRGB_565_SkColorType != ct;
29 } 29 }
30 30
31 // copyTo() should preserve isOpaque when it makes sense 31 // copyTo() should preserve isOpaque when it makes sense
32 static void test_isOpaque(skiatest::Reporter* reporter, 32 static void test_isOpaque(skiatest::Reporter* reporter,
33 const SkBitmap& srcOpaque, const SkBitmap& srcPremul, 33 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
34 SkBitmap::Config dstConfig) { 34 SkColorType dstColorType) {
35 SkBitmap dst; 35 SkBitmap dst;
36 36
37 if (canHaveAlpha(srcPremul.config()) && canHaveAlpha(dstConfig)) { 37 if (canHaveAlpha(srcPremul.colorType()) && canHaveAlpha(dstColorType)) {
38 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstConfig)); 38 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstColorType));
39 REPORTER_ASSERT(reporter, dst.config() == dstConfig); 39 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
40 if (srcPremul.isOpaque() != dst.isOpaque()) { 40 if (srcPremul.isOpaque() != dst.isOpaque()) {
41 report_opaqueness(reporter, srcPremul, dst); 41 report_opaqueness(reporter, srcPremul, dst);
42 } 42 }
43 } 43 }
44 44
45 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstConfig)); 45 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstColorType));
46 REPORTER_ASSERT(reporter, dst.config() == dstConfig); 46 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
47 if (srcOpaque.isOpaque() != dst.isOpaque()) { 47 if (srcOpaque.isOpaque() != dst.isOpaque()) {
48 report_opaqueness(reporter, srcOpaque, dst); 48 report_opaqueness(reporter, srcOpaque, dst);
49 } 49 }
50 } 50 }
51 51
52 static void init_src(const SkBitmap& bitmap) { 52 static void init_src(const SkBitmap& bitmap) {
53 SkAutoLockPixels lock(bitmap); 53 SkAutoLockPixels lock(bitmap);
54 if (bitmap.getPixels()) { 54 if (bitmap.getPixels()) {
55 if (bitmap.getColorTable()) { 55 if (bitmap.getColorTable()) {
56 sk_bzero(bitmap.getPixels(), bitmap.getSize()); 56 sk_bzero(bitmap.getPixels(), bitmap.getSize());
57 } else { 57 } else {
58 bitmap.eraseColor(SK_ColorWHITE); 58 bitmap.eraseColor(SK_ColorWHITE);
59 } 59 }
60 } 60 }
61 } 61 }
62 62
63 static SkColorTable* init_ctable(SkAlphaType alphaType) { 63 static SkColorTable* init_ctable(SkAlphaType alphaType) {
64 static const SkColor colors[] = { 64 static const SkColor colors[] = {
65 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE 65 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
66 }; 66 };
67 return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType); 67 return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType);
68 } 68 }
69 69
70 struct Pair { 70 struct Pair {
71 SkBitmap::Config fConfig; 71 SkColorType fColorType;
72 const char* fValid; 72 const char* fValid;
73 }; 73 };
74 74
75 // Utility functions for copyPixelsTo()/copyPixelsFrom() tests. 75 // Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
76 // getPixel() 76 // getPixel()
77 // setPixel() 77 // setPixel()
78 // getSkConfigName() 78 // getSkConfigName()
79 // struct Coordinates 79 // struct Coordinates
80 // reportCopyVerification() 80 // reportCopyVerification()
81 // writeCoordPixels() 81 // writeCoordPixels()
82 82
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 } 192 }
193 } 193 }
194 194
195 // Writes unique pixel values at locations specified by coords. 195 // Writes unique pixel values at locations specified by coords.
196 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { 196 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
197 for (int i = 0; i < coords.length; ++i) 197 for (int i = 0; i < coords.length; ++i)
198 setPixel(coords[i]->fX, coords[i]->fY, i, bm); 198 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
199 } 199 }
200 200
201 static const Pair gPairs[] = { 201 static const Pair gPairs[] = {
202 { SkBitmap::kNo_Config, "0000000" }, 202 { kUnknown_SkColorType, "000000" },
203 { SkBitmap::kA8_Config, "0101010" }, 203 { kAlpha_8_SkColorType, "010101" },
204 { SkBitmap::kIndex8_Config, "0111010" }, 204 { kIndex_8_SkColorType, "011101" },
205 { SkBitmap::kRGB_565_Config, "0101010" }, 205 { kRGB_565_SkColorType, "010101" },
206 { SkBitmap::kARGB_4444_Config, "0101110" }, 206 { kARGB_4444_SkColorType, "010111" },
207 { SkBitmap::kARGB_8888_Config, "0101110" }, 207 { kPMColor_SkColorType, "010111" },
208 }; 208 };
209 209
210 static const int W = 20; 210 static const int W = 20;
211 static const int H = 33; 211 static const int H = 33;
212 212
213 static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul, 213 static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
214 SkBitmap::Config config) { 214 SkColorType ct) {
215 SkColorTable* ctOpaque = NULL; 215 SkColorTable* ctOpaque = NULL;
216 SkColorTable* ctPremul = NULL; 216 SkColorTable* ctPremul = NULL;
217 217 if (kIndex_8_SkColorType == ct) {
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); 218 ctOpaque = init_ctable(kOpaque_SkAlphaType);
222 ctPremul = init_ctable(kPremul_SkAlphaType); 219 ctPremul = init_ctable(kPremul_SkAlphaType);
223 } 220 }
224 srcOpaque->allocPixels(ctOpaque); 221
225 srcPremul->allocPixels(ctPremul); 222 srcOpaque->allocPixels(SkImageInfo::Make(W, H, ct, kOpaque_SkAlphaType),
223 NULL, ctOpaque);
224 srcPremul->allocPixels(SkImageInfo::Make(W, H, ct, kPremul_SkAlphaType),
225 NULL, ctPremul);
226 SkSafeUnref(ctOpaque); 226 SkSafeUnref(ctOpaque);
227 SkSafeUnref(ctPremul); 227 SkSafeUnref(ctPremul);
228 init_src(*srcOpaque); 228 init_src(*srcOpaque);
229 init_src(*srcPremul); 229 init_src(*srcPremul);
230 } 230 }
231 231
232 DEF_TEST(BitmapCopy_extractSubset, reporter) { 232 DEF_TEST(BitmapCopy_extractSubset, reporter) {
233 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { 233 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
234 SkBitmap srcOpaque, srcPremul; 234 SkBitmap srcOpaque, srcPremul;
235 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig); 235 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
236 236
237 SkBitmap bitmap(srcOpaque); 237 SkBitmap bitmap(srcOpaque);
238 SkBitmap subset; 238 SkBitmap subset;
239 SkIRect r; 239 SkIRect r;
240 // Extract a subset which has the same width as the original. This 240 // Extract a subset which has the same width as the original. This
241 // catches a bug where we cloned the genID incorrectly. 241 // catches a bug where we cloned the genID incorrectly.
242 r.set(0, 1, W, 3); 242 r.set(0, 1, W, 3);
243 bitmap.setIsVolatile(true); 243 bitmap.setIsVolatile(true);
244 if (bitmap.extractSubset(&subset, r)) { 244 // Relies on old behavior of extractSubset failing if colortype is unkno wn
245 if (kUnknown_SkColorType != bitmap.colorType() && bitmap.extractSubset(& subset, r)) {
245 REPORTER_ASSERT(reporter, subset.width() == W); 246 REPORTER_ASSERT(reporter, subset.width() == W);
246 REPORTER_ASSERT(reporter, subset.height() == 2); 247 REPORTER_ASSERT(reporter, subset.height() == 2);
247 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); 248 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
248 REPORTER_ASSERT(reporter, subset.isVolatile() == true); 249 REPORTER_ASSERT(reporter, subset.isVolatile() == true);
249 250
250 // Test copying an extracted subset. 251 // Test copying an extracted subset.
251 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { 252 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
252 SkBitmap copy; 253 SkBitmap copy;
253 bool success = subset.copyTo(&copy, gPairs[j].fConfig); 254 bool success = subset.copyTo(&copy, gPairs[j].fColorType);
254 if (!success) { 255 if (!success) {
255 // Skip checking that success matches fValid, which is redun dant 256 // Skip checking that success matches fValid, which is redun dant
256 // with the code below. 257 // with the code below.
257 REPORTER_ASSERT(reporter, gPairs[i].fConfig != gPairs[j].fCo nfig); 258 REPORTER_ASSERT(reporter, gPairs[i].fColorType != gPairs[j]. fColorType);
258 continue; 259 continue;
259 } 260 }
260 261
261 // When performing a copy of an extracted subset, the gen id sho uld 262 // When performing a copy of an extracted subset, the gen id sho uld
262 // change. 263 // change.
263 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGe nerationID()); 264 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGe nerationID());
264 265
265 REPORTER_ASSERT(reporter, copy.width() == W); 266 REPORTER_ASSERT(reporter, copy.width() == W);
266 REPORTER_ASSERT(reporter, copy.height() == 2); 267 REPORTER_ASSERT(reporter, copy.height() == 2);
267 268
268 if (gPairs[i].fConfig == gPairs[j].fConfig) { 269 if (gPairs[i].fColorType == gPairs[j].fColorType) {
269 SkAutoLockPixels alp0(subset); 270 SkAutoLockPixels alp0(subset);
270 SkAutoLockPixels alp1(copy); 271 SkAutoLockPixels alp1(copy);
271 // they should both have, or both not-have, a colortable 272 // they should both have, or both not-have, a colortable
272 bool hasCT = subset.getColorTable() != NULL; 273 bool hasCT = subset.getColorTable() != NULL;
273 REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT); 274 REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT);
274 } 275 }
275 } 276 }
276 } 277 }
277 278
278 bitmap = srcPremul; 279 bitmap = srcPremul;
279 bitmap.setIsVolatile(false); 280 bitmap.setIsVolatile(false);
280 if (bitmap.extractSubset(&subset, r)) { 281 if (bitmap.extractSubset(&subset, r)) {
281 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); 282 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
282 REPORTER_ASSERT(reporter, subset.isVolatile() == false); 283 REPORTER_ASSERT(reporter, subset.isVolatile() == false);
283 } 284 }
284 } 285 }
285 } 286 }
286 287
287 DEF_TEST(BitmapCopy, reporter) { 288 DEF_TEST(BitmapCopy, reporter) {
288 static const bool isExtracted[] = { 289 static const bool isExtracted[] = {
289 false, true 290 false, true
290 }; 291 };
291 292
292 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { 293 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
293 SkBitmap srcOpaque, srcPremul; 294 SkBitmap srcOpaque, srcPremul;
294 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig); 295 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
295 296
296 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { 297 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
297 SkBitmap dst; 298 SkBitmap dst;
298 299
299 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig); 300 bool success = srcPremul.copyTo(&dst, gPairs[j].fColorType);
300 bool expected = gPairs[i].fValid[j] != '0'; 301 bool expected = gPairs[i].fValid[j] != '0';
301 if (success != expected) { 302 if (success != expected) {
302 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s " 303 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
303 "returned %s", gConfigName[i], gConfigName[j], 304 "returned %s", gColorTypeName[i], gColorTypeName[j],
304 boolStr(expected), boolStr(success)); 305 boolStr(expected), boolStr(success));
305 } 306 }
306 307
307 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig); 308 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fColorType);
308 if (success != canSucceed) { 309 if (success != canSucceed) {
309 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s " 310 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s "
310 "canCopyTo %s", gConfigName[i], gConfigName[j], 311 "canCopyTo %s", gColorTypeName[i], gColorTypeName[j],
311 boolStr(success), boolStr(canSucceed)); 312 boolStr(success), boolStr(canSucceed));
312 } 313 }
313 314
314 if (success) { 315 if (success) {
315 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width()); 316 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
316 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height()); 317 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
317 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig); 318 REPORTER_ASSERT(reporter, dst.colorType() == gPairs[j].fColorTyp e);
318 test_isOpaque(reporter, srcOpaque, srcPremul, dst.config()); 319 test_isOpaque(reporter, srcOpaque, srcPremul, dst.colorType());
319 if (srcPremul.config() == dst.config()) { 320 if (srcPremul.config() == dst.config()) {
320 SkAutoLockPixels srcLock(srcPremul); 321 SkAutoLockPixels srcLock(srcPremul);
321 SkAutoLockPixels dstLock(dst); 322 SkAutoLockPixels dstLock(dst);
322 REPORTER_ASSERT(reporter, srcPremul.readyToDraw()); 323 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
323 REPORTER_ASSERT(reporter, dst.readyToDraw()); 324 REPORTER_ASSERT(reporter, dst.readyToDraw());
324 const char* srcP = (const char*)srcPremul.getAddr(0, 0); 325 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
325 const char* dstP = (const char*)dst.getAddr(0, 0); 326 const char* dstP = (const char*)dst.getAddr(0, 0);
326 REPORTER_ASSERT(reporter, srcP != dstP); 327 REPORTER_ASSERT(reporter, srcP != dstP);
327 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, 328 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
328 srcPremul.getSize())); 329 srcPremul.getSize()));
(...skipping 15 matching lines...) Expand all
344 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted); 345 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
345 ++copyCase) { 346 ++copyCase) {
346 // Test copying to/from external buffer. 347 // Test copying to/from external buffer.
347 // Note: the tests below have hard-coded values --- 348 // Note: the tests below have hard-coded values ---
348 // Please take care if modifying. 349 // Please take care if modifying.
349 350
350 // Tests for getSafeSize64(). 351 // Tests for getSafeSize64().
351 // Test with a very large configuration without pixel buffer 352 // Test with a very large configuration without pixel buffer
352 // attached. 353 // attached.
353 SkBitmap tstSafeSize; 354 SkBitmap tstSafeSize;
354 tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U, 355 tstSafeSize.setConfig(SkImageInfo::Make(100000000U, 100000000U,
355 100000000U); 356 gPairs[i].fColorType,
357 kPremul_SkAlphaType));
356 int64_t safeSize = tstSafeSize.computeSafeSize64(); 358 int64_t safeSize = tstSafeSize.computeSafeSize64();
357 if (safeSize < 0) { 359 if (safeSize < 0) {
358 ERRORF(reporter, "getSafeSize64() negative: %s", 360 ERRORF(reporter, "getSafeSize64() negative: %s",
359 getSkConfigName(tstSafeSize)); 361 getSkConfigName(tstSafeSize));
360 } 362 }
361 bool sizeFail = false; 363 bool sizeFail = false;
362 // Compare against hand-computed values. 364 // Compare against hand-computed values.
363 switch (gPairs[i].fConfig) { 365 switch (gPairs[i].fColorType) {
364 case SkBitmap::kNo_Config: 366 case kUnknown_SkColorType:
365 break; 367 break;
366 368
367 case SkBitmap::kA8_Config: 369 case kAlpha_8_SkColorType:
368 case SkBitmap::kIndex8_Config: 370 case kIndex_8_SkColorType:
369 if (safeSize != 0x2386F26FC10000LL) { 371 if (safeSize != 0x2386F26FC10000LL) {
370 sizeFail = true; 372 sizeFail = true;
371 } 373 }
372 break; 374 break;
373 375
374 case SkBitmap::kRGB_565_Config: 376 case kRGB_565_SkColorType:
375 case SkBitmap::kARGB_4444_Config: 377 case kARGB_4444_SkColorType:
376 if (safeSize != 0x470DE4DF820000LL) { 378 if (safeSize != 0x470DE4DF820000LL) {
377 sizeFail = true; 379 sizeFail = true;
378 } 380 }
379 break; 381 break;
380 382
381 case SkBitmap::kARGB_8888_Config: 383 case kPMColor_SkColorType:
382 if (safeSize != 0x8E1BC9BF040000LL) { 384 if (safeSize != 0x8E1BC9BF040000LL) {
383 sizeFail = true; 385 sizeFail = true;
384 } 386 }
385 break; 387 break;
386 388
387 default: 389 default:
388 break; 390 break;
389 } 391 }
390 if (sizeFail) { 392 if (sizeFail) {
391 ERRORF(reporter, "computeSafeSize64() wrong size: %s", 393 ERRORF(reporter, "computeSafeSize64() wrong size: %s",
392 getSkConfigName(tstSafeSize)); 394 getSkConfigName(tstSafeSize));
393 } 395 }
394 396
395 int subW = 2; 397 int subW = 2;
396 int subH = 2; 398 int subH = 2;
397 399
398 // Create bitmap to act as source for copies and subsets. 400 // Create bitmap to act as source for copies and subsets.
399 SkBitmap src, subset; 401 SkBitmap src, subset;
400 SkColorTable* ct = NULL; 402 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()) { 403 if (SkBitmap::kIndex8_Config == src.config()) {
407 ct = init_ctable(kPremul_SkAlphaType); 404 ct = init_ctable(kPremul_SkAlphaType);
408 } 405 }
409 406
410 src.allocPixels(ct); 407 if (isExtracted[copyCase]) { // A larger image to extract from.
408 src.allocPixels(SkImageInfo::Make(2 * subW + 1, subH,
409 gPairs[i].fColorType,
410 kPremul_SkAlphaType));
411 } else { // Tests expect a 2x2 bitmap, so make smaller.
412 src.allocPixels(SkImageInfo::Make(subW, subH,
413 gPairs[i].fColorType,
414 kPremul_SkAlphaType));
415 }
411 SkSafeUnref(ct); 416 SkSafeUnref(ct);
412 417
413 // Either copy src or extract into 'subset', which is used 418 // Either copy src or extract into 'subset', which is used
414 // for subsequent calls to copyPixelsTo/From. 419 // for subsequent calls to copyPixelsTo/From.
415 bool srcReady = false; 420 bool srcReady = false;
416 if (isExtracted[copyCase]) { 421 // Test relies on older behavior that extractSubset will fail on
422 // no_config
423 if (kUnknown_SkColorType != src.colorType() &&
424 isExtracted[copyCase]) {
417 // The extractedSubset() test case allows us to test copy- 425 // The extractedSubset() test case allows us to test copy-
418 // ing when src and dst mave possibly different strides. 426 // ing when src and dst mave possibly different strides.
419 SkIRect r; 427 SkIRect r;
420 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap 428 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
421 429
422 srcReady = src.extractSubset(&subset, r); 430 srcReady = src.extractSubset(&subset, r);
423 } else { 431 } else {
424 srcReady = src.copyTo(&subset, src.config()); 432 srcReady = src.copyTo(&subset);
425 } 433 }
426 434
427 // Not all configurations will generate a valid 'subset'. 435 // Not all configurations will generate a valid 'subset'.
428 if (srcReady) { 436 if (srcReady) {
429 437
430 // Allocate our target buffer 'buf' for all copies. 438 // Allocate our target buffer 'buf' for all copies.
431 // To simplify verifying correctness of copies attach 439 // To simplify verifying correctness of copies attach
432 // buf to a SkBitmap, but copies are done using the 440 // buf to a SkBitmap, but copies are done using the
433 // raw buffer pointer. 441 // raw buffer pointer.
434 const size_t bufSize = subH * 442 const size_t bufSize = subH *
(...skipping 12 matching lines...) Expand all
447 int index = y * subW + x; 455 int index = y * subW + x;
448 SkASSERT(index < coords.length); 456 SkASSERT(index < coords.length);
449 coords[index]->fX = x; 457 coords[index]->fX = x;
450 coords[index]->fY = y; 458 coords[index]->fY = y;
451 } 459 }
452 460
453 writeCoordPixels(subset, coords); 461 writeCoordPixels(subset, coords);
454 462
455 // Test #1 //////////////////////////////////////////// 463 // Test #1 ////////////////////////////////////////////
456 464
465 const SkImageInfo info = SkImageInfo::Make(subW, subH,
466 gPairs[i].fColorType,
467 kPremul_SkAlphaType);
457 // Before/after comparisons easier if we attach buf 468 // Before/after comparisons easier if we attach buf
458 // to an appropriately configured SkBitmap. 469 // to an appropriately configured SkBitmap.
459 memset(buf, 0xFF, bufSize); 470 memset(buf, 0xFF, bufSize);
460 // Config with stride greater than src but that fits in buf. 471 // Config with stride greater than src but that fits in buf.
461 bufBm.setConfig(gPairs[i].fConfig, subW, subH, 472 bufBm.installPixels(info, buf, info.minRowBytes() * 2);
462 SkBitmap::ComputeRowBytes(subset.config(), subW) * 2);
463 bufBm.setPixels(buf);
464 successExpected = false; 473 successExpected = false;
465 // Then attempt to copy with a stride that is too large 474 // Then attempt to copy with a stride that is too large
466 // to fit in the buffer. 475 // to fit in the buffer.
467 REPORTER_ASSERT(reporter, 476 REPORTER_ASSERT(reporter,
468 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3) 477 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
469 == successExpected); 478 == successExpected);
470 479
471 if (successExpected) 480 if (successExpected)
472 reportCopyVerification(subset, bufBm, coords, 481 reportCopyVerification(subset, bufBm, coords,
473 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)", 482 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
474 reporter); 483 reporter);
475 484
476 // Test #2 //////////////////////////////////////////// 485 // Test #2 ////////////////////////////////////////////
477 // This test should always succeed, but in the case 486 // This test should always succeed, but in the case
478 // of extracted bitmaps only because we handle the 487 // of extracted bitmaps only because we handle the
479 // issue of getSafeSize(). Without getSafeSize() 488 // issue of getSafeSize(). Without getSafeSize()
480 // buffer overrun/read would occur. 489 // buffer overrun/read would occur.
481 memset(buf, 0xFF, bufSize); 490 memset(buf, 0xFF, bufSize);
482 bufBm.setConfig(gPairs[i].fConfig, subW, subH, 491 bufBm.installPixels(info, buf, subset.rowBytes());
483 subset.rowBytes());
484 bufBm.setPixels(buf);
485 successExpected = subset.getSafeSize() <= bufSize; 492 successExpected = subset.getSafeSize() <= bufSize;
486 REPORTER_ASSERT(reporter, 493 REPORTER_ASSERT(reporter,
487 subset.copyPixelsTo(buf, bufSize) == 494 subset.copyPixelsTo(buf, bufSize) ==
488 successExpected); 495 successExpected);
489 if (successExpected) 496 if (successExpected)
490 reportCopyVerification(subset, bufBm, coords, 497 reportCopyVerification(subset, bufBm, coords,
491 "copyPixelsTo(buf, bufSize)", reporter); 498 "copyPixelsTo(buf, bufSize)", reporter);
492 499
493 // Test #3 //////////////////////////////////////////// 500 // Test #3 ////////////////////////////////////////////
494 // Copy with different stride between src and dst. 501 // Copy with different stride between src and dst.
495 memset(buf, 0xFF, bufSize); 502 memset(buf, 0xFF, bufSize);
496 bufBm.setConfig(gPairs[i].fConfig, subW, subH, 503 bufBm.installPixels(info, buf, subset.rowBytes()+1);
497 subset.rowBytes()+1);
498 bufBm.setPixels(buf);
499 successExpected = true; // Should always work. 504 successExpected = true; // Should always work.
500 REPORTER_ASSERT(reporter, 505 REPORTER_ASSERT(reporter,
501 subset.copyPixelsTo(buf, bufSize, 506 subset.copyPixelsTo(buf, bufSize,
502 subset.rowBytes()+1) == successExpected); 507 subset.rowBytes()+1) == successExpected);
503 if (successExpected) 508 if (successExpected)
504 reportCopyVerification(subset, bufBm, coords, 509 reportCopyVerification(subset, bufBm, coords,
505 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter); 510 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
506 511
507 // Test #4 //////////////////////////////////////////// 512 // Test #4 ////////////////////////////////////////////
508 // Test copy with stride too small. 513 // Test copy with stride too small.
509 memset(buf, 0xFF, bufSize); 514 memset(buf, 0xFF, bufSize);
510 bufBm.setConfig(gPairs[i].fConfig, subW, subH); 515 bufBm.installPixels(info, buf, info.minRowBytes());
511 bufBm.setPixels(buf);
512 successExpected = false; 516 successExpected = false;
513 // Request copy with stride too small. 517 // Request copy with stride too small.
514 REPORTER_ASSERT(reporter, 518 REPORTER_ASSERT(reporter,
515 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1) 519 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
516 == successExpected); 520 == successExpected);
517 if (successExpected) 521 if (successExpected)
518 reportCopyVerification(subset, bufBm, coords, 522 reportCopyVerification(subset, bufBm, coords,
519 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter); 523 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
520 524
521 #if 0 // copyPixelsFrom is gone 525 #if 0 // copyPixelsFrom is gone
522 // Test #5 //////////////////////////////////////////// 526 // Test #5 ////////////////////////////////////////////
523 // Tests the case where the source stride is too small 527 // Tests the case where the source stride is too small
524 // for the source configuration. 528 // for the source configuration.
525 memset(buf, 0xFF, bufSize); 529 memset(buf, 0xFF, bufSize);
526 bufBm.setConfig(gPairs[i].fConfig, subW, subH); 530 bufBm.installPixels(info, buf, info.minRowBytes());
527 bufBm.setPixels(buf);
528 writeCoordPixels(bufBm, coords); 531 writeCoordPixels(bufBm, coords);
529 REPORTER_ASSERT(reporter, 532 REPORTER_ASSERT(reporter,
530 subset.copyPixelsFrom(buf, bufSize, 1) == false); 533 subset.copyPixelsFrom(buf, bufSize, 1) == false);
531 534
532 // Test #6 /////////////////////////////////////////// 535 // Test #6 ///////////////////////////////////////////
533 // Tests basic copy from an external buffer to the bitmap. 536 // Tests basic copy from an external buffer to the bitmap.
534 // If the bitmap is "extracted", this also tests the case 537 // If the bitmap is "extracted", this also tests the case
535 // where the source stride is different from the dest. 538 // where the source stride is different from the dest.
536 // stride. 539 // stride.
537 // We've made the buffer large enough to always succeed. 540 // We've made the buffer large enough to always succeed.
538 bufBm.setConfig(gPairs[i].fConfig, subW, subH); 541 bufBm.installPixels(info, buf, info.minRowBytes());
539 bufBm.setPixels(buf);
540 writeCoordPixels(bufBm, coords); 542 writeCoordPixels(bufBm, coords);
541 REPORTER_ASSERT(reporter, 543 REPORTER_ASSERT(reporter,
542 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) == 544 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
543 true); 545 true);
544 reportCopyVerification(bufBm, subset, coords, 546 reportCopyVerification(bufBm, subset, coords,
545 "copyPixelsFrom(buf, bufSize)", 547 "copyPixelsFrom(buf, bufSize)",
546 reporter); 548 reporter);
547 549
548 // Test #7 //////////////////////////////////////////// 550 // Test #7 ////////////////////////////////////////////
549 // Tests the case where the source buffer is too small 551 // Tests the case where the source buffer is too small
550 // for the transfer. 552 // for the transfer.
551 REPORTER_ASSERT(reporter, 553 REPORTER_ASSERT(reporter,
552 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == 554 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
553 false); 555 false);
554 556
555 #endif 557 #endif
556 } 558 }
557 } // for (size_t copyCase ... 559 } // for (size_t copyCase ...
558 } 560 }
559 } 561 }
OLDNEW
« no previous file with comments | « src/utils/mac/SkCreateCGImageRef.cpp ('k') | tests/GpuBitmapCopyTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698