• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  public:
31   // Takes ownership of |next|:
32   explicit TimestampFilterInterpreter(PropRegistry* prop_reg,
33                                       Interpreter* next,
34                                       Tracer* tracer);
~TimestampFilterInterpreter()35   virtual ~TimestampFilterInterpreter() {}
36 
37  protected:
38   virtual void SyncInterpretImpl(HardwareState* hwstate, stime_t* timeout);
39 
40  private:
41 
42   // Before this function is applied, there are two possibilities:
43   //   1) hwstate->timestamp == CLOCK_MONOTONIC &&
44   //      hwstate->msc_timestamp == 0.0
45   //        - No changes are needed in this case
46   //   2) hwstate->timestamp == CLOCK_MONOTONIC &&
47   //      hwstate->msc_timestamp == MSC_TIMESTAMP
48   //        - MSC_TIMESTAMP will be more accurate than CLOCK_MONOTONIC, so we
49   //          want to use it for time deltas in the gesture library.  However,
50   //          MSC_TIMESTAMP will reset to 0.0 if there are no touch events for
51   //          at least 1 second. So whenever MSC_TIMESTAMP resets, we record the
52   //          offset between CLOCK_MONOTONIC and MSC_TIMESTAMP and add this
53   //          offset to subsequent events.
54   // After this function is applied:
55   //   - hwstate->timestamp uses CLOCK_MONOTONIC as the time base, possibly with
56   //     fine tuning provided by MSC_TIMESTAMP.
57   //   - hwstate->msc_timestamp should not be used.
58   void ChangeTimestampDefault(HardwareState* hwstate);
59 
60   // If neither hwstate->timestamp nor hwstate->msc_timestamp has reliable
61   // deltas, we use fake_timestamp_delta_ as the delta between consecutive
62   // reports, but don't allow our faked timestamp to diverge too far from
63   // hwstate->timestamp.
64   void ChangeTimestampUsingFake(HardwareState* hwstate);
65 
66   void ConsumeGesture(const Gesture& gs);
67 
68   stime_t prev_msc_timestamp_;
69 
70   // Difference between msc_timestamp and timestamp as of last timestamp reset.
71   stime_t msc_timestamp_offset_;
72 
73   // If we are using fake timestamps, this holds the most recent fake
74   stime_t fake_timestamp_;
75   // Maximum we let fake_timestamp_ diverge from hwstate->timestamp
76   stime_t fake_timestamp_max_divergence_;
77 
78   // The difference between the original timestamp and the timestamp after
79   // adjustment by this interpreter. When contact begins this will be zero, but
80   // the two clocks may get out of sync by a small amount as time goes on
81   stime_t skew_;
82 
83   // If we don't have a reliable timestamp, we use this as the timestamp delta.
84   DoubleProperty fake_timestamp_delta_;
85 };
86 
87 }  // namespace gestures
88 
89 #endif  // GESTURES_TIMESTAMP_FILTER_INTERPRETER_H_
90