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.h"
14 // These constants define a reasonable range for a calculated refresh interval.
15 // Calculating refreshes out of this range will be considered a fatal error.
16 const int64 kMinVsyncIntervalUs
= base::Time::kMicrosecondsPerSecond
/ 400;
17 const int64 kMaxVsyncIntervalUs
= base::Time::kMicrosecondsPerSecond
/ 10;
19 // How much noise we'll tolerate between successive computed intervals before
20 // we think the latest computed interval is invalid (noisey due to
21 // monitor configuration change, moving a window between monitors, etc.).
22 const double kRelativeIntervalDifferenceThreshold
= 0.05;
28 VSyncProvider::VSyncProvider() {
31 VSyncProvider::~VSyncProvider() {
34 SyncControlVSyncProvider::SyncControlVSyncProvider()
36 last_media_stream_counter_(0) {
37 // On platforms where we can't get an accurate reading on the refresh
38 // rate we fall back to the assumption that we're displaying 60 frames
40 last_good_interval_
= base::TimeDelta::FromSeconds(1) / 60;
43 SyncControlVSyncProvider::~SyncControlVSyncProvider() {
46 void SyncControlVSyncProvider::GetVSyncParameters(
47 const UpdateVSyncCallback
& callback
) {
49 base::TimeTicks timebase
;
51 // The actual clock used for the system time returned by glXGetSyncValuesOML
52 // is unspecified. In practice, the clock used is likely to be either
53 // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the
54 // current time according to both clocks, and assume that the returned time
55 // was produced by the clock whose current time is closest to it, subject
56 // to the restriction that the returned time must not be in the future
57 // (since it is the time of a vblank that has already occurred).
59 int64 media_stream_counter
;
60 int64 swap_buffer_counter
;
61 if (!GetSyncValues(&system_time
,
62 &media_stream_counter
,
63 &swap_buffer_counter
))
66 // Both Intel and Mali drivers will return TRUE for GetSyncValues
67 // but a value of 0 for MSC if they cannot access the CRTC data structure
68 // associated with the surface. crbug.com/231945
69 if (media_stream_counter
== 0) {
70 LOG(ERROR
) << "glXGetSyncValuesOML should not return TRUE with a "
71 << "media stream counter of 0.";
75 struct timespec real_time
;
76 struct timespec monotonic_time
;
77 clock_gettime(CLOCK_REALTIME
, &real_time
);
78 clock_gettime(CLOCK_MONOTONIC
, &monotonic_time
);
80 int64 real_time_in_microseconds
=
81 real_time
.tv_sec
* base::Time::kMicrosecondsPerSecond
+
82 real_time
.tv_nsec
/ base::Time::kNanosecondsPerMicrosecond
;
83 int64 monotonic_time_in_microseconds
=
84 monotonic_time
.tv_sec
* base::Time::kMicrosecondsPerSecond
+
85 monotonic_time
.tv_nsec
/ base::Time::kNanosecondsPerMicrosecond
;
87 // We need the time according to CLOCK_MONOTONIC, so if we've been given
88 // a time from CLOCK_REALTIME, we need to convert.
89 bool time_conversion_needed
=
90 llabs(system_time
- real_time_in_microseconds
) <
91 llabs(system_time
- monotonic_time_in_microseconds
);
93 if (time_conversion_needed
)
94 system_time
+= monotonic_time_in_microseconds
- real_time_in_microseconds
;
96 // Return if |system_time| is more than 1 frames in the future.
97 int64 interval_in_microseconds
= last_good_interval_
.InMicroseconds();
98 if (system_time
> monotonic_time_in_microseconds
+ interval_in_microseconds
)
101 // If |system_time| is slightly in the future, adjust it to the previous
102 // frame and use the last frame counter to prevent issues in the callback.
103 if (system_time
> monotonic_time_in_microseconds
) {
104 system_time
-= interval_in_microseconds
;
105 media_stream_counter
--;
107 if (monotonic_time_in_microseconds
- system_time
>
108 base::Time::kMicrosecondsPerSecond
)
111 timebase
= base::TimeTicks::FromInternalValue(system_time
);
113 // Only need the previous calculated interval for our filtering.
114 while (last_computed_intervals_
.size() > 1)
115 last_computed_intervals_
.pop();
117 int32 numerator
, denominator
;
118 if (GetMscRate(&numerator
, &denominator
)) {
119 last_computed_intervals_
.push(
120 base::TimeDelta::FromSeconds(denominator
) / numerator
);
121 } else if (!last_timebase_
.is_null()) {
122 base::TimeDelta timebase_diff
= timebase
- last_timebase_
;
123 int64 counter_diff
= media_stream_counter
-
124 last_media_stream_counter_
;
125 if (counter_diff
> 0 && timebase
> last_timebase_
)
126 last_computed_intervals_
.push(timebase_diff
/ counter_diff
);
129 if (last_computed_intervals_
.size() == 2) {
130 const base::TimeDelta
& old_interval
= last_computed_intervals_
.front();
131 const base::TimeDelta
& new_interval
= last_computed_intervals_
.back();
133 double relative_change
=
134 fabs(old_interval
.InMillisecondsF() - new_interval
.InMillisecondsF()) /
135 new_interval
.InMillisecondsF();
136 if (relative_change
< kRelativeIntervalDifferenceThreshold
) {
137 if (new_interval
.InMicroseconds() < kMinVsyncIntervalUs
||
138 new_interval
.InMicroseconds() > kMaxVsyncIntervalUs
) {
139 LOG(FATAL
) << "Calculated bogus refresh interval of "
140 << new_interval
.InMicroseconds() << " us. "
141 << "Last time base of "
142 << last_timebase_
.ToInternalValue() << " us. "
143 << "Current time base of "
144 << timebase
.ToInternalValue() << " us. "
145 << "Last media stream count of "
146 << last_media_stream_counter_
<< ". "
147 << "Current media stream count of "
148 << media_stream_counter
<< ".";
150 last_good_interval_
= new_interval
;
155 last_timebase_
= timebase
;
156 last_media_stream_counter_
= media_stream_counter
;
157 callback
.Run(timebase
, last_good_interval_
);
158 #endif // defined(OS_LINUX)