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. 4 5 #include "ui/gl/vsync_provider.h" 6 7 #include <math.h> 8 9 #include "base/logging.h" 10 #include "base/time/time.h" 11 12 namespace { 13 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; 18 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 24 } // namespace 25 26 namespace gfx { 27 28 VSyncProvider::VSyncProvider() { 29 } 30 31 VSyncProvider::~VSyncProvider() { 32 } 33 34 SyncControlVSyncProvider::SyncControlVSyncProvider() 35 : VSyncProvider(), 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 39 // per second. 40 last_good_interval_ = base::TimeDelta::FromSeconds(1) / 60; 41 } 42 43 SyncControlVSyncProvider::~SyncControlVSyncProvider() { 44 } 45 46 void SyncControlVSyncProvider::GetVSyncParameters( 47 const UpdateVSyncCallback& callback) { 48 #if defined(OS_LINUX) 49 base::TimeTicks timebase; 50 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). 58 int64 system_time; 59 int64 media_stream_counter; 60 int64 swap_buffer_counter; 61 if (!GetSyncValues(&system_time, 62 &media_stream_counter, 63 &swap_buffer_counter)) 64 return; 65 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."; 72 return; 73 } 74 75 struct timespec real_time; 76 struct timespec monotonic_time; 77 clock_gettime(CLOCK_REALTIME, &real_time); 78 clock_gettime(CLOCK_MONOTONIC, &monotonic_time); 79 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; 86 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); 92 93 if (time_conversion_needed) 94 system_time += monotonic_time_in_microseconds - real_time_in_microseconds; 95 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) 99 return; 100 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--; 106 } 107 if (monotonic_time_in_microseconds - system_time > 108 base::Time::kMicrosecondsPerSecond) 109 return; 110 111 timebase = base::TimeTicks::FromInternalValue(system_time); 112 113 // Only need the previous calculated interval for our filtering. 114 while (last_computed_intervals_.size() > 1) 115 last_computed_intervals_.pop(); 116 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); 127 } 128 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(); 132 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 << "."; 149 } else { 150 last_good_interval_ = new_interval; 151 } 152 } 153 } 154 155 last_timebase_ = timebase; 156 last_media_stream_counter_ = media_stream_counter; 157 callback.Run(timebase, last_good_interval_); 158 #endif // defined(OS_LINUX) 159 } 160 161 } // namespace gfx 162