sync_control_vsync_provider.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "ui/gl/sync_control_vsync_provider.h"
  5. #include <math.h>
  6. #include "base/logging.h"
  7. #include "base/time/time.h"
  8. #include "base/trace_event/trace_event.h"
  9. #include "build/build_config.h"
  10. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  11. // These constants define a reasonable range for a calculated refresh interval.
  12. // Calculating refreshes out of this range will be considered a fatal error.
  13. const int64_t kMinVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 400;
  14. const int64_t kMaxVsyncIntervalUs = base::Time::kMicrosecondsPerSecond / 10;
  15. // How much noise we'll tolerate between successive computed intervals before
  16. // we think the latest computed interval is invalid (noisey due to
  17. // monitor configuration change, moving a window between monitors, etc.).
  18. const double kRelativeIntervalDifferenceThreshold = 0.05;
  19. #endif
  20. namespace gl {
  21. SyncControlVSyncProvider::SyncControlVSyncProvider() : gfx::VSyncProvider() {
  22. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  23. // On platforms where we can't get an accurate reading on the refresh
  24. // rate we fall back to the assumption that we're displaying 60 frames
  25. // per second.
  26. last_good_interval_ = base::Seconds(1) / 60;
  27. #endif
  28. }
  29. SyncControlVSyncProvider::~SyncControlVSyncProvider() {}
  30. void SyncControlVSyncProvider::GetVSyncParameters(
  31. UpdateVSyncCallback callback) {
  32. base::TimeTicks timebase;
  33. base::TimeDelta interval;
  34. if (GetVSyncParametersIfAvailable(&timebase, &interval))
  35. std::move(callback).Run(timebase, interval);
  36. }
  37. bool SyncControlVSyncProvider::GetVSyncParametersIfAvailable(
  38. base::TimeTicks* timebase_out,
  39. base::TimeDelta* interval_out) {
  40. TRACE_EVENT0("gpu", "SyncControlVSyncProvider::GetVSyncParameters");
  41. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  42. // The actual clock used for the system time returned by glXGetSyncValuesOML
  43. // is unspecified. In practice, the clock used is likely to be either
  44. // CLOCK_REALTIME or CLOCK_MONOTONIC, so we compare the returned time to the
  45. // current time according to both clocks, and assume that the returned time
  46. // was produced by the clock whose current time is closest to it, subject
  47. // to the restriction that the returned time must not be in the future
  48. // (since it is the time of a vblank that has already occurred).
  49. int64_t system_time;
  50. int64_t media_stream_counter;
  51. int64_t swap_buffer_counter;
  52. if (!GetSyncValues(&system_time, &media_stream_counter, &swap_buffer_counter))
  53. return false;
  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. invalid_msc_ = (media_stream_counter == 0);
  58. if (invalid_msc_)
  59. return false;
  60. struct timespec real_time;
  61. clock_gettime(CLOCK_REALTIME, &real_time);
  62. // Note: A thread context switch could happen here, between the sampling of
  63. // the two different clocks.
  64. const base::TimeTicks monotonic_time = base::TimeTicks::Now();
  65. DCHECK_EQ(base::TimeTicks::GetClock(),
  66. base::TimeTicks::Clock::LINUX_CLOCK_MONOTONIC);
  67. int64_t real_time_in_microseconds =
  68. base::TimeDelta::FromTimeSpec(real_time).InMicroseconds();
  69. int64_t monotonic_time_in_microseconds =
  70. monotonic_time.since_origin().InMicroseconds();
  71. // We need the time according to CLOCK_MONOTONIC, so if we've been given
  72. // a time from CLOCK_REALTIME, we need to convert.
  73. bool time_conversion_needed =
  74. llabs(system_time - real_time_in_microseconds) <
  75. llabs(system_time - monotonic_time_in_microseconds);
  76. if (time_conversion_needed)
  77. system_time += monotonic_time_in_microseconds - real_time_in_microseconds;
  78. // Return if |system_time| is more than 1 frames in the future.
  79. int64_t interval_in_microseconds = last_good_interval_.InMicroseconds();
  80. if (system_time > monotonic_time_in_microseconds + interval_in_microseconds)
  81. return false;
  82. // If |system_time| is slightly in the future, adjust it to the previous
  83. // frame and use the last frame counter to prevent issues in the callback.
  84. if (system_time > monotonic_time_in_microseconds) {
  85. system_time -= interval_in_microseconds;
  86. media_stream_counter--;
  87. }
  88. if (monotonic_time_in_microseconds - system_time >
  89. base::Time::kMicrosecondsPerSecond)
  90. return false;
  91. const base::TimeTicks timebase =
  92. base::TimeTicks() + base::Microseconds(system_time);
  93. // Only need the previous calculated interval for our filtering.
  94. while (last_computed_intervals_.size() > 1)
  95. last_computed_intervals_.pop();
  96. int32_t numerator, denominator;
  97. if (GetMscRate(&numerator, &denominator) && numerator) {
  98. last_computed_intervals_.push(base::Seconds(denominator) / numerator);
  99. } else if (!last_timebase_.is_null()) {
  100. base::TimeDelta timebase_diff = timebase - last_timebase_;
  101. int64_t counter_diff = media_stream_counter - last_media_stream_counter_;
  102. if (counter_diff > 0 && timebase > last_timebase_)
  103. last_computed_intervals_.push(timebase_diff / counter_diff);
  104. }
  105. if (last_computed_intervals_.size() == 2) {
  106. const base::TimeDelta& old_interval = last_computed_intervals_.front();
  107. const base::TimeDelta& new_interval = last_computed_intervals_.back();
  108. double relative_change =
  109. fabs(old_interval.InMillisecondsF() - new_interval.InMillisecondsF()) /
  110. new_interval.InMillisecondsF();
  111. if (relative_change < kRelativeIntervalDifferenceThreshold) {
  112. if (new_interval.InMicroseconds() < kMinVsyncIntervalUs ||
  113. new_interval.InMicroseconds() > kMaxVsyncIntervalUs) {
  114. // For ChromeOS, we get the refresh interval from DRM through Ozone.
  115. // For Linux, we could use XRandR.
  116. // http://crbug.com/340851
  117. LOG(ERROR)
  118. << "Calculated bogus refresh interval=" << new_interval
  119. << ", last_timebase_=" << last_timebase_
  120. << ", timebase=" << timebase
  121. << ", last_media_stream_counter_=" << last_media_stream_counter_
  122. << ", media_stream_counter=" << media_stream_counter;
  123. } else {
  124. last_good_interval_ = new_interval;
  125. }
  126. }
  127. }
  128. last_timebase_ = timebase;
  129. last_media_stream_counter_ = media_stream_counter;
  130. *timebase_out = timebase;
  131. *interval_out = last_good_interval_;
  132. return true;
  133. #else
  134. return false;
  135. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  136. }
  137. bool SyncControlVSyncProvider::SupportGetVSyncParametersIfAvailable() const {
  138. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  139. return true;
  140. #else
  141. return false;
  142. #endif
  143. }
  144. } // namespace gl