• 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 "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(NULL, 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       fake_timestamp_delta_(prop_reg, "Fake Timestamp Delta", 0.0) {
23   InitName();
24 }
25 
SyncInterpretImpl(HardwareState * hwstate,stime_t * timeout)26 void TimestampFilterInterpreter::SyncInterpretImpl(
27     HardwareState* hwstate, stime_t* timeout) {
28   if (fake_timestamp_delta_.val_ == 0.0)
29     ChangeTimestampDefault(hwstate);
30   else
31     ChangeTimestampUsingFake(hwstate);
32   next_->SyncInterpret(hwstate, timeout);
33 }
34 
ChangeTimestampDefault(HardwareState * hwstate)35 void TimestampFilterInterpreter::ChangeTimestampDefault(
36     HardwareState* hwstate) {
37   // Check if this is the first event or there has been a jump backwards.
38   if (prev_msc_timestamp_ < 0.0 ||
39       hwstate->msc_timestamp == 0.0 ||
40       hwstate->msc_timestamp < prev_msc_timestamp_) {
41     msc_timestamp_offset_ = hwstate->timestamp - hwstate->msc_timestamp;
42   }
43   prev_msc_timestamp_ = hwstate->msc_timestamp;
44 
45   stime_t new_timestamp = hwstate->msc_timestamp + msc_timestamp_offset_;
46   skew_ = new_timestamp - hwstate->timestamp;
47   hwstate->timestamp = new_timestamp;
48 
49   hwstate->msc_timestamp = 0.0;
50 }
51 
ChangeTimestampUsingFake(HardwareState * hwstate)52 void TimestampFilterInterpreter::ChangeTimestampUsingFake(
53     HardwareState* hwstate) {
54   fake_timestamp_ += fake_timestamp_delta_.val_;
55   if (fabs(fake_timestamp_ - hwstate->timestamp) >
56       fake_timestamp_max_divergence_)
57     fake_timestamp_ = hwstate->timestamp;
58 
59   skew_ = fake_timestamp_ - hwstate->timestamp;
60   hwstate->timestamp = fake_timestamp_;
61 }
62 
ConsumeGesture(const Gesture & gs)63 void TimestampFilterInterpreter::ConsumeGesture(const Gesture& gs) {
64   // Adjust gesture timestamp by latest skew to match browser clock
65   Gesture copy = gs;
66   copy.start_time -= skew_;
67   copy.end_time -= skew_;
68   ProduceGesture(copy);
69 }
70 
71 }  // namespace gestures
72