gecko-dev/gfx/thebes/VsyncSource.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

218 строки
7.2 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 20; 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/. */
#include "VsyncSource.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "mozilla/VsyncDispatcher.h"
#include "MainThreadUtils.h"
namespace mozilla {
namespace gfx {
void VsyncSource::EnableCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
// Just use the global display until we have enough information to get the
// corresponding display for compositor.
GetGlobalDisplay().EnableCompositorVsyncDispatcher(
aCompositorVsyncDispatcher);
}
void VsyncSource::DisableCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
// See also EnableCompositorVsyncDispatcher().
GetGlobalDisplay().DisableCompositorVsyncDispatcher(
aCompositorVsyncDispatcher);
}
void VsyncSource::RegisterCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
GetGlobalDisplay().RegisterCompositorVsyncDispatcher(
aCompositorVsyncDispatcher);
}
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);
}
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<RefreshTimerVsyncDispatcher>
VsyncSource::GetRefreshTimerVsyncDispatcher() {
MOZ_ASSERT(XRE_IsParentProcess());
// See also AddCompositorVsyncDispatcher().
return GetGlobalDisplay().GetRefreshTimerVsyncDispatcher();
}
VsyncSource::Display::Display()
: mDispatcherLock("display dispatcher lock"),
mRefreshTimerNeedsVsync(false) {
MOZ_ASSERT(NS_IsMainThread());
mRefreshTimerVsyncDispatcher = new RefreshTimerVsyncDispatcher(this);
}
VsyncSource::Display::~Display() {
MOZ_ASSERT(NS_IsMainThread());
MutexAutoLock lock(mDispatcherLock);
mRefreshTimerVsyncDispatcher = nullptr;
MOZ_ASSERT(mRegisteredCompositorVsyncDispatchers.Length() == 0);
MOZ_ASSERT(mEnabledCompositorVsyncDispatchers.Length() == 0);
}
void VsyncSource::Display::NotifyVsync(TimeStamp aVsyncTimestamp) {
// Called on the vsync thread
MutexAutoLock lock(mDispatcherLock);
// 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;
}
mVsyncId = mVsyncId.Next();
VsyncEvent event(mVsyncId, aVsyncTimestamp);
for (size_t i = 0; i < mEnabledCompositorVsyncDispatchers.Length(); i++) {
mEnabledCompositorVsyncDispatchers[i]->NotifyVsync(event);
}
mRefreshTimerVsyncDispatcher->NotifyVsync(event);
}
TimeDuration VsyncSource::Display::GetVsyncRate() {
// If hardware queries fail / are unsupported, we have to just guess.
return TimeDuration::FromMilliseconds(1000.0 / 60.0);
}
void VsyncSource::Display::RegisterCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aCompositorVsyncDispatcher);
{ // scope lock
MutexAutoLock lock(mDispatcherLock);
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);
}
}
UpdateVsyncStatus();
}
void VsyncSource::Display::DisableCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aCompositorVsyncDispatcher);
{ // Scope lock
MutexAutoLock lock(mDispatcherLock);
if (mEnabledCompositorVsyncDispatchers.Contains(
aCompositorVsyncDispatcher)) {
mEnabledCompositorVsyncDispatchers.RemoveElement(
aCompositorVsyncDispatcher);
}
}
UpdateVsyncStatus();
}
void VsyncSource::Display::MoveListenersToNewSource(
const RefPtr<VsyncSource>& aNewSource) {
MOZ_ASSERT(NS_IsMainThread());
VsyncSource::Display& aNewDisplay = aNewSource->GetGlobalDisplay();
MutexAutoLock lock(mDispatcherLock);
MutexAutoLock newLock(aNewDisplay.mDispatcherLock);
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);
}
aNewDisplay.mRefreshTimerVsyncDispatcher = mRefreshTimerVsyncDispatcher;
mRefreshTimerVsyncDispatcher->MoveToDisplay(&aNewDisplay);
mRefreshTimerVsyncDispatcher = nullptr;
}
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 = !mEnabledCompositorVsyncDispatchers.IsEmpty() ||
mRefreshTimerNeedsVsync;
}
if (enableVsync) {
EnableVsync();
} else {
DisableVsync();
}
if (IsVsyncEnabled() != enableVsync) {
NS_WARNING("Vsync status did not change.");
}
}
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<RefreshTimerVsyncDispatcher>
VsyncSource::Display::GetRefreshTimerVsyncDispatcher() {
return mRefreshTimerVsyncDispatcher;
}
void VsyncSource::Shutdown() { GetGlobalDisplay().Shutdown(); }
} // namespace gfx
} // namespace mozilla