blob: 9363b8280b6813820d0e6703fb91ccf050459e77 [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;
Brian Osman45580d32016-11-23 09:37:01 -050020class GrTextureOpList;
Robert Phillipseaa86252016-11-08 13:49:39 +000021class GrTextureProvider;
robertphillips76948d42016-05-04 12:47:41 -070022class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070023
Robert Phillipsc7635fa2016-10-28 13:25:24 -040024// This class replicates the functionality GrIORef<GrSurface> but tracks the
25// utilitization for later resource allocation (for the deferred case) and
26// forwards on the utilization in the wrapped case
27class GrIORefProxy : public SkNoncopyable {
28public:
29 void ref() const {
30 this->validate();
31
32 ++fRefCnt;
33 if (fTarget) {
34 fTarget->ref();
35 }
36 }
37
38 void unref() const {
39 this->validate();
40
41 if (fTarget) {
42 fTarget->unref();
43 }
44
45 if (!(--fRefCnt)) {
46 delete this;
47 return;
48 }
49
50 this->validate();
51 }
52
53 void validate() const {
54#ifdef SK_DEBUG
robertphillips1125a032016-11-16 11:17:17 -080055 SkASSERT(fRefCnt >= 1);
56 SkASSERT(fPendingReads >= 0);
57 SkASSERT(fPendingWrites >= 0);
58 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
59
60 if (fTarget) {
61 SkASSERT(!fPendingReads && !fPendingWrites);
62 // The backing GrSurface can have more refs than the proxy if the proxy
63 // started off wrapping an external resource (that came in with refs).
64 // The GrSurface should never have fewer refs than the proxy however.
65 SkASSERT(fTarget->fRefCnt >= fRefCnt);
66 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040067#endif
68 }
69
robertphillips1125a032016-11-16 11:17:17 -080070 int32_t getProxyRefCnt_TestOnly() const;
71 int32_t getBackingRefCnt_TestOnly() const;
72 int32_t getPendingReadCnt_TestOnly() const;
73 int32_t getPendingWriteCnt_TestOnly() const;
74
Robert Phillipsc7635fa2016-10-28 13:25:24 -040075protected:
robertphillips1125a032016-11-16 11:17:17 -080076 GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {}
77 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040078 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
79 // anything extra.
80 fTarget = surface.release();
81 }
82 virtual ~GrIORefProxy() {
83 // We don't unref 'fTarget' here since the 'unref' method will already
84 // have forwarded on the unref call that got use here.
85 }
86
robertphillips1125a032016-11-16 11:17:17 -080087 // This GrIORefProxy was deferred before but has just been instantiated. To
88 // make all the reffing & unreffing work out we now need to transfer any deferred
89 // refs & unrefs to the new GrSurface
90 void transferRefs() {
91 SkASSERT(fTarget);
92
93 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
94 fTarget->fPendingReads += fPendingReads;
95 fTarget->fPendingWrites += fPendingWrites;
96
97 fPendingReads = 0;
98 fPendingWrites = 0;
99 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400100
101 // For deferred proxies this will be null. For wrapped proxies it will point to the
102 // wrapped resource.
103 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800104
105private:
106 // This class is used to manage conversion of refs to pending reads/writes.
107 friend class GrGpuResourceRef;
108 template <typename, GrIOType> friend class GrPendingIOResource;
109
110 void addPendingRead() const {
111 this->validate();
112
113 if (fTarget) {
114 fTarget->addPendingRead();
115 return;
116 }
117
118 ++fPendingReads;
119 }
120
121 void completedRead() const {
122 this->validate();
123
124 if (fTarget) {
125 fTarget->completedRead();
126 return;
127 }
128
129 SkFAIL("How was the read completed if the Proxy hasn't been instantiated?");
130 }
131
132 void addPendingWrite() const {
133 this->validate();
134
135 if (fTarget) {
136 fTarget->addPendingWrite();
137 return;
138 }
139
140 ++fPendingWrites;
141 }
142
143 void completedWrite() const {
144 this->validate();
145
146 if (fTarget) {
147 fTarget->completedWrite();
148 return;
149 }
150
151 SkFAIL("How was the write completed if the Proxy hasn't been instantiated?");
152 }
153
154 mutable int32_t fRefCnt;
155 mutable int32_t fPendingReads;
156 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400157};
158
159class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700160public:
Robert Phillips37430132016-11-09 06:50:43 -0500161 static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>);
162
163 static sk_sp<GrSurfaceProxy> MakeDeferred(const GrCaps&, const GrSurfaceDesc&,
164 SkBackingFit, SkBudgeted);
165
166 // TODO: need to refine ownership semantics of 'srcData' if we're in completely
167 // deferred mode
168 static sk_sp<GrSurfaceProxy> MakeDeferred(const GrCaps&, GrTextureProvider*,
169 const GrSurfaceDesc&, SkBudgeted,
170 const void* srcData, size_t rowBytes);
171
robertphillips76948d42016-05-04 12:47:41 -0700172 const GrSurfaceDesc& desc() const { return fDesc; }
173
174 GrSurfaceOrigin origin() const {
175 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
176 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
177 return fDesc.fOrigin;
178 }
179 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400180 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -0700181 GrPixelConfig config() const { return fDesc.fConfig; }
182
Robert Phillips294870f2016-11-11 12:38:40 -0500183 class UniqueID {
184 public:
185 // wrapped
186 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
187 // deferred
188 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
189
190 uint32_t asUInt() const { return fID; }
191
192 bool operator==(const UniqueID& other) const {
193 return fID == other.fID;
194 }
195 bool operator!=(const UniqueID& other) const {
196 return !(*this == other);
197 }
198
199 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
200
201 private:
202 const uint32_t fID;
203 };
204
205 /*
206 * The contract for the uniqueID is:
207 * for wrapped resources:
208 * the uniqueID will match that of the wrapped resource
209 *
210 * for deferred resources:
211 * the uniqueID will be different from the real resource, when it is allocated
212 * the proxy's uniqueID will not change across the instantiate call
213 *
214 * the uniqueIDs of the proxies and the resources draw from the same pool
215 *
216 * What this boils down to is that the uniqueID of a proxy can be used to consistently
217 * track/identify a proxy but should never be used to distinguish between
218 * resources and proxies - beware!
219 */
220 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700221
Robert Phillips37430132016-11-09 06:50:43 -0500222 GrSurface* instantiate(GrTextureProvider* texProvider);
223
robertphillips76948d42016-05-04 12:47:41 -0700224 /**
robertphillips13a7eee2016-08-31 15:06:24 -0700225 * Helper that gets the width and height of the surface as a bounding rectangle.
226 */
227 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
Robert Phillips784b7bf2016-12-09 13:35:02 -0500228
229 int worstCaseWidth(const GrCaps& caps) const;
230 int worstCaseHeight(const GrCaps& caps) const;
Robert Phillips93f16332016-11-23 19:37:13 -0500231
robertphillips13a7eee2016-08-31 15:06:24 -0700232 /**
robertphillips76948d42016-05-04 12:47:41 -0700233 * @return the texture proxy associated with the surface proxy, may be NULL.
234 */
235 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
236 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
237
238 /**
239 * @return the render target proxy associated with the surface proxy, may be NULL.
240 */
241 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
242 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
243
robertphillips13a7eee2016-08-31 15:06:24 -0700244 /**
245 * Does the resource count against the resource budget?
246 */
247 SkBudgeted isBudgeted() const { return fBudgeted; }
248
Robert Phillipsf2361d22016-10-25 14:20:06 -0400249 void setLastOpList(GrOpList* opList);
250 GrOpList* getLastOpList() { return fLastOpList; }
251
Brian Osman45580d32016-11-23 09:37:01 -0500252 GrRenderTargetOpList* getLastRenderTargetOpList();
253 GrTextureOpList* getLastTextureOpList();
254
Robert Phillips8bc06d02016-11-01 17:28:40 -0400255 /**
256 * Retrieves the amount of GPU memory that will be or currently is used by this resource
257 * in bytes. It is approximate since we aren't aware of additional padding or copies made
258 * by the driver.
259 *
260 * @return the amount of GPU memory used in bytes
261 */
262 size_t gpuMemorySize() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400263 if (kInvalidGpuMemorySize == fGpuMemorySize) {
264 fGpuMemorySize = this->onGpuMemorySize();
265 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
266 }
267 return fGpuMemorySize;
268 }
269
Robert Phillipse2f7d182016-12-15 09:23:05 -0500270 // Helper function that creates a temporary SurfaceContext to perform the copy
271 static sk_sp<GrSurfaceProxy> Copy(GrContext*, GrSurfaceProxy* src,
272 SkIRect srcRect, SkBudgeted);
273
274 // Copy the entire 'src'
275 static sk_sp<GrSurfaceProxy> Copy(GrContext* context, GrSurfaceProxy* src,
276 SkBudgeted budgeted) {
277 return Copy(context, src, SkIRect::MakeWH(src->width(), src->height()), budgeted);
278 }
279
280 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd46697a2017-01-25 12:10:37 -0500281 static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
282 GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500283
Robert Phillipseaa86252016-11-08 13:49:39 +0000284 bool isWrapped_ForTesting() const;
285
Brian Osman45580d32016-11-23 09:37:01 -0500286 SkDEBUGCODE(void validate(GrContext*) const;)
287
robertphillips76948d42016-05-04 12:47:41 -0700288protected:
robertphillips8abb3702016-08-31 14:04:06 -0700289 // Deferred version
robertphillips76948d42016-05-04 12:47:41 -0700290 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted)
291 : fDesc(desc)
292 , fFit(fit)
293 , fBudgeted(budgeted)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400294 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400295 , fLastOpList(nullptr) {
Robert Phillips294870f2016-11-11 12:38:40 -0500296 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700297 }
298
299 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400300 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700301
Robert Phillipsf2361d22016-10-25 14:20:06 -0400302 virtual ~GrSurfaceProxy();
303
robertphillips76948d42016-05-04 12:47:41 -0700304 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
Robert Phillips294870f2016-11-11 12:38:40 -0500305 const GrSurfaceDesc fDesc;
306 const SkBackingFit fFit; // always exact for wrapped resources
307 const SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
308 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700309
Robert Phillips8bc06d02016-11-01 17:28:40 -0400310 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400311 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
312
313private:
314 virtual size_t onGpuMemorySize() const = 0;
315
Robert Phillips8bc06d02016-11-01 17:28:40 -0400316 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
317 // will be called but, when the proxy is deferred, it will compute the answer itself.
318 // If the proxy computes its own answer that answer is checked (in debug mode) in
319 // the instantiation method.
320 mutable size_t fGpuMemorySize;
321
Robert Phillipsf2361d22016-10-25 14:20:06 -0400322 // The last opList that wrote to or is currently going to write to this surface
Brian Osman11052242016-10-27 14:47:55 -0400323 // The opList can be closed (e.g., no render target context is currently bound
Robert Phillipsf2361d22016-10-25 14:20:06 -0400324 // to this renderTarget).
325 // This back-pointer is required so that we can add a dependancy between
326 // the opList used to create the current contents of this surface
327 // and the opList of a destination surface to which this one is being drawn or copied.
328 GrOpList* fLastOpList;
329
Robert Phillips29e52f12016-11-03 10:19:14 -0400330
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400331 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700332};
333
334#endif