1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gl/vsync_provider.h"
9 #include "base/logging.h"
10 #include "base/time/time.h"
13 // These constants define a reasonable range for a calculated refresh interval.
14 // Calculating refreshes out of this range will be considered a fatal error.
15 const int64 kMinVsyncIntervalUs
= base::Time::kMicrosecondsPerSecond
/ 400;
16 const int64 kMaxVsyncIntervalUs
= base::Time::kMicrosecondsPerSecond
/ 10;
18 // How much noise we'll tolerate between successive computed intervals before
19 // we think the latest computed interval is invalid (noisey due to
20 // monitor configuration change, moving a window between monitors, etc.).
21 const double kRelativeIntervalDifferenceThreshold
= 0.05;
26 VSyncProvider::VSyncProvider() {
29 VSyncProvider::~VSyncProvider() {
32 SyncControlVSyncProvider::SyncControlVSyncProvider()
34 last_media_stream_counter_(0) {
35 // On platforms where we can't get an accurate reading on the refresh
36 // rate we fall back to the assumption that we're displaying 60 frames
38 last_good_interval_
= base::TimeDelta::FromSeconds(1) / 60;
41 SyncControlVSyncProvider::~SyncControlVSyncProvider() {
44 void SyncControlVSyncProvider::GetVSyncParameters(
45 const UpdateVSyncCallback
& callback
) {
47 base::TimeTicks timebase
;
49 // The actual clock used for the system time returned by glXGetSyncValuesOML
50 // is unspecified. In practice, the clock used is likely to be either
51 // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the
52 // current time according to both clocks, and assume that the returned time
53 // was produced by the clock whose current time is closest to it, subject
54 // to the restriction that the returned time must not be in the future
55 // (since it is the time of a vblank that has already occurred).
57 int64 media_stream_counter
;
58 int64 swap_buffer_counter
;
59 if (!GetSyncValues(&system_time
,
60 &media_stream_counter
,
61 &swap_buffer_counter
))
64 // Both Intel and Mali drivers will return TRUE for GetSyncValues
65 // but a value of 0 for MSC if they cannot access the CRTC data structure
66 // associated with the surface. crbug.com/231945
67 if (media_stream_counter
== 0) {
68 LOG(ERROR
) << "glXGetSyncValuesOML should not return TRUE with a "
69 << "media stream counter of 0.";
73 struct timespec real_time
;
74 struct timespec monotonic_time
;
75 clock_gettime(CLOCK_REALTIME
, &real_time
);
76 clock_gettime(CLOCK_MONOTONIC
, &monotonic_time
);
78 int64 real_time_in_microseconds
=
79 real_time
.tv_sec
* base::Time::kMicrosecondsPerSecond
+
80 real_time
.tv_nsec
/ base::Time::kNanosecondsPerMicrosecond
;
81 int64 monotonic_time_in_microseconds
=
82 monotonic_time
.tv_sec
* base::Time::kMicrosecondsPerSecond
+
83 monotonic_time
.tv_nsec
/ base::Time::kNanosecondsPerMicrosecond
;
85 // We need the time according to CLOCK_MONOTONIC, so if we've been given
86 // a time from CLOCK_REALTIME, we need to convert.
87 bool time_conversion_needed
=
88 llabs(system_time
- real_time_in_microseconds
) <
89 llabs(system_time
- monotonic_time_in_microseconds
);
91 if (time_conversion_needed
)
92 system_time
+= monotonic_time_in_microseconds
- real_time_in_microseconds
;
94 // Return if |system_time| is more than 1 frames in the future.
95 int64 interval_in_microseconds
= last_good_interval_
.InMicroseconds();
96 if (system_time
> monotonic_time_in_microseconds
+ interval_in_microseconds
)
99 // If |system_time| is slightly in the future, adjust it to the previous
100 // frame and use the last frame counter to prevent issues in the callback.
101 if (system_time
> monotonic_time_in_microseconds
) {
102 system_time
-= interval_in_microseconds
;
103 media_stream_counter
--;
105 if (monotonic_time_in_microseconds
- system_time
>
106 base::Time::kMicrosecondsPerSecond
)
109 timebase
= base::TimeTicks::FromInternalValue(system_time
);
111 // Only need the previous calculated interval for our filtering.
112 while (last_computed_intervals_
.size() > 1)
113 last_computed_intervals_
.pop();
115 int32 numerator
, denominator
;
116 if (GetMscRate(&numerator
, &denominator
)) {
117 last_computed_intervals_
.push(
118 base::TimeDelta::FromSeconds(denominator
) / numerator
);
119 } else if (!last_timebase_
.is_null()) {
120 base::TimeDelta timebase_diff
= timebase
- last_timebase_
;
121 int64 counter_diff
= media_stream_counter
-
122 last_media_stream_counter_
;
123 if (counter_diff
> 0 && timebase
> last_timebase_
)
124 last_computed_intervals_
.push(timebase_diff
/ counter_diff
);
127 if (last_computed_intervals_
.size() == 2) {
128 const base::TimeDelta
& old_interval
= last_computed_intervals_
.front();
129 const base::TimeDelta
& new_interval
= last_computed_intervals_
.back();
131 double relative_change
=
132 fabs(old_interval
.InMillisecondsF() - new_interval
.InMillisecondsF()) /
133 new_interval
.InMillisecondsF();
134 if (relative_change
< kRelativeIntervalDifferenceThreshold
) {
135 if (new_interval
.InMicroseconds() < kMinVsyncIntervalUs
||
136 new_interval
.InMicroseconds() > kMaxVsyncIntervalUs
) {
137 LOG(FATAL
) << "Calculated bogus refresh interval of "
138 << new_interval
.InMicroseconds() << " us. "
139 << "Last time base of "
140 << last_timebase_
.ToInternalValue() << " us. "
141 << "Current time base of "
142 << timebase
.ToInternalValue() << " us. "
143 << "Last media stream count of "
144 << last_media_stream_counter_
<< ". "
145 << "Current media stream count of "
146 << media_stream_counter
<< ".";
148 last_good_interval_
= new_interval
;
153 last_timebase_
= timebase
;
154 last_media_stream_counter_
= media_stream_counter
;
155 callback
.Run(timebase
, last_good_interval_
);
156 #endif // defined(OS_LINUX)