| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" | 5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | |
| 10 #include "chromeos/accelerometer/accelerometer_reader.h" | 9 #include "chromeos/accelerometer/accelerometer_reader.h" |
| 11 #include "chromeos/accelerometer/accelerometer_types.h" | 10 #include "chromeos/accelerometer/accelerometer_types.h" |
| 11 #include "content/browser/device_sensors/inertial_sensor_consts.h" |
| 12 #include "ui/gfx/geometry/vector3d_f.h" | 12 #include "ui/gfx/geometry/vector3d_f.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 // Conversion ratio from radians to degrees. | 15 // Conversion ratio from radians to degrees. |
| 16 const double kRad2deg = 180.0 / M_PI; | 16 const double kRad2deg = 180.0 / M_PI; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 SensorManagerChromeOS::SensorManagerChromeOS() : orientation_buffer_(nullptr) { | 21 SensorManagerChromeOS::SensorManagerChromeOS() |
| 22 : motion_buffer_(nullptr), orientation_buffer_(nullptr) { |
| 22 } | 23 } |
| 23 | 24 |
| 24 SensorManagerChromeOS::~SensorManagerChromeOS() { | 25 SensorManagerChromeOS::~SensorManagerChromeOS() { |
| 25 } | 26 } |
| 26 | 27 |
| 28 bool SensorManagerChromeOS::StartFetchingDeviceMotionData( |
| 29 DeviceMotionHardwareBuffer* buffer) { |
| 30 DCHECK(buffer); |
| 31 if (motion_buffer_) |
| 32 return false; |
| 33 motion_buffer_ = buffer; |
| 34 |
| 35 // TODO(jonross): this is currently the rate at which events are pumped, not |
| 36 // at which new accelerometer updates are received. The accelerometer will |
| 37 // be updated (crbug.com/459218) |
| 38 motion_buffer_->seqlock.WriteBegin(); |
| 39 motion_buffer_->data.interval = kInertialSensorIntervalMicroseconds / 1000; |
| 40 motion_buffer_->seqlock.WriteEnd(); |
| 41 |
| 42 StartObservingAccelerometer(); |
| 43 return true; |
| 44 } |
| 45 |
| 46 bool SensorManagerChromeOS::StopFetchingDeviceMotionData() { |
| 47 if (!motion_buffer_) |
| 48 return false; |
| 49 // Make sure to indicate that the sensor data is no longer available. |
| 50 motion_buffer_->seqlock.WriteBegin(); |
| 51 motion_buffer_->data.allAvailableSensorsAreActive = false; |
| 52 motion_buffer_->seqlock.WriteEnd(); |
| 53 |
| 54 motion_buffer_ = nullptr; |
| 55 |
| 56 StopObservingAccelerometer(); |
| 57 return true; |
| 58 } |
| 59 |
| 27 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | 60 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( |
| 28 DeviceOrientationHardwareBuffer* buffer) { | 61 DeviceOrientationHardwareBuffer* buffer) { |
| 29 DCHECK(buffer); | 62 DCHECK(buffer); |
| 30 { | 63 if (orientation_buffer_) |
| 31 base::AutoLock autolock(orientation_buffer_lock_); | 64 return false; |
| 32 if (orientation_buffer_) | 65 orientation_buffer_ = buffer; |
| 33 return false; | |
| 34 orientation_buffer_ = buffer; | |
| 35 | 66 |
| 36 // No compass information, so we cannot provide absolute orientation. | 67 // No compass information, so we cannot provide absolute orientation. |
| 37 orientation_buffer_->seqlock.WriteBegin(); | 68 orientation_buffer_->seqlock.WriteBegin(); |
| 38 orientation_buffer_->data.absolute = false; | 69 orientation_buffer_->data.absolute = false; |
| 39 orientation_buffer_->data.hasAbsolute = true; | 70 orientation_buffer_->data.hasAbsolute = true; |
| 40 orientation_buffer_->seqlock.WriteEnd(); | 71 orientation_buffer_->seqlock.WriteEnd(); |
| 41 } | |
| 42 | 72 |
| 43 StartObservingAccelerometer(); | 73 StartObservingAccelerometer(); |
| 44 return true; | 74 return true; |
| 45 } | 75 } |
| 46 | 76 |
| 47 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | 77 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
| 48 { | 78 if (!orientation_buffer_) |
| 49 base::AutoLock autolock(orientation_buffer_lock_); | 79 return false; |
| 50 if (!orientation_buffer_) | 80 // Make sure to indicate that the sensor data is no longer available. |
| 51 return false; | 81 orientation_buffer_->seqlock.WriteBegin(); |
| 52 // Make sure to indicate that the sensor data is no longer available. | 82 orientation_buffer_->data.allAvailableSensorsAreActive = false; |
| 53 orientation_buffer_->seqlock.WriteBegin(); | 83 orientation_buffer_->seqlock.WriteEnd(); |
| 54 orientation_buffer_->data.allAvailableSensorsAreActive = false; | 84 orientation_buffer_ = nullptr; |
| 55 orientation_buffer_->seqlock.WriteEnd(); | |
| 56 orientation_buffer_ = nullptr; | |
| 57 } | |
| 58 | 85 |
| 59 StopObservingAccelerometer(); | 86 StopObservingAccelerometer(); |
| 60 return true; | 87 return true; |
| 61 } | 88 } |
| 62 | 89 |
| 63 void SensorManagerChromeOS::OnAccelerometerUpdated( | 90 void SensorManagerChromeOS::OnAccelerometerUpdated( |
| 64 const chromeos::AccelerometerUpdate& update) { | 91 const chromeos::AccelerometerUpdate& update) { |
| 65 base::AutoLock autolock(orientation_buffer_lock_); | 92 chromeos::AccelerometerSource source; |
| 66 if (!orientation_buffer_) | 93 if (update.has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) |
| 94 source = chromeos::ACCELEROMETER_SOURCE_SCREEN; |
| 95 else if (update.has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) |
| 96 source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; |
| 97 else |
| 67 return; | 98 return; |
| 68 | 99 |
| 69 chromeos::AccelerometerSource source; | |
| 70 if (update.has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) { | |
| 71 source = chromeos::ACCELEROMETER_SOURCE_SCREEN; | |
| 72 } else if (update.has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) { | |
| 73 source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
| 74 } else { | |
| 75 return; | |
| 76 } | |
| 77 | |
| 78 double x = update.get(source).x; | 100 double x = update.get(source).x; |
| 79 double y = update.get(source).y; | 101 double y = update.get(source).y; |
| 80 double z = update.get(source).z; | 102 double z = update.get(source).z; |
| 81 | 103 |
| 104 GenerateMotionEvent(x, y, z); |
| 105 GenerateOrientationEvent(x, y, z); |
| 106 } |
| 107 |
| 108 void SensorManagerChromeOS::StartObservingAccelerometer() { |
| 109 if (chromeos::AccelerometerReader::GetInstance()->HasObserver(this)) |
| 110 return; |
| 111 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); |
| 112 } |
| 113 |
| 114 void SensorManagerChromeOS::StopObservingAccelerometer() { |
| 115 if (orientation_buffer_ || motion_buffer_) |
| 116 return; |
| 117 DCHECK(chromeos::AccelerometerReader::GetInstance()->HasObserver(this)); |
| 118 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); |
| 119 } |
| 120 |
| 121 void SensorManagerChromeOS::GenerateMotionEvent(double x, double y, double z) { |
| 122 if (!motion_buffer_) |
| 123 return; |
| 124 |
| 125 motion_buffer_->seqlock.WriteBegin(); |
| 126 motion_buffer_->data.accelerationIncludingGravityX = x; |
| 127 motion_buffer_->data.hasAccelerationIncludingGravityX = true; |
| 128 motion_buffer_->data.accelerationIncludingGravityY = y; |
| 129 motion_buffer_->data.hasAccelerationIncludingGravityY = true; |
| 130 motion_buffer_->data.accelerationIncludingGravityZ = z; |
| 131 motion_buffer_->data.hasAccelerationIncludingGravityZ = true; |
| 132 motion_buffer_->data.allAvailableSensorsAreActive = true; |
| 133 motion_buffer_->seqlock.WriteEnd(); |
| 134 } |
| 135 |
| 136 void SensorManagerChromeOS::GenerateOrientationEvent(double x, |
| 137 double y, |
| 138 double z) { |
| 139 if (!orientation_buffer_) |
| 140 return; |
| 141 |
| 82 // Create a unit vector for trigonometry | 142 // Create a unit vector for trigonometry |
| 83 // TODO(jonross): Stop reversing signs for vector components once | 143 // TODO(jonross): Stop reversing signs for vector components once |
| 84 // accelerometer values have been fixed. crbug.com/431391 | 144 // accelerometer values have been fixed. crbug.com/431391 |
| 85 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | 145 // Ternaries are to remove -0.0f which gives incorrect trigonometrical |
| 86 // results. | 146 // results. |
| 87 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | 147 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
| 88 data.Scale(1.0f / data.Length()); | 148 data.Scale(1.0f / data.Length()); |
| 89 | 149 |
| 90 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | 150 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
| 91 // x = sin(gamma) | 151 // x = sin(gamma) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 103 gamma = -90.0f; | 163 gamma = -90.0f; |
| 104 orientation_buffer_->seqlock.WriteBegin(); | 164 orientation_buffer_->seqlock.WriteBegin(); |
| 105 orientation_buffer_->data.beta = beta; | 165 orientation_buffer_->data.beta = beta; |
| 106 orientation_buffer_->data.hasBeta = true; | 166 orientation_buffer_->data.hasBeta = true; |
| 107 orientation_buffer_->data.gamma = gamma; | 167 orientation_buffer_->data.gamma = gamma; |
| 108 orientation_buffer_->data.hasGamma = true; | 168 orientation_buffer_->data.hasGamma = true; |
| 109 orientation_buffer_->data.allAvailableSensorsAreActive = true; | 169 orientation_buffer_->data.allAvailableSensorsAreActive = true; |
| 110 orientation_buffer_->seqlock.WriteEnd(); | 170 orientation_buffer_->seqlock.WriteEnd(); |
| 111 } | 171 } |
| 112 | 172 |
| 113 void SensorManagerChromeOS::StartObservingAccelerometer() { | |
| 114 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
| 115 } | |
| 116 | |
| 117 void SensorManagerChromeOS::StopObservingAccelerometer() { | |
| 118 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
| 119 } | |
| 120 | |
| 121 } // namespace content | 173 } // namespace content |
| OLD | NEW |