This source file includes following definitions.
- FetchMotion
- FetchOrientation
- GetType
- Start
- Stop
#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);
float axis_value[3];
if (!sensor->ReadSensorValues(axis_value))
return;
const double kRad2deg = 180.0 / M_PI;
double beta = kRad2deg * atan2(-axis_value[1], axis_value[2]);
double gamma = kRad2deg * asin(axis_value[0]);
if (beta == 180.0)
beta = -180;
if (gamma == 90.0)
gamma = nextafter(90, 0);
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 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) {
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) {
orientation_buffer_->seqlock.WriteBegin();
orientation_buffer_->data.absolute = false;
orientation_buffer_->data.hasAbsolute = true;
orientation_buffer_->seqlock.WriteEnd();
} else {
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;
}
}