dtapuska | 4661692 | 2016-03-17 22:52:01 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ |
| 6 | #define CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ |
| 7 | |
| 8 | #include <deque> |
| 9 | #include "content/common/content_export.h" |
| 10 | #include "content/common/input/event_with_latency_info.h" |
| 11 | #include "content/common/input/input_event_ack_state.h" |
| 12 | #include "content/common/input/input_event_dispatch_type.h" |
| 13 | #include "content/common/input/web_input_event_queue.h" |
| 14 | #include "third_party/WebKit/public/web/WebInputEvent.h" |
Nico Weber | 9d6e1294 | 2016-04-19 18:51:09 | [diff] [blame] | 15 | #include "ui/events/latency_info.h" |
dtapuska | 4661692 | 2016-03-17 22:52:01 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | template <typename BaseClass, typename BaseType> |
| 20 | class EventWithDispatchType : public BaseClass { |
| 21 | public: |
| 22 | EventWithDispatchType(const BaseType& e, |
| 23 | const ui::LatencyInfo& l, |
| 24 | InputEventDispatchType t) |
| 25 | : BaseClass(e, l), type(t) {} |
| 26 | |
| 27 | InputEventDispatchType type; |
| 28 | |
| 29 | bool CanCoalesceWith(const EventWithDispatchType& other) const |
| 30 | WARN_UNUSED_RESULT { |
| 31 | return other.type == type && BaseClass::CanCoalesceWith(other); |
| 32 | } |
| 33 | |
| 34 | void CoalesceWith(const EventWithDispatchType& other) { |
| 35 | BaseClass::CoalesceWith(other); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | using PendingMouseWheelEvent = |
| 40 | EventWithDispatchType<MouseWheelEventWithLatencyInfo, |
| 41 | blink::WebMouseWheelEvent>; |
| 42 | |
| 43 | using PendingTouchEvent = |
| 44 | EventWithDispatchType<TouchEventWithLatencyInfo, blink::WebTouchEvent>; |
| 45 | |
| 46 | class CONTENT_EXPORT MainThreadEventQueueClient { |
| 47 | public: |
| 48 | // Send an |event| that was previously queued (possibly |
| 49 | // coalesced with another event) to the |routing_id|'s |
| 50 | // channel. Implementors must implement this callback. |
| 51 | virtual void SendEventToMainThread(int routing_id, |
| 52 | const blink::WebInputEvent* event, |
| 53 | const ui::LatencyInfo& latency, |
| 54 | InputEventDispatchType dispatch_type) = 0; |
| 55 | }; |
| 56 | |
| 57 | // MainThreadEventQueue implements a series of queues (one touch |
| 58 | // and one mouse wheel) for events that need to be queued between |
| 59 | // the compositor and main threads. When an event is sent |
| 60 | // from the compositor to main it can either be sent directly if no |
| 61 | // outstanding events of that type are in flight; or it needs to |
| 62 | // wait in a queue until the main thread has finished processing |
| 63 | // the in-flight event. This class tracks the state and queues |
| 64 | // for the event types. Methods on this class should only be called |
| 65 | // from the compositor thread. |
| 66 | // |
| 67 | // Below some example flows are how the code behaves. |
| 68 | // Legend: B=Browser, C=Compositor, M=Main Thread, NB=Non-blocking |
| 69 | // BL=Blocking, PT=Post Task, ACK=Acknowledgement |
| 70 | // |
| 71 | // Normal blocking event sent to main thread. |
| 72 | // B C M |
| 73 | // ---(BL)--> |
| 74 | // ---(PT)--> |
| 75 | // <-------(ACK)------ |
| 76 | // |
| 77 | // Non-blocking event sent to main thread. |
| 78 | // B C M |
| 79 | // ---(NB)--> |
| 80 | // ---(PT)--> |
| 81 | // <--(PT)--- |
| 82 | // |
| 83 | // Non-blocking followed by blocking event sent to main thread. |
| 84 | // B C M |
| 85 | // ---(NB)--> |
| 86 | // ---(PT)--> |
| 87 | // ---(BL)--> |
| 88 | // <--(PT)--- // Notify from NB event. |
| 89 | // ---(PT)--> // Release blocking event. |
| 90 | // <--(PT)--- // Notify from BL event. |
| 91 | // <-------(ACK)------ |
| 92 | // |
| 93 | class CONTENT_EXPORT MainThreadEventQueue { |
| 94 | public: |
| 95 | MainThreadEventQueue(int routing_id, MainThreadEventQueueClient* client); |
| 96 | ~MainThreadEventQueue(); |
| 97 | |
| 98 | // Called once the compositor has handled |event| and indicated that it is |
| 99 | // a non-blocking event to be queued to the main thread. |
| 100 | bool HandleEvent(const blink::WebInputEvent* event, |
| 101 | const ui::LatencyInfo& latency, |
| 102 | InputEventDispatchType dispatch_type, |
| 103 | InputEventAckState ack_result); |
| 104 | |
| 105 | // Call once the main thread has handled an outstanding |type| event |
| 106 | // in flight. |
| 107 | void EventHandled(blink::WebInputEvent::Type type); |
| 108 | |
lanwei | 8f486d6 | 2016-05-17 18:56:27 | [diff] [blame] | 109 | void set_is_flinging(bool is_flinging) { is_flinging_ = is_flinging; } |
| 110 | |
dtapuska | 4661692 | 2016-03-17 22:52:01 | [diff] [blame] | 111 | private: |
| 112 | friend class MainThreadEventQueueTest; |
| 113 | int routing_id_; |
| 114 | MainThreadEventQueueClient* client_; |
| 115 | WebInputEventQueue<PendingMouseWheelEvent> wheel_events_; |
| 116 | WebInputEventQueue<PendingTouchEvent> touch_events_; |
lanwei | 8f486d6 | 2016-05-17 18:56:27 | [diff] [blame] | 117 | bool is_flinging_; |
dtapuska | 4661692 | 2016-03-17 22:52:01 | [diff] [blame] | 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(MainThreadEventQueue); |
| 120 | }; |
| 121 | |
| 122 | } // namespace content |
| 123 | |
| 124 | #endif // CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ |