author Kearwood "Kip" Gilbert <>
Fri, 13 Jul 2018 17:16:35 -0700
changeset 499389 06020ce3e33a83bc1dd1514aac97beacdab1505f
parent 499377 32976a484a5ee5060962cbe84475f2bfddbece08
child 502277 e16d573e399117c6967b1c6d755711ef9cdc063b
permissions -rw-r--r--
Bug 1466700 - Refactor gfxVROculus.cpp to use gfxVRExternal interface, r=daoshengmu Tags: #secure-revision Bug #: 1466700 Differential Revision:

/* -*- 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 */


#include "nsTArray.h"
#include "nsIScreen.h"
#include "nsCOMPtr.h"
#include "mozilla/RefPtr.h"

#include "mozilla/gfx/2D.h"
#include "mozilla/EnumeratedArray.h"
#include "mozilla/UniquePtr.h"

#include "gfxVR.h"
#include "VRDisplayHost.h"

#if defined(XP_MACOSX)
class MacIOSurface;
namespace mozilla {
namespace gfx {
namespace impl {

class VRDisplayExternal : public VRDisplayHost
  void ZeroSensor() override;

  VRHMDSensorState GetSensorState() override;
  void StartPresentation() override;
  void StopPresentation() override;
  void StartVRNavigation() override;
  void StopVRNavigation(const TimeDuration& aTimeout) override;

  bool SubmitFrame(const layers::SurfaceDescriptor& aTexture,
                   uint64_t aFrameId,
                   const gfx::Rect& aLeftEyeRect,
                   const gfx::Rect& aRightEyeRect) override;

  explicit VRDisplayExternal(const VRDisplayState& aDisplayState);
  void Refresh();
  const VRControllerState& GetLastControllerState(uint32_t aStateIndex) const;
  void VibrateHaptic(uint32_t aControllerIdx,
                     uint32_t aHapticIndex,
                     double aIntensity,
                     double aDuration,
                     const VRManagerPromise& aPromise);
  void StopVibrateHaptic(uint32_t aControllerIdx);
  void StopAllHaptics();
  void Run1msTasks(double aDeltaTime) override;
  void Run10msTasks() override;
  virtual ~VRDisplayExternal();
  void Destroy();

  bool PopulateLayerTexture(const layers::SurfaceDescriptor& aTexture,
                            VRLayerTextureType* aTextureType,
                            VRLayerTextureHandle* aTextureHandle);
  void PushState(bool aNotifyCond = false);
  bool PullState(const std::function<bool()>& aWaitCondition = nullptr);
  bool PullState();
  void ClearHapticSlot(size_t aSlot);
  void ExpireNavigationTransition();
  void UpdateHaptics(double aDeltaTime);
  nsTArray<UniquePtr<VRManagerPromise>> mHapticPromises;
  // Duration of haptic pulse time remaining (milliseconds)
  double mHapticPulseRemaining[kVRHapticsMaxCount];
  VRTelemetry mTelemetry;
  TimeStamp mVRNavigationTransitionEnd;
  VRBrowserState mBrowserState;
  VRHMDSensorState mLastSensorState;

} // namespace impl

class VRSystemManagerExternal : public VRSystemManager
  static already_AddRefed<VRSystemManagerExternal> Create(VRExternalShmem* aAPIShmem = nullptr);

  virtual void Destroy() override;
  virtual void Shutdown() override;
  virtual void Run100msTasks() override;
  virtual void Enumerate() override;
  virtual bool ShouldInhibitEnumeration() override;
  virtual void GetHMDs(nsTArray<RefPtr<VRDisplayHost>>& aHMDResult) override;
  virtual bool GetIsPresenting() override;
  virtual void HandleInput() override;
  virtual void GetControllers(nsTArray<RefPtr<VRControllerHost>>&
                              aControllerResult) override;
  virtual void ScanForControllers() override;
  virtual void RemoveControllers() override;
  virtual void VibrateHaptic(uint32_t aControllerIdx,
                             uint32_t aHapticIndex,
                             double aIntensity,
                             double aDuration,
                             const VRManagerPromise& aPromise) override;
  virtual void StopVibrateHaptic(uint32_t aControllerIdx) override;
  bool PullState(VRDisplayState* aDisplayState,
                 VRHMDSensorState* aSensorState = nullptr,
                 VRControllerState* aControllerState = nullptr,
                 const std::function<bool()>& aWaitCondition = nullptr);
  bool PullState(VRDisplayState* aDisplayState,
                 VRHMDSensorState* aSensorState = nullptr,
                 VRControllerState* aControllerState = nullptr);
  void PushState(VRBrowserState* aBrowserState, const bool aNotifyCond = false);

  explicit VRSystemManagerExternal(VRExternalShmem* aAPIShmem = nullptr);
  virtual ~VRSystemManagerExternal();

  // there can only be one
  RefPtr<impl::VRDisplayExternal> mDisplay;
#if defined(XP_MACOSX)
  int mShmemFD;
#elif defined(XP_WIN)
  base::ProcessHandle mShmemFile;
#elif defined(MOZ_WIDGET_ANDROID)
  bool mExternalStructFailed;
  bool mEnumerationCompleted;
  bool mDoShutdown;

  volatile VRExternalShmem* mExternalShmem;
#if !defined(MOZ_WIDGET_ANDROID)
  bool mSameProcess;
  TimeStamp mEarliestRestartTime;

  void OpenShmem();
  void CloseShmem();
  void CheckForShutdown();

} // namespace gfx
} // namespace mozilla

#endif /* GFX_VR_EXTERNAL_H */