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
qsgplaintexture.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 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#include "qsgplaintexture_p.h"
5#include <QtQuick/private/qsgcontext_p.h>
6#include <qmath.h>
7#include <private/qquickprofiler_p.h>
8#include <private/qqmlglobal_p.h>
9#include <QtGui/qguiapplication.h>
10#include <QtGui/qpa/qplatformnativeinterface.h>
11#include <rhi/qrhi.h>
12#include <QtQuick/private/qsgrhisupport_p.h>
13
14#include <qtquick_tracepoints_p.h>
15
17
20 , m_texture(nullptr)
21 , m_has_alpha(false)
22 , m_dirty_texture(false)
23 , m_owns_texture(true)
24 , m_mipmaps_generated(false)
25 , m_retain_image(false)
26 , m_mipmap_warned(false)
27{
28}
29
31 : QSGTexture(dd)
32 , m_texture(nullptr)
33 , m_has_alpha(false)
34 , m_dirty_texture(false)
35 , m_owns_texture(true)
36 , m_mipmaps_generated(false)
37 , m_retain_image(false)
38 , m_mipmap_warned(false)
39{
40}
41
47
49{
50 m_image = image;
51 m_texture_size = image.size();
52 m_has_alpha = image.hasAlphaChannel();
53 m_dirty_texture = true;
54 m_mipmaps_generated = false;
55}
56
58{
60 delete m_texture;
61
63 m_dirty_texture = false;
64 m_image = QImage();
65 m_mipmaps_generated = false;
66}
67
69 quint64 nativeObjectHandle,
70 int nativeLayoutOrState,
71 uint nativeFormat,
72 const QSize &size,
73 QQuickWindow::CreateTextureOptions options,
74 QQuickWindowPrivate::TextureFromNativeTextureFlags flags)
75{
76 QRhiTexture::Flags texFlags;
77 if (options.testFlag(QQuickWindow::TextureHasMipmaps))
80 texFlags |= QRhiTexture::ExternalOES;
81
83
84 QRhiTexture::Flags formatFlags;
85 auto rhiFormat = QSGRhiSupport::instance()->toRhiTextureFormat(nativeFormat, &formatFlags);
86 if (rhiFormat != QRhiTexture::UnknownFormat) {
87 format = rhiFormat;
88 texFlags |= formatFlags;
89 }
90
91 QRhiTexture *t = rhi->newTexture(format, size, 1, texFlags);
92
93 // ownership of the native object is never taken
94 t->createFrom({nativeObjectHandle, nativeLayoutOrState});
95
97}
98
100{
101 if (m_texture)
102 return qint64(m_texture);
103
104 // two textures (and so materials) with not-yet-created texture underneath are never equal
105 return qint64(this);
106}
107
109{
110 return m_texture;
111}
112
114{
115 Q_D(QSGPlainTexture);
116
117 const bool hasMipMaps = mipmapFiltering() != QSGTexture::None;
118 const bool mipmappingChanged = m_texture && ((hasMipMaps && !m_texture->flags().testFlag(QRhiTexture::MipMapped)) // did not have it before
119 || (!hasMipMaps && m_texture->flags().testFlag(QRhiTexture::MipMapped))); // does not have it anymore
120
121 if (!m_dirty_texture) {
122 if (!m_texture)
123 return;
124 if (m_texture && !mipmappingChanged) {
125 if (hasMipMaps && !m_mipmaps_generated) {
126 resourceUpdates->generateMips(m_texture);
127 m_mipmaps_generated = true;
128 }
129 return;
130 }
131 }
132
133 if (m_image.isNull()) {
134 if (!m_dirty_texture && mipmappingChanged) {
135 // Full Mipmap Panic!
136 if (!m_mipmap_warned) {
137 qWarning("QSGPlainTexture: Mipmap settings changed without having image data available. "
138 "Call setImage() again or enable m_retain_image. "
139 "Falling back to previous mipmap filtering mode.");
140 m_mipmap_warned = true;
141 }
142 // leave the texture valid and rather ignore the mipmap mode change attempt
143 setMipmapFiltering(d->m_last_mipmap_filter);
144 return;
145 }
146
148 delete m_texture;
149
150 m_texture = nullptr;
152 m_has_alpha = false;
153
154 m_dirty_texture = false;
155 return;
156 }
157
158 m_dirty_texture = false;
159
160 QImage tmp;
161 bool bgra = false;
162 bool needsConvert = false;
164#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
166 tmp = m_image;
167 bgra = true;
168 } else {
169 needsConvert = true;
170 }
171#else
172 needsConvert = true;
173#endif
175 tmp = m_image;
176 } else {
177 needsConvert = true;
178 }
179
180 if (needsConvert)
182
183 // Downscale the texture to fit inside the max texture limit if it is too big.
184 // It would be better if the image was already downscaled to the right size,
185 // but this information is not always available at that time, so as a last
186 // resort we can do it here. Texture coordinates are normalized, so it
187 // won't cause any problems and actual texture sizes will be written
188 // based on QSGTexture::textureSize which is updated after this, so that
189 // should be ok.
190 const int max = rhi->resourceLimit(QRhi::TextureSizeMax);
191 if (tmp.width() > max || tmp.height() > max) {
192 tmp = tmp.scaled(qMin(max, tmp.width()), qMin(max, tmp.height()), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
193 m_texture_size = tmp.size();
194 }
195
200 {
201 const int w = qNextPowerOfTwo(tmp.width() - 1);
202 const int h = qNextPowerOfTwo(tmp.height() - 1);
203 if (tmp.width() != w || tmp.height() != h) {
205 m_texture_size = tmp.size();
206 }
207 }
208
209 bool needsRebuild = false;
210
213 needsRebuild = true;
214 }
215
216 if (mipmappingChanged) {
217 QRhiTexture::Flags f = m_texture->flags();
218 f.setFlag(QRhiTexture::MipMapped, hasMipMaps);
219 f.setFlag(QRhiTexture::UsedWithGenerateMips, hasMipMaps);
221 needsRebuild = true;
222 }
223
224 if (!m_texture) {
225 QRhiTexture::Flags f;
226 if (hasMipMaps)
228
230 needsRebuild = true;
231 }
232
233 if (needsRebuild) {
234 if (!m_texture->create()) {
235 qWarning("Failed to build texture for QSGPlainTexture (size %dx%d)",
237 return;
238 }
239 }
240
241 if (tmp.width() * 4 != tmp.bytesPerLine())
242 tmp = tmp.copy();
243
244 resourceUpdates->uploadTexture(m_texture, tmp);
245
246 if (hasMipMaps) {
247 resourceUpdates->generateMips(m_texture);
248 m_mipmaps_generated = true;
249 }
250
251 d->m_last_mipmap_filter = mipmapFiltering();
252 m_texture_rect = QRectF(0, 0, 1, 1);
253
254 if (!m_retain_image)
255 m_image = QImage();
256}
257
259
260#include "moc_qsgplaintexture_p.cpp"
\inmodule QtGui
Definition qimage.h:37
QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.h:209
qsizetype bytesPerLine() const
Returns the number of bytes per image scanline.
Definition qimage.cpp:1560
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
QSize size() const
Returns the size of the image, i.e.
int width() const
Returns the width of the image.
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition qimage.cpp:1222
int height() const
Returns the height of the image.
@ Format_RGB32
Definition qimage.h:46
@ Format_RGBA8888_Premultiplied
Definition qimage.h:60
@ Format_ARGB32_Premultiplied
Definition qimage.h:48
@ Format_RGBX8888
Definition qimage.h:58
Format format() const
Returns the format of the image.
Definition qimage.cpp:2162
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:125
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtGui
Definition qrhi.h:1731
\inmodule QtGui
Definition qrhi.h:895
void setFlags(Flags f)
Sets the texture flags to f.
Definition qrhi.h:993
@ UsedWithGenerateMips
Definition qrhi.h:903
@ MipMapped
Definition qrhi.h:900
@ ExternalOES
Definition qrhi.h:906
virtual bool create()=0
Creates the corresponding native graphics resources.
Format
Specifies the texture format.
Definition qrhi.h:914
@ UnknownFormat
Definition qrhi.h:915
Flags flags() const
Definition qrhi.h:992
virtual bool createFrom(NativeTexture src)
Similar to create(), except that no new native textures are created.
Definition qrhi.cpp:4487
QSize pixelSize() const
Definition qrhi.h:975
void setPixelSize(const QSize &sz)
Sets the texture size, specified in pixels, to sz.
Definition qrhi.h:976
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags={}) const
Definition qrhi.cpp:10102
int resourceLimit(ResourceLimit limit) const
Definition qrhi.cpp:10121
bool isFeatureSupported(QRhi::Feature feature) const
Definition qrhi.cpp:10110
@ TextureSizeMax
Definition qrhi.h:1888
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition qrhi.cpp:10562
@ NPOTTextureRepeat
Definition qrhi.h:1841
void setTextureFromNativeTexture(QRhi *rhi, quint64 nativeObjectHandle, int nativeLayoutOrState, uint nativeFormat, const QSize &size, QQuickWindow::CreateTextureOptions options, QQuickWindowPrivate::TextureFromNativeTextureFlags flags)
void setImage(const QImage &image)
void setTexture(QRhiTexture *texture)
~QSGPlainTexture() override
QRhiTexture * rhiTexture() const override
const QImage & image()
void commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) override
Call this function to enqueue image upload operations to resourceUpdates, in case there are any pendi...
qint64 comparisonKey() const override
Returns a key suitable for comparing textures.
QRhiTexture * m_texture
static QSGRhiSupport * instance()
\inmodule QtQuick
Definition qsgtexture.h:20
QSGTexture::Filtering mipmapFiltering() const
Returns whether mipmapping should be used when sampling from this texture.
QSGTexture::WrapMode verticalWrapMode() const
Returns the vertical wrap mode to be used for this texture.
void setMipmapFiltering(Filtering filter)
Sets the mipmap sampling mode to filter.
QSGTexture::WrapMode horizontalWrapMode() const
Returns the horizontal wrap mode to be used for this texture.
\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
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
@ SmoothTransformation
@ IgnoreAspectRatio
Definition image.cpp:4
#define qWarning
Definition qlogging.h:166
constexpr quint32 qNextPowerOfTwo(quint32 v)
Definition qmath.h:335
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLfloat GLfloat f
GLbitfield flags
GLenum GLuint texture
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
GLdouble GLdouble t
Definition qopenglext.h:243
unsigned long long quint64
Definition qtypes.h:61
unsigned int uint
Definition qtypes.h:34
long long qint64
Definition qtypes.h:60
QObject::connect nullptr