xref: /aosp_15_r20/external/libchrome-gestures/include/timestamp_filter_interpreter.h (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
1 // Copyright 2017 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <gtest/gtest.h>  // for FRIEND_TEST
6 
7 #include "include/filter_interpreter.h"
8 #include "include/gestures.h"
9 #include "include/prop_registry.h"
10 #include "include/tracer.h"
11 
12 #ifndef GESTURES_TIMESTAMP_FILTER_INTERPRETER_H_
13 #define GESTURES_TIMESTAMP_FILTER_INTERPRETER_H_
14 
15 // This class fixes up the timestamp of the hardware state. There are three
16 // possibilities:
17 //   1) hwstate.timestamp is reliable.
18 //   2) hwstate.timestamp may be unreliable, but a reliable
19 //      hwstate.msc_timestamp has been provided.
20 //   3) hwstate.timestamp and hwstate.msc_timestamp are both unreliable.
21 // ComputeTimestampDefault handles the first two cases, and
22 // ComputeTimestampUsingFake handles the third case.
23 
24 namespace gestures {
25 
26 class TimestampFilterInterpreter : public FilterInterpreter {
27   FRIEND_TEST(TimestampFilterInterpreterTest, FakeTimestampTest);
28   FRIEND_TEST(TimestampFilterInterpreterTest, FakeTimestampJumpForwardTest);
29   FRIEND_TEST(TimestampFilterInterpreterTest, FakeTimestampFallBackwardTest);
30   FRIEND_TEST(TimestampFilterInterpreterTest, GestureDebugTest);
31   FRIEND_TEST(TimestampFilterInterpreterParmTest, TimestampDebugLoggingTest);
32  public:
33   // Takes ownership of |next|:
34   explicit TimestampFilterInterpreter(PropRegistry* prop_reg,
35                                       Interpreter* next,
36                                       Tracer* tracer);
~TimestampFilterInterpreter()37   virtual ~TimestampFilterInterpreter() {}
38 
39  protected:
40   virtual void SyncInterpretImpl(HardwareState& hwstate, stime_t* timeout);
41   virtual void HandleTimerImpl(stime_t now, stime_t* timeout);
42 
43  private:
44 
45   // Before this function is applied, there are two possibilities:
46   //   1) hwstate.timestamp == CLOCK_MONOTONIC &&
47   //      hwstate.msc_timestamp == 0.0
48   //        - No changes are needed in this case
49   //   2) hwstate.timestamp == CLOCK_MONOTONIC &&
50   //      hwstate.msc_timestamp == MSC_TIMESTAMP
51   //        - MSC_TIMESTAMP will be more accurate than CLOCK_MONOTONIC, so we
52   //          want to use it for time deltas in the gesture library.  However,
53   //          MSC_TIMESTAMP will reset to 0.0 if there are no touch events for
54   //          at least 1 second. So whenever MSC_TIMESTAMP resets, we record the
55   //          offset between CLOCK_MONOTONIC and MSC_TIMESTAMP and add this
56   //          offset to subsequent events.
57   // After this function is applied:
58   //   - hwstate.timestamp uses CLOCK_MONOTONIC as the time base, possibly with
59   //     fine tuning provided by MSC_TIMESTAMP.
60   //   - hwstate.msc_timestamp should not be used.
61   void ChangeTimestampDefault(
62       HardwareState& hwstate,
63       ActivityLog::TimestampHardwareStateDebug& debug_data);
64 
65   // If neither hwstate.timestamp nor hwstate.msc_timestamp has reliable
66   // deltas, we use fake_timestamp_delta_ as the delta between consecutive
67   // reports, but don't allow our faked timestamp to diverge too far from
68   // hwstate.timestamp.
69   void ChangeTimestampUsingFake(
70       HardwareState& hwstate,
71       ActivityLog::TimestampHardwareStateDebug& debug_data);
72 
73   void ConsumeGesture(const Gesture& gs);
74 
75   template<typename T>
LogDebugData(const T & debug_data)76   void LogDebugData(const T& debug_data) {
77     using EventDebug = ActivityLog::EventDebug;
78     if (EventDebugLoggingIsEnabled(EventDebug::Timestamp))
79       log_->LogDebugData(debug_data);
80   }
81 
82   stime_t prev_msc_timestamp_;
83 
84   // Difference between msc_timestamp and timestamp as of last timestamp reset.
85   stime_t msc_timestamp_offset_;
86 
87   // If we are using fake timestamps, this holds the most recent fake
88   stime_t fake_timestamp_;
89   // Maximum we let fake_timestamp_ diverge from hwstate.timestamp
90   stime_t fake_timestamp_max_divergence_;
91 
92   // The difference between the original timestamp and the timestamp after
93   // adjustment by this interpreter. When contact begins this will be zero, but
94   // the two clocks may get out of sync by a small amount as time goes on
95   stime_t skew_;
96 
97   // Maximum skew_ since the last reset.
98   stime_t max_skew_;
99 
100   // If we don't have a reliable timestamp, we use this as the timestamp delta.
101   DoubleProperty fake_timestamp_delta_;
102 };
103 
104 }  // namespace gestures
105 
106 #endif  // GESTURES_TIMESTAMP_FILTER_INTERPRETER_H_
107