Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
timvolodine
2015/01/26 15:29:07
nit: 2015?
jonross
2015/01/26 16:40:04
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "chromeos/accelerometer/accelerometer_reader.h" | |
| 9 #include "ui/gfx/geometry/vector3d_f.h" | |
| 10 | |
| 11 namespace { | |
| 12 // Conversion ratio from radians to degrees. | |
| 13 const double kRad2deg = 180.0 / M_PI; | |
| 14 } | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 SensorManagerChromeOS::SensorManagerChromeOS() : orientation_buffer_(nullptr) { | |
| 19 } | |
| 20 | |
| 21 SensorManagerChromeOS::~SensorManagerChromeOS() { | |
| 22 } | |
| 23 | |
| 24 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | |
| 25 DeviceOrientationHardwareBuffer* buffer) { | |
| 26 DCHECK(buffer); | |
|
timvolodine
2015/01/26 15:29:07
what happens if StartFetchingDeviceOrientationData
jonross
2015/01/26 16:40:04
Done.
| |
| 27 { | |
| 28 base::AutoLock autolock(orientation_buffer_lock_); | |
| 29 orientation_buffer_ = buffer; | |
| 30 | |
| 31 // No compass information, so we cannot provide absolute orientation. | |
| 32 orientation_buffer_->seqlock.WriteBegin(); | |
| 33 orientation_buffer_->data.absolute = false; | |
| 34 orientation_buffer_->data.hasAbsolute = true; | |
| 35 orientation_buffer_->seqlock.WriteEnd(); | |
| 36 } | |
| 37 | |
| 38 StartObservingAccelerometer(); | |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | |
| 43 base::AutoLock autolock(orientation_buffer_lock_); | |
|
timvolodine
2015/01/26 15:29:07
nit: do you need a lock to hold over the whole bod
jonross
2015/01/26 16:40:04
Done.
| |
| 44 if (!orientation_buffer_) | |
| 45 return false; | |
| 46 // Make sure to indicate that the sensor data is no longer available. | |
| 47 orientation_buffer_->seqlock.WriteBegin(); | |
| 48 orientation_buffer_->data.allAvailableSensorsAreActive = false; | |
| 49 orientation_buffer_->seqlock.WriteEnd(); | |
| 50 orientation_buffer_ = nullptr; | |
| 51 | |
| 52 StopObservingAccelerometer(); | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 void SensorManagerChromeOS::OnAccelerometerUpdated( | |
| 57 const ui::AccelerometerUpdate& update) { | |
| 58 base::AutoLock autolock(orientation_buffer_lock_); | |
| 59 if (!orientation_buffer_) | |
| 60 return; | |
| 61 | |
| 62 ui::AccelerometerSource source; | |
| 63 if (update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) { | |
| 64 source = ui::ACCELEROMETER_SOURCE_SCREEN; | |
| 65 } else if (update.has(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) { | |
| 66 source = ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
| 67 } else { | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 double x = update.get(source).x(); | |
| 72 double y = update.get(source).y(); | |
| 73 double z = update.get(source).z(); | |
| 74 | |
| 75 // Create a unit vector for trigonometry | |
| 76 // TODO(jonross): Stop reversing signs for vector components once | |
| 77 // accelerometer values have been fixed. crbug.com/431391 | |
| 78 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | |
| 79 // results. | |
| 80 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | |
| 81 data.Scale(1.0f / data.Length()); | |
| 82 | |
| 83 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | |
| 84 // x = sin(gamma) | |
| 85 // y = -cos(gamma) * sin(beta) | |
| 86 // z = cos(beta) * cos(gamma) | |
| 87 // With only accelerometer alpha cannot be provided. | |
| 88 double beta = kRad2deg * atan2(data.y(), data.z()); | |
| 89 double gamma = kRad2deg * asin(data.x()); | |
| 90 | |
| 91 // Convert beta and gamma to fit the intervals in the specification. Beta is | |
| 92 // [-180, 180) and gamma is [-90, 90). | |
| 93 if (beta >= 180.0f) | |
| 94 beta = -180.0f; | |
| 95 if (gamma >= 90.0f) | |
| 96 gamma = -90.0f; | |
| 97 orientation_buffer_->seqlock.WriteBegin(); | |
| 98 orientation_buffer_->data.beta = beta; | |
| 99 orientation_buffer_->data.hasBeta = true; | |
| 100 orientation_buffer_->data.gamma = gamma; | |
| 101 orientation_buffer_->data.hasGamma = true; | |
| 102 orientation_buffer_->data.allAvailableSensorsAreActive = true; | |
| 103 orientation_buffer_->seqlock.WriteEnd(); | |
| 104 } | |
| 105 | |
| 106 void SensorManagerChromeOS::StartObservingAccelerometer() { | |
| 107 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
| 108 } | |
| 109 | |
| 110 void SensorManagerChromeOS::StopObservingAccelerometer() { | |
| 111 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
| 112 } | |
| 113 | |
| 114 } // namespace content | |
| OLD | NEW |