Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #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 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
|
timvolodine
2015/01/21 13:16:57
it looks a bit strange you are adding an observer
jonross
2015/01/21 16:02:11
I had originally considered that. However when I i
timvolodine
2015/01/21 19:08:21
I think the add/remove observer should generally c
| |
| 20 } | |
| 21 | |
| 22 SensorManagerChromeOS::~SensorManagerChromeOS() { | |
| 23 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
| 24 } | |
| 25 | |
| 26 bool SensorManagerChromeOS::IsFetching() { | |
| 27 // TODO(jonross): When DeviceMotion API is implemented update this to check | |
| 28 // each of the different buffers. | |
| 29 return orientation_buffer_ != nullptr; | |
| 30 } | |
| 31 | |
| 32 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | |
| 33 DeviceOrientationHardwareBuffer* buffer) { | |
| 34 DCHECK(buffer); | |
| 35 base::AutoLock autolock(orientation_buffer_lock_); | |
| 36 orientation_buffer_ = buffer; | |
| 37 | |
| 38 // No compass information, so we cannot provide absolute orientation. | |
| 39 orientation_buffer_->seqlock.WriteBegin(); | |
| 40 orientation_buffer_->data.absolute = false; | |
| 41 orientation_buffer_->data.hasAbsolute = true; | |
| 42 orientation_buffer_->seqlock.WriteEnd(); | |
| 43 return true; | |
| 44 } | |
| 45 | |
| 46 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | |
| 47 base::AutoLock autolock(orientation_buffer_lock_); | |
| 48 if (!orientation_buffer_) | |
| 49 return false; | |
| 50 // Make sure to indicate that the sensor data is no longer available. | |
| 51 orientation_buffer_->seqlock.WriteBegin(); | |
| 52 orientation_buffer_->data.allAvailableSensorsAreActive = false; | |
| 53 orientation_buffer_->seqlock.WriteEnd(); | |
| 54 orientation_buffer_ = nullptr; | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 void SensorManagerChromeOS::OnAccelerometerUpdated( | |
| 59 const ui::AccelerometerUpdate& update) { | |
| 60 base::AutoLock autolock(orientation_buffer_lock_); | |
| 61 if (!orientation_buffer_) | |
| 62 return; | |
| 63 | |
| 64 ui::AccelerometerSource source; | |
| 65 if (update.has(ui::ACCELEROMETER_SOURCE_SCREEN)) { | |
| 66 source = ui::ACCELEROMETER_SOURCE_SCREEN; | |
| 67 } else if (update.has(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) { | |
| 68 source = ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
| 69 } else { | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 double x = update.get(source).x(); | |
| 74 double y = update.get(source).y(); | |
| 75 double z = update.get(source).z(); | |
| 76 | |
| 77 // Create a unit vector for trigonometry | |
| 78 // TODO(jonross): Stop reversing signs for vector components once | |
| 79 // accelerometer values have been fixed. crbug.com/431391 | |
| 80 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | |
| 81 // results. | |
| 82 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | |
| 83 data.Scale(1.0f / data.Length()); | |
| 84 | |
| 85 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | |
| 86 // x = sin(gamma) | |
| 87 // y = -cos(gamma) * sin(beta) | |
| 88 // z = cos(beta) * cos(gamma) | |
| 89 // With only accelerometer alpha cannot be provided. | |
| 90 double beta = kRad2deg * atan2(data.y(), data.z()); | |
| 91 double gamma = kRad2deg * asin(data.x()); | |
| 92 | |
| 93 // Convert beta and gamma to fit the intervals in the specification. Beta is | |
| 94 // [-180, 180) and gamma is [-90, 90). | |
| 95 if (beta >= 180.0f) | |
| 96 beta = -180.0f; | |
| 97 if (gamma >= 90.0f) | |
| 98 gamma = -90.0f; | |
| 99 // TODO: move chromeos/accelerometer to /content/browser/device_sensors/ and | |
|
timvolodine
2015/01/21 13:16:57
is this still relevant?
jonross
2015/01/21 16:02:10
Nope, missed removing it.
| |
| 100 // have it generate the beta and gamma values. crbug.com/431865 | |
| 101 orientation_buffer_->seqlock.WriteBegin(); | |
| 102 orientation_buffer_->data.beta = beta; | |
| 103 orientation_buffer_->data.hasBeta = true; | |
| 104 orientation_buffer_->data.gamma = gamma; | |
| 105 orientation_buffer_->data.hasGamma = true; | |
| 106 orientation_buffer_->data.allAvailableSensorsAreActive = true; | |
| 107 orientation_buffer_->seqlock.WriteEnd(); | |
| 108 } | |
| 109 | |
| 110 } // namespace content | |
| OLD | NEW |