motion_event_buffer.h 3.6 KB

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