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
|
|
|
|
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 {
|
|
|
|
|
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
|
2016-03-22 21:08:38 +03:00
|
|
|
layers::CompositorBridgeParent::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-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
|
|
|
{
|
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;
|
2016-05-05 11:45:00 +03:00
|
|
|
nsCOMPtr<nsIRunnable> vsyncControl = NewRunnableMethod<bool>(this,
|
2015-01-20 19:31:24 +03:00
|
|
|
&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
|
2016-03-22 21:08:38 +03:00
|
|
|
// shuts down and the CompositorBridgeParent shuts down.
|
2014-12-18 19:30:06 +03:00
|
|
|
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()) {
|
2016-05-05 11:45:00 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod(this,
|
|
|
|
&RefreshTimerVsyncDispatcher::UpdateVsyncStatus));
|
2015-01-20 19:31:26 +03:00
|
|
|
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
|