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 | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 15 | class GrOpList; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame^] | 16 | class GrTextureProvider; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 17 | class GrTextureProxy; |
| 18 | class GrRenderTargetProxy; |
| 19 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 20 | // This class replicates the functionality GrIORef<GrSurface> but tracks the |
| 21 | // utilitization for later resource allocation (for the deferred case) and |
| 22 | // forwards on the utilization in the wrapped case |
| 23 | class GrIORefProxy : public SkNoncopyable { |
| 24 | public: |
| 25 | void ref() const { |
| 26 | this->validate(); |
| 27 | |
| 28 | ++fRefCnt; |
| 29 | if (fTarget) { |
| 30 | fTarget->ref(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void unref() const { |
| 35 | this->validate(); |
| 36 | |
| 37 | if (fTarget) { |
| 38 | fTarget->unref(); |
| 39 | } |
| 40 | |
| 41 | if (!(--fRefCnt)) { |
| 42 | delete this; |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | this->validate(); |
| 47 | } |
| 48 | |
| 49 | void validate() const { |
| 50 | #ifdef SK_DEBUG |
| 51 | SkASSERT(fRefCnt >= 0); |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | GrIORefProxy() : fRefCnt(1), fTarget(nullptr) {} |
| 57 | GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1) { |
| 58 | // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing |
| 59 | // anything extra. |
| 60 | fTarget = surface.release(); |
| 61 | } |
| 62 | virtual ~GrIORefProxy() { |
| 63 | // We don't unref 'fTarget' here since the 'unref' method will already |
| 64 | // have forwarded on the unref call that got use here. |
| 65 | } |
| 66 | |
| 67 | // TODO: add the IO ref counts. Although if we can delay shader creation to flush time |
| 68 | // we may not even need to do that. |
| 69 | mutable int32_t fRefCnt; |
| 70 | |
| 71 | // For deferred proxies this will be null. For wrapped proxies it will point to the |
| 72 | // wrapped resource. |
| 73 | GrSurface* fTarget; |
| 74 | }; |
| 75 | |
| 76 | class GrSurfaceProxy : public GrIORefProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 77 | public: |
| 78 | const GrSurfaceDesc& desc() const { return fDesc; } |
| 79 | |
| 80 | GrSurfaceOrigin origin() const { |
| 81 | SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || |
| 82 | kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin); |
| 83 | return fDesc.fOrigin; |
| 84 | } |
| 85 | int width() const { return fDesc.fWidth; } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 86 | int height() const { return fDesc.fHeight; } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 87 | GrPixelConfig config() const { return fDesc.fConfig; } |
| 88 | |
| 89 | uint32_t uniqueID() const { return fUniqueID; } |
| 90 | |
| 91 | /** |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 92 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 93 | */ |
| 94 | SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); } |
| 95 | |
| 96 | /** |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 97 | * @return the texture proxy associated with the surface proxy, may be NULL. |
| 98 | */ |
| 99 | virtual GrTextureProxy* asTextureProxy() { return nullptr; } |
| 100 | virtual const GrTextureProxy* asTextureProxy() const { return nullptr; } |
| 101 | |
| 102 | /** |
| 103 | * @return the render target proxy associated with the surface proxy, may be NULL. |
| 104 | */ |
| 105 | virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; } |
| 106 | virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; } |
| 107 | |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Does the resource count against the resource budget? |
| 110 | */ |
| 111 | SkBudgeted isBudgeted() const { return fBudgeted; } |
| 112 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 113 | void setLastOpList(GrOpList* opList); |
| 114 | GrOpList* getLastOpList() { return fLastOpList; } |
| 115 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 116 | /** |
| 117 | * Retrieves the amount of GPU memory that will be or currently is used by this resource |
| 118 | * in bytes. It is approximate since we aren't aware of additional padding or copies made |
| 119 | * by the driver. |
| 120 | * |
| 121 | * @return the amount of GPU memory used in bytes |
| 122 | */ |
| 123 | size_t gpuMemorySize() const { |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 124 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
| 125 | fGpuMemorySize = this->onGpuMemorySize(); |
| 126 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 127 | } |
| 128 | return fGpuMemorySize; |
| 129 | } |
| 130 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame^] | 131 | bool isWrapped_ForTesting() const; |
| 132 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 133 | protected: |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 134 | // Deferred version |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 135 | GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted) |
| 136 | : fDesc(desc) |
| 137 | , fFit(fit) |
| 138 | , fBudgeted(budgeted) |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 139 | , fUniqueID(GrGpuResource::CreateUniqueID()) |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 140 | , fGpuMemorySize(kInvalidGpuMemorySize) |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 141 | , fLastOpList(nullptr) { |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Wrapped version |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 145 | GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 146 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 147 | virtual ~GrSurfaceProxy(); |
| 148 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame^] | 149 | GrSurface* instantiate(GrTextureProvider* texProvider); |
| 150 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 151 | // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource. |
| 152 | const GrSurfaceDesc fDesc; |
| 153 | const SkBackingFit fFit; // always exact for wrapped resources |
| 154 | const SkBudgeted fBudgeted; // set from the backing resource for wrapped resources |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 155 | const uint32_t fUniqueID; // set from the backing resource for wrapped resources |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 156 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 157 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 158 | SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; }) |
| 159 | |
| 160 | private: |
| 161 | virtual size_t onGpuMemorySize() const = 0; |
| 162 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 163 | // This entry is lazily evaluated so, when the proxy wraps a resource, the resource |
| 164 | // will be called but, when the proxy is deferred, it will compute the answer itself. |
| 165 | // If the proxy computes its own answer that answer is checked (in debug mode) in |
| 166 | // the instantiation method. |
| 167 | mutable size_t fGpuMemorySize; |
| 168 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 169 | // 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] | 170 | // 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] | 171 | // to this renderTarget). |
| 172 | // This back-pointer is required so that we can add a dependancy between |
| 173 | // the opList used to create the current contents of this surface |
| 174 | // and the opList of a destination surface to which this one is being drawn or copied. |
| 175 | GrOpList* fLastOpList; |
| 176 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 177 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 178 | typedef GrIORefProxy INHERITED; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | #endif |