[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[WebKit-https.git] / Source / WebCore / rendering / RenderLayerModelObject.cpp
index f48b4d6..128c96d 100644 (file)
 #include "RenderLayerModelObject.h"
 
 #include "RenderLayer.h"
+#include "RenderLayerBacking.h"
+#include "RenderLayerCompositor.h"
 #include "RenderView.h"
-
-using namespace std;
+#include "Settings.h"
+#include "StyleScrollSnapPoints.h"
+#include <wtf/IsoMallocInlines.h>
 
 namespace WebCore {
 
+WTF_MAKE_ISO_ALLOCATED_IMPL(RenderLayerModelObject);
+
 bool RenderLayerModelObject::s_wasFloating = false;
 bool RenderLayerModelObject::s_hadLayer = false;
 bool RenderLayerModelObject::s_hadTransform = false;
 bool RenderLayerModelObject::s_layerWasSelfPainting = false;
 
-RenderLayerModelObject::RenderLayerModelObject(Element* element, unsigned baseTypeFlags)
-    : RenderElement(element, baseTypeFlags | RenderLayerModelObjectFlag)
-    , m_layer(0)
-{
-}
+typedef WTF::HashMap<const RenderLayerModelObject*, RepaintLayoutRects> RepaintLayoutRectsMap;
+static RepaintLayoutRectsMap* gRepaintLayoutRectsMap = nullptr;
 
-RenderLayerModelObject::~RenderLayerModelObject()
+RepaintLayoutRects::RepaintLayoutRects(const RenderLayerModelObject& renderer, const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap)
+    : m_repaintRect(renderer.clippedOverflowRectForRepaint(repaintContainer))
+    , m_outlineBox(renderer.outlineBoundsForRepaint(repaintContainer, geometryMap))
 {
-    // Our layer should have been destroyed and cleared by now
-    ASSERT(!hasLayer());
-    ASSERT(!m_layer);
 }
 
-void RenderLayerModelObject::destroyLayer()
+RenderLayerModelObject::RenderLayerModelObject(Element& element, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
+    : RenderElement(element, WTFMove(style), baseTypeFlags | RenderLayerModelObjectFlag)
 {
-    ASSERT(!hasLayer()); // Callers should have already called setHasLayer(false)
-    ASSERT(m_layer);
-    m_layer->destroy(renderArena());
-    m_layer = 0;
 }
 
-void RenderLayerModelObject::ensureLayer()
+RenderLayerModelObject::RenderLayerModelObject(Document& document, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
+    : RenderElement(document, WTFMove(style), baseTypeFlags | RenderLayerModelObjectFlag)
 {
-    if (m_layer)
-        return;
-
-    m_layer = new (renderArena()) RenderLayer(*this);
-    setHasLayer(true);
-    m_layer->insertOnlyThisLayer();
 }
 
-bool RenderLayerModelObject::hasSelfPaintingLayer() const
+RenderLayerModelObject::~RenderLayerModelObject()
 {
-    return m_layer && m_layer->isSelfPaintingLayer();
+    // Do not add any code here. Add it to willBeDestroyed() instead.
 }
 
 void RenderLayerModelObject::willBeDestroyed()
 {
     if (isPositioned()) {
-        if (style()->hasViewportConstrainedPosition())
+        if (style().hasViewportConstrainedPosition())
             view().frameView().removeViewportConstrainedObject(this);
     }
 
-    // RenderObject::willBeDestroyed calls back to destroyLayer() for layer destruction
+    if (hasLayer()) {
+        setHasLayer(false);
+        destroyLayer();
+    }
+
     RenderElement::willBeDestroyed();
+    
+    clearRepaintLayoutRects();
+}
+
+void RenderLayerModelObject::destroyLayer()
+{
+    ASSERT(!hasLayer());
+    ASSERT(m_layer);
+    if (m_layer->isSelfPaintingLayer())
+        clearRepaintLayoutRects();
+    m_layer = nullptr;
 }
 
-void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
+void RenderLayerModelObject::createLayer()
+{
+    ASSERT(!m_layer);
+    m_layer = makeUnique<RenderLayer>(*this);
+    setHasLayer(true);
+    m_layer->insertOnlyThisLayer();
+}
+
+bool RenderLayerModelObject::hasSelfPaintingLayer() const
+{
+    return m_layer && m_layer->isSelfPaintingLayer();
+}
+
+void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
 {
     s_wasFloating = isFloating();
     s_hadLayer = hasLayer();
@@ -94,36 +115,33 @@ void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderS
 
     // If our z-index changes value or our visibility changes,
     // we need to dirty our stacking context's z-order list.
-    RenderStyle* oldStyle = style();
-    if (oldStyle && newStyle) {
+    const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
+    if (oldStyle) {
         if (parent()) {
             // Do a repaint with the old style first, e.g., for example if we go from
             // having an outline to not having an outline.
-            if (diff == StyleDifferenceRepaintLayer) {
+            if (diff == StyleDifference::RepaintLayer) {
                 layer()->repaintIncludingDescendants();
-                if (!(oldStyle->clip() == newStyle->clip()))
+                if (!(oldStyle->clip() == newStyle.clip()))
                     layer()->clearClipRectsIncludingDescendants();
-            } else if (diff == StyleDifferenceRepaint || newStyle->outlineSize() < oldStyle->outlineSize())
+            } else if (diff == StyleDifference::Repaint || newStyle.outlineSize() < oldStyle->outlineSize())
                 repaint();
         }
 
-        if (diff == StyleDifferenceLayout || diff == StyleDifferenceSimplifiedLayout) {
-            // When a layout hint happens, we go ahead and do a repaint of the layer, since the layer could
-            // end up being destroyed.
+        if (diff == StyleDifference::Layout || diff == StyleDifference::SimplifiedLayout) {
+            // When a layout hint happens, we do a repaint of the layer, since the layer could end up being destroyed.
             if (hasLayer()) {
-                if (oldStyle->position() != newStyle->position()
-                    || oldStyle->zIndex() != newStyle->zIndex()
-                    || oldStyle->hasAutoZIndex() != newStyle->hasAutoZIndex()
-                    || !(oldStyle->clip() == newStyle->clip())
-                    || oldStyle->hasClip() != newStyle->hasClip()
-                    || oldStyle->opacity() != newStyle->opacity()
-                    || oldStyle->transform() != newStyle->transform()
-#if ENABLE(CSS_FILTERS)
-                    || oldStyle->filter() != newStyle->filter()
-#endif
+                if (oldStyle->position() != newStyle.position()
+                    || oldStyle->zIndex() != newStyle.zIndex()
+                    || oldStyle->hasAutoZIndex() != newStyle.hasAutoZIndex()
+                    || !(oldStyle->clip() == newStyle.clip())
+                    || oldStyle->hasClip() != newStyle.hasClip()
+                    || oldStyle->opacity() != newStyle.opacity()
+                    || oldStyle->transform() != newStyle.transform()
+                    || oldStyle->filter() != newStyle.filter()
                     )
                 layer()->repaintIncludingDescendants();
-            } else if (newStyle->hasTransform() || newStyle->opacity() < 1 || newStyle->hasFilter()) {
+            } else if (newStyle.hasTransform() || newStyle.opacity() < 1 || newStyle.hasFilter() || newStyle.hasBackdropFilter()) {
                 // If we don't have a layer yet, but we are going to get one because of transform or opacity,
                 //  then we need to repaint the old position of the object.
                 repaint();
@@ -134,6 +152,13 @@ void RenderLayerModelObject::styleWillChange(StyleDifference diff, const RenderS
     RenderElement::styleWillChange(diff, newStyle);
 }
 
+#if ENABLE(CSS_SCROLL_SNAP)
+static bool scrollSnapContainerRequiresUpdateForStyleUpdate(const RenderStyle& oldStyle, const RenderStyle& newStyle)
+{
+    return oldStyle.scrollSnapPort() != newStyle.scrollSnapPort();
+}
+#endif
+
 void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
 {
     RenderElement::styleDidChange(diff, oldStyle);
@@ -143,17 +168,22 @@ void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderSt
         if (!layer() && layerCreationAllowedForSubtree()) {
             if (s_wasFloating && isFloating())
                 setChildNeedsLayout();
-            ensureLayer();
+            createLayer();
             if (parent() && !needsLayout() && containingBlock()) {
                 layer()->setRepaintStatus(NeedsFullRepaint);
-                // There is only one layer to update, it is not worth using |cachedOffset| since
-                // we are not sure the value will be used.
-                layer()->updateLayerPositions(0);
+                layer()->updateLayerPositions();
             }
         }
     } else if (layer() && layer()->parent()) {
-        setHasTransform(false); // Either a transform wasn't specified or the object doesn't support transforms, so just null out the bit.
+#if ENABLE(CSS_COMPOSITING)
+        if (oldStyle->hasBlendMode())
+            layer()->parent()->dirtyAncestorChainHasBlendingDescendants();
+#endif
+        setHasTransformRelatedProperty(false); // All transform-related propeties force layers, so we know we don't have one or the object doesn't support them.
         setHasReflection(false);
+        // Repaint the about to be destroyed self-painting layer when style change also triggers repaint.
+        if (layer()->isSelfPaintingLayer() && layer()->repaintStatus() == NeedsFullRepaint && hasRepaintLayoutRects())
+            repaintUsingContainer(containerForRepaint(), repaintLayoutRects().m_repaintRect);
         layer()->removeOnlyThisLayer(); // calls destroyLayer() which clears m_layer
         if (s_wasFloating && isFloating())
             setChildNeedsLayout();
@@ -167,14 +197,147 @@ void RenderLayerModelObject::styleDidChange(StyleDifference diff, const RenderSt
             setChildNeedsLayout();
     }
 
-    bool newStyleIsViewportConstained = style()->hasViewportConstrainedPosition();
+    bool newStyleIsViewportConstrained = style().hasViewportConstrainedPosition();
     bool oldStyleIsViewportConstrained = oldStyle && oldStyle->hasViewportConstrainedPosition();
-    if (newStyleIsViewportConstained != oldStyleIsViewportConstrained) {
-        if (newStyleIsViewportConstained && layer())
+    if (newStyleIsViewportConstrained != oldStyleIsViewportConstrained) {
+        if (newStyleIsViewportConstrained && layer())
             view().frameView().addViewportConstrainedObject(this);
         else
             view().frameView().removeViewportConstrainedObject(this);
     }
+
+#if ENABLE(CSS_SCROLL_SNAP)
+    const RenderStyle& newStyle = style();
+    if (oldStyle && scrollSnapContainerRequiresUpdateForStyleUpdate(*oldStyle, newStyle)) {
+        if (RenderLayer* renderLayer = layer()) {
+            renderLayer->updateSnapOffsets();
+            renderLayer->updateScrollSnapState();
+        } else if (isBody() || isDocumentElementRenderer()) {
+            FrameView& frameView = view().frameView();
+            frameView.updateSnapOffsets();
+            frameView.updateScrollSnapState();
+            frameView.updateScrollingCoordinatorScrollSnapProperties();
+        }
+    }
+    if (oldStyle && oldStyle->scrollSnapArea() != newStyle.scrollSnapArea()) {
+        auto* scrollSnapBox = enclosingScrollableContainerForSnapping();
+        if (scrollSnapBox && scrollSnapBox->layer()) {
+            const RenderStyle& style = scrollSnapBox->style();
+            if (style.scrollSnapType().strictness != ScrollSnapStrictness::None) {
+                scrollSnapBox->layer()->updateSnapOffsets();
+                scrollSnapBox->layer()->updateScrollSnapState();
+                if (scrollSnapBox->isBody() || scrollSnapBox->isDocumentElementRenderer())
+                    scrollSnapBox->view().frameView().updateScrollingCoordinatorScrollSnapProperties();
+            }
+        }
+    }
+#endif
+}
+
+bool RenderLayerModelObject::shouldPlaceBlockDirectionScrollbarOnLeft() const
+{
+// RTL Scrollbars require some system support, and this system support does not exist on certain versions of OS X. iOS uses a separate mechanism.
+#if PLATFORM(IOS_FAMILY)
+    return false;
+#else
+    switch (settings().userInterfaceDirectionPolicy()) {
+    case UserInterfaceDirectionPolicy::Content:
+        return style().shouldPlaceBlockDirectionScrollbarOnLeft();
+    case UserInterfaceDirectionPolicy::System:
+        return settings().systemLayoutDirection() == TextDirection::RTL;
+    }
+    ASSERT_NOT_REACHED();
+    return style().shouldPlaceBlockDirectionScrollbarOnLeft();
+#endif
+}
+
+bool RenderLayerModelObject::hasRepaintLayoutRects() const
+{
+    return gRepaintLayoutRectsMap && gRepaintLayoutRectsMap->contains(this);
+}
+
+void RenderLayerModelObject::setRepaintLayoutRects(const RepaintLayoutRects& rects)
+{
+    if (!gRepaintLayoutRectsMap)
+        gRepaintLayoutRectsMap = new RepaintLayoutRectsMap();
+    gRepaintLayoutRectsMap->set(this, rects);
+}
+
+void RenderLayerModelObject::clearRepaintLayoutRects()
+{
+    if (gRepaintLayoutRectsMap)
+        gRepaintLayoutRectsMap->remove(this);
+}
+
+RepaintLayoutRects RenderLayerModelObject::repaintLayoutRects() const
+{
+    if (!hasRepaintLayoutRects())
+        return RepaintLayoutRects();
+    return gRepaintLayoutRectsMap->get(this);
+}
+
+void RenderLayerModelObject::computeRepaintLayoutRects(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap)
+{
+    if (!m_layer || !m_layer->isSelfPaintingLayer())
+        clearRepaintLayoutRects();
+    else
+        setRepaintLayoutRects(RepaintLayoutRects(*this, repaintContainer, geometryMap));
+}
+
+bool RenderLayerModelObject::startTransition(double timeOffset, CSSPropertyID propertyId, const RenderStyle* fromStyle, const RenderStyle* toStyle)
+{
+    if (!layer() || !layer()->backing())
+        return false;
+    return layer()->backing()->startTransition(timeOffset, propertyId, fromStyle, toStyle);
+}
+
+void RenderLayerModelObject::transitionPaused(double timeOffset, CSSPropertyID propertyId)
+{
+    if (!layer() || !layer()->backing())
+        return;
+    layer()->backing()->transitionPaused(timeOffset, propertyId);
+}
+
+void RenderLayerModelObject::transitionFinished(CSSPropertyID propertyId)
+{
+    if (!layer() || !layer()->backing())
+        return;
+    layer()->backing()->transitionFinished(propertyId);
+}
+
+bool RenderLayerModelObject::startAnimation(double timeOffset, const Animation& animation, const KeyframeList& keyframes)
+{
+    if (!layer() || !layer()->backing())
+        return false;
+    return layer()->backing()->startAnimation(timeOffset, animation, keyframes);
+}
+
+void RenderLayerModelObject::animationPaused(double timeOffset, const String& name)
+{
+    if (!layer() || !layer()->backing())
+        return;
+    layer()->backing()->animationPaused(timeOffset, name);
+}
+
+void RenderLayerModelObject::animationSeeked(double timeOffset, const String& name)
+{
+    if (!layer() || !layer()->backing())
+        return;
+    layer()->backing()->animationSeeked(timeOffset, name);
+}
+
+void RenderLayerModelObject::animationFinished(const String& name)
+{
+    if (!layer() || !layer()->backing())
+        return;
+    layer()->backing()->animationFinished(name);
+}
+
+void RenderLayerModelObject::suspendAnimations(MonotonicTime time)
+{
+    if (!layer() || !layer()->backing())
+        return;
+    layer()->backing()->suspendAnimations(time);
 }
 
 } // namespace WebCore