Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderLayerModelObject.cpp
1 /*
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.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include "config.h"
26 #include "core/rendering/RenderLayerModelObject.h"
27
28 #include "core/frame/LocalFrame.h"
29 #include "core/rendering/RenderLayer.h"
30 #include "core/rendering/RenderView.h"
31
32 namespace blink {
33
34 bool RenderLayerModelObject::s_wasFloating = false;
35
36 RenderLayerModelObject::RenderLayerModelObject(ContainerNode* node)
37     : RenderObject(node)
38 {
39 }
40
41 RenderLayerModelObject::~RenderLayerModelObject()
42 {
43     // Our layer should have been destroyed and cleared by now
44     ASSERT(!hasLayer());
45     ASSERT(!m_layer);
46 }
47
48 void RenderLayerModelObject::destroyLayer()
49 {
50     setHasLayer(false);
51     m_layer = nullptr;
52 }
53
54 void RenderLayerModelObject::createLayer(LayerType type)
55 {
56     ASSERT(!m_layer);
57     m_layer = adoptPtr(new RenderLayer(this, type));
58     setHasLayer(true);
59     m_layer->insertOnlyThisLayer();
60 }
61
62 bool RenderLayerModelObject::hasSelfPaintingLayer() const
63 {
64     return m_layer && m_layer->isSelfPaintingLayer();
65 }
66
67 ScrollableArea* RenderLayerModelObject::scrollableArea() const
68 {
69     return m_layer ? m_layer->scrollableArea() : 0;
70 }
71
72 void RenderLayerModelObject::willBeDestroyed()
73 {
74     if (isPositioned()) {
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);
80             }
81         }
82     }
83
84     RenderObject::willBeDestroyed();
85
86     destroyLayer();
87 }
88
89 void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
90 {
91     s_wasFloating = isFloating();
92
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();
98         }
99     }
100
101     RenderObject::styleWillChange(diff, newStyle);
102 }
103
104 void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
105 {
106     bool hadTransform = hasTransform();
107     bool hadLayer = hasLayer();
108     bool layerWasSelfPainting = hadLayer && layer()->isSelfPaintingLayer();
109
110     RenderObject::styleDidChange(diff, oldStyle);
111     updateFromStyle();
112
113     LayerType type = layerTypeRequired();
114     if (type != NoLayer) {
115         if (!layer() && layerCreationAllowedForSubtree()) {
116             if (s_wasFloating && isFloating())
117                 setChildNeedsLayout();
118             createLayer(type);
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();
125             }
126         }
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();
133         if (hadTransform)
134             setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
135     }
136
137     if (layer()) {
138         // FIXME: Ideally we shouldn't need this setter but we can't easily infer an overflow-only layer
139         // from the style.
140         layer()->setLayerType(type);
141
142         layer()->styleChanged(diff, oldStyle);
143         if (hadLayer && layer()->isSelfPaintingLayer() != layerWasSelfPainting)
144             setChildNeedsLayout();
145     }
146
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);
153             else
154                 frameView->removeViewportConstrainedObject(this);
155         }
156     }
157 }
158
159 void RenderLayerModelObject::addLayerHitTestRects(LayerHitTestRects& rects, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const
160 {
161     if (hasLayer()) {
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());
165         } else {
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
169             // savings.
170             layer()->addLayerHitTestRects(rects);
171         }
172     } else {
173         RenderObject::addLayerHitTestRects(rects, currentLayer, layerOffset, containerRect);
174     }
175 }
176
177 InvalidationReason RenderLayerModelObject::invalidatePaintIfNeeded(const PaintInvalidationState& paintInvalidationState, const RenderLayerModelObject& newPaintInvalidationContainer)
178 {
179     const LayoutRect oldPaintInvalidationRect = previousPaintInvalidationRect();
180     const LayoutPoint oldPositionFromPaintInvalidationContainer = previousPositionFromPaintInvalidationContainer();
181     setPreviousPaintInvalidationRect(boundsRectForPaintInvalidation(&newPaintInvalidationContainer, &paintInvalidationState));
182     setPreviousPositionFromPaintInvalidationContainer(RenderLayer::positionFromPaintInvalidationContainer(this, &newPaintInvalidationContainer, &paintInvalidationState));
183
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;
189
190     return RenderObject::invalidatePaintIfNeeded(newPaintInvalidationContainer, oldPaintInvalidationRect, oldPositionFromPaintInvalidationContainer, paintInvalidationState);
191 }
192
193 void RenderLayerModelObject::invalidateTreeIfNeeded(const PaintInvalidationState& paintInvalidationState)
194 {
195     // FIXME: SVG should probably also go through this unified paint invalidation system.
196     ASSERT(!needsLayout());
197
198     if (!shouldCheckForPaintInvalidation(paintInvalidationState))
199         return;
200
201     bool establishesNewPaintInvalidationContainer = isPaintInvalidationContainer();
202     const RenderLayerModelObject& newPaintInvalidationContainer = *adjustCompositedContainerForSpecialAncestors(establishesNewPaintInvalidationContainer ? this : &paintInvalidationState.paintInvalidationContainer());
203     ASSERT(&newPaintInvalidationContainer == containerForPaintInvalidation());
204
205     InvalidationReason reason = invalidatePaintIfNeeded(paintInvalidationState, newPaintInvalidationContainer);
206
207     PaintInvalidationState childTreeWalkState(paintInvalidationState, *this, newPaintInvalidationContainer);
208     if (reason == InvalidationLocationChange || reason == InvalidationFull)
209         childTreeWalkState.setForceCheckForPaintInvalidation();
210     RenderObject::invalidateTreeIfNeeded(childTreeWalkState);
211 }
212
213 } // namespace blink
214