2014-10-22 02:40:54 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
2015-01-08 05:17:36 +03:00
|
|
|
#include "MainThreadUtils.h"
|
2014-10-22 02:40:54 +04:00
|
|
|
#include "VsyncDispatcher.h"
|
2014-12-18 19:30:06 +03:00
|
|
|
#include "VsyncSource.h"
|
2015-01-08 05:17:36 +03:00
|
|
|
#include "gfxPlatform.h"
|
2015-01-23 22:19:53 +03:00
|
|
|
#include "mozilla/layers/Compositor.h"
|
2016-03-22 21:08:38 +03:00
|
|
|
#include "mozilla/layers/CompositorBridgeParent.h"
|
2016-07-19 21:56:07 +03:00
|
|
|
#include "mozilla/layers/CompositorThread.h"
|
2014-10-24 05:50:31 +04:00
|
|
|
|
2017-03-14 13:44:54 +03:00
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
2014-10-22 02:40:54 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher::CompositorVsyncDispatcher()
|
2019-11-27 03:21:33 +03:00
|
|
|
: mVsyncSource(gfxPlatform::GetPlatform()->GetHardwareVsync()),
|
|
|
|
mCompositorObserverLock("CompositorObserverLock"),
|
|
|
|
mDidShutdown(false) {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-03-17 02:24:39 +03:00
|
|
|
|
|
|
|
mVsyncSource->RegisterCompositorVsyncDispatcher(this);
|
2019-11-27 03:21:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CompositorVsyncDispatcher::CompositorVsyncDispatcher(
|
|
|
|
RefPtr<gfx::VsyncSource> aVsyncSource)
|
|
|
|
: mVsyncSource(std::move(aVsyncSource)),
|
|
|
|
mCompositorObserverLock("CompositorObserverLock"),
|
|
|
|
mDidShutdown(false) {
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2015-01-08 05:17:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-03-17 02:24:39 +03:00
|
|
|
|
|
|
|
mVsyncSource->RegisterCompositorVsyncDispatcher(this);
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher::~CompositorVsyncDispatcher() {
|
2015-01-08 05:17:36 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-12-18 19:30:06 +03:00
|
|
|
// We auto remove this vsync dispatcher from the vsync source in the
|
|
|
|
// nsBaseWidget
|
2014-10-24 05:50:31 +04:00
|
|
|
}
|
|
|
|
|
2018-12-08 02:27:28 +03:00
|
|
|
void CompositorVsyncDispatcher::NotifyVsync(const VsyncEvent& aVsync) {
|
2015-01-20 19:31:24 +03:00
|
|
|
// In vsync thread
|
2018-12-08 02:27:28 +03:00
|
|
|
layers::CompositorBridgeParent::PostInsertVsyncProfilerMarker(aVsync.mTime);
|
2014-11-19 00:28:42 +03:00
|
|
|
|
2014-12-18 19:30:06 +03:00
|
|
|
MutexAutoLock lock(mCompositorObserverLock);
|
2015-01-08 05:17:36 +03:00
|
|
|
if (mCompositorVsyncObserver) {
|
2018-12-08 02:27:28 +03:00
|
|
|
mCompositorVsyncObserver->NotifyVsync(aVsync);
|
2014-10-24 05:50:31 +04:00
|
|
|
}
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
|
|
|
|
2020-03-17 02:24:39 +03:00
|
|
|
void CompositorVsyncDispatcher::MoveToSource(
|
|
|
|
const RefPtr<gfx::VsyncSource>& aVsyncSource) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
mVsyncSource = aVsyncSource;
|
|
|
|
}
|
|
|
|
|
2015-01-20 19:31:24 +03:00
|
|
|
void CompositorVsyncDispatcher::ObserveVsync(bool aEnable) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
if (mDidShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aEnable) {
|
2020-03-17 02:24:39 +03:00
|
|
|
mVsyncSource->EnableCompositorVsyncDispatcher(this);
|
2015-01-20 19:31:24 +03:00
|
|
|
} else {
|
2020-03-17 02:24:39 +03:00
|
|
|
mVsyncSource->DisableCompositorVsyncDispatcher(this);
|
2015-01-20 19:31:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
void CompositorVsyncDispatcher::SetCompositorVsyncObserver(
|
|
|
|
VsyncObserver* aVsyncObserver) {
|
2016-07-19 21:56:07 +03:00
|
|
|
// When remote compositing or running gtests, vsync observation is
|
|
|
|
// initiated on the main thread. Otherwise, it is initiated from the
|
|
|
|
// compositor thread.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread() ||
|
|
|
|
CompositorThreadHolder::IsInCompositorThread());
|
|
|
|
|
2015-01-23 22:19:53 +03:00
|
|
|
{ // scope lock
|
|
|
|
MutexAutoLock lock(mCompositorObserverLock);
|
|
|
|
mCompositorVsyncObserver = aVsyncObserver;
|
|
|
|
}
|
2015-01-20 19:31:24 +03:00
|
|
|
|
|
|
|
bool observeVsync = aVsyncObserver != nullptr;
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> vsyncControl = NewRunnableMethod<bool>(
|
|
|
|
"CompositorVsyncDispatcher::ObserveVsync", this,
|
|
|
|
&CompositorVsyncDispatcher::ObserveVsync, observeVsync);
|
2015-01-20 19:31:24 +03:00
|
|
|
NS_DispatchToMainThread(vsyncControl);
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
void CompositorVsyncDispatcher::Shutdown() {
|
|
|
|
// Need to explicitly remove CompositorVsyncDispatcher when the nsBaseWidget
|
2014-12-18 19:30:06 +03:00
|
|
|
// shuts down. Otherwise, we would get dead vsync notifications between when
|
2016-03-22 21:08:38 +03:00
|
|
|
// the nsBaseWidget shuts down and the CompositorBridgeParent shuts down.
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-03-17 02:24:39 +03:00
|
|
|
MOZ_ASSERT(!mDidShutdown);
|
2015-01-20 19:31:24 +03:00
|
|
|
ObserveVsync(false);
|
|
|
|
mDidShutdown = true;
|
|
|
|
{ // scope lock
|
|
|
|
MutexAutoLock lock(mCompositorObserverLock);
|
|
|
|
mCompositorVsyncObserver = nullptr;
|
|
|
|
}
|
2020-03-17 02:24:39 +03:00
|
|
|
mVsyncSource->DeregisterCompositorVsyncDispatcher(this);
|
|
|
|
mVsyncSource = nullptr;
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
2015-01-08 05:17:36 +03:00
|
|
|
|
2019-11-27 03:21:33 +03:00
|
|
|
RefreshTimerVsyncDispatcher::RefreshTimerVsyncDispatcher(
|
|
|
|
gfx::VsyncSource::Display* aDisplay)
|
2020-03-17 02:24:39 +03:00
|
|
|
: mDisplay(aDisplay), mRefreshTimersLock("RefreshTimers lock") {
|
2020-02-27 20:39:15 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2015-01-08 05:17:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
RefreshTimerVsyncDispatcher::~RefreshTimerVsyncDispatcher() {
|
2020-02-27 20:39:15 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2015-01-08 05:17:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2019-11-27 03:21:33 +03:00
|
|
|
void RefreshTimerVsyncDispatcher::MoveToDisplay(
|
|
|
|
gfx::VsyncSource::Display* aDisplay) {
|
2020-03-17 02:24:39 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-11-27 03:21:33 +03:00
|
|
|
mDisplay = aDisplay;
|
|
|
|
}
|
|
|
|
|
2018-12-08 02:27:28 +03:00
|
|
|
void RefreshTimerVsyncDispatcher::NotifyVsync(const VsyncEvent& aVsync) {
|
2015-01-08 05:17:36 +03:00
|
|
|
MutexAutoLock lock(mRefreshTimersLock);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mChildRefreshTimers.Length(); i++) {
|
2018-12-08 02:27:28 +03:00
|
|
|
mChildRefreshTimers[i]->NotifyVsync(aVsync);
|
2015-01-08 05:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mParentRefreshTimer) {
|
2018-12-08 02:27:28 +03:00
|
|
|
mParentRefreshTimer->NotifyVsync(aVsync);
|
2015-01-08 05:17:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefreshTimerVsyncDispatcher::SetParentRefreshTimer(
|
|
|
|
VsyncObserver* aVsyncObserver) {
|
2015-01-20 19:31:26 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
{ // lock scope because UpdateVsyncStatus runs on main thread and will
|
|
|
|
// deadlock
|
|
|
|
MutexAutoLock lock(mRefreshTimersLock);
|
|
|
|
mParentRefreshTimer = aVsyncObserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateVsyncStatus();
|
2015-01-08 05:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RefreshTimerVsyncDispatcher::AddChildRefreshTimer(
|
|
|
|
VsyncObserver* aVsyncObserver) {
|
2015-01-20 19:31:26 +03:00
|
|
|
{ // scope lock - called on pbackground thread
|
|
|
|
MutexAutoLock lock(mRefreshTimersLock);
|
|
|
|
MOZ_ASSERT(aVsyncObserver);
|
|
|
|
if (!mChildRefreshTimers.Contains(aVsyncObserver)) {
|
|
|
|
mChildRefreshTimers.AppendElement(aVsyncObserver);
|
|
|
|
}
|
2015-01-08 05:17:36 +03:00
|
|
|
}
|
2015-01-20 19:31:26 +03:00
|
|
|
|
|
|
|
UpdateVsyncStatus();
|
2015-01-08 05:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RefreshTimerVsyncDispatcher::RemoveChildRefreshTimer(
|
|
|
|
VsyncObserver* aVsyncObserver) {
|
2015-01-20 19:31:26 +03:00
|
|
|
{ // scope lock - called on pbackground thread
|
|
|
|
MutexAutoLock lock(mRefreshTimersLock);
|
|
|
|
MOZ_ASSERT(aVsyncObserver);
|
|
|
|
mChildRefreshTimers.RemoveElement(aVsyncObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateVsyncStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefreshTimerVsyncDispatcher::UpdateVsyncStatus() {
|
|
|
|
if (!NS_IsMainThread()) {
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod(
|
|
|
|
"RefreshTimerVsyncDispatcher::UpdateVsyncStatus", this,
|
|
|
|
&RefreshTimerVsyncDispatcher::UpdateVsyncStatus));
|
2015-01-20 19:31:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-27 03:21:33 +03:00
|
|
|
mDisplay->NotifyRefreshTimerVsyncStatus(NeedsVsync());
|
2015-01-20 19:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RefreshTimerVsyncDispatcher::NeedsVsync() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-08 05:17:36 +03:00
|
|
|
MutexAutoLock lock(mRefreshTimersLock);
|
2015-01-20 19:31:26 +03:00
|
|
|
return (mParentRefreshTimer != nullptr) || !mChildRefreshTimers.IsEmpty();
|
2015-01-08 05:17:36 +03:00
|
|
|
}
|
|
|
|
|
2014-10-22 02:40:54 +04:00
|
|
|
} // namespace mozilla
|