| 1 |
// Copyright © 2016 Canonical Ltd. |
| 2 |
// Author: Loïc Molinari <[email protected]> |
| 3 |
// |
| 4 |
// This file is part of Lomiri UI Toolkit. |
| 5 |
// |
| 6 |
// Lomiri UI Toolkit is free software: you can redistribute it and/or modify it |
| 7 |
// under the terms of the GNU Lesser General Public License as published by the |
| 8 |
// Free Software Foundation; version 3. |
| 9 |
// |
| 10 |
// Lomiri UI Toolkit is distributed in the hope that it will be useful, but |
| 11 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 13 |
// for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#ifndef GPUTIMER_P_H |
| 19 |
#define GPUTIMER_P_H |
| 20 |
|
| 21 |
#include <QtGui/QOpenGLFunctions> |
| 22 |
|
| 23 |
#include <EGL/egl.h> |
| 24 |
#include <EGL/eglext.h> |
| 25 |
|
| 26 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 27 |
#include <LomiriMetrics-Qt6/private/lomirimetricsglobal_p.h> |
| 28 |
#else |
| 29 |
#include <LomiriMetrics/private/lomirimetricsglobal_p.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
// GPUTimer is used to measure the amount of time taken by the GPU to fully |
| 33 |
// complete a set of graphics commands. As opposed to a basic timer which would |
| 34 |
// determine the time taken by the graphics driver to push the graphics commands |
| 35 |
// in the command buffer from the CPU, this timer pushes dedicated |
| 36 |
// synchronization commands to the command buffer, which the GPU signals |
| 37 |
// whenever completed. That allows one to get accurate GPU timings. |
| 38 |
class LOMIRI_METRICS_PRIVATE_EXPORT GPUTimer |
| 39 |
{ |
| 40 |
public: |
| 41 |
GPUTimer() : |
| 42 |
#if !defined QT_NO_DEBUG |
| 43 |
m_context(nullptr), m_started(false), |
| 44 |
#endif |
| 45 |
m_type(Unset) {} |
| 46 |
|
| 47 |
// Allocates/Deletes the OpenGL resources. finalize() is not called at |
| 48 |
// destruction, it must be explicitly called to free the resources at the |
| 49 |
// right time in a thread with the same OpenGL context bound than at |
| 50 |
// initialize(). |
| 51 |
void initialize(); |
| 52 |
void finalize(); |
| 53 |
|
| 54 |
// Starts/Stops the timer. stop() returns the time elapsed in nanoseconds |
| 55 |
// since the call to start(). Calling start()/stop() two times in a row |
| 56 |
// triggers an assertion in debug builds and leads to undefined results in |
| 57 |
// non-debug builds. Must be called in a thread with the same OpenGL context |
| 58 |
// bound than at initialize(). |
| 59 |
void start(); |
| 60 |
quint64 stop(); |
| 61 |
|
| 62 |
private: |
| 63 |
enum Type { |
| 64 |
Unset, |
| 65 |
Finish, |
| 66 |
KHRFence, |
| 67 |
NVFence, |
| 68 |
ARBTimerQuery, |
| 69 |
EXTTimerQuery |
| 70 |
}; |
| 71 |
|
| 72 |
#if !defined QT_NO_DEBUG |
| 73 |
QOpenGLContext* m_context; |
| 74 |
bool m_started; |
| 75 |
#endif |
| 76 |
Type m_type; |
| 77 |
|
| 78 |
struct { |
| 79 |
void (QOPENGLF_APIENTRYP genFencesNV)(GLsizei n, GLuint* fences); |
| 80 |
void (QOPENGLF_APIENTRYP deleteFencesNV)(GLsizei n, const GLuint* fences); |
| 81 |
void (QOPENGLF_APIENTRYP setFenceNV)(GLuint fence, GLenum condition); |
| 82 |
void (QOPENGLF_APIENTRYP finishFenceNV)(GLuint fence); |
| 83 |
} m_fenceNV; |
| 84 |
GLuint m_fence[2]; |
| 85 |
|
| 86 |
struct { |
| 87 |
EGLSyncKHR (QOPENGLF_APIENTRYP createSyncKHR)(EGLDisplay dpy, EGLenum type, |
| 88 |
const EGLint* attrib_list); |
| 89 |
EGLBoolean (QOPENGLF_APIENTRYP destroySyncKHR)(EGLDisplay dpy, EGLSyncKHR sync); |
| 90 |
EGLint (QOPENGLF_APIENTRYP clientWaitSyncKHR)(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, |
| 91 |
EGLTimeKHR timeout); |
| 92 |
} m_fenceSyncKHR; |
| 93 |
EGLSyncKHR m_beforeSync; |
| 94 |
|
| 95 |
struct { |
| 96 |
void (QOPENGLF_APIENTRYP genQueries)(GLsizei n, GLuint* ids); |
| 97 |
void (QOPENGLF_APIENTRYP deleteQueries)(GLsizei n, const GLuint* ids); |
| 98 |
void (QOPENGLF_APIENTRYP beginQuery)(GLenum target, GLuint id); |
| 99 |
void (QOPENGLF_APIENTRYP endQuery)(GLenum target); |
| 100 |
void (QOPENGLF_APIENTRYP getQueryObjectui64v)(GLuint id, GLenum pname, GLuint64* params); |
| 101 |
void (QOPENGLF_APIENTRYP getQueryObjectui64vExt)(GLuint id, GLenum pname, |
| 102 |
GLuint64EXT* params); |
| 103 |
void (QOPENGLF_APIENTRYP queryCounter)(GLuint id, GLenum target); |
| 104 |
} m_timerQuery; |
| 105 |
GLuint m_timer[2]; |
| 106 |
}; |
| 107 |
|
| 108 |
#endif // GPUTIMER_P_H |