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

Side by Side Diff: content/renderer/input/main_thread_event_queue.cc

Issue 2166703003: Implement Main Thread RAF Aligned Input (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master_main_thread_queue
Patch Set: Fix build Created 4 years, 3 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
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 "content/renderer/input/main_thread_event_queue.h" 5 #include "content/renderer/input/main_thread_event_queue.h"
6 6
7 #include "base/metrics/histogram_macros.h"
7 #include "content/common/input/event_with_latency_info.h" 8 #include "content/common/input/event_with_latency_info.h"
8 #include "content/common/input_messages.h" 9 #include "content/common/input_messages.h"
9 10
10 namespace content { 11 namespace content {
11 12
13 namespace {
14
15 // The maximum number of post-coalesced events processed per rAF task. 10 was
16 // chosen because it really should never be hit yet prevents an infinite loop if
17 // the compositor keeps delivering events faster than the main thread can
18 // process them.
19 const size_t kMaxEventsPerRafTask = 10;
20
21 bool isContinuousEvent(const std::unique_ptr<EventWithDispatchType>& event) {
22 switch (event->event().type) {
23 case blink::WebInputEvent::MouseMove:
24 case blink::WebInputEvent::TouchMove:
25 case blink::WebInputEvent::MouseWheel:
26 return true;
27 default:
28 return false;
29 }
30 }
tdresser 2016/08/25 14:38:41 Missing newline.
dtapuska 2016/08/25 15:01:35 Done.
31 } // namespace
32
12 EventWithDispatchType::EventWithDispatchType( 33 EventWithDispatchType::EventWithDispatchType(
13 ui::ScopedWebInputEvent event, 34 ui::ScopedWebInputEvent event,
14 const ui::LatencyInfo& latency, 35 const ui::LatencyInfo& latency,
15 InputEventDispatchType dispatch_type) 36 InputEventDispatchType dispatch_type)
16 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency), 37 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency),
17 dispatch_type_(dispatch_type) {} 38 dispatch_type_(dispatch_type),
39 creation_timestamp_(base::TimeTicks::Now()),
40 last_coalesced_timestamp_(creation_timestamp_) {}
18 41
19 EventWithDispatchType::~EventWithDispatchType() {} 42 EventWithDispatchType::~EventWithDispatchType() {}
20 43
21 bool EventWithDispatchType::CanCoalesceWith( 44 bool EventWithDispatchType::CanCoalesceWith(
22 const EventWithDispatchType& other) const { 45 const EventWithDispatchType& other) const {
23 return other.dispatch_type_ == dispatch_type_ && 46 return other.dispatch_type_ == dispatch_type_ &&
24 ScopedWebInputEventWithLatencyInfo::CanCoalesceWith(other); 47 ScopedWebInputEventWithLatencyInfo::CanCoalesceWith(other);
25 } 48 }
26 49
27 void EventWithDispatchType::CoalesceWith(const EventWithDispatchType& other) { 50 void EventWithDispatchType::CoalesceWith(const EventWithDispatchType& other) {
28 // If we are blocking and are coalescing touch, make sure to keep 51 coalesced_event_ids_.push_back(
29 // the touch ids that need to be acked. 52 ui::WebInputEventTraits::GetUniqueTouchEventId(other.event()));
30 if (dispatch_type_ == DISPATCH_TYPE_BLOCKING) {
31 // We should only have blocking touch events that need coalescing.
32 eventsToAck_.push_back(
33 ui::WebInputEventTraits::GetUniqueTouchEventId(other.event()));
34 }
35 ScopedWebInputEventWithLatencyInfo::CoalesceWith(other); 53 ScopedWebInputEventWithLatencyInfo::CoalesceWith(other);
54 last_coalesced_timestamp_ = base::TimeTicks::Now();
36 } 55 }
37 56
57 MainThreadEventQueue::SharedState::SharedState()
58 : sent_main_frame_request_(false) {}
59
60 MainThreadEventQueue::SharedState::~SharedState() {}
61
38 MainThreadEventQueue::MainThreadEventQueue( 62 MainThreadEventQueue::MainThreadEventQueue(
39 int routing_id, 63 int routing_id,
40 MainThreadEventQueueClient* client, 64 MainThreadEventQueueClient* client,
41 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, 65 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
42 blink::scheduler::RendererScheduler* renderer_scheduler) 66 blink::scheduler::RendererScheduler* renderer_scheduler)
43 : routing_id_(routing_id), 67 : routing_id_(routing_id),
44 client_(client), 68 client_(client),
45 is_flinging_(false), 69 is_flinging_(false),
46 last_touch_start_forced_nonblocking_due_to_fling_(false), 70 last_touch_start_forced_nonblocking_due_to_fling_(false),
47 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled( 71 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled(
48 features::kPassiveEventListenersDueToFling)), 72 features::kPassiveEventListenersDueToFling)),
73 handle_raf_aligned_input_(
74 base::FeatureList::IsEnabled(features::kRafAlignedInputEvents)),
49 main_task_runner_(main_task_runner), 75 main_task_runner_(main_task_runner),
50 renderer_scheduler_(renderer_scheduler) {} 76 renderer_scheduler_(renderer_scheduler) {}
51 77
52 MainThreadEventQueue::~MainThreadEventQueue() {} 78 MainThreadEventQueue::~MainThreadEventQueue() {}
53 79
54 bool MainThreadEventQueue::HandleEvent( 80 bool MainThreadEventQueue::HandleEvent(
55 ui::ScopedWebInputEvent event, 81 ui::ScopedWebInputEvent event,
56 const ui::LatencyInfo& latency, 82 const ui::LatencyInfo& latency,
57 InputEventDispatchType original_dispatch_type, 83 InputEventDispatchType original_dispatch_type,
58 InputEventAckState ack_result) { 84 InputEventAckState ack_result) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 non_blocking ? DISPATCH_TYPE_NON_BLOCKING : DISPATCH_TYPE_BLOCKING; 129 non_blocking ? DISPATCH_TYPE_NON_BLOCKING : DISPATCH_TYPE_BLOCKING;
104 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type( 130 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type(
105 new EventWithDispatchType(std::move(event), latency, dispatch_type)); 131 new EventWithDispatchType(std::move(event), latency, dispatch_type));
106 132
107 QueueEvent(std::move(event_with_dispatch_type)); 133 QueueEvent(std::move(event_with_dispatch_type));
108 134
109 // send an ack when we are non-blocking. 135 // send an ack when we are non-blocking.
110 return non_blocking; 136 return non_blocking;
111 } 137 }
112 138
113 void MainThreadEventQueue::PopEventOnMainThread() { 139 void MainThreadEventQueue::DispatchInFlightEvent() {
114 { 140 if (in_flight_event_) {
115 base::AutoLock lock(event_queue_lock_); 141 // Report the coalesced count only for continuous events; otherwise
116 if (!events_.empty()) 142 // the zero value would be dominated by non-continuous events.
117 in_flight_event_ = events_.Pop(); 143 if (isContinuousEvent(in_flight_event_)) {
118 } 144 UMA_HISTOGRAM_CUSTOM_COUNTS(
145 "Event.MainThreadEventQueue.Continuous.QueueingTime",
146 (base::TimeTicks::Now() - in_flight_event_->creationTimestamp())
147 .ToInternalValue(),
148 1, 10000000, 100);
119 149
120 if (in_flight_event_) { 150 UMA_HISTOGRAM_CUSTOM_COUNTS(
151 "Event.MainThreadEventQueue.Continuous.FreshnessTime",
152 (base::TimeTicks::Now() - in_flight_event_->lastCoalescedTimestamp())
153 .ToInternalValue(),
154 1, 10000000, 100);
155
156 UMA_HISTOGRAM_COUNTS_100("Event.MainThreadEventQueue.CoalescedCount",
157 in_flight_event_->coalescedEventIds().size());
158 } else {
159 UMA_HISTOGRAM_CUSTOM_COUNTS(
160 "Event.MainThreadEventQueue.NonContinuous.QueueingTime",
161 (base::TimeTicks::Now() - in_flight_event_->creationTimestamp())
162 .ToInternalValue(),
163 1, 10000000, 100);
164 }
165
121 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType(); 166 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType();
122 if (!in_flight_event_->eventsToAck().empty() && 167 if (!in_flight_event_->coalescedEventIds().empty() &&
123 dispatch_type == DISPATCH_TYPE_BLOCKING) { 168 dispatch_type == DISPATCH_TYPE_BLOCKING) {
124 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN; 169 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN;
125 } 170 }
126 171
127 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(), 172 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(),
128 in_flight_event_->latencyInfo(), 173 in_flight_event_->latencyInfo(),
129 dispatch_type); 174 dispatch_type);
130 } 175 }
131 176
132 in_flight_event_.reset(); 177 in_flight_event_.reset();
133 } 178 }
134 179
180 void MainThreadEventQueue::PossiblyScheduleMainFrame() {
181 if (!handle_raf_aligned_input_)
182 return;
183 bool needs_main_frame = false;
184 {
185 base::AutoLock lock(shared_state_lock_);
186 if (!shared_state_.sent_main_frame_request_ &&
187 !shared_state_.events_.empty() &&
188 isContinuousEvent(shared_state_.events_.front())) {
189 needs_main_frame = !shared_state_.sent_main_frame_request_;
190 shared_state_.sent_main_frame_request_ = false;
191 }
192 }
193 if (needs_main_frame)
194 client_->NeedsMainFrame(routing_id_);
195 }
196
197 void MainThreadEventQueue::DispatchSingleEvent() {
198 {
199 base::AutoLock lock(shared_state_lock_);
200 if (shared_state_.events_.empty())
201 return;
202
203 in_flight_event_ = shared_state_.events_.Pop();
204 }
205 DispatchInFlightEvent();
206 PossiblyScheduleMainFrame();
207 }
208
135 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, 209 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type,
136 InputEventAckState ack_result) { 210 InputEventAckState ack_result) {
137 if (in_flight_event_) { 211 if (in_flight_event_ &&
138 // Send acks for blocking touch events. 212 in_flight_event_->dispatchType() == DISPATCH_TYPE_BLOCKING) {
139 for (const auto id : in_flight_event_->eventsToAck()) { 213 // We should only have blocking touch events that need coalescing.
214 for (const auto id : in_flight_event_->coalescedEventIds()) {
140 client_->SendInputEventAck(routing_id_, type, ack_result, id); 215 client_->SendInputEventAck(routing_id_, type, ack_result, id);
141 if (renderer_scheduler_) { 216 if (renderer_scheduler_) {
142 renderer_scheduler_->DidHandleInputEventOnMainThread( 217 renderer_scheduler_->DidHandleInputEventOnMainThread(
143 in_flight_event_->event()); 218 in_flight_event_->event());
144 } 219 }
145 } 220 }
146 } 221 }
147 } 222 }
148 223
224 void MainThreadEventQueue::DispatchRafAlignedInput() {
225 if (!handle_raf_aligned_input_)
226 return;
227
228 {
229 base::AutoLock lock(shared_state_lock_);
230 shared_state_.sent_main_frame_request_ = false;
231 }
232
233 for (size_t i = 0; i < kMaxEventsPerRafTask; ++i) {
234 {
235 base::AutoLock lock(shared_state_lock_);
236 if (shared_state_.events_.empty())
237 break;
238
239 if (!isContinuousEvent(shared_state_.events_.front()))
240 break;
241 in_flight_event_ = shared_state_.events_.Pop();
242 }
243 DispatchInFlightEvent();
244 }
245 PossiblyScheduleMainFrame();
246 }
247
149 void MainThreadEventQueue::SendEventNotificationToMainThread() { 248 void MainThreadEventQueue::SendEventNotificationToMainThread() {
150 main_task_runner_->PostTask( 249 main_task_runner_->PostTask(
151 FROM_HERE, base::Bind(&MainThreadEventQueue::PopEventOnMainThread, 250 FROM_HERE, base::Bind(&MainThreadEventQueue::DispatchSingleEvent, this));
152 this));
153 } 251 }
154 252
155 void MainThreadEventQueue::QueueEvent( 253 void MainThreadEventQueue::QueueEvent(
156 std::unique_ptr<EventWithDispatchType> event) { 254 std::unique_ptr<EventWithDispatchType> event) {
157 bool send_notification = false; 255 bool is_continuous = isContinuousEvent(event);
256 size_t send_notification_count = 0;
257 bool needs_main_frame = false;
158 { 258 {
159 base::AutoLock lock(event_queue_lock_); 259 base::AutoLock lock(shared_state_lock_);
160 size_t size_before = events_.size(); 260 size_t size_before = shared_state_.events_.size();
161 events_.Queue(std::move(event)); 261 shared_state_.events_.Queue(std::move(event));
162 send_notification = events_.size() != size_before; 262 size_t size_after = shared_state_.events_.size();
263 if (size_before != size_after) {
264 if (!handle_raf_aligned_input_) {
265 send_notification_count = 1;
266 } else if (!is_continuous) {
267 send_notification_count = 1;
268 // If we had just enqueued a non-rAF input event we will send a series
269 // of normal post messages to ensure they are all handled right away.
270 for (size_t pos = size_after - 1; pos >= 1; --pos) {
271 if (isContinuousEvent(shared_state_.events_.at(pos - 1)))
272 send_notification_count++;
273 else
274 break;
275 }
276 } else {
277 needs_main_frame = !shared_state_.sent_main_frame_request_;
278 shared_state_.sent_main_frame_request_ = true;
279 }
280 }
163 } 281 }
164 if (send_notification) 282 for (size_t i = 0; i < send_notification_count; ++i)
165 SendEventNotificationToMainThread(); 283 SendEventNotificationToMainThread();
284 if (needs_main_frame)
285 client_->NeedsMainFrame(routing_id_);
166 } 286 }
167 287
168 } // namespace content 288 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698