1 // Copyright 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.
4
5 #include "ui/gl/sync_control_vsync_provider.h"
6
7 #include <math.h>
8
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "base/trace_event/trace_event.h"
12 #include "build/build_config.h"
13
14 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
15 // These constants define a reasonable range for a calculated refresh interval.
16 // Calculating refreshes out of this range will be considered a fatal error.
17 const int64_t kMinVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 400;
18 const int64_t kMaxVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 10;
19
20 // How much noise we'll tolerate between successive computed intervals before
21 // we think the latest computed interval is invalid (noisey due to
22 // monitor configuration change, moving a window between monitors, etc.).
23 const double kRelativeIntervalDifferenceThreshold = 0.05;
24 #endif
25
26 namespace gl {
27
SyncControlVSyncProvider()28 SyncControlVSyncProvider::SyncControlVSyncProvider() : gfx::VSyncProvider() {
29 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
30 // On platforms where we can't get an accurate reading on the refresh
31 // rate we fall back to the assumption that we're displaying 60 frames
32 // per second.
33 last_good_interval_ = base::TimeDelta::FromSeconds(1) / 60;
34 #endif
35 }
36
~SyncControlVSyncProvider()37 SyncControlVSyncProvider::~SyncControlVSyncProvider() {}
38
GetVSyncParameters(UpdateVSyncCallback callback)39 void SyncControlVSyncProvider::GetVSyncParameters(
40 UpdateVSyncCallback callback) {
41 base::TimeTicks timebase;
42 base::TimeDelta interval;
43 if (GetVSyncParametersIfAvailable(&timebase, &interval))
44 std::move(callback).Run(timebase, interval);
45 }
46
GetVSyncParametersIfAvailable(base::TimeTicks * timebase_out,base::TimeDelta * interval_out)47 bool SyncControlVSyncProvider::GetVSyncParametersIfAvailable(
48 base::TimeTicks* timebase_out,
49 base::TimeDelta* interval_out) {
50 TRACE_EVENT0("gpu", "SyncControlVSyncProvider::GetVSyncParameters");
51 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
52 // The actual clock used for the system time returned by glXGetSyncValuesOML
53 // is unspecified. In practice, the clock used is likely to be either
54 // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the
55 // current time according to both clocks, and assume that the returned time
56 // was produced by the clock whose current time is closest to it, subject
57 // to the restriction that the returned time must not be in the future
58 // (since it is the time of a vblank that has already occurred).
59 int64_t system_time;
60 int64_t media_stream_counter;
61 int64_t swap_buffer_counter;
62 if (!GetSyncValues(&system_time, &media_stream_counter, &swap_buffer_counter))
63 return false;
64
65 // Both Intel and Mali drivers will return TRUE for GetSyncValues
66 // but a value of 0 for MSC if they cannot access the CRTC data structure
67 // associated with the surface. crbug.com/231945
68 invalid_msc_ = (media_stream_counter == 0);
69 if (invalid_msc_)
70 return false;
71
72 struct timespec real_time;
73 clock_gettime(CLOCK_REALTIME, &real_time);
74 // Note: A thread context switch could happen here, between the sampling of
75 // the two different clocks.
76 const base::TimeTicks monotonic_time = base::TimeTicks::Now();
77 DCHECK_EQ(base::TimeTicks::GetClock(),
78 base::TimeTicks::Clock::LINUX_CLOCK_MONOTONIC);
79
80 int64_t real_time_in_microseconds =
81 base::TimeDelta::FromTimeSpec(real_time).InMicroseconds();
82 int64_t monotonic_time_in_microseconds =
83 monotonic_time.since_origin().InMicroseconds();
84
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);
90
91 if (time_conversion_needed)
92 system_time += monotonic_time_in_microseconds - real_time_in_microseconds;
93
94 // Return if |system_time| is more than 1 frames in the future.
95 int64_t interval_in_microseconds = last_good_interval_.InMicroseconds();
96 if (system_time > monotonic_time_in_microseconds + interval_in_microseconds)
97 return false;
98
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--;
104 }
105 if (monotonic_time_in_microseconds - system_time >
106 base::Time::kMicrosecondsPerSecond)
107 return false;
108
109 const base::TimeTicks timebase =
110 base::TimeTicks() + base::TimeDelta::FromMicroseconds(system_time);
111
112 // Only need the previous calculated interval for our filtering.
113 while (last_computed_intervals_.size() > 1)
114 last_computed_intervals_.pop();
115
116 int32_t numerator, denominator;
117 if (GetMscRate(&numerator, &denominator) && numerator) {
118 last_computed_intervals_.push(base::TimeDelta::FromSeconds(denominator) /
119 numerator);
120 } else if (!last_timebase_.is_null()) {
121 base::TimeDelta timebase_diff = timebase - last_timebase_;
122 int64_t counter_diff = media_stream_counter - last_media_stream_counter_;
123 if (counter_diff > 0 && timebase > last_timebase_)
124 last_computed_intervals_.push(timebase_diff / counter_diff);
125 }
126
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();
130
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 // For ChromeOS, we get the refresh interval from DRM through Ozone.
138 // For Linux, we could use XRandR.
139 // http://crbug.com/340851
140 LOG(ERROR)
141 << "Calculated bogus refresh interval=" << new_interval
142 << ", last_timebase_=" << last_timebase_
143 << ", timebase=" << timebase
144 << ", last_media_stream_counter_=" << last_media_stream_counter_
145 << ", media_stream_counter=" << media_stream_counter;
146 } else {
147 last_good_interval_ = new_interval;
148 }
149 }
150 }
151
152 last_timebase_ = timebase;
153 last_media_stream_counter_ = media_stream_counter;
154 *timebase_out = timebase;
155 *interval_out = last_good_interval_;
156 return true;
157 #else
158 return false;
159 #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
160 }
161
SupportGetVSyncParametersIfAvailable() const162 bool SyncControlVSyncProvider::SupportGetVSyncParametersIfAvailable() const {
163 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD)
164 return true;
165 #else
166 return false;
167 #endif
168 }
169
170 } // namespace gl
171