VRServiceTest.h (8186B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_VRServiceTest_h_ 8 #define mozilla_dom_VRServiceTest_h_ 9 10 #include "gfxVR.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "mozilla/dom/TypedArray.h" 13 #include "mozilla/dom/VRServiceTestBinding.h" 14 15 namespace mozilla { 16 namespace gfx { 17 enum class VRDisplayCapabilityFlags : uint16_t; 18 enum class VRPuppet_Command : uint64_t; 19 } // namespace gfx 20 namespace dom { 21 enum class GamepadCapabilityFlags : uint16_t; 22 23 class VRMockDisplay final : public DOMEventTargetHelper { 24 public: 25 NS_DECL_ISUPPORTS_INHERITED 26 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRMockDisplay, DOMEventTargetHelper) 27 28 explicit VRMockDisplay(VRServiceTest* aVRServiceTest); 29 30 void Create(); 31 void Clear(); 32 33 void SetConnected(bool aConnected); 34 bool Connected() const; 35 void SetMounted(bool aMounted); 36 bool Mounted() const; 37 void SetCapPosition(bool aEnabled); 38 bool CapPosition() const; 39 void SetCapOrientation(bool aEnabled); 40 bool CapOrientation() const; 41 void SetCapPresent(bool aEnabled); 42 bool CapPresent() const; 43 void SetCapExternal(bool aEnabled); 44 bool CapExternal() const; 45 void SetCapAngularAcceleration(bool aEnabled); 46 bool CapAngularAcceleration() const; 47 void SetCapLinearAcceleration(bool aEnabled); 48 bool CapLinearAcceleration() const; 49 void SetCapStageParameters(bool aEnabled); 50 bool CapStageParameters() const; 51 void SetCapMountDetection(bool aEnabled); 52 bool CapMountDetection() const; 53 void SetCapPositionEmulated(bool aEnabled); 54 bool CapPositionEmulated() const; 55 void SetEyeFOV(VREye aEye, double aUpDegree, double aRightDegree, 56 double aDownDegree, double aLeftDegree); 57 void SetEyeOffset(VREye aEye, double aOffsetX, double aOffsetY, 58 double aOffsetZ); 59 void SetEyeResolution(uint32_t aRenderWidth, uint32_t aRenderHeight); 60 void SetStageSize(double aWidth, double aHeight); 61 void SetSittingToStandingTransform(const Float32Array& aTransform, 62 ErrorResult& aRv); 63 void SetPose(const Nullable<Float32Array>& aPosition, 64 const Nullable<Float32Array>& aLinearVelocity, 65 const Nullable<Float32Array>& aLinearAcceleration, 66 const Nullable<Float32Array>& aOrientation, 67 const Nullable<Float32Array>& aAngularVelocity, 68 const Nullable<Float32Array>& aAngularAcceleration, 69 ErrorResult& aRv); 70 virtual JSObject* WrapObject(JSContext* aCx, 71 JS::Handle<JSObject*> aGivenProto) override; 72 73 private: 74 ~VRMockDisplay() = default; 75 gfx::VRDisplayState& DisplayState() const; 76 gfx::VRHMDSensorState& SensorState() const; 77 void SetCapFlag(gfx::VRDisplayCapabilityFlags aFlag, bool aEnabled); 78 bool GetCapFlag(gfx::VRDisplayCapabilityFlags aFlag) const; 79 80 RefPtr<VRServiceTest> mVRServiceTest; 81 }; 82 83 class VRMockController : public DOMEventTargetHelper { 84 public: 85 NS_DECL_ISUPPORTS_INHERITED 86 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRMockController, 87 DOMEventTargetHelper) 88 89 VRMockController(VRServiceTest* aVRServiceTest, uint32_t aControllerIdx); 90 void Create(); 91 void Clear(); 92 void SetHand(GamepadHand aHand); 93 GamepadHand Hand() const; 94 void SetCapPosition(bool aEnabled); 95 bool CapPosition() const; 96 void SetCapOrientation(bool aEnabled); 97 bool CapOrientation() const; 98 void SetCapAngularAcceleration(bool aEnabled); 99 bool CapAngularAcceleration() const; 100 void SetCapLinearAcceleration(bool aEnabled); 101 bool CapLinearAcceleration() const; 102 void SetAxisCount(uint32_t aCount); 103 uint32_t AxisCount() const; 104 void SetButtonCount(uint32_t aCount); 105 uint32_t ButtonCount() const; 106 void SetHapticCount(uint32_t aCount); 107 uint32_t HapticCount() const; 108 void SetButtonPressed(uint32_t aButtonIdx, bool aPressed); 109 void SetButtonTouched(uint32_t aButtonIdx, bool aTouched); 110 void SetButtonTrigger(uint32_t aButtonIdx, double aTrigger); 111 void SetAxisValue(uint32_t aAxisIdx, double aValue); 112 void SetPose(const Nullable<Float32Array>& aPosition, 113 const Nullable<Float32Array>& aLinearVelocity, 114 const Nullable<Float32Array>& aLinearAcceleration, 115 const Nullable<Float32Array>& aOrientation, 116 const Nullable<Float32Array>& aAngularVelocity, 117 const Nullable<Float32Array>& aAngularAcceleration, 118 ErrorResult& aRv); 119 120 virtual JSObject* WrapObject(JSContext* aCx, 121 JS::Handle<JSObject*> aGivenProto) override; 122 123 private: 124 ~VRMockController() = default; 125 gfx::VRControllerState& ControllerState() const; 126 void SetCapFlag(GamepadCapabilityFlags aFlag, bool aEnabled); 127 bool GetCapFlag(GamepadCapabilityFlags aFlag) const; 128 RefPtr<VRServiceTest> mVRServiceTest; 129 uint32_t mControllerIdx; 130 }; 131 132 class VRServiceTest final : public DOMEventTargetHelper { 133 public: 134 NS_DECL_ISUPPORTS_INHERITED 135 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRServiceTest, DOMEventTargetHelper) 136 137 // WebIDL interface 138 139 void ClearAll(); 140 void ClearController(uint32_t aControllerIdx); 141 void Commit(); 142 void End(); 143 already_AddRefed<Promise> Run(ErrorResult& aRv); 144 already_AddRefed<Promise> Reset(ErrorResult& aRv); 145 VRMockDisplay* GetVRDisplay(); 146 VRMockController* GetVRController(uint32_t aControllerIdx, ErrorResult& aRv); 147 void Timeout(uint32_t aDuration); 148 void Wait(uint32_t aDuration); 149 void WaitSubmit(); 150 void WaitPresentationStart(); 151 void WaitPresentationEnd(); 152 void WaitHapticIntensity(uint32_t aControllerIdx, uint32_t aHapticIdx, 153 double aIntensity, ErrorResult& aRv); 154 void CaptureFrame(); 155 void AcknowledgeFrame(); 156 void RejectFrame(); 157 void StartTimer(); 158 void StopTimer(); 159 160 // Implementation 161 void Shutdown(); 162 void AddCommand(uint64_t aCommand); 163 static already_AddRefed<VRServiceTest> CreateTestService( 164 nsPIDOMWindowInner* aWindow); 165 virtual JSObject* WrapObject(JSContext* aCx, 166 JS::Handle<JSObject*> aGivenProto) override; 167 gfx::VRSystemState& SystemState(); 168 169 private: 170 explicit VRServiceTest(nsPIDOMWindowInner* aWindow); 171 ~VRServiceTest() = default; 172 void EncodeData(); 173 174 RefPtr<VRMockDisplay> mDisplay; 175 nsTArray<RefPtr<VRMockController>> mControllers; 176 nsCOMPtr<nsPIDOMWindowInner> mWindow; 177 // mPendingState records the state of the emulated VR hardware, including 178 // changes that have not yet been committed to the command buffer. 179 gfx::VRSystemState mPendingState; 180 // mEncodedState records the state of the emulate VR hardware at the end 181 // of the last committed transaction, submitted with VRServiceTest::Commit(). 182 // mPendingState represents the resulting state if all of the commands in 183 // mCommandBuffer have been played back. 184 gfx::VRSystemState mEncodedState; 185 // mCommandBuffer encodes a sequence of steps to be executed asynchronously by 186 // the simulated VR hardware. The steps are encoded as a stream of uint64's, 187 // using the format described in gfx/vr/VRPuppetCommandBuffer.h 188 // mCommandBuffer includes only complete transactions, which will be played 189 // back such that multiple values in VRSystemState will be updated atomically. 190 // When the command buffer is submitted to the PuppetSession, with 191 // VRServiceTest::Run(), it is cleared to ensure that the commands are not 192 // sent redundantly in subsequent VRServicetest::Run() calls. 193 // VRServiceTest::Commit() will perform a binary comparison of mPendingState 194 // and mEncodedState to determine what instructions need to be appended to 195 // mCommandBuffer. 196 // VRServiceTest::Reset() will effectively cancel all transactions and clear 197 // mCommandBuffer before submitting the reset request to the PuppetSession. 198 nsTArray<uint64_t> mCommandBuffer; 199 bool mShuttingDown; 200 }; 201 202 } // namespace dom 203 } // namespace mozilla 204 205 #endif // mozilla_dom_VRServiceTest_h_