Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: content/renderer/gamepad_shared_memory_reader.cc

Issue 8772004: Improve GamepadSeqLock impl Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 12 matching lines...) Expand all
23 void *memory = renderer_shared_memory_->memory(); 23 void *memory = renderer_shared_memory_->memory();
24 CHECK(memory); 24 CHECK(memory);
25 gamepad_hardware_buffer_ = 25 gamepad_hardware_buffer_ =
26 static_cast<GamepadHardwareBuffer*>(memory); 26 static_cast<GamepadHardwareBuffer*>(memory);
27 } 27 }
28 28
29 void GamepadSharedMemoryReader::SampleGamepads(WebKit::WebGamepads& gamepads) { 29 void GamepadSharedMemoryReader::SampleGamepads(WebKit::WebGamepads& gamepads) {
30 WebKit::WebGamepads read_into; 30 WebKit::WebGamepads read_into;
31 TRACE_EVENT0("GAMEPAD", "SampleGamepads"); 31 TRACE_EVENT0("GAMEPAD", "SampleGamepads");
32 32
33 // Only try to read this many times before failing to avoid waiting here 33 gamepad_hardware_buffer_->gamepads.ReadTo(&gamepads);
34 // very long in case of contention with the writer. TODO(scottmg) Tune this
35 // number (as low as 1?) if histogram shows distribution as mostly
36 // 0-and-maximum.
37 const int kMaximumContentionCount = 10;
38 int contention_count = -1;
39 base::subtle::Atomic32 version;
40 do {
41 version = gamepad_hardware_buffer_->sequence.ReadBegin();
42 memcpy(&read_into, &gamepad_hardware_buffer_->buffer, sizeof(read_into));
43 ++contention_count;
44 if (contention_count == kMaximumContentionCount)
45 break;
46 } while (gamepad_hardware_buffer_->sequence.ReadRetry(version));
47 HISTOGRAM_COUNTS("Gamepad.ReadContentionCount", contention_count);
48
49 if (contention_count >= kMaximumContentionCount) {
50 // We failed to successfully read, presumably because the hardware
51 // thread was taking unusually long. Don't copy the data to the output
52 // buffer, and simply leave what was there before.
53 return;
54 }
55
56 // New data was read successfully, copy it into the output buffer.
57 memcpy(&gamepads, &read_into, sizeof(gamepads));
58 34
59 // Override the "connected" with false until the user has interacted 35 // Override the "connected" with false until the user has interacted
60 // with the gamepad. This is to prevent fingerprinting on drive-by pages. 36 // with the gamepad. This is to prevent fingerprinting on drive-by pages.
61 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { 37 for (unsigned i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) {
62 WebKit::WebGamepad& pad = gamepads.items[i]; 38 WebKit::WebGamepad& pad = gamepads.items[i];
63 // If the device is physically connected, then check if we should 39 // If the device is physically connected, then check if we should
64 // keep it disabled. We track if any of the primary 4 buttons have been 40 // keep it disabled. We track if any of the primary 4 buttons have been
65 // pressed to determine a reasonable intentional interaction from the user. 41 // pressed to determine a reasonable intentional interaction from the user.
66 if (pad.connected) { 42 if (pad.connected) {
67 if (ever_interacted_with_[i]) 43 if (ever_interacted_with_[i])
68 continue; 44 continue;
69 const unsigned kPrimaryInteractionButtons = 4; 45 const unsigned kPrimaryInteractionButtons = 4;
70 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j) 46 for (unsigned j = 0; j < kPrimaryInteractionButtons; ++j)
71 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f; 47 ever_interacted_with_[i] |= pad.buttons[j] > 0.5f;
72 // If we've not previously set, and the user still hasn't touched 48 // If we've not previously set, and the user still hasn't touched
73 // these buttons, then don't pass the data on to the Chromium port. 49 // these buttons, then don't pass the data on to the Chromium port.
74 if (!ever_interacted_with_[i]) 50 if (!ever_interacted_with_[i])
75 pad.connected = false; 51 pad.connected = false;
76 } 52 }
77 } 53 }
78 } 54 }
79 55
80 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() { 56 GamepadSharedMemoryReader::~GamepadSharedMemoryReader() {
81 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling()); 57 RenderThread::Get()->Send(new GamepadHostMsg_StopPolling());
82 } 58 }
83 59
84 } // namespace content 60 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698