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 #include "content/common/input/event_with_latency_info.h" | |
| 7 #include "content/common/input_messages.h" | |
| 6 | 8 |
| 7 namespace content { | 9 namespace content { |
| 8 | 10 |
| 11 EventWithDispatchType::EventWithDispatchType( | |
| 12 const blink::WebInputEvent& event, | |
| 13 const ui::LatencyInfo& latency, | |
| 14 InputEventDispatchType dispatch_type) | |
| 15 : ScopedWebInputEventWithLatencyInfo(event, latency), | |
| 16 dispatch_type_(dispatch_type) {} | |
| 17 | |
| 18 EventWithDispatchType::~EventWithDispatchType() {} | |
| 19 | |
| 20 bool EventWithDispatchType::CanCoalesceWith( | |
| 21 const EventWithDispatchType& other) const { | |
| 22 return other.dispatch_type_ == dispatch_type_ && | |
| 23 ScopedWebInputEventWithLatencyInfo::CanCoalesceWith(other); | |
| 24 } | |
| 25 | |
| 26 void EventWithDispatchType::CoalesceWith(const EventWithDispatchType& other) { | |
| 27 // If we are blocking and are coalescing touch, make sure to keep | |
| 28 // the touch ids that need to be acked. | |
| 29 if (dispatch_type_ == DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN) { | |
| 30 // We should only have blocking touch events that need coalescing. | |
| 31 DCHECK(blink::WebInputEvent::isTouchEventType(other.event().type)); | |
| 32 eventsToAck_.push_back( | |
| 33 WebInputEventTraits::GetUniqueTouchEventId(other.event())); | |
| 34 } | |
| 35 ScopedWebInputEventWithLatencyInfo::CoalesceWith(other); | |
| 36 } | |
| 37 | |
| 9 MainThreadEventQueue::MainThreadEventQueue(int routing_id, | 38 MainThreadEventQueue::MainThreadEventQueue(int routing_id, |
| 10 MainThreadEventQueueClient* client) | 39 MainThreadEventQueueClient* client) |
| 11 : routing_id_(routing_id), client_(client), is_flinging_(false) {} | 40 : routing_id_(routing_id), |
| 41 client_(client), | |
| 42 is_flinging_(false), | |
| 43 sent_notification_to_main_(false) {} | |
| 12 | 44 |
| 13 MainThreadEventQueue::~MainThreadEventQueue() {} | 45 MainThreadEventQueue::~MainThreadEventQueue() {} |
| 14 | 46 |
| 15 bool MainThreadEventQueue::HandleEvent( | 47 bool MainThreadEventQueue::HandleEvent( |
| 16 const blink::WebInputEvent* event, | 48 const blink::WebInputEvent* event, |
| 17 const ui::LatencyInfo& latency, | 49 const ui::LatencyInfo& latency, |
| 18 InputEventDispatchType original_dispatch_type, | 50 InputEventDispatchType original_dispatch_type, |
| 19 InputEventAckState ack_result) { | 51 InputEventAckState ack_result) { |
| 20 DCHECK(original_dispatch_type == DISPATCH_TYPE_BLOCKING || | 52 DCHECK(original_dispatch_type == DISPATCH_TYPE_BLOCKING || |
| 21 original_dispatch_type == DISPATCH_TYPE_NON_BLOCKING); | 53 original_dispatch_type == DISPATCH_TYPE_NON_BLOCKING); |
| 22 DCHECK(ack_result == INPUT_EVENT_ACK_STATE_SET_NON_BLOCKING || | 54 DCHECK(ack_result == INPUT_EVENT_ACK_STATE_SET_NON_BLOCKING || |
| 23 ack_result == INPUT_EVENT_ACK_STATE_NOT_CONSUMED); | 55 ack_result == INPUT_EVENT_ACK_STATE_NOT_CONSUMED); |
| 24 | 56 |
| 25 bool non_blocking = original_dispatch_type == DISPATCH_TYPE_NON_BLOCKING || | 57 bool non_blocking = original_dispatch_type == DISPATCH_TYPE_NON_BLOCKING || |
| 26 ack_result == INPUT_EVENT_ACK_STATE_SET_NON_BLOCKING; | 58 ack_result == INPUT_EVENT_ACK_STATE_SET_NON_BLOCKING; |
| 27 | 59 |
| 28 InputEventDispatchType dispatch_type = | 60 InputEventDispatchType dispatch_type = |
| 29 non_blocking ? DISPATCH_TYPE_NON_BLOCKING_NOTIFY_MAIN | 61 non_blocking ? DISPATCH_TYPE_NON_BLOCKING_NOTIFY_MAIN |
| 30 : DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN; | 62 : DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN; |
| 31 | 63 |
| 32 if (event->type == blink::WebInputEvent::MouseWheel) { | 64 bool is_touch = blink::WebInputEvent::isTouchEventType(event->type); |
|
tdresser
2016/07/25 14:01:40
Can we have a bool for is_wheel, just to make this
dtapuska
2016/07/25 20:05:24
Done.
| |
| 33 PendingMouseWheelEvent modified_dispatch_type_event = | 65 |
| 34 PendingMouseWheelEvent( | 66 if (event->type == blink::WebInputEvent::MouseWheel || is_touch) { |
| 35 *static_cast<const blink::WebMouseWheelEvent*>(event), latency, | 67 std::unique_ptr<EventWithDispatchType> cloned_event( |
| 36 dispatch_type); | 68 new EventWithDispatchType(*event, latency, dispatch_type)); |
| 37 | 69 |
| 38 // Adjust the |dispatchType| on the event since the compositor | 70 // Adjust the |dispatchType| on the event since the compositor |
| 39 // determined all event listeners are passive. | 71 // determined all event listeners are passive. |
| 40 if (non_blocking) { | 72 if (non_blocking) { |
| 41 modified_dispatch_type_event.event.dispatchType = | 73 if (event->type == blink::WebInputEvent::MouseWheel) { |
| 42 blink::WebInputEvent::ListenersNonBlockingPassive; | 74 static_cast<blink::WebMouseWheelEvent&>(cloned_event->event()) |
| 75 .dispatchType = blink::WebInputEvent::ListenersNonBlockingPassive; | |
| 76 } else if (is_touch) { | |
| 77 static_cast<blink::WebTouchEvent&>(cloned_event->event()).dispatchType = | |
| 78 blink::WebInputEvent::ListenersNonBlockingPassive; | |
| 79 } | |
| 43 } | 80 } |
| 44 | 81 |
| 45 if (wheel_events_.state() == WebInputEventQueueState::ITEM_PENDING) { | 82 if (is_touch) { |
| 46 wheel_events_.Queue(modified_dispatch_type_event); | 83 static_cast<blink::WebTouchEvent&>(cloned_event->event()) |
| 84 .dispatchedDuringFling = is_flinging_; | |
| 85 } | |
| 86 | |
| 87 if (sent_notification_to_main_) { | |
| 88 events_.Queue(std::move(cloned_event)); | |
| 47 } else { | 89 } else { |
| 48 if (non_blocking) { | 90 if (non_blocking) { |
| 49 wheel_events_.set_state(WebInputEventQueueState::ITEM_PENDING); | 91 sent_notification_to_main_ = true; |
| 50 client_->SendEventToMainThread(routing_id_, | 92 client_->SendEventToMainThread(routing_id_, &cloned_event->event(), |
| 51 &modified_dispatch_type_event.event, | |
| 52 latency, dispatch_type); | 93 latency, dispatch_type); |
| 53 } else { | 94 } else { |
| 54 // If there is nothing in the event queue and the event is | 95 // If there is nothing in the event queue and the event is |
| 55 // blocking pass the |original_dispatch_type| to avoid | 96 // blocking pass the |original_dispatch_type| to avoid |
| 56 // having the main thread call us back as an optimization. | 97 // having the main thread call us back as an optimization. |
| 57 client_->SendEventToMainThread(routing_id_, | 98 client_->SendEventToMainThread(routing_id_, &cloned_event->event(), |
| 58 &modified_dispatch_type_event.event, | |
| 59 latency, original_dispatch_type); | 99 latency, original_dispatch_type); |
| 60 } | 100 } |
| 61 } | 101 } |
| 62 } else if (blink::WebInputEvent::isTouchEventType(event->type)) { | |
| 63 PendingTouchEvent modified_dispatch_type_event = | |
| 64 PendingTouchEvent(*static_cast<const blink::WebTouchEvent*>(event), | |
| 65 latency, dispatch_type); | |
| 66 modified_dispatch_type_event.event.dispatchedDuringFling = is_flinging_; | |
| 67 | |
| 68 // Adjust the |dispatchType| on the event since the compositor | |
| 69 // determined all event listeners are passive. | |
| 70 if (non_blocking) { | |
| 71 modified_dispatch_type_event.event.dispatchType = | |
| 72 blink::WebInputEvent::ListenersNonBlockingPassive; | |
| 73 } | |
| 74 | |
| 75 if (touch_events_.state() == WebInputEventQueueState::ITEM_PENDING) { | |
| 76 touch_events_.Queue(modified_dispatch_type_event); | |
| 77 } else { | |
| 78 if (non_blocking) { | |
| 79 touch_events_.set_state(WebInputEventQueueState::ITEM_PENDING); | |
| 80 client_->SendEventToMainThread(routing_id_, | |
| 81 &modified_dispatch_type_event.event, | |
| 82 latency, dispatch_type); | |
| 83 } else { | |
| 84 // If there is nothing in the event queue and the event is | |
| 85 // blocking pass the |original_dispatch_type| to avoid | |
| 86 // having the main thread call us back as an optimization. | |
| 87 client_->SendEventToMainThread(routing_id_, | |
| 88 &modified_dispatch_type_event.event, | |
| 89 latency, original_dispatch_type); | |
| 90 } | |
| 91 } | |
| 92 } else { | 102 } else { |
| 93 client_->SendEventToMainThread(routing_id_, event, latency, | 103 client_->SendEventToMainThread(routing_id_, event, latency, |
| 94 original_dispatch_type); | 104 original_dispatch_type); |
| 95 } | 105 } |
| 96 | 106 |
| 97 // send an ack when we are non-blocking. | 107 // send an ack when we are non-blocking. |
| 98 return non_blocking; | 108 return non_blocking; |
| 99 } | 109 } |
| 100 | 110 |
| 101 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, | 111 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, |
| 102 InputEventAckState ack_result) { | 112 InputEventAckState ack_result) { |
| 103 if (type == blink::WebInputEvent::MouseWheel) { | 113 if (in_flight_event_) { |
| 104 // There should not be two blocking wheel events in flight. | 114 // Send acks for blocking touch events. |
| 105 DCHECK(!in_flight_wheel_event_ || | 115 for (const auto id : in_flight_event_->eventsToAck()) |
| 106 in_flight_wheel_event_->eventsToAck.size() == 0); | 116 client_->SendInputEventAck(routing_id_, type, ack_result, id); |
| 107 | 117 } |
| 108 if (!wheel_events_.empty()) { | 118 if (!events_.empty()) { |
| 109 in_flight_wheel_event_ = wheel_events_.Pop(); | 119 in_flight_event_ = events_.Pop(); |
| 110 client_->SendEventToMainThread( | 120 client_->SendEventToMainThread(routing_id_, &in_flight_event_->event(), |
| 111 routing_id_, &in_flight_wheel_event_->event, | 121 in_flight_event_->latencyInfo(), |
| 112 in_flight_wheel_event_->latency, in_flight_wheel_event_->type); | 122 in_flight_event_->dispatchType()); |
| 113 } else { | 123 } else { |
| 114 in_flight_wheel_event_.reset(); | 124 in_flight_event_.reset(); |
| 115 wheel_events_.set_state(WebInputEventQueueState::ITEM_NOT_PENDING); | 125 sent_notification_to_main_ = false; |
| 116 } | 126 } |
| 117 } else if (blink::WebInputEvent::isTouchEventType(type)) { | |
| 118 if (in_flight_touch_event_) { | |
| 119 // Send acks for blocking touch events. | |
| 120 for (const auto id : in_flight_touch_event_->eventsToAck) | |
| 121 client_->SendInputEventAck(routing_id_, type, ack_result, id); | |
| 122 } | |
| 123 | |
| 124 if (!touch_events_.empty()) { | |
| 125 in_flight_touch_event_ = touch_events_.Pop(); | |
| 126 client_->SendEventToMainThread( | |
| 127 routing_id_, &in_flight_touch_event_->event, | |
| 128 in_flight_touch_event_->latency, in_flight_touch_event_->type); | |
| 129 } else { | |
| 130 in_flight_touch_event_.reset(); | |
| 131 touch_events_.set_state(WebInputEventQueueState::ITEM_NOT_PENDING); | |
| 132 } | |
| 133 } else { | |
| 134 NOTREACHED() << "Invalid passive event type"; | |
| 135 } | |
| 136 } | 127 } |
| 137 | 128 |
| 138 } // namespace content | 129 } // namespace content |
| OLD | NEW |