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 | * |
| 29 | * Like SkAutoTUnref, its constructor and setter adopt a ref from their caller. |
| 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 | SK_DECLARE_INST_COUNT_ROOT(GrGpuResourceRef); |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 38 | |
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 | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 55 | GrGpuResourceRef(GrGpuResource*, GrIORef::IOType); |
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 | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 59 | void setResource(GrGpuResource*, GrIORef::IOType); |
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 | |
egdaniel | 89af44a | 2014-09-26 06:15:04 -0700 | [diff] [blame] | 78 | friend class GrDrawState; |
| 79 | friend class GrOptDrawState; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 80 | friend class GrProgramElement; |
| 81 | |
| 82 | GrGpuResource* fResource; |
| 83 | mutable bool fOwnRef; |
| 84 | mutable bool fPendingIO; |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 85 | GrIORef::IOType fIOType; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 86 | |
| 87 | typedef SkNoncopyable INHERITED; |
| 88 | }; |
| 89 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 90 | /** |
| 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. */ |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 99 | GrTGpuResourceRef(T* resource, GrIORef::IOType ioType) : INHERITED(resource, ioType) {} |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 100 | |
| 101 | T* get() const { return static_cast<T*>(this->getResource()); } |
| 102 | |
| 103 | /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as |
| 104 | pending on the resource when markPendingIO is called. */ |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 105 | void set(T* resource, GrIORef::IOType ioType) { this->setResource(resource, ioType); } |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | typedef GrGpuResourceRef INHERITED; |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 111 | /** |
| 112 | * This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a |
| 113 | * ref. |
| 114 | */ |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 115 | template <typename T, GrIORef::IOType IO_TYPE> class GrPendingIOResource : SkNoncopyable { |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 116 | public: |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 117 | GrPendingIOResource(T* resource) : fResource(resource) { |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 118 | if (NULL != fResource) { |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 119 | switch (IO_TYPE) { |
| 120 | case GrIORef::kRead_IOType: |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 121 | fResource->addPendingRead(); |
| 122 | break; |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 123 | case GrIORef::kWrite_IOType: |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 124 | fResource->addPendingWrite(); |
| 125 | break; |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 126 | case GrIORef::kRW_IOType: |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 127 | fResource->addPendingRead(); |
| 128 | fResource->addPendingWrite(); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
bsalomon | c492334 | 2014-09-16 13:54:53 -0700 | [diff] [blame] | 133 | |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 134 | ~GrPendingIOResource() { |
| 135 | if (NULL != fResource) { |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 136 | switch (IO_TYPE) { |
| 137 | case GrIORef::kRead_IOType: |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 138 | fResource->completedRead(); |
| 139 | break; |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 140 | case GrIORef::kWrite_IOType: |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 141 | fResource->completedWrite(); |
| 142 | break; |
bsalomon | 45725db | 2014-09-19 11:48:02 -0700 | [diff] [blame] | 143 | case GrIORef::kRW_IOType: |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 144 | fResource->completedRead(); |
| 145 | fResource->completedWrite(); |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | T* get() const { return fResource; } |
| 152 | |
| 153 | private: |
| 154 | T* fResource; |
bsalomon | b3e3a95 | 2014-09-19 11:10:40 -0700 | [diff] [blame] | 155 | }; |
bsalomon | 9574098 | 2014-09-04 13:12:37 -0700 | [diff] [blame] | 156 | #endif |