OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrGpuResourceRef_DEFINED | 8 #ifndef GrGpuResourceRef_DEFINED |
9 #define GrGpuResourceRef_DEFINED | 9 #define GrGpuResourceRef_DEFINED |
10 | 10 |
11 #include "GrGpuResource.h" | |
11 #include "SkRefCnt.h" | 12 #include "SkRefCnt.h" |
12 | 13 |
13 class GrGpuResource; | |
14 | |
15 /** | 14 /** |
16 * This class is intended only for internal use in core Gr code. | 15 * This class is intended only for internal use in core Gr code. |
17 * | 16 * |
18 * Class that wraps a resource referenced by a GrProgramElement or GrDrawState. It manages | 17 * Class that wraps a resource referenced by a GrProgramElement or GrDrawState. It manages |
19 * converting refs to pending IO operations. It allows a resource ownership to b e in three | 18 * converting refs to pending IO operations. It allows a resource ownership to b e in three |
20 * states: | 19 * states: |
21 * 1. Owns a single ref | 20 * 1. Owns a single ref |
22 * 2. Owns a single ref and a pending IO operation (read, write, or rea d-write) | 21 * 2. Owns a single ref and a pending IO operation (read, write, or rea d-write) |
23 * 3. Owns a single pending IO operation. | 22 * 3. Owns a single pending IO operation. |
24 * | 23 * |
25 * It is legal to destroy the GrGpuResourceRef in any of these states. It starts in state | 24 * It is legal to destroy the GrGpuResourceRef in any of these states. It starts in state |
26 * 1. Calling markPendingIO() converts it from state 1 to state 2. Calling remov eRef() goes from | 25 * 1. Calling markPendingIO() converts it from state 1 to state 2. Calling remov eRef() goes from |
27 * state 2 to state 3. Calling pendingIOComplete() moves from state 2 to state 1 . There is no | 26 * state 2 to state 3. Calling pendingIOComplete() moves from state 2 to state 1 . There is no |
28 * valid way of going from state 3 back to 2 or 1. | 27 * valid way of going from state 3 back to 2 or 1. |
29 * | 28 * |
30 * Like SkAutoTUnref, its constructor and setter adopt a ref from their caller. | 29 * Like SkAutoTUnref, its constructor and setter adopt a ref from their caller. |
31 * | 30 * |
32 * TODO: Once GrDODrawState no longer exists and therefore GrDrawState and GrOpt DrawState no | 31 * TODO: Once GrDODrawState no longer exists and therefore GrDrawState and GrOpt DrawState no |
33 * longer share an instance of this class, attempt to make the resource owned by GrGpuResourceRef | 32 * longer share an instance of this class, attempt to make the resource owned by GrGpuResourceRef |
34 * only settable via the constructor. | 33 * only settable via the constructor. |
35 */ | 34 */ |
36 class GrGpuResourceRef : SkNoncopyable { | 35 class GrGpuResourceRef : SkNoncopyable { |
37 public: | 36 public: |
38 SK_DECLARE_INST_COUNT_ROOT(GrGpuResourceRef); | 37 SK_DECLARE_INST_COUNT_ROOT(GrGpuResourceRef); |
39 | 38 |
40 enum IOType { | |
41 kRead_IOType, | |
42 kWrite_IOType, | |
43 kRW_IOType, | |
44 | |
45 kNone_IOType, // For internal use only, don't specify to constructor or setResource(). | |
46 }; | |
47 | |
48 ~GrGpuResourceRef(); | 39 ~GrGpuResourceRef(); |
49 | 40 |
50 GrGpuResource* getResource() const { return fResource; } | 41 GrGpuResource* getResource() const { return fResource; } |
51 | 42 |
52 /** Does this object own a pending read or write on the resource it is wrapp ing. */ | 43 /** Does this object own a pending read or write on the resource it is wrapp ing. */ |
53 bool ownsPendingIO() const { return fPendingIO; } | 44 bool ownsPendingIO() const { return fPendingIO; } |
54 | 45 |
55 /** Shortcut for calling setResource() with NULL. It cannot be called after markingPendingIO | 46 /** Shortcut for calling setResource() with NULL. It cannot be called after markingPendingIO |
56 is called. */ | 47 is called. */ |
57 void reset(); | 48 void reset(); |
58 | 49 |
59 protected: | 50 protected: |
60 GrGpuResourceRef(); | 51 GrGpuResourceRef(); |
61 | 52 |
62 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as | 53 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as |
63 pending on the resource when markPendingIO is called. */ | 54 pending on the resource when markPendingIO is called. */ |
64 GrGpuResourceRef(GrGpuResource*, IOType); | 55 GrGpuResourceRef(GrGpuResource*, GrIORef::IOType); |
65 | 56 |
66 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as | 57 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as |
67 pending on the resource when markPendingIO is called. */ | 58 pending on the resource when markPendingIO is called. */ |
68 void setResource(GrGpuResource*, IOType); | 59 void setResource(GrGpuResource*, GrIORef::IOType); |
69 | 60 |
70 private: | 61 private: |
71 /** Called by owning GrProgramElement when the program element is first sche duled for | 62 /** Called by owning GrProgramElement when the program element is first sche duled for |
72 execution. It can only be called once. */ | 63 execution. It can only be called once. */ |
73 void markPendingIO() const; | 64 void markPendingIO() const; |
74 | 65 |
75 /** Called when the program element/draw state is no longer owned by GrDrawT arget-client code. | 66 /** Called when the program element/draw state is no longer owned by GrDrawT arget-client code. |
76 This lets the cache know that the drawing code will no longer schedule a dditional reads or | 67 This lets the cache know that the drawing code will no longer schedule a dditional reads or |
77 writes to the resource using the program element or draw state. It can o nly be called once. | 68 writes to the resource using the program element or draw state. It can o nly be called once. |
78 */ | 69 */ |
79 void removeRef() const; | 70 void removeRef() const; |
80 | 71 |
81 /** Called to indicate that the previous pending IO is complete. Useful when the owning object | 72 /** Called to indicate that the previous pending IO is complete. Useful when the owning object |
82 still has refs, so it is not about to destroy this GrGpuResourceRef, but its previously | 73 still has refs, so it is not about to destroy this GrGpuResourceRef, but its previously |
83 pending executions have been complete. Can only be called if removeRef() was not previously | 74 pending executions have been complete. Can only be called if removeRef() was not previously |
84 called. */ | 75 called. */ |
85 void pendingIOComplete() const; | 76 void pendingIOComplete() const; |
86 | 77 |
87 friend class GrRODrawState; | 78 friend class GrRODrawState; |
88 friend class GrProgramElement; | 79 friend class GrProgramElement; |
89 | 80 |
90 GrGpuResource* fResource; | 81 GrGpuResource* fResource; |
91 mutable bool fOwnRef; | 82 mutable bool fOwnRef; |
92 mutable bool fPendingIO; | 83 mutable bool fPendingIO; |
93 IOType fIOType; | 84 GrIORef::IOType fIOType; |
94 | 85 |
95 typedef SkNoncopyable INHERITED; | 86 typedef SkNoncopyable INHERITED; |
96 }; | 87 }; |
97 | 88 |
98 /** | 89 /** |
99 * Templated version of GrGpuResourceRef to enforce type safety. | 90 * Templated version of GrGpuResourceRef to enforce type safety. |
100 */ | 91 */ |
101 template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef { | 92 template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef { |
102 public: | 93 public: |
103 GrTGpuResourceRef() {} | 94 GrTGpuResourceRef() {} |
104 | 95 |
105 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as | 96 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as |
106 pending on the resource when markPendingIO is called. */ | 97 pending on the resource when markPendingIO is called. */ |
107 GrTGpuResourceRef(T* resource, IOType ioType) : INHERITED(resource, ioType) {} | 98 GrTGpuResourceRef(T* resource, GrIORef::IOType ioType) : INHERITED(resource, ioType) {} |
108 | 99 |
109 T* get() const { return static_cast<T*>(this->getResource()); } | 100 T* get() const { return static_cast<T*>(this->getResource()); } |
110 | 101 |
111 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as | 102 /** Adopts a ref from the caller. ioType expresses what type of IO operation s will be marked as |
112 pending on the resource when markPendingIO is called. */ | 103 pending on the resource when markPendingIO is called. */ |
113 void set(T* resource, IOType ioType) { this->setResource(resource, ioType); } | 104 void set(T* resource, GrIORef::IOType ioType) { this->setResource(resource, ioType); } |
114 | 105 |
115 private: | 106 private: |
116 typedef GrGpuResourceRef INHERITED; | 107 typedef GrGpuResourceRef INHERITED; |
117 }; | 108 }; |
118 | 109 |
119 /** | 110 /** |
120 * This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a | 111 * This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a |
121 * ref. | 112 * ref. |
122 */ | 113 */ |
robertphillips
2014/09/19 13:17:03
This seems very anti-templatey.
bsalomon
2014/09/19 13:32:58
Can you elaborate?
| |
123 template <typename T> class GrPendingIOResource : SkNoncopyable { | 114 template <typename T, GrIORef::IOType IO_TYPE> class GrPendingIOResource : SkNon copyable { |
124 public: | 115 public: |
125 typedef GrGpuResourceRef::IOType IOType; | 116 GrPendingIOResource(T* resource) : fResource(resource) { |
126 GrPendingIOResource(T* resource, IOType ioType) : fResource(resource), fIOTy pe(ioType) { | |
127 if (NULL != fResource) { | 117 if (NULL != fResource) { |
128 switch (fIOType) { | 118 switch (IO_TYPE) { |
129 case GrGpuResourceRef::kNone_IOType: | 119 case GrIORef::kRead_IOType: |
130 SkFAIL("GrPendingIOResource with neither reads nor writes?") ; | |
131 break; | |
132 case GrGpuResourceRef::kRead_IOType: | |
133 fResource->addPendingRead(); | 120 fResource->addPendingRead(); |
134 break; | 121 break; |
135 case GrGpuResourceRef::kWrite_IOType: | 122 case GrIORef::kWrite_IOType: |
136 fResource->addPendingWrite(); | 123 fResource->addPendingWrite(); |
137 break; | 124 break; |
138 case GrGpuResourceRef::kRW_IOType: | 125 case GrIORef::kRW_IOType: |
139 fResource->addPendingRead(); | 126 fResource->addPendingRead(); |
140 fResource->addPendingWrite(); | 127 fResource->addPendingWrite(); |
141 break; | 128 break; |
142 } | 129 } |
143 } | 130 } |
144 } | 131 } |
145 | 132 |
146 ~GrPendingIOResource() { | 133 ~GrPendingIOResource() { |
147 if (NULL != fResource) { | 134 if (NULL != fResource) { |
148 switch (fIOType) { | 135 switch (IO_TYPE) { |
149 case GrGpuResourceRef::kNone_IOType: | 136 case GrIORef::kRead_IOType: |
150 SkFAIL("GrPendingIOResource with neither reads nor writes?") ; | |
151 break; | |
152 case GrGpuResourceRef::kRead_IOType: | |
153 fResource->completedRead(); | 137 fResource->completedRead(); |
154 break; | 138 break; |
155 case GrGpuResourceRef::kWrite_IOType: | 139 case GrIORef::kWrite_IOType: |
156 fResource->completedWrite(); | 140 fResource->completedWrite(); |
157 break; | 141 break; |
158 case GrGpuResourceRef::kRW_IOType: | 142 case GrIORef::kRW_IOType: |
159 fResource->completedRead(); | 143 fResource->completedRead(); |
160 fResource->completedWrite(); | 144 fResource->completedWrite(); |
161 break; | 145 break; |
162 } | 146 } |
163 } | 147 } |
164 } | 148 } |
165 | 149 |
166 T* get() const { return fResource; } | 150 T* get() const { return fResource; } |
167 | 151 |
168 private: | 152 private: |
169 IOType fIOType; | |
170 T* fResource; | 153 T* fResource; |
171 }; | 154 }; |
172 #endif | 155 #endif |
OLD | NEW |