/src/mozilla-central/gfx/vr/ipc/VRProcessManager.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "VRProcessManager.h" |
8 | | |
9 | | #include "VRProcessParent.h" |
10 | | #include "VRChild.h" |
11 | | #include "VRGPUChild.h" |
12 | | #include "VRGPUParent.h" |
13 | | |
14 | | |
15 | | namespace mozilla { |
16 | | namespace gfx { |
17 | | |
18 | | static StaticAutoPtr<VRProcessManager> sSingleton; |
19 | | |
20 | | /* static */ VRProcessManager* |
21 | | VRProcessManager::Get() |
22 | 0 | { |
23 | 0 | return sSingleton; |
24 | 0 | } |
25 | | |
26 | | /* static */ void |
27 | | VRProcessManager::Initialize() |
28 | 0 | { |
29 | 0 | MOZ_ASSERT(XRE_IsParentProcess()); |
30 | 0 | sSingleton = new VRProcessManager(); |
31 | 0 | } |
32 | | |
33 | | /* static */ void |
34 | | VRProcessManager::Shutdown() |
35 | 0 | { |
36 | 0 | sSingleton = nullptr; |
37 | 0 | } |
38 | | |
39 | | VRProcessManager::VRProcessManager() |
40 | | : mProcess(nullptr) |
41 | 0 | { |
42 | 0 | MOZ_COUNT_CTOR(VRProcessManager); |
43 | 0 |
|
44 | 0 | mObserver = new Observer(this); |
45 | 0 | nsContentUtils::RegisterShutdownObserver(mObserver); |
46 | 0 | } |
47 | | |
48 | | VRProcessManager::~VRProcessManager() |
49 | 0 | { |
50 | 0 | MOZ_COUNT_DTOR(VRProcessManager); |
51 | 0 |
|
52 | 0 | DestroyProcess(); |
53 | 0 | // The VR process should have already been shut down. |
54 | 0 | MOZ_ASSERT(!mProcess); |
55 | 0 | } |
56 | | |
57 | | void |
58 | | VRProcessManager::LaunchVRProcess() |
59 | 0 | { |
60 | 0 | if (mProcess) { |
61 | 0 | return; |
62 | 0 | } |
63 | 0 | |
64 | 0 | // The subprocess is launched asynchronously, so we wait for a callback to |
65 | 0 | // acquire the IPDL actor. |
66 | 0 | mProcess = new VRProcessParent(); |
67 | 0 | if (!mProcess->Launch()) { |
68 | 0 | DisableVRProcess("Failed to launch VR process"); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | | void |
73 | | VRProcessManager::DisableVRProcess(const char* aMessage) |
74 | 0 | { |
75 | 0 | if (!gfxPrefs::VRProcessEnabled()) { |
76 | 0 | return; |
77 | 0 | } |
78 | 0 | |
79 | 0 | DestroyProcess(); |
80 | 0 | } |
81 | | |
82 | | void |
83 | | VRProcessManager::DestroyProcess() |
84 | 0 | { |
85 | 0 | if (!mProcess) { |
86 | 0 | return; |
87 | 0 | } |
88 | 0 | |
89 | 0 | mProcess->Shutdown(); |
90 | 0 | mProcess = nullptr; |
91 | 0 | } |
92 | | |
93 | | bool |
94 | | VRProcessManager::CreateGPUBridges(base::ProcessId aOtherProcess, |
95 | | mozilla::ipc::Endpoint<PVRGPUChild>* aOutVRBridge) |
96 | 0 | { |
97 | 0 | if (!CreateGPUVRManager(aOtherProcess, aOutVRBridge)) { |
98 | 0 | return false; |
99 | 0 | } |
100 | 0 | return true; |
101 | 0 | } |
102 | | |
103 | | bool |
104 | | VRProcessManager::CreateGPUVRManager(base::ProcessId aOtherProcess, |
105 | | mozilla::ipc::Endpoint<PVRGPUChild>* aOutEndpoint) |
106 | 0 | { |
107 | 0 | base::ProcessId vrparentPid = mProcess |
108 | 0 | ? mProcess->OtherPid() // VR process id. |
109 | 0 | : base::GetCurrentProcId(); |
110 | 0 |
|
111 | 0 | ipc::Endpoint<PVRGPUParent> vrparentPipe; |
112 | 0 | ipc::Endpoint<PVRGPUChild> vrchildPipe; |
113 | 0 | nsresult rv = PVRGPU::CreateEndpoints(vrparentPid, // vr process id |
114 | 0 | aOtherProcess, // gpu process id |
115 | 0 | &vrparentPipe, |
116 | 0 | &vrchildPipe); |
117 | 0 |
|
118 | 0 | if (NS_FAILED(rv)) { |
119 | 0 | gfxCriticalNote << "Could not create gpu-vr bridge: " << hexa(int(rv)); |
120 | 0 | return false; |
121 | 0 | } |
122 | 0 |
|
123 | 0 | // Bind vr-gpu pipe to VRParent and make a PVRGPU connection. |
124 | 0 | VRChild* vrChild = mProcess->GetActor(); |
125 | 0 | vrChild->SendNewGPUVRManager(std::move(vrparentPipe)); |
126 | 0 |
|
127 | 0 | *aOutEndpoint = std::move(vrchildPipe); |
128 | 0 | return true; |
129 | 0 | } |
130 | | |
131 | | NS_IMPL_ISUPPORTS(VRProcessManager::Observer, nsIObserver); |
132 | | |
133 | | VRProcessManager::Observer::Observer(VRProcessManager* aManager) |
134 | | : mManager(aManager) |
135 | 0 | { |
136 | 0 | } |
137 | | |
138 | | NS_IMETHODIMP |
139 | | VRProcessManager::Observer::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData) |
140 | 0 | { |
141 | 0 | if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) { |
142 | 0 | mManager->OnXPCOMShutdown(); |
143 | 0 | } |
144 | 0 | return NS_OK; |
145 | 0 | } |
146 | | |
147 | | void |
148 | | VRProcessManager::CleanShutdown() |
149 | 0 | { |
150 | 0 | DestroyProcess(); |
151 | 0 | } |
152 | | |
153 | | void |
154 | | VRProcessManager::OnXPCOMShutdown() |
155 | 0 | { |
156 | 0 | if (mObserver) { |
157 | 0 | nsContentUtils::UnregisterShutdownObserver(mObserver); |
158 | 0 | mObserver = nullptr; |
159 | 0 | } |
160 | 0 |
|
161 | 0 | CleanShutdown(); |
162 | 0 | } |
163 | | |
164 | | VRChild* |
165 | | VRProcessManager::GetVRChild() |
166 | 0 | { |
167 | 0 | return mProcess->GetActor(); |
168 | 0 | } |
169 | | |
170 | | } // namespace gfx |
171 | | } // namespace mozilla |