aboutsummaryrefslogtreecommitdiff
path: root/demos/widgetsgallery/swaphook.cpp
blob: 926c31e76abdba86cca438f672c6f8d3a5b1ae9e (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "swaphook.h"

#include <cstdio>
#include <ctime>

#include <QObject>
#include <QEvent>
#include <QDebug>
#include <QTimer>

#include <MApplication>
#include <MWindow>

#ifdef EGL
#include <dlfcn.h>
#endif

class SwapHookPrivate : public QObject
{
    Q_OBJECT
public:
    SwapHookPrivate();
    ~SwapHookPrivate();

    bool eventFilter(QObject *object, QEvent *e);

    void frameFinished();

#ifdef EGL
    EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface);
    typedef EGLAPI EGLBoolean EGLAPIENTRY (*eglSwapBuffers_ptr)(EGLDisplay dpy, EGLSurface surface);
    eglSwapBuffers_ptr func;
#else
    void *func;
#endif

    void *lib;
    bool lurk;
    timestamp lurkBegin;
    bool firstTimestamp;
    QLinkedList<timestamp> stamps;
};

class Clock
{
public:
    static timestamp time();
};

#ifdef EGL
extern "C" EGLAPI EGLBoolean EGLAPIENTRY eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
{
    return SwapHook::instance()->d->eglSwapBuffers(dpy, surface);
}
#endif

timestamp Clock::time()
{
    // TODO: port this to QElapsedTimer once we have Qt 4.7
#ifdef Q_OS_LINUX
    struct timespec now;
    if(clock_gettime(CLOCK_MONOTONIC, &now))
        qFatal("clock_gettime errored");
    return ((timestamp)(now.tv_sec))*1000 + now.tv_nsec / 1000000;
#else
    qFatal("swap monitoring on this operating system is not supported right now");
    return 0;
#endif
}

SwapHookPrivate::SwapHookPrivate() :
        QObject(),
        func(0),
        lib(0),
        lurk(false),
        lurkBegin(0),
        firstTimestamp(true),
        stamps()
{
#ifdef EGL
    QString preload = qgetenv("LD_PRELOAD");

    QStringList libs = preload.split(':', QString::SkipEmptyParts);
    libs << QString("libEGL.so");

    foreach (const QString& candidate, libs) {

        lib = dlopen(candidate.toAscii().data(), RTLD_NOW);

        if(!lib) {
            qWarning("%s:%d: can't dlopen %s (%s)\n", __FILE__, __LINE__, qPrintable(candidate), dlerror());
            continue;
        }

        func = (eglSwapBuffers_ptr)dlsym(lib, "eglSwapBuffers");
        if(!func) {
            qWarning("%s:%d: can't find eglSwapBuffers from %s (%s)\n", __FILE__, __LINE__, qPrintable(candidate), dlerror());
            continue;
        }
        return;
    }
    qFatal("%s:%d: failed to redirect eglSwapBuffers()", __FILE__, __LINE__);
#endif
}

SwapHookPrivate::~SwapHookPrivate()
{
#ifdef EGL
    if(lib) {
        dlclose(lib);
    }
#endif
}

bool SwapHookPrivate::eventFilter(QObject *object, QEvent *e) {
    if (e->type() == QEvent::Paint) {
        frameFinished();
    }

    return QObject::eventFilter(object, e);
}

#ifdef EGL
EGLBoolean SwapHookPrivate::eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
{
    if (!func)
        qFatal("SwapHook object not properly initialized");

    frameFinished();

    return func(dpy, surface);
}
#endif


void SwapHookPrivate::frameFinished() {
    if (lurk) {
        timestamp ts = Clock::time();
        if (firstTimestamp) {
            lurkBegin = ts;
            firstTimestamp = false;
        }
        stamps.push_back(ts);
        if (!MApplication::showFps()) {
            // Update again on return to the main loop, in order to refresh
            // the scene as frequently as possible for benchmark purposes.
            QTimer::singleShot(0, MApplication::activeWindow()->viewport(), SLOT(update()));
        }
    }
}

SwapHook::SwapHook()
    : QObject(),
      d(new SwapHookPrivate)
{
}

SwapHook::~SwapHook()
{
}

SwapHook* SwapHook::instance() {
    static SwapHook *gSwapHook = 0;
    if (!gSwapHook) {
        gSwapHook = new SwapHook();
    }
    return gSwapHook;
}

void SwapHook::startLurking()
{
    if(d->lurk)
        return;

    if (!d->func) {
        // fallback to even filtering as swap buffer monitoring is not available
        MApplication::activeWindow()->viewport()->installEventFilter(d.data());
    }

    d->stamps.clear();
    d->lurk = true;
    d->firstTimestamp = true;
    MApplication::activeWindow()->viewport()->update();
}

void SwapHook::stopLurking()
{
    if(!(d->lurk))
        return;

    if (!d->func) {
        MApplication::activeWindow()->viewport()->removeEventFilter(d.data());
    }

    d->lurk = false;
}

QLinkedList<timestamp> SwapHook::timestamps() {
    return d->stamps;
}

#include "swaphook.moc"