d4af219a6b90275f8e762e33ca0aaeb303c55d80
[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     // RenderObject::willBeDestroyed calls back to destroyLayer() for layer destruction
80     RenderObject::willBeDestroyed();
81 }
82
83 void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
84 {
85     s_wasFloating = isFloating();
86     s_hadLayer = hasLayer();
87     s_hadTransform = hasTransform();
88     if (s_hadLayer)
89         s_layerWasSelfPainting = layer()->isSelfPaintingLayer();
90
91     // If our z-index changes value or our visibility changes,
92     // we need to dirty our stacking context's z-order list.
93     RenderStyle* oldStyle = style();
94     if (oldStyle && newStyle) {
95         if (parent()) {
96             // Do a repaint with the old style first, e.g., for example if we go from
97             // having an outline to not having an outline.
98             if (diff == StyleDifferenceRepaintLayer) {
99                 layer()->repaintIncludingDescendants();
100                 if (!(oldStyle->clip() == newStyle->clip()))
101                     layer()->clearClipRectsIncludingDescendants();
102             } else if (diff == StyleDifferenceRepaint || newStyle->outlineSize() < oldStyle->outlineSize())
103                 repaint();
104         }
105
106         if (diff == StyleDifferenceLayout || diff == StyleDifferenceSimplifiedLayout) {
107             // When a layout hint happens, we go ahead and do a repaint of the layer, since the layer could
108             // end up being destroyed.
109             if (hasLayer()) {
110                 if (oldStyle->position() != newStyle->position()
111                     || oldStyle->zIndex() != newStyle->zIndex()
112                     || oldStyle->hasAutoZIndex() != newStyle->hasAutoZIndex()
113                     || !(oldStyle->clip() == newStyle->clip())
114                     || oldStyle->hasClip() != newStyle->hasClip()
115                     || oldStyle->opacity() != newStyle->opacity()
116                     || oldStyle->transform() != newStyle->transform()
117 #if ENABLE(CSS_FILTERS)
118                     || oldStyle->filter() != newStyle->filter()
119 #endif
120                     )
121                 layer()->repaintIncludingDescendants();
122             } else if (newStyle->hasTransform() || newStyle->opacity() < 1 || newStyle->hasFilter()) {
123                 // If we don't have a layer yet, but we are going to get one because of transform or opacity,
124                 //  then we need to repaint the old position of the object.
125                 repaint();
126             }
127         }
128     }
129
130     RenderObject::styleWillChange(diff, newStyle);
131 }
132
133 void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
134 {
135     RenderObject::styleDidChange(diff, oldStyle);
136     updateFromStyle();
137
138     if (requiresLayer()) {
139         if (!layer() && layerCreationAllowedForSubtree()) {
140             if (s_wasFloating && isFloating())
141                 setChildNeedsLayout(true);
142             ensureLayer();
143             if (parent() && !needsLayout() && containingBlock()) {
144                 layer()->setRepaintStatus(NeedsFullRepaint);
145                 // There is only one layer to update, it is not worth using |cachedOffset| since
146                 // we are not sure the value will be used.
147                 layer()->updateLayerPositions(0);
148             }
149         }
150     } else if (layer() && layer()->parent()) {
151         setHasTransform(false); // Either a transform wasn't specified or the object doesn't support transforms, so just null out the bit.
152         setHasReflection(false);
153         layer()->removeOnlyThisLayer(); // calls destroyLayer() which clears m_layer
154         if (s_wasFloating && isFloating())
155             setChildNeedsLayout(true);
156         if (s_hadTransform)
157             setNeedsLayoutAndPrefWidthsRecalc();
158     }
159
160     if (layer()) {
161         layer()->styleChanged(diff, oldStyle);
162         if (s_hadLayer && layer()->isSelfPaintingLayer() != s_layerWasSelfPainting)
163             setChildNeedsLayout(true);
164     }
165
166     if (FrameView *frameView = view()->frameView()) {
167         bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition();
168         bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
169         if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) {
170             if (newStyleIsViewportConstained && layer())
171                 frameView->addViewportConstrainedObject(this);
172             else
173                 frameView->removeViewportConstrainedObject(this);
174         }
175     }
176 }
177
178 void RenderLayerModelObject::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
179 {
180     MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::Rendering);
181     RenderObject::reportMemoryUsage(memoryObjectInfo);
182     info.addWeakPointer(m_layer);
183 }
184
185 } // namespace WebCore
186