KWin
Loading...
Searching...
No Matches
glrendertimequery.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@gmail.com>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#include "glrendertimequery.h"
10#include "opengl/glplatform.h"
11
12namespace KWin
13{
14
16{
18 glGenQueries(1, &m_query);
19 }
20}
21
23{
24 if (m_query) {
25 glDeleteQueries(1, &m_query);
26 }
27}
28
30{
31 if (m_query) {
32 GLint64 nanos = 0;
33 glGetInteger64v(GL_TIMESTAMP, &nanos);
34 m_cpuStart = std::chrono::nanoseconds(nanos);
35 } else {
36 m_cpuStart = std::chrono::steady_clock::now().time_since_epoch();
37 }
38}
39
41{
42 if (m_query) {
43 glQueryCounter(m_query, GL_TIMESTAMP);
44 } else {
45 m_cpuEnd = std::chrono::steady_clock::now().time_since_epoch();
46 }
47 m_hasResult = true;
48}
49
50std::chrono::nanoseconds GLRenderTimeQuery::result()
51{
52 if (!m_hasResult) {
53 return std::chrono::nanoseconds::zero();
54 }
55 m_hasResult = false;
56 if (m_query) {
57 uint64_t nanos = 0;
58 glGetQueryObjectui64v(m_query, GL_QUERY_RESULT, &nanos);
59 if (nanos == 0) {
60 return std::chrono::nanoseconds::zero();
61 }
62 return std::chrono::nanoseconds(nanos) - m_cpuStart;
63 } else {
64 return m_cpuEnd - m_cpuStart;
65 }
66}
67
68}
static GLPlatform * instance()
Definition glplatform.h:394
std::chrono::nanoseconds result()