summaryrefslogtreecommitdiffstats
path: root/src/debug/gdk-event-latency-tracker.cpp
blob: 8e34d2fc2c5a0a64530bf025ea29632e52dbe2d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Inkscape::Debug::GdkEventLatencyTracker - tracks backlog of GDK events
 *
 * Authors:
 *   MenTaLguY <mental@rydia.net>
 *
 * Copyright (C) 2008 MenTaLguY
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "debug/gdk-event-latency-tracker.h"

namespace Inkscape {
namespace Debug {

GdkEventLatencyTracker::GdkEventLatencyTracker()
: start_seconds(0.0), max_latency(0.0)
{
    elapsed.stop();
    elapsed.reset();
}

boost::optional<double> GdkEventLatencyTracker::process(GdkEvent const *event) {
    guint32 const timestamp=gdk_event_get_time(const_cast<GdkEvent *>(event));
    if (timestamp == GDK_CURRENT_TIME) {
        return boost::optional<double>();
    }
    double const timestamp_seconds = timestamp / 1000.0;

    if (start_seconds == 0.0) {
        elapsed.start();
        start_seconds = timestamp_seconds;
        return boost::optional<double>(0.0);
    } else {
        double const current_seconds = elapsed.elapsed() + start_seconds;
        double delta = current_seconds - timestamp_seconds;
        if (delta < 0.0) {
            start_seconds += -delta;
            delta = 0.0;
        } else if (delta > max_latency) {
            max_latency = delta;
        }
        return boost::optional<double>(delta);
    }
}

GdkEventLatencyTracker &GdkEventLatencyTracker::default_tracker() {
    static GdkEventLatencyTracker tracker;
    return tracker;
}

}
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :