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
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkData.h"
10
11 #include "include/core/SkGraphics.h"
12 #include "include/core/SkImageGenerator.h"
13 #include "include/private/SkImageInfoPriv.h"
14 #include "tests/Test.h"
15
16 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
17 #include "include/ports/SkImageGeneratorCG.h"
18 #elif defined(SK_BUILD_FOR_WIN)
19 #include "include/ports/SkImageGeneratorWIC.h"
20 #endif
21
22 static bool gMyFactoryWasCalled;
23
my_factory(sk_sp<SkData>)24 static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) {
25 gMyFactoryWasCalled = true;
26 return nullptr;
27 }
28
test_imagegenerator_factory(skiatest::Reporter * reporter)29 static void test_imagegenerator_factory(skiatest::Reporter* reporter) {
30 // just need a non-empty data to test things
31 sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory"));
32
33 gMyFactoryWasCalled = false;
34
35 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
36
37 std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data);
38 REPORTER_ASSERT(reporter, nullptr == gen);
39 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
40
41 // Test is racy, in that it hopes no other thread is changing this global...
42 auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory);
43 gen = SkImageGenerator::MakeFromEncoded(data);
44 REPORTER_ASSERT(reporter, nullptr == gen);
45 REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
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
54 SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev);
55 }
56
57 class MyImageGenerator : public SkImageGenerator {
58 public:
MyImageGenerator()59 MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
60 };
61
DEF_TEST(ImageGenerator,reporter)62 DEF_TEST(ImageGenerator, reporter) {
63 MyImageGenerator ig;
64 SkYUVAPixmapInfo yuvaPixmapInfo;
65
66 // Check that the YUV decoding API does not cause any crashes
67 ig.queryYUVAInfo(SkYUVAPixmapInfo::SupportedDataTypes::All(), &yuvaPixmapInfo);
68 SkYUVAInfo yuvaInfo({250, 250},
69 SkYUVAInfo::PlaneConfig::kY_UV,
70 SkYUVAInfo::Subsampling::k420,
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);
77
78 // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
79 if (false) {
80 test_imagegenerator_factory(reporter);
81 }
82 }
83
84 #include "include/core/SkPictureRecorder.h"
85 #include "src/core/SkAutoMalloc.h"
86
make_picture()87 static sk_sp<SkPicture> make_picture() {
88 SkPictureRecorder recorder;
89 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
90 return recorder.finishRecordingAsPicture();
91 }
92
DEF_TEST(PictureImageGenerator,reporter)93 DEF_TEST(PictureImageGenerator, reporter) {
94 const struct {
95 SkColorType fColorType;
96 SkAlphaType fAlphaType;
97 } recs[] = {
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 },
103
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 },
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
117 SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F32_SkColorType));
118
119 for (const auto& rec : recs) {
120 SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace);
121 REPORTER_ASSERT(reporter, gen->getPixels(info, storage.get(), info.minRowBytes()));
122 }
123 }
124
125