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_chromeos.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 void SensorManagerChromeOS::SetDelegate( | |
26 SensorManagerDelegateChromeOS* delegate) { | |
27 delegate_ = delegate; | |
28 } | |
29 | |
30 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | |
31 DeviceOrientationHardwareBuffer* buffer) { | |
32 DCHECK(buffer); | |
33 if (!delegate_) | |
34 return false; | |
35 | |
36 { | |
37 // Lock must be released before providing the callback to prevent potential | |
38 // deadlock. | |
39 base::AutoLock autolock(orientation_buffer_lock_); | |
40 orientation_buffer_ = buffer; | |
41 | |
42 // No compass information, so we cannot provide absolute orientation. | |
43 orientation_buffer_->seqlock.WriteBegin(); | |
44 orientation_buffer_->data.absolute = false; | |
45 orientation_buffer_->data.hasAbsolute = true; | |
46 orientation_buffer_->seqlock.WriteEnd(); | |
47 } | |
48 | |
49 delegate_->StartFetchingDeviceOrientationData(callback_); | |
50 | |
51 return true; | |
52 } | |
53 | |
54 void SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | |
55 if (!delegate_) | |
56 return; | |
57 delegate_->StopFetchingDeviceOrientationData(); | |
58 base::AutoLock autolock(orientation_buffer_lock_); | |
59 if (!orientation_buffer_) | |
60 return; | |
61 // Null message to signify that sensors are no longer available | |
timvolodine
2014/12/03 13:19:55
nit: is not really a null message, maybe better so
jonross
2014/12/03 14:43:58
Done.
| |
62 orientation_buffer_->seqlock.WriteBegin(); | |
63 orientation_buffer_->data.allAvailableSensorsAreActive = false; | |
64 orientation_buffer_->seqlock.WriteEnd(); | |
65 orientation_buffer_ = nullptr; | |
66 } | |
67 | |
68 SensorManagerChromeOS::~SensorManagerChromeOS() { | |
69 } | |
70 | |
71 SensorManagerChromeOS::SensorManagerChromeOS() | |
72 : callback_( | |
73 base::Bind(&SensorManagerChromeOS::OnAccelerationIncludingGravity, | |
74 base::Unretained(this))), | |
75 orientation_buffer_(nullptr), | |
76 delegate_(nullptr) { | |
77 } | |
78 | |
79 void SensorManagerChromeOS::OnAccelerationIncludingGravity(double x, | |
80 double y, | |
81 double z) { | |
82 base::AutoLock autolock(orientation_buffer_lock_); | |
83 if (!orientation_buffer_) | |
84 return; | |
85 // Create a unit vector for trigonometry | |
86 // TODO(jonross): Stop reversing signs for vector components once | |
87 // accelerometer values have been fixed. crbug.com/431391 | |
88 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | |
89 // results. | |
90 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | |
91 data.Scale(1.0f / data.Length()); | |
92 | |
93 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | |
94 // x = sin(gamma) | |
95 // y = -cos(gamma) * sin(beta) | |
96 // z = cos(beta) * cos(gamma) | |
97 // With only accelerometer alpha cannot be provided. | |
98 double beta = kRad2deg * atan2(data.y(), data.z()); | |
99 double gamma = kRad2deg * asin(data.x()); | |
100 | |
101 // Convert beta and gamma to fit the intervals in the specification. Beta is | |
102 // [-180, 180) and gamma is [-90, 90). | |
103 if (beta >= 180.0f) | |
104 beta = -180.0f; | |
105 if (gamma >= 90.0f) | |
106 gamma = -90.0f; | |
107 // TODO: move chromeos/accelerometer to /content/browser/device_sensors/ and | |
108 // have it generate the beta and gamma values. crbug.com/431865 | |
109 orientation_buffer_->seqlock.WriteBegin(); | |
110 orientation_buffer_->data.beta = beta; | |
111 orientation_buffer_->data.hasBeta = true; | |
112 orientation_buffer_->data.gamma = gamma; | |
113 orientation_buffer_->data.hasGamma = true; | |
114 orientation_buffer_->data.allAvailableSensorsAreActive = true; | |
115 orientation_buffer_->seqlock.WriteEnd(); | |
116 } | |
117 | |
118 } // namespace content | |
OLD | NEW |