robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1 | /* |
| 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 Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 11 | #include "../private/SkNoncopyable.h" |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 12 | #include "GrBackendSurface.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 13 | #include "GrGpuResource.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 14 | #include "GrSurface.h" |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 15 | #include "GrTexture.h" |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 16 | #include "SkRect.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 17 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 18 | class GrCaps; |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 19 | class GrContext_Base; |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 20 | class GrOpList; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 21 | class GrProxyProvider; |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 22 | class GrRecordingContext; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 23 | class GrRenderTargetOpList; |
| 24 | class GrRenderTargetProxy; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 25 | class GrResourceProvider; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 26 | class GrSurfaceContext; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 27 | class GrSurfaceProxyPriv; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 28 | class GrTextureOpList; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 29 | class GrTextureProxy; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 30 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 31 | // 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 |
| 34 | class GrIORefProxy : public SkNoncopyable { |
| 35 | public: |
| 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 Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 52 | --fRefCnt; |
| 53 | this->didRemoveRefOrPendingIO(); |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 56 | #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 Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 63 | void release() { |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 64 | // 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 Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 66 | SkASSERT(0 == fPendingReads); |
| 67 | SkASSERT(0 == fPendingWrites); |
| 68 | |
Robert Phillips | e4aae34 | 2018-03-14 10:26:57 -0400 | [diff] [blame] | 69 | // In the current hybrid world, the proxy and backing surface are ref/unreffed in |
Brian Salomon | 0d60676 | 2019-01-25 09:58:38 -0500 | [diff] [blame] | 70 | // 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 Phillips | e4aae34 | 2018-03-14 10:26:57 -0400 | [diff] [blame] | 76 | fTarget->unref(); |
| 77 | } |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 78 | fTarget = nullptr; |
| 79 | } |
| 80 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 81 | void validate() const { |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 82 | #ifdef SK_DEBUG |
| 83 | SkASSERT(fRefCnt >= 0); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 84 | SkASSERT(fPendingReads >= 0); |
| 85 | SkASSERT(fPendingWrites >= 0); |
| 86 | SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1); |
| 87 | |
| 88 | if (fTarget) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 89 | // 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 Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 93 | SkASSERT(fTarget->fPendingReads >= fPendingReads); |
| 94 | SkASSERT(fTarget->fPendingWrites >= fPendingWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 95 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 96 | #endif |
| 97 | } |
| 98 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 99 | int32_t getBackingRefCnt_TestOnly() const; |
| 100 | int32_t getPendingReadCnt_TestOnly() const; |
| 101 | int32_t getPendingWriteCnt_TestOnly() const; |
| 102 | |
Brian Salomon | cf75b00 | 2017-08-16 10:42:09 -0400 | [diff] [blame] | 103 | 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 Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 143 | protected: |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 144 | GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {} |
| 145 | GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 146 | // 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 Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 152 | // have forwarded on the unref call that got us here. |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 153 | } |
| 154 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 155 | // 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 Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 161 | SkASSERT(fTarget->fRefCnt > 0); |
Robert Phillips | 2d9cb57 | 2017-11-13 13:38:05 +0000 | [diff] [blame] | 162 | fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 163 | fTarget->fPendingReads += fPendingReads; |
| 164 | fTarget->fPendingWrites += fPendingWrites; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 165 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 166 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 167 | int32_t internalGetProxyRefCnt() const { |
| 168 | return fRefCnt; |
| 169 | } |
| 170 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 171 | bool internalHasPendingIO() const { |
| 172 | if (fTarget) { |
| 173 | return fTarget->internalHasPendingIO(); |
| 174 | } |
| 175 | |
| 176 | return SkToBool(fPendingWrites | fPendingReads); |
| 177 | } |
| 178 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 179 | bool internalHasPendingWrite() const { |
| 180 | if (fTarget) { |
| 181 | return fTarget->internalHasPendingWrite(); |
| 182 | } |
| 183 | |
| 184 | return SkToBool(fPendingWrites); |
| 185 | } |
| 186 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 187 | // For deferred proxies this will be null. For wrapped proxies it will point to the |
| 188 | // wrapped resource. |
| 189 | GrSurface* fTarget; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 190 | |
| 191 | private: |
| 192 | // This class is used to manage conversion of refs to pending reads/writes. |
Brian Salomon | ee78396 | 2018-08-01 09:55:10 -0400 | [diff] [blame] | 193 | template <typename> friend class GrProxyRef; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 194 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 195 | void didRemoveRefOrPendingIO() const { |
| 196 | if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) { |
| 197 | delete this; |
| 198 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | mutable int32_t fRefCnt; |
| 202 | mutable int32_t fPendingReads; |
| 203 | mutable int32_t fPendingWrites; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | class GrSurfaceProxy : public GrIORefProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 207 | public: |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 208 | /** |
| 209 | * Some lazy proxy callbacks want to set their own (or no key) on the GrSurfaces they return. |
| 210 | * Others want the GrSurface's key to be kept in sync with the proxy's key. This enum controls |
| 211 | * the key relationship between proxies and their targets. |
| 212 | */ |
| 213 | enum class LazyInstantiationKeyMode { |
| 214 | /** |
| 215 | * Don't key the GrSurface with the proxy's key. The lazy instantiation callback is free to |
| 216 | * return a GrSurface that already has a unique key unrelated to the proxy's key. |
| 217 | */ |
| 218 | kUnsynced, |
| 219 | /** |
| 220 | * Keep the GrSurface's unique key in sync with the proxy's unique key. The GrSurface |
| 221 | * returned from the lazy instantiation callback must not have a unique key or have the same |
| 222 | * same unique key as the proxy. If the proxy is later assigned a key it is in turn assigned |
| 223 | * to the GrSurface. |
| 224 | */ |
| 225 | kSynced |
| 226 | }; |
| 227 | |
| 228 | struct LazyInstantiationResult { |
| 229 | LazyInstantiationResult() = default; |
| 230 | LazyInstantiationResult(const LazyInstantiationResult&) = default; |
| 231 | LazyInstantiationResult(LazyInstantiationResult&& that) = default; |
| 232 | LazyInstantiationResult(sk_sp<GrSurface> surf, |
| 233 | LazyInstantiationKeyMode mode = LazyInstantiationKeyMode::kSynced) |
| 234 | : fSurface(std::move(surf)), fKeyMode(mode) {} |
| 235 | LazyInstantiationResult(sk_sp<GrTexture> tex) |
| 236 | : LazyInstantiationResult(sk_sp<GrSurface>(std::move(tex))) {} |
| 237 | |
| 238 | LazyInstantiationResult& operator=(const LazyInstantiationResult&) = default; |
| 239 | LazyInstantiationResult& operator=(LazyInstantiationResult&&) = default; |
| 240 | |
| 241 | sk_sp<GrSurface> fSurface; |
| 242 | LazyInstantiationKeyMode fKeyMode = LazyInstantiationKeyMode::kSynced; |
| 243 | }; |
| 244 | |
| 245 | using LazyInstantiateCallback = std::function<LazyInstantiationResult(GrResourceProvider*)>; |
| 246 | |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 247 | enum class LazyInstantiationType { |
Brian Salomon | 967df20 | 2018-12-07 11:15:53 -0500 | [diff] [blame] | 248 | kSingleUse, // Instantiation callback is allowed to be called only once. |
| 249 | kMultipleUse, // Instantiation callback can be called multiple times. |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 250 | kDeinstantiate, // Instantiation callback can be called multiple times, |
| 251 | // but we will deinstantiate the proxy after every flush. |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 252 | }; |
| 253 | |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 254 | enum class LazyState { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 255 | kNot, // The proxy is instantiated or does not have a lazy callback |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 256 | kPartially, // The proxy has a lazy callback but knows basic information about itself. |
| 257 | kFully, // The proxy has a lazy callback and also doesn't know its width, height, etc. |
| 258 | }; |
| 259 | |
| 260 | LazyState lazyInstantiationState() const { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 261 | if (fTarget || !SkToBool(fLazyInstantiateCallback)) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 262 | return LazyState::kNot; |
| 263 | } else { |
| 264 | if (fWidth <= 0) { |
| 265 | SkASSERT(fHeight <= 0); |
| 266 | return LazyState::kFully; |
| 267 | } else { |
| 268 | SkASSERT(fHeight > 0); |
| 269 | return LazyState::kPartially; |
| 270 | } |
| 271 | } |
| 272 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 273 | |
| 274 | GrPixelConfig config() const { return fConfig; } |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 275 | int width() const { |
| 276 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 277 | return fWidth; |
| 278 | } |
| 279 | int height() const { |
| 280 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 281 | return fHeight; |
| 282 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 283 | |
| 284 | SkISize isize() const { return {fWidth, fHeight}; } |
| 285 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 286 | int worstCaseWidth() const; |
| 287 | int worstCaseHeight() const; |
Brian Salomon | d8eb7b6 | 2018-05-25 10:08:05 -0400 | [diff] [blame] | 288 | /** |
| 289 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 290 | */ |
| 291 | SkRect getBoundsRect() const { |
| 292 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 293 | return SkRect::MakeIWH(this->width(), this->height()); |
| 294 | } |
| 295 | /** |
| 296 | * Helper that gets the worst case width and height of the surface as a bounding rectangle. |
| 297 | */ |
| 298 | SkRect getWorstCaseBoundsRect() const { |
| 299 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 300 | return SkRect::MakeIWH(this->worstCaseWidth(), this->worstCaseHeight()); |
| 301 | } |
| 302 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 303 | GrSurfaceOrigin origin() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 304 | SkASSERT(kTopLeft_GrSurfaceOrigin == fOrigin || kBottomLeft_GrSurfaceOrigin == fOrigin); |
| 305 | return fOrigin; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 306 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 307 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 308 | const GrBackendFormat& backendFormat() const { return fFormat; } |
| 309 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 310 | class UniqueID { |
| 311 | public: |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 312 | static UniqueID InvalidID() { |
| 313 | return UniqueID(uint32_t(SK_InvalidUniqueID)); |
| 314 | } |
| 315 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 316 | // wrapped |
| 317 | explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 318 | // deferred and lazy-callback |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 319 | UniqueID() : fID(GrGpuResource::CreateUniqueID()) { } |
| 320 | |
| 321 | uint32_t asUInt() const { return fID; } |
| 322 | |
| 323 | bool operator==(const UniqueID& other) const { |
| 324 | return fID == other.fID; |
| 325 | } |
| 326 | bool operator!=(const UniqueID& other) const { |
| 327 | return !(*this == other); |
| 328 | } |
| 329 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 330 | void makeInvalid() { fID = SK_InvalidUniqueID; } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 331 | bool isInvalid() const { return SK_InvalidUniqueID == fID; } |
| 332 | |
| 333 | private: |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 334 | explicit UniqueID(uint32_t id) : fID(id) {} |
| 335 | |
| 336 | uint32_t fID; |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /* |
| 340 | * The contract for the uniqueID is: |
| 341 | * for wrapped resources: |
| 342 | * the uniqueID will match that of the wrapped resource |
| 343 | * |
| 344 | * for deferred resources: |
| 345 | * the uniqueID will be different from the real resource, when it is allocated |
| 346 | * the proxy's uniqueID will not change across the instantiate call |
| 347 | * |
| 348 | * the uniqueIDs of the proxies and the resources draw from the same pool |
| 349 | * |
| 350 | * What this boils down to is that the uniqueID of a proxy can be used to consistently |
| 351 | * track/identify a proxy but should never be used to distinguish between |
| 352 | * resources and proxies - beware! |
| 353 | */ |
| 354 | UniqueID uniqueID() const { return fUniqueID; } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 355 | |
Robert Phillips | 159e3c6 | 2017-06-19 12:02:00 -0400 | [diff] [blame] | 356 | UniqueID underlyingUniqueID() const { |
| 357 | if (fTarget) { |
| 358 | return UniqueID(fTarget->uniqueID()); |
| 359 | } |
| 360 | |
| 361 | return fUniqueID; |
| 362 | } |
| 363 | |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 364 | virtual bool instantiate(GrResourceProvider* resourceProvider, |
| 365 | bool dontForceNoPendingIO = false) = 0; |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 366 | |
Brian Salomon | 967df20 | 2018-12-07 11:15:53 -0500 | [diff] [blame] | 367 | void deinstantiate(); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 368 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 369 | /** |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 370 | * Proxies that are already instantiated and whose backing surface cannot be recycled to |
| 371 | * instantiate other proxies do not need to be considered by GrResourceAllocator. |
| 372 | */ |
| 373 | bool canSkipResourceAllocator() const; |
| 374 | |
| 375 | /** |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 376 | * @return the texture proxy associated with the surface proxy, may be NULL. |
| 377 | */ |
| 378 | virtual GrTextureProxy* asTextureProxy() { return nullptr; } |
| 379 | virtual const GrTextureProxy* asTextureProxy() const { return nullptr; } |
| 380 | |
| 381 | /** |
| 382 | * @return the render target proxy associated with the surface proxy, may be NULL. |
| 383 | */ |
| 384 | virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; } |
| 385 | virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; } |
| 386 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 387 | bool isInstantiated() const { return SkToBool(fTarget); } |
| 388 | |
| 389 | // If the proxy is already instantiated, return its backing GrTexture; if not, return null. |
| 390 | GrSurface* peekSurface() const { return fTarget; } |
| 391 | |
| 392 | // If this is a texture proxy and the proxy is already instantiated, return its backing |
| 393 | // GrTexture; if not, return null. |
| 394 | GrTexture* peekTexture() const { return fTarget ? fTarget->asTexture() : nullptr; } |
| 395 | |
| 396 | // If this is a render target proxy and the proxy is already instantiated, return its backing |
| 397 | // GrRenderTarget; if not, return null. |
| 398 | GrRenderTarget* peekRenderTarget() const { |
| 399 | return fTarget ? fTarget->asRenderTarget() : nullptr; |
| 400 | } |
| 401 | |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 402 | /** |
| 403 | * Does the resource count against the resource budget? |
| 404 | */ |
| 405 | SkBudgeted isBudgeted() const { return fBudgeted; } |
| 406 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 407 | /** |
| 408 | * The pixel values of this proxy's surface cannot be modified (e.g. doesn't support write |
Brian Salomon | 9cadc31 | 2018-12-05 15:09:19 -0500 | [diff] [blame] | 409 | * pixels or MIP map level regen). Read-only proxies also bypass interval tracking and |
| 410 | * assignment in GrResourceAllocator. |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 411 | */ |
| 412 | bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; } |
| 413 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 414 | void setLastOpList(GrOpList* opList); |
| 415 | GrOpList* getLastOpList() { return fLastOpList; } |
| 416 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 417 | GrRenderTargetOpList* getLastRenderTargetOpList(); |
| 418 | GrTextureOpList* getLastTextureOpList(); |
| 419 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 420 | /** |
Robert Phillips | f5442bb | 2017-04-17 14:18:34 -0400 | [diff] [blame] | 421 | * Retrieves the amount of GPU memory that will be or currently is used by this resource |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 422 | * in bytes. It is approximate since we aren't aware of additional padding or copies made |
| 423 | * by the driver. |
| 424 | * |
| 425 | * @return the amount of GPU memory used in bytes |
| 426 | */ |
| 427 | size_t gpuMemorySize() const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 428 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 429 | if (fTarget) { |
| 430 | return fTarget->gpuMemorySize(); |
| 431 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 432 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 433 | fGpuMemorySize = this->onUninstantiatedGpuMemorySize(); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 434 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 435 | } |
| 436 | return fGpuMemorySize; |
| 437 | } |
| 438 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 439 | // Helper function that creates a temporary SurfaceContext to perform the copy |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 440 | // The copy is is not a render target and not multisampled. |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 441 | static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped, |
| 442 | SkIRect srcRect, SkBackingFit, SkBudgeted); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 443 | |
| 444 | // Copy the entire 'src' |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 445 | static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped, |
| 446 | SkBackingFit, SkBudgeted); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 447 | |
| 448 | // Test-only entry point - should decrease in use as proxies propagate |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 449 | static sk_sp<GrSurfaceContext> TestCopy(GrRecordingContext* context, |
| 450 | const GrSurfaceDesc& dstDesc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 451 | GrSurfaceOrigin, GrSurfaceProxy* srcProxy); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 452 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 453 | bool isWrapped_ForTesting() const; |
| 454 | |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 455 | SkDEBUGCODE(void validate(GrContext_Base*) const;) |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 456 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 457 | // Provides access to functions that aren't part of the public API. |
Robert Phillips | 420c4cf | 2017-09-28 09:00:45 -0400 | [diff] [blame] | 458 | inline GrSurfaceProxyPriv priv(); |
| 459 | inline const GrSurfaceProxyPriv priv() const; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 460 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 461 | GrInternalSurfaceFlags testingOnly_getFlags() const; |
| 462 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 463 | protected: |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 464 | // Deferred version |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 465 | GrSurfaceProxy(const GrBackendFormat& format, const GrSurfaceDesc& desc, |
| 466 | GrSurfaceOrigin origin, SkBackingFit fit, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 467 | SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags) |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 468 | : GrSurfaceProxy(nullptr, LazyInstantiationType::kSingleUse, format, desc, origin, fit, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 469 | budgeted, surfaceFlags) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 470 | // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 473 | // Lazy-callback version |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 474 | GrSurfaceProxy(LazyInstantiateCallback&&, LazyInstantiationType, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 475 | const GrBackendFormat& format, const GrSurfaceDesc&, GrSurfaceOrigin, |
| 476 | SkBackingFit, SkBudgeted, GrInternalSurfaceFlags); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 477 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 478 | // Wrapped version. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 479 | GrSurfaceProxy(sk_sp<GrSurface>, GrSurfaceOrigin, SkBackingFit); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 480 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 481 | virtual ~GrSurfaceProxy(); |
| 482 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 483 | friend class GrSurfaceProxyPriv; |
| 484 | |
| 485 | // Methods made available via GrSurfaceProxyPriv |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 486 | int32_t getProxyRefCnt() const { |
| 487 | return this->internalGetProxyRefCnt(); |
| 488 | } |
| 489 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 490 | bool hasPendingIO() const { |
| 491 | return this->internalHasPendingIO(); |
| 492 | } |
| 493 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 494 | bool hasPendingWrite() const { |
| 495 | return this->internalHasPendingWrite(); |
| 496 | } |
| 497 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 498 | void computeScratchKey(GrScratchKey*) const; |
| 499 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 500 | virtual sk_sp<GrSurface> createSurface(GrResourceProvider*) const = 0; |
| 501 | void assign(sk_sp<GrSurface> surface); |
| 502 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 503 | sk_sp<GrSurface> createSurfaceImpl(GrResourceProvider*, int sampleCnt, bool needsStencil, |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 504 | GrSurfaceDescFlags, GrMipMapped, |
| 505 | bool forceNoPendingIO) const; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 506 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 507 | bool instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, bool needsStencil, |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 508 | GrSurfaceDescFlags descFlags, GrMipMapped, const GrUniqueKey*, |
| 509 | bool dontForceNoPendingIO); |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 510 | |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 511 | // In many cases these flags aren't actually known until the proxy has been instantiated. |
| 512 | // However, Ganesh frequently needs to change its behavior based on these settings. For |
| 513 | // internally create proxies we will know these properties ahead of time. For wrapped |
| 514 | // proxies we will copy the properties off of the GrSurface. For lazy proxies we force the |
| 515 | // call sites to provide the required information ahead of time. At instantiation time |
| 516 | // we verify that the assumed properties match the actual properties. |
| 517 | GrInternalSurfaceFlags fSurfaceFlags; |
| 518 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 519 | private: |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 520 | // For wrapped resources, 'fFormat', 'fConfig', 'fWidth', 'fHeight', and 'fOrigin; will always |
| 521 | // be filled in from the wrapped resource. |
| 522 | GrBackendFormat fFormat; |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 523 | GrPixelConfig fConfig; |
| 524 | int fWidth; |
| 525 | int fHeight; |
| 526 | GrSurfaceOrigin fOrigin; |
| 527 | SkBackingFit fFit; // always kApprox for lazy-callback resources |
| 528 | // always kExact for wrapped resources |
| 529 | mutable SkBudgeted fBudgeted; // always kYes for lazy-callback resources |
| 530 | // set from the backing resource for wrapped resources |
| 531 | // mutable bc of SkSurface/SkImage wishy-washiness |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 532 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 533 | const UniqueID fUniqueID; // set from the backing resource for wrapped resources |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 534 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 535 | LazyInstantiateCallback fLazyInstantiateCallback; |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 536 | // If this is set to kSingleuse, then after one call to fLazyInstantiateCallback we will cleanup |
| 537 | // the lazy callback and then delete it. This will allow for any refs and resources being held |
| 538 | // by the standard function to be released. This is specifically useful in non-dll cases where |
| 539 | // we make lazy proxies and instantiate them immediately. |
| 540 | // Note: This is ignored if fLazyInstantiateCallback is null. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 541 | LazyInstantiationType fLazyInstantiationType; |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 542 | |
| 543 | SkDEBUGCODE(void validateSurface(const GrSurface*);) |
| 544 | SkDEBUGCODE(virtual void onValidateSurface(const GrSurface*) = 0;) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 545 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 546 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 547 | SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; }) |
| 548 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 549 | virtual size_t onUninstantiatedGpuMemorySize() const = 0; |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 550 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 551 | bool fNeedsClear; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 552 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 553 | // This entry is lazily evaluated so, when the proxy wraps a resource, the resource |
| 554 | // will be called but, when the proxy is deferred, it will compute the answer itself. |
| 555 | // If the proxy computes its own answer that answer is checked (in debug mode) in |
| 556 | // the instantiation method. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 557 | mutable size_t fGpuMemorySize; |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 558 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 559 | // The last opList that wrote to or is currently going to write to this surface |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 560 | // The opList can be closed (e.g., no surface context is currently bound |
| 561 | // to this proxy). |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 562 | // This back-pointer is required so that we can add a dependancy between |
| 563 | // the opList used to create the current contents of this surface |
| 564 | // and the opList of a destination surface to which this one is being drawn or copied. |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 565 | // This pointer is unreffed. OpLists own a ref on their surface proxies. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 566 | GrOpList* fLastOpList; |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 567 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 568 | typedef GrIORefProxy INHERITED; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | #endif |