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