Chromium Code Reviews| 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_sensors/sensor_manager_android.h" | 5 #include "content/browser/device_sensors/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_sensors/inertial_sensor_consts.h" | 12 #include "content/browser/device_sensors/inertial_sensor_consts.h" |
| 13 #include "jni/DeviceSensors_jni.h" | 13 #include "jni/DeviceSensors_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_sensors_.Reset(Java_DeviceSensors_getInstance( |
| 35 Java_DeviceSensors_getInstance( | 37 AttachCurrentThread(), base::android::GetApplicationContext())); |
| 36 AttachCurrentThread(), | |
| 37 base::android::GetApplicationContext())); | |
| 38 } | 38 } |
| 39 | 39 |
| 40 SensorManagerAndroid::~SensorManagerAndroid() { | 40 SensorManagerAndroid::~SensorManagerAndroid() { |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool SensorManagerAndroid::Register(JNIEnv* env) { | 43 bool SensorManagerAndroid::Register(JNIEnv* env) { |
| 44 return RegisterNativesImpl(env); | 44 return RegisterNativesImpl(env); |
| 45 } | 45 } |
| 46 | 46 |
| 47 SensorManagerAndroid* SensorManagerAndroid::GetInstance() { | 47 SensorManagerAndroid* SensorManagerAndroid::GetInstance() { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 device_motion_buffer_->data.rotationRateGamma = gamma; | 130 device_motion_buffer_->data.rotationRateGamma = gamma; |
| 131 device_motion_buffer_->data.hasRotationRateGamma = true; | 131 device_motion_buffer_->data.hasRotationRateGamma = true; |
| 132 device_motion_buffer_->seqlock.WriteEnd(); | 132 device_motion_buffer_->seqlock.WriteEnd(); |
| 133 | 133 |
| 134 if (!is_motion_buffer_ready_) { | 134 if (!is_motion_buffer_ready_) { |
| 135 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; | 135 received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] = 1; |
| 136 CheckMotionBufferReadyToRead(); | 136 CheckMotionBufferReadyToRead(); |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 | 139 |
| 140 void SensorManagerAndroid::GotLight(JNIEnv*, jobject, double value) { | |
| 141 base::AutoLock autolock(light_buffer_lock_); | |
| 142 | |
| 143 if (!device_light_buffer_) | |
| 144 return; | |
| 145 | |
| 146 device_light_buffer_->seqlock.WriteBegin(); | |
| 147 device_light_buffer_->data.value = value; | |
| 148 device_light_buffer_->seqlock.WriteEnd(); | |
| 149 } | |
| 150 | |
| 140 bool SensorManagerAndroid::Start(EventType event_type) { | 151 bool SensorManagerAndroid::Start(EventType event_type) { |
| 141 DCHECK(!device_orientation_.is_null()); | 152 DCHECK(!device_sensors_.is_null()); |
| 142 return Java_DeviceSensors_start( | 153 int rate_in_milliseconds = (event_type == kTypeLight) |
| 143 AttachCurrentThread(), device_orientation_.obj(), | 154 ? kInertialLightSensorIntervalMillis |
| 144 reinterpret_cast<intptr_t>(this), static_cast<jint>(event_type), | 155 : kInertialSensorIntervalMillis; |
| 145 kInertialSensorIntervalMillis); | 156 return Java_DeviceSensors_start(AttachCurrentThread(), |
| 157 device_sensors_.obj(), | |
| 158 reinterpret_cast<intptr_t>(this), | |
| 159 static_cast<jint>(event_type), | |
| 160 rate_in_milliseconds); | |
| 146 } | 161 } |
| 147 | 162 |
| 148 void SensorManagerAndroid::Stop(EventType event_type) { | 163 void SensorManagerAndroid::Stop(EventType event_type) { |
| 149 DCHECK(!device_orientation_.is_null()); | 164 DCHECK(!device_sensors_.is_null()); |
| 150 Java_DeviceSensors_stop( | 165 Java_DeviceSensors_stop(AttachCurrentThread(), |
| 151 AttachCurrentThread(), device_orientation_.obj(), | 166 device_sensors_.obj(), |
| 152 static_cast<jint>(event_type)); | 167 static_cast<jint>(event_type)); |
| 153 } | 168 } |
| 154 | 169 |
| 155 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { | 170 int SensorManagerAndroid::GetNumberActiveDeviceMotionSensors() { |
| 156 DCHECK(!device_orientation_.is_null()); | 171 DCHECK(!device_sensors_.is_null()); |
| 157 return Java_DeviceSensors_getNumberActiveDeviceMotionSensors( | 172 return Java_DeviceSensors_getNumberActiveDeviceMotionSensors( |
| 158 AttachCurrentThread(), device_orientation_.obj()); | 173 AttachCurrentThread(), device_sensors_.obj()); |
| 159 } | 174 } |
| 160 | 175 |
| 161 | 176 |
| 162 // ----- Shared memory API methods | 177 // ----- Shared memory API methods |
| 163 | 178 |
| 179 // --- Device Light | |
| 180 | |
| 181 bool SensorManagerAndroid::StartFetchingDeviceLightData( | |
| 182 DeviceLightHardwareBuffer* buffer) { | |
| 183 DCHECK(buffer); | |
| 184 { | |
| 185 base::AutoLock autolock(light_buffer_lock_); | |
| 186 device_light_buffer_ = buffer; | |
| 187 ClearInternalLightBuffers(); | |
| 188 } | |
| 189 bool success = Start(kTypeLight); | |
|
timvolodine
2014/09/04 17:16:57
should the buffer be set to infinity when !success
riju_
2014/09/08 09:26:17
Done.
| |
| 190 return success; | |
| 191 } | |
| 192 | |
| 193 void SensorManagerAndroid::StopFetchingDeviceLightData() { | |
| 194 Stop(kTypeLight); | |
| 195 { | |
| 196 base::AutoLock autolock(light_buffer_lock_); | |
| 197 if (device_light_buffer_) { | |
| 198 ClearInternalLightBuffers(); | |
| 199 device_light_buffer_ = NULL; | |
| 200 } | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 void SensorManagerAndroid::ClearInternalLightBuffers() { | |
| 205 device_light_buffer_->data.value = -1; | |
|
timvolodine
2014/09/04 17:16:57
wrap with writeBegin writeEnd.
riju_
2014/09/08 09:26:17
Done.
| |
| 206 } | |
| 164 // --- Device Motion | 207 // --- Device Motion |
| 165 | 208 |
| 166 bool SensorManagerAndroid::StartFetchingDeviceMotionData( | 209 bool SensorManagerAndroid::StartFetchingDeviceMotionData( |
| 167 DeviceMotionHardwareBuffer* buffer) { | 210 DeviceMotionHardwareBuffer* buffer) { |
| 168 DCHECK(buffer); | 211 DCHECK(buffer); |
| 169 { | 212 { |
| 170 base::AutoLock autolock(motion_buffer_lock_); | 213 base::AutoLock autolock(motion_buffer_lock_); |
| 171 device_motion_buffer_ = buffer; | 214 device_motion_buffer_ = buffer; |
| 172 ClearInternalMotionBuffers(); | 215 ClearInternalMotionBuffers(); |
| 173 } | 216 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 { | 310 { |
| 268 base::AutoLock autolock(orientation_buffer_lock_); | 311 base::AutoLock autolock(orientation_buffer_lock_); |
| 269 if (device_orientation_buffer_) { | 312 if (device_orientation_buffer_) { |
| 270 SetOrientationBufferReadyStatus(false); | 313 SetOrientationBufferReadyStatus(false); |
| 271 device_orientation_buffer_ = NULL; | 314 device_orientation_buffer_ = NULL; |
| 272 } | 315 } |
| 273 } | 316 } |
| 274 } | 317 } |
| 275 | 318 |
| 276 } // namespace content | 319 } // namespace content |
| OLD | NEW |