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