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
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15#ifndef QOPENGLTEXTURECACHE_P_H
16#define QOPENGLTEXTURECACHE_P_H
17
18#include <QtOpenGL/qtopenglglobal.h>
19#include <QHash>
20#include <QObject>
21#include <QCache>
22#include <private/qopenglcontext_p.h>
23#include <private/qopengltextureuploader_p.h>
24#include <QtCore/qmutex.h>
25
26QT_BEGIN_NAMESPACE
27
28class QOpenGLCachedTexture;
29
30class Q_OPENGL_EXPORT QOpenGLTextureCache : public QOpenGLSharedResource
31{
32public:
33 static QOpenGLTextureCache *cacheForContext(QOpenGLContext *context);
34
35 QOpenGLTextureCache(QOpenGLContext *);
36 ~QOpenGLTextureCache();
37
38 enum class BindResultFlag : quint8 {
39 NewTexture = 0x01
40 };
41 Q_DECLARE_FLAGS(BindResultFlags, BindResultFlag)
42
43 struct BindResult {
44 GLuint id;
45 BindResultFlags flags;
46 };
47
48 BindResult bindTexture(QOpenGLContext *context, const QPixmap &pixmap,
49 QOpenGLTextureUploader::BindOptions options = QOpenGLTextureUploader::PremultipliedAlphaBindOption);
50 BindResult bindTexture(QOpenGLContext *context, const QImage &image,
51 QOpenGLTextureUploader::BindOptions options = QOpenGLTextureUploader::PremultipliedAlphaBindOption);
52
53 void invalidate(qint64 key);
54
55 void invalidateResource() override;
56 void freeResource(QOpenGLContext *ctx) override;
57
58private:
59 BindResult bindTexture(QOpenGLContext *context, qint64 key, const QImage &image, QOpenGLTextureUploader::BindOptions options);
60
61 QMutex m_mutex;
62 QCache<quint64, QOpenGLCachedTexture> m_cache;
63};
64
65Q_DECLARE_OPERATORS_FOR_FLAGS(QOpenGLTextureCache::BindResultFlags)
66
67class QOpenGLCachedTexture
68{
69public:
70 QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context);
71 ~QOpenGLCachedTexture() { m_resource->free(); }
72
73 GLuint id() const { return m_resource->id(); }
74 QOpenGLTextureUploader::BindOptions options() const { return m_options; }
75
76private:
77 QOpenGLSharedResourceGuard *m_resource;
78 QOpenGLTextureUploader::BindOptions m_options;
79};
80
81QT_END_NAMESPACE
82
83#endif
84
85

source code of qtbase/src/opengl/qopengltexturecache_p.h