Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qopengltexturecache.cpp
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
5#include <private/qopengltextureuploader_p.h>
6#include <qmath.h>
7#include <qopenglfunctions.h>
8#include <private/qimagepixmapcleanuphooks_p.h>
9#include <qpa/qplatformpixmap.h>
10
11#include <qtopengl_tracepoints_p.h>
12
14
44
46
48{
49 return qt_texture_caches()->cacheForContext(context);
50}
51
53{
54 QList<QOpenGLSharedResource *> resources = qt_texture_caches()->m_resource.resources();
55 for (QList<QOpenGLSharedResource *>::iterator it = resources.begin(); it != resources.end(); ++it)
56 static_cast<QOpenGLTextureCache *>(*it)->invalidate(key);
57}
58
63
65{
66 bool ok = false;
67 const int envCacheSize = qEnvironmentVariableIntValue("QT_OPENGL_TEXTURE_CACHE_SIZE", &ok);
68 if (ok)
69 return envCacheSize;
70
71 return 1024 * 1024; // 1024 MB cache default
72}
73
79
83
85 const QPixmap &pixmap,
86 QOpenGLTextureUploader::BindOptions options)
87{
88 if (pixmap.isNull())
89 return { 0, {} };
90 QMutexLocker locker(&m_mutex);
91 qint64 key = pixmap.cacheKey();
92
93 // A QPainter is active on the image - take the safe route and replace the texture.
94 if (!pixmap.paintingActive()) {
96 if (entry && entry->options() == options) {
97 context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id());
98 return { entry->id(), {} };
99 }
100 }
101
102 BindResult result = bindTexture(context, key, pixmap.toImage(), options);
103 if (result.id > 0)
105
106 return result;
107}
108
110 const QImage &image,
111 QOpenGLTextureUploader::BindOptions options)
112{
113 if (image.isNull())
114 return { 0, {} };
115 QMutexLocker locker(&m_mutex);
116 qint64 key = image.cacheKey();
117
118 // A QPainter is active on the image - take the safe route and replace the texture.
119 if (!image.paintingActive()) {
121 if (entry && entry->options() == options) {
122 context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id());
123 return { entry->id(), {} };
124 }
125 }
126
127 QImage img = image;
128 if (!context->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextures))
130
132 if (result.id > 0)
134
135 return result;
136}
137
138Q_TRACE_POINT(qtopengl, QOpenGLTextureCache_bindTexture_entry, QOpenGLContext *context, qint64 key, const unsigned char *image, int options);
139Q_TRACE_POINT(qtopengl, QOpenGLTextureCache_bindTexture_exit);
140
142 qint64 key,
143 const QImage &image,
144 QOpenGLTextureUploader::BindOptions options)
145{
146 Q_TRACE_SCOPE(QOpenGLTextureCache_bindTexture, context, key, image.bits(), options);
147
148 GLuint id;
149 QOpenGLFunctions *funcs = context->functions();
150 funcs->glGenTextures(1, &id);
151 funcs->glBindTexture(GL_TEXTURE_2D, id);
152
153 int cost = QOpenGLTextureUploader::textureImage(GL_TEXTURE_2D, image, options);
154
155 m_cache.insert(key, new QOpenGLCachedTexture(id, options, context), cost / 1024);
156
157 return { id, BindResultFlag::NewTexture };
158}
159
161{
162 QMutexLocker locker(&m_mutex);
163 m_cache.remove(key);
164}
165
167{
168 m_cache.clear();
169}
170
172{
173 Q_ASSERT(false); // the texture cache lives until the context group disappears
174}
175
177{
178 funcs->glDeleteTextures(1, &id);
179}
180
181QOpenGLCachedTexture::QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context) : m_options(options)
182{
183 m_resource = new QOpenGLSharedResourceGuard(context, id, freeTexture);
184}
185
bool remove(const Key &key) noexcept(std::is_nothrow_destructible_v< Node >)
Definition qcache.h:222
T * object(const Key &key) const noexcept
Definition qcache.h:209
void clear() noexcept(std::is_nothrow_destructible_v< Node >)
Definition qcache.h:176
bool insert(const Key &key, T *object, qsizetype cost=1)
Definition qcache.h:184
static QImagePixmapCleanupHooks * instance()
static void enableCleanupHooks(const QImage &image)
\inmodule QtGui
Definition qimage.h:37
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
Definition qmutex.h:281
QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context)
\inmodule QtGui
The QOpenGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API.
The QOpenGLMultiGroupSharedResource keeps track of a shared resource that might be needed from multip...
QOpenGLSharedResource * value(QOpenGLContext *context)
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...
static void cleanupTexturesForPixmapData(QPlatformPixmap *pmd)
QOpenGLTextureCache * cacheForContext(QOpenGLContext *context)
static void cleanupTexturesForCacheKey(qint64 key)
void freeResource(QOpenGLContext *ctx) override
QOpenGLTextureCache(QOpenGLContext *)
void invalidateResource() override
BindResult bindTexture(QOpenGLContext *context, const QPixmap &pixmap, QOpenGLTextureUploader::BindOptions options=QOpenGLTextureUploader::PremultipliedAlphaBindOption)
static QOpenGLTextureCache * cacheForContext(QOpenGLContext *context)
static qsizetype textureImage(GLenum target, const QImage &image, BindOptions options, QSize maxSize=QSize())
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
The QPlatformPixmap class provides an abstraction for native pixmaps.
EGLContext ctx
static VulkanServerBufferGlFunctions * funcs
QSet< QString >::iterator it
Combined button and popup list for selecting options.
Definition image.cpp:4
static void * context
SharedPointerFileDialogOptions m_options
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
GLuint64 key
GLenum GLuint id
[7]
GLint void * img
Definition qopenglext.h:233
GLuint entry
GLuint64EXT * result
[6]
static void freeTexture(QOpenGLFunctions *funcs, GLuint id)
static quint64 cacheSize()
static qsizetype cost(const QPixmap &pixmap)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define GLuint
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
#define Q_TRACE_SCOPE(x,...)
Definition qtrace_p.h:146
#define Q_TRACE_POINT(provider, tracepoint,...)
Definition qtrace_p.h:232
unsigned long long quint64
Definition qtypes.h:61
long long qint64
Definition qtypes.h:60
QReadWriteLock lock
[0]
widget render & pixmap