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"
|
|
|
|
|
2015-09-10 13:24:34 +03:00
|
|
|
#include "gfxPrefs.h"
|
2015-07-21 17:51:55 +03:00
|
|
|
#include "LayersLogging.h"
|
2016-11-30 06:14:28 +03:00
|
|
|
#include "mozilla/PresShell.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"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
#define MVM_LOG(...)
|
|
|
|
// #define MVM_LOG(...) printf_stderr("MVM: " __VA_ARGS__)
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(MobileViewportManager, nsIDOMEventListener, nsIObserver)
|
|
|
|
|
2017-11-23 09:06:15 +03:00
|
|
|
#define DOM_META_ADDED NS_LITERAL_STRING("DOMMetaAdded")
|
|
|
|
#define DOM_META_CHANGED NS_LITERAL_STRING("DOMMetaChanged")
|
|
|
|
#define FULL_ZOOM_CHANGE NS_LITERAL_STRING("FullZoomChange")
|
|
|
|
#define LOAD NS_LITERAL_STRING("load")
|
|
|
|
#define BEFORE_FIRST_PAINT NS_LITERAL_CSTRING("before-first-paint")
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
MobileViewportManager::MobileViewportManager(nsIPresShell* aPresShell,
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* aDocument)
|
2015-07-21 17:51:55 +03:00
|
|
|
: mDocument(aDocument),
|
|
|
|
mPresShell(aPresShell),
|
|
|
|
mIsFirstPaint(false),
|
2015-08-03 05:50:54 +03:00
|
|
|
mPainted(false) {
|
2015-07-21 17:51:55 +03:00
|
|
|
MOZ_ASSERT(mPresShell);
|
|
|
|
MOZ_ASSERT(mDocument);
|
|
|
|
|
|
|
|
MVM_LOG("%p: creating with presShell %p document %p\n", this, mPresShell,
|
|
|
|
aDocument);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
if (nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow()) {
|
2015-07-21 17:51:55 +03:00
|
|
|
mEventTarget = window->GetChromeEventHandler();
|
|
|
|
}
|
|
|
|
if (mEventTarget) {
|
|
|
|
mEventTarget->AddEventListener(DOM_META_ADDED, this, false);
|
2015-08-24 22:35:00 +03:00
|
|
|
mEventTarget->AddEventListener(DOM_META_CHANGED, this, false);
|
2015-07-21 17:51:55 +03:00
|
|
|
mEventTarget->AddEventListener(FULL_ZOOM_CHANGE, this, false);
|
2016-01-12 17:47:29 +03:00
|
|
|
mEventTarget->AddEventListener(LOAD, this, true);
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MobileViewportManager::~MobileViewportManager() {}
|
|
|
|
|
|
|
|
void MobileViewportManager::Destroy() {
|
|
|
|
MVM_LOG("%p: destroying\n", this);
|
|
|
|
|
|
|
|
if (mEventTarget) {
|
|
|
|
mEventTarget->RemoveEventListener(DOM_META_ADDED, this, false);
|
2015-08-24 22:35:00 +03:00
|
|
|
mEventTarget->RemoveEventListener(DOM_META_CHANGED, this, false);
|
2015-07-21 17:51:55 +03:00
|
|
|
mEventTarget->RemoveEventListener(FULL_ZOOM_CHANGE, this, false);
|
2016-01-12 17:47:29 +03:00
|
|
|
mEventTarget->RemoveEventListener(LOAD, this, true);
|
2015-07-21 17:51:55 +03:00
|
|
|
mEventTarget = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, BEFORE_FIRST_PAINT.Data());
|
|
|
|
}
|
|
|
|
|
|
|
|
mDocument = nullptr;
|
|
|
|
mPresShell = nullptr;
|
|
|
|
}
|
|
|
|
|
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-01-08 23:26:08 +03:00
|
|
|
if (!mDocument || !mPresShell) {
|
|
|
|
return 1.f;
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:18:42 +03:00
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
CSSToScreenScale intrinsicScale =
|
|
|
|
ComputeIntrinsicScale(mDocument->GetViewportInfo(displaySize),
|
|
|
|
displaySize, mMobileViewportSize);
|
|
|
|
CSSToLayoutDeviceScale cssToDev =
|
|
|
|
mPresShell->GetPresContext()->CSSToDevPixelScale();
|
|
|
|
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 =
|
2018-12-30 05:15:42 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
void MobileViewportManager::RequestReflow() {
|
|
|
|
MVM_LOG("%p: got a reflow request\n", this);
|
|
|
|
RefreshViewportSize(false);
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:53:14 +03:00
|
|
|
void MobileViewportManager::ResolutionUpdated() {
|
|
|
|
MVM_LOG("%p: resolution updated\n", this);
|
2019-01-08 23:26:08 +03:00
|
|
|
|
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-29 19:26:37 +03:00
|
|
|
if (!mPainted) {
|
|
|
|
// Save the value, so our default zoom calculation
|
|
|
|
// can take it into account later on.
|
|
|
|
SetRestoreResolution(mPresShell->GetResolution());
|
|
|
|
}
|
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)) {
|
|
|
|
MVM_LOG("%p: got a dom-meta-added event\n", this);
|
2015-08-03 05:50:54 +03:00
|
|
|
RefreshViewportSize(mPainted);
|
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);
|
2015-07-21 17:51:55 +03:00
|
|
|
} else if (type.Equals(FULL_ZOOM_CHANGE)) {
|
|
|
|
MVM_LOG("%p: got a full-zoom-change event\n", this);
|
|
|
|
RefreshViewportSize(false);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MobileViewportManager::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
2019-01-08 23:26:08 +03:00
|
|
|
if (!mDocument) {
|
2019-01-08 23:42:17 +03:00
|
|
|
return NS_OK;
|
2019-01-08 23:26:08 +03:00
|
|
|
}
|
|
|
|
|
2018-12-26 05:32:55 +03:00
|
|
|
if (SameCOMIdentity(aSubject, ToSupports(mDocument)) &&
|
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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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) {
|
2016-07-02 23:15:19 +03:00
|
|
|
float cssViewportChangeRatio = (aOldViewport.width == 0)
|
|
|
|
? 1.0f
|
|
|
|
: aNewViewport.width / aOldViewport.width;
|
2018-11-30 04:01:13 +03:00
|
|
|
CSSToScreenScale newZoom(aZoom.scale * aDisplayWidthChangeRatio /
|
2016-07-02 23:15:19 +03:00
|
|
|
cssViewportChangeRatio);
|
2018-11-30 04:01:13 +03:00
|
|
|
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);
|
2016-07-02 23:15:19 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
void MobileViewportManager::UpdateResolution(
|
|
|
|
const nsViewportInfo& aViewportInfo, const ScreenIntSize& aDisplaySize,
|
2018-11-30 03:58:56 +03:00
|
|
|
const CSSSize& aViewportOrContentSize,
|
|
|
|
const Maybe<float>& aDisplayWidthChangeRatio, UpdateType aType) {
|
2019-01-08 23:26:08 +03:00
|
|
|
if (!mPresShell || !mDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-29 03:14:26 +03:00
|
|
|
CSSToLayoutDeviceScale cssToDev =
|
|
|
|
mPresShell->GetPresContext()->CSSToDevPixelScale();
|
2015-11-23 17:14:18 +03:00
|
|
|
LayoutDeviceToLayerScale res(mPresShell->GetResolution());
|
2018-11-30 04:01:13 +03:00
|
|
|
CSSToScreenScale zoom = ResolutionToZoom(res, cssToDev);
|
2018-11-30 03:56:49 +03:00
|
|
|
Maybe<CSSToScreenScale> newZoom;
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2018-11-30 03:58:56 +03:00
|
|
|
ScreenIntSize compositionSize = GetCompositionSize(aDisplaySize);
|
|
|
|
CSSToScreenScale intrinsicScale = ComputeIntrinsicScale(
|
|
|
|
aViewportInfo, compositionSize, aViewportOrContentSize);
|
|
|
|
|
|
|
|
if (aType == UpdateType::ViewportSize) {
|
|
|
|
const CSSSize& viewportSize = aViewportOrContentSize;
|
|
|
|
if (mIsFirstPaint) {
|
|
|
|
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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-30 03:58:56 +03:00
|
|
|
restoreZoom = ScaleZoomWithDisplayWidth(
|
|
|
|
restoreZoom, restoreDisplayWidthChangeRatio, viewportSize,
|
|
|
|
prevViewport);
|
|
|
|
}
|
|
|
|
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 = intrinsicScale;
|
|
|
|
}
|
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
|
|
|
}
|
2018-11-30 03:58:56 +03:00
|
|
|
MOZ_ASSERT(aViewportInfo.GetMinZoom() <= defaultZoom &&
|
|
|
|
defaultZoom <= aViewportInfo.GetMaxZoom());
|
|
|
|
|
|
|
|
// On first paint, we always consider the zoom to have changed.
|
|
|
|
newZoom = Some(defaultZoom);
|
2016-05-17 21:50:08 +03:00
|
|
|
} else {
|
2018-11-30 03:58:56 +03:00
|
|
|
// 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
|
2018-12-05 21:44:05 +03:00
|
|
|
// (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.
|
2018-11-30 03:58:56 +03:00
|
|
|
// 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 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
|
2018-12-05 21:44:05 +03:00
|
|
|
// viewport or a fixed size viewport)
|
2018-11-30 03:58:56 +03:00
|
|
|
// 2. screen size changes, CSS viewport also does (pages with a
|
2018-12-05 21:44:05 +03:00
|
|
|
// device-width viewport)
|
2018-11-30 03:58:56 +03:00
|
|
|
// 3. screen size remains constant, but CSS viewport changes (meta
|
2018-12-05 21:44:05 +03:00
|
|
|
// viewport tag is added or removed)
|
2018-11-30 03:58:56 +03:00
|
|
|
// 4. neither screen size nor CSS viewport changes
|
|
|
|
if (aDisplayWidthChangeRatio) {
|
|
|
|
newZoom = Some(
|
|
|
|
ScaleZoomWithDisplayWidth(zoom, aDisplayWidthChangeRatio.value(),
|
|
|
|
viewportSize, mMobileViewportSize));
|
2015-10-03 06:08:18 +03:00
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
2018-11-30 03:58:56 +03:00
|
|
|
} else { // aType == UpdateType::ContentSize
|
|
|
|
MOZ_ASSERT(aType == UpdateType::ContentSize);
|
|
|
|
MOZ_ASSERT(aDisplayWidthChangeRatio.isNothing());
|
2018-11-30 03:59:11 +03:00
|
|
|
|
|
|
|
// We try to scale down the contents only IF the document has no
|
|
|
|
// initial-scale AND IF it's the initial paint AND IF it's not restored
|
|
|
|
// documents.
|
|
|
|
if (mIsFirstPaint && !mRestoreResolution &&
|
|
|
|
!aViewportInfo.IsDefaultZoomValid()) {
|
|
|
|
if (zoom != intrinsicScale) {
|
|
|
|
newZoom = Some(intrinsicScale);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
if (zoom < intrinsicScale) {
|
|
|
|
newZoom = Some(intrinsicScale);
|
|
|
|
}
|
2018-11-30 03:56:49 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2018-12-01 08:34:57 +03:00
|
|
|
// If the zoom has changed, update the pres shell resolution accordingly.
|
2018-11-30 03:56:49 +03:00
|
|
|
if (newZoom) {
|
|
|
|
LayoutDeviceToLayerScale resolution = ZoomToResolution(*newZoom, cssToDev);
|
2015-07-21 17:51:55 +03:00
|
|
|
MVM_LOG("%p: setting resolution %f\n", this, resolution.scale);
|
2018-12-30 05:45:47 +03:00
|
|
|
mPresShell->SetResolutionAndScaleTo(
|
|
|
|
resolution.scale, nsIPresShell::ChangeOrigin::eMainThread);
|
2015-07-21 17:51:55 +03:00
|
|
|
|
2018-11-30 03:56:49 +03:00
|
|
|
MVM_LOG("%p: New zoom is %f\n", this, newZoom->scale);
|
2018-12-01 08:34:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The visual viewport size depends on both the zoom and the display size,
|
|
|
|
// and needs to be updated if either might have changed.
|
|
|
|
if (newZoom || aType == UpdateType::ViewportSize) {
|
|
|
|
UpdateVisualViewportSize(aDisplaySize, newZoom ? *newZoom : zoom);
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 06:56:44 +03:00
|
|
|
ScreenIntSize MobileViewportManager::GetCompositionSize(
|
|
|
|
const ScreenIntSize& aDisplaySize) const {
|
2019-01-08 23:26:08 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return ScreenIntSize();
|
|
|
|
}
|
|
|
|
|
2018-11-20 06:56:44 +03:00
|
|
|
ScreenIntSize compositionSize(aDisplaySize);
|
2015-07-21 17:51:55 +03:00
|
|
|
ScreenMargin scrollbars =
|
2015-08-07 21:23:13 +03:00
|
|
|
LayoutDeviceMargin::FromAppUnits(
|
2015-07-21 17:51:55 +03:00
|
|
|
nsLayoutUtils::ScrollbarAreaToExcludeFromCompositionBoundsFor(
|
2015-08-07 21:23:13 +03:00
|
|
|
mPresShell->GetRootScrollFrame()),
|
|
|
|
mPresShell->GetPresContext()->AppUnitsPerDevPixel())
|
|
|
|
// Scrollbars are not subject to resolution scaling, so LD pixels =
|
|
|
|
// Screen pixels for them.
|
|
|
|
* LayoutDeviceToScreenScale(1.0f);
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
compositionSize.width -= scrollbars.LeftRight();
|
|
|
|
compositionSize.height -= scrollbars.TopBottom();
|
2018-11-20 06:56:44 +03:00
|
|
|
|
|
|
|
return compositionSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MobileViewportManager::UpdateVisualViewportSize(
|
|
|
|
const ScreenIntSize& aDisplaySize, const CSSToScreenScale& aZoom) {
|
2019-01-08 23:26:08 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
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;
|
2018-07-22 22:49:38 +03:00
|
|
|
MVM_LOG("%p: Setting VVPS %s\n", this, Stringify(compSize).c_str());
|
|
|
|
nsLayoutUtils::SetVisualViewportSize(mPresShell, compSize);
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MobileViewportManager::UpdateDisplayPortMargins() {
|
2019-01-08 23:26:08 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:26:00 +03:00
|
|
|
if (nsIFrame* root = mPresShell->GetRootScrollFrame()) {
|
2015-12-17 01:22:23 +03:00
|
|
|
bool hasDisplayPort = nsLayoutUtils::HasDisplayPort(root->GetContent());
|
2018-12-10 22:32:59 +03:00
|
|
|
bool hasResolution = mPresShell->GetResolution() != 1.0f;
|
2015-11-27 19:20:04 +03:00
|
|
|
if (!hasDisplayPort && !hasResolution) {
|
|
|
|
// We only want to update the displayport if there is one already, or
|
|
|
|
// add one if there's a resolution on the document (see bug 1225508
|
|
|
|
// comment 1).
|
2015-08-27 15:26:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-03-21 08:16:41 +03:00
|
|
|
nsRect displayportBase = nsRect(
|
|
|
|
nsPoint(0, 0), nsLayoutUtils::CalculateCompositionSizeForFrame(root));
|
|
|
|
// We only create MobileViewportManager for root content documents. If that
|
|
|
|
// ever changes we'd need to limit the size of this displayport base rect
|
|
|
|
// because non-toplevel documents have no limit on their size.
|
|
|
|
MOZ_ASSERT(mPresShell->GetPresContext()->IsRootContentDocument());
|
|
|
|
nsLayoutUtils::SetDisplayPortBaseIfNotSet(root->GetContent(),
|
|
|
|
displayportBase);
|
2015-08-27 15:26:00 +03:00
|
|
|
nsIScrollableFrame* scrollable = do_QueryFrame(root);
|
|
|
|
nsLayoutUtils::CalculateAndSetDisplayPortMargins(
|
2015-07-21 17:51:55 +03:00
|
|
|
scrollable, nsLayoutUtils::RepaintMode::DoNotRepaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-08 23:26:08 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:23:39 +03:00
|
|
|
if (!gfxPrefs::APZAllowZooming()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 21:53:14 +03:00
|
|
|
ScreenIntSize displaySize = ViewAs<ScreenPixel>(
|
|
|
|
mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
|
|
|
|
|
|
|
|
CSSToLayoutDeviceScale cssToDev =
|
|
|
|
mPresShell->GetPresContext()->CSSToDevPixelScale();
|
2015-11-23 17:14:18 +03:00
|
|
|
LayoutDeviceToLayerScale res(mPresShell->GetResolution());
|
2015-11-18 21:53:14 +03:00
|
|
|
CSSToScreenScale zoom = ViewTargetAs<ScreenPixel>(
|
|
|
|
cssToDev * res / ParentLayerToLayerScale(1),
|
|
|
|
PixelCastJustification::ScreenIsParentLayerForRoot);
|
|
|
|
|
2018-09-14 22:23:28 +03:00
|
|
|
UpdateVisualViewportSize(displaySize, zoom);
|
2015-11-18 21:53:14 +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
|
|
|
|
// UpdateResolution for some more detail on this. An important assumption we
|
|
|
|
// 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
|
|
|
|
// complicated logic in UpdateResolution to ensure we only do the resolution
|
|
|
|
// update in the right scenarios.
|
|
|
|
|
2019-01-08 23:26:08 +03:00
|
|
|
if (!mPresShell || !mDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
Maybe<float> displayWidthChangeRatio;
|
|
|
|
LayoutDeviceIntSize newDisplaySize;
|
|
|
|
if (nsLayoutUtils::GetContentViewerSize(mPresShell->GetPresContext(),
|
|
|
|
newDisplaySize)) {
|
|
|
|
// See the comment in UpdateResolution for why we're doing this.
|
|
|
|
if (mDisplaySize.width > 0) {
|
|
|
|
if (aForceAdjustResolution ||
|
|
|
|
mDisplaySize.width != newDisplaySize.width) {
|
|
|
|
displayWidthChangeRatio =
|
|
|
|
Some((float)newDisplaySize.width / (float)mDisplaySize.width);
|
|
|
|
}
|
|
|
|
} else if (aForceAdjustResolution) {
|
|
|
|
displayWidthChangeRatio = Some(1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
MVM_LOG("%p: Display width change ratio is %f\n", this,
|
|
|
|
displayWidthChangeRatio.valueOr(0.0f));
|
|
|
|
mDisplaySize = newDisplaySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-10-23 20:21:57 +03:00
|
|
|
nsViewportInfo viewportInfo = mDocument->GetViewportInfo(displaySize);
|
2015-07-21 17:51:55 +03:00
|
|
|
|
|
|
|
CSSSize viewport = viewportInfo.GetSize();
|
|
|
|
MVM_LOG("%p: Computed CSS viewport %s\n", this, Stringify(viewport).c_str());
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2015-08-24 20:45:44 +03:00
|
|
|
if (gfxPrefs::APZAllowZooming()) {
|
2018-11-30 03:58:56 +03:00
|
|
|
UpdateResolution(viewportInfo, displaySize, viewport,
|
|
|
|
displayWidthChangeRatio, UpdateType::ViewportSize);
|
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
|
|
|
|
2016-05-12 03:06:11 +03:00
|
|
|
CSSSize oldSize = mMobileViewportSize;
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
// Update internal state.
|
|
|
|
mMobileViewportSize = viewport;
|
|
|
|
|
2019-01-08 23:26:08 +03:00
|
|
|
RefPtr<MobileViewportManager> strongThis(this);
|
|
|
|
|
2015-07-21 17:51:55 +03:00
|
|
|
// Kick off a reflow.
|
|
|
|
mPresShell->ResizeReflowIgnoreOverride(
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(viewport.width),
|
2016-05-12 03:06:11 +03:00
|
|
|
nsPresContext::CSSPixelsToAppUnits(viewport.height),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(oldSize.width),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(oldSize.height));
|
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.
|
|
|
|
ShrinkToDisplaySizeIfNeeded(viewportInfo, displaySize);
|
|
|
|
|
|
|
|
mIsFirstPaint = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MobileViewportManager::ShrinkToDisplaySizeIfNeeded(
|
|
|
|
nsViewportInfo& aViewportInfo, const ScreenIntSize& aDisplaySize) {
|
2019-01-08 23:26:08 +03:00
|
|
|
if (!mPresShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-20 06:56:44 +03:00
|
|
|
if (!gfxPrefs::APZAllowZooming()) {
|
|
|
|
// If the APZ is disabled, we don't scale down wider contents to fit them
|
|
|
|
// into device screen because users won't be able to zoom out the tiny
|
|
|
|
// contents.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScrollableFrame* rootScrollableFrame =
|
|
|
|
mPresShell->GetRootScrollFrameAsScrollable();
|
|
|
|
if (rootScrollableFrame) {
|
|
|
|
nsRect scrollableRect = nsLayoutUtils::CalculateScrollableRectForFrame(
|
|
|
|
rootScrollableFrame, nullptr);
|
|
|
|
CSSSize contentSize = CSSSize::FromAppUnits(scrollableRect.Size());
|
2018-11-30 03:58:56 +03:00
|
|
|
UpdateResolution(aViewportInfo, aDisplaySize, contentSize, Nothing(),
|
|
|
|
UpdateType::ContentSize);
|
2018-11-20 06:56:44 +03:00
|
|
|
}
|
2015-07-21 17:51:55 +03:00
|
|
|
}
|