Bug 1509552 - MobileViewportManager::UpdateResolution cleanup, part 1. r=kats
authorBotond Ballo <botond@mozilla.com>
Fri, 30 Nov 2018 01:01:13 +0000
changeset 508120 3cdb29bf81f0e5074e3f4708158333c285299a59
parent 508119 aeb00b8974a472794d513c5884c38a96397334da
child 508121 8085138baf577e8f849155ab743a9376e5c9e2cd
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskats
bugs1509552
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1509552 - MobileViewportManager::UpdateResolution cleanup, part 1. r=kats * Introduce helpers for converting between zoom and resolution * Perform calculations in terms of zoom rather than resolution Differential Revision: https://phabricator.services.mozilla.com/D13312
layout/base/MobileViewportManager.cpp
layout/base/MobileViewportManager.h
--- a/layout/base/MobileViewportManager.cpp
+++ b/layout/base/MobileViewportManager.cpp
@@ -202,81 +202,99 @@ MobileViewportManager::ClampZoom(const C
   }
   if (zoom > aViewportInfo.GetMaxZoom()) {
     zoom = aViewportInfo.GetMaxZoom();
     MVM_LOG("%p: Clamped to %f\n", this, zoom.scale);
   }
   return zoom;
 }
 
-LayoutDeviceToLayerScale
-MobileViewportManager::ScaleResolutionWithDisplayWidth(const LayoutDeviceToLayerScale& aRes,
-                                                       const float& aDisplayWidthChangeRatio,
-                                                       const CSSSize& aNewViewport,
-                                                       const CSSSize& aOldViewport)
+CSSToScreenScale
+MobileViewportManager::ScaleZoomWithDisplayWidth(const CSSToScreenScale& aZoom,
+                                                 const float& aDisplayWidthChangeRatio,
+                                                 const CSSSize& aNewViewport,
+                                                 const CSSSize& aOldViewport)
 {
   float cssViewportChangeRatio = (aOldViewport.width == 0)
      ? 1.0f : aNewViewport.width / aOldViewport.width;
-  LayoutDeviceToLayerScale newRes(aRes.scale * aDisplayWidthChangeRatio
+  CSSToScreenScale newZoom(aZoom.scale * aDisplayWidthChangeRatio
     / cssViewportChangeRatio);
-  MVM_LOG("%p: Old resolution was %f, changed by %f/%f to %f\n", this, aRes.scale,
-    aDisplayWidthChangeRatio, cssViewportChangeRatio, newRes.scale);
-  return newRes;
+  MVM_LOG("%p: Old zoom was %f, changed by %f/%f to %f\n", this, aZoom.scale,
+    aDisplayWidthChangeRatio, cssViewportChangeRatio, newZoom.scale);
+  return newZoom;
+}
+
+static CSSToScreenScale
+ResolutionToZoom(LayoutDeviceToLayerScale aResolution,
+                 CSSToLayoutDeviceScale aCssToDev)
+{
+  return ViewTargetAs<ScreenPixel>(aCssToDev * aResolution / ParentLayerToLayerScale(1),
+    PixelCastJustification::ScreenIsParentLayerForRoot);
+}
+
+static LayoutDeviceToLayerScale
+ZoomToResolution(CSSToScreenScale aZoom,
+                 CSSToLayoutDeviceScale aCssToDev)
+{
+  return ViewTargetAs<ParentLayerPixel>(aZoom,
+      PixelCastJustification::ScreenIsParentLayerForRoot)
+    / aCssToDev * ParentLayerToLayerScale(1);
 }
 
 CSSToScreenScale
 MobileViewportManager::UpdateResolution(const nsViewportInfo& aViewportInfo,
                                         const ScreenIntSize& aDisplaySize,
                                         const CSSSize& aViewport,
                                         const Maybe<float>& aDisplayWidthChangeRatio)
 {
   CSSToLayoutDeviceScale cssToDev =
       mPresShell->GetPresContext()->CSSToDevPixelScale();
   LayoutDeviceToLayerScale res(mPresShell->GetResolution());
+  CSSToScreenScale zoom = ResolutionToZoom(res, cssToDev);
 
   if (mIsFirstPaint) {
     ScreenIntSize compositionSize = GetCompositionSize(aDisplaySize);
 
     CSSToScreenScale defaultZoom;
     if (mRestoreResolution) {
       LayoutDeviceToLayerScale restoreResolution(mRestoreResolution.value());
+      CSSToScreenScale restoreZoom = ResolutionToZoom(restoreResolution, cssToDev);
       if (mRestoreDisplaySize) {
         CSSSize prevViewport = mDocument->GetViewportInfo(mRestoreDisplaySize.value()).GetSize();
         float restoreDisplayWidthChangeRatio = (mRestoreDisplaySize.value().width > 0)
           ? (float)compositionSize.width / (float)mRestoreDisplaySize.value().width : 1.0f;
 
-        restoreResolution =
-          ScaleResolutionWithDisplayWidth(restoreResolution,
-                                          restoreDisplayWidthChangeRatio,
-                                          aViewport,
-                                          prevViewport);
+        restoreZoom =
+          ScaleZoomWithDisplayWidth(restoreZoom,
+                                    restoreDisplayWidthChangeRatio,
+                                    aViewport,
+                                    prevViewport);
       }
-      defaultZoom = CSSToScreenScale(restoreResolution.scale * cssToDev.scale);
+      defaultZoom = restoreZoom;
       MVM_LOG("%p: restored zoom is %f\n", this, defaultZoom.scale);
       defaultZoom = ClampZoom(defaultZoom, aViewportInfo);
     } else {
       defaultZoom = aViewportInfo.GetDefaultZoom();
       MVM_LOG("%p: default zoom from viewport is %f\n", this, defaultZoom.scale);
       if (!aViewportInfo.IsDefaultZoomValid()) {
         defaultZoom = ComputeIntrinsicScale(aViewportInfo,
                                             compositionSize,
                                             aViewport);
       }
     }
     MOZ_ASSERT(aViewportInfo.GetMinZoom() <= defaultZoom &&
       defaultZoom <= aViewportInfo.GetMaxZoom());
 
-    CSSToParentLayerScale zoom = ViewTargetAs<ParentLayerPixel>(defaultZoom,
-      PixelCastJustification::ScreenIsParentLayerForRoot);
+    zoom = defaultZoom;
 
-    LayoutDeviceToLayerScale resolution = zoom / cssToDev * ParentLayerToLayerScale(1);
+    LayoutDeviceToLayerScale resolution = ZoomToResolution(zoom, cssToDev);
     MVM_LOG("%p: setting resolution %f\n", this, resolution.scale);
     mPresShell->SetResolutionAndScaleTo(resolution.scale);
 
-    return defaultZoom;
+    return zoom;
   }
 
   // If this is not a first paint, then in some cases we want to update the pre-
   // existing resolution so as to maintain how much actual content is visible
   // within the display width. Note that "actual content" may be different with
   // respect to CSS pixels because of the CSS viewport size changing.
   //
   // aDisplayWidthChangeRatio is non-empty if:
@@ -295,23 +313,22 @@ MobileViewportManager::UpdateResolution(
   // 1. screen size changes, CSS viewport does not (pages with no meta viewport
   //    or a fixed size viewport)
   // 2. screen size changes, CSS viewport also does (pages with a device-width
   //    viewport)
   // 3. screen size remains constant, but CSS viewport changes (meta viewport
   //    tag is added or removed)
   // 4. neither screen size nor CSS viewport changes
   if (aDisplayWidthChangeRatio) {
-    res = ScaleResolutionWithDisplayWidth(res, aDisplayWidthChangeRatio.value(),
+    zoom = ScaleZoomWithDisplayWidth(zoom, aDisplayWidthChangeRatio.value(),
       aViewport, mMobileViewportSize);
-    mPresShell->SetResolutionAndScaleTo(res.scale);
+    mPresShell->SetResolutionAndScaleTo(ZoomToResolution(zoom, cssToDev).scale);
   }
 
-  return ViewTargetAs<ScreenPixel>(cssToDev * res / ParentLayerToLayerScale(1),
-    PixelCastJustification::ScreenIsParentLayerForRoot);
+  return zoom;
 }
 
 
 ScreenIntSize
 MobileViewportManager::GetCompositionSize(const ScreenIntSize& aDisplaySize) const
 {
   ScreenIntSize compositionSize(aDisplaySize);
   ScreenMargin scrollbars =
--- a/layout/base/MobileViewportManager.h
+++ b/layout/base/MobileViewportManager.h
@@ -81,22 +81,22 @@ public:
 
   /* Secondary main helper method to update just the visual viewport size. */
   void RefreshVisualViewportSize();
 
   /* Helper to clamp the given zoom by the min/max in the viewport info. */
   mozilla::CSSToScreenScale ClampZoom(const mozilla::CSSToScreenScale& aZoom,
                                       const nsViewportInfo& aViewportInfo) const;
 
-  /* Helper to update the given resolution according to changed display and viewport widths. */
-  mozilla::LayoutDeviceToLayerScale
-  ScaleResolutionWithDisplayWidth(const mozilla::LayoutDeviceToLayerScale& aRes,
-                                  const float& aDisplayWidthChangeRatio,
-                                  const mozilla::CSSSize& aNewViewport,
-                                  const mozilla::CSSSize& aOldViewport);
+  /* Helper to update the given zoom according to changed display and viewport widths. */
+  mozilla::CSSToScreenScale
+  ScaleZoomWithDisplayWidth(const mozilla::CSSToScreenScale& aZoom,
+                            const float& aDisplayWidthChangeRatio,
+                            const mozilla::CSSSize& aNewViewport,
+                            const mozilla::CSSSize& aOldViewport);
 
   /* Updates the presShell resolution and returns the new zoom. */
   mozilla::CSSToScreenScale UpdateResolution(const nsViewportInfo& aViewportInfo,
                                              const mozilla::ScreenIntSize& aDisplaySize,
                                              const mozilla::CSSSize& aViewport,
                                              const mozilla::Maybe<float>& aDisplayWidthChangeRatio);
 
   void UpdateVisualViewportSize(const mozilla::ScreenIntSize& aDisplaySize,