Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderLayerStackingNode.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3  *
4  * Portions are Copyright (C) 1998 Netscape Communications Corporation.
5  *
6  * Other contributors:
7  *   Robert O'Callahan <roc+@cs.cmu.edu>
8  *   David Baron <dbaron@fas.harvard.edu>
9  *   Christian Biesinger <cbiesinger@web.de>
10  *   Randall Jesup <rjesup@wgate.com>
11  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
12  *   Josh Soref <timeless@mac.com>
13  *   Boris Zbarsky <bzbarsky@mit.edu>
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2.1 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
28  *
29  * Alternatively, the contents of this file may be used under the terms
30  * of either the Mozilla Public License Version 1.1, found at
31  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
32  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
33  * (the "GPL"), in which case the provisions of the MPL or the GPL are
34  * applicable instead of those above.  If you wish to allow use of your
35  * version of this file only under the terms of one of those two
36  * licenses (the MPL or the GPL) and not to allow others to use your
37  * version of this file under the LGPL, indicate your decision by
38  * deletingthe provisions above and replace them with the notice and
39  * other provisions required by the MPL or the GPL, as the case may be.
40  * If you do not delete the provisions above, a recipient may use your
41  * version of this file under any of the LGPL, the MPL or the GPL.
42  */
43
44 #include "config.h"
45 #include "core/rendering/RenderLayerStackingNode.h"
46
47 #include "core/rendering/RenderLayer.h"
48 #include "core/rendering/RenderView.h"
49 #include "core/rendering/compositing/RenderLayerCompositor.h"
50 #include "public/platform/Platform.h"
51
52 namespace blink {
53
54 // FIXME: This should not require RenderLayer. There is currently a cycle where
55 // in order to determine if we shoulBeNormalFlowOnly() we have to ask the render
56 // layer about some of its state.
57 RenderLayerStackingNode::RenderLayerStackingNode(RenderLayer* layer)
58     : m_layer(layer)
59     , m_normalFlowListDirty(true)
60 #if ENABLE(ASSERT)
61     , m_layerListMutationAllowed(true)
62     , m_stackingParent(0)
63 #endif
64 {
65     m_isNormalFlowOnly = shouldBeNormalFlowOnly();
66
67     // Non-stacking contexts should have empty z-order lists. As this is already the case,
68     // there is no need to dirty / recompute these lists.
69     m_zOrderListsDirty = isStackingContext();
70 }
71
72 RenderLayerStackingNode::~RenderLayerStackingNode()
73 {
74 #if ENABLE(ASSERT)
75     if (!renderer()->documentBeingDestroyed()) {
76         ASSERT(!isInStackingParentZOrderLists());
77         ASSERT(!isInStackingParentNormalFlowList());
78
79         updateStackingParentForZOrderLists(0);
80         updateStackingParentForNormalFlowList(0);
81     }
82 #endif
83 }
84
85 // Helper for the sorting of layers by z-index.
86 static inline bool compareZIndex(RenderLayerStackingNode* first, RenderLayerStackingNode* second)
87 {
88     return first->zIndex() < second->zIndex();
89 }
90
91 RenderLayerCompositor* RenderLayerStackingNode::compositor() const
92 {
93     ASSERT(renderer()->view());
94     return renderer()->view()->compositor();
95 }
96
97 void RenderLayerStackingNode::dirtyZOrderLists()
98 {
99     ASSERT(m_layerListMutationAllowed);
100     ASSERT(isStackingContext());
101
102 #if ENABLE(ASSERT)
103     updateStackingParentForZOrderLists(0);
104 #endif
105
106     if (m_posZOrderList)
107         m_posZOrderList->clear();
108     if (m_negZOrderList)
109         m_negZOrderList->clear();
110     m_zOrderListsDirty = true;
111
112     if (!renderer()->documentBeingDestroyed())
113         compositor()->setNeedsCompositingUpdate(CompositingUpdateRebuildTree);
114 }
115
116 void RenderLayerStackingNode::dirtyStackingContextZOrderLists()
117 {
118     if (RenderLayerStackingNode* stackingNode = ancestorStackingContextNode())
119         stackingNode->dirtyZOrderLists();
120 }
121
122 void RenderLayerStackingNode::dirtyNormalFlowList()
123 {
124     ASSERT(m_layerListMutationAllowed);
125
126 #if ENABLE(ASSERT)
127     updateStackingParentForNormalFlowList(0);
128 #endif
129
130     if (m_normalFlowList)
131         m_normalFlowList->clear();
132     m_normalFlowListDirty = true;
133
134     if (!renderer()->documentBeingDestroyed())
135         compositor()->setNeedsCompositingUpdate(CompositingUpdateRebuildTree);
136 }
137
138 void RenderLayerStackingNode::rebuildZOrderLists()
139 {
140     ASSERT(m_layerListMutationAllowed);
141     ASSERT(isDirtyStackingContext());
142
143     for (RenderLayer* child = layer()->firstChild(); child; child = child->nextSibling()) {
144         if (!layer()->reflectionInfo() || layer()->reflectionInfo()->reflectionLayer() != child)
145             child->stackingNode()->collectLayers(m_posZOrderList, m_negZOrderList);
146     }
147
148     // Sort the two lists.
149     if (m_posZOrderList)
150         std::stable_sort(m_posZOrderList->begin(), m_posZOrderList->end(), compareZIndex);
151
152     if (m_negZOrderList)
153         std::stable_sort(m_negZOrderList->begin(), m_negZOrderList->end(), compareZIndex);
154
155     // Append layers for top layer elements after normal layer collection, to ensure they are on top regardless of z-indexes.
156     // The renderers of top layer elements are children of the view, sorted in top layer stacking order.
157     if (layer()->isRootLayer()) {
158         RenderView* view = renderer()->view();
159         for (RenderObject* child = view->firstChild(); child; child = child->nextSibling()) {
160             Element* childElement = (child->node() && child->node()->isElementNode()) ? toElement(child->node()) : 0;
161             if (childElement && childElement->isInTopLayer()) {
162                 RenderLayer* layer = toRenderLayerModelObject(child)->layer();
163                 // Create the buffer if it doesn't exist yet.
164                 if (!m_posZOrderList)
165                     m_posZOrderList = adoptPtr(new Vector<RenderLayerStackingNode*>);
166                 m_posZOrderList->append(layer->stackingNode());
167             }
168         }
169     }
170
171 #if ENABLE(ASSERT)
172     updateStackingParentForZOrderLists(this);
173 #endif
174
175     m_zOrderListsDirty = false;
176 }
177
178 void RenderLayerStackingNode::updateNormalFlowList()
179 {
180     if (!m_normalFlowListDirty)
181         return;
182
183     ASSERT(m_layerListMutationAllowed);
184
185     for (RenderLayer* child = layer()->firstChild(); child; child = child->nextSibling()) {
186         if (child->stackingNode()->isNormalFlowOnly() && (!layer()->reflectionInfo() || layer()->reflectionInfo()->reflectionLayer() != child)) {
187             if (!m_normalFlowList)
188                 m_normalFlowList = adoptPtr(new Vector<RenderLayerStackingNode*>);
189             m_normalFlowList->append(child->stackingNode());
190         }
191     }
192
193 #if ENABLE(ASSERT)
194     updateStackingParentForNormalFlowList(this);
195 #endif
196
197     m_normalFlowListDirty = false;
198 }
199
200 void RenderLayerStackingNode::collectLayers(OwnPtr<Vector<RenderLayerStackingNode*> >& posBuffer, OwnPtr<Vector<RenderLayerStackingNode*> >& negBuffer)
201 {
202     if (layer()->isInTopLayer())
203         return;
204
205     if (!isNormalFlowOnly()) {
206         OwnPtr<Vector<RenderLayerStackingNode*> >& buffer = (zIndex() >= 0) ? posBuffer : negBuffer;
207         if (!buffer)
208             buffer = adoptPtr(new Vector<RenderLayerStackingNode*>);
209         buffer->append(this);
210     }
211
212     if (!isStackingContext()) {
213         for (RenderLayer* child = layer()->firstChild(); child; child = child->nextSibling()) {
214             if (!layer()->reflectionInfo() || layer()->reflectionInfo()->reflectionLayer() != child)
215                 child->stackingNode()->collectLayers(posBuffer, negBuffer);
216         }
217     }
218 }
219
220 #if ENABLE(ASSERT)
221 bool RenderLayerStackingNode::isInStackingParentZOrderLists() const
222 {
223     if (!m_stackingParent || m_stackingParent->zOrderListsDirty())
224         return false;
225
226     if (m_stackingParent->posZOrderList() && m_stackingParent->posZOrderList()->find(this) != kNotFound)
227         return true;
228
229     if (m_stackingParent->negZOrderList() && m_stackingParent->negZOrderList()->find(this) != kNotFound)
230         return true;
231
232     return false;
233 }
234
235 bool RenderLayerStackingNode::isInStackingParentNormalFlowList() const
236 {
237     if (!m_stackingParent || m_stackingParent->normalFlowListDirty())
238         return false;
239
240     return (m_stackingParent->normalFlowList() && m_stackingParent->normalFlowList()->find(this) != kNotFound);
241 }
242
243 void RenderLayerStackingNode::updateStackingParentForZOrderLists(RenderLayerStackingNode* stackingParent)
244 {
245     if (m_posZOrderList) {
246         for (size_t i = 0; i < m_posZOrderList->size(); ++i)
247             m_posZOrderList->at(i)->setStackingParent(stackingParent);
248     }
249
250     if (m_negZOrderList) {
251         for (size_t i = 0; i < m_negZOrderList->size(); ++i)
252             m_negZOrderList->at(i)->setStackingParent(stackingParent);
253     }
254 }
255
256 void RenderLayerStackingNode::updateStackingParentForNormalFlowList(RenderLayerStackingNode* stackingParent)
257 {
258     if (m_normalFlowList) {
259         for (size_t i = 0; i < m_normalFlowList->size(); ++i)
260             m_normalFlowList->at(i)->setStackingParent(stackingParent);
261     }
262 }
263 #endif
264
265 void RenderLayerStackingNode::updateLayerListsIfNeeded()
266 {
267     updateZOrderLists();
268     updateNormalFlowList();
269
270     if (!layer()->reflectionInfo())
271         return;
272
273     RenderLayer* reflectionLayer = layer()->reflectionInfo()->reflectionLayer();
274     reflectionLayer->stackingNode()->updateZOrderLists();
275     reflectionLayer->stackingNode()->updateNormalFlowList();
276 }
277
278 void RenderLayerStackingNode::updateStackingNodesAfterStyleChange(const RenderStyle* oldStyle)
279 {
280     bool wasStackingContext = oldStyle ? !oldStyle->hasAutoZIndex() : false;
281     int oldZIndex = oldStyle ? oldStyle->zIndex() : 0;
282
283     bool isStackingContext = this->isStackingContext();
284     if (isStackingContext == wasStackingContext && oldZIndex == zIndex())
285         return;
286
287     dirtyStackingContextZOrderLists();
288
289     if (isStackingContext)
290         dirtyZOrderLists();
291     else
292         clearZOrderLists();
293 }
294
295 // FIXME: Rename shouldBeNormalFlowOnly to something more accurate now that CSS
296 // 2.1 defines the term "normal flow".
297 bool RenderLayerStackingNode::shouldBeNormalFlowOnly() const
298 {
299     return !isStackingContext() && !renderer()->isPositioned();
300 }
301
302 void RenderLayerStackingNode::updateIsNormalFlowOnly()
303 {
304     bool isNormalFlowOnly = shouldBeNormalFlowOnly();
305     if (isNormalFlowOnly == this->isNormalFlowOnly())
306         return;
307
308     m_isNormalFlowOnly = isNormalFlowOnly;
309     if (RenderLayer* p = layer()->parent())
310         p->stackingNode()->dirtyNormalFlowList();
311     dirtyStackingContextZOrderLists();
312 }
313
314 RenderLayerStackingNode* RenderLayerStackingNode::ancestorStackingContextNode() const
315 {
316     for (RenderLayer* ancestor = layer()->parent(); ancestor; ancestor = ancestor->parent()) {
317         RenderLayerStackingNode* stackingNode = ancestor->stackingNode();
318         if (stackingNode->isStackingContext())
319             return stackingNode;
320     }
321     return 0;
322 }
323
324 RenderLayerModelObject* RenderLayerStackingNode::renderer() const
325 {
326     return m_layer->renderer();
327 }
328
329 } // namespace blink