[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / gl / sync_control_vsync_provider.cc
blob9fee3687dde9da7eb78ced4623004edeafd9b5da
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.
5 #include "ui/gl/sync_control_vsync_provider.h"
7 #include <math.h>
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "base/trace_event/trace_event.h"
13 #if defined(OS_LINUX)
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;
23 #endif
25 namespace gfx {
27 SyncControlVSyncProvider::SyncControlVSyncProvider()
28 : VSyncProvider(), last_media_stream_counter_(0), invalid_msc_(false) {
29 // On platforms where we can't get an accurate reading on the refresh
30 // rate we fall back to the assumption that we're displaying 60 frames
31 // per second.
32 last_good_interval_ = base::TimeDelta::FromSeconds(1) / 60;
35 SyncControlVSyncProvider::~SyncControlVSyncProvider() {}
37 void SyncControlVSyncProvider::GetVSyncParameters(
38 const UpdateVSyncCallback& callback) {
39 TRACE_EVENT0("gpu", "SyncControlVSyncProvider::GetVSyncParameters");
40 #if defined(OS_LINUX)
41 base::TimeTicks timebase;
43 // The actual clock used for the system time returned by glXGetSyncValuesOML
44 // is unspecified. In practice, the clock used is likely to be either
45 // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the
46 // current time according to both clocks, and assume that the returned time
47 // was produced by the clock whose current time is closest to it, subject
48 // to the restriction that the returned time must not be in the future
49 // (since it is the time of a vblank that has already occurred).
50 int64 system_time;
51 int64 media_stream_counter;
52 int64 swap_buffer_counter;
53 if (!GetSyncValues(&system_time, &media_stream_counter, &swap_buffer_counter))
54 return;
56 // Both Intel and Mali drivers will return TRUE for GetSyncValues
57 // but a value of 0 for MSC if they cannot access the CRTC data structure
58 // associated with the surface. crbug.com/231945
59 bool prev_invalid_msc = invalid_msc_;
60 invalid_msc_ = (media_stream_counter == 0);
61 if (invalid_msc_) {
62 LOG_IF(ERROR, !prev_invalid_msc) << "glXGetSyncValuesOML "
63 "should not return TRUE with a media stream counter of 0.";
64 return;
67 struct timespec real_time;
68 struct timespec monotonic_time;
69 clock_gettime(CLOCK_REALTIME, &real_time);
70 clock_gettime(CLOCK_MONOTONIC, &monotonic_time);
72 int64 real_time_in_microseconds =
73 real_time.tv_sec * base::Time::kMicrosecondsPerSecond +
74 real_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
75 int64 monotonic_time_in_microseconds =
76 monotonic_time.tv_sec * base::Time::kMicrosecondsPerSecond +
77 monotonic_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
79 // We need the time according to CLOCK_MONOTONIC, so if we've been given
80 // a time from CLOCK_REALTIME, we need to convert.
81 bool time_conversion_needed =
82 llabs(system_time - real_time_in_microseconds) <
83 llabs(system_time - monotonic_time_in_microseconds);
85 if (time_conversion_needed)
86 system_time += monotonic_time_in_microseconds - real_time_in_microseconds;
88 // Return if |system_time| is more than 1 frames in the future.
89 int64 interval_in_microseconds = last_good_interval_.InMicroseconds();
90 if (system_time > monotonic_time_in_microseconds + interval_in_microseconds)
91 return;
93 // If |system_time| is slightly in the future, adjust it to the previous
94 // frame and use the last frame counter to prevent issues in the callback.
95 if (system_time > monotonic_time_in_microseconds) {
96 system_time -= interval_in_microseconds;
97 media_stream_counter--;
99 if (monotonic_time_in_microseconds - system_time >
100 base::Time::kMicrosecondsPerSecond)
101 return;
103 timebase = base::TimeTicks::FromInternalValue(system_time);
105 // Only need the previous calculated interval for our filtering.
106 while (last_computed_intervals_.size() > 1)
107 last_computed_intervals_.pop();
109 int32 numerator, denominator;
110 if (GetMscRate(&numerator, &denominator)) {
111 last_computed_intervals_.push(base::TimeDelta::FromSeconds(denominator) /
112 numerator);
113 } else if (!last_timebase_.is_null()) {
114 base::TimeDelta timebase_diff = timebase - last_timebase_;
115 int64 counter_diff = media_stream_counter - last_media_stream_counter_;
116 if (counter_diff > 0 && timebase > last_timebase_)
117 last_computed_intervals_.push(timebase_diff / counter_diff);
120 if (last_computed_intervals_.size() == 2) {
121 const base::TimeDelta& old_interval = last_computed_intervals_.front();
122 const base::TimeDelta& new_interval = last_computed_intervals_.back();
124 double relative_change =
125 fabs(old_interval.InMillisecondsF() - new_interval.InMillisecondsF()) /
126 new_interval.InMillisecondsF();
127 if (relative_change < kRelativeIntervalDifferenceThreshold) {
128 if (new_interval.InMicroseconds() < kMinVsyncIntervalUs ||
129 new_interval.InMicroseconds() > kMaxVsyncIntervalUs) {
130 #if defined(USE_ASH)
131 // On ash platforms (ChromeOS essentially), the real refresh interval is
132 // queried from XRandR, regardless of the value calculated here, and
133 // this value is overriden by ui::CompositorVSyncManager. The log
134 // should not be fatal in this case. Reconsider all this when XRandR
135 // support is added to non-ash platforms.
136 // http://crbug.com/340851
137 LOG(ERROR)
138 #else
139 LOG(FATAL)
140 #endif // USE_ASH
141 << "Calculated bogus refresh interval="
142 << new_interval.InMicroseconds()
143 << " us., last_timebase_=" << last_timebase_.ToInternalValue()
144 << " us., timebase=" << timebase.ToInternalValue()
145 << " us., last_media_stream_counter_=" << last_media_stream_counter_
146 << ", media_stream_counter=" << media_stream_counter;
147 } else {
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)
159 } // namespace gfx