| 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 "device/sensors/data_fetcher_shared_memory.h" | 5 #include "device/sensors/data_fetcher_shared_memory.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "device/sensors/ambient_light_mac.h" | |
| 13 #include "device/sensors/public/cpp/device_util_mac.h" | |
| 14 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" | 12 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" |
| 15 | 13 |
| 16 namespace device { | 14 namespace device { |
| 17 | 15 |
| 18 const double kMeanGravity = 9.80665; | 16 const double kMeanGravity = 9.80665; |
| 19 | 17 |
| 20 void FetchLight(AmbientLightSensor* sensor, DeviceLightHardwareBuffer* buffer) { | |
| 21 DCHECK(sensor); | |
| 22 DCHECK(buffer); | |
| 23 // Macbook pro has 2 lux values, left and right, we take the average. | |
| 24 // The raw sensor values are converted to lux using LMUvalueToLux(raw_value) | |
| 25 // similar to how it is done in Firefox. | |
| 26 uint64_t lux_value[2]; | |
| 27 if (!sensor->ReadSensorValue(lux_value)) | |
| 28 return; | |
| 29 uint64_t mean = (lux_value[0] + lux_value[1]) / 2; | |
| 30 double lux = LMUvalueToLux(mean); | |
| 31 buffer->seqlock.WriteBegin(); | |
| 32 buffer->data.value = lux; | |
| 33 buffer->seqlock.WriteEnd(); | |
| 34 } | |
| 35 | |
| 36 void FetchMotion(SuddenMotionSensor* sensor, | 18 void FetchMotion(SuddenMotionSensor* sensor, |
| 37 DeviceMotionHardwareBuffer* buffer) { | 19 DeviceMotionHardwareBuffer* buffer) { |
| 38 DCHECK(sensor); | 20 DCHECK(sensor); |
| 39 DCHECK(buffer); | 21 DCHECK(buffer); |
| 40 | 22 |
| 41 float axis_value[3]; | 23 float axis_value[3]; |
| 42 if (!sensor->ReadSensorValues(axis_value)) | 24 if (!sensor->ReadSensorValues(axis_value)) |
| 43 return; | 25 return; |
| 44 | 26 |
| 45 buffer->seqlock.WriteBegin(); | 27 buffer->seqlock.WriteBegin(); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 buffer->seqlock.WriteEnd(); | 92 buffer->seqlock.WriteEnd(); |
| 111 } | 93 } |
| 112 | 94 |
| 113 DataFetcherSharedMemory::DataFetcherSharedMemory() {} | 95 DataFetcherSharedMemory::DataFetcherSharedMemory() {} |
| 114 | 96 |
| 115 DataFetcherSharedMemory::~DataFetcherSharedMemory() {} | 97 DataFetcherSharedMemory::~DataFetcherSharedMemory() {} |
| 116 | 98 |
| 117 void DataFetcherSharedMemory::Fetch(unsigned consumer_bitmask) { | 99 void DataFetcherSharedMemory::Fetch(unsigned consumer_bitmask) { |
| 118 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); | 100 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); |
| 119 DCHECK(consumer_bitmask & CONSUMER_TYPE_ORIENTATION || | 101 DCHECK(consumer_bitmask & CONSUMER_TYPE_ORIENTATION || |
| 120 consumer_bitmask & CONSUMER_TYPE_MOTION || | 102 consumer_bitmask & CONSUMER_TYPE_MOTION); |
| 121 consumer_bitmask & CONSUMER_TYPE_LIGHT); | |
| 122 | 103 |
| 123 if (consumer_bitmask & CONSUMER_TYPE_ORIENTATION) | 104 if (consumer_bitmask & CONSUMER_TYPE_ORIENTATION) |
| 124 FetchOrientation(sudden_motion_sensor_.get(), orientation_buffer_); | 105 FetchOrientation(sudden_motion_sensor_.get(), orientation_buffer_); |
| 125 if (consumer_bitmask & CONSUMER_TYPE_MOTION) | 106 if (consumer_bitmask & CONSUMER_TYPE_MOTION) |
| 126 FetchMotion(sudden_motion_sensor_.get(), motion_buffer_); | 107 FetchMotion(sudden_motion_sensor_.get(), motion_buffer_); |
| 127 if (consumer_bitmask & CONSUMER_TYPE_LIGHT) | |
| 128 FetchLight(ambient_light_sensor_.get(), light_buffer_); | |
| 129 } | 108 } |
| 130 | 109 |
| 131 DataFetcherSharedMemory::FetcherType DataFetcherSharedMemory::GetType() const { | 110 DataFetcherSharedMemory::FetcherType DataFetcherSharedMemory::GetType() const { |
| 132 return FETCHER_TYPE_POLLING_CALLBACK; | 111 return FETCHER_TYPE_POLLING_CALLBACK; |
| 133 } | 112 } |
| 134 | 113 |
| 135 bool DataFetcherSharedMemory::Start(ConsumerType consumer_type, void* buffer) { | 114 bool DataFetcherSharedMemory::Start(ConsumerType consumer_type, void* buffer) { |
| 136 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); | 115 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); |
| 137 DCHECK(buffer); | 116 DCHECK(buffer); |
| 138 | 117 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 static_cast<DeviceOrientationHardwareBuffer*>(buffer); | 161 static_cast<DeviceOrientationHardwareBuffer*>(buffer); |
| 183 // Absolute device orientation not available on Mac, let the | 162 // Absolute device orientation not available on Mac, let the |
| 184 // implementation fire an all-null event to signal this to blink. | 163 // implementation fire an all-null event to signal this to blink. |
| 185 orientation_absolute_buffer_->seqlock.WriteBegin(); | 164 orientation_absolute_buffer_->seqlock.WriteBegin(); |
| 186 orientation_absolute_buffer_->data.absolute = true; | 165 orientation_absolute_buffer_->data.absolute = true; |
| 187 orientation_absolute_buffer_->data.all_available_sensors_are_active = | 166 orientation_absolute_buffer_->data.all_available_sensors_are_active = |
| 188 true; | 167 true; |
| 189 orientation_absolute_buffer_->seqlock.WriteEnd(); | 168 orientation_absolute_buffer_->seqlock.WriteEnd(); |
| 190 return false; | 169 return false; |
| 191 } | 170 } |
| 192 case CONSUMER_TYPE_LIGHT: { | |
| 193 if (!ambient_light_sensor_) | |
| 194 ambient_light_sensor_ = AmbientLightSensor::Create(); | |
| 195 bool ambient_light_sensor_available = | |
| 196 ambient_light_sensor_.get() != nullptr; | |
| 197 | |
| 198 light_buffer_ = static_cast<DeviceLightHardwareBuffer*>(buffer); | |
| 199 if (!ambient_light_sensor_available) { | |
| 200 light_buffer_->seqlock.WriteBegin(); | |
| 201 light_buffer_->data.value = std::numeric_limits<double>::infinity(); | |
| 202 light_buffer_->seqlock.WriteEnd(); | |
| 203 } | |
| 204 return ambient_light_sensor_available; | |
| 205 } | |
| 206 default: | 171 default: |
| 207 NOTREACHED(); | 172 NOTREACHED(); |
| 208 } | 173 } |
| 209 return false; | 174 return false; |
| 210 } | 175 } |
| 211 | 176 |
| 212 bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) { | 177 bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) { |
| 213 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); | 178 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); |
| 214 | 179 |
| 215 switch (consumer_type) { | 180 switch (consumer_type) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 231 return true; | 196 return true; |
| 232 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | 197 case CONSUMER_TYPE_ORIENTATION_ABSOLUTE: |
| 233 if (orientation_absolute_buffer_) { | 198 if (orientation_absolute_buffer_) { |
| 234 orientation_absolute_buffer_->seqlock.WriteBegin(); | 199 orientation_absolute_buffer_->seqlock.WriteBegin(); |
| 235 orientation_absolute_buffer_->data.all_available_sensors_are_active = | 200 orientation_absolute_buffer_->data.all_available_sensors_are_active = |
| 236 false; | 201 false; |
| 237 orientation_absolute_buffer_->seqlock.WriteEnd(); | 202 orientation_absolute_buffer_->seqlock.WriteEnd(); |
| 238 orientation_absolute_buffer_ = nullptr; | 203 orientation_absolute_buffer_ = nullptr; |
| 239 } | 204 } |
| 240 return true; | 205 return true; |
| 241 case CONSUMER_TYPE_LIGHT: | |
| 242 if (light_buffer_) { | |
| 243 light_buffer_->seqlock.WriteBegin(); | |
| 244 light_buffer_->data.value = -1; | |
| 245 light_buffer_->seqlock.WriteEnd(); | |
| 246 light_buffer_ = nullptr; | |
| 247 } | |
| 248 return true; | |
| 249 default: | 206 default: |
| 250 NOTREACHED(); | 207 NOTREACHED(); |
| 251 } | 208 } |
| 252 return false; | 209 return false; |
| 253 } | 210 } |
| 254 | 211 |
| 255 } // namespace device | 212 } // namespace device |
| OLD | NEW |