Title: [181396] trunk
Revision
181396
Author
hy...@apple.com
Date
2015-03-11 11:13:45 -0700 (Wed, 11 Mar 2015)

Log Message

Optimize offsetWidth and offsetHeight to avoid doing layouts.
https://bugs.webkit.org/show_bug.cgi?id=142544

Reviewed by Beth Dakin.

Source/WebCore:

* dom/Document.cpp:
(WebCore::Document::updateLayoutIfDimensionsOutOfDate):
* dom/Document.h:
Added a new method that only updates layout if it determines that the desired dimensions are out
of date.

* dom/Element.cpp:
(WebCore::Element::offsetWidth):
(WebCore::Element::offsetHeight):
Patch offsetWidth and offsetHeight to call the new method rather than updateLayoutIgnorePendingStylesheets.

LayoutTests:

* fast/images/repaint-subrect-grid.html:
Patch this test to use the window.internals update layout method rather than
relying on document.body.offsetWidth.

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (181395 => 181396)


--- trunk/LayoutTests/ChangeLog	2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/LayoutTests/ChangeLog	2015-03-11 18:13:45 UTC (rev 181396)
@@ -1,3 +1,14 @@
+2015-03-11  David Hyatt  <hy...@apple.com>
+
+        Optimize offsetWidth and offsetHeight to avoid doing layouts.
+        https://bugs.webkit.org/show_bug.cgi?id=142544
+
+        Reviewed by Beth Dakin.
+
+        * fast/images/repaint-subrect-grid.html:
+        Patch this test to use the window.internals update layout method rather than
+        relying on document.body.offsetWidth.
+
 2015-03-11  Myles C. Maxfield  <mmaxfi...@apple.com>
 
         [Win] Test gardening after r181260

Modified: trunk/LayoutTests/fast/images/repaint-subrect-grid.html (181395 => 181396)


--- trunk/LayoutTests/fast/images/repaint-subrect-grid.html	2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/LayoutTests/fast/images/repaint-subrect-grid.html	2015-03-11 18:13:45 UTC (rev 181396)
@@ -40,8 +40,9 @@
             ++currentPos;
 
             // Force a style recalc.
-            document.body.offsetWidth;
-
+            if (internals)
+                internals.updateLayoutIgnorePendingStylesheetsAndRunPostLayoutTasks();
+                
             if (window.testRunner)
                 repaintTest();
             else

Modified: trunk/Source/WebCore/ChangeLog (181395 => 181396)


--- trunk/Source/WebCore/ChangeLog	2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/ChangeLog	2015-03-11 18:13:45 UTC (rev 181396)
@@ -1,3 +1,21 @@
+2015-03-11  David Hyatt  <hy...@apple.com>
+
+        Optimize offsetWidth and offsetHeight to avoid doing layouts.
+        https://bugs.webkit.org/show_bug.cgi?id=142544
+
+        Reviewed by Beth Dakin.
+
+        * dom/Document.cpp:
+        (WebCore::Document::updateLayoutIfDimensionsOutOfDate):
+        * dom/Document.h:
+        Added a new method that only updates layout if it determines that the desired dimensions are out
+        of date.
+
+        * dom/Element.cpp:
+        (WebCore::Element::offsetWidth):
+        (WebCore::Element::offsetHeight):
+        Patch offsetWidth and offsetHeight to call the new method rather than updateLayoutIgnorePendingStylesheets.
+
 2015-03-11  Commit Queue  <commit-qu...@webkit.org>
 
         Unreviewed, rolling out r181367.

Modified: trunk/Source/WebCore/dom/Document.cpp (181395 => 181396)


--- trunk/Source/WebCore/dom/Document.cpp	2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/dom/Document.cpp	2015-03-11 18:13:45 UTC (rev 181396)
@@ -1892,6 +1892,103 @@
     return ensureStyleResolver().styleForElement(element, element->parentNode() ? element->parentNode()->computedStyle() : nullptr);
 }
 
+bool Document::updateLayoutIfDimensionsOutOfDate(Element* element, DimensionsCheck dimensionsCheck)
+{
+    ASSERT(isMainThread());
+    
+    // If the stylesheets haven't loaded, just give up and do a full layout ignoring pending stylesheets.
+    if (!haveStylesheetsLoaded()) {
+        updateLayoutIgnorePendingStylesheets();
+        return true;
+    }
+    
+    // Check for re-entrancy and assert (same code that is in updateLayout()).
+    FrameView* frameView = view();
+    if (frameView && frameView->isInLayout()) {
+        // View layout should not be re-entrant.
+        ASSERT_NOT_REACHED();
+        return true;
+    }
+    
+    RenderView::RepaintRegionAccumulator repaintRegionAccumulator(renderView());
+    
+    // Mimic the structure of updateLayout(), but at each step, see if we have been forced into doing a full
+    // layout.
+    bool requireFullLayout = false;
+    if (HTMLFrameOwnerElement* owner = ownerElement()) {
+        if (owner->document().updateLayoutIfDimensionsOutOfDate(owner))
+            requireFullLayout = true;
+    }
+    
+    updateStyleIfNeeded();
+    
+    RenderObject* renderer = element->renderer();
+    if (!renderer || renderer->needsLayout())
+        requireFullLayout = true;
+
+    bool isVertical = renderer && !renderer->isHorizontalWritingMode();
+    bool checkingLogicalWidth = ((dimensionsCheck & WidthDimensionsCheck) && !isVertical) || ((dimensionsCheck & HeightDimensionsCheck) && isVertical);
+    bool checkingLogicalHeight = ((dimensionsCheck & HeightDimensionsCheck) && !isVertical) || ((dimensionsCheck & WidthDimensionsCheck) && !isVertical);
+    bool hasSpecifiedLogicalHeight = renderer && renderer->style().logicalMinHeight() == Length(0, Fixed) && renderer->style().logicalHeight().isFixed() && renderer->style().logicalMaxHeight().isAuto();
+    
+    if (!requireFullLayout) {
+        RenderBox* previousBox = nullptr;
+        RenderBox* currentBox = nullptr;
+        
+        // Check our containing block chain. If anything in the chain needs a layout, then require a full layout.
+        for (RenderObject* currRenderer = element->renderer(); currRenderer && !currRenderer->isRenderView(); currRenderer = currRenderer->container()) {
+            
+            // Require the entire container chain to be boxes.
+            if (!is<RenderBox>(currRenderer)) {
+                requireFullLayout = true;
+                break;
+            }
+            
+            previousBox = currentBox;
+            currentBox = downcast<RenderBox>(currRenderer);
+            
+            // If a box needs layout for itself or if a box has changed children and sizes its width to
+            // its content, then require a full layout.
+            if (currentBox->selfNeedsLayout() ||
+                (checkingLogicalWidth && currRenderer->needsLayout() && currentBox->sizesLogicalWidthToFitContent(MainOrPreferredSize))) {
+                requireFullLayout = true;
+                break;
+            }
+            
+            // If a block contains floats and the child's height isn't specified, then
+            // give up also, since our height could end up being influenced by the floats.
+            if (checkingLogicalHeight && !hasSpecifiedLogicalHeight && currentBox->isRenderBlockFlow()) {
+                RenderBlockFlow* currentBlockFlow = downcast<RenderBlockFlow>(currentBox);
+                if (currentBlockFlow->containsFloats() && previousBox && !previousBox->isFloatingOrOutOfFlowPositioned()) {
+                    requireFullLayout = true;
+                    break;
+                }
+            }
+            
+            if (!currentBox->isRenderBlockFlow() || currentBox->isInline() || currentBox->isOutOfFlowPositioned() || currentBox->flowThreadContainingBlock() || currentBox->isWritingModeRoot()) {
+                // FIXME: For now require only block-level non-positioned
+                // block flows all the way back to the root. This limits the optimization
+                // for now, and we'll expand it in future patches to apply to more and more scenarios.
+                // Disallow regions/columns from having the optimization.
+                // Give up if the writing mode changes at all in the containing block chain.
+                requireFullLayout = true;
+                break;
+            }
+            
+            if (currRenderer == frameView->layoutRoot())
+                break;
+        }
+    }
+    
+    StackStats::LayoutCheckPoint layoutCheckPoint;
+
+    // Only do a layout if changes have occurred that make it necessary.      
+    if (requireFullLayout && frameView && renderView() && (frameView->layoutPending() || renderView()->needsLayout()))
+        frameView->layout();
+    
+    return requireFullLayout;
+}
+
 bool Document::isPageBoxVisible(int pageIndex)
 {
     Ref<RenderStyle> pageStyle(ensureStyleResolver().styleForPage(pageIndex));

Modified: trunk/Source/WebCore/dom/Document.h (181395 => 181396)


--- trunk/Source/WebCore/dom/Document.h	2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/dom/Document.h	2015-03-11 18:13:45 UTC (rev 181396)
@@ -258,6 +258,8 @@
     LimitedQuirksMode = 1 << 2
 };
 
+enum DimensionsCheck { WidthDimensionsCheck = 0x1, HeightDimensionsCheck = 0x2, AllDimensionsCheck = 0x3 };
+
 class Document : public ContainerNode, public TreeScope, public ScriptExecutionContext, public FontSelectorClient {
 public:
     static Ref<Document> create(Frame* frame, const URL& url)
@@ -564,7 +566,9 @@
 
     bool renderTreeBeingDestroyed() const { return m_renderTreeBeingDestroyed; }
     bool hasLivingRenderTree() const { return renderView() && !renderTreeBeingDestroyed(); }
-
+    
+    bool updateLayoutIfDimensionsOutOfDate(Element*, DimensionsCheck = AllDimensionsCheck);
+    
     AXObjectCache* existingAXObjectCache() const;
     WEBCORE_EXPORT AXObjectCache* axObjectCache() const;
     void clearAXObjectCache();

Modified: trunk/Source/WebCore/dom/Element.cpp (181395 => 181396)


--- trunk/Source/WebCore/dom/Element.cpp	2015-03-11 18:08:39 UTC (rev 181395)
+++ trunk/Source/WebCore/dom/Element.cpp	2015-03-11 18:13:45 UTC (rev 181396)
@@ -723,7 +723,7 @@
 
 double Element::offsetWidth()
 {
-    document().updateLayoutIgnorePendingStylesheets();
+    document().updateLayoutIfDimensionsOutOfDate(this, WidthDimensionsCheck);
     if (RenderBoxModelObject* renderer = renderBoxModelObject()) {
         LayoutUnit offsetWidth = subpixelMetricsEnabled(renderer->document()) ? renderer->offsetWidth() : LayoutUnit(renderer->pixelSnappedOffsetWidth());
         return convertToNonSubpixelValueIfNeeded(adjustLayoutUnitForAbsoluteZoom(offsetWidth, *renderer).toDouble(), renderer->document());
@@ -733,7 +733,7 @@
 
 double Element::offsetHeight()
 {
-    document().updateLayoutIgnorePendingStylesheets();
+    document().updateLayoutIfDimensionsOutOfDate(this, HeightDimensionsCheck);
     if (RenderBoxModelObject* renderer = renderBoxModelObject()) {
         LayoutUnit offsetHeight = subpixelMetricsEnabled(renderer->document()) ? renderer->offsetHeight() : LayoutUnit(renderer->pixelSnappedOffsetHeight());
         return convertToNonSubpixelValueIfNeeded(adjustLayoutUnitForAbsoluteZoom(offsetHeight, *renderer).toDouble(), renderer->document());
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to