1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef DEVICE_VR_ORIENTATION_DEVICE_H 6 #define DEVICE_VR_ORIENTATION_DEVICE_H 7 8 #include <memory> 9 10 #include "base/callback_forward.h" 11 #include "base/macros.h" 12 #include "base/threading/simple_thread.h" 13 #include "build/build_config.h" 14 #include "device/vr/public/mojom/vr_service.mojom.h" 15 #include "device/vr/vr_device_base.h" 16 #include "mojo/public/cpp/bindings/receiver.h" 17 #include "mojo/public/cpp/bindings/remote.h" 18 #include "services/device/public/mojom/sensor_provider.mojom.h" 19 #include "ui/gfx/geometry/quaternion.h" 20 21 namespace device { 22 23 class SensorReadingSharedBufferReader; 24 class VROrientationSession; 25 26 // Use RELATIVE_ORIENTATION_QUATERNION rather than 27 // ABSOLUTE_ORIENTATION_QUATERNION because compass readings can be inacurate 28 // when used indoors, unless we're on Windows which doesn't support 29 // RELATIVE_ORIENTATION_QUATERNION. 30 // TODO(crbug.com/730440) If RELATIVE_ORIENTATION_QUATERNION is ever 31 // implemented on Windows, use that instead. 32 static constexpr mojom::SensorType kOrientationSensorType = 33 #if defined(OS_WIN) 34 mojom::SensorType::ABSOLUTE_ORIENTATION_QUATERNION; 35 #else 36 mojom::SensorType::RELATIVE_ORIENTATION_QUATERNION; 37 #endif 38 39 // This class connects the orientation sensor events to the Web VR apis. 40 class DEVICE_VR_EXPORT VROrientationDevice : public VRDeviceBase, 41 public mojom::SensorClient { 42 public: 43 VROrientationDevice(mojom::SensorProvider* sensor_provider, 44 base::OnceClosure ready_callback); 45 ~VROrientationDevice() override; 46 47 // VRDevice 48 void RequestSession( 49 mojom::XRRuntimeSessionOptionsPtr options, 50 mojom::XRRuntime::RequestSessionCallback callback) override; 51 52 // Indicates whether the device was able to connect to orientation events. IsAvailable()53 bool IsAvailable() const { return available_; } 54 55 void EndMagicWindowSession(VROrientationSession* session); 56 virtual void GetInlineFrameData( 57 mojom::XRFrameDataProvider::GetFrameDataCallback callback); 58 59 private: 60 // SensorClient Functions. 61 void RaiseError() override; SensorReadingChanged()62 void SensorReadingChanged() override {} 63 64 // Sensor event reaction functions. 65 void SensorReady(device::mojom::SensorCreationResult result, 66 device::mojom::SensorInitParamsPtr params); 67 void HandleSensorError(); 68 void OnSensorAddConfiguration(bool success); 69 70 gfx::Quaternion SensorSpaceToWorldSpace(gfx::Quaternion q); 71 gfx::Quaternion WorldSpaceToUserOrientedSpace(gfx::Quaternion q); 72 73 bool available_ = false; 74 base::OnceClosure ready_callback_; 75 76 // The initial state of the world used to define forwards. 77 base::Optional<gfx::Quaternion> base_pose_; 78 gfx::Quaternion latest_pose_; 79 80 mojo::Remote<mojom::Sensor> sensor_; 81 std::unique_ptr<SensorReadingSharedBufferReader> shared_buffer_reader_; 82 mojo::Receiver<mojom::SensorClient> receiver_{this}; 83 84 std::vector<std::unique_ptr<VROrientationSession>> magic_window_sessions_; 85 }; 86 87 } // namespace device 88 89 #endif // DEVICE_VR_ORIENTATION_DEVICE_H 90