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

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 histogram content 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 const size_t kTenSeconds = 10 * 1000 * 1000;
22
23 bool isContinuousEvent(const std::unique_ptr<EventWithDispatchType>& event) {
24 switch (event->event().type) {
25 case blink::WebInputEvent::MouseMove:
26 case blink::WebInputEvent::TouchMove:
27 case blink::WebInputEvent::MouseWheel:
28 return true;
29 default:
30 return false;
31 }
32 }
33
34 } // namespace
35
12 EventWithDispatchType::EventWithDispatchType( 36 EventWithDispatchType::EventWithDispatchType(
13 ui::ScopedWebInputEvent event, 37 ui::ScopedWebInputEvent event,
14 const ui::LatencyInfo& latency, 38 const ui::LatencyInfo& latency,
15 InputEventDispatchType dispatch_type) 39 InputEventDispatchType dispatch_type)
16 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency), 40 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency),
17 dispatch_type_(dispatch_type) {} 41 dispatch_type_(dispatch_type),
42 creation_timestamp_(base::TimeTicks::Now()),
43 last_coalesced_timestamp_(creation_timestamp_) {}
18 44
19 EventWithDispatchType::~EventWithDispatchType() {} 45 EventWithDispatchType::~EventWithDispatchType() {}
20 46
21 bool EventWithDispatchType::CanCoalesceWith( 47 bool EventWithDispatchType::CanCoalesceWith(
22 const EventWithDispatchType& other) const { 48 const EventWithDispatchType& other) const {
23 return other.dispatch_type_ == dispatch_type_ && 49 return other.dispatch_type_ == dispatch_type_ &&
24 ScopedWebInputEventWithLatencyInfo::CanCoalesceWith(other); 50 ScopedWebInputEventWithLatencyInfo::CanCoalesceWith(other);
25 } 51 }
26 52
27 void EventWithDispatchType::CoalesceWith(const EventWithDispatchType& other) { 53 void EventWithDispatchType::CoalesceWith(const EventWithDispatchType& other) {
28 // If we are blocking and are coalescing touch, make sure to keep 54 coalesced_event_ids_.push_back(
29 // the touch ids that need to be acked. 55 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); 56 ScopedWebInputEventWithLatencyInfo::CoalesceWith(other);
57 last_coalesced_timestamp_ = base::TimeTicks::Now();
36 } 58 }
37 59
60 MainThreadEventQueue::SharedState::SharedState()
61 : sent_main_frame_request_(false) {}
62
63 MainThreadEventQueue::SharedState::~SharedState() {}
64
38 MainThreadEventQueue::MainThreadEventQueue( 65 MainThreadEventQueue::MainThreadEventQueue(
39 int routing_id, 66 int routing_id,
40 MainThreadEventQueueClient* client, 67 MainThreadEventQueueClient* client,
41 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, 68 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
42 blink::scheduler::RendererScheduler* renderer_scheduler) 69 blink::scheduler::RendererScheduler* renderer_scheduler)
43 : routing_id_(routing_id), 70 : routing_id_(routing_id),
44 client_(client), 71 client_(client),
45 is_flinging_(false), 72 is_flinging_(false),
46 last_touch_start_forced_nonblocking_due_to_fling_(false), 73 last_touch_start_forced_nonblocking_due_to_fling_(false),
47 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled( 74 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled(
48 features::kPassiveEventListenersDueToFling)), 75 features::kPassiveEventListenersDueToFling)),
76 handle_raf_aligned_input_(
77 base::FeatureList::IsEnabled(features::kRafAlignedInputEvents)),
49 main_task_runner_(main_task_runner), 78 main_task_runner_(main_task_runner),
50 renderer_scheduler_(renderer_scheduler) {} 79 renderer_scheduler_(renderer_scheduler) {}
51 80
52 MainThreadEventQueue::~MainThreadEventQueue() {} 81 MainThreadEventQueue::~MainThreadEventQueue() {}
53 82
54 bool MainThreadEventQueue::HandleEvent( 83 bool MainThreadEventQueue::HandleEvent(
55 ui::ScopedWebInputEvent event, 84 ui::ScopedWebInputEvent event,
56 const ui::LatencyInfo& latency, 85 const ui::LatencyInfo& latency,
57 InputEventDispatchType original_dispatch_type, 86 InputEventDispatchType original_dispatch_type,
58 InputEventAckState ack_result) { 87 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; 132 non_blocking ? DISPATCH_TYPE_NON_BLOCKING : DISPATCH_TYPE_BLOCKING;
104 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type( 133 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type(
105 new EventWithDispatchType(std::move(event), latency, dispatch_type)); 134 new EventWithDispatchType(std::move(event), latency, dispatch_type));
106 135
107 QueueEvent(std::move(event_with_dispatch_type)); 136 QueueEvent(std::move(event_with_dispatch_type));
108 137
109 // send an ack when we are non-blocking. 138 // send an ack when we are non-blocking.
110 return non_blocking; 139 return non_blocking;
111 } 140 }
112 141
113 void MainThreadEventQueue::PopEventOnMainThread() { 142 void MainThreadEventQueue::DispatchInFlightEvent() {
114 { 143 if (in_flight_event_) {
115 base::AutoLock lock(event_queue_lock_); 144 // Report the coalesced count only for continuous events; otherwise
116 if (!events_.empty()) 145 // the zero value would be dominated by non-continuous events.
117 in_flight_event_ = events_.Pop(); 146 base::TimeTicks now = base::TimeTicks::Now();
118 } 147 if (isContinuousEvent(in_flight_event_)) {
148 UMA_HISTOGRAM_CUSTOM_COUNTS(
149 "Event.MainThreadEventQueue.Continuous.QueueingTime",
150 (now - in_flight_event_->creationTimestamp()).InMicroseconds(), 1,
151 kTenSeconds, 50);
119 152
120 if (in_flight_event_) { 153 UMA_HISTOGRAM_CUSTOM_COUNTS(
154 "Event.MainThreadEventQueue.Continuous.FreshnessTime",
155 (now - in_flight_event_->lastCoalescedTimestamp()).InMicroseconds(),
156 1, kTenSeconds, 50);
157
158 UMA_HISTOGRAM_COUNTS_1000("Event.MainThreadEventQueue.CoalescedCount",
159 in_flight_event_->coalescedEventIds().size());
160 } else {
161 UMA_HISTOGRAM_CUSTOM_COUNTS(
162 "Event.MainThreadEventQueue.NonContinuous.QueueingTime",
163 (now - in_flight_event_->creationTimestamp()).InMicroseconds(), 1,
164 kTenSeconds, 50);
165 }
166
121 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType(); 167 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType();
122 if (!in_flight_event_->eventsToAck().empty() && 168 if (!in_flight_event_->coalescedEventIds().empty() &&
123 dispatch_type == DISPATCH_TYPE_BLOCKING) { 169 dispatch_type == DISPATCH_TYPE_BLOCKING) {
124 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN; 170 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN;
125 } 171 }
126 172
127 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(), 173 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(),
128 in_flight_event_->latencyInfo(), 174 in_flight_event_->latencyInfo(),
129 dispatch_type); 175 dispatch_type);
130 } 176 }
131 177
132 in_flight_event_.reset(); 178 in_flight_event_.reset();
133 } 179 }
134 180
181 void MainThreadEventQueue::PossiblyScheduleMainFrame() {
182 if (!handle_raf_aligned_input_)
183 return;
184 bool needs_main_frame = false;
185 {
186 base::AutoLock lock(shared_state_lock_);
187 if (!shared_state_.sent_main_frame_request_ &&
188 !shared_state_.events_.empty() &&
189 isContinuousEvent(shared_state_.events_.front())) {
190 needs_main_frame = !shared_state_.sent_main_frame_request_;
191 shared_state_.sent_main_frame_request_ = false;
192 }
193 }
194 if (needs_main_frame)
195 client_->NeedsMainFrame(routing_id_);
196 }
197
198 void MainThreadEventQueue::DispatchSingleEvent() {
199 {
200 base::AutoLock lock(shared_state_lock_);
201 if (shared_state_.events_.empty())
202 return;
203
204 in_flight_event_ = shared_state_.events_.Pop();
205 }
206 DispatchInFlightEvent();
207 PossiblyScheduleMainFrame();
208 }
209
135 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, 210 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type,
136 InputEventAckState ack_result) { 211 InputEventAckState ack_result) {
137 if (in_flight_event_) { 212 if (in_flight_event_ &&
138 // Send acks for blocking touch events. 213 in_flight_event_->dispatchType() == DISPATCH_TYPE_BLOCKING) {
139 for (const auto id : in_flight_event_->eventsToAck()) { 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