Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(671)

Unified Diff: content/renderer/device_orientation/device_motion_shared_memory_reader.cc

Issue 14678012: Implement the content/renderer and content/browser part of the Device Motion API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: implemented async messaging for obtaining the shared memory handle Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..84091ffbd855819845cd767b91b2758a6c0e30cf
--- /dev/null
+++ b/content/renderer/device_orientation/device_motion_shared_memory_reader.cc
@@ -0,0 +1,77 @@
+// 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 "third_party/WebKit/public/platform/WebDeviceMotionData.h"
+
+namespace content {
+
+bool DeviceMotionSharedMemoryReader::GetLatestDeviceMotionData(
+ 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
darin (slow to review) 2013/06/17 05:27:46 it looks like you mostly just copy-pasted code fro
timvolodine 2013/06/17 20:10:55 yes, here the code is mostly the same as for gamep
+ // 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);
+
+LOG(INFO) << "PUMP CONTENTION = " << contention_count;
darin (slow to review) 2013/06/17 05:27:46 nit: indentation... also, is this really needed in
timvolodine 2013/06/17 20:10:55 Done.
+
+ 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) {
+}
+
+bool DeviceMotionSharedMemoryReader::Initialize(
+ base::SharedMemoryHandle shared_memory_handle) {
+ renderer_shared_memory_handle_ = shared_memory_handle;
+ if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_))
+ return false;
+ renderer_shared_memory_.reset(new base::SharedMemory(
+ renderer_shared_memory_handle_, true));
+
+ if (renderer_shared_memory_->Map(sizeof(DeviceMotionHardwareBuffer))) {
+ if (void *memory = renderer_shared_memory_->memory()) {
darin (slow to review) 2013/06/17 05:27:46 nit: indentation
timvolodine 2013/06/17 20:10:55 Done.
+ device_motion_hardware_buffer_ =
+ static_cast<DeviceMotionHardwareBuffer*>(memory);
+ return true;
+ }
+ }
+ return false;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698