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