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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "VsyncSource.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "mozilla/VsyncDispatcher.h"
#include "MainThreadUtils.h"
namespace mozilla {
namespace gfx {
void VsyncSource::EnableCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
// Just use the global display until we have enough information to get the
// corresponding display for compositor.
GetGlobalDisplay().EnableCompositorVsyncDispatcher(
aCompositorVsyncDispatcher);
}
void VsyncSource::DisableCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
// See also EnableCompositorVsyncDispatcher().
GetGlobalDisplay().DisableCompositorVsyncDispatcher(
aCompositorVsyncDispatcher);
}
void VsyncSource::RegisterCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
GetGlobalDisplay().RegisterCompositorVsyncDispatcher(
aCompositorVsyncDispatcher);
}
void VsyncSource::DeregisterCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
GetGlobalDisplay().DeregisterCompositorVsyncDispatcher(
aCompositorVsyncDispatcher);
}
void VsyncSource::AddGenericObserver(VsyncObserver* aObserver) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
GetGlobalDisplay().AddGenericObserver(aObserver);
}
void VsyncSource::RemoveGenericObserver(VsyncObserver* aObserver) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
GetGlobalDisplay().RemoveGenericObserver(aObserver);
}
void VsyncSource::MoveListenersToNewSource(
const RefPtr<VsyncSource>& aNewSource) {
GetGlobalDisplay().MoveListenersToNewSource(aNewSource);
}
RefPtr<RefreshTimerVsyncDispatcher>
VsyncSource::GetRefreshTimerVsyncDispatcher() {
MOZ_ASSERT(XRE_IsParentProcess());
// See also AddCompositorVsyncDispatcher().
return GetGlobalDisplay().GetRefreshTimerVsyncDispatcher();
}
VsyncSource::Display::Display()
: mDispatcherLock("display dispatcher lock"),
mRefreshTimerNeedsVsync(false),
mHasGenericObservers(false) {
MOZ_ASSERT(NS_IsMainThread());
mRefreshTimerVsyncDispatcher = new RefreshTimerVsyncDispatcher(this);
}
VsyncSource::Display::~Display() {
MOZ_ASSERT(NS_IsMainThread());
MutexAutoLock lock(mDispatcherLock);
mRefreshTimerVsyncDispatcher = nullptr;
MOZ_ASSERT(mRegisteredCompositorVsyncDispatchers.Length() == 0);
MOZ_ASSERT(mEnabledCompositorVsyncDispatchers.Length() == 0);
}
void VsyncSource::Display::NotifyVsync(const TimeStamp& aVsyncTimestamp,
const TimeStamp& aOutputTimestamp) {
// Called on the vsync thread
MutexAutoLock lock(mDispatcherLock);
// mRefreshTimerVsyncDispatcher might be null here if MoveListenersToNewSource
// was called concurrently with this function and won the race to acquire
// mDispatcherLock. In this case the new VsyncSource that is replacing this
// one will handle notifications from now on, so we can abort.
if (!mRefreshTimerVsyncDispatcher) {
return;
}
// If the task posted to the main thread from the last NotifyVsync call
// hasn't been processed yet, then don't send another one. Otherwise we might
// end up flooding the main thread.
bool dispatchToMainThread =
mHasGenericObservers &&
(mLastVsyncIdSentToMainThread == mLastMainThreadProcessedVsyncId);
mVsyncId = mVsyncId.Next();
const VsyncEvent event(mVsyncId, aVsyncTimestamp, aOutputTimestamp);
for (size_t i = 0; i < mEnabledCompositorVsyncDispatchers.Length(); i++) {
mEnabledCompositorVsyncDispatchers[i]->NotifyVsync(event);
}
mRefreshTimerVsyncDispatcher->NotifyVsync(event);
if (dispatchToMainThread) {
mLastVsyncIdSentToMainThread = mVsyncId;
NS_DispatchToMainThread(NewRunnableMethod<VsyncEvent>(
"VsyncSource::Display::NotifyGenericObservers", this,
&VsyncSource::Display::NotifyGenericObservers, event));
}
}
void VsyncSource::Display::NotifyGenericObservers(VsyncEvent aEvent) {
MOZ_ASSERT(NS_IsMainThread());
for (size_t i = 0; i < mGenericObservers.Length(); i++) {
mGenericObservers[i]->NotifyVsync(aEvent);
}
{ // Scope lock
MutexAutoLock lock(mDispatcherLock);
mLastMainThreadProcessedVsyncId = aEvent.mId;
}
}
TimeDuration VsyncSource::Display::GetVsyncRate() {
// If hardware queries fail / are unsupported, we have to just guess.
return TimeDuration::FromMilliseconds(1000.0 / 60.0);
}
void VsyncSource::Display::RegisterCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aCompositorVsyncDispatcher);
{ // scope lock
MutexAutoLock lock(mDispatcherLock);
mRegisteredCompositorVsyncDispatchers.AppendElement(
aCompositorVsyncDispatcher);
}
}
void VsyncSource::Display::DeregisterCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aCompositorVsyncDispatcher);
{ // Scope lock
MutexAutoLock lock(mDispatcherLock);
mRegisteredCompositorVsyncDispatchers.RemoveElement(
aCompositorVsyncDispatcher);
}
}
void VsyncSource::Display::EnableCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aCompositorVsyncDispatcher);
{ // scope lock
MutexAutoLock lock(mDispatcherLock);
if (!mEnabledCompositorVsyncDispatchers.Contains(
aCompositorVsyncDispatcher)) {
mEnabledCompositorVsyncDispatchers.AppendElement(
aCompositorVsyncDispatcher);
}
}
UpdateVsyncStatus();
}
void VsyncSource::Display::DisableCompositorVsyncDispatcher(
CompositorVsyncDispatcher* aCompositorVsyncDispatcher) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aCompositorVsyncDispatcher);
{ // Scope lock
MutexAutoLock lock(mDispatcherLock);
if (mEnabledCompositorVsyncDispatchers.Contains(
aCompositorVsyncDispatcher)) {
mEnabledCompositorVsyncDispatchers.RemoveElement(
aCompositorVsyncDispatcher);
}
}
UpdateVsyncStatus();
}
void VsyncSource::Display::AddGenericObserver(VsyncObserver* aObserver) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aObserver);
mGenericObservers.AppendElement(aObserver);
UpdateVsyncStatus();
}
void VsyncSource::Display::RemoveGenericObserver(VsyncObserver* aObserver) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aObserver);
mGenericObservers.RemoveElement(aObserver);
UpdateVsyncStatus();
}
void VsyncSource::Display::MoveListenersToNewSource(
const RefPtr<VsyncSource>& aNewSource) {
MOZ_ASSERT(NS_IsMainThread());
VsyncSource::Display& aNewDisplay = aNewSource->GetGlobalDisplay();
MutexAutoLock lock(mDispatcherLock);
MutexAutoLock newLock(aNewDisplay.mDispatcherLock);
aNewDisplay.mRegisteredCompositorVsyncDispatchers.AppendElements(
std::move(mRegisteredCompositorVsyncDispatchers));
aNewDisplay.mEnabledCompositorVsyncDispatchers.AppendElements(
std::move(mEnabledCompositorVsyncDispatchers));
aNewDisplay.mGenericObservers.AppendElements(std::move(mGenericObservers));
for (size_t i = 0;
i < aNewDisplay.mRegisteredCompositorVsyncDispatchers.Length(); i++) {
aNewDisplay.mRegisteredCompositorVsyncDispatchers[i]->MoveToSource(
aNewSource);
}
aNewDisplay.mRefreshTimerVsyncDispatcher = mRefreshTimerVsyncDispatcher;
mRefreshTimerVsyncDispatcher->MoveToDisplay(&aNewDisplay);
mRefreshTimerVsyncDispatcher = nullptr;
}
void VsyncSource::Display::NotifyRefreshTimerVsyncStatus(bool aEnable) {
MOZ_ASSERT(NS_IsMainThread());
mRefreshTimerNeedsVsync = aEnable;
UpdateVsyncStatus();
}
void VsyncSource::Display::UpdateVsyncStatus() {
MOZ_ASSERT(NS_IsMainThread());
// WARNING: This function SHOULD NOT BE CALLED WHILE HOLDING LOCKS
// NotifyVsync grabs a lock to dispatch vsync events
// When disabling vsync, we wait for the underlying thread to stop on some
// platforms We can deadlock if we wait for the underlying vsync thread to
// stop while the vsync thread is in NotifyVsync.
bool enableVsync = false;
{ // scope lock
MutexAutoLock lock(mDispatcherLock);
enableVsync = !mEnabledCompositorVsyncDispatchers.IsEmpty() ||
mRefreshTimerNeedsVsync || !mGenericObservers.IsEmpty();
mHasGenericObservers = !mGenericObservers.IsEmpty();
}
if (enableVsync) {
EnableVsync();
} else {
DisableVsync();
}
if (IsVsyncEnabled() != enableVsync) {
NS_WARNING("Vsync status did not change.");
}
}
RefPtr<RefreshTimerVsyncDispatcher>
VsyncSource::Display::GetRefreshTimerVsyncDispatcher() {
return mRefreshTimerVsyncDispatcher;
}
void VsyncSource::Shutdown() { GetGlobalDisplay().Shutdown(); }
} // namespace gfx
} // namespace mozilla
|