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

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 up tdresser's comments 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 events processed per rAF task.
16 const size_t kMaxEventsPerRafTask = 10;
17
18 bool isContinuousEvent(const std::unique_ptr<EventWithDispatchType>& event) {
19 switch (event->event().type) {
20 case blink::WebInputEvent::MouseMove:
21 case blink::WebInputEvent::TouchMove:
22 case blink::WebInputEvent::MouseWheel:
23 return true;
24 default:
25 return false;
26 }
27 }
28 } // namespace
29
12 EventWithDispatchType::EventWithDispatchType( 30 EventWithDispatchType::EventWithDispatchType(
13 ui::ScopedWebInputEvent event, 31 ui::ScopedWebInputEvent event,
14 const ui::LatencyInfo& latency, 32 const ui::LatencyInfo& latency,
15 InputEventDispatchType dispatch_type) 33 InputEventDispatchType dispatch_type)
16 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency), 34 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency),
17 dispatch_type_(dispatch_type) {} 35 dispatch_type_(dispatch_type),
36 creation_timestamp_(base::TimeTicks::Now()),
37 last_coalesced_timestamp_(creation_timestamp_) {}
tdresser 2016/08/24 17:45:16 I was thinking these timestamps would be pulled fr
dtapuska 2016/08/25 14:07:23 I think using Now represents how long the events a
18 38
19 EventWithDispatchType::~EventWithDispatchType() {} 39 EventWithDispatchType::~EventWithDispatchType() {}
20 40
21 bool EventWithDispatchType::CanCoalesceWith( 41 bool EventWithDispatchType::CanCoalesceWith(
22 const EventWithDispatchType& other) const { 42 const EventWithDispatchType& other) const {
23 return other.dispatch_type_ == dispatch_type_ && 43 return other.dispatch_type_ == dispatch_type_ &&
24 ScopedWebInputEventWithLatencyInfo::CanCoalesceWith(other); 44 ScopedWebInputEventWithLatencyInfo::CanCoalesceWith(other);
25 } 45 }
26 46
27 void EventWithDispatchType::CoalesceWith(const EventWithDispatchType& other) { 47 void EventWithDispatchType::CoalesceWith(const EventWithDispatchType& other) {
28 // If we are blocking and are coalescing touch, make sure to keep 48 coalesced_event_ids_.push_back(
29 // the touch ids that need to be acked. 49 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); 50 ScopedWebInputEventWithLatencyInfo::CoalesceWith(other);
51 last_coalesced_timestamp_ = base::TimeTicks::Now();
36 } 52 }
37 53
54 MainThreadEventQueue::SharedState::SharedState()
55 : sent_main_frame_request_(false) {}
56
57 MainThreadEventQueue::SharedState::~SharedState() {}
58
38 MainThreadEventQueue::MainThreadEventQueue( 59 MainThreadEventQueue::MainThreadEventQueue(
39 int routing_id, 60 int routing_id,
40 MainThreadEventQueueClient* client, 61 MainThreadEventQueueClient* client,
41 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, 62 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
42 blink::scheduler::RendererScheduler* renderer_scheduler) 63 blink::scheduler::RendererScheduler* renderer_scheduler)
43 : routing_id_(routing_id), 64 : routing_id_(routing_id),
44 client_(client), 65 client_(client),
45 is_flinging_(false), 66 is_flinging_(false),
46 last_touch_start_forced_nonblocking_due_to_fling_(false), 67 last_touch_start_forced_nonblocking_due_to_fling_(false),
47 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled( 68 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled(
48 features::kPassiveEventListenersDueToFling)), 69 features::kPassiveEventListenersDueToFling)),
70 handle_raf_aligned_input_(
71 base::FeatureList::IsEnabled(features::kRafAlignedInputEvents)),
49 main_task_runner_(main_task_runner), 72 main_task_runner_(main_task_runner),
50 renderer_scheduler_(renderer_scheduler) {} 73 renderer_scheduler_(renderer_scheduler) {}
51 74
52 MainThreadEventQueue::~MainThreadEventQueue() {} 75 MainThreadEventQueue::~MainThreadEventQueue() {}
53 76
54 bool MainThreadEventQueue::HandleEvent( 77 bool MainThreadEventQueue::HandleEvent(
55 ui::ScopedWebInputEvent event, 78 ui::ScopedWebInputEvent event,
56 const ui::LatencyInfo& latency, 79 const ui::LatencyInfo& latency,
57 InputEventDispatchType original_dispatch_type, 80 InputEventDispatchType original_dispatch_type,
58 InputEventAckState ack_result) { 81 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; 126 non_blocking ? DISPATCH_TYPE_NON_BLOCKING : DISPATCH_TYPE_BLOCKING;
104 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type( 127 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type(
105 new EventWithDispatchType(std::move(event), latency, dispatch_type)); 128 new EventWithDispatchType(std::move(event), latency, dispatch_type));
106 129
107 QueueEvent(std::move(event_with_dispatch_type)); 130 QueueEvent(std::move(event_with_dispatch_type));
108 131
109 // send an ack when we are non-blocking. 132 // send an ack when we are non-blocking.
110 return non_blocking; 133 return non_blocking;
111 } 134 }
112 135
113 void MainThreadEventQueue::PopEventOnMainThread() { 136 void MainThreadEventQueue::DispatchInFlightEvent() {
114 { 137 if (in_flight_event_) {
115 base::AutoLock lock(event_queue_lock_); 138 // Report the coalesced count only for continuous events; otherwise
116 if (!events_.empty()) 139 // the zero value would be dominated by non-continuous events.
117 in_flight_event_ = events_.Pop(); 140 if (isContinuousEvent(in_flight_event_)) {
118 } 141 UMA_HISTOGRAM_CUSTOM_COUNTS(
142 "Event.MainThreadEventQueue.Continuous.QueueingTime",
143 (base::TimeTicks::Now() - in_flight_event_->creationTimestamp())
tdresser 2016/08/24 17:45:16 Factor out call to now, to avoid repeated calls.
dtapuska 2016/08/25 14:07:23 done
144 .ToInternalValue(),
145 1, 10000000, 100);
tdresser 2016/08/24 17:45:16 We should only use 50 buckets for all of these.
dtapuska 2016/08/25 14:07:23 done
119 146
120 if (in_flight_event_) { 147 UMA_HISTOGRAM_CUSTOM_COUNTS(
148 "Event.MainThreadEventQueue.Continuous.FreshnessTime",
149 (base::TimeTicks::Now() - in_flight_event_->lastCoalescedTimestamp())
150 .ToInternalValue(),
151 1, 10000000, 100);
152
153 UMA_HISTOGRAM_COUNTS_100("Event.MainThreadEventQueue.CoalescedCount",
154 in_flight_event_->coalescedEventIds().size());
155 } else {
156 UMA_HISTOGRAM_CUSTOM_COUNTS(
157 "Event.MainThreadEventQueue.NonContinuous.QueueingTime",
158 (base::TimeTicks::Now() - in_flight_event_->creationTimestamp())
159 .ToInternalValue(),
160 1, 10000000, 100);
161 }
162
121 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType(); 163 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType();
122 if (!in_flight_event_->eventsToAck().empty() && 164 if (!in_flight_event_->coalescedEventIds().empty() &&
123 dispatch_type == DISPATCH_TYPE_BLOCKING) { 165 dispatch_type == DISPATCH_TYPE_BLOCKING) {
124 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN; 166 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN;
125 } 167 }
126 168
127 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(), 169 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(),
128 in_flight_event_->latencyInfo(), 170 in_flight_event_->latencyInfo(),
129 dispatch_type); 171 dispatch_type);
130 } 172 }
131 173
132 in_flight_event_.reset(); 174 in_flight_event_.reset();
133 } 175 }
134 176
177 void MainThreadEventQueue::PossiblyScheduleMainFrame() {
178 if (!handle_raf_aligned_input_)
179 return;
180 bool needs_main_frame = false;
181 {
182 base::AutoLock lock(shared_state_lock_);
183 if (!shared_state_.sent_main_frame_request_ &&
184 !shared_state_.events_.empty() &&
185 isContinuousEvent(shared_state_.events_.front())) {
186 needs_main_frame = !shared_state_.sent_main_frame_request_;
187 shared_state_.sent_main_frame_request_ = false;
188 }
189 }
190 if (needs_main_frame)
191 client_->NeedsMainFrame(routing_id_);
192 }
193
194 void MainThreadEventQueue::DispatchSingleEvent() {
195 {
196 base::AutoLock lock(shared_state_lock_);
197 if (shared_state_.events_.empty())
198 return;
199
200 in_flight_event_ = shared_state_.events_.Pop();
201 }
202 DispatchInFlightEvent();
203 PossiblyScheduleMainFrame();
204 }
205
135 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, 206 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type,
136 InputEventAckState ack_result) { 207 InputEventAckState ack_result) {
137 if (in_flight_event_) { 208 if (in_flight_event_ &&
138 // Send acks for blocking touch events. 209 in_flight_event_->dispatchType() == DISPATCH_TYPE_BLOCKING) {
139 for (const auto id : in_flight_event_->eventsToAck()) { 210 // We should only have blocking touch events that need coalescing.
tdresser 2016/08/24 17:45:16 Aren't some of these not touch events?
dtapuska 2016/08/25 14:07:23 Yes; I've removed the comment which was copied due
211 for (const auto id : in_flight_event_->coalescedEventIds()) {
140 client_->SendInputEventAck(routing_id_, type, ack_result, id); 212 client_->SendInputEventAck(routing_id_, type, ack_result, id);
141 if (renderer_scheduler_) { 213 if (renderer_scheduler_) {
142 renderer_scheduler_->DidHandleInputEventOnMainThread( 214 renderer_scheduler_->DidHandleInputEventOnMainThread(
143 in_flight_event_->event()); 215 in_flight_event_->event());
144 } 216 }
145 } 217 }
146 } 218 }
147 } 219 }
148 220
221 void MainThreadEventQueue::DispatchRafAlignedInput() {
222 if (!handle_raf_aligned_input_)
223 return;
224
225 {
226 base::AutoLock lock(shared_state_lock_);
227 shared_state_.sent_main_frame_request_ = false;
228 }
229
230 for (size_t i = 0; i < kMaxEventsPerRafTask; ++i) {
231 {
232 base::AutoLock lock(shared_state_lock_);
233 if (shared_state_.events_.empty())
234 break;
235
236 if (!isContinuousEvent(shared_state_.events_.front()))
237 break;
238 in_flight_event_ = shared_state_.events_.Pop();
239 }
240 DispatchInFlightEvent();
241 }
242 PossiblyScheduleMainFrame();
243 }
244
149 void MainThreadEventQueue::SendEventNotificationToMainThread() { 245 void MainThreadEventQueue::SendEventNotificationToMainThread() {
150 main_task_runner_->PostTask( 246 main_task_runner_->PostTask(
151 FROM_HERE, base::Bind(&MainThreadEventQueue::PopEventOnMainThread, 247 FROM_HERE, base::Bind(&MainThreadEventQueue::DispatchSingleEvent, this));
152 this));
153 } 248 }
154 249
155 void MainThreadEventQueue::QueueEvent( 250 void MainThreadEventQueue::QueueEvent(
156 std::unique_ptr<EventWithDispatchType> event) { 251 std::unique_ptr<EventWithDispatchType> event) {
157 bool send_notification = false; 252 bool is_continuous = isContinuousEvent(event);
253 size_t send_notification_count = 0;
254 bool needs_main_frame = false;
158 { 255 {
159 base::AutoLock lock(event_queue_lock_); 256 base::AutoLock lock(shared_state_lock_);
160 size_t size_before = events_.size(); 257 size_t size_before = shared_state_.events_.size();
161 events_.Queue(std::move(event)); 258 shared_state_.events_.Queue(std::move(event));
162 send_notification = events_.size() != size_before; 259 size_t size_after = shared_state_.events_.size();
260 if (size_before != size_after) {
261 if (!handle_raf_aligned_input_) {
262 send_notification_count = 1;
263 } else if (!is_continuous) {
264 send_notification_count = 1;
265 // If we had just enqueued a non-rAF input event we will send a series
266 // of normal post messages to ensure they are all handled right away.
267 for (size_t pos = size_after - 1; pos >= 1; --pos) {
268 if (isContinuousEvent(shared_state_.events_.at(pos - 1)))
269 send_notification_count++;
270 else
271 break;
272 }
273 } else {
274 needs_main_frame = !shared_state_.sent_main_frame_request_;
275 shared_state_.sent_main_frame_request_ = true;
276 }
277 }
163 } 278 }
164 if (send_notification) 279 for (size_t i = 0; i < send_notification_count; ++i)
165 SendEventNotificationToMainThread(); 280 SendEventNotificationToMainThread();
281 if (needs_main_frame)
282 client_->NeedsMainFrame(routing_id_);
166 } 283 }
167 284
168 } // namespace content 285 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698