OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_LAYERS_LAYER_ITERATOR_H_ | 5 #ifndef CC_LAYERS_LAYER_ITERATOR_H_ |
6 #define CC_LAYERS_LAYER_ITERATOR_H_ | 6 #define CC_LAYERS_LAYER_ITERATOR_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | |
9 #include "cc/base/cc_export.h" | 8 #include "cc/base/cc_export.h" |
10 #include "cc/trees/layer_tree_host_common.h" | 9 #include "cc/trees/layer_tree_host_common.h" |
11 | 10 |
12 namespace cc { | 11 namespace cc { |
13 | 12 |
14 // These classes provide means to iterate over the | 13 // These classes provide means to iterate over the |
15 // RenderSurface-Layer tree. | 14 // RenderSurface-Layer tree. |
16 | 15 |
17 // Example code follows, for a tree of Layer/RenderSurface objects. | 16 // Example code follows, for a tree of Layer/RenderSurface objects. |
18 // See below for details. | 17 // See below for details. |
19 // | 18 // |
20 // void DoStuffOnLayers( | 19 // void DoStuffOnLayers( |
21 // const RenderSurfaceLayerList& render_surface_layer_list) { | 20 // const RenderSurfaceLayerList& render_surface_layer_list) { |
22 // typedef LayerIterator<Layer, | 21 // typedef LayerIterator<Layer> LayerIteratorType; |
23 // RenderSurfaceLayerList, | |
24 // RenderSurface, | |
25 // LayerIteratorActions::FrontToBack> | |
26 // LayerIteratorType; | |
27 // | 22 // |
28 // LayerIteratorType end = | 23 // LayerIteratorType end = |
29 // LayerIteratorType::End(&render_surface_layer_list); | 24 // LayerIteratorType::End(&render_surface_layer_list); |
30 // for (LayerIteratorType | 25 // for (LayerIteratorType |
31 // it = LayerIteratorType::Begin(&render_surface_layer_list); | 26 // it = LayerIteratorType::Begin(&render_surface_layer_list); |
32 // it != end; | 27 // it != end; |
33 // ++it) { | 28 // ++it) { |
34 // // Only one of these will be true | 29 // // Only one of these will be true |
35 // if (it.represents_target_render_surface()) | 30 // if (it.represents_target_render_surface()) |
36 // foo(*it); // *it is a layer representing a target RenderSurface | 31 // foo(*it); // *it is a layer representing a target RenderSurface |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 template <typename LayerType> struct LayerIteratorPosition { | 93 template <typename LayerType> struct LayerIteratorPosition { |
99 bool represents_target_render_surface; | 94 bool represents_target_render_surface; |
100 bool represents_contributing_render_surface; | 95 bool represents_contributing_render_surface; |
101 bool represents_itself; | 96 bool represents_itself; |
102 LayerType* target_render_surface_layer; | 97 LayerType* target_render_surface_layer; |
103 LayerType* current_layer; | 98 LayerType* current_layer; |
104 }; | 99 }; |
105 | 100 |
106 // An iterator class for walking over layers in the | 101 // An iterator class for walking over layers in the |
107 // RenderSurface-Layer tree. | 102 // RenderSurface-Layer tree. |
108 template <typename LayerType, | 103 template <typename LayerType> |
109 typename LayerList, | |
110 typename RenderSurfaceType, | |
111 typename IteratorActionType> | |
112 class LayerIterator { | 104 class LayerIterator { |
113 typedef LayerIterator<LayerType, | 105 typedef LayerIterator<LayerType> LayerIteratorType; |
114 LayerList, | 106 typedef typename LayerType::RenderSurfaceListType LayerList; |
115 RenderSurfaceType, | 107 typedef typename LayerType::RenderSurfaceType RenderSurfaceType; |
116 IteratorActionType> LayerIteratorType; | |
117 | 108 |
118 public: | 109 public: |
119 LayerIterator() : render_surface_layer_list_(NULL) {} | 110 LayerIterator() : render_surface_layer_list_(NULL) {} |
120 | 111 |
121 static LayerIteratorType Begin(const LayerList* render_surface_layer_list) { | 112 static LayerIteratorType Begin(const LayerList* render_surface_layer_list) { |
122 return LayerIteratorType(render_surface_layer_list, true); | 113 return LayerIteratorType(render_surface_layer_list, true); |
123 } | 114 } |
124 static LayerIteratorType End(const LayerList* render_surface_layer_list) { | 115 static LayerIteratorType End(const LayerList* render_surface_layer_list) { |
125 return LayerIteratorType(render_surface_layer_list, false); | 116 return LayerIteratorType(render_surface_layer_list, false); |
126 } | 117 } |
127 | 118 |
128 LayerIteratorType& operator++() { | 119 LayerIteratorType& operator++() { |
129 actions_.Next(this); | 120 MoveToNext(); |
130 return *this; | 121 return *this; |
131 } | 122 } |
132 bool operator==(const LayerIterator& other) const { | 123 bool operator==(const LayerIterator& other) const { |
133 return target_render_surface_layer_index_ == | 124 return target_render_surface_layer_index_ == |
134 other.target_render_surface_layer_index_ && | 125 other.target_render_surface_layer_index_ && |
135 current_layer_index_ == other.current_layer_index_; | 126 current_layer_index_ == other.current_layer_index_; |
136 } | 127 } |
137 bool operator!=(const LayerIteratorType& other) const { | 128 bool operator!=(const LayerIteratorType& other) const { |
138 return !(*this == other); | 129 return !(*this == other); |
139 } | 130 } |
(...skipping 29 matching lines...) Expand all Loading... |
169 return position; | 160 return position; |
170 } | 161 } |
171 | 162 |
172 private: | 163 private: |
173 LayerIterator(const LayerList* render_surface_layer_list, bool start) | 164 LayerIterator(const LayerList* render_surface_layer_list, bool start) |
174 : render_surface_layer_list_(render_surface_layer_list), | 165 : render_surface_layer_list_(render_surface_layer_list), |
175 target_render_surface_layer_index_(0) { | 166 target_render_surface_layer_index_(0) { |
176 for (size_t i = 0; i < render_surface_layer_list->size(); ++i) { | 167 for (size_t i = 0; i < render_surface_layer_list->size(); ++i) { |
177 if (!render_surface_layer_list->at(i)->render_surface()) { | 168 if (!render_surface_layer_list->at(i)->render_surface()) { |
178 NOTREACHED(); | 169 NOTREACHED(); |
179 actions_.End(this); | 170 MoveToEnd(); |
180 return; | 171 return; |
181 } | 172 } |
182 } | 173 } |
183 | 174 |
184 if (start && !render_surface_layer_list->empty()) | 175 if (start && !render_surface_layer_list->empty()) |
185 actions_.Begin(this); | 176 MoveToBegin(); |
186 else | 177 else |
187 actions_.End(this); | 178 MoveToEnd(); |
| 179 } |
| 180 |
| 181 void MoveToBegin() { |
| 182 target_render_surface_layer_index_ = 0; |
| 183 current_layer_index_ = target_render_surface_children().size() - 1; |
| 184 MoveToHighestInSubtree(); |
| 185 } |
| 186 |
| 187 void MoveToEnd() { |
| 188 target_render_surface_layer_index_ = |
| 189 LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex; |
| 190 current_layer_index_ = 0; |
| 191 } |
| 192 |
| 193 void MoveToNext() { |
| 194 // Moves to the previous layer in the current RS layer list. |
| 195 // Then we check if the new current layer has its own RS, |
| 196 // in which case there are things in that RS layer list that are higher, |
| 197 // so we find the highest layer in that subtree. |
| 198 // If we move back past the front of the list, |
| 199 // we jump up to the previous RS layer list, picking up again where we |
| 200 // had previously recursed into the current RS layer list. |
| 201 |
| 202 if (!current_layer_represents_target_render_surface()) { |
| 203 // Subtracting one here will eventually cause the current layer |
| 204 // to become that layer representing the target render surface. |
| 205 --current_layer_index_; |
| 206 MoveToHighestInSubtree(); |
| 207 } else { |
| 208 while (current_layer_represents_target_render_surface()) { |
| 209 if (!target_render_surface_layer_index_) { |
| 210 // End of the list. |
| 211 target_render_surface_layer_index_ = |
| 212 LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex; |
| 213 current_layer_index_ = 0; |
| 214 return; |
| 215 } |
| 216 target_render_surface_layer_index_ = |
| 217 target_render_surface()->target_render_surface_layer_index_history_; |
| 218 current_layer_index_ = |
| 219 target_render_surface()->current_layer_index_history_; |
| 220 } |
| 221 } |
| 222 } |
| 223 |
| 224 void MoveToHighestInSubtree() { |
| 225 if (current_layer_represents_target_render_surface()) |
| 226 return; |
| 227 while (current_layer_represents_contributing_render_surface()) { |
| 228 // Save where we were in the current target surface, move to the next one, |
| 229 // and save the target surface that we came from there |
| 230 // so we can go back to it. |
| 231 target_render_surface()->current_layer_index_history_ = |
| 232 current_layer_index_; |
| 233 int previous_target_render_surface_layer = |
| 234 target_render_surface_layer_index_; |
| 235 |
| 236 for (LayerType* layer = current_layer(); |
| 237 target_render_surface_layer() != layer; |
| 238 ++target_render_surface_layer_index_) { |
| 239 } |
| 240 current_layer_index_ = target_render_surface_children().size() - 1; |
| 241 |
| 242 target_render_surface()->target_render_surface_layer_index_history_ = |
| 243 previous_target_render_surface_layer; |
| 244 } |
188 } | 245 } |
189 | 246 |
190 inline LayerType* current_layer() const { | 247 inline LayerType* current_layer() const { |
191 return current_layer_represents_target_render_surface() | 248 return current_layer_represents_target_render_surface() |
192 ? target_render_surface_layer() | 249 ? target_render_surface_layer() |
193 : target_render_surface_children().at(current_layer_index_); | 250 : target_render_surface_children().at(current_layer_index_); |
194 } | 251 } |
195 | 252 |
196 inline bool current_layer_represents_contributing_render_surface() const { | 253 inline bool current_layer_represents_contributing_render_surface() const { |
197 return LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerType>( | 254 return LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerType>( |
198 current_layer(), target_render_surface_layer()->id()); | 255 current_layer(), target_render_surface_layer()->id()); |
199 } | 256 } |
200 inline bool current_layer_represents_target_render_surface() const { | 257 inline bool current_layer_represents_target_render_surface() const { |
201 return current_layer_index_ == | 258 return current_layer_index_ == |
202 LayerIteratorValue::kLayerIndexRepresentingTargetRenderSurface; | 259 LayerIteratorValue::kLayerIndexRepresentingTargetRenderSurface; |
203 } | 260 } |
204 | 261 |
205 inline RenderSurfaceType* target_render_surface() const { | 262 inline RenderSurfaceType* target_render_surface() const { |
206 return target_render_surface_layer()->render_surface(); | 263 return target_render_surface_layer()->render_surface(); |
207 } | 264 } |
208 inline const LayerList& target_render_surface_children() const { | 265 inline const LayerList& target_render_surface_children() const { |
209 return target_render_surface()->layer_list(); | 266 return target_render_surface()->layer_list(); |
210 } | 267 } |
211 | 268 |
212 IteratorActionType actions_; | |
213 const LayerList* render_surface_layer_list_; | 269 const LayerList* render_surface_layer_list_; |
214 | 270 |
215 // The iterator's current position. | 271 // The iterator's current position. |
216 | 272 |
217 // A position in the render_surface_layer_list. This points to a layer which | 273 // A position in the render_surface_layer_list. This points to a layer which |
218 // owns the current target surface. This is a value from 0 to n-1 | 274 // owns the current target surface. This is a value from 0 to n-1 |
219 // (n = size of render_surface_layer_list = number of surfaces). | 275 // (n = size of render_surface_layer_list = number of surfaces). |
220 // A value outside of this range | 276 // A value outside of this range |
221 // (for example, LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex) | 277 // (for example, LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex) |
222 // is used to indicate a position outside the bounds of the tree. | 278 // is used to indicate a position outside the bounds of the tree. |
223 int target_render_surface_layer_index_; | 279 int target_render_surface_layer_index_; |
224 // A position in the list of layers that are children of the | 280 // A position in the list of layers that are children of the |
225 // current target surface. When pointing to one of these layers, | 281 // current target surface. When pointing to one of these layers, |
226 // this is a value from 0 to n-1 (n = number of children). | 282 // this is a value from 0 to n-1 (n = number of children). |
227 // Since the iterator must also stop at the layers representing | 283 // Since the iterator must also stop at the layers representing |
228 // the target surface, this is done by setting the current_layerIndex | 284 // the target surface, this is done by setting the current_layerIndex |
229 // to a value of LayerIteratorValue::LayerRepresentingTargetRenderSurface. | 285 // to a value of LayerIteratorValue::LayerRepresentingTargetRenderSurface. |
230 int current_layer_index_; | 286 int current_layer_index_; |
231 | |
232 friend struct LayerIteratorActions; | |
233 }; | |
234 | |
235 // Orderings for iterating over the RenderSurface-Layer tree. | |
236 struct CC_EXPORT LayerIteratorActions { | |
237 // Walks layers sorted by z-order from front to back | |
238 class CC_EXPORT FrontToBack { | |
239 public: | |
240 template <typename LayerType, | |
241 typename LayerList, | |
242 typename RenderSurfaceType, | |
243 typename ActionType> | |
244 void Begin( | |
245 LayerIterator<LayerType, LayerList, RenderSurfaceType, ActionType>* it); | |
246 | |
247 template <typename LayerType, | |
248 typename LayerList, | |
249 typename RenderSurfaceType, | |
250 typename ActionType> | |
251 void End( | |
252 LayerIterator<LayerType, LayerList, RenderSurfaceType, ActionType>* it); | |
253 | |
254 template <typename LayerType, | |
255 typename LayerList, | |
256 typename RenderSurfaceType, | |
257 typename ActionType> | |
258 void Next( | |
259 LayerIterator<LayerType, LayerList, RenderSurfaceType, ActionType>* it); | |
260 | |
261 private: | |
262 template <typename LayerType, | |
263 typename LayerList, | |
264 typename RenderSurfaceType, | |
265 typename ActionType> | |
266 void GoToHighestInSubtree( | |
267 LayerIterator<LayerType, LayerList, RenderSurfaceType, ActionType>* it); | |
268 }; | |
269 }; | 287 }; |
270 | 288 |
271 } // namespace cc | 289 } // namespace cc |
272 | 290 |
273 #endif // CC_LAYERS_LAYER_ITERATOR_H_ | 291 #endif // CC_LAYERS_LAYER_ITERATOR_H_ |
OLD | NEW |