• 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     virtual skgpu::graphite::Recorder* onGetRecorder();
25 
26 #if defined(SK_GANESH)
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 
onResolveMSAA()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      */
onFlush(BackendSurfaceAccess access,const GrFlushInfo &,const skgpu::MutableTextureState *)42     virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, const GrFlushInfo&,
43                                           const skgpu::MutableTextureState*) {
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 #if defined(SK_GRAPHITE)
onAsImage()70     virtual sk_sp<SkImage> onAsImage() { return nullptr; }
71 
onMakeImageCopy(const SkIRect *,skgpu::Mipmapped)72     virtual sk_sp<SkImage> onMakeImageCopy(const SkIRect* /* subset */,
73                                            skgpu::Mipmapped) {
74         return nullptr;
75     }
76 #endif
77 
78     virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
79 
80     /**
81      * Default implementation does a rescale/read and then calls the callback.
82      */
83     virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
84                                              const SkIRect srcRect,
85                                              RescaleGamma,
86                                              RescaleMode,
87                                              ReadPixelsCallback,
88                                              ReadPixelsContext);
89     /**
90      * Default implementation does a rescale/read/yuv conversion and then calls the callback.
91      */
92     virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
93                                                    sk_sp<SkColorSpace> dstColorSpace,
94                                                    SkIRect srcRect,
95                                                    SkISize dstSize,
96                                                    RescaleGamma,
97                                                    RescaleMode,
98                                                    ReadPixelsCallback,
99                                                    ReadPixelsContext);
100 
101     /**
102      *  Default implementation:
103      *
104      *  image = this->newImageSnapshot();
105      *  if (image) {
106      *      image->draw(canvas, ...);
107      *      image->unref();
108      *  }
109      */
110     virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkSamplingOptions&,const SkPaint*);
111 
112     /**
113      * Called as a performance hint when the Surface is allowed to make it's contents
114      * undefined.
115      */
onDiscard()116     virtual void onDiscard() {}
117 
118     /**
119      *  If the surface is about to change, we call this so that our subclass
120      *  can optionally fork their backend (copy-on-write) in case it was
121      *  being shared with the cachedImage.
122      *
123      *  Returns false if the backing cannot be un-shared.
124      */
125     virtual bool SK_WARN_UNUSED_RESULT onCopyOnWrite(ContentChangeMode) = 0;
126 
127     /**
128      *  Signal the surface to remind its backing store that it's mutable again.
129      *  Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
130      */
onRestoreBackingMutability()131     virtual void onRestoreBackingMutability() {}
132 
133     /**
134      * Caused the current backend 3D API to wait on the passed in semaphores before executing new
135      * commands on the gpu. Any previously submitting commands will not be blocked by these
136      * semaphores.
137      */
onWait(int numSemaphores,const GrBackendSemaphore * waitSemaphores,bool deleteSemaphoresAfterWait)138     virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
139                         bool deleteSemaphoresAfterWait) {
140         return false;
141     }
142 
onCharacterize(SkSurfaceCharacterization *)143     virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
onIsCompatible(const SkSurfaceCharacterization &)144     virtual bool onIsCompatible(const SkSurfaceCharacterization&) const { return false; }
onDraw(sk_sp<const SkDeferredDisplayList>,SkIPoint offset)145     virtual bool onDraw(sk_sp<const SkDeferredDisplayList>, SkIPoint offset) {
146         return false;
147     }
148 
149     // TODO: Remove this (make it pure virtual) after updating Android (which has a class derived
150     // from SkSurface_Base).
151     virtual sk_sp<const SkCapabilities> onCapabilities();
152 
153     // True for surfaces instantiated by Graphite in GPU memory
isGraphiteBacked()154     virtual bool isGraphiteBacked() const { return false; }
155 
156     inline SkCanvas* getCachedCanvas();
157     inline sk_sp<SkImage> refCachedImage();
158 
hasCachedImage()159     bool hasCachedImage() const { return fCachedImage != nullptr; }
160 
161     // called by SkSurface to compute a new genID
162     uint32_t newGenerationID();
163 
164 private:
165     std::unique_ptr<SkCanvas>   fCachedCanvas;
166     sk_sp<SkImage>              fCachedImage;
167 
168     // Returns false if drawing should not take place (allocation failure).
169     bool SK_WARN_UNUSED_RESULT aboutToDraw(ContentChangeMode mode);
170 
171     // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
172     // would trigger a copy-on-write.
173     bool outstandingImageSnapshot() const;
174 
175     friend class SkCanvas;
176     friend class SkSurface;
177 
178     using INHERITED = SkSurface;
179 };
180 
getCachedCanvas()181 SkCanvas* SkSurface_Base::getCachedCanvas() {
182     if (nullptr == fCachedCanvas) {
183         fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
184         if (fCachedCanvas) {
185             fCachedCanvas->setSurfaceBase(this);
186         }
187     }
188     return fCachedCanvas.get();
189 }
190 
refCachedImage()191 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
192     if (fCachedImage) {
193         return fCachedImage;
194     }
195 
196     fCachedImage = this->onNewImageSnapshot();
197 
198     SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
199     return fCachedImage;
200 }
201 
202 #endif
203