OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 306 matching lines...) Loading... |
317 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); | 317 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1); |
318 { | 318 { |
319 SkAutoLockPixels alp(bm); | 319 SkAutoLockPixels alp(bm); |
320 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7f7f7f7f); | 320 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7f7f7f7f); |
321 } | 321 } |
322 } | 322 } |
323 } | 323 } |
324 #endif // SK_BUILD_FOR_UNIX/ANDROID skbug.com/2388 | 324 #endif // SK_BUILD_FOR_UNIX/ANDROID skbug.com/2388 |
325 | 325 |
326 #ifdef SK_DEBUG | 326 #ifdef SK_DEBUG |
327 // Create a stream containing a bitmap encoded to Type type. | |
328 static SkMemoryStream* create_image_stream(SkImageEncoder::Type type) { | |
329 SkBitmap bm; | |
330 const int size = 50; | |
331 bm.allocN32Pixels(size, size); | |
332 SkCanvas canvas(bm); | |
333 SkPoint points[2] = { | |
334 { SkIntToScalar(0), SkIntToScalar(0) }, | |
335 { SkIntToScalar(size), SkIntToScalar(size) } | |
336 }; | |
337 SkColor colors[2] = { SK_ColorWHITE, SK_ColorBLUE }; | |
338 SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL, | |
339 SK_ARRAY_COUNT(colors), | |
340 SkShader::kClamp_TileMode)
; | |
341 SkPaint paint; | |
342 paint.setShader(shader)->unref(); | |
343 canvas.drawPaint(paint); | |
344 // Now encode it to a stream. | |
345 SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(bm, type, 100)); | |
346 if (NULL == data.get()) { | |
347 return NULL; | |
348 } | |
349 return SkNEW_ARGS(SkMemoryStream, (data.get())); | |
350 } | |
351 | |
352 // For every format that supports tile based decoding, ensure that | |
353 // calling decodeSubset will not fail if the caller has unreffed the | |
354 // stream provided in buildTileIndex. | |
355 // Only runs in debug mode since we are testing for a crash. | |
356 static void test_stream_life() { | |
357 const SkImageEncoder::Type gTypes[] = { | |
358 #ifdef SK_BUILD_FOR_ANDROID | |
359 SkImageEncoder::kJPEG_Type, | |
360 SkImageEncoder::kPNG_Type, | |
361 #endif | |
362 SkImageEncoder::kWEBP_Type, | |
363 }; | |
364 for (size_t i = 0; i < SK_ARRAY_COUNT(gTypes); ++i) { | |
365 // SkDebugf("encoding to %i\n", i); | |
366 SkAutoTUnref<SkMemoryStream> stream(create_image_stream(gTypes[i])); | |
367 if (NULL == stream.get()) { | |
368 SkDebugf("no stream\n"); | |
369 continue; | |
370 } | |
371 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); | |
372 if (NULL == decoder.get()) { | |
373 SkDebugf("no decoder\n"); | |
374 continue; | |
375 } | |
376 int width, height; | |
377 if (!decoder->buildTileIndex(stream.get(), &width, &height)) { | |
378 SkDebugf("could not build a tile index\n"); | |
379 continue; | |
380 } | |
381 // Now unref the stream to make sure it survives | |
382 stream.reset(NULL); | |
383 SkBitmap bm; | |
384 decoder->decodeSubset(&bm, SkIRect::MakeWH(width, height), kN32_SkColorT
ype); | |
385 } | |
386 } | |
387 | |
388 // Test inside SkScaledBitmapSampler.cpp | 327 // Test inside SkScaledBitmapSampler.cpp |
389 extern void test_row_proc_choice(); | 328 extern void test_row_proc_choice(); |
390 | |
391 #endif // SK_DEBUG | 329 #endif // SK_DEBUG |
392 | 330 |
393 DEF_TEST(ImageDecoding, reporter) { | 331 DEF_TEST(ImageDecoding, reporter) { |
394 test_unpremul(reporter); | 332 test_unpremul(reporter); |
395 #ifdef SK_DEBUG | 333 #ifdef SK_DEBUG |
396 test_stream_life(); | |
397 test_row_proc_choice(); | 334 test_row_proc_choice(); |
398 #endif | 335 #endif |
399 } | 336 } |
400 | 337 |
401 // expected output for 8x8 bitmap | 338 // expected output for 8x8 bitmap |
402 static const int kExpectedWidth = 8; | 339 static const int kExpectedWidth = 8; |
403 static const int kExpectedHeight = 8; | 340 static const int kExpectedHeight = 8; |
404 static const SkColor kExpectedPixels[] = { | 341 static const SkColor kExpectedPixels[] = { |
405 0xffbba570, 0xff395f5d, 0xffe25c39, 0xff197666, | 342 0xffbba570, 0xff395f5d, 0xffe25c39, 0xff197666, |
406 0xff3cba27, 0xffdefcb0, 0xffc13874, 0xfffa0093, | 343 0xff3cba27, 0xffdefcb0, 0xffc13874, 0xfffa0093, |
(...skipping 74 matching lines...) Loading... |
481 | 418 |
482 //////////////////////////////////////////////////////////////////////////////// | 419 //////////////////////////////////////////////////////////////////////////////// |
483 | 420 |
484 // example of how Android will do this inside their BitmapFactory | 421 // example of how Android will do this inside their BitmapFactory |
485 static SkPixelRef* install_pixel_ref(SkBitmap* bitmap, | 422 static SkPixelRef* install_pixel_ref(SkBitmap* bitmap, |
486 SkStreamRewindable* stream, | 423 SkStreamRewindable* stream, |
487 int sampleSize, bool ditherImage) { | 424 int sampleSize, bool ditherImage) { |
488 SkASSERT(bitmap != NULL); | 425 SkASSERT(bitmap != NULL); |
489 SkASSERT(stream != NULL); | 426 SkASSERT(stream != NULL); |
490 SkASSERT(stream->rewind()); | 427 SkASSERT(stream->rewind()); |
491 SkASSERT(stream->unique()); | |
492 SkColorType colorType = bitmap->colorType(); | 428 SkColorType colorType = bitmap->colorType(); |
493 SkDecodingImageGenerator::Options opts(sampleSize, ditherImage, colorType); | 429 SkDecodingImageGenerator::Options opts(sampleSize, ditherImage, colorType); |
494 if (SkInstallDiscardablePixelRef( | 430 if (SkInstallDiscardablePixelRef( |
495 SkDecodingImageGenerator::Create(stream, opts), bitmap)) { | 431 SkDecodingImageGenerator::Create(stream, opts), bitmap)) { |
496 return bitmap->pixelRef(); | 432 return bitmap->pixelRef(); |
497 } | 433 } |
498 return NULL; | 434 return NULL; |
499 } | 435 } |
500 /** | 436 /** |
501 * A test for the SkDecodingImageGenerator::Create and | 437 * A test for the SkDecodingImageGenerator::Create and |
502 * SkInstallDiscardablePixelRef functions. | 438 * SkInstallDiscardablePixelRef functions. |
503 */ | 439 */ |
504 DEF_TEST(ImprovedBitmapFactory, reporter) { | 440 DEF_TEST(ImprovedBitmapFactory, reporter) { |
505 SkString pngFilename = GetResourcePath("randPixels.png"); | 441 SkString pngFilename = GetResourcePath("randPixels.png"); |
506 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(pngFilename.c_
str())); | 442 SkAutoTDelete<SkStreamRewindable> stream(SkStream::NewFromFile(pngFilename.c
_str())); |
507 if (sk_exists(pngFilename.c_str())) { | 443 if (sk_exists(pngFilename.c_str())) { |
508 SkBitmap bm; | 444 SkBitmap bm; |
509 SkAssertResult(bm.setInfo(SkImageInfo::MakeN32Premul(1, 1))); | 445 SkAssertResult(bm.setInfo(SkImageInfo::MakeN32Premul(1, 1))); |
510 REPORTER_ASSERT(reporter, | 446 REPORTER_ASSERT(reporter, |
511 install_pixel_ref(&bm, stream.detach(), 1, true)); | 447 install_pixel_ref(&bm, stream.detach(), 1, true)); |
512 SkAutoLockPixels alp(bm); | 448 SkAutoLockPixels alp(bm); |
513 REPORTER_ASSERT(reporter, bm.getPixels()); | 449 REPORTER_ASSERT(reporter, bm.getPixels()); |
514 } | 450 } |
515 } | 451 } |
516 | 452 |
(...skipping 174 matching lines...) Loading... |
691 const bool useDataList[] = {true, false}; | 627 const bool useDataList[] = {true, false}; |
692 | 628 |
693 for (size_t fidx = 0; fidx < SK_ARRAY_COUNT(files); ++fidx) { | 629 for (size_t fidx = 0; fidx < SK_ARRAY_COUNT(files); ++fidx) { |
694 SkString path = SkOSPath::Join(resourceDir.c_str(), files[fidx]); | 630 SkString path = SkOSPath::Join(resourceDir.c_str(), files[fidx]); |
695 if (!sk_exists(path.c_str())) { | 631 if (!sk_exists(path.c_str())) { |
696 continue; | 632 continue; |
697 } | 633 } |
698 | 634 |
699 SkAutoDataUnref encodedData(SkData::NewFromFileName(path.c_str())); | 635 SkAutoDataUnref encodedData(SkData::NewFromFileName(path.c_str())); |
700 REPORTER_ASSERT(reporter, encodedData.get() != NULL); | 636 REPORTER_ASSERT(reporter, encodedData.get() != NULL); |
701 SkAutoTUnref<SkStreamRewindable> encodedStream( | 637 SkAutoTDelete<SkStreamRewindable> encodedStream( |
702 SkStream::NewFromFile(path.c_str())); | 638 SkStream::NewFromFile(path.c_str())); |
703 REPORTER_ASSERT(reporter, encodedStream.get() != NULL); | 639 REPORTER_ASSERT(reporter, encodedStream.get() != NULL); |
704 | 640 |
705 for (size_t i = 0; i < SK_ARRAY_COUNT(scaleList); ++i) { | 641 for (size_t i = 0; i < SK_ARRAY_COUNT(scaleList); ++i) { |
706 for (size_t j = 0; j < SK_ARRAY_COUNT(ditherList); ++j) { | 642 for (size_t j = 0; j < SK_ARRAY_COUNT(ditherList); ++j) { |
707 for (size_t m = 0; m < SK_ARRAY_COUNT(useDataList); ++m) { | 643 for (size_t m = 0; m < SK_ARRAY_COUNT(useDataList); ++m) { |
708 for (size_t k = 0; k < SK_ARRAY_COUNT(colorList); ++k) { | 644 for (size_t k = 0; k < SK_ARRAY_COUNT(colorList); ++k) { |
709 SkDecodingImageGenerator::Options opts(scaleList[i], | 645 SkDecodingImageGenerator::Options opts(scaleList[i], |
710 ditherList[j], | 646 ditherList[j], |
711 colorList[k]); | 647 colorList[k]); |
(...skipping 63 matching lines...) Loading... |
775 void* fPixels; | 711 void* fPixels; |
776 size_t fSize; | 712 size_t fSize; |
777 }; | 713 }; |
778 } // namespace | 714 } // namespace |
779 | 715 |
780 /* This tests for a bug in libjpeg where INT32 is typedefed to long | 716 /* This tests for a bug in libjpeg where INT32 is typedefed to long |
781 and memory can be written to outside of the array. */ | 717 and memory can be written to outside of the array. */ |
782 DEF_TEST(ImageDecoding_JpegOverwrite, r) { | 718 DEF_TEST(ImageDecoding_JpegOverwrite, r) { |
783 SkString resourceDir = GetResourcePath(); | 719 SkString resourceDir = GetResourcePath(); |
784 SkString path = SkOSPath::Join(resourceDir.c_str(), "randPixels.jpg"); | 720 SkString path = SkOSPath::Join(resourceDir.c_str(), "randPixels.jpg"); |
785 SkAutoTUnref<SkStreamAsset> stream( | 721 SkAutoTDelete<SkStreamAsset> stream( |
786 SkStream::NewFromFile(path.c_str())); | 722 SkStream::NewFromFile(path.c_str())); |
787 if (!stream.get()) { | 723 if (!stream.get()) { |
788 SkDebugf("\nPath '%s' missing.\n", path.c_str()); | 724 SkDebugf("\nPath '%s' missing.\n", path.c_str()); |
789 return; | 725 return; |
790 } | 726 } |
791 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); | 727 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
792 if (NULL == decoder.get()) { | 728 if (NULL == decoder.get()) { |
793 ERRORF(r, "\nSkImageDecoder::Factory failed.\n"); | 729 ERRORF(r, "\nSkImageDecoder::Factory failed.\n"); |
794 return; | 730 return; |
795 } | 731 } |
(...skipping 12 matching lines...) Loading... |
808 ((void*)pixels.get(), sizeof(uint16_t) * pixelCount))); | 744 ((void*)pixels.get(), sizeof(uint16_t) * pixelCount))); |
809 decoder->setAllocator(allocator); | 745 decoder->setAllocator(allocator); |
810 decoder->setSampleSize(2); | 746 decoder->setSampleSize(2); |
811 SkBitmap bitmap; | 747 SkBitmap bitmap; |
812 bool success = decoder->decode(stream, &bitmap, kRGB_565_SkColorType, | 748 bool success = decoder->decode(stream, &bitmap, kRGB_565_SkColorType, |
813 SkImageDecoder::kDecodePixels_Mode) != SkImag
eDecoder::kFailure; | 749 SkImageDecoder::kDecodePixels_Mode) != SkImag
eDecoder::kFailure; |
814 REPORTER_ASSERT(r, success); | 750 REPORTER_ASSERT(r, success); |
815 REPORTER_ASSERT(r, !allocator->ready()); // Decoder used correct memory | 751 REPORTER_ASSERT(r, !allocator->ready()); // Decoder used correct memory |
816 REPORTER_ASSERT(r, sentinal == pixels[pixelCount]); | 752 REPORTER_ASSERT(r, sentinal == pixels[pixelCount]); |
817 } | 753 } |
OLD | NEW |