.: Heap-use-after-free in WebCore::ScrollingCoordinator::hasVisibleSlowRepaintViewpor...
[WebKit-https.git] / Source / WebCore / 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 "RenderLayerModelObject.h"
27
28 #include "RenderLayer.h"
29 #include "RenderView.h"
30 #include "WebCoreMemoryInstrumentation.h"
31
32 using namespace std;
33
34 namespace WebCore {
35
36 bool RenderLayerModelObject::s_wasFloating = false;
37 bool RenderLayerModelObject::s_hadLayer = false;
38 bool RenderLayerModelObject::s_hadTransform = false;
39 bool RenderLayerModelObject::s_layerWasSelfPainting = false;
40
41 RenderLayerModelObject::RenderLayerModelObject(ContainerNode* node)
42     : RenderObject(node)
43     , m_layer(0)
44 {
45 }
46
47 RenderLayerModelObject::~RenderLayerModelObject()
48 {
49     // Our layer should have been destroyed and cleared by now
50     ASSERT(!hasLayer());
51     ASSERT(!m_layer);
52 }
53
54 void RenderLayerModelObject::destroyLayer()
55 {
56     ASSERT(!hasLayer()); // Callers should have already called setHasLayer(false)
57     ASSERT(m_layer);
58     m_layer->destroy(renderArena());
59     m_layer = 0;
60 }
61
62 void RenderLayerModelObject::ensureLayer()
63 {
64     if (m_layer)
65         return;
66
67     m_layer = new (renderArena()) RenderLayer(this);
68     setHasLayer(true);
69     m_layer->insertOnlyThisLayer();
70 }
71
72 bool RenderLayerModelObject::hasSelfPaintingLayer() const
73 {
74     return m_layer && m_layer->isSelfPaintingLayer();
75 }
76
77 void RenderLayerModelObject::willBeDestroyed()
78 {
79     if (isPositioned()) {
80         // Don't use this->view() because the document's renderView has been set to 0 during destruction.
81         if (Frame* frame = this->frame()) {
82             if (FrameView* frameView = frame->view()) {
83                 if (style()->hasViewportConstrainedPosition())
84                     frameView->removeViewportConstrainedObject(this);
85             }
86         }
87     }
88
89     // RenderObject::willBeDestroyed calls back to destroyLayer() for layer destruction
90     RenderObject::willBeDestroyed();
91 }
92
93 void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
94 {
95     s_wasFloating = isFloating();
96     s_hadLayer = hasLayer();
97     s_hadTransform = hasTransform();
98     if (s_hadLayer)
99         s_layerWasSelfPainting = layer()->isSelfPaintingLayer();
100
101     // If our z-index changes value or our visibility changes,
102     // we need to dirty our stacking context's z-order list.
103     RenderStyle* oldStyle = style();
104     if (oldStyle && newStyle) {
105         if (parent()) {
106             // Do a repaint with the old style first, e.g., for example if we go from
107             // having an outline to not having an outline.
108             if (diff == StyleDifferenceRepaintLayer) {
109                 layer()->repaintIncludingDescendants();
110                 if (!(oldStyle->clip() == newStyle->clip()))
111                     layer()->clearClipRectsIncludingDescendants();
112             } else if (diff == StyleDifferenceRepaint || newStyle->outlineSize() < oldStyle->outlineSize())
113                 repaint();
114         }
115
116         if (diff == StyleDifferenceLayout || diff == StyleDifferenceSimplifiedLayout) {
117             // When a layout hint happens, we go ahead and do a repaint of the layer, since the layer could
118             // end up being destroyed.
119             if (hasLayer()) {
120                 if (oldStyle->position() != newStyle->position()
121                     || oldStyle->zIndex() != newStyle->zIndex()
122                     || oldStyle->hasAutoZIndex() != newStyle->hasAutoZIndex()
123                     || !(oldStyle->clip() == newStyle->clip())
124                     || oldStyle->hasClip() != newStyle->hasClip()
125                     || oldStyle->opacity() != newStyle->opacity()
126                     || oldStyle->transform() != newStyle->transform()
127 #if ENABLE(CSS_FILTERS)
128                     || oldStyle->filter() != newStyle->filter()
129 #endif
130                     )
131                 layer()->repaintIncludingDescendants();
132             } else if (newStyle->hasTransform() || newStyle->opacity() < 1 || newStyle->hasFilter()) {
133                 // If we don't have a layer yet, but we are going to get one because of transform or opacity,
134                 //  then we need to repaint the old position of the object.
135                 repaint();
136             }
137         }
138     }
139
140     RenderObject::styleWillChange(diff, newStyle);
141 }
142
143 void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
144 {
145     RenderObject::styleDidChange(diff, oldStyle);
146     updateFromStyle();
147
148     if (requiresLayer()) {
149         if (!layer() && layerCreationAllowedForSubtree()) {
150             if (s_wasFloating && isFloating())
151                 setChildNeedsLayout(true);
152             ensureLayer();
153             if (parent() && !needsLayout() && containingBlock()) {
154                 layer()->setRepaintStatus(NeedsFullRepaint);
155                 // There is only one layer to update, it is not worth using |cachedOffset| since
156                 // we are not sure the value will be used.
157                 layer()->updateLayerPositions(0);
158             }
159         }
160     } else if (layer() && layer()->parent()) {
161         setHasTransform(false); // Either a transform wasn't specified or the object doesn't support transforms, so just null out the bit.
162         setHasReflection(false);
163         layer()->removeOnlyThisLayer(); // calls destroyLayer() which clears m_layer
164         if (s_wasFloating && isFloating())
165             setChildNeedsLayout(true);
166         if (s_hadTransform)
167             setNeedsLayoutAndPrefWidthsRecalc();
168     }
169
170     if (layer()) {
171         layer()->styleChanged(diff, oldStyle);
172         if (s_hadLayer && layer()->isSelfPaintingLayer() != s_layerWasSelfPainting)
173             setChildNeedsLayout(true);
174     }
175
176     if (FrameView *frameView = view()->frameView()) {
177         bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition();
178         bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
179         if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) {
180             if (newStyleIsViewportConstained && layer())
181                 frameView->addViewportConstrainedObject(this);
182             else
183                 frameView->removeViewportConstrainedObject(this);
184         }
185     }
186 }
187
188 void RenderLayerModelObject::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
189 {
190     MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::Rendering);
191     RenderObject::reportMemoryUsage(memoryObjectInfo);
192     info.addWeakPointer(m_layer);
193 }
194
195 } // namespace WebCore
196