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/common/gamepad_user_gesture.h" | 10 #include "content/common/gamepad_user_gesture.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 return; | 28 return; |
29 renderer_shared_memory_.reset( | 29 renderer_shared_memory_.reset( |
30 new base::SharedMemory(renderer_shared_memory_handle_, true)); | 30 new base::SharedMemory(renderer_shared_memory_handle_, true)); |
31 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); | 31 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); |
32 void *memory = renderer_shared_memory_->memory(); | 32 void *memory = renderer_shared_memory_->memory(); |
33 CHECK(memory); | 33 CHECK(memory); |
34 gamepad_hardware_buffer_ = | 34 gamepad_hardware_buffer_ = |
35 static_cast<GamepadHardwareBuffer*>(memory); | 35 static_cast<GamepadHardwareBuffer*>(memory); |
36 } | 36 } |
37 | 37 |
| 38 void GamepadSharedMemoryReader::ResumeGamepads() { |
| 39 RenderThread::Get()->Send(new GamepadHostMsg_ResumePolling()); |
| 40 } |
| 41 |
| 42 void GamepadSharedMemoryReader::PauseGamepads() { |
| 43 RenderThread::Get()->Send(new GamepadHostMsg_PausePolling()); |
| 44 } |
| 45 |
38 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { | 46 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
39 // ========== | 47 // ========== |
40 // DANGER | 48 // DANGER |
41 // ========== | 49 // ========== |
42 // | 50 // |
43 // This logic is duplicated in Pepper as well. If you change it, that also | 51 // This logic is duplicated in Pepper as well. If you change it, that also |
44 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. | 52 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. |
45 blink::WebGamepads read_into; | 53 blink::WebGamepads read_into; |
46 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 54 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
47 | 55 |
48 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 56 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) |
49 return; | 57 return; |
50 | 58 |
51 // Only try to read this many times before failing to avoid waiting here | 59 // Only try to read this many times before failing to avoid waiting here |
52 // very long in case of contention with the writer. TODO(scottmg) Tune this | 60 // very long in case of contention with the writer. TODO(scottmg) Tune this |
53 // number (as low as 1?) if histogram shows distribution as mostly | 61 // number (as low as 1?) if histogram shows distribution as mostly |
54 // 0-and-maximum. | 62 // 0-and-maximum. |
55 const int kMaximumContentionCount = 10; | 63 const int kMaximumContentionCount = 10; |
56 int contention_count = -1; | 64 int contention_count = -1; |
57 base::subtle::Atomic32 version; | 65 base::subtle::Atomic32 version; |
| 66 // Update the timestamp for Gamepad Data access by the consumer |
| 67 RenderThread::Get()->Send(new GamepadHostMsg_UpdateTimestamp()); |
58 do { | 68 do { |
59 version = gamepad_hardware_buffer_->sequence.ReadBegin(); | 69 version = gamepad_hardware_buffer_->sequence.ReadBegin(); |
60 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into)); | 70 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into)); |
61 ++contention_count; | 71 ++contention_count; |
62 if (contention_count == kMaximumContentionCount) | 72 if (contention_count == kMaximumContentionCount) |
63 break; | 73 break; |
64 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version)); | 74 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version)); |
65 UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count); | 75 UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count); |
66 | 76 |
67 if (contention_count >= kMaximumContentionCount) { | 77 if (contention_count >= kMaximumContentionCount) { |
(...skipping 18 matching lines...) Expand all Loading... |
86 gamepads.items[i].connected = false; | 96 gamepads.items[i].connected = false; |
87 } | 97 } |
88 } | 98 } |
89 } | 99 } |
90 | 100 |
91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 101 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | 102 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
93 } | 103 } |
94 | 104 |
95 } // namespace content | 105 } // namespace content |
OLD | NEW |