blob: 8b3779c4e9ff4d670d34709e03ffc9921148eba1 [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
Robert Phillipsb6deea82017-05-11 14:14:30 -040048 --fRefCnt;
49 this->didRemoveRefOrPendingIO();
Robert Phillipsc7635fa2016-10-28 13:25:24 -040050 }
51
52 void validate() const {
Robert Phillipsb6deea82017-05-11 14:14:30 -040053#ifdef SK_DEBUG
54 SkASSERT(fRefCnt >= 0);
robertphillips1125a032016-11-16 11:17:17 -080055 SkASSERT(fPendingReads >= 0);
56 SkASSERT(fPendingWrites >= 0);
57 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
58
59 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080060 // The backing GrSurface can have more refs than the proxy if the proxy
61 // started off wrapping an external resource (that came in with refs).
62 // The GrSurface should never have fewer refs than the proxy however.
63 SkASSERT(fTarget->fRefCnt >= fRefCnt);
Robert Phillipsb6deea82017-05-11 14:14:30 -040064 SkASSERT(fTarget->fPendingReads >= fPendingReads);
65 SkASSERT(fTarget->fPendingWrites >= fPendingWrites);
robertphillips1125a032016-11-16 11:17:17 -080066 }
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;
robertphillips1125a032016-11-16 11:17:17 -080096 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040097
Robert Phillips757914d2017-01-25 15:48:30 -050098 bool internalHasPendingIO() const {
99 if (fTarget) {
100 return fTarget->internalHasPendingIO();
101 }
102
103 return SkToBool(fPendingWrites | fPendingReads);
104 }
105
Robert Phillips7ee385e2017-03-30 08:02:11 -0400106 bool internalHasPendingWrite() const {
107 if (fTarget) {
108 return fTarget->internalHasPendingWrite();
109 }
110
111 return SkToBool(fPendingWrites);
112 }
113
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400114 // For deferred proxies this will be null. For wrapped proxies it will point to the
115 // wrapped resource.
116 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800117
118private:
119 // This class is used to manage conversion of refs to pending reads/writes.
Robert Phillipsb6deea82017-05-11 14:14:30 -0400120 friend class GrSurfaceProxyRef;
robertphillips1125a032016-11-16 11:17:17 -0800121 template <typename, GrIOType> friend class GrPendingIOResource;
122
123 void addPendingRead() const {
124 this->validate();
125
Robert Phillipsb6deea82017-05-11 14:14:30 -0400126 ++fPendingReads;
robertphillips1125a032016-11-16 11:17:17 -0800127 if (fTarget) {
128 fTarget->addPendingRead();
robertphillips1125a032016-11-16 11:17:17 -0800129 }
robertphillips1125a032016-11-16 11:17:17 -0800130 }
131
132 void completedRead() const {
133 this->validate();
134
135 if (fTarget) {
136 fTarget->completedRead();
robertphillips1125a032016-11-16 11:17:17 -0800137 }
Robert Phillipsb6deea82017-05-11 14:14:30 -0400138
139 --fPendingReads;
140 this->didRemoveRefOrPendingIO();
robertphillips1125a032016-11-16 11:17:17 -0800141 }
142
143 void addPendingWrite() const {
144 this->validate();
145
Robert Phillipsb6deea82017-05-11 14:14:30 -0400146 ++fPendingWrites;
robertphillips1125a032016-11-16 11:17:17 -0800147 if (fTarget) {
148 fTarget->addPendingWrite();
robertphillips1125a032016-11-16 11:17:17 -0800149 }
robertphillips1125a032016-11-16 11:17:17 -0800150 }
151
152 void completedWrite() const {
153 this->validate();
154
155 if (fTarget) {
156 fTarget->completedWrite();
robertphillips1125a032016-11-16 11:17:17 -0800157 }
Robert Phillipsb6deea82017-05-11 14:14:30 -0400158
159 --fPendingWrites;
160 this->didRemoveRefOrPendingIO();
161 }
162
163 void didRemoveRefOrPendingIO() const {
164 if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
165 delete this;
166 }
robertphillips1125a032016-11-16 11:17:17 -0800167 }
168
169 mutable int32_t fRefCnt;
170 mutable int32_t fPendingReads;
171 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400172};
173
174class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700175public:
Robert Phillips37430132016-11-09 06:50:43 -0500176 static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>);
Robert Phillips63c67462017-02-15 14:19:01 -0500177 static sk_sp<GrTextureProxy> MakeWrapped(sk_sp<GrTexture>);
Robert Phillips37430132016-11-09 06:50:43 -0500178
Robert Phillips26c90e02017-03-14 14:39:29 -0400179 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips7928e762017-02-28 16:30:28 -0500180 const GrSurfaceDesc&, SkBackingFit,
181 SkBudgeted, uint32_t flags = 0);
Robert Phillips37430132016-11-09 06:50:43 -0500182
183 // TODO: need to refine ownership semantics of 'srcData' if we're in completely
184 // deferred mode
Robert Phillips26c90e02017-03-14 14:39:29 -0400185 static sk_sp<GrTextureProxy> MakeDeferred(GrResourceProvider*,
Robert Phillips37430132016-11-09 06:50:43 -0500186 const GrSurfaceDesc&, SkBudgeted,
187 const void* srcData, size_t rowBytes);
188
Greg Daniel7ef28f32017-04-20 16:41:55 +0000189 static sk_sp<GrTextureProxy> MakeWrappedBackend(GrContext*, GrBackendTexture&, GrSurfaceOrigin);
Robert Phillips26caf892017-01-27 10:58:31 -0500190
robertphillips76948d42016-05-04 12:47:41 -0700191 GrSurfaceOrigin origin() const {
192 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
193 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
194 return fDesc.fOrigin;
195 }
196 int width() const { return fDesc.fWidth; }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400197 int height() const { return fDesc.fHeight; }
robertphillips76948d42016-05-04 12:47:41 -0700198 GrPixelConfig config() const { return fDesc.fConfig; }
Brian Salomon63e79732017-05-15 21:23:13 -0400199 bool isMipMapped() const { return fDesc.fIsMipMapped; }
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
Brian Osman32342f02017-03-04 08:12:46 -0500247 GrSurface* instantiate(GrResourceProvider* resourceProvider);
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
254 int worstCaseWidth(const GrCaps& caps) const;
255 int worstCaseHeight(const GrCaps& caps) const;
Robert Phillips93f16332016-11-23 19:37:13 -0500256
robertphillips13a7eee2016-08-31 15:06:24 -0700257 /**
robertphillips76948d42016-05-04 12:47:41 -0700258 * @return the texture proxy associated with the surface proxy, may be NULL.
259 */
260 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
261 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
262
263 /**
264 * @return the render target proxy associated with the surface proxy, may be NULL.
265 */
266 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
267 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
268
robertphillips13a7eee2016-08-31 15:06:24 -0700269 /**
270 * Does the resource count against the resource budget?
271 */
272 SkBudgeted isBudgeted() const { return fBudgeted; }
273
Robert Phillipsf2361d22016-10-25 14:20:06 -0400274 void setLastOpList(GrOpList* opList);
275 GrOpList* getLastOpList() { return fLastOpList; }
276
Brian Osman45580d32016-11-23 09:37:01 -0500277 GrRenderTargetOpList* getLastRenderTargetOpList();
278 GrTextureOpList* getLastTextureOpList();
279
Robert Phillips8bc06d02016-11-01 17:28:40 -0400280 /**
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400281 * Retrieves the amount of GPU memory that will be or currently is used by this resource
Robert Phillips8bc06d02016-11-01 17:28:40 -0400282 * in bytes. It is approximate since we aren't aware of additional padding or copies made
283 * by the driver.
284 *
285 * @return the amount of GPU memory used in bytes
286 */
287 size_t gpuMemorySize() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400288 if (kInvalidGpuMemorySize == fGpuMemorySize) {
289 fGpuMemorySize = this->onGpuMemorySize();
290 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)
robertphillips76948d42016-05-04 12:47:41 -0700321 : fDesc(desc)
322 , fFit(fit)
323 , fBudgeted(budgeted)
Robert Phillipsc787e492017-02-28 11:26:32 -0500324 , fFlags(flags)
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400325 // fMipColorMode is only valid for texturable proxies
326 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy)
Robert Phillips8bc06d02016-11-01 17:28:40 -0400327 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400328 , fLastOpList(nullptr) {
Robert Phillips294870f2016-11-11 12:38:40 -0500329 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700330 }
331
332 // Wrapped version
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400333 GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit);
robertphillips76948d42016-05-04 12:47:41 -0700334
Robert Phillipsf2361d22016-10-25 14:20:06 -0400335 virtual ~GrSurfaceProxy();
336
Robert Phillips757914d2017-01-25 15:48:30 -0500337 friend class GrSurfaceProxyPriv;
338
339 // Methods made available via GrSurfaceProxyPriv
340 bool hasPendingIO() const {
341 return this->internalHasPendingIO();
342 }
343
Robert Phillips7ee385e2017-03-30 08:02:11 -0400344 bool hasPendingWrite() const {
345 return this->internalHasPendingWrite();
346 }
347
robertphillips76948d42016-05-04 12:47:41 -0700348 // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource.
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400349 GrSurfaceDesc fDesc;
350 SkBackingFit fFit; // always exact for wrapped resources
Robert Phillipsb726d582017-03-09 16:36:32 -0500351 mutable SkBudgeted fBudgeted; // set from the backing resource for wrapped resources
352 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsc787e492017-02-28 11:26:32 -0500353 const uint32_t fFlags;
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400354
355 SkDestinationSurfaceColorMode fMipColorMode;
356
Robert Phillips294870f2016-11-11 12:38:40 -0500357 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700358
Robert Phillips8bc06d02016-11-01 17:28:40 -0400359 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400360 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
361
362private:
363 virtual size_t onGpuMemorySize() const = 0;
364
Robert Phillips8bc06d02016-11-01 17:28:40 -0400365 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
366 // will be called but, when the proxy is deferred, it will compute the answer itself.
367 // If the proxy computes its own answer that answer is checked (in debug mode) in
368 // the instantiation method.
369 mutable size_t fGpuMemorySize;
370
Robert Phillipsf2361d22016-10-25 14:20:06 -0400371 // The last opList that wrote to or is currently going to write to this surface
Robert Phillipsb6deea82017-05-11 14:14:30 -0400372 // The opList can be closed (e.g., no surface context is currently bound
373 // to this proxy).
Robert Phillipsf2361d22016-10-25 14:20:06 -0400374 // This back-pointer is required so that we can add a dependancy between
375 // the opList used to create the current contents of this surface
376 // and the opList of a destination surface to which this one is being drawn or copied.
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400377 // This pointer is unreffed. OpLists own a ref on their surface proxies.
Robert Phillipsf2361d22016-10-25 14:20:06 -0400378 GrOpList* fLastOpList;
379
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400380 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700381};
382
383#endif