Skip to content

Commit

Permalink
Bug 1676760 - Replace VsyncRefreshDriverTimer constructors with named…
Browse files Browse the repository at this point in the history
… creation methods. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D139762
  • Loading branch information
mstange committed Mar 1, 2022
1 parent 956c714 commit f046915
Showing 1 changed file with 61 additions and 42 deletions.
103 changes: 61 additions & 42 deletions layout/base/nsRefreshDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gfx::VsyncSource>& 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<VsyncMainChild>&& 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<VsyncRefreshDriverTimer>
CreateForParentProcessWithGlobalHardwareVsync() {
MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
MOZ_RELEASE_ASSERT(NS_IsMainThread());
RefPtr<gfx::VsyncSource> vsyncSource =
gfxPlatform::GetPlatform()->GetHardwareVsync();
RefPtr<RefreshTimerVsyncDispatcher> vsyncDispatcher =
vsyncSource->GetRefreshTimerVsyncDispatcher();
RefPtr<VsyncRefreshDriverTimer> 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<VsyncRefreshDriverTimer>
CreateForParentProcessWithLocalVsyncSource(
RefPtr<gfx::VsyncSource>&& aVsyncSource) {
MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
MOZ_RELEASE_ASSERT(NS_IsMainThread());
RefPtr<RefreshTimerVsyncDispatcher> vsyncDispatcher =
aVsyncSource->GetRefreshTimerVsyncDispatcher();
RefPtr<VsyncRefreshDriverTimer> timer = new VsyncRefreshDriverTimer(
std::move(aVsyncSource), std::move(vsyncDispatcher), nullptr);
return timer.forget();
}

// This is used in the content process.
static RefPtr<VsyncRefreshDriverTimer> CreateForContentProcess(
RefPtr<VsyncMainChild>&& aVsyncChild) {
MOZ_RELEASE_ASSERT(XRE_IsContentProcess());
MOZ_RELEASE_ASSERT(NS_IsMainThread());
RefPtr<VsyncRefreshDriverTimer> timer =
new VsyncRefreshDriverTimer(nullptr, nullptr, std::move(aVsyncChild));
return timer.forget();
}

TimeDuration GetTimerRate() override {
Expand Down Expand Up @@ -726,6 +728,17 @@ class VsyncRefreshDriverTimer : public RefreshDriverTimer {
bool mProcessedVsync;
}; // RefreshDriverVsyncObserver

VsyncRefreshDriverTimer(
RefPtr<gfx::VsyncSource>&& aVsyncSource,
RefPtr<RefreshTimerVsyncDispatcher>&& aVsyncDispatcher,
RefPtr<VsyncMainChild>&& aVsyncChild)
: mVsyncSource(aVsyncSource),
mVsyncDispatcher(aVsyncDispatcher),
mVsyncChild(aVsyncChild),
mVsyncRate(TimeDuration::Forever()) {
mVsyncObserver = new RefreshDriverVsyncObserver(this);
}

~VsyncRefreshDriverTimer() override {
if (mVsyncDispatcher) {
mVsyncDispatcher->RemoveChildRefreshTimer(mVsyncObserver);
Expand Down Expand Up @@ -995,14 +1008,17 @@ void nsRefreshDriver::CreateVsyncRefreshTimer() {
if (widget) {
if (RefPtr<gfx::VsyncSource> 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<VsyncMainChild> localVsyncSource =
if (RefPtr<VsyncMainChild> vsyncChildViaPBrowser =
browserChild->GetVsyncChild()) {
mOwnTimer = new VsyncRefreshDriverTimer(std::move(localVsyncSource));
mOwnTimer = VsyncRefreshDriverTimer::CreateForContentProcess(
std::move(vsyncChildViaPBrowser));
sRegularRateTimerList->AppendElement(mOwnTimer.get());
return;
}
Expand All @@ -1014,22 +1030,25 @@ 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();
if (NS_WARN_IF(!actorChild)) {
return;
}

auto child = MakeRefPtr<dom::VsyncMainChild>();
dom::PVsyncChild* actor = actorChild->SendPVsyncConstructor(child);
auto vsyncChildViaPBackground = MakeRefPtr<dom::VsyncMainChild>();
dom::PVsyncChild* actor =
actorChild->SendPVsyncConstructor(vsyncChildViaPBackground);
if (NS_WARN_IF(!actor)) {
return;
}

RefPtr<RefreshDriverTimer> vsyncRefreshDriverTimer =
new VsyncRefreshDriverTimer(std::move(child));
VsyncRefreshDriverTimer::CreateForContentProcess(
std::move(vsyncChildViaPBackground));

sRegularRateTimer = std::move(vsyncRefreshDriverTimer);
}
Expand Down

0 comments on commit f046915

Please sign in to comment.