1// Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QT3DEXTRAS_QTEXTUREATLAS_P_P_H
5#define QT3DEXTRAS_QTEXTUREATLAS_P_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qscopedpointer.h>
19#include <Qt3DRender/private/qabstracttexture_p.h>
20#include <Qt3DRender/private/qtexturegenerator_p.h>
21#include <Qt3DExtras/private/areaallocator_p.h>
22#include <Qt3DExtras/private/qtextureatlas_p.h>
23
24QT_BEGIN_NAMESPACE
25
26namespace Qt3DExtras {
27
28// Used to store texture info within atlas
29struct AtlasTexture
30{
31 QRect position;
32 int padding = 0;
33};
34
35// data shared between QTextureAtlasPrivate and the QTextureGenerators
36// we use this extra indirection so we can lazily copy the sub-images
37// into the actual texture image in the backend texture loader thread.
38class QTextureAtlasData
39{
40public:
41 QTextureAtlasData(int w, int h, QImage::Format fmt);
42 ~QTextureAtlasData();
43
44 int width() const { return m_image.width(); }
45 int height() const { return m_image.height(); }
46
47 void addImage(const AtlasTexture &texture, const QImage &image);
48 QByteArray createUpdatedImageData();
49
50private:
51 struct Update {
52 AtlasTexture textureInfo;
53 QImage image;
54 };
55
56 QMutex m_mutex;
57 QImage m_image;
58 QList<Update> m_updates;
59};
60
61typedef QSharedPointer<QTextureAtlasData> QTextureAtlasDataPtr;
62
63class QTextureAtlasPrivate : public Qt3DRender::QAbstractTexturePrivate
64{
65public:
66 QTextureAtlasPrivate();
67 ~QTextureAtlasPrivate();
68
69 Q_DECLARE_PUBLIC(QTextureAtlas)
70
71 QTextureAtlas::TextureId m_currId = 1; // IDs for new sub-textures
72 int m_currGen = 0;
73
74 QTextureAtlasDataPtr m_data;
75 QScopedPointer<AreaAllocator> m_allocator;
76 QOpenGLTexture::PixelFormat m_pixelFormat;
77 QHash<QTextureAtlas::TextureId, AtlasTexture> m_textures;
78};
79
80class QTextureAtlasGenerator : public Qt3DRender::QTextureGenerator
81{
82public:
83 QTextureAtlasGenerator(const QTextureAtlasPrivate *texAtlas);
84 ~QTextureAtlasGenerator();
85 Qt3DRender::QTextureDataPtr operator()() override;
86 bool operator==(const QTextureGenerator &other) const override;
87
88 QT3D_FUNCTOR(QTextureAtlasGenerator)
89
90private:
91 QTextureAtlasDataPtr m_data;
92 Qt3DRender::QAbstractTexture::TextureFormat m_format;
93 QOpenGLTexture::PixelFormat m_pixelFormat;
94 int m_generation;
95 Qt3DCore::QNodeId m_atlasId;
96};
97typedef QSharedPointer<QTextureAtlasGenerator> QTextureAtlasGeneratorPtr;
98
99} // namespace Qt3DExtras
100
101QT_END_NAMESPACE
102
103#endif // QT3DEXTRAS_QTEXTUREATLAS_P_P_H
104

source code of qt3d/src/extras/text/qtextureatlas_p_p.h