OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_orientation/sensor_manager_android.h" | 5 #include "content/browser/device_orientation/sensor_manager_android.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "content/browser/device_orientation/inertial_sensor_consts.h" | 12 #include "content/browser/device_orientation/inertial_sensor_consts.h" |
13 #include "jni/DeviceMotionAndOrientation_jni.h" | 13 #include "jni/DeviceMotionAndOrientation_jni.h" |
14 | 14 |
15 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 static void updateRotationVectorHistogram(bool value) { | 19 static void updateRotationVectorHistogram(bool value) { |
20 UMA_HISTOGRAM_BOOLEAN("InertialSensor.RotationVectorAndroidAvailable", value); | 20 UMA_HISTOGRAM_BOOLEAN("InertialSensor.RotationVectorAndroidAvailable", value); |
21 } | 21 } |
22 | 22 |
23 } | 23 } |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 | 26 |
27 SensorManagerAndroid::SensorManagerAndroid() | 27 SensorManagerAndroid::SensorManagerAndroid() |
28 : number_active_device_motion_sensors_(0), | 28 : number_active_device_motion_sensors_(0), |
| 29 device_light_buffer_(NULL), |
29 device_motion_buffer_(NULL), | 30 device_motion_buffer_(NULL), |
30 device_orientation_buffer_(NULL), | 31 device_orientation_buffer_(NULL), |
| 32 is_light_buffer_ready_(false), |
31 is_motion_buffer_ready_(false), | 33 is_motion_buffer_ready_(false), |
32 is_orientation_buffer_ready_(false) { | 34 is_orientation_buffer_ready_(false) { |
33 memset(received_motion_data_, 0, sizeof(received_motion_data_)); | 35 memset(received_motion_data_, 0, sizeof(received_motion_data_)); |
34 device_orientation_.Reset( | 36 device_orientation_.Reset( |
35 Java_DeviceMotionAndOrientation_getInstance( | 37 Java_DeviceMotionAndOrientation_getInstance( |
36 AttachCurrentThread(), | 38 AttachCurrentThread(), |
37 base::android::GetApplicationContext())); | 39 base::android::GetApplicationContext())); |
38 } | 40 } |
39 | 41 |
40 SensorManagerAndroid::~SensorManagerAndroid() { | 42 SensorManagerAndroid::~SensorManagerAndroid() { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 device_motion_buffer_->data.rotationRateGamma = gamma; | 132 device_motion_buffer_->data.rotationRateGamma = gamma; |
131 device_motion_buffer_->data.hasRotationRateGamma = true; | 133 device_motion_buffer_->data.hasRotationRateGamma = true; |
132 device_motion_buffer_->seqlock.WriteEnd(); | 134 device_motion_buffer_->seqlock.WriteEnd(); |
133 | 135 |
134 if (!is_motion_buffer_ready_) { | 136 if (!is_motion_buffer_ready_) { |
135 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; | 137 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; |
136 CheckMotionBufferReadyToRead(); | 138 CheckMotionBufferReadyToRead(); |
137 } | 139 } |
138 } | 140 } |
139 | 141 |
| 142 void SensorManagerAndroid::GotLight( |
| 143 JNIEnv*, jobject, double value) { |
| 144 base::AutoLock autolock(light_buffer_lock_); |
| 145 |
| 146 if (!device_light_buffer_) |
| 147 return; |
| 148 |
| 149 device_light_buffer_->seqlock.WriteBegin(); |
| 150 device_light_buffer_->data = value; |
| 151 |
| 152 device_light_buffer_->seqlock.WriteEnd(); |
| 153 |
| 154 if (!is_light_buffer_ready_) { |
| 155 SetLightBufferReadyStatus(true); |
| 156 } |
| 157 } |
| 158 |
140 bool SensorManagerAndroid::Start(EventType event_type) { | 159 bool SensorManagerAndroid::Start(EventType event_type) { |
141 DCHECK(!device_orientation_.is_null()); | 160 DCHECK(!device_orientation_.is_null()); |
142 return Java_DeviceMotionAndOrientation_start( | 161 return Java_DeviceMotionAndOrientation_start( |
143 AttachCurrentThread(), device_orientation_.obj(), | 162 AttachCurrentThread(), device_orientation_.obj(), |
144 reinterpret_cast<intptr_t>(this), static_cast<jint>(event_type), | 163 reinterpret_cast<intptr_t>(this), static_cast<jint>(event_type), |
145 kInertialSensorIntervalMillis); | 164 kInertialSensorIntervalMillis); |
146 } | 165 } |
147 | 166 |
148 void SensorManagerAndroid::Stop(EventType event_type) { | 167 void SensorManagerAndroid::Stop(EventType event_type) { |
149 DCHECK(!device_orientation_.is_null()); | 168 DCHECK(!device_orientation_.is_null()); |
150 Java_DeviceMotionAndOrientation_stop( | 169 Java_DeviceMotionAndOrientation_stop( |
151 AttachCurrentThread(), device_orientation_.obj(), | 170 AttachCurrentThread(), device_orientation_.obj(), |
152 static_cast<jint>(event_type)); | 171 static_cast<jint>(event_type)); |
153 } | 172 } |
154 | 173 |
155 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { | 174 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { |
156 DCHECK(!device_orientation_.is_null()); | 175 DCHECK(!device_orientation_.is_null()); |
157 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( | 176 return Java_DeviceMotionAndOrientation_getNumberActiveDeviceMotionSensors( |
158 AttachCurrentThread(), device_orientation_.obj()); | 177 AttachCurrentThread(), device_orientation_.obj()); |
159 } | 178 } |
160 | 179 |
161 | 180 |
162 // ----- Shared memory API methods | 181 // ----- Shared memory API methods |
163 | 182 |
| 183 // --- Device Light |
| 184 |
| 185 bool SensorManagerAndroid::StartFetchingDeviceLightData( |
| 186 DeviceLightHardwareBuffer* buffer) { |
| 187 DCHECK(buffer); |
| 188 { |
| 189 base::AutoLock autolock(light_buffer_lock_); |
| 190 device_light_buffer_ = buffer; |
| 191 ClearInternalLightBuffers(); |
| 192 } |
| 193 bool success = Start(kTypeLight); |
| 194 { |
| 195 base::AutoLock autolock(light_buffer_lock_); |
| 196 // If Start() was unsuccessful then set the buffer ready flag to true |
| 197 // to start firing all-null events. |
| 198 SetLightBufferReadyStatus(!success); |
| 199 } |
| 200 return success; |
| 201 } |
| 202 |
| 203 void SensorManagerAndroid::StopFetchingDeviceLightData() { |
| 204 Stop(kTypeLight); |
| 205 { |
| 206 base::AutoLock autolock(light_buffer_lock_); |
| 207 if (device_light_buffer_) { |
| 208 ClearInternalLightBuffers(); |
| 209 device_light_buffer_ = NULL; |
| 210 } |
| 211 } |
| 212 } |
| 213 |
| 214 void SensorManagerAndroid::SetLightBufferReadyStatus(bool ready) { |
| 215 is_light_buffer_ready_ = ready; |
| 216 } |
| 217 |
| 218 void SensorManagerAndroid::ClearInternalLightBuffers() { |
| 219 SetLightBufferReadyStatus(false); |
| 220 } |
| 221 |
164 // --- Device Motion | 222 // --- Device Motion |
165 | 223 |
166 bool SensorManagerAndroid::StartFetchingDeviceMotionData( | 224 bool SensorManagerAndroid::StartFetchingDeviceMotionData( |
167 DeviceMotionHardwareBuffer* buffer) { | 225 DeviceMotionHardwareBuffer* buffer) { |
168 DCHECK(buffer); | 226 DCHECK(buffer); |
169 { | 227 { |
170 base::AutoLock autolock(motion_buffer_lock_); | 228 base::AutoLock autolock(motion_buffer_lock_); |
171 device_motion_buffer_ = buffer; | 229 device_motion_buffer_ = buffer; |
172 ClearInternalMotionBuffers(); | 230 ClearInternalMotionBuffers(); |
173 } | 231 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 { | 325 { |
268 base::AutoLock autolock(orientation_buffer_lock_); | 326 base::AutoLock autolock(orientation_buffer_lock_); |
269 if (device_orientation_buffer_) { | 327 if (device_orientation_buffer_) { |
270 SetOrientationBufferReadyStatus(false); | 328 SetOrientationBufferReadyStatus(false); |
271 device_orientation_buffer_ = NULL; | 329 device_orientation_buffer_ = NULL; |
272 } | 330 } |
273 } | 331 } |
274 } | 332 } |
275 | 333 |
276 } // namespace content | 334 } // namespace content |
OLD | NEW |