blob: eb4f2814e360e147d08ea2472641b16cd703c918 [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 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 Phillips7ee385e2017-03-30 08:02:11 -0400112 bool internalHasPendingWrite() const {
113 if (fTarget) {
114 return fTarget->internalHasPendingWrite();
115 }
116
117 return SkToBool(fPendingWrites);
118 }
119
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400120 // For deferred proxies this will be null. For wrapped proxies it will point to the
121 // wrapped resource.
122 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800123
124private:
125 // This class is used to manage conversion of refs to pending reads/writes.
126 friend class GrGpuResourceRef;
127 template <typename, GrIOType> friend class GrPendingIOResource;
128
129 void addPendingRead() const {
130 this->validate();
131
132 if (fTarget) {
133 fTarget->addPendingRead();
134 return;
135 }
136
137 ++fPendingReads;
138 }
139
140 void completedRead() const {
141 this->validate();
142
143 if (fTarget) {
144 fTarget->completedRead();
145 return;
146 }
147
148 SkFAIL("How was the read completed if the Proxy hasn't been instantiated?");
149 }
150
151 void addPendingWrite() const {
152 this->validate();
153
154 if (fTarget) {
155 fTarget->addPendingWrite();
156 return;
157 }
158
159 ++fPendingWrites;
160 }
161
162 void completedWrite() const {
163 this->validate();
164
165 if (fTarget) {
166 fTarget->completedWrite();
167 return;
168 }
169
170 SkFAIL("How was the write completed if the Proxy hasn't been instantiated?");
171 }
172
173 mutable int32_t fRefCnt;
174 mutable int32_t fPendingReads;
175 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400176};
177
178class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700179public:
Robert Phillips37430132016-11-09 06:50:43 -0500180 static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>);
Robert Phillips63c67462017-02-15 14:19:01 -0500181 static sk_sp<GrTextureProxy> MakeWrapped(sk_sp<GrTexture>);
Robert Phillips37430132016-11-09 06:50:43 -0500182
Robert Phillips26c90e02017-03-14 14:39:29 -0400183 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips7928e762017-02-28 16:30:28 -0500184 const GrSurfaceDesc&, SkBackingFit,
185 SkBudgeted, uint32_t flags = 0);
Robert Phillips37430132016-11-09 06:50:43 -0500186
187 // TODO: need to refine ownership semantics of 'srcData' if we're in completely
188 // deferred mode
Robert Phillips26c90e02017-03-14 14:39:29 -0400189 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips37430132016-11-09 06:50:43 -0500190 const GrSurfaceDesc&, SkBudgeted,
191 const void* srcData, size_t rowBytes);
192
Greg Daniel7ef28f32017-04-20 16:41:55 +0000193 static sk_sp<GrTextureProxy> MakeWrappedBackend(GrContext*, GrBackendTexture&, GrSurfaceOrigin);
Robert Phillips26caf892017-01-27 10:58:31 -0500194
robertphillips76948d42016-05-04 12:47:41 -0700195 const GrSurfaceDesc& desc() const { return fDesc; }
196
197 GrSurfaceOrigin origin() const {
198 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
199 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
200 return fDesc.fOrigin;
201 }
202 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400203 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -0700204 GrPixelConfig config() const { return fDesc.fConfig; }
205
Robert Phillips294870f2016-11-11 12:38:40 -0500206 class UniqueID {
207 public:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400208 static UniqueID InvalidID() {
209 return UniqueID(uint32_t(SK_InvalidUniqueID));
210 }
211
Robert Phillips294870f2016-11-11 12:38:40 -0500212 // wrapped
213 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
214 // deferred
215 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
216
217 uint32_t asUInt() const { return fID; }
218
219 bool operator==(const UniqueID& other) const {
220 return fID == other.fID;
221 }
222 bool operator!=(const UniqueID& other) const {
223 return !(*this == other);
224 }
225
Robert Phillips7ee385e2017-03-30 08:02:11 -0400226 void makeInvalid() { fID = SK_InvalidUniqueID; }
Robert Phillips294870f2016-11-11 12:38:40 -0500227 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
228
229 private:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400230 explicit UniqueID(uint32_t id) : fID(id) {}
231
232 uint32_t fID;
Robert Phillips294870f2016-11-11 12:38:40 -0500233 };
234
235 /*
236 * The contract for the uniqueID is:
237 * for wrapped resources:
238 * the uniqueID will match that of the wrapped resource
239 *
240 * for deferred resources:
241 * the uniqueID will be different from the real resource, when it is allocated
242 * the proxy's uniqueID will not change across the instantiate call
243 *
244 * the uniqueIDs of the proxies and the resources draw from the same pool
245 *
246 * What this boils down to is that the uniqueID of a proxy can be used to consistently
247 * track/identify a proxy but should never be used to distinguish between
248 * resources and proxies - beware!
249 */
250 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700251
Brian Osman32342f02017-03-04 08:12:46 -0500252 GrSurface* instantiate(GrResourceProvider* resourceProvider);
Robert Phillips37430132016-11-09 06:50:43 -0500253
robertphillips76948d42016-05-04 12:47:41 -0700254 /**
robertphillips13a7eee2016-08-31 15:06:24 -0700255 * Helper that gets the width and height of the surface as a bounding rectangle.
256 */
257 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
Robert Phillips784b7bf2016-12-09 13:35:02 -0500258
259 int worstCaseWidth(const GrCaps& caps) const;
260 int worstCaseHeight(const GrCaps& caps) const;
Robert Phillips93f16332016-11-23 19:37:13 -0500261
robertphillips13a7eee2016-08-31 15:06:24 -0700262 /**
robertphillips76948d42016-05-04 12:47:41 -0700263 * @return the texture proxy associated with the surface proxy, may be NULL.
264 */
265 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
266 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
267
268 /**
269 * @return the render target proxy associated with the surface proxy, may be NULL.
270 */
271 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
272 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
273
robertphillips13a7eee2016-08-31 15:06:24 -0700274 /**
275 * Does the resource count against the resource budget?
276 */
277 SkBudgeted isBudgeted() const { return fBudgeted; }
278
Robert Phillipsf2361d22016-10-25 14:20:06 -0400279 void setLastOpList(GrOpList* opList);
280 GrOpList* getLastOpList() { return fLastOpList; }
281
Brian Osman45580d32016-11-23 09:37:01 -0500282 GrRenderTargetOpList* getLastRenderTargetOpList();
283 GrTextureOpList* getLastTextureOpList();
284
Robert Phillips8bc06d02016-11-01 17:28:40 -0400285 /**
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400286 * Retrieves the amount of GPU memory that will be or currently is used by this resource
Robert Phillips8bc06d02016-11-01 17:28:40 -0400287 * in bytes. It is approximate since we aren't aware of additional padding or copies made
288 * by the driver.
289 *
290 * @return the amount of GPU memory used in bytes
291 */
292 size_t gpuMemorySize() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400293 if (kInvalidGpuMemorySize == fGpuMemorySize) {
294 fGpuMemorySize = this->onGpuMemorySize();
295 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
296 }
297 return fGpuMemorySize;
298 }
299
Robert Phillipse2f7d182016-12-15 09:23:05 -0500300 // Helper function that creates a temporary SurfaceContext to perform the copy
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400301 // It always returns a kExact-backed proxy bc it is used when converting an SkSpecialImage
302 // to an SkImage.
Robert Phillips63c67462017-02-15 14:19:01 -0500303 static sk_sp<GrTextureProxy> Copy(GrContext*, GrSurfaceProxy* src,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500304 SkIRect srcRect, SkBudgeted);
305
306 // Copy the entire 'src'
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400307 // It always returns a kExact-backed proxy bc it is used in SkGpuDevice::snapSpecial
Robert Phillips63c67462017-02-15 14:19:01 -0500308 static sk_sp<GrTextureProxy> Copy(GrContext* context, GrSurfaceProxy* src,
309 SkBudgeted budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500310
311 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd46697a2017-01-25 12:10:37 -0500312 static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
313 GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500314
Robert Phillipseaa86252016-11-08 13:49:39 +0000315 bool isWrapped_ForTesting() const;
316
Brian Osman45580d32016-11-23 09:37:01 -0500317 SkDEBUGCODE(void validate(GrContext*) const;)
318
Robert Phillips757914d2017-01-25 15:48:30 -0500319 // Provides access to functions that aren't part of the public API.
320 GrSurfaceProxyPriv priv();
321 const GrSurfaceProxyPriv priv() const;
322
robertphillips76948d42016-05-04 12:47:41 -0700323protected:
robertphillips8abb3702016-08-31 14:04:06 -0700324 // Deferred version
Robert Phillipsc787e492017-02-28 11:26:32 -0500325 GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted, uint32_t flags)
robertphillips76948d42016-05-04 12:47:41 -0700326 : fDesc(desc)
327 , fFit(fit)
328 , fBudgeted(budgeted)
Robert Phillipsc787e492017-02-28 11:26:32 -0500329 , fFlags(flags)
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400330 // fMipColorMode is only valid for texturable proxies
331 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400332 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400333 , fLastOpList(nullptr) {
Robert Phillips294870f2016-11-11 12:38:40 -0500334 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700335 }
336
337 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400338 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700339
Robert Phillipsf2361d22016-10-25 14:20:06 -0400340 virtual ~GrSurfaceProxy();
341
Robert Phillips757914d2017-01-25 15:48:30 -0500342 friend class GrSurfaceProxyPriv;
343
344 // Methods made available via GrSurfaceProxyPriv
345 bool hasPendingIO() const {
346 return this->internalHasPendingIO();
347 }
348
Robert Phillips7ee385e2017-03-30 08:02:11 -0400349 bool hasPendingWrite() const {
350 return this->internalHasPendingWrite();
351 }
352
robertphillips76948d42016-05-04 12:47:41 -0700353 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400354 GrSurfaceDesc fDesc;
355 SkBackingFit fFit; // always exact for wrapped resources
Robert Phillipsb726d582017-03-09 16:36:32 -0500356 mutable SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
357 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsc787e492017-02-28 11:26:32 -0500358 const uint32_t fFlags;
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400359
360 SkDestinationSurfaceColorMode fMipColorMode;
361
Robert Phillips294870f2016-11-11 12:38:40 -0500362 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700363
Robert Phillips8bc06d02016-11-01 17:28:40 -0400364 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400365 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
366
367private:
368 virtual size_t onGpuMemorySize() const = 0;
369
Robert Phillips8bc06d02016-11-01 17:28:40 -0400370 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
371 // will be called but, when the proxy is deferred, it will compute the answer itself.
372 // If the proxy computes its own answer that answer is checked (in debug mode) in
373 // the instantiation method.
374 mutable size_t fGpuMemorySize;
375
Robert Phillipsf2361d22016-10-25 14:20:06 -0400376 // The last opList that wrote to or is currently going to write to this surface
Brian Osman11052242016-10-27 14:47:55 -0400377 // The opList can be closed (e.g., no render target context is currently bound
Robert Phillipsf2361d22016-10-25 14:20:06 -0400378 // to this renderTarget).
379 // This back-pointer is required so that we can add a dependancy between
380 // the opList used to create the current contents of this surface
381 // and the opList of a destination surface to which this one is being drawn or copied.
382 GrOpList* fLastOpList;
383
Robert Phillips7ce67db2017-04-18 17:10:13 +0000384
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400385 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700386};
387
388#endif