Chromium Code Reviews| Index: content/browser/device_sensors/sensor_manager_chromeos.cc |
| diff --git a/content/browser/device_sensors/sensor_manager_chromeos.cc b/content/browser/device_sensors/sensor_manager_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c9541814c09a9f33416a1a2926a2968d29d1dfe |
| --- /dev/null |
| +++ b/content/browser/device_sensors/sensor_manager_chromeos.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/device_sensors/sensor_manager_chromeos.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/memory/singleton.h" |
| +#include "content/public/browser/sensor_manager_delegate.h" |
| +#include "ui/gfx/geometry/vector3d_f.h" |
| + |
| +namespace { |
| +// Conversion ratio from radians to degrees. |
| +const double kRad2deg = 180.0 / M_PI; |
| +} |
| + |
| +namespace content { |
| + |
| +SensorManagerChromeOS* SensorManagerChromeOS::GetInstance() { |
| + return Singleton<SensorManagerChromeOS, |
| + LeakySingletonTraits<SensorManagerChromeOS>>::get(); |
| +} |
| + |
| +bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( |
| + DeviceOrientationHardwareBuffer* buffer) { |
| + DCHECK(buffer); |
| + if (!SensorManagerDelegate::HasInstance()) |
| + return false; |
| + |
| + { |
| + // Lock must be released before providing the callback to prevent potential |
| + // deadlock. |
| + base::AutoLock autolock(orientation_buffer_lock_); |
| + orientation_buffer_ = buffer; |
| + |
| + // No compass information, so we cannot provide absolute orientation. |
| + orientation_buffer_->seqlock.WriteBegin(); |
| + orientation_buffer_->data.absolute = false; |
| + orientation_buffer_->data.hasAbsolute = true; |
| + orientation_buffer_->seqlock.WriteEnd(); |
| + } |
| + |
| + base::Callback<void(double, double, double)> callback = base::Bind( |
| + &SensorManagerChromeOS::OnAccelerationIncludingGravity, callback_ref_); |
| + SensorManagerDelegate::GetInstance()->StartFetchingDeviceOrientationData( |
| + callback); |
| + |
| + return true; |
| +} |
| + |
| +void SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
| + if (!SensorManagerDelegate::HasInstance()) |
| + return; |
| + SensorManagerDelegate::GetInstance()->StopFetchingDeviceOrientationData(); |
| + base::AutoLock autolock(orientation_buffer_lock_); |
| + if (!orientation_buffer_) |
| + return; |
| + // Null message to signify that sensors are no longer available |
| + orientation_buffer_->seqlock.WriteBegin(); |
| + orientation_buffer_->data.allAvailableSensorsAreActive = false; |
| + orientation_buffer_->seqlock.WriteEnd(); |
| + orientation_buffer_ = nullptr; |
| +} |
| + |
| +SensorManagerChromeOS::~SensorManagerChromeOS() { |
| +} |
| + |
| +SensorManagerChromeOS::SensorManagerChromeOS() |
| + : orientation_buffer_(nullptr), callback_ref_(this) { |
| +} |
| + |
| +void SensorManagerChromeOS::OnAccelerationIncludingGravity(double x, |
| + double y, |
| + double z) { |
| + base::AutoLock autolock(orientation_buffer_lock_); |
| + if (!orientation_buffer_) |
| + return; |
| + // Create a unit vector to remove gravity. |
|
flackr
2014/11/13 00:07:34
Did you mean for a TODO? It doesn't look like grav
jonross
2014/11/13 01:34:05
Poor comment, as this smooths out all acceleration
|
| + // TODO(jonross): Stop reversing signs for vector components once |
| + // accelerometer values have been reversed. |
| + gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
|
flackr
2014/11/13 00:07:34
Isn't this just the following? (i.e. why the terna
jonross
2014/11/13 01:34:05
Comment added, as -0.0f messed up trig
|
| + data.Scale(1.0f / data.Length()); |
| + |
| + // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
| + // x = sin(gamma) |
| + // y = -cos(gamma) * sin(beta) |
| + // z = cos(beta) * cos(gamma) |
| + // With only accelerometer alpha cannot be provided. |
| + double beta = kRad2deg * atan2(data.y(), data.z()); |
| + double gamma = kRad2deg * asin(data.x()); |
| + |
| + // Have the interval boundaries comply with the specification. Beta is |
|
flackr
2014/11/13 00:07:34
nit: Convert beta and gamma to fit the intervals i
jonross
2014/11/13 01:34:05
Done.
|
| + // [-180,180) and gamma is [-90, 90). |
|
flackr
2014/11/13 00:07:34
nit: [-180, 180)
jonross
2014/11/13 01:34:05
Done.
|
| + if (beta >= 180.0f) |
| + beta = -180.0f; |
| + if (gamma >= 90.0f) |
| + gamma = -90.0f; |
| + orientation_buffer_->seqlock.WriteBegin(); |
|
flackr
2014/11/13 00:07:34
Should we be moving this to accelerometer_reader.c
jonross
2014/11/13 01:34:05
TODO added
|
| + orientation_buffer_->data.beta = beta; |
| + orientation_buffer_->data.hasBeta = true; |
| + orientation_buffer_->data.gamma = gamma; |
| + orientation_buffer_->data.hasGamma = true; |
| + orientation_buffer_->data.allAvailableSensorsAreActive = true; |
| + orientation_buffer_->seqlock.WriteEnd(); |
| +} |
| + |
| +} // namespace content |