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 12 #if defined(OS_LINUX) 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; 17 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; 22 #endif 23 24 namespace gfx { 25 26 SyncControlVSyncProvider::SyncControlVSyncProvider() 27 : VSyncProvider(), last_media_stream_counter_(0) { 28 // On platforms where we can't get an accurate reading on the refresh 29 // rate we fall back to the assumption that we're displaying 60 frames 30 // per second. 31 last_good_interval_ = base::TimeDelta::FromSeconds(1) / 60; 32 } 33 34 SyncControlVSyncProvider::~SyncControlVSyncProvider() {} 35 36 void SyncControlVSyncProvider::GetVSyncParameters( 37 const UpdateVSyncCallback& callback) { 38 #if defined(OS_LINUX) 39 base::TimeTicks timebase; 40 41 // The actual clock used for the system time returned by glXGetSyncValuesOML 42 // is unspecified. In practice, the clock used is likely to be either 43 // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the 44 // current time according to both clocks, and assume that the returned time 45 // was produced by the clock whose current time is closest to it, subject 46 // to the restriction that the returned time must not be in the future 47 // (since it is the time of a vblank that has already occurred). 48 int64 system_time; 49 int64 media_stream_counter; 50 int64 swap_buffer_counter; 51 if (!GetSyncValues(&system_time, &media_stream_counter, &swap_buffer_counter)) 52 return; 53 54 // Both Intel and Mali drivers will return TRUE for GetSyncValues 55 // but a value of 0 for MSC if they cannot access the CRTC data structure 56 // associated with the surface. crbug.com/231945 57 if (media_stream_counter == 0) { 58 LOG(ERROR) << "glXGetSyncValuesOML should not return TRUE with a " 59 << "media stream counter of 0."; 60 return; 61 } 62 63 struct timespec real_time; 64 struct timespec monotonic_time; 65 clock_gettime(CLOCK_REALTIME, &real_time); 66 clock_gettime(CLOCK_MONOTONIC, &monotonic_time); 67 68 int64 real_time_in_microseconds = 69 real_time.tv_sec * base::Time::kMicrosecondsPerSecond + 70 real_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond; 71 int64 monotonic_time_in_microseconds = 72 monotonic_time.tv_sec * base::Time::kMicrosecondsPerSecond + 73 monotonic_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond; 74 75 // We need the time according to CLOCK_MONOTONIC, so if we've been given 76 // a time from CLOCK_REALTIME, we need to convert. 77 bool time_conversion_needed = 78 llabs(system_time - real_time_in_microseconds) < 79 llabs(system_time - monotonic_time_in_microseconds); 80 81 if (time_conversion_needed) 82 system_time += monotonic_time_in_microseconds - real_time_in_microseconds; 83 84 // Return if |system_time| is more than 1 frames in the future. 85 int64 interval_in_microseconds = last_good_interval_.InMicroseconds(); 86 if (system_time > monotonic_time_in_microseconds + interval_in_microseconds) 87 return; 88 89 // If |system_time| is slightly in the future, adjust it to the previous 90 // frame and use the last frame counter to prevent issues in the callback. 91 if (system_time > monotonic_time_in_microseconds) { 92 system_time -= interval_in_microseconds; 93 media_stream_counter--; 94 } 95 if (monotonic_time_in_microseconds - system_time > 96 base::Time::kMicrosecondsPerSecond) 97 return; 98 99 timebase = base::TimeTicks::FromInternalValue(system_time); 100 101 // Only need the previous calculated interval for our filtering. 102 while (last_computed_intervals_.size() > 1) 103 last_computed_intervals_.pop(); 104 105 int32 numerator, denominator; 106 if (GetMscRate(&numerator, &denominator)) { 107 last_computed_intervals_.push(base::TimeDelta::FromSeconds(denominator) / 108 numerator); 109 } else if (!last_timebase_.is_null()) { 110 base::TimeDelta timebase_diff = timebase - last_timebase_; 111 int64 counter_diff = media_stream_counter - last_media_stream_counter_; 112 if (counter_diff > 0 && timebase > last_timebase_) 113 last_computed_intervals_.push(timebase_diff / counter_diff); 114 } 115 116 if (last_computed_intervals_.size() == 2) { 117 const base::TimeDelta& old_interval = last_computed_intervals_.front(); 118 const base::TimeDelta& new_interval = last_computed_intervals_.back(); 119 120 double relative_change = 121 fabs(old_interval.InMillisecondsF() - new_interval.InMillisecondsF()) / 122 new_interval.InMillisecondsF(); 123 if (relative_change < kRelativeIntervalDifferenceThreshold) { 124 if (new_interval.InMicroseconds() < kMinVsyncIntervalUs || 125 new_interval.InMicroseconds() > kMaxVsyncIntervalUs) { 126 LOG(FATAL) << "Calculated bogus refresh interval of " 127 << new_interval.InMicroseconds() << " us. " 128 << "Last time base of " << last_timebase_.ToInternalValue() 129 << " us. " 130 << "Current time base of " << timebase.ToInternalValue() 131 << " us. " 132 << "Last media stream count of " 133 << last_media_stream_counter_ << ". " 134 << "Current media stream count of " << media_stream_counter 135 << "."; 136 } else { 137 last_good_interval_ = new_interval; 138 } 139 } 140 } 141 142 last_timebase_ = timebase; 143 last_media_stream_counter_ = media_stream_counter; 144 callback.Run(timebase, last_good_interval_); 145 #endif // defined(OS_LINUX) 146 } 147 148 } // namespace gfx 149