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 20:21:34 +03:00
|
|
|
void VsyncSource::AddCompositorVsyncDispatcher(
|
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 20:21:34 +03:00
|
|
|
GetGlobalDisplay().AddCompositorVsyncDispatcher(aCompositorVsyncDispatcher);
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-13 20:21:34 +03:00
|
|
|
void VsyncSource::RemoveCompositorVsyncDispatcher(
|
2020-03-13 19:04:36 +03:00
|
|
|
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-03-13 20:21:34 +03:00
|
|
|
// See also AddCompositorVsyncDispatcher().
|
|
|
|
GetGlobalDisplay().RemoveCompositorVsyncDispatcher(
|
2020-03-13 19:04:36 +03:00
|
|
|
aCompositorVsyncDispatcher);
|
|
|
|
}
|
|
|
|
|
2020-03-13 20:21:34 +03:00
|
|
|
void VsyncSource::MoveListenersToNewSource(VsyncSource* aNewSource) {
|
|
|
|
GetGlobalDisplay().MoveListenersToNewSource(aNewSource->GetGlobalDisplay());
|
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 20:21:34 +03:00
|
|
|
mCompositorVsyncDispatchers.Clear();
|
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 20:21:34 +03:00
|
|
|
for (size_t i = 0; i < mCompositorVsyncDispatchers.Length(); i++) {
|
|
|
|
mCompositorVsyncDispatchers[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 20:21:34 +03:00
|
|
|
void VsyncSource::Display::AddCompositorVsyncDispatcher(
|
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 20:21:34 +03:00
|
|
|
if (!mCompositorVsyncDispatchers.Contains(aCompositorVsyncDispatcher)) {
|
|
|
|
mCompositorVsyncDispatchers.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 20:21:34 +03:00
|
|
|
void VsyncSource::Display::RemoveCompositorVsyncDispatcher(
|
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 20:21:34 +03:00
|
|
|
if (mCompositorVsyncDispatchers.Contains(aCompositorVsyncDispatcher)) {
|
|
|
|
mCompositorVsyncDispatchers.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 20:21:34 +03:00
|
|
|
VsyncSource::Display& aNewDisplay) {
|
2018-12-14 23:16:23 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
|
|
|
MutexAutoLock newLock(aNewDisplay.mDispatcherLock);
|
2020-03-13 20:21:34 +03:00
|
|
|
aNewDisplay.mCompositorVsyncDispatchers.AppendElements(
|
|
|
|
std::move(mCompositorVsyncDispatchers));
|
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 20:21:34 +03:00
|
|
|
enableVsync =
|
|
|
|
!mCompositorVsyncDispatchers.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
|