1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "data_fetcher_shared_memory.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "third_party/sudden_motion_sensor/sudden_motion_sensor_mac.h"
namespace {
const double kMeanGravity = 9.80665;
void FetchMotion(SuddenMotionSensor* sensor,
content::DeviceMotionHardwareBuffer* buffer) {
DCHECK(buffer);
float axis_value[3];
if (!sensor->ReadSensorValues(axis_value))
return;
buffer->seqlock.WriteBegin();
buffer->data.accelerationIncludingGravityX = axis_value[0] * kMeanGravity;
buffer->data.hasAccelerationIncludingGravityX = true;
buffer->data.accelerationIncludingGravityY = axis_value[1] * kMeanGravity;
buffer->data.hasAccelerationIncludingGravityY = true;
buffer->data.accelerationIncludingGravityZ = axis_value[2] * kMeanGravity;
buffer->data.hasAccelerationIncludingGravityZ = true;
buffer->data.allAvailableSensorsAreActive = true;
buffer->seqlock.WriteEnd();
}
void FetchOrientation(SuddenMotionSensor* sensor,
content::DeviceOrientationHardwareBuffer* buffer) {
DCHECK(buffer);
// Retrieve per-axis calibrated values.
float axis_value[3];
if (!sensor->ReadSensorValues(axis_value))
return;
// Transform the accelerometer values to W3C draft angles.
//
// Accelerometer values are just dot products of the sensor axes
// by the gravity vector 'g' with the result for the z axis inverted.
//
// To understand this transformation calculate the 3rd row of the z-x-y
// Euler angles rotation matrix (because of the 'g' vector, only 3rd row
// affects to the result). Note that z-x-y matrix means R = Ry * Rx * Rz.
// Then, assume alpha = 0 and you get this:
//
// x_acc = sin(gamma)
// y_acc = - cos(gamma) * sin(beta)
// z_acc = cos(beta) * cos(gamma)
//
// After that the rest is just a bit of trigonometry.
//
// Also note that alpha can't be provided but it's assumed to be always zero.
// This is necessary in order to provide enough information to solve
// the equations.
//
const double kRad2deg = 180.0 / M_PI;
double beta = kRad2deg * atan2(-axis_value[1], axis_value[2]);
double gamma = kRad2deg * asin(axis_value[0]);
// Make sure that the interval boundaries comply with the specification. At
// this point, beta is [-180, 180] and gamma is [-90, 90], but the spec has
// the upper bound open on both.
if (beta == 180.0)
beta = -180; // -180 == 180 (upside-down)
if (gamma == 90.0)
gamma = nextafter(90, 0);
// At this point, DCHECKing is paranoia. Never hurts.
DCHECK_GE(beta, -180.0);
DCHECK_LT(beta, 180.0);
DCHECK_GE(gamma, -90.0);
DCHECK_LT(gamma, 90.0);
buffer->seqlock.WriteBegin();
buffer->data.beta = beta;
buffer->data.hasBeta = true;
buffer->data.gamma = gamma;
buffer->data.hasGamma = true;
buffer->data.allAvailableSensorsAreActive = true;
buffer->seqlock.WriteEnd();
}
} // namespace
namespace content {
DataFetcherSharedMemory::DataFetcherSharedMemory() {
}
DataFetcherSharedMemory::~DataFetcherSharedMemory() {
}
void DataFetcherSharedMemory::Fetch(unsigned consumer_bitmask) {
DCHECK(base::MessageLoop::current() == GetPollingMessageLoop());
DCHECK(sudden_motion_sensor_);
DCHECK(consumer_bitmask & CONSUMER_TYPE_ORIENTATION ||
consumer_bitmask & CONSUMER_TYPE_MOTION);
if (consumer_bitmask & CONSUMER_TYPE_ORIENTATION)
FetchOrientation(sudden_motion_sensor_.get(), orientation_buffer_);
if (consumer_bitmask & CONSUMER_TYPE_MOTION)
FetchMotion(sudden_motion_sensor_.get(), motion_buffer_);
}
DataFetcherSharedMemory::FetcherType DataFetcherSharedMemory::GetType() const {
return FETCHER_TYPE_POLLING_CALLBACK;
}
bool DataFetcherSharedMemory::Start(ConsumerType consumer_type, void* buffer) {
DCHECK(base::MessageLoop::current() == GetPollingMessageLoop());
DCHECK(buffer);
if (!sudden_motion_sensor_)
sudden_motion_sensor_.reset(SuddenMotionSensor::Create());
bool sudden_motion_sensor_available = sudden_motion_sensor_.get() != NULL;
switch (consumer_type) {
case CONSUMER_TYPE_MOTION:
motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer);
UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionMacAvailable",
sudden_motion_sensor_available);
if (!sudden_motion_sensor_available) {
// No motion sensor available, fire an all-null event.
motion_buffer_->seqlock.WriteBegin();
motion_buffer_->data.allAvailableSensorsAreActive = true;
motion_buffer_->seqlock.WriteEnd();
}
return sudden_motion_sensor_available;
case CONSUMER_TYPE_ORIENTATION:
orientation_buffer_ =
static_cast<DeviceOrientationHardwareBuffer*>(buffer);
UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationMacAvailable",
sudden_motion_sensor_available);
if (sudden_motion_sensor_available) {
// On Mac we cannot provide absolute orientation.
orientation_buffer_->seqlock.WriteBegin();
orientation_buffer_->data.absolute = false;
orientation_buffer_->data.hasAbsolute = true;
orientation_buffer_->seqlock.WriteEnd();
} else {
// No motion sensor available, fire an all-null event.
orientation_buffer_->seqlock.WriteBegin();
orientation_buffer_->data.allAvailableSensorsAreActive = true;
orientation_buffer_->seqlock.WriteEnd();
}
return sudden_motion_sensor_available;
default:
NOTREACHED();
}
return false;
}
bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) {
DCHECK(base::MessageLoop::current() == GetPollingMessageLoop());
switch (consumer_type) {
case CONSUMER_TYPE_MOTION:
if (motion_buffer_) {
motion_buffer_->seqlock.WriteBegin();
motion_buffer_->data.allAvailableSensorsAreActive = false;
motion_buffer_->seqlock.WriteEnd();
motion_buffer_ = NULL;
}
return true;
case CONSUMER_TYPE_ORIENTATION:
if (orientation_buffer_) {
orientation_buffer_->seqlock.WriteBegin();
orientation_buffer_->data.allAvailableSensorsAreActive = false;
orientation_buffer_->seqlock.WriteEnd();
orientation_buffer_ = NULL;
}
return true;
default:
NOTREACHED();
}
return false;
}
} // namespace content
|