Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / gfx / vr / ipc / VRParent.cpp
blob234352ba08358eeb2c8770b49a8d9102f0cf6af3
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/. */
7 #include "VRParent.h"
8 #include "VRGPUParent.h"
9 #include "gfxConfig.h"
10 #include "nsDebugImpl.h"
11 #include "nsThreadManager.h"
12 #include "nsPrintfCString.h"
14 #include "mozilla/dom/MemoryReportRequest.h"
15 #include "mozilla/gfx/gfxVars.h"
16 #include "mozilla/ipc/CrashReporterClient.h"
17 #include "mozilla/ipc/ProcessChild.h"
18 #include "mozilla/ipc/ProcessUtils.h"
19 #include "mozilla/Preferences.h"
21 #if defined(XP_WIN)
22 # include <process.h>
23 # include "mozilla/gfx/DeviceManagerDx.h"
24 #else
25 # include <unistd.h>
26 #endif
28 namespace mozilla {
29 namespace gfx {
31 using mozilla::ipc::IPCResult;
33 VRParent::VRParent() : mVRGPUParent(nullptr) {}
35 IPCResult VRParent::RecvNewGPUVRManager(Endpoint<PVRGPUParent>&& aEndpoint) {
36 RefPtr<VRGPUParent> vrGPUParent =
37 VRGPUParent::CreateForGPU(std::move(aEndpoint));
38 if (!vrGPUParent) {
39 return IPC_FAIL_NO_REASON(this);
42 mVRGPUParent = std::move(vrGPUParent);
43 return IPC_OK();
46 IPCResult VRParent::RecvInit(nsTArray<GfxVarUpdate>&& vars,
47 const DevicePrefs& devicePrefs) {
48 Unused << SendInitComplete();
50 for (const auto& var : vars) {
51 gfxVars::ApplyUpdate(var);
54 // Inherit device preferences.
55 gfxConfig::Inherit(Feature::HW_COMPOSITING, devicePrefs.hwCompositing());
56 gfxConfig::Inherit(Feature::D3D11_COMPOSITING,
57 devicePrefs.d3d11Compositing());
58 gfxConfig::Inherit(Feature::OPENGL_COMPOSITING, devicePrefs.oglCompositing());
59 gfxConfig::Inherit(Feature::DIRECT2D, devicePrefs.useD2D1());
61 #if defined(XP_WIN)
62 if (gfxConfig::IsEnabled(Feature::D3D11_COMPOSITING)) {
63 DeviceManagerDx::Get()->CreateCompositorDevices();
65 #endif
66 return IPC_OK();
69 IPCResult VRParent::RecvUpdateVar(const GfxVarUpdate& aUpdate) {
70 gfxVars::ApplyUpdate(aUpdate);
71 return IPC_OK();
74 mozilla::ipc::IPCResult VRParent::RecvPreferenceUpdate(const Pref& aPref) {
75 Preferences::SetPreference(aPref);
76 return IPC_OK();
79 mozilla::ipc::IPCResult VRParent::RecvOpenVRControllerActionPathToVR(
80 const nsCString& aPath) {
81 mOpenVRControllerAction = aPath;
82 return IPC_OK();
85 mozilla::ipc::IPCResult VRParent::RecvOpenVRControllerManifestPathToVR(
86 const VRControllerType& aType, const nsCString& aPath) {
87 mOpenVRControllerManifest.InsertOrUpdate(static_cast<uint32_t>(aType), aPath);
88 return IPC_OK();
91 mozilla::ipc::IPCResult VRParent::RecvRequestMemoryReport(
92 const uint32_t& aGeneration, const bool& aAnonymize,
93 const bool& aMinimizeMemoryUsage, const Maybe<FileDescriptor>& aDMDFile,
94 const RequestMemoryReportResolver& aResolver) {
95 MOZ_ASSERT(XRE_IsVRProcess());
96 nsPrintfCString processName("VR (pid %u)", (unsigned)getpid());
98 mozilla::dom::MemoryReportRequestClient::Start(
99 aGeneration, aAnonymize, aMinimizeMemoryUsage, aDMDFile, processName,
100 [&](const MemoryReport& aReport) {
101 Unused << SendAddMemoryReport(aReport);
103 aResolver);
104 return IPC_OK();
107 void VRParent::ActorDestroy(ActorDestroyReason aWhy) {
108 if (AbnormalShutdown == aWhy) {
109 NS_WARNING("Shutting down VR process early due to a crash!");
110 ipc::ProcessChild::QuickExit();
112 if (mVRGPUParent && !mVRGPUParent->IsClosed()) {
113 mVRGPUParent->Close();
115 mVRGPUParent = nullptr;
117 #ifndef NS_FREE_PERMANENT_DATA
118 // No point in going through XPCOM shutdown because we don't keep persistent
119 // state.
120 ipc::ProcessChild::QuickExit();
121 #endif
123 #if defined(XP_WIN)
124 DeviceManagerDx::Shutdown();
125 #endif
126 gfxVars::Shutdown();
127 gfxConfig::Shutdown();
128 ipc::CrashReporterClient::DestroySingleton();
129 // Only calling XRE_ShutdownChildProcess() at the child process
130 // instead of the main process. Otherwise, it will close all child processes
131 // that are spawned from the main process.
132 XRE_ShutdownChildProcess();
135 bool VRParent::Init(mozilla::ipc::UntypedEndpoint&& aEndpoint,
136 const char* aParentBuildID) {
137 // Initialize the thread manager before starting IPC. Otherwise, messages
138 // may be posted to the main thread and we won't be able to process them.
139 if (NS_WARN_IF(NS_FAILED(nsThreadManager::get().Init()))) {
140 return false;
143 // Now it's safe to start IPC.
144 if (NS_WARN_IF(!aEndpoint.Bind(this))) {
145 return false;
148 nsDebugImpl::SetMultiprocessMode("VR");
150 // This must be checked before any IPDL message, which may hit sentinel
151 // errors due to parent and content processes having different
152 // versions.
153 MessageChannel* channel = GetIPCChannel();
154 if (channel && !channel->SendBuildIDsMatchMessage(aParentBuildID)) {
155 // We need to quit this process if the buildID doesn't match the parent's.
156 // This can occur when an update occurred in the background.
157 ipc::ProcessChild::QuickExit();
160 // Init crash reporter support.
161 ipc::CrashReporterClient::InitSingleton(this);
163 gfxConfig::Init();
164 gfxVars::Initialize();
165 #if defined(XP_WIN)
166 DeviceManagerDx::Init();
167 #endif
168 if (NS_FAILED(NS_InitMinimalXPCOM())) {
169 return false;
172 mozilla::ipc::SetThisProcessName("VR Process");
173 return true;
176 bool VRParent::GetOpenVRControllerActionPath(nsCString* aPath) {
177 if (!mOpenVRControllerAction.IsEmpty()) {
178 *aPath = mOpenVRControllerAction;
179 return true;
182 return false;
185 bool VRParent::GetOpenVRControllerManifestPath(VRControllerType aType,
186 nsCString* aPath) {
187 return mOpenVRControllerManifest.Get(static_cast<uint32_t>(aType), aPath);
190 } // namespace gfx
191 } // namespace mozilla