File: vsync_thread_win.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (156 lines) | stat: -rw-r--r-- 5,210 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gl/vsync_thread_win.h"

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/memory/stack_allocated.h"
#include "base/notreached.h"
#include "base/power_monitor/power_monitor.h"
#include "base/synchronization/lock_subtle.h"
#include "base/time/time.h"
#include "base/trace_event/typed_macros.h"
#include "base/win/windows_version.h"
#include "ui/gl/direct_composition_support.h"
#include "ui/gl/gl_features.h"
#include "ui/gl/vsync_thread_win_dcomp.h"
#include "ui/gl/vsync_thread_win_dxgi.h"

namespace gl {
namespace {
// Whether the current thread holds the `VSyncThreadWin` lock.
thread_local bool g_current_thread_holds_lock = false;
}  // namespace

// static
VSyncThreadWin* VSyncThreadWin::GetInstance() {
  static VSyncThreadWin* vsync_thread = []() -> VSyncThreadWin* {
    if (features::UseCompositorClockVSyncInterval()) {
      return new VSyncThreadWinDComp();
    } else {
      Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device(
          GetDirectCompositionD3D11Device());
      if (!d3d11_device) {
        return nullptr;
      }
      Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device;
      CHECK_EQ(d3d11_device.As(&dxgi_device), S_OK);
      return new VSyncThreadWinDXGI(std::move(dxgi_device));
    }
  }();
  return vsync_thread;
}

class VSyncThreadWin::AutoVSyncThreadLock {
  STACK_ALLOCATED();

 public:
  AutoVSyncThreadLock(VSyncThreadWin* vsync_thread)
      EXCLUSIVE_LOCK_FUNCTION(vsync_thread->lock_) {
    if (g_current_thread_holds_lock) {
      vsync_thread->lock_.AssertAcquired();
    } else {
      auto_lock_.emplace(
          vsync_thread->lock_,
          // This lock is used to satisfy a mutual exclusion guarantee verified
          // by a SEQUENCE_CHECKER in `observers_`.
          base::subtle::LockTracking::kEnabled);
      g_current_thread_holds_lock = true;
    }
  }

  ~AutoVSyncThreadLock() UNLOCK_FUNCTION() {
    DCHECK(g_current_thread_holds_lock);
    if (auto_lock_.has_value()) {
      g_current_thread_holds_lock = false;
    }
  }

 private:
  std::optional<base::AutoLock> auto_lock_;
};

VSyncThreadWin::VSyncThreadWin() : vsync_thread_("GpuVSyncThread") {
  is_suspended_ = base::PowerMonitor::GetInstance()
                      ->AddPowerSuspendObserverAndReturnSuspendedState(this);
  vsync_thread_.StartWithOptions(
      base::Thread::Options(base::ThreadType::kDisplayCritical));
}

VSyncThreadWin::~VSyncThreadWin() {
  NOTREACHED();
}

void VSyncThreadWin::PostTaskIfNeeded() {
  lock_.AssertAcquired();
  // PostTaskIfNeeded is called from AddObserver and OnResume.
  // Before queuing up a task, make sure that there are observers waiting for
  // VSync and that we're not already firing events to consumers. Avoid firing
  // events if we're suspended to conserve battery life.
  if (!is_vsync_task_posted_ && !observers_.empty() && !is_suspended_) {
    vsync_thread_.task_runner()->PostTask(
        FROM_HERE,
        base::BindOnce(&VSyncThreadWin::WaitForVSync, base::Unretained(this)));
    is_vsync_task_posted_ = true;
  }
}

void VSyncThreadWin::AddObserver(VSyncObserver* obs) {
  AutoVSyncThreadLock auto_lock(this);
  observers_.AddObserver(obs);
  PostTaskIfNeeded();
}

void VSyncThreadWin::RemoveObserver(VSyncObserver* obs) {
  AutoVSyncThreadLock auto_lock(this);
  observers_.RemoveObserver(obs);
}

void VSyncThreadWin::OnSuspend() {
  AutoVSyncThreadLock auto_lock(this);
  is_suspended_ = true;
}

void VSyncThreadWin::OnResume() {
  AutoVSyncThreadLock auto_lock(this);
  is_suspended_ = false;
  PostTaskIfNeeded();
}

void VSyncThreadWin::WaitForVSync() {
  base::TimeDelta vsync_interval;

  const base::TimeTicks wait_for_vsync_start_time = base::TimeTicks::Now();
  bool wait_succeeded = WaitForVSyncImpl(&vsync_interval);
  const base::TimeDelta wait_for_vsync_elapsed_time =
      base::TimeTicks::Now() - wait_for_vsync_start_time;

  // WaitForVBlank and DCompositionWaitForCompositorClock returns very early
  // instead of waiting until vblank when the monitor goes to sleep or is
  // unplugged (nothing to present due to desktop occlusion). We use 1ms as
  // a threshhold for the duration of the wait functions and fallback to
  // Sleep() if it returns before that. This could happen during normal
  // operation for the first call after the vsync thread becomes non-idle,
  // but it shouldn't happen often.
  constexpr auto kVBlankIntervalThreshold = base::Milliseconds(1);
  if (!wait_succeeded ||
      wait_for_vsync_elapsed_time < kVBlankIntervalThreshold) {
    TRACE_EVENT0("gpu", "WaitForVSync Sleep");
    base::Time::ActivateHighResolutionTimer(true);
    Sleep(static_cast<DWORD>(vsync_interval.InMillisecondsRoundedUp()));
    base::Time::ActivateHighResolutionTimer(false);
  }

  AutoVSyncThreadLock auto_lock(this);
  DCHECK(is_vsync_task_posted_);
  is_vsync_task_posted_ = false;
  PostTaskIfNeeded();

  const base::TimeTicks vsync_time = base::TimeTicks::Now();
  observers_.Notify(&VSyncObserver::OnVSync, vsync_time, vsync_interval);
}

}  // namespace gl