blob: c9eec6c607275fd1f946d77861bd1d3e108a8755 [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
Ben Wagnerd5148e32018-07-16 17:44:06 -040011#include "../private/SkNoncopyable.h"
Greg Daniel4065d452018-11-16 15:43:41 -050012#include "GrBackendSurface.h"
robertphillips76948d42016-05-04 12:47:41 -070013#include "GrGpuResource.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040014#include "GrSurface.h"
Robert Phillips93f16332016-11-23 19:37:13 -050015
csmartdaltonbf4a8f92016-09-06 10:01:06 -070016#include "SkRect.h"
robertphillips76948d42016-05-04 12:47:41 -070017
Robert Phillips37430132016-11-09 06:50:43 -050018class GrCaps;
Robert Phillips292a6b22019-02-14 14:49:02 -050019class GrContext_Base;
Robert Phillipsc589b0b2017-04-17 07:53:07 -040020class GrOpList;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050021class GrProxyProvider;
Robert Phillipsd44146b2019-02-15 14:44:28 -050022class GrRecordingContext;
Brian Osman45580d32016-11-23 09:37:01 -050023class GrRenderTargetOpList;
24class GrRenderTargetProxy;
Brian Osman32342f02017-03-04 08:12:46 -050025class GrResourceProvider;
Robert Phillipsd46697a2017-01-25 12:10:37 -050026class GrSurfaceContext;
Robert Phillips757914d2017-01-25 15:48:30 -050027class GrSurfaceProxyPriv;
Brian Osman45580d32016-11-23 09:37:01 -050028class GrTextureOpList;
robertphillips76948d42016-05-04 12:47:41 -070029class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070030
Robert Phillipsc7635fa2016-10-28 13:25:24 -040031// This class replicates the functionality GrIORef<GrSurface> but tracks the
32// utilitization for later resource allocation (for the deferred case) and
33// forwards on the utilization in the wrapped case
34class GrIORefProxy : public SkNoncopyable {
35public:
36 void ref() const {
37 this->validate();
38
39 ++fRefCnt;
40 if (fTarget) {
41 fTarget->ref();
42 }
43 }
44
45 void unref() const {
46 this->validate();
47
48 if (fTarget) {
49 fTarget->unref();
50 }
51
Robert Phillipsb6deea82017-05-11 14:14:30 -040052 --fRefCnt;
53 this->didRemoveRefOrPendingIO();
Robert Phillipsc7635fa2016-10-28 13:25:24 -040054 }
55
Chris Daltona32a3c32017-12-05 10:05:21 -070056#ifdef SK_DEBUG
57 bool isUnique_debugOnly() const { // For asserts.
58 SkASSERT(fRefCnt >= 0 && fPendingWrites >= 0 && fPendingReads >= 0);
59 return 1 == fRefCnt + fPendingWrites + fPendingReads;
60 }
61#endif
62
Robert Phillips4bc70112018-03-01 10:24:02 -050063 void release() {
Greg Daniel4684f822018-03-08 15:27:36 -050064 // The proxy itself may still have multiple refs. It can be owned by an SkImage and multiple
65 // SkDeferredDisplayLists at the same time if we are using DDLs.
Robert Phillips4bc70112018-03-01 10:24:02 -050066 SkASSERT(0 == fPendingReads);
67 SkASSERT(0 == fPendingWrites);
68
Robert Phillipse4aae342018-03-14 10:26:57 -040069 // In the current hybrid world, the proxy and backing surface are ref/unreffed in
Brian Salomon0d606762019-01-25 09:58:38 -050070 // synchrony. Each ref we've added or removed to the proxy was mirrored to the backing
71 // surface. Though, that backing surface could be owned by other proxies as well. Remove
72 // a ref from the backing surface for each ref the proxy has since we are about to remove
73 // our pointer to the surface. If this proxy is reinstantiated then all the proxy's refs
74 // get transferred to the (possibly new) backing surface.
75 for (int refs = fRefCnt; refs; --refs) {
Robert Phillipse4aae342018-03-14 10:26:57 -040076 fTarget->unref();
77 }
Robert Phillips4bc70112018-03-01 10:24:02 -050078 fTarget = nullptr;
79 }
80
Robert Phillipsc7635fa2016-10-28 13:25:24 -040081 void validate() const {
Robert Phillipsb6deea82017-05-11 14:14:30 -040082#ifdef SK_DEBUG
83 SkASSERT(fRefCnt >= 0);
robertphillips1125a032016-11-16 11:17:17 -080084 SkASSERT(fPendingReads >= 0);
85 SkASSERT(fPendingWrites >= 0);
86 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
87
88 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080089 // The backing GrSurface can have more refs than the proxy if the proxy
90 // started off wrapping an external resource (that came in with refs).
91 // The GrSurface should never have fewer refs than the proxy however.
92 SkASSERT(fTarget->fRefCnt >= fRefCnt);
Robert Phillipsb6deea82017-05-11 14:14:30 -040093 SkASSERT(fTarget->fPendingReads >= fPendingReads);
94 SkASSERT(fTarget->fPendingWrites >= fPendingWrites);
robertphillips1125a032016-11-16 11:17:17 -080095 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040096#endif
97 }
98
robertphillips1125a032016-11-16 11:17:17 -080099 int32_t getBackingRefCnt_TestOnly() const;
100 int32_t getPendingReadCnt_TestOnly() const;
101 int32_t getPendingWriteCnt_TestOnly() const;
102
Brian Salomoncf75b002017-08-16 10:42:09 -0400103 void addPendingRead() const {
104 this->validate();
105
106 ++fPendingReads;
107 if (fTarget) {
108 fTarget->addPendingRead();
109 }
110 }
111
112 void completedRead() const {
113 this->validate();
114
115 if (fTarget) {
116 fTarget->completedRead();
117 }
118
119 --fPendingReads;
120 this->didRemoveRefOrPendingIO();
121 }
122
123 void addPendingWrite() const {
124 this->validate();
125
126 ++fPendingWrites;
127 if (fTarget) {
128 fTarget->addPendingWrite();
129 }
130 }
131
132 void completedWrite() const {
133 this->validate();
134
135 if (fTarget) {
136 fTarget->completedWrite();
137 }
138
139 --fPendingWrites;
140 this->didRemoveRefOrPendingIO();
141 }
142
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400143protected:
robertphillips1125a032016-11-16 11:17:17 -0800144 GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {}
145 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400146 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
147 // anything extra.
148 fTarget = surface.release();
149 }
150 virtual ~GrIORefProxy() {
151 // We don't unref 'fTarget' here since the 'unref' method will already
Robert Phillips4bc70112018-03-01 10:24:02 -0500152 // have forwarded on the unref call that got us here.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400153 }
154
robertphillips1125a032016-11-16 11:17:17 -0800155 // This GrIORefProxy was deferred before but has just been instantiated. To
156 // make all the reffing & unreffing work out we now need to transfer any deferred
157 // refs & unrefs to the new GrSurface
158 void transferRefs() {
159 SkASSERT(fTarget);
160
Robert Phillipsf8e25022017-11-08 15:24:31 -0500161 SkASSERT(fTarget->fRefCnt > 0);
Robert Phillips2d9cb572017-11-13 13:38:05 +0000162 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
robertphillips1125a032016-11-16 11:17:17 -0800163 fTarget->fPendingReads += fPendingReads;
164 fTarget->fPendingWrites += fPendingWrites;
robertphillips1125a032016-11-16 11:17:17 -0800165 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400166
Robert Phillips715d08c2018-07-18 13:56:48 -0400167 int32_t internalGetProxyRefCnt() const {
168 return fRefCnt;
169 }
170
Robert Phillips757914d2017-01-25 15:48:30 -0500171 bool internalHasPendingIO() const {
172 if (fTarget) {
173 return fTarget->internalHasPendingIO();
174 }
175
176 return SkToBool(fPendingWrites | fPendingReads);
177 }
178
Robert Phillips7ee385e2017-03-30 08:02:11 -0400179 bool internalHasPendingWrite() const {
180 if (fTarget) {
181 return fTarget->internalHasPendingWrite();
182 }
183
184 return SkToBool(fPendingWrites);
185 }
186
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400187 // For deferred proxies this will be null. For wrapped proxies it will point to the
188 // wrapped resource.
189 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800190
191private:
192 // This class is used to manage conversion of refs to pending reads/writes.
Brian Salomonee783962018-08-01 09:55:10 -0400193 template <typename> friend class GrProxyRef;
robertphillips1125a032016-11-16 11:17:17 -0800194
Robert Phillipsb6deea82017-05-11 14:14:30 -0400195 void didRemoveRefOrPendingIO() const {
196 if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
197 delete this;
198 }
robertphillips1125a032016-11-16 11:17:17 -0800199 }
200
201 mutable int32_t fRefCnt;
202 mutable int32_t fPendingReads;
203 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400204};
205
206class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700207public:
Greg Daniel457469c2018-02-08 15:05:44 -0500208 enum class LazyInstantiationType {
Brian Salomon967df202018-12-07 11:15:53 -0500209 kSingleUse, // Instantiation callback is allowed to be called only once.
210 kMultipleUse, // Instantiation callback can be called multiple times.
Brian Salomon876a0172019-03-08 11:12:14 -0500211 kDeinstantiate, // Instantiation callback can be called multiple times,
212 // but we will deinstantiate the proxy after every flush.
Greg Daniel457469c2018-02-08 15:05:44 -0500213 };
214
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500215 enum class LazyState {
Greg Daniel0a375db2018-02-01 12:21:39 -0500216 kNot, // The proxy is instantiated or does not have a lazy callback
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500217 kPartially, // The proxy has a lazy callback but knows basic information about itself.
218 kFully, // The proxy has a lazy callback and also doesn't know its width, height, etc.
219 };
220
221 LazyState lazyInstantiationState() const {
Greg Daniel0a375db2018-02-01 12:21:39 -0500222 if (fTarget || !SkToBool(fLazyInstantiateCallback)) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500223 return LazyState::kNot;
224 } else {
225 if (fWidth <= 0) {
226 SkASSERT(fHeight <= 0);
227 return LazyState::kFully;
228 } else {
229 SkASSERT(fHeight > 0);
230 return LazyState::kPartially;
231 }
232 }
233 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700234
235 GrPixelConfig config() const { return fConfig; }
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500236 int width() const {
237 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
238 return fWidth;
239 }
240 int height() const {
241 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
242 return fHeight;
243 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000244
245 SkISize isize() const { return {fWidth, fHeight}; }
246
Chris Dalton706a6ff2017-11-29 22:01:06 -0700247 int worstCaseWidth() const;
248 int worstCaseHeight() const;
Brian Salomond8eb7b62018-05-25 10:08:05 -0400249 /**
250 * Helper that gets the width and height of the surface as a bounding rectangle.
251 */
252 SkRect getBoundsRect() const {
253 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
254 return SkRect::MakeIWH(this->width(), this->height());
255 }
256 /**
257 * Helper that gets the worst case width and height of the surface as a bounding rectangle.
258 */
259 SkRect getWorstCaseBoundsRect() const {
260 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
261 return SkRect::MakeIWH(this->worstCaseWidth(), this->worstCaseHeight());
262 }
263
robertphillips76948d42016-05-04 12:47:41 -0700264 GrSurfaceOrigin origin() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400265 SkASSERT(kTopLeft_GrSurfaceOrigin == fOrigin || kBottomLeft_GrSurfaceOrigin == fOrigin);
266 return fOrigin;
robertphillips76948d42016-05-04 12:47:41 -0700267 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700268
Greg Daniel4065d452018-11-16 15:43:41 -0500269 const GrBackendFormat& backendFormat() const { return fFormat; }
270
Robert Phillips294870f2016-11-11 12:38:40 -0500271 class UniqueID {
272 public:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400273 static UniqueID InvalidID() {
274 return UniqueID(uint32_t(SK_InvalidUniqueID));
275 }
276
Robert Phillips294870f2016-11-11 12:38:40 -0500277 // wrapped
278 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700279 // deferred and lazy-callback
Robert Phillips294870f2016-11-11 12:38:40 -0500280 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
281
282 uint32_t asUInt() const { return fID; }
283
284 bool operator==(const UniqueID& other) const {
285 return fID == other.fID;
286 }
287 bool operator!=(const UniqueID& other) const {
288 return !(*this == other);
289 }
290
Robert Phillips7ee385e2017-03-30 08:02:11 -0400291 void makeInvalid() { fID = SK_InvalidUniqueID; }
Robert Phillips294870f2016-11-11 12:38:40 -0500292 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
293
294 private:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400295 explicit UniqueID(uint32_t id) : fID(id) {}
296
297 uint32_t fID;
Robert Phillips294870f2016-11-11 12:38:40 -0500298 };
299
300 /*
301 * The contract for the uniqueID is:
302 * for wrapped resources:
303 * the uniqueID will match that of the wrapped resource
304 *
305 * for deferred resources:
306 * the uniqueID will be different from the real resource, when it is allocated
307 * the proxy's uniqueID will not change across the instantiate call
308 *
309 * the uniqueIDs of the proxies and the resources draw from the same pool
310 *
311 * What this boils down to is that the uniqueID of a proxy can be used to consistently
312 * track/identify a proxy but should never be used to distinguish between
313 * resources and proxies - beware!
314 */
315 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700316
Robert Phillips159e3c62017-06-19 12:02:00 -0400317 UniqueID underlyingUniqueID() const {
318 if (fTarget) {
319 return UniqueID(fTarget->uniqueID());
320 }
321
322 return fUniqueID;
323 }
324
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400325 virtual bool instantiate(GrResourceProvider* resourceProvider) = 0;
Robert Phillips37430132016-11-09 06:50:43 -0500326
Brian Salomon967df202018-12-07 11:15:53 -0500327 void deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500328
robertphillips76948d42016-05-04 12:47:41 -0700329 /**
Brian Salomon7d94bb52018-10-12 14:37:19 -0400330 * Proxies that are already instantiated and whose backing surface cannot be recycled to
331 * instantiate other proxies do not need to be considered by GrResourceAllocator.
332 */
333 bool canSkipResourceAllocator() const;
334
335 /**
robertphillips76948d42016-05-04 12:47:41 -0700336 * @return the texture proxy associated with the surface proxy, may be NULL.
337 */
338 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
339 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
340
341 /**
342 * @return the render target proxy associated with the surface proxy, may be NULL.
343 */
344 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
345 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
346
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400347 bool isInstantiated() const { return SkToBool(fTarget); }
348
349 // If the proxy is already instantiated, return its backing GrTexture; if not, return null.
350 GrSurface* peekSurface() const { return fTarget; }
351
352 // If this is a texture proxy and the proxy is already instantiated, return its backing
353 // GrTexture; if not, return null.
354 GrTexture* peekTexture() const { return fTarget ? fTarget->asTexture() : nullptr; }
355
356 // If this is a render target proxy and the proxy is already instantiated, return its backing
357 // GrRenderTarget; if not, return null.
358 GrRenderTarget* peekRenderTarget() const {
359 return fTarget ? fTarget->asRenderTarget() : nullptr;
360 }
361
robertphillips13a7eee2016-08-31 15:06:24 -0700362 /**
363 * Does the resource count against the resource budget?
364 */
365 SkBudgeted isBudgeted() const { return fBudgeted; }
366
Brian Salomonc67c31c2018-12-06 10:00:03 -0500367 /**
368 * The pixel values of this proxy's surface cannot be modified (e.g. doesn't support write
Brian Salomon9cadc312018-12-05 15:09:19 -0500369 * pixels or MIP map level regen). Read-only proxies also bypass interval tracking and
370 * assignment in GrResourceAllocator.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500371 */
372 bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; }
373
Robert Phillipsf2361d22016-10-25 14:20:06 -0400374 void setLastOpList(GrOpList* opList);
375 GrOpList* getLastOpList() { return fLastOpList; }
376
Brian Osman45580d32016-11-23 09:37:01 -0500377 GrRenderTargetOpList* getLastRenderTargetOpList();
378 GrTextureOpList* getLastTextureOpList();
379
Robert Phillips8bc06d02016-11-01 17:28:40 -0400380 /**
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400381 * Retrieves the amount of GPU memory that will be or currently is used by this resource
Robert Phillips8bc06d02016-11-01 17:28:40 -0400382 * in bytes. It is approximate since we aren't aware of additional padding or copies made
383 * by the driver.
384 *
385 * @return the amount of GPU memory used in bytes
386 */
387 size_t gpuMemorySize() const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500388 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400389 if (fTarget) {
390 return fTarget->gpuMemorySize();
391 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400392 if (kInvalidGpuMemorySize == fGpuMemorySize) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400393 fGpuMemorySize = this->onUninstantiatedGpuMemorySize();
Robert Phillips8bc06d02016-11-01 17:28:40 -0400394 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
395 }
396 return fGpuMemorySize;
397 }
398
Robert Phillipse2f7d182016-12-15 09:23:05 -0500399 // Helper function that creates a temporary SurfaceContext to perform the copy
Brian Salomonfee3f9b2018-12-19 12:34:12 -0500400 // The copy is is not a render target and not multisampled.
Robert Phillipsd44146b2019-02-15 14:44:28 -0500401 static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped,
402 SkIRect srcRect, SkBackingFit, SkBudgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500403
404 // Copy the entire 'src'
Robert Phillipsd44146b2019-02-15 14:44:28 -0500405 static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped,
406 SkBackingFit, SkBudgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500407
408 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd44146b2019-02-15 14:44:28 -0500409 static sk_sp<GrSurfaceContext> TestCopy(GrRecordingContext* context,
410 const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500411 GrSurfaceOrigin, GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500412
Robert Phillipseaa86252016-11-08 13:49:39 +0000413 bool isWrapped_ForTesting() const;
414
Robert Phillips292a6b22019-02-14 14:49:02 -0500415 SkDEBUGCODE(void validate(GrContext_Base*) const;)
Brian Osman45580d32016-11-23 09:37:01 -0500416
Robert Phillips757914d2017-01-25 15:48:30 -0500417 // Provides access to functions that aren't part of the public API.
Robert Phillips420c4cf2017-09-28 09:00:45 -0400418 inline GrSurfaceProxyPriv priv();
419 inline const GrSurfaceProxyPriv priv() const;
Robert Phillips757914d2017-01-25 15:48:30 -0500420
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400421 GrInternalSurfaceFlags testingOnly_getFlags() const;
422
robertphillips76948d42016-05-04 12:47:41 -0700423protected:
robertphillips8abb3702016-08-31 14:04:06 -0700424 // Deferred version
Greg Daniel4065d452018-11-16 15:43:41 -0500425 GrSurfaceProxy(const GrBackendFormat& format, const GrSurfaceDesc& desc,
426 GrSurfaceOrigin origin, SkBackingFit fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400427 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500428 : GrSurfaceProxy(nullptr, LazyInstantiationType::kSingleUse, format, desc, origin, fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400429 budgeted, surfaceFlags) {
Robert Phillips294870f2016-11-11 12:38:40 -0500430 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700431 }
432
Robert Phillipsce5209a2018-02-13 11:13:51 -0500433 using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>;
Robert Phillips777707b2018-01-17 11:40:14 -0500434
Chris Dalton706a6ff2017-11-29 22:01:06 -0700435 // Lazy-callback version
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400436 GrSurfaceProxy(LazyInstantiateCallback&&, LazyInstantiationType,
Greg Daniel4065d452018-11-16 15:43:41 -0500437 const GrBackendFormat& format, const GrSurfaceDesc&, GrSurfaceOrigin,
438 SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700439
Brian Salomonc67c31c2018-12-06 10:00:03 -0500440 // Wrapped version.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400441 GrSurfaceProxy(sk_sp<GrSurface>, GrSurfaceOrigin, SkBackingFit);
robertphillips76948d42016-05-04 12:47:41 -0700442
Robert Phillipsf2361d22016-10-25 14:20:06 -0400443 virtual ~GrSurfaceProxy();
444
Robert Phillips757914d2017-01-25 15:48:30 -0500445 friend class GrSurfaceProxyPriv;
446
447 // Methods made available via GrSurfaceProxyPriv
Robert Phillips715d08c2018-07-18 13:56:48 -0400448 int32_t getProxyRefCnt() const {
449 return this->internalGetProxyRefCnt();
450 }
451
Robert Phillips757914d2017-01-25 15:48:30 -0500452 bool hasPendingIO() const {
453 return this->internalHasPendingIO();
454 }
455
Robert Phillips7ee385e2017-03-30 08:02:11 -0400456 bool hasPendingWrite() const {
457 return this->internalHasPendingWrite();
458 }
459
Robert Phillips57aa3672017-07-21 11:38:13 -0400460 void computeScratchKey(GrScratchKey*) const;
461
Robert Phillips5af44de2017-07-18 14:49:38 -0400462 virtual sk_sp<GrSurface> createSurface(GrResourceProvider*) const = 0;
463 void assign(sk_sp<GrSurface> surface);
464
Robert Phillips65048132017-08-10 08:44:49 -0400465 sk_sp<GrSurface> createSurfaceImpl(GrResourceProvider*, int sampleCnt, bool needsStencil,
Robert Phillipsba5c4392018-07-25 12:37:14 -0400466 GrSurfaceDescFlags, GrMipMapped) const;
Robert Phillips5af44de2017-07-18 14:49:38 -0400467
Robert Phillips65048132017-08-10 08:44:49 -0400468 bool instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, bool needsStencil,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400469 GrSurfaceDescFlags descFlags, GrMipMapped, const GrUniqueKey*);
470
Greg Daniele3204862018-04-16 11:24:10 -0400471 // In many cases these flags aren't actually known until the proxy has been instantiated.
472 // However, Ganesh frequently needs to change its behavior based on these settings. For
473 // internally create proxies we will know these properties ahead of time. For wrapped
474 // proxies we will copy the properties off of the GrSurface. For lazy proxies we force the
475 // call sites to provide the required information ahead of time. At instantiation time
476 // we verify that the assumed properties match the actual properties.
477 GrInternalSurfaceFlags fSurfaceFlags;
478
Chris Dalton706a6ff2017-11-29 22:01:06 -0700479private:
Greg Daniel4065d452018-11-16 15:43:41 -0500480 // For wrapped resources, 'fFormat', 'fConfig', 'fWidth', 'fHeight', and 'fOrigin; will always
481 // be filled in from the wrapped resource.
482 GrBackendFormat fFormat;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400483 GrPixelConfig fConfig;
484 int fWidth;
485 int fHeight;
486 GrSurfaceOrigin fOrigin;
487 SkBackingFit fFit; // always kApprox for lazy-callback resources
488 // always kExact for wrapped resources
489 mutable SkBudgeted fBudgeted; // always kYes for lazy-callback resources
490 // set from the backing resource for wrapped resources
491 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400492
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400493 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700494
Chris Dalton706a6ff2017-11-29 22:01:06 -0700495 LazyInstantiateCallback fLazyInstantiateCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500496 // If this is set to kSingleuse, then after one call to fLazyInstantiateCallback we will cleanup
497 // the lazy callback and then delete it. This will allow for any refs and resources being held
498 // by the standard function to be released. This is specifically useful in non-dll cases where
499 // we make lazy proxies and instantiate them immediately.
500 // Note: This is ignored if fLazyInstantiateCallback is null.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400501 LazyInstantiationType fLazyInstantiationType;
Greg Daniel849dce12018-04-24 14:32:53 -0400502
503 SkDEBUGCODE(void validateSurface(const GrSurface*);)
504 SkDEBUGCODE(virtual void onValidateSurface(const GrSurface*) = 0;)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700505
Robert Phillips8bc06d02016-11-01 17:28:40 -0400506 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400507 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
508
Brian Salomonbb5711a2017-05-17 13:49:59 -0400509 virtual size_t onUninstantiatedGpuMemorySize() const = 0;
Robert Phillips29e52f12016-11-03 10:19:14 -0400510
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400511 bool fNeedsClear;
Brian Salomond17b4a62017-05-23 16:53:47 -0400512
Robert Phillips8bc06d02016-11-01 17:28:40 -0400513 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
514 // will be called but, when the proxy is deferred, it will compute the answer itself.
515 // If the proxy computes its own answer that answer is checked (in debug mode) in
516 // the instantiation method.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400517 mutable size_t fGpuMemorySize;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400518
Robert Phillipsf2361d22016-10-25 14:20:06 -0400519 // The last opList that wrote to or is currently going to write to this surface
Robert Phillipsb6deea82017-05-11 14:14:30 -0400520 // The opList can be closed (e.g., no surface context is currently bound
521 // to this proxy).
Robert Phillipsf2361d22016-10-25 14:20:06 -0400522 // This back-pointer is required so that we can add a dependancy between
523 // the opList used to create the current contents of this surface
524 // and the opList of a destination surface to which this one is being drawn or copied.
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400525 // This pointer is unreffed. OpLists own a ref on their surface proxies.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400526 GrOpList* fLastOpList;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400527
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400528 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700529};
530
531#endif