2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
5 * (C) 2005, 2006 Samuel Weinig (sam.weinig@gmail.com)
6 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2010, 2012 Google Inc. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 #include "core/rendering/RenderLayerModelObject.h"
28 #include "core/frame/LocalFrame.h"
29 #include "core/rendering/RenderLayer.h"
30 #include "core/rendering/RenderView.h"
31 #include "core/rendering/compositing/CompositedLayerMapping.h"
35 bool RenderLayerModelObject::s_wasFloating = false;
37 RenderLayerModelObject::RenderLayerModelObject(ContainerNode* node)
42 RenderLayerModelObject::~RenderLayerModelObject()
44 // Our layer should have been destroyed and cleared by now
49 void RenderLayerModelObject::destroyLayer()
55 void RenderLayerModelObject::createLayer(LayerType type)
58 m_layer = adoptPtr(new RenderLayer(this, type));
60 m_layer->insertOnlyThisLayer();
63 bool RenderLayerModelObject::hasSelfPaintingLayer() const
65 return m_layer && m_layer->isSelfPaintingLayer();
68 ScrollableArea* RenderLayerModelObject::scrollableArea() const
70 return m_layer ? m_layer->scrollableArea() : 0;
73 void RenderLayerModelObject::willBeDestroyed()
76 // Don't use this->view() because the document's renderView has been set to 0 during destruction.
77 if (LocalFrame* frame = this->frame()) {
78 if (FrameView* frameView = frame->view()) {
79 if (style()->hasViewportConstrainedPosition())
80 frameView->removeViewportConstrainedObject(this);
85 RenderObject::willBeDestroyed();
90 void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
92 s_wasFloating = isFloating();
94 if (RenderStyle* oldStyle = style()) {
95 if (parent() && diff.needsPaintInvalidationLayer()) {
96 if (oldStyle->hasAutoClip() != newStyle.hasAutoClip()
97 || oldStyle->clip() != newStyle.clip())
98 layer()->clipper().clearClipRectsIncludingDescendants();
102 RenderObject::styleWillChange(diff, newStyle);
105 void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
107 bool hadTransform = hasTransform();
108 bool hadLayer = hasLayer();
109 bool layerWasSelfPainting = hadLayer && layer()->isSelfPaintingLayer();
111 RenderObject::styleDidChange(diff, oldStyle);
114 LayerType type = layerTypeRequired();
115 if (type != NoLayer) {
116 if (!layer() && layerCreationAllowedForSubtree()) {
117 if (s_wasFloating && isFloating())
118 setChildNeedsLayout();
120 if (parent() && !needsLayout()) {
121 // FIXME: We should call a specialized version of this function.
122 layer()->updateLayerPositionsAfterLayout();
125 } else if (layer() && layer()->parent()) {
126 setHasTransform(false); // Either a transform wasn't specified or the object doesn't support transforms, so just null out the bit.
127 setHasReflection(false);
128 layer()->removeOnlyThisLayer(); // calls destroyLayer() which clears m_layer
129 if (s_wasFloating && isFloating())
130 setChildNeedsLayout();
132 setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
136 // FIXME: Ideally we shouldn't need this setter but we can't easily infer an overflow-only layer
138 layer()->setLayerType(type);
140 layer()->styleChanged(diff, oldStyle);
141 if (hadLayer && layer()->isSelfPaintingLayer() != layerWasSelfPainting)
142 setChildNeedsLayout();
145 if (FrameView *frameView = view()->frameView()) {
146 bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition();
147 bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
148 if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) {
149 if (newStyleIsViewportConstained && layer())
150 frameView->addViewportConstrainedObject(this);
152 frameView->removeViewportConstrainedObject(this);
157 void RenderLayerModelObject::addLayerHitTestRects(LayerHitTestRects& rects, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const
160 if (isRenderView()) {
161 // RenderView is handled with a special fast-path, but it needs to know the current layer.
162 RenderObject::addLayerHitTestRects(rects, layer(), LayoutPoint(), LayoutRect());
164 // Since a RenderObject never lives outside it's container RenderLayer, we can switch
165 // to marking entire layers instead. This may sometimes mark more than necessary (when
166 // a layer is made of disjoint objects) but in practice is a significant performance
168 layer()->addLayerHitTestRects(rects);
171 RenderObject::addLayerHitTestRects(rects, currentLayer, layerOffset, containerRect);
175 void RenderLayerModelObject::invalidateTreeIfNeeded(const PaintInvalidationState& paintInvalidationState)
177 ASSERT(!needsLayout());
179 if (!shouldCheckForPaintInvalidation(paintInvalidationState))
182 bool establishesNewPaintInvalidationContainer = isPaintInvalidationContainer();
183 const RenderLayerModelObject& newPaintInvalidationContainer = *adjustCompositedContainerForSpecialAncestors(establishesNewPaintInvalidationContainer ? this : &paintInvalidationState.paintInvalidationContainer());
184 ASSERT(&newPaintInvalidationContainer == containerForPaintInvalidation());
186 InvalidationReason reason = invalidatePaintIfNeeded(paintInvalidationState, newPaintInvalidationContainer);
187 clearPaintInvalidationState(paintInvalidationState);
189 PaintInvalidationState childTreeWalkState(paintInvalidationState, *this, newPaintInvalidationContainer);
190 if (reason == InvalidationLocationChange || reason == InvalidationFull)
191 childTreeWalkState.setForceCheckForPaintInvalidation();
192 invalidatePaintOfSubtreesIfNeeded(childTreeWalkState);
195 void RenderLayerModelObject::setBackingNeedsPaintInvalidationInRect(const LayoutRect& r) const
197 // https://bugs.webkit.org/show_bug.cgi?id=61159 describes an unreproducible crash here,
198 // so assert but check that the layer is composited.
199 ASSERT(compositingState() != NotComposited);
201 WebInvalidationDebugAnnotations annotations = WebInvalidationDebugAnnotationsNone;
202 if (!hadPaintInvalidation())
203 annotations = WebInvalidationDebugAnnotationsFirstPaint;
204 // FIXME: The callers assume they are calling a const function but this function has a side effect.
205 const_cast<RenderLayerModelObject*>(this)->setHadPaintInvalidation();
207 // FIXME: generalize accessors to backing GraphicsLayers so that this code is squashing-agnostic.
208 if (layer()->groupedMapping()) {
209 LayoutRect paintInvalidationRect = r;
210 paintInvalidationRect.move(layer()->subpixelAccumulation());
211 if (GraphicsLayer* squashingLayer = layer()->groupedMapping()->squashingLayer())
212 squashingLayer->setNeedsDisplayInRect(pixelSnappedIntRect(paintInvalidationRect), annotations);
214 layer()->compositedLayerMapping()->setContentsNeedDisplayInRect(r, annotations);