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/device_sensors/data_fetcher_shared_memory.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "content/browser/device_sensors/ambient_light_mac.h" | 9 #include "device/device_sensors/ambient_light_mac.h" |
10 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" | 10 #include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 const double kMeanGravity = 9.80665; | 14 const double kMeanGravity = 9.80665; |
15 | 15 |
16 double LMUvalueToLux(uint64_t raw_value) { | 16 double LMUvalueToLux(uint64_t raw_value) { |
17 // Conversion formula from regression. | 17 // Conversion formula from regression. |
18 // https://bugzilla.mozilla.org/show_bug.cgi?id=793728 | 18 // https://bugzilla.mozilla.org/show_bug.cgi?id=793728 |
19 // Let x = raw_value, then | 19 // Let x = raw_value, then |
(...skipping 25 matching lines...) Expand all Loading... |
45 if (!sensor->ReadSensorValue(lux_value)) | 45 if (!sensor->ReadSensorValue(lux_value)) |
46 return; | 46 return; |
47 uint64_t mean = (lux_value[0] + lux_value[1]) / 2; | 47 uint64_t mean = (lux_value[0] + lux_value[1]) / 2; |
48 double lux = LMUvalueToLux(mean); | 48 double lux = LMUvalueToLux(mean); |
49 buffer->seqlock.WriteBegin(); | 49 buffer->seqlock.WriteBegin(); |
50 buffer->data.value = lux; | 50 buffer->data.value = lux; |
51 buffer->seqlock.WriteEnd(); | 51 buffer->seqlock.WriteEnd(); |
52 } | 52 } |
53 | 53 |
54 void FetchMotion(SuddenMotionSensor* sensor, | 54 void FetchMotion(SuddenMotionSensor* sensor, |
55 content::DeviceMotionHardwareBuffer* buffer) { | 55 content::DeviceMotionHardwareBuffer* buffer) { |
56 DCHECK(sensor); | 56 DCHECK(sensor); |
57 DCHECK(buffer); | 57 DCHECK(buffer); |
58 | 58 |
59 float axis_value[3]; | 59 float axis_value[3]; |
60 if (!sensor->ReadSensorValues(axis_value)) | 60 if (!sensor->ReadSensorValues(axis_value)) |
61 return; | 61 return; |
62 | 62 |
63 buffer->seqlock.WriteBegin(); | 63 buffer->seqlock.WriteBegin(); |
64 buffer->data.accelerationIncludingGravityX = axis_value[0] * kMeanGravity; | 64 buffer->data.accelerationIncludingGravityX = axis_value[0] * kMeanGravity; |
65 buffer->data.hasAccelerationIncludingGravityX = true; | 65 buffer->data.hasAccelerationIncludingGravityX = true; |
66 buffer->data.accelerationIncludingGravityY = axis_value[1] * kMeanGravity; | 66 buffer->data.accelerationIncludingGravityY = axis_value[1] * kMeanGravity; |
67 buffer->data.hasAccelerationIncludingGravityY = true; | 67 buffer->data.hasAccelerationIncludingGravityY = true; |
68 buffer->data.accelerationIncludingGravityZ = axis_value[2] * kMeanGravity; | 68 buffer->data.accelerationIncludingGravityZ = axis_value[2] * kMeanGravity; |
69 buffer->data.hasAccelerationIncludingGravityZ = true; | 69 buffer->data.hasAccelerationIncludingGravityZ = true; |
70 buffer->data.allAvailableSensorsAreActive = true; | 70 buffer->data.allAvailableSensorsAreActive = true; |
71 buffer->seqlock.WriteEnd(); | 71 buffer->seqlock.WriteEnd(); |
72 } | 72 } |
73 | 73 |
74 void FetchOrientation(SuddenMotionSensor* sensor, | 74 void FetchOrientation(SuddenMotionSensor* sensor, |
75 content::DeviceOrientationHardwareBuffer* buffer) { | 75 content::DeviceOrientationHardwareBuffer* buffer) { |
76 DCHECK(sensor); | 76 DCHECK(sensor); |
77 DCHECK(buffer); | 77 DCHECK(buffer); |
78 | 78 |
79 // Retrieve per-axis calibrated values. | 79 // Retrieve per-axis calibrated values. |
80 float axis_value[3]; | 80 float axis_value[3]; |
81 if (!sensor->ReadSensorValues(axis_value)) | 81 if (!sensor->ReadSensorValues(axis_value)) |
82 return; | 82 return; |
83 | 83 |
84 // Transform the accelerometer values to W3C draft angles. | 84 // Transform the accelerometer values to W3C draft angles. |
85 // | 85 // |
(...skipping 22 matching lines...) Expand all Loading... |
108 // Make sure that the interval boundaries comply with the specification. At | 108 // Make sure that the interval boundaries comply with the specification. At |
109 // this point, beta is [-180, 180] and gamma is [-90, 90], but the spec has | 109 // this point, beta is [-180, 180] and gamma is [-90, 90], but the spec has |
110 // the upper bound open on both. | 110 // the upper bound open on both. |
111 if (beta == 180.0) | 111 if (beta == 180.0) |
112 beta = -180; // -180 == 180 (upside-down) | 112 beta = -180; // -180 == 180 (upside-down) |
113 if (gamma == 90.0) | 113 if (gamma == 90.0) |
114 gamma = nextafter(90, 0); | 114 gamma = nextafter(90, 0); |
115 | 115 |
116 // At this point, DCHECKing is paranoia. Never hurts. | 116 // At this point, DCHECKing is paranoia. Never hurts. |
117 DCHECK_GE(beta, -180.0); | 117 DCHECK_GE(beta, -180.0); |
118 DCHECK_LT(beta, 180.0); | 118 DCHECK_LT(beta, 180.0); |
119 DCHECK_GE(gamma, -90.0); | 119 DCHECK_GE(gamma, -90.0); |
120 DCHECK_LT(gamma, 90.0); | 120 DCHECK_LT(gamma, 90.0); |
121 | 121 |
122 buffer->seqlock.WriteBegin(); | 122 buffer->seqlock.WriteBegin(); |
123 buffer->data.beta = beta; | 123 buffer->data.beta = beta; |
124 buffer->data.hasBeta = true; | 124 buffer->data.hasBeta = true; |
125 buffer->data.gamma = gamma; | 125 buffer->data.gamma = gamma; |
126 buffer->data.hasGamma = true; | 126 buffer->data.hasGamma = true; |
127 buffer->data.allAvailableSensorsAreActive = true; | 127 buffer->data.allAvailableSensorsAreActive = true; |
128 buffer->seqlock.WriteEnd(); | 128 buffer->seqlock.WriteEnd(); |
129 } | 129 } |
130 | 130 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 162 |
163 switch (consumer_type) { | 163 switch (consumer_type) { |
164 case CONSUMER_TYPE_MOTION: { | 164 case CONSUMER_TYPE_MOTION: { |
165 if (!sudden_motion_sensor_) | 165 if (!sudden_motion_sensor_) |
166 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); | 166 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); |
167 bool sudden_motion_sensor_available = | 167 bool sudden_motion_sensor_available = |
168 sudden_motion_sensor_.get() != nullptr; | 168 sudden_motion_sensor_.get() != nullptr; |
169 | 169 |
170 motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer); | 170 motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer); |
171 UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionMacAvailable", | 171 UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionMacAvailable", |
172 sudden_motion_sensor_available); | 172 sudden_motion_sensor_available); |
173 if (!sudden_motion_sensor_available) { | 173 if (!sudden_motion_sensor_available) { |
174 // No motion sensor available, fire an all-null event. | 174 // No motion sensor available, fire an all-null event. |
175 motion_buffer_->seqlock.WriteBegin(); | 175 motion_buffer_->seqlock.WriteBegin(); |
176 motion_buffer_->data.allAvailableSensorsAreActive = true; | 176 motion_buffer_->data.allAvailableSensorsAreActive = true; |
177 motion_buffer_->seqlock.WriteEnd(); | 177 motion_buffer_->seqlock.WriteEnd(); |
178 } | 178 } |
179 return sudden_motion_sensor_available; | 179 return sudden_motion_sensor_available; |
180 } | 180 } |
181 case CONSUMER_TYPE_ORIENTATION: { | 181 case CONSUMER_TYPE_ORIENTATION: { |
182 if (!sudden_motion_sensor_) | 182 if (!sudden_motion_sensor_) |
183 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); | 183 sudden_motion_sensor_.reset(SuddenMotionSensor::Create()); |
184 bool sudden_motion_sensor_available = | 184 bool sudden_motion_sensor_available = |
185 sudden_motion_sensor_.get() != nullptr; | 185 sudden_motion_sensor_.get() != nullptr; |
186 | 186 |
187 orientation_buffer_ = | 187 orientation_buffer_ = |
188 static_cast<DeviceOrientationHardwareBuffer*>(buffer); | 188 static_cast<DeviceOrientationHardwareBuffer*>(buffer); |
189 UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationMacAvailable", | 189 UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationMacAvailable", |
190 sudden_motion_sensor_available); | 190 sudden_motion_sensor_available); |
191 if (sudden_motion_sensor_available) { | 191 if (sudden_motion_sensor_available) { |
192 // On Mac we cannot provide absolute orientation. | 192 // On Mac we cannot provide absolute orientation. |
193 orientation_buffer_->seqlock.WriteBegin(); | 193 orientation_buffer_->seqlock.WriteBegin(); |
194 orientation_buffer_->data.absolute = false; | 194 orientation_buffer_->data.absolute = false; |
195 orientation_buffer_->data.hasAbsolute = true; | 195 orientation_buffer_->data.hasAbsolute = true; |
196 orientation_buffer_->seqlock.WriteEnd(); | 196 orientation_buffer_->seqlock.WriteEnd(); |
197 } else { | 197 } else { |
198 // No motion sensor available, fire an all-null event. | 198 // No motion sensor available, fire an all-null event. |
199 orientation_buffer_->seqlock.WriteBegin(); | 199 orientation_buffer_->seqlock.WriteBegin(); |
200 orientation_buffer_->data.allAvailableSensorsAreActive = true; | 200 orientation_buffer_->data.allAvailableSensorsAreActive = true; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 light_buffer_ = nullptr; | 250 light_buffer_ = nullptr; |
251 } | 251 } |
252 return true; | 252 return true; |
253 default: | 253 default: |
254 NOTREACHED(); | 254 NOTREACHED(); |
255 } | 255 } |
256 return false; | 256 return false; |
257 } | 257 } |
258 | 258 |
259 } // namespace content | 259 } // namespace content |
OLD | NEW |