453212b1e2cc40f66af099a599567be995cb9b55
[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
108     RenderObject::styleDidChange(diff, oldStyle);
109     updateFromStyle();
110
111     LayerType type = layerTypeRequired();
112     if (type != NoLayer) {
113         if (!layer() && layerCreationAllowedForSubtree()) {
114             if (s_wasFloating && isFloating())
115                 setChildNeedsLayout();
116             createLayer(type);
117             if (parent() && !needsLayout()) {
118                 // FIXME: This invalidation is overly broad. We should update to
119                 // do the correct invalidation at RenderStyle::diff time. crbug.com/349061
120                 layer()->renderer()->setShouldDoFullPaintInvalidation(true);
121                 // FIXME: We should call a specialized version of this function.
122                 layer()->updateLayerPositionsAfterLayout();
123             }
124         }
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();
131         if (hadTransform)
132             setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
133     }
134
135     if (layer()) {
136         // FIXME: Ideally we shouldn't need this setter but we can't easily infer an overflow-only layer
137         // from the style.
138         layer()->setLayerType(type);
139         layer()->styleChanged(diff, oldStyle);
140     }
141
142     if (FrameView *frameView = view()->frameView()) {
143         bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition();
144         bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
145         if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) {
146             if (newStyleIsViewportConstained && layer())
147                 frameView->addViewportConstrainedObject(this);
148             else
149                 frameView->removeViewportConstrainedObject(this);
150         }
151     }
152 }
153
154 void RenderLayerModelObject::addLayerHitTestRects(LayerHitTestRects& rects, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const
155 {
156     if (hasLayer()) {
157         if (isRenderView()) {
158             // RenderView is handled with a special fast-path, but it needs to know the current layer.
159             RenderObject::addLayerHitTestRects(rects, layer(), LayoutPoint(), LayoutRect());
160         } else {
161             // Since a RenderObject never lives outside it's container RenderLayer, we can switch
162             // to marking entire layers instead. This may sometimes mark more than necessary (when
163             // a layer is made of disjoint objects) but in practice is a significant performance
164             // savings.
165             layer()->addLayerHitTestRects(rects);
166         }
167     } else {
168         RenderObject::addLayerHitTestRects(rects, currentLayer, layerOffset, containerRect);
169     }
170 }
171
172 InvalidationReason RenderLayerModelObject::invalidatePaintIfNeeded(const PaintInvalidationState& paintInvalidationState, const RenderLayerModelObject& newPaintInvalidationContainer)
173 {
174     const LayoutRect oldPaintInvalidationRect = previousPaintInvalidationRect();
175     const LayoutPoint oldPositionFromPaintInvalidationContainer = previousPositionFromPaintInvalidationContainer();
176     setPreviousPaintInvalidationRect(boundsRectForPaintInvalidation(&newPaintInvalidationContainer, &paintInvalidationState));
177     setPreviousPositionFromPaintInvalidationContainer(RenderLayer::positionFromPaintInvalidationContainer(this, &newPaintInvalidationContainer, &paintInvalidationState));
178
179     // If we are set to do a full paint invalidation that means the RenderView will issue
180     // paint invalidations. We can then skip issuing of paint invalidations for the child
181     // renderers as they'll be covered by the RenderView.
182     if (view()->doingFullPaintInvalidation())
183         return InvalidationNone;
184
185     return RenderObject::invalidatePaintIfNeeded(newPaintInvalidationContainer, oldPaintInvalidationRect, oldPositionFromPaintInvalidationContainer, paintInvalidationState);
186 }
187
188 void RenderLayerModelObject::invalidateTreeIfNeeded(const PaintInvalidationState& paintInvalidationState)
189 {
190     // FIXME: SVG should probably also go through this unified paint invalidation system.
191     ASSERT(!needsLayout());
192
193     if (!shouldCheckForPaintInvalidation(paintInvalidationState))
194         return;
195
196     bool establishesNewPaintInvalidationContainer = isPaintInvalidationContainer();
197     const RenderLayerModelObject& newPaintInvalidationContainer = *adjustCompositedContainerForSpecialAncestors(establishesNewPaintInvalidationContainer ? this : &paintInvalidationState.paintInvalidationContainer());
198     ASSERT(&newPaintInvalidationContainer == containerForPaintInvalidation());
199
200     InvalidationReason reason = invalidatePaintIfNeeded(paintInvalidationState, newPaintInvalidationContainer);
201
202     PaintInvalidationState childTreeWalkState(paintInvalidationState, *this, newPaintInvalidationContainer);
203     if (reason == InvalidationLocationChange || reason == InvalidationFull)
204         childTreeWalkState.setForceCheckForPaintInvalidation();
205     RenderObject::invalidateTreeIfNeeded(childTreeWalkState);
206 }
207
208 } // namespace blink
209