1 // Copyright 2014 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.
5 #ifndef UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_BUFFER_H_
6 #define UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_BUFFER_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/time/time.h"
11 #include "ui/events/gesture_detection/gesture_detection_export.h"
16 class MotionEventGeneric
;
18 // Allows event forwarding and flush requests from a |MotionEventBuffer|.
19 class MotionEventBufferClient
{
21 virtual ~MotionEventBufferClient() {}
22 virtual void ForwardMotionEvent(const MotionEvent
& event
) = 0;
23 virtual void SetNeedsFlush() = 0;
26 // Utility class for buffering streamed MotionEventVector until a given flush.
27 // Events that can be combined will remain buffered, and depending on the flush
28 // time and buffered events, a resampled event with history will be synthesized.
29 // The primary purpose of this class is to ensure a smooth output motion signal
30 // by resampling a discrete input signal that may run on a different frequency
31 // or lack alignment with the output display signal.
32 // Note that this class is largely based on code from Android's existing touch
33 // pipeline (in particular, logic from ImageTransport, http://goo.gl/Ixsb0D).
34 // See the design doc at http://goo.gl/MdmpCf for more details.
35 class GESTURE_DETECTION_EXPORT MotionEventBuffer
{
37 // The provided |client| must not be null, and |enable_resampling| determines
38 // resampling behavior (see |resample_|).
39 MotionEventBuffer(MotionEventBufferClient
* client
, bool enable_resampling
);
42 // Should be called upon receipt of an event from the platform, prior to event
43 // dispatch to UI or content components. Events that can be coalesced will
44 // remain buffered until the next |Flush()|, while other events will be
45 // forwarded immediately (incidentally flushing currently buffered events).
46 void OnMotionEvent(const MotionEvent
& event
);
48 // Forward any buffered events, resampling if necessary (see |resample_|)
49 // according to the provided |frame_time|. This should be called in response
50 // to |SetNeedsFlush()| calls on the client. If the buffer is empty, no
51 // events will be forwarded, and if another flush is necessary it will be
53 void Flush(base::TimeTicks frame_time
);
56 typedef ScopedVector
<MotionEventGeneric
> MotionEventVector
;
58 void FlushWithResampling(MotionEventVector events
,
59 base::TimeTicks resample_time
);
60 void FlushWithoutResampling(MotionEventVector events
);
62 MotionEventBufferClient
* const client_
;
63 MotionEventVector buffered_events_
;
65 // Time of the most recently extrapolated event. This will be 0 if the
66 // last sent event was not extrapolated. Used internally to guard against
67 // conflicts between events received from the platform that may have an
68 // earlier timestamp than that synthesized at the latest resample.
69 base::TimeTicks last_extrapolated_event_time_
;
71 // Whether buffered events should be resampled upon |Flush()|. If true, short
72 // horizon interpolation/extrapolation will be used to synthesize the
73 // forwarded event. Otherwise the most recently buffered event will be
74 // forwarded, with preceding events as historical entries. Defaults to true.
77 DISALLOW_COPY_AND_ASSIGN(MotionEventBuffer
);
82 #endif // UI_EVENTS_GESTURE_DETECTION_MOTION_EVENT_BUFFER_H_