| 1 |
/* |
| 2 |
* Copyright 2014 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "rendertimer.h" |
| 20 |
|
| 21 |
#include <QtCore/QPair> |
| 22 |
#include <QtGui/QOpenGLContext> |
| 23 |
|
| 24 |
#include <EGL/egl.h> |
| 25 |
#include <EGL/eglext.h> |
| 26 |
|
| 27 |
#if !defined(GL_TIME_ELAPSED) |
| 28 |
#define GL_TIME_ELAPSED 0x88BF |
| 29 |
#endif |
| 30 |
|
| 31 |
#if !defined(GL_TIMESTAMP) |
| 32 |
#define GL_TIMESTAMP 0x8E28 |
| 33 |
#endif |
| 34 |
|
| 35 |
class RenderTimerPrivate |
| 36 |
{ |
| 37 |
public: |
| 38 |
|
| 39 |
RenderTimerPrivate() : |
| 40 |
m_type(RenderTimer::Trivial) |
| 41 |
{ } |
| 42 |
|
| 43 |
RenderTimer::TimerType m_type; |
| 44 |
QElapsedTimer m_trivialTimer; |
| 45 |
|
| 46 |
struct { |
| 47 |
void (QOPENGLF_APIENTRYP genFencesNV)(GLsizei n, GLuint* fences); |
| 48 |
void (QOPENGLF_APIENTRYP deleteFencesNV)(GLsizei n, const GLuint* fences); |
| 49 |
void (QOPENGLF_APIENTRYP setFenceNV)(GLuint fence, GLenum condition); |
| 50 |
void (QOPENGLF_APIENTRYP finishFenceNV)(GLuint fence); |
| 51 |
} m_fenceNV; |
| 52 |
GLuint m_fence[2]; |
| 53 |
|
| 54 |
struct { |
| 55 |
EGLSyncKHR (QOPENGLF_APIENTRYP createSyncKHR)(EGLDisplay dpy, EGLenum type, |
| 56 |
const EGLint* attrib_list); |
| 57 |
EGLBoolean (QOPENGLF_APIENTRYP destroySyncKHR)(EGLDisplay dpy, EGLSyncKHR sync); |
| 58 |
EGLint (QOPENGLF_APIENTRYP clientWaitSyncKHR)(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, |
| 59 |
EGLTimeKHR timeout); |
| 60 |
} m_fenceSyncKHR; |
| 61 |
EGLSyncKHR m_beforeSync; |
| 62 |
|
| 63 |
struct { |
| 64 |
void (QOPENGLF_APIENTRYP genQueries)(GLsizei n, GLuint* ids); |
| 65 |
void (QOPENGLF_APIENTRYP deleteQueries)(GLsizei n, const GLuint* ids); |
| 66 |
void (QOPENGLF_APIENTRYP beginQuery)(GLenum target, GLuint id); |
| 67 |
void (QOPENGLF_APIENTRYP endQuery)(GLenum target); |
| 68 |
void (QOPENGLF_APIENTRYP getQueryObjectui64v)(GLuint id, GLenum pname, GLuint64* params); |
| 69 |
void (QOPENGLF_APIENTRYP getQueryObjectui64vExt)(GLuint id, GLenum pname, GLuint64EXT* params); |
| 70 |
void (QOPENGLF_APIENTRYP queryCounter)(GLuint id, GLenum target); |
| 71 |
} m_timerQuery; |
| 72 |
enum { TimerQueryUnavailable, TimerQueryCore, TimerQueryExt } m_timerQueryVersion; |
| 73 |
GLuint m_timer[2]; |
| 74 |
}; |
| 75 |
|
| 76 |
RenderTimer::RenderTimer(QObject* parent) : |
| 77 |
QObject(parent) |
| 78 |
{ |
| 79 |
d_ptr = new RenderTimerPrivate; |
| 80 |
} |
| 81 |
|
| 82 |
RenderTimer::~RenderTimer() |
| 83 |
{ |
| 84 |
delete d_ptr; |
| 85 |
} |
| 86 |
|
| 87 |
bool RenderTimer::isAvailable(TimerType type) |
| 88 |
{ |
| 89 |
auto context = QOpenGLContext::currentContext(); |
| 90 |
|
| 91 |
if (type == RenderTimer::Trivial) { |
| 92 |
return true; |
| 93 |
|
| 94 |
} else if (type == RenderTimer::KHRFence) { |
| 95 |
if (!context->isOpenGLES()) |
| 96 |
return false; |
| 97 |
QList<QByteArray> eglExtensions = QByteArray( |
| 98 |
static_cast<const char*>(eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS))).split(' '); |
| 99 |
// Note the workaround for PowerVR that declares 'GL_OES_egl_sync'. |
| 100 |
return eglExtensions.contains("EGL_KHR_fence_sync") && |
| 101 |
(context->hasExtension(QByteArrayLiteral("GL_OES_EGL_sync")) || |
| 102 |
context->hasExtension(QByteArrayLiteral("GL_OES_egl_sync"))); |
| 103 |
|
| 104 |
} else if (type == RenderTimer::NVFence) { |
| 105 |
return context->hasExtension(QByteArrayLiteral("GL_NV_fence")); |
| 106 |
|
| 107 |
} else if (type == RenderTimer::ARBTimerQuery) { |
| 108 |
// We could use the thin QOpenGLTimerQuery wrapper from Qt 5.1, but the lack of a method to check |
| 109 |
// the presence of glQueryCounter() would force us to inspect OpenGL version and extensions, which |
| 110 |
// is basically as annoying as doing the whole thing here. |
| 111 |
// TODO(loicm) Add an hasQuerycounter() method to QOpenGLTimerQuery. |
| 112 |
if (context->isOpenGLES()) |
| 113 |
return false; |
| 114 |
QSurfaceFormat format = context->format(); |
| 115 |
return qMakePair(format.majorVersion(), format.minorVersion()) >= qMakePair(3, 2) |
| 116 |
&& context->hasExtension(QByteArrayLiteral("GL_ARB_timer_query")); |
| 117 |
|
| 118 |
} else if (type == RenderTimer::EXTTimerQuery) { |
| 119 |
if (context->isOpenGLES()) |
| 120 |
return false; |
| 121 |
return context->hasExtension(QByteArrayLiteral("GL_EXT_timer_query")); |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
RenderTimer::TimerType RenderTimer::optimalTimerType() |
| 128 |
{ |
| 129 |
QList<TimerType> types { |
| 130 |
RenderTimer::ARBTimerQuery, |
| 131 |
RenderTimer::EXTTimerQuery, |
| 132 |
RenderTimer::KHRFence, |
| 133 |
RenderTimer::NVFence, |
| 134 |
}; |
| 135 |
|
| 136 |
Q_FOREACH(TimerType type, types) { |
| 137 |
// isAvailable will automatically reject type that is not suitable |
| 138 |
// for the current GL variant (desktop or GLES). |
| 139 |
if (RenderTimer::isAvailable(type)) { |
| 140 |
return type; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return RenderTimer::Trivial; |
| 145 |
} |
| 146 |
|
| 147 |
void RenderTimer::setup(TimerType type) |
| 148 |
{ |
| 149 |
Q_D(RenderTimer); |
| 150 |
if (type == RenderTimer::Automatic) { |
| 151 |
type = RenderTimer::optimalTimerType(); |
| 152 |
} |
| 153 |
|
| 154 |
d->m_type = type; |
| 155 |
|
| 156 |
if (d->m_type == RenderTimer::Trivial) { |
| 157 |
|
| 158 |
} else if (d->m_type == RenderTimer::KHRFence) { |
| 159 |
d->m_fenceSyncKHR.createSyncKHR = reinterpret_cast< |
| 160 |
EGLSyncKHR (QOPENGLF_APIENTRYP)(EGLDisplay, EGLenum, const EGLint*)>( |
| 161 |
eglGetProcAddress("eglCreateSyncKHR")); |
| 162 |
d->m_fenceSyncKHR.destroySyncKHR = reinterpret_cast< |
| 163 |
EGLBoolean (QOPENGLF_APIENTRYP)(EGLDisplay, EGLSyncKHR)>( |
| 164 |
eglGetProcAddress("eglDestroySyncKHR")); |
| 165 |
d->m_fenceSyncKHR.clientWaitSyncKHR = reinterpret_cast< |
| 166 |
EGLint (QOPENGLF_APIENTRYP)(EGLDisplay, EGLSyncKHR, EGLint, EGLTimeKHR)>( |
| 167 |
eglGetProcAddress("eglClientWaitSyncKHR")); |
| 168 |
|
| 169 |
} else if (d->m_type == RenderTimer::NVFence) { |
| 170 |
d->m_fenceNV.genFencesNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, GLuint*)>( |
| 171 |
eglGetProcAddress("glGenFencesNV")); |
| 172 |
d->m_fenceNV.deleteFencesNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, const GLuint*)>( |
| 173 |
eglGetProcAddress("glDeleteFencesNV")); |
| 174 |
d->m_fenceNV.setFenceNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum)>( |
| 175 |
eglGetProcAddress("glSetFenceNV")); |
| 176 |
d->m_fenceNV.finishFenceNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint)>( |
| 177 |
eglGetProcAddress("glFinishFenceNV")); |
| 178 |
d->m_fenceNV.genFencesNV(2, d->m_fence); |
| 179 |
|
| 180 |
} else if (d->m_type == RenderTimer::ARBTimerQuery) { |
| 181 |
QOpenGLContext* context = QOpenGLContext::currentContext(); |
| 182 |
d->m_timerQuery.genQueries = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, GLuint*)>( |
| 183 |
context->getProcAddress("glGenQueries")); |
| 184 |
d->m_timerQuery.deleteQueries = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, const GLuint*)>( |
| 185 |
context->getProcAddress("glDeleteQueries")); |
| 186 |
d->m_timerQuery.getQueryObjectui64v = |
| 187 |
reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum, GLuint64*)>( |
| 188 |
context->getProcAddress("glGetQueryObjectui64v")); |
| 189 |
d->m_timerQuery.queryCounter = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum)>( |
| 190 |
context->getProcAddress("glQueryCounter")); |
| 191 |
d->m_timerQueryVersion = RenderTimerPrivate::TimerQueryCore; |
| 192 |
d->m_timerQuery.genQueries(2, d->m_timer); |
| 193 |
|
| 194 |
} else if (d->m_type == RenderTimer::EXTTimerQuery) { |
| 195 |
QOpenGLContext* context = QOpenGLContext::currentContext(); |
| 196 |
d->m_timerQuery.genQueries = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, GLuint*)>( |
| 197 |
context->getProcAddress("glGenQueries")); |
| 198 |
d->m_timerQuery.deleteQueries = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, const GLuint*)>( |
| 199 |
context->getProcAddress("glDeleteQueries")); |
| 200 |
d->m_timerQuery.beginQuery = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum, GLuint)>( |
| 201 |
context->getProcAddress("glBeginQuery")); |
| 202 |
d->m_timerQuery.endQuery = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum)>( |
| 203 |
context->getProcAddress("glEndQuery")); |
| 204 |
d->m_timerQuery.getQueryObjectui64vExt = |
| 205 |
reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum, GLuint64EXT*)>( |
| 206 |
context->getProcAddress("glGetQueryObjectui64vEXT")); |
| 207 |
d->m_timerQueryVersion = RenderTimerPrivate::TimerQueryExt; |
| 208 |
d->m_timerQuery.genQueries(1, d->m_timer); |
| 209 |
|
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void RenderTimer::teardown() |
| 214 |
{ |
| 215 |
teardown(d_ptr->m_type); |
| 216 |
} |
| 217 |
|
| 218 |
void RenderTimer::teardown(TimerType type) |
| 219 |
{ |
| 220 |
Q_D(RenderTimer); |
| 221 |
if (type == RenderTimer::Trivial) { |
| 222 |
|
| 223 |
} else if (type == RenderTimer::KHRFence) { |
| 224 |
if (d->m_beforeSync != EGL_NO_SYNC_KHR) { |
| 225 |
d->m_fenceSyncKHR.destroySyncKHR(eglGetCurrentDisplay(), d->m_beforeSync); |
| 226 |
} |
| 227 |
|
| 228 |
} else if (type == RenderTimer::NVFence) { |
| 229 |
d->m_fenceNV.deleteFencesNV(2, d->m_fence); |
| 230 |
|
| 231 |
} else if (type == RenderTimer::ARBTimerQuery) { |
| 232 |
d->m_timerQuery.deleteQueries(2, d->m_timer); |
| 233 |
|
| 234 |
} else if (type == RenderTimer::EXTTimerQuery) { |
| 235 |
d->m_timerQuery.deleteQueries(1, d->m_timer); |
| 236 |
|
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void RenderTimer::start() |
| 241 |
{ |
| 242 |
Q_D(RenderTimer); |
| 243 |
if (d->m_type == RenderTimer::Trivial) { |
| 244 |
d-> m_trivialTimer.start(); |
| 245 |
|
| 246 |
} else if (d->m_type == RenderTimer::KHRFence) { |
| 247 |
d->m_beforeSync = d->m_fenceSyncKHR.createSyncKHR(eglGetCurrentDisplay(), EGL_SYNC_FENCE_KHR, NULL); |
| 248 |
|
| 249 |
} else if (d->m_type == RenderTimer::NVFence) { |
| 250 |
d->m_fenceNV.setFenceNV(d->m_fence[0], GL_ALL_COMPLETED_NV); |
| 251 |
|
| 252 |
} else if (d->m_type == RenderTimer::ARBTimerQuery) { |
| 253 |
d->m_timerQuery.queryCounter(d->m_timer[0], GL_TIMESTAMP); |
| 254 |
|
| 255 |
} else if (d->m_type == RenderTimer::EXTTimerQuery) { |
| 256 |
d->m_timerQuery.beginQuery(GL_TIME_ELAPSED, d->m_timer[0]); |
| 257 |
|
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
qint64 RenderTimer::stop() |
| 262 |
{ |
| 263 |
Q_D(RenderTimer); |
| 264 |
if (d->m_type == RenderTimer::Trivial) { |
| 265 |
auto functions = QOpenGLContext::currentContext()->functions(); |
| 266 |
functions->glFinish(); |
| 267 |
return d-> m_trivialTimer.nsecsElapsed(); |
| 268 |
|
| 269 |
} else if (d->m_type == RenderTimer::KHRFence) { |
| 270 |
QElapsedTimer timer; |
| 271 |
EGLDisplay dpy = eglGetCurrentDisplay(); |
| 272 |
EGLSyncKHR afterSync = d->m_fenceSyncKHR.createSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL); |
| 273 |
EGLint beforeSyncValue = d->m_fenceSyncKHR.clientWaitSyncKHR(dpy, d->m_beforeSync, 0, EGL_FOREVER_KHR); |
| 274 |
qint64 beforeTime = timer.nsecsElapsed(); |
| 275 |
EGLint afterSyncValue = d->m_fenceSyncKHR.clientWaitSyncKHR(dpy, afterSync, 0, EGL_FOREVER_KHR); |
| 276 |
qint64 afterTime = timer.nsecsElapsed(); |
| 277 |
d->m_fenceSyncKHR.destroySyncKHR(dpy, afterSync); |
| 278 |
d->m_fenceSyncKHR.destroySyncKHR(dpy, d->m_beforeSync); |
| 279 |
d->m_beforeSync = EGL_NO_SYNC_KHR; |
| 280 |
if (beforeSyncValue == EGL_CONDITION_SATISFIED_KHR |
| 281 |
&& afterSyncValue == EGL_CONDITION_SATISFIED_KHR) { |
| 282 |
return afterTime - beforeTime; |
| 283 |
} else { |
| 284 |
return -1; |
| 285 |
} |
| 286 |
|
| 287 |
} else if (d->m_type == RenderTimer::NVFence) { |
| 288 |
QElapsedTimer timer; |
| 289 |
d->m_fenceNV.setFenceNV(d->m_fence[1], GL_ALL_COMPLETED_NV); |
| 290 |
d->m_fenceNV.finishFenceNV(d->m_fence[0]); |
| 291 |
qint64 beforeTime = timer.nsecsElapsed(); |
| 292 |
d->m_fenceNV.finishFenceNV(d->m_fence[1]); |
| 293 |
qint64 afterTime = timer.nsecsElapsed(); |
| 294 |
return afterTime - beforeTime; |
| 295 |
|
| 296 |
} else if (d->m_type == RenderTimer::ARBTimerQuery) { |
| 297 |
GLuint64 time[2] = { 0, 0 }; |
| 298 |
d->m_timerQuery.queryCounter(d->m_timer[1], GL_TIMESTAMP); |
| 299 |
d->m_timerQuery.getQueryObjectui64v(d->m_timer[0], GL_QUERY_RESULT, &time[0]); |
| 300 |
d->m_timerQuery.getQueryObjectui64v(d->m_timer[1], GL_QUERY_RESULT, &time[1]); |
| 301 |
if (time[0] != 0 && time[1] != 0) { |
| 302 |
return time[1] - time[0]; |
| 303 |
} else { |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
} else if (d->m_type == RenderTimer::EXTTimerQuery) { |
| 308 |
GLuint64EXT time; |
| 309 |
d->m_timerQuery.endQuery(GL_TIME_ELAPSED); |
| 310 |
d->m_timerQuery.getQueryObjectui64vExt(d->m_timer[0], GL_QUERY_RESULT, &time); |
| 311 |
return static_cast<qint64>(time); |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |