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 | |
mtklein | 6f07665 | 2015-01-13 08:22:43 -0800 | [diff] [blame] | 90 | /** |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 91 | * Templated version of GrGpuResourceRef to enforce type safety. |
| 92 | */ |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 93 | template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef { |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 94 | public: |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 95 | GrTGpuResourceRef() {} |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 96 | |
| 97 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 98 | pending on the resource when markPendingIO is called. */ |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 99 | GrTGpuResourceRef(T* resource, GrIOType ioType) : INHERITED(resource, ioType) {} |
| 100 | GrTGpuResourceRef(sk_sp<T> resource, GrIOType ioType) : INHERITED(resource, ioType) {} |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 101 | |
| 102 | T* get() const { return static_cast<T*>(this->getResource()); } |
| 103 | |
| 104 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 105 | pending on the resource when markPendingIO is called. */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 106 | void set(T* resource, GrIOType ioType) { this->setResource(resource, ioType); } |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 107 | |
| 108 | private: |
| 109 | typedef GrGpuResourceRef INHERITED; |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 112 | /** |
| 113 | * This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a |
| 114 | * ref. |
| 115 | */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 116 | template <typename T, GrIOType IO_TYPE> class GrPendingIOResource : SkNoncopyable { |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 117 | public: |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 118 | GrPendingIOResource(T* resource = nullptr) : fResource(nullptr) { |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 119 | this->reset(resource); |
| 120 | } |
| 121 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 122 | GrPendingIOResource(const GrPendingIOResource& that) |
| 123 | : GrPendingIOResource(that.get()) { |
| 124 | } |
| 125 | |
Robert Phillips | 5efd5ea | 2017-05-30 13:47:32 -0400 | [diff] [blame] | 126 | void reset(T* resource = nullptr) { |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 127 | if (resource) { |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 128 | switch (IO_TYPE) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 129 | case kRead_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 130 | resource->addPendingRead(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 131 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 132 | case kWrite_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 133 | resource->addPendingWrite(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 134 | break; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 135 | case kRW_GrIOType: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 136 | resource->addPendingRead(); |
| 137 | resource->addPendingWrite(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 141 | this->release(); |
| 142 | fResource = resource; |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 143 | } |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 144 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 145 | ~GrPendingIOResource() { |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 146 | this->release(); |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 147 | } |
| 148 | |
reed | d9ffaed | 2015-11-10 04:55:08 -0800 | [diff] [blame] | 149 | explicit operator bool() const { return SkToBool(fResource); } |
| 150 | |
| 151 | bool operator==(const GrPendingIOResource& other) const { |
| 152 | return fResource == other.fResource; |
| 153 | } |
bsalomon | b03c4a3 | 2014-11-20 09:56:11 -0800 | [diff] [blame] | 154 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 155 | T* get() const { return fResource; } |
| 156 | |
| 157 | private: |
joshualitt | 7eb8c7b | 2014-11-18 14:24:27 -0800 | [diff] [blame] | 158 | void release() { |
| 159 | if (fResource) { |
| 160 | switch (IO_TYPE) { |
| 161 | case kRead_GrIOType: |
| 162 | fResource->completedRead(); |
| 163 | break; |
| 164 | case kWrite_GrIOType: |
| 165 | fResource->completedWrite(); |
| 166 | break; |
| 167 | case kRW_GrIOType: |
| 168 | fResource->completedRead(); |
| 169 | fResource->completedWrite(); |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 175 | T* fResource; |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 176 | }; |
Robert Phillips | 6200036 | 2018-02-01 09:10:04 -0500 | [diff] [blame] | 177 | |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 178 | #endif |