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

Side by Side Diff: content/renderer/input/main_thread_event_queue.cc

Issue 2170913002: Generalize the main thread event queue into a common event queue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix nits Created 4 years, 4 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 #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_wheel = event->type == blink::WebInputEvent::MouseWheel;
33 PendingMouseWheelEvent modified_dispatch_type_event = 65 bool is_touch = blink::WebInputEvent::isTouchEventType(event->type);
34 PendingMouseWheelEvent( 66
35 *static_cast<const blink::WebMouseWheelEvent*>(event), latency, 67 if (is_wheel || is_touch) {
36 dispatch_type); 68 std::unique_ptr<EventWithDispatchType> cloned_event(
69 new EventWithDispatchType(*event, latency, dispatch_type));
37 70
38 // Adjust the |dispatchType| on the event since the compositor 71 // Adjust the |dispatchType| on the event since the compositor
39 // determined all event listeners are passive. 72 // determined all event listeners are passive.
40 if (non_blocking) { 73 if (non_blocking) {
41 modified_dispatch_type_event.event.dispatchType = 74 if (is_wheel) {
42 blink::WebInputEvent::ListenersNonBlockingPassive; 75 static_cast<blink::WebMouseWheelEvent&>(cloned_event->event())
76 .dispatchType = blink::WebInputEvent::ListenersNonBlockingPassive;
77 } else if (is_touch) {
78 static_cast<blink::WebTouchEvent&>(cloned_event->event()).dispatchType =
79 blink::WebInputEvent::ListenersNonBlockingPassive;
80 }
43 } 81 }
44 82
45 if (wheel_events_.state() == WebInputEventQueueState::ITEM_PENDING) { 83 if (is_touch) {
46 wheel_events_.Queue(modified_dispatch_type_event); 84 static_cast<blink::WebTouchEvent&>(cloned_event->event())
85 .dispatchedDuringFling = is_flinging_;
86 }
87
88 if (sent_notification_to_main_) {
89 events_.Queue(std::move(cloned_event));
47 } else { 90 } else {
48 if (non_blocking) { 91 if (non_blocking) {
49 wheel_events_.set_state(WebInputEventQueueState::ITEM_PENDING); 92 sent_notification_to_main_ = true;
50 client_->SendEventToMainThread(routing_id_, 93 client_->SendEventToMainThread(routing_id_, &cloned_event->event(),
51 &modified_dispatch_type_event.event,
52 latency, dispatch_type); 94 latency, dispatch_type);
53 } else { 95 } else {
54 // If there is nothing in the event queue and the event is 96 // If there is nothing in the event queue and the event is
55 // blocking pass the |original_dispatch_type| to avoid 97 // blocking pass the |original_dispatch_type| to avoid
56 // having the main thread call us back as an optimization. 98 // having the main thread call us back as an optimization.
57 client_->SendEventToMainThread(routing_id_, 99 client_->SendEventToMainThread(routing_id_, &cloned_event->event(),
58 &modified_dispatch_type_event.event,
59 latency, original_dispatch_type); 100 latency, original_dispatch_type);
60 } 101 }
61 } 102 }
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 { 103 } else {
93 client_->SendEventToMainThread(routing_id_, event, latency, 104 client_->SendEventToMainThread(routing_id_, event, latency,
94 original_dispatch_type); 105 original_dispatch_type);
95 } 106 }
96 107
97 // send an ack when we are non-blocking. 108 // send an ack when we are non-blocking.
98 return non_blocking; 109 return non_blocking;
99 } 110 }
100 111
101 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, 112 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type,
102 InputEventAckState ack_result) { 113 InputEventAckState ack_result) {
103 if (type == blink::WebInputEvent::MouseWheel) { 114 if (in_flight_event_) {
104 // There should not be two blocking wheel events in flight. 115 // Send acks for blocking touch events.
105 DCHECK(!in_flight_wheel_event_ || 116 for (const auto id : in_flight_event_->eventsToAck())
106 in_flight_wheel_event_->eventsToAck.size() == 0); 117 client_->SendInputEventAck(routing_id_, type, ack_result, id);
107 118 }
108 if (!wheel_events_.empty()) { 119 if (!events_.empty()) {
109 in_flight_wheel_event_ = wheel_events_.Pop(); 120 in_flight_event_ = events_.Pop();
110 client_->SendEventToMainThread( 121 client_->SendEventToMainThread(routing_id_, &in_flight_event_->event(),
111 routing_id_, &in_flight_wheel_event_->event, 122 in_flight_event_->latencyInfo(),
112 in_flight_wheel_event_->latency, in_flight_wheel_event_->type); 123 in_flight_event_->dispatchType());
113 } else { 124 } else {
114 in_flight_wheel_event_.reset(); 125 in_flight_event_.reset();
115 wheel_events_.set_state(WebInputEventQueueState::ITEM_NOT_PENDING); 126 sent_notification_to_main_ = false;
116 } 127 }
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 } 128 }
137 129
138 } // namespace content 130 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/input/main_thread_event_queue.h ('k') | content/renderer/input/main_thread_event_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698