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 "include/timestamp_filter_interpreter.h"
6
7 #include <math.h>
8
9 #include "include/logging.h"
10 #include "include/tracer.h"
11
12 namespace gestures {
13
TimestampFilterInterpreter(PropRegistry * prop_reg,Interpreter * next,Tracer * tracer)14 TimestampFilterInterpreter::TimestampFilterInterpreter(
15 PropRegistry* prop_reg, Interpreter* next, Tracer* tracer)
16 : FilterInterpreter(nullptr, next, tracer, false),
17 prev_msc_timestamp_(-1.0),
18 msc_timestamp_offset_(-1.0),
19 fake_timestamp_(-1.0),
20 fake_timestamp_max_divergence_(0.1),
21 skew_(0.0),
22 max_skew_(0.0),
23 fake_timestamp_delta_(prop_reg, "Fake Timestamp Delta", 0.0) {
24 InitName();
25 }
26
SyncInterpretImpl(HardwareState & hwstate,stime_t * timeout)27 void TimestampFilterInterpreter::SyncInterpretImpl(
28 HardwareState& hwstate, stime_t* timeout) {
29 const char name[] = "TimestampFilterInterpreter::SyncInterpretImpl";
30 LogHardwareStatePre(name, hwstate);
31 auto debug_data = ActivityLog::TimestampHardwareStateDebug{};
32
33 if (fake_timestamp_delta_.val_ == 0.0)
34 ChangeTimestampDefault(hwstate, debug_data);
35 else
36 ChangeTimestampUsingFake(hwstate, debug_data);
37
38 LogDebugData(debug_data);
39 LogHardwareStatePost(name, hwstate);
40 next_->SyncInterpret(hwstate, timeout);
41 }
42
ChangeTimestampDefault(HardwareState & hwstate,ActivityLog::TimestampHardwareStateDebug & debug_data)43 void TimestampFilterInterpreter::ChangeTimestampDefault(
44 HardwareState& hwstate,
45 ActivityLog::TimestampHardwareStateDebug& debug_data) {
46 // Check if this is the first event or there has been a jump backwards.
47 debug_data.prev_msc_timestamp_in = prev_msc_timestamp_;
48 if (prev_msc_timestamp_ < 0.0 ||
49 hwstate.msc_timestamp == 0.0 ||
50 hwstate.msc_timestamp < prev_msc_timestamp_) {
51 msc_timestamp_offset_ = hwstate.timestamp - hwstate.msc_timestamp;
52 max_skew_ = 0.0;
53 debug_data.was_first_or_backward = true;
54 }
55 prev_msc_timestamp_ = hwstate.msc_timestamp;
56 debug_data.prev_msc_timestamp_out = prev_msc_timestamp_;
57
58 stime_t new_timestamp = hwstate.msc_timestamp + msc_timestamp_offset_;
59 skew_ = new_timestamp - hwstate.timestamp;
60 max_skew_ = std::max(max_skew_, skew_);
61 hwstate.timestamp = new_timestamp;
62
63 hwstate.msc_timestamp = 0.0;
64 debug_data.skew = skew_;
65 debug_data.max_skew = max_skew_;
66 }
67
ChangeTimestampUsingFake(HardwareState & hwstate,ActivityLog::TimestampHardwareStateDebug & debug_data)68 void TimestampFilterInterpreter::ChangeTimestampUsingFake(
69 HardwareState& hwstate,
70 ActivityLog::TimestampHardwareStateDebug& debug_data) {
71 debug_data.is_using_fake = true;
72 debug_data.fake_timestamp_in = fake_timestamp_;
73 debug_data.fake_timestamp_delta = fake_timestamp_delta_.val_;
74 fake_timestamp_ += fake_timestamp_delta_.val_;
75 if (fabs(fake_timestamp_ - hwstate.timestamp) >
76 fake_timestamp_max_divergence_) {
77 fake_timestamp_ = hwstate.timestamp;
78 max_skew_ = 0.0;
79 debug_data.was_divergence_reset = true;
80 }
81 debug_data.fake_timestamp_out = fake_timestamp_;
82
83 skew_ = fake_timestamp_ - hwstate.timestamp;
84 max_skew_ = std::max(max_skew_, skew_);
85 hwstate.timestamp = fake_timestamp_;
86 debug_data.skew = skew_;
87 debug_data.max_skew = max_skew_;
88 }
89
HandleTimerImpl(stime_t now,stime_t * timeout)90 void TimestampFilterInterpreter::HandleTimerImpl(stime_t now,
91 stime_t* timeout) {
92 const char name[] = "TimestampFilterInterpreter::HandleTimerImpl";
93 LogHandleTimerPre(name, now, timeout);
94
95 // Adjust the timestamp by the largest skew_ since reset. This ensures that
96 // the callback isn't ignored because it looks like it's coming too early.
97 now += max_skew_;
98 next_->HandleTimer(now, timeout);
99
100 LogHandleTimerPost(name, now, timeout);
101 }
102
ConsumeGesture(const Gesture & gs)103 void TimestampFilterInterpreter::ConsumeGesture(const Gesture& gs) {
104 const char name[] = "TimestampFilterInterpreter::ConsumeGesture";
105 LogGestureConsume(name, gs);
106 auto debug_data = ActivityLog::TimestampGestureDebug{ skew_ };
107
108 // Adjust gesture timestamp by latest skew to match browser clock
109 Gesture copy = gs;
110 copy.start_time -= skew_;
111 copy.end_time -= skew_;
112
113 LogDebugData(debug_data);
114 LogGestureProduce(name, copy);
115 ProduceGesture(copy);
116 }
117
118 } // namespace gestures
119