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" |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 10 | #include "SkGraphics.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 11 | #include "SkImageGenerator.h" |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 12 | #include "SkImageInfoPriv.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 13 | #include "Test.h" |
| 14 | |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 15 | static bool gMyFactoryWasCalled; |
| 16 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 17 | static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) { |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 18 | gMyFactoryWasCalled = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 19 | return nullptr; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | static void test_imagegenerator_factory(skiatest::Reporter* reporter) { |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 23 | // just need a non-empty data to test things |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 24 | sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory")); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 25 | |
| 26 | gMyFactoryWasCalled = false; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 27 | |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 28 | REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 29 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 30 | std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, nullptr == gen); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 32 | REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 33 | |
| 34 | // 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] | 35 | auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory); |
| 36 | gen = SkImageGenerator::MakeFromEncoded(data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, nullptr == gen); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, gMyFactoryWasCalled); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 39 | SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 42 | class MyImageGenerator : public SkImageGenerator { |
| 43 | public: |
| 44 | MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} |
| 45 | }; |
| 46 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 47 | DEF_TEST(ImageGenerator, reporter) { |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 48 | MyImageGenerator ig; |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 49 | SkYUVSizeInfo sizeInfo; |
| 50 | sizeInfo.fSizes[SkYUVSizeInfo::kY] = SkISize::Make(200, 200); |
| 51 | sizeInfo.fSizes[SkYUVSizeInfo::kU] = SkISize::Make(100, 100); |
| 52 | sizeInfo.fSizes[SkYUVSizeInfo::kV] = SkISize::Make( 50, 50); |
| 53 | sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 0; |
| 54 | sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 0; |
| 55 | sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 0; |
| 56 | void* planes[3] = { nullptr }; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 57 | SkYUVColorSpace colorSpace; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 58 | |
| 59 | // Check that the YUV decoding API does not cause any crashes |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 60 | ig.queryYUV8(&sizeInfo, nullptr); |
| 61 | ig.queryYUV8(&sizeInfo, &colorSpace); |
| 62 | sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 250; |
| 63 | sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 250; |
| 64 | sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 250; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 65 | int dummy; |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 66 | planes[SkYUVSizeInfo::kY] = planes[SkYUVSizeInfo::kU] = planes[SkYUVSizeInfo::kV] = &dummy; |
| 67 | ig.getYUV8Planes(sizeInfo, planes); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 68 | |
fmalita | 5dd918b | 2015-09-14 14:51:04 -0700 | [diff] [blame] | 69 | // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339 |
| 70 | if (false) { |
| 71 | test_imagegenerator_factory(reporter); |
| 72 | } |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 73 | } |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 74 | |
| 75 | #include "SkAutoMalloc.h" |
| 76 | #include "SkPictureRecorder.h" |
| 77 | |
| 78 | static sk_sp<SkPicture> make_picture() { |
| 79 | SkPictureRecorder recorder; |
| 80 | recorder.beginRecording(100, 100)->drawColor(SK_ColorRED); |
| 81 | return recorder.finishRecordingAsPicture(); |
| 82 | } |
| 83 | |
| 84 | DEF_TEST(PictureImageGenerator, reporter) { |
| 85 | const struct { |
| 86 | SkColorType fColorType; |
| 87 | SkAlphaType fAlphaType; |
| 88 | bool fExpectSuccess; |
| 89 | } recs[] = { |
| 90 | { kRGBA_8888_SkColorType, kPremul_SkAlphaType, kRGBA_8888_SkColorType == kN32_SkColorType }, |
| 91 | { kBGRA_8888_SkColorType, kPremul_SkAlphaType, kBGRA_8888_SkColorType == kN32_SkColorType }, |
| 92 | { kRGBA_F16_SkColorType, kPremul_SkAlphaType, true }, |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame^] | 93 | { kRGBA_1010102_SkColorType, kPremul_SkAlphaType, true }, |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 94 | |
| 95 | { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, false }, |
| 96 | { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, false }, |
| 97 | { kRGBA_F16_SkColorType, kUnpremul_SkAlphaType, false }, |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame^] | 98 | { kRGBA_1010102_SkColorType, kUnpremul_SkAlphaType, false }, |
Mike Reed | c756c7a | 2017-04-13 15:13:36 -0400 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | auto colorspace = SkColorSpace::MakeSRGB(); |
| 102 | auto picture = make_picture(); |
| 103 | auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr, |
| 104 | SkImage::BitDepth::kU8, colorspace); |
| 105 | |
| 106 | // worst case for all requests |
| 107 | SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType)); |
| 108 | |
| 109 | for (const auto& rec : recs) { |
| 110 | SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace); |
| 111 | bool success = gen->getPixels(info, storage.get(), info.minRowBytes()); |
| 112 | REPORTER_ASSERT(reporter, success == rec.fExpectSuccess); |
| 113 | } |
| 114 | } |
Matt Sarett | dc79270 | 2017-06-08 09:37:01 -0400 | [diff] [blame] | 115 | |