| 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/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 "content/browser/device_sensors/ambient_light_mac.h" | 12 #include "device/sensors/ambient_light_mac.h" |
| 13 #include "device/sensors/public/cpp/device_util_mac.h" | 13 #include "device/sensors/public/cpp/device_util_mac.h" |
| 14 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" | 14 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const double kMeanGravity = 9.80665; | 18 const double kMeanGravity = 9.80665; |
| 19 | 19 |
| 20 void FetchLight(content::AmbientLightSensor* sensor, | 20 void FetchLight(device::AmbientLightSensor* sensor, |
| 21 content::DeviceLightHardwareBuffer* buffer) { | 21 device::DeviceLightHardwareBuffer* buffer) { |
| 22 DCHECK(sensor); | 22 DCHECK(sensor); |
| 23 DCHECK(buffer); | 23 DCHECK(buffer); |
| 24 // Macbook pro has 2 lux values, left and right, we take the average. | 24 // Macbook pro has 2 lux values, left and right, we take the average. |
| 25 // The raw sensor values are converted to lux using LMUvalueToLux(raw_value) | 25 // The raw sensor values are converted to lux using LMUvalueToLux(raw_value) |
| 26 // similar to how it is done in Firefox. | 26 // similar to how it is done in Firefox. |
| 27 uint64_t lux_value[2]; | 27 uint64_t lux_value[2]; |
| 28 if (!sensor->ReadSensorValue(lux_value)) | 28 if (!sensor->ReadSensorValue(lux_value)) |
| 29 return; | 29 return; |
| 30 uint64_t mean = (lux_value[0] + lux_value[1]) / 2; | 30 uint64_t mean = (lux_value[0] + lux_value[1]) / 2; |
| 31 double lux = device::LMUvalueToLux(mean); | 31 double lux = device::LMUvalueToLux(mean); |
| 32 buffer->seqlock.WriteBegin(); | 32 buffer->seqlock.WriteBegin(); |
| 33 buffer->data.value = lux; | 33 buffer->data.value = lux; |
| 34 buffer->seqlock.WriteEnd(); | 34 buffer->seqlock.WriteEnd(); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void FetchMotion(SuddenMotionSensor* sensor, | 37 void FetchMotion(SuddenMotionSensor* sensor, |
| 38 content::DeviceMotionHardwareBuffer* buffer) { | 38 device::DeviceMotionHardwareBuffer* buffer) { |
| 39 DCHECK(sensor); | 39 DCHECK(sensor); |
| 40 DCHECK(buffer); | 40 DCHECK(buffer); |
| 41 | 41 |
| 42 float axis_value[3]; | 42 float axis_value[3]; |
| 43 if (!sensor->ReadSensorValues(axis_value)) | 43 if (!sensor->ReadSensorValues(axis_value)) |
| 44 return; | 44 return; |
| 45 | 45 |
| 46 buffer->seqlock.WriteBegin(); | 46 buffer->seqlock.WriteBegin(); |
| 47 buffer->data.accelerationIncludingGravityX = axis_value[0] * kMeanGravity; | 47 buffer->data.accelerationIncludingGravityX = axis_value[0] * kMeanGravity; |
| 48 buffer->data.hasAccelerationIncludingGravityX = true; | 48 buffer->data.hasAccelerationIncludingGravityX = true; |
| 49 buffer->data.accelerationIncludingGravityY = axis_value[1] * kMeanGravity; | 49 buffer->data.accelerationIncludingGravityY = axis_value[1] * kMeanGravity; |
| 50 buffer->data.hasAccelerationIncludingGravityY = true; | 50 buffer->data.hasAccelerationIncludingGravityY = true; |
| 51 buffer->data.accelerationIncludingGravityZ = axis_value[2] * kMeanGravity; | 51 buffer->data.accelerationIncludingGravityZ = axis_value[2] * kMeanGravity; |
| 52 buffer->data.hasAccelerationIncludingGravityZ = true; | 52 buffer->data.hasAccelerationIncludingGravityZ = true; |
| 53 buffer->data.allAvailableSensorsAreActive = true; | 53 buffer->data.allAvailableSensorsAreActive = true; |
| 54 buffer->seqlock.WriteEnd(); | 54 buffer->seqlock.WriteEnd(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void FetchOrientation(SuddenMotionSensor* sensor, | 57 void FetchOrientation(SuddenMotionSensor* sensor, |
| 58 content::DeviceOrientationHardwareBuffer* buffer) { | 58 device::DeviceOrientationHardwareBuffer* buffer) { |
| 59 DCHECK(sensor); | 59 DCHECK(sensor); |
| 60 DCHECK(buffer); | 60 DCHECK(buffer); |
| 61 | 61 |
| 62 // Retrieve per-axis calibrated values. | 62 // Retrieve per-axis calibrated values. |
| 63 float axis_value[3]; | 63 float axis_value[3]; |
| 64 if (!sensor->ReadSensorValues(axis_value)) | 64 if (!sensor->ReadSensorValues(axis_value)) |
| 65 return; | 65 return; |
| 66 | 66 |
| 67 // Transform the accelerometer values to W3C draft angles. | 67 // Transform the accelerometer values to W3C draft angles. |
| 68 // | 68 // |
| (...skipping 22 matching lines...) Expand all Loading... |
| 91 // Make sure that the interval boundaries comply with the specification. At | 91 // Make sure that the interval boundaries comply with the specification. At |
| 92 // this point, beta is [-180, 180] and gamma is [-90, 90], but the spec has | 92 // this point, beta is [-180, 180] and gamma is [-90, 90], but the spec has |
| 93 // the upper bound open on both. | 93 // the upper bound open on both. |
| 94 if (beta == 180.0) | 94 if (beta == 180.0) |
| 95 beta = -180; // -180 == 180 (upside-down) | 95 beta = -180; // -180 == 180 (upside-down) |
| 96 if (gamma == 90.0) | 96 if (gamma == 90.0) |
| 97 gamma = nextafter(90, 0); | 97 gamma = nextafter(90, 0); |
| 98 | 98 |
| 99 // At this point, DCHECKing is paranoia. Never hurts. | 99 // At this point, DCHECKing is paranoia. Never hurts. |
| 100 DCHECK_GE(beta, -180.0); | 100 DCHECK_GE(beta, -180.0); |
| 101 DCHECK_LT(beta, 180.0); | 101 DCHECK_LT(beta, 180.0); |
| 102 DCHECK_GE(gamma, -90.0); | 102 DCHECK_GE(gamma, -90.0); |
| 103 DCHECK_LT(gamma, 90.0); | 103 DCHECK_LT(gamma, 90.0); |
| 104 | 104 |
| 105 buffer->seqlock.WriteBegin(); | 105 buffer->seqlock.WriteBegin(); |
| 106 buffer->data.beta = beta; | 106 buffer->data.beta = beta; |
| 107 buffer->data.hasBeta = true; | 107 buffer->data.hasBeta = true; |
| 108 buffer->data.gamma = gamma; | 108 buffer->data.gamma = gamma; |
| 109 buffer->data.hasGamma = true; | 109 buffer->data.hasGamma = true; |
| 110 buffer->data.allAvailableSensorsAreActive = true; | 110 buffer->data.allAvailableSensorsAreActive = true; |
| 111 buffer->seqlock.WriteEnd(); | 111 buffer->seqlock.WriteEnd(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 } // namespace | 114 } // namespace |
| 115 | 115 |
| 116 namespace content { | 116 namespace device { |
| 117 | 117 |
| 118 DataFetcherSharedMemory::DataFetcherSharedMemory() { | 118 DataFetcherSharedMemory::DataFetcherSharedMemory() {} |
| 119 } | |
| 120 | 119 |
| 121 DataFetcherSharedMemory::~DataFetcherSharedMemory() { | 120 DataFetcherSharedMemory::~DataFetcherSharedMemory() {} |
| 122 } | |
| 123 | 121 |
| 124 void DataFetcherSharedMemory::Fetch(unsigned consumer_bitmask) { | 122 void DataFetcherSharedMemory::Fetch(unsigned consumer_bitmask) { |
| 125 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); | 123 DCHECK(GetPollingMessageLoop()->task_runner()->BelongsToCurrentThread()); |
| 126 DCHECK(consumer_bitmask & CONSUMER_TYPE_ORIENTATION || | 124 DCHECK(consumer_bitmask & CONSUMER_TYPE_ORIENTATION || |
| 127 consumer_bitmask & CONSUMER_TYPE_MOTION || | 125 consumer_bitmask & CONSUMER_TYPE_MOTION || |
| 128 consumer_bitmask & CONSUMER_TYPE_LIGHT); | 126 consumer_bitmask & CONSUMER_TYPE_LIGHT); |
| 129 | 127 |
| 130 if (consumer_bitmask & CONSUMER_TYPE_ORIENTATION) | 128 if (consumer_bitmask & CONSUMER_TYPE_ORIENTATION) |
| 131 FetchOrientation(sudden_motion_sensor_.get(), orientation_buffer_); | 129 FetchOrientation(sudden_motion_sensor_.get(), orientation_buffer_); |
| 132 if (consumer_bitmask & CONSUMER_TYPE_MOTION) | 130 if (consumer_bitmask & CONSUMER_TYPE_MOTION) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 145 | 143 |
| 146 switch (consumer_type) { | 144 switch (consumer_type) { |
| 147 case CONSUMER_TYPE_MOTION: { | 145 case CONSUMER_TYPE_MOTION: { |
| 148 if (!sudden_motion_sensor_) | 146 if (!sudden_motion_sensor_) |
| 149 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); | 147 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); |
| 150 bool sudden_motion_sensor_available = | 148 bool sudden_motion_sensor_available = |
| 151 sudden_motion_sensor_.get() != nullptr; | 149 sudden_motion_sensor_.get() != nullptr; |
| 152 | 150 |
| 153 motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer); | 151 motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer); |
| 154 UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionMacAvailable", | 152 UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionMacAvailable", |
| 155 sudden_motion_sensor_available); | 153 sudden_motion_sensor_available); |
| 156 if (!sudden_motion_sensor_available) { | 154 if (!sudden_motion_sensor_available) { |
| 157 // No motion sensor available, fire an all-null event. | 155 // No motion sensor available, fire an all-null event. |
| 158 motion_buffer_->seqlock.WriteBegin(); | 156 motion_buffer_->seqlock.WriteBegin(); |
| 159 motion_buffer_->data.allAvailableSensorsAreActive = true; | 157 motion_buffer_->data.allAvailableSensorsAreActive = true; |
| 160 motion_buffer_->seqlock.WriteEnd(); | 158 motion_buffer_->seqlock.WriteEnd(); |
| 161 } | 159 } |
| 162 return sudden_motion_sensor_available; | 160 return sudden_motion_sensor_available; |
| 163 } | 161 } |
| 164 case CONSUMER_TYPE_ORIENTATION: { | 162 case CONSUMER_TYPE_ORIENTATION: { |
| 165 if (!sudden_motion_sensor_) | 163 if (!sudden_motion_sensor_) |
| 166 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); | 164 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); |
| 167 bool sudden_motion_sensor_available = | 165 bool sudden_motion_sensor_available = |
| 168 sudden_motion_sensor_.get() != nullptr; | 166 sudden_motion_sensor_.get() != nullptr; |
| 169 | 167 |
| 170 orientation_buffer_ = | 168 orientation_buffer_ = |
| 171 static_cast<DeviceOrientationHardwareBuffer*>(buffer); | 169 static_cast<DeviceOrientationHardwareBuffer*>(buffer); |
| 172 UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationMacAvailable", | 170 UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationMacAvailable", |
| 173 sudden_motion_sensor_available); | 171 sudden_motion_sensor_available); |
| 174 if (sudden_motion_sensor_available) { | 172 if (sudden_motion_sensor_available) { |
| 175 // On Mac we cannot provide absolute orientation. | 173 // On Mac we cannot provide absolute orientation. |
| 176 orientation_buffer_->seqlock.WriteBegin(); | 174 orientation_buffer_->seqlock.WriteBegin(); |
| 177 orientation_buffer_->data.absolute = false; | 175 orientation_buffer_->data.absolute = false; |
| 178 orientation_buffer_->seqlock.WriteEnd(); | 176 orientation_buffer_->seqlock.WriteEnd(); |
| 179 } else { | 177 } else { |
| 180 // No motion sensor available, fire an all-null event. | 178 // No motion sensor available, fire an all-null event. |
| 181 orientation_buffer_->seqlock.WriteBegin(); | 179 orientation_buffer_->seqlock.WriteBegin(); |
| 182 orientation_buffer_->data.allAvailableSensorsAreActive = true; | 180 orientation_buffer_->data.allAvailableSensorsAreActive = true; |
| 183 orientation_buffer_->seqlock.WriteEnd(); | 181 orientation_buffer_->seqlock.WriteEnd(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 light_buffer_->seqlock.WriteEnd(); | 248 light_buffer_->seqlock.WriteEnd(); |
| 251 light_buffer_ = nullptr; | 249 light_buffer_ = nullptr; |
| 252 } | 250 } |
| 253 return true; | 251 return true; |
| 254 default: | 252 default: |
| 255 NOTREACHED(); | 253 NOTREACHED(); |
| 256 } | 254 } |
| 257 return false; | 255 return false; |
| 258 } | 256 } |
| 259 | 257 |
| 260 } // namespace content | 258 } // namespace device |
| OLD | NEW |