blob: 57dfaeda5eab6a3d943c5c925ae37c46a52dc76c [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"
Brian Salomonb6a3a3b2019-04-01 12:29:34 -040015#include "GrTexture.h"
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 Phillipsd44146b2019-02-15 14:44:28 -050021class GrRecordingContext;
Brian Osman45580d32016-11-23 09:37:01 -050022class GrRenderTargetOpList;
23class GrRenderTargetProxy;
Brian Osman32342f02017-03-04 08:12:46 -050024class GrResourceProvider;
Robert Phillipsd46697a2017-01-25 12:10:37 -050025class GrSurfaceContext;
Robert Phillips757914d2017-01-25 15:48:30 -050026class GrSurfaceProxyPriv;
Brian Osman45580d32016-11-23 09:37:01 -050027class GrTextureOpList;
robertphillips76948d42016-05-04 12:47:41 -070028class GrTextureProxy;
robertphillips76948d42016-05-04 12:47:41 -070029
Robert Phillipsc7635fa2016-10-28 13:25:24 -040030// This class replicates the functionality GrIORef<GrSurface> but tracks the
31// utilitization for later resource allocation (for the deferred case) and
32// forwards on the utilization in the wrapped case
33class GrIORefProxy : public SkNoncopyable {
34public:
35 void ref() const {
36 this->validate();
37
38 ++fRefCnt;
39 if (fTarget) {
40 fTarget->ref();
41 }
42 }
43
44 void unref() const {
45 this->validate();
46
47 if (fTarget) {
48 fTarget->unref();
49 }
50
Robert Phillipsb6deea82017-05-11 14:14:30 -040051 --fRefCnt;
52 this->didRemoveRefOrPendingIO();
Robert Phillipsc7635fa2016-10-28 13:25:24 -040053 }
54
Chris Daltona32a3c32017-12-05 10:05:21 -070055#ifdef SK_DEBUG
56 bool isUnique_debugOnly() const { // For asserts.
57 SkASSERT(fRefCnt >= 0 && fPendingWrites >= 0 && fPendingReads >= 0);
58 return 1 == fRefCnt + fPendingWrites + fPendingReads;
59 }
60#endif
61
Robert Phillips4bc70112018-03-01 10:24:02 -050062 void release() {
Greg Daniel4684f822018-03-08 15:27:36 -050063 // The proxy itself may still have multiple refs. It can be owned by an SkImage and multiple
64 // SkDeferredDisplayLists at the same time if we are using DDLs.
Robert Phillips4bc70112018-03-01 10:24:02 -050065 SkASSERT(0 == fPendingReads);
66 SkASSERT(0 == fPendingWrites);
67
Robert Phillipse4aae342018-03-14 10:26:57 -040068 // In the current hybrid world, the proxy and backing surface are ref/unreffed in
Brian Salomon0d606762019-01-25 09:58:38 -050069 // synchrony. Each ref we've added or removed to the proxy was mirrored to the backing
70 // surface. Though, that backing surface could be owned by other proxies as well. Remove
71 // a ref from the backing surface for each ref the proxy has since we are about to remove
72 // our pointer to the surface. If this proxy is reinstantiated then all the proxy's refs
73 // get transferred to the (possibly new) backing surface.
74 for (int refs = fRefCnt; refs; --refs) {
Robert Phillipse4aae342018-03-14 10:26:57 -040075 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
Brian Salomon01ceae92019-04-02 11:49:54 -0400154 // Privileged method that allows going from ref count = 0 to ref count = 1.
155 void addInitialRef() const {
156 this->validate();
157 ++fRefCnt;
158 if (fTarget) {
159 fTarget->proxyAccess().ref();
160 }
161 }
162
robertphillips1125a032016-11-16 11:17:17 -0800163 // This GrIORefProxy was deferred before but has just been instantiated. To
164 // make all the reffing & unreffing work out we now need to transfer any deferred
165 // refs & unrefs to the new GrSurface
166 void transferRefs() {
167 SkASSERT(fTarget);
168
Robert Phillipsf8e25022017-11-08 15:24:31 -0500169 SkASSERT(fTarget->fRefCnt > 0);
Robert Phillips2d9cb572017-11-13 13:38:05 +0000170 fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
robertphillips1125a032016-11-16 11:17:17 -0800171 fTarget->fPendingReads += fPendingReads;
172 fTarget->fPendingWrites += fPendingWrites;
robertphillips1125a032016-11-16 11:17:17 -0800173 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400174
Robert Phillips715d08c2018-07-18 13:56:48 -0400175 int32_t internalGetProxyRefCnt() const {
176 return fRefCnt;
177 }
178
Robert Phillips757914d2017-01-25 15:48:30 -0500179 bool internalHasPendingIO() const {
180 if (fTarget) {
181 return fTarget->internalHasPendingIO();
182 }
183
184 return SkToBool(fPendingWrites | fPendingReads);
185 }
186
Robert Phillips7ee385e2017-03-30 08:02:11 -0400187 bool internalHasPendingWrite() const {
188 if (fTarget) {
189 return fTarget->internalHasPendingWrite();
190 }
191
192 return SkToBool(fPendingWrites);
193 }
194
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400195 // For deferred proxies this will be null. For wrapped proxies it will point to the
196 // wrapped resource.
197 GrSurface* fTarget;
robertphillips1125a032016-11-16 11:17:17 -0800198
199private:
200 // This class is used to manage conversion of refs to pending reads/writes.
Brian Salomonee783962018-08-01 09:55:10 -0400201 template <typename> friend class GrProxyRef;
robertphillips1125a032016-11-16 11:17:17 -0800202
Robert Phillipsb6deea82017-05-11 14:14:30 -0400203 void didRemoveRefOrPendingIO() const {
204 if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
205 delete this;
206 }
robertphillips1125a032016-11-16 11:17:17 -0800207 }
208
209 mutable int32_t fRefCnt;
210 mutable int32_t fPendingReads;
211 mutable int32_t fPendingWrites;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400212};
213
214class GrSurfaceProxy : public GrIORefProxy {
robertphillips76948d42016-05-04 12:47:41 -0700215public:
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400216 /**
217 * Some lazy proxy callbacks want to set their own (or no key) on the GrSurfaces they return.
218 * Others want the GrSurface's key to be kept in sync with the proxy's key. This enum controls
219 * the key relationship between proxies and their targets.
220 */
221 enum class LazyInstantiationKeyMode {
222 /**
223 * Don't key the GrSurface with the proxy's key. The lazy instantiation callback is free to
224 * return a GrSurface that already has a unique key unrelated to the proxy's key.
225 */
226 kUnsynced,
227 /**
228 * Keep the GrSurface's unique key in sync with the proxy's unique key. The GrSurface
229 * returned from the lazy instantiation callback must not have a unique key or have the same
230 * same unique key as the proxy. If the proxy is later assigned a key it is in turn assigned
231 * to the GrSurface.
232 */
233 kSynced
234 };
235
236 struct LazyInstantiationResult {
237 LazyInstantiationResult() = default;
238 LazyInstantiationResult(const LazyInstantiationResult&) = default;
239 LazyInstantiationResult(LazyInstantiationResult&& that) = default;
240 LazyInstantiationResult(sk_sp<GrSurface> surf,
241 LazyInstantiationKeyMode mode = LazyInstantiationKeyMode::kSynced)
242 : fSurface(std::move(surf)), fKeyMode(mode) {}
243 LazyInstantiationResult(sk_sp<GrTexture> tex)
244 : LazyInstantiationResult(sk_sp<GrSurface>(std::move(tex))) {}
245
246 LazyInstantiationResult& operator=(const LazyInstantiationResult&) = default;
247 LazyInstantiationResult& operator=(LazyInstantiationResult&&) = default;
248
249 sk_sp<GrSurface> fSurface;
250 LazyInstantiationKeyMode fKeyMode = LazyInstantiationKeyMode::kSynced;
251 };
252
253 using LazyInstantiateCallback = std::function<LazyInstantiationResult(GrResourceProvider*)>;
254
Greg Daniel457469c2018-02-08 15:05:44 -0500255 enum class LazyInstantiationType {
Brian Salomon967df202018-12-07 11:15:53 -0500256 kSingleUse, // Instantiation callback is allowed to be called only once.
257 kMultipleUse, // Instantiation callback can be called multiple times.
Brian Salomon876a0172019-03-08 11:12:14 -0500258 kDeinstantiate, // Instantiation callback can be called multiple times,
259 // but we will deinstantiate the proxy after every flush.
Greg Daniel457469c2018-02-08 15:05:44 -0500260 };
261
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500262 enum class LazyState {
Greg Daniel0a375db2018-02-01 12:21:39 -0500263 kNot, // The proxy is instantiated or does not have a lazy callback
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500264 kPartially, // The proxy has a lazy callback but knows basic information about itself.
265 kFully, // The proxy has a lazy callback and also doesn't know its width, height, etc.
266 };
267
268 LazyState lazyInstantiationState() const {
Greg Daniel0a375db2018-02-01 12:21:39 -0500269 if (fTarget || !SkToBool(fLazyInstantiateCallback)) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500270 return LazyState::kNot;
271 } else {
272 if (fWidth <= 0) {
273 SkASSERT(fHeight <= 0);
274 return LazyState::kFully;
275 } else {
276 SkASSERT(fHeight > 0);
277 return LazyState::kPartially;
278 }
279 }
280 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700281
282 GrPixelConfig config() const { return fConfig; }
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500283 int width() const {
284 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
285 return fWidth;
286 }
287 int height() const {
288 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
289 return fHeight;
290 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000291
292 SkISize isize() const { return {fWidth, fHeight}; }
293
Chris Dalton706a6ff2017-11-29 22:01:06 -0700294 int worstCaseWidth() const;
295 int worstCaseHeight() const;
Brian Salomond8eb7b62018-05-25 10:08:05 -0400296 /**
297 * Helper that gets the width and height of the surface as a bounding rectangle.
298 */
299 SkRect getBoundsRect() const {
300 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
301 return SkRect::MakeIWH(this->width(), this->height());
302 }
303 /**
304 * Helper that gets the worst case width and height of the surface as a bounding rectangle.
305 */
306 SkRect getWorstCaseBoundsRect() const {
307 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
308 return SkRect::MakeIWH(this->worstCaseWidth(), this->worstCaseHeight());
309 }
310
robertphillips76948d42016-05-04 12:47:41 -0700311 GrSurfaceOrigin origin() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400312 SkASSERT(kTopLeft_GrSurfaceOrigin == fOrigin || kBottomLeft_GrSurfaceOrigin == fOrigin);
313 return fOrigin;
robertphillips76948d42016-05-04 12:47:41 -0700314 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700315
Greg Daniel4065d452018-11-16 15:43:41 -0500316 const GrBackendFormat& backendFormat() const { return fFormat; }
317
Robert Phillips294870f2016-11-11 12:38:40 -0500318 class UniqueID {
319 public:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400320 static UniqueID InvalidID() {
321 return UniqueID(uint32_t(SK_InvalidUniqueID));
322 }
323
Robert Phillips294870f2016-11-11 12:38:40 -0500324 // wrapped
325 explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700326 // deferred and lazy-callback
Robert Phillips294870f2016-11-11 12:38:40 -0500327 UniqueID() : fID(GrGpuResource::CreateUniqueID()) { }
328
329 uint32_t asUInt() const { return fID; }
330
331 bool operator==(const UniqueID& other) const {
332 return fID == other.fID;
333 }
334 bool operator!=(const UniqueID& other) const {
335 return !(*this == other);
336 }
337
Robert Phillips7ee385e2017-03-30 08:02:11 -0400338 void makeInvalid() { fID = SK_InvalidUniqueID; }
Robert Phillips294870f2016-11-11 12:38:40 -0500339 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
340
341 private:
Robert Phillips7ee385e2017-03-30 08:02:11 -0400342 explicit UniqueID(uint32_t id) : fID(id) {}
343
344 uint32_t fID;
Robert Phillips294870f2016-11-11 12:38:40 -0500345 };
346
347 /*
348 * The contract for the uniqueID is:
349 * for wrapped resources:
350 * the uniqueID will match that of the wrapped resource
351 *
352 * for deferred resources:
353 * the uniqueID will be different from the real resource, when it is allocated
354 * the proxy's uniqueID will not change across the instantiate call
355 *
356 * the uniqueIDs of the proxies and the resources draw from the same pool
357 *
358 * What this boils down to is that the uniqueID of a proxy can be used to consistently
359 * track/identify a proxy but should never be used to distinguish between
360 * resources and proxies - beware!
361 */
362 UniqueID uniqueID() const { return fUniqueID; }
robertphillips76948d42016-05-04 12:47:41 -0700363
Robert Phillips159e3c62017-06-19 12:02:00 -0400364 UniqueID underlyingUniqueID() const {
365 if (fTarget) {
366 return UniqueID(fTarget->uniqueID());
367 }
368
369 return fUniqueID;
370 }
371
Robert Phillips73cc4e82019-04-01 07:46:13 -0400372 virtual bool instantiate(GrResourceProvider* resourceProvider,
373 bool dontForceNoPendingIO = false) = 0;
Robert Phillips37430132016-11-09 06:50:43 -0500374
Brian Salomon967df202018-12-07 11:15:53 -0500375 void deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500376
robertphillips76948d42016-05-04 12:47:41 -0700377 /**
Brian Salomon7d94bb52018-10-12 14:37:19 -0400378 * Proxies that are already instantiated and whose backing surface cannot be recycled to
379 * instantiate other proxies do not need to be considered by GrResourceAllocator.
380 */
381 bool canSkipResourceAllocator() const;
382
383 /**
robertphillips76948d42016-05-04 12:47:41 -0700384 * @return the texture proxy associated with the surface proxy, may be NULL.
385 */
386 virtual GrTextureProxy* asTextureProxy() { return nullptr; }
387 virtual const GrTextureProxy* asTextureProxy() const { return nullptr; }
388
389 /**
390 * @return the render target proxy associated with the surface proxy, may be NULL.
391 */
392 virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; }
393 virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; }
394
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400395 bool isInstantiated() const { return SkToBool(fTarget); }
396
397 // If the proxy is already instantiated, return its backing GrTexture; if not, return null.
398 GrSurface* peekSurface() const { return fTarget; }
399
400 // If this is a texture proxy and the proxy is already instantiated, return its backing
401 // GrTexture; if not, return null.
402 GrTexture* peekTexture() const { return fTarget ? fTarget->asTexture() : nullptr; }
403
404 // If this is a render target proxy and the proxy is already instantiated, return its backing
405 // GrRenderTarget; if not, return null.
406 GrRenderTarget* peekRenderTarget() const {
407 return fTarget ? fTarget->asRenderTarget() : nullptr;
408 }
409
robertphillips13a7eee2016-08-31 15:06:24 -0700410 /**
411 * Does the resource count against the resource budget?
412 */
413 SkBudgeted isBudgeted() const { return fBudgeted; }
414
Brian Salomonc67c31c2018-12-06 10:00:03 -0500415 /**
416 * The pixel values of this proxy's surface cannot be modified (e.g. doesn't support write
Brian Salomon9cadc312018-12-05 15:09:19 -0500417 * pixels or MIP map level regen). Read-only proxies also bypass interval tracking and
418 * assignment in GrResourceAllocator.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500419 */
420 bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; }
421
Robert Phillipsf2361d22016-10-25 14:20:06 -0400422 void setLastOpList(GrOpList* opList);
423 GrOpList* getLastOpList() { return fLastOpList; }
424
Brian Osman45580d32016-11-23 09:37:01 -0500425 GrRenderTargetOpList* getLastRenderTargetOpList();
426 GrTextureOpList* getLastTextureOpList();
427
Robert Phillips8bc06d02016-11-01 17:28:40 -0400428 /**
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400429 * Retrieves the amount of GPU memory that will be or currently is used by this resource
Robert Phillips8bc06d02016-11-01 17:28:40 -0400430 * in bytes. It is approximate since we aren't aware of additional padding or copies made
431 * by the driver.
432 *
433 * @return the amount of GPU memory used in bytes
434 */
435 size_t gpuMemorySize() const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500436 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Brian Salomonbb5711a2017-05-17 13:49:59 -0400437 if (fTarget) {
438 return fTarget->gpuMemorySize();
439 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400440 if (kInvalidGpuMemorySize == fGpuMemorySize) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400441 fGpuMemorySize = this->onUninstantiatedGpuMemorySize();
Robert Phillips8bc06d02016-11-01 17:28:40 -0400442 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
443 }
444 return fGpuMemorySize;
445 }
446
Robert Phillipse2f7d182016-12-15 09:23:05 -0500447 // Helper function that creates a temporary SurfaceContext to perform the copy
Brian Salomonfee3f9b2018-12-19 12:34:12 -0500448 // The copy is is not a render target and not multisampled.
Robert Phillipsd44146b2019-02-15 14:44:28 -0500449 static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped,
450 SkIRect srcRect, SkBackingFit, SkBudgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500451
452 // Copy the entire 'src'
Robert Phillipsd44146b2019-02-15 14:44:28 -0500453 static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped,
454 SkBackingFit, SkBudgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500455
456 // Test-only entry point - should decrease in use as proxies propagate
Robert Phillipsd44146b2019-02-15 14:44:28 -0500457 static sk_sp<GrSurfaceContext> TestCopy(GrRecordingContext* context,
458 const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500459 GrSurfaceOrigin, GrSurfaceProxy* srcProxy);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500460
Robert Phillipseaa86252016-11-08 13:49:39 +0000461 bool isWrapped_ForTesting() const;
462
Robert Phillips292a6b22019-02-14 14:49:02 -0500463 SkDEBUGCODE(void validate(GrContext_Base*) const;)
Brian Osman45580d32016-11-23 09:37:01 -0500464
Robert Phillips757914d2017-01-25 15:48:30 -0500465 // Provides access to functions that aren't part of the public API.
Robert Phillips420c4cf2017-09-28 09:00:45 -0400466 inline GrSurfaceProxyPriv priv();
467 inline const GrSurfaceProxyPriv priv() const;
Robert Phillips757914d2017-01-25 15:48:30 -0500468
Brian Salomon01ceae92019-04-02 11:49:54 -0400469 /**
470 * Provides privileged access to select callers to be able to add a ref to a GrSurfaceProxy
471 * with zero refs.
472 */
473 class FirstRefAccess;
474 inline FirstRefAccess firstRefAccess();
475
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400476 GrInternalSurfaceFlags testingOnly_getFlags() const;
477
robertphillips76948d42016-05-04 12:47:41 -0700478protected:
robertphillips8abb3702016-08-31 14:04:06 -0700479 // Deferred version
Greg Daniel4065d452018-11-16 15:43:41 -0500480 GrSurfaceProxy(const GrBackendFormat& format, const GrSurfaceDesc& desc,
481 GrSurfaceOrigin origin, SkBackingFit fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400482 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -0500483 : GrSurfaceProxy(nullptr, LazyInstantiationType::kSingleUse, format, desc, origin, fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400484 budgeted, surfaceFlags) {
Robert Phillips294870f2016-11-11 12:38:40 -0500485 // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources
robertphillips8abb3702016-08-31 14:04:06 -0700486 }
487
Chris Dalton706a6ff2017-11-29 22:01:06 -0700488 // Lazy-callback version
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400489 GrSurfaceProxy(LazyInstantiateCallback&&, LazyInstantiationType,
Greg Daniel4065d452018-11-16 15:43:41 -0500490 const GrBackendFormat& format, const GrSurfaceDesc&, GrSurfaceOrigin,
491 SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700492
Brian Salomonc67c31c2018-12-06 10:00:03 -0500493 // Wrapped version.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400494 GrSurfaceProxy(sk_sp<GrSurface>, GrSurfaceOrigin, SkBackingFit);
robertphillips76948d42016-05-04 12:47:41 -0700495
Robert Phillipsf2361d22016-10-25 14:20:06 -0400496 virtual ~GrSurfaceProxy();
497
Robert Phillips757914d2017-01-25 15:48:30 -0500498 friend class GrSurfaceProxyPriv;
499
500 // Methods made available via GrSurfaceProxyPriv
Robert Phillips715d08c2018-07-18 13:56:48 -0400501 int32_t getProxyRefCnt() const {
502 return this->internalGetProxyRefCnt();
503 }
504
Robert Phillips757914d2017-01-25 15:48:30 -0500505 bool hasPendingIO() const {
506 return this->internalHasPendingIO();
507 }
508
Robert Phillips7ee385e2017-03-30 08:02:11 -0400509 bool hasPendingWrite() const {
510 return this->internalHasPendingWrite();
511 }
512
Robert Phillips57aa3672017-07-21 11:38:13 -0400513 void computeScratchKey(GrScratchKey*) const;
514
Robert Phillips5af44de2017-07-18 14:49:38 -0400515 virtual sk_sp<GrSurface> createSurface(GrResourceProvider*) const = 0;
516 void assign(sk_sp<GrSurface> surface);
517
Robert Phillips65048132017-08-10 08:44:49 -0400518 sk_sp<GrSurface> createSurfaceImpl(GrResourceProvider*, int sampleCnt, bool needsStencil,
Robert Phillips73cc4e82019-04-01 07:46:13 -0400519 GrSurfaceDescFlags, GrMipMapped,
520 bool forceNoPendingIO) const;
Robert Phillips5af44de2017-07-18 14:49:38 -0400521
Robert Phillips65048132017-08-10 08:44:49 -0400522 bool instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, bool needsStencil,
Robert Phillips73cc4e82019-04-01 07:46:13 -0400523 GrSurfaceDescFlags descFlags, GrMipMapped, const GrUniqueKey*,
524 bool dontForceNoPendingIO);
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400525
Greg Daniele3204862018-04-16 11:24:10 -0400526 // In many cases these flags aren't actually known until the proxy has been instantiated.
527 // However, Ganesh frequently needs to change its behavior based on these settings. For
528 // internally create proxies we will know these properties ahead of time. For wrapped
529 // proxies we will copy the properties off of the GrSurface. For lazy proxies we force the
530 // call sites to provide the required information ahead of time. At instantiation time
531 // we verify that the assumed properties match the actual properties.
532 GrInternalSurfaceFlags fSurfaceFlags;
533
Chris Dalton706a6ff2017-11-29 22:01:06 -0700534private:
Greg Daniel4065d452018-11-16 15:43:41 -0500535 // For wrapped resources, 'fFormat', 'fConfig', 'fWidth', 'fHeight', and 'fOrigin; will always
536 // be filled in from the wrapped resource.
537 GrBackendFormat fFormat;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400538 GrPixelConfig fConfig;
539 int fWidth;
540 int fHeight;
541 GrSurfaceOrigin fOrigin;
542 SkBackingFit fFit; // always kApprox for lazy-callback resources
543 // always kExact for wrapped resources
544 mutable SkBudgeted fBudgeted; // always kYes for lazy-callback resources
545 // set from the backing resource for wrapped resources
546 // mutable bc of SkSurface/SkImage wishy-washiness
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400547
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400548 const UniqueID fUniqueID; // set from the backing resource for wrapped resources
robertphillips76948d42016-05-04 12:47:41 -0700549
Chris Dalton706a6ff2017-11-29 22:01:06 -0700550 LazyInstantiateCallback fLazyInstantiateCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500551 // If this is set to kSingleuse, then after one call to fLazyInstantiateCallback we will cleanup
552 // the lazy callback and then delete it. This will allow for any refs and resources being held
553 // by the standard function to be released. This is specifically useful in non-dll cases where
554 // we make lazy proxies and instantiate them immediately.
555 // Note: This is ignored if fLazyInstantiateCallback is null.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400556 LazyInstantiationType fLazyInstantiationType;
Greg Daniel849dce12018-04-24 14:32:53 -0400557
558 SkDEBUGCODE(void validateSurface(const GrSurface*);)
559 SkDEBUGCODE(virtual void onValidateSurface(const GrSurface*) = 0;)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700560
Robert Phillips8bc06d02016-11-01 17:28:40 -0400561 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Robert Phillips29e52f12016-11-03 10:19:14 -0400562 SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; })
563
Brian Salomonbb5711a2017-05-17 13:49:59 -0400564 virtual size_t onUninstantiatedGpuMemorySize() const = 0;
Robert Phillips29e52f12016-11-03 10:19:14 -0400565
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400566 bool fNeedsClear;
Brian Salomond17b4a62017-05-23 16:53:47 -0400567
Robert Phillips8bc06d02016-11-01 17:28:40 -0400568 // This entry is lazily evaluated so, when the proxy wraps a resource, the resource
569 // will be called but, when the proxy is deferred, it will compute the answer itself.
570 // If the proxy computes its own answer that answer is checked (in debug mode) in
571 // the instantiation method.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400572 mutable size_t fGpuMemorySize;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400573
Robert Phillipsf2361d22016-10-25 14:20:06 -0400574 // The last opList that wrote to or is currently going to write to this surface
Robert Phillipsb6deea82017-05-11 14:14:30 -0400575 // The opList can be closed (e.g., no surface context is currently bound
576 // to this proxy).
Robert Phillipsf2361d22016-10-25 14:20:06 -0400577 // This back-pointer is required so that we can add a dependancy between
578 // the opList used to create the current contents of this surface
579 // and the opList of a destination surface to which this one is being drawn or copied.
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400580 // This pointer is unreffed. OpLists own a ref on their surface proxies.
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400581 GrOpList* fLastOpList;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400582
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400583 typedef GrIORefProxy INHERITED;
robertphillips76948d42016-05-04 12:47:41 -0700584};
585
Brian Salomon01ceae92019-04-02 11:49:54 -0400586class GrSurfaceProxy::FirstRefAccess {
587private:
588 void ref() { fProxy->addInitialRef(); }
589
590 FirstRefAccess(GrSurfaceProxy* proxy) : fProxy(proxy) {}
591
592 // No taking addresses of this type.
593 const FirstRefAccess* operator&() const = delete;
594 FirstRefAccess* operator&() = delete;
595
596 GrSurfaceProxy* fProxy;
597
598 friend class GrSurfaceProxy;
599 friend class GrProxyProvider;
600 friend class GrDeinstantiateProxyTracker;
601};
602
603inline GrSurfaceProxy::FirstRefAccess GrSurfaceProxy::firstRefAccess() {
604 return FirstRefAccess(this);
605}
606
robertphillips76948d42016-05-04 12:47:41 -0700607#endif