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 (at) fas.harvard.edu> 9 * Christian Biesinger <cbiesinger (at) web.de> 10 * Randall Jesup <rjesup (at) wgate.com> 11 * Roland Mainz <roland.mainz (at) informatik.med.uni-giessen.de> 12 * Josh Soref <timeless (at) mac.com> 13 * Boris Zbarsky <bzbarsky (at) 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 WebCore { 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 ASSERT_ENABLED 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 ASSERT_ENABLED 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 ASSERT_ENABLED 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 ASSERT_ENABLED 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 ASSERT_ENABLED 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 ASSERT_ENABLED 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 layer()->updateDescendantDependentFlags(); 206 207 if (!isNormalFlowOnly()) { 208 OwnPtr<Vector<RenderLayerStackingNode*> >& buffer = (zIndex() >= 0) ? posBuffer : negBuffer; 209 if (!buffer) 210 buffer = adoptPtr(new Vector<RenderLayerStackingNode*>); 211 buffer->append(this); 212 } 213 214 if (!isStackingContext()) { 215 for (RenderLayer* child = layer()->firstChild(); child; child = child->nextSibling()) { 216 if (!layer()->reflectionInfo() || layer()->reflectionInfo()->reflectionLayer() != child) 217 child->stackingNode()->collectLayers(posBuffer, negBuffer); 218 } 219 } 220 } 221 222 #if ASSERT_ENABLED 223 bool RenderLayerStackingNode::isInStackingParentZOrderLists() const 224 { 225 if (!m_stackingParent || m_stackingParent->zOrderListsDirty()) 226 return false; 227 228 if (m_stackingParent->posZOrderList() && m_stackingParent->posZOrderList()->find(this) != kNotFound) 229 return true; 230 231 if (m_stackingParent->negZOrderList() && m_stackingParent->negZOrderList()->find(this) != kNotFound) 232 return true; 233 234 return false; 235 } 236 237 bool RenderLayerStackingNode::isInStackingParentNormalFlowList() const 238 { 239 if (!m_stackingParent || m_stackingParent->normalFlowListDirty()) 240 return false; 241 242 return (m_stackingParent->normalFlowList() && m_stackingParent->normalFlowList()->find(this) != kNotFound); 243 } 244 245 void RenderLayerStackingNode::updateStackingParentForZOrderLists(RenderLayerStackingNode* stackingParent) 246 { 247 if (m_posZOrderList) { 248 for (size_t i = 0; i < m_posZOrderList->size(); ++i) 249 m_posZOrderList->at(i)->setStackingParent(stackingParent); 250 } 251 252 if (m_negZOrderList) { 253 for (size_t i = 0; i < m_negZOrderList->size(); ++i) 254 m_negZOrderList->at(i)->setStackingParent(stackingParent); 255 } 256 } 257 258 void RenderLayerStackingNode::updateStackingParentForNormalFlowList(RenderLayerStackingNode* stackingParent) 259 { 260 if (m_normalFlowList) { 261 for (size_t i = 0; i < m_normalFlowList->size(); ++i) 262 m_normalFlowList->at(i)->setStackingParent(stackingParent); 263 } 264 } 265 #endif 266 267 void RenderLayerStackingNode::updateLayerListsIfNeeded() 268 { 269 updateZOrderLists(); 270 updateNormalFlowList(); 271 272 if (!layer()->reflectionInfo()) 273 return; 274 275 RenderLayer* reflectionLayer = layer()->reflectionInfo()->reflectionLayer(); 276 reflectionLayer->stackingNode()->updateZOrderLists(); 277 reflectionLayer->stackingNode()->updateNormalFlowList(); 278 } 279 280 void RenderLayerStackingNode::updateStackingNodesAfterStyleChange(const RenderStyle* oldStyle) 281 { 282 bool wasStackingContext = oldStyle ? !oldStyle->hasAutoZIndex() : false; 283 int oldZIndex = oldStyle ? oldStyle->zIndex() : 0; 284 285 bool isStackingContext = this->isStackingContext(); 286 if (isStackingContext == wasStackingContext && oldZIndex == zIndex()) 287 return; 288 289 dirtyStackingContextZOrderLists(); 290 291 if (isStackingContext) 292 dirtyZOrderLists(); 293 else 294 clearZOrderLists(); 295 } 296 297 // FIXME: Rename shouldBeNormalFlowOnly to something more accurate now that CSS 298 // 2.1 defines the term "normal flow". 299 bool RenderLayerStackingNode::shouldBeNormalFlowOnly() const 300 { 301 return !isStackingContext() && !renderer()->isPositioned(); 302 } 303 304 void RenderLayerStackingNode::updateIsNormalFlowOnly() 305 { 306 bool isNormalFlowOnly = shouldBeNormalFlowOnly(); 307 if (isNormalFlowOnly == this->isNormalFlowOnly()) 308 return; 309 310 m_isNormalFlowOnly = isNormalFlowOnly; 311 if (RenderLayer* p = layer()->parent()) 312 p->stackingNode()->dirtyNormalFlowList(); 313 dirtyStackingContextZOrderLists(); 314 } 315 316 RenderLayerStackingNode* RenderLayerStackingNode::ancestorStackingContextNode() const 317 { 318 for (RenderLayer* ancestor = layer()->parent(); ancestor; ancestor = ancestor->parent()) { 319 RenderLayerStackingNode* stackingNode = ancestor->stackingNode(); 320 if (stackingNode->isStackingContext()) 321 return stackingNode; 322 } 323 return 0; 324 } 325 326 RenderLayerModelObject* RenderLayerStackingNode::renderer() const 327 { 328 return m_layer->renderer(); 329 } 330 331 } // namespace WebCore 332