OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" | 5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/logging.h" | |
10 #include "chromeos/accelerometer/accelerometer_reader.h" | 9 #include "chromeos/accelerometer/accelerometer_reader.h" |
11 #include "chromeos/accelerometer/accelerometer_types.h" | 10 #include "chromeos/accelerometer/accelerometer_types.h" |
11 #include "content/browser/device_sensors/inertial_sensor_consts.h" | |
12 #include "ui/gfx/geometry/vector3d_f.h" | 12 #include "ui/gfx/geometry/vector3d_f.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 // Conversion ratio from radians to degrees. | 15 // Conversion ratio from radians to degrees. |
16 const double kRad2deg = 180.0 / M_PI; | 16 const double kRad2deg = 180.0 / M_PI; |
17 } | 17 } |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 SensorManagerChromeOS::SensorManagerChromeOS() : orientation_buffer_(nullptr) { | 21 SensorManagerChromeOS::SensorManagerChromeOS() |
22 : motion_buffer_(nullptr), orientation_buffer_(nullptr) { | |
22 } | 23 } |
23 | 24 |
24 SensorManagerChromeOS::~SensorManagerChromeOS() { | 25 SensorManagerChromeOS::~SensorManagerChromeOS() { |
25 } | 26 } |
26 | 27 |
28 bool SensorManagerChromeOS::StartFetchingDeviceMotionData( | |
29 DeviceMotionHardwareBuffer* buffer) { | |
30 DCHECK(buffer); | |
31 if (motion_buffer_) | |
32 return false; | |
33 motion_buffer_ = buffer; | |
34 | |
35 // TODO(jonross): this is currently the rate at which events are pumped, not | |
36 // at which new accelerometer updates are received. The accelerometer will | |
37 // be updated (crbug.com/459218) | |
flackr
2015/03/05 19:59:45
nit: nix the comment, while we will update the acc
jonross
2015/03/05 21:13:39
Done.
| |
38 motion_buffer_->seqlock.WriteBegin(); | |
39 motion_buffer_->data.interval = std::max( | |
40 kInertialSensorIntervalMicroseconds / 1000, | |
41 chromeos::AccelerometerReader::GetInstance()->DelayBetweenReadsMs()); | |
42 motion_buffer_->seqlock.WriteEnd(); | |
43 | |
44 StartObservingAccelerometer(); | |
45 return true; | |
46 } | |
47 | |
48 bool SensorManagerChromeOS::StopFetchingDeviceMotionData() { | |
49 if (!motion_buffer_) | |
50 return false; | |
51 // Make sure to indicate that the sensor data is no longer available. | |
52 motion_buffer_->seqlock.WriteBegin(); | |
53 motion_buffer_->data.allAvailableSensorsAreActive = false; | |
54 motion_buffer_->seqlock.WriteEnd(); | |
55 | |
56 motion_buffer_ = nullptr; | |
57 | |
58 StopObservingAccelerometer(); | |
59 return true; | |
60 } | |
61 | |
27 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | 62 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( |
28 DeviceOrientationHardwareBuffer* buffer) { | 63 DeviceOrientationHardwareBuffer* buffer) { |
29 DCHECK(buffer); | 64 DCHECK(buffer); |
30 { | 65 if (orientation_buffer_) |
31 base::AutoLock autolock(orientation_buffer_lock_); | 66 return false; |
32 if (orientation_buffer_) | 67 orientation_buffer_ = buffer; |
33 return false; | |
34 orientation_buffer_ = buffer; | |
35 | 68 |
36 // No compass information, so we cannot provide absolute orientation. | 69 // No compass information, so we cannot provide absolute orientation. |
37 orientation_buffer_->seqlock.WriteBegin(); | 70 orientation_buffer_->seqlock.WriteBegin(); |
38 orientation_buffer_->data.absolute = false; | 71 orientation_buffer_->data.absolute = false; |
39 orientation_buffer_->data.hasAbsolute = true; | 72 orientation_buffer_->data.hasAbsolute = true; |
40 orientation_buffer_->seqlock.WriteEnd(); | 73 orientation_buffer_->seqlock.WriteEnd(); |
41 } | |
42 | 74 |
43 StartObservingAccelerometer(); | 75 StartObservingAccelerometer(); |
44 return true; | 76 return true; |
45 } | 77 } |
46 | 78 |
47 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | 79 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
48 { | 80 if (!orientation_buffer_) |
49 base::AutoLock autolock(orientation_buffer_lock_); | 81 return false; |
50 if (!orientation_buffer_) | 82 // Make sure to indicate that the sensor data is no longer available. |
51 return false; | 83 orientation_buffer_->seqlock.WriteBegin(); |
52 // Make sure to indicate that the sensor data is no longer available. | 84 orientation_buffer_->data.allAvailableSensorsAreActive = false; |
53 orientation_buffer_->seqlock.WriteBegin(); | 85 orientation_buffer_->seqlock.WriteEnd(); |
54 orientation_buffer_->data.allAvailableSensorsAreActive = false; | 86 orientation_buffer_ = nullptr; |
55 orientation_buffer_->seqlock.WriteEnd(); | |
56 orientation_buffer_ = nullptr; | |
57 } | |
58 | 87 |
59 StopObservingAccelerometer(); | 88 StopObservingAccelerometer(); |
60 return true; | 89 return true; |
61 } | 90 } |
62 | 91 |
63 void SensorManagerChromeOS::OnAccelerometerUpdated( | 92 void SensorManagerChromeOS::OnAccelerometerUpdated( |
64 const chromeos::AccelerometerUpdate& update) { | 93 scoped_refptr<const chromeos::AccelerometerUpdate> update) { |
65 base::AutoLock autolock(orientation_buffer_lock_); | 94 chromeos::AccelerometerSource source; |
95 if (update->has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) | |
96 source = chromeos::ACCELEROMETER_SOURCE_SCREEN; | |
97 else if (update->has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) | |
98 source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
99 else | |
100 return; | |
101 | |
102 double x = update->get(source).x; | |
103 double y = update->get(source).y; | |
104 double z = update->get(source).z; | |
105 | |
106 GenerateMotionEvent(x, y, z); | |
107 GenerateOrientationEvent(x, y, z); | |
108 } | |
109 | |
110 void SensorManagerChromeOS::StartObservingAccelerometer() { | |
111 if (chromeos::AccelerometerReader::GetInstance()->HasObserver(this)) | |
112 return; | |
113 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
114 } | |
115 | |
116 void SensorManagerChromeOS::StopObservingAccelerometer() { | |
117 if (orientation_buffer_ || motion_buffer_) | |
118 return; | |
119 DCHECK(chromeos::AccelerometerReader::GetInstance()->HasObserver(this)); | |
120 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
121 } | |
122 | |
123 void SensorManagerChromeOS::GenerateMotionEvent(double x, double y, double z) { | |
124 if (!motion_buffer_) | |
125 return; | |
126 | |
127 motion_buffer_->seqlock.WriteBegin(); | |
128 motion_buffer_->data.accelerationIncludingGravityX = x; | |
129 motion_buffer_->data.hasAccelerationIncludingGravityX = true; | |
130 motion_buffer_->data.accelerationIncludingGravityY = y; | |
131 motion_buffer_->data.hasAccelerationIncludingGravityY = true; | |
132 motion_buffer_->data.accelerationIncludingGravityZ = z; | |
133 motion_buffer_->data.hasAccelerationIncludingGravityZ = true; | |
134 motion_buffer_->data.allAvailableSensorsAreActive = true; | |
135 motion_buffer_->seqlock.WriteEnd(); | |
136 } | |
137 | |
138 void SensorManagerChromeOS::GenerateOrientationEvent(double x, | |
139 double y, | |
140 double z) { | |
66 if (!orientation_buffer_) | 141 if (!orientation_buffer_) |
67 return; | 142 return; |
68 | 143 |
69 chromeos::AccelerometerSource source; | |
70 if (update.has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) { | |
71 source = chromeos::ACCELEROMETER_SOURCE_SCREEN; | |
72 } else if (update.has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) { | |
73 source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
74 } else { | |
75 return; | |
76 } | |
77 | |
78 double x = update.get(source).x; | |
79 double y = update.get(source).y; | |
80 double z = update.get(source).z; | |
81 | |
82 // Create a unit vector for trigonometry | 144 // Create a unit vector for trigonometry |
83 // TODO(jonross): Stop reversing signs for vector components once | 145 // TODO(jonross): Stop reversing signs for vector components once |
84 // accelerometer values have been fixed. crbug.com/431391 | 146 // accelerometer values have been fixed. crbug.com/431391 |
85 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | 147 // Ternaries are to remove -0.0f which gives incorrect trigonometrical |
86 // results. | 148 // results. |
87 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | 149 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
88 data.Scale(1.0f / data.Length()); | 150 data.Scale(1.0f / data.Length()); |
89 | 151 |
90 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | 152 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
91 // x = sin(gamma) | 153 // x = sin(gamma) |
(...skipping 11 matching lines...) Expand all Loading... | |
103 gamma = -90.0f; | 165 gamma = -90.0f; |
104 orientation_buffer_->seqlock.WriteBegin(); | 166 orientation_buffer_->seqlock.WriteBegin(); |
105 orientation_buffer_->data.beta = beta; | 167 orientation_buffer_->data.beta = beta; |
106 orientation_buffer_->data.hasBeta = true; | 168 orientation_buffer_->data.hasBeta = true; |
107 orientation_buffer_->data.gamma = gamma; | 169 orientation_buffer_->data.gamma = gamma; |
108 orientation_buffer_->data.hasGamma = true; | 170 orientation_buffer_->data.hasGamma = true; |
109 orientation_buffer_->data.allAvailableSensorsAreActive = true; | 171 orientation_buffer_->data.allAvailableSensorsAreActive = true; |
110 orientation_buffer_->seqlock.WriteEnd(); | 172 orientation_buffer_->seqlock.WriteEnd(); |
111 } | 173 } |
112 | 174 |
113 void SensorManagerChromeOS::StartObservingAccelerometer() { | |
114 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
115 } | |
116 | |
117 void SensorManagerChromeOS::StopObservingAccelerometer() { | |
118 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
119 } | |
120 | |
121 } // namespace content | 175 } // namespace content |
OLD | NEW |