Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / layers / layer_iterator.h
blob2c129e48cb322843ae500197f683d993e487d944
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/layers/layer_impl.h"
10 #include "cc/trees/layer_tree_host_common.h"
12 namespace cc {
14 // These classes provide means to iterate over the
15 // RenderSurfaceImpl-LayerImpl tree.
17 // Example code follows, for a tree of LayerImpl/RenderSurfaceImpl objects.
18 // See below for details.
20 // void DoStuffOnLayers(
21 // const LayerImplList& render_surface_layer_list) {
23 // LayerIterator end =
24 // LayerIterator::End(&render_surface_layer_list);
25 // for (LayerIterator
26 // it = LayerIterator::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 struct LayerIteratorValue {
83 static const int kInvalidTargetRenderSurfaceLayerIndex = -1;
84 // This must be (size_t)-1 since the iterator action code assumes that this
85 // value can be reached by subtracting one from the position of the first
86 // layer in the current target surface's child layer list, which is 0.
87 static const size_t kLayerIndexRepresentingTargetRenderSurface =
88 static_cast<size_t>(-1);
91 // The position of a layer iterator that is independent
92 // of its many template types.
93 struct LayerIteratorPosition {
94 bool represents_target_render_surface;
95 bool represents_contributing_render_surface;
96 bool represents_itself;
97 LayerImpl* target_render_surface_layer;
98 LayerImpl* current_layer;
101 // An iterator class for walking over layers in the
102 // RenderSurface-Layer tree.
103 // TODO(enne): This class probably shouldn't be entirely inline and
104 // should get moved to a .cc file where it makes sense.
105 class LayerIterator {
106 public:
107 LayerIterator() : render_surface_layer_list_(nullptr) {}
109 static LayerIterator Begin(const LayerImplList* render_surface_layer_list) {
110 return LayerIterator(render_surface_layer_list, true);
112 static LayerIterator End(const LayerImplList* render_surface_layer_list) {
113 return LayerIterator(render_surface_layer_list, false);
116 LayerIterator& operator++() {
117 MoveToNext();
118 return *this;
120 bool operator==(const LayerIterator& other) const {
121 return target_render_surface_layer_index_ ==
122 other.target_render_surface_layer_index_ &&
123 current_layer_index_ == other.current_layer_index_;
125 bool operator!=(const LayerIterator& other) const {
126 return !(*this == other);
129 LayerImpl* operator->() const { return current_layer(); }
130 LayerImpl* operator*() const { return current_layer(); }
132 bool represents_target_render_surface() const {
133 return current_layer_represents_target_render_surface();
135 bool represents_contributing_render_surface() const {
136 return !represents_target_render_surface() &&
137 current_layer_represents_contributing_render_surface();
139 bool represents_itself() const {
140 return !represents_target_render_surface() &&
141 !represents_contributing_render_surface();
144 LayerImpl* target_render_surface_layer() const {
145 return render_surface_layer_list_->at(target_render_surface_layer_index_);
148 operator const LayerIteratorPosition() const {
149 LayerIteratorPosition position;
150 position.represents_target_render_surface =
151 represents_target_render_surface();
152 position.represents_contributing_render_surface =
153 represents_contributing_render_surface();
154 position.represents_itself = represents_itself();
155 position.target_render_surface_layer = target_render_surface_layer();
156 position.current_layer = current_layer();
157 return position;
160 private:
161 LayerIterator(const LayerImplList* render_surface_layer_list, bool start)
162 : render_surface_layer_list_(render_surface_layer_list),
163 target_render_surface_layer_index_(0) {
164 for (size_t i = 0; i < render_surface_layer_list->size(); ++i) {
165 if (!render_surface_layer_list->at(i)->render_surface()) {
166 NOTREACHED();
167 MoveToEnd();
168 return;
172 if (start && !render_surface_layer_list->empty())
173 MoveToBegin();
174 else
175 MoveToEnd();
178 void MoveToBegin() {
179 target_render_surface_layer_index_ = 0;
180 current_layer_index_ = target_render_surface_children().size() - 1;
181 MoveToHighestInSubtree();
184 void MoveToEnd() {
185 target_render_surface_layer_index_ =
186 LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex;
187 current_layer_index_ = 0;
190 void MoveToNext() {
191 // Moves to the previous layer in the current RS layer list.
192 // Then we check if the new current layer has its own RS,
193 // in which case there are things in that RS layer list that are higher,
194 // so we find the highest layer in that subtree.
195 // If we move back past the front of the list,
196 // we jump up to the previous RS layer list, picking up again where we
197 // had previously recursed into the current RS layer list.
199 if (!current_layer_represents_target_render_surface()) {
200 // Subtracting one here will eventually cause the current layer
201 // to become that layer representing the target render surface.
202 --current_layer_index_;
203 MoveToHighestInSubtree();
204 } else {
205 while (current_layer_represents_target_render_surface()) {
206 if (!target_render_surface_layer_index_) {
207 // End of the list.
208 target_render_surface_layer_index_ =
209 LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex;
210 current_layer_index_ = 0;
211 return;
213 target_render_surface_layer_index_ =
214 target_render_surface()->target_render_surface_layer_index_history_;
215 current_layer_index_ =
216 target_render_surface()->current_layer_index_history_;
221 void MoveToHighestInSubtree() {
222 if (current_layer_represents_target_render_surface())
223 return;
224 while (current_layer_represents_contributing_render_surface()) {
225 // Save where we were in the current target surface, move to the next one,
226 // and save the target surface that we came from there
227 // so we can go back to it.
228 target_render_surface()->current_layer_index_history_ =
229 current_layer_index_;
230 int previous_target_render_surface_layer =
231 target_render_surface_layer_index_;
233 for (LayerImpl* layer = current_layer();
234 target_render_surface_layer() != layer;
235 ++target_render_surface_layer_index_) {
237 current_layer_index_ = target_render_surface_children().size() - 1;
239 target_render_surface()->target_render_surface_layer_index_history_ =
240 previous_target_render_surface_layer;
244 inline LayerImpl* current_layer() const {
245 return current_layer_represents_target_render_surface()
246 ? target_render_surface_layer()
247 : LayerTreeHostCommon::get_layer_as_raw_ptr(
248 target_render_surface_children(), current_layer_index_);
251 inline bool current_layer_represents_contributing_render_surface() const {
252 return LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerImpl>(
253 current_layer(), target_render_surface_layer()->id());
255 inline bool current_layer_represents_target_render_surface() const {
256 return current_layer_index_ ==
257 LayerIteratorValue::kLayerIndexRepresentingTargetRenderSurface;
260 inline RenderSurfaceImpl* target_render_surface() const {
261 return target_render_surface_layer()->render_surface();
263 inline const LayerImplList& target_render_surface_children() const {
264 return target_render_surface()->layer_list();
267 const LayerImplList* render_surface_layer_list_;
269 // The iterator's current position.
271 // A position in the render_surface_layer_list. This points to a layer which
272 // owns the current target surface. This is a value from 0 to n-1
273 // (n = size of render_surface_layer_list = number of surfaces).
274 // A value outside of this range
275 // (for example, LayerIteratorValue::kInvalidTargetRenderSurfaceLayerIndex)
276 // is used to indicate a position outside the bounds of the tree.
277 int target_render_surface_layer_index_;
278 // A position in the list of layers that are children of the
279 // current target surface. When pointing to one of these layers,
280 // this is a value from 0 to n-1 (n = number of children).
281 // Since the iterator must also stop at the layers representing
282 // the target surface, this is done by setting the current_layer_index
283 // to a value of
284 // LayerIteratorValue::kLayerIndexRepresentingTargetRenderSurface.
285 size_t current_layer_index_;
288 } // namespace cc
290 #endif // CC_LAYERS_LAYER_ITERATOR_H_