2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-07-21 17:51:55 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "MobileViewportManager.h"
|
|
|
|
|
2016-11-30 06:14:28 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2020-09-29 14:54:33 +03:00
|
|
|
#include "mozilla/ToString.h"
|
2019-04-02 23:11:56 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-04-20 07:49:29 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2018-04-05 20:42:40 +03:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2015-09-10 13:24:34 +03:00
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
2015-07-21 17:51:55 +03:00
|
|
|
#include "nsViewManager.h"
|
|
|
|
#include "nsViewportInfo.h"
|
2015-09-10 13:24:34 +03:00
|
|
|
#include "UnitTransforms.h"
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2020-02-24 19:31:04 +03:00
|
|
|
static mozilla::LazyLogModule sApzMvmLog("apz.mobileviewport");
|
|
|
|
#define MVM_LOG(...) MOZ_LOG(sApzMvmLog, LogLevel::Debug, (__VA_ARGS__))
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(MobileViewportManager, nsIDOMEventListener, nsIObserver)
|
|
|
|
|
2017-11-23 09:06:15 +03:00
|
|
|
#define DOM_META_ADDED u"DOMMetaAdded"_ns
|
|
|
|
#define DOM_META_CHANGED u"DOMMetaChanged"_ns
|
2021-03-09 06:28:02 +03:00
|
|
|
#define FULLSCREEN_CHANGED u"fullscreenchange"_ns
|
2017-11-23 09:06:15 +03:00
|
|
|
#define LOAD u"load"_ns
|
|
|
|
#define BEFORE_FIRST_PAINT "before-first-paint"_ns
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2019-04-02 23:11:56 +03:00
|
|
|
using namespace mozilla::dom;
|
2015-07-21 17:51:55 +03:00
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
2020-06-12 04:15:42 +03:00
|
|
|
MobileViewportManager::MobileViewportManager(MVMContext* aContext,
|
|
|
|
ManagerType aType)
|
|
|
|
: mContext(aContext),
|
|
|
|
mManagerType(aType),
|
|
|
|
mIsFirstPaint(false),
|
|
|
|
mPainted(false) {
|
2019-04-05 01:11:40 +03:00
|
|
|
MOZ_ASSERT(mContext);
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2020-02-24 19:31:04 +03:00
|
|
|
MVM_LOG("%p: creating with context %p\n", this, mContext.get());
|
2019-04-05 01:11:40 +03:00
|
|
|
|
|
|
|
mContext->AddEventListener(DOM_META_ADDED, this, false);
|
|
|
|
mContext->AddEventListener(DOM_META_CHANGED, this, false);
|
2021-03-09 06:28:02 +03:00
|
|
|
mContext->AddEventListener(FULLSCREEN_CHANGED, this, false);
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->AddEventListener(LOAD, this, true);
|
|
|
|
|
|
|
|
mContext->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false);
|
2021-08-03 00:27:19 +03:00
|
|
|
|
|
|
|
// We need to initialize the display size and the CSS viewport size before
|
|
|
|
// the initial reflow happens.
|
|
|
|
UpdateSizesBeforeReflow();
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
2020-03-17 12:38:32 +03:00
|
|
|
MobileViewportManager::~MobileViewportManager() = default;
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
void MobileViewportManager::Destroy() {
|
|
|
|
MVM_LOG("%p: destroying\n", this);
|
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->RemoveEventListener(DOM_META_ADDED, this, false);
|
|
|
|
mContext->RemoveEventListener(DOM_META_CHANGED, this, false);
|
2021-03-09 06:28:02 +03:00
|
|
|
mContext->RemoveEventListener(FULLSCREEN_CHANGED, this, false);
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->RemoveEventListener(LOAD, this, true);
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->RemoveObserver(this, BEFORE_FIRST_PAINT.Data());
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->Destroy();
|
|
|
|
mContext = nullptr;
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
Bug 1282902 - Part 3 - Let the MobileViewportManager recalculate the saved resolution if the display width changed before restoring. r=kats
The mobile session store saves the current document resolution in order to restore the previous zoom level when restoring a page. If the display width has changed since the session data was captured (e.g. because the device was rotated), the resolution might have to be scaled appropriately.
Currently, the session store does this scaling by itself by comparing the stored and current window widths, however this implementation is slightly simplified and doesn't cover all use cases, which means some pages can be restored at a wrong zoom level after rotation. To correctly cover all cases, the session store would have to compare viewport widths, too.
Because the MobileViewportManager doesn't wait for the session store to set the restore resolution, the latter has to call setRestoreResolution() as early as possible in order to guarantee that the restore resolution is set before the first paint of the document. Therefore the session store currently calls this after receiving a LocationChange notification. However at that time, the correct viewport for the current document is not yet available, which means the resolution cannot be recalculated by the session store at that point.
Therefore, this patch changes the approach taken and lets the MVM handle all resolution calculations instead. The session store now simply passes the stored previous display dimensions along with the previous document resolution to the MVM, which can then compare them to the current display and viewport widths and scale the resolution appropriately before using it during first paint.
MozReview-Commit-ID: IGxWw87yftK
--HG--
extra : transplant_source : e%8D%BD%26%D2%C3%8E5%E3%2B%C0t%BA%DB%C1%BBs%3F%13%1F
2016-07-01 22:23:25 +03:00
|
|
|
void MobileViewportManager::SetRestoreResolution(
|
|
|
|
float aResolution, LayoutDeviceIntSize aDisplaySize) {
|
2017-06-29 19:26:37 +03:00
|
|
|
SetRestoreResolution(aResolution);
|
Bug 1282902 - Part 3 - Let the MobileViewportManager recalculate the saved resolution if the display width changed before restoring. r=kats
The mobile session store saves the current document resolution in order to restore the previous zoom level when restoring a page. If the display width has changed since the session data was captured (e.g. because the device was rotated), the resolution might have to be scaled appropriately.
Currently, the session store does this scaling by itself by comparing the stored and current window widths, however this implementation is slightly simplified and doesn't cover all use cases, which means some pages can be restored at a wrong zoom level after rotation. To correctly cover all cases, the session store would have to compare viewport widths, too.
Because the MobileViewportManager doesn't wait for the session store to set the restore resolution, the latter has to call setRestoreResolution() as early as possible in order to guarantee that the restore resolution is set before the first paint of the document. Therefore the session store currently calls this after receiving a LocationChange notification. However at that time, the correct viewport for the current document is not yet available, which means the resolution cannot be recalculated by the session store at that point.
Therefore, this patch changes the approach taken and lets the MVM handle all resolution calculations instead. The session store now simply passes the stored previous display dimensions along with the previous document resolution to the MVM, which can then compare them to the current display and viewport widths and scale the resolution appropriately before using it during first paint.
MozReview-Commit-ID: IGxWw87yftK
--HG--
extra : transplant_source : e%8D%BD%26%D2%C3%8E5%E3%2B%C0t%BA%DB%C1%BBs%3F%13%1F
2016-07-01 22:23:25 +03:00
|
|
|
ScreenIntSize restoreDisplaySize = ViewAs<ScreenPixel>(
|
|
|
|
aDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
mRestoreDisplaySize = Some(restoreDisplaySize);
|
2016-05-17 21:50:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:26:37 +03:00
|
|
|
void MobileViewportManager::SetRestoreResolution(float aResolution) {
|
|
|
|
mRestoreResolution = Some(aResolution);
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:18:42 +03:00
|
|
|
float MobileViewportManager::ComputeIntrinsicResolution() const {
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return 1.f;
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:18:42 +03:00
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
2019-04-05 01:11:40 +03:00
|
|
|
CSSToScreenScale intrinsicScale = ComputeIntrinsicScale(
|
|
|
|
mContext->GetViewportInfo(displaySize), displaySize, mMobileViewportSize);
|
|
|
|
CSSToLayoutDeviceScale cssToDev = mContext->CSSToDevPixelScale();
|
2018-11-03 00:18:42 +03:00
|
|
|
return (intrinsicScale / cssToDev).scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::CSSToScreenScale MobileViewportManager::ComputeIntrinsicScale(
|
|
|
|
const nsViewportInfo& aViewportInfo,
|
|
|
|
const mozilla::ScreenIntSize& aDisplaySize,
|
2018-12-30 05:15:42 +03:00
|
|
|
const mozilla::CSSSize& aViewportOrContentSize) const {
|
2018-11-03 00:18:42 +03:00
|
|
|
CSSToScreenScale intrinsicScale =
|
2019-07-27 03:11:20 +03:00
|
|
|
aViewportOrContentSize.IsEmpty()
|
|
|
|
? CSSToScreenScale(1.0)
|
|
|
|
: MaxScaleRatio(ScreenSize(aDisplaySize), aViewportOrContentSize);
|
2018-11-03 00:18:42 +03:00
|
|
|
MVM_LOG("%p: Intrinsic computed zoom is %f\n", this, intrinsicScale.scale);
|
|
|
|
return ClampZoom(intrinsicScale, aViewportInfo);
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:58:31 +03:00
|
|
|
void MobileViewportManager::RequestReflow(bool aForceAdjustResolution) {
|
|
|
|
MVM_LOG("%p: got a reflow request with force resolution: %d\n", this,
|
|
|
|
aForceAdjustResolution);
|
|
|
|
RefreshViewportSize(aForceAdjustResolution);
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 01:23:34 +03:00
|
|
|
void MobileViewportManager::ResolutionUpdated(
|
|
|
|
mozilla::ResolutionChangeOrigin aOrigin) {
|
2015-11-18 21:53:14 +03:00
|
|
|
MVM_LOG("%p: resolution updated\n", this);
|
2019-01-08 23:26:08 +03:00
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-31 00:34:39 +03:00
|
|
|
if ((!mPainted &&
|
|
|
|
aOrigin == mozilla::ResolutionChangeOrigin::MainThreadRestore) ||
|
|
|
|
aOrigin == mozilla::ResolutionChangeOrigin::Test) {
|
2017-06-29 19:26:37 +03:00
|
|
|
// Save the value, so our default zoom calculation
|
|
|
|
// can take it into account later on.
|
2019-04-05 01:11:40 +03:00
|
|
|
SetRestoreResolution(mContext->GetResolution());
|
2017-06-29 19:26:37 +03:00
|
|
|
}
|
2018-09-14 22:23:28 +03:00
|
|
|
RefreshVisualViewportSize();
|
2015-11-18 21:53:14 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
NS_IMETHODIMP
|
2018-04-20 07:49:29 +03:00
|
|
|
MobileViewportManager::HandleEvent(dom::Event* event) {
|
2015-07-21 17:51:55 +03:00
|
|
|
nsAutoString type;
|
|
|
|
event->GetType(type);
|
|
|
|
|
|
|
|
if (type.Equals(DOM_META_ADDED)) {
|
2019-08-13 01:13:38 +03:00
|
|
|
HandleDOMMetaAdded();
|
2015-08-24 22:35:00 +03:00
|
|
|
} else if (type.Equals(DOM_META_CHANGED)) {
|
|
|
|
MVM_LOG("%p: got a dom-meta-changed event\n", this);
|
|
|
|
RefreshViewportSize(mPainted);
|
2021-03-09 06:28:02 +03:00
|
|
|
} else if (type.Equals(FULLSCREEN_CHANGED)) {
|
|
|
|
MVM_LOG("%p: got a fullscreenchange event\n", this);
|
|
|
|
RefreshViewportSize(mPainted);
|
2016-01-12 17:47:29 +03:00
|
|
|
} else if (type.Equals(LOAD)) {
|
|
|
|
MVM_LOG("%p: got a load event\n", this);
|
|
|
|
if (!mPainted) {
|
|
|
|
// Load event got fired before the before-first-paint message
|
|
|
|
SetInitialViewport();
|
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-13 01:13:38 +03:00
|
|
|
void MobileViewportManager::HandleDOMMetaAdded() {
|
|
|
|
MVM_LOG("%p: got a dom-meta-added event\n", this);
|
2019-08-22 01:00:43 +03:00
|
|
|
if (mPainted && mContext->IsDocumentLoading()) {
|
|
|
|
// It's possible that we get a DOMMetaAdded event after the page
|
|
|
|
// has already been painted, but before the document finishes loading.
|
|
|
|
// In such a case, we've already run SetInitialViewport() on
|
|
|
|
// "before-first-paint", and won't run it again on "load" (because
|
|
|
|
// mPainted=true). But that SetInitialViewport() call didn't know the
|
|
|
|
// "initial-scale" from this meta viewport tag. To ensure we respect
|
|
|
|
// the "initial-scale", call SetInitialViewport() again.
|
|
|
|
// Note: It's important that we only do this if mPainted=true. In the
|
|
|
|
// usual case, we get the DOMMetaAdded before the first paint, sometimes
|
|
|
|
// even before we have a frame tree, and calling SetInitialViewport()
|
|
|
|
// before we have a frame tree will skip some important steps (e.g.
|
|
|
|
// updating display port margins).
|
|
|
|
SetInitialViewport();
|
|
|
|
} else {
|
|
|
|
RefreshViewportSize(mPainted);
|
|
|
|
}
|
2019-08-13 01:13:38 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileViewportManager::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:42:17 +03:00
|
|
|
return NS_OK;
|
2019-01-08 23:26:08 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
if (mContext->SubjectMatchesDocument(aSubject) &&
|
2015-07-21 17:51:55 +03:00
|
|
|
BEFORE_FIRST_PAINT.EqualsASCII(aTopic)) {
|
|
|
|
MVM_LOG("%p: got a before-first-paint event\n", this);
|
2016-01-12 17:47:29 +03:00
|
|
|
if (!mPainted) {
|
|
|
|
// before-first-paint message arrived before load event
|
|
|
|
SetInitialViewport();
|
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-12 17:47:29 +03:00
|
|
|
void MobileViewportManager::SetInitialViewport() {
|
|
|
|
MVM_LOG("%p: setting initial viewport\n", this);
|
|
|
|
mIsFirstPaint = true;
|
|
|
|
mPainted = true;
|
|
|
|
RefreshViewportSize(false);
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:50:08 +03:00
|
|
|
CSSToScreenScale MobileViewportManager::ClampZoom(
|
2018-11-03 00:18:42 +03:00
|
|
|
const CSSToScreenScale& aZoom, const nsViewportInfo& aViewportInfo) const {
|
2016-05-17 21:50:08 +03:00
|
|
|
CSSToScreenScale zoom = aZoom;
|
2019-07-31 19:59:51 +03:00
|
|
|
if (IsNaN(zoom.scale)) {
|
|
|
|
NS_ERROR("Don't pass NaN to ClampZoom; check caller for 0/0 division");
|
|
|
|
zoom = CSSToScreenScale(1.0);
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:50:08 +03:00
|
|
|
if (zoom < aViewportInfo.GetMinZoom()) {
|
|
|
|
zoom = aViewportInfo.GetMinZoom();
|
|
|
|
MVM_LOG("%p: Clamped to %f\n", this, zoom.scale);
|
|
|
|
}
|
|
|
|
if (zoom > aViewportInfo.GetMaxZoom()) {
|
|
|
|
zoom = aViewportInfo.GetMaxZoom();
|
|
|
|
MVM_LOG("%p: Clamped to %f\n", this, zoom.scale);
|
|
|
|
}
|
2019-07-27 02:38:18 +03:00
|
|
|
|
|
|
|
// Non-positive zoom factors can produce NaN or negative viewport sizes,
|
|
|
|
// so we better be sure we've got a positive zoom factor. Just for good
|
|
|
|
// measure, we check our min/max as well as the final clamped value.
|
|
|
|
MOZ_ASSERT(aViewportInfo.GetMinZoom() > CSSToScreenScale(0.0f),
|
|
|
|
"zoom factor must be positive");
|
|
|
|
MOZ_ASSERT(aViewportInfo.GetMaxZoom() > CSSToScreenScale(0.0f),
|
|
|
|
"zoom factor must be positive");
|
|
|
|
MOZ_ASSERT(zoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
|
2016-05-17 21:50:08 +03:00
|
|
|
return zoom;
|
|
|
|
}
|
|
|
|
|
2018-11-30 04:01:13 +03:00
|
|
|
CSSToScreenScale MobileViewportManager::ScaleZoomWithDisplayWidth(
|
|
|
|
const CSSToScreenScale& aZoom, const float& aDisplayWidthChangeRatio,
|
|
|
|
const CSSSize& aNewViewport, const CSSSize& aOldViewport) {
|
2019-06-05 01:25:06 +03:00
|
|
|
float inverseCssWidthChangeRatio =
|
|
|
|
(aNewViewport.width == 0) ? 1.0f
|
|
|
|
: aOldViewport.width / aNewViewport.width;
|
|
|
|
CSSToScreenScale newZoom(aZoom.scale * aDisplayWidthChangeRatio *
|
|
|
|
inverseCssWidthChangeRatio);
|
|
|
|
MVM_LOG("%p: Old zoom was %f, changed by %f * %f to %f\n", this, aZoom.scale,
|
|
|
|
aDisplayWidthChangeRatio, inverseCssWidthChangeRatio, newZoom.scale);
|
2018-11-30 04:01:13 +03:00
|
|
|
return newZoom;
|
|
|
|
}
|
|
|
|
|
2020-06-25 02:48:51 +03:00
|
|
|
CSSToScreenScale MobileViewportManager::ResolutionToZoom(
|
|
|
|
const LayoutDeviceToLayerScale& aResolution) const {
|
2018-11-30 04:01:13 +03:00
|
|
|
return ViewTargetAs<ScreenPixel>(
|
2020-06-25 02:48:51 +03:00
|
|
|
mContext->CSSToDevPixelScale() * aResolution / ParentLayerToLayerScale(1),
|
2018-11-30 04:01:13 +03:00
|
|
|
PixelCastJustification::ScreenIsParentLayerForRoot);
|
|
|
|
}
|
|
|
|
|
2020-06-25 02:48:51 +03:00
|
|
|
LayoutDeviceToLayerScale MobileViewportManager::ZoomToResolution(
|
|
|
|
const CSSToScreenScale& aZoom) const {
|
2018-11-30 04:01:13 +03:00
|
|
|
return ViewTargetAs<ParentLayerPixel>(
|
|
|
|
aZoom, PixelCastJustification::ScreenIsParentLayerForRoot) /
|
2020-06-25 02:48:51 +03:00
|
|
|
mContext->CSSToDevPixelScale() * ParentLayerToLayerScale(1);
|
2016-07-02 23:15:19 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
void MobileViewportManager::UpdateResolutionForFirstPaint(
|
|
|
|
const CSSSize& aViewportSize) {
|
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
|
|
|
|
ScreenIntSize compositionSize = GetCompositionSize(displaySize);
|
|
|
|
|
|
|
|
if (mRestoreResolution) {
|
|
|
|
LayoutDeviceToLayerScale restoreResolution(*mRestoreResolution);
|
2020-06-25 02:48:51 +03:00
|
|
|
CSSToScreenScale restoreZoom = ResolutionToZoom(restoreResolution);
|
2020-06-25 02:45:00 +03:00
|
|
|
if (mRestoreDisplaySize) {
|
|
|
|
CSSSize prevViewport =
|
|
|
|
mContext->GetViewportInfo(*mRestoreDisplaySize).GetSize();
|
|
|
|
float restoreDisplayWidthChangeRatio =
|
|
|
|
(mRestoreDisplaySize->width > 0)
|
|
|
|
? (float)compositionSize.width / (float)mRestoreDisplaySize->width
|
|
|
|
: 1.0f;
|
|
|
|
|
|
|
|
restoreZoom =
|
|
|
|
ScaleZoomWithDisplayWidth(restoreZoom, restoreDisplayWidthChangeRatio,
|
|
|
|
aViewportSize, prevViewport);
|
|
|
|
}
|
|
|
|
MVM_LOG("%p: restored zoom is %f\n", this, restoreZoom.scale);
|
|
|
|
restoreZoom = ClampZoom(restoreZoom, viewportInfo);
|
|
|
|
|
|
|
|
ApplyNewZoom(displaySize, restoreZoom);
|
2019-01-08 23:26:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
CSSToScreenScale defaultZoom = viewportInfo.GetDefaultZoom();
|
|
|
|
MVM_LOG("%p: default zoom from viewport is %f\n", this, defaultZoom.scale);
|
|
|
|
if (!viewportInfo.IsDefaultZoomValid()) {
|
2020-06-25 11:41:24 +03:00
|
|
|
CSSSize contentSize = aViewportSize;
|
|
|
|
if (Maybe<CSSRect> scrollableRect =
|
|
|
|
mContext->CalculateScrollableRectForRSF()) {
|
|
|
|
contentSize = scrollableRect->Size();
|
|
|
|
}
|
2020-06-25 02:45:00 +03:00
|
|
|
defaultZoom =
|
2020-06-25 11:41:24 +03:00
|
|
|
ComputeIntrinsicScale(viewportInfo, compositionSize, contentSize);
|
2020-06-25 02:45:00 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(viewportInfo.GetMinZoom() <= defaultZoom &&
|
|
|
|
defaultZoom <= viewportInfo.GetMaxZoom());
|
|
|
|
|
|
|
|
ApplyNewZoom(displaySize, defaultZoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MobileViewportManager::UpdateResolutionForViewportSizeChange(
|
|
|
|
const CSSSize& aViewportSize,
|
|
|
|
const Maybe<float>& aDisplayWidthChangeRatio) {
|
2020-06-25 01:22:17 +03:00
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
|
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
CSSToScreenScale zoom = GetZoom();
|
2019-07-23 21:07:26 +03:00
|
|
|
// Non-positive zoom factors can produce NaN or negative viewport sizes,
|
|
|
|
// so we better be sure we've got a positive zoom factor.
|
|
|
|
MOZ_ASSERT(zoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
|
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
MOZ_ASSERT(!mIsFirstPaint);
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
// (a) The meta-viewport tag information changes, and so the CSS viewport
|
|
|
|
// might change as a result. If this happens after the content has
|
|
|
|
// been painted, we want to adjust the zoom to compensate. OR
|
|
|
|
// (b) The display size changed from a nonzero value to another
|
|
|
|
// nonzero value. This covers the case where e.g. the device was
|
|
|
|
// rotated, and again we want to adjust the zoom to compensate.
|
|
|
|
// Note in particular that aDisplayWidthChangeRatio will be None if all
|
|
|
|
// that happened was a change in the full-zoom. In this case, we still
|
|
|
|
// want to compute a new CSS and visual viewport, but we don't want to update
|
|
|
|
// the resolution.
|
|
|
|
//
|
|
|
|
// Given the above, the algorithm below accounts for all types of changes
|
|
|
|
// I can conceive of:
|
|
|
|
// 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) {
|
|
|
|
UpdateVisualViewportSize(displaySize, zoom);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
// One more complication is that our current zoom level may be the
|
|
|
|
// result of clamping to either the minimum or maximum zoom level
|
|
|
|
// allowed by the viewport. If we naively scale the zoom level with
|
|
|
|
// the change in the display width, we might be scaling one of these
|
|
|
|
// previously clamped values. What we really want to do is to make
|
|
|
|
// scaling of the zoom aware of these minimum and maximum clamping
|
|
|
|
// points for the existing content size, so that we keep display
|
|
|
|
// width changes completely reversible.
|
2018-11-30 03:58:56 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
// We don't consider here if we are scaling to a zoom value outside
|
|
|
|
// of our viewport limits, because we'll clamp to the viewport limits
|
|
|
|
// as a final step.
|
2018-11-30 03:58:56 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
// Because of the behavior of ShrinkToDisplaySizeIfNeeded, we are
|
|
|
|
// choosing zoom clamping points based on the content size of the
|
|
|
|
// scrollable rect, which might different from aViewportSize.
|
|
|
|
CSSSize contentSize = aViewportSize;
|
|
|
|
if (Maybe<CSSRect> scrollableRect =
|
|
|
|
mContext->CalculateScrollableRectForRSF()) {
|
|
|
|
contentSize = scrollableRect->Size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We scale the sizes, though we only care about the scaled widths.
|
|
|
|
ScreenSize minZoomDisplaySize = contentSize * viewportInfo.GetMinZoom();
|
|
|
|
ScreenSize maxZoomDisplaySize = contentSize * viewportInfo.GetMaxZoom();
|
|
|
|
|
|
|
|
ScreenSize newDisplaySize(displaySize);
|
|
|
|
ScreenSize oldDisplaySize = newDisplaySize / *aDisplayWidthChangeRatio;
|
|
|
|
|
|
|
|
// To calculate an adjusted ratio, we use some combination of these
|
|
|
|
// four values:
|
|
|
|
float a(minZoomDisplaySize.width);
|
|
|
|
float b(maxZoomDisplaySize.width);
|
|
|
|
float c(oldDisplaySize.width);
|
|
|
|
float d(newDisplaySize.width);
|
|
|
|
|
|
|
|
// The oldDisplaySize value is in one of three "zones":
|
|
|
|
// 1) Less than or equal to minZoomDisplaySize.
|
|
|
|
// 2) Between minZoomDisplaySize and maxZoomDisplaySize.
|
|
|
|
// 3) Greater than or equal to maxZoomDisplaySize.
|
|
|
|
|
|
|
|
// Depending on which zone each are in, the adjusted ratio is shown in
|
|
|
|
// the table below (using the a-b-c-d coding from above):
|
|
|
|
|
|
|
|
// c +---+
|
|
|
|
// | d |
|
|
|
|
// 1 | a |
|
|
|
|
// +---+
|
|
|
|
// | d |
|
|
|
|
// 2 | c |
|
|
|
|
// +---+
|
|
|
|
// | d |
|
|
|
|
// 3 | b |
|
|
|
|
// +---+
|
|
|
|
|
|
|
|
// Conveniently, the denominator is c clamped to a..b.
|
|
|
|
float denominator = clamped(c, a, b);
|
|
|
|
|
|
|
|
float adjustedRatio = d / denominator;
|
|
|
|
CSSToScreenScale adjustedZoom = ScaleZoomWithDisplayWidth(
|
|
|
|
zoom, adjustedRatio, aViewportSize, mMobileViewportSize);
|
|
|
|
CSSToScreenScale newZoom = ClampZoom(adjustedZoom, viewportInfo);
|
|
|
|
|
|
|
|
ApplyNewZoom(displaySize, newZoom);
|
|
|
|
}
|
2019-03-23 00:01:26 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
void MobileViewportManager::UpdateResolutionForContentSizeChange(
|
|
|
|
const CSSSize& aContentSize) {
|
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
|
2019-03-23 00:01:26 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
CSSToScreenScale zoom = GetZoom();
|
|
|
|
// Non-positive zoom factors can produce NaN or negative viewport sizes,
|
|
|
|
// so we better be sure we've got a positive zoom factor.
|
|
|
|
MOZ_ASSERT(zoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
|
2019-03-23 00:01:26 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
ScreenIntSize compositionSize = GetCompositionSize(displaySize);
|
|
|
|
CSSToScreenScale intrinsicScale =
|
|
|
|
ComputeIntrinsicScale(viewportInfo, compositionSize, aContentSize);
|
|
|
|
|
|
|
|
// We try to scale down the contents only IF the document has no
|
|
|
|
// initial-scale AND IF it's not restored documents AND IF the resolution
|
|
|
|
// has never been changed by APZ.
|
|
|
|
if (!mRestoreResolution && !mContext->IsResolutionUpdatedByApz() &&
|
|
|
|
!viewportInfo.IsDefaultZoomValid()) {
|
|
|
|
if (zoom != intrinsicScale) {
|
|
|
|
ApplyNewZoom(displaySize, intrinsicScale);
|
2018-11-30 03:56:49 +03:00
|
|
|
}
|
2020-06-25 02:45:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even in other scenarios, we want to ensure that zoom level is
|
|
|
|
// not _smaller_ than the intrinsic scale, otherwise we might be
|
|
|
|
// trying to show regions where there is no content to show.
|
|
|
|
CSSToScreenScale clampedZoom = zoom;
|
|
|
|
|
|
|
|
if (clampedZoom < intrinsicScale) {
|
|
|
|
clampedZoom = intrinsicScale;
|
2018-11-30 03:56:49 +03:00
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
// Also clamp to the restrictions imposed by viewportInfo.
|
|
|
|
clampedZoom = ClampZoom(clampedZoom, viewportInfo);
|
|
|
|
|
|
|
|
if (clampedZoom != zoom) {
|
|
|
|
ApplyNewZoom(displaySize, clampedZoom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MobileViewportManager::ApplyNewZoom(const ScreenIntSize& aDisplaySize,
|
|
|
|
const CSSToScreenScale& aNewZoom) {
|
2018-12-01 08:34:57 +03:00
|
|
|
// If the zoom has changed, update the pres shell resolution accordingly.
|
2019-08-13 01:23:34 +03:00
|
|
|
// We characterize this as MainThreadAdjustment, because we don't want our
|
|
|
|
// change here to be remembered as a restore resolution.
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2020-06-25 02:45:00 +03:00
|
|
|
// Non-positive zoom factors can produce NaN or negative viewport sizes,
|
|
|
|
// so we better be sure we've got a positive zoom factor.
|
|
|
|
MOZ_ASSERT(aNewZoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
|
2018-12-01 08:34:57 +03:00
|
|
|
|
2020-06-25 02:48:51 +03:00
|
|
|
LayoutDeviceToLayerScale resolution = ZoomToResolution(aNewZoom);
|
2020-06-25 02:45:00 +03:00
|
|
|
MVM_LOG("%p: setting resolution %f\n", this, resolution.scale);
|
|
|
|
mContext->SetResolutionAndScaleTo(
|
|
|
|
resolution.scale, ResolutionChangeOrigin::MainThreadAdjustment);
|
|
|
|
|
|
|
|
MVM_LOG("%p: New zoom is %f\n", this, aNewZoom.scale);
|
|
|
|
|
|
|
|
UpdateVisualViewportSize(aDisplaySize, aNewZoom);
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 06:56:44 +03:00
|
|
|
ScreenIntSize MobileViewportManager::GetCompositionSize(
|
|
|
|
const ScreenIntSize& aDisplaySize) const {
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return ScreenIntSize();
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:00:05 +03:00
|
|
|
// FIXME: Bug 1586986 - To update VisualViewport in response to the dynamic
|
|
|
|
// toolbar transition we probably need to include the dynamic toolbar
|
|
|
|
// _current_ height.
|
2018-11-20 06:56:44 +03:00
|
|
|
ScreenIntSize compositionSize(aDisplaySize);
|
2015-07-21 17:51:55 +03:00
|
|
|
ScreenMargin scrollbars =
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->ScrollbarAreaToExcludeFromCompositionBounds()
|
2015-08-07 21:23:13 +03:00
|
|
|
// Scrollbars are not subject to resolution scaling, so LD pixels =
|
|
|
|
// Screen pixels for them.
|
|
|
|
* LayoutDeviceToScreenScale(1.0f);
|
|
|
|
|
2019-05-13 20:26:34 +03:00
|
|
|
compositionSize.width =
|
|
|
|
std::max(0.0f, compositionSize.width - scrollbars.LeftRight());
|
|
|
|
compositionSize.height =
|
|
|
|
std::max(0.0f, compositionSize.height - scrollbars.TopBottom());
|
2018-11-20 06:56:44 +03:00
|
|
|
|
|
|
|
return compositionSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MobileViewportManager::UpdateVisualViewportSize(
|
|
|
|
const ScreenIntSize& aDisplaySize, const CSSToScreenScale& aZoom) {
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-20 06:56:44 +03:00
|
|
|
ScreenSize compositionSize = ScreenSize(GetCompositionSize(aDisplaySize));
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
CSSSize compSize = compositionSize / aZoom;
|
2020-09-29 14:54:33 +03:00
|
|
|
MVM_LOG("%p: Setting VVPS %s\n", this, ToString(compSize).c_str());
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->SetVisualViewportSize(compSize);
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 00:36:59 +03:00
|
|
|
CSSToScreenScale MobileViewportManager::GetZoom() const {
|
|
|
|
LayoutDeviceToLayerScale res(mContext->GetResolution());
|
2020-06-25 02:48:51 +03:00
|
|
|
return ResolutionToZoom(res);
|
2019-11-22 00:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MobileViewportManager::UpdateVisualViewportSizeByDynamicToolbar(
|
|
|
|
ScreenIntCoord aToolbarHeight) {
|
|
|
|
if (!mContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
displaySize.height += aToolbarHeight;
|
|
|
|
CSSSize compSize = ScreenSize(GetCompositionSize(displaySize)) / GetZoom();
|
|
|
|
|
|
|
|
mVisualViewportSizeUpdatedByDynamicToolbar =
|
|
|
|
nsSize(nsPresContext::CSSPixelsToAppUnits(compSize.width),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(compSize.height));
|
|
|
|
|
|
|
|
mContext->PostVisualViewportResizeEventByDynamicToolbar();
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:30:00 +03:00
|
|
|
void MobileViewportManager::
|
|
|
|
UpdateVisualViewportSizeForPotentialScrollbarChange() {
|
|
|
|
RefreshVisualViewportSize();
|
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
void MobileViewportManager::UpdateDisplayPortMargins() {
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-04-05 01:11:40 +03:00
|
|
|
mContext->UpdateDisplayPortMargins();
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 22:23:28 +03:00
|
|
|
void MobileViewportManager::RefreshVisualViewportSize() {
|
2015-11-18 21:53:14 +03:00
|
|
|
// This function is a subset of RefreshViewportSize, and only updates the
|
2018-09-14 22:23:28 +03:00
|
|
|
// visual viewport size.
|
2015-11-18 21:53:14 +03:00
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:53:14 +03:00
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
|
2020-06-21 23:48:37 +03:00
|
|
|
if (displaySize.width == 0 || displaySize.height == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-22 00:36:59 +03:00
|
|
|
UpdateVisualViewportSize(displaySize, GetZoom());
|
2015-11-18 21:53:14 +03:00
|
|
|
}
|
|
|
|
|
Bug 1647034 - Change how the visual viewport is updated to eliminate spurious reflows. r=tnikkel
There's two code changes in this patch:
- The update to the visual viewport that was happening just before positioning
the fixed items gets moved to happen after determining the scrollbars for
the root scrollframe. This moves it a little bit earlier, to basically the
earliest point at which the visual viewport can actually be computed, since
it depends on the presence of the root scrollframe's scrollbars.
More importantly, this change sets the visual viewport without checking to
see if one was already set, as the old code did. This means every reflow
of the root scrollframe on a presShell with an MVM will now have a visual
viewport set. Previously the visual viewport would only get set for the first
time when the MVM got a load or first-paint event, and then would get updated
for subsequent reflows. The net effect here is that the visual viewport is
set earlier, and this can sometimes eliminate extra reflows from after the
load event, because everything is already in a consistent state.
- The NotifyResizeReflow call to MVM is replaced by a NotifyReflow call that
runs before every reflow, instead of just on resizes. Note that the
NotifyReflow also doesn't update the visual viewport like NotifyResizeReflow
used to do, because that is taken care of by the above-mentioned code change
to set the visual viewport.
This is desirable because there are things that run during reflow that attempt
to read the display size from the MVM, and they were getting a zero size
for reflows that happened before the first resize or load/first-paint events.
Now they get a valid display size on every reflow, and so again this allows
fewer overall reflows as the code converges to a stable state faster.
Together these changes ensure that every reflow has access to up-to-date
properties (display size, mobile viewport size, visual viewport size) from the
MVM. This eliminates unnecessary reflows because of out-of-order computations
based on stale values and such. Therefore the number of reflows goes down,
which is reflected by the changes to the crashtest assertion counts.
Differential Revision: https://phabricator.services.mozilla.com/D81375
2020-06-29 21:39:57 +03:00
|
|
|
void MobileViewportManager::UpdateSizesBeforeReflow() {
|
2020-06-15 16:32:44 +03:00
|
|
|
if (Maybe<LayoutDeviceIntSize> newDisplaySize =
|
|
|
|
mContext->GetContentViewerSize()) {
|
|
|
|
mDisplaySize = *newDisplaySize;
|
Bug 1647034 - Change how the visual viewport is updated to eliminate spurious reflows. r=tnikkel
There's two code changes in this patch:
- The update to the visual viewport that was happening just before positioning
the fixed items gets moved to happen after determining the scrollbars for
the root scrollframe. This moves it a little bit earlier, to basically the
earliest point at which the visual viewport can actually be computed, since
it depends on the presence of the root scrollframe's scrollbars.
More importantly, this change sets the visual viewport without checking to
see if one was already set, as the old code did. This means every reflow
of the root scrollframe on a presShell with an MVM will now have a visual
viewport set. Previously the visual viewport would only get set for the first
time when the MVM got a load or first-paint event, and then would get updated
for subsequent reflows. The net effect here is that the visual viewport is
set earlier, and this can sometimes eliminate extra reflows from after the
load event, because everything is already in a consistent state.
- The NotifyResizeReflow call to MVM is replaced by a NotifyReflow call that
runs before every reflow, instead of just on resizes. Note that the
NotifyReflow also doesn't update the visual viewport like NotifyResizeReflow
used to do, because that is taken care of by the above-mentioned code change
to set the visual viewport.
This is desirable because there are things that run during reflow that attempt
to read the display size from the MVM, and they were getting a zero size
for reflows that happened before the first resize or load/first-paint events.
Now they get a valid display size on every reflow, and so again this allows
fewer overall reflows as the code converges to a stable state faster.
Together these changes ensure that every reflow has access to up-to-date
properties (display size, mobile viewport size, visual viewport size) from the
MVM. This eliminates unnecessary reflows because of out-of-order computations
based on stale values and such. Therefore the number of reflows goes down,
which is reflected by the changes to the crashtest assertion counts.
Differential Revision: https://phabricator.services.mozilla.com/D81375
2020-06-29 21:39:57 +03:00
|
|
|
MVM_LOG("%p: Reflow starting, display size updated to %s\n", this,
|
2020-09-29 14:54:33 +03:00
|
|
|
ToString(mDisplaySize).c_str());
|
2020-06-15 16:32:44 +03:00
|
|
|
|
|
|
|
if (mDisplaySize.width == 0 || mDisplaySize.height == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
|
|
|
|
mMobileViewportSize = viewportInfo.GetSize();
|
|
|
|
MVM_LOG("%p: MVSize updated to %s\n", this,
|
2020-09-29 14:54:33 +03:00
|
|
|
ToString(mMobileViewportSize).c_str());
|
2020-06-15 16:32:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
void MobileViewportManager::RefreshViewportSize(bool aForceAdjustResolution) {
|
|
|
|
// This function gets called by the various triggers that may result in a
|
|
|
|
// change of the CSS viewport. In some of these cases (e.g. the meta-viewport
|
|
|
|
// tag changes) we want to update the resolution and in others (e.g. the full
|
|
|
|
// zoom changing) we don't want to update the resolution. See the comment in
|
2020-06-25 02:45:00 +03:00
|
|
|
// UpdateResolutionForViewportSizeChange for some more detail on this.
|
|
|
|
// An important assumption we
|
2015-07-21 17:51:55 +03:00
|
|
|
// make here is that this RefreshViewportSize function will be called
|
|
|
|
// separately for each trigger that changes. For instance it should never get
|
|
|
|
// called such that both the full zoom and the meta-viewport tag have changed;
|
|
|
|
// instead it would get called twice - once after each trigger changes. This
|
|
|
|
// assumption is what allows the aForceAdjustResolution parameter to work as
|
|
|
|
// intended; if this assumption is violated then we will need to add extra
|
2020-06-25 02:45:00 +03:00
|
|
|
// complicated logic in UpdateResolutionForViewportSizeChange to ensure we
|
|
|
|
// only do the resolution update in the right scenarios.
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
Maybe<float> displayWidthChangeRatio;
|
2019-04-05 01:11:40 +03:00
|
|
|
if (Maybe<LayoutDeviceIntSize> newDisplaySize =
|
|
|
|
mContext->GetContentViewerSize()) {
|
2020-06-25 02:45:00 +03:00
|
|
|
// See the comment in UpdateResolutionForViewportSizeChange for why we're
|
|
|
|
// doing this.
|
2015-07-21 17:51:55 +03:00
|
|
|
if (mDisplaySize.width > 0) {
|
|
|
|
if (aForceAdjustResolution ||
|
2019-04-05 01:11:40 +03:00
|
|
|
mDisplaySize.width != newDisplaySize->width) {
|
2015-07-21 17:51:55 +03:00
|
|
|
displayWidthChangeRatio =
|
2019-04-05 01:11:40 +03:00
|
|
|
Some((float)newDisplaySize->width / (float)mDisplaySize.width);
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
} else if (aForceAdjustResolution) {
|
|
|
|
displayWidthChangeRatio = Some(1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
MVM_LOG("%p: Display width change ratio is %f\n", this,
|
|
|
|
displayWidthChangeRatio.valueOr(0.0f));
|
2019-04-05 01:11:40 +03:00
|
|
|
mDisplaySize = *newDisplaySize;
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MVM_LOG("%p: Computing CSS viewport using %d,%d\n", this, mDisplaySize.width,
|
|
|
|
mDisplaySize.height);
|
|
|
|
if (mDisplaySize.width == 0 || mDisplaySize.height == 0) {
|
|
|
|
// We can't do anything useful here, we should just bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
2019-04-05 01:11:40 +03:00
|
|
|
nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
|
2020-05-06 17:40:23 +03:00
|
|
|
MVM_LOG("%p: viewport info has zooms min=%f max=%f default=%f,valid=%d\n",
|
|
|
|
this, viewportInfo.GetMinZoom().scale,
|
|
|
|
viewportInfo.GetMaxZoom().scale, viewportInfo.GetDefaultZoom().scale,
|
|
|
|
viewportInfo.IsDefaultZoomValid());
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
CSSSize viewport = viewportInfo.GetSize();
|
2020-09-29 14:54:33 +03:00
|
|
|
MVM_LOG("%p: Computed CSS viewport %s\n", this, ToString(viewport).c_str());
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
if (!mIsFirstPaint && mMobileViewportSize == viewport) {
|
|
|
|
// Nothing changed, so no need to do a reflow
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's the first-paint or the viewport changed, we need to update
|
|
|
|
// various APZ properties (the zoom and some things that might depend on it)
|
|
|
|
MVM_LOG("%p: Updating properties because %d || %d\n", this, mIsFirstPaint,
|
|
|
|
mMobileViewportSize != viewport);
|
|
|
|
|
2020-06-12 04:15:46 +03:00
|
|
|
if (mManagerType == ManagerType::VisualAndMetaViewport &&
|
|
|
|
(aForceAdjustResolution || mContext->AllowZoomingForDocument())) {
|
2020-06-12 04:15:48 +03:00
|
|
|
MVM_LOG("%p: Updating resolution because %d || %d\n", this,
|
|
|
|
aForceAdjustResolution, mContext->AllowZoomingForDocument());
|
2020-06-25 02:45:00 +03:00
|
|
|
if (mIsFirstPaint) {
|
|
|
|
UpdateResolutionForFirstPaint(viewport);
|
|
|
|
} else {
|
|
|
|
UpdateResolutionForViewportSizeChange(viewport, displayWidthChangeRatio);
|
|
|
|
}
|
2019-01-17 00:56:54 +03:00
|
|
|
} else {
|
|
|
|
// Even without zoom, we need to update that the visual viewport size
|
|
|
|
// has changed.
|
2020-06-12 04:15:48 +03:00
|
|
|
MVM_LOG("%p: Updating VV size\n", this);
|
2019-01-17 00:56:54 +03:00
|
|
|
RefreshVisualViewportSize();
|
2015-08-24 20:45:44 +03:00
|
|
|
}
|
2015-08-24 20:45:45 +03:00
|
|
|
if (gfxPlatform::AsyncPanZoomEnabled()) {
|
|
|
|
UpdateDisplayPortMargins();
|
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
// Update internal state.
|
|
|
|
mMobileViewportSize = viewport;
|
|
|
|
|
2020-06-12 04:15:46 +03:00
|
|
|
if (mManagerType == ManagerType::VisualViewportOnly) {
|
2020-06-12 04:15:48 +03:00
|
|
|
MVM_LOG("%p: Visual-only, so aborting before reflow\n", this);
|
2020-10-14 22:26:27 +03:00
|
|
|
mIsFirstPaint = false;
|
2020-06-12 04:15:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-08 23:26:08 +03:00
|
|
|
RefPtr<MobileViewportManager> strongThis(this);
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
// Kick off a reflow.
|
2020-06-12 04:15:48 +03:00
|
|
|
MVM_LOG("%p: Triggering reflow with viewport %s\n", this,
|
2020-09-29 14:54:33 +03:00
|
|
|
ToString(viewport).c_str());
|
Bug 1551659 - Remove MVMContext::ResizeEventFlag and related code. r=botond,hiro
D46944 / bug 1583534 is what fixes the root cause of bug 1528052 by not
having the first call to ResizeReflow have a wrong old size of 0x0.
This removes the code that bug introduces to suppress resize events, which
fixes this bug. I think our behavior now is pretty sane.
In particular, consider the test-case:
<!doctype html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<a href="" target="_blank">Open me in a separate tab</a>
<pre id="log"></pre>
<script>
// This shouldn't be needed, but otherwise Fenix doesn't show the tooltip on
// longpress...
document.querySelector("a").href = location.href;
function logSize() {
log.innerText += window.innerWidth + "x" + window.innerHeight + "\n";
}
logSize();
onresize = logSize;
</script>
(Hosted at https://crisal.io/tmp/gecko-mobile-resize.html for convenience)
Right now on trunk, when you click the link from GVE or Fenix, we're only
getting an initial size of 0x0 (which is not great, btw), and only after first
paint we get the real device size, but content doesn't get a resize event.
This is obviously wrong, every time the layout viewport changes we should fire
resize events.
Pages that get opened in new tabs and get refreshed when resized may get an
extra reload with this approach, but this seems not avoidable unless widget sets
the viewport size right in advance (which from discussion with snorp and agi
doesn't seem possible in the general case).
What used to happen is that we were triggering a redundant resize reflow from
the initial paint which didn't update the layout viewport (because the content
viewer and co had the right viewport from the previous navigation).
Now that we optimize those away, I think our behavior should be correct.
Differential Revision: https://phabricator.services.mozilla.com/D46956
--HG--
extra : moz-landing-system : lando
2019-09-25 22:35:29 +03:00
|
|
|
mContext->Reflow(viewport);
|
2018-11-20 06:56:44 +03:00
|
|
|
|
|
|
|
// We are going to fit the content to the display width if the initial-scale
|
|
|
|
// is not specied and if the content is still wider than the display width.
|
2020-06-25 01:22:17 +03:00
|
|
|
ShrinkToDisplaySizeIfNeeded();
|
2018-11-20 06:56:44 +03:00
|
|
|
|
|
|
|
mIsFirstPaint = false;
|
|
|
|
}
|
|
|
|
|
2020-06-25 01:22:17 +03:00
|
|
|
void MobileViewportManager::ShrinkToDisplaySizeIfNeeded() {
|
2019-04-05 01:11:40 +03:00
|
|
|
if (!mContext) {
|
2019-01-08 23:26:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-12 04:15:46 +03:00
|
|
|
if (mManagerType == ManagerType::VisualViewportOnly) {
|
2020-06-12 04:15:48 +03:00
|
|
|
MVM_LOG("%p: Visual-only, so aborting ShrinkToDisplaySizeIfNeeded\n", this);
|
2020-06-12 04:15:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-22 02:33:14 +03:00
|
|
|
if (!mContext->AllowZoomingForDocument() || mContext->IsInReaderMode()) {
|
2019-03-18 17:56:55 +03:00
|
|
|
// If zoom is disabled, we don't scale down wider contents to fit them
|
2018-11-20 06:56:44 +03:00
|
|
|
// into device screen because users won't be able to zoom out the tiny
|
|
|
|
// contents.
|
2019-08-22 02:33:14 +03:00
|
|
|
// We special-case reader mode, because it doesn't allow zooming, but
|
|
|
|
// the restriction is often not yet in place at the time this logic
|
|
|
|
// runs.
|
2018-11-20 06:56:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 01:11:40 +03:00
|
|
|
if (Maybe<CSSRect> scrollableRect =
|
|
|
|
mContext->CalculateScrollableRectForRSF()) {
|
2020-06-25 01:22:17 +03:00
|
|
|
MVM_LOG("%p: ShrinkToDisplaySize using scrollableRect %s\n", this,
|
2020-09-29 14:54:33 +03:00
|
|
|
ToString(scrollableRect->Size()).c_str());
|
2020-06-25 02:45:00 +03:00
|
|
|
UpdateResolutionForContentSizeChange(scrollableRect->Size());
|
2018-11-20 06:56:44 +03:00
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
2020-07-21 05:53:09 +03:00
|
|
|
|
|
|
|
CSSSize MobileViewportManager::GetIntrinsicCompositionSize() const {
|
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
ScreenIntSize compositionSize = GetCompositionSize(displaySize);
|
|
|
|
CSSToScreenScale intrinsicScale =
|
|
|
|
ComputeIntrinsicScale(mContext->GetViewportInfo(displaySize),
|
|
|
|
compositionSize, mMobileViewportSize);
|
|
|
|
|
|
|
|
return ScreenSize(compositionSize) / intrinsicScale;
|
|
|
|
}
|
2020-11-04 06:16:04 +03:00
|
|
|
|
|
|
|
ParentLayerSize MobileViewportManager::GetCompositionSizeWithoutDynamicToolbar()
|
|
|
|
const {
|
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
return ViewAs<ParentLayerPixel>(
|
|
|
|
ScreenSize(GetCompositionSize(displaySize)),
|
|
|
|
PixelCastJustification::ScreenIsParentLayerForRoot);
|
|
|
|
}
|