tor-browser

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

gfxVR.h (5071B)


      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_H
      8 #define GFX_VR_H
      9 
     10 #include "moz_external_vr.h"
     11 #include "nsTArray.h"
     12 #include "nsString.h"
     13 #include "nsCOMPtr.h"
     14 #include "mozilla/gfx/2D.h"
     15 #include "mozilla/TiedFields.h"
     16 #include "mozilla/TimeStamp.h"
     17 #include <type_traits>
     18 
     19 namespace mozilla {
     20 namespace layers {
     21 class PTextureParent;
     22 }
     23 namespace dom {
     24 enum class GamepadMappingType : uint8_t;
     25 enum class GamepadHand : uint8_t;
     26 }  // namespace dom
     27 namespace gfx {
     28 enum class VRAPIMode : uint8_t { WebXR, WebVR, NumVRAPIModes };
     29 
     30 class VRLayerParent;
     31 class VRDisplayHost;
     32 class VRManagerPromise;
     33 
     34 // The maximum number of frames of latency that we would expect before we
     35 // should give up applying pose prediction.
     36 // If latency is greater than one second, then the experience is not likely
     37 // to be corrected by pose prediction.  Setting this value too
     38 // high may result in unnecessary memory allocation.
     39 // As the current fastest refresh rate is 90hz, 100 is selected as a
     40 // conservative value.
     41 static const int kVRMaxLatencyFrames = 100;
     42 
     43 struct VRDisplayInfo {
     44  uint32_t mDisplayID;
     45  uint32_t mPresentingGroups;
     46  uint32_t mGroupMask;
     47  uint32_t _padding;
     48  uint64_t mFrameId;
     49  VRDisplayState mDisplayState;
     50  std::array<VRControllerState, kVRControllerMaxCount> mControllerState;
     51  std::array<VRHMDSensorState, kVRMaxLatencyFrames> mLastSensorState;
     52 
     53  // -
     54 
     55  auto MutTiedFields() {
     56    return std::tie(mDisplayID, mPresentingGroups, mGroupMask, _padding,
     57                    mFrameId, mDisplayState, mControllerState,
     58                    mLastSensorState);
     59  }
     60 
     61  // -
     62 
     63  void Clear() { memset(this, 0, sizeof(VRDisplayInfo)); }
     64  const VRHMDSensorState& GetSensorState() const {
     65    return mLastSensorState[mFrameId % kVRMaxLatencyFrames];
     66  }
     67 
     68  uint32_t GetDisplayID() const { return mDisplayID; }
     69  const char* GetDisplayName() const {
     70    return mDisplayState.displayName.data();
     71  }
     72  VRDisplayCapabilityFlags GetCapabilities() const {
     73    return mDisplayState.capabilityFlags;
     74  }
     75 
     76  const IntSize SuggestedEyeResolution() const;
     77  const Point3D GetEyeTranslation(uint32_t whichEye) const;
     78  const VRFieldOfView& GetEyeFOV(uint32_t whichEye) const {
     79    return mDisplayState.eyeFOV[whichEye];
     80  }
     81  bool GetIsConnected() const { return mDisplayState.isConnected; }
     82  bool GetIsMounted() const { return mDisplayState.isMounted; }
     83  uint32_t GetPresentingGroups() const { return mPresentingGroups; }
     84  uint32_t GetGroupMask() const { return mGroupMask; }
     85  const Size GetStageSize() const;
     86  const Matrix4x4 GetSittingToStandingTransform() const;
     87  uint64_t GetFrameId() const { return mFrameId; }
     88 
     89  bool operator==(const VRDisplayInfo& other) const {
     90    return TiedFields(*this) == TiedFields(other);
     91  }
     92 
     93  bool operator!=(const VRDisplayInfo& other) const {
     94    return !(*this == other);
     95  }
     96 };
     97 
     98 static_assert(std::is_trivial_v<VRDisplayInfo>,
     99              "VRDisplayInfo must be a trivial type.");
    100 
    101 struct VRSubmitFrameResultInfo {
    102  VRSubmitFrameResultInfo()
    103      : mFormat(SurfaceFormat::UNKNOWN), mFrameNum(0), mWidth(0), mHeight(0) {}
    104 
    105  nsCString mBase64Image;
    106  SurfaceFormat mFormat;
    107  uint64_t mFrameNum;
    108  uint32_t mWidth;
    109  uint32_t mHeight;
    110 };
    111 
    112 struct VRControllerInfo {
    113  uint32_t GetControllerID() const { return mControllerID; }
    114  const char* GetControllerName() const {
    115    return mControllerState.controllerName.data();
    116  }
    117  dom::GamepadMappingType GetMappingType() const { return mMappingType; }
    118  uint32_t GetDisplayID() const { return mDisplayID; }
    119  dom::GamepadHand GetHand() const { return mControllerState.hand; }
    120  uint32_t GetNumButtons() const { return mControllerState.numButtons; }
    121  uint32_t GetNumAxes() const { return mControllerState.numAxes; }
    122  uint32_t GetNumHaptics() const { return mControllerState.numHaptics; }
    123 
    124  uint32_t mControllerID;
    125  dom::GamepadMappingType mMappingType;
    126  uint32_t mDisplayID;
    127  VRControllerState mControllerState;
    128  bool operator==(const VRControllerInfo& other) const {
    129    // Note that mControllerState is asserted to be a POD type, so memcmp is
    130    // safe
    131    return mControllerID == other.mControllerID &&
    132           memcmp(&mControllerState, &other.mControllerState,
    133                  sizeof(VRControllerState)) == 0 &&
    134           mMappingType == other.mMappingType && mDisplayID == other.mDisplayID;
    135  }
    136 
    137  bool operator!=(const VRControllerInfo& other) const {
    138    return !(*this == other);
    139  }
    140 };
    141 
    142 struct VRTelemetry {
    143  VRTelemetry() : mLastDroppedFrameCount(-1) {}
    144 
    145  void Clear() {
    146    mPresentationStart = TimeStamp();
    147    mLastDroppedFrameCount = -1;
    148  }
    149 
    150  bool IsLastDroppedFrameValid() { return (mLastDroppedFrameCount != -1); }
    151 
    152  TimeStamp mPresentationStart;
    153  int32_t mLastDroppedFrameCount;
    154 };
    155 
    156 }  // namespace gfx
    157 }  // namespace mozilla
    158 
    159 #endif /* GFX_VR_H */