2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
4 * Portions are Copyright (C) 1998 Netscape Communications Corporation.
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>
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.
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.
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
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.
45 #include "core/rendering/RenderLayerStackingNode.h"
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"
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)
59 , m_normalFlowListDirty(true)
61 , m_layerListMutationAllowed(true)
65 m_isNormalFlowOnly = shouldBeNormalFlowOnly();
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();
72 RenderLayerStackingNode::~RenderLayerStackingNode()
75 if (!renderer()->documentBeingDestroyed()) {
76 ASSERT(!isInStackingParentZOrderLists());
77 ASSERT(!isInStackingParentNormalFlowList());
79 updateStackingParentForZOrderLists(0);
80 updateStackingParentForNormalFlowList(0);
85 // Helper for the sorting of layers by z-index.
86 static inline bool compareZIndex(RenderLayerStackingNode* first, RenderLayerStackingNode* second)
88 return first->zIndex() < second->zIndex();
91 RenderLayerCompositor* RenderLayerStackingNode::compositor() const
93 ASSERT(renderer()->view());
94 return renderer()->view()->compositor();
97 void RenderLayerStackingNode::dirtyZOrderLists()
99 ASSERT(m_layerListMutationAllowed);
100 ASSERT(isStackingContext());
103 updateStackingParentForZOrderLists(0);
107 m_posZOrderList->clear();
109 m_negZOrderList->clear();
110 m_zOrderListsDirty = true;
112 if (!renderer()->documentBeingDestroyed())
113 compositor()->setNeedsCompositingUpdate(CompositingUpdateRebuildTree);
116 void RenderLayerStackingNode::dirtyStackingContextZOrderLists()
118 if (RenderLayerStackingNode* stackingNode = ancestorStackingContextNode())
119 stackingNode->dirtyZOrderLists();
122 void RenderLayerStackingNode::dirtyNormalFlowList()
124 ASSERT(m_layerListMutationAllowed);
127 updateStackingParentForNormalFlowList(0);
130 if (m_normalFlowList)
131 m_normalFlowList->clear();
132 m_normalFlowListDirty = true;
134 if (!renderer()->documentBeingDestroyed())
135 compositor()->setNeedsCompositingUpdate(CompositingUpdateRebuildTree);
138 void RenderLayerStackingNode::rebuildZOrderLists()
140 ASSERT(m_layerListMutationAllowed);
141 ASSERT(isDirtyStackingContext());
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);
148 // Sort the two lists.
150 std::stable_sort(m_posZOrderList->begin(), m_posZOrderList->end(), compareZIndex);
153 std::stable_sort(m_negZOrderList->begin(), m_negZOrderList->end(), compareZIndex);
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());
172 updateStackingParentForZOrderLists(this);
175 m_zOrderListsDirty = false;
178 void RenderLayerStackingNode::updateNormalFlowList()
180 if (!m_normalFlowListDirty)
183 ASSERT(m_layerListMutationAllowed);
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());
194 updateStackingParentForNormalFlowList(this);
197 m_normalFlowListDirty = false;
200 void RenderLayerStackingNode::collectLayers(OwnPtr<Vector<RenderLayerStackingNode*> >& posBuffer, OwnPtr<Vector<RenderLayerStackingNode*> >& negBuffer)
202 if (layer()->isInTopLayer())
205 if (!isNormalFlowOnly()) {
206 OwnPtr<Vector<RenderLayerStackingNode*> >& buffer = (zIndex() >= 0) ? posBuffer : negBuffer;
208 buffer = adoptPtr(new Vector<RenderLayerStackingNode*>);
209 buffer->append(this);
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);
221 bool RenderLayerStackingNode::isInStackingParentZOrderLists() const
223 if (!m_stackingParent || m_stackingParent->zOrderListsDirty())
226 if (m_stackingParent->posZOrderList() && m_stackingParent->posZOrderList()->find(this) != kNotFound)
229 if (m_stackingParent->negZOrderList() && m_stackingParent->negZOrderList()->find(this) != kNotFound)
235 bool RenderLayerStackingNode::isInStackingParentNormalFlowList() const
237 if (!m_stackingParent || m_stackingParent->normalFlowListDirty())
240 return (m_stackingParent->normalFlowList() && m_stackingParent->normalFlowList()->find(this) != kNotFound);
243 void RenderLayerStackingNode::updateStackingParentForZOrderLists(RenderLayerStackingNode* stackingParent)
245 if (m_posZOrderList) {
246 for (size_t i = 0; i < m_posZOrderList->size(); ++i)
247 m_posZOrderList->at(i)->setStackingParent(stackingParent);
250 if (m_negZOrderList) {
251 for (size_t i = 0; i < m_negZOrderList->size(); ++i)
252 m_negZOrderList->at(i)->setStackingParent(stackingParent);
256 void RenderLayerStackingNode::updateStackingParentForNormalFlowList(RenderLayerStackingNode* stackingParent)
258 if (m_normalFlowList) {
259 for (size_t i = 0; i < m_normalFlowList->size(); ++i)
260 m_normalFlowList->at(i)->setStackingParent(stackingParent);
265 void RenderLayerStackingNode::updateLayerListsIfNeeded()
268 updateNormalFlowList();
270 if (!layer()->reflectionInfo())
273 RenderLayer* reflectionLayer = layer()->reflectionInfo()->reflectionLayer();
274 reflectionLayer->stackingNode()->updateZOrderLists();
275 reflectionLayer->stackingNode()->updateNormalFlowList();
278 void RenderLayerStackingNode::updateStackingNodesAfterStyleChange(const RenderStyle* oldStyle)
280 bool wasStackingContext = oldStyle ? !oldStyle->hasAutoZIndex() : false;
281 int oldZIndex = oldStyle ? oldStyle->zIndex() : 0;
283 bool isStackingContext = this->isStackingContext();
284 if (isStackingContext == wasStackingContext && oldZIndex == zIndex())
287 dirtyStackingContextZOrderLists();
289 if (isStackingContext)
295 // FIXME: Rename shouldBeNormalFlowOnly to something more accurate now that CSS
296 // 2.1 defines the term "normal flow".
297 bool RenderLayerStackingNode::shouldBeNormalFlowOnly() const
299 return !isStackingContext() && !renderer()->isPositioned();
302 void RenderLayerStackingNode::updateIsNormalFlowOnly()
304 bool isNormalFlowOnly = shouldBeNormalFlowOnly();
305 if (isNormalFlowOnly == this->isNormalFlowOnly())
308 m_isNormalFlowOnly = isNormalFlowOnly;
309 if (RenderLayer* p = layer()->parent())
310 p->stackingNode()->dirtyNormalFlowList();
311 dirtyStackingContextZOrderLists();
314 RenderLayerStackingNode* RenderLayerStackingNode::ancestorStackingContextNode() const
316 for (RenderLayer* ancestor = layer()->parent(); ancestor; ancestor = ancestor->parent()) {
317 RenderLayerStackingNode* stackingNode = ancestor->stackingNode();
318 if (stackingNode->isStackingContext())
324 RenderLayerModelObject* RenderLayerStackingNode::renderer() const
326 return m_layer->renderer();