Commit f56f6e2e authored by Jun Cai's avatar Jun Cai Committed by Commit Bot

Add SensorReadingSharedBufferReader class to read sensor data

This CL adds a SensorReadingSharedBufferReader class at
//services/device/public/cpp/generic_sensor so that it can be
used by both //content/renderer/device_sensors and Blink code.

Bug: 727788
Change-Id: I9d03b64e5cf191738e87bbe29fd7611654f3fd5b
Reviewed-on: https://chromium-review.googlesource.com/541685Reviewed-by: default avatarTim Volodine <timvolodine@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarAlexander Shalamov <alexander.shalamov@intel.com>
Commit-Queue: Jun Cai <juncai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486865}
parent 88f1ff6e
...@@ -8,6 +8,8 @@ source_set("generic_sensor") { ...@@ -8,6 +8,8 @@ source_set("generic_sensor") {
"platform_sensor_configuration.h", "platform_sensor_configuration.h",
"sensor_reading.cc", "sensor_reading.cc",
"sensor_reading.h", "sensor_reading.h",
"sensor_reading_shared_buffer_reader.cc",
"sensor_reading_shared_buffer_reader.h",
] ]
public_deps = [ public_deps = [
......
// Copyright 2017 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 "services/device/public/cpp/generic_sensor/sensor_reading_shared_buffer_reader.h"
#include "device/base/synchronization/shared_memory_seqlock_buffer.h"
namespace {
constexpr int kMaxReadAttemptsCount = 10;
} // namespace
namespace device {
SensorReadingSharedBufferReader::SensorReadingSharedBufferReader(
const SensorReadingSharedBuffer* buffer)
: buffer_(buffer) {}
SensorReadingSharedBufferReader::~SensorReadingSharedBufferReader() = default;
bool SensorReadingSharedBufferReader::GetReading(SensorReading* result) {
int read_attempts = 0;
while (!TryReadFromBuffer(result)) {
// Only try to read this many times before failing to avoid waiting here
// very long in case of contention with the writer.
if (++read_attempts == kMaxReadAttemptsCount) {
// Failed to successfully read, presumably because the hardware
// thread was taking unusually long. Data in |result| was not updated
// and was simply left what was there before.
return false;
}
}
return true;
}
bool SensorReadingSharedBufferReader::TryReadFromBuffer(SensorReading* result) {
auto version = buffer_->seqlock.value().ReadBegin();
temp_reading_data_ = buffer_->reading;
if (buffer_->seqlock.value().ReadRetry(version))
return false;
*result = temp_reading_data_;
return true;
}
} // namespace device
// Copyright 2017 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.
#ifndef SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_SHARED_BUFFER_READER_H_
#define SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_SHARED_BUFFER_READER_H_
#include "base/macros.h"
#include "services/device/public/cpp/generic_sensor/sensor_reading.h"
namespace device {
class SensorReadingSharedBufferReader {
public:
// TODO(juncai): Maybe pass a mojo::ScopedSharedBufferHandle to the
// constructor.
// https://crbug.com/742408
explicit SensorReadingSharedBufferReader(
const SensorReadingSharedBuffer* buffer);
~SensorReadingSharedBufferReader();
// Get sensor reading from shared buffer.
bool GetReading(SensorReading* result);
private:
bool TryReadFromBuffer(SensorReading* result);
const SensorReadingSharedBuffer* buffer_;
SensorReading temp_reading_data_;
DISALLOW_COPY_AND_ASSIGN(SensorReadingSharedBufferReader);
};
} // namespace device
#endif // SERVICES_DEVICE_PUBLIC_CPP_GENERIC_SENSOR_SENSOR_READING_SHARED_BUFFER_READER_H_
...@@ -114,14 +114,11 @@ const SensorConfiguration* SensorProxy::DefaultConfig() const { ...@@ -114,14 +114,11 @@ const SensorConfiguration* SensorProxy::DefaultConfig() const {
void SensorProxy::UpdateSensorReading() { void SensorProxy::UpdateSensorReading() {
DCHECK(ShouldProcessReadings()); DCHECK(ShouldProcessReadings());
int read_attempts = 0;
const int kMaxReadAttemptsCount = 10;
device::SensorReading reading_data; device::SensorReading reading_data;
while (!TryReadFromBuffer(reading_data)) { if (!shared_buffer_handle_->is_valid() ||
if (++read_attempts == kMaxReadAttemptsCount) { !shared_buffer_reader_->GetReading(&reading_data)) {
HandleSensorError(); HandleSensorError();
return; return;
}
} }
if (reading_.timestamp != reading_data.timestamp) { if (reading_.timestamp != reading_data.timestamp) {
...@@ -202,6 +199,11 @@ void SensorProxy::OnSensorCreated(SensorInitParamsPtr params, ...@@ -202,6 +199,11 @@ void SensorProxy::OnSensorCreated(SensorInitParamsPtr params,
HandleSensorError(); HandleSensorError();
return; return;
} }
const auto* buffer = static_cast<const device::SensorReadingSharedBuffer*>(
shared_buffer_.get());
shared_buffer_reader_.reset(
new device::SensorReadingSharedBufferReader(buffer));
frequency_limits_.first = params->minimum_frequency; frequency_limits_.first = params->minimum_frequency;
frequency_limits_.second = params->maximum_frequency; frequency_limits_.second = params->maximum_frequency;
...@@ -254,19 +256,6 @@ void SensorProxy::OnRemoveConfigurationCompleted(double frequency, ...@@ -254,19 +256,6 @@ void SensorProxy::OnRemoveConfigurationCompleted(double frequency,
UpdatePollingStatus(); UpdatePollingStatus();
} }
bool SensorProxy::TryReadFromBuffer(device::SensorReading& result) {
DCHECK(IsInitialized());
const ReadingBuffer* buffer =
static_cast<const ReadingBuffer*>(shared_buffer_.get());
const device::OneWriterSeqLock& seqlock = buffer->seqlock.value();
auto version = seqlock.ReadBegin();
auto reading_data = buffer->reading;
if (seqlock.ReadRetry(version))
return false;
result = reading_data;
return true;
}
void SensorProxy::OnPollingTimer(TimerBase*) { void SensorProxy::OnPollingTimer(TimerBase*) {
UpdateSensorReading(); UpdateSensorReading();
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "platform/heap/Handle.h" #include "platform/heap/Handle.h"
#include "platform/wtf/Vector.h" #include "platform/wtf/Vector.h"
#include "services/device/public/cpp/generic_sensor/sensor_reading.h" #include "services/device/public/cpp/generic_sensor/sensor_reading.h"
#include "services/device/public/cpp/generic_sensor/sensor_reading_shared_buffer_reader.h"
#include "services/device/public/interfaces/sensor.mojom-blink.h" #include "services/device/public/interfaces/sensor.mojom-blink.h"
#include "services/device/public/interfaces/sensor_provider.mojom-blink.h" #include "services/device/public/interfaces/sensor_provider.mojom-blink.h"
...@@ -112,8 +113,6 @@ class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, ...@@ -112,8 +113,6 @@ class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>,
bool result); bool result);
void OnRemoveConfigurationCompleted(double frequency, bool result); void OnRemoveConfigurationCompleted(double frequency, bool result);
bool TryReadFromBuffer(device::SensorReading& result);
void OnPollingTimer(TimerBase*); void OnPollingTimer(TimerBase*);
// Returns 'true' if readings should be propagated to Observers // Returns 'true' if readings should be propagated to Observers
...@@ -141,6 +140,8 @@ class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>, ...@@ -141,6 +140,8 @@ class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>,
State state_; State state_;
mojo::ScopedSharedBufferHandle shared_buffer_handle_; mojo::ScopedSharedBufferHandle shared_buffer_handle_;
mojo::ScopedSharedBufferMapping shared_buffer_; mojo::ScopedSharedBufferMapping shared_buffer_;
std::unique_ptr<device::SensorReadingSharedBufferReader>
shared_buffer_reader_;
bool suspended_; bool suspended_;
device::SensorReading reading_; device::SensorReading reading_;
std::pair<double, double> frequency_limits_; std::pair<double, double> frequency_limits_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment