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

Side by Side Diff: components/exo/gamepad.cc

Issue 2129003002: Refactored gamepad polling to support dynamic sources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Disabled sanitization test on Android. Suffers from same bug as PollingAccess test Created 4 years, 4 months 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
« no previous file with comments | « no previous file | content/browser/gamepad/gamepad_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/exo/gamepad.h" 5 #include "components/exo/gamepad.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "ash/shell.h" 9 #include "ash/shell.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
14 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
15 #include "components/exo/gamepad_delegate.h" 15 #include "components/exo/gamepad_delegate.h"
16 #include "components/exo/shell_surface.h" 16 #include "components/exo/shell_surface.h"
17 #include "components/exo/surface.h" 17 #include "components/exo/surface.h"
18 #include "device/gamepad/gamepad_data_fetcher.h" 18 #include "device/gamepad/gamepad_data_fetcher.h"
19 #include "device/gamepad/gamepad_platform_data_fetcher.h" 19 #include "device/gamepad/gamepad_pad_state_provider.h"
20 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h"
20 #include "ui/aura/client/focus_client.h" 21 #include "ui/aura/client/focus_client.h"
21 #include "ui/aura/window.h" 22 #include "ui/aura/window.h"
22 23
23 namespace exo { 24 namespace exo {
24 namespace { 25 namespace {
25 26
26 constexpr double kGamepadButtonValueEpsilon = 0.001; 27 constexpr double kGamepadButtonValueEpsilon = 0.001;
27 bool GamepadButtonValuesAreEqual(double a, double b) { 28 bool GamepadButtonValuesAreEqual(double a, double b) {
28 return fabs(a - b) < kGamepadButtonValueEpsilon; 29 return fabs(a - b) < kGamepadButtonValueEpsilon;
29 } 30 }
30 31
31 std::unique_ptr<device::GamepadDataFetcher> CreateGamepadPlatformDataFetcher() { 32 std::unique_ptr<device::GamepadDataFetcher> CreateGamepadPlatformDataFetcher() {
32 return std::unique_ptr<device::GamepadDataFetcher>( 33 return std::unique_ptr<device::GamepadDataFetcher>(
33 new device::GamepadPlatformDataFetcher()); 34 new device::GamepadPlatformDataFetcherLinux());
34 } 35 }
35 36
36 // Time between gamepad polls in milliseconds. 37 // Time between gamepad polls in milliseconds.
37 constexpr unsigned kPollingTimeIntervalMs = 16; 38 constexpr unsigned kPollingTimeIntervalMs = 16;
38 39
39 } // namespace 40 } // namespace
40 41
41 //////////////////////////////////////////////////////////////////////////////// 42 ////////////////////////////////////////////////////////////////////////////////
42 // Gamepad::ThreadSafeGamepadChangeFetcher 43 // Gamepad::ThreadSafeGamepadChangeFetcher
43 44
44 // Implements all methods and resources running on the polling thread. 45 // Implements all methods and resources running on the polling thread.
45 // This class is reference counted to allow it to shut down safely on the 46 // This class is reference counted to allow it to shut down safely on the
46 // polling thread even if the Gamepad has been destroyed on the origin thread. 47 // polling thread even if the Gamepad has been destroyed on the origin thread.
47 class Gamepad::ThreadSafeGamepadChangeFetcher 48 class Gamepad::ThreadSafeGamepadChangeFetcher
48 : public base::RefCountedThreadSafe< 49 : public device::GamepadPadStateProvider,
50 public base::RefCountedThreadSafe<
49 Gamepad::ThreadSafeGamepadChangeFetcher> { 51 Gamepad::ThreadSafeGamepadChangeFetcher> {
50 public: 52 public:
51 using ProcessGamepadChangesCallback = 53 using ProcessGamepadChangesCallback =
52 base::Callback<void(const blink::WebGamepad)>; 54 base::Callback<void(const blink::WebGamepad)>;
53 55
54 ThreadSafeGamepadChangeFetcher( 56 ThreadSafeGamepadChangeFetcher(
55 const ProcessGamepadChangesCallback& post_gamepad_changes, 57 const ProcessGamepadChangesCallback& post_gamepad_changes,
56 const CreateGamepadDataFetcherCallback& create_fetcher_callback, 58 const CreateGamepadDataFetcherCallback& create_fetcher_callback,
57 base::SingleThreadTaskRunner* task_runner) 59 base::SingleThreadTaskRunner* task_runner)
58 : process_gamepad_changes_(post_gamepad_changes), 60 : process_gamepad_changes_(post_gamepad_changes),
59 create_fetcher_callback_(create_fetcher_callback), 61 create_fetcher_callback_(create_fetcher_callback),
60 polling_task_runner_(task_runner), 62 polling_task_runner_(task_runner),
61 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) { 63 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
62 thread_checker_.DetachFromThread(); 64 thread_checker_.DetachFromThread();
63 } 65 }
64 66
65 // Enable or disable gamepad polling. Can be called from any thread. 67 // Enable or disable gamepad polling. Can be called from any thread.
66 void EnablePolling(bool enabled) { 68 void EnablePolling(bool enabled) {
67 polling_task_runner_->PostTask( 69 polling_task_runner_->PostTask(
68 FROM_HERE, 70 FROM_HERE,
69 base::Bind( 71 base::Bind(
70 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread, 72 &ThreadSafeGamepadChangeFetcher::EnablePollingOnPollingThread,
71 make_scoped_refptr(this), enabled)); 73 make_scoped_refptr(this), enabled));
72 } 74 }
73 75
74 private: 76 private:
75 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>; 77 friend class base::RefCountedThreadSafe<ThreadSafeGamepadChangeFetcher>;
76 78
77 virtual ~ThreadSafeGamepadChangeFetcher() {} 79 ~ThreadSafeGamepadChangeFetcher() override {}
78 80
79 // Enables or disables polling. 81 // Enables or disables polling.
80 void EnablePollingOnPollingThread(bool enabled) { 82 void EnablePollingOnPollingThread(bool enabled) {
81 DCHECK(thread_checker_.CalledOnValidThread()); 83 DCHECK(thread_checker_.CalledOnValidThread());
82 is_enabled_ = enabled; 84 is_enabled_ = enabled;
83 85
84 if (is_enabled_) { 86 if (is_enabled_) {
85 if (!fetcher_) { 87 if (!fetcher_) {
86 fetcher_ = create_fetcher_callback_.Run(); 88 fetcher_ = create_fetcher_callback_.Run();
89 InitializeDataFetcher(fetcher_.get());
87 DCHECK(fetcher_); 90 DCHECK(fetcher_);
88 } 91 }
89 SchedulePollOnPollingThread(); 92 SchedulePollOnPollingThread();
90 } else { 93 } else {
91 fetcher_.reset(); 94 fetcher_.reset();
92 } 95 }
93 } 96 }
94 97
95 // Schedules the next poll on the polling thread. 98 // Schedules the next poll on the polling thread.
96 void SchedulePollOnPollingThread() { 99 void SchedulePollOnPollingThread() {
(...skipping 15 matching lines...) Expand all
112 void PollOnPollingThread() { 115 void PollOnPollingThread() {
113 DCHECK(thread_checker_.CalledOnValidThread()); 116 DCHECK(thread_checker_.CalledOnValidThread());
114 117
115 has_poll_scheduled_ = false; 118 has_poll_scheduled_ = false;
116 if (!is_enabled_) 119 if (!is_enabled_)
117 return; 120 return;
118 121
119 DCHECK(fetcher_); 122 DCHECK(fetcher_);
120 123
121 blink::WebGamepads new_state = state_; 124 blink::WebGamepads new_state = state_;
122 fetcher_->GetGamepadData(&new_state, false); 125 fetcher_->GetGamepadData(
126 false /* No hardware changed notification from the system */);
127
128 new_state.length = 0;
129 device::PadState& pad_state = pad_states_.get()[0];
130
131 // After querying the gamepad clear the state if it did not have it's active
132 // state updated but is still listed as being associated with a specific
133 // source. This indicates the gamepad is disconnected.
134 if (!pad_state.active_state &&
135 pad_state.source != device::GAMEPAD_SOURCE_NONE) {
136 ClearPadState(pad_state);
137 }
138
139 MapAndSanitizeGamepadData(&pad_state, &new_state.items[0],
140 false /* Don't sanitize gamepad data */);
141
142 // If the gamepad was active then increment the length of the WebGamepads
143 // struct to indicate it's valid, then set the pad state to inactive. If the
144 // gamepad is still actively reporting the next call to GetGamepadData will
145 // set the active state to active again.
146 if (pad_state.active_state) {
147 new_state.length++;
148 pad_state.active_state = device::GAMEPAD_INACTIVE;
149 }
123 150
124 if (std::max(new_state.length, state_.length) > 0) { 151 if (std::max(new_state.length, state_.length) > 0) {
125 if (new_state.items[0].connected != state_.items[0].connected || 152 if (new_state.items[0].connected != state_.items[0].connected ||
126 new_state.items[0].timestamp > state_.items[0].timestamp) { 153 new_state.items[0].timestamp > state_.items[0].timestamp) {
127 origin_task_runner_->PostTask( 154 origin_task_runner_->PostTask(
128 FROM_HERE, 155 FROM_HERE,
129 base::Bind(process_gamepad_changes_, new_state.items[0])); 156 base::Bind(process_gamepad_changes_, new_state.items[0]));
130 } 157 }
131 } 158 }
132 159
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 delegate_->OnButton(button_id, new_button.pressed, new_button.value); 283 delegate_->OnButton(button_id, new_button.pressed, new_button.value);
257 } 284 }
258 } 285 }
259 if (send_frame) 286 if (send_frame)
260 delegate_->OnFrame(); 287 delegate_->OnFrame();
261 288
262 pad_state_ = new_pad; 289 pad_state_ = new_pad;
263 } 290 }
264 291
265 } // namespace exo 292 } // namespace exo
OLDNEW
« no previous file with comments | « no previous file | content/browser/gamepad/gamepad_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698