blob: aedb364b4d88844e8f84f9087375b55a7db75667 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
2 * Copyright 2016 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 GrSurfaceProxy_DEFINED
9#define GrSurfaceProxy_DEFINED
10
11#include "GrGpuResource.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040012#include "GrSurface.h"
Robert Phillips93f16332016-11-23 19:37:13 -050013
csmartdaltonbf4a8f92016-09-06 10:01:06 -070014#include "SkRect.h"
robertphillips76948d42016-05-04 12:47:41 -070015
Robert Phillips37430132016-11-09 06:50:43 -050016class GrCaps;
Brian Osman45580d32016-11-23 09:37:01 -050017class GrRenderTargetOpList;
18class GrRenderTargetProxy;
Robert Phillipsd46697a2017-01-25 12:10:37 -050019class GrSurfaceContext;
Robert Phillips757914d2017-01-25 15:48:30 -050020class GrSurfaceProxyPriv;
Brian Osman45580d32016-11-23 09:37:01 -050021class GrTextureOpList;
Robert Phillipseaa86252016-11-08 13:49:39 +000022class GrTextureProvider;
robertphillips76948d42016-05-04 12:47:41 -070023class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070024
Robert Phillips7928e762017-02-28 16:30:28 -050025//#define SK_DISABLE_DEFERRED_PROXIES 1
26
Robert Phillipsc7635fa2016-10-28 13:25:24 -040027// This class replicates the functionality GrIORef<GrSurface> but tracks the
28// utilitization for later resource allocation (for the deferred case) and
29// forwards on the utilization in the wrapped case
30class GrIORefProxy : public SkNoncopyable {
31public:
32 void ref() const {
33 this->validate();
34
35 ++fRefCnt;
36 if (fTarget) {
37 fTarget->ref();
38 }
39 }
40
41 void unref() const {
42 this->validate();
43
44 if (fTarget) {
45 fTarget->unref();
46 }
47
48 if (!(--fRefCnt)) {
49 delete this;
50 return;
51 }
52
53 this->validate();
54 }
55
56 void validate() const {
57#ifdef SK_DEBUG
robertphillips1125a032016-11-16 11:17:17 -080058 SkASSERT(fRefCnt >= 1);
59 SkASSERT(fPendingReads >= 0);
60 SkASSERT(fPendingWrites >= 0);
61 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
62
63 if (fTarget) {
64 SkASSERT(!fPendingReads && !fPendingWrites);
65 // The backing GrSurface can have more refs than the proxy if the proxy
66 // started off wrapping an external resource (that came in with refs).
67 // The GrSurface should never have fewer refs than the proxy however.
68 SkASSERT(fTarget->fRefCnt >= fRefCnt);
69 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040070#endif
71 }
72
robertphillips1125a032016-11-16 11:17:17 -080073 int32_t getProxyRefCnt_TestOnly() const;
74 int32_t getBackingRefCnt_TestOnly() const;
75 int32_t getPendingReadCnt_TestOnly() const;
76 int32_t getPendingWriteCnt_TestOnly() const;
77
Robert Phillipsc7635fa2016-10-28 13:25:24 -040078protected:
robertphillips1125a032016-11-16 11:17:17 -080079 GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {}
80 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040081 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
82 // anything extra.
83 fTarget = surface.release();
84 }
85 virtual ~GrIORefProxy() {
86 // We don't unref 'fTarget' here since the 'unref' method will already
87 // have forwarded on the unref call that got use here.
88 }
89
robertphillips1125a032016-11-16 11:17:17 -080090 // This GrIORefProxy was deferred before but has just been instantiated. To
91 // make all the reffing & unreffing work out we now need to transfer any deferred
92 // refs & unrefs to the new GrSurface
93 void transferRefs() {
94 SkASSERT(fTarget);
95
96 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
97 fTarget->fPendingReads += fPendingReads;
98 fTarget->fPendingWrites += fPendingWrites;
99
100 fPendingReads = 0;
101 fPendingWrites = 0;
102 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400103
Robert Phillips757914d2017-01-25 15:48:30 -0500104 bool internalHasPendingIO() const {
105 if (fTarget) {
106 return fTarget->internalHasPendingIO();
107 }
108
109 return SkToBool(fPendingWrites | fPendingReads);
110 }
111
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400112 // For deferred proxies this will be null. For wrapped proxies it will point to the
113 // wrapped resource.
114 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800115
116private:
117 // This class is used to manage conversion of refs to pending reads/writes.
118 friend class GrGpuResourceRef;
119 template <typename, GrIOType> friend class GrPendingIOResource;
120
121 void addPendingRead() const {
122 this->validate();
123
124 if (fTarget) {
125 fTarget->addPendingRead();
126 return;
127 }
128
129 ++fPendingReads;
130 }
131
132 void completedRead() const {
133 this->validate();
134
135 if (fTarget) {
136 fTarget->completedRead();
137 return;
138 }
139
140 SkFAIL("How was the read completed if the Proxy hasn't been instantiated?");
141 }
142
143 void addPendingWrite() const {
144 this->validate();
145
146 if (fTarget) {
147 fTarget->addPendingWrite();
148 return;
149 }
150
151 ++fPendingWrites;
152 }
153
154 void completedWrite() const {
155 this->validate();
156
157 if (fTarget) {
158 fTarget->completedWrite();
159 return;
160 }
161
162 SkFAIL("How was the write completed if the Proxy hasn't been instantiated?");
163 }
164
165 mutable int32_t fRefCnt;
166 mutable int32_t fPendingReads;
167 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400168};
169
170class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700171public:
Robert Phillips37430132016-11-09 06:50:43 -0500172 static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>);
Robert Phillips63c67462017-02-15 14:19:01 -0500173 static sk_sp<GrTextureProxy> MakeWrapped(sk_sp<GrTexture>);
Robert Phillips37430132016-11-09 06:50:43 -0500174
Robert Phillips7928e762017-02-28 16:30:28 -0500175 static sk_sp<GrSurfaceProxy> MakeDeferred(GrTextureProvider*, const GrCaps&,
176 const GrSurfaceDesc&, SkBackingFit,
177 SkBudgeted, uint32_t flags = 0);
Robert Phillips37430132016-11-09 06:50:43 -0500178
179 // TODO: need to refine ownership semantics of 'srcData' if we're in completely
180 // deferred mode
181 static sk_sp<GrSurfaceProxy> MakeDeferred(const GrCaps&, GrTextureProvider*,
182 const GrSurfaceDesc&, SkBudgeted,
183 const void* srcData, size_t rowBytes);
184
Robert Phillips26caf892017-01-27 10:58:31 -0500185 static sk_sp<GrSurfaceProxy> MakeWrappedBackend(
186 GrContext*,
187 GrBackendTextureDesc&,
188 GrWrapOwnership ownership = kBorrow_GrWrapOwnership);
189
robertphillips76948d42016-05-04 12:47:41 -0700190 const GrSurfaceDesc& desc() const { return fDesc; }
191
192 GrSurfaceOrigin origin() const {
193 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
194 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
195 return fDesc.fOrigin;
196 }
197 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400198 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -0700199 GrPixelConfig config() const { return fDesc.fConfig; }
200
Robert Phillips294870f2016-11-11 12:38:40 -0500201 class UniqueID {
202 public:
203 // wrapped
204 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
205 // deferred
206 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
207
208 uint32_t asUInt() const { return fID; }
209
210 bool operator==(const UniqueID& other) const {
211 return fID == other.fID;
212 }
213 bool operator!=(const UniqueID& other) const {
214 return !(*this == other);
215 }
216
217 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
218
219 private:
220 const uint32_t fID;
221 };
222
223 /*
224 * The contract for the uniqueID is:
225 * for wrapped resources:
226 * the uniqueID will match that of the wrapped resource
227 *
228 * for deferred resources:
229 * the uniqueID will be different from the real resource, when it is allocated
230 * the proxy's uniqueID will not change across the instantiate call
231 *
232 * the uniqueIDs of the proxies and the resources draw from the same pool
233 *
234 * What this boils down to is that the uniqueID of a proxy can be used to consistently
235 * track/identify a proxy but should never be used to distinguish between
236 * resources and proxies - beware!
237 */
238 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700239
Robert Phillips37430132016-11-09 06:50:43 -0500240 GrSurface* instantiate(GrTextureProvider* texProvider);
241
robertphillips76948d42016-05-04 12:47:41 -0700242 /**
robertphillips13a7eee2016-08-31 15:06:24 -0700243 * Helper that gets the width and height of the surface as a bounding rectangle.
244 */
245 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
Robert Phillips784b7bf2016-12-09 13:35:02 -0500246
247 int worstCaseWidth(const GrCaps& caps) const;
248 int worstCaseHeight(const GrCaps& caps) const;
Robert Phillips93f16332016-11-23 19:37:13 -0500249
robertphillips13a7eee2016-08-31 15:06:24 -0700250 /**
robertphillips76948d42016-05-04 12:47:41 -0700251 * @return the texture proxy associated with the surface proxy, may be NULL.
252 */
253 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
254 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
255
256 /**
257 * @return the render target proxy associated with the surface proxy, may be NULL.
258 */
259 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
260 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
261
robertphillips13a7eee2016-08-31 15:06:24 -0700262 /**
263 * Does the resource count against the resource budget?
264 */
265 SkBudgeted isBudgeted() const { return fBudgeted; }
266
Robert Phillipsf2361d22016-10-25 14:20:06 -0400267 void setLastOpList(GrOpList* opList);
268 GrOpList* getLastOpList() { return fLastOpList; }
269
Brian Osman45580d32016-11-23 09:37:01 -0500270 GrRenderTargetOpList* getLastRenderTargetOpList();
271 GrTextureOpList* getLastTextureOpList();
272
Robert Phillips8bc06d02016-11-01 17:28:40 -0400273 /**
274 * Retrieves the amount of GPU memory that will be or currently is used by this resource
275 * in bytes. It is approximate since we aren't aware of additional padding or copies made
276 * by the driver.
277 *
278 * @return the amount of GPU memory used in bytes
279 */
280 size_t gpuMemorySize() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400281 if (kInvalidGpuMemorySize == fGpuMemorySize) {
282 fGpuMemorySize = this->onGpuMemorySize();
283 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
284 }
285 return fGpuMemorySize;
286 }
287
Robert Phillipse2f7d182016-12-15 09:23:05 -0500288 // Helper function that creates a temporary SurfaceContext to perform the copy
Robert Phillips63c67462017-02-15 14:19:01 -0500289 static sk_sp<GrTextureProxy> Copy(GrContext*, GrSurfaceProxy* src,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500290 SkIRect srcRect, SkBudgeted);
291
292 // Copy the entire 'src'
Robert Phillips63c67462017-02-15 14:19:01 -0500293 static sk_sp<GrTextureProxy> Copy(GrContext* context, GrSurfaceProxy* src,
294 SkBudgeted budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500295
296 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd46697a2017-01-25 12:10:37 -0500297 static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
298 GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500299
Robert Phillipseaa86252016-11-08 13:49:39 +0000300 bool isWrapped_ForTesting() const;
301
Brian Osman45580d32016-11-23 09:37:01 -0500302 SkDEBUGCODE(void validate(GrContext*) const;)
303
Robert Phillips757914d2017-01-25 15:48:30 -0500304 // Provides access to functions that aren't part of the public API.
305 GrSurfaceProxyPriv priv();
306 const GrSurfaceProxyPriv priv() const;
307
robertphillips76948d42016-05-04 12:47:41 -0700308protected:
robertphillips8abb3702016-08-31 14:04:06 -0700309 // Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -0500310 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted, uint32_t flags)
robertphillips76948d42016-05-04 12:47:41 -0700311 : fDesc(desc)
312 , fFit(fit)
313 , fBudgeted(budgeted)
Robert Phillipsc787e492017-02-28 11:26:32 -0500314 , fFlags(flags)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400315 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400316 , fLastOpList(nullptr) {
Robert Phillips294870f2016-11-11 12:38:40 -0500317 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700318 }
319
320 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400321 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700322
Robert Phillipsf2361d22016-10-25 14:20:06 -0400323 virtual ~GrSurfaceProxy();
324
Robert Phillips757914d2017-01-25 15:48:30 -0500325 friend class GrSurfaceProxyPriv;
326
327 // Methods made available via GrSurfaceProxyPriv
328 bool hasPendingIO() const {
329 return this->internalHasPendingIO();
330 }
331
robertphillips76948d42016-05-04 12:47:41 -0700332 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
Robert Phillips294870f2016-11-11 12:38:40 -0500333 const GrSurfaceDesc fDesc;
334 const SkBackingFit fFit; // always exact for wrapped resources
335 const SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
Robert Phillipsc787e492017-02-28 11:26:32 -0500336 const uint32_t fFlags;
Robert Phillips294870f2016-11-11 12:38:40 -0500337 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700338
Robert Phillips8bc06d02016-11-01 17:28:40 -0400339 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400340 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
341
342private:
343 virtual size_t onGpuMemorySize() const = 0;
344
Robert Phillips8bc06d02016-11-01 17:28:40 -0400345 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
346 // will be called but, when the proxy is deferred, it will compute the answer itself.
347 // If the proxy computes its own answer that answer is checked (in debug mode) in
348 // the instantiation method.
349 mutable size_t fGpuMemorySize;
350
Robert Phillipsf2361d22016-10-25 14:20:06 -0400351 // The last opList that wrote to or is currently going to write to this surface
Brian Osman11052242016-10-27 14:47:55 -0400352 // The opList can be closed (e.g., no render target context is currently bound
Robert Phillipsf2361d22016-10-25 14:20:06 -0400353 // to this renderTarget).
354 // This back-pointer is required so that we can add a dependancy between
355 // the opList used to create the current contents of this surface
356 // and the opList of a destination surface to which this one is being drawn or copied.
357 GrOpList* fLastOpList;
358
Robert Phillips29e52f12016-11-03 10:19:14 -0400359
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400360 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700361};
362
363#endif