Chromium Code Reviews| Index: content/renderer/device_orientation/device_motion_shared_memory_reader.cc |
| diff --git a/content/renderer/device_orientation/device_motion_shared_memory_reader.cc b/content/renderer/device_orientation/device_motion_shared_memory_reader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..510639b85e596f842dc341f0c522ad0aa9031670 |
| --- /dev/null |
| +++ b/content/renderer/device_orientation/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( |
|
darin (slow to review)
2013/05/29 06:41:53
are you sure you need to CHECK here?
timvolodine
2013/05/29 18:59:06
hmm, I thought the reasoning for this was that the
|
| + new DeviceMotionHostMsg_StartPolling(&renderer_shared_memory_handle_))); |
|
darin (slow to review)
2013/05/29 06:41:53
Can you do this using an asynchronous IPC instead?
timvolodine
2013/05/29 18:59:06
I was basing this IPC messaging on how the gamepad
|
| + |
| + 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); |
|
darin (slow to review)
2013/05/29 06:41:53
i have seen cases where it is difficult to map a l
timvolodine
2013/05/29 18:59:06
I actually borrowed that code from the gamepad imp
|
| + device_motion_hardware_buffer_ = |
| + static_cast<DeviceMotionHardwareBuffer*>(memory); |
| + |
|
darin (slow to review)
2013/05/29 06:41:53
nit: kill gratuitous new line
timvolodine
2013/05/29 18:59:06
Done.
|
| +} |
| + |
| +void DeviceMotionSharedMemoryReader::stopUpdating() { |
| + CHECK(RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling())); |
|
darin (slow to review)
2013/05/29 06:41:53
why do you need to CHECK here? this could fail du
timvolodine
2013/05/29 18:59:06
Done. since the StopPolling message is async now (
|
| +} |
| + |
| +} // namespace content |