6#include <QtMultimedia/private/qhwvideobuffer_p.h>
7#include <QtMultimedia/private/qvideoframetexturepool_p.h>
8#include <QtMultimedia/private/qvideotexturehelper_p.h>
9#include <QtMultimediaQuick/private/qquickvideooutput_p.h>
10#include <QtQuick/qsgmaterial.h>
11#include <QtQuick/private/qquickitem_p.h>
12#include <QtQuick/private/qsginternaltextnode_p.h>
14#if QT_CONFIG(opengles2)
15#include <QtGui/private/qshaderdescription_p.h>
23static inline void qSetGeom(QSGGeometry::TexturedPoint2D *v,
const QPointF &p)
29static inline void qSetTex(QSGGeometry::TexturedPoint2D *v,
const QPointF &p)
35static inline void qSwapTex(QSGGeometry::TexturedPoint2D *v0, QSGGeometry::TexturedPoint2D *v1)
51 const QRhiSwapChain::Format surfaceFormat,
52 const QRhiSwapChainHdrInfo &hdrInfo,
58 setShaderFileName(VertexStage, QVideoTextureHelper::vertexShaderFileName(m_videoFormat));
59 if (QVideoTextureHelper::forceGlTextureExternalOesIsSet()
60 && rhi && rhi->backend() == QRhi::OpenGLES2)
63 setShaderFileName(FragmentStage, QVideoTextureHelper::fragmentShaderFileName(
64 m_videoFormat, rhi, m_surfaceFormat));
70 QSGMaterial *oldMaterial)
override;
73 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
83#if QT_CONFIG(opengles2)
84 qDebug() <<
"QSGVideoMaterialRhiShader: Setting up external OES shader for OpenGLES2";
86 using namespace Qt::Literals::StringLiterals;
87 QByteArray fragmentShader = R"(
88 #extension GL_OES_EGL_image_external : require
89 precision highp float;
90 varying vec2 texCoord;
91 uniform samplerExternalOES tex0;
92 void main()
93 {
94 gl_FragColor = texture2D(tex0, texCoord);
95 }
96 )"_ba;
98 QShaderDescription desc;
99 QShaderDescriptionPrivate *descData = QShaderDescriptionPrivate::get(&desc);
101 QShaderDescription::InOutVariable texCoordInput;
102 texCoordInput.name =
"texCoord";
103 texCoordInput.type = QShaderDescription::Vec2;
104 texCoordInput.location = 0;
106 descData->inVars = { texCoordInput };
108 QShaderDescription::InOutVariable fragColorOutput;
109 fragColorOutput.name =
"gl_FragColor";
110 fragColorOutput.type = QShaderDescription::Vec4;
111 fragColorOutput.location = 0;
113 descData->outVars = { fragColorOutput };
115 QShaderDescription::InOutVariable samplerTex0;
116 samplerTex0.name =
"tex0";
117 samplerTex0.type = QShaderDescription::SamplerExternalOES;
118 samplerTex0.binding = 1;
120 descData->combinedImageSamplers = { samplerTex0 };
123 shaderPack.setStage(QShader::FragmentStage);
124 shaderPack.setDescription(desc);
125 shaderPack.setShader(QShaderKey(QShader::GlslShader, QShaderVersion(100, QShaderVersion::GlslEs)),
126 QShaderCode(fragmentShader));
128 setShader(FragmentStage, shaderPack);
138 static constexpr int NFormats = QRhiSwapChain::HDRExtendedDisplayP3Linear + 1;
139 static QSGMaterialType type[QVideoFrameFormat::NPixelFormats][NFormats];
140 return &type[m_videoFormat.pixelFormat()][m_surfaceFormat];
144 return new QSGVideoMaterialRhiShader(m_videoFormat, m_surfaceFormat, m_hdrInfo, m_rhi);
147 int compare(
const QSGMaterial *other)
const override {
150 qint64 diff = m_textures[0].comparisonKey() - m->m_textures[0].comparisonKey();
152 diff = m_textures[1].comparisonKey() - m->m_textures[1].comparisonKey();
154 diff = m_textures[2].comparisonKey() - m->m_textures[2].comparisonKey();
156 return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
161 setFlag(Blending, !QtPrivate::fuzzyCompare(m_opacity,
float(1.0)));
166 m_surfaceFormat = surfaceFormat;
189 if (!m_texturePool->texturesDirty())
192 QVideoFrameTextures *textures = m_texturePool->updateTextures(*rhi, *resourceUpdates);
196 for (
int plane = 0; plane < 3; ++plane)
197 m_textures[plane].setRhiTexture(textures->texture(plane));
202 QSGMaterial *oldMaterial)
204 Q_UNUSED(oldMaterial);
208 if (!state.isMatrixDirty() && !state.isOpacityDirty())
211 if (state.isOpacityDirty()) {
212 m->m_opacity = state.opacity();
219 m->updateTextures(state.rhi(), state.resourceUpdateBatch());
222 if (m_surfaceFormat == QRhiSwapChain::HDRExtendedSrgbLinear) {
223 if (m_hdrInfo.limitsType == QRhiSwapChainHdrInfo::ColorComponentValue)
224 maxNits = 100 * m_hdrInfo.limits.colorComponentValue.maxColorComponentValue;
226 maxNits = m_hdrInfo.limits.luminanceInNits.maxLuminance;
229 QVideoTextureHelper::updateUniformData(state.uniformData(), m->m_rhi, m_videoFormat,
230 m->m_texturePool->currentFrame(), state.combinedMatrix(),
231 state.opacity(), maxNits);
237 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
240 Q_UNUSED(oldMaterial);
241 if (binding < 1 || binding > 3)
245 *texture = &m->m_textures[binding - 1];
252 setFlag(Blending,
false);
259 setFlag(QSGNode::OwnsMaterial);
260 setFlag(QSGNode::OwnsGeometry);
262 setMaterial(m_material);
267 delete m_subtitleTextNode;
272 texturePool()->setCurrentFrame(frame);
273 markDirty(DirtyMaterial);
274 updateSubtitle(frame);
279 m_material->setSurfaceFormat(surfaceFormat);
280 markDirty(DirtyMaterial);
285 m_material->setHdrInfo(hdrInfo);
286 markDirty(DirtyMaterial);
289void QSGVideoNode::updateSubtitle(
const QVideoFrame &frame)
291 QSize subtitleFrameSize = m_rect.size().toSize();
292 if (subtitleFrameSize.isEmpty())
295 subtitleFrameSize = qRotatedFrameSize(subtitleFrameSize, m_videoOutputTransformation.rotation);
297 if (!m_subtitleLayout.update(subtitleFrameSize, frame.subtitleText()))
300 delete m_subtitleTextNode;
301 m_subtitleTextNode =
nullptr;
302 if (frame.subtitleText().isEmpty())
305 QQuickItemPrivate *parent_d = QQuickItemPrivate::get(m_parent);
307 m_subtitleTextNode = parent_d->sceneGraphContext()->createInternalTextNode(parent_d->sceneGraphRenderContext());
308 m_subtitleTextNode->setColor(Qt::white);
309 QColor bgColor = Qt::black;
310 bgColor.setAlpha(128);
311 m_subtitleTextNode->addRectangleNode(m_subtitleLayout.bounds, bgColor);
312 m_subtitleTextNode->addTextLayout(m_subtitleLayout.layout.position(), &m_subtitleLayout.layout);
313 appendChildNode(m_subtitleTextNode);
314 setSubtitleGeometry();
319 if (!m_subtitleTextNode)
323 updateSubtitle(texturePool()->currentFrame());
325 float rotate = -1.f * qToUnderlying(m_videoOutputTransformation.rotation);
326 float yTranslate = 0;
327 float xTranslate = 0;
328 if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise90) {
329 yTranslate = m_rect.height();
330 }
else if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise180) {
331 yTranslate = m_rect.height();
332 xTranslate = m_rect.width();
333 }
else if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise270) {
334 xTranslate = m_rect.width();
337 QMatrix4x4 transform;
338 transform.translate(m_rect.x() + xTranslate, m_rect.y() + yTranslate);
339 transform.rotate(rotate, 0, 0, 1);
344 m_subtitleTextNode->setMatrix(transform);
345 m_subtitleTextNode->markDirty(DirtyGeometry);
350 VideoTransformation videoOutputTransformation)
352 const VideoTransformation currentFrameTransformation = qNormalizedFrameTransformation(
353 m_material ? texturePool()->currentFrame() : QVideoFrame{}, videoOutputTransformation);
355 if (rect == m_rect && textureRect == m_textureRect
356 && videoOutputTransformation == m_videoOutputTransformation
357 && currentFrameTransformation == m_frameTransformation)
361 m_textureRect = textureRect;
362 m_videoOutputTransformation = videoOutputTransformation;
363 m_frameTransformation = currentFrameTransformation;
365 QSGGeometry *g = geometry();
368 g =
new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
370 QSGGeometry::TexturedPoint2D *v = g->vertexDataAsTexturedPoint2D();
378 qSetGeom(v + 0, rect.topLeft());
379 qSetGeom(v + 1, rect.bottomLeft());
380 qSetGeom(v + 2, rect.topRight());
381 qSetGeom(v + 3, rect.bottomRight());
384 switch (currentFrameTransformation.rotation) {
387 qSetTex(v + 0, textureRect.topLeft());
388 qSetTex(v + 1, textureRect.bottomLeft());
389 qSetTex(v + 2, textureRect.topRight());
390 qSetTex(v + 3, textureRect.bottomRight());
393 case QtVideo::Rotation::Clockwise90:
395 qSetTex(v + 0, textureRect.bottomLeft());
396 qSetTex(v + 1, textureRect.bottomRight());
397 qSetTex(v + 2, textureRect.topLeft());
398 qSetTex(v + 3, textureRect.topRight());
401 case QtVideo::Rotation::Clockwise180:
403 qSetTex(v + 0, textureRect.bottomRight());
404 qSetTex(v + 1, textureRect.topRight());
405 qSetTex(v + 2, textureRect.bottomLeft());
406 qSetTex(v + 3, textureRect.topLeft());
409 case QtVideo::Rotation::Clockwise270:
411 qSetTex(v + 0, textureRect.topRight());
412 qSetTex(v + 1, textureRect.topLeft());
413 qSetTex(v + 2, textureRect.bottomRight());
414 qSetTex(v + 3, textureRect.bottomLeft());
418 if (m_frameTransformation.mirroredHorizontallyAfterRotation) {
419 qSwapTex(v + 0, v + 2);
420 qSwapTex(v + 1, v + 3);
426 markDirty(DirtyGeometry);
428 setSubtitleGeometry();
433 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)