1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qsgcompressedatlastexture_p.h"
41
42#include <QtCore/QVarLengthArray>
43#include <QtCore/QElapsedTimer>
44#include <QtCore/QtMath>
45
46#include <QtGui/QOpenGLContext>
47#include <QtGui/QGuiApplication>
48#include <QtGui/QScreen>
49#include <QtGui/QSurface>
50#include <QtGui/QWindow>
51#include <QtGui/QOpenGLFunctions>
52#include <QtGui/QOpenGLTexture>
53#include <QDebug>
54
55#include <private/qqmlglobal_p.h>
56#include <private/qquickprofiler_p.h>
57#include <private/qsgtexture_p.h>
58#include <private/qsgcompressedtexture_p.h>
59
60QT_BEGIN_NAMESPACE
61
62static QElapsedTimer qsg_renderer_timer;
63
64namespace QSGCompressedAtlasTexture
65{
66
67Atlas::Atlas(const QSize &size, uint format)
68 : QSGOpenGLAtlasTexture::AtlasBase(size)
69 , m_format(format)
70{
71}
72
73Atlas::~Atlas()
74{
75}
76
77Texture *Atlas::create(const QByteArray &data, int dataLength, int dataOffset, const QSize &size, const QSize &paddedSize)
78{
79 // No need to lock, as manager already locked it.
80 QRect rect = m_allocator.allocate(paddedSize);
81 if (rect.width() > 0 && rect.height() > 0) {
82 Texture *t = new Texture(this, rect, data, dataLength, dataOffset, size);
83 m_pending_uploads << t;
84 return t;
85 }
86 return nullptr;
87}
88
89void Atlas::generateTexture()
90{
91 QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions();
92 funcs->glCompressedTexImage2D(GL_TEXTURE_2D, 0, m_format,
93 m_size.width(), m_size.height(), 0,
94 (m_size.width() * m_size.height()) / 2,
95 nullptr);
96}
97
98void Atlas::uploadPendingTexture(int i)
99{
100 Texture *texture = static_cast<Texture*>(m_pending_uploads.at(i));
101
102 const QRect &r = texture->atlasSubRect();
103
104 QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions();
105 funcs->glCompressedTexSubImage2D(GL_TEXTURE_2D, 0,
106 r.x(), r.y(), r.width(), r.height(), m_format,
107 texture->sizeInBytes(),
108 texture->data().constData() + texture->dataOffset());
109
110 qCDebug(QSG_LOG_TIME_TEXTURE).nospace() << "compressed atlastexture uploaded in: " << qsg_renderer_timer.elapsed()
111 << "ms (" << texture->textureSize().width() << "x"
112 << texture->textureSize().height() << ")";
113
114 // TODO: consider releasing the data (as is done in the regular atlas)?
115 // The advantage of keeping this data around is that it makes it much easier
116 // to remove the texture from the atlas
117}
118
119Texture::Texture(Atlas *atlas, const QRect &textureRect, const QByteArray &data, int dataLength, int dataOffset, const QSize &size)
120 : QSGOpenGLAtlasTexture::TextureBase(atlas, textureRect)
121 , m_nonatlas_texture(nullptr)
122 , m_data(data)
123 , m_size(size)
124 , m_dataLength(dataLength)
125 , m_dataOffset(dataOffset)
126{
127 float w = atlas->size().width();
128 float h = atlas->size().height();
129 QRect nopad = atlasSubRect();
130 // offset by half-pixel to prevent bleeding when scaling
131 m_texture_coords_rect = QRectF((nopad.x() + .5) / w,
132 (nopad.y() + .5) / h,
133 (nopad.width() - 1.) / w,
134 (nopad.height() - 1.) / h);
135}
136
137Texture::~Texture()
138{
139 delete m_nonatlas_texture;
140}
141
142bool Texture::hasAlphaChannel() const
143{
144 return QSGCompressedTexture::formatIsOpaque(static_cast<Atlas*>(m_atlas)->format());
145}
146
147QSGTexture *Texture::removedFromAtlas() const
148{
149 if (m_nonatlas_texture) {
150 m_nonatlas_texture->setMipmapFiltering(mipmapFiltering());
151 m_nonatlas_texture->setFiltering(filtering());
152 return m_nonatlas_texture;
153 }
154
155 if (!m_data.isEmpty()) {
156 QTextureFileData texData;
157 texData.setData(m_data);
158 texData.setSize(m_size);
159 texData.setGLInternalFormat(static_cast<Atlas*>(m_atlas)->format());
160 texData.setDataLength(m_dataLength);
161 texData.setDataOffset(m_dataOffset);
162 m_nonatlas_texture = new QSGCompressedTexture(texData);
163 m_nonatlas_texture->setMipmapFiltering(mipmapFiltering());
164 m_nonatlas_texture->setFiltering(filtering());
165 }
166
167 return m_nonatlas_texture;
168}
169
170}
171
172QT_END_NAMESPACE
173