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 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 115 | protected: |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 116 | // Deferred version |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 117 | GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted) |
| 118 | : fDesc(desc) |
| 119 | , fFit(fit) |
| 120 | , fBudgeted(budgeted) |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 121 | , fUniqueID(GrGpuResource::CreateUniqueID()) |
| 122 | , fLastOpList(nullptr) { |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Wrapped version |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 126 | GrSurfaceProxy(sk_sp<GrSurface> surface, SkBackingFit fit); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 127 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 128 | virtual ~GrSurfaceProxy(); |
| 129 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 130 | // For wrapped resources, 'fDesc' will always be filled in from the wrapped resource. |
| 131 | const GrSurfaceDesc fDesc; |
| 132 | const SkBackingFit fFit; // always exact for wrapped resources |
| 133 | const SkBudgeted fBudgeted; // set from the backing resource for wrapped resources |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 134 | const uint32_t fUniqueID; // set from the backing resource for wrapped resources |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 135 | |
| 136 | private: |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 137 | // 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] | 138 | // 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] | 139 | // to this renderTarget). |
| 140 | // This back-pointer is required so that we can add a dependancy between |
| 141 | // the opList used to create the current contents of this surface |
| 142 | // and the opList of a destination surface to which this one is being drawn or copied. |
| 143 | GrOpList* fLastOpList; |
| 144 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 145 | typedef GrIORefProxy INHERITED; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | #endif |