summaryrefslogtreecommitdiffstats
path: root/gfx/vr/ipc/VRParent.cpp
blob: 234352ba08358eeb2c8770b49a8d9102f0cf6af3 (plain)
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
/* -*- 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 "VRParent.h"
#include "VRGPUParent.h"
#include "gfxConfig.h"
#include "nsDebugImpl.h"
#include "nsThreadManager.h"
#include "nsPrintfCString.h"

#include "mozilla/dom/MemoryReportRequest.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/ipc/CrashReporterClient.h"
#include "mozilla/ipc/ProcessChild.h"
#include "mozilla/ipc/ProcessUtils.h"
#include "mozilla/Preferences.h"

#if defined(XP_WIN)
#  include <process.h>
#  include "mozilla/gfx/DeviceManagerDx.h"
#else
#  include <unistd.h>
#endif

namespace mozilla {
namespace gfx {

using mozilla::ipc::IPCResult;

VRParent::VRParent() : mVRGPUParent(nullptr) {}

IPCResult VRParent::RecvNewGPUVRManager(Endpoint<PVRGPUParent>&& aEndpoint) {
  RefPtr<VRGPUParent> vrGPUParent =
      VRGPUParent::CreateForGPU(std::move(aEndpoint));
  if (!vrGPUParent) {
    return IPC_FAIL_NO_REASON(this);
  }

  mVRGPUParent = std::move(vrGPUParent);
  return IPC_OK();
}

IPCResult VRParent::RecvInit(nsTArray<GfxVarUpdate>&& vars,
                             const DevicePrefs& devicePrefs) {
  Unused << SendInitComplete();

  for (const auto& var : vars) {
    gfxVars::ApplyUpdate(var);
  }

  // Inherit device preferences.
  gfxConfig::Inherit(Feature::HW_COMPOSITING, devicePrefs.hwCompositing());
  gfxConfig::Inherit(Feature::D3D11_COMPOSITING,
                     devicePrefs.d3d11Compositing());
  gfxConfig::Inherit(Feature::OPENGL_COMPOSITING, devicePrefs.oglCompositing());
  gfxConfig::Inherit(Feature::DIRECT2D, devicePrefs.useD2D1());

#if defined(XP_WIN)
  if (gfxConfig::IsEnabled(Feature::D3D11_COMPOSITING)) {
    DeviceManagerDx::Get()->CreateCompositorDevices();
  }
#endif
  return IPC_OK();
}

IPCResult VRParent::RecvUpdateVar(const GfxVarUpdate& aUpdate) {
  gfxVars::ApplyUpdate(aUpdate);
  return IPC_OK();
}

mozilla::ipc::IPCResult VRParent::RecvPreferenceUpdate(const Pref& aPref) {
  Preferences::SetPreference(aPref);
  return IPC_OK();
}

mozilla::ipc::IPCResult VRParent::RecvOpenVRControllerActionPathToVR(
    const nsCString& aPath) {
  mOpenVRControllerAction = aPath;
  return IPC_OK();
}

mozilla::ipc::IPCResult VRParent::RecvOpenVRControllerManifestPathToVR(
    const VRControllerType& aType, const nsCString& aPath) {
  mOpenVRControllerManifest.InsertOrUpdate(static_cast<uint32_t>(aType), aPath);
  return IPC_OK();
}

mozilla::ipc::IPCResult VRParent::RecvRequestMemoryReport(
    const uint32_t& aGeneration, const bool& aAnonymize,
    const bool& aMinimizeMemoryUsage, const Maybe<FileDescriptor>& aDMDFile,
    const RequestMemoryReportResolver& aResolver) {
  MOZ_ASSERT(XRE_IsVRProcess());
  nsPrintfCString processName("VR (pid %u)", (unsigned)getpid());

  mozilla::dom::MemoryReportRequestClient::Start(
      aGeneration, aAnonymize, aMinimizeMemoryUsage, aDMDFile, processName,
      [&](const MemoryReport& aReport) {
        Unused << SendAddMemoryReport(aReport);
      },
      aResolver);
  return IPC_OK();
}

void VRParent::ActorDestroy(ActorDestroyReason aWhy) {
  if (AbnormalShutdown == aWhy) {
    NS_WARNING("Shutting down VR process early due to a crash!");
    ipc::ProcessChild::QuickExit();
  }
  if (mVRGPUParent && !mVRGPUParent->IsClosed()) {
    mVRGPUParent->Close();
  }
  mVRGPUParent = nullptr;

#ifndef NS_FREE_PERMANENT_DATA
  // No point in going through XPCOM shutdown because we don't keep persistent
  // state.
  ipc::ProcessChild::QuickExit();
#endif

#if defined(XP_WIN)
  DeviceManagerDx::Shutdown();
#endif
  gfxVars::Shutdown();
  gfxConfig::Shutdown();
  ipc::CrashReporterClient::DestroySingleton();
  // Only calling XRE_ShutdownChildProcess() at the child process
  // instead of the main process. Otherwise, it will close all child processes
  // that are spawned from the main process.
  XRE_ShutdownChildProcess();
}

bool VRParent::Init(mozilla::ipc::UntypedEndpoint&& aEndpoint,
                    const char* aParentBuildID) {
  // Initialize the thread manager before starting IPC. Otherwise, messages
  // may be posted to the main thread and we won't be able to process them.
  if (NS_WARN_IF(NS_FAILED(nsThreadManager::get().Init()))) {
    return false;
  }

  // Now it's safe to start IPC.
  if (NS_WARN_IF(!aEndpoint.Bind(this))) {
    return false;
  }

  nsDebugImpl::SetMultiprocessMode("VR");

  // This must be checked before any IPDL message, which may hit sentinel
  // errors due to parent and content processes having different
  // versions.
  MessageChannel* channel = GetIPCChannel();
  if (channel && !channel->SendBuildIDsMatchMessage(aParentBuildID)) {
    // We need to quit this process if the buildID doesn't match the parent's.
    // This can occur when an update occurred in the background.
    ipc::ProcessChild::QuickExit();
  }

  // Init crash reporter support.
  ipc::CrashReporterClient::InitSingleton(this);

  gfxConfig::Init();
  gfxVars::Initialize();
#if defined(XP_WIN)
  DeviceManagerDx::Init();
#endif
  if (NS_FAILED(NS_InitMinimalXPCOM())) {
    return false;
  }

  mozilla::ipc::SetThisProcessName("VR Process");
  return true;
}

bool VRParent::GetOpenVRControllerActionPath(nsCString* aPath) {
  if (!mOpenVRControllerAction.IsEmpty()) {
    *aPath = mOpenVRControllerAction;
    return true;
  }

  return false;
}

bool VRParent::GetOpenVRControllerManifestPath(VRControllerType aType,
                                               nsCString* aPath) {
  return mOpenVRControllerManifest.Get(static_cast<uint32_t>(aType), aPath);
}

}  // namespace gfx
}  // namespace mozilla