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 events processed per RAF task. | |
| 16 const size_t kMaxEventsPerRafTask = 10; | |
|
tdresser
2016/08/24 13:42:59
I don't completely understand this constant.
Supp
dtapuska
2016/08/24 17:10:06
So you want to send them all to the main thread at
| |
| 17 | |
| 18 bool isContiniousEvent(const std::unique_ptr<EventWithDispatchType>& event) { | |
|
tdresser
2016/08/24 13:42:59
Continuous
dtapuska
2016/08/24 17:10:06
done; face palm.
| |
| 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 coalesced_timestamp_(creation_timestamp_) {} | |
| 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 coalesced_timestamp_ = base::TimeTicks::Now(); | |
| 36 } | 52 } |
| 37 | 53 |
| 38 MainThreadEventQueue::MainThreadEventQueue( | 54 MainThreadEventQueue::MainThreadEventQueue( |
| 39 int routing_id, | 55 int routing_id, |
| 40 MainThreadEventQueueClient* client, | 56 MainThreadEventQueueClient* client, |
| 41 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, | 57 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, |
| 42 blink::scheduler::RendererScheduler* renderer_scheduler) | 58 blink::scheduler::RendererScheduler* renderer_scheduler, |
| 59 bool handle_raf_aligned_input) | |
| 43 : routing_id_(routing_id), | 60 : routing_id_(routing_id), |
| 44 client_(client), | 61 client_(client), |
| 45 is_flinging_(false), | 62 is_flinging_(false), |
| 46 last_touch_start_forced_nonblocking_due_to_fling_(false), | 63 last_touch_start_forced_nonblocking_due_to_fling_(false), |
| 47 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled( | 64 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled( |
| 48 features::kPassiveEventListenersDueToFling)), | 65 features::kPassiveEventListenersDueToFling)), |
| 66 handle_raf_aligned_input_(handle_raf_aligned_input), | |
| 67 sent_main_frame_request_(false), | |
| 49 main_task_runner_(main_task_runner), | 68 main_task_runner_(main_task_runner), |
| 50 renderer_scheduler_(renderer_scheduler) {} | 69 renderer_scheduler_(renderer_scheduler) {} |
| 51 | 70 |
| 52 MainThreadEventQueue::~MainThreadEventQueue() {} | 71 MainThreadEventQueue::~MainThreadEventQueue() {} |
| 53 | 72 |
| 54 bool MainThreadEventQueue::HandleEvent( | 73 bool MainThreadEventQueue::HandleEvent( |
| 55 ui::ScopedWebInputEvent event, | 74 ui::ScopedWebInputEvent event, |
| 56 const ui::LatencyInfo& latency, | 75 const ui::LatencyInfo& latency, |
| 57 InputEventDispatchType original_dispatch_type, | 76 InputEventDispatchType original_dispatch_type, |
| 58 InputEventAckState ack_result) { | 77 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; | 122 non_blocking ? DISPATCH_TYPE_NON_BLOCKING : DISPATCH_TYPE_BLOCKING; |
| 104 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type( | 123 std::unique_ptr<EventWithDispatchType> event_with_dispatch_type( |
| 105 new EventWithDispatchType(std::move(event), latency, dispatch_type)); | 124 new EventWithDispatchType(std::move(event), latency, dispatch_type)); |
| 106 | 125 |
| 107 QueueEvent(std::move(event_with_dispatch_type)); | 126 QueueEvent(std::move(event_with_dispatch_type)); |
| 108 | 127 |
| 109 // send an ack when we are non-blocking. | 128 // send an ack when we are non-blocking. |
| 110 return non_blocking; | 129 return non_blocking; |
| 111 } | 130 } |
| 112 | 131 |
| 113 void MainThreadEventQueue::PopEventOnMainThread() { | 132 void MainThreadEventQueue::DispatchInFlightEvent() { |
| 114 { | 133 if (in_flight_event_) { |
| 115 base::AutoLock lock(event_queue_lock_); | 134 // Report the coalesced count only for continious events; otherwise |
| 116 if (!events_.empty()) | 135 // the zero value would be dominated by non-continious events. |
| 117 in_flight_event_ = events_.Pop(); | 136 if (isContiniousEvent(in_flight_event_)) { |
| 118 } | 137 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 138 "Event.MainThreadEventQueue.Continious.QueueingTime", | |
| 139 (base::TimeTicks::Now() - in_flight_event_->creationTimestamp()) | |
| 140 .ToInternalValue(), | |
| 141 1, 10000000, 100); | |
| 119 | 142 |
| 120 if (in_flight_event_) { | 143 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 144 "Event.MainThreadEventQueue.Continious.FreshnessTime", | |
| 145 (base::TimeTicks::Now() - in_flight_event_->coalescedTimestamp()) | |
| 146 .ToInternalValue(), | |
| 147 1, 10000000, 100); | |
| 148 | |
| 149 UMA_HISTOGRAM_COUNTS_100("Event.MainThreadEventQueue.CoalescedCount", | |
| 150 in_flight_event_->coalescedEventIds().size()); | |
| 151 } else { | |
| 152 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 153 "Event.MainThreadEventQueue.NonContinious.QueueingTime", | |
| 154 (base::TimeTicks::Now() - in_flight_event_->creationTimestamp()) | |
| 155 .ToInternalValue(), | |
| 156 1, 10000000, 100); | |
| 157 } | |
| 158 | |
| 121 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType(); | 159 InputEventDispatchType dispatch_type = in_flight_event_->dispatchType(); |
| 122 if (!in_flight_event_->eventsToAck().empty() && | 160 if (!in_flight_event_->coalescedEventIds().empty() && |
| 123 dispatch_type == DISPATCH_TYPE_BLOCKING) { | 161 dispatch_type == DISPATCH_TYPE_BLOCKING) { |
| 124 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN; | 162 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN; |
| 125 } | 163 } |
| 126 | 164 |
| 127 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(), | 165 client_->HandleEventOnMainThread(routing_id_, &in_flight_event_->event(), |
| 128 in_flight_event_->latencyInfo(), | 166 in_flight_event_->latencyInfo(), |
| 129 dispatch_type); | 167 dispatch_type); |
| 130 } | 168 } |
| 131 | 169 |
| 132 in_flight_event_.reset(); | 170 in_flight_event_.reset(); |
| 133 } | 171 } |
| 134 | 172 |
| 173 void MainThreadEventQueue::PossiblyScheduleMainFrame() { | |
| 174 if (!handle_raf_aligned_input_) | |
| 175 return; | |
| 176 bool needs_main_frame = false; | |
| 177 { | |
| 178 base::AutoLock lock(event_queue_lock_); | |
| 179 if (!sent_main_frame_request_ && !events_.empty() && | |
| 180 isContiniousEvent(events_.front())) { | |
| 181 needs_main_frame = !sent_main_frame_request_; | |
| 182 sent_main_frame_request_ = false; | |
| 183 } | |
| 184 } | |
| 185 if (needs_main_frame) | |
| 186 client_->NeedsMainFrame(routing_id_); | |
| 187 } | |
| 188 | |
| 189 void MainThreadEventQueue::DispatchSingleEvent() { | |
| 190 { | |
| 191 base::AutoLock lock(event_queue_lock_); | |
| 192 if (events_.empty()) | |
| 193 return; | |
| 194 | |
| 195 in_flight_event_ = events_.Pop(); | |
| 196 } | |
| 197 DispatchInFlightEvent(); | |
| 198 PossiblyScheduleMainFrame(); | |
| 199 } | |
| 200 | |
| 135 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, | 201 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, |
| 136 InputEventAckState ack_result) { | 202 InputEventAckState ack_result) { |
| 137 if (in_flight_event_) { | 203 if (in_flight_event_ && |
| 138 // Send acks for blocking touch events. | 204 in_flight_event_->dispatchType() == DISPATCH_TYPE_BLOCKING) { |
| 139 for (const auto id : in_flight_event_->eventsToAck()) { | 205 // We should only have blocking touch events that need coalescing. |
| 206 for (const auto id : in_flight_event_->coalescedEventIds()) { | |
| 140 client_->SendInputEventAck(routing_id_, type, ack_result, id); | 207 client_->SendInputEventAck(routing_id_, type, ack_result, id); |
| 141 if (renderer_scheduler_) { | 208 if (renderer_scheduler_) { |
| 142 renderer_scheduler_->DidHandleInputEventOnMainThread( | 209 renderer_scheduler_->DidHandleInputEventOnMainThread( |
| 143 in_flight_event_->event()); | 210 in_flight_event_->event()); |
| 144 } | 211 } |
| 145 } | 212 } |
| 146 } | 213 } |
| 147 } | 214 } |
| 148 | 215 |
| 216 void MainThreadEventQueue::DispatchRafAlignedInput() { | |
| 217 if (!handle_raf_aligned_input_) | |
| 218 return; | |
| 219 | |
| 220 { | |
| 221 base::AutoLock lock(event_queue_lock_); | |
| 222 sent_main_frame_request_ = false; | |
| 223 } | |
| 224 | |
| 225 for (size_t i = 0; i < kMaxEventsPerRafTask; ++i) { | |
| 226 { | |
| 227 base::AutoLock lock(event_queue_lock_); | |
| 228 if (events_.empty()) | |
| 229 break; | |
| 230 | |
| 231 if (!isContiniousEvent(events_.front())) | |
| 232 break; | |
| 233 in_flight_event_ = events_.Pop(); | |
| 234 } | |
| 235 DispatchInFlightEvent(); | |
| 236 } | |
| 237 PossiblyScheduleMainFrame(); | |
| 238 } | |
| 239 | |
| 149 void MainThreadEventQueue::SendEventNotificationToMainThread() { | 240 void MainThreadEventQueue::SendEventNotificationToMainThread() { |
| 150 main_task_runner_->PostTask( | 241 main_task_runner_->PostTask( |
| 151 FROM_HERE, base::Bind(&MainThreadEventQueue::PopEventOnMainThread, | 242 FROM_HERE, base::Bind(&MainThreadEventQueue::DispatchSingleEvent, this)); |
| 152 this)); | |
| 153 } | 243 } |
| 154 | 244 |
| 155 void MainThreadEventQueue::QueueEvent( | 245 void MainThreadEventQueue::QueueEvent( |
| 156 std::unique_ptr<EventWithDispatchType> event) { | 246 std::unique_ptr<EventWithDispatchType> event) { |
| 157 bool send_notification = false; | 247 bool isContinious = isContiniousEvent(event); |
|
tdresser
2016/08/24 13:42:59
is_continuous
dtapuska
2016/08/24 17:10:06
done
| |
| 248 size_t send_notification_count = 0; | |
| 249 bool needs_main_frame = false; | |
| 158 { | 250 { |
| 159 base::AutoLock lock(event_queue_lock_); | 251 base::AutoLock lock(event_queue_lock_); |
| 160 size_t size_before = events_.size(); | 252 size_t size_before = events_.size(); |
| 161 events_.Queue(std::move(event)); | 253 events_.Queue(std::move(event)); |
| 162 send_notification = events_.size() != size_before; | 254 size_t size_after = events_.size(); |
| 255 if (size_before != size_after) { | |
| 256 if (!handle_raf_aligned_input_) { | |
| 257 send_notification_count = 1; | |
| 258 } else if (!isContinious) { | |
| 259 send_notification_count = 1; | |
| 260 // If we had just enqueued a non-RAF input event we will send a series | |
| 261 // of normal post messages to ensure they are all handled right away. | |
| 262 for (size_t pos = size_after - 1; pos >= 1; --pos) { | |
| 263 if (isContiniousEvent(events_.at(pos - 1))) | |
| 264 send_notification_count++; | |
| 265 else | |
| 266 break; | |
| 267 } | |
| 268 } else { | |
| 269 needs_main_frame = !sent_main_frame_request_; | |
| 270 sent_main_frame_request_ = true; | |
| 271 } | |
| 272 } | |
| 163 } | 273 } |
| 164 if (send_notification) | 274 for (size_t i = 0; i < send_notification_count; ++i) |
| 165 SendEventNotificationToMainThread(); | 275 SendEventNotificationToMainThread(); |
| 276 if (needs_main_frame) | |
| 277 client_->NeedsMainFrame(routing_id_); | |
| 166 } | 278 } |
| 167 | 279 |
| 168 } // namespace content | 280 } // namespace content |
| OLD | NEW |