blob: 327ceb13a91b329031375b2d71cdc90b3189aa71 [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"
robertphillips76948d42016-05-04 12:47:41 -070012#include "GrGpuResource.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040013#include "GrSurface.h"
Robert Phillips93f16332016-11-23 19:37:13 -050014
csmartdaltonbf4a8f92016-09-06 10:01:06 -070015#include "SkRect.h"
robertphillips76948d42016-05-04 12:47:41 -070016
Greg Daniel7ef28f32017-04-20 16:41:55 +000017class GrBackendTexture;
Robert Phillips37430132016-11-09 06:50:43 -050018class GrCaps;
Robert Phillipsc589b0b2017-04-17 07:53:07 -040019class GrOpList;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050020class GrProxyProvider;
Brian Osman45580d32016-11-23 09:37:01 -050021class GrRenderTargetOpList;
22class GrRenderTargetProxy;
Brian Osman32342f02017-03-04 08:12:46 -050023class GrResourceProvider;
Robert Phillipsd46697a2017-01-25 12:10:37 -050024class GrSurfaceContext;
Robert Phillips757914d2017-01-25 15:48:30 -050025class GrSurfaceProxyPriv;
Brian Osman45580d32016-11-23 09:37:01 -050026class GrTextureOpList;
robertphillips76948d42016-05-04 12:47:41 -070027class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070028
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
Chris Daltona32a3c32017-12-05 10:05:21 -070054#ifdef SK_DEBUG
55 bool isUnique_debugOnly() const { // For asserts.
56 SkASSERT(fRefCnt >= 0 && fPendingWrites >= 0 && fPendingReads >= 0);
57 return 1 == fRefCnt + fPendingWrites + fPendingReads;
58 }
59#endif
60
Robert Phillips4bc70112018-03-01 10:24:02 -050061 void release() {
Greg Daniel4684f822018-03-08 15:27:36 -050062 // The proxy itself may still have multiple refs. It can be owned by an SkImage and multiple
63 // SkDeferredDisplayLists at the same time if we are using DDLs.
Robert Phillips4bc70112018-03-01 10:24:02 -050064 SkASSERT(0 == fPendingReads);
65 SkASSERT(0 == fPendingWrites);
66
Robert Phillipse4aae342018-03-14 10:26:57 -040067 SkASSERT(fRefCnt == fTarget->fRefCnt);
Robert Phillips4bc70112018-03-01 10:24:02 -050068 SkASSERT(!fTarget->internalHasPendingIO());
Robert Phillipse4aae342018-03-14 10:26:57 -040069 // In the current hybrid world, the proxy and backing surface are ref/unreffed in
70 // synchrony. In this instance we're deInstantiating the proxy so, regardless of the
71 // number of refs on the backing surface, we're going to remove it. If/when the proxy
72 // is re-instantiated all the refs on the proxy (presumably due to multiple uses in ops)
73 // will be transfered to the new surface.
74 for (int refs = fTarget->fRefCnt; refs; --refs) {
75 fTarget->unref();
76 }
Robert Phillips4bc70112018-03-01 10:24:02 -050077 fTarget = nullptr;
78 }
79
Robert Phillipsc7635fa2016-10-28 13:25:24 -040080 void validate() const {
Robert Phillipsb6deea82017-05-11 14:14:30 -040081#ifdef SK_DEBUG
82 SkASSERT(fRefCnt >= 0);
robertphillips1125a032016-11-16 11:17:17 -080083 SkASSERT(fPendingReads >= 0);
84 SkASSERT(fPendingWrites >= 0);
85 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1);
86
87 if (fTarget) {
robertphillips1125a032016-11-16 11:17:17 -080088 // The backing GrSurface can have more refs than the proxy if the proxy
89 // started off wrapping an external resource (that came in with refs).
90 // The GrSurface should never have fewer refs than the proxy however.
91 SkASSERT(fTarget->fRefCnt >= fRefCnt);
Robert Phillipsb6deea82017-05-11 14:14:30 -040092 SkASSERT(fTarget->fPendingReads >= fPendingReads);
93 SkASSERT(fTarget->fPendingWrites >= fPendingWrites);
robertphillips1125a032016-11-16 11:17:17 -080094 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -040095#endif
96 }
97
robertphillips1125a032016-11-16 11:17:17 -080098 int32_t getBackingRefCnt_TestOnly() const;
99 int32_t getPendingReadCnt_TestOnly() const;
100 int32_t getPendingWriteCnt_TestOnly() const;
101
Brian Salomoncf75b002017-08-16 10:42:09 -0400102 void addPendingRead() const {
103 this->validate();
104
105 ++fPendingReads;
106 if (fTarget) {
107 fTarget->addPendingRead();
108 }
109 }
110
111 void completedRead() const {
112 this->validate();
113
114 if (fTarget) {
115 fTarget->completedRead();
116 }
117
118 --fPendingReads;
119 this->didRemoveRefOrPendingIO();
120 }
121
122 void addPendingWrite() const {
123 this->validate();
124
125 ++fPendingWrites;
126 if (fTarget) {
127 fTarget->addPendingWrite();
128 }
129 }
130
131 void completedWrite() const {
132 this->validate();
133
134 if (fTarget) {
135 fTarget->completedWrite();
136 }
137
138 --fPendingWrites;
139 this->didRemoveRefOrPendingIO();
140 }
141
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400142protected:
robertphillips1125a032016-11-16 11:17:17 -0800143 GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {}
144 GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400145 // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing
146 // anything extra.
147 fTarget = surface.release();
148 }
149 virtual ~GrIORefProxy() {
150 // We don't unref 'fTarget' here since the 'unref' method will already
Robert Phillips4bc70112018-03-01 10:24:02 -0500151 // have forwarded on the unref call that got us here.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400152 }
153
robertphillips1125a032016-11-16 11:17:17 -0800154 // This GrIORefProxy was deferred before but has just been instantiated. To
155 // make all the reffing & unreffing work out we now need to transfer any deferred
156 // refs & unrefs to the new GrSurface
157 void transferRefs() {
158 SkASSERT(fTarget);
159
Robert Phillipsf8e25022017-11-08 15:24:31 -0500160 SkASSERT(fTarget->fRefCnt > 0);
Robert Phillips2d9cb572017-11-13 13:38:05 +0000161 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
robertphillips1125a032016-11-16 11:17:17 -0800162 fTarget->fPendingReads += fPendingReads;
163 fTarget->fPendingWrites += fPendingWrites;
robertphillips1125a032016-11-16 11:17:17 -0800164 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400165
Robert Phillips715d08c2018-07-18 13:56:48 -0400166 int32_t internalGetProxyRefCnt() const {
167 return fRefCnt;
168 }
169
Robert Phillips757914d2017-01-25 15:48:30 -0500170 bool internalHasPendingIO() const {
171 if (fTarget) {
172 return fTarget->internalHasPendingIO();
173 }
174
175 return SkToBool(fPendingWrites | fPendingReads);
176 }
177
Robert Phillips7ee385e2017-03-30 08:02:11 -0400178 bool internalHasPendingWrite() const {
179 if (fTarget) {
180 return fTarget->internalHasPendingWrite();
181 }
182
183 return SkToBool(fPendingWrites);
184 }
185
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400186 // For deferred proxies this will be null. For wrapped proxies it will point to the
187 // wrapped resource.
188 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800189
190private:
191 // This class is used to manage conversion of refs to pending reads/writes.
Robert Phillipsb6deea82017-05-11 14:14:30 -0400192 friend class GrSurfaceProxyRef;
robertphillips1125a032016-11-16 11:17:17 -0800193 template <typename, GrIOType> friend class GrPendingIOResource;
194
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 {
Greg Daniel4684f822018-03-08 15:27:36 -0500209 kSingleUse, // Instantiation callback is allowed to be called only once
210 kMultipleUse, // Instantiation callback can be called multiple times.
211 kUninstantiate, // Instantiation callback can be called multiple times,
212 // but we will uninstantiate 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 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700244 int worstCaseWidth() const;
245 int worstCaseHeight() const;
Brian Salomond8eb7b62018-05-25 10:08:05 -0400246 /**
247 * Helper that gets the width and height of the surface as a bounding rectangle.
248 */
249 SkRect getBoundsRect() const {
250 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
251 return SkRect::MakeIWH(this->width(), this->height());
252 }
253 /**
254 * Helper that gets the worst case width and height of the surface as a bounding rectangle.
255 */
256 SkRect getWorstCaseBoundsRect() const {
257 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
258 return SkRect::MakeIWH(this->worstCaseWidth(), this->worstCaseHeight());
259 }
260
robertphillips76948d42016-05-04 12:47:41 -0700261 GrSurfaceOrigin origin() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400262 SkASSERT(kTopLeft_GrSurfaceOrigin == fOrigin || kBottomLeft_GrSurfaceOrigin == fOrigin);
263 return fOrigin;
robertphillips76948d42016-05-04 12:47:41 -0700264 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700265
Robert Phillips294870f2016-11-11 12:38:40 -0500266 class UniqueID {
267 public:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400268 static UniqueID InvalidID() {
269 return UniqueID(uint32_t(SK_InvalidUniqueID));
270 }
271
Robert Phillips294870f2016-11-11 12:38:40 -0500272 // wrapped
273 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700274 // deferred and lazy-callback
Robert Phillips294870f2016-11-11 12:38:40 -0500275 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
276
277 uint32_t asUInt() const { return fID; }
278
279 bool operator==(const UniqueID& other) const {
280 return fID == other.fID;
281 }
282 bool operator!=(const UniqueID& other) const {
283 return !(*this == other);
284 }
285
Robert Phillips7ee385e2017-03-30 08:02:11 -0400286 void makeInvalid() { fID = SK_InvalidUniqueID; }
Robert Phillips294870f2016-11-11 12:38:40 -0500287 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
288
289 private:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400290 explicit UniqueID(uint32_t id) : fID(id) {}
291
292 uint32_t fID;
Robert Phillips294870f2016-11-11 12:38:40 -0500293 };
294
295 /*
296 * The contract for the uniqueID is:
297 * for wrapped resources:
298 * the uniqueID will match that of the wrapped resource
299 *
300 * for deferred resources:
301 * the uniqueID will be different from the real resource, when it is allocated
302 * the proxy's uniqueID will not change across the instantiate call
303 *
304 * the uniqueIDs of the proxies and the resources draw from the same pool
305 *
306 * What this boils down to is that the uniqueID of a proxy can be used to consistently
307 * track/identify a proxy but should never be used to distinguish between
308 * resources and proxies - beware!
309 */
310 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700311
Robert Phillips159e3c62017-06-19 12:02:00 -0400312 UniqueID underlyingUniqueID() const {
313 if (fTarget) {
314 return UniqueID(fTarget->uniqueID());
315 }
316
317 return fUniqueID;
318 }
319
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400320 virtual bool instantiate(GrResourceProvider* resourceProvider) = 0;
Robert Phillips37430132016-11-09 06:50:43 -0500321
Robert Phillips4bc70112018-03-01 10:24:02 -0500322 void deInstantiate();
323
robertphillips76948d42016-05-04 12:47:41 -0700324 /**
325 * @return the texture proxy associated with the surface proxy, may be NULL.
326 */
327 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
328 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
329
330 /**
331 * @return the render target proxy associated with the surface proxy, may be NULL.
332 */
333 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
334 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
335
robertphillips13a7eee2016-08-31 15:06:24 -0700336 /**
337 * Does the resource count against the resource budget?
338 */
339 SkBudgeted isBudgeted() const { return fBudgeted; }
340
Robert Phillipsf2361d22016-10-25 14:20:06 -0400341 void setLastOpList(GrOpList* opList);
342 GrOpList* getLastOpList() { return fLastOpList; }
343
Brian Osman45580d32016-11-23 09:37:01 -0500344 GrRenderTargetOpList* getLastRenderTargetOpList();
345 GrTextureOpList* getLastTextureOpList();
346
Robert Phillips8bc06d02016-11-01 17:28:40 -0400347 /**
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400348 * Retrieves the amount of GPU memory that will be or currently is used by this resource
Robert Phillips8bc06d02016-11-01 17:28:40 -0400349 * in bytes. It is approximate since we aren't aware of additional padding or copies made
350 * by the driver.
351 *
352 * @return the amount of GPU memory used in bytes
353 */
354 size_t gpuMemorySize() const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500355 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400356 if (fTarget) {
357 return fTarget->gpuMemorySize();
358 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400359 if (kInvalidGpuMemorySize == fGpuMemorySize) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400360 fGpuMemorySize = this->onUninstantiatedGpuMemorySize();
Robert Phillips8bc06d02016-11-01 17:28:40 -0400361 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
362 }
363 return fGpuMemorySize;
364 }
365
Robert Phillipse2f7d182016-12-15 09:23:05 -0500366 // Helper function that creates a temporary SurfaceContext to perform the copy
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400367 // It always returns a kExact-backed proxy bc it is used when converting an SkSpecialImage
Brian Salomon63e79732017-05-15 21:23:13 -0400368 // to an SkImage. The copy is is not a render target and not multisampled.
Greg Daniel65c7f662017-10-30 13:39:09 -0400369 static sk_sp<GrTextureProxy> Copy(GrContext*, GrSurfaceProxy* src, GrMipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500370 SkIRect srcRect, SkBudgeted);
371
372 // Copy the entire 'src'
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400373 // It always returns a kExact-backed proxy bc it is used in SkGpuDevice::snapSpecial
Greg Daniel65c7f662017-10-30 13:39:09 -0400374 static sk_sp<GrTextureProxy> Copy(GrContext* context, GrSurfaceProxy* src, GrMipMapped,
Robert Phillips63c67462017-02-15 14:19:01 -0500375 SkBudgeted budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500376
377 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd46697a2017-01-25 12:10:37 -0500378 static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500379 GrSurfaceOrigin, GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500380
Robert Phillipseaa86252016-11-08 13:49:39 +0000381 bool isWrapped_ForTesting() const;
382
Brian Osman45580d32016-11-23 09:37:01 -0500383 SkDEBUGCODE(void validate(GrContext*) const;)
384
Robert Phillips757914d2017-01-25 15:48:30 -0500385 // Provides access to functions that aren't part of the public API.
Robert Phillips420c4cf2017-09-28 09:00:45 -0400386 inline GrSurfaceProxyPriv priv();
387 inline const GrSurfaceProxyPriv priv() const;
Robert Phillips757914d2017-01-25 15:48:30 -0500388
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400389 GrInternalSurfaceFlags testingOnly_getFlags() const;
390
robertphillips76948d42016-05-04 12:47:41 -0700391protected:
robertphillips8abb3702016-08-31 14:04:06 -0700392 // Deferred version
Brian Salomon2a4f9832018-03-03 22:43:43 -0500393 GrSurfaceProxy(const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400394 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Brian Salomon2a4f9832018-03-03 22:43:43 -0500395 : GrSurfaceProxy(nullptr, LazyInstantiationType::kSingleUse, desc, origin, fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400396 budgeted, surfaceFlags) {
Robert Phillips294870f2016-11-11 12:38:40 -0500397 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700398 }
399
Robert Phillipsce5209a2018-02-13 11:13:51 -0500400 using LazyInstantiateCallback = std::function<sk_sp<GrSurface>(GrResourceProvider*)>;
Robert Phillips777707b2018-01-17 11:40:14 -0500401
Chris Dalton706a6ff2017-11-29 22:01:06 -0700402 // Lazy-callback version
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400403 GrSurfaceProxy(LazyInstantiateCallback&&, LazyInstantiationType,
404 const GrSurfaceDesc&, GrSurfaceOrigin, SkBackingFit,
405 SkBudgeted, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700406
robertphillips8abb3702016-08-31 14:04:06 -0700407 // Wrapped version
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400408 GrSurfaceProxy(sk_sp<GrSurface>, GrSurfaceOrigin, SkBackingFit);
robertphillips76948d42016-05-04 12:47:41 -0700409
Robert Phillipsf2361d22016-10-25 14:20:06 -0400410 virtual ~GrSurfaceProxy();
411
Robert Phillips757914d2017-01-25 15:48:30 -0500412 friend class GrSurfaceProxyPriv;
413
414 // Methods made available via GrSurfaceProxyPriv
Robert Phillips715d08c2018-07-18 13:56:48 -0400415 int32_t getProxyRefCnt() const {
416 return this->internalGetProxyRefCnt();
417 }
418
Robert Phillips757914d2017-01-25 15:48:30 -0500419 bool hasPendingIO() const {
420 return this->internalHasPendingIO();
421 }
422
Robert Phillips7ee385e2017-03-30 08:02:11 -0400423 bool hasPendingWrite() const {
424 return this->internalHasPendingWrite();
425 }
426
Robert Phillips57aa3672017-07-21 11:38:13 -0400427 void computeScratchKey(GrScratchKey*) const;
428
Robert Phillips5af44de2017-07-18 14:49:38 -0400429 virtual sk_sp<GrSurface> createSurface(GrResourceProvider*) const = 0;
430 void assign(sk_sp<GrSurface> surface);
431
Robert Phillips65048132017-08-10 08:44:49 -0400432 sk_sp<GrSurface> createSurfaceImpl(GrResourceProvider*, int sampleCnt, bool needsStencil,
Robert Phillipsba5c4392018-07-25 12:37:14 -0400433 GrSurfaceDescFlags, GrMipMapped) const;
Robert Phillips5af44de2017-07-18 14:49:38 -0400434
Robert Phillips65048132017-08-10 08:44:49 -0400435 bool instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, bool needsStencil,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400436 GrSurfaceDescFlags descFlags, GrMipMapped, const GrUniqueKey*);
437
Greg Daniele3204862018-04-16 11:24:10 -0400438 // In many cases these flags aren't actually known until the proxy has been instantiated.
439 // However, Ganesh frequently needs to change its behavior based on these settings. For
440 // internally create proxies we will know these properties ahead of time. For wrapped
441 // proxies we will copy the properties off of the GrSurface. For lazy proxies we force the
442 // call sites to provide the required information ahead of time. At instantiation time
443 // we verify that the assumed properties match the actual properties.
444 GrInternalSurfaceFlags fSurfaceFlags;
445
Chris Dalton706a6ff2017-11-29 22:01:06 -0700446private:
Brian Salomonbb5711a2017-05-17 13:49:59 -0400447 // For wrapped resources, 'fConfig', 'fWidth', 'fHeight', and 'fOrigin; will always be filled in
448 // from the wrapped resource.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400449 GrPixelConfig fConfig;
450 int fWidth;
451 int fHeight;
452 GrSurfaceOrigin fOrigin;
453 SkBackingFit fFit; // always kApprox for lazy-callback resources
454 // always kExact for wrapped resources
455 mutable SkBudgeted fBudgeted; // always kYes for lazy-callback resources
456 // set from the backing resource for wrapped resources
457 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400458
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400459 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700460
Chris Dalton706a6ff2017-11-29 22:01:06 -0700461 LazyInstantiateCallback fLazyInstantiateCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500462 // If this is set to kSingleuse, then after one call to fLazyInstantiateCallback we will cleanup
463 // the lazy callback and then delete it. This will allow for any refs and resources being held
464 // by the standard function to be released. This is specifically useful in non-dll cases where
465 // we make lazy proxies and instantiate them immediately.
466 // Note: This is ignored if fLazyInstantiateCallback is null.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400467 LazyInstantiationType fLazyInstantiationType;
Greg Daniel849dce12018-04-24 14:32:53 -0400468
469 SkDEBUGCODE(void validateSurface(const GrSurface*);)
470 SkDEBUGCODE(virtual void onValidateSurface(const GrSurface*) = 0;)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700471
Robert Phillips8bc06d02016-11-01 17:28:40 -0400472 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400473 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
474
Brian Salomonbb5711a2017-05-17 13:49:59 -0400475 virtual size_t onUninstantiatedGpuMemorySize() const = 0;
Robert Phillips29e52f12016-11-03 10:19:14 -0400476
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400477 bool fNeedsClear;
Brian Salomond17b4a62017-05-23 16:53:47 -0400478
Robert Phillips8bc06d02016-11-01 17:28:40 -0400479 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
480 // will be called but, when the proxy is deferred, it will compute the answer itself.
481 // If the proxy computes its own answer that answer is checked (in debug mode) in
482 // the instantiation method.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400483 mutable size_t fGpuMemorySize;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400484
Robert Phillipsf2361d22016-10-25 14:20:06 -0400485 // The last opList that wrote to or is currently going to write to this surface
Robert Phillipsb6deea82017-05-11 14:14:30 -0400486 // The opList can be closed (e.g., no surface context is currently bound
487 // to this proxy).
Robert Phillipsf2361d22016-10-25 14:20:06 -0400488 // This back-pointer is required so that we can add a dependancy between
489 // the opList used to create the current contents of this surface
490 // and the opList of a destination surface to which this one is being drawn or copied.
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400491 // This pointer is unreffed. OpLists own a ref on their surface proxies.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400492 GrOpList* fLastOpList;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400493
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400494 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700495};
496
497#endif