blob: 790b422ca3643c5d413192dae1f14322176bc810 [file] [log] [blame]
sugoi518d83d2014-07-21 11:37:39 -07001/*
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
reed1c846342015-07-09 11:47:36 -07008#include "SkData.h"
Mike Reedc090c642017-05-16 10:39:06 -04009#include "SkCanvas.h"
Jim Van Verth8f11e432018-10-18 14:36:59 -040010
reed1c846342015-07-09 11:47:36 -070011#include "SkGraphics.h"
sugoi518d83d2014-07-21 11:37:39 -070012#include "SkImageGenerator.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050013#include "SkImageInfoPriv.h"
Jim Van Verth8f11e432018-10-18 14:36:59 -040014#include "SkYUVAIndex.h"
sugoi518d83d2014-07-21 11:37:39 -070015#include "Test.h"
16
Leon Scroggins IIIa57488a2018-06-25 14:01:29 -040017#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
reedd7c05bf2015-07-09 14:08:49 -070023static bool gMyFactoryWasCalled;
24
Mike Reed185130c2017-02-15 15:14:16 -050025static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) {
reedd7c05bf2015-07-09 14:08:49 -070026 gMyFactoryWasCalled = true;
halcanary96fcdcc2015-08-27 07:41:13 -070027 return nullptr;
reed1c846342015-07-09 11:47:36 -070028}
29
30static void test_imagegenerator_factory(skiatest::Reporter* reporter) {
reedd7c05bf2015-07-09 14:08:49 -070031 // just need a non-empty data to test things
bungeman38d909e2016-08-02 14:40:46 -070032 sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory"));
reedd7c05bf2015-07-09 14:08:49 -070033
34 gMyFactoryWasCalled = false;
reed1c846342015-07-09 11:47:36 -070035
reedd7c05bf2015-07-09 14:08:49 -070036 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070037
Mike Reed185130c2017-02-15 15:14:16 -050038 std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data);
halcanary96fcdcc2015-08-27 07:41:13 -070039 REPORTER_ASSERT(reporter, nullptr == gen);
reedd7c05bf2015-07-09 14:08:49 -070040 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070041
42 // Test is racy, in that it hopes no other thread is changing this global...
Mike Reed185130c2017-02-15 15:14:16 -050043 auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory);
44 gen = SkImageGenerator::MakeFromEncoded(data);
halcanary96fcdcc2015-08-27 07:41:13 -070045 REPORTER_ASSERT(reporter, nullptr == gen);
reedd7c05bf2015-07-09 14:08:49 -070046 REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
Leon Scroggins IIIa57488a2018-06-25 14:01:29 -040047
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 Reed185130c2017-02-15 15:14:16 -050055 SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev);
reed1c846342015-07-09 11:47:36 -070056}
57
reed3ef71e32015-03-19 08:31:14 -070058class MyImageGenerator : public SkImageGenerator {
59public:
60 MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
61};
62
sugoi518d83d2014-07-21 11:37:39 -070063DEF_TEST(ImageGenerator, reporter) {
reed3ef71e32015-03-19 08:31:14 -070064 MyImageGenerator ig;
Jim Van Verthe24b5872018-10-29 16:26:02 -040065 SkYUVASizeInfo sizeInfo;
Jim Van Verth8f11e432018-10-18 14:36:59 -040066 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];
rileyaabaef862014-09-12 17:45:58 -070076 SkYUVColorSpace colorSpace;
sugoi518d83d2014-07-21 11:37:39 -070077
78 // Check that the YUV decoding API does not cause any crashes
Jim Van Verth8f11e432018-10-18 14:36:59 -040079 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 };
sugoi518d83d2014-07-21 11:37:39 -070089 int dummy;
Jim Van Verth8f11e432018-10-18 14:36:59 -040090 planes[0] = planes[1] = planes[2] = planes[3] = &dummy;
91 ig.getYUVA8Planes(sizeInfo, yuvaIndices, planes);
reed1c846342015-07-09 11:47:36 -070092
fmalita5dd918b2015-09-14 14:51:04 -070093 // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
94 if (false) {
95 test_imagegenerator_factory(reporter);
96 }
sugoi518d83d2014-07-21 11:37:39 -070097}
Mike Reedc756c7a2017-04-13 15:13:36 -040098
99#include "SkAutoMalloc.h"
100#include "SkPictureRecorder.h"
101
102static sk_sp<SkPicture> make_picture() {
103 SkPictureRecorder recorder;
104 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
105 return recorder.finishRecordingAsPicture();
106}
107
108DEF_TEST(PictureImageGenerator, reporter) {
109 const struct {
110 SkColorType fColorType;
111 SkAlphaType fAlphaType;
Mike Reedc756c7a2017-04-13 15:13:36 -0400112 } recs[] = {
Brian Osman431ac562018-10-10 13:20:38 -0400113 { 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 Reedc756c7a2017-04-13 15:13:36 -0400118
Brian Osman431ac562018-10-10 13:20:38 -0400119 { 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 Reedc756c7a2017-04-13 15:13:36 -0400124 };
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 Klein37854712018-06-26 11:43:06 -0400132 SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F32_SkColorType));
Mike Reedc756c7a2017-04-13 15:13:36 -0400133
134 for (const auto& rec : recs) {
135 SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace);
Brian Osman431ac562018-10-10 13:20:38 -0400136 REPORTER_ASSERT(reporter, gen->getPixels(info, storage.get(), info.minRowBytes()));
Mike Reedc756c7a2017-04-13 15:13:36 -0400137 }
138}
Matt Sarettdc792702017-06-08 09:37:01 -0400139