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 CCLayerIterator_h | 5 // Temporary forwarding header |
6 #define CCLayerIterator_h | 6 #include "cc/layer_iterator.h" |
7 | |
8 #include "CCLayerTreeHostCommon.h" | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 | |
12 namespace cc { | |
13 | |
14 // These classes provide means to iterate over the RenderSurface-Layer tree. | |
15 | |
16 // Example code follows, for a tree of LayerChromium/RenderSurfaceChromium objec
ts. See below for details. | |
17 // | |
18 // void doStuffOnLayers(const std::vector<scoped_refptr<LayerChromium> >& render
SurfaceLayerList) | |
19 // { | |
20 // typedef CCLayerIterator<LayerChromium, RenderSurfaceChromium, CCLayerIter
atorActions::FrontToBack> CCLayerIteratorType; | |
21 // | |
22 // CCLayerIteratorType end = CCLayerIteratorType::end(&renderSurfaceLayerLis
t); | |
23 // for (CCLayerIteratorType it = CCLayerIteratorType::begin(&renderSurfaceLa
yerList); it != end; ++it) { | |
24 // // Only one of these will be true | |
25 // if (it.representsTargetRenderSurface()) | |
26 // foo(*it); // *it is a layer representing a target RenderSurface | |
27 // if (it.representsContributingRenderSurface()) | |
28 // bar(*it); // *it is a layer representing a RenderSurface that con
tributes to the layer's target RenderSurface | |
29 // if (it.representsItself()) | |
30 // baz(*it); // *it is a layer representing itself, as it contribute
s to its own target RenderSurface | |
31 // } | |
32 // } | |
33 | |
34 // A RenderSurface R may be referred to in one of two different contexts. One Re
nderSurface is "current" at any time, for | |
35 // whatever operation is being performed. This current surface is referred to as
a target surface. For example, when R is | |
36 // being painted it would be the target surface. Once R has been painted, its co
ntents may be included into another | |
37 // surface S. While S is considered the target surface when it is being painted,
R is called a contributing surface | |
38 // in this context as it contributes to the content of the target surface S. | |
39 // | |
40 // The iterator's current position in the tree always points to some layer. The
state of the iterator indicates the role of the | |
41 // layer, and will be one of the following three states. A single layer L will a
ppear in the iteration process in at least one, | |
42 // and possibly all, of these states. | |
43 // 1. Representing the target surface: The iterator in this state, pointing at l
ayer L, indicates that the target RenderSurface | |
44 // is now the surface owned by L. This will occur exactly once for each RenderSu
rface in the tree. | |
45 // 2. Representing a contributing surface: The iterator in this state, pointing
at layer L, refers to the RenderSurface owned | |
46 // by L as a contributing surface, without changing the current target RenderSur
face. | |
47 // 3. Representing itself: The iterator in this state, pointing at layer L, refe
rs to the layer itself, as a child of the | |
48 // current target RenderSurface. | |
49 // | |
50 // The BackToFront iterator will return a layer representing the target surface
before returning layers representing themselves | |
51 // as children of the current target surface. Whereas the FrontToBack ordering w
ill iterate over children layers of a surface | |
52 // before the layer representing the surface as a target surface. | |
53 // | |
54 // To use the iterators: | |
55 // | |
56 // Create a stepping iterator and end iterator by calling CCLayerIterator::begin
() and CCLayerIterator::end() and passing in the | |
57 // list of layers owning target RenderSurfaces. Step through the tree by increme
nting the stepping iterator while it is != to | |
58 // the end iterator. At each step the iterator knows what the layer is represent
ing, and you can query the iterator to decide | |
59 // what actions to perform with the layer given what it represents. | |
60 | |
61 ////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////// | |
62 | |
63 // Non-templated constants | |
64 struct CCLayerIteratorValue { | |
65 static const int InvalidTargetRenderSurfaceLayerIndex = -1; | |
66 // This must be -1 since the iterator action code assumes that this value ca
n be | |
67 // reached by subtracting one from the position of the first layer in the cu
rrent | |
68 // target surface's child layer list, which is 0. | |
69 static const int LayerIndexRepresentingTargetRenderSurface = -1; | |
70 }; | |
71 | |
72 // The position of a layer iterator that is independent of its many template typ
es. | |
73 template <typename LayerType> | |
74 struct CCLayerIteratorPosition { | |
75 bool representsTargetRenderSurface; | |
76 bool representsContributingRenderSurface; | |
77 bool representsItself; | |
78 LayerType* targetRenderSurfaceLayer; | |
79 LayerType* currentLayer; | |
80 }; | |
81 | |
82 // An iterator class for walking over layers in the RenderSurface-Layer tree. | |
83 template <typename LayerType, typename LayerList, typename RenderSurfaceType, ty
pename IteratorActionType> | |
84 class CCLayerIterator { | |
85 typedef CCLayerIterator<LayerType, LayerList, RenderSurfaceType, IteratorAct
ionType> CCLayerIteratorType; | |
86 | |
87 public: | |
88 CCLayerIterator() : m_renderSurfaceLayerList(0) { } | |
89 | |
90 static CCLayerIteratorType begin(const LayerList* renderSurfaceLayerList) {
return CCLayerIteratorType(renderSurfaceLayerList, true); } | |
91 static CCLayerIteratorType end(const LayerList* renderSurfaceLayerList) { re
turn CCLayerIteratorType(renderSurfaceLayerList, false); } | |
92 | |
93 CCLayerIteratorType& operator++() { m_actions.next(*this); return *this; } | |
94 bool operator==(const CCLayerIterator& other) const | |
95 { | |
96 return m_targetRenderSurfaceLayerIndex == other.m_targetRenderSurfaceLay
erIndex | |
97 && m_currentLayerIndex == other.m_currentLayerIndex; | |
98 } | |
99 bool operator!=(const CCLayerIteratorType& other) const { return !(*this ==
other); } | |
100 | |
101 LayerType* operator->() const { return currentLayer(); } | |
102 LayerType* operator*() const { return currentLayer(); } | |
103 | |
104 bool representsTargetRenderSurface() const { return currentLayerRepresentsTa
rgetRenderSurface(); } | |
105 bool representsContributingRenderSurface() const { return !representsTargetR
enderSurface() && currentLayerRepresentsContributingRenderSurface(); } | |
106 bool representsItself() const { return !representsTargetRenderSurface() && !
representsContributingRenderSurface(); } | |
107 | |
108 LayerType* targetRenderSurfaceLayer() const { return getRawPtr((*m_renderSur
faceLayerList)[m_targetRenderSurfaceLayerIndex]); } | |
109 | |
110 operator const CCLayerIteratorPosition<LayerType>() const | |
111 { | |
112 CCLayerIteratorPosition<LayerType> position; | |
113 position.representsTargetRenderSurface = representsTargetRenderSurface()
; | |
114 position.representsContributingRenderSurface = representsContributingRen
derSurface(); | |
115 position.representsItself = representsItself(); | |
116 position.targetRenderSurfaceLayer = targetRenderSurfaceLayer(); | |
117 position.currentLayer = currentLayer(); | |
118 return position; | |
119 } | |
120 | |
121 private: | |
122 CCLayerIterator(const LayerList* renderSurfaceLayerList, bool start) | |
123 : m_renderSurfaceLayerList(renderSurfaceLayerList) | |
124 , m_targetRenderSurfaceLayerIndex(0) | |
125 { | |
126 for (size_t i = 0; i < renderSurfaceLayerList->size(); ++i) { | |
127 if (!(*renderSurfaceLayerList)[i]->renderSurface()) { | |
128 ASSERT_NOT_REACHED(); | |
129 m_actions.end(*this); | |
130 return; | |
131 } | |
132 } | |
133 | |
134 if (start && !renderSurfaceLayerList->empty()) | |
135 m_actions.begin(*this); | |
136 else | |
137 m_actions.end(*this); | |
138 } | |
139 | |
140 inline static LayerChromium* getRawPtr(const scoped_refptr<LayerChromium>& p
tr) { return ptr.get(); } | |
141 inline static CCLayerImpl* getRawPtr(CCLayerImpl* ptr) { return ptr; } | |
142 | |
143 inline LayerType* currentLayer() const { return currentLayerRepresentsTarget
RenderSurface() ? targetRenderSurfaceLayer() : getRawPtr(targetRenderSurfaceChil
dren()[m_currentLayerIndex]); } | |
144 | |
145 inline bool currentLayerRepresentsContributingRenderSurface() const { return
CCLayerTreeHostCommon::renderSurfaceContributesToTarget<LayerType>(currentLayer
(), targetRenderSurfaceLayer()->id()); } | |
146 inline bool currentLayerRepresentsTargetRenderSurface() const { return m_cur
rentLayerIndex == CCLayerIteratorValue::LayerIndexRepresentingTargetRenderSurfac
e; } | |
147 | |
148 inline RenderSurfaceType* targetRenderSurface() const { return targetRenderS
urfaceLayer()->renderSurface(); } | |
149 inline const LayerList& targetRenderSurfaceChildren() const { return targetR
enderSurface()->layerList(); } | |
150 | |
151 IteratorActionType m_actions; | |
152 const LayerList* m_renderSurfaceLayerList; | |
153 | |
154 // The iterator's current position. | |
155 | |
156 // A position in the renderSurfaceLayerList. This points to a layer which ow
ns the current target surface. | |
157 // This is a value from 0 to n-1 (n = size of renderSurfaceLayerList = numbe
r of surfaces). A value outside of | |
158 // this range (for example, CCLayerIteratorValue::InvalidTargetRenderSurface
LayerIndex) is used to | |
159 // indicate a position outside the bounds of the tree. | |
160 int m_targetRenderSurfaceLayerIndex; | |
161 // A position in the list of layers that are children of the current target
surface. When pointing to one of | |
162 // these layers, this is a value from 0 to n-1 (n = number of children). Sin
ce the iterator must also stop at | |
163 // the layers representing the target surface, this is done by setting the c
urrentLayerIndex to a value of | |
164 // CCLayerIteratorValue::LayerRepresentingTargetRenderSurface. | |
165 int m_currentLayerIndex; | |
166 | |
167 friend struct CCLayerIteratorActions; | |
168 }; | |
169 | |
170 // Orderings for iterating over the RenderSurface-Layer tree. | |
171 struct CCLayerIteratorActions { | |
172 // Walks layers sorted by z-order from back to front. | |
173 class BackToFront { | |
174 public: | |
175 template <typename LayerType, typename LayerList, typename RenderSurface
Type, typename ActionType> | |
176 void begin(CCLayerIterator<LayerType, LayerList, RenderSurfaceType, Acti
onType>&); | |
177 | |
178 template <typename LayerType, typename LayerList, typename RenderSurface
Type, typename ActionType> | |
179 void end(CCLayerIterator<LayerType, LayerList, RenderSurfaceType, Action
Type>&); | |
180 | |
181 template <typename LayerType, typename LayerList, typename RenderSurface
Type, typename ActionType> | |
182 void next(CCLayerIterator<LayerType, LayerList, RenderSurfaceType, Actio
nType>&); | |
183 | |
184 private: | |
185 int m_highestTargetRenderSurfaceLayer; | |
186 }; | |
187 | |
188 // Walks layers sorted by z-order from front to back | |
189 class FrontToBack { | |
190 public: | |
191 template <typename LayerType, typename LayerList, typename RenderSurface
Type, typename ActionType> | |
192 void begin(CCLayerIterator<LayerType, LayerList, RenderSurfaceType, Acti
onType>&); | |
193 | |
194 template <typename LayerType, typename LayerList, typename RenderSurface
Type, typename ActionType> | |
195 void end(CCLayerIterator<LayerType, LayerList, RenderSurfaceType, Action
Type>&); | |
196 | |
197 template <typename LayerType, typename LayerList, typename RenderSurface
Type, typename ActionType> | |
198 void next(CCLayerIterator<LayerType, LayerList, RenderSurfaceType, Actio
nType>&); | |
199 | |
200 private: | |
201 template <typename LayerType, typename LayerList, typename RenderSurface
Type, typename ActionType> | |
202 void goToHighestInSubtree(CCLayerIterator<LayerType, LayerList, RenderSu
rfaceType, ActionType>&); | |
203 }; | |
204 }; | |
205 | |
206 } // namespace cc | |
207 | |
208 #endif | |
OLD | NEW |