Bug 1630781 - Encapsulate the mComposited* variables into a class. r=botond

No functional changes intended here, just code getting moved into a helper
class. Note that this patch folds RecalculateCompositedLayoutViewport into
ClampCompositedScrollOffset since there are no longer any independent callers
of the former function (as of bug 1627716).

Depends on D72041

Differential Revision: https://phabricator.services.mozilla.com/D72042
This commit is contained in:
Kartikaya Gupta 2020-04-25 01:00:07 +00:00
parent 5475382650
commit dba00154a9
5 changed files with 142 additions and 60 deletions

View File

@ -3149,7 +3149,8 @@ void AsyncPanZoomController::UpdateWithTouchAtDevicePoint(
}
Maybe<CompositionPayload> AsyncPanZoomController::NotifyScrollSampling() {
return std::move(mCompositedScrollPayload);
RecursiveMutexAutoLock lock(mRecursiveMutex);
return mSampledState.TakeScrollPayload();
}
bool AsyncPanZoomController::AttemptScroll(
@ -3641,19 +3642,6 @@ void AsyncPanZoomController::ClampAndSetScrollOffset(const CSSPoint& aOffset) {
Metrics().RecalculateLayoutViewportOffset();
}
void AsyncPanZoomController::ClampCompositedScrollOffset() {
mCompositedScrollOffset =
Metrics().CalculateScrollRange().ClampPoint(mCompositedScrollOffset);
RecalculateCompositedLayoutViewport();
}
void AsyncPanZoomController::RecalculateCompositedLayoutViewport() {
FrameMetrics::KeepLayoutViewportEnclosingVisualViewport(
CSSRect(mCompositedScrollOffset,
Metrics().CalculateCompositedSizeInCssPixels()),
Metrics().GetScrollableRect(), mCompositedLayoutViewport);
}
void AsyncPanZoomController::ScrollBy(const CSSPoint& aOffset) {
SetScrollOffset(Metrics().GetScrollOffset() + aOffset);
}
@ -4179,7 +4167,7 @@ CSSRect AsyncPanZoomController::GetEffectiveLayoutViewport(
return mLastContentPaintMetrics.GetLayoutViewport();
}
if (aMode == eForCompositing) {
return mCompositedLayoutViewport;
return mSampledState.GetLayoutViewport();
}
return Metrics().GetLayoutViewport();
}
@ -4191,7 +4179,7 @@ CSSPoint AsyncPanZoomController::GetEffectiveScrollOffset(
return mLastContentPaintMetrics.GetVisualViewportOffset();
}
if (aMode == eForCompositing) {
return mCompositedScrollOffset;
return mSampledState.GetScrollOffset();
}
return Metrics().GetScrollOffset();
}
@ -4203,20 +4191,15 @@ CSSToParentLayerScale2D AsyncPanZoomController::GetEffectiveZoom(
return mLastContentPaintMetrics.GetZoom();
}
if (aMode == eForCompositing) {
return mCompositedZoom;
return mSampledState.GetZoom();
}
return Metrics().GetZoom();
}
bool AsyncPanZoomController::SampleCompositedAsyncTransform(
const RecursiveMutexAutoLock& aProofOfLock) {
if (!mCompositedLayoutViewport.IsEqualEdges(Metrics().GetLayoutViewport()) ||
mCompositedScrollOffset != Metrics().GetScrollOffset() ||
mCompositedZoom != Metrics().GetZoom()) {
mCompositedLayoutViewport = Metrics().GetLayoutViewport();
mCompositedScrollOffset = Metrics().GetScrollOffset();
mCompositedZoom = Metrics().GetZoom();
mCompositedScrollPayload = std::move(mScrollPayload);
if (mSampledState != SampledAPZCState(Metrics())) {
mSampledState = SampledAPZCState(Metrics(), std::move(mScrollPayload));
return true;
}
return false;
@ -4546,9 +4529,8 @@ void AsyncPanZoomController::NotifyLayersUpdated(
mExpectedGeckoMetrics = aLayerMetrics;
ShareCompositorFrameMetrics();
mCompositedLayoutViewport = Metrics().GetLayoutViewport();
mCompositedScrollOffset = Metrics().GetScrollOffset();
mCompositedZoom = Metrics().GetZoom();
mSampledState.UpdateScrollProperties(Metrics());
mSampledState.UpdateZoomProperties(Metrics());
if (Metrics().GetDisplayPortMargins() != ScreenMargin()) {
// A non-zero display port margin here indicates a displayport has
@ -4586,16 +4568,13 @@ void AsyncPanZoomController::NotifyLayersUpdated(
needContentRepaint = true;
}
Metrics().ZoomBy(totalResolutionChange / presShellResolutionChange);
mCompositedZoom.xScale *=
(totalResolutionChange / presShellResolutionChange).width;
mCompositedZoom.yScale *=
(totalResolutionChange / presShellResolutionChange).height;
mSampledState.ZoomBy(totalResolutionChange / presShellResolutionChange);
} else {
// Take the new zoom as either device scale or composition width or
// viewport size got changed (e.g. due to orientation change, or content
// changing the meta-viewport tag).
Metrics().SetZoom(aLayerMetrics.GetZoom());
mCompositedZoom = aLayerMetrics.GetZoom();
mSampledState.UpdateZoomProperties(aLayerMetrics);
Metrics().SetDevPixelsPerCSSPixel(
aLayerMetrics.GetDevPixelsPerCSSPixel());
}
@ -4670,8 +4649,7 @@ void AsyncPanZoomController::NotifyLayersUpdated(
}
Metrics().RecalculateLayoutViewportOffset();
mCompositedLayoutViewport = Metrics().GetLayoutViewport();
mCompositedScrollOffset = Metrics().GetScrollOffset();
mSampledState.UpdateScrollProperties(Metrics());
mExpectedGeckoMetrics = aLayerMetrics;
// If an animation is underway, tell it about the scroll offset update.
@ -4700,7 +4678,7 @@ void AsyncPanZoomController::NotifyLayersUpdated(
// scrollable rect or composition bounds may have changed in a way that
// makes our local scroll offset out of bounds, so re-clamp it.
ClampAndSetScrollOffset(Metrics().GetScrollOffset());
ClampCompositedScrollOffset();
mSampledState.ClampScrollOffset(Metrics());
}
}
@ -4736,8 +4714,7 @@ void AsyncPanZoomController::NotifyLayersUpdated(
// The rest of this branch largely follows the code in the
// |if (scrollOffsetUpdated)| branch above.
Metrics().RecalculateLayoutViewportOffset();
mCompositedLayoutViewport = Metrics().GetLayoutViewport();
mCompositedScrollOffset = Metrics().GetScrollOffset();
mSampledState.UpdateScrollProperties(Metrics());
mExpectedGeckoMetrics = aLayerMetrics;
if (ShouldCancelAnimationForScrollUpdate(Nothing())) {
CancelAnimation();

View File

@ -31,6 +31,7 @@
#include "nsTArray.h"
#include "PotentialCheckerboardDurationTracker.h"
#include "RecentEventsBuffer.h" // for RecentEventsBuffer
#include "SampledAPZCState.h"
#include "base/message_loop.h"
@ -697,21 +698,6 @@ class AsyncPanZoomController {
*/
void ClampAndSetScrollOffset(const CSSPoint& aOffset);
/**
* Re-clamp mCompositedScrollOffset to the scroll range. This only needs to
* be called if the composited scroll offset changes outside of
* SampleCompositedAsyncTransform().
*/
void ClampCompositedScrollOffset();
/**
* Recalculate mCompositedLayoutViewport so that it continues to enclose
* the composited visual viewport. This only needs to be called if the
* composited layout viewport changes outside of
* SampleCompositedAsyncTransform().
*/
void RecalculateCompositedLayoutViewport();
/**
* Scroll the scroll frame by an X,Y offset.
* The resulting scroll offset is not clamped to the scrollable rect;
@ -974,12 +960,10 @@ class AsyncPanZoomController {
// This allows us to transform events into Gecko's coordinate space.
FrameMetrics mExpectedGeckoMetrics;
// These variables cache the layout viewport, scroll offset, and zoom stored
// in |Metrics()| the last time SampleCompositedAsyncTransform() was
// called. mRecursiveMutex must be held with using or modifying these fields.
CSSRect mCompositedLayoutViewport;
CSSPoint mCompositedScrollOffset;
CSSToParentLayerScale2D mCompositedZoom;
// This holds important state from the Metrics() at the last time
// SampleCompositedAsyncTransform() was called. mRecursiveMutex must be held
// when using or modifying this member.
SampledAPZCState mSampledState;
// Groups state variables that are specific to a platform.
// Initialized on first use.
@ -1034,8 +1018,6 @@ class AsyncPanZoomController {
// Position on screen where user first put their finger down.
ExternalPoint mStartTouch;
Maybe<CompositionPayload> mCompositedScrollPayload;
// Accessing mScrollPayload needs to be protected by mRecursiveMutex
Maybe<CompositionPayload> mScrollPayload;

View File

@ -0,0 +1,63 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "SampledAPZCState.h"
namespace mozilla {
namespace layers {
SampledAPZCState::SampledAPZCState() {}
SampledAPZCState::SampledAPZCState(const FrameMetrics& aMetrics)
: mLayoutViewport(aMetrics.GetLayoutViewport()),
mScrollOffset(aMetrics.GetScrollOffset()),
mZoom(aMetrics.GetZoom()) {}
SampledAPZCState::SampledAPZCState(const FrameMetrics& aMetrics,
Maybe<CompositionPayload>&& aPayload)
: mLayoutViewport(aMetrics.GetLayoutViewport()),
mScrollOffset(aMetrics.GetScrollOffset()),
mZoom(aMetrics.GetZoom()),
mScrollPayload(std::move(aPayload)) {}
bool SampledAPZCState::operator==(const SampledAPZCState& aOther) const {
// The payload doesn't factor into equality, that just comes along for
// the ride.
return mLayoutViewport.IsEqualEdges(aOther.mLayoutViewport) &&
mScrollOffset == aOther.mScrollOffset && mZoom == aOther.mZoom;
}
bool SampledAPZCState::operator!=(const SampledAPZCState& aOther) const {
return !(*this == aOther);
}
Maybe<CompositionPayload> SampledAPZCState::TakeScrollPayload() {
return std::move(mScrollPayload);
}
void SampledAPZCState::UpdateScrollProperties(const FrameMetrics& aMetrics) {
mLayoutViewport = aMetrics.GetLayoutViewport();
mScrollOffset = aMetrics.GetScrollOffset();
}
void SampledAPZCState::UpdateZoomProperties(const FrameMetrics& aMetrics) {
mZoom = aMetrics.GetZoom();
}
void SampledAPZCState::ClampScrollOffset(const FrameMetrics& aMetrics) {
mScrollOffset = aMetrics.CalculateScrollRange().ClampPoint(mScrollOffset);
FrameMetrics::KeepLayoutViewportEnclosingVisualViewport(
CSSRect(mScrollOffset, aMetrics.CalculateCompositedSizeInCssPixels()),
aMetrics.GetScrollableRect(), mLayoutViewport);
}
void SampledAPZCState::ZoomBy(const gfxSize& aScale) {
mZoom.xScale *= aScale.width;
mZoom.yScale *= aScale.height;
}
} // namespace layers
} // namespace mozilla

View File

@ -0,0 +1,59 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_layers_SampledAPZCState_h
#define mozilla_layers_SampledAPZCState_h
#include "FrameMetrics.h"
#include "mozilla/Maybe.h"
namespace mozilla {
namespace layers {
class SampledAPZCState {
public:
SampledAPZCState();
explicit SampledAPZCState(const FrameMetrics& aMetrics);
explicit SampledAPZCState(const FrameMetrics& aMetrics,
Maybe<CompositionPayload>&& aPayload);
bool operator==(const SampledAPZCState& aOther) const;
bool operator!=(const SampledAPZCState& aOther) const;
CSSRect GetLayoutViewport() const { return mLayoutViewport; }
CSSPoint GetScrollOffset() const { return mScrollOffset; }
CSSToParentLayerScale2D GetZoom() const { return mZoom; }
Maybe<CompositionPayload> TakeScrollPayload();
void UpdateScrollProperties(const FrameMetrics& aMetrics);
void UpdateZoomProperties(const FrameMetrics& aMetrics);
/**
* Re-clamp mScrollOffset to the scroll range specified by the provided
* metrics. This only needs to be called if the scroll offset changes
* outside of AsyncPanZoomController::SampleCompositedAsyncTransform().
* It also recalculates mLayoutViewport so that it continues to enclose
* the visual viewport. This only needs to be called if the
* layout viewport changes outside of SampleCompositedAsyncTransform().
*/
void ClampScrollOffset(const FrameMetrics& aMetrics);
void ZoomBy(const gfxSize& aScale);
private:
// These variables cache the layout viewport, scroll offset, and zoom stored
// in |Metrics()| at the time this class was constructed.
CSSRect mLayoutViewport;
CSSPoint mScrollOffset;
CSSToParentLayerScale2D mZoom;
// An optional payload that rides along with the sampled state.
Maybe<CompositionPayload> mScrollPayload;
};
} // namespace layers
} // namespace mozilla
#endif // mozilla_layers_SampledAPZCState_h

View File

@ -363,6 +363,7 @@ UNIFIED_SOURCES += [
'apz/src/OverscrollHandoffState.cpp',
'apz/src/PotentialCheckerboardDurationTracker.cpp',
'apz/src/QueuedInput.cpp',
'apz/src/SampledAPZCState.cpp',
'apz/src/SimpleVelocityTracker.cpp',
'apz/src/WheelScrollAnimation.cpp',
'apz/testutil/APZTestData.cpp',