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 "content/common/device_motion_messages.h" | |
11 #include "content/public/renderer/render_thread.h" | |
12 #include "ipc/ipc_sync_message_filter.h" | |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionData .h" | |
14 | |
15 namespace content { | |
16 | |
17 bool DeviceMotionSharedMemoryReader::latestDeviceMotionData( | |
18 WebKit::WebDeviceMotionData& data) { | |
19 | |
20 WebKit::WebDeviceMotionData read_into; | |
21 TRACE_EVENT0("DEVICEMOTION", "lastDeviceMotionData"); | |
22 | |
23 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | |
24 return false; | |
25 | |
26 // Only try to read this many times before failing to avoid waiting here | |
27 // very long in case of contention with the writer. | |
28 bool is_ready_for_read = false; | |
29 const int kMaximumContentionCount = 10; | |
30 int contention_count = -1; | |
31 base::subtle::Atomic32 version; | |
32 do { | |
33 version = device_motion_hardware_buffer_->sequence.ReadBegin(); | |
34 is_ready_for_read = device_motion_hardware_buffer_->is_ready_for_read; | |
35 memcpy(&read_into, &device_motion_hardware_buffer_->buffer, | |
36 sizeof(read_into)); | |
37 ++contention_count; | |
38 if (contention_count == kMaximumContentionCount) | |
39 break; | |
40 } while (device_motion_hardware_buffer_->sequence.ReadRetry(version)); | |
41 UMA_HISTOGRAM_COUNTS("DeviceMotion.ReadContentionCount", contention_count); | |
42 | |
43 if (contention_count >= kMaximumContentionCount) { | |
44 // We failed to successfully read, presumably because the hardware | |
45 // thread was taking unusually long. Don't copy the data to the output | |
46 // buffer, and simply leave what was there before. | |
47 return false; | |
48 } | |
49 | |
50 // New data was read successfully, copy it into the output buffer. | |
51 memcpy(&data, &read_into, sizeof(data)); | |
52 | |
53 return is_ready_for_read; | |
54 } | |
55 | |
56 DeviceMotionSharedMemoryReader::DeviceMotionSharedMemoryReader() | |
57 : device_motion_hardware_buffer_(NULL) { | |
58 } | |
59 | |
60 void DeviceMotionSharedMemoryReader::startUpdating() { | |
61 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
| |
62 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
| |
63 | |
64 bool valid_handle = base::SharedMemory::IsHandleValid( | |
65 renderer_shared_memory_handle_); | |
66 if (!valid_handle) | |
67 return; | |
68 renderer_shared_memory_.reset(new base::SharedMemory( | |
69 renderer_shared_memory_handle_, true)); | |
70 CHECK(renderer_shared_memory_->Map(sizeof(DeviceMotionHardwareBuffer))); | |
71 void *memory = renderer_shared_memory_->memory(); | |
72 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
| |
73 device_motion_hardware_buffer_ = | |
74 static_cast<DeviceMotionHardwareBuffer*>(memory); | |
75 | |
darin (slow to review)
2013/05/29 06:41:53
nit: kill gratuitous new line
timvolodine
2013/05/29 18:59:06
Done.
| |
76 } | |
77 | |
78 void DeviceMotionSharedMemoryReader::stopUpdating() { | |
79 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 (
| |
80 } | |
81 | |
82 } // namespace content | |
OLD | NEW |