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 | * |
| 31 | * Like SkAutoTUnref, its constructor and setter adopt a ref from their caller. |
| 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 | |
| 46 | /** Shortcut for calling setResource() with NULL. It cannot be called after markingPendingIO |
| 47 | is called. */ |
| 48 | void reset(); |
| 49 | |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 50 | protected: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 51 | GrGpuResourceRef(); |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 52 | |
| 53 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 54 | pending on the resource when markPendingIO is called. */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 55 | GrGpuResourceRef(GrGpuResource*, GrIOType); |
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 | void setResource(GrGpuResource*, GrIOType); |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 60 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 61 | private: |
| 62 | /** Called by owning GrProgramElement when the program element is first scheduled for |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 63 | execution. It can only be called once. */ |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 64 | void markPendingIO() const; |
| 65 | |
| 66 | /** Called when the program element/draw state is no longer owned by GrDrawTarget-client code. |
| 67 | 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] | 68 | writes to the resource using the program element or draw state. It can only be called once. |
| 69 | */ |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 70 | void removeRef() const; |
| 71 | |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 72 | /** 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] | 73 | 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] | 74 | pending executions have been complete. Can only be called if removeRef() was not previously |
| 75 | called. */ |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 76 | void pendingIOComplete() const; |
| 77 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 78 | friend class GrProgramElement; |
| 79 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 80 | GrGpuResource* fResource; |
| 81 | mutable bool fOwnRef; |
| 82 | mutable bool fPendingIO; |
| 83 | GrIOType fIOType; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 84 | |
| 85 | typedef SkNoncopyable INHERITED; |
| 86 | }; |
| 87 | |
mtklein | 6f07665 | 2015-01-13 08:22:43 -0800 | [diff] [blame] | 88 | /** |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 89 | * Templated version of GrGpuResourceRef to enforce type safety. |
| 90 | */ |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 91 | template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef { |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 92 | public: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 93 | GrTGpuResourceRef() {} |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 94 | |
| 95 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 96 | pending on the resource when markPendingIO is called. */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 97 | GrTGpuResourceRef(T* resource, GrIOType ioType) : INHERITED(resource, ioType) { } |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 98 | |
| 99 | T* get() const { return static_cast<T*>(this->getResource()); } |
| 100 | |
| 101 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 102 | pending on the resource when markPendingIO is called. */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 103 | void set(T* resource, GrIOType ioType) { this->setResource(resource, ioType); } |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 104 | |
| 105 | private: |
| 106 | typedef GrGpuResourceRef INHERITED; |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 109 | // Specializations for GrTexture and GrRenderTarget because they use virtual inheritance. |
| 110 | template<> class GrTGpuResourceRef<GrTexture> : public GrGpuResourceRef { |
| 111 | public: |
| 112 | GrTGpuResourceRef() {} |
| 113 | |
| 114 | GrTGpuResourceRef(GrTexture* texture, GrIOType ioType) : INHERITED(texture, ioType) { } |
| 115 | |
| 116 | GrTexture* get() const { |
| 117 | GrSurface* surface = static_cast<GrSurface*>(this->getResource()); |
| 118 | if (surface) { |
| 119 | return surface->asTexture(); |
| 120 | } else { |
| 121 | return NULL; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void set(GrTexture* texture, GrIOType ioType) { this->setResource(texture, ioType); } |
| 126 | |
| 127 | private: |
| 128 | typedef GrGpuResourceRef INHERITED; |
| 129 | }; |
| 130 | |
| 131 | template<> class GrTGpuResourceRef<GrRenderTarget> : public GrGpuResourceRef { |
| 132 | public: |
| 133 | GrTGpuResourceRef() {} |
| 134 | |
| 135 | GrTGpuResourceRef(GrRenderTarget* rt, GrIOType ioType) : INHERITED(rt, ioType) { } |
| 136 | |
| 137 | GrRenderTarget* get() const { |
| 138 | GrSurface* surface = static_cast<GrSurface*>(this->getResource()); |
| 139 | if (surface) { |
| 140 | return surface->asRenderTarget(); |
| 141 | } else { |
| 142 | return NULL; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void set(GrRenderTarget* rt, GrIOType ioType) { this->setResource(rt, ioType); } |
| 147 | |
| 148 | private: |
| 149 | typedef GrGpuResourceRef INHERITED; |
| 150 | }; |
| 151 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 152 | /** |
| 153 | * This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a |
| 154 | * ref. |
| 155 | */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 156 | template <typename T, GrIOType IO_TYPE> class GrPendingIOResource : SkNoncopyable { |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 157 | public: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 158 | GrPendingIOResource(T* resource = NULL) : fResource(NULL) { |
| 159 | this->reset(resource); |
| 160 | } |
| 161 | |
| 162 | void reset(T* resource) { |
| 163 | if (resource) { |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 164 | switch (IO_TYPE) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 165 | case kRead_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 166 | resource->addPendingRead(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 167 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 168 | case kWrite_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 169 | resource->addPendingWrite(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 170 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 171 | case kRW_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 172 | resource->addPendingRead(); |
| 173 | resource->addPendingWrite(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | } |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 177 | this->release(); |
| 178 | fResource = resource; |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 179 | } |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 180 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 181 | ~GrPendingIOResource() { |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 182 | this->release(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 183 | } |
| 184 | |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 185 | operator bool() const { return SkToBool(fResource); } |
| 186 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 187 | T* get() const { return fResource; } |
| 188 | |
| 189 | private: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 190 | void release() { |
| 191 | if (fResource) { |
| 192 | switch (IO_TYPE) { |
| 193 | case kRead_GrIOType: |
| 194 | fResource->completedRead(); |
| 195 | break; |
| 196 | case kWrite_GrIOType: |
| 197 | fResource->completedWrite(); |
| 198 | break; |
| 199 | case kRW_GrIOType: |
| 200 | fResource->completedRead(); |
| 201 | fResource->completedWrite(); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 207 | T* fResource; |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 208 | }; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 209 | #endif |