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