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