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 |
27 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | 28 void SensorManagerChromeOS::StartFetchingDeviceMotionData( |
28 DeviceOrientationHardwareBuffer* buffer) { | 29 DeviceMotionHardwareBuffer* buffer) { |
29 DCHECK(buffer); | 30 DCHECK(buffer); |
30 { | 31 if (motion_buffer_) |
flackr
2015/03/10 05:32:36
Start should never be called twice without calling
jonross
2015/03/10 15:03:58
It appears so. Once the buffer is created new page
| |
31 base::AutoLock autolock(orientation_buffer_lock_); | 32 return; |
32 if (orientation_buffer_) | 33 motion_buffer_ = buffer; |
33 return false; | |
34 orientation_buffer_ = buffer; | |
35 | 34 |
36 // No compass information, so we cannot provide absolute orientation. | 35 motion_buffer_->seqlock.WriteBegin(); |
37 orientation_buffer_->seqlock.WriteBegin(); | 36 // The interval between updates is the longer of the rate set on the buffer, |
38 orientation_buffer_->data.absolute = false; | 37 // and the rate at which AccelerometerReader polls the sensor. |
39 orientation_buffer_->data.hasAbsolute = true; | 38 motion_buffer_->data.interval = std::max( |
40 orientation_buffer_->seqlock.WriteEnd(); | 39 kInertialSensorIntervalMicroseconds / 1000, |
41 } | 40 chromeos::AccelerometerReader::kDelayBetweenReadsMs); |
41 motion_buffer_->seqlock.WriteEnd(); | |
42 | 42 |
43 StartObservingAccelerometer(); | 43 StartObservingAccelerometer(); |
44 } | |
45 | |
46 bool SensorManagerChromeOS::StopFetchingDeviceMotionData() { | |
47 if (!motion_buffer_) | |
48 return false; | |
49 | |
50 // Make sure to indicate that the sensor data is no longer available. | |
51 motion_buffer_->seqlock.WriteBegin(); | |
52 motion_buffer_->data.allAvailableSensorsAreActive = false; | |
53 motion_buffer_->seqlock.WriteEnd(); | |
54 | |
55 motion_buffer_ = nullptr; | |
56 | |
57 StopObservingAccelerometer(); | |
44 return true; | 58 return true; |
45 } | 59 } |
46 | 60 |
61 void SensorManagerChromeOS::StartFetchingDeviceOrientationData( | |
62 DeviceOrientationHardwareBuffer* buffer) { | |
63 DCHECK(buffer); | |
64 if (orientation_buffer_) | |
flackr
2015/03/10 05:32:36
ditto
jonross
2015/03/10 15:03:58
Done.
| |
65 return; | |
66 orientation_buffer_ = buffer; | |
67 | |
68 // No compass information, so we cannot provide absolute orientation. | |
69 orientation_buffer_->seqlock.WriteBegin(); | |
70 orientation_buffer_->data.absolute = false; | |
71 orientation_buffer_->data.hasAbsolute = true; | |
72 orientation_buffer_->seqlock.WriteEnd(); | |
73 | |
74 StartObservingAccelerometer(); | |
75 } | |
76 | |
47 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | 77 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
48 { | 78 if (!orientation_buffer_) |
49 base::AutoLock autolock(orientation_buffer_lock_); | 79 return false; |
50 if (!orientation_buffer_) | 80 // Make sure to indicate that the sensor data is no longer available. |
51 return false; | 81 orientation_buffer_->seqlock.WriteBegin(); |
52 // Make sure to indicate that the sensor data is no longer available. | 82 orientation_buffer_->data.allAvailableSensorsAreActive = false; |
53 orientation_buffer_->seqlock.WriteBegin(); | 83 orientation_buffer_->seqlock.WriteEnd(); |
54 orientation_buffer_->data.allAvailableSensorsAreActive = false; | 84 orientation_buffer_ = nullptr; |
55 orientation_buffer_->seqlock.WriteEnd(); | |
56 orientation_buffer_ = nullptr; | |
57 } | |
58 | 85 |
59 StopObservingAccelerometer(); | 86 StopObservingAccelerometer(); |
60 return true; | 87 return true; |
61 } | 88 } |
62 | 89 |
63 void SensorManagerChromeOS::OnAccelerometerUpdated( | 90 void SensorManagerChromeOS::OnAccelerometerUpdated( |
64 const chromeos::AccelerometerUpdate& update) { | 91 scoped_refptr<const chromeos::AccelerometerUpdate> update) { |
65 base::AutoLock autolock(orientation_buffer_lock_); | 92 chromeos::AccelerometerSource source; |
93 if (update->has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) | |
94 source = chromeos::ACCELEROMETER_SOURCE_SCREEN; | |
95 else if (update->has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) | |
96 source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
97 else | |
98 return; | |
99 | |
100 double x = update->get(source).x; | |
101 double y = update->get(source).y; | |
102 double z = update->get(source).z; | |
103 | |
104 GenerateMotionEvent(x, y, z); | |
105 GenerateOrientationEvent(x, y, z); | |
106 } | |
107 | |
108 void SensorManagerChromeOS::StartObservingAccelerometer() { | |
109 if (chromeos::AccelerometerReader::GetInstance()->HasObserver(this)) | |
110 return; | |
111 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
112 } | |
113 | |
114 void SensorManagerChromeOS::StopObservingAccelerometer() { | |
115 if (orientation_buffer_ || motion_buffer_) | |
116 return; | |
117 DCHECK(chromeos::AccelerometerReader::GetInstance()->HasObserver(this)); | |
118 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
119 } | |
120 | |
121 void SensorManagerChromeOS::GenerateMotionEvent(double x, double y, double z) { | |
122 if (!motion_buffer_) | |
123 return; | |
124 | |
125 motion_buffer_->seqlock.WriteBegin(); | |
126 motion_buffer_->data.accelerationIncludingGravityX = x; | |
127 motion_buffer_->data.hasAccelerationIncludingGravityX = true; | |
128 motion_buffer_->data.accelerationIncludingGravityY = y; | |
129 motion_buffer_->data.hasAccelerationIncludingGravityY = true; | |
130 motion_buffer_->data.accelerationIncludingGravityZ = z; | |
131 motion_buffer_->data.hasAccelerationIncludingGravityZ = true; | |
132 motion_buffer_->data.allAvailableSensorsAreActive = true; | |
133 motion_buffer_->seqlock.WriteEnd(); | |
134 } | |
135 | |
136 void SensorManagerChromeOS::GenerateOrientationEvent(double x, | |
137 double y, | |
138 double z) { | |
66 if (!orientation_buffer_) | 139 if (!orientation_buffer_) |
67 return; | 140 return; |
68 | 141 |
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 | 142 // Create a unit vector for trigonometry |
83 // TODO(jonross): Stop reversing signs for vector components once | 143 // TODO(jonross): Stop reversing signs for vector components once |
84 // accelerometer values have been fixed. crbug.com/431391 | 144 // accelerometer values have been fixed. crbug.com/431391 |
85 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | 145 // Ternaries are to remove -0.0f which gives incorrect trigonometrical |
86 // results. | 146 // results. |
87 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | 147 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
88 data.Scale(1.0f / data.Length()); | 148 data.Scale(1.0f / data.Length()); |
89 | 149 |
90 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | 150 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
91 // x = sin(gamma) | 151 // x = sin(gamma) |
(...skipping 11 matching lines...) Expand all Loading... | |
103 gamma = -90.0f; | 163 gamma = -90.0f; |
104 orientation_buffer_->seqlock.WriteBegin(); | 164 orientation_buffer_->seqlock.WriteBegin(); |
105 orientation_buffer_->data.beta = beta; | 165 orientation_buffer_->data.beta = beta; |
106 orientation_buffer_->data.hasBeta = true; | 166 orientation_buffer_->data.hasBeta = true; |
107 orientation_buffer_->data.gamma = gamma; | 167 orientation_buffer_->data.gamma = gamma; |
108 orientation_buffer_->data.hasGamma = true; | 168 orientation_buffer_->data.hasGamma = true; |
109 orientation_buffer_->data.allAvailableSensorsAreActive = true; | 169 orientation_buffer_->data.allAvailableSensorsAreActive = true; |
110 orientation_buffer_->seqlock.WriteEnd(); | 170 orientation_buffer_->seqlock.WriteEnd(); |
111 } | 171 } |
112 | 172 |
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 | 173 } // namespace content |
OLD | NEW |