2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2014-12-18 19:30:06 +03:00
|
|
|
* 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 "VsyncSource.h"
|
2015-01-13 16:04:00 +03:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2014-12-18 19:30:06 +03:00
|
|
|
#include "mozilla/VsyncDispatcher.h"
|
2015-01-08 05:17:36 +03:00
|
|
|
#include "MainThreadUtils.h"
|
2014-12-18 19:30:06 +03:00
|
|
|
|
2015-01-13 16:04:00 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
2014-12-18 19:30:06 +03:00
|
|
|
|
2020-03-13 19:04:36 +03:00
|
|
|
void VsyncSource::EnableCompositorVsyncDispatcher(
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
2015-01-13 16:04:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-13 16:04:00 +03:00
|
|
|
// Just use the global display until we have enough information to get the
|
|
|
|
// corresponding display for compositor.
|
2020-03-13 19:04:36 +03:00
|
|
|
GetGlobalDisplay().EnableCompositorVsyncDispatcher(
|
|
|
|
aCompositorVsyncDispatcher);
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-13 19:04:36 +03:00
|
|
|
void VsyncSource::DisableCompositorVsyncDispatcher(
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
2015-01-13 16:04:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-03-13 19:04:36 +03:00
|
|
|
// See also EnableCompositorVsyncDispatcher().
|
|
|
|
GetGlobalDisplay().DisableCompositorVsyncDispatcher(
|
|
|
|
aCompositorVsyncDispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VsyncSource::RegisterCompositorVsyncDispatcher(
|
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
GetGlobalDisplay().RegisterCompositorVsyncDispatcher(
|
2014-12-19 23:52:42 +03:00
|
|
|
aCompositorVsyncDispatcher);
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-13 19:04:36 +03:00
|
|
|
void VsyncSource::DeregisterCompositorVsyncDispatcher(
|
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
GetGlobalDisplay().DeregisterCompositorVsyncDispatcher(
|
|
|
|
aCompositorVsyncDispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VsyncSource::MoveListenersToNewSource(
|
|
|
|
const RefPtr<VsyncSource>& aNewSource) {
|
|
|
|
GetGlobalDisplay().MoveListenersToNewSource(aNewSource);
|
2018-12-14 23:16:23 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RefreshTimerVsyncDispatcher>
|
2015-01-13 16:04:00 +03:00
|
|
|
VsyncSource::GetRefreshTimerVsyncDispatcher() {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
// See also AddCompositorVsyncDispatcher().
|
|
|
|
return GetGlobalDisplay().GetRefreshTimerVsyncDispatcher();
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VsyncSource::Display::Display()
|
2015-01-13 16:04:00 +03:00
|
|
|
: mDispatcherLock("display dispatcher lock"),
|
2015-01-20 19:31:24 +03:00
|
|
|
mRefreshTimerNeedsVsync(false) {
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-11-27 03:21:33 +03:00
|
|
|
mRefreshTimerVsyncDispatcher = new RefreshTimerVsyncDispatcher(this);
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VsyncSource::Display::~Display() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-13 16:04:00 +03:00
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
|
|
|
mRefreshTimerVsyncDispatcher = nullptr;
|
2020-03-13 19:04:36 +03:00
|
|
|
MOZ_ASSERT(mRegisteredCompositorVsyncDispatchers.Length() == 0);
|
|
|
|
MOZ_ASSERT(mEnabledCompositorVsyncDispatchers.Length() == 0);
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
2015-01-13 16:04:00 +03:00
|
|
|
void VsyncSource::Display::NotifyVsync(TimeStamp aVsyncTimestamp) {
|
|
|
|
// Called on the vsync thread
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
|
|
|
|
2019-02-14 01:46:19 +03:00
|
|
|
// mRefreshTimerVsyncDispatcher might be null here if MoveListenersToNewSource
|
|
|
|
// was called concurrently with this function and won the race to acquire
|
|
|
|
// mDispatcherLock. In this case the new VsyncSource that is replacing this
|
|
|
|
// one will handle notifications from now on, so we can abort.
|
|
|
|
if (!mRefreshTimerVsyncDispatcher) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-08 02:27:28 +03:00
|
|
|
mVsyncId = mVsyncId.Next();
|
|
|
|
VsyncEvent event(mVsyncId, aVsyncTimestamp);
|
|
|
|
|
2020-03-13 19:04:36 +03:00
|
|
|
for (size_t i = 0; i < mEnabledCompositorVsyncDispatchers.Length(); i++) {
|
|
|
|
mEnabledCompositorVsyncDispatchers[i]->NotifyVsync(event);
|
2015-01-13 16:04:00 +03:00
|
|
|
}
|
|
|
|
|
2018-12-08 02:27:28 +03:00
|
|
|
mRefreshTimerVsyncDispatcher->NotifyVsync(event);
|
2015-01-13 16:04:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-06 19:20:58 +03:00
|
|
|
TimeDuration VsyncSource::Display::GetVsyncRate() {
|
|
|
|
// If hardware queries fail / are unsupported, we have to just guess.
|
|
|
|
return TimeDuration::FromMilliseconds(1000.0 / 60.0);
|
|
|
|
}
|
|
|
|
|
2020-03-13 19:04:36 +03:00
|
|
|
void VsyncSource::Display::RegisterCompositorVsyncDispatcher(
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-13 16:04:00 +03:00
|
|
|
MOZ_ASSERT(aCompositorVsyncDispatcher);
|
2015-01-20 19:31:24 +03:00
|
|
|
{ // scope lock
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
2020-03-13 19:04:36 +03:00
|
|
|
mRegisteredCompositorVsyncDispatchers.AppendElement(
|
|
|
|
aCompositorVsyncDispatcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VsyncSource::Display::DeregisterCompositorVsyncDispatcher(
|
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aCompositorVsyncDispatcher);
|
|
|
|
{ // Scope lock
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
|
|
|
mRegisteredCompositorVsyncDispatchers.RemoveElement(
|
|
|
|
aCompositorVsyncDispatcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VsyncSource::Display::EnableCompositorVsyncDispatcher(
|
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aCompositorVsyncDispatcher);
|
|
|
|
{ // scope lock
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
|
|
|
if (!mEnabledCompositorVsyncDispatchers.Contains(
|
|
|
|
aCompositorVsyncDispatcher)) {
|
|
|
|
mEnabledCompositorVsyncDispatchers.AppendElement(
|
|
|
|
aCompositorVsyncDispatcher);
|
2015-01-20 19:31:24 +03:00
|
|
|
}
|
2015-01-13 16:04:00 +03:00
|
|
|
}
|
2015-01-20 19:31:24 +03:00
|
|
|
UpdateVsyncStatus();
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-13 19:04:36 +03:00
|
|
|
void VsyncSource::Display::DisableCompositorVsyncDispatcher(
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-13 16:04:00 +03:00
|
|
|
MOZ_ASSERT(aCompositorVsyncDispatcher);
|
2015-01-20 19:31:24 +03:00
|
|
|
{ // Scope lock
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
2020-03-13 19:04:36 +03:00
|
|
|
if (mEnabledCompositorVsyncDispatchers.Contains(
|
|
|
|
aCompositorVsyncDispatcher)) {
|
|
|
|
mEnabledCompositorVsyncDispatchers.RemoveElement(
|
|
|
|
aCompositorVsyncDispatcher);
|
2015-01-20 19:31:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UpdateVsyncStatus();
|
|
|
|
}
|
|
|
|
|
2018-12-14 23:16:23 +03:00
|
|
|
void VsyncSource::Display::MoveListenersToNewSource(
|
2020-03-13 19:04:36 +03:00
|
|
|
const RefPtr<VsyncSource>& aNewSource) {
|
2018-12-14 23:16:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-03-13 19:04:36 +03:00
|
|
|
VsyncSource::Display& aNewDisplay = aNewSource->GetGlobalDisplay();
|
2018-12-14 23:16:23 +03:00
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
|
|
|
MutexAutoLock newLock(aNewDisplay.mDispatcherLock);
|
2020-03-13 19:04:36 +03:00
|
|
|
aNewDisplay.mRegisteredCompositorVsyncDispatchers.AppendElements(
|
|
|
|
std::move(mRegisteredCompositorVsyncDispatchers));
|
|
|
|
aNewDisplay.mEnabledCompositorVsyncDispatchers.AppendElements(
|
|
|
|
std::move(mEnabledCompositorVsyncDispatchers));
|
|
|
|
|
|
|
|
for (size_t i = 0;
|
|
|
|
i < aNewDisplay.mRegisteredCompositorVsyncDispatchers.Length(); i++) {
|
|
|
|
aNewDisplay.mRegisteredCompositorVsyncDispatchers[i]->MoveToSource(
|
|
|
|
aNewSource);
|
|
|
|
}
|
2018-12-14 23:16:23 +03:00
|
|
|
|
|
|
|
aNewDisplay.mRefreshTimerVsyncDispatcher = mRefreshTimerVsyncDispatcher;
|
2019-11-27 03:21:33 +03:00
|
|
|
mRefreshTimerVsyncDispatcher->MoveToDisplay(&aNewDisplay);
|
2018-12-14 23:16:23 +03:00
|
|
|
mRefreshTimerVsyncDispatcher = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-20 19:31:24 +03:00
|
|
|
void VsyncSource::Display::NotifyRefreshTimerVsyncStatus(bool aEnable) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mRefreshTimerNeedsVsync = aEnable;
|
|
|
|
UpdateVsyncStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VsyncSource::Display::UpdateVsyncStatus() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// WARNING: This function SHOULD NOT BE CALLED WHILE HOLDING LOCKS
|
|
|
|
// NotifyVsync grabs a lock to dispatch vsync events
|
|
|
|
// When disabling vsync, we wait for the underlying thread to stop on some
|
|
|
|
// platforms We can deadlock if we wait for the underlying vsync thread to
|
|
|
|
// stop while the vsync thread is in NotifyVsync.
|
|
|
|
bool enableVsync = false;
|
|
|
|
{ // scope lock
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
2020-03-13 19:04:36 +03:00
|
|
|
enableVsync = !mEnabledCompositorVsyncDispatchers.IsEmpty() ||
|
|
|
|
mRefreshTimerNeedsVsync;
|
2015-01-20 19:31:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (enableVsync) {
|
|
|
|
EnableVsync();
|
|
|
|
} else {
|
|
|
|
DisableVsync();
|
|
|
|
}
|
2015-03-15 07:23:53 +03:00
|
|
|
|
|
|
|
if (IsVsyncEnabled() != enableVsync) {
|
|
|
|
NS_WARNING("Vsync status did not change.");
|
|
|
|
}
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
2015-01-13 16:04:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RefreshTimerVsyncDispatcher>
|
2015-01-13 16:04:00 +03:00
|
|
|
VsyncSource::Display::GetRefreshTimerVsyncDispatcher() {
|
|
|
|
return mRefreshTimerVsyncDispatcher;
|
|
|
|
}
|
|
|
|
|
2016-06-06 20:07:29 +03:00
|
|
|
void VsyncSource::Shutdown() { GetGlobalDisplay().Shutdown(); }
|
|
|
|
|
2015-01-13 16:04:00 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|