Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device_motion_shared_memory_reader.h" | |
| 6 | |
| 7 #include "base/debug/trace_event.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "content/common/device_motion_hardware_buffer.h" | |
| 10 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 bool DeviceMotionSharedMemoryReader::GetLatestDeviceMotionData( | |
| 15 WebKit::WebDeviceMotionData& data) { | |
| 16 | |
| 17 WebKit::WebDeviceMotionData read_into; | |
| 18 TRACE_EVENT0("DEVICEMOTION", "lastDeviceMotionData"); | |
| 19 | |
| 20 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | |
| 21 return false; | |
| 22 | |
| 23 // 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
| |
| 24 // very long in case of contention with the writer. | |
| 25 bool is_ready_for_read = false; | |
| 26 const int kMaximumContentionCount = 10; | |
| 27 int contention_count = -1; | |
| 28 base::subtle::Atomic32 version; | |
| 29 do { | |
| 30 version = device_motion_hardware_buffer_->sequence.ReadBegin(); | |
| 31 is_ready_for_read = device_motion_hardware_buffer_->is_ready_for_read; | |
| 32 memcpy(&read_into, &device_motion_hardware_buffer_->buffer, | |
| 33 sizeof(read_into)); | |
| 34 ++contention_count; | |
| 35 if (contention_count == kMaximumContentionCount) | |
| 36 break; | |
| 37 } while (device_motion_hardware_buffer_->sequence.ReadRetry(version)); | |
| 38 UMA_HISTOGRAM_COUNTS("DeviceMotion.ReadContentionCount", contention_count); | |
| 39 | |
| 40 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.
| |
| 41 | |
| 42 if (contention_count >= kMaximumContentionCount) { | |
| 43 // We failed to successfully read, presumably because the hardware | |
| 44 // thread was taking unusually long. Don't copy the data to the output | |
| 45 // buffer, and simply leave what was there before. | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 // New data was read successfully, copy it into the output buffer. | |
| 50 memcpy(&data, &read_into, sizeof(data)); | |
| 51 | |
| 52 return is_ready_for_read; | |
| 53 } | |
| 54 | |
| 55 DeviceMotionSharedMemoryReader::DeviceMotionSharedMemoryReader() | |
| 56 : device_motion_hardware_buffer_(NULL) { | |
| 57 } | |
| 58 | |
| 59 bool DeviceMotionSharedMemoryReader::Initialize( | |
| 60 base::SharedMemoryHandle shared_memory_handle) { | |
| 61 renderer_shared_memory_handle_ = shared_memory_handle; | |
| 62 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | |
| 63 return false; | |
| 64 renderer_shared_memory_.reset(new base::SharedMemory( | |
| 65 renderer_shared_memory_handle_, true)); | |
| 66 | |
| 67 if (renderer_shared_memory_->Map(sizeof(DeviceMotionHardwareBuffer))) { | |
| 68 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.
| |
| 69 device_motion_hardware_buffer_ = | |
| 70 static_cast<DeviceMotionHardwareBuffer*>(memory); | |
| 71 return true; | |
| 72 } | |
| 73 } | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 } // namespace content | |
| OLD | NEW |