| Index: content/renderer/device_motion_shared_memory_reader.cc
|
| diff --git a/content/renderer/device_motion_shared_memory_reader.cc b/content/renderer/device_motion_shared_memory_reader.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..510639b85e596f842dc341f0c522ad0aa9031670
|
| --- /dev/null
|
| +++ b/content/renderer/device_motion_shared_memory_reader.cc
|
| @@ -0,0 +1,82 @@
|
| +// Copyright (c) 2013 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 "device_motion_shared_memory_reader.h"
|
| +
|
| +#include "base/debug/trace_event.h"
|
| +#include "base/metrics/histogram.h"
|
| +#include "content/common/device_motion_hardware_buffer.h"
|
| +#include "content/common/device_motion_messages.h"
|
| +#include "content/public/renderer/render_thread.h"
|
| +#include "ipc/ipc_sync_message_filter.h"
|
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionData.h"
|
| +
|
| +namespace content {
|
| +
|
| +bool DeviceMotionSharedMemoryReader::latestDeviceMotionData(
|
| + WebKit::WebDeviceMotionData& data) {
|
| +
|
| + WebKit::WebDeviceMotionData read_into;
|
| + TRACE_EVENT0("DEVICEMOTION", "lastDeviceMotionData");
|
| +
|
| + if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
|
| + return false;
|
| +
|
| + // Only try to read this many times before failing to avoid waiting here
|
| + // very long in case of contention with the writer.
|
| + bool is_ready_for_read = false;
|
| + const int kMaximumContentionCount = 10;
|
| + int contention_count = -1;
|
| + base::subtle::Atomic32 version;
|
| + do {
|
| + version = device_motion_hardware_buffer_->sequence.ReadBegin();
|
| + is_ready_for_read = device_motion_hardware_buffer_->is_ready_for_read;
|
| + memcpy(&read_into, &device_motion_hardware_buffer_->buffer,
|
| + sizeof(read_into));
|
| + ++contention_count;
|
| + if (contention_count == kMaximumContentionCount)
|
| + break;
|
| + } while (device_motion_hardware_buffer_->sequence.ReadRetry(version));
|
| + UMA_HISTOGRAM_COUNTS("DeviceMotion.ReadContentionCount", contention_count);
|
| +
|
| + if (contention_count >= kMaximumContentionCount) {
|
| + // We failed to successfully read, presumably because the hardware
|
| + // thread was taking unusually long. Don't copy the data to the output
|
| + // buffer, and simply leave what was there before.
|
| + return false;
|
| + }
|
| +
|
| + // New data was read successfully, copy it into the output buffer.
|
| + memcpy(&data, &read_into, sizeof(data));
|
| +
|
| + return is_ready_for_read;
|
| +}
|
| +
|
| +DeviceMotionSharedMemoryReader::DeviceMotionSharedMemoryReader()
|
| + : device_motion_hardware_buffer_(NULL) {
|
| +}
|
| +
|
| +void DeviceMotionSharedMemoryReader::startUpdating() {
|
| + CHECK(RenderThread::Get()->Send(
|
| + new DeviceMotionHostMsg_StartPolling(&renderer_shared_memory_handle_)));
|
| +
|
| + bool valid_handle = base::SharedMemory::IsHandleValid(
|
| + renderer_shared_memory_handle_);
|
| + if (!valid_handle)
|
| + return;
|
| + renderer_shared_memory_.reset(new base::SharedMemory(
|
| + renderer_shared_memory_handle_, true));
|
| + CHECK(renderer_shared_memory_->Map(sizeof(DeviceMotionHardwareBuffer)));
|
| + void *memory = renderer_shared_memory_->memory();
|
| + CHECK(memory);
|
| + device_motion_hardware_buffer_ =
|
| + static_cast<DeviceMotionHardwareBuffer*>(memory);
|
| +
|
| +}
|
| +
|
| +void DeviceMotionSharedMemoryReader::stopUpdating() {
|
| + CHECK(RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling()));
|
| +}
|
| +
|
| +} // namespace content
|
|
|