1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2014 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
9cb93a386Sopenharmony_ci#include "include/core/SkData.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkGraphics.h"
12cb93a386Sopenharmony_ci#include "include/core/SkImageGenerator.h"
13cb93a386Sopenharmony_ci#include "include/private/SkImageInfoPriv.h"
14cb93a386Sopenharmony_ci#include "tests/Test.h"
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
17cb93a386Sopenharmony_ci    #include "include/ports/SkImageGeneratorCG.h"
18cb93a386Sopenharmony_ci#elif defined(SK_BUILD_FOR_WIN)
19cb93a386Sopenharmony_ci    #include "include/ports/SkImageGeneratorWIC.h"
20cb93a386Sopenharmony_ci#endif
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_cistatic bool gMyFactoryWasCalled;
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_cistatic std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) {
25cb93a386Sopenharmony_ci    gMyFactoryWasCalled = true;
26cb93a386Sopenharmony_ci    return nullptr;
27cb93a386Sopenharmony_ci}
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_cistatic void test_imagegenerator_factory(skiatest::Reporter* reporter) {
30cb93a386Sopenharmony_ci    // just need a non-empty data to test things
31cb93a386Sopenharmony_ci    sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory"));
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    gMyFactoryWasCalled = false;
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data);
38cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, nullptr == gen);
39cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    // Test is racy, in that it hopes no other thread is changing this global...
42cb93a386Sopenharmony_ci    auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory);
43cb93a386Sopenharmony_ci    gen = SkImageGenerator::MakeFromEncoded(data);
44cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, nullptr == gen);
45cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci    // This just verifies that the signatures match.
48cb93a386Sopenharmony_ci#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
49cb93a386Sopenharmony_ci    SkGraphics::SetImageGeneratorFromEncodedDataFactory(SkImageGeneratorCG::MakeFromEncodedCG);
50cb93a386Sopenharmony_ci#elif defined(SK_BUILD_FOR_WIN)
51cb93a386Sopenharmony_ci    SkGraphics::SetImageGeneratorFromEncodedDataFactory(SkImageGeneratorWIC::MakeFromEncodedWIC);
52cb93a386Sopenharmony_ci#endif
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci    SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev);
55cb93a386Sopenharmony_ci}
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ciclass MyImageGenerator : public SkImageGenerator {
58cb93a386Sopenharmony_cipublic:
59cb93a386Sopenharmony_ci    MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
60cb93a386Sopenharmony_ci};
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ciDEF_TEST(ImageGenerator, reporter) {
63cb93a386Sopenharmony_ci    MyImageGenerator ig;
64cb93a386Sopenharmony_ci    SkYUVAPixmapInfo yuvaPixmapInfo;
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ci    // Check that the YUV decoding API does not cause any crashes
67cb93a386Sopenharmony_ci    ig.queryYUVAInfo(SkYUVAPixmapInfo::SupportedDataTypes::All(), &yuvaPixmapInfo);
68cb93a386Sopenharmony_ci    SkYUVAInfo yuvaInfo({250, 250},
69cb93a386Sopenharmony_ci                        SkYUVAInfo::PlaneConfig::kY_UV,
70cb93a386Sopenharmony_ci                        SkYUVAInfo::Subsampling::k420,
71cb93a386Sopenharmony_ci                        kJPEG_Full_SkYUVColorSpace);
72cb93a386Sopenharmony_ci    yuvaPixmapInfo = SkYUVAPixmapInfo(yuvaInfo,
73cb93a386Sopenharmony_ci                                      SkYUVAPixmapInfo::DataType::kUnorm8,
74cb93a386Sopenharmony_ci                                      /*rowBytes[]*/ nullptr);
75cb93a386Sopenharmony_ci    SkYUVAPixmaps yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaPixmapInfo);
76cb93a386Sopenharmony_ci    ig.getYUVAPlanes(yuvaPixmaps);
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci    // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
79cb93a386Sopenharmony_ci    if (false) {
80cb93a386Sopenharmony_ci        test_imagegenerator_factory(reporter);
81cb93a386Sopenharmony_ci    }
82cb93a386Sopenharmony_ci}
83cb93a386Sopenharmony_ci
84cb93a386Sopenharmony_ci#include "include/core/SkPictureRecorder.h"
85cb93a386Sopenharmony_ci#include "src/core/SkAutoMalloc.h"
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_cistatic sk_sp<SkPicture> make_picture() {
88cb93a386Sopenharmony_ci    SkPictureRecorder recorder;
89cb93a386Sopenharmony_ci    recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
90cb93a386Sopenharmony_ci    return recorder.finishRecordingAsPicture();
91cb93a386Sopenharmony_ci}
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ciDEF_TEST(PictureImageGenerator, reporter) {
94cb93a386Sopenharmony_ci    const struct {
95cb93a386Sopenharmony_ci        SkColorType fColorType;
96cb93a386Sopenharmony_ci        SkAlphaType fAlphaType;
97cb93a386Sopenharmony_ci    } recs[] = {
98cb93a386Sopenharmony_ci        { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
99cb93a386Sopenharmony_ci        { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
100cb93a386Sopenharmony_ci        { kRGBA_F16_SkColorType,  kPremul_SkAlphaType },
101cb93a386Sopenharmony_ci        { kRGBA_F32_SkColorType,  kPremul_SkAlphaType },
102cb93a386Sopenharmony_ci        { kRGBA_1010102_SkColorType, kPremul_SkAlphaType },
103cb93a386Sopenharmony_ci
104cb93a386Sopenharmony_ci        { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
105cb93a386Sopenharmony_ci        { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
106cb93a386Sopenharmony_ci        { kRGBA_F16_SkColorType,  kUnpremul_SkAlphaType },
107cb93a386Sopenharmony_ci        { kRGBA_F32_SkColorType,  kUnpremul_SkAlphaType },
108cb93a386Sopenharmony_ci        { kRGBA_1010102_SkColorType, kUnpremul_SkAlphaType },
109cb93a386Sopenharmony_ci    };
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ci    auto colorspace = SkColorSpace::MakeSRGB();
112cb93a386Sopenharmony_ci    auto picture = make_picture();
113cb93a386Sopenharmony_ci    auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
114cb93a386Sopenharmony_ci                                                 SkImage::BitDepth::kU8, colorspace);
115cb93a386Sopenharmony_ci
116cb93a386Sopenharmony_ci    // worst case for all requests
117cb93a386Sopenharmony_ci    SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F32_SkColorType));
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_ci    for (const auto& rec : recs) {
120cb93a386Sopenharmony_ci        SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace);
121cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, gen->getPixels(info, storage.get(), info.minRowBytes()));
122cb93a386Sopenharmony_ci    }
123cb93a386Sopenharmony_ci}
124cb93a386Sopenharmony_ci
125