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/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 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
| |
| 80 // TODO(jonross): Stop reversing signs for vector components once | |
| 81 // accelerometer values have been reversed. | |
| 82 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
| |
| 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 // 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.
| |
| 94 // [-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.
| |
| 95 if (beta >= 180.0f) | |
| 96 beta = -180.0f; | |
| 97 if (gamma >= 90.0f) | |
| 98 gamma = -90.0f; | |
| 99 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
| |
| 100 orientation_buffer_->data.beta = beta; | |
| 101 orientation_buffer_->data.hasBeta = true; | |
| 102 orientation_buffer_->data.gamma = gamma; | |
| 103 orientation_buffer_->data.hasGamma = true; | |
| 104 orientation_buffer_->data.allAvailableSensorsAreActive = true; | |
| 105 orientation_buffer_->seqlock.WriteEnd(); | |
| 106 } | |
| 107 | |
| 108 } // namespace content | |
| OLD | NEW |