Debian Codegraph @

src/LomiriMetrics/gputimer.cpp

raw
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 #include "gputimer_p.h"
19
20 #include <QtCore/QElapsedTimer>
21
22 #include "lomirimetricsglobal_p.h"
23
24 #if !defined(GL_TIME_ELAPSED)
25 #define GL_TIME_ELAPSED 0x88BF // For GL_EXT_timer_query.
26 #endif
27
28 #if !defined(GL_TIMESTAMP)
29 #define GL_TIMESTAMP 0x8E28
30 #endif
31
32 void GPUTimer::initialize()
33 {
34 DASSERT(QOpenGLContext::currentContext());
35 DASSERT(m_type == Unset);
36
37 auto context = QOpenGLContext::currentContext();
38 #if !defined QT_NO_DEBUG
39 m_context = context;
40 #endif
41
42 QSurfaceFormat format = context->format();
43
44 if (format.renderableType() == QSurfaceFormat::OpenGLES) {
45 QList<QByteArray> eglExtensions = QByteArray(
46 static_cast<const char*>(
47 eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS))).split(' ');
48
49 // KHRFence.
50 if (eglExtensions.contains("EGL_KHR_fence_sync")
51 && (context->hasExtension(QByteArrayLiteral("GL_OES_EGL_sync"))
52 || context->hasExtension(QByteArrayLiteral("GL_OES_egl_sync")) /*PowerVR fix*/)) {
53 m_fenceSyncKHR.createSyncKHR = reinterpret_cast<
54 EGLSyncKHR (QOPENGLF_APIENTRYP)(EGLDisplay, EGLenum, const EGLint*)>(
55 eglGetProcAddress("eglCreateSyncKHR"));
56 m_fenceSyncKHR.destroySyncKHR = reinterpret_cast<
57 EGLBoolean (QOPENGLF_APIENTRYP)(EGLDisplay, EGLSyncKHR)>(
58 eglGetProcAddress("eglDestroySyncKHR"));
59 m_fenceSyncKHR.clientWaitSyncKHR = reinterpret_cast<
60 EGLint (QOPENGLF_APIENTRYP)(EGLDisplay, EGLSyncKHR, EGLint, EGLTimeKHR)>(
61 eglGetProcAddress("eglClientWaitSyncKHR"));
62 m_type = KHRFence;
63 DLOG("GPUTimer is based on GL_OES_EGL_sync");
64 return;
65 }
66 }
67
68 if (format.renderableType() == QSurfaceFormat::OpenGL) {
69 // We could use the thin QOpenGLTimerQuery wrapper from Qt 5.1, but the lack
70 // of a method to check the presence of glQueryCounter() would force us to
71 // inspect OpenGL version and extensions, which is basically as annoying as
72 // doing the whole thing here.
73 // TODO(loicm) Add an hasQuerycounter() method to QOpenGLTimerQuery.
74
75 // ARBTimerQuery.
76 if (qMakePair(format.majorVersion(), format.minorVersion()) >= qMakePair(3, 2)
77 && context->hasExtension(QByteArrayLiteral("GL_ARB_timer_query"))) {
78 m_timerQuery.genQueries = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, GLuint*)>(
79 context->getProcAddress("glGenQueries"));
80 m_timerQuery.deleteQueries =
81 reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, const GLuint*)>(
82 context->getProcAddress("glDeleteQueries"));
83 m_timerQuery.getQueryObjectui64v =
84 reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum, GLuint64*)>(
85 context->getProcAddress("glGetQueryObjectui64v"));
86 m_timerQuery.queryCounter = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum)>(
87 context->getProcAddress("glQueryCounter"));
88 m_timerQuery.genQueries(2, m_timer);
89 m_type = ARBTimerQuery;
90 DLOG("GPUTimer is based on GL_ARB_timer_query");
91 return;
92 // EXTTimerQuery.
93 } else if (context->hasExtension(QByteArrayLiteral("GL_EXT_timer_query"))) {
94 m_timerQuery.genQueries = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, GLuint*)>(
95 context->getProcAddress("glGenQueries"));
96 m_timerQuery.deleteQueries =
97 reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, const GLuint*)>(
98 context->getProcAddress("glDeleteQueries"));
99 m_timerQuery.beginQuery = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum, GLuint)>(
100 context->getProcAddress("glBeginQuery"));
101 m_timerQuery.endQuery = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum)>(
102 context->getProcAddress("glEndQuery"));
103 m_timerQuery.getQueryObjectui64vExt =
104 reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum, GLuint64EXT*)>(
105 context->getProcAddress("glGetQueryObjectui64vEXT"));
106 m_timerQuery.genQueries(1, m_timer);
107 m_type = EXTTimerQuery;
108 DLOG("GPUTimer is based on GL_EXT_timer_query");
109 return;
110 }
111 }
112
113 // NVFence. Might be implemented by OpenGL or OpenGL ES implemenntations.
114 if (context->hasExtension(QByteArrayLiteral("GL_NV_fence"))) {
115 m_fenceNV.genFencesNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, GLuint*)>(
116 eglGetProcAddress("glGenFencesNV"));
117 m_fenceNV.deleteFencesNV =
118 reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLsizei, const GLuint*)>(
119 eglGetProcAddress("glDeleteFencesNV"));
120 m_fenceNV.setFenceNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint, GLenum)>(
121 eglGetProcAddress("glSetFenceNV"));
122 m_fenceNV.finishFenceNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint)>(
123 eglGetProcAddress("glFinishFenceNV"));
124 m_fenceNV.genFencesNV(2, m_fence);
125 m_type = NVFence;
126 DLOG("GPUTimer is based on GL_NV_fence");
127 return;
128 }
129
130 m_type = Finish;
131 DLOG("GPUTimer is based on glFinish");
132 }
133
134 void GPUTimer::finalize()
135 {
136 DASSERT(m_context == QOpenGLContext::currentContext());
137 DASSERT(m_type != Unset);
138
139 #if !defined QT_NO_DEBUG
140 m_context = nullptr;
141 #endif
142
143 // KHRFence.
144 if (m_type == KHRFence) {
145 if (m_beforeSync != EGL_NO_SYNC_KHR) {
146 m_fenceSyncKHR.destroySyncKHR(eglGetCurrentDisplay(), m_beforeSync);
147 }
148 m_type = Unset;
149
150 // NVFence.
151 } else if (m_type == NVFence) {
152 m_fenceNV.deleteFencesNV(2, m_fence);
153 m_type = Unset;
154 }
155
156 // ARBTimerQuery.
157 else if (m_type == ARBTimerQuery) {
158 m_timerQuery.deleteQueries(2, m_timer);
159 m_type = Unset;
160
161 // EXTTimerQuery.
162 } else if (m_type == EXTTimerQuery) {
163 m_timerQuery.deleteQueries(1, m_timer);
164 m_type = Unset;
165 }
166 }
167
168 void GPUTimer::start()
169 {
170 DASSERT(m_context == QOpenGLContext::currentContext());
171 DASSERT(m_type != Unset);
172 DASSERT(!m_started);
173
174 #if !defined QT_NO_DEBUG
175 m_started = true;
176 #endif
177
178 // KHRFence.
179 if (m_type == KHRFence) {
180 m_beforeSync = m_fenceSyncKHR.createSyncKHR(
181 eglGetCurrentDisplay(), EGL_SYNC_FENCE_KHR, NULL);
182
183 // NVFence.
184 } else if (m_type == NVFence) {
185 m_fenceNV.setFenceNV(m_fence[0], GL_ALL_COMPLETED_NV);
186 }
187
188 // ARBTimerQuery.
189 else if (m_type == ARBTimerQuery) {
190 m_timerQuery.queryCounter(m_timer[0], GL_TIMESTAMP);
191
192 // EXTTimerQuery.
193 } else if (m_type == EXTTimerQuery) {
194 m_timerQuery.beginQuery(GL_TIME_ELAPSED, m_timer[0]);
195 }
196 }
197
198 quint64 GPUTimer::stop()
199 {
200 DASSERT(m_context == QOpenGLContext::currentContext());
201 DASSERT(m_type != Unset);
202 DASSERT(m_started);
203
204 #if !defined QT_NO_DEBUG
205 m_started = false;
206 #endif
207
208 // KHRFence.
209 if (m_type == KHRFence) {
210 QElapsedTimer timer;
211 EGLDisplay dpy = eglGetCurrentDisplay();
212 EGLSyncKHR afterSync = m_fenceSyncKHR.createSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
213 EGLint beforeSyncValue =
214 m_fenceSyncKHR.clientWaitSyncKHR(dpy, m_beforeSync, 0, EGL_FOREVER_KHR);
215 quint64 beforeTime = timer.nsecsElapsed();
216 EGLint afterSyncValue =
217 m_fenceSyncKHR.clientWaitSyncKHR(
218 dpy, afterSync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
219 quint64 afterTime = timer.nsecsElapsed();
220 m_fenceSyncKHR.destroySyncKHR(dpy, afterSync);
221 m_fenceSyncKHR.destroySyncKHR(dpy, m_beforeSync);
222 m_beforeSync = EGL_NO_SYNC_KHR;
223 if (beforeSyncValue == EGL_CONDITION_SATISFIED_KHR
224 && afterSyncValue == EGL_CONDITION_SATISFIED_KHR) {
225 return afterTime - beforeTime;
226 } else {
227 return 0;
228 }
229
230 // NVFence.
231 } else if (m_type == NVFence) {
232 QElapsedTimer timer;
233 m_fenceNV.setFenceNV(m_fence[1], GL_ALL_COMPLETED_NV);
234 m_fenceNV.finishFenceNV(m_fence[0]);
235 quint64 beforeTime = timer.nsecsElapsed();
236 m_fenceNV.finishFenceNV(m_fence[1]);
237 quint64 afterTime = timer.nsecsElapsed();
238 return afterTime - beforeTime;
239 }
240
241 // ARBTimerQuery.
242 else if (m_type == ARBTimerQuery) {
243 GLuint64 time[2] = { 0, 0 };
244 m_timerQuery.queryCounter(m_timer[1], GL_TIMESTAMP);
245 m_timerQuery.getQueryObjectui64v(m_timer[0], GL_QUERY_RESULT, &time[0]);
246 m_timerQuery.getQueryObjectui64v(m_timer[1], GL_QUERY_RESULT, &time[1]);
247 if (time[0] != 0 && time[1] != 0) {
248 return time[1] - time[0];
249 } else {
250 return -1;
251 }
252
253 // EXTTimerQuery.
254 } else if (m_type == EXTTimerQuery) {
255 GLuint64EXT time;
256 m_timerQuery.endQuery(GL_TIME_ELAPSED);
257 m_timerQuery.getQueryObjectui64vExt(m_timer[0], GL_QUERY_RESULT, &time);
258 return time;
259 }
260
261 // Finish.
262 else {
263 QOpenGLFunctions* functions = QOpenGLContext::currentContext()->functions();
264 QElapsedTimer timer;
265 timer.start();
266 functions->glFinish();
267 return static_cast<quint64>(timer.nsecsElapsed());
268 }
269
270 DNOT_REACHED();
271 return 0;
272 }