diff --git a/layout/base/nsDisplayList.cpp b/layout/base/nsDisplayList.cpp index f48387f106e..79ff5b003bf 100644 --- a/layout/base/nsDisplayList.cpp +++ b/layout/base/nsDisplayList.cpp @@ -622,83 +622,6 @@ static bool GetApzcTreePrintPref() { return gPrintApzcTree; } -static CSSSize -CalculateRootCompositionSize(FrameMetrics& aMetrics, - bool aIsRootContentDocRootScrollFrame, - nsPresContext* aPresContext, - nsIFrame* aForFrame, nsIFrame* aScrollFrame) -{ - - if (aIsRootContentDocRootScrollFrame) { - return ViewAs(ParentLayerSize(aMetrics.mCompositionBounds.Size()), - PixelCastJustification::ParentLayerToLayerForRootComposition) - / aMetrics.LayersPixelsPerCSSPixel(); - } - LayerSize rootCompositionSize; - nsPresContext* rootPresContext = - aPresContext->GetToplevelContentDocumentPresContext(); - if (!rootPresContext) { - rootPresContext = aPresContext->GetRootPresContext(); - } - nsIPresShell* rootPresShell = nullptr; - if (rootPresContext) { - // See the comments in the code that calculates the root - // composition bounds in RecordFrameMetrics. - // TODO: Reuse that code here. - nsIPresShell* rootPresShell = rootPresContext->PresShell(); - if (nsIFrame* rootFrame = rootPresShell->GetRootFrame()) { - if (nsView* view = rootFrame->GetView()) { - LayoutDeviceToParentLayerScale parentResolution( - rootPresShell->GetCumulativeResolution().width - / rootPresShell->GetResolution().width); - int32_t rootAUPerDevPixel = rootPresContext->AppUnitsPerDevPixel(); - nsRect viewBounds = view->GetBounds(); - LayerSize viewSize = ViewAs( - (LayoutDeviceRect::FromAppUnits(viewBounds, rootAUPerDevPixel) - * parentResolution).Size(), PixelCastJustification::ParentLayerToLayerForRootComposition); - nsIWidget* widget = -#ifdef MOZ_WIDGET_ANDROID - rootFrame->GetNearestWidget(); -#else - view->GetWidget(); -#endif - if (widget) { - nsIntRect widgetBounds; - widget->GetBounds(widgetBounds); - rootCompositionSize = LayerSize(ViewAs(widgetBounds.Size())); -#ifdef MOZ_WIDGET_ANDROID - if (viewSize.height < rootCompositionSize.height) { - rootCompositionSize.height = viewSize.height; - } -#endif - } else { - rootCompositionSize = viewSize; - } - } - } - } else { - nsIWidget* widget = (aScrollFrame ? aScrollFrame : aForFrame)->GetNearestWidget(); - nsIntRect bounds; - widget->GetBounds(bounds); - rootCompositionSize = LayerSize(ViewAs(bounds.Size())); - } - - // Adjust composition size for the size of scroll bars. - nsIFrame* rootRootScrollFrame = rootPresShell ? rootPresShell->GetRootScrollFrame() : nullptr; - nsIScrollableFrame* rootScrollableFrame = nullptr; - if (rootRootScrollFrame) { - rootScrollableFrame = aScrollFrame->GetScrollTargetFrame(); - } - if (rootScrollableFrame && !LookAndFeel::GetInt(LookAndFeel::eIntID_UseOverlayScrollbars)) { - CSSMargin margins = CSSMargin::FromAppUnits(rootScrollableFrame->GetActualScrollbarSizes()); - // Scrollbars are not subject to scaling, so CSS pixels = layer pixels for them. - rootCompositionSize.width -= margins.LeftRight(); - rootCompositionSize.height -= margins.TopBottom(); - } - - return rootCompositionSize / aMetrics.LayersPixelsPerCSSPixel(); -} - static void RecordFrameMetrics(nsIFrame* aForFrame, nsIFrame* aScrollFrame, const nsIFrame* aReferenceFrame, @@ -858,8 +781,8 @@ static void RecordFrameMetrics(nsIFrame* aForFrame, } metrics.SetRootCompositionSize( - CalculateRootCompositionSize(metrics, isRootContentDocRootScrollFrame, - presContext, aForFrame, aScrollFrame)); + nsLayoutUtils::CalculateRootCompositionSize(aScrollFrame ? aScrollFrame : aForFrame, + isRootContentDocRootScrollFrame, metrics)); if (GetApzcTreePrintPref()) { if (nsIContent* content = frameForCompositionBoundsCalculation->GetContent()) { diff --git a/layout/base/nsLayoutUtils.cpp b/layout/base/nsLayoutUtils.cpp index a7f9cc48cd5..6cb60f0e852 100644 --- a/layout/base/nsLayoutUtils.cpp +++ b/layout/base/nsLayoutUtils.cpp @@ -74,6 +74,7 @@ #include "mozilla/gfx/2D.h" #include "gfx2DGlue.h" #include "mozilla/LookAndFeel.h" +#include "UnitTransforms.h" #include "mozilla/Preferences.h" @@ -6216,6 +6217,82 @@ nsLayoutUtils::CalculateCompositionSizeForFrame(nsIFrame* aFrame) return size; } +/* static */ CSSSize +nsLayoutUtils::CalculateRootCompositionSize(nsIFrame* aFrame, + bool aIsRootContentDocRootScrollFrame, + const FrameMetrics& aMetrics) +{ + + if (aIsRootContentDocRootScrollFrame) { + return ViewAs(ParentLayerSize(aMetrics.mCompositionBounds.Size()), + PixelCastJustification::ParentLayerToLayerForRootComposition) + / aMetrics.LayersPixelsPerCSSPixel(); + } + nsPresContext* presContext = aFrame->PresContext(); + LayerSize rootCompositionSize; + nsPresContext* rootPresContext = + presContext->GetToplevelContentDocumentPresContext(); + if (!rootPresContext) { + rootPresContext = presContext->GetRootPresContext(); + } + nsIPresShell* rootPresShell = nullptr; + if (rootPresContext) { + // See the comments in the code that calculates the root + // composition bounds in RecordFrameMetrics. + // TODO: Reuse that code here. + nsIPresShell* rootPresShell = rootPresContext->PresShell(); + if (nsIFrame* rootFrame = rootPresShell->GetRootFrame()) { + if (nsView* view = rootFrame->GetView()) { + LayoutDeviceToParentLayerScale parentResolution( + rootPresShell->GetCumulativeResolution().width + / rootPresShell->GetResolution().width); + int32_t rootAUPerDevPixel = rootPresContext->AppUnitsPerDevPixel(); + nsRect viewBounds = view->GetBounds(); + LayerSize viewSize = ViewAs( + (LayoutDeviceRect::FromAppUnits(viewBounds, rootAUPerDevPixel) + * parentResolution).Size(), PixelCastJustification::ParentLayerToLayerForRootComposition); + nsIWidget* widget = +#ifdef MOZ_WIDGET_ANDROID + rootFrame->GetNearestWidget(); +#else + view->GetWidget(); +#endif + if (widget) { + nsIntRect widgetBounds; + widget->GetBounds(widgetBounds); + rootCompositionSize = LayerSize(ViewAs(widgetBounds.Size())); +#ifdef MOZ_WIDGET_ANDROID + if (viewSize.height < rootCompositionSize.height) { + rootCompositionSize.height = viewSize.height; + } +#endif + } else { + rootCompositionSize = viewSize; + } + } + } + } else { + nsIWidget* widget = aFrame->GetNearestWidget(); + nsIntRect widgetBounds; + widget->GetBounds(widgetBounds); + rootCompositionSize = LayerSize(ViewAs(widgetBounds.Size())); + } + + // Adjust composition size for the size of scroll bars. + nsIFrame* rootRootScrollFrame = rootPresShell ? rootPresShell->GetRootScrollFrame() : nullptr; + nsIScrollableFrame* rootScrollableFrame = nullptr; + if (rootRootScrollFrame) { + rootScrollableFrame = rootRootScrollFrame->GetScrollTargetFrame(); + } + if (rootScrollableFrame && !LookAndFeel::GetInt(LookAndFeel::eIntID_UseOverlayScrollbars)) { + CSSMargin margins = CSSMargin::FromAppUnits(rootScrollableFrame->GetActualScrollbarSizes()); + // Scrollbars are not subject to scaling, so CSS pixels = layer pixels for them. + rootCompositionSize.width -= margins.LeftRight(); + rootCompositionSize.height -= margins.TopBottom(); + } + + return rootCompositionSize / aMetrics.LayersPixelsPerCSSPixel(); +} /* static */ nsRect nsLayoutUtils::CalculateScrollableRectForFrame(nsIScrollableFrame* aScrollableFrame, nsIFrame* aRootFrame) diff --git a/layout/base/nsLayoutUtils.h b/layout/base/nsLayoutUtils.h index f48c42ef659..fbe139ad49d 100644 --- a/layout/base/nsLayoutUtils.h +++ b/layout/base/nsLayoutUtils.h @@ -126,6 +126,7 @@ public: typedef mozilla::layers::FrameMetrics FrameMetrics; typedef FrameMetrics::ViewID ViewID; typedef mozilla::CSSPoint CSSPoint; + typedef mozilla::CSSSize CSSSize; /** * Finds previously assigned ViewID for the given content element, if any. @@ -2100,6 +2101,22 @@ public: static nsSize CalculateCompositionSizeForFrame(nsIFrame* aFrame); + /** + * Calculate the composition size for the root scroll frame of the root + * content document. + * @param aFrame A frame in the root content document (or a descendant of it). + * @param aIsRootContentDocRootScrollFrame Whether aFrame is already the root + * scroll frame of the root content document. In this case we just + * use aFrame's own composition size. + * @param aMetrics A partially populated FrameMetrics for aFrame. Must have at + * least mCompositionBounds, mCumulativeResolution, and + * mDevPixelsPerCSSPixel set. + */ + static CSSSize + CalculateRootCompositionSize(nsIFrame* aFrame, + bool aIsRootContentDocRootScrollFrame, + const FrameMetrics& aMetrics); + /** * Calculate the scrollable rect for a frame. See FrameMetrics.h for * defintion of scrollable rect. aScrollableFrame is the scroll frame to calculate