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