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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRect.h" |
| 12 | #include "include/gpu/GrBackendSurface.h" |
| 13 | #include "include/gpu/GrGpuResource.h" |
| 14 | #include "include/gpu/GrSurface.h" |
| 15 | #include "include/gpu/GrTexture.h" |
| 16 | #include "include/private/SkNoncopyable.h" |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrSwizzle.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 18 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 19 | class GrCaps; |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 20 | class GrContext_Base; |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 21 | class GrOpList; |
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 | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 31 | // This is basically SkRefCntBase except Ganesh uses internalGetProxyRefCnt for more than asserts. |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 32 | class GrIORefProxy : public SkNoncopyable { |
| 33 | public: |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 34 | GrIORefProxy() : fRefCnt(1) {} |
| 35 | |
| 36 | virtual ~GrIORefProxy() {} |
| 37 | |
| 38 | bool unique() const { |
| 39 | SkASSERT(fRefCnt > 0); |
| 40 | return 1 == fRefCnt; |
| 41 | } |
| 42 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 43 | void ref() const { |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 44 | SkASSERT(fRefCnt > 0); |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 45 | ++fRefCnt; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void unref() const { |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 49 | SkASSERT(fRefCnt > 0); |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 50 | --fRefCnt; |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 51 | if (0 == fRefCnt) { |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 52 | delete this; |
| 53 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 56 | protected: |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 57 | int32_t internalGetProxyRefCnt() const { return fRefCnt; } |
| 58 | |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 59 | private: |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 60 | mutable int32_t fRefCnt; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class GrSurfaceProxy : public GrIORefProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 64 | public: |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 65 | /** |
| 66 | * Some lazy proxy callbacks want to set their own (or no key) on the GrSurfaces they return. |
| 67 | * Others want the GrSurface's key to be kept in sync with the proxy's key. This enum controls |
| 68 | * the key relationship between proxies and their targets. |
| 69 | */ |
| 70 | enum class LazyInstantiationKeyMode { |
| 71 | /** |
| 72 | * Don't key the GrSurface with the proxy's key. The lazy instantiation callback is free to |
| 73 | * return a GrSurface that already has a unique key unrelated to the proxy's key. |
| 74 | */ |
| 75 | kUnsynced, |
| 76 | /** |
| 77 | * Keep the GrSurface's unique key in sync with the proxy's unique key. The GrSurface |
| 78 | * returned from the lazy instantiation callback must not have a unique key or have the same |
| 79 | * same unique key as the proxy. If the proxy is later assigned a key it is in turn assigned |
| 80 | * to the GrSurface. |
| 81 | */ |
| 82 | kSynced |
| 83 | }; |
| 84 | |
| 85 | struct LazyInstantiationResult { |
| 86 | LazyInstantiationResult() = default; |
| 87 | LazyInstantiationResult(const LazyInstantiationResult&) = default; |
| 88 | LazyInstantiationResult(LazyInstantiationResult&& that) = default; |
| 89 | LazyInstantiationResult(sk_sp<GrSurface> surf, |
| 90 | LazyInstantiationKeyMode mode = LazyInstantiationKeyMode::kSynced) |
| 91 | : fSurface(std::move(surf)), fKeyMode(mode) {} |
| 92 | LazyInstantiationResult(sk_sp<GrTexture> tex) |
| 93 | : LazyInstantiationResult(sk_sp<GrSurface>(std::move(tex))) {} |
| 94 | |
| 95 | LazyInstantiationResult& operator=(const LazyInstantiationResult&) = default; |
| 96 | LazyInstantiationResult& operator=(LazyInstantiationResult&&) = default; |
| 97 | |
| 98 | sk_sp<GrSurface> fSurface; |
| 99 | LazyInstantiationKeyMode fKeyMode = LazyInstantiationKeyMode::kSynced; |
| 100 | }; |
| 101 | |
| 102 | using LazyInstantiateCallback = std::function<LazyInstantiationResult(GrResourceProvider*)>; |
| 103 | |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 104 | enum class LazyInstantiationType { |
Brian Salomon | 967df20 | 2018-12-07 11:15:53 -0500 | [diff] [blame] | 105 | kSingleUse, // Instantiation callback is allowed to be called only once. |
| 106 | kMultipleUse, // Instantiation callback can be called multiple times. |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 107 | kDeinstantiate, // Instantiation callback can be called multiple times, |
| 108 | // but we will deinstantiate the proxy after every flush. |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 109 | }; |
| 110 | |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 111 | enum class LazyState { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 112 | kNot, // The proxy is instantiated or does not have a lazy callback |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 113 | kPartially, // The proxy has a lazy callback but knows basic information about itself. |
| 114 | kFully, // The proxy has a lazy callback and also doesn't know its width, height, etc. |
| 115 | }; |
| 116 | |
| 117 | LazyState lazyInstantiationState() const { |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 118 | if (this->isInstantiated() || !SkToBool(fLazyInstantiateCallback)) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 119 | return LazyState::kNot; |
| 120 | } else { |
| 121 | if (fWidth <= 0) { |
| 122 | SkASSERT(fHeight <= 0); |
| 123 | return LazyState::kFully; |
| 124 | } else { |
| 125 | SkASSERT(fHeight > 0); |
| 126 | return LazyState::kPartially; |
| 127 | } |
| 128 | } |
| 129 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 130 | |
| 131 | GrPixelConfig config() const { return fConfig; } |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 132 | int width() const { |
| 133 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 134 | return fWidth; |
| 135 | } |
| 136 | int height() const { |
| 137 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 138 | return fHeight; |
| 139 | } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 140 | |
| 141 | SkISize isize() const { return {fWidth, fHeight}; } |
| 142 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 143 | int worstCaseWidth() const; |
| 144 | int worstCaseHeight() const; |
Brian Salomon | d8eb7b6 | 2018-05-25 10:08:05 -0400 | [diff] [blame] | 145 | /** |
| 146 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 147 | */ |
| 148 | SkRect getBoundsRect() const { |
| 149 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 150 | return SkRect::MakeIWH(this->width(), this->height()); |
| 151 | } |
| 152 | /** |
| 153 | * Helper that gets the worst case width and height of the surface as a bounding rectangle. |
| 154 | */ |
| 155 | SkRect getWorstCaseBoundsRect() const { |
| 156 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
| 157 | return SkRect::MakeIWH(this->worstCaseWidth(), this->worstCaseHeight()); |
| 158 | } |
| 159 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 160 | GrSurfaceOrigin origin() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 161 | SkASSERT(kTopLeft_GrSurfaceOrigin == fOrigin || kBottomLeft_GrSurfaceOrigin == fOrigin); |
| 162 | return fOrigin; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 163 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 164 | |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 165 | const GrSwizzle& textureSwizzle() const { return fTextureSwizzle; } |
| 166 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 167 | const GrBackendFormat& backendFormat() const { return fFormat; } |
| 168 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 169 | class UniqueID { |
| 170 | public: |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 171 | static UniqueID InvalidID() { |
| 172 | return UniqueID(uint32_t(SK_InvalidUniqueID)); |
| 173 | } |
| 174 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 175 | // wrapped |
| 176 | explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 177 | // deferred and lazy-callback |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 178 | UniqueID() : fID(GrGpuResource::CreateUniqueID()) { } |
| 179 | |
| 180 | uint32_t asUInt() const { return fID; } |
| 181 | |
| 182 | bool operator==(const UniqueID& other) const { |
| 183 | return fID == other.fID; |
| 184 | } |
| 185 | bool operator!=(const UniqueID& other) const { |
| 186 | return !(*this == other); |
| 187 | } |
| 188 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 189 | void makeInvalid() { fID = SK_InvalidUniqueID; } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 190 | bool isInvalid() const { return SK_InvalidUniqueID == fID; } |
| 191 | |
| 192 | private: |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 193 | explicit UniqueID(uint32_t id) : fID(id) {} |
| 194 | |
| 195 | uint32_t fID; |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | /* |
| 199 | * The contract for the uniqueID is: |
| 200 | * for wrapped resources: |
| 201 | * the uniqueID will match that of the wrapped resource |
| 202 | * |
| 203 | * for deferred resources: |
| 204 | * the uniqueID will be different from the real resource, when it is allocated |
| 205 | * the proxy's uniqueID will not change across the instantiate call |
| 206 | * |
| 207 | * the uniqueIDs of the proxies and the resources draw from the same pool |
| 208 | * |
| 209 | * What this boils down to is that the uniqueID of a proxy can be used to consistently |
| 210 | * track/identify a proxy but should never be used to distinguish between |
| 211 | * resources and proxies - beware! |
| 212 | */ |
| 213 | UniqueID uniqueID() const { return fUniqueID; } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 214 | |
Robert Phillips | 159e3c6 | 2017-06-19 12:02:00 -0400 | [diff] [blame] | 215 | UniqueID underlyingUniqueID() const { |
| 216 | if (fTarget) { |
| 217 | return UniqueID(fTarget->uniqueID()); |
| 218 | } |
| 219 | |
| 220 | return fUniqueID; |
| 221 | } |
| 222 | |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 223 | virtual bool instantiate(GrResourceProvider*) = 0; |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 224 | |
Brian Salomon | 967df20 | 2018-12-07 11:15:53 -0500 | [diff] [blame] | 225 | void deinstantiate(); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 226 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 227 | /** |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 228 | * Proxies that are already instantiated and whose backing surface cannot be recycled to |
| 229 | * instantiate other proxies do not need to be considered by GrResourceAllocator. |
| 230 | */ |
| 231 | bool canSkipResourceAllocator() const; |
| 232 | |
| 233 | /** |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 234 | * @return the texture proxy associated with the surface proxy, may be NULL. |
| 235 | */ |
| 236 | virtual GrTextureProxy* asTextureProxy() { return nullptr; } |
| 237 | virtual const GrTextureProxy* asTextureProxy() const { return nullptr; } |
| 238 | |
| 239 | /** |
| 240 | * @return the render target proxy associated with the surface proxy, may be NULL. |
| 241 | */ |
| 242 | virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; } |
| 243 | virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; } |
| 244 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 245 | bool isInstantiated() const { return SkToBool(fTarget); } |
| 246 | |
| 247 | // If the proxy is already instantiated, return its backing GrTexture; if not, return null. |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 248 | GrSurface* peekSurface() const { return fTarget.get(); } |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 249 | |
| 250 | // If this is a texture proxy and the proxy is already instantiated, return its backing |
| 251 | // GrTexture; if not, return null. |
| 252 | GrTexture* peekTexture() const { return fTarget ? fTarget->asTexture() : nullptr; } |
| 253 | |
| 254 | // If this is a render target proxy and the proxy is already instantiated, return its backing |
| 255 | // GrRenderTarget; if not, return null. |
| 256 | GrRenderTarget* peekRenderTarget() const { |
| 257 | return fTarget ? fTarget->asRenderTarget() : nullptr; |
| 258 | } |
| 259 | |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Does the resource count against the resource budget? |
| 262 | */ |
| 263 | SkBudgeted isBudgeted() const { return fBudgeted; } |
| 264 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 265 | /** |
| 266 | * 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] | 267 | * pixels or MIP map level regen). Read-only proxies also bypass interval tracking and |
| 268 | * assignment in GrResourceAllocator. |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 269 | */ |
| 270 | bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; } |
| 271 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 272 | void setLastOpList(GrOpList* opList); |
| 273 | GrOpList* getLastOpList() { return fLastOpList; } |
| 274 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 275 | GrRenderTargetOpList* getLastRenderTargetOpList(); |
| 276 | GrTextureOpList* getLastTextureOpList(); |
| 277 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 278 | /** |
Robert Phillips | f5442bb | 2017-04-17 14:18:34 -0400 | [diff] [blame] | 279 | * 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] | 280 | * in bytes. It is approximate since we aren't aware of additional padding or copies made |
| 281 | * by the driver. |
| 282 | * |
| 283 | * @return the amount of GPU memory used in bytes |
| 284 | */ |
| 285 | size_t gpuMemorySize() const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 286 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 287 | if (fTarget) { |
| 288 | return fTarget->gpuMemorySize(); |
| 289 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 290 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 291 | fGpuMemorySize = this->onUninstantiatedGpuMemorySize(); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 292 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 293 | } |
| 294 | return fGpuMemorySize; |
| 295 | } |
| 296 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 297 | enum class RectsMustMatch : bool { |
| 298 | kNo = false, |
| 299 | kYes = true |
| 300 | }; |
| 301 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 302 | // Helper function that creates a temporary SurfaceContext to perform the copy |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 303 | // The copy is is not a render target and not multisampled. |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 304 | static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped, |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 305 | SkIRect srcRect, SkBackingFit, SkBudgeted, |
| 306 | RectsMustMatch = RectsMustMatch::kNo); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 307 | |
| 308 | // Copy the entire 'src' |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 309 | static sk_sp<GrTextureProxy> Copy(GrRecordingContext*, GrSurfaceProxy* src, GrMipMapped, |
| 310 | SkBackingFit, SkBudgeted); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 311 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 312 | #if GR_TEST_UTILS |
| 313 | int32_t testingOnly_getBackingRefCnt() const; |
| 314 | GrInternalSurfaceFlags testingOnly_getFlags() const; |
| 315 | #endif |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 316 | |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 317 | SkDEBUGCODE(void validate(GrContext_Base*) const;) |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 318 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 319 | // Provides access to functions that aren't part of the public API. |
Robert Phillips | 420c4cf | 2017-09-28 09:00:45 -0400 | [diff] [blame] | 320 | inline GrSurfaceProxyPriv priv(); |
| 321 | inline const GrSurfaceProxyPriv priv() const; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 322 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 323 | // Returns true if we are working with protected content. |
| 324 | bool isProtected() const { return fIsProtected == GrProtected::kYes; } |
| 325 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 326 | protected: |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 327 | // Deferred version |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 328 | GrSurfaceProxy(const GrBackendFormat& format, const GrSurfaceDesc& desc, |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 329 | GrSurfaceOrigin origin, const GrSwizzle& textureSwizzle, SkBackingFit fit, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 330 | SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags) |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 331 | : GrSurfaceProxy(nullptr, LazyInstantiationType::kSingleUse, format, desc, origin, |
| 332 | textureSwizzle, fit, budgeted, surfaceFlags) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 333 | // 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] | 334 | } |
| 335 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 336 | // Lazy-callback version |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 337 | GrSurfaceProxy(LazyInstantiateCallback&&, LazyInstantiationType, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 338 | const GrBackendFormat& format, const GrSurfaceDesc&, GrSurfaceOrigin, |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 339 | const GrSwizzle& textureSwizzle, SkBackingFit, SkBudgeted, |
| 340 | GrInternalSurfaceFlags); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 341 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 342 | // Wrapped version. |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 343 | GrSurfaceProxy(sk_sp<GrSurface>, GrSurfaceOrigin, const GrSwizzle& textureSwizzle, |
| 344 | SkBackingFit); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 345 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 346 | ~GrSurfaceProxy() override; |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 347 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 348 | friend class GrSurfaceProxyPriv; |
| 349 | |
| 350 | // Methods made available via GrSurfaceProxyPriv |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 351 | bool ignoredByResourceAllocator() const { return fIgnoredByResourceAllocator; } |
| 352 | void setIgnoredByResourceAllocator() { fIgnoredByResourceAllocator = true; } |
| 353 | |
| 354 | int32_t getProxyRefCnt() const { return this->internalGetProxyRefCnt(); } |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 355 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 356 | void computeScratchKey(GrScratchKey*) const; |
| 357 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 358 | virtual sk_sp<GrSurface> createSurface(GrResourceProvider*) const = 0; |
| 359 | void assign(sk_sp<GrSurface> surface); |
| 360 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 361 | sk_sp<GrSurface> createSurfaceImpl(GrResourceProvider*, int sampleCnt, bool needsStencil, |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 362 | GrSurfaceDescFlags, GrMipMapped) const; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 363 | |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 364 | // Once the size of a fully-lazy proxy is decided, and before it gets instantiated, the client |
| 365 | // can use this optional method to specify the proxy's size. (A proxy's size can be less than |
| 366 | // the GPU surface that backs it. e.g., SkBackingFit::kApprox.) Otherwise, the proxy's size will |
| 367 | // be set to match the underlying GPU surface upon instantiation. |
| 368 | void setLazySize(int width, int height) { |
| 369 | SkASSERT(GrSurfaceProxy::LazyState::kFully == this->lazyInstantiationState()); |
| 370 | SkASSERT(width > 0 && height > 0); |
| 371 | fWidth = width; |
| 372 | fHeight = height; |
| 373 | } |
| 374 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 375 | bool instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, bool needsStencil, |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 376 | GrSurfaceDescFlags descFlags, GrMipMapped, const GrUniqueKey*); |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 377 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 378 | // For deferred proxies this will be null until the proxy is instantiated. |
| 379 | // For wrapped proxies it will point to the wrapped resource. |
| 380 | sk_sp<GrSurface> fTarget; |
| 381 | |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 382 | // In many cases these flags aren't actually known until the proxy has been instantiated. |
| 383 | // However, Ganesh frequently needs to change its behavior based on these settings. For |
| 384 | // internally create proxies we will know these properties ahead of time. For wrapped |
| 385 | // proxies we will copy the properties off of the GrSurface. For lazy proxies we force the |
| 386 | // call sites to provide the required information ahead of time. At instantiation time |
| 387 | // we verify that the assumed properties match the actual properties. |
| 388 | GrInternalSurfaceFlags fSurfaceFlags; |
| 389 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 390 | private: |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 391 | // For wrapped resources, 'fFormat', 'fConfig', 'fWidth', 'fHeight', and 'fOrigin; will always |
| 392 | // be filled in from the wrapped resource. |
| 393 | GrBackendFormat fFormat; |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 394 | GrPixelConfig fConfig; |
| 395 | int fWidth; |
| 396 | int fHeight; |
| 397 | GrSurfaceOrigin fOrigin; |
Greg Daniel | 2c19e7f | 2019-06-18 13:29:21 -0400 | [diff] [blame] | 398 | GrSwizzle fTextureSwizzle; |
| 399 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 400 | SkBackingFit fFit; // always kApprox for lazy-callback resources |
| 401 | // always kExact for wrapped resources |
| 402 | mutable SkBudgeted fBudgeted; // always kYes for lazy-callback resources |
| 403 | // set from the backing resource for wrapped resources |
| 404 | // mutable bc of SkSurface/SkImage wishy-washiness |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 405 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 406 | const UniqueID fUniqueID; // set from the backing resource for wrapped resources |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 407 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 408 | LazyInstantiateCallback fLazyInstantiateCallback; |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 409 | // If this is set to kSingleuse, then after one call to fLazyInstantiateCallback we will cleanup |
| 410 | // the lazy callback and then delete it. This will allow for any refs and resources being held |
| 411 | // by the standard function to be released. This is specifically useful in non-dll cases where |
| 412 | // we make lazy proxies and instantiate them immediately. |
| 413 | // Note: This is ignored if fLazyInstantiateCallback is null. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 414 | LazyInstantiationType fLazyInstantiationType; |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 415 | |
| 416 | SkDEBUGCODE(void validateSurface(const GrSurface*);) |
| 417 | SkDEBUGCODE(virtual void onValidateSurface(const GrSurface*) = 0;) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 418 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 419 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 420 | SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; }) |
| 421 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 422 | virtual size_t onUninstantiatedGpuMemorySize() const = 0; |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 423 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 424 | bool fNeedsClear; |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 425 | bool fIgnoredByResourceAllocator = false; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 426 | GrProtected fIsProtected; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 427 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 428 | // This entry is lazily evaluated so, when the proxy wraps a resource, the resource |
| 429 | // will be called but, when the proxy is deferred, it will compute the answer itself. |
| 430 | // If the proxy computes its own answer that answer is checked (in debug mode) in |
| 431 | // the instantiation method. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 432 | mutable size_t fGpuMemorySize; |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 433 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 434 | // 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] | 435 | // The opList can be closed (e.g., no surface context is currently bound |
| 436 | // to this proxy). |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 437 | // This back-pointer is required so that we can add a dependancy between |
| 438 | // the opList used to create the current contents of this surface |
| 439 | // 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] | 440 | // This pointer is unreffed. OpLists own a ref on their surface proxies. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 441 | GrOpList* fLastOpList; |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 442 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 443 | typedef GrIORefProxy INHERITED; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 444 | }; |
| 445 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 446 | #endif |