| 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/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
| 9 #include "content/public/renderer/render_thread.h" | 9 #include "content/public/renderer/render_thread.h" |
| 10 #include "content/renderer/renderer_blink_platform_impl.h" | 10 #include "content/renderer/renderer_blink_platform_impl.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 binding_(this) { | 22 binding_(this) { |
| 23 if (thread) { | 23 if (thread) { |
| 24 thread->GetRemoteInterfaces()->GetInterface( | 24 thread->GetRemoteInterfaces()->GetInterface( |
| 25 mojo::GetProxy(&gamepad_monitor_)); | 25 mojo::GetProxy(&gamepad_monitor_)); |
| 26 gamepad_monitor_->SetObserver(binding_.CreateInterfacePtrAndBind()); | 26 gamepad_monitor_->SetObserver(binding_.CreateInterfacePtrAndBind()); |
| 27 } | 27 } |
| 28 } | 28 } |
| 29 | 29 |
| 30 void GamepadSharedMemoryReader::SendStartMessage() { | 30 void GamepadSharedMemoryReader::SendStartMessage() { |
| 31 if (gamepad_monitor_) { | 31 if (gamepad_monitor_) { |
| 32 mojo::ScopedSharedBufferHandle buffer_handle; | 32 gamepad_monitor_->GamepadStartPolling(&renderer_shared_buffer_handle_); |
| 33 gamepad_monitor_->GamepadStartPolling(&buffer_handle); | |
| 34 // TODO(heke): Use mojo::SharedBuffer rather than base::SharedMemory. See | |
| 35 // crbug.com/670655. | |
| 36 MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
| 37 std::move(buffer_handle), &renderer_shared_memory_handle_, nullptr, | |
| 38 nullptr); | |
| 39 CHECK_EQ(MOJO_RESULT_OK, result); | |
| 40 } | 33 } |
| 41 } | 34 } |
| 42 | 35 |
| 43 void GamepadSharedMemoryReader::SendStopMessage() { | 36 void GamepadSharedMemoryReader::SendStopMessage() { |
| 44 if (gamepad_monitor_) { | 37 if (gamepad_monitor_) { |
| 45 gamepad_monitor_->GamepadStopPolling(); | 38 gamepad_monitor_->GamepadStopPolling(); |
| 46 } | 39 } |
| 47 } | 40 } |
| 48 | 41 |
| 49 void GamepadSharedMemoryReader::Start( | 42 void GamepadSharedMemoryReader::Start( |
| 50 blink::WebPlatformEventListener* listener) { | 43 blink::WebPlatformEventListener* listener) { |
| 51 PlatformEventObserver::Start(listener); | 44 PlatformEventObserver::Start(listener); |
| 52 | 45 |
| 53 // If we don't get a valid handle from the browser, don't try to Map (we're | 46 // If we don't get a valid handle from the browser, don't try to Map (we're |
| 54 // probably out of memory or file handles). | 47 // probably out of memory or file handles). |
| 55 bool valid_handle = base::SharedMemory::IsHandleValid( | 48 bool valid_handle = renderer_shared_buffer_handle_.is_valid(); |
| 56 renderer_shared_memory_handle_); | |
| 57 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); | 49 UMA_HISTOGRAM_BOOLEAN("Gamepad.ValidSharedMemoryHandle", valid_handle); |
| 58 if (!valid_handle) | 50 if (!valid_handle) |
| 59 return; | 51 return; |
| 60 | 52 |
| 61 renderer_shared_memory_.reset( | 53 renderer_shared_buffer_mapping_ = |
| 62 new base::SharedMemory(renderer_shared_memory_handle_, true)); | 54 renderer_shared_buffer_handle_->Map(sizeof(GamepadHardwareBuffer)); |
| 63 CHECK(renderer_shared_memory_->Map(sizeof(GamepadHardwareBuffer))); | 55 CHECK(renderer_shared_buffer_mapping_); |
| 64 void *memory = renderer_shared_memory_->memory(); | 56 void* memory = renderer_shared_buffer_mapping_.get(); |
| 65 CHECK(memory); | 57 CHECK(memory); |
| 66 gamepad_hardware_buffer_ = | 58 gamepad_hardware_buffer_ = |
| 67 static_cast<GamepadHardwareBuffer*>(memory); | 59 static_cast<GamepadHardwareBuffer*>(memory); |
| 68 } | 60 } |
| 69 | 61 |
| 70 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { | 62 void GamepadSharedMemoryReader::SampleGamepads(blink::WebGamepads& gamepads) { |
| 71 // Blink should have started observing at that point. | 63 // Blink should have started observing at that point. |
| 72 CHECK(is_observing()); | 64 CHECK(is_observing()); |
| 73 | 65 |
| 74 // ========== | 66 // ========== |
| 75 // DANGER | 67 // DANGER |
| 76 // ========== | 68 // ========== |
| 77 // | 69 // |
| 78 // This logic is duplicated in Pepper as well. If you change it, that also | 70 // This logic is duplicated in Pepper as well. If you change it, that also |
| 79 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. | 71 // needs to be in sync. See ppapi/proxy/gamepad_resource.cc. |
| 80 blink::WebGamepads read_into; | 72 blink::WebGamepads read_into; |
| 81 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); | 73 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); |
| 82 | 74 |
| 83 if (!base::SharedMemory::IsHandleValid(renderer_shared_memory_handle_)) | 75 if (!renderer_shared_buffer_handle_.is_valid()) |
| 84 return; | 76 return; |
| 85 | 77 |
| 86 // Only try to read this many times before failing to avoid waiting here | 78 // Only try to read this many times before failing to avoid waiting here |
| 87 // very long in case of contention with the writer. TODO(scottmg) Tune this | 79 // very long in case of contention with the writer. TODO(scottmg) Tune this |
| 88 // number (as low as 1?) if histogram shows distribution as mostly | 80 // number (as low as 1?) if histogram shows distribution as mostly |
| 89 // 0-and-maximum. | 81 // 0-and-maximum. |
| 90 const int kMaximumContentionCount = 10; | 82 const int kMaximumContentionCount = 10; |
| 91 int contention_count = -1; | 83 int contention_count = -1; |
| 92 base::subtle::Atomic32 version; | 84 base::subtle::Atomic32 version; |
| 93 do { | 85 do { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 126 } |
| 135 | 127 |
| 136 void GamepadSharedMemoryReader::GamepadDisconnected( | 128 void GamepadSharedMemoryReader::GamepadDisconnected( |
| 137 int index, | 129 int index, |
| 138 const blink::WebGamepad& gamepad) { | 130 const blink::WebGamepad& gamepad) { |
| 139 if (listener()) | 131 if (listener()) |
| 140 listener()->didDisconnectGamepad(index, gamepad); | 132 listener()->didDisconnectGamepad(index, gamepad); |
| 141 } | 133 } |
| 142 | 134 |
| 143 } // namespace content | 135 } // namespace content |
| OLD | NEW |