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