OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/gamepad_shared_memory_reader.h" | 5 #include "content/renderer/gamepad_shared_memory_reader.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "content/common/gamepad_messages.h" | 9 #include "content/common/gamepad_messages.h" |
10 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 static_cast<GamepadHardwareBuffer*>(memory); | 34 static_cast<GamepadHardwareBuffer*>(memory); |
35 } | 35 } |
36 | 36 |
37 void GamepadSharedMemoryReader::SampleGamepads(WebKit::WebGamepads& gamepads) { | 37 void GamepadSharedMemoryReader::SampleGamepads(WebKit::WebGamepads& gamepads) { |
38 WebKit::WebGamepads read_into; | 38 WebKit::WebGamepads read_into; |
39 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 39 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
40 | 40 |
41 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 41 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) |
42 return; | 42 return; |
43 | 43 |
44 gamepad_hardware_buffer_->gamepads.ReadTo(&gamepads); | 44 // Only try to read this many times before failing to avoid waiting here |
| 45 // very long in case of contention with the writer. TODO(scottmg) Tune this |
| 46 // number (as low as 1?) if histogram shows distribution as mostly |
| 47 // 0-and-maximum. |
| 48 const int kMaximumContentionCount = 10; |
| 49 int contention_count = -1; |
| 50 base::subtle::Atomic32 version; |
| 51 do { |
| 52 version = gamepad_hardware_buffer_->sequence.ReadBegin(); |
| 53 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into)); |
| 54 ++contention_count; |
| 55 if (contention_count == kMaximumContentionCount) |
| 56 break; |
| 57 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version)); |
| 58 UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count); |
| 59 |
| 60 if (contention_count >= kMaximumContentionCount) { |
| 61 // We failed to successfully read, presumably because the hardware |
| 62 // thread was taking unusually long. Don't copy the data to the output |
| 63 // buffer, and simply leave what was there before. |
| 64 return; |
| 65 } |
| 66 |
| 67 // New data was read successfully, copy it into the output buffer. |
| 68 memcpy(&gamepads, &read_into, sizeof(gamepads)); |
45 | 69 |
46 // Override the "connected" with false until the user has interacted | 70 // Override the "connected" with false until the user has interacted |
47 // with the gamepad. This is to prevent fingerprinting on drive-by pages. | 71 // with the gamepad. This is to prevent fingerprinting on drive-by pages. |
48 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { | 72 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { |
49 WebKit::WebGamepad& pad = gamepads.items[i]; | 73 WebKit::WebGamepad& pad = gamepads.items[i]; |
50 // If the device is physically connected, then check if we should | 74 // If the device is physically connected, then check if we should |
51 // keep it disabled. We track if any of the primary 4 buttons have been | 75 // keep it disabled. We track if any of the primary 4 buttons have been |
52 // pressed to determine a reasonable intentional interaction from the user. | 76 // pressed to determine a reasonable intentional interaction from the user. |
53 if (pad.connected) { | 77 if (pad.connected) { |
54 if (ever_interacted_with_[i]) | 78 if (ever_interacted_with_[i]) |
55 continue; | 79 continue; |
56 const unsigned kPrimaryInteractionButtons = 4; | 80 const unsigned kPrimaryInteractionButtons = 4; |
57 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j) | 81 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j) |
58 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f; | 82 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f; |
59 // If we've not previously set, and the user still hasn't touched | 83 // If we've not previously set, and the user still hasn't touched |
60 // these buttons, then don't pass the data on to the Chromium port. | 84 // these buttons, then don't pass the data on to the Chromium port. |
61 if (!ever_interacted_with_[i]) | 85 if (!ever_interacted_with_[i]) |
62 pad.connected = false; | 86 pad.connected = false; |
63 } | 87 } |
64 } | 88 } |
65 } | 89 } |
66 | 90 |
67 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
68 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | 92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
69 } | 93 } |
70 | 94 |
71 } // namespace content | 95 } // namespace content |
OLD | NEW |