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