From f0469153525403c0a346102102efdb24f2dd9522 Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Tue, 1 Mar 2022 22:48:16 +0000 Subject: [PATCH] Bug 1676760 - Replace VsyncRefreshDriverTimer constructors with named creation methods. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D139762 --- layout/base/nsRefreshDriver.cpp | 103 +++++++++++++++++++------------- 1 file changed, 61 insertions(+), 42 deletions(-) diff --git a/layout/base/nsRefreshDriver.cpp b/layout/base/nsRefreshDriver.cpp index 1d0ab9808a142..b145016b4e737 100644 --- a/layout/base/nsRefreshDriver.cpp +++ b/layout/base/nsRefreshDriver.cpp @@ -421,41 +421,43 @@ class SimpleTimerBasedRefreshDriverTimer : public RefreshDriverTimer { */ class VsyncRefreshDriverTimer : public RefreshDriverTimer { public: - VsyncRefreshDriverTimer() - : mVsyncDispatcher(nullptr), - mVsyncChild(nullptr), - mVsyncRate(TimeDuration::Forever()) { - MOZ_ASSERT(XRE_IsParentProcess()); - MOZ_ASSERT(NS_IsMainThread()); - mVsyncSource = gfxPlatform::GetPlatform()->GetHardwareVsync(); - mVsyncObserver = new RefreshDriverVsyncObserver(this); - MOZ_ALWAYS_TRUE(mVsyncDispatcher = - mVsyncSource->GetRefreshTimerVsyncDispatcher()); - } - - // Constructor for when we have a local vsync source. As it is local, we do - // not have to worry about it being re-inited by gfxPlatform on frame rate - // change on the global source. - explicit VsyncRefreshDriverTimer(const RefPtr& aVsyncSource) - : mVsyncSource(aVsyncSource), - mVsyncDispatcher(nullptr), - mVsyncChild(nullptr), - mVsyncRate(TimeDuration::Forever()) { - MOZ_ASSERT(XRE_IsParentProcess()); - MOZ_ASSERT(NS_IsMainThread()); - mVsyncObserver = new RefreshDriverVsyncObserver(this); - MOZ_ALWAYS_TRUE(mVsyncDispatcher = - aVsyncSource->GetRefreshTimerVsyncDispatcher()); - } - - explicit VsyncRefreshDriverTimer(RefPtr&& aVsyncChild) - : mVsyncSource(nullptr), - mVsyncDispatcher(nullptr), - mVsyncChild(std::move(aVsyncChild)), - mVsyncRate(TimeDuration::Forever()) { - MOZ_ASSERT(XRE_IsContentProcess()); - MOZ_ASSERT(NS_IsMainThread()); - mVsyncObserver = new RefreshDriverVsyncObserver(this); + // This is used in the parent process for all platforms except Linux Wayland. + static RefPtr + CreateForParentProcessWithGlobalHardwareVsync() { + MOZ_RELEASE_ASSERT(XRE_IsParentProcess()); + MOZ_RELEASE_ASSERT(NS_IsMainThread()); + RefPtr vsyncSource = + gfxPlatform::GetPlatform()->GetHardwareVsync(); + RefPtr vsyncDispatcher = + vsyncSource->GetRefreshTimerVsyncDispatcher(); + RefPtr timer = new VsyncRefreshDriverTimer( + std::move(vsyncSource), std::move(vsyncDispatcher), nullptr); + return timer.forget(); + } + + // This is used in the parent process for Linux Wayland only, where we have a + // per-widget VsyncSource which is independent from the gfxPlatform's global + // VsyncSource. + static RefPtr + CreateForParentProcessWithLocalVsyncSource( + RefPtr&& aVsyncSource) { + MOZ_RELEASE_ASSERT(XRE_IsParentProcess()); + MOZ_RELEASE_ASSERT(NS_IsMainThread()); + RefPtr vsyncDispatcher = + aVsyncSource->GetRefreshTimerVsyncDispatcher(); + RefPtr timer = new VsyncRefreshDriverTimer( + std::move(aVsyncSource), std::move(vsyncDispatcher), nullptr); + return timer.forget(); + } + + // This is used in the content process. + static RefPtr CreateForContentProcess( + RefPtr&& aVsyncChild) { + MOZ_RELEASE_ASSERT(XRE_IsContentProcess()); + MOZ_RELEASE_ASSERT(NS_IsMainThread()); + RefPtr timer = + new VsyncRefreshDriverTimer(nullptr, nullptr, std::move(aVsyncChild)); + return timer.forget(); } TimeDuration GetTimerRate() override { @@ -726,6 +728,17 @@ class VsyncRefreshDriverTimer : public RefreshDriverTimer { bool mProcessedVsync; }; // RefreshDriverVsyncObserver + VsyncRefreshDriverTimer( + RefPtr&& aVsyncSource, + RefPtr&& aVsyncDispatcher, + RefPtr&& aVsyncChild) + : mVsyncSource(aVsyncSource), + mVsyncDispatcher(aVsyncDispatcher), + mVsyncChild(aVsyncChild), + mVsyncRate(TimeDuration::Forever()) { + mVsyncObserver = new RefreshDriverVsyncObserver(this); + } + ~VsyncRefreshDriverTimer() override { if (mVsyncDispatcher) { mVsyncDispatcher->RemoveChildRefreshTimer(mVsyncObserver); @@ -995,14 +1008,17 @@ void nsRefreshDriver::CreateVsyncRefreshTimer() { if (widget) { if (RefPtr localVsyncSource = widget->GetVsyncSource()) { - mOwnTimer = new VsyncRefreshDriverTimer(localVsyncSource); + mOwnTimer = + VsyncRefreshDriverTimer::CreateForParentProcessWithLocalVsyncSource( + std::move(localVsyncSource)); sRegularRateTimerList->AppendElement(mOwnTimer.get()); return; } if (BrowserChild* browserChild = widget->GetOwningBrowserChild()) { - if (RefPtr localVsyncSource = + if (RefPtr vsyncChildViaPBrowser = browserChild->GetVsyncChild()) { - mOwnTimer = new VsyncRefreshDriverTimer(std::move(localVsyncSource)); + mOwnTimer = VsyncRefreshDriverTimer::CreateForContentProcess( + std::move(vsyncChildViaPBrowser)); sRegularRateTimerList->AppendElement(mOwnTimer.get()); return; } @@ -1014,7 +1030,8 @@ void nsRefreshDriver::CreateVsyncRefreshTimer() { // Make sure all vsync systems are ready. gfxPlatform::GetPlatform(); // In parent process, we can create the VsyncRefreshDriverTimer directly. - sRegularRateTimer = new VsyncRefreshDriverTimer(); + sRegularRateTimer = VsyncRefreshDriverTimer:: + CreateForParentProcessWithGlobalHardwareVsync(); } else { PBackgroundChild* actorChild = BackgroundChild::GetOrCreateForCurrentThread(); @@ -1022,14 +1039,16 @@ void nsRefreshDriver::CreateVsyncRefreshTimer() { return; } - auto child = MakeRefPtr(); - dom::PVsyncChild* actor = actorChild->SendPVsyncConstructor(child); + auto vsyncChildViaPBackground = MakeRefPtr(); + dom::PVsyncChild* actor = + actorChild->SendPVsyncConstructor(vsyncChildViaPBackground); if (NS_WARN_IF(!actor)) { return; } RefPtr vsyncRefreshDriverTimer = - new VsyncRefreshDriverTimer(std::move(child)); + VsyncRefreshDriverTimer::CreateForContentProcess( + std::move(vsyncChildViaPBackground)); sRegularRateTimer = std::move(vsyncRefreshDriverTimer); }