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
82 // gives up ownership of the GL resource, so that free() won't delete it
83 GLuint take() { return std::exchange(m_id, 0); }
84
85protected:
86 void invalidateResource() override
87 {
88 m_id = 0;
89 }
90
91 void freeResource(QOpenGLContext *context) override;
92
93private:
94 GLuint m_id;
95 FreeResourceFunc m_func;
96};
97
98class Q_GUI_EXPORT QOpenGLContextGroupPrivate : public QObjectPrivate
99{
100 Q_DECLARE_PUBLIC(QOpenGLContextGroup)
101public:
102 QOpenGLContextGroupPrivate()
103 : m_context(nullptr)
104 , m_refs(0)
105 {
106 }
107 ~QOpenGLContextGroupPrivate() override;
108
109 void addContext(QOpenGLContext *ctx);
110 void removeContext(QOpenGLContext *ctx);
111
112 void cleanup();
113
114 void deletePendingResources(QOpenGLContext *ctx);
115
116 QOpenGLContext *m_context;
117
118 QList<QOpenGLContext *> m_shares;
119 QRecursiveMutex m_mutex;
120
121 QHash<QOpenGLMultiGroupSharedResource *, QOpenGLSharedResource *> m_resources;
122 QAtomicInt m_refs;
123
124 QList<QOpenGLSharedResource *> m_sharedResources;
125 QList<QOpenGLSharedResource *> m_pendingDeletion;
126};
127
129{
130public:
131 QOpenGLMultiGroupSharedResource();
132 ~QOpenGLMultiGroupSharedResource();
133
134 void insert(QOpenGLContext *context, QOpenGLSharedResource *value);
135 void cleanup(QOpenGLContextGroup *group, QOpenGLSharedResource *value);
136
137 QOpenGLSharedResource *value(QOpenGLContext *context);
138
139 QList<QOpenGLSharedResource *> resources() const;
140
141 template <typename T>
142 T *value(QOpenGLContext *context) {
143 QOpenGLContextGroup *group = context->shareGroup();
144 // Have to use our own mutex here, not the group's, since
145 // m_groups has to be protected too against any concurrent access.
146 QMutexLocker locker(&m_mutex);
147 T *resource = static_cast<T *>(group->d_func()->m_resources.value(this, nullptr));
148 if (!resource) {
149 resource = new T(context);
150 insert(context, resource);
151 }
152 return resource;
153 }
154
155private:
156 QAtomicInt active;
157 QList<QOpenGLContextGroup *> m_groups;
158 QRecursiveMutex m_mutex;
159};
160
161class QPaintEngineEx;
162class QOpenGLFunctions;
165
167{
168public:
169 virtual ~QOpenGLContextVersionFunctionHelper();
170};
171
172class Q_GUI_EXPORT QOpenGLContextPrivate : public QObjectPrivate
173{
174 Q_DECLARE_PUBLIC(QOpenGLContext)
175public:
176 QOpenGLContextPrivate()
177 : platformGLContext(nullptr)
178 , shareContext(nullptr)
179 , shareGroup(nullptr)
180 , screen(nullptr)
181 , surface(nullptr)
182 , functions(nullptr)
183 , textureFunctions(nullptr)
184 , versionFunctions(nullptr)
185 , vaoHelper(nullptr)
186 , vaoHelperDestroyCallback(nullptr)
187 , max_texture_size(-1)
188 , workaround_brokenFBOReadBack(false)
189 , workaround_brokenTexSubImage(false)
190 , workaround_missingPrecisionQualifiers(false)
191 , active_engine(nullptr)
192 , qgl_current_fbo_invalid(false)
193 , qgl_current_fbo(nullptr)
194 , defaultFboRedirect(0)
195 {
196 requestedFormat = QSurfaceFormat::defaultFormat();
197 }
198
199 ~QOpenGLContextPrivate() override;
200
201 void adopt(QPlatformOpenGLContext *);
202
203 QSurfaceFormat requestedFormat;
204 QPlatformOpenGLContext *platformGLContext;
205 QOpenGLContext *shareContext;
206 QOpenGLContextGroup *shareGroup;
207 QScreen *screen;
208 QSurface *surface;
209 QOpenGLFunctions *functions;
210 mutable QSet<QByteArray> extensionNames;
211 QOpenGLTextureHelper* textureFunctions;
212 std::function<void()> textureFunctionsDestroyCallback;
213 QOpenGLContextVersionFunctionHelper *versionFunctions;
214 QOpenGLVertexArrayObjectHelper *vaoHelper;
215 using QOpenGLVertexArrayObjectHelperDestroyCallback_t = void (*)(QOpenGLVertexArrayObjectHelper *);
216 QOpenGLVertexArrayObjectHelperDestroyCallback_t vaoHelperDestroyCallback;
217
218 GLint max_texture_size;
219
220 bool workaround_brokenFBOReadBack;
221 bool workaround_brokenTexSubImage;
222 bool workaround_missingPrecisionQualifiers;
223
224 QPaintEngineEx *active_engine;
225
226 bool qgl_current_fbo_invalid;
227
228 // Set and unset in QOpenGLFramebufferObject::bind()/unbind().
229 // (Only meaningful for QOGLFBO since an FBO might be bound by other means)
230 // Saves us from querying the driver for the current FBO in most paths.
231 QOpenGLFramebufferObject *qgl_current_fbo;
232
233 GLuint defaultFboRedirect;
234
235 static QOpenGLContext *setCurrentContext(QOpenGLContext *context);
236
237 int maxTextureSize();
238
239 static QOpenGLContextPrivate *get(QOpenGLContext *context)
240 {
241 return context ? context->d_func() : nullptr;
242 }
243
244#if !defined(QT_NO_DEBUG)
245 static bool toggleMakeCurrentTracker(QOpenGLContext *context, bool value)
246 {
247 QMutexLocker locker(&makeCurrentTrackerMutex);
248 bool old = makeCurrentTracker.value(context, false);
249 makeCurrentTracker.insert(context, value);
250 return old;
251 }
252 static void cleanMakeCurrentTracker(QOpenGLContext *context)
253 {
254 QMutexLocker locker(&makeCurrentTrackerMutex);
255 makeCurrentTracker.remove(context);
256 }
257 static QHash<QOpenGLContext *, bool> makeCurrentTracker;
258 static QMutex makeCurrentTrackerMutex;
259#endif
260
261 void _q_screenDestroyed(QObject *object);
262};
263
264Q_GUI_EXPORT void qt_gl_set_global_share_context(QOpenGLContext *context);
265Q_GUI_EXPORT QOpenGLContext *qt_gl_global_share_context();
266
267QT_END_NAMESPACE
268
269#endif // QT_NO_OPENGL
270#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:2620
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