VRShMem.h (4123B)
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 #ifndef GFX_VR_VRSHMEM_H 8 #define GFX_VR_VRSHMEM_H 9 10 // This file declares VRShMem, which is used for cross-platform, cross-process 11 // communication between VR components. 12 // A shared memory handle is opened for the struct of type VRExternalShmem, and 13 // different processes write or read members of it to share data. Note that no 14 // process should be reading or writing to the same members, except for the 15 // versioning members used for synchronization. 16 17 #include "moz_external_vr.h" 18 #include "base/process.h" // for base::ProcessHandle 19 #include <functional> 20 21 namespace mozilla { 22 namespace gfx { 23 class VRShMem final { 24 public: 25 VRShMem(volatile VRExternalShmem* aShmem, bool aRequiresMutex); 26 ~VRShMem() = default; 27 28 void CreateShMem(bool aCreateOnSharedMemory); 29 void CreateShMemForAndroid(); 30 void ClearShMem(); 31 void CloseShMem(); 32 33 bool JoinShMem(); 34 void LeaveShMem(); 35 36 void PushBrowserState(VRBrowserState& aBrowserState, bool aNotifyCond); 37 void PullBrowserState(mozilla::gfx::VRBrowserState& aState); 38 39 void PushSystemState(const mozilla::gfx::VRSystemState& aState); 40 void PullSystemState(VRDisplayState& aDisplayState, 41 VRHMDSensorState& aSensorState, 42 std::array<VRControllerState, kVRControllerMaxCount>*, 43 bool& aEnumerationCompleted, 44 const std::function<bool()>& aWaitCondition = nullptr); 45 46 void PushWindowState(VRWindowState& aState); 47 void PullWindowState(VRWindowState& aState); 48 49 void PushTelemetryState(VRTelemetryState& aState); 50 void PullTelemetryState(VRTelemetryState& aState); 51 52 void SendIMEState(uint64_t aWindowID, mozilla::gfx::VRFxEventState aImeState); 53 void SendFullscreenState(uint64_t aWindowID, bool aFullscreen); 54 void SendShutdowmState(uint64_t aWindowID); 55 56 bool HasExternalShmem() const { return mExternalShmem != nullptr; } 57 bool IsSharedExternalShmem() const { return mIsSharedExternalShmem; } 58 volatile VRExternalShmem* GetExternalShmem() const; 59 bool IsDisplayStateShutdown() const; 60 61 private: 62 bool IsCreatedOnSharedMemory() const; 63 void SendEvent(uint64_t aWindowID, mozilla::gfx::VRFxEventType aEventType, 64 mozilla::gfx::VRFxEventState aEventState); 65 66 // mExternalShmem can have one of three sources: 67 // - Allocated outside of this class on the heap and passed in via 68 // constructor, or acquired via GeckoVRManager (for GeckoView scenario). 69 // This is usually for scenarios where there is no VR process for cross- 70 // proc communication, and VRService is receiving the object. 71 // --> mIsSharedExternalShmem == true, IsCreatedOnSharedMemory() == false 72 // --> [Windows 7, Mac, Android, Linux] 73 // - Allocated inside this class on the heap. This is usually for scenarios 74 // where there is no VR process, and VRManager is creating the object. 75 // --> mIsSharedExternalShmem == false, IsCreatedOnSharedMemory() == false 76 // --> [Windows 7, Mac, Linux] 77 // - Allocated inside this class on shared memory. This is usually for 78 // scenarios where there is a VR process and cross-process communication 79 // is necessary 80 // --> mIsSharedExternalShmem == false, IsCreatedOnSharedMemory() == true 81 // --> [Windows 10 with VR process enabled] 82 volatile VRExternalShmem* mExternalShmem = nullptr; 83 // This member is true when mExternalShmem was allocated externally, via 84 // being passed into the constructor or accessed via GeckoVRManager 85 bool mIsSharedExternalShmem; 86 87 #if defined(XP_WIN) 88 bool mRequiresMutex; 89 #endif 90 91 #if defined(XP_MACOSX) 92 int mShmemFD; 93 #elif defined(XP_WIN) 94 base::ProcessHandle mShmemFile; 95 base::ProcessHandle mMutex; 96 #endif 97 98 #if !defined(MOZ_WIDGET_ANDROID) 99 int64_t mBrowserGeneration = 0; 100 #endif 101 }; 102 } // namespace gfx 103 } // namespace mozilla 104 105 #endif