| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 | 7 |
| 8 #include "Resources.h" | 8 #include "Resources.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkCodec.h" | 10 #include "SkCodec.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 otherAt = kUnpremul_SkAlphaType; | 119 otherAt = kUnpremul_SkAlphaType; |
| 120 } else { | 120 } else { |
| 121 otherAt = kPremul_SkAlphaType; | 121 otherAt = kPremul_SkAlphaType; |
| 122 } | 122 } |
| 123 // The other non-opaque alpha type should always succeed, but not ma
tch. | 123 // The other non-opaque alpha type should always succeed, but not ma
tch. |
| 124 test_info(r, codec, info.makeAlphaType(otherAt), SkCodec::kSuccess,
nullptr); | 124 test_info(r, codec, info.makeAlphaType(otherAt), SkCodec::kSuccess,
nullptr); |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 // FIXME: SkScaledCodec is currently only supported for types used by BRD |
| 130 // skbug.com/4428 |
| 131 static bool supports_scaled_codec(const char path[]) { |
| 132 static const char* const exts[] = { |
| 133 "jpg", "jpeg", "png", "webp" |
| 134 "JPG", "JPEG", "PNG", "WEBP" |
| 135 }; |
| 136 |
| 137 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) { |
| 138 if (SkStrEndsWith(path, exts[i])) { |
| 139 return true; |
| 140 } |
| 141 } |
| 142 return false; |
| 143 } |
| 144 |
| 129 static void check(skiatest::Reporter* r, | 145 static void check(skiatest::Reporter* r, |
| 130 const char path[], | 146 const char path[], |
| 131 SkISize size, | 147 SkISize size, |
| 132 bool supportsScanlineDecoding, | 148 bool supportsScanlineDecoding, |
| 133 bool supportsSubsetDecoding, | 149 bool supportsSubsetDecoding, |
| 134 bool supports565 = true) { | 150 bool supports565 = true) { |
| 135 | 151 |
| 136 SkAutoTDelete<SkStream> stream(resource(path)); | 152 SkAutoTDelete<SkStream> stream(resource(path)); |
| 137 if (!stream) { | 153 if (!stream) { |
| 138 SkDebugf("Missing resource '%s'\n", path); | 154 SkDebugf("Missing resource '%s'\n", path); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 // Webp is the only codec that supports subsets, and it will have mo
dified the subset | 236 // Webp is the only codec that supports subsets, and it will have mo
dified the subset |
| 221 // to have even left/top. | 237 // to have even left/top. |
| 222 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTo
p)); | 238 REPORTER_ASSERT(r, SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTo
p)); |
| 223 } else { | 239 } else { |
| 224 // No subsets will work. | 240 // No subsets will work. |
| 225 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented); | 241 REPORTER_ASSERT(r, result == SkCodec::kUnimplemented); |
| 226 } | 242 } |
| 227 } | 243 } |
| 228 | 244 |
| 229 // SkScaledCodec tests | 245 // SkScaledCodec tests |
| 230 if (supportsScanlineDecoding || supportsSubsetDecoding){ | 246 if ((supportsScanlineDecoding || supportsSubsetDecoding) && supports_scaled_
codec(path)) { |
| 247 |
| 231 SkAutoTDelete<SkStream> stream(resource(path)); | 248 SkAutoTDelete<SkStream> stream(resource(path)); |
| 232 if (!stream) { | 249 if (!stream) { |
| 233 SkDebugf("Missing resource '%s'\n", path); | 250 SkDebugf("Missing resource '%s'\n", path); |
| 234 return; | 251 return; |
| 235 } | 252 } |
| 236 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach(
))); | 253 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach(
))); |
| 237 if (!codec) { | 254 if (!codec) { |
| 238 ERRORF(r, "Unable to decode '%s'", path); | 255 ERRORF(r, "Unable to decode '%s'", path); |
| 239 return; | 256 return; |
| 240 } | 257 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 | 395 |
| 379 result = codec->getScanlines(bm.getAddr(0, height - remainingLines), | 396 result = codec->getScanlines(bm.getAddr(0, height - remainingLines), |
| 380 remainingLines, bm.rowBytes()); | 397 remainingLines, bm.rowBytes()); |
| 381 REPORTER_ASSERT(r, result == SkCodec::kSuccess); | 398 REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 382 } | 399 } |
| 383 | 400 |
| 384 compare_to_good_digest(r, digest, bm); | 401 compare_to_good_digest(r, digest, bm); |
| 385 } | 402 } |
| 386 | 403 |
| 387 static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_
t len) { | 404 static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_
t len) { |
| 405 // Neither of these calls should return a codec. Bots should catch us if we
leaked anything. |
| 388 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, fals
e)); | 406 SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, fals
e)); |
| 389 // We should not have gotten a codec. Bots should catch us if we leaked anyt
hing. | 407 REPORTER_ASSERT(r, !codec); |
| 408 |
| 409 codec = SkScaledCodec::NewFromStream(new SkMemoryStream(stream, len, false))
; |
| 390 REPORTER_ASSERT(r, !codec); | 410 REPORTER_ASSERT(r, !codec); |
| 391 } | 411 } |
| 392 | 412 |
| 393 // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, | 413 // Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream, |
| 394 // even on failure. Test some bad streams. | 414 // even on failure. Test some bad streams. |
| 395 DEF_TEST(Codec_leaks, r) { | 415 DEF_TEST(Codec_leaks, r) { |
| 396 // No codec should claim this as their format, so this tests SkCodec::NewFro
mStream. | 416 // No codec should claim this as their format, so this tests SkCodec::NewFro
mStream. |
| 397 const char nonSupportedStream[] = "hello world"; | 417 const char nonSupportedStream[] = "hello world"; |
| 398 // The other strings should look like the beginning of a file type, so we'll
call some | 418 // The other strings should look like the beginning of a file type, so we'll
call some |
| 399 // internal version of NewFromStream, which must also delete the stream on f
ailure. | 419 // internal version of NewFromStream, which must also delete the stream on f
ailure. |
| 400 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a,
0x0a }; | 420 const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a,
0x0a }; |
| 401 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; | 421 const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF }; |
| 402 const char emptyWebp[] = "RIFF1234WEBPVP"; | 422 const char emptyWebp[] = "RIFF1234WEBPVP"; |
| 403 const char emptyBmp[] = { 'B', 'M' }; | 423 const char emptyBmp[] = { 'B', 'M' }; |
| 404 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; | 424 const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 405 const char emptyGif[] = "GIFVER"; | 425 const char emptyGif[] = "GIFVER"; |
| 406 | 426 |
| 407 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); | 427 test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream)); |
| 408 test_invalid_stream(r, emptyPng, sizeof(emptyPng)); | 428 test_invalid_stream(r, emptyPng, sizeof(emptyPng)); |
| 409 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); | 429 test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg)); |
| 410 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); | 430 test_invalid_stream(r, emptyWebp, sizeof(emptyWebp)); |
| 411 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); | 431 test_invalid_stream(r, emptyBmp, sizeof(emptyBmp)); |
| 412 test_invalid_stream(r, emptyIco, sizeof(emptyIco)); | 432 test_invalid_stream(r, emptyIco, sizeof(emptyIco)); |
| 413 test_invalid_stream(r, emptyGif, sizeof(emptyGif)); | 433 test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 414 } | 434 } |
| 415 | 435 |
| 436 DEF_TEST(Codec_null, r) { |
| 437 // Attempting to create an SkCodec or an SkScaledCodec with null should not |
| 438 // crash. |
| 439 SkCodec* codec = SkCodec::NewFromStream(nullptr); |
| 440 REPORTER_ASSERT(r, !codec); |
| 441 |
| 442 codec = SkScaledCodec::NewFromStream(nullptr); |
| 443 REPORTER_ASSERT(r, !codec); |
| 444 } |
| 445 |
| 416 static void test_dimensions(skiatest::Reporter* r, const char path[]) { | 446 static void test_dimensions(skiatest::Reporter* r, const char path[]) { |
| 417 // Create the codec from the resource file | 447 // Create the codec from the resource file |
| 418 SkAutoTDelete<SkStream> stream(resource(path)); | 448 SkAutoTDelete<SkStream> stream(resource(path)); |
| 419 if (!stream) { | 449 if (!stream) { |
| 420 SkDebugf("Missing resource '%s'\n", path); | 450 SkDebugf("Missing resource '%s'\n", path); |
| 421 return; | 451 return; |
| 422 } | 452 } |
| 423 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach())); | 453 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach())); |
| 424 if (!codec) { | 454 if (!codec) { |
| 425 ERRORF(r, "Unable to create codec '%s'", path); | 455 ERRORF(r, "Unable to create codec '%s'", path); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); | 552 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 523 result = decoder->startScanlineDecode( | 553 result = decoder->startScanlineDecode( |
| 524 decoder->getInfo().makeColorType(kIndex_8_SkColorType)); | 554 decoder->getInfo().makeColorType(kIndex_8_SkColorType)); |
| 525 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); | 555 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 526 } | 556 } |
| 527 | 557 |
| 528 DEF_TEST(Codec_Params, r) { | 558 DEF_TEST(Codec_Params, r) { |
| 529 test_invalid_parameters(r, "index8.png"); | 559 test_invalid_parameters(r, "index8.png"); |
| 530 test_invalid_parameters(r, "mandrill.wbmp"); | 560 test_invalid_parameters(r, "mandrill.wbmp"); |
| 531 } | 561 } |
| OLD | NEW |