This source file includes following definitions.
- invalid_msc_
- GetVSyncParameters
#include "ui/gl/sync_control_vsync_provider.h"
#include <math.h>
#include "base/logging.h"
#include "base/time/time.h"
#if defined(OS_LINUX)
const int64 kMinVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 400;
const int64 kMaxVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 10;
const double kRelativeIntervalDifferenceThreshold = 0.05;
#endif
namespace gfx {
SyncControlVSyncProvider::SyncControlVSyncProvider()
: VSyncProvider(), last_media_stream_counter_(0), invalid_msc_(false) {
last_good_interval_ = base::TimeDelta::FromSeconds(1) / 60;
}
SyncControlVSyncProvider::~SyncControlVSyncProvider() {}
void SyncControlVSyncProvider::GetVSyncParameters(
const UpdateVSyncCallback& callback) {
#if defined(OS_LINUX)
base::TimeTicks timebase;
int64 system_time;
int64 media_stream_counter;
int64 swap_buffer_counter;
if (!GetSyncValues(&system_time, &media_stream_counter, &swap_buffer_counter))
return;
bool prev_invalid_msc = invalid_msc_;
invalid_msc_ = (media_stream_counter == 0);
if (invalid_msc_) {
LOG_IF(ERROR, !prev_invalid_msc) << "glXGetSyncValuesOML "
"should not return TRUE with a media stream counter of 0.";
return;
}
struct timespec real_time;
struct timespec monotonic_time;
clock_gettime(CLOCK_REALTIME, &real_time);
clock_gettime(CLOCK_MONOTONIC, &monotonic_time);
int64 real_time_in_microseconds =
real_time.tv_sec * base::Time::kMicrosecondsPerSecond +
real_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
int64 monotonic_time_in_microseconds =
monotonic_time.tv_sec * base::Time::kMicrosecondsPerSecond +
monotonic_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
bool time_conversion_needed =
llabs(system_time - real_time_in_microseconds) <
llabs(system_time - monotonic_time_in_microseconds);
if (time_conversion_needed)
system_time += monotonic_time_in_microseconds - real_time_in_microseconds;
int64 interval_in_microseconds = last_good_interval_.InMicroseconds();
if (system_time > monotonic_time_in_microseconds + interval_in_microseconds)
return;
if (system_time > monotonic_time_in_microseconds) {
system_time -= interval_in_microseconds;
media_stream_counter--;
}
if (monotonic_time_in_microseconds - system_time >
base::Time::kMicrosecondsPerSecond)
return;
timebase = base::TimeTicks::FromInternalValue(system_time);
while (last_computed_intervals_.size() > 1)
last_computed_intervals_.pop();
int32 numerator, denominator;
if (GetMscRate(&numerator, &denominator)) {
last_computed_intervals_.push(base::TimeDelta::FromSeconds(denominator) /
numerator);
} else if (!last_timebase_.is_null()) {
base::TimeDelta timebase_diff = timebase - last_timebase_;
int64 counter_diff = media_stream_counter - last_media_stream_counter_;
if (counter_diff > 0 && timebase > last_timebase_)
last_computed_intervals_.push(timebase_diff / counter_diff);
}
if (last_computed_intervals_.size() == 2) {
const base::TimeDelta& old_interval = last_computed_intervals_.front();
const base::TimeDelta& new_interval = last_computed_intervals_.back();
double relative_change =
fabs(old_interval.InMillisecondsF() - new_interval.InMillisecondsF()) /
new_interval.InMillisecondsF();
if (relative_change < kRelativeIntervalDifferenceThreshold) {
if (new_interval.InMicroseconds() < kMinVsyncIntervalUs ||
new_interval.InMicroseconds() > kMaxVsyncIntervalUs) {
#if defined(USE_ASH)
LOG(ERROR)
#else
LOG(FATAL)
#endif
<< "Calculated bogus refresh interval="
<< new_interval.InMicroseconds()
<< " us., last_timebase_=" << last_timebase_.ToInternalValue()
<< " us., timebase=" << timebase.ToInternalValue()
<< " us., last_media_stream_counter_=" << last_media_stream_counter_
<< ", media_stream_counter=" << media_stream_counter;
} else {
last_good_interval_ = new_interval;
}
}
}
last_timebase_ = timebase;
last_media_stream_counter_ = media_stream_counter;
callback.Run(timebase, last_good_interval_);
#endif
}
}