2 * Copyright 2012 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_Base_DEFINED
9 #define SkImage_Base_DEFINED
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkSurface.h"
14 #include "src/core/SkMipmap.h"
18 #include "include/private/SkTDArray.h"
19 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
20 #include "src/gpu/ganesh/GrTextureProxy.h"
21 #include "src/gpu/ganesh/SkGr.h"
26 #ifdef SK_GRAPHITE_ENABLED
27 #include "src/gpu/graphite/TextureProxyView.h"
32 class GrDirectContext;
38 kNeedNewImageUniqueID = 0
41 class SkImage_Base : public SkImage {
43 ~SkImage_Base() override;
45 virtual bool onPeekPixels(SkPixmap*) const { return false; }
47 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
49 virtual bool onReadPixels(GrDirectContext*,
50 const SkImageInfo& dstInfo,
55 CachingHint) const = 0;
57 virtual bool onHasMipmaps() const = 0;
59 virtual SkMipmap* onPeekMips() const { return nullptr; }
61 sk_sp<SkMipmap> refMips() const {
62 return sk_ref_sp(this->onPeekMips());
66 * Default implementation does a rescale/read and then calls the callback.
68 virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
69 const SkIRect& srcRect,
73 ReadPixelsContext) const;
75 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
77 virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
78 sk_sp<SkColorSpace> dstColorSpace,
79 const SkIRect& srcRect,
80 const SkISize& dstSize,
84 ReadPixelsContext) const;
86 virtual GrImageContext* context() const { return nullptr; }
88 /** this->context() try-casted to GrDirectContext. Useful for migrations – avoid otherwise! */
89 GrDirectContext* directContext() const;
92 virtual GrSemaphoresSubmitted onFlush(GrDirectContext*, const GrFlushInfo&) const {
93 return GrSemaphoresSubmitted::kNo;
96 // Returns a GrSurfaceProxyView representation of the image, if possible. This also returns
97 // a color type. This may be different than the image's color type when the image is not
98 // texture-backed and the capabilities of the GPU require a data type conversion to put
99 // the data in a texture.
100 std::tuple<GrSurfaceProxyView, GrColorType> asView(
101 GrRecordingContext* context,
102 GrMipmapped mipmapped,
103 GrImageTexGenPolicy policy = GrImageTexGenPolicy::kDraw) const;
106 * Returns a GrFragmentProcessor that can be used with the passed GrRecordingContext to
107 * draw the image. SkSamplingOptions indicates the filter and SkTileMode[] indicates the x and
108 * y tile modes. The passed matrix is applied to the coordinates before sampling the image.
109 * Optional 'subset' indicates whether the tile modes should be applied to a subset of the image
110 * Optional 'domain' is a bound on the coordinates of the image that will be required and can be
111 * used to optimize the shader if 'subset' is also specified.
113 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*,
117 const SkRect* subset = nullptr,
118 const SkRect* domain = nullptr) const;
120 virtual bool isYUVA() const { return false; }
122 // If this image is the current cached image snapshot of a surface then this is called when the
123 // surface is destroyed to indicate no further writes may happen to surface backing store.
124 virtual void generatingSurfaceIsDeleted() {}
126 virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
127 GrSurfaceOrigin* origin) const;
129 #ifdef SK_GRAPHITE_ENABLED
130 // Returns a TextureProxyView representation of the image, if possible. This also returns
131 // a color type. This may be different than the image's color type when the image is not
132 // texture-backed and the capabilities of the GPU require a data type conversion to put
133 // the data in a texture.
134 std::tuple<skgpu::graphite::TextureProxyView, SkColorType> asView(
135 skgpu::graphite::Recorder*,
136 skgpu::graphite::Mipmapped mipmapped) const;
139 virtual bool onPinAsTexture(GrRecordingContext*) const { return false; }
140 virtual void onUnpinAsTexture(GrRecordingContext*) const {}
141 virtual bool isPinnedOnContext(GrRecordingContext*) const { return false; }
143 // return a read-only copy of the pixels. We promise to not modify them,
144 // but only inspect them (or encode them).
145 virtual bool getROPixels(GrDirectContext*, SkBitmap*,
146 CachingHint = kAllow_CachingHint) const = 0;
148 virtual sk_sp<SkImage> onMakeSubset(const SkIRect&, GrDirectContext*) const = 0;
150 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
152 virtual bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const;
154 // True for picture-backed and codec-backed
155 virtual bool onIsLazyGenerated() const { return false; }
157 // True for images instantiated by Ganesh in GPU memory
158 virtual bool isGaneshBacked() const { return false; }
160 // True for images instantiated by Graphite in GPU memory
161 virtual bool isGraphiteBacked() const { return false; }
163 // Amount of texture memory used by texture-backed images.
164 virtual size_t onTextureSize() const { return 0; }
166 // Call when this image is part of the key to a resourcecache entry. This allows the cache
167 // to know automatically those entries can be purged when this SkImage deleted.
168 virtual void notifyAddedToRasterCache() const {
169 fAddedToRasterCache.store(true);
172 virtual bool onIsValid(GrRecordingContext*) const = 0;
174 virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
175 GrDirectContext*) const = 0;
177 virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0;
179 // on failure, returns nullptr
180 virtual sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap>) const {
185 SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
188 // Utility for making a copy of an existing view when the GrImageTexGenPolicy is not kDraw.
189 static GrSurfaceProxyView CopyView(GrRecordingContext*,
190 GrSurfaceProxyView src,
192 GrImageTexGenPolicy);
194 static std::unique_ptr<GrFragmentProcessor> MakeFragmentProcessorFromView(GrRecordingContext*,
200 const SkRect* subset,
201 const SkRect* domain);
204 * Returns input view if it is already mipmapped. Otherwise, attempts to make a mipmapped view
205 * with the same contents. If the mipmapped copy is successfully created it will be cached
206 * using the image unique ID. A subsequent call with the same unique ID will return the cached
207 * view if it has not been purged. The view is cached with a key domain specific to this
210 static GrSurfaceProxyView FindOrMakeCachedMipmappedView(GrRecordingContext*,
212 uint32_t imageUniqueID);
217 virtual std::tuple<GrSurfaceProxyView, GrColorType> onAsView(
220 GrImageTexGenPolicy policy) const = 0;
222 virtual std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(
227 const SkRect* subset,
228 const SkRect* domain) const = 0;
230 #ifdef SK_GRAPHITE_ENABLED
231 virtual std::tuple<skgpu::graphite::TextureProxyView, SkColorType> onAsView(
232 skgpu::graphite::Recorder*,
233 skgpu::graphite::Mipmapped mipmapped) const {
234 return {}; // TODO: once incompatible derived classes are removed make this pure virtual
237 // Set true by caches when they cache content that's derived from the current pixels.
238 mutable std::atomic<bool> fAddedToRasterCache;
240 using INHERITED = SkImage;
243 static inline SkImage_Base* as_IB(SkImage* image) {
244 return static_cast<SkImage_Base*>(image);
247 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
248 return static_cast<SkImage_Base*>(image.get());
251 static inline const SkImage_Base* as_IB(const SkImage* image) {
252 return static_cast<const SkImage_Base*>(image);
256 inline GrSurfaceProxyView SkImage_Base::CopyView(GrRecordingContext* context,
257 GrSurfaceProxyView src,
258 GrMipmapped mipmapped,
259 GrImageTexGenPolicy policy) {
260 SkBudgeted budgeted = policy == GrImageTexGenPolicy::kNew_Uncached_Budgeted
263 return GrSurfaceProxyView::Copy(context,
266 SkBackingFit::kExact,