forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayLink.cpp
More file actions
257 lines (201 loc) · 10.6 KB
/
DisplayLink.cpp
File metadata and controls
257 lines (201 loc) · 10.6 KB
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
/*
* Copyright (C) 2018-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DisplayLink.h"
#if HAVE(CVDISPLAYLINK)
#include "EventDispatcherMessages.h"
#include "Logging.h"
#include "WebProcessMessages.h"
#include <WebCore/AnimationFrameRate.h>
#include <wtf/ProcessPrivilege.h>
#include <wtf/text/TextStream.h>
namespace WebKit {
bool DisplayLink::shouldSendIPCOnBackgroundQueue { false };
constexpr unsigned maxFireCountWithoutObservers { 20 };
DisplayLink::DisplayLink(WebCore::PlatformDisplayID displayID)
: m_displayID(displayID)
{
// FIXME: We can get here with displayID == 0 (webkit.org/b/212120), in which case CVDisplayLinkCreateWithCGDisplay()
// probably defaults to the main screen.
ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
CVReturn error = CVDisplayLinkCreateWithCGDisplay(displayID, &m_displayLink);
if (error) {
WTFLogAlways("Could not create a display link for display %u: error %d", displayID, error);
return;
}
error = CVDisplayLinkSetOutputCallback(m_displayLink, displayLinkCallback, this);
if (error) {
WTFLogAlways("Could not set the display link output callback for display %u: error %d", displayID, error);
return;
}
m_displayNominalFramesPerSecond = nominalFramesPerSecondFromDisplayLink(m_displayLink);
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] Created DisplayLink " << this << " for display " << displayID << " with nominal fps " << m_displayNominalFramesPerSecond);
}
DisplayLink::~DisplayLink()
{
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] Destroying DisplayLink " << this << " for display " << m_displayID);
ASSERT(hasProcessPrivilege(ProcessPrivilege::CanCommunicateWithWindowServer));
ASSERT(m_displayLink);
if (!m_displayLink)
return;
CVDisplayLinkStop(m_displayLink);
CVDisplayLinkRelease(m_displayLink);
}
WebCore::FramesPerSecond DisplayLink::nominalFramesPerSecondFromDisplayLink(CVDisplayLinkRef displayLink)
{
CVTime refreshPeriod = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLink);
if (!refreshPeriod.timeValue)
return WebCore::FullSpeedFramesPerSecond;
WebCore::FramesPerSecond result = round((double)refreshPeriod.timeScale / (double)refreshPeriod.timeValue);
return result ?: WebCore::FullSpeedFramesPerSecond;
}
void DisplayLink::addObserver(IPC::Connection& connection, DisplayLinkObserverID observerID, WebCore::FramesPerSecond preferredFramesPerSecond)
{
ASSERT(RunLoop::isMain());
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] DisplayLink " << this << " for display display " << m_displayID << " add observer " << observerID << " fps " << preferredFramesPerSecond);
{
Locker locker { m_observersLock };
m_observers.ensure(connection.uniqueID(), [] {
return ConnectionClientInfo { };
}).iterator->value.observers.append({ observerID, preferredFramesPerSecond });
}
if (!CVDisplayLinkIsRunning(m_displayLink)) {
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] DisplayLink for display " << m_displayID << " starting CVDisplayLink with fps " << m_displayNominalFramesPerSecond);
CVReturn error = CVDisplayLinkStart(m_displayLink);
if (error)
WTFLogAlways("Could not start the display link: %d", error);
m_currentUpdate = { 0, m_displayNominalFramesPerSecond };
}
}
void DisplayLink::removeObserver(IPC::Connection& connection, DisplayLinkObserverID observerID)
{
ASSERT(RunLoop::isMain());
Locker locker { m_observersLock };
auto it = m_observers.find(connection.uniqueID());
if (it == m_observers.end())
return;
auto& connectionInfo = it->value;
bool removed = connectionInfo.observers.removeFirstMatching([observerID](const auto& value) {
return value.observerID == observerID;
});
ASSERT_UNUSED(removed, removed);
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] DisplayLink " << this << " for display " << m_displayID << " remove observer " << observerID);
removeInfoForConnectionIfPossible(connection);
// We do not stop the display link right away when |m_observers| becomes empty. Instead, we
// let the display link fire up to |maxFireCountWithoutObservers| times without observers to avoid
// killing & restarting too many threads when observers gets removed & added in quick succession.
}
void DisplayLink::removeObservers(IPC::Connection& connection)
{
ASSERT(RunLoop::isMain());
Locker locker { m_observersLock };
m_observers.remove(connection.uniqueID());
// We do not stop the display link right away when |m_observers| becomes empty. Instead, we
// let the display link fire up to |maxFireCountWithoutObservers| times without observers to avoid
// killing & restarting too many threads when observers gets removed & added in quick succession.
}
void DisplayLink::removeInfoForConnectionIfPossible(IPC::Connection& connection)
{
auto it = m_observers.find(connection.uniqueID());
if (it == m_observers.end())
return;
auto& connectionInfo = it->value;
if (connectionInfo.observers.isEmpty() && !connectionInfo.fullSpeedUpdatesClientCount)
m_observers.remove(it);
}
void DisplayLink::incrementFullSpeedRequestClientCount(IPC::Connection& connection)
{
Locker locker { m_observersLock };
auto& connectionInfo = m_observers.ensure(connection.uniqueID(), [] {
return ConnectionClientInfo { };
}).iterator->value;
++connectionInfo.fullSpeedUpdatesClientCount;
}
void DisplayLink::decrementFullSpeedRequestClientCount(IPC::Connection& connection)
{
Locker locker { m_observersLock };
auto it = m_observers.find(connection.uniqueID());
if (it == m_observers.end())
return;
auto& connectionInfo = it->value;
ASSERT(connectionInfo.fullSpeedUpdatesClientCount);
--connectionInfo.fullSpeedUpdatesClientCount;
removeInfoForConnectionIfPossible(connection);
}
void DisplayLink::setPreferredFramesPerSecond(IPC::Connection& connection, DisplayLinkObserverID observerID, WebCore::FramesPerSecond preferredFramesPerSecond)
{
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] DisplayLink " << this << " setPreferredFramesPerSecond - display " << m_displayID << " observer " << observerID << " fps " << preferredFramesPerSecond);
Locker locker { m_observersLock };
auto it = m_observers.find(connection.uniqueID());
if (it == m_observers.end())
return;
auto& connectionInfo = it->value;
auto index = connectionInfo.observers.findMatching([observerID](const auto& observer) {
return observer.observerID == observerID;
});
if (index != notFound)
connectionInfo.observers[index].preferredFramesPerSecond = preferredFramesPerSecond;
}
CVReturn DisplayLink::displayLinkCallback(CVDisplayLinkRef displayLinkRef, const CVTimeStamp*, const CVTimeStamp*, CVOptionFlags, CVOptionFlags*, void* data)
{
static_cast<DisplayLink*>(data)->notifyObserversDisplayWasRefreshed();
return kCVReturnSuccess;
}
void DisplayLink::notifyObserversDisplayWasRefreshed()
{
ASSERT(!RunLoop::isMain());
Locker locker { m_observersLock };
auto maxFramesPerSecond = [](const Vector<ObserverInfo>& observers) {
std::optional<WebCore::FramesPerSecond> observersMaxFramesPerSecond;
for (const auto& observer : observers)
observersMaxFramesPerSecond = std::max(observersMaxFramesPerSecond.value_or(0), observer.preferredFramesPerSecond);
return observersMaxFramesPerSecond;
};
bool anyConnectionHadObservers = false;
for (auto& [connectionID, connectionInfo] : m_observers) {
if (connectionInfo.observers.isEmpty())
continue;
anyConnectionHadObservers = true;
auto observersMaxFramesPerSecond = maxFramesPerSecond(connectionInfo.observers);
auto mainThreadWantsUpdate = m_currentUpdate.relevantForUpdateFrequency(observersMaxFramesPerSecond.value_or(WebCore::FullSpeedFramesPerSecond));
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] DisplayLink " << this << " for display " << m_displayID << " (display fps " << m_displayNominalFramesPerSecond << ") update " << m_currentUpdate << " " << connectionInfo.observers.size()
<< " observers, on background queue " << shouldSendIPCOnBackgroundQueue << " maxFramesPerSecond " << observersMaxFramesPerSecond << " full speed clients " << connectionInfo.fullSpeedUpdatesClientCount << " relevant " << mainThreadWantsUpdate);
if (connectionInfo.fullSpeedUpdatesClientCount) {
IPC::Connection::send(connectionID, Messages::EventDispatcher::DisplayWasRefreshed(m_displayID, m_currentUpdate, mainThreadWantsUpdate), 0);
} else if (mainThreadWantsUpdate)
IPC::Connection::send(connectionID, Messages::WebProcess::DisplayWasRefreshed(m_displayID, m_currentUpdate), 0);
}
m_currentUpdate = m_currentUpdate.nextUpdate();
if (!anyConnectionHadObservers) {
if (++m_fireCountWithoutObservers >= maxFireCountWithoutObservers) {
LOG_WITH_STREAM(DisplayLink, stream << "[UI ] DisplayLink for display " << m_displayID << " fired " << m_fireCountWithoutObservers << " times with no observers; stopping CVDisplayLink");
CVDisplayLinkStop(m_displayLink);
}
return;
}
m_fireCountWithoutObservers = 0;
}
} // namespace WebKit
#endif // HAVE(CVDISPLAYLINK)