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 | |
| 11 | #include "GrGpuResource.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 12 | #include "GrSurface.h" |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 13 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 14 | #include "SkRect.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 15 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 16 | class GrCaps; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 17 | class GrRenderTargetOpList; |
| 18 | class GrRenderTargetProxy; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 19 | class GrSurfaceContext; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 20 | class GrSurfaceProxyPriv; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 21 | class GrTextureOpList; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 22 | class GrTextureProvider; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 23 | class GrTextureProxy; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 24 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 25 | // This class replicates the functionality GrIORef<GrSurface> but tracks the |
| 26 | // utilitization for later resource allocation (for the deferred case) and |
| 27 | // forwards on the utilization in the wrapped case |
| 28 | class GrIORefProxy : public SkNoncopyable { |
| 29 | public: |
| 30 | void ref() const { |
| 31 | this->validate(); |
| 32 | |
| 33 | ++fRefCnt; |
| 34 | if (fTarget) { |
| 35 | fTarget->ref(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void unref() const { |
| 40 | this->validate(); |
| 41 | |
| 42 | if (fTarget) { |
| 43 | fTarget->unref(); |
| 44 | } |
| 45 | |
| 46 | if (!(--fRefCnt)) { |
| 47 | delete this; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | this->validate(); |
| 52 | } |
| 53 | |
| 54 | void validate() const { |
| 55 | #ifdef SK_DEBUG |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 56 | SkASSERT(fRefCnt >= 1); |
| 57 | SkASSERT(fPendingReads >= 0); |
| 58 | SkASSERT(fPendingWrites >= 0); |
| 59 | SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1); |
| 60 | |
| 61 | if (fTarget) { |
| 62 | SkASSERT(!fPendingReads && !fPendingWrites); |
| 63 | // The backing GrSurface can have more refs than the proxy if the proxy |
| 64 | // started off wrapping an external resource (that came in with refs). |
| 65 | // The GrSurface should never have fewer refs than the proxy however. |
| 66 | SkASSERT(fTarget->fRefCnt >= fRefCnt); |
| 67 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 68 | #endif |
| 69 | } |
| 70 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 71 | int32_t getProxyRefCnt_TestOnly() const; |
| 72 | int32_t getBackingRefCnt_TestOnly() const; |
| 73 | int32_t getPendingReadCnt_TestOnly() const; |
| 74 | int32_t getPendingWriteCnt_TestOnly() const; |
| 75 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 76 | protected: |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 77 | GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {} |
| 78 | GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 79 | // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing |
| 80 | // anything extra. |
| 81 | fTarget = surface.release(); |
| 82 | } |
| 83 | virtual ~GrIORefProxy() { |
| 84 | // We don't unref 'fTarget' here since the 'unref' method will already |
| 85 | // have forwarded on the unref call that got use here. |
| 86 | } |
| 87 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 88 | // This GrIORefProxy was deferred before but has just been instantiated. To |
| 89 | // make all the reffing & unreffing work out we now need to transfer any deferred |
| 90 | // refs & unrefs to the new GrSurface |
| 91 | void transferRefs() { |
| 92 | SkASSERT(fTarget); |
| 93 | |
| 94 | fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref |
| 95 | fTarget->fPendingReads += fPendingReads; |
| 96 | fTarget->fPendingWrites += fPendingWrites; |
| 97 | |
| 98 | fPendingReads = 0; |
| 99 | fPendingWrites = 0; |
| 100 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 101 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 102 | bool internalHasPendingIO() const { |
| 103 | if (fTarget) { |
| 104 | return fTarget->internalHasPendingIO(); |
| 105 | } |
| 106 | |
| 107 | return SkToBool(fPendingWrites | fPendingReads); |
| 108 | } |
| 109 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 110 | // For deferred proxies this will be null. For wrapped proxies it will point to the |
| 111 | // wrapped resource. |
| 112 | GrSurface* fTarget; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | // This class is used to manage conversion of refs to pending reads/writes. |
| 116 | friend class GrGpuResourceRef; |
| 117 | template <typename, GrIOType> friend class GrPendingIOResource; |
| 118 | |
| 119 | void addPendingRead() const { |
| 120 | this->validate(); |
| 121 | |
| 122 | if (fTarget) { |
| 123 | fTarget->addPendingRead(); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | ++fPendingReads; |
| 128 | } |
| 129 | |
| 130 | void completedRead() const { |
| 131 | this->validate(); |
| 132 | |
| 133 | if (fTarget) { |
| 134 | fTarget->completedRead(); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | SkFAIL("How was the read completed if the Proxy hasn't been instantiated?"); |
| 139 | } |
| 140 | |
| 141 | void addPendingWrite() const { |
| 142 | this->validate(); |
| 143 | |
| 144 | if (fTarget) { |
| 145 | fTarget->addPendingWrite(); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | ++fPendingWrites; |
| 150 | } |
| 151 | |
| 152 | void completedWrite() const { |
| 153 | this->validate(); |
| 154 | |
| 155 | if (fTarget) { |
| 156 | fTarget->completedWrite(); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | SkFAIL("How was the write completed if the Proxy hasn't been instantiated?"); |
| 161 | } |
| 162 | |
| 163 | mutable int32_t fRefCnt; |
| 164 | mutable int32_t fPendingReads; |
| 165 | mutable int32_t fPendingWrites; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | class GrSurfaceProxy : public GrIORefProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 169 | public: |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 170 | static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>); |
| 171 | |
| 172 | static sk_sp<GrSurfaceProxy> MakeDeferred(const GrCaps&, const GrSurfaceDesc&, |
| 173 | SkBackingFit, SkBudgeted); |
| 174 | |
| 175 | // TODO: need to refine ownership semantics of 'srcData' if we're in completely |
| 176 | // deferred mode |
| 177 | static sk_sp<GrSurfaceProxy> MakeDeferred(const GrCaps&, GrTextureProvider*, |
| 178 | const GrSurfaceDesc&, SkBudgeted, |
| 179 | const void* srcData, size_t rowBytes); |
| 180 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 181 | static sk_sp<GrSurfaceProxy> MakeWrappedBackend( |
| 182 | GrContext*, |
| 183 | GrBackendTextureDesc&, |
| 184 | GrWrapOwnership ownership = kBorrow_GrWrapOwnership); |
| 185 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 186 | const GrSurfaceDesc& desc() const { return fDesc; } |
| 187 | |
| 188 | GrSurfaceOrigin origin() const { |
| 189 | SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || |
| 190 | kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin); |
| 191 | return fDesc.fOrigin; |
| 192 | } |
| 193 | int width() const { return fDesc.fWidth; } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 194 | int height() const { return fDesc.fHeight; } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 195 | GrPixelConfig config() const { return fDesc.fConfig; } |
| 196 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 197 | class UniqueID { |
| 198 | public: |
| 199 | // wrapped |
| 200 | explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { } |
| 201 | // deferred |
| 202 | UniqueID() : fID(GrGpuResource::CreateUniqueID()) { } |
| 203 | |
| 204 | uint32_t asUInt() const { return fID; } |
| 205 | |
| 206 | bool operator==(const UniqueID& other) const { |
| 207 | return fID == other.fID; |
| 208 | } |
| 209 | bool operator!=(const UniqueID& other) const { |
| 210 | return !(*this == other); |
| 211 | } |
| 212 | |
| 213 | bool isInvalid() const { return SK_InvalidUniqueID == fID; } |
| 214 | |
| 215 | private: |
| 216 | const uint32_t fID; |
| 217 | }; |
| 218 | |
| 219 | /* |
| 220 | * The contract for the uniqueID is: |
| 221 | * for wrapped resources: |
| 222 | * the uniqueID will match that of the wrapped resource |
| 223 | * |
| 224 | * for deferred resources: |
| 225 | * the uniqueID will be different from the real resource, when it is allocated |
| 226 | * the proxy's uniqueID will not change across the instantiate call |
| 227 | * |
| 228 | * the uniqueIDs of the proxies and the resources draw from the same pool |
| 229 | * |
| 230 | * What this boils down to is that the uniqueID of a proxy can be used to consistently |
| 231 | * track/identify a proxy but should never be used to distinguish between |
| 232 | * resources and proxies - beware! |
| 233 | */ |
| 234 | UniqueID uniqueID() const { return fUniqueID; } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 235 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 236 | GrSurface* instantiate(GrTextureProvider* texProvider); |
| 237 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 238 | /** |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 239 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 240 | */ |
| 241 | SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); } |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 242 | |
| 243 | int worstCaseWidth(const GrCaps& caps) const; |
| 244 | int worstCaseHeight(const GrCaps& caps) const; |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 245 | |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 246 | /** |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 247 | * @return the texture proxy associated with the surface proxy, may be NULL. |
| 248 | */ |
| 249 | virtual GrTextureProxy* asTextureProxy() { return nullptr; } |
| 250 | virtual const GrTextureProxy* asTextureProxy() const { return nullptr; } |
| 251 | |
| 252 | /** |
| 253 | * @return the render target proxy associated with the surface proxy, may be NULL. |
| 254 | */ |
| 255 | virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; } |
| 256 | virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; } |
| 257 | |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Does the resource count against the resource budget? |
| 260 | */ |
| 261 | SkBudgeted isBudgeted() const { return fBudgeted; } |
| 262 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 263 | void setLastOpList(GrOpList* opList); |
| 264 | GrOpList* getLastOpList() { return fLastOpList; } |
| 265 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 266 | GrRenderTargetOpList* getLastRenderTargetOpList(); |
| 267 | GrTextureOpList* getLastTextureOpList(); |
| 268 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 269 | /** |
| 270 | * Retrieves the amount of GPU memory that will be or currently is used by this resource |
| 271 | * in bytes. It is approximate since we aren't aware of additional padding or copies made |
| 272 | * by the driver. |
| 273 | * |
| 274 | * @return the amount of GPU memory used in bytes |
| 275 | */ |
| 276 | size_t gpuMemorySize() const { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 277 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
| 278 | fGpuMemorySize = this->onGpuMemorySize(); |
| 279 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 280 | } |
| 281 | return fGpuMemorySize; |
| 282 | } |
| 283 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 284 | // Helper function that creates a temporary SurfaceContext to perform the copy |
| 285 | static sk_sp<GrSurfaceProxy> Copy(GrContext*, GrSurfaceProxy* src, |
| 286 | SkIRect srcRect, SkBudgeted); |
| 287 | |
| 288 | // Copy the entire 'src' |
| 289 | static sk_sp<GrSurfaceProxy> Copy(GrContext* context, GrSurfaceProxy* src, |
| 290 | SkBudgeted budgeted) { |
| 291 | return Copy(context, src, SkIRect::MakeWH(src->width(), src->height()), budgeted); |
| 292 | } |
| 293 | |
| 294 | // Test-only entry point - should decrease in use as proxies propagate |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 295 | static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc, |
| 296 | GrSurfaceProxy* srcProxy); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 297 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 298 | bool isWrapped_ForTesting() const; |
| 299 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 300 | SkDEBUGCODE(void validate(GrContext*) const;) |
| 301 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 302 | // Provides access to functions that aren't part of the public API. |
| 303 | GrSurfaceProxyPriv priv(); |
| 304 | const GrSurfaceProxyPriv priv() const; |
| 305 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 306 | protected: |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 307 | // Deferred version |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 308 | GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted) |
| 309 | : fDesc(desc) |
| 310 | , fFit(fit) |
| 311 | , fBudgeted(budgeted) |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 312 | , fGpuMemorySize(kInvalidGpuMemorySize) |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 313 | , fLastOpList(nullptr) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 314 | // 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] | 315 | } |
| 316 | |
| 317 | // Wrapped version |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 318 | GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 319 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 320 | virtual ~GrSurfaceProxy(); |
| 321 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 322 | friend class GrSurfaceProxyPriv; |
| 323 | |
| 324 | // Methods made available via GrSurfaceProxyPriv |
| 325 | bool hasPendingIO() const { |
| 326 | return this->internalHasPendingIO(); |
| 327 | } |
| 328 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 329 | // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource. |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 330 | const GrSurfaceDesc fDesc; |
| 331 | const SkBackingFit fFit; // always exact for wrapped resources |
| 332 | const SkBudgeted fBudgeted; // set from the backing resource for wrapped resources |
| 333 | const UniqueID fUniqueID; // set from the backing resource for wrapped resources |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 334 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 335 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 336 | SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; }) |
| 337 | |
| 338 | private: |
| 339 | virtual size_t onGpuMemorySize() const = 0; |
| 340 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 341 | // This entry is lazily evaluated so, when the proxy wraps a resource, the resource |
| 342 | // will be called but, when the proxy is deferred, it will compute the answer itself. |
| 343 | // If the proxy computes its own answer that answer is checked (in debug mode) in |
| 344 | // the instantiation method. |
| 345 | mutable size_t fGpuMemorySize; |
| 346 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 347 | // The last opList that wrote to or is currently going to write to this surface |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 348 | // The opList can be closed (e.g., no render target context is currently bound |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 349 | // to this renderTarget). |
| 350 | // This back-pointer is required so that we can add a dependancy between |
| 351 | // the opList used to create the current contents of this surface |
| 352 | // and the opList of a destination surface to which this one is being drawn or copied. |
| 353 | GrOpList* fLastOpList; |
| 354 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 355 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 356 | typedef GrIORefProxy INHERITED; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | #endif |