tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

VRManager.h (6702B)


      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_MANAGER_H
      8 #define GFX_VR_MANAGER_H
      9 
     10 #include "nsIObserver.h"
     11 #include "nsTArray.h"
     12 #include "nsTHashSet.h"
     13 #include "nsThreadUtils.h"
     14 #include "mozilla/Atomics.h"
     15 #include "mozilla/dom/GamepadHandle.h"
     16 #include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
     17 #include "mozilla/Monitor.h"
     18 #include "mozilla/TimeStamp.h"
     19 #include "gfxVR.h"
     20 
     21 class nsITimer;
     22 namespace mozilla {
     23 namespace gfx {
     24 class VRLayerParent;
     25 class VRManagerParent;
     26 class VRServiceHost;
     27 class VRThread;
     28 class VRShMem;
     29 
     30 enum class VRManagerState : uint32_t {
     31  Disabled,  // All VRManager activity is stopped
     32  Idle,  // No VR hardware has been activated, but background tasks are running
     33  RuntimeDetection,  // Waiting for detection of runtimes without starting up VR
     34                     // hardware
     35  Enumeration,       // Waiting for enumeration and startup of VR hardware
     36  Active,            // VR hardware is active
     37  Stopping,          // Waiting for the VRService to stop
     38 };
     39 
     40 class VRManager : nsIObserver {
     41 public:
     42  NS_DECL_THREADSAFE_ISUPPORTS
     43  NS_DECL_NSIOBSERVER
     44 
     45  static void ManagerInit();
     46  static VRManager* Get();
     47  static VRManager* MaybeGet();
     48 
     49  void AddVRManagerParent(VRManagerParent* aVRManagerParent);
     50  void RemoveVRManagerParent(VRManagerParent* aVRManagerParent);
     51 
     52  void NotifyVsync(const TimeStamp& aVsyncTimestamp);
     53 
     54  void DetectRuntimes();
     55  void EnumerateDevices();
     56  void StopAllHaptics();
     57 
     58  void VibrateHaptic(mozilla::dom::GamepadHandle aGamepadHandle,
     59                     uint32_t aHapticIndex, double aIntensity, double aDuration,
     60                     const VRManagerPromise& aPromise);
     61  void StopVibrateHaptic(mozilla::dom::GamepadHandle aGamepadHandle);
     62  void NotifyVibrateHapticCompleted(const VRManagerPromise& aPromise);
     63  void StartVRNavigation(const uint32_t& aDisplayID);
     64  void StopVRNavigation(const uint32_t& aDisplayID,
     65                        const TimeDuration& aTimeout);
     66  void Shutdown();
     67  void ShutdownVRManagerParents();
     68 #if !defined(MOZ_WIDGET_ANDROID)
     69  bool RunPuppet(const nsTArray<uint64_t>& aBuffer,
     70                 VRManagerParent* aManagerParent);
     71  void ResetPuppet(VRManagerParent* aManagerParent);
     72  void NotifyPuppetComplete();
     73 #endif
     74  void AddLayer(VRLayerParent* aLayer);
     75  void RemoveLayer(VRLayerParent* aLayer);
     76  void SetGroupMask(uint32_t aGroupMask);
     77  void SubmitFrame(VRLayerParent* aLayer,
     78                   const layers::SurfaceDescriptor& aTexture, uint64_t aFrameId,
     79                   const gfx::Rect& aLeftEyeRect,
     80                   const gfx::Rect& aRightEyeRect);
     81  bool IsPresenting();
     82 
     83 private:
     84  VRManager();
     85  virtual ~VRManager();
     86  void Destroy();
     87  void StartTasks();
     88  void StopTasks();
     89  static void TaskTimerCallback(nsITimer* aTimer, void* aClosure);
     90  void RunTasks();
     91  void Run1msTasks(double aDeltaTime);
     92  void Run10msTasks();
     93  void Run100msTasks();
     94  uint32_t GetOptimalTaskInterval();
     95  void ProcessManagerState();
     96  void ProcessManagerState_Disabled();
     97  void ProcessManagerState_Idle();
     98  void ProcessManagerState_Idle_StartRuntimeDetection();
     99  void ProcessManagerState_Idle_StartEnumeration();
    100  void ProcessManagerState_DetectRuntimes();
    101  void ProcessManagerState_Enumeration();
    102  void ProcessManagerState_Active();
    103  void ProcessManagerState_Stopping();
    104  void PullState(const std::function<bool()>& aWaitCondition = nullptr);
    105  void PushState(const bool aNotifyCond = false);
    106  static uint32_t AllocateDisplayID();
    107  void DispatchVRDisplayInfoUpdate();
    108  void DispatchRuntimeCapabilitiesUpdate();
    109  void UpdateRequestedDevices();
    110  void CheckForInactiveTimeout();
    111 #if !defined(MOZ_WIDGET_ANDROID)
    112  void CheckForPuppetCompletion();
    113 #endif
    114  void CheckForShutdown();
    115  void CheckWatchDog();
    116  void ExpireNavigationTransition();
    117  void OpenShmem();
    118  void CloseShmem();
    119  void UpdateHaptics(double aDeltaTime);
    120  void ClearHapticSlot(size_t aSlot);
    121  void StartFrame();
    122  void ShutdownSubmitThread();
    123  void StartPresentation();
    124  void StopPresentation();
    125  void CancelCurrentSubmitTask();
    126 
    127  void SubmitFrameInternal(const layers::SurfaceDescriptor& aTexture,
    128                           uint64_t aFrameId, const gfx::Rect& aLeftEyeRect,
    129                           const gfx::Rect& aRightEyeRect);
    130  bool SubmitFrame(const layers::SurfaceDescriptor& aTexture, uint64_t aFrameId,
    131                   const gfx::Rect& aLeftEyeRect,
    132                   const gfx::Rect& aRightEyeRect);
    133 
    134  Atomic<VRManagerState> mState;
    135  typedef nsTHashSet<RefPtr<VRManagerParent>> VRManagerParentSet;
    136  VRManagerParentSet mVRManagerParents;
    137 #if !defined(MOZ_WIDGET_ANDROID)
    138  VRManagerParentSet mManagerParentsWaitingForPuppetReset;
    139  RefPtr<VRManagerParent> mManagerParentRunningPuppet;
    140 #endif
    141  // Weak reference to mLayers entries are cleared in
    142  // VRLayerParent destructor
    143  nsTArray<VRLayerParent*> mLayers;
    144 
    145  TimeStamp mLastDisplayEnumerationTime;
    146  TimeStamp mLastActiveTime;
    147  TimeStamp mLastTickTime;
    148  TimeStamp mEarliestRestartTime;
    149  TimeStamp mVRNavigationTransitionEnd;
    150  TimeStamp mLastFrameStart[kVRMaxLatencyFrames];
    151  double mAccumulator100ms;
    152  bool mRuntimeDetectionRequested;
    153  bool mRuntimeDetectionCompleted;
    154  bool mEnumerationRequested;
    155  bool mEnumerationCompleted;
    156  bool mVRDisplaysRequested;
    157  bool mVRDisplaysRequestedNonFocus;
    158  bool mVRControllersRequested;
    159  bool mFrameStarted;
    160  uint32_t mTaskInterval;
    161  RefPtr<nsITimer> mTaskTimer;
    162  mozilla::Monitor mCurrentSubmitTaskMonitor MOZ_UNANNOTATED;
    163  RefPtr<CancelableRunnable> mCurrentSubmitTask;
    164  uint64_t mLastSubmittedFrameId;
    165  uint64_t mLastStartedFrame;
    166  VRDisplayCapabilityFlags mRuntimeSupportFlags;
    167  bool mAppPaused;
    168 
    169  // Note: mShmem doesn't support RefPtr; thus, do not share this private
    170  // pointer so that its lifetime can still be controlled by VRManager
    171  VRShMem* mShmem;
    172  bool mVRProcessEnabled;
    173 
    174 #if !defined(MOZ_WIDGET_ANDROID)
    175  RefPtr<VRServiceHost> mServiceHost;
    176 #endif
    177 
    178  static Atomic<uint32_t> sDisplayBase;
    179  RefPtr<VRThread> mSubmitThread;
    180  VRTelemetry mTelemetry;
    181  nsTArray<UniquePtr<VRManagerPromise>> mHapticPromises;
    182  // Duration of haptic pulse time remaining (milliseconds)
    183  double mHapticPulseRemaining[kVRHapticsMaxCount];
    184 
    185  VRDisplayInfo mDisplayInfo;
    186  VRDisplayInfo mLastUpdateDisplayInfo;
    187  VRBrowserState mBrowserState;
    188  VRHMDSensorState mLastSensorState;
    189 };
    190 
    191 }  // namespace gfx
    192 }  // namespace mozilla
    193 
    194 #endif  // GFX_VR_MANAGER_H