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"
34 bool RenderLayerModelObject::s_wasFloating = false;
36 RenderLayerModelObject::RenderLayerModelObject(ContainerNode* node)
41 RenderLayerModelObject::~RenderLayerModelObject()
43 // Our layer should have been destroyed and cleared by now
48 void RenderLayerModelObject::destroyLayer()
54 void RenderLayerModelObject::createLayer(LayerType type)
57 m_layer = adoptPtr(new RenderLayer(this, type));
59 m_layer->insertOnlyThisLayer();
62 bool RenderLayerModelObject::hasSelfPaintingLayer() const
64 return m_layer && m_layer->isSelfPaintingLayer();
67 ScrollableArea* RenderLayerModelObject::scrollableArea() const
69 return m_layer ? m_layer->scrollableArea() : 0;
72 void RenderLayerModelObject::willBeDestroyed()
75 // Don't use this->view() because the document's renderView has been set to 0 during destruction.
76 if (LocalFrame* frame = this->frame()) {
77 if (FrameView* frameView = frame->view()) {
78 if (style()->hasViewportConstrainedPosition())
79 frameView->removeViewportConstrainedObject(this);
84 RenderObject::willBeDestroyed();
89 void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
91 s_wasFloating = isFloating();
93 if (RenderStyle* oldStyle = style()) {
94 if (parent() && diff.needsPaintInvalidationLayer()) {
95 if (oldStyle->hasClip() != newStyle.hasClip()
96 || oldStyle->clip() != newStyle.clip())
97 layer()->clipper().clearClipRectsIncludingDescendants();
101 RenderObject::styleWillChange(diff, newStyle);
104 void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
106 bool hadTransform = hasTransform();
107 bool hadLayer = hasLayer();
108 bool layerWasSelfPainting = hadLayer && layer()->isSelfPaintingLayer();
110 RenderObject::styleDidChange(diff, oldStyle);
113 LayerType type = layerTypeRequired();
114 if (type != NoLayer) {
115 if (!layer() && layerCreationAllowedForSubtree()) {
116 if (s_wasFloating && isFloating())
117 setChildNeedsLayout();
119 if (parent() && !needsLayout()) {
120 // FIXME: This invalidation is overly broad. We should update to
121 // do the correct invalidation at RenderStyle::diff time. crbug.com/349061
122 layer()->renderer()->setShouldDoFullPaintInvalidation(true);
123 // FIXME: We should call a specialized version of this function.
124 layer()->updateLayerPositionsAfterLayout();
127 } else if (layer() && layer()->parent()) {
128 setHasTransform(false); // Either a transform wasn't specified or the object doesn't support transforms, so just null out the bit.
129 setHasReflection(false);
130 layer()->removeOnlyThisLayer(); // calls destroyLayer() which clears m_layer
131 if (s_wasFloating && isFloating())
132 setChildNeedsLayout();
134 setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
138 // FIXME: Ideally we shouldn't need this setter but we can't easily infer an overflow-only layer
140 layer()->setLayerType(type);
142 layer()->styleChanged(diff, oldStyle);
143 if (hadLayer && layer()->isSelfPaintingLayer() != layerWasSelfPainting)
144 setChildNeedsLayout();
147 if (FrameView *frameView = view()->frameView()) {
148 bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition();
149 bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
150 if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) {
151 if (newStyleIsViewportConstained && layer())
152 frameView->addViewportConstrainedObject(this);
154 frameView->removeViewportConstrainedObject(this);
159 void RenderLayerModelObject::addLayerHitTestRects(LayerHitTestRects& rects, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const
162 if (isRenderView()) {
163 // RenderView is handled with a special fast-path, but it needs to know the current layer.
164 RenderObject::addLayerHitTestRects(rects, layer(), LayoutPoint(), LayoutRect());
166 // Since a RenderObject never lives outside it's container RenderLayer, we can switch
167 // to marking entire layers instead. This may sometimes mark more than necessary (when
168 // a layer is made of disjoint objects) but in practice is a significant performance
170 layer()->addLayerHitTestRects(rects);
173 RenderObject::addLayerHitTestRects(rects, currentLayer, layerOffset, containerRect);
177 InvalidationReason RenderLayerModelObject::invalidatePaintIfNeeded(const PaintInvalidationState& paintInvalidationState, const RenderLayerModelObject& newPaintInvalidationContainer)
179 const LayoutRect oldPaintInvalidationRect = previousPaintInvalidationRect();
180 const LayoutPoint oldPositionFromPaintInvalidationContainer = previousPositionFromPaintInvalidationContainer();
181 setPreviousPaintInvalidationRect(boundsRectForPaintInvalidation(&newPaintInvalidationContainer, &paintInvalidationState));
182 setPreviousPositionFromPaintInvalidationContainer(RenderLayer::positionFromPaintInvalidationContainer(this, &newPaintInvalidationContainer, &paintInvalidationState));
184 // If we are set to do a full paint invalidation that means the RenderView will issue
185 // paint invalidations. We can then skip issuing of paint invalidations for the child
186 // renderers as they'll be covered by the RenderView.
187 if (view()->doingFullPaintInvalidation())
188 return InvalidationNone;
190 return RenderObject::invalidatePaintIfNeeded(newPaintInvalidationContainer, oldPaintInvalidationRect, oldPositionFromPaintInvalidationContainer, paintInvalidationState);
193 void RenderLayerModelObject::invalidateTreeIfNeeded(const PaintInvalidationState& paintInvalidationState)
195 // FIXME: SVG should probably also go through this unified paint invalidation system.
196 ASSERT(!needsLayout());
198 if (!shouldCheckForPaintInvalidation(paintInvalidationState))
201 bool establishesNewPaintInvalidationContainer = isPaintInvalidationContainer();
202 const RenderLayerModelObject& newPaintInvalidationContainer = *adjustCompositedContainerForSpecialAncestors(establishesNewPaintInvalidationContainer ? this : &paintInvalidationState.paintInvalidationContainer());
203 ASSERT(&newPaintInvalidationContainer == containerForPaintInvalidation());
205 InvalidationReason reason = invalidatePaintIfNeeded(paintInvalidationState, newPaintInvalidationContainer);
207 PaintInvalidationState childTreeWalkState(paintInvalidationState, *this, newPaintInvalidationContainer);
208 if (reason == InvalidationLocationChange || reason == InvalidationFull)
209 childTreeWalkState.setForceCheckForPaintInvalidation();
210 RenderObject::invalidateTreeIfNeeded(childTreeWalkState);