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