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