blob: bded2c1be6caeb13878157515038e716d9892ffb [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
Greg Daniel7ef28f32017-04-20 16:41:55 +000016class GrBackendTexture;
Robert Phillips37430132016-11-09 06:50:43 -050017class GrCaps;
Robert Phillipsc589b0b2017-04-17 07:53:07 -040018class GrOpList;
Brian Osman45580d32016-11-23 09:37:01 -050019class GrRenderTargetOpList;
20class GrRenderTargetProxy;
Brian Osman32342f02017-03-04 08:12:46 -050021class GrResourceProvider;
Robert Phillipsd46697a2017-01-25 12:10:37 -050022class GrSurfaceContext;
Robert Phillips757914d2017-01-25 15:48:30 -050023class GrSurfaceProxyPriv;
Brian Osman45580d32016-11-23 09:37:01 -050024class GrTextureOpList;
robertphillips76948d42016-05-04 12:47:41 -070025class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070026
Robert Phillips8bf1f9f2017-05-31 15:01:20 -040027//#define SK_DISABLE_DEFERRED_PROXIES 1
28
Robert Phillipsc7635fa2016-10-28 13:25:24 -040029// This class replicates the functionality GrIORef<GrSurface> but tracks the
30// utilitization for later resource allocation (for the deferred case) and
31// forwards on the utilization in the wrapped case
32class GrIORefProxy : public SkNoncopyable {
33public:
34 void ref() const {
35 this->validate();
36
37 ++fRefCnt;
38 if (fTarget) {
39 fTarget->ref();
40 }
41 }
42
43 void unref() const {
44 this->validate();
45
46 if (fTarget) {
47 fTarget->unref();
48 }
49
Robert Phillipsb6deea82017-05-11 14:14:30 -040050 --fRefCnt;
51 this->didRemoveRefOrPendingIO();
Robert Phillipsc7635fa2016-10-28 13:25:24 -040052 }
53
54 void validate() const {
Robert Phillipsb6deea82017-05-11 14:14:30 -040055#ifdef SK_DEBUG
56 SkASSERT(fRefCnt >= 0);
robertphillips1125a032016-11-16 11:17:17 -080057 SkASSERT(fPendingReads >= 0);
58 SkASSERT(fPendingWrites >= 0);
59 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
60
61 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080062 // 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);
Robert Phillipsb6deea82017-05-11 14:14:30 -040066 SkASSERT(fTarget->fPendingReads >= fPendingReads);
67 SkASSERT(fTarget->fPendingWrites >= fPendingWrites);
robertphillips1125a032016-11-16 11:17:17 -080068 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040069#endif
70 }
71
robertphillips1125a032016-11-16 11:17:17 -080072 int32_t getProxyRefCnt_TestOnly() const;
73 int32_t getBackingRefCnt_TestOnly() const;
74 int32_t getPendingReadCnt_TestOnly() const;
75 int32_t getPendingWriteCnt_TestOnly() const;
76
Robert Phillipsc7635fa2016-10-28 13:25:24 -040077protected:
robertphillips1125a032016-11-16 11:17:17 -080078 GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {}
79 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040080 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
81 // anything extra.
82 fTarget = surface.release();
83 }
84 virtual ~GrIORefProxy() {
85 // We don't unref 'fTarget' here since the 'unref' method will already
86 // have forwarded on the unref call that got use here.
87 }
88
robertphillips1125a032016-11-16 11:17:17 -080089 // This GrIORefProxy was deferred before but has just been instantiated. To
90 // make all the reffing & unreffing work out we now need to transfer any deferred
91 // refs & unrefs to the new GrSurface
92 void transferRefs() {
93 SkASSERT(fTarget);
94
95 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
96 fTarget->fPendingReads += fPendingReads;
97 fTarget->fPendingWrites += fPendingWrites;
robertphillips1125a032016-11-16 11:17:17 -080098 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040099
Robert Phillips757914d2017-01-25 15:48:30 -0500100 bool internalHasPendingIO() const {
101 if (fTarget) {
102 return fTarget->internalHasPendingIO();
103 }
104
105 return SkToBool(fPendingWrites | fPendingReads);
106 }
107
Robert Phillips7ee385e2017-03-30 08:02:11 -0400108 bool internalHasPendingWrite() const {
109 if (fTarget) {
110 return fTarget->internalHasPendingWrite();
111 }
112
113 return SkToBool(fPendingWrites);
114 }
115
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400116 // For deferred proxies this will be null. For wrapped proxies it will point to the
117 // wrapped resource.
118 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800119
120private:
121 // This class is used to manage conversion of refs to pending reads/writes.
Robert Phillipsb6deea82017-05-11 14:14:30 -0400122 friend class GrSurfaceProxyRef;
robertphillips1125a032016-11-16 11:17:17 -0800123 template <typename, GrIOType> friend class GrPendingIOResource;
124
125 void addPendingRead() const {
126 this->validate();
127
Robert Phillipsb6deea82017-05-11 14:14:30 -0400128 ++fPendingReads;
robertphillips1125a032016-11-16 11:17:17 -0800129 if (fTarget) {
130 fTarget->addPendingRead();
robertphillips1125a032016-11-16 11:17:17 -0800131 }
robertphillips1125a032016-11-16 11:17:17 -0800132 }
133
134 void completedRead() const {
135 this->validate();
136
137 if (fTarget) {
138 fTarget->completedRead();
robertphillips1125a032016-11-16 11:17:17 -0800139 }
Robert Phillipsb6deea82017-05-11 14:14:30 -0400140
141 --fPendingReads;
142 this->didRemoveRefOrPendingIO();
robertphillips1125a032016-11-16 11:17:17 -0800143 }
144
145 void addPendingWrite() const {
146 this->validate();
147
Robert Phillipsb6deea82017-05-11 14:14:30 -0400148 ++fPendingWrites;
robertphillips1125a032016-11-16 11:17:17 -0800149 if (fTarget) {
150 fTarget->addPendingWrite();
robertphillips1125a032016-11-16 11:17:17 -0800151 }
robertphillips1125a032016-11-16 11:17:17 -0800152 }
153
154 void completedWrite() const {
155 this->validate();
156
157 if (fTarget) {
158 fTarget->completedWrite();
robertphillips1125a032016-11-16 11:17:17 -0800159 }
Robert Phillipsb6deea82017-05-11 14:14:30 -0400160
161 --fPendingWrites;
162 this->didRemoveRefOrPendingIO();
163 }
164
165 void didRemoveRefOrPendingIO() const {
166 if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
167 delete this;
168 }
robertphillips1125a032016-11-16 11:17:17 -0800169 }
170
171 mutable int32_t fRefCnt;
172 mutable int32_t fPendingReads;
173 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400174};
175
176class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700177public:
Robert Phillips37430132016-11-09 06:50:43 -0500178 static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>);
Robert Phillips63c67462017-02-15 14:19:01 -0500179 static sk_sp<GrTextureProxy> MakeWrapped(sk_sp<GrTexture>);
Robert Phillips37430132016-11-09 06:50:43 -0500180
Robert Phillips26c90e02017-03-14 14:39:29 -0400181 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips7928e762017-02-28 16:30:28 -0500182 const GrSurfaceDesc&, SkBackingFit,
183 SkBudgeted, uint32_t flags = 0);
Robert Phillips37430132016-11-09 06:50:43 -0500184
185 // TODO: need to refine ownership semantics of 'srcData' if we're in completely
186 // deferred mode
Robert Phillips26c90e02017-03-14 14:39:29 -0400187 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips37430132016-11-09 06:50:43 -0500188 const GrSurfaceDesc&, SkBudgeted,
189 const void* srcData, size_t rowBytes);
190
Greg Daniel7ef28f32017-04-20 16:41:55 +0000191 static sk_sp<GrTextureProxy> MakeWrappedBackend(GrContext*, GrBackendTexture&, GrSurfaceOrigin);
Robert Phillips26caf892017-01-27 10:58:31 -0500192
robertphillips76948d42016-05-04 12:47:41 -0700193 GrSurfaceOrigin origin() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400194 SkASSERT(kTopLeft_GrSurfaceOrigin == fOrigin || kBottomLeft_GrSurfaceOrigin == fOrigin);
195 return fOrigin;
robertphillips76948d42016-05-04 12:47:41 -0700196 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400197 int width() const { return fWidth; }
198 int height() const { return fHeight; }
199 GrPixelConfig config() const { return fConfig; }
robertphillips76948d42016-05-04 12:47:41 -0700200
Robert Phillips294870f2016-11-11 12:38:40 -0500201 class UniqueID {
202 public:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400203 static UniqueID InvalidID() {
204 return UniqueID(uint32_t(SK_InvalidUniqueID));
205 }
206
Robert Phillips294870f2016-11-11 12:38:40 -0500207 // wrapped
208 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
209 // deferred
210 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
211
212 uint32_t asUInt() const { return fID; }
213
214 bool operator==(const UniqueID& other) const {
215 return fID == other.fID;
216 }
217 bool operator!=(const UniqueID& other) const {
218 return !(*this == other);
219 }
220
Robert Phillips7ee385e2017-03-30 08:02:11 -0400221 void makeInvalid() { fID = SK_InvalidUniqueID; }
Robert Phillips294870f2016-11-11 12:38:40 -0500222 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
223
224 private:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400225 explicit UniqueID(uint32_t id) : fID(id) {}
226
227 uint32_t fID;
Robert Phillips294870f2016-11-11 12:38:40 -0500228 };
229
230 /*
231 * The contract for the uniqueID is:
232 * for wrapped resources:
233 * the uniqueID will match that of the wrapped resource
234 *
235 * for deferred resources:
236 * the uniqueID will be different from the real resource, when it is allocated
237 * the proxy's uniqueID will not change across the instantiate call
238 *
239 * the uniqueIDs of the proxies and the resources draw from the same pool
240 *
241 * What this boils down to is that the uniqueID of a proxy can be used to consistently
242 * track/identify a proxy but should never be used to distinguish between
243 * resources and proxies - beware!
244 */
245 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700246
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400247 virtual bool instantiate(GrResourceProvider* resourceProvider) = 0;
Robert Phillips37430132016-11-09 06:50:43 -0500248
robertphillips76948d42016-05-04 12:47:41 -0700249 /**
robertphillips13a7eee2016-08-31 15:06:24 -0700250 * Helper that gets the width and height of the surface as a bounding rectangle.
251 */
252 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
Robert Phillips784b7bf2016-12-09 13:35:02 -0500253
robertphillips13a7eee2016-08-31 15:06:24 -0700254 /**
robertphillips76948d42016-05-04 12:47:41 -0700255 * @return the texture proxy associated with the surface proxy, may be NULL.
256 */
257 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
258 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
259
260 /**
261 * @return the render target proxy associated with the surface proxy, may be NULL.
262 */
263 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
264 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
265
robertphillips13a7eee2016-08-31 15:06:24 -0700266 /**
267 * Does the resource count against the resource budget?
268 */
269 SkBudgeted isBudgeted() const { return fBudgeted; }
270
Robert Phillipsf2361d22016-10-25 14:20:06 -0400271 void setLastOpList(GrOpList* opList);
272 GrOpList* getLastOpList() { return fLastOpList; }
273
Brian Osman45580d32016-11-23 09:37:01 -0500274 GrRenderTargetOpList* getLastRenderTargetOpList();
275 GrTextureOpList* getLastTextureOpList();
276
Robert Phillips8bc06d02016-11-01 17:28:40 -0400277 /**
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400278 * Retrieves the amount of GPU memory that will be or currently is used by this resource
Robert Phillips8bc06d02016-11-01 17:28:40 -0400279 * in bytes. It is approximate since we aren't aware of additional padding or copies made
280 * by the driver.
281 *
282 * @return the amount of GPU memory used in bytes
283 */
284 size_t gpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400285 if (fTarget) {
286 return fTarget->gpuMemorySize();
287 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400288 if (kInvalidGpuMemorySize == fGpuMemorySize) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400289 fGpuMemorySize = this->onUninstantiatedGpuMemorySize();
Robert Phillips8bc06d02016-11-01 17:28:40 -0400290 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
291 }
292 return fGpuMemorySize;
293 }
294
Robert Phillipse2f7d182016-12-15 09:23:05 -0500295 // Helper function that creates a temporary SurfaceContext to perform the copy
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400296 // It always returns a kExact-backed proxy bc it is used when converting an SkSpecialImage
Brian Salomon63e79732017-05-15 21:23:13 -0400297 // to an SkImage. The copy is is not a render target and not multisampled.
Robert Phillips63c67462017-02-15 14:19:01 -0500298 static sk_sp<GrTextureProxy> Copy(GrContext*, GrSurfaceProxy* src,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500299 SkIRect srcRect, SkBudgeted);
300
301 // Copy the entire 'src'
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400302 // It always returns a kExact-backed proxy bc it is used in SkGpuDevice::snapSpecial
Robert Phillips63c67462017-02-15 14:19:01 -0500303 static sk_sp<GrTextureProxy> Copy(GrContext* context, GrSurfaceProxy* src,
304 SkBudgeted budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500305
306 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd46697a2017-01-25 12:10:37 -0500307 static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
308 GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500309
Robert Phillipseaa86252016-11-08 13:49:39 +0000310 bool isWrapped_ForTesting() const;
311
Brian Osman45580d32016-11-23 09:37:01 -0500312 SkDEBUGCODE(void validate(GrContext*) const;)
313
Robert Phillips757914d2017-01-25 15:48:30 -0500314 // Provides access to functions that aren't part of the public API.
315 GrSurfaceProxyPriv priv();
316 const GrSurfaceProxyPriv priv() const;
317
robertphillips76948d42016-05-04 12:47:41 -0700318protected:
robertphillips8abb3702016-08-31 14:04:06 -0700319 // Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -0500320 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted, uint32_t flags)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400321 : fConfig(desc.fConfig)
322 , fWidth(desc.fWidth)
323 , fHeight(desc.fHeight)
324 , fOrigin(desc.fOrigin)
325 , fFit(fit)
326 , fBudgeted(budgeted)
327 , fFlags(flags)
Brian Salomond17b4a62017-05-23 16:53:47 -0400328 , fNeedsClear(SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag))
Brian Salomonbb5711a2017-05-17 13:49:59 -0400329 , fGpuMemorySize(kInvalidGpuMemorySize)
330 , fLastOpList(nullptr) {
Robert Phillips294870f2016-11-11 12:38:40 -0500331 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700332 }
333
334 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400335 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700336
Robert Phillipsf2361d22016-10-25 14:20:06 -0400337 virtual ~GrSurfaceProxy();
338
Robert Phillips757914d2017-01-25 15:48:30 -0500339 friend class GrSurfaceProxyPriv;
340
341 // Methods made available via GrSurfaceProxyPriv
342 bool hasPendingIO() const {
343 return this->internalHasPendingIO();
344 }
345
Robert Phillips7ee385e2017-03-30 08:02:11 -0400346 bool hasPendingWrite() const {
347 return this->internalHasPendingWrite();
348 }
349
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400350 bool instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
351 GrSurfaceFlags flags, bool isMipMapped,
352 SkDestinationSurfaceColorMode mipColorMode);
Brian Salomonbb5711a2017-05-17 13:49:59 -0400353
354 // For wrapped resources, 'fConfig', 'fWidth', 'fHeight', and 'fOrigin; will always be filled in
355 // from the wrapped resource.
356 GrPixelConfig fConfig;
357 int fWidth;
358 int fHeight;
359 GrSurfaceOrigin fOrigin;
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400360 SkBackingFit fFit; // always exact for wrapped resources
Robert Phillipsb726d582017-03-09 16:36:32 -0500361 mutable SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
362 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsc787e492017-02-28 11:26:32 -0500363 const uint32_t fFlags;
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400364
Robert Phillips294870f2016-11-11 12:38:40 -0500365 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700366
Robert Phillips8bc06d02016-11-01 17:28:40 -0400367 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400368 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
369
370private:
Brian Salomonbb5711a2017-05-17 13:49:59 -0400371 virtual size_t onUninstantiatedGpuMemorySize() const = 0;
Robert Phillips29e52f12016-11-03 10:19:14 -0400372
Brian Salomond17b4a62017-05-23 16:53:47 -0400373 bool fNeedsClear;
374
Robert Phillips8bc06d02016-11-01 17:28:40 -0400375 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
376 // will be called but, when the proxy is deferred, it will compute the answer itself.
377 // If the proxy computes its own answer that answer is checked (in debug mode) in
378 // the instantiation method.
379 mutable size_t fGpuMemorySize;
380
Robert Phillipsf2361d22016-10-25 14:20:06 -0400381 // The last opList that wrote to or is currently going to write to this surface
Robert Phillipsb6deea82017-05-11 14:14:30 -0400382 // The opList can be closed (e.g., no surface context is currently bound
383 // to this proxy).
Robert Phillipsf2361d22016-10-25 14:20:06 -0400384 // This back-pointer is required so that we can add a dependancy between
385 // the opList used to create the current contents of this surface
386 // and the opList of a destination surface to which this one is being drawn or copied.
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400387 // This pointer is unreffed. OpLists own a ref on their surface proxies.
Robert Phillipsf2361d22016-10-25 14:20:06 -0400388 GrOpList* fLastOpList;
389
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400390 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700391};
392
393#endif