| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 4 | 5 |
| 5 #include "content/renderer/gamepad_shared_memory_reader.h" | 6 #include "content/renderer/gamepad_shared_memory_reader.h" |
| 6 | 7 |
| 7 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 8 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 9 #include "content/common/gamepad_messages.h" | 10 #include "content/common/gamepad_messages.h" |
| 10 #include "content/common/gamepad_user_gesture.h" | 11 #include "content/common/gamepad_user_gesture.h" |
| 11 #include "content/public/renderer/render_thread.h" | 12 #include "content/public/renderer/render_thread.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 28 return; | 29 return; |
| 29 renderer_shared_memory_.reset( | 30 renderer_shared_memory_.reset( |
| 30 new base::SharedMemory(renderer_shared_memory_handle_, true)); | 31 new base::SharedMemory(renderer_shared_memory_handle_, true)); |
| 31 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); | 32 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); |
| 32 void *memory = renderer_shared_memory_->memory(); | 33 void *memory = renderer_shared_memory_->memory(); |
| 33 CHECK(memory); | 34 CHECK(memory); |
| 34 gamepad_hardware_buffer_ = | 35 gamepad_hardware_buffer_ = |
| 35 static_cast<GamepadHardwareBuffer*>(memory); | 36 static_cast<GamepadHardwareBuffer*>(memory); |
| 36 } | 37 } |
| 37 | 38 |
| 39 #if defined(OS_ANDROID) |
| 40 void GamepadSharedMemoryReader::ResumeGamepads() { |
| 41 RenderThread::Get()->Send(new GamepadHostMsg_ResumePolling()); |
| 42 } |
| 43 |
| 44 void GamepadSharedMemoryReader::PauseGamepads() { |
| 45 RenderThread::Get()->Send(new GamepadHostMsg_PausePolling()); |
| 46 } |
| 47 #endif |
| 48 |
| 38 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { | 49 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
| 39 // ========== | 50 // ========== |
| 40 // DANGER | 51 // DANGER |
| 41 // ========== | 52 // ========== |
| 42 // | 53 // |
| 43 // This logic is duplicated in Pepper as well. If you change it, that also | 54 // 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. | 55 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. |
| 45 blink::WebGamepads read_into; | 56 blink::WebGamepads read_into; |
| 46 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 57 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
| 47 | 58 |
| 48 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 59 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) |
| 49 return; | 60 return; |
| 50 | 61 |
| 51 // Only try to read this many times before failing to avoid waiting here | 62 // 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 | 63 // 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 | 64 // number (as low as 1?) if histogram shows distribution as mostly |
| 54 // 0-and-maximum. | 65 // 0-and-maximum. |
| 55 const int kMaximumContentionCount = 10; | 66 const int kMaximumContentionCount = 10; |
| 56 int contention_count = -1; | 67 int contention_count = -1; |
| 57 base::subtle::Atomic32 version; | 68 base::subtle::Atomic32 version; |
| 69 #if defined(OS_ANDROID) |
| 70 //Update the timestamp for Gamepad Data access by the consumer |
| 71 RenderThread::Get()->Send(new GamepadHostMsg_UpdateTimestamp()); |
| 72 #endif |
| 58 do { | 73 do { |
| 59 version = gamepad_hardware_buffer_->sequence.ReadBegin(); | 74 version = gamepad_hardware_buffer_->sequence.ReadBegin(); |
| 60 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into)); | 75 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into)); |
| 61 ++contention_count; | 76 ++contention_count; |
| 62 if (contention_count == kMaximumContentionCount) | 77 if (contention_count == kMaximumContentionCount) |
| 63 break; | 78 break; |
| 64 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version)); | 79 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version)); |
| 65 UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count); | 80 UMA_HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count); |
| 66 | 81 |
| 67 if (contention_count >= kMaximumContentionCount) { | 82 if (contention_count >= kMaximumContentionCount) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 86 gamepads.items[i].connected = false; | 101 gamepads.items[i].connected = false; |
| 87 } | 102 } |
| 88 } | 103 } |
| 89 } | 104 } |
| 90 | 105 |
| 91 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { | 106 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { |
| 92 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); | 107 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); |
| 93 } | 108 } |
| 94 | 109 |
| 95 } // namespace content | 110 } // namespace content |
| OLD | NEW |