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