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