1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2018 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#ifndef SkImage_Lazy_DEFINED
9cb93a386Sopenharmony_ci#define SkImage_Lazy_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/private/SkIDChangeListener.h"
12cb93a386Sopenharmony_ci#include "include/private/SkMutex.h"
13cb93a386Sopenharmony_ci#include "src/image/SkImage_Base.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU
16cb93a386Sopenharmony_ci#include "include/core/SkYUVAPixmaps.h"
17cb93a386Sopenharmony_ci#endif
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ciclass SharedGenerator;
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ciclass SkImage_Lazy : public SkImage_Base {
22cb93a386Sopenharmony_cipublic:
23cb93a386Sopenharmony_ci    struct Validator {
24cb93a386Sopenharmony_ci        Validator(sk_sp<SharedGenerator>, const SkColorType*, sk_sp<SkColorSpace>);
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci        operator bool() const { return fSharedGenerator.get(); }
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci        sk_sp<SharedGenerator> fSharedGenerator;
29cb93a386Sopenharmony_ci        SkImageInfo            fInfo;
30cb93a386Sopenharmony_ci        sk_sp<SkColorSpace>    fColorSpace;
31cb93a386Sopenharmony_ci        uint32_t               fUniqueID;
32cb93a386Sopenharmony_ci    };
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci    SkImage_Lazy(Validator* validator);
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    bool onHasMipmaps() const override {
37cb93a386Sopenharmony_ci        // TODO: Should we defer to the generator? The generator interface currently doesn't have
38cb93a386Sopenharmony_ci        // a way to provide content for levels other than via SkImageGenerator::generateTexture().
39cb93a386Sopenharmony_ci        return false;
40cb93a386Sopenharmony_ci    }
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY,
43cb93a386Sopenharmony_ci                      CachingHint) const override;
44cb93a386Sopenharmony_ci    sk_sp<SkData> onRefEncoded() const override;
45cb93a386Sopenharmony_ci    sk_sp<SkImage> onMakeSubset(const SkIRect&, GrDirectContext*) const override;
46cb93a386Sopenharmony_ci    bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override;
47cb93a386Sopenharmony_ci    bool onIsLazyGenerated() const override { return true; }
48cb93a386Sopenharmony_ci    sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
49cb93a386Sopenharmony_ci                                                GrDirectContext*) const override;
50cb93a386Sopenharmony_ci    sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final;
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci    bool onIsValid(GrRecordingContext*) const override;
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU
55cb93a386Sopenharmony_ci    // Returns the texture proxy. CachingHint refers to whether the generator's output should be
56cb93a386Sopenharmony_ci    // cached in CPU memory. We will always cache the generated texture on success.
57cb93a386Sopenharmony_ci    GrSurfaceProxyView lockTextureProxyView(GrRecordingContext*,
58cb93a386Sopenharmony_ci                                            GrImageTexGenPolicy,
59cb93a386Sopenharmony_ci                                            GrMipmapped) const;
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci    // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This
62cb93a386Sopenharmony_ci    // may be different from the color type on the image in the case where we need up upload CPU
63cb93a386Sopenharmony_ci    // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert
64cb93a386Sopenharmony_ci    // the data to RGBA_8888 unorm on the CPU then upload that.
65cb93a386Sopenharmony_ci    GrColorType colorTypeOfLockTextureProxy(const GrCaps* caps) const;
66cb93a386Sopenharmony_ci#endif
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ciprivate:
69cb93a386Sopenharmony_ci    void addUniqueIDListener(sk_sp<SkIDChangeListener>) const;
70cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU
71cb93a386Sopenharmony_ci    std::tuple<GrSurfaceProxyView, GrColorType> onAsView(GrRecordingContext*,
72cb93a386Sopenharmony_ci                                                         GrMipmapped,
73cb93a386Sopenharmony_ci                                                         GrImageTexGenPolicy) const override;
74cb93a386Sopenharmony_ci    std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(GrRecordingContext*,
75cb93a386Sopenharmony_ci                                                               SkSamplingOptions,
76cb93a386Sopenharmony_ci                                                               const SkTileMode[],
77cb93a386Sopenharmony_ci                                                               const SkMatrix&,
78cb93a386Sopenharmony_ci                                                               const SkRect*,
79cb93a386Sopenharmony_ci                                                               const SkRect*) const override;
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci    GrSurfaceProxyView textureProxyViewFromPlanes(GrRecordingContext*, SkBudgeted) const;
82cb93a386Sopenharmony_ci    sk_sp<SkCachedData> getPlanes(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
83cb93a386Sopenharmony_ci                                  SkYUVAPixmaps* pixmaps) const;
84cb93a386Sopenharmony_ci#endif
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci    class ScopedGenerator;
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    // Note that this->imageInfo() is not necessarily the info from the generator. It may be
89cb93a386Sopenharmony_ci    // cropped by onMakeSubset and its color type/space may be changed by
90cb93a386Sopenharmony_ci    // onMakeColorTypeAndColorSpace.
91cb93a386Sopenharmony_ci    sk_sp<SharedGenerator> fSharedGenerator;
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci    // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs
94cb93a386Sopenharmony_ci    // and SkImage_Lazy instances. Cache the result of the last successful call.
95cb93a386Sopenharmony_ci    mutable SkMutex             fOnMakeColorTypeAndSpaceMutex;
96cb93a386Sopenharmony_ci    mutable sk_sp<SkImage>      fOnMakeColorTypeAndSpaceResult;
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU
99cb93a386Sopenharmony_ci    // When the SkImage_Lazy goes away, we will iterate over all the listeners to inform them
100cb93a386Sopenharmony_ci    // of the unique ID's demise. This is used to remove cached textures from GrContext.
101cb93a386Sopenharmony_ci    mutable SkIDChangeListener::List fUniqueIDListeners;
102cb93a386Sopenharmony_ci#endif
103cb93a386Sopenharmony_ci
104cb93a386Sopenharmony_ci    using INHERITED = SkImage_Base;
105cb93a386Sopenharmony_ci};
106cb93a386Sopenharmony_ci
107cb93a386Sopenharmony_ci#endif
108