bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 8 | #ifndef GrGpuResourceRef_DEFINED |
| 9 | #define GrGpuResourceRef_DEFINED |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 10 | |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 11 | #include "GrGpuResource.h" |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 12 | #include "GrRenderTarget.h" |
| 13 | #include "GrTexture.h" |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
| 15 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 16 | /** |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 17 | * This class is intended only for internal use in core Gr code. |
| 18 | * |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 19 | * Class that wraps a resource referenced by a GrProgramElement or GrDrawState. It manages |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 20 | * converting refs to pending IO operations. It allows a resource ownership to be in three |
| 21 | * states: |
| 22 | * 1. Owns a single ref |
| 23 | * 2. Owns a single ref and a pending IO operation (read, write, or read-write) |
| 24 | * 3. Owns a single pending IO operation. |
| 25 | * |
| 26 | * It is legal to destroy the GrGpuResourceRef in any of these states. It starts in state |
| 27 | * 1. Calling markPendingIO() converts it from state 1 to state 2. Calling removeRef() goes from |
| 28 | * state 2 to state 3. Calling pendingIOComplete() moves from state 2 to state 1. There is no |
| 29 | * valid way of going from state 3 back to 2 or 1. |
| 30 | * |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 31 | * Like sk_sp, its constructor and setter adopt a ref from their caller. |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 32 | * |
| 33 | * TODO: Once GrDODrawState no longer exists and therefore GrDrawState and GrOptDrawState no |
| 34 | * longer share an instance of this class, attempt to make the resource owned by GrGpuResourceRef |
| 35 | * only settable via the constructor. |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 36 | */ |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 37 | class GrGpuResourceRef : SkNoncopyable { |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 38 | public: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 39 | ~GrGpuResourceRef(); |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 40 | |
| 41 | GrGpuResource* getResource() const { return fResource; } |
| 42 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 43 | /** Does this object own a pending read or write on the resource it is wrapping. */ |
| 44 | bool ownsPendingIO() const { return fPendingIO; } |
| 45 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 46 | /** What type of IO does this represent? This is independent of whether a normal ref or a |
| 47 | pending IO is currently held. */ |
| 48 | GrIOType ioType() const { return fIOType; } |
| 49 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 50 | /** Shortcut for calling setResource() with NULL. It cannot be called after markingPendingIO |
| 51 | is called. */ |
| 52 | void reset(); |
| 53 | |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 54 | protected: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 55 | GrGpuResourceRef(); |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 56 | |
| 57 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 58 | pending on the resource when markPendingIO is called. */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 59 | GrGpuResourceRef(GrGpuResource*, GrIOType); |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 60 | |
| 61 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 62 | pending on the resource when markPendingIO is called. */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 63 | void setResource(GrGpuResource*, GrIOType); |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 64 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 65 | private: |
| 66 | /** Called by owning GrProgramElement when the program element is first scheduled for |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 67 | execution. It can only be called once. */ |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 68 | void markPendingIO() const; |
| 69 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 70 | /** Called when the program element/draw state is no longer owned by GrOpList-client code. |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 71 | This lets the cache know that the drawing code will no longer schedule additional reads or |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 72 | writes to the resource using the program element or draw state. It can only be called once. |
| 73 | */ |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 74 | void removeRef() const; |
| 75 | |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 76 | /** Called to indicate that the previous pending IO is complete. Useful when the owning object |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 77 | still has refs, so it is not about to destroy this GrGpuResourceRef, but its previously |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 78 | pending executions have been complete. Can only be called if removeRef() was not previously |
| 79 | called. */ |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 80 | void pendingIOComplete() const; |
| 81 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 82 | friend class GrProgramElement; |
| 83 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 84 | GrGpuResource* fResource; |
| 85 | mutable bool fOwnRef; |
| 86 | mutable bool fPendingIO; |
| 87 | GrIOType fIOType; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 88 | |
| 89 | typedef SkNoncopyable INHERITED; |
| 90 | }; |
| 91 | |
mtklein | 6f07665 | 2015-01-13 08:22:43 -0800 | [diff] [blame] | 92 | /** |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 93 | * Templated version of GrGpuResourceRef to enforce type safety. |
| 94 | */ |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 95 | template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef { |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 96 | public: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 97 | GrTGpuResourceRef() {} |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 98 | |
| 99 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 100 | pending on the resource when markPendingIO is called. */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 101 | GrTGpuResourceRef(T* resource, GrIOType ioType) : INHERITED(resource, ioType) { } |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 102 | |
| 103 | T* get() const { return static_cast<T*>(this->getResource()); } |
| 104 | |
| 105 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 106 | pending on the resource when markPendingIO is called. */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 107 | void set(T* resource, GrIOType ioType) { this->setResource(resource, ioType); } |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | typedef GrGpuResourceRef INHERITED; |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 113 | // Specializations for GrTexture and GrRenderTarget because they use virtual inheritance. |
| 114 | template<> class GrTGpuResourceRef<GrTexture> : public GrGpuResourceRef { |
| 115 | public: |
| 116 | GrTGpuResourceRef() {} |
| 117 | |
| 118 | GrTGpuResourceRef(GrTexture* texture, GrIOType ioType) : INHERITED(texture, ioType) { } |
| 119 | |
| 120 | GrTexture* get() const { |
| 121 | GrSurface* surface = static_cast<GrSurface*>(this->getResource()); |
| 122 | if (surface) { |
| 123 | return surface->asTexture(); |
| 124 | } else { |
| 125 | return NULL; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void set(GrTexture* texture, GrIOType ioType) { this->setResource(texture, ioType); } |
| 130 | |
| 131 | private: |
| 132 | typedef GrGpuResourceRef INHERITED; |
| 133 | }; |
| 134 | |
| 135 | template<> class GrTGpuResourceRef<GrRenderTarget> : public GrGpuResourceRef { |
| 136 | public: |
| 137 | GrTGpuResourceRef() {} |
| 138 | |
| 139 | GrTGpuResourceRef(GrRenderTarget* rt, GrIOType ioType) : INHERITED(rt, ioType) { } |
| 140 | |
| 141 | GrRenderTarget* get() const { |
| 142 | GrSurface* surface = static_cast<GrSurface*>(this->getResource()); |
| 143 | if (surface) { |
| 144 | return surface->asRenderTarget(); |
| 145 | } else { |
| 146 | return NULL; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void set(GrRenderTarget* rt, GrIOType ioType) { this->setResource(rt, ioType); } |
| 151 | |
| 152 | private: |
| 153 | typedef GrGpuResourceRef INHERITED; |
| 154 | }; |
| 155 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 156 | /** |
| 157 | * This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a |
| 158 | * ref. |
| 159 | */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 160 | template <typename T, GrIOType IO_TYPE> class GrPendingIOResource : SkNoncopyable { |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 161 | public: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 162 | GrPendingIOResource(T* resource = NULL) : fResource(NULL) { |
| 163 | this->reset(resource); |
| 164 | } |
| 165 | |
| 166 | void reset(T* resource) { |
| 167 | if (resource) { |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 168 | switch (IO_TYPE) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 169 | case kRead_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 170 | resource->addPendingRead(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 171 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 172 | case kWrite_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 173 | resource->addPendingWrite(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 174 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 175 | case kRW_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 176 | resource->addPendingRead(); |
| 177 | resource->addPendingWrite(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 181 | this->release(); |
| 182 | fResource = resource; |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 183 | } |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 184 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 185 | ~GrPendingIOResource() { |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 186 | this->release(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 187 | } |
| 188 | |
reed | d9ffaed | 2015-11-10 04:55:08 -0800 | [diff] [blame] | 189 | explicit operator bool() const { return SkToBool(fResource); } |
| 190 | |
| 191 | bool operator==(const GrPendingIOResource& other) const { |
| 192 | return fResource == other.fResource; |
| 193 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 194 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 195 | T* get() const { return fResource; } |
| 196 | |
| 197 | private: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 198 | void release() { |
| 199 | if (fResource) { |
| 200 | switch (IO_TYPE) { |
| 201 | case kRead_GrIOType: |
| 202 | fResource->completedRead(); |
| 203 | break; |
| 204 | case kWrite_GrIOType: |
| 205 | fResource->completedWrite(); |
| 206 | break; |
| 207 | case kRW_GrIOType: |
| 208 | fResource->completedRead(); |
| 209 | fResource->completedWrite(); |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 215 | T* fResource; |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 216 | }; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 217 | #endif |