Implement the updated port/area-based Scroll Snap Module Level 1 Spec
[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 "RenderLayerCompositor.h"
30 #include "RenderView.h"
31 #include "Settings.h"
32 #include "StyleScrollSnapPoints.h"
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(Element& element, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
42     : RenderElement(element, WTFMove(style), baseTypeFlags | RenderLayerModelObjectFlag)
43 {
44 }
45
46 RenderLayerModelObject::RenderLayerModelObject(Document& document, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
47     : RenderElement(document, WTFMove(style), baseTypeFlags | RenderLayerModelObjectFlag)
48 {
49 }
50
51 RenderLayerModelObject::~RenderLayerModelObject()
52 {
53     if (isPositioned()) {
54         if (style().hasViewportConstrainedPosition())
55             view().frameView().removeViewportConstrainedObject(this);
56     }
57
58     // Our layer should have been destroyed and cleared by now
59     ASSERT(!hasLayer());
60     ASSERT(!m_layer);
61 }
62
63 void RenderLayerModelObject::destroyLayer()
64 {
65     ASSERT(!hasLayer()); // Callers should have already called setHasLayer(false)
66     ASSERT(m_layer);
67     m_layer = nullptr;
68 }
69
70 void RenderLayerModelObject::createLayer()
71 {
72     ASSERT(!m_layer);
73     m_layer = std::make_unique<RenderLayer>(*this);
74     setHasLayer(true);
75     m_layer->insertOnlyThisLayer();
76 }
77
78 bool RenderLayerModelObject::hasSelfPaintingLayer() const
79 {
80     return m_layer && m_layer->isSelfPaintingLayer();
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     const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
94     if (oldStyle) {
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 do a repaint of the layer, since the layer could end up being destroyed.
108             if (hasLayer()) {
109                 if (oldStyle->position() != newStyle.position()
110                     || oldStyle->zIndex() != newStyle.zIndex()
111                     || oldStyle->hasAutoZIndex() != newStyle.hasAutoZIndex()
112                     || !(oldStyle->clip() == newStyle.clip())
113                     || oldStyle->hasClip() != newStyle.hasClip()
114                     || oldStyle->opacity() != newStyle.opacity()
115                     || oldStyle->transform() != newStyle.transform()
116                     || oldStyle->filter() != newStyle.filter()
117                     )
118                 layer()->repaintIncludingDescendants();
119             } else if (newStyle.hasTransform() || newStyle.opacity() < 1 || newStyle.hasFilter() || newStyle.hasBackdropFilter()) {
120                 // If we don't have a layer yet, but we are going to get one because of transform or opacity,
121                 //  then we need to repaint the old position of the object.
122                 repaint();
123             }
124         }
125     }
126
127     RenderElement::styleWillChange(diff, newStyle);
128 }
129
130 #if ENABLE(CSS_SCROLL_SNAP)
131 static bool scrollSnapContainerRequiresUpdateForStyleUpdate(const RenderStyle& oldStyle, const RenderStyle& newStyle)
132 {
133     return oldStyle.scrollSnapPort() != newStyle.scrollSnapPort();
134 }
135 #endif
136
137 void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
138 {
139     RenderElement::styleDidChange(diff, oldStyle);
140     updateFromStyle();
141
142     if (requiresLayer()) {
143         if (!layer() && layerCreationAllowedForSubtree()) {
144             if (s_wasFloating && isFloating())
145                 setChildNeedsLayout();
146             createLayer();
147             if (parent() && !needsLayout() && containingBlock()) {
148                 layer()->setRepaintStatus(NeedsFullRepaint);
149                 // There is only one layer to update, it is not worth using |cachedOffset| since
150                 // we are not sure the value will be used.
151                 layer()->updateLayerPositions(0);
152             }
153         }
154     } else if (layer() && layer()->parent()) {
155 #if ENABLE(CSS_COMPOSITING)
156         if (oldStyle->hasBlendMode())
157             layer()->parent()->dirtyAncestorChainHasBlendingDescendants();
158 #endif
159         setHasTransformRelatedProperty(false); // All transform-related propeties force layers, so we know we don't have one or the object doesn't support them.
160         setHasReflection(false);
161         // Repaint the about to be destroyed self-painting layer when style change also triggers repaint.
162         if (layer()->isSelfPaintingLayer() && layer()->repaintStatus() == NeedsFullRepaint && layer()->hasComputedRepaintRect())
163             repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
164         layer()->removeOnlyThisLayer(); // calls destroyLayer() which clears m_layer
165         if (s_wasFloating && isFloating())
166             setChildNeedsLayout();
167         if (s_hadTransform)
168             setNeedsLayoutAndPrefWidthsRecalc();
169     }
170
171     if (layer()) {
172         layer()->styleChanged(diff, oldStyle);
173         if (s_hadLayer && layer()->isSelfPaintingLayer() != s_layerWasSelfPainting)
174             setChildNeedsLayout();
175     }
176
177     bool newStyleIsViewportConstrained = style().hasViewportConstrainedPosition();
178     bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
179     if (newStyleIsViewportConstrained != oldStyleIsViewportConstrained) {
180         if (newStyleIsViewportConstrained && layer())
181             view().frameView().addViewportConstrainedObject(this);
182         else
183             view().frameView().removeViewportConstrainedObject(this);
184     }
185
186 #if ENABLE(CSS_SCROLL_SNAP)
187     const RenderStyle& newStyle = style();
188     if (oldStyle && scrollSnapContainerRequiresUpdateForStyleUpdate(*oldStyle, newStyle)) {
189         if (RenderLayer* renderLayer = layer()) {
190             renderLayer->updateSnapOffsets();
191             renderLayer->updateScrollSnapState();
192         } else if (isBody() || isDocumentElementRenderer()) {
193             FrameView& frameView = view().frameView();
194             frameView.updateSnapOffsets();
195             frameView.updateScrollSnapState();
196             frameView.updateScrollingCoordinatorScrollSnapProperties();
197         }
198     }
199     if (oldStyle && oldStyle->scrollSnapArea() != newStyle.scrollSnapArea()) {
200         const RenderBox* scrollSnapBox = enclosingBox().findEnclosingScrollableContainer();
201         if (scrollSnapBox && scrollSnapBox->layer()) {
202             const RenderStyle& style = scrollSnapBox->style();
203             if (style.scrollSnapType().strictness != ScrollSnapStrictness::None) {
204                 scrollSnapBox->layer()->updateSnapOffsets();
205                 scrollSnapBox->layer()->updateScrollSnapState();
206                 if (scrollSnapBox->isBody() || scrollSnapBox->isDocumentElementRenderer())
207                     scrollSnapBox->view().frameView().updateScrollingCoordinatorScrollSnapProperties();
208             }
209         }
210     }
211 #endif
212 }
213
214 bool RenderLayerModelObject::shouldPlaceBlockDirectionScrollbarOnLeft() const
215 {
216 // RTL Scrollbars require some system support, and this system support does not exist on certain versions of OS X. iOS uses a separate mechanism.
217 #if PLATFORM(IOS) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200)
218     return false;
219 #else
220     switch (frame().settings().userInterfaceDirectionPolicy()) {
221     case UserInterfaceDirectionPolicy::Content:
222         return style().shouldPlaceBlockDirectionScrollbarOnLeft();
223     case UserInterfaceDirectionPolicy::System:
224         return frame().settings().systemLayoutDirection() == RTL;
225     }
226     ASSERT_NOT_REACHED();
227     return style().shouldPlaceBlockDirectionScrollbarOnLeft();
228 #endif
229 }
230
231 } // namespace WebCore
232