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