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
qsgcompressedatlastexture.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
6#include <QtCore/QVarLengthArray>
7#include <QtCore/QElapsedTimer>
8#include <QtCore/QtMath>
9
10#include <QtGui/QGuiApplication>
11#include <QtGui/QScreen>
12#include <QtGui/QSurface>
13#include <QtGui/QWindow>
14#include <QDebug>
15
16#include <private/qqmlglobal_p.h>
17#include <private/qquickprofiler_p.h>
18#include <private/qsgtexture_p.h>
19#include <private/qsgcompressedtexture_p.h>
20
22
24{
25
27 : QSGRhiAtlasTexture::AtlasBase(rc, size), m_format(format)
28{
29}
30
32{
33}
34
36{
37 // Align reservation to 16x16, >= any compressed block size
38 QSize paddedSize(((size.width() + 15) / 16) * 16, ((size.height() + 15) / 16) * 16);
39 // No need to lock, as manager already locked it.
40 QRect rect = m_allocator.allocate(paddedSize);
41 if (rect.width() > 0 && rect.height() > 0) {
42 Texture *t = new Texture(this, rect, data, size);
44 return t;
45 }
46 return nullptr;
47}
48
50{
53 flags.setFlag(QRhiTexture::sRGB, fmt.isSRGB);
54 m_texture = m_rhi->newTexture(fmt.rhiFormat, m_size, 1, flags);
55 if (!m_texture)
56 return false;
57
58 if (!m_texture->create()) {
59 delete m_texture;
60 m_texture = nullptr;
61 return false;
62 }
63
64 qCDebug(QSG_LOG_TEXTUREIO, "Created compressed atlas of size %dx%d for format 0x%x (rhi: %d)",
65 m_size.width(), m_size.height(), m_format, fmt.rhiFormat);
66
67 return true;
68}
69
71{
72 Texture *texture = static_cast<Texture *>(t);
73
74 const QRect &r = texture->atlasSubRect();
75
76 QRhiTextureSubresourceUploadDescription subresDesc(texture->data().constData(),
77 texture->sizeInBytes());
78 subresDesc.setSourceSize(texture->textureSize());
79 subresDesc.setDestinationTopLeft(r.topLeft());
80
82 rcub->uploadTexture(m_texture, desc);
83
84 qCDebug(QSG_LOG_TEXTUREIO, "compressed atlastexture upload, size %dx%d format 0x%x",
85 t->textureSize().width(), t->textureSize().height(), m_format);
86}
87
88Texture::Texture(Atlas *atlas, const QRect &textureRect, QByteArrayView data, const QSize &size)
89 : QSGRhiAtlasTexture::TextureBase(atlas, textureRect),
90 m_nonatlas_texture(nullptr),
91 m_data(data.toByteArray()),
92 m_size(size)
93{
94 float w = atlas->size().width();
95 float h = atlas->size().height();
96 const QRect &r = atlasSubRect();
97 // offset by half-pixel to prevent bleeding when scaling
98 m_texture_coords_rect = QRectF((r.x() + .5) / w,
99 (r.y() + .5) / h,
100 (size.width() - 1.) / w,
101 (size.height() - 1.) / h);
102}
103
105{
106 delete m_nonatlas_texture;
107}
108
110{
111 return !QSGCompressedTexture::formatIsOpaque(static_cast<Atlas*>(m_atlas)->format());
112}
113
115{
116 if (m_nonatlas_texture) {
117 m_nonatlas_texture->setMipmapFiltering(mipmapFiltering());
118 m_nonatlas_texture->setFiltering(filtering());
119 return m_nonatlas_texture;
120 }
121
122 if (!m_data.isEmpty()) {
123 QTextureFileData texData;
124 texData.setData(m_data);
125 texData.setSize(m_size);
126 texData.setGLInternalFormat(static_cast<Atlas*>(m_atlas)->format());
127 texData.setDataLength(m_data.size());
128 texData.setDataOffset(0);
129 m_nonatlas_texture = new QSGCompressedTexture(texData);
130 m_nonatlas_texture->setMipmapFiltering(mipmapFiltering());
131 m_nonatlas_texture->setFiltering(filtering());
132 }
133
134 return m_nonatlas_texture;
135}
136
137}
138
140
141#include "moc_qsgcompressedatlastexture_p.cpp"
NSData * m_data
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtGui
Definition qrhi.h:1731
void setDestinationTopLeft(const QPoint &p)
Sets the destination top-left position p.
Definition qrhi.h:673
void setSourceSize(const QSize &size)
Sets the source size in pixels.
Definition qrhi.h:676
\inmodule QtGui
Definition qrhi.h:716
\inmodule QtGui
Definition qrhi.h:693
@ UsedAsCompressedAtlas
Definition qrhi.h:905
@ UsedAsTransferSource
Definition qrhi.h:902
virtual bool create()=0
Creates the corresponding native graphics resources.
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition qrhi.cpp:10562
QRect allocate(const QSize &size)
Texture * create(QByteArrayView data, const QSize &size)
void enqueueTextureUpload(QSGRhiAtlasTexture::TextureBase *t, QRhiResourceUpdateBatch *rcub) override
Atlas(QSGDefaultRenderContext *rc, const QSize &size, uint format)
bool hasAlphaChannel() const override
Returns true if the texture data contains an alpha channel.
QSGTexture * removedFromAtlas(QRhiResourceUpdateBatch *) const override
This function returns a copy of the current texture which is removed from its atlas.
Texture(Atlas *atlas, const QRect &textureRect, QByteArrayView data, const QSize &size)
static FormatInfo formatInfo(quint32 glTextureFormat)
static bool formatIsOpaque(quint32 glTextureFormat)
QVector< TextureBase * > m_pending_uploads
\inmodule QtQuick
Definition qsgtexture.h:20
QSGTexture::Filtering mipmapFiltering() const
Returns whether mipmapping should be used when sampling from this texture.
void setFiltering(Filtering filter)
Sets the sampling mode to filter.
QSGTexture::Filtering filtering() const
Returns the sampling mode to be used for this texture.
void setMipmapFiltering(Filtering filter)
Sets the mipmap sampling mode to filter.
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
void setData(const QByteArray &data)
rect
[4]
Combined button and popup list for selecting options.
#define qCDebug(category,...)
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLbitfield flags
GLenum GLuint texture
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
GLdouble GLdouble t
Definition qopenglext.h:243
unsigned int uint
Definition qtypes.h:34
QVideoFrameFormat::PixelFormat fmt
QObject::connect nullptr