Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: ui/gl/sync_control_vsync_provider.cc

Issue 2696203004: Revert of "Route DXGI Frame Statistics to VSyncProvider" (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gl/sync_control_vsync_provider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gl/sync_control_vsync_provider.h" 5 #include "ui/gl/sync_control_vsync_provider.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "base/trace_event/trace_event.h" 11 #include "base/trace_event/trace_event.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 13
14 #if defined(OS_LINUX) || defined(OS_WIN) 14 #if defined(OS_LINUX)
15 // These constants define a reasonable range for a calculated refresh interval. 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. 16 // Calculating refreshes out of this range will be considered a fatal error.
17 const int64_t kMinVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 400; 17 const int64_t kMinVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 400;
18 const int64_t kMaxVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 10; 18 const int64_t kMaxVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 10;
19 19
20 // How much noise we'll tolerate between successive computed intervals before 20 // How much noise we'll tolerate between successive computed intervals before
21 // we think the latest computed interval is invalid (noisey due to 21 // we think the latest computed interval is invalid (noisey due to
22 // monitor configuration change, moving a window between monitors, etc.). 22 // monitor configuration change, moving a window between monitors, etc.).
23 const double kRelativeIntervalDifferenceThreshold = 0.05; 23 const double kRelativeIntervalDifferenceThreshold = 0.05;
24 #endif // defined(OS_LINUX) || defined(OS_WIN) 24 #endif
25 25
26 namespace gl { 26 namespace gl {
27 27
28 SyncControlVSyncProvider::SyncControlVSyncProvider() : gfx::VSyncProvider() { 28 SyncControlVSyncProvider::SyncControlVSyncProvider() : gfx::VSyncProvider() {
29 #if defined(OS_LINUX) || defined(OS_WIN) 29 #if defined(OS_LINUX)
30 // On platforms where we can't get an accurate reading on the refresh 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 31 // rate we fall back to the assumption that we're displaying 60 frames
32 // per second. 32 // per second.
33 last_good_interval_ = base::TimeDelta::FromSeconds(1) / 60; 33 last_good_interval_ = base::TimeDelta::FromSeconds(1) / 60;
34 #endif // defined(OS_LINUX) || defined(OS_WIN) 34 #endif
35 } 35 }
36 36
37 SyncControlVSyncProvider::~SyncControlVSyncProvider() {} 37 SyncControlVSyncProvider::~SyncControlVSyncProvider() {}
38 38
39 void SyncControlVSyncProvider::GetVSyncParameters( 39 void SyncControlVSyncProvider::GetVSyncParameters(
40 const UpdateVSyncCallback& callback) { 40 const UpdateVSyncCallback& callback) {
41 TRACE_EVENT0("gpu", "SyncControlVSyncProvider::GetVSyncParameters"); 41 TRACE_EVENT0("gpu", "SyncControlVSyncProvider::GetVSyncParameters");
42 #if defined(OS_LINUX) || defined(OS_WIN) 42 #if defined(OS_LINUX)
43 base::TimeTicks timebase; 43 base::TimeTicks timebase;
44 44
45 int64_t system_time;
46 int64_t media_stream_counter;
47 int64_t swap_buffer_counter;
48 if (!GetSyncValues(&system_time, &media_stream_counter, &swap_buffer_counter))
49 return;
50
51 if (media_stream_counter == last_media_stream_counter_) {
52 // SyncValues haven't updated, there is no reason to invoke the callback.
53 return;
54 }
55
56 // Perform platform specific adjustment of |system_time| and
57 // |media_stream_counter|.
58 if (!AdjustSyncValues(&system_time, &media_stream_counter))
59 return;
60
61 timebase = base::TimeTicks::FromInternalValue(system_time);
62
63 // Only need the previous calculated interval for our filtering.
64 while (last_computed_intervals_.size() > 1)
65 last_computed_intervals_.pop();
66
67 base::TimeDelta timebase_diff;
68 int64_t counter_diff = 0;
69
70 int32_t numerator, denominator;
71 if (GetMscRate(&numerator, &denominator) && numerator) {
72 timebase_diff = base::TimeDelta::FromSeconds(denominator);
73 counter_diff = numerator;
74 } else if (!last_timebase_.is_null()) {
75 timebase_diff = timebase - last_timebase_;
76 counter_diff = media_stream_counter - last_media_stream_counter_;
77 }
78
79 if (counter_diff > 0 && timebase_diff > base::TimeDelta()) {
80 last_computed_intervals_.push(timebase_diff / counter_diff);
81
82 if (last_computed_intervals_.size() == 2) {
83 const base::TimeDelta& old_interval = last_computed_intervals_.front();
84 const base::TimeDelta& new_interval = last_computed_intervals_.back();
85
86 double relative_change = fabs(old_interval.InMillisecondsF() -
87 new_interval.InMillisecondsF()) /
88 new_interval.InMillisecondsF();
89 if (relative_change < kRelativeIntervalDifferenceThreshold) {
90 if (new_interval.InMicroseconds() < kMinVsyncIntervalUs ||
91 new_interval.InMicroseconds() > kMaxVsyncIntervalUs) {
92 #if defined(OS_WIN) || defined(USE_ASH)
93 // On ash platforms (ChromeOS essentially), the real refresh interval
94 // is queried from XRandR, regardless of the value calculated here,
95 // and this value is overriden by ui::CompositorVSyncManager. The log
96 // should not be fatal in this case. Reconsider all this when XRandR
97 // support is added to non-ash platforms.
98 // http://crbug.com/340851
99 // On Windows |system_time| is based on QPC and it seems it may
100 // produce invalid value after a suspend/resume cycle.
101 // http://crbug.com/656469
102 LOG(ERROR)
103 #else
104 LOG(FATAL)
105 #endif // OS_WIN || USE_ASH
106 << "Calculated bogus refresh interval="
107 << new_interval.InMicroseconds()
108 << " us, old_interval=" << old_interval.InMicroseconds()
109 << " us, last_timebase_=" << last_timebase_.ToInternalValue()
110 << " us, timebase=" << timebase.ToInternalValue()
111 << " us, timebase_diff=" << timebase_diff.ToInternalValue()
112 << " us, last_timebase_diff_="
113 << last_timebase_diff_.ToInternalValue()
114 << " us, last_media_stream_counter_="
115 << last_media_stream_counter_
116 << ", media_stream_counter=" << media_stream_counter
117 << ", counter_diff=" << counter_diff
118 << ", last_counter_diff_=" << last_counter_diff_;
119 } else {
120 last_good_interval_ = new_interval;
121 }
122 }
123 }
124
125 last_timebase_diff_ = timebase_diff;
126 last_counter_diff_ = counter_diff;
127 }
128
129 last_timebase_ = timebase;
130 last_media_stream_counter_ = media_stream_counter;
131 callback.Run(timebase, last_good_interval_);
132 #endif // defined(OS_LINUX) || defined(OS_WIN)
133 }
134
135 #if defined(OS_LINUX)
136 bool SyncControlVSyncProvider::AdjustSyncValues(int64_t* system_time,
137 int64_t* media_stream_counter) {
138 // Both Intel and Mali drivers will return TRUE for GetSyncValues
139 // but a value of 0 for MSC if they cannot access the CRTC data structure
140 // associated with the surface. crbug.com/231945
141 bool prev_invalid_msc = invalid_msc_;
142 invalid_msc_ = (*media_stream_counter == 0);
143 if (invalid_msc_) {
144 LOG_IF(ERROR, !prev_invalid_msc)
145 << "glXGetSyncValuesOML "
146 "should not return TRUE with a media stream counter of 0.";
147 return false;
148 }
149
150 // The actual clock used for the system time returned by glXGetSyncValuesOML 45 // The actual clock used for the system time returned by glXGetSyncValuesOML
151 // is unspecified. In practice, the clock used is likely to be either 46 // is unspecified. In practice, the clock used is likely to be either
152 // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the 47 // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the
153 // current time according to both clocks, and assume that the returned time 48 // current time according to both clocks, and assume that the returned time
154 // was produced by the clock whose current time is closest to it, subject 49 // was produced by the clock whose current time is closest to it, subject
155 // to the restriction that the returned time must not be in the future 50 // to the restriction that the returned time must not be in the future
156 // (since it is the time of a vblank that has already occurred). 51 // (since it is the time of a vblank that has already occurred).
52 int64_t system_time;
53 int64_t media_stream_counter;
54 int64_t swap_buffer_counter;
55 if (!GetSyncValues(&system_time, &media_stream_counter, &swap_buffer_counter))
56 return;
57
58 // Both Intel and Mali drivers will return TRUE for GetSyncValues
59 // but a value of 0 for MSC if they cannot access the CRTC data structure
60 // associated with the surface. crbug.com/231945
61 bool prev_invalid_msc = invalid_msc_;
62 invalid_msc_ = (media_stream_counter == 0);
63 if (invalid_msc_) {
64 LOG_IF(ERROR, !prev_invalid_msc) << "glXGetSyncValuesOML "
65 "should not return TRUE with a media stream counter of 0.";
66 return;
67 }
68
157 struct timespec real_time; 69 struct timespec real_time;
158 struct timespec monotonic_time; 70 struct timespec monotonic_time;
159 clock_gettime(CLOCK_REALTIME, &real_time); 71 clock_gettime(CLOCK_REALTIME, &real_time);
160 clock_gettime(CLOCK_MONOTONIC, &monotonic_time); 72 clock_gettime(CLOCK_MONOTONIC, &monotonic_time);
161 73
162 int64_t real_time_in_microseconds = 74 int64_t real_time_in_microseconds =
163 real_time.tv_sec * base::Time::kMicrosecondsPerSecond + 75 real_time.tv_sec * base::Time::kMicrosecondsPerSecond +
164 real_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond; 76 real_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
165 int64_t monotonic_time_in_microseconds = 77 int64_t monotonic_time_in_microseconds =
166 monotonic_time.tv_sec * base::Time::kMicrosecondsPerSecond + 78 monotonic_time.tv_sec * base::Time::kMicrosecondsPerSecond +
167 monotonic_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond; 79 monotonic_time.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
168 80
169 // We need the time according to CLOCK_MONOTONIC, so if we've been given 81 // We need the time according to CLOCK_MONOTONIC, so if we've been given
170 // a time from CLOCK_REALTIME, we need to convert. 82 // a time from CLOCK_REALTIME, we need to convert.
171 bool time_conversion_needed = 83 bool time_conversion_needed =
172 llabs(*system_time - real_time_in_microseconds) < 84 llabs(system_time - real_time_in_microseconds) <
173 llabs(*system_time - monotonic_time_in_microseconds); 85 llabs(system_time - monotonic_time_in_microseconds);
174 86
175 if (time_conversion_needed) 87 if (time_conversion_needed)
176 *system_time += monotonic_time_in_microseconds - real_time_in_microseconds; 88 system_time += monotonic_time_in_microseconds - real_time_in_microseconds;
177 89
178 // Return if |*system_time| is more than 1 frames in the future. 90 // Return if |system_time| is more than 1 frames in the future.
179 int64_t interval_in_microseconds = last_good_interval_.InMicroseconds(); 91 int64_t interval_in_microseconds = last_good_interval_.InMicroseconds();
180 if (*system_time > monotonic_time_in_microseconds + interval_in_microseconds) 92 if (system_time > monotonic_time_in_microseconds + interval_in_microseconds)
181 return false; 93 return;
182 94
183 // If |system_time| is slightly in the future, adjust it to the previous 95 // If |system_time| is slightly in the future, adjust it to the previous
184 // frame and use the last frame counter to prevent issues in the callback. 96 // frame and use the last frame counter to prevent issues in the callback.
185 if (*system_time > monotonic_time_in_microseconds) { 97 if (system_time > monotonic_time_in_microseconds) {
186 *system_time -= interval_in_microseconds; 98 system_time -= interval_in_microseconds;
187 (*media_stream_counter)--; 99 media_stream_counter--;
188 } 100 }
189 if (monotonic_time_in_microseconds - *system_time > 101 if (monotonic_time_in_microseconds - system_time >
190 base::Time::kMicrosecondsPerSecond) 102 base::Time::kMicrosecondsPerSecond)
191 return false; 103 return;
192 104
193 return true; 105 timebase = base::TimeTicks::FromInternalValue(system_time);
106
107 // Only need the previous calculated interval for our filtering.
108 while (last_computed_intervals_.size() > 1)
109 last_computed_intervals_.pop();
110
111 int32_t numerator, denominator;
112 if (GetMscRate(&numerator, &denominator) && numerator) {
113 last_computed_intervals_.push(base::TimeDelta::FromSeconds(denominator) /
114 numerator);
115 } else if (!last_timebase_.is_null()) {
116 base::TimeDelta timebase_diff = timebase - last_timebase_;
117 int64_t counter_diff = media_stream_counter - last_media_stream_counter_;
118 if (counter_diff > 0 && timebase > last_timebase_)
119 last_computed_intervals_.push(timebase_diff / counter_diff);
120 }
121
122 if (last_computed_intervals_.size() == 2) {
123 const base::TimeDelta& old_interval = last_computed_intervals_.front();
124 const base::TimeDelta& new_interval = last_computed_intervals_.back();
125
126 double relative_change =
127 fabs(old_interval.InMillisecondsF() - new_interval.InMillisecondsF()) /
128 new_interval.InMillisecondsF();
129 if (relative_change < kRelativeIntervalDifferenceThreshold) {
130 if (new_interval.InMicroseconds() < kMinVsyncIntervalUs ||
131 new_interval.InMicroseconds() > kMaxVsyncIntervalUs) {
132 #if defined(USE_ASH)
133 // On ash platforms (ChromeOS essentially), the real refresh interval is
134 // queried from XRandR, regardless of the value calculated here, and
135 // this value is overriden by ui::CompositorVSyncManager. The log
136 // should not be fatal in this case. Reconsider all this when XRandR
137 // support is added to non-ash platforms.
138 // http://crbug.com/340851
139 LOG(ERROR)
140 #else
141 LOG(FATAL)
142 #endif // USE_ASH
143 << "Calculated bogus refresh interval="
144 << new_interval.InMicroseconds()
145 << " us., last_timebase_=" << last_timebase_.ToInternalValue()
146 << " us., timebase=" << timebase.ToInternalValue()
147 << " us., last_media_stream_counter_=" << last_media_stream_counter_
148 << ", media_stream_counter=" << 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)
194 } 159 }
195 #endif // defined(OS_LINUX)
196
197 #if defined(OS_WIN)
198 bool SyncControlVSyncProvider::AdjustSyncValues(int64_t* system_time,
199 int64_t* media_stream_counter) {
200 // Zero MSC is returned once when switching between windowed and full screen
201 // modes.
202 if (*media_stream_counter == 0)
203 return false;
204
205 // The actual clock used for the system time returned by glXGetSyncValuesEGL
206 // is unspecified. In practice, the clock comes from QueryPerformanceCounter.
207 LARGE_INTEGER perf_counter_now = {};
208 ::QueryPerformanceCounter(&perf_counter_now);
209 int64_t qpc_now =
210 base::TimeDelta::FromQPCValue(perf_counter_now.QuadPart).InMicroseconds();
211
212 // Return if |system_time| is more than 1 frames in the future.
213 int64_t interval_in_microseconds = last_good_interval_.InMicroseconds();
214 if (*system_time > qpc_now + interval_in_microseconds)
215 return false;
216
217 // If |system_time| is slightly in the future, adjust it to the previous
218 // frame and use the last frame counter to prevent issues in the callback.
219 if (*system_time > qpc_now) {
220 *system_time -= interval_in_microseconds;
221 (*media_stream_counter)--;
222 }
223 if (qpc_now - *system_time > base::Time::kMicrosecondsPerSecond)
224 return false;
225
226 return true;
227 }
228 #endif // defined(OS_WIN)
229 160
230 } // namespace gl 161 } // namespace gl
OLDNEW
« no previous file with comments | « ui/gl/sync_control_vsync_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698