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"
|
2015-01-08 05:17:36 +03:00
|
|
|
#include "mozilla/layers/CompositorParent.h"
|
2014-10-24 05:50:31 +04:00
|
|
|
|
2014-11-19 00:28:42 +03:00
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#include "ProfilerMarkers.h"
|
|
|
|
#endif
|
|
|
|
|
2014-10-22 02:40:54 +04:00
|
|
|
namespace mozilla {
|
2015-01-23 22:19:53 +03:00
|
|
|
static bool sThreadAssertionsEnabled = true;
|
|
|
|
|
|
|
|
void CompositorVsyncDispatcher::SetThreadAssertionsEnabled(bool aEnable)
|
|
|
|
{
|
|
|
|
// Should only be used in test environments
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
sThreadAssertionsEnabled = aEnable;
|
|
|
|
}
|
2014-10-22 02:40:54 +04:00
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher::CompositorVsyncDispatcher()
|
2014-10-24 05:50:31 +04:00
|
|
|
: mCompositorObserverLock("CompositorObserverLock")
|
2015-01-20 19:31:24 +03:00
|
|
|
, mDidShutdown(false)
|
2014-10-22 02:40:54 +04:00
|
|
|
{
|
2014-12-18 19:30:06 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2015-01-08 05:17:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
|
|
|
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher::~CompositorVsyncDispatcher()
|
2014-10-22 02:40:54 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher::NotifyVsync(TimeStamp aVsyncTimestamp)
|
2014-10-24 05:50:31 +04:00
|
|
|
{
|
2015-01-20 19:31:24 +03:00
|
|
|
// In vsync thread
|
2014-11-19 00:28:42 +03:00
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
2015-03-14 04:14:11 +03:00
|
|
|
layers::CompositorParent::PostInsertVsyncProfilerMarker(aVsyncTimestamp);
|
2014-11-19 00:28:42 +03:00
|
|
|
#endif
|
|
|
|
|
2014-12-18 19:30:06 +03:00
|
|
|
MutexAutoLock lock(mCompositorObserverLock);
|
2015-01-08 05:17:36 +03:00
|
|
|
if (mCompositorVsyncObserver) {
|
2014-12-18 19:30:06 +03:00
|
|
|
mCompositorVsyncObserver->NotifyVsync(aVsyncTimestamp);
|
2014-10-24 05:50:31 +04:00
|
|
|
}
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
|
|
|
|
2015-01-23 22:19:53 +03:00
|
|
|
void
|
|
|
|
CompositorVsyncDispatcher::AssertOnCompositorThread()
|
|
|
|
{
|
|
|
|
if (!sThreadAssertionsEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Compositor::AssertOnCompositorThread();
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
gfxPlatform::GetPlatform()->GetHardwareVsync()->AddCompositorVsyncDispatcher(this);
|
|
|
|
} else {
|
|
|
|
gfxPlatform::GetPlatform()->GetHardwareVsync()->RemoveCompositorVsyncDispatcher(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 02:40:54 +04:00
|
|
|
void
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher::SetCompositorVsyncObserver(VsyncObserver* aVsyncObserver)
|
2014-10-22 02:40:54 +04:00
|
|
|
{
|
2015-01-23 22:19:53 +03:00
|
|
|
AssertOnCompositorThread();
|
|
|
|
{ // scope lock
|
|
|
|
MutexAutoLock lock(mCompositorObserverLock);
|
|
|
|
mCompositorVsyncObserver = aVsyncObserver;
|
|
|
|
}
|
2015-01-20 19:31:24 +03:00
|
|
|
|
|
|
|
bool observeVsync = aVsyncObserver != nullptr;
|
|
|
|
nsCOMPtr<nsIRunnable> vsyncControl = NS_NewRunnableMethodWithArg<bool>(this,
|
|
|
|
&CompositorVsyncDispatcher::ObserveVsync,
|
|
|
|
observeVsync);
|
|
|
|
NS_DispatchToMainThread(vsyncControl);
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-19 23:52:42 +03:00
|
|
|
CompositorVsyncDispatcher::Shutdown()
|
2014-10-22 02:40:54 +04:00
|
|
|
{
|
2014-12-19 23:52:42 +03:00
|
|
|
// Need to explicitly remove CompositorVsyncDispatcher when the nsBaseWidget shuts down.
|
2014-12-18 19:30:06 +03:00
|
|
|
// Otherwise, we would get dead vsync notifications between when the nsBaseWidget
|
|
|
|
// shuts down and the CompositorParent shuts down.
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-20 19:31:24 +03:00
|
|
|
ObserveVsync(false);
|
|
|
|
mDidShutdown = true;
|
|
|
|
{ // scope lock
|
|
|
|
MutexAutoLock lock(mCompositorObserverLock);
|
|
|
|
mCompositorVsyncObserver = nullptr;
|
|
|
|
}
|
2014-10-22 02:40:54 +04:00
|
|
|
}
|
2015-01-08 05:17:36 +03:00
|
|
|
|
|
|
|
RefreshTimerVsyncDispatcher::RefreshTimerVsyncDispatcher()
|
|
|
|
: mRefreshTimersLock("RefreshTimers lock")
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
RefreshTimerVsyncDispatcher::~RefreshTimerVsyncDispatcher()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RefreshTimerVsyncDispatcher::NotifyVsync(TimeStamp aVsyncTimestamp)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mRefreshTimersLock);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mChildRefreshTimers.Length(); i++) {
|
|
|
|
mChildRefreshTimers[i]->NotifyVsync(aVsyncTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mParentRefreshTimer) {
|
|
|
|
mParentRefreshTimer->NotifyVsync(aVsyncTimestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
nsCOMPtr<nsIRunnable> vsyncControl = NS_NewRunnableMethod(this,
|
|
|
|
&RefreshTimerVsyncDispatcher::UpdateVsyncStatus);
|
|
|
|
NS_DispatchToMainThread(vsyncControl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::VsyncSource::Display& display = gfxPlatform::GetPlatform()->GetHardwareVsync()->GetGlobalDisplay();
|
|
|
|
display.NotifyRefreshTimerVsyncStatus(NeedsVsync());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|