Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qopenglcontext_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QOPENGLCONTEXT_P_H
6#define QOPENGLCONTEXT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtGui/private/qtguiglobal_p.h>
20
21#ifndef QT_NO_OPENGL
22
23#include <qopengl.h>
24#include "qopenglcontext.h"
25#include <private/qobject_p.h>
26#include <qmutex.h>
27
28#include <QtCore/QByteArray>
29#include <QtCore/QHash>
30#include <QtCore/QSet>
31
33
34
35class QOpenGLFunctions;
36class QOpenGLContext;
37class QOpenGLFramebufferObject;
38class QOpenGLMultiGroupSharedResource;
39
40class Q_GUI_EXPORT QOpenGLSharedResource
41{
42public:
43 QOpenGLSharedResource(QOpenGLContextGroup *group);
44 virtual ~QOpenGLSharedResource() = 0;
45
46 QOpenGLContextGroup *group() const { return m_group; }
47
48 // schedule the resource for deletion at an appropriate time
49 void free();
50
51protected:
52 // the resource's share group no longer exists, invalidate the resource
53 virtual void invalidateResource() = 0;
54
55 // a valid context in the group is current, free the resource
56 virtual void freeResource(QOpenGLContext *context) = 0;
57
58private:
59 QOpenGLContextGroup *m_group;
60
61 friend class QOpenGLContextGroup;
62 friend class QOpenGLContextGroupPrivate;
63 friend class QOpenGLMultiGroupSharedResource;
64
65 Q_DISABLE_COPY_MOVE(QOpenGLSharedResource)
66};
67
68class Q_GUI_EXPORT QOpenGLSharedResourceGuard : public QOpenGLSharedResource
69{
70public:
71 typedef void (*FreeResourceFunc)(QOpenGLFunctions *functions, GLuint id);
72 QOpenGLSharedResourceGuard(QOpenGLContext *context, GLuint id, FreeResourceFunc func)
73 : QOpenGLSharedResource(context->shareGroup())
74 , m_id(id)
75 , m_func(func)
76 {
77 }
78 ~QOpenGLSharedResourceGuard() override;
79
80 GLuint id() const { return m_id; }
81
82protected:
83 void invalidateResource() override
84 {
85 m_id = 0;
86 }
87
88 void freeResource(QOpenGLContext *context) override;
89
90private:
91 GLuint m_id;
92 FreeResourceFunc m_func;
93};
94
95class Q_GUI_EXPORT QOpenGLContextGroupPrivate : public QObjectPrivate
96{
97 Q_DECLARE_PUBLIC(QOpenGLContextGroup)
98public:
99 QOpenGLContextGroupPrivate()
100 : m_context(nullptr)
101 , m_refs(0)
102 {
103 }
104 ~QOpenGLContextGroupPrivate() override;
105
106 void addContext(QOpenGLContext *ctx);
107 void removeContext(QOpenGLContext *ctx);
108
109 void cleanup();
110
111 void deletePendingResources(QOpenGLContext *ctx);
112
113 QOpenGLContext *m_context;
114
115 QList<QOpenGLContext *> m_shares;
116 QRecursiveMutex m_mutex;
117
118 QHash<QOpenGLMultiGroupSharedResource *, QOpenGLSharedResource *> m_resources;
119 QAtomicInt m_refs;
120
121 QList<QOpenGLSharedResource *> m_sharedResources;
122 QList<QOpenGLSharedResource *> m_pendingDeletion;
123};
124
126{
127public:
128 QOpenGLMultiGroupSharedResource();
129 ~QOpenGLMultiGroupSharedResource();
130
131 void insert(QOpenGLContext *context, QOpenGLSharedResource *value);
132 void cleanup(QOpenGLContextGroup *group, QOpenGLSharedResource *value);
133
134 QOpenGLSharedResource *value(QOpenGLContext *context);
135
136 QList<QOpenGLSharedResource *> resources() const;
137
138 template <typename T>
139 T *value(QOpenGLContext *context) {
140 QOpenGLContextGroup *group = context->shareGroup();
141 // Have to use our own mutex here, not the group's, since
142 // m_groups has to be protected too against any concurrent access.
143 QMutexLocker locker(&m_mutex);
144 T *resource = static_cast<T *>(group->d_func()->m_resources.value(this, nullptr));
145 if (!resource) {
146 resource = new T(context);
147 insert(context, resource);
148 }
149 return resource;
150 }
151
152private:
153 QAtomicInt active;
154 QList<QOpenGLContextGroup *> m_groups;
155 QRecursiveMutex m_mutex;
156};
157
158class QPaintEngineEx;
159class QOpenGLFunctions;
162
164{
165public:
166 virtual ~QOpenGLContextVersionFunctionHelper();
167};
168
169class Q_GUI_EXPORT QOpenGLContextPrivate : public QObjectPrivate
170{
171 Q_DECLARE_PUBLIC(QOpenGLContext)
172public:
173 QOpenGLContextPrivate()
174 : platformGLContext(nullptr)
175 , shareContext(nullptr)
176 , shareGroup(nullptr)
177 , screen(nullptr)
178 , surface(nullptr)
179 , functions(nullptr)
180 , textureFunctions(nullptr)
181 , versionFunctions(nullptr)
182 , vaoHelper(nullptr)
183 , vaoHelperDestroyCallback(nullptr)
184 , max_texture_size(-1)
185 , workaround_brokenFBOReadBack(false)
186 , workaround_brokenTexSubImage(false)
187 , workaround_missingPrecisionQualifiers(false)
188 , active_engine(nullptr)
189 , qgl_current_fbo_invalid(false)
190 , qgl_current_fbo(nullptr)
191 , defaultFboRedirect(0)
192 {
193 requestedFormat = QSurfaceFormat::defaultFormat();
194 }
195
196 ~QOpenGLContextPrivate() override;
197
198 void adopt(QPlatformOpenGLContext *);
199
200 QSurfaceFormat requestedFormat;
201 QPlatformOpenGLContext *platformGLContext;
202 QOpenGLContext *shareContext;
203 QOpenGLContextGroup *shareGroup;
204 QScreen *screen;
205 QSurface *surface;
206 QOpenGLFunctions *functions;
207 mutable QSet<QByteArray> extensionNames;
208 QOpenGLTextureHelper* textureFunctions;
209 std::function<void()> textureFunctionsDestroyCallback;
210 QOpenGLContextVersionFunctionHelper *versionFunctions;
211 QOpenGLVertexArrayObjectHelper *vaoHelper;
212 using QOpenGLVertexArrayObjectHelperDestroyCallback_t = void (*)(QOpenGLVertexArrayObjectHelper *);
213 QOpenGLVertexArrayObjectHelperDestroyCallback_t vaoHelperDestroyCallback;
214
215 GLint max_texture_size;
216
217 bool workaround_brokenFBOReadBack;
218 bool workaround_brokenTexSubImage;
219 bool workaround_missingPrecisionQualifiers;
220
221 QPaintEngineEx *active_engine;
222
223 bool qgl_current_fbo_invalid;
224
225 // Set and unset in QOpenGLFramebufferObject::bind()/unbind().
226 // (Only meaningful for QOGLFBO since an FBO might be bound by other means)
227 // Saves us from querying the driver for the current FBO in most paths.
228 QOpenGLFramebufferObject *qgl_current_fbo;
229
230 GLuint defaultFboRedirect;
231
232 static QOpenGLContext *setCurrentContext(QOpenGLContext *context);
233
234 int maxTextureSize();
235
236 static QOpenGLContextPrivate *get(QOpenGLContext *context)
237 {
238 return context ? context->d_func() : nullptr;
239 }
240
241#if !defined(QT_NO_DEBUG)
242 static bool toggleMakeCurrentTracker(QOpenGLContext *context, bool value)
243 {
244 QMutexLocker locker(&makeCurrentTrackerMutex);
245 bool old = makeCurrentTracker.value(context, false);
246 makeCurrentTracker.insert(context, value);
247 return old;
248 }
249 static void cleanMakeCurrentTracker(QOpenGLContext *context)
250 {
251 QMutexLocker locker(&makeCurrentTrackerMutex);
252 makeCurrentTracker.remove(context);
253 }
254 static QHash<QOpenGLContext *, bool> makeCurrentTracker;
255 static QMutex makeCurrentTrackerMutex;
256#endif
257
258 void _q_screenDestroyed(QObject *object);
259};
260
261Q_GUI_EXPORT void qt_gl_set_global_share_context(QOpenGLContext *context);
262Q_GUI_EXPORT QOpenGLContext *qt_gl_global_share_context();
263
264QT_END_NAMESPACE
265
266#endif // QT_NO_OPENGL
267#endif // QOPENGLCONTEXT_P_H
QOpenGLContext * context
The QOpenGLContextGroup class represents a group of contexts sharing OpenGL resources....
\inmodule QtGui
The QOpenGLMultiGroupSharedResource keeps track of a shared resource that might be needed from multip...
The QOpenGLSharedResourceGuard class is a convenience sub-class of QOpenGLSharedResource to be used t...
The QOpenGLSharedResource class is used to keep track of resources that are shared between OpenGL con...
Combined button and popup list for selecting options.
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QOpenGLContext * qt_gl_global_share_context()
void qt_gl_set_global_share_context(QOpenGLContext *context)
static QOpenGLContext * global_share_context