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