blob: 3c65cd84ce1345a25a6abbe96c00e019a346f6aa [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;
Brian Osman32342f02017-03-04 08:12:46 -050019class GrResourceProvider;
Robert Phillipsd46697a2017-01-25 12:10:37 -050020class GrSurfaceContext;
Robert Phillips757914d2017-01-25 15:48:30 -050021class GrSurfaceProxyPriv;
Brian Osman45580d32016-11-23 09:37:01 -050022class GrTextureOpList;
robertphillips76948d42016-05-04 12:47:41 -070023class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070024
Robert Phillipsc7635fa2016-10-28 13:25:24 -040025// This class replicates the functionality GrIORef<GrSurface> but tracks the
26// utilitization for later resource allocation (for the deferred case) and
27// forwards on the utilization in the wrapped case
28class GrIORefProxy : public SkNoncopyable {
29public:
30 void ref() const {
31 this->validate();
32
33 ++fRefCnt;
34 if (fTarget) {
35 fTarget->ref();
36 }
37 }
38
39 void unref() const {
40 this->validate();
41
42 if (fTarget) {
43 fTarget->unref();
44 }
45
46 if (!(--fRefCnt)) {
47 delete this;
48 return;
49 }
50
51 this->validate();
52 }
53
54 void validate() const {
55#ifdef SK_DEBUG
robertphillips1125a032016-11-16 11:17:17 -080056 SkASSERT(fRefCnt >= 1);
57 SkASSERT(fPendingReads >= 0);
58 SkASSERT(fPendingWrites >= 0);
59 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
60
61 if (fTarget) {
62 SkASSERT(!fPendingReads && !fPendingWrites);
63 // The backing GrSurface can have more refs than the proxy if the proxy
64 // started off wrapping an external resource (that came in with refs).
65 // The GrSurface should never have fewer refs than the proxy however.
66 SkASSERT(fTarget->fRefCnt >= fRefCnt);
67 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040068#endif
69 }
70
robertphillips1125a032016-11-16 11:17:17 -080071 int32_t getProxyRefCnt_TestOnly() const;
72 int32_t getBackingRefCnt_TestOnly() const;
73 int32_t getPendingReadCnt_TestOnly() const;
74 int32_t getPendingWriteCnt_TestOnly() const;
75
Robert Phillipsc7635fa2016-10-28 13:25:24 -040076protected:
robertphillips1125a032016-11-16 11:17:17 -080077 GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {}
78 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -040079 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
80 // anything extra.
81 fTarget = surface.release();
82 }
83 virtual ~GrIORefProxy() {
84 // We don't unref 'fTarget' here since the 'unref' method will already
85 // have forwarded on the unref call that got use here.
86 }
87
robertphillips1125a032016-11-16 11:17:17 -080088 // This GrIORefProxy was deferred before but has just been instantiated. To
89 // make all the reffing & unreffing work out we now need to transfer any deferred
90 // refs & unrefs to the new GrSurface
91 void transferRefs() {
92 SkASSERT(fTarget);
93
94 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
95 fTarget->fPendingReads += fPendingReads;
96 fTarget->fPendingWrites += fPendingWrites;
97
98 fPendingReads = 0;
99 fPendingWrites = 0;
100 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400101
Robert Phillips757914d2017-01-25 15:48:30 -0500102 bool internalHasPendingIO() const {
103 if (fTarget) {
104 return fTarget->internalHasPendingIO();
105 }
106
107 return SkToBool(fPendingWrites | fPendingReads);
108 }
109
Robert Phillips7ee385e2017-03-30 08:02:11 -0400110 bool internalHasPendingWrite() const {
111 if (fTarget) {
112 return fTarget->internalHasPendingWrite();
113 }
114
115 return SkToBool(fPendingWrites);
116 }
117
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400118 // For deferred proxies this will be null. For wrapped proxies it will point to the
119 // wrapped resource.
120 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800121
122private:
123 // This class is used to manage conversion of refs to pending reads/writes.
124 friend class GrGpuResourceRef;
125 template <typename, GrIOType> friend class GrPendingIOResource;
126
127 void addPendingRead() const {
128 this->validate();
129
130 if (fTarget) {
131 fTarget->addPendingRead();
132 return;
133 }
134
135 ++fPendingReads;
136 }
137
138 void completedRead() const {
139 this->validate();
140
141 if (fTarget) {
142 fTarget->completedRead();
143 return;
144 }
145
146 SkFAIL("How was the read completed if the Proxy hasn't been instantiated?");
147 }
148
149 void addPendingWrite() const {
150 this->validate();
151
152 if (fTarget) {
153 fTarget->addPendingWrite();
154 return;
155 }
156
157 ++fPendingWrites;
158 }
159
160 void completedWrite() const {
161 this->validate();
162
163 if (fTarget) {
164 fTarget->completedWrite();
165 return;
166 }
167
168 SkFAIL("How was the write completed if the Proxy hasn't been instantiated?");
169 }
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
Robert Phillipsd3749482017-03-14 09:17:43 -0400191 static sk_sp<GrSurfaceProxy> MakeWrappedBackend(GrContext*, GrBackendTextureDesc&);
Robert Phillips26caf892017-01-27 10:58:31 -0500192
robertphillips76948d42016-05-04 12:47:41 -0700193 const GrSurfaceDesc& desc() const { return fDesc; }
194
195 GrSurfaceOrigin origin() const {
196 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
197 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
198 return fDesc.fOrigin;
199 }
200 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400201 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -0700202 GrPixelConfig config() const { return fDesc.fConfig; }
203
Robert Phillips294870f2016-11-11 12:38:40 -0500204 class UniqueID {
205 public:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400206 static UniqueID InvalidID() {
207 return UniqueID(uint32_t(SK_InvalidUniqueID));
208 }
209
Robert Phillips294870f2016-11-11 12:38:40 -0500210 // wrapped
211 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
212 // deferred
213 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
214
215 uint32_t asUInt() const { return fID; }
216
217 bool operator==(const UniqueID& other) const {
218 return fID == other.fID;
219 }
220 bool operator!=(const UniqueID& other) const {
221 return !(*this == other);
222 }
223
Robert Phillips7ee385e2017-03-30 08:02:11 -0400224 void makeInvalid() { fID = SK_InvalidUniqueID; }
Robert Phillips294870f2016-11-11 12:38:40 -0500225 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
226
227 private:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400228 explicit UniqueID(uint32_t id) : fID(id) {}
229
230 uint32_t fID;
Robert Phillips294870f2016-11-11 12:38:40 -0500231 };
232
233 /*
234 * The contract for the uniqueID is:
235 * for wrapped resources:
236 * the uniqueID will match that of the wrapped resource
237 *
238 * for deferred resources:
239 * the uniqueID will be different from the real resource, when it is allocated
240 * the proxy's uniqueID will not change across the instantiate call
241 *
242 * the uniqueIDs of the proxies and the resources draw from the same pool
243 *
244 * What this boils down to is that the uniqueID of a proxy can be used to consistently
245 * track/identify a proxy but should never be used to distinguish between
246 * resources and proxies - beware!
247 */
248 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700249
Brian Osman32342f02017-03-04 08:12:46 -0500250 GrSurface* instantiate(GrResourceProvider* resourceProvider);
Robert Phillips37430132016-11-09 06:50:43 -0500251
robertphillips76948d42016-05-04 12:47:41 -0700252 /**
robertphillips13a7eee2016-08-31 15:06:24 -0700253 * Helper that gets the width and height of the surface as a bounding rectangle.
254 */
255 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
Robert Phillips784b7bf2016-12-09 13:35:02 -0500256
257 int worstCaseWidth(const GrCaps& caps) const;
258 int worstCaseHeight(const GrCaps& caps) const;
Robert Phillips93f16332016-11-23 19:37:13 -0500259
robertphillips13a7eee2016-08-31 15:06:24 -0700260 /**
robertphillips76948d42016-05-04 12:47:41 -0700261 * @return the texture proxy associated with the surface proxy, may be NULL.
262 */
263 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
264 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
265
266 /**
267 * @return the render target proxy associated with the surface proxy, may be NULL.
268 */
269 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
270 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
271
robertphillips13a7eee2016-08-31 15:06:24 -0700272 /**
273 * Does the resource count against the resource budget?
274 */
275 SkBudgeted isBudgeted() const { return fBudgeted; }
276
Robert Phillipsf2361d22016-10-25 14:20:06 -0400277 void setLastOpList(GrOpList* opList);
278 GrOpList* getLastOpList() { return fLastOpList; }
279
Brian Osman45580d32016-11-23 09:37:01 -0500280 GrRenderTargetOpList* getLastRenderTargetOpList();
281 GrTextureOpList* getLastTextureOpList();
282
Robert Phillips8bc06d02016-11-01 17:28:40 -0400283 /**
284 * Retrieves the amount of GPU memory that will be or currently is used by this resource
285 * in bytes. It is approximate since we aren't aware of additional padding or copies made
286 * by the driver.
287 *
288 * @return the amount of GPU memory used in bytes
289 */
290 size_t gpuMemorySize() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400291 if (kInvalidGpuMemorySize == fGpuMemorySize) {
292 fGpuMemorySize = this->onGpuMemorySize();
293 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
294 }
295 return fGpuMemorySize;
296 }
297
Robert Phillipse2f7d182016-12-15 09:23:05 -0500298 // Helper function that creates a temporary SurfaceContext to perform the copy
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400299 // It always returns a kExact-backed proxy bc it is used when converting an SkSpecialImage
300 // to an SkImage.
Robert Phillips63c67462017-02-15 14:19:01 -0500301 static sk_sp<GrTextureProxy> Copy(GrContext*, GrSurfaceProxy* src,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500302 SkIRect srcRect, SkBudgeted);
303
304 // Copy the entire 'src'
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400305 // It always returns a kExact-backed proxy bc it is used in SkGpuDevice::snapSpecial
Robert Phillips63c67462017-02-15 14:19:01 -0500306 static sk_sp<GrTextureProxy> Copy(GrContext* context, GrSurfaceProxy* src,
307 SkBudgeted budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500308
309 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd46697a2017-01-25 12:10:37 -0500310 static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
311 GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500312
Robert Phillipseaa86252016-11-08 13:49:39 +0000313 bool isWrapped_ForTesting() const;
314
Brian Osman45580d32016-11-23 09:37:01 -0500315 SkDEBUGCODE(void validate(GrContext*) const;)
316
Robert Phillips757914d2017-01-25 15:48:30 -0500317 // Provides access to functions that aren't part of the public API.
318 GrSurfaceProxyPriv priv();
319 const GrSurfaceProxyPriv priv() const;
320
robertphillips76948d42016-05-04 12:47:41 -0700321protected:
robertphillips8abb3702016-08-31 14:04:06 -0700322 // Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -0500323 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted, uint32_t flags)
robertphillips76948d42016-05-04 12:47:41 -0700324 : fDesc(desc)
325 , fFit(fit)
326 , fBudgeted(budgeted)
Robert Phillipsc787e492017-02-28 11:26:32 -0500327 , fFlags(flags)
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400328 // fMipColorMode is only valid for texturable proxies
329 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400330 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400331 , fLastOpList(nullptr) {
Robert Phillips294870f2016-11-11 12:38:40 -0500332 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700333 }
334
335 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400336 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700337
Robert Phillipsf2361d22016-10-25 14:20:06 -0400338 virtual ~GrSurfaceProxy();
339
Robert Phillips757914d2017-01-25 15:48:30 -0500340 friend class GrSurfaceProxyPriv;
341
342 // Methods made available via GrSurfaceProxyPriv
343 bool hasPendingIO() const {
344 return this->internalHasPendingIO();
345 }
346
Robert Phillips7ee385e2017-03-30 08:02:11 -0400347 bool hasPendingWrite() const {
348 return this->internalHasPendingWrite();
349 }
350
robertphillips76948d42016-05-04 12:47:41 -0700351 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400352 GrSurfaceDesc fDesc;
353 SkBackingFit fFit; // always exact for wrapped resources
Robert Phillipsb726d582017-03-09 16:36:32 -0500354 mutable SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
355 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsc787e492017-02-28 11:26:32 -0500356 const uint32_t fFlags;
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400357
358 SkDestinationSurfaceColorMode fMipColorMode;
359
Robert Phillips294870f2016-11-11 12:38:40 -0500360 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700361
Robert Phillips8bc06d02016-11-01 17:28:40 -0400362 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400363 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
364
365private:
366 virtual size_t onGpuMemorySize() const = 0;
367
Robert Phillips8bc06d02016-11-01 17:28:40 -0400368 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
369 // will be called but, when the proxy is deferred, it will compute the answer itself.
370 // If the proxy computes its own answer that answer is checked (in debug mode) in
371 // the instantiation method.
372 mutable size_t fGpuMemorySize;
373
Robert Phillipsf2361d22016-10-25 14:20:06 -0400374 // The last opList that wrote to or is currently going to write to this surface
Brian Osman11052242016-10-27 14:47:55 -0400375 // The opList can be closed (e.g., no render target context is currently bound
Robert Phillipsf2361d22016-10-25 14:20:06 -0400376 // to this renderTarget).
377 // This back-pointer is required so that we can add a dependancy between
378 // the opList used to create the current contents of this surface
379 // and the opList of a destination surface to which this one is being drawn or copied.
380 GrOpList* fLastOpList;
381
Robert Phillips29e52f12016-11-03 10:19:14 -0400382
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400383 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700384};
385
386#endif