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) { | |
timvolodine
2015/03/09 15:55:57
Do StartFetchingDeviceMotionData and OnAcceleromet
jonross
2015/03/09 16:35:26
AccelerometerReader has been updated to use a thre
timvolodine
2015/03/10 13:03:08
I see, in that case how about adding a ThreadCheck
jonross
2015/03/10 15:03:58
Done.
| |
30 DCHECK(buffer); | |
31 if (motion_buffer_) | |
32 return false; | |
33 motion_buffer_ = buffer; | |
34 | |
35 motion_buffer_->seqlock.WriteBegin(); | |
36 motion_buffer_->data.interval = std::max( | |
timvolodine
2015/03/09 15:55:57
nit: maybe add a comment why this should be max(..
jonross
2015/03/09 20:46:24
Done.
| |
37 kInertialSensorIntervalMicroseconds / 1000, | |
38 chromeos::AccelerometerReader::kDelayBetweenReadsMs); | |
39 motion_buffer_->seqlock.WriteEnd(); | |
40 | |
41 StartObservingAccelerometer(); | |
42 return true; | |
43 } | |
44 | |
45 bool SensorManagerChromeOS::StopFetchingDeviceMotionData() { | |
46 if (!motion_buffer_) | |
47 return false; | |
timvolodine
2015/03/09 15:55:57
nit: maybe extra empty line for readability.
jonross
2015/03/09 20:46:24
Done.
| |
48 // Make sure to indicate that the sensor data is no longer available. | |
49 motion_buffer_->seqlock.WriteBegin(); | |
50 motion_buffer_->data.allAvailableSensorsAreActive = false; | |
51 motion_buffer_->seqlock.WriteEnd(); | |
52 | |
53 motion_buffer_ = nullptr; | |
54 | |
55 StopObservingAccelerometer(); | |
56 return true; | |
57 } | |
58 | |
27 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( | 59 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData( |
28 DeviceOrientationHardwareBuffer* buffer) { | 60 DeviceOrientationHardwareBuffer* buffer) { |
29 DCHECK(buffer); | 61 DCHECK(buffer); |
30 { | 62 if (orientation_buffer_) |
31 base::AutoLock autolock(orientation_buffer_lock_); | 63 return false; |
32 if (orientation_buffer_) | 64 orientation_buffer_ = buffer; |
33 return false; | |
34 orientation_buffer_ = buffer; | |
35 | 65 |
36 // No compass information, so we cannot provide absolute orientation. | 66 // No compass information, so we cannot provide absolute orientation. |
37 orientation_buffer_->seqlock.WriteBegin(); | 67 orientation_buffer_->seqlock.WriteBegin(); |
38 orientation_buffer_->data.absolute = false; | 68 orientation_buffer_->data.absolute = false; |
39 orientation_buffer_->data.hasAbsolute = true; | 69 orientation_buffer_->data.hasAbsolute = true; |
40 orientation_buffer_->seqlock.WriteEnd(); | 70 orientation_buffer_->seqlock.WriteEnd(); |
41 } | |
42 | 71 |
43 StartObservingAccelerometer(); | 72 StartObservingAccelerometer(); |
44 return true; | 73 return true; |
45 } | 74 } |
46 | 75 |
47 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { | 76 bool SensorManagerChromeOS::StopFetchingDeviceOrientationData() { |
48 { | 77 if (!orientation_buffer_) |
49 base::AutoLock autolock(orientation_buffer_lock_); | 78 return false; |
50 if (!orientation_buffer_) | 79 // Make sure to indicate that the sensor data is no longer available. |
51 return false; | 80 orientation_buffer_->seqlock.WriteBegin(); |
52 // Make sure to indicate that the sensor data is no longer available. | 81 orientation_buffer_->data.allAvailableSensorsAreActive = false; |
53 orientation_buffer_->seqlock.WriteBegin(); | 82 orientation_buffer_->seqlock.WriteEnd(); |
54 orientation_buffer_->data.allAvailableSensorsAreActive = false; | 83 orientation_buffer_ = nullptr; |
55 orientation_buffer_->seqlock.WriteEnd(); | |
56 orientation_buffer_ = nullptr; | |
57 } | |
58 | 84 |
59 StopObservingAccelerometer(); | 85 StopObservingAccelerometer(); |
60 return true; | 86 return true; |
61 } | 87 } |
62 | 88 |
63 void SensorManagerChromeOS::OnAccelerometerUpdated( | 89 void SensorManagerChromeOS::OnAccelerometerUpdated( |
64 const chromeos::AccelerometerUpdate& update) { | 90 scoped_refptr<const chromeos::AccelerometerUpdate> update) { |
65 base::AutoLock autolock(orientation_buffer_lock_); | 91 chromeos::AccelerometerSource source; |
92 if (update->has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) | |
93 source = chromeos::ACCELEROMETER_SOURCE_SCREEN; | |
94 else if (update->has(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD)) | |
95 source = chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD; | |
96 else | |
97 return; | |
98 | |
99 double x = update->get(source).x; | |
100 double y = update->get(source).y; | |
101 double z = update->get(source).z; | |
102 | |
103 GenerateMotionEvent(x, y, z); | |
104 GenerateOrientationEvent(x, y, z); | |
105 } | |
106 | |
107 void SensorManagerChromeOS::StartObservingAccelerometer() { | |
108 if (chromeos::AccelerometerReader::GetInstance()->HasObserver(this)) | |
109 return; | |
110 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); | |
111 } | |
112 | |
113 void SensorManagerChromeOS::StopObservingAccelerometer() { | |
114 if (orientation_buffer_ || motion_buffer_) | |
115 return; | |
timvolodine
2015/03/09 15:55:57
it's a bit strange to have this kind of logic, i.e
jonross
2015/03/09 16:35:26
It is not sufficient if we have two pages currentl
timvolodine
2015/03/10 13:03:08
ok I see, IMHO it would be more readable to have s
jonross
2015/03/10 15:03:58
Done.
Went with the check in each Stop*Buffer met
| |
116 DCHECK(chromeos::AccelerometerReader::GetInstance()->HasObserver(this)); | |
117 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); | |
118 } | |
119 | |
120 void SensorManagerChromeOS::GenerateMotionEvent(double x, double y, double z) { | |
121 if (!motion_buffer_) | |
122 return; | |
123 | |
124 motion_buffer_->seqlock.WriteBegin(); | |
125 motion_buffer_->data.accelerationIncludingGravityX = x; | |
126 motion_buffer_->data.hasAccelerationIncludingGravityX = true; | |
127 motion_buffer_->data.accelerationIncludingGravityY = y; | |
128 motion_buffer_->data.hasAccelerationIncludingGravityY = true; | |
129 motion_buffer_->data.accelerationIncludingGravityZ = z; | |
130 motion_buffer_->data.hasAccelerationIncludingGravityZ = true; | |
131 motion_buffer_->data.allAvailableSensorsAreActive = true; | |
132 motion_buffer_->seqlock.WriteEnd(); | |
133 } | |
134 | |
135 void SensorManagerChromeOS::GenerateOrientationEvent(double x, | |
136 double y, | |
137 double z) { | |
66 if (!orientation_buffer_) | 138 if (!orientation_buffer_) |
67 return; | 139 return; |
68 | 140 |
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 | 141 // Create a unit vector for trigonometry |
83 // TODO(jonross): Stop reversing signs for vector components once | 142 // TODO(jonross): Stop reversing signs for vector components once |
84 // accelerometer values have been fixed. crbug.com/431391 | 143 // accelerometer values have been fixed. crbug.com/431391 |
85 // Ternaries are to remove -0.0f which gives incorrect trigonometrical | 144 // Ternaries are to remove -0.0f which gives incorrect trigonometrical |
86 // results. | 145 // results. |
87 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); | 146 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f); |
88 data.Scale(1.0f / data.Length()); | 147 data.Scale(1.0f / data.Length()); |
89 | 148 |
90 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. | 149 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix. |
91 // x = sin(gamma) | 150 // x = sin(gamma) |
(...skipping 11 matching lines...) Expand all Loading... | |
103 gamma = -90.0f; | 162 gamma = -90.0f; |
104 orientation_buffer_->seqlock.WriteBegin(); | 163 orientation_buffer_->seqlock.WriteBegin(); |
105 orientation_buffer_->data.beta = beta; | 164 orientation_buffer_->data.beta = beta; |
106 orientation_buffer_->data.hasBeta = true; | 165 orientation_buffer_->data.hasBeta = true; |
107 orientation_buffer_->data.gamma = gamma; | 166 orientation_buffer_->data.gamma = gamma; |
108 orientation_buffer_->data.hasGamma = true; | 167 orientation_buffer_->data.hasGamma = true; |
109 orientation_buffer_->data.allAvailableSensorsAreActive = true; | 168 orientation_buffer_->data.allAvailableSensorsAreActive = true; |
110 orientation_buffer_->seqlock.WriteEnd(); | 169 orientation_buffer_->seqlock.WriteEnd(); |
111 } | 170 } |
112 | 171 |
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 | 172 } // namespace content |
OLD | NEW |