6#include <QtMultimedia/private/qhwvideobuffer_p.h>
7#include <QtMultimedia/private/qvideoframetexturepool_p.h>
8#include <QtMultimedia/private/qvideotexturehelper_p.h>
10#include <QtMultimediaQuick/private/qsgvideotexture_p.h>
11#include <QtMultimediaQuick/private/qquickvideooutput_p.h>
13#include <QtQuick/qsgmaterial.h>
14#include <QtQuick/private/qquickitem_p.h>
15#include <QtQuick/private/qsginternaltextnode_p.h>
17#if QT_CONFIG(opengles2)
18#include <QtGui/private/qshaderdescription_p.h>
24static inline void qSetGeom(QSGGeometry::TexturedPoint2D *v,
const QPointF &p)
30static inline void qSetTex(QSGGeometry::TexturedPoint2D *v,
const QPointF &p)
36static inline void qSwapTex(QSGGeometry::TexturedPoint2D *v0, QSGGeometry::TexturedPoint2D *v1)
52 const QRhiSwapChain::Format surfaceFormat,
53 const QRhiSwapChainHdrInfo &hdrInfo,
59 setShaderFileName(VertexStage, QVideoTextureHelper::vertexShaderFileName(m_videoFormat));
60 if (QVideoTextureHelper::forceGlTextureExternalOesIsSet()
61 && rhi && rhi->backend() == QRhi::OpenGLES2)
64 setShaderFileName(FragmentStage, QVideoTextureHelper::fragmentShaderFileName(
65 m_videoFormat, rhi, m_surfaceFormat));
71 QSGMaterial *oldMaterial)
override;
74 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
84#if QT_CONFIG(opengles2)
85 qDebug() <<
"QSGVideoMaterialRhiShader: Setting up external OES shader for OpenGLES2";
87 using namespace Qt::Literals::StringLiterals;
88 QByteArray fragmentShader = R"(
89 #extension GL_OES_EGL_image_external : require
90 precision highp float;
91 varying vec2 texCoord;
92 uniform samplerExternalOES tex0;
93 void main()
94 {
95 gl_FragColor = texture2D(tex0, texCoord);
96 }
97 )"_ba;
99 QShaderDescription desc;
100 QShaderDescriptionPrivate *descData = QShaderDescriptionPrivate::get(&desc);
102 QShaderDescription::InOutVariable texCoordInput;
103 texCoordInput.name =
"texCoord";
104 texCoordInput.type = QShaderDescription::Vec2;
105 texCoordInput.location = 0;
107 descData->inVars = { texCoordInput };
109 QShaderDescription::InOutVariable fragColorOutput;
110 fragColorOutput.name =
"gl_FragColor";
111 fragColorOutput.type = QShaderDescription::Vec4;
112 fragColorOutput.location = 0;
114 descData->outVars = { fragColorOutput };
116 QShaderDescription::InOutVariable samplerTex0;
117 samplerTex0.name =
"tex0";
118 samplerTex0.type = QShaderDescription::SamplerExternalOES;
119 samplerTex0.binding = 1;
121 descData->combinedImageSamplers = { samplerTex0 };
124 shaderPack.setStage(QShader::FragmentStage);
125 shaderPack.setDescription(desc);
126 shaderPack.setShader(QShaderKey(QShader::GlslShader, QShaderVersion(100, QShaderVersion::GlslEs)),
127 QShaderCode(fragmentShader));
129 setShader(FragmentStage, shaderPack);
139 static constexpr int NFormats = QRhiSwapChain::HDRExtendedDisplayP3Linear + 1;
140 static QSGMaterialType type[QVideoFrameFormat::NPixelFormats][NFormats];
141 return &type[m_videoFormat.pixelFormat()][m_surfaceFormat];
145 return new QSGVideoMaterialRhiShader(m_videoFormat, m_surfaceFormat, m_hdrInfo, m_rhi);
148 int compare(
const QSGMaterial *other)
const override {
151 qint64 diff = m_textures[0].comparisonKey() - m->m_textures[0].comparisonKey();
153 diff = m_textures[1].comparisonKey() - m->m_textures[1].comparisonKey();
155 diff = m_textures[2].comparisonKey() - m->m_textures[2].comparisonKey();
157 return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
162 setFlag(Blending, !QtPrivate::fuzzyCompare(m_opacity,
float(1.0)));
167 m_surfaceFormat = surfaceFormat;
190 if (!m_texturePool->texturesDirty())
193 QVideoFrameTextures *textures = m_texturePool->updateTextures(*rhi, *resourceUpdates);
197 for (
int plane = 0; plane < 3; ++plane)
198 m_textures[plane].setRhiTexture(textures->texture(plane));
203 QSGMaterial *oldMaterial)
205 Q_UNUSED(oldMaterial);
209 if (!state.isMatrixDirty() && !state.isOpacityDirty())
212 if (state.isOpacityDirty()) {
213 m->m_opacity = state.opacity();
220 m->updateTextures(state.rhi(), state.resourceUpdateBatch());
223 if (m_surfaceFormat == QRhiSwapChain::HDRExtendedSrgbLinear) {
224 if (m_hdrInfo.limitsType == QRhiSwapChainHdrInfo::ColorComponentValue)
225 maxNits = 100 * m_hdrInfo.limits.colorComponentValue.maxColorComponentValue;
227 maxNits = m_hdrInfo.limits.luminanceInNits.maxLuminance;
230 QVideoTextureHelper::updateUniformData(state.uniformData(), m->m_rhi, m_videoFormat,
231 m->m_texturePool->currentFrame(), state.combinedMatrix(),
232 state.opacity(), maxNits);
238 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
241 Q_UNUSED(oldMaterial);
242 if (binding < 1 || binding > 3)
246 *texture = &m->m_textures[binding - 1];
253 setFlag(Blending,
false);
260 setFlag(QSGNode::OwnsMaterial);
261 setFlag(QSGNode::OwnsGeometry);
263 setMaterial(m_material);
268 delete m_subtitleTextNode;
273 texturePool()->setCurrentFrame(frame);
274 markDirty(DirtyMaterial);
275 updateSubtitle(frame);
280 m_material->setSurfaceFormat(surfaceFormat);
281 markDirty(DirtyMaterial);
286 m_material->setHdrInfo(hdrInfo);
287 markDirty(DirtyMaterial);
290void QSGVideoNode::updateSubtitle(
const QVideoFrame &frame)
292 QSize subtitleFrameSize = m_rect.size().toSize();
293 if (subtitleFrameSize.isEmpty())
296 subtitleFrameSize = qRotatedFrameSize(subtitleFrameSize, m_videoOutputTransformation.rotation);
298 if (!m_subtitleLayout.update(subtitleFrameSize, frame.subtitleText()))
301 delete m_subtitleTextNode;
302 m_subtitleTextNode =
nullptr;
303 if (frame.subtitleText().isEmpty())
306 QQuickItemPrivate *parent_d = QQuickItemPrivate::get(m_parent);
308 m_subtitleTextNode = parent_d->sceneGraphContext()->createInternalTextNode(parent_d->sceneGraphRenderContext());
309 m_subtitleTextNode->setColor(Qt::white);
310 QColor bgColor = Qt::black;
311 bgColor.setAlpha(128);
312 m_subtitleTextNode->addRectangleNode(m_subtitleLayout.bounds, bgColor,
nullptr);
313 m_subtitleTextNode->addTextLayout(m_subtitleLayout.layout.position(),
314 &m_subtitleLayout.layout,
nullptr);
315 appendChildNode(m_subtitleTextNode);
316 setSubtitleGeometry();
321 if (!m_subtitleTextNode)
325 updateSubtitle(texturePool()->currentFrame());
327 float rotate = -1.f * qToUnderlying(m_videoOutputTransformation.rotation);
328 float yTranslate = 0;
329 float xTranslate = 0;
330 if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise90) {
331 yTranslate = m_rect.height();
332 }
else if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise180) {
333 yTranslate = m_rect.height();
334 xTranslate = m_rect.width();
335 }
else if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise270) {
336 xTranslate = m_rect.width();
339 QMatrix4x4 transform;
340 transform.translate(m_rect.x() + xTranslate, m_rect.y() + yTranslate);
341 transform.rotate(rotate, 0, 0, 1);
346 m_subtitleTextNode->setMatrix(transform);
347 m_subtitleTextNode->markDirty(DirtyGeometry);
352 VideoTransformation videoOutputTransformation)
354 const VideoTransformation currentFrameTransformation = qNormalizedFrameTransformation(
355 m_material ? texturePool()->currentFrame() : QVideoFrame{}, videoOutputTransformation);
357 if (rect == m_rect && textureRect == m_textureRect
358 && videoOutputTransformation == m_videoOutputTransformation
359 && currentFrameTransformation == m_frameTransformation)
363 m_textureRect = textureRect;
364 m_videoOutputTransformation = videoOutputTransformation;
365 m_frameTransformation = currentFrameTransformation;
367 QSGGeometry *g = geometry();
370 g =
new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
372 QSGGeometry::TexturedPoint2D *v = g->vertexDataAsTexturedPoint2D();
380 qSetGeom(v + 0, rect.topLeft());
381 qSetGeom(v + 1, rect.bottomLeft());
382 qSetGeom(v + 2, rect.topRight());
383 qSetGeom(v + 3, rect.bottomRight());
386 switch (currentFrameTransformation.rotation) {
389 qSetTex(v + 0, textureRect.topLeft());
390 qSetTex(v + 1, textureRect.bottomLeft());
391 qSetTex(v + 2, textureRect.topRight());
392 qSetTex(v + 3, textureRect.bottomRight());
395 case QtVideo::Rotation::Clockwise90:
397 qSetTex(v + 0, textureRect.bottomLeft());
398 qSetTex(v + 1, textureRect.bottomRight());
399 qSetTex(v + 2, textureRect.topLeft());
400 qSetTex(v + 3, textureRect.topRight());
403 case QtVideo::Rotation::Clockwise180:
405 qSetTex(v + 0, textureRect.bottomRight());
406 qSetTex(v + 1, textureRect.topRight());
407 qSetTex(v + 2, textureRect.bottomLeft());
408 qSetTex(v + 3, textureRect.topLeft());
411 case QtVideo::Rotation::Clockwise270:
413 qSetTex(v + 0, textureRect.topRight());
414 qSetTex(v + 1, textureRect.topLeft());
415 qSetTex(v + 2, textureRect.bottomRight());
416 qSetTex(v + 3, textureRect.bottomLeft());
420 if (m_frameTransformation.mirroredHorizontallyAfterRotation) {
421 qSwapTex(v + 0, v + 2);
422 qSwapTex(v + 1, v + 3);
428 markDirty(DirtyGeometry);
430 setSubtitleGeometry();
435 return m_material->m_texturePool;
QVideoFrameFormat m_videoFormat
QRhiSwapChainHdrInfo m_hdrInfo
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
QSGVideoMaterialRhiShader(const QVideoFrameFormat &videoFormat, const QRhiSwapChain::Format surfaceFormat, const QRhiSwapChainHdrInfo &hdrInfo, QRhi *rhi)
QRhiSwapChain::Format m_surfaceFormat
void setupExternalOESShader()
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
QVideoFrameTexturePoolPtr m_texturePool
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
void updateTextures(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
void setHdrInfo(const QRhiSwapChainHdrInfo &hdrInfo)
int compare(const QSGMaterial *other) const override
Compares this material to other and returns 0 if they are equal; -1 if this material should sort befo...
QRhiSwapChainHdrInfo m_hdrInfo
std::array< QSGVideoTexture, 3 > m_textures
void setSurfaceFormat(const QRhiSwapChain::Format surfaceFormat)
QSGVideoMaterial(const QVideoFrameFormat &videoFormat, QRhi *rhi)
QRhiSwapChain::Format m_surfaceFormat
QVideoFrameFormat m_videoFormat
const QVideoFrameTexturePoolPtr & texturePool() const
void setHdrInfo(const QRhiSwapChainHdrInfo &hdrInfo)
void setSurfaceFormat(const QRhiSwapChain::Format surfaceFormat)
QSGVideoNode(QQuickVideoOutput *parent, const QVideoFrameFormat &videoFormat, QRhi *rhi)
void setCurrentFrame(const QVideoFrame &frame)
void setTexturedRectGeometry(const QRectF &boundingRect, const QRectF &textureRect, VideoTransformation videoOutputTransformation)
Combined button and popup list for selecting options.
static void qSetTex(QSGGeometry::TexturedPoint2D *v, const QPointF &p)
static QT_BEGIN_NAMESPACE void qSetGeom(QSGGeometry::TexturedPoint2D *v, const QPointF &p)
static void qSwapTex(QSGGeometry::TexturedPoint2D *v0, QSGGeometry::TexturedPoint2D *v1)