Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / image / SkImage_Base.h
1 /*
2  * Copyright 2012 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 #ifndef SkImage_Base_DEFINED
9 #define SkImage_Base_DEFINED
10
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkSurface.h"
14 #include "src/core/SkMipmap.h"
15 #include <atomic>
16
17 #if SK_SUPPORT_GPU
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"
22
23 class GrTexture;
24 #endif
25
26 #ifdef SK_GRAPHITE_ENABLED
27 #include "src/gpu/graphite/TextureProxyView.h"
28 #endif
29
30 #include <new>
31
32 class GrDirectContext;
33 class GrImageContext;
34 class GrSamplerState;
35 class SkCachedData;
36
37 enum {
38     kNeedNewImageUniqueID = 0
39 };
40
41 class SkImage_Base : public SkImage {
42 public:
43     ~SkImage_Base() override;
44
45     virtual bool onPeekPixels(SkPixmap*) const { return false; }
46
47     virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
48
49     virtual bool onReadPixels(GrDirectContext*,
50                               const SkImageInfo& dstInfo,
51                               void* dstPixels,
52                               size_t dstRowBytes,
53                               int srcX,
54                               int srcY,
55                               CachingHint) const = 0;
56
57     virtual bool onHasMipmaps() const = 0;
58
59     virtual SkMipmap* onPeekMips() const { return nullptr; }
60
61     sk_sp<SkMipmap> refMips() const {
62         return sk_ref_sp(this->onPeekMips());
63     }
64
65     /**
66      * Default implementation does a rescale/read and then calls the callback.
67      */
68     virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
69                                              const SkIRect& srcRect,
70                                              RescaleGamma,
71                                              RescaleMode,
72                                              ReadPixelsCallback,
73                                              ReadPixelsContext) const;
74     /**
75      * Default implementation does a rescale/read/yuv conversion and then calls the callback.
76      */
77     virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
78                                                    sk_sp<SkColorSpace> dstColorSpace,
79                                                    const SkIRect& srcRect,
80                                                    const SkISize& dstSize,
81                                                    RescaleGamma,
82                                                    RescaleMode,
83                                                    ReadPixelsCallback,
84                                                    ReadPixelsContext) const;
85
86     virtual GrImageContext* context() const { return nullptr; }
87
88     /** this->context() try-casted to GrDirectContext. Useful for migrations – avoid otherwise! */
89     GrDirectContext* directContext() const;
90
91 #if SK_SUPPORT_GPU
92     virtual GrSemaphoresSubmitted onFlush(GrDirectContext*, const GrFlushInfo&) const {
93         return GrSemaphoresSubmitted::kNo;
94     }
95
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;
104
105     /**
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.
112      */
113     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*,
114                                                              SkSamplingOptions,
115                                                              const SkTileMode[2],
116                                                              const SkMatrix&,
117                                                              const SkRect* subset = nullptr,
118                                                              const SkRect* domain = nullptr) const;
119
120     virtual bool isYUVA() const { return false; }
121
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() {}
125
126     virtual GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
127                                                  GrSurfaceOrigin* origin) const;
128 #endif
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;
137 #endif
138
139     virtual bool onPinAsTexture(GrRecordingContext*) const { return false; }
140     virtual void onUnpinAsTexture(GrRecordingContext*) const {}
141     virtual bool isPinnedOnContext(GrRecordingContext*) const { return false; }
142
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;
147
148     virtual sk_sp<SkImage> onMakeSubset(const SkIRect&, GrDirectContext*) const = 0;
149
150     virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
151
152     virtual bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const;
153
154     // True for picture-backed and codec-backed
155     virtual bool onIsLazyGenerated() const { return false; }
156
157     // True for images instantiated by Ganesh in GPU memory
158     virtual bool isGaneshBacked() const { return false; }
159
160     // True for images instantiated by Graphite in GPU memory
161     virtual bool isGraphiteBacked() const { return false; }
162
163     // Amount of texture memory used by texture-backed images.
164     virtual size_t onTextureSize() const { return 0; }
165
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);
170     }
171
172     virtual bool onIsValid(GrRecordingContext*) const = 0;
173
174     virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
175                                                         GrDirectContext*) const = 0;
176
177     virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0;
178
179     // on failure, returns nullptr
180     virtual sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap>) const {
181         return nullptr;
182     }
183
184 protected:
185     SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
186
187 #if SK_SUPPORT_GPU
188     // Utility for making a copy of an existing view when the GrImageTexGenPolicy is not kDraw.
189     static GrSurfaceProxyView CopyView(GrRecordingContext*,
190                                        GrSurfaceProxyView src,
191                                        GrMipmapped,
192                                        GrImageTexGenPolicy);
193
194     static std::unique_ptr<GrFragmentProcessor> MakeFragmentProcessorFromView(GrRecordingContext*,
195                                                                               GrSurfaceProxyView,
196                                                                               SkAlphaType,
197                                                                               SkSamplingOptions,
198                                                                               const SkTileMode[2],
199                                                                               const SkMatrix&,
200                                                                               const SkRect* subset,
201                                                                               const SkRect* domain);
202
203     /**
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
208      * function.
209      */
210     static GrSurfaceProxyView FindOrMakeCachedMipmappedView(GrRecordingContext*,
211                                                             GrSurfaceProxyView,
212                                                             uint32_t imageUniqueID);
213 #endif
214
215 private:
216 #if SK_SUPPORT_GPU
217     virtual std::tuple<GrSurfaceProxyView, GrColorType> onAsView(
218             GrRecordingContext*,
219             GrMipmapped,
220             GrImageTexGenPolicy policy) const = 0;
221
222     virtual std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(
223             GrRecordingContext*,
224             SkSamplingOptions,
225             const SkTileMode[2],
226             const SkMatrix&,
227             const SkRect* subset,
228             const SkRect* domain) const = 0;
229 #endif
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
235     }
236 #endif
237     // Set true by caches when they cache content that's derived from the current pixels.
238     mutable std::atomic<bool> fAddedToRasterCache;
239
240     using INHERITED = SkImage;
241 };
242
243 static inline SkImage_Base* as_IB(SkImage* image) {
244     return static_cast<SkImage_Base*>(image);
245 }
246
247 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
248     return static_cast<SkImage_Base*>(image.get());
249 }
250
251 static inline const SkImage_Base* as_IB(const SkImage* image) {
252     return static_cast<const SkImage_Base*>(image);
253 }
254
255 #if SK_SUPPORT_GPU
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
261                           ? SkBudgeted::kYes
262                           : SkBudgeted::kNo;
263     return GrSurfaceProxyView::Copy(context,
264                                     std::move(src),
265                                     mipmapped,
266                                     SkBackingFit::kExact,
267                                     budgeted);
268 }
269 #endif
270
271 #endif