1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Inkscape::Debug::GdkEventLatencyTracker - tracks backlog of GDK events
4  *
5  * Authors:
6  *   MenTaLguY <mental@rydia.net>
7  *
8  * Copyright (C) 2008 MenTaLguY
9  *
10  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11  */
12 
13 #include <gdk/gdk.h>
14 
15 #include "debug/gdk-event-latency-tracker.h"
16 #include "preferences.h"
17 
18 namespace Inkscape {
19 namespace Debug {
20 
GdkEventLatencyTracker()21 GdkEventLatencyTracker::GdkEventLatencyTracker()
22     : start_seconds(0.0), max_latency(0.0), skew(1.0), last_elapsed(0.0), last_seconds(0.0)
23 {
24     elapsed.stop();
25     elapsed.reset();
26 }
27 
process(GdkEvent const * event)28 std::optional<double> GdkEventLatencyTracker::process(GdkEvent const *event) {
29     guint32 const timestamp=gdk_event_get_time(const_cast<GdkEvent *>(event));
30     if (timestamp == GDK_CURRENT_TIME) {
31         return std::optional<double>();
32     }
33     double const timestamp_seconds = timestamp / 1000.0;
34 
35     if (start_seconds == 0.0) {
36         elapsed.start();
37         start_seconds = timestamp_seconds;
38         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
39         skew = prefs->getDoubleLimited("/debug/latency/skew", 1.0, 0.5, 2.0);
40         return std::optional<double>(0.0);
41     } else {
42         last_elapsed = elapsed.elapsed();
43         last_seconds = timestamp_seconds;
44         double const current_seconds = (last_elapsed * skew) + start_seconds;
45         double delta = current_seconds - timestamp_seconds;
46         if (delta < 0.0) {
47             start_seconds += -delta;
48             delta = 0.0;
49         } else if (delta > max_latency) {
50             max_latency = delta;
51         }
52         return std::optional<double>(delta);
53     }
54 }
55 
getSkew()56 double GdkEventLatencyTracker::getSkew() {
57     double val = 0.0;
58     if ((last_elapsed > 0.0) && (last_seconds > 0.0)) {
59         val = (last_seconds - start_seconds) / last_elapsed;
60     }
61     return val;
62 }
63 
default_tracker()64 GdkEventLatencyTracker &GdkEventLatencyTracker::default_tracker() {
65     static GdkEventLatencyTracker tracker;
66     return tracker;
67 }
68 
69 }
70 }
71 
72 /*
73   Local Variables:
74   mode:c++
75   c-file-style:"stroustrup"
76   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
77   indent-tabs-mode:nil
78   fill-column:99
79   End:
80 */
81 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
82