2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkSurface_Base_DEFINED
9 #define SkSurface_Base_DEFINED
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"
17 class SkSurface_Base : public SkSurface {
19 SkSurface_Base(int width, int height, const SkSurfaceProps*);
20 SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
21 ~SkSurface_Base() override;
23 virtual GrRecordingContext* onGetRecordingContext();
24 virtual skgpu::graphite::Recorder* onGetRecorder();
27 virtual GrBackendTexture onGetBackendTexture(BackendHandleAccess);
28 virtual GrBackendRenderTarget onGetBackendRenderTarget(BackendHandleAccess);
29 virtual bool onReplaceBackendTexture(const GrBackendTexture&,
35 virtual void onResolveMSAA() {}
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.
42 virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, const GrFlushInfo&,
43 const GrBackendSurfaceMutableState*) {
44 return GrSemaphoresSubmitted::kNo;
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
54 virtual SkCanvas* onNewCanvas() = 0;
56 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
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).
64 * If a subset is specified, the the impl must make a copy, rather than try to wait
67 virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
69 virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
72 * Default implementation does a rescale/read and then calls the callback.
74 virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
75 const SkIRect& srcRect,
81 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
83 virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
84 sk_sp<SkColorSpace> dstColorSpace,
85 const SkIRect& srcRect,
86 const SkISize& dstSize,
93 * Default implementation:
95 * image = this->newImageSnapshot();
97 * image->draw(canvas, ...);
101 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkSamplingOptions&,const SkPaint*);
104 * Called as a performance hint when the Surface is allowed to make it's contents
107 virtual void onDiscard() {}
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.
114 * Returns false if the backing cannot be un-shared.
116 virtual bool SK_WARN_UNUSED_RESULT onCopyOnWrite(ContentChangeMode) = 0;
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.
122 virtual void onRestoreBackingMutability() {}
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
129 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
130 bool deleteSemaphoresAfterWait) {
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) {
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();
144 inline SkCanvas* getCachedCanvas();
145 inline sk_sp<SkImage> refCachedImage();
147 bool hasCachedImage() const { return fCachedImage != nullptr; }
149 // called by SkSurface to compute a new genID
150 uint32_t newGenerationID();
153 std::unique_ptr<SkCanvas> fCachedCanvas;
154 sk_sp<SkImage> fCachedImage;
156 // Returns false if drawing should not take place (allocation failure).
157 bool SK_WARN_UNUSED_RESULT aboutToDraw(ContentChangeMode mode);
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;
163 friend class SkCanvas;
164 friend class SkSurface;
166 using INHERITED = SkSurface;
169 SkCanvas* SkSurface_Base::getCachedCanvas() {
170 if (nullptr == fCachedCanvas) {
171 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
173 fCachedCanvas->setSurfaceBase(this);
176 return fCachedCanvas.get();
179 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
184 fCachedImage = this->onNewImageSnapshot();
186 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);