blob: 4f0ab964d5c5776950181b42037a2c53f6e1b8e6 [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;
Robert Phillipsc589b0b2017-04-17 07:53:07 -040017class GrOpList;
Brian Osman45580d32016-11-23 09:37:01 -050018class GrRenderTargetOpList;
19class GrRenderTargetProxy;
Brian Osman32342f02017-03-04 08:12:46 -050020class GrResourceProvider;
Robert Phillipsd46697a2017-01-25 12:10:37 -050021class GrSurfaceContext;
Robert Phillips757914d2017-01-25 15:48:30 -050022class GrSurfaceProxyPriv;
Brian Osman45580d32016-11-23 09:37:01 -050023class GrTextureOpList;
robertphillips76948d42016-05-04 12:47:41 -070024class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070025
Robert Phillips123b7b82017-04-11 09:09:24 -040026//#define SK_DISABLE_DEFERRED_PROXIES 1
27
Robert Phillipsc7635fa2016-10-28 13:25:24 -040028// This class replicates the functionality GrIORef<GrSurface> but tracks the
29// utilitization for later resource allocation (for the deferred case) and
30// forwards on the utilization in the wrapped case
31class GrIORefProxy : public SkNoncopyable {
32public:
33 void ref() const {
34 this->validate();
35
36 ++fRefCnt;
37 if (fTarget) {
38 fTarget->ref();
39 }
40 }
41
42 void unref() const {
43 this->validate();
44
45 if (fTarget) {
46 fTarget->unref();
47 }
48
49 if (!(--fRefCnt)) {
50 delete this;
51 return;
52 }
53
54 this->validate();
55 }
56
57 void validate() const {
58#ifdef SK_DEBUG
robertphillips1125a032016-11-16 11:17:17 -080059 SkASSERT(fRefCnt >= 1);
60 SkASSERT(fPendingReads >= 0);
61 SkASSERT(fPendingWrites >= 0);
62 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
63
64 if (fTarget) {
65 SkASSERT(!fPendingReads && !fPendingWrites);
66 // The backing GrSurface can have more refs than the proxy if the proxy
67 // started off wrapping an external resource (that came in with refs).
68 // The GrSurface should never have fewer refs than the proxy however.
69 SkASSERT(fTarget->fRefCnt >= fRefCnt);
70 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040071#endif
72 }
73
robertphillips1125a032016-11-16 11:17:17 -080074 int32_t getProxyRefCnt_TestOnly() const;
75 int32_t getBackingRefCnt_TestOnly() const;
76 int32_t getPendingReadCnt_TestOnly() const;
77 int32_t getPendingWriteCnt_TestOnly() const;
78
Robert Phillipsc7635fa2016-10-28 13:25:24 -040079protected:
robertphillips1125a032016-11-16 11:17:17 -080080 GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {}
81 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040082 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
83 // anything extra.
84 fTarget = surface.release();
85 }
86 virtual ~GrIORefProxy() {
87 // We don't unref 'fTarget' here since the 'unref' method will already
88 // have forwarded on the unref call that got use here.
89 }
90
robertphillips1125a032016-11-16 11:17:17 -080091 // This GrIORefProxy was deferred before but has just been instantiated. To
92 // make all the reffing & unreffing work out we now need to transfer any deferred
93 // refs & unrefs to the new GrSurface
94 void transferRefs() {
95 SkASSERT(fTarget);
96
97 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
98 fTarget->fPendingReads += fPendingReads;
99 fTarget->fPendingWrites += fPendingWrites;
100
101 fPendingReads = 0;
102 fPendingWrites = 0;
103 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400104
Robert Phillips757914d2017-01-25 15:48:30 -0500105 bool internalHasPendingIO() const {
106 if (fTarget) {
107 return fTarget->internalHasPendingIO();
108 }
109
110 return SkToBool(fPendingWrites | fPendingReads);
111 }
112
Robert Phillips7ee385e2017-03-30 08:02:11 -0400113 bool internalHasPendingWrite() const {
114 if (fTarget) {
115 return fTarget->internalHasPendingWrite();
116 }
117
118 return SkToBool(fPendingWrites);
119 }
120
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400121 // For deferred proxies this will be null. For wrapped proxies it will point to the
122 // wrapped resource.
123 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800124
125private:
126 // This class is used to manage conversion of refs to pending reads/writes.
127 friend class GrGpuResourceRef;
128 template <typename, GrIOType> friend class GrPendingIOResource;
129
130 void addPendingRead() const {
131 this->validate();
132
133 if (fTarget) {
134 fTarget->addPendingRead();
135 return;
136 }
137
138 ++fPendingReads;
139 }
140
141 void completedRead() const {
142 this->validate();
143
144 if (fTarget) {
145 fTarget->completedRead();
146 return;
147 }
148
149 SkFAIL("How was the read completed if the Proxy hasn't been instantiated?");
150 }
151
152 void addPendingWrite() const {
153 this->validate();
154
155 if (fTarget) {
156 fTarget->addPendingWrite();
157 return;
158 }
159
160 ++fPendingWrites;
161 }
162
163 void completedWrite() const {
164 this->validate();
165
166 if (fTarget) {
167 fTarget->completedWrite();
168 return;
169 }
170
171 SkFAIL("How was the write completed if the Proxy hasn't been instantiated?");
172 }
173
174 mutable int32_t fRefCnt;
175 mutable int32_t fPendingReads;
176 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400177};
178
179class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700180public:
Robert Phillips37430132016-11-09 06:50:43 -0500181 static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>);
Robert Phillips63c67462017-02-15 14:19:01 -0500182 static sk_sp<GrTextureProxy> MakeWrapped(sk_sp<GrTexture>);
Robert Phillips37430132016-11-09 06:50:43 -0500183
Robert Phillips26c90e02017-03-14 14:39:29 -0400184 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips7928e762017-02-28 16:30:28 -0500185 const GrSurfaceDesc&, SkBackingFit,
186 SkBudgeted, uint32_t flags = 0);
Robert Phillips37430132016-11-09 06:50:43 -0500187
188 // TODO: need to refine ownership semantics of 'srcData' if we're in completely
189 // deferred mode
Robert Phillips26c90e02017-03-14 14:39:29 -0400190 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips37430132016-11-09 06:50:43 -0500191 const GrSurfaceDesc&, SkBudgeted,
192 const void* srcData, size_t rowBytes);
193
Robert Phillipsd3749482017-03-14 09:17:43 -0400194 static sk_sp<GrSurfaceProxy> MakeWrappedBackend(GrContext*, GrBackendTextureDesc&);
Robert Phillips26caf892017-01-27 10:58:31 -0500195
robertphillips76948d42016-05-04 12:47:41 -0700196 const GrSurfaceDesc& desc() const { return fDesc; }
197
198 GrSurfaceOrigin origin() const {
199 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
200 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
201 return fDesc.fOrigin;
202 }
203 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400204 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -0700205 GrPixelConfig config() const { return fDesc.fConfig; }
206
Robert Phillips294870f2016-11-11 12:38:40 -0500207 class UniqueID {
208 public:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400209 static UniqueID InvalidID() {
210 return UniqueID(uint32_t(SK_InvalidUniqueID));
211 }
212
Robert Phillips294870f2016-11-11 12:38:40 -0500213 // wrapped
214 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
215 // deferred
216 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
217
218 uint32_t asUInt() const { return fID; }
219
220 bool operator==(const UniqueID& other) const {
221 return fID == other.fID;
222 }
223 bool operator!=(const UniqueID& other) const {
224 return !(*this == other);
225 }
226
Robert Phillips7ee385e2017-03-30 08:02:11 -0400227 void makeInvalid() { fID = SK_InvalidUniqueID; }
Robert Phillips294870f2016-11-11 12:38:40 -0500228 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
229
230 private:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400231 explicit UniqueID(uint32_t id) : fID(id) {}
232
233 uint32_t fID;
Robert Phillips294870f2016-11-11 12:38:40 -0500234 };
235
236 /*
237 * The contract for the uniqueID is:
238 * for wrapped resources:
239 * the uniqueID will match that of the wrapped resource
240 *
241 * for deferred resources:
242 * the uniqueID will be different from the real resource, when it is allocated
243 * the proxy's uniqueID will not change across the instantiate call
244 *
245 * the uniqueIDs of the proxies and the resources draw from the same pool
246 *
247 * What this boils down to is that the uniqueID of a proxy can be used to consistently
248 * track/identify a proxy but should never be used to distinguish between
249 * resources and proxies - beware!
250 */
251 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700252
Brian Osman32342f02017-03-04 08:12:46 -0500253 GrSurface* instantiate(GrResourceProvider* resourceProvider);
Robert Phillips37430132016-11-09 06:50:43 -0500254
robertphillips76948d42016-05-04 12:47:41 -0700255 /**
robertphillips13a7eee2016-08-31 15:06:24 -0700256 * Helper that gets the width and height of the surface as a bounding rectangle.
257 */
258 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
Robert Phillips784b7bf2016-12-09 13:35:02 -0500259
260 int worstCaseWidth(const GrCaps& caps) const;
261 int worstCaseHeight(const GrCaps& caps) const;
Robert Phillips93f16332016-11-23 19:37:13 -0500262
robertphillips13a7eee2016-08-31 15:06:24 -0700263 /**
robertphillips76948d42016-05-04 12:47:41 -0700264 * @return the texture proxy associated with the surface proxy, may be NULL.
265 */
266 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
267 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
268
269 /**
270 * @return the render target proxy associated with the surface proxy, may be NULL.
271 */
272 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
273 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
274
robertphillips13a7eee2016-08-31 15:06:24 -0700275 /**
276 * Does the resource count against the resource budget?
277 */
278 SkBudgeted isBudgeted() const { return fBudgeted; }
279
Robert Phillipsf2361d22016-10-25 14:20:06 -0400280 void setLastOpList(GrOpList* opList);
281 GrOpList* getLastOpList() { return fLastOpList; }
282
Brian Osman45580d32016-11-23 09:37:01 -0500283 GrRenderTargetOpList* getLastRenderTargetOpList();
284 GrTextureOpList* getLastTextureOpList();
285
Robert Phillips8bc06d02016-11-01 17:28:40 -0400286 /**
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400287 * Retrieves the amount of GPU memory that will be or currently is used by this resource
Robert Phillips8bc06d02016-11-01 17:28:40 -0400288 * in bytes. It is approximate since we aren't aware of additional padding or copies made
289 * by the driver.
290 *
291 * @return the amount of GPU memory used in bytes
292 */
293 size_t gpuMemorySize() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400294 if (kInvalidGpuMemorySize == fGpuMemorySize) {
295 fGpuMemorySize = this->onGpuMemorySize();
296 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
297 }
298 return fGpuMemorySize;
299 }
300
Robert Phillipse2f7d182016-12-15 09:23:05 -0500301 // Helper function that creates a temporary SurfaceContext to perform the copy
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400302 // It always returns a kExact-backed proxy bc it is used when converting an SkSpecialImage
303 // to an SkImage.
Robert Phillips63c67462017-02-15 14:19:01 -0500304 static sk_sp<GrTextureProxy> Copy(GrContext*, GrSurfaceProxy* src,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500305 SkIRect srcRect, SkBudgeted);
306
307 // Copy the entire 'src'
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400308 // It always returns a kExact-backed proxy bc it is used in SkGpuDevice::snapSpecial
Robert Phillips63c67462017-02-15 14:19:01 -0500309 static sk_sp<GrTextureProxy> Copy(GrContext* context, GrSurfaceProxy* src,
310 SkBudgeted budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500311
312 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd46697a2017-01-25 12:10:37 -0500313 static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
314 GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500315
Robert Phillipseaa86252016-11-08 13:49:39 +0000316 bool isWrapped_ForTesting() const;
317
Brian Osman45580d32016-11-23 09:37:01 -0500318 SkDEBUGCODE(void validate(GrContext*) const;)
319
Robert Phillips757914d2017-01-25 15:48:30 -0500320 // Provides access to functions that aren't part of the public API.
321 GrSurfaceProxyPriv priv();
322 const GrSurfaceProxyPriv priv() const;
323
robertphillips76948d42016-05-04 12:47:41 -0700324protected:
robertphillips8abb3702016-08-31 14:04:06 -0700325 // Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -0500326 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted, uint32_t flags)
robertphillips76948d42016-05-04 12:47:41 -0700327 : fDesc(desc)
328 , fFit(fit)
329 , fBudgeted(budgeted)
Robert Phillipsc787e492017-02-28 11:26:32 -0500330 , fFlags(flags)
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400331 // fMipColorMode is only valid for texturable proxies
332 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400333 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400334 , fLastOpList(nullptr) {
Robert Phillips294870f2016-11-11 12:38:40 -0500335 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700336 }
337
338 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400339 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700340
Robert Phillipsf2361d22016-10-25 14:20:06 -0400341 virtual ~GrSurfaceProxy();
342
Robert Phillips757914d2017-01-25 15:48:30 -0500343 friend class GrSurfaceProxyPriv;
344
345 // Methods made available via GrSurfaceProxyPriv
346 bool hasPendingIO() const {
347 return this->internalHasPendingIO();
348 }
349
Robert Phillips7ee385e2017-03-30 08:02:11 -0400350 bool hasPendingWrite() const {
351 return this->internalHasPendingWrite();
352 }
353
robertphillips76948d42016-05-04 12:47:41 -0700354 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400355 GrSurfaceDesc fDesc;
356 SkBackingFit fFit; // always exact for wrapped resources
Robert Phillipsb726d582017-03-09 16:36:32 -0500357 mutable SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
358 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsc787e492017-02-28 11:26:32 -0500359 const uint32_t fFlags;
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400360
361 SkDestinationSurfaceColorMode fMipColorMode;
362
Robert Phillips294870f2016-11-11 12:38:40 -0500363 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700364
Robert Phillips8bc06d02016-11-01 17:28:40 -0400365 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400366 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
367
368private:
369 virtual size_t onGpuMemorySize() const = 0;
370
Robert Phillips8bc06d02016-11-01 17:28:40 -0400371 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
372 // will be called but, when the proxy is deferred, it will compute the answer itself.
373 // If the proxy computes its own answer that answer is checked (in debug mode) in
374 // the instantiation method.
375 mutable size_t fGpuMemorySize;
376
Robert Phillipsf2361d22016-10-25 14:20:06 -0400377 // The last opList that wrote to or is currently going to write to this surface
Brian Osman11052242016-10-27 14:47:55 -0400378 // The opList can be closed (e.g., no render target context is currently bound
Robert Phillipsf2361d22016-10-25 14:20:06 -0400379 // to this renderTarget).
380 // This back-pointer is required so that we can add a dependancy between
381 // the opList used to create the current contents of this surface
382 // and the opList of a destination surface to which this one is being drawn or copied.
383 GrOpList* fLastOpList;
384
Robert Phillips29e52f12016-11-03 10:19:14 -0400385
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400386 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700387};
388
389#endif