VRGPUChild.cpp (2407B)
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 "VRGPUChild.h" 8 #include "VRServiceHost.h" 9 10 #include "mozilla/ipc/Endpoint.h" 11 #include "mozilla/layers/CompositorThread.h" 12 #include "mozilla/StaticPrefs_dom.h" 13 #include "VRManager.h" 14 15 namespace mozilla { 16 namespace gfx { 17 18 static StaticRefPtr<VRGPUChild> sVRGPUChildSingleton; 19 20 /* static */ 21 bool VRGPUChild::InitForGPUProcess(Endpoint<PVRGPUChild>&& aEndpoint) { 22 MOZ_ASSERT(NS_IsMainThread()); 23 MOZ_ASSERT(!sVRGPUChildSingleton); 24 25 if (!StaticPrefs::dom_vr_enabled() && !StaticPrefs::dom_vr_webxr_enabled()) { 26 return false; 27 } 28 29 RefPtr<VRGPUChild> child(new VRGPUChild()); 30 if (!aEndpoint.Bind(child)) { 31 return false; 32 } 33 sVRGPUChildSingleton = child; 34 35 #if !defined(MOZ_WIDGET_ANDROID) 36 RefPtr<Runnable> task = NS_NewRunnableFunction( 37 "VRServiceHost::NotifyVRProcessStarted", []() -> void { 38 VRServiceHost* host = VRServiceHost::Get(); 39 host->NotifyVRProcessStarted(); 40 }); 41 42 NS_DispatchToMainThread(task.forget()); 43 #endif 44 45 return true; 46 } 47 48 /* static */ 49 bool VRGPUChild::IsCreated() { return !!sVRGPUChildSingleton; } 50 51 /* static */ 52 VRGPUChild* VRGPUChild::Get() { 53 MOZ_ASSERT(IsCreated(), "VRGPUChild haven't initialized yet."); 54 return sVRGPUChildSingleton; 55 } 56 57 /*static*/ 58 void VRGPUChild::Shutdown() { 59 MOZ_ASSERT(NS_IsMainThread()); 60 if (sVRGPUChildSingleton && !sVRGPUChildSingleton->IsClosed()) { 61 sVRGPUChildSingleton->Close(); 62 } 63 sVRGPUChildSingleton = nullptr; 64 } 65 66 void VRGPUChild::ActorDestroy(ActorDestroyReason aWhy) { 67 VRManager* vm = VRManager::Get(); 68 mozilla::layers::CompositorThread()->Dispatch( 69 NewRunnableMethod("VRGPUChild::ActorDestroy", vm, &VRManager::Shutdown)); 70 71 mClosed = true; 72 } 73 74 mozilla::ipc::IPCResult VRGPUChild::RecvNotifyPuppetComplete() { 75 #if !defined(MOZ_WIDGET_ANDROID) 76 VRManager* vm = VRManager::Get(); 77 mozilla::layers::CompositorThread()->Dispatch(NewRunnableMethod( 78 "VRManager::NotifyPuppetComplete", vm, &VRManager::NotifyPuppetComplete)); 79 #endif 80 return IPC_OK(); 81 } 82 83 bool VRGPUChild::IsClosed() { return mClosed; } 84 85 } // namespace gfx 86 } // namespace mozilla