Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / layout / base / MobileViewportManager.cpp
blob0dcb57bd26eaea6d265c2018824a254cd7ee47fc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "MobileViewportManager.h"
9 #include "mozilla/PresShell.h"
10 #include "mozilla/ToString.h"
11 #include "mozilla/dom/Document.h"
12 #include "mozilla/dom/Event.h"
13 #include "mozilla/dom/EventTarget.h"
14 #include "nsIFrame.h"
15 #include "nsLayoutUtils.h"
16 #include "nsViewManager.h"
17 #include "nsViewportInfo.h"
18 #include "UnitTransforms.h"
20 mozilla::LazyLogModule MobileViewportManager::gLog("apz.mobileviewport");
21 #define MVM_LOG(...) \
22 MOZ_LOG(MobileViewportManager::gLog, LogLevel::Debug, (__VA_ARGS__))
24 NS_IMPL_ISUPPORTS(MobileViewportManager, nsIDOMEventListener, nsIObserver)
26 #define DOM_META_ADDED u"DOMMetaAdded"_ns
27 #define DOM_META_CHANGED u"DOMMetaChanged"_ns
28 #define FULLSCREEN_CHANGED u"fullscreenchange"_ns
29 #define LOAD u"load"_ns
30 #define BEFORE_FIRST_PAINT "before-first-paint"_ns
32 using namespace mozilla;
33 using namespace mozilla::dom;
34 using namespace mozilla::layers;
36 MobileViewportManager::MobileViewportManager(MVMContext* aContext,
37 ManagerType aType)
38 : mContext(aContext),
39 mManagerType(aType),
40 mIsFirstPaint(false),
41 mPainted(false) {
42 MOZ_ASSERT(mContext);
44 MVM_LOG("%p: creating with context %p\n", this, mContext.get());
46 mContext->AddEventListener(DOM_META_ADDED, this, false);
47 mContext->AddEventListener(DOM_META_CHANGED, this, false);
48 mContext->AddEventListener(FULLSCREEN_CHANGED, this, false);
49 mContext->AddEventListener(LOAD, this, true);
51 mContext->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false);
53 // We need to initialize the display size and the CSS viewport size before
54 // the initial reflow happens.
55 UpdateSizesBeforeReflow();
58 MobileViewportManager::~MobileViewportManager() = default;
60 void MobileViewportManager::Destroy() {
61 MVM_LOG("%p: destroying\n", this);
63 mContext->RemoveEventListener(DOM_META_ADDED, this, false);
64 mContext->RemoveEventListener(DOM_META_CHANGED, this, false);
65 mContext->RemoveEventListener(FULLSCREEN_CHANGED, this, false);
66 mContext->RemoveEventListener(LOAD, this, true);
68 mContext->RemoveObserver(this, BEFORE_FIRST_PAINT.Data());
70 mContext->Destroy();
71 mContext = nullptr;
74 void MobileViewportManager::SetRestoreResolution(
75 float aResolution, LayoutDeviceIntSize aDisplaySize) {
76 SetRestoreResolution(aResolution);
77 ScreenIntSize restoreDisplaySize = ViewAs<ScreenPixel>(
78 aDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
79 mRestoreDisplaySize = Some(restoreDisplaySize);
82 void MobileViewportManager::SetRestoreResolution(float aResolution) {
83 mRestoreResolution = Some(aResolution);
86 float MobileViewportManager::ComputeIntrinsicResolution() const {
87 if (!mContext) {
88 return 1.f;
91 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
92 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
93 CSSToScreenScale intrinsicScale = ComputeIntrinsicScale(
94 mContext->GetViewportInfo(displaySize), displaySize, mMobileViewportSize);
95 CSSToLayoutDeviceScale cssToDev = mContext->CSSToDevPixelScale();
96 return (intrinsicScale / cssToDev).scale;
99 mozilla::CSSToScreenScale MobileViewportManager::ComputeIntrinsicScale(
100 const nsViewportInfo& aViewportInfo,
101 const mozilla::ScreenIntSize& aDisplaySize,
102 const mozilla::CSSSize& aViewportOrContentSize) const {
103 CSSToScreenScale intrinsicScale =
104 aViewportOrContentSize.IsEmpty()
105 ? CSSToScreenScale(1.0)
106 : MaxScaleRatio(ScreenSize(aDisplaySize), aViewportOrContentSize);
107 MVM_LOG("%p: Intrinsic computed zoom is %f\n", this, intrinsicScale.scale);
108 return ClampZoom(intrinsicScale, aViewportInfo);
111 void MobileViewportManager::RequestReflow(bool aForceAdjustResolution) {
112 MVM_LOG("%p: got a reflow request with force resolution: %d\n", this,
113 aForceAdjustResolution);
114 RefreshViewportSize(aForceAdjustResolution);
117 void MobileViewportManager::ResolutionUpdated(
118 mozilla::ResolutionChangeOrigin aOrigin) {
119 MVM_LOG("%p: resolution updated\n", this);
121 if (!mContext) {
122 return;
125 if ((!mPainted &&
126 aOrigin == mozilla::ResolutionChangeOrigin::MainThreadRestore) ||
127 aOrigin == mozilla::ResolutionChangeOrigin::Test) {
128 // Save the value, so our default zoom calculation
129 // can take it into account later on.
130 SetRestoreResolution(mContext->GetResolution());
132 RefreshVisualViewportSize();
135 NS_IMETHODIMP
136 MobileViewportManager::HandleEvent(dom::Event* event) {
137 nsAutoString type;
138 event->GetType(type);
140 if (type.Equals(DOM_META_ADDED)) {
141 HandleDOMMetaAdded();
142 } else if (type.Equals(DOM_META_CHANGED)) {
143 MVM_LOG("%p: got a dom-meta-changed event\n", this);
144 RefreshViewportSize(mPainted);
145 } else if (type.Equals(FULLSCREEN_CHANGED)) {
146 MVM_LOG("%p: got a fullscreenchange event\n", this);
147 RefreshViewportSize(mPainted);
148 } else if (type.Equals(LOAD)) {
149 MVM_LOG("%p: got a load event\n", this);
150 if (!mPainted) {
151 // Load event got fired before the before-first-paint message
152 SetInitialViewport();
155 return NS_OK;
158 void MobileViewportManager::HandleDOMMetaAdded() {
159 MVM_LOG("%p: got a dom-meta-added event\n", this);
160 if (mPainted && mContext->IsDocumentLoading()) {
161 // It's possible that we get a DOMMetaAdded event after the page
162 // has already been painted, but before the document finishes loading.
163 // In such a case, we've already run SetInitialViewport() on
164 // "before-first-paint", and won't run it again on "load" (because
165 // mPainted=true). But that SetInitialViewport() call didn't know the
166 // "initial-scale" from this meta viewport tag. To ensure we respect
167 // the "initial-scale", call SetInitialViewport() again.
168 // Note: It's important that we only do this if mPainted=true. In the
169 // usual case, we get the DOMMetaAdded before the first paint, sometimes
170 // even before we have a frame tree, and calling SetInitialViewport()
171 // before we have a frame tree will skip some important steps (e.g.
172 // updating display port margins).
173 SetInitialViewport();
174 } else {
175 RefreshViewportSize(mPainted);
179 NS_IMETHODIMP
180 MobileViewportManager::Observe(nsISupports* aSubject, const char* aTopic,
181 const char16_t* aData) {
182 if (!mContext) {
183 return NS_OK;
186 if (mContext->SubjectMatchesDocument(aSubject) &&
187 BEFORE_FIRST_PAINT.EqualsASCII(aTopic)) {
188 MVM_LOG("%p: got a before-first-paint event\n", this);
189 if (!mPainted) {
190 // before-first-paint message arrived before load event
191 SetInitialViewport();
194 return NS_OK;
197 void MobileViewportManager::SetInitialViewport() {
198 MVM_LOG("%p: setting initial viewport\n", this);
199 mIsFirstPaint = true;
200 mPainted = true;
201 RefreshViewportSize(false);
204 CSSToScreenScale MobileViewportManager::ClampZoom(
205 const CSSToScreenScale& aZoom, const nsViewportInfo& aViewportInfo) const {
206 CSSToScreenScale zoom = aZoom;
207 if (std::isnan(zoom.scale)) {
208 NS_ERROR("Don't pass NaN to ClampZoom; check caller for 0/0 division");
209 zoom = CSSToScreenScale(1.0);
212 if (zoom < aViewportInfo.GetMinZoom()) {
213 zoom = aViewportInfo.GetMinZoom();
214 MVM_LOG("%p: Clamped to %f\n", this, zoom.scale);
216 if (zoom > aViewportInfo.GetMaxZoom()) {
217 zoom = aViewportInfo.GetMaxZoom();
218 MVM_LOG("%p: Clamped to %f\n", this, zoom.scale);
221 // Non-positive zoom factors can produce NaN or negative viewport sizes,
222 // so we better be sure we've got a positive zoom factor. Just for good
223 // measure, we check our min/max as well as the final clamped value.
224 MOZ_ASSERT(aViewportInfo.GetMinZoom() > CSSToScreenScale(0.0f),
225 "zoom factor must be positive");
226 MOZ_ASSERT(aViewportInfo.GetMaxZoom() > CSSToScreenScale(0.0f),
227 "zoom factor must be positive");
228 MOZ_ASSERT(zoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
229 return zoom;
232 CSSToScreenScale MobileViewportManager::ScaleZoomWithDisplayWidth(
233 const CSSToScreenScale& aZoom, const float& aDisplayWidthChangeRatio,
234 const CSSSize& aNewViewport, const CSSSize& aOldViewport) {
235 float inverseCssWidthChangeRatio =
236 (aNewViewport.width == 0) ? 1.0f
237 : aOldViewport.width / aNewViewport.width;
238 CSSToScreenScale newZoom(aZoom.scale * aDisplayWidthChangeRatio *
239 inverseCssWidthChangeRatio);
240 MVM_LOG("%p: Old zoom was %f, changed by %f * %f to %f\n", this, aZoom.scale,
241 aDisplayWidthChangeRatio, inverseCssWidthChangeRatio, newZoom.scale);
242 return newZoom;
245 CSSToScreenScale MobileViewportManager::ResolutionToZoom(
246 const LayoutDeviceToLayerScale& aResolution) const {
247 return ViewTargetAs<ScreenPixel>(
248 mContext->CSSToDevPixelScale() * aResolution / ParentLayerToLayerScale(1),
249 PixelCastJustification::ScreenIsParentLayerForRoot);
252 LayoutDeviceToLayerScale MobileViewportManager::ZoomToResolution(
253 const CSSToScreenScale& aZoom) const {
254 return ViewTargetAs<ParentLayerPixel>(
255 aZoom, PixelCastJustification::ScreenIsParentLayerForRoot) /
256 mContext->CSSToDevPixelScale() * ParentLayerToLayerScale(1);
259 void MobileViewportManager::UpdateResolutionForFirstPaint(
260 const CSSSize& aViewportSize) {
261 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
262 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
263 nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
264 ScreenIntSize compositionSize = GetCompositionSize(displaySize);
266 if (mRestoreResolution) {
267 LayoutDeviceToLayerScale restoreResolution(*mRestoreResolution);
268 CSSToScreenScale restoreZoom = ResolutionToZoom(restoreResolution);
269 if (mRestoreDisplaySize) {
270 CSSSize prevViewport =
271 mContext->GetViewportInfo(*mRestoreDisplaySize).GetSize();
272 float restoreDisplayWidthChangeRatio =
273 (mRestoreDisplaySize->width > 0)
274 ? (float)compositionSize.width / (float)mRestoreDisplaySize->width
275 : 1.0f;
277 restoreZoom =
278 ScaleZoomWithDisplayWidth(restoreZoom, restoreDisplayWidthChangeRatio,
279 aViewportSize, prevViewport);
281 MVM_LOG("%p: restored zoom is %f\n", this, restoreZoom.scale);
282 restoreZoom = ClampZoom(restoreZoom, viewportInfo);
284 ApplyNewZoom(displaySize, restoreZoom);
285 return;
288 CSSToScreenScale defaultZoom = viewportInfo.GetDefaultZoom();
289 MVM_LOG("%p: default zoom from viewport is %f\n", this, defaultZoom.scale);
290 if (!viewportInfo.IsDefaultZoomValid()) {
291 CSSSize contentSize = aViewportSize;
292 if (Maybe<CSSRect> scrollableRect =
293 mContext->CalculateScrollableRectForRSF()) {
294 contentSize = scrollableRect->Size();
296 defaultZoom =
297 ComputeIntrinsicScale(viewportInfo, compositionSize, contentSize);
299 MOZ_ASSERT(viewportInfo.GetMinZoom() <= defaultZoom &&
300 defaultZoom <= viewportInfo.GetMaxZoom());
302 ApplyNewZoom(displaySize, defaultZoom);
305 void MobileViewportManager::UpdateResolutionForViewportSizeChange(
306 const CSSSize& aViewportSize,
307 const Maybe<float>& aDisplayWidthChangeRatio) {
308 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
309 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
310 nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
312 CSSToScreenScale zoom = GetZoom();
313 // Non-positive zoom factors can produce NaN or negative viewport sizes,
314 // so we better be sure we've got a positive zoom factor.
315 MOZ_ASSERT(zoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
317 MOZ_ASSERT(!mIsFirstPaint);
319 // If this is not a first paint, then in some cases we want to update the
320 // pre- existing resolution so as to maintain how much actual content is
321 // visible within the display width. Note that "actual content" may be
322 // different with respect to CSS pixels because of the CSS viewport size
323 // changing.
325 // aDisplayWidthChangeRatio is non-empty if:
326 // (a) The meta-viewport tag information changes, and so the CSS viewport
327 // might change as a result. If this happens after the content has
328 // been painted, we want to adjust the zoom to compensate. OR
329 // (b) The display size changed from a nonzero value to another
330 // nonzero value. This covers the case where e.g. the device was
331 // rotated, and again we want to adjust the zoom to compensate.
332 // Note in particular that aDisplayWidthChangeRatio will be None if all
333 // that happened was a change in the full-zoom. In this case, we still
334 // want to compute a new CSS and visual viewport, but we don't want to update
335 // the resolution.
337 // Given the above, the algorithm below accounts for all types of changes
338 // I can conceive of:
339 // 1. screen size changes, CSS viewport does not (pages with no meta
340 // viewport or a fixed size viewport)
341 // 2. screen size changes, CSS viewport also does (pages with a
342 // device-width viewport)
343 // 3. screen size remains constant, but CSS viewport changes (meta
344 // viewport tag is added or removed)
345 // 4. neither screen size nor CSS viewport changes
347 if (!aDisplayWidthChangeRatio) {
348 UpdateVisualViewportSize(displaySize, zoom);
349 return;
352 // One more complication is that our current zoom level may be the
353 // result of clamping to either the minimum or maximum zoom level
354 // allowed by the viewport. If we naively scale the zoom level with
355 // the change in the display width, we might be scaling one of these
356 // previously clamped values. What we really want to do is to make
357 // scaling of the zoom aware of these minimum and maximum clamping
358 // points for the existing content size, so that we keep display
359 // width changes completely reversible.
361 // We don't consider here if we are scaling to a zoom value outside
362 // of our viewport limits, because we'll clamp to the viewport limits
363 // as a final step.
365 // Because of the behavior of ShrinkToDisplaySizeIfNeeded, we are
366 // choosing zoom clamping points based on the content size of the
367 // scrollable rect, which might different from aViewportSize.
368 CSSSize contentSize = aViewportSize;
369 if (Maybe<CSSRect> scrollableRect =
370 mContext->CalculateScrollableRectForRSF()) {
371 contentSize = scrollableRect->Size();
374 // We scale the sizes, though we only care about the scaled widths.
375 ScreenSize minZoomDisplaySize = contentSize * viewportInfo.GetMinZoom();
376 ScreenSize maxZoomDisplaySize = contentSize * viewportInfo.GetMaxZoom();
378 ScreenSize newDisplaySize(displaySize);
379 ScreenSize oldDisplaySize = newDisplaySize / *aDisplayWidthChangeRatio;
381 // To calculate an adjusted ratio, we use some combination of these
382 // four values:
383 float a(minZoomDisplaySize.width);
384 float b(maxZoomDisplaySize.width);
385 float c(oldDisplaySize.width);
386 float d(newDisplaySize.width);
388 // The oldDisplaySize value is in one of three "zones":
389 // 1) Less than or equal to minZoomDisplaySize.
390 // 2) Between minZoomDisplaySize and maxZoomDisplaySize.
391 // 3) Greater than or equal to maxZoomDisplaySize.
393 // Depending on which zone each are in, the adjusted ratio is shown in
394 // the table below (using the a-b-c-d coding from above):
396 // c +---+
397 // | d |
398 // 1 | a |
399 // +---+
400 // | d |
401 // 2 | c |
402 // +---+
403 // | d |
404 // 3 | b |
405 // +---+
407 // Conveniently, the denominator is c clamped to a..b.
408 float denominator = clamped(c, a, b);
410 float adjustedRatio = d / denominator;
411 CSSToScreenScale adjustedZoom = ScaleZoomWithDisplayWidth(
412 zoom, adjustedRatio, aViewportSize, mMobileViewportSize);
413 CSSToScreenScale newZoom = ClampZoom(adjustedZoom, viewportInfo);
415 ApplyNewZoom(displaySize, newZoom);
418 void MobileViewportManager::UpdateResolutionForContentSizeChange(
419 const CSSSize& aContentSize) {
420 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
421 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
422 nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
424 CSSToScreenScale zoom = GetZoom();
425 // Non-positive zoom factors can produce NaN or negative viewport sizes,
426 // so we better be sure we've got a positive zoom factor.
427 MOZ_ASSERT(zoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
429 ScreenIntSize compositionSize = GetCompositionSize(displaySize);
430 CSSToScreenScale intrinsicScale =
431 ComputeIntrinsicScale(viewportInfo, compositionSize, aContentSize);
433 // We try to scale down the contents only IF the document has no
434 // initial-scale AND IF it's not restored documents AND IF the resolution
435 // has never been changed by APZ.
436 if (MOZ_LOG_TEST(gLog, LogLevel::Debug)) {
437 MVM_LOG("%p: conditions preventing shrink-to-fit: %d %d %d\n", this,
438 mRestoreResolution.isSome(), mContext->IsResolutionUpdatedByApz(),
439 viewportInfo.IsDefaultZoomValid());
441 if (!mRestoreResolution && !mContext->IsResolutionUpdatedByApz() &&
442 !viewportInfo.IsDefaultZoomValid()) {
443 if (zoom != intrinsicScale) {
444 ApplyNewZoom(displaySize, intrinsicScale);
446 return;
449 // Even in other scenarios, we want to ensure that zoom level is
450 // not _smaller_ than the intrinsic scale, otherwise we might be
451 // trying to show regions where there is no content to show.
452 CSSToScreenScale clampedZoom = zoom;
454 if (clampedZoom < intrinsicScale) {
455 clampedZoom = intrinsicScale;
458 // Also clamp to the restrictions imposed by viewportInfo.
459 clampedZoom = ClampZoom(clampedZoom, viewportInfo);
461 if (clampedZoom != zoom) {
462 ApplyNewZoom(displaySize, clampedZoom);
466 void MobileViewportManager::ApplyNewZoom(const ScreenIntSize& aDisplaySize,
467 const CSSToScreenScale& aNewZoom) {
468 // If the zoom has changed, update the pres shell resolution accordingly.
469 // We characterize this as MainThreadAdjustment, because we don't want our
470 // change here to be remembered as a restore resolution.
472 // Non-positive zoom factors can produce NaN or negative viewport sizes,
473 // so we better be sure we've got a positive zoom factor.
474 MOZ_ASSERT(aNewZoom > CSSToScreenScale(0.0f), "zoom factor must be positive");
476 LayoutDeviceToLayerScale resolution = ZoomToResolution(aNewZoom);
477 MVM_LOG("%p: setting resolution %f\n", this, resolution.scale);
478 mContext->SetResolutionAndScaleTo(
479 resolution.scale, ResolutionChangeOrigin::MainThreadAdjustment);
481 MVM_LOG("%p: New zoom is %f\n", this, aNewZoom.scale);
483 UpdateVisualViewportSize(aDisplaySize, aNewZoom);
486 ScreenIntSize MobileViewportManager::GetCompositionSize(
487 const ScreenIntSize& aDisplaySize) const {
488 if (!mContext) {
489 return ScreenIntSize();
492 // FIXME: Bug 1586986 - To update VisualViewport in response to the dynamic
493 // toolbar transition we probably need to include the dynamic toolbar
494 // _current_ height.
495 ScreenIntSize compositionSize(aDisplaySize);
496 ScreenMargin scrollbars =
497 mContext->ScrollbarAreaToExcludeFromCompositionBounds()
498 // Scrollbars are not subject to resolution scaling, so LD pixels =
499 // Screen pixels for them.
500 * LayoutDeviceToScreenScale(1.0f);
502 compositionSize.width =
503 std::max(0.0f, compositionSize.width - scrollbars.LeftRight());
504 compositionSize.height =
505 std::max(0.0f, compositionSize.height - scrollbars.TopBottom());
507 return compositionSize;
510 void MobileViewportManager::UpdateVisualViewportSize(
511 const ScreenIntSize& aDisplaySize, const CSSToScreenScale& aZoom) {
512 if (!mContext) {
513 return;
516 ScreenSize compositionSize = ScreenSize(GetCompositionSize(aDisplaySize));
518 CSSSize compSize = compositionSize / aZoom;
519 MVM_LOG("%p: Setting VVPS %s\n", this, ToString(compSize).c_str());
520 mContext->SetVisualViewportSize(compSize);
522 UpdateVisualViewportSizeByDynamicToolbar(mContext->GetDynamicToolbarOffset());
525 CSSToScreenScale MobileViewportManager::GetZoom() const {
526 LayoutDeviceToLayerScale res(mContext->GetResolution());
527 return ResolutionToZoom(res);
530 void MobileViewportManager::UpdateVisualViewportSizeByDynamicToolbar(
531 ScreenIntCoord aToolbarHeight) {
532 if (!mContext) {
533 return;
536 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
537 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
538 displaySize.height += aToolbarHeight;
539 nsSize compSize = CSSSize::ToAppUnits(
540 ScreenSize(GetCompositionSize(displaySize)) / GetZoom());
542 if (mVisualViewportSizeUpdatedByDynamicToolbar == compSize) {
543 return;
546 mVisualViewportSizeUpdatedByDynamicToolbar = compSize;
548 mContext->PostVisualViewportResizeEventByDynamicToolbar();
551 void MobileViewportManager::
552 UpdateVisualViewportSizeForPotentialScrollbarChange() {
553 RefreshVisualViewportSize();
556 void MobileViewportManager::UpdateDisplayPortMargins() {
557 if (!mContext) {
558 return;
560 mContext->UpdateDisplayPortMargins();
563 void MobileViewportManager::RefreshVisualViewportSize() {
564 // This function is a subset of RefreshViewportSize, and only updates the
565 // visual viewport size.
567 if (!mContext) {
568 return;
571 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
572 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
574 if (displaySize.width == 0 || displaySize.height == 0) {
575 return;
578 UpdateVisualViewportSize(displaySize, GetZoom());
581 void MobileViewportManager::UpdateSizesBeforeReflow() {
582 if (Maybe<LayoutDeviceIntSize> newDisplaySize =
583 mContext->GetDocumentViewerSize()) {
584 mDisplaySize = *newDisplaySize;
585 MVM_LOG("%p: Reflow starting, display size updated to %s\n", this,
586 ToString(mDisplaySize).c_str());
588 if (mDisplaySize.width == 0 || mDisplaySize.height == 0) {
589 return;
592 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
593 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
594 nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
595 mMobileViewportSize = viewportInfo.GetSize();
596 MVM_LOG("%p: MVSize updated to %s\n", this,
597 ToString(mMobileViewportSize).c_str());
601 void MobileViewportManager::RefreshViewportSize(bool aForceAdjustResolution) {
602 // This function gets called by the various triggers that may result in a
603 // change of the CSS viewport. In some of these cases (e.g. the meta-viewport
604 // tag changes) we want to update the resolution and in others (e.g. the full
605 // zoom changing) we don't want to update the resolution. See the comment in
606 // UpdateResolutionForViewportSizeChange for some more detail on this.
607 // An important assumption we
608 // make here is that this RefreshViewportSize function will be called
609 // separately for each trigger that changes. For instance it should never get
610 // called such that both the full zoom and the meta-viewport tag have changed;
611 // instead it would get called twice - once after each trigger changes. This
612 // assumption is what allows the aForceAdjustResolution parameter to work as
613 // intended; if this assumption is violated then we will need to add extra
614 // complicated logic in UpdateResolutionForViewportSizeChange to ensure we
615 // only do the resolution update in the right scenarios.
617 if (!mContext) {
618 return;
621 Maybe<float> displayWidthChangeRatio;
622 if (Maybe<LayoutDeviceIntSize> newDisplaySize =
623 mContext->GetDocumentViewerSize()) {
624 // See the comment in UpdateResolutionForViewportSizeChange for why we're
625 // doing this.
626 if (mDisplaySize.width > 0) {
627 if (aForceAdjustResolution ||
628 mDisplaySize.width != newDisplaySize->width) {
629 displayWidthChangeRatio =
630 Some((float)newDisplaySize->width / (float)mDisplaySize.width);
632 } else if (aForceAdjustResolution) {
633 displayWidthChangeRatio = Some(1.0f);
636 MVM_LOG("%p: Display width change ratio is %f\n", this,
637 displayWidthChangeRatio.valueOr(0.0f));
638 mDisplaySize = *newDisplaySize;
641 MVM_LOG("%p: Computing CSS viewport using %d,%d\n", this, mDisplaySize.width,
642 mDisplaySize.height);
643 if (mDisplaySize.width == 0 || mDisplaySize.height == 0) {
644 // We can't do anything useful here, we should just bail out
645 return;
648 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
649 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
650 nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
651 MVM_LOG("%p: viewport info has zooms min=%f max=%f default=%f,valid=%d\n",
652 this, viewportInfo.GetMinZoom().scale,
653 viewportInfo.GetMaxZoom().scale, viewportInfo.GetDefaultZoom().scale,
654 viewportInfo.IsDefaultZoomValid());
656 CSSSize viewport = viewportInfo.GetSize();
657 MVM_LOG("%p: Computed CSS viewport %s\n", this, ToString(viewport).c_str());
659 if (!mIsFirstPaint && mMobileViewportSize == viewport) {
660 // Nothing changed, so no need to do a reflow
661 return;
664 // If it's the first-paint or the viewport changed, we need to update
665 // various APZ properties (the zoom and some things that might depend on it)
666 MVM_LOG("%p: Updating properties because %d || %d\n", this, mIsFirstPaint,
667 mMobileViewportSize != viewport);
669 if (mManagerType == ManagerType::VisualAndMetaViewport &&
670 (aForceAdjustResolution || mContext->AllowZoomingForDocument())) {
671 MVM_LOG("%p: Updating resolution because %d || %d\n", this,
672 aForceAdjustResolution, mContext->AllowZoomingForDocument());
673 if (mIsFirstPaint) {
674 UpdateResolutionForFirstPaint(viewport);
675 } else {
676 UpdateResolutionForViewportSizeChange(viewport, displayWidthChangeRatio);
678 } else {
679 // Even without zoom, we need to update that the visual viewport size
680 // has changed.
681 MVM_LOG("%p: Updating VV size\n", this);
682 RefreshVisualViewportSize();
684 if (gfxPlatform::AsyncPanZoomEnabled()) {
685 UpdateDisplayPortMargins();
688 // Update internal state.
689 mMobileViewportSize = viewport;
691 if (mManagerType == ManagerType::VisualViewportOnly) {
692 MVM_LOG("%p: Visual-only, so aborting before reflow\n", this);
693 mIsFirstPaint = false;
694 return;
697 RefPtr<MobileViewportManager> strongThis(this);
699 // Kick off a reflow.
700 MVM_LOG("%p: Triggering reflow with viewport %s\n", this,
701 ToString(viewport).c_str());
702 mContext->Reflow(viewport);
704 // We are going to fit the content to the display width if the initial-scale
705 // is not specied and if the content is still wider than the display width.
706 ShrinkToDisplaySizeIfNeeded();
708 mIsFirstPaint = false;
711 void MobileViewportManager::ShrinkToDisplaySizeIfNeeded() {
712 if (!mContext) {
713 return;
716 if (mManagerType == ManagerType::VisualViewportOnly) {
717 MVM_LOG("%p: Visual-only, so aborting ShrinkToDisplaySizeIfNeeded\n", this);
718 return;
721 if (!mContext->AllowZoomingForDocument() || mContext->IsInReaderMode()) {
722 // If zoom is disabled, we don't scale down wider contents to fit them
723 // into device screen because users won't be able to zoom out the tiny
724 // contents.
725 // We special-case reader mode, because it doesn't allow zooming, but
726 // the restriction is often not yet in place at the time this logic
727 // runs.
728 return;
731 if (Maybe<CSSRect> scrollableRect =
732 mContext->CalculateScrollableRectForRSF()) {
733 MVM_LOG("%p: ShrinkToDisplaySize using scrollableRect %s\n", this,
734 ToString(scrollableRect->Size()).c_str());
735 UpdateResolutionForContentSizeChange(scrollableRect->Size());
739 CSSSize MobileViewportManager::GetIntrinsicCompositionSize() const {
740 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
741 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
742 ScreenIntSize compositionSize = GetCompositionSize(displaySize);
743 CSSToScreenScale intrinsicScale =
744 ComputeIntrinsicScale(mContext->GetViewportInfo(displaySize),
745 compositionSize, mMobileViewportSize);
747 return ScreenSize(compositionSize) / intrinsicScale;
750 ParentLayerSize MobileViewportManager::GetCompositionSizeWithoutDynamicToolbar()
751 const {
752 ScreenIntSize displaySize = ViewAs<ScreenPixel>(
753 mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
754 return ViewAs<ParentLayerPixel>(
755 ScreenSize(GetCompositionSize(displaySize)),
756 PixelCastJustification::ScreenIsParentLayerForRoot);