Bug 1754323 - Ensure safe-area calculations aren't too off. r=m_kato a=pascalc
authorEmilio Cobos Álvarez <emilio@crisal.io>
Mon, 14 Feb 2022 16:03:40 +0000
changeset 676821 3e8645206408469a81e4e23b3dbe2680f3504380
parent 676820 33e222c692b6fb3fcd1b21e2600ca59a00402819
child 676822 3b3c9ad52284cbfa0a19dd8823fa200e8ac64561
push id16355
push userpchevrel@mozilla.com
push dateWed, 16 Feb 2022 13:39:02 +0000
treeherdermozilla-beta@f6546f4cfd2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato, pascalc
bugs1754323
milestone98.0
Bug 1754323 - Ensure safe-area calculations aren't too off. r=m_kato a=pascalc The fix here is the EnsureAtMost call, but we can simplify a bit of the related code by replacing the four conditionals with an EnsureAtLeast call too, and using Deflate() rather than the manual calculations. This is sort of a wall paper over bug 1754323, but should be safe and prevents ridiculously big safe areas. Differential Revision: https://phabricator.services.mozilla.com/D138453
dom/base/nsContentUtils.cpp
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -10484,40 +10484,44 @@ ScreenIntMargin nsContentUtils::GetWindo
 
   int32_t screenLeft, screenTop, screenWidth, screenHeight;
   nsresult rv =
       aScreen->GetRect(&screenLeft, &screenTop, &screenWidth, &screenHeight);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return windowSafeAreaInsets;
   }
 
-  // Screen's rect of safe area
-  LayoutDeviceIntRect safeAreaRect(
-      screenLeft + aSafeAreaInsets.left, screenTop + aSafeAreaInsets.top,
-      screenWidth - aSafeAreaInsets.right - aSafeAreaInsets.left,
-      screenHeight - aSafeAreaInsets.bottom - aSafeAreaInsets.top);
+  const ScreenIntRect screenRect(screenLeft, screenTop, screenWidth,
+                                 screenHeight);
+
+  ScreenIntRect safeAreaRect = screenRect;
+  safeAreaRect.Deflate(aSafeAreaInsets);
+
+  ScreenIntRect windowRect = ViewAs<ScreenPixel>(
+      aWindowRect, PixelCastJustification::LayoutDeviceIsScreenForTabDims);
+
+  // FIXME(bug 1754323): This can trigger because the screen rect is not
+  // orientation-aware.
+  // MOZ_ASSERT(screenRect.Contains(windowRect),
+  //            "Screen doesn't contain window rect? Something seems off");
+
   // window's rect of safe area
-  safeAreaRect = safeAreaRect.Intersect(aWindowRect);
-
-  windowSafeAreaInsets.top =
-      aSafeAreaInsets.top ? std::max(safeAreaRect.y - aWindowRect.y, 0) : 0;
-  windowSafeAreaInsets.left =
-      aSafeAreaInsets.left ? std::max(safeAreaRect.x - aWindowRect.x, 0) : 0;
+  safeAreaRect = safeAreaRect.Intersect(windowRect);
+
+  windowSafeAreaInsets.top = safeAreaRect.y - aWindowRect.y;
+  windowSafeAreaInsets.left = safeAreaRect.x - aWindowRect.x;
   windowSafeAreaInsets.right =
-      aSafeAreaInsets.right
-          ? std::max((aWindowRect.x + aWindowRect.width) -
-                         (safeAreaRect.x + safeAreaRect.width),
-                     0)
-          : 0;
-  windowSafeAreaInsets.bottom =
-      aSafeAreaInsets.bottom
-          ? std::max(aWindowRect.y + aWindowRect.height -
-                         (safeAreaRect.y + safeAreaRect.height),
-                     0)
-          : 0;
+      aWindowRect.x + aWindowRect.width - (safeAreaRect.x + safeAreaRect.width);
+  windowSafeAreaInsets.bottom = aWindowRect.y + aWindowRect.height -
+                                (safeAreaRect.y + safeAreaRect.height);
+
+  windowSafeAreaInsets.EnsureAtLeast(ScreenIntMargin());
+  // This shouldn't be needed, but it wallpapers orientation issues, see bug
+  // 1754323.
+  windowSafeAreaInsets.EnsureAtMost(aSafeAreaInsets);
 
   return windowSafeAreaInsets;
 }
 
 /* static */
 nsContentUtils::SubresourceCacheValidationInfo
 nsContentUtils::GetSubresourceCacheValidationInfo(nsIRequest* aRequest,
                                                   nsIURI* aURI) {