Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / image / SkSurface_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 SkSurface_Base_DEFINED
9 #define SkSurface_Base_DEFINED
10
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkDeferredDisplayList.h"
13 #include "include/core/SkSurface.h"
14 #include "src/core/SkImagePriv.h"
15 #include "src/core/SkSurfacePriv.h"
16
17 class SkSurface_Base : public SkSurface {
18 public:
19     SkSurface_Base(int width, int height, const SkSurfaceProps*);
20     SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
21     ~SkSurface_Base() override;
22
23     virtual GrRecordingContext* onGetRecordingContext();
24     virtual skgpu::graphite::Recorder* onGetRecorder();
25
26 #if SK_SUPPORT_GPU
27     virtual GrBackendTexture onGetBackendTexture(BackendHandleAccess);
28     virtual GrBackendRenderTarget onGetBackendRenderTarget(BackendHandleAccess);
29     virtual bool onReplaceBackendTexture(const GrBackendTexture&,
30                                          GrSurfaceOrigin,
31                                          ContentChangeMode,
32                                          TextureReleaseProc,
33                                          ReleaseContext);
34
35     virtual void onResolveMSAA() {}
36
37     /**
38      * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
39      * Inserts the requested number of semaphores for the gpu to signal when work is complete on the
40      * gpu and inits the array of GrBackendSemaphores with the signaled semaphores.
41      */
42     virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, const GrFlushInfo&,
43                                           const GrBackendSurfaceMutableState*) {
44         return GrSemaphoresSubmitted::kNo;
45     }
46 #endif
47
48     /**
49      *  Allocate a canvas that will draw into this surface. We will cache this
50      *  canvas, to return the same object to the caller multiple times. We
51      *  take ownership, and will call unref() on the canvas when we go out of
52      *  scope.
53      */
54     virtual SkCanvas* onNewCanvas() = 0;
55
56     virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
57
58     /**
59      *  Allocate an SkImage that represents the current contents of the surface.
60      *  This needs to be able to outlive the surface itself (if need be), and
61      *  must faithfully represent the current contents, even if the surface
62      *  is changed after this called (e.g. it is drawn to via its canvas).
63      *
64      *  If a subset is specified, the the impl must make a copy, rather than try to wait
65      *  on copy-on-write.
66      */
67     virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
68
69     virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
70
71     /**
72      * Default implementation does a rescale/read and then calls the callback.
73      */
74     virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
75                                              const SkIRect& srcRect,
76                                              RescaleGamma,
77                                              RescaleMode,
78                                              ReadPixelsCallback,
79                                              ReadPixelsContext);
80     /**
81      * Default implementation does a rescale/read/yuv conversion and then calls the callback.
82      */
83     virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
84                                                    sk_sp<SkColorSpace> dstColorSpace,
85                                                    const SkIRect& srcRect,
86                                                    const SkISize& dstSize,
87                                                    RescaleGamma,
88                                                    RescaleMode,
89                                                    ReadPixelsCallback,
90                                                    ReadPixelsContext);
91
92     /**
93      *  Default implementation:
94      *
95      *  image = this->newImageSnapshot();
96      *  if (image) {
97      *      image->draw(canvas, ...);
98      *      image->unref();
99      *  }
100      */
101     virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkSamplingOptions&,const SkPaint*);
102
103     /**
104      * Called as a performance hint when the Surface is allowed to make it's contents
105      * undefined.
106      */
107     virtual void onDiscard() {}
108
109     /**
110      *  If the surface is about to change, we call this so that our subclass
111      *  can optionally fork their backend (copy-on-write) in case it was
112      *  being shared with the cachedImage.
113      *
114      *  Returns false if the backing cannot be un-shared.
115      */
116     virtual bool SK_WARN_UNUSED_RESULT onCopyOnWrite(ContentChangeMode) = 0;
117
118     /**
119      *  Signal the surface to remind its backing store that it's mutable again.
120      *  Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
121      */
122     virtual void onRestoreBackingMutability() {}
123
124     /**
125      * Caused the current backend 3D API to wait on the passed in semaphores before executing new
126      * commands on the gpu. Any previously submitting commands will not be blocked by these
127      * semaphores.
128      */
129     virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
130                         bool deleteSemaphoresAfterWait) {
131         return false;
132     }
133
134     virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
135     virtual bool onIsCompatible(const SkSurfaceCharacterization&) const { return false; }
136     virtual bool onDraw(sk_sp<const SkDeferredDisplayList>, SkIPoint offset) {
137         return false;
138     }
139
140     // TODO: Remove this (make it pure virtual) after updating Android (which has a class derived
141     // from SkSurface_Base).
142     virtual sk_sp<const SkCapabilities> onCapabilities();
143
144     inline SkCanvas* getCachedCanvas();
145     inline sk_sp<SkImage> refCachedImage();
146
147     bool hasCachedImage() const { return fCachedImage != nullptr; }
148
149     // called by SkSurface to compute a new genID
150     uint32_t newGenerationID();
151
152 private:
153     std::unique_ptr<SkCanvas>   fCachedCanvas;
154     sk_sp<SkImage>              fCachedImage;
155
156     // Returns false if drawing should not take place (allocation failure).
157     bool SK_WARN_UNUSED_RESULT aboutToDraw(ContentChangeMode mode);
158
159     // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
160     // would trigger a copy-on-write.
161     bool outstandingImageSnapshot() const;
162
163     friend class SkCanvas;
164     friend class SkSurface;
165
166     using INHERITED = SkSurface;
167 };
168
169 SkCanvas* SkSurface_Base::getCachedCanvas() {
170     if (nullptr == fCachedCanvas) {
171         fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
172         if (fCachedCanvas) {
173             fCachedCanvas->setSurfaceBase(this);
174         }
175     }
176     return fCachedCanvas.get();
177 }
178
179 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
180     if (fCachedImage) {
181         return fCachedImage;
182     }
183
184     fCachedImage = this->onNewImageSnapshot();
185
186     SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
187     return fCachedImage;
188 }
189
190 #endif