| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // The position of a layer iterator that is independent | 91 // The position of a layer iterator that is independent |
| 97 // of its many template types. | 92 // of its many template types. |
| 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 |
| 101 struct LayerIteratorPadding { |
| 102 // This struct has nothing in it! It exists to make the LayerIterator a bit |
| 103 // larger for debugging some weird behaviour when this CL previously landed. |
| 104 // See crbug.com/345757 for more details. |
| 105 void Foo() {} |
| 106 }; |
| 107 |
| 106 // An iterator class for walking over layers in the | 108 // An iterator class for walking over layers in the |
| 107 // RenderSurface-Layer tree. | 109 // RenderSurface-Layer tree. |
| 108 template <typename LayerType, | 110 template <typename LayerType> |
| 109 typename LayerList, | |
| 110 typename RenderSurfaceType, | |
| 111 typename IteratorActionType> | |
| 112 class LayerIterator { | 111 class LayerIterator { |
| 113 typedef LayerIterator<LayerType, | 112 typedef LayerIterator<LayerType> LayerIteratorType; |
| 114 LayerList, | 113 typedef typename LayerType::RenderSurfaceListType LayerList; |
| 115 RenderSurfaceType, | 114 typedef typename LayerType::RenderSurfaceType RenderSurfaceType; |
| 116 IteratorActionType> LayerIteratorType; | |
| 117 | 115 |
| 118 public: | 116 public: |
| 119 LayerIterator() : render_surface_layer_list_(NULL) {} | 117 LayerIterator() : render_surface_layer_list_(NULL) {} |
| 120 | 118 |
| 121 static LayerIteratorType Begin(const LayerList* render_surface_layer_list) { | 119 static LayerIteratorType Begin(const LayerList* render_surface_layer_list) { |
| 122 return LayerIteratorType(render_surface_layer_list, true); | 120 return LayerIteratorType(render_surface_layer_list, true); |
| 123 } | 121 } |
| 124 static LayerIteratorType End(const LayerList* render_surface_layer_list) { | 122 static LayerIteratorType End(const LayerList* render_surface_layer_list) { |
| 125 return LayerIteratorType(render_surface_layer_list, false); | 123 return LayerIteratorType(render_surface_layer_list, false); |
| 126 } | 124 } |
| 127 | 125 |
| 128 LayerIteratorType& operator++() { | 126 LayerIteratorType& operator++() { |
| 129 actions_.Next(this); | 127 MoveToNext(); |
| 130 return *this; | 128 return *this; |
| 131 } | 129 } |
| 132 bool operator==(const LayerIterator& other) const { | 130 bool operator==(const LayerIterator& other) const { |
| 133 return target_render_surface_layer_index_ == | 131 return target_render_surface_layer_index_ == |
| 134 other.target_render_surface_layer_index_ && | 132 other.target_render_surface_layer_index_ && |
| 135 current_layer_index_ == other.current_layer_index_; | 133 current_layer_index_ == other.current_layer_index_; |
| 136 } | 134 } |
| 137 bool operator!=(const LayerIteratorType& other) const { | 135 bool operator!=(const LayerIteratorType& other) const { |
| 138 return !(*this == other); | 136 return !(*this == other); |
| 139 } | 137 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 169 return position; | 167 return position; |
| 170 } | 168 } |
| 171 | 169 |
| 172 private: | 170 private: |
| 173 LayerIterator(const LayerList* render_surface_layer_list, bool start) | 171 LayerIterator(const LayerList* render_surface_layer_list, bool start) |
| 174 : render_surface_layer_list_(render_surface_layer_list), | 172 : render_surface_layer_list_(render_surface_layer_list), |
| 175 target_render_surface_layer_index_(0) { | 173 target_render_surface_layer_index_(0) { |
| 176 for (size_t i = 0; i < render_surface_layer_list->size(); ++i) { | 174 for (size_t i = 0; i < render_surface_layer_list->size(); ++i) { |
| 177 if (!render_surface_layer_list->at(i)->render_surface()) { | 175 if (!render_surface_layer_list->at(i)->render_surface()) { |
| 178 NOTREACHED(); | 176 NOTREACHED(); |
| 179 actions_.End(this); | 177 MoveToEnd(); |
| 180 return; | 178 return; |
| 181 } | 179 } |
| 182 } | 180 } |
| 183 | 181 |
| 184 if (start && !render_surface_layer_list->empty()) | 182 if (start && !render_surface_layer_list->empty()) |
| 185 actions_.Begin(this); | 183 MoveToBegin(); |
| 186 else | 184 else |
| 187 actions_.End(this); | 185 MoveToEnd(); |
| 186 } |
| 187 |
| 188 void MoveToBegin() { |
| 189 target_render_surface_layer_index_ = 0; |
| 190 current_layer_index_ = target_render_surface_children().size() - 1; |
| 191 MoveToHighestInSubtree(); |
| 192 } |
| 193 |
| 194 void MoveToEnd() { |
| 195 target_render_surface_layer_index_ = |
| 196 LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex; |
| 197 current_layer_index_ = 0; |
| 198 } |
| 199 |
| 200 void MoveToNext() { |
| 201 // Moves to the previous layer in the current RS layer list. |
| 202 // Then we check if the new current layer has its own RS, |
| 203 // in which case there are things in that RS layer list that are higher, |
| 204 // so we find the highest layer in that subtree. |
| 205 // If we move back past the front of the list, |
| 206 // we jump up to the previous RS layer list, picking up again where we |
| 207 // had previously recursed into the current RS layer list. |
| 208 |
| 209 if (!current_layer_represents_target_render_surface()) { |
| 210 // Subtracting one here will eventually cause the current layer |
| 211 // to become that layer representing the target render surface. |
| 212 --current_layer_index_; |
| 213 MoveToHighestInSubtree(); |
| 214 } else { |
| 215 while (current_layer_represents_target_render_surface()) { |
| 216 if (!target_render_surface_layer_index_) { |
| 217 // End of the list. |
| 218 target_render_surface_layer_index_ = |
| 219 LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex; |
| 220 current_layer_index_ = 0; |
| 221 return; |
| 222 } |
| 223 target_render_surface_layer_index_ = |
| 224 target_render_surface()->target_render_surface_layer_index_history_; |
| 225 current_layer_index_ = |
| 226 target_render_surface()->current_layer_index_history_; |
| 227 } |
| 228 } |
| 229 } |
| 230 |
| 231 void MoveToHighestInSubtree() { |
| 232 if (current_layer_represents_target_render_surface()) |
| 233 return; |
| 234 while (current_layer_represents_contributing_render_surface()) { |
| 235 // Save where we were in the current target surface, move to the next one, |
| 236 // and save the target surface that we came from there |
| 237 // so we can go back to it. |
| 238 target_render_surface()->current_layer_index_history_ = |
| 239 current_layer_index_; |
| 240 int previous_target_render_surface_layer = |
| 241 target_render_surface_layer_index_; |
| 242 |
| 243 for (LayerType* layer = current_layer(); |
| 244 target_render_surface_layer() != layer; |
| 245 ++target_render_surface_layer_index_) { |
| 246 } |
| 247 current_layer_index_ = target_render_surface_children().size() - 1; |
| 248 |
| 249 target_render_surface()->target_render_surface_layer_index_history_ = |
| 250 previous_target_render_surface_layer; |
| 251 } |
| 188 } | 252 } |
| 189 | 253 |
| 190 inline LayerType* current_layer() const { | 254 inline LayerType* current_layer() const { |
| 191 return current_layer_represents_target_render_surface() | 255 return current_layer_represents_target_render_surface() |
| 192 ? target_render_surface_layer() | 256 ? target_render_surface_layer() |
| 193 : target_render_surface_children().at(current_layer_index_); | 257 : target_render_surface_children().at(current_layer_index_); |
| 194 } | 258 } |
| 195 | 259 |
| 196 inline bool current_layer_represents_contributing_render_surface() const { | 260 inline bool current_layer_represents_contributing_render_surface() const { |
| 197 return LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerType>( | 261 return LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerType>( |
| 198 current_layer(), target_render_surface_layer()->id()); | 262 current_layer(), target_render_surface_layer()->id()); |
| 199 } | 263 } |
| 200 inline bool current_layer_represents_target_render_surface() const { | 264 inline bool current_layer_represents_target_render_surface() const { |
| 201 return current_layer_index_ == | 265 return current_layer_index_ == |
| 202 LayerIteratorValue::kLayerIndexRepresentingTargetRenderSurface; | 266 LayerIteratorValue::kLayerIndexRepresentingTargetRenderSurface; |
| 203 } | 267 } |
| 204 | 268 |
| 205 inline RenderSurfaceType* target_render_surface() const { | 269 inline RenderSurfaceType* target_render_surface() const { |
| 206 return target_render_surface_layer()->render_surface(); | 270 return target_render_surface_layer()->render_surface(); |
| 207 } | 271 } |
| 208 inline const LayerList& target_render_surface_children() const { | 272 inline const LayerList& target_render_surface_children() const { |
| 209 return target_render_surface()->layer_list(); | 273 return target_render_surface()->layer_list(); |
| 210 } | 274 } |
| 211 | 275 |
| 212 IteratorActionType actions_; | 276 LayerIteratorPadding padding_; |
| 213 const LayerList* render_surface_layer_list_; | 277 const LayerList* render_surface_layer_list_; |
| 214 | 278 |
| 215 // The iterator's current position. | 279 // The iterator's current position. |
| 216 | 280 |
| 217 // A position in the render_surface_layer_list. This points to a layer which | 281 // 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 | 282 // 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). | 283 // (n = size of render_surface_layer_list = number of surfaces). |
| 220 // A value outside of this range | 284 // A value outside of this range |
| 221 // (for example, LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex) | 285 // (for example, LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex) |
| 222 // is used to indicate a position outside the bounds of the tree. | 286 // is used to indicate a position outside the bounds of the tree. |
| 223 int target_render_surface_layer_index_; | 287 int target_render_surface_layer_index_; |
| 224 // A position in the list of layers that are children of the | 288 // A position in the list of layers that are children of the |
| 225 // current target surface. When pointing to one of these layers, | 289 // current target surface. When pointing to one of these layers, |
| 226 // this is a value from 0 to n-1 (n = number of children). | 290 // this is a value from 0 to n-1 (n = number of children). |
| 227 // Since the iterator must also stop at the layers representing | 291 // Since the iterator must also stop at the layers representing |
| 228 // the target surface, this is done by setting the current_layerIndex | 292 // the target surface, this is done by setting the current_layerIndex |
| 229 // to a value of LayerIteratorValue::LayerRepresentingTargetRenderSurface. | 293 // to a value of LayerIteratorValue::LayerRepresentingTargetRenderSurface. |
| 230 int current_layer_index_; | 294 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 }; | 295 }; |
| 270 | 296 |
| 271 } // namespace cc | 297 } // namespace cc |
| 272 | 298 |
| 273 #endif // CC_LAYERS_LAYER_ITERATOR_H_ | 299 #endif // CC_LAYERS_LAYER_ITERATOR_H_ |
| OLD | NEW |