MobileViewportManager.cpp 24.8 KB
Newer Older
1
2
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
4
5
6
7
8
9
/* 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"

#include "LayersLogging.h"
10
#include "mozilla/PresShell.h"
11
#include "mozilla/dom/Document.h"
12
#include "mozilla/dom/Event.h"
13
#include "mozilla/dom/EventTarget.h"
14
15
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
16
17
#include "nsViewManager.h"
#include "nsViewportInfo.h"
18
#include "UnitTransforms.h"
19

20
21
static mozilla::LazyLogModule sApzMvmLog("apz.mobileviewport");
#define MVM_LOG(...) MOZ_LOG(sApzMvmLog, LogLevel::Debug, (__VA_ARGS__))
22
23
24

NS_IMPL_ISUPPORTS(MobileViewportManager, nsIDOMEventListener, nsIObserver)

25
26
27
28
#define DOM_META_ADDED NS_LITERAL_STRING("DOMMetaAdded")
#define DOM_META_CHANGED NS_LITERAL_STRING("DOMMetaChanged")
#define LOAD NS_LITERAL_STRING("load")
#define BEFORE_FIRST_PAINT NS_LITERAL_CSTRING("before-first-paint")
29
30

using namespace mozilla;
31
using namespace mozilla::dom;
32
33
using namespace mozilla::layers;

34
35
36
MobileViewportManager::MobileViewportManager(MVMContext* aContext)
    : mContext(aContext), mIsFirstPaint(false), mPainted(false) {
  MOZ_ASSERT(mContext);
37

38
  MVM_LOG("%p: creating with context %p\n", this, mContext.get());
39
40
41
42
43
44

  mContext->AddEventListener(DOM_META_ADDED, this, false);
  mContext->AddEventListener(DOM_META_CHANGED, this, false);
  mContext->AddEventListener(LOAD, this, true);

  mContext->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false);
45
46
}

47
MobileViewportManager::~MobileViewportManager() = default;
48

49
void MobileViewportManager::Destroy() {
50
51
  MVM_LOG("%p: destroying\n", this);

52
53
54
  mContext->RemoveEventListener(DOM_META_ADDED, this, false);
  mContext->RemoveEventListener(DOM_META_CHANGED, this, false);
  mContext->RemoveEventListener(LOAD, this, true);
55

56
  mContext->RemoveObserver(this, BEFORE_FIRST_PAINT.Data());
57

58
59
  mContext->Destroy();
  mContext = nullptr;
60
61
}

62
63
void MobileViewportManager::SetRestoreResolution(
    float aResolution, LayoutDeviceIntSize aDisplaySize) {
64
  SetRestoreResolution(aResolution);
65
66
  ScreenIntSize restoreDisplaySize = ViewAs<ScreenPixel>(
      aDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
67
  mRestoreDisplaySize = Some(restoreDisplaySize);
68
69
}

70
void MobileViewportManager::SetRestoreResolution(float aResolution) {
71
72
73
  mRestoreResolution = Some(aResolution);
}

74
float MobileViewportManager::ComputeIntrinsicResolution() const {
75
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
76
77
78
    return 1.f;
  }

79
80
  ScreenIntSize displaySize = ViewAs<ScreenPixel>(
      mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
81
82
83
  CSSToScreenScale intrinsicScale = ComputeIntrinsicScale(
      mContext->GetViewportInfo(displaySize), displaySize, mMobileViewportSize);
  CSSToLayoutDeviceScale cssToDev = mContext->CSSToDevPixelScale();
84
85
86
  return (intrinsicScale / cssToDev).scale;
}

87
88
89
mozilla::CSSToScreenScale MobileViewportManager::ComputeIntrinsicScale(
    const nsViewportInfo& aViewportInfo,
    const mozilla::ScreenIntSize& aDisplaySize,
90
    const mozilla::CSSSize& aViewportOrContentSize) const {
91
  CSSToScreenScale intrinsicScale =
92
93
94
      aViewportOrContentSize.IsEmpty()
          ? CSSToScreenScale(1.0)
          : MaxScaleRatio(ScreenSize(aDisplaySize), aViewportOrContentSize);
95
96
97
98
  MVM_LOG("%p: Intrinsic computed zoom is %f\n", this, intrinsicScale.scale);
  return ClampZoom(intrinsicScale, aViewportInfo);
}

99
100
101
102
void MobileViewportManager::RequestReflow(bool aForceAdjustResolution) {
  MVM_LOG("%p: got a reflow request with force resolution: %d\n", this,
          aForceAdjustResolution);
  RefreshViewportSize(aForceAdjustResolution);
103
104
}

105
106
void MobileViewportManager::ResolutionUpdated(
    mozilla::ResolutionChangeOrigin aOrigin) {
107
  MVM_LOG("%p: resolution updated\n", this);
Timothy Nikkel's avatar
Timothy Nikkel committed
108

109
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
110
111
112
    return;
  }

113
114
115
  if ((!mPainted &&
       aOrigin == mozilla::ResolutionChangeOrigin::MainThreadRestore) ||
      aOrigin == mozilla::ResolutionChangeOrigin::Test) {
116
117
    // Save the value, so our default zoom calculation
    // can take it into account later on.
118
    SetRestoreResolution(mContext->GetResolution());
119
  }
120
  RefreshVisualViewportSize();
121
122
}

123
NS_IMETHODIMP
124
MobileViewportManager::HandleEvent(dom::Event* event) {
125
126
127
128
  nsAutoString type;
  event->GetType(type);

  if (type.Equals(DOM_META_ADDED)) {
129
    HandleDOMMetaAdded();
130
131
132
  } else if (type.Equals(DOM_META_CHANGED)) {
    MVM_LOG("%p: got a dom-meta-changed event\n", this);
    RefreshViewportSize(mPainted);
133
134
135
136
137
138
  } 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();
    }
139
140
141
142
  }
  return NS_OK;
}

143
144
void MobileViewportManager::HandleDOMMetaAdded() {
  MVM_LOG("%p: got a dom-meta-added event\n", this);
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  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);
  }
162
163
}

164
NS_IMETHODIMP
165
166
MobileViewportManager::Observe(nsISupports* aSubject, const char* aTopic,
                               const char16_t* aData) {
167
  if (!mContext) {
168
    return NS_OK;
Timothy Nikkel's avatar
Timothy Nikkel committed
169
170
  }

171
  if (mContext->SubjectMatchesDocument(aSubject) &&
172
      BEFORE_FIRST_PAINT.EqualsASCII(aTopic)) {
173
    MVM_LOG("%p: got a before-first-paint event\n", this);
174
175
176
177
    if (!mPainted) {
      // before-first-paint message arrived before load event
      SetInitialViewport();
    }
178
179
180
181
  }
  return NS_OK;
}

182
void MobileViewportManager::SetInitialViewport() {
183
184
185
186
187
188
  MVM_LOG("%p: setting initial viewport\n", this);
  mIsFirstPaint = true;
  mPainted = true;
  RefreshViewportSize(false);
}

189
190
CSSToScreenScale MobileViewportManager::ClampZoom(
    const CSSToScreenScale& aZoom, const nsViewportInfo& aViewportInfo) const {
191
  CSSToScreenScale zoom = aZoom;
192
193
194
195
196
  if (IsNaN(zoom.scale)) {
    NS_ERROR("Don't pass NaN to ClampZoom; check caller for 0/0 division");
    zoom = CSSToScreenScale(1.0);
  }

197
198
199
200
201
202
203
204
  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);
  }
205
206
207
208
209
210
211
212
213

  // 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");
214
215
216
  return zoom;
}

217
218
219
CSSToScreenScale MobileViewportManager::ScaleZoomWithDisplayWidth(
    const CSSToScreenScale& aZoom, const float& aDisplayWidthChangeRatio,
    const CSSSize& aNewViewport, const CSSSize& aOldViewport) {
220
221
222
223
224
225
226
  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);
227
228
229
  return newZoom;
}

230
231
232
233
234
static CSSToScreenScale ResolutionToZoom(LayoutDeviceToLayerScale aResolution,
                                         CSSToLayoutDeviceScale aCssToDev) {
  return ViewTargetAs<ScreenPixel>(
      aCssToDev * aResolution / ParentLayerToLayerScale(1),
      PixelCastJustification::ScreenIsParentLayerForRoot);
235
236
}

237
238
239
240
241
static LayoutDeviceToLayerScale ZoomToResolution(
    CSSToScreenScale aZoom, CSSToLayoutDeviceScale aCssToDev) {
  return ViewTargetAs<ParentLayerPixel>(
             aZoom, PixelCastJustification::ScreenIsParentLayerForRoot) /
         aCssToDev * ParentLayerToLayerScale(1);
242
243
}

244
245
246
247
void MobileViewportManager::UpdateResolution(
    const nsViewportInfo& aViewportInfo, const ScreenIntSize& aDisplaySize,
    const CSSSize& aViewportOrContentSize,
    const Maybe<float>& aDisplayWidthChangeRatio, UpdateType aType) {
248
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
249
250
251
    return;
  }

252
253
  CSSToLayoutDeviceScale cssToDev = mContext->CSSToDevPixelScale();
  LayoutDeviceToLayerScale res(mContext->GetResolution());
254
  CSSToScreenScale zoom = ResolutionToZoom(res, cssToDev);
255
256
257
258
  // 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");

259
  Maybe<CSSToScreenScale> newZoom;
260

261
  ScreenIntSize compositionSize = GetCompositionSize(aDisplaySize);
262
263
  CSSToScreenScale intrinsicScale = ComputeIntrinsicScale(
      aViewportInfo, compositionSize, aViewportOrContentSize);
264
265
266
267
268
269
270

  if (aType == UpdateType::ViewportSize) {
    const CSSSize& viewportSize = aViewportOrContentSize;
    if (mIsFirstPaint) {
      CSSToScreenScale defaultZoom;
      if (mRestoreResolution) {
        LayoutDeviceToLayerScale restoreResolution(mRestoreResolution.value());
271
272
        CSSToScreenScale restoreZoom =
            ResolutionToZoom(restoreResolution, cssToDev);
273
        if (mRestoreDisplaySize) {
274
          CSSSize prevViewport =
275
              mContext->GetViewportInfo(mRestoreDisplaySize.value()).GetSize();
276
277
278
279
280
281
282
283
284
          float restoreDisplayWidthChangeRatio =
              (mRestoreDisplaySize.value().width > 0)
                  ? (float)compositionSize.width /
                        (float)mRestoreDisplaySize.value().width
                  : 1.0f;

          restoreZoom = ScaleZoomWithDisplayWidth(
              restoreZoom, restoreDisplayWidthChangeRatio, viewportSize,
              prevViewport);
285
286
287
288
289
290
        }
        defaultZoom = restoreZoom;
        MVM_LOG("%p: restored zoom is %f\n", this, defaultZoom.scale);
        defaultZoom = ClampZoom(defaultZoom, aViewportInfo);
      } else {
        defaultZoom = aViewportInfo.GetDefaultZoom();
291
292
        MVM_LOG("%p: default zoom from viewport is %f\n", this,
                defaultZoom.scale);
293
294
295
        if (!aViewportInfo.IsDefaultZoomValid()) {
          defaultZoom = intrinsicScale;
        }
296
      }
297
      MOZ_ASSERT(aViewportInfo.GetMinZoom() <= defaultZoom &&
298
                 defaultZoom <= aViewportInfo.GetMaxZoom());
299
300
301

      // On first paint, we always consider the zoom to have changed.
      newZoom = Some(defaultZoom);
302
    } else {
303
304
305
306
307
      // 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.
308
309
310
      //
      // aDisplayWidthChangeRatio is non-empty if:
      // (a) The meta-viewport tag information changes, and so the CSS viewport
311
312
      //     might change as a result. If this happens after the content has
      //     been painted, we want to adjust the zoom to compensate. OR
313
314
315
      // (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.
316
317
318
319
      // 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.
320
      //
321
322
323
      // 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
324
      //    viewport or a fixed size viewport)
325
      // 2. screen size changes, CSS viewport also does (pages with a
326
      //    device-width viewport)
327
      // 3. screen size remains constant, but CSS viewport changes (meta
328
      //    viewport tag is added or removed)
329
330
      // 4. neither screen size nor CSS viewport changes
      if (aDisplayWidthChangeRatio) {
331
332
333
334
        // 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
335
336
337
338
339
340
341
342
        // 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.

        // 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.
343
344
345
346
347

        // 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 aViewportOrContentSize.
        CSSSize contentSize = aViewportOrContentSize;
348
349
350
        if (Maybe<CSSRect> scrollableRect =
                mContext->CalculateScrollableRectForRSF()) {
          contentSize = scrollableRect->Size();
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
        }

        // We scale the sizes, though we only care about the scaled widths.
        ScreenSize minZoomDisplaySize =
            contentSize * aViewportInfo.GetMinZoom();
        ScreenSize maxZoomDisplaySize =
            contentSize * aViewportInfo.GetMaxZoom();

        float ratio = aDisplayWidthChangeRatio.value();
        ScreenSize newDisplaySize(aDisplaySize);
        ScreenSize oldDisplaySize = newDisplaySize / ratio;

        // 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);

370
        // The oldDisplaySize value is in one of three "zones":
371
372
373
374
375
376
377
        // 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):

378
379
380
381
382
383
384
385
386
387
388
389
        // c   +---+
        //     | d |
        // 1   | a |
        //     +---+
        //     | d |
        // 2   | c |
        //     +---+
        //     | d |
        // 3   | b |
        //     +---+

        // Conveniently, the denominator is c clamped to a..b.
390
391
        float denominator = clamped(c, a, b);

392
393
394
395
        float adjustedRatio = d / denominator;
        CSSToScreenScale adjustedZoom = ScaleZoomWithDisplayWidth(
            zoom, adjustedRatio, viewportSize, mMobileViewportSize);
        newZoom = Some(ClampZoom(adjustedZoom, aViewportInfo));
396
      }
397
    }
398
399
400
  } else {  // aType == UpdateType::ContentSize
    MOZ_ASSERT(aType == UpdateType::ContentSize);
    MOZ_ASSERT(aDisplayWidthChangeRatio.isNothing());
401

402
    // We try to scale down the contents only IF the document has no
403
404
    // initial-scale AND IF it's not restored documents AND IF the resolution
    // has never been changed by APZ.
405
    if (!mRestoreResolution && !mContext->IsResolutionUpdatedByApz() &&
406
407
408
409
410
411
412
413
        !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.
414
415
416
417
418
419
420
421
422
423
424
      CSSToScreenScale clampedZoom = zoom;

      if (clampedZoom < intrinsicScale) {
        clampedZoom = intrinsicScale;
      }

      // Also clamp to the restrictions imposed by aViewportInfo.
      clampedZoom = ClampZoom(clampedZoom, aViewportInfo);

      if (clampedZoom != zoom) {
        newZoom = Some(clampedZoom);
425
      }
426
427
    }
  }
428

429
  // If the zoom has changed, update the pres shell resolution accordingly.
430
431
  // We characterize this as MainThreadAdjustment, because we don't want our
  // change here to be remembered as a restore resolution.
432
  if (newZoom) {
433
434
435
436
    // 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(*newZoom > CSSToScreenScale(0.0f),
               "zoom factor must be positive");
437
    LayoutDeviceToLayerScale resolution = ZoomToResolution(*newZoom, cssToDev);
438
    MVM_LOG("%p: setting resolution %f\n", this, resolution.scale);
439
    mContext->SetResolutionAndScaleTo(
440
        resolution.scale, ResolutionChangeOrigin::MainThreadAdjustment);
441

442
    MVM_LOG("%p: New zoom is %f\n", this, newZoom->scale);
443
444
  }

445
446
447
  // 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) {
448
    UpdateVisualViewportSize(aDisplaySize, newZoom ? *newZoom : zoom);
449
450
451
  }
}

452
453
ScreenIntSize MobileViewportManager::GetCompositionSize(
    const ScreenIntSize& aDisplaySize) const {
454
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
455
456
457
    return ScreenIntSize();
  }

458
459
460
  // FIXME: Bug 1586986 - To update VisualViewport in response to the dynamic
  // toolbar transition we probably need to include the dynamic toolbar
  // _current_ height.
461
  ScreenIntSize compositionSize(aDisplaySize);
462
  ScreenMargin scrollbars =
463
      mContext->ScrollbarAreaToExcludeFromCompositionBounds()
464
465
466
      // Scrollbars are not subject to resolution scaling, so LD pixels =
      // Screen pixels for them.
      * LayoutDeviceToScreenScale(1.0f);
467

468
469
470
471
  compositionSize.width =
      std::max(0.0f, compositionSize.width - scrollbars.LeftRight());
  compositionSize.height =
      std::max(0.0f, compositionSize.height - scrollbars.TopBottom());
472
473
474
475

  return compositionSize;
}

476
477
void MobileViewportManager::UpdateVisualViewportSize(
    const ScreenIntSize& aDisplaySize, const CSSToScreenScale& aZoom) {
478
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
479
480
481
    return;
  }

482
483
  ScreenSize compositionSize = ScreenSize(GetCompositionSize(aDisplaySize));

484
  CSSSize compSize = compositionSize / aZoom;
485
  MVM_LOG("%p: Setting VVPS %s\n", this, Stringify(compSize).c_str());
486
  mContext->SetVisualViewportSize(compSize);
487
488
}

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
CSSToScreenScale MobileViewportManager::GetZoom() const {
  CSSToLayoutDeviceScale cssToDev = mContext->CSSToDevPixelScale();
  LayoutDeviceToLayerScale res(mContext->GetResolution());
  return ResolutionToZoom(res, cssToDev);
}

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();
}

513
void MobileViewportManager::UpdateDisplayPortMargins() {
514
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
515
516
    return;
  }
517
  mContext->UpdateDisplayPortMargins();
518
519
}

520
void MobileViewportManager::RefreshVisualViewportSize() {
521
  // This function is a subset of RefreshViewportSize, and only updates the
522
  // visual viewport size.
523

524
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
525
526
527
    return;
  }

528
  ScreenIntSize displaySize = ViewAs<ScreenPixel>(
529
      mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
530

531
  UpdateVisualViewportSize(displaySize, GetZoom());
532
533
}

534
void MobileViewportManager::RefreshViewportSize(bool aForceAdjustResolution) {
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  // 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.

549
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
550
551
552
    return;
  }

553
  Maybe<float> displayWidthChangeRatio;
554
555
  if (Maybe<LayoutDeviceIntSize> newDisplaySize =
          mContext->GetContentViewerSize()) {
556
557
    // See the comment in UpdateResolution for why we're doing this.
    if (mDisplaySize.width > 0) {
558
      if (aForceAdjustResolution ||
559
          mDisplaySize.width != newDisplaySize->width) {
560
        displayWidthChangeRatio =
561
            Some((float)newDisplaySize->width / (float)mDisplaySize.width);
562
563
564
565
566
      }
    } else if (aForceAdjustResolution) {
      displayWidthChangeRatio = Some(1.0f);
    }

567
568
    MVM_LOG("%p: Display width change ratio is %f\n", this,
            displayWidthChangeRatio.valueOr(0.0f));
569
    mDisplaySize = *newDisplaySize;
570
571
  }

572
573
  MVM_LOG("%p: Computing CSS viewport using %d,%d\n", this, mDisplaySize.width,
          mDisplaySize.height);
574
575
576
577
578
579
  if (mDisplaySize.width == 0 || mDisplaySize.height == 0) {
    // We can't do anything useful here, we should just bail out
    return;
  }

  ScreenIntSize displaySize = ViewAs<ScreenPixel>(
580
      mDisplaySize, PixelCastJustification::LayoutDeviceIsScreenForBounds);
581
  nsViewportInfo viewportInfo = mContext->GetViewportInfo(displaySize);
582
583
584
585
  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());
586
587
588
589
590
591
592
593
594
595
596

  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)
597
598
  MVM_LOG("%p: Updating properties because %d || %d\n", this, mIsFirstPaint,
          mMobileViewportSize != viewport);
599

600
  if (aForceAdjustResolution || mContext->AllowZoomingForDocument()) {
601
    UpdateResolution(viewportInfo, displaySize, viewport,
602
                     displayWidthChangeRatio, UpdateType::ViewportSize);
603
604
605
606
  } else {
    // Even without zoom, we need to update that the visual viewport size
    // has changed.
    RefreshVisualViewportSize();
607
  }
608
609
610
  if (gfxPlatform::AsyncPanZoomEnabled()) {
    UpdateDisplayPortMargins();
  }
611
612
613
614

  // Update internal state.
  mMobileViewportSize = viewport;

Timothy Nikkel's avatar
Timothy Nikkel committed
615
616
  RefPtr<MobileViewportManager> strongThis(this);

617
  // Kick off a reflow.
618
  mContext->Reflow(viewport);
619
620
621
622
623
624
625
626

  // 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;
}

627
628
void MobileViewportManager::ShrinkToDisplaySizeIfNeeded(
    nsViewportInfo& aViewportInfo, const ScreenIntSize& aDisplaySize) {
629
  if (!mContext) {
Timothy Nikkel's avatar
Timothy Nikkel committed
630
631
632
    return;
  }

633
  if (!mContext->AllowZoomingForDocument() || mContext->IsInReaderMode()) {
634
    // If zoom is disabled, we don't scale down wider contents to fit them
635
636
    // into device screen because users won't be able to zoom out the tiny
    // contents.
637
638
639
    // 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.
640
641
642
    return;
  }

643
644
645
646
  if (Maybe<CSSRect> scrollableRect =
          mContext->CalculateScrollableRectForRSF()) {
    UpdateResolution(aViewportInfo, aDisplaySize, scrollableRect->Size(),
                     Nothing(), UpdateType::ContentSize);
647
  }
648
}