• 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 "SkCanvas.h"
12 #include "SkImagePriv.h"
13 #include "SkSurface.h"
14 #include "SkSurfacePriv.h"
15 
16 class SkSurface_Base : public SkSurface {
17 public:
18     SkSurface_Base(int width, int height, const SkSurfaceProps*);
19     SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
20     virtual ~SkSurface_Base();
21 
22     virtual GrBackendTexture onGetBackendTexture(BackendHandleAccess);
23     virtual GrBackendRenderTarget onGetBackendRenderTarget(BackendHandleAccess);
24 
25     /**
26      *  Allocate a canvas that will draw into this surface. We will cache this
27      *  canvas, to return the same object to the caller multiple times. We
28      *  take ownership, and will call unref() on the canvas when we go out of
29      *  scope.
30      */
31     virtual SkCanvas* onNewCanvas() = 0;
32 
33     virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
34 
35     /**
36      *  Allocate an SkImage that represents the current contents of the surface.
37      *  This needs to be able to outlive the surface itself (if need be), and
38      *  must faithfully represent the current contents, even if the surface
39      *  is changed after this called (e.g. it is drawn to via its canvas).
40      *
41      *  If a subset is specified, the the impl must make a copy, rather than try to wait
42      *  on copy-on-write.
43      */
44     virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
45 
46     virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
47 
48     /**
49      *  Default implementation:
50      *
51      *  image = this->newImageSnapshot();
52      *  if (image) {
53      *      image->draw(canvas, ...);
54      *      image->unref();
55      *  }
56      */
57     virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
58 
59     /**
60      * Called as a performance hint when the Surface is allowed to make it's contents
61      * undefined.
62      */
onDiscard()63     virtual void onDiscard() {}
64 
65     /**
66      *  If the surface is about to change, we call this so that our subclass
67      *  can optionally fork their backend (copy-on-write) in case it was
68      *  being shared with the cachedImage.
69      */
70     virtual void onCopyOnWrite(ContentChangeMode) = 0;
71 
72     /**
73      *  Signal the surface to remind its backing store that it's mutable again.
74      *  Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
75      */
onRestoreBackingMutability()76     virtual void onRestoreBackingMutability() {}
77 
78     /**
79      * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
80      * Inserts the requested number of semaphores for the gpu to signal when work is complete on the
81      * gpu and inits the array of GrBackendSemaphores with the signaled semaphores.
82      */
onFlush(int numSemaphores,GrBackendSemaphore signalSemaphores[])83     virtual GrSemaphoresSubmitted onFlush(int numSemaphores,
84                                           GrBackendSemaphore signalSemaphores[]) {
85         return GrSemaphoresSubmitted::kNo;
86     }
87 
88     /**
89      * Caused the current backend 3D API to wait on the passed in semaphores before executing new
90      * commands on the gpu. Any previously submitting commands will not be blocked by these
91      * semaphores.
92      */
onWait(int numSemaphores,const GrBackendSemaphore * waitSemaphores)93     virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
94         return false;
95     }
96 
onCharacterize(SkSurfaceCharacterization *)97     virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
onDraw(const SkDeferredDisplayList *)98     virtual bool onDraw(const SkDeferredDisplayList*) { return false; }
99 
100     inline SkCanvas* getCachedCanvas();
101     inline sk_sp<SkImage> refCachedImage();
102 
hasCachedImage()103     bool hasCachedImage() const { return fCachedImage != nullptr; }
104 
105     // called by SkSurface to compute a new genID
106     uint32_t newGenerationID();
107 
108 private:
109     std::unique_ptr<SkCanvas>   fCachedCanvas;
110     sk_sp<SkImage>              fCachedImage;
111 
112     void aboutToDraw(ContentChangeMode mode);
113 
114     // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
115     // would trigger a copy-on-write.
116     bool outstandingImageSnapshot() const;
117 
118     friend class SkCanvas;
119     friend class SkSurface;
120 
121     typedef SkSurface INHERITED;
122 };
123 
getCachedCanvas()124 SkCanvas* SkSurface_Base::getCachedCanvas() {
125     if (nullptr == fCachedCanvas) {
126         fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
127         if (fCachedCanvas) {
128             fCachedCanvas->setSurfaceBase(this);
129         }
130     }
131     return fCachedCanvas.get();
132 }
133 
refCachedImage()134 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
135     if (fCachedImage) {
136         return fCachedImage;
137     }
138 
139     fCachedImage = this->onNewImageSnapshot();
140 
141     SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
142     return fCachedImage;
143 }
144 
145 #endif
146