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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/layers/CompositorManagerChild.h"
#include "mozilla/StaticPrefs_layers.h"
#include "mozilla/layers/CompositorBridgeChild.h"
#include "mozilla/layers/CompositorManagerParent.h"
#include "mozilla/layers/CompositorThread.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/gfx/GPUProcessManager.h"
#include "mozilla/dom/ContentChild.h" // for ContentChild
#include "mozilla/dom/BrowserChild.h" // for BrowserChild
#include "mozilla/ipc/Endpoint.h"
#include "VsyncSource.h"
namespace mozilla {
namespace layers {
using gfx::GPUProcessManager;
StaticRefPtr<CompositorManagerChild> CompositorManagerChild::sInstance;
Atomic<base::ProcessId> CompositorManagerChild::sOtherPid(0);
/* static */
bool CompositorManagerChild::IsInitialized(uint64_t aProcessToken) {
MOZ_ASSERT(NS_IsMainThread());
return sInstance && sInstance->CanSend() &&
sInstance->mProcessToken == aProcessToken;
}
/* static */
void CompositorManagerChild::InitSameProcess(uint32_t aNamespace,
uint64_t aProcessToken) {
MOZ_ASSERT(NS_IsMainThread());
if (NS_WARN_IF(IsInitialized(aProcessToken))) {
MOZ_ASSERT_UNREACHABLE("Already initialized same process");
return;
}
RefPtr<CompositorManagerParent> parent =
CompositorManagerParent::CreateSameProcess();
RefPtr<CompositorManagerChild> child =
new CompositorManagerChild(parent, aProcessToken, aNamespace);
if (NS_WARN_IF(!child->CanSend())) {
MOZ_DIAGNOSTIC_ASSERT(false, "Failed to open same process protocol");
return;
}
parent->BindComplete(/* aIsRoot */ true);
sInstance = std::move(child);
sOtherPid = sInstance->OtherPid();
}
/* static */
bool CompositorManagerChild::Init(Endpoint<PCompositorManagerChild>&& aEndpoint,
uint32_t aNamespace,
uint64_t aProcessToken /* = 0 */) {
MOZ_ASSERT(NS_IsMainThread());
if (sInstance) {
MOZ_ASSERT(sInstance->mNamespace != aNamespace);
}
sInstance = new CompositorManagerChild(std::move(aEndpoint), aProcessToken,
aNamespace);
sOtherPid = sInstance->OtherPid();
return sInstance->CanSend();
}
/* static */
void CompositorManagerChild::Shutdown() {
MOZ_ASSERT(NS_IsMainThread());
CompositorBridgeChild::ShutDown();
if (!sInstance) {
return;
}
sInstance->Close();
sInstance = nullptr;
sOtherPid = 0;
}
/* static */
void CompositorManagerChild::OnGPUProcessLost(uint64_t aProcessToken) {
MOZ_ASSERT(NS_IsMainThread());
// Since GPUChild and CompositorManagerChild will race on ActorDestroy, we
// cannot know if the CompositorManagerChild is about to be released but has
// yet to be. As such, we want to pre-emptively set mCanSend to false.
if (sInstance && sInstance->mProcessToken == aProcessToken) {
sInstance->mCanSend = false;
sOtherPid = 0;
}
}
/* static */
bool CompositorManagerChild::CreateContentCompositorBridge(
uint32_t aNamespace) {
MOZ_ASSERT(NS_IsMainThread());
if (NS_WARN_IF(!sInstance || !sInstance->CanSend())) {
return false;
}
CompositorBridgeOptions options = ContentCompositorOptions();
RefPtr<CompositorBridgeChild> bridge = new CompositorBridgeChild(sInstance);
if (NS_WARN_IF(
!sInstance->SendPCompositorBridgeConstructor(bridge, options))) {
return false;
}
bridge->InitForContent(aNamespace);
return true;
}
/* static */
already_AddRefed<CompositorBridgeChild>
CompositorManagerChild::CreateWidgetCompositorBridge(
uint64_t aProcessToken, WebRenderLayerManager* aLayerManager,
uint32_t aNamespace, CSSToLayoutDeviceScale aScale,
const CompositorOptions& aOptions, bool aUseExternalSurfaceSize,
const gfx::IntSize& aSurfaceSize, uint64_t aInnerWindowId) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
if (NS_WARN_IF(!sInstance || !sInstance->CanSend())) {
return nullptr;
}
TimeDuration vsyncRate =
gfxPlatform::GetPlatform()->GetGlobalVsyncDispatcher()->GetVsyncRate();
CompositorBridgeOptions options = WidgetCompositorOptions(
aScale, vsyncRate, aOptions, aUseExternalSurfaceSize, aSurfaceSize,
aInnerWindowId);
RefPtr<CompositorBridgeChild> bridge = new CompositorBridgeChild(sInstance);
if (NS_WARN_IF(
!sInstance->SendPCompositorBridgeConstructor(bridge, options))) {
return nullptr;
}
bridge->InitForWidget(aProcessToken, aLayerManager, aNamespace);
return bridge.forget();
}
/* static */
already_AddRefed<CompositorBridgeChild>
CompositorManagerChild::CreateSameProcessWidgetCompositorBridge(
WebRenderLayerManager* aLayerManager, uint32_t aNamespace) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
if (NS_WARN_IF(!sInstance || !sInstance->CanSend())) {
return nullptr;
}
CompositorBridgeOptions options = SameProcessWidgetCompositorOptions();
RefPtr<CompositorBridgeChild> bridge = new CompositorBridgeChild(sInstance);
if (NS_WARN_IF(
!sInstance->SendPCompositorBridgeConstructor(bridge, options))) {
return nullptr;
}
bridge->InitForWidget(1, aLayerManager, aNamespace);
return bridge.forget();
}
CompositorManagerChild::CompositorManagerChild(CompositorManagerParent* aParent,
uint64_t aProcessToken,
uint32_t aNamespace)
: mProcessToken(aProcessToken),
mNamespace(aNamespace),
mResourceId(0),
mCanSend(false),
mSameProcess(true) {
MOZ_ASSERT(aParent);
SetOtherProcessId(base::GetCurrentProcId());
if (NS_WARN_IF(!Open(aParent, CompositorThread(), ipc::ChildSide))) {
return;
}
mCanSend = true;
AddRef();
SetReplyTimeout();
}
CompositorManagerChild::CompositorManagerChild(
Endpoint<PCompositorManagerChild>&& aEndpoint, uint64_t aProcessToken,
uint32_t aNamespace)
: mProcessToken(aProcessToken),
mNamespace(aNamespace),
mResourceId(0),
mCanSend(false),
mSameProcess(false) {
if (NS_WARN_IF(!aEndpoint.Bind(this))) {
return;
}
mCanSend = true;
AddRef();
SetReplyTimeout();
}
void CompositorManagerChild::ActorDealloc() {
MOZ_ASSERT(!mCanSend);
Release();
}
void CompositorManagerChild::ActorDestroy(ActorDestroyReason aReason) {
mCanSend = false;
if (sInstance == this) {
sInstance = nullptr;
}
}
void CompositorManagerChild::HandleFatalError(const char* aMsg) const {
dom::ContentChild::FatalErrorIfNotUsingGPUProcess(aMsg, OtherPid());
}
void CompositorManagerChild::ProcessingError(Result aCode,
const char* aReason) {
if (aCode != MsgDropped) {
gfxDevCrash(gfx::LogReason::ProcessingError)
<< "Processing error in CompositorBridgeChild: " << int(aCode);
}
}
void CompositorManagerChild::SetReplyTimeout() {
#ifndef DEBUG
// Add a timeout for release builds to kill GPU process when it hangs.
if (XRE_IsParentProcess() && GPUProcessManager::Get()->GetGPUChild()) {
int32_t timeout =
StaticPrefs::layers_gpu_process_ipc_reply_timeout_ms_AtStartup();
SetReplyTimeoutMs(timeout);
}
#endif
}
bool CompositorManagerChild::ShouldContinueFromReplyTimeout() {
if (XRE_IsParentProcess()) {
gfxCriticalNote << "Killing GPU process due to IPC reply timeout";
MOZ_DIAGNOSTIC_ASSERT(GPUProcessManager::Get()->GetGPUChild());
GPUProcessManager::Get()->KillProcess();
}
return false;
}
mozilla::ipc::IPCResult CompositorManagerChild::RecvNotifyWebRenderError(
const WebRenderError&& aError) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
GPUProcessManager::Get()->NotifyWebRenderError(aError);
return IPC_OK();
}
} // namespace layers
} // namespace mozilla
|