2 * Copyright 2018 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkImage_Lazy_DEFINED
9 #define SkImage_Lazy_DEFINED
11 #include "include/private/SkIDChangeListener.h"
12 #include "include/private/SkMutex.h"
13 #include "src/image/SkImage_Base.h"
16 #include "include/core/SkYUVAPixmaps.h"
19 class SharedGenerator;
21 class SkImage_Lazy : public SkImage_Base {
24 Validator(sk_sp<SharedGenerator>, const SkColorType*, sk_sp<SkColorSpace>);
26 explicit operator bool() const { return fSharedGenerator.get(); }
28 sk_sp<SharedGenerator> fSharedGenerator;
30 sk_sp<SkColorSpace> fColorSpace;
34 SkImage_Lazy(Validator* validator);
36 bool onHasMipmaps() const override {
37 // TODO: Should we defer to the generator? The generator interface currently doesn't have
38 // a way to provide content for levels other than via SkImageGenerator::generateTexture().
42 bool onReadPixels(GrDirectContext*, const SkImageInfo&, void*, size_t, int srcX, int srcY,
43 CachingHint) const override;
44 sk_sp<SkData> onRefEncoded() const override;
45 sk_sp<SkImage> onMakeSubset(const SkIRect&, GrDirectContext*) const override;
46 bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint) const override;
47 bool onIsLazyGenerated() const override { return true; }
48 sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
49 GrDirectContext*) const override;
50 sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final;
52 bool onIsValid(GrRecordingContext*) const override;
55 // Returns the texture proxy. CachingHint refers to whether the generator's output should be
56 // cached in CPU memory. We will always cache the generated texture on success.
57 GrSurfaceProxyView lockTextureProxyView(GrRecordingContext*,
61 // Returns the GrColorType to use with the GrTextureProxy returned from lockTextureProxy. This
62 // may be different from the color type on the image in the case where we need up upload CPU
63 // data to a texture but the GPU doesn't support the format of CPU data. In this case we convert
64 // the data to RGBA_8888 unorm on the CPU then upload that.
65 GrColorType colorTypeOfLockTextureProxy(const GrCaps* caps) const;
69 void addUniqueIDListener(sk_sp<SkIDChangeListener>) const;
71 std::tuple<GrSurfaceProxyView, GrColorType> onAsView(GrRecordingContext*,
73 GrImageTexGenPolicy) const override;
74 std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(GrRecordingContext*,
79 const SkRect*) const override;
81 GrSurfaceProxyView textureProxyViewFromPlanes(GrRecordingContext*, SkBudgeted) const;
82 sk_sp<SkCachedData> getPlanes(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
83 SkYUVAPixmaps* pixmaps) const;
86 class ScopedGenerator;
88 // Note that this->imageInfo() is not necessarily the info from the generator. It may be
89 // cropped by onMakeSubset and its color type/space may be changed by
90 // onMakeColorTypeAndColorSpace.
91 sk_sp<SharedGenerator> fSharedGenerator;
93 // Repeated calls to onMakeColorTypeAndColorSpace will result in a proliferation of unique IDs
94 // and SkImage_Lazy instances. Cache the result of the last successful call.
95 mutable SkMutex fOnMakeColorTypeAndSpaceMutex;
96 mutable sk_sp<SkImage> fOnMakeColorTypeAndSpaceResult;
99 // When the SkImage_Lazy goes away, we will iterate over all the listeners to inform them
100 // of the unique ID's demise. This is used to remove cached textures from GrContext.
101 mutable SkIDChangeListener::List fUniqueIDListeners;
104 using INHERITED = SkImage_Base;