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/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/memory/singleton.h" |
| 10 #include "content/public/browser/sensor_manager_delegate.h" |
| 11 #include "ui/gfx/geometry/vector3d_f.h" |
| 12 |
| 13 namespace { |
| 14 // Conversion ratio from radians to degrees. |
| 15 const double kRad2deg = 180.0 / M_PI; |
| 16 } |
| 17 |
| 18 namespace content { |
| 19 |
| 20 SensorManagerChromeOS* SensorManagerChromeOS::GetInstance() { |
| 21 return Singleton<SensorManagerChromeOS, |
| 22 LeakySingletonTraits<SensorManagerChromeOS>>::get(); |
| 23 } |
| 24 |
| 25 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( |
| 26 DeviceOrientationHardwareBuffer* buffer) { |
| 27 DCHECK(buffer); |
| 28 if (!SensorManagerDelegate::HasInstance()) |
| 29 return false; |
| 30 |
| 31 { |
| 32 // Lock must be released before providing the callback to prevent potential |
| 33 // deadlock. |
| 34 base::AutoLock autolock(orientation_buffer_lock_); |
| 35 orientation_buffer_ = buffer; |
| 36 |
| 37 // No compass information, so we cannot provide absolute orientation. |
| 38 orientation_buffer_->seqlock.WriteBegin(); |
| 39 orientation_buffer_->data.absolute = false; |
| 40 orientation_buffer_->data.hasAbsolute = true; |
| 41 orientation_buffer_->seqlock.WriteEnd(); |
| 42 } |
| 43 |
| 44 base::Callback<void(double, double, double)> callback = base::Bind( |
| 45 &SensorManagerChromeOS::OnAccelerationIncludingGravity, callback_ref_); |
| 46 SensorManagerDelegate::GetInstance()->StartFetchingDeviceOrientationData( |
| 47 callback); |
| 48 |
| 49 return true; |
| 50 } |
| 51 |
| 52 void SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
| 53 if (!SensorManagerDelegate::HasInstance()) |
| 54 return; |
| 55 SensorManagerDelegate::GetInstance()->StopFetchingDeviceOrientationData(); |
| 56 base::AutoLock autolock(orientation_buffer_lock_); |
| 57 if (!orientation_buffer_) |
| 58 return; |
| 59 // Null message to signify that sensors are no longer available |
| 60 orientation_buffer_->seqlock.WriteBegin(); |
| 61 orientation_buffer_->data.allAvailableSensorsAreActive = false; |
| 62 orientation_buffer_->seqlock.WriteEnd(); |
| 63 orientation_buffer_ = nullptr; |
| 64 } |
| 65 |
| 66 SensorManagerChromeOS::~SensorManagerChromeOS() { |
| 67 } |
| 68 |
| 69 SensorManagerChromeOS::SensorManagerChromeOS() |
| 70 : orientation_buffer_(nullptr), callback_ref_(this) { |
| 71 } |
| 72 |
| 73 void SensorManagerChromeOS::OnAccelerationIncludingGravity(double x, |
| 74 double y, |
| 75 double z) { |
| 76 base::AutoLock autolock(orientation_buffer_lock_); |
| 77 if (!orientation_buffer_) |
| 78 return; |
| 79 // Create a unit vector for trigonometry |
| 80 // TODO(jonross): Stop reversing signs for vector components once |
| 81 // accelerometer values have been fixed. crbug.com/431391 |
| 82 // Ternaries are to remove -0.0f which gives incorrect trigonometrical |
| 83 // results. |
| 84 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
| 85 data.Scale(1.0f / data.Length()); |
| 86 |
| 87 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
| 88 // x = sin(gamma) |
| 89 // y = -cos(gamma) * sin(beta) |
| 90 // z = cos(beta) * cos(gamma) |
| 91 // With only accelerometer alpha cannot be provided. |
| 92 double beta = kRad2deg * atan2(data.y(), data.z()); |
| 93 double gamma = kRad2deg * asin(data.x()); |
| 94 |
| 95 // Convert beta and gamma to fit the intervals in the specification. Beta is |
| 96 // [-180, 180) and gamma is [-90, 90). |
| 97 if (beta >= 180.0f) |
| 98 beta = -180.0f; |
| 99 if (gamma >= 90.0f) |
| 100 gamma = -90.0f; |
| 101 // TODO: move chromeos/accelerometer to /content/browser/device_sensors/ and |
| 102 // have it generate the beta and gamma values. crbug.com/431865 |
| 103 orientation_buffer_->seqlock.WriteBegin(); |
| 104 orientation_buffer_->data.beta = beta; |
| 105 orientation_buffer_->data.hasBeta = true; |
| 106 orientation_buffer_->data.gamma = gamma; |
| 107 orientation_buffer_->data.hasGamma = true; |
| 108 orientation_buffer_->data.allAvailableSensorsAreActive = true; |
| 109 orientation_buffer_->seqlock.WriteEnd(); |
| 110 } |
| 111 |
| 112 } // namespace content |
OLD | NEW |