sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
| 7 | |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 8 | #include "SkData.h" |
Mike Reed | c090c64 | 2017-05-16 10:39:06 -0400 | [diff] [blame] | 9 | #include "SkCanvas.h" |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 10 | |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 11 | #include "SkGraphics.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 12 | #include "SkImageGenerator.h" |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 13 | #include "SkImageInfoPriv.h" |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 14 | #include "SkYUVAIndex.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 15 | #include "Test.h" |
| 16 | |
Leon Scroggins III | a57488a | 2018-06-25 14:01:29 -0400 | [diff] [blame] | 17 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 18 | #include "SkImageGeneratorCG.h" |
| 19 | #elif defined(SK_BUILD_FOR_WIN) |
| 20 | #include "SkImageGeneratorWIC.h" |
| 21 | #endif |
| 22 | |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 23 | static bool gMyFactoryWasCalled; |
| 24 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 25 | static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) { |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 26 | gMyFactoryWasCalled = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | return nullptr; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void test_imagegenerator_factory(skiatest::Reporter* reporter) { |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 31 | // just need a non-empty data to test things |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 32 | sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory")); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 33 | |
| 34 | gMyFactoryWasCalled = false; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 35 | |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 37 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 38 | std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, nullptr == gen); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 40 | REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 41 | |
| 42 | // Test is racy, in that it hopes no other thread is changing this global... |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 43 | auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory); |
| 44 | gen = SkImageGenerator::MakeFromEncoded(data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, nullptr == gen); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, gMyFactoryWasCalled); |
Leon Scroggins III | a57488a | 2018-06-25 14:01:29 -0400 | [diff] [blame] | 47 | |
| 48 | // This just verifies that the signatures match. |
| 49 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 50 | SkGraphics::SetImageGeneratorFromEncodedDataFactory(SkImageGeneratorCG::MakeFromEncodedCG); |
| 51 | #elif defined(SK_BUILD_FOR_WIN) |
| 52 | SkGraphics::SetImageGeneratorFromEncodedDataFactory(SkImageGeneratorWIC::MakeFromEncodedWIC); |
| 53 | #endif |
| 54 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 55 | SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 58 | class MyImageGenerator : public SkImageGenerator { |
| 59 | public: |
| 60 | MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} |
| 61 | }; |
| 62 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 63 | DEF_TEST(ImageGenerator, reporter) { |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 64 | MyImageGenerator ig; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 65 | SkYUVASizeInfo sizeInfo; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 66 | sizeInfo.fSizes[0] = SkISize::Make(200, 200); |
| 67 | sizeInfo.fSizes[1] = SkISize::Make(100, 100); |
| 68 | sizeInfo.fSizes[2] = SkISize::Make( 50, 50); |
| 69 | sizeInfo.fSizes[3] = SkISize::Make( 25, 25); |
| 70 | sizeInfo.fWidthBytes[0] = 0; |
| 71 | sizeInfo.fWidthBytes[1] = 0; |
| 72 | sizeInfo.fWidthBytes[2] = 0; |
| 73 | sizeInfo.fWidthBytes[3] = 0; |
| 74 | void* planes[4] = { nullptr }; |
| 75 | SkYUVAIndex yuvaIndices[4]; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 76 | SkYUVColorSpace colorSpace; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 77 | |
| 78 | // Check that the YUV decoding API does not cause any crashes |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 79 | ig.queryYUVA8(&sizeInfo, yuvaIndices, nullptr); |
| 80 | ig.queryYUVA8(&sizeInfo, yuvaIndices, &colorSpace); |
| 81 | sizeInfo.fWidthBytes[0] = 250; |
| 82 | sizeInfo.fWidthBytes[1] = 250; |
| 83 | sizeInfo.fWidthBytes[2] = 250; |
| 84 | sizeInfo.fWidthBytes[3] = 250; |
| 85 | yuvaIndices[0] = { 0, SkColorChannel::kR }; |
| 86 | yuvaIndices[1] = { 1, SkColorChannel::kR }; |
| 87 | yuvaIndices[2] = { 2, SkColorChannel::kR }; |
| 88 | yuvaIndices[3] = { 3, SkColorChannel::kR }; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 89 | int dummy; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 90 | planes[0] = planes[1] = planes[2] = planes[3] = &dummy; |
| 91 | ig.getYUVA8Planes(sizeInfo, yuvaIndices, planes); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 92 | |
fmalita | 5dd918b | 2015-09-14 14:51:04 -0700 | [diff] [blame] | 93 | // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339 |
| 94 | if (false) { |
| 95 | test_imagegenerator_factory(reporter); |
| 96 | } |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 97 | } |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 98 | |
| 99 | #include "SkAutoMalloc.h" |
| 100 | #include "SkPictureRecorder.h" |
| 101 | |
| 102 | static sk_sp<SkPicture> make_picture() { |
| 103 | SkPictureRecorder recorder; |
| 104 | recorder.beginRecording(100, 100)->drawColor(SK_ColorRED); |
| 105 | return recorder.finishRecordingAsPicture(); |
| 106 | } |
| 107 | |
| 108 | DEF_TEST(PictureImageGenerator, reporter) { |
| 109 | const struct { |
| 110 | SkColorType fColorType; |
| 111 | SkAlphaType fAlphaType; |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 112 | } recs[] = { |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 113 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
| 114 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
| 115 | { kRGBA_F16_SkColorType, kPremul_SkAlphaType }, |
| 116 | { kRGBA_F32_SkColorType, kPremul_SkAlphaType }, |
| 117 | { kRGBA_1010102_SkColorType, kPremul_SkAlphaType }, |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 118 | |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 119 | { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 120 | { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, |
| 121 | { kRGBA_F16_SkColorType, kUnpremul_SkAlphaType }, |
| 122 | { kRGBA_F32_SkColorType, kUnpremul_SkAlphaType }, |
| 123 | { kRGBA_1010102_SkColorType, kUnpremul_SkAlphaType }, |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | auto colorspace = SkColorSpace::MakeSRGB(); |
| 127 | auto picture = make_picture(); |
| 128 | auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr, |
| 129 | SkImage::BitDepth::kU8, colorspace); |
| 130 | |
| 131 | // worst case for all requests |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 132 | SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F32_SkColorType)); |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 133 | |
| 134 | for (const auto& rec : recs) { |
| 135 | SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace); |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 136 | REPORTER_ASSERT(reporter, gen->getPixels(info, storage.get(), info.minRowBytes())); |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 137 | } |
| 138 | } |
Matt Sarett | dc79270 | 2017-06-08 09:37:01 -0400 | [diff] [blame] | 139 | |