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
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
void VsyncSource::AddCompositorVsyncDispatcher(
|
|
|
|
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.
|
2014-12-19 23:52:42 +03:00
|
|
|
GetGlobalDisplay().AddCompositorVsyncDispatcher(aCompositorVsyncDispatcher);
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
void VsyncSource::RemoveCompositorVsyncDispatcher(
|
|
|
|
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
|
|
|
// See also AddCompositorVsyncDispatcher().
|
2014-12-19 23:52:42 +03:00
|
|
|
GetGlobalDisplay().RemoveCompositorVsyncDispatcher(
|
|
|
|
aCompositorVsyncDispatcher);
|
2014-12-18 19:30:06 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 23:16:23 +03:00
|
|
|
void VsyncSource::MoveListenersToNewSource(VsyncSource* aNewSource) {
|
|
|
|
GetGlobalDisplay().MoveListenersToNewSource(aNewSource->GetGlobalDisplay());
|
|
|
|
}
|
|
|
|
|
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());
|
2015-01-13 16:04:00 +03:00
|
|
|
mRefreshTimerVsyncDispatcher = new RefreshTimerVsyncDispatcher();
|
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;
|
2014-12-19 23:52:42 +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);
|
|
|
|
|
2015-01-13 16:04:00 +03:00
|
|
|
for (size_t i = 0; i < mCompositorVsyncDispatchers.Length(); i++) {
|
2018-12-08 02:27:28 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
void VsyncSource::Display::AddCompositorVsyncDispatcher(
|
|
|
|
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);
|
|
|
|
if (!mCompositorVsyncDispatchers.Contains(aCompositorVsyncDispatcher)) {
|
|
|
|
mCompositorVsyncDispatchers.AppendElement(aCompositorVsyncDispatcher);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
void VsyncSource::Display::RemoveCompositorVsyncDispatcher(
|
|
|
|
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);
|
|
|
|
if (mCompositorVsyncDispatchers.Contains(aCompositorVsyncDispatcher)) {
|
|
|
|
mCompositorVsyncDispatchers.RemoveElement(aCompositorVsyncDispatcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UpdateVsyncStatus();
|
|
|
|
}
|
|
|
|
|
2018-12-14 23:16:23 +03:00
|
|
|
void VsyncSource::Display::MoveListenersToNewSource(
|
|
|
|
VsyncSource::Display& aNewDisplay) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MutexAutoLock lock(mDispatcherLock);
|
|
|
|
MutexAutoLock newLock(aNewDisplay.mDispatcherLock);
|
|
|
|
aNewDisplay.mCompositorVsyncDispatchers.AppendElements(
|
|
|
|
std::move(mCompositorVsyncDispatchers));
|
|
|
|
|
|
|
|
aNewDisplay.mRefreshTimerVsyncDispatcher = mRefreshTimerVsyncDispatcher;
|
|
|
|
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);
|
|
|
|
enableVsync =
|
|
|
|
!mCompositorVsyncDispatchers.IsEmpty() || mRefreshTimerNeedsVsync;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|