2016-05-05 11:44:59 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2015-01-09 02:12:47 +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 "SoftwareVsyncSource.h"
|
|
|
|
#include "base/task.h"
|
2015-09-15 18:13:57 +03:00
|
|
|
#include "gfxPlatform.h"
|
2015-01-09 02:12:47 +03:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-01-09 02:12:47 +03:00
|
|
|
SoftwareVsyncSource::SoftwareVsyncSource()
|
|
|
|
{
|
2015-03-09 18:19:07 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-09 02:12:47 +03:00
|
|
|
mGlobalDisplay = new SoftwareDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
SoftwareVsyncSource::~SoftwareVsyncSource()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mGlobalDisplay = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SoftwareDisplay::SoftwareDisplay()
|
2016-04-28 03:06:05 +03:00
|
|
|
: mVsyncEnabled(false)
|
2015-01-09 02:12:47 +03:00
|
|
|
{
|
|
|
|
// Mimic 60 fps
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-09-15 18:13:57 +03:00
|
|
|
const double rate = 1000.0 / (double) gfxPlatform::GetSoftwareVsyncRate();
|
2015-01-09 02:12:47 +03:00
|
|
|
mVsyncRate = mozilla::TimeDuration::FromMilliseconds(rate);
|
|
|
|
mVsyncThread = new base::Thread("SoftwareVsyncThread");
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mVsyncThread->Start(), "GFX: Could not start software vsync thread");
|
2015-01-09 02:12:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-10 17:59:21 +03:00
|
|
|
SoftwareDisplay::~SoftwareDisplay() {}
|
|
|
|
|
2015-01-09 02:12:47 +03:00
|
|
|
void
|
|
|
|
SoftwareDisplay::EnableVsync()
|
|
|
|
{
|
2015-04-10 17:59:21 +03:00
|
|
|
MOZ_ASSERT(mVsyncThread->IsRunning());
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (mVsyncEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mVsyncEnabled = true;
|
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
mVsyncThread->message_loop()->PostTask(
|
|
|
|
NewRunnableMethod(this, &SoftwareDisplay::EnableVsync));
|
2015-01-20 19:31:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-10 17:59:21 +03:00
|
|
|
MOZ_ASSERT(IsInSoftwareVsyncThread());
|
|
|
|
NotifyVsync(mozilla::TimeStamp::Now());
|
2015-01-09 02:12:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SoftwareDisplay::DisableVsync()
|
|
|
|
{
|
2015-04-10 17:59:21 +03:00
|
|
|
MOZ_ASSERT(mVsyncThread->IsRunning());
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (!mVsyncEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mVsyncEnabled = false;
|
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
mVsyncThread->message_loop()->PostTask(
|
|
|
|
NewRunnableMethod(this, &SoftwareDisplay::DisableVsync));
|
2015-01-20 19:31:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-10 17:59:21 +03:00
|
|
|
MOZ_ASSERT(IsInSoftwareVsyncThread());
|
|
|
|
if (mCurrentVsyncTask) {
|
|
|
|
mCurrentVsyncTask->Cancel();
|
|
|
|
mCurrentVsyncTask = nullptr;
|
2015-01-09 02:12:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SoftwareDisplay::IsVsyncEnabled()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mVsyncEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SoftwareDisplay::IsInSoftwareVsyncThread()
|
|
|
|
{
|
|
|
|
return mVsyncThread->thread_id() == PlatformThread::CurrentId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SoftwareDisplay::NotifyVsync(mozilla::TimeStamp aVsyncTimestamp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsInSoftwareVsyncThread());
|
2015-01-14 06:30:29 +03:00
|
|
|
|
|
|
|
mozilla::TimeStamp displayVsyncTime = aVsyncTimestamp;
|
|
|
|
mozilla::TimeStamp now = mozilla::TimeStamp::Now();
|
|
|
|
// Posted tasks can only have integer millisecond delays
|
|
|
|
// whereas TimeDurations can have floating point delays.
|
|
|
|
// Thus the vsync timestamp can be in the future, which large parts
|
|
|
|
// of the system can't handle, including animations. Force the timestamp to be now.
|
|
|
|
if (aVsyncTimestamp > now) {
|
|
|
|
displayVsyncTime = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
Display::NotifyVsync(displayVsyncTime);
|
|
|
|
|
|
|
|
// Prevent skew by still scheduling based on the original
|
|
|
|
// vsync timestamp
|
2015-01-09 02:12:47 +03:00
|
|
|
ScheduleNextVsync(aVsyncTimestamp);
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:20:58 +03:00
|
|
|
mozilla::TimeDuration
|
|
|
|
SoftwareDisplay::GetVsyncRate()
|
|
|
|
{
|
|
|
|
return mVsyncRate;
|
|
|
|
}
|
|
|
|
|
2015-01-09 02:12:47 +03:00
|
|
|
void
|
|
|
|
SoftwareDisplay::ScheduleNextVsync(mozilla::TimeStamp aVsyncTimestamp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsInSoftwareVsyncThread());
|
|
|
|
mozilla::TimeStamp nextVsync = aVsyncTimestamp + mVsyncRate;
|
|
|
|
mozilla::TimeDuration delay = nextVsync - mozilla::TimeStamp::Now();
|
|
|
|
if (delay.ToMilliseconds() < 0) {
|
|
|
|
delay = mozilla::TimeDuration::FromMilliseconds(0);
|
2015-04-10 23:44:09 +03:00
|
|
|
nextVsync = mozilla::TimeStamp::Now();
|
2015-01-09 02:12:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-05 11:44:59 +03:00
|
|
|
mCurrentVsyncTask =
|
2016-05-05 11:45:00 +03:00
|
|
|
NewCancelableRunnableMethod<mozilla::TimeStamp>(this,
|
|
|
|
&SoftwareDisplay::NotifyVsync,
|
|
|
|
nextVsync);
|
2015-01-09 02:12:47 +03:00
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
RefPtr<Runnable> addrefedTask = mCurrentVsyncTask;
|
2016-04-28 03:06:05 +03:00
|
|
|
mVsyncThread->message_loop()->PostDelayedTask(
|
2016-05-05 11:44:59 +03:00
|
|
|
addrefedTask.forget(),
|
|
|
|
delay.ToMilliseconds());
|
2015-01-09 02:12:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-10 17:59:21 +03:00
|
|
|
void
|
|
|
|
SoftwareDisplay::Shutdown()
|
2015-01-09 02:12:47 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-04-10 17:59:21 +03:00
|
|
|
DisableVsync();
|
2015-03-09 18:19:07 +03:00
|
|
|
mVsyncThread->Stop();
|
2015-01-09 02:12:47 +03:00
|
|
|
delete mVsyncThread;
|
|
|
|
}
|