[safe browsing] Push missing download BINHASH histograms.
[chromium-blink-merge.git] / cc / layers / layer_iterator.h
blobf929eb69bd0e87fd066583738a0cd5c9a49f355f
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
3 // found in the LICENSE file.
5 #ifndef CC_LAYERS_LAYER_ITERATOR_H_
6 #define CC_LAYERS_LAYER_ITERATOR_H_
8 #include "cc/base/cc_export.h"
9 #include "cc/trees/layer_tree_host_common.h"
11 namespace cc {
13 // These classes provide means to iterate over the
14 // RenderSurface-Layer tree.
16 // Example code follows, for a tree of Layer/RenderSurface objects.
17 // See below for details.
19 // void DoStuffOnLayers(
20 // const RenderSurfaceLayerList& render_surface_layer_list) {
21 // typedef LayerIterator<Layer> LayerIteratorType;
23 // LayerIteratorType end =
24 // LayerIteratorType::End(&render_surface_layer_list);
25 // for (LayerIteratorType
26 // it = LayerIteratorType::Begin(&render_surface_layer_list);
27 // it != end;
28 // ++it) {
29 // // Only one of these will be true
30 // if (it.represents_target_render_surface())
31 // foo(*it); // *it is a layer representing a target RenderSurface
32 // if (it.represents_contributing_render_surface())
33 // bar(*it); // *it is a layer representing a RenderSurface that
34 // // contributes to the layer's target RenderSurface
35 // if (it.represents_itself())
36 // baz(*it); // *it is a layer representing itself,
37 // // as it contributes to its own target RenderSurface
38 // }
39 // }
41 // A RenderSurface R may be referred to in one of two different contexts.
42 // One RenderSurface is "current" at any time, for whatever operation
43 // is being performed. This current surface is referred to as a target surface.
44 // For example, when R is being painted it would be the target surface.
45 // Once R has been painted, its contents may be included into another
46 // surface S. While S is considered the target surface when it is being
47 // painted, R is called a contributing surface in this context as it
48 // contributes to the content of the target surface S.
50 // The iterator's current position in the tree always points to some layer.
51 // The state of the iterator indicates the role of the layer,
52 // and will be one of the following three states.
53 // A single layer L will appear in the iteration process in at least one,
54 // and possibly all, of these states.
55 // 1. Representing the target surface: The iterator in this state,
56 // pointing at layer L, indicates that the target RenderSurface
57 // is now the surface owned by L. This will occur exactly once for each
58 // RenderSurface in the tree.
59 // 2. Representing a contributing surface: The iterator in this state,
60 // pointing at layer L, refers to the RenderSurface owned
61 // by L as a contributing surface, without changing the current
62 // target RenderSurface.
63 // 3. Representing itself: The iterator in this state, pointing at layer L,
64 // refers to the layer itself, as a child of the
65 // current target RenderSurface.
67 // The FrontToBack iterator will iterate over children layers of a surface
68 // before the layer representing the surface as a target surface.
70 // To use the iterators:
72 // Create a stepping iterator and end iterator by calling
73 // LayerIterator::Begin() and LayerIterator::End() and passing in the
74 // list of layers owning target RenderSurfaces. Step through the tree
75 // by incrementing the stepping iterator while it is != to
76 // the end iterator. At each step the iterator knows what the layer
77 // is representing, and you can query the iterator to decide
78 // what actions to perform with the layer given what it represents.
80 ////////////////////////////////////////////////////////////////////////////////
82 // Non-templated constants
83 struct LayerIteratorValue {
84 static const int kInvalidTargetRenderSurfaceLayerIndex = -1;
85 // This must be -1 since the iterator action code assumes that this value can
86 // be reached by subtracting one from the position of the first layer in the
87 // current target surface's child layer list, which is 0.
88 static const int kLayerIndexRepresentingTargetRenderSurface = -1;
91 // The position of a layer iterator that is independent
92 // of its many template types.
93 template <typename LayerType> struct LayerIteratorPosition {
94 bool represents_target_render_surface;
95 bool represents_contributing_render_surface;
96 bool represents_itself;
97 LayerType* target_render_surface_layer;
98 LayerType* current_layer;
101 // An iterator class for walking over layers in the
102 // RenderSurface-Layer tree.
103 template <typename LayerType>
104 class LayerIterator {
105 typedef LayerIterator<LayerType> LayerIteratorType;
106 typedef typename LayerType::RenderSurfaceListType LayerList;
107 typedef typename LayerType::RenderSurfaceType RenderSurfaceType;
109 public:
110 LayerIterator() : render_surface_layer_list_(NULL) {}
112 static LayerIteratorType Begin(const LayerList* render_surface_layer_list) {
113 return LayerIteratorType(render_surface_layer_list, true);
115 static LayerIteratorType End(const LayerList* render_surface_layer_list) {
116 return LayerIteratorType(render_surface_layer_list, false);
119 LayerIteratorType& operator++() {
120 MoveToNext();
121 return *this;
123 bool operator==(const LayerIterator& other) const {
124 return target_render_surface_layer_index_ ==
125 other.target_render_surface_layer_index_ &&
126 current_layer_index_ == other.current_layer_index_;
128 bool operator!=(const LayerIteratorType& other) const {
129 return !(*this == other);
132 LayerType* operator->() const { return current_layer(); }
133 LayerType* operator*() const { return current_layer(); }
135 bool represents_target_render_surface() const {
136 return current_layer_represents_target_render_surface();
138 bool represents_contributing_render_surface() const {
139 return !represents_target_render_surface() &&
140 current_layer_represents_contributing_render_surface();
142 bool represents_itself() const {
143 return !represents_target_render_surface() &&
144 !represents_contributing_render_surface();
147 LayerType* target_render_surface_layer() const {
148 return render_surface_layer_list_->at(target_render_surface_layer_index_);
151 operator const LayerIteratorPosition<LayerType>() const {
152 LayerIteratorPosition<LayerType> position;
153 position.represents_target_render_surface =
154 represents_target_render_surface();
155 position.represents_contributing_render_surface =
156 represents_contributing_render_surface();
157 position.represents_itself = represents_itself();
158 position.target_render_surface_layer = target_render_surface_layer();
159 position.current_layer = current_layer();
160 return position;
163 private:
164 LayerIterator(const LayerList* render_surface_layer_list, bool start)
165 : render_surface_layer_list_(render_surface_layer_list),
166 target_render_surface_layer_index_(0) {
167 for (size_t i = 0; i < render_surface_layer_list->size(); ++i) {
168 if (!render_surface_layer_list->at(i)->render_surface()) {
169 NOTREACHED();
170 MoveToEnd();
171 return;
175 if (start && !render_surface_layer_list->empty())
176 MoveToBegin();
177 else
178 MoveToEnd();
181 void MoveToBegin() {
182 target_render_surface_layer_index_ = 0;
183 current_layer_index_ = target_render_surface_children().size() - 1;
184 MoveToHighestInSubtree();
187 void MoveToEnd() {
188 target_render_surface_layer_index_ =
189 LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex;
190 current_layer_index_ = 0;
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.
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;
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_;
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_;
236 for (LayerType* layer = current_layer();
237 target_render_surface_layer() != layer;
238 ++target_render_surface_layer_index_) {
240 current_layer_index_ = target_render_surface_children().size() - 1;
242 target_render_surface()->target_render_surface_layer_index_history_ =
243 previous_target_render_surface_layer;
247 inline LayerType* current_layer() const {
248 return current_layer_represents_target_render_surface()
249 ? target_render_surface_layer()
250 : target_render_surface_children().at(current_layer_index_);
253 inline bool current_layer_represents_contributing_render_surface() const {
254 return LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerType>(
255 current_layer(), target_render_surface_layer()->id());
257 inline bool current_layer_represents_target_render_surface() const {
258 return current_layer_index_ ==
259 LayerIteratorValue::kLayerIndexRepresentingTargetRenderSurface;
262 inline RenderSurfaceType* target_render_surface() const {
263 return target_render_surface_layer()->render_surface();
265 inline const LayerList& target_render_surface_children() const {
266 return target_render_surface()->layer_list();
269 const LayerList* render_surface_layer_list_;
271 // The iterator's current position.
273 // A position in the render_surface_layer_list. This points to a layer which
274 // owns the current target surface. This is a value from 0 to n-1
275 // (n = size of render_surface_layer_list = number of surfaces).
276 // A value outside of this range
277 // (for example, LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex)
278 // is used to indicate a position outside the bounds of the tree.
279 int target_render_surface_layer_index_;
280 // A position in the list of layers that are children of the
281 // current target surface. When pointing to one of these layers,
282 // this is a value from 0 to n-1 (n = number of children).
283 // Since the iterator must also stop at the layers representing
284 // the target surface, this is done by setting the current_layerIndex
285 // to a value of LayerIteratorValue::LayerRepresentingTargetRenderSurface.
286 int current_layer_index_;
289 } // namespace cc
291 #endif // CC_LAYERS_LAYER_ITERATOR_H_