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)
46class QSGVideoMaterial;
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);
133class QSGVideoMaterial
final :
public QSGMaterial
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 {
149 const QSGVideoMaterial *m =
static_cast<
const QSGVideoMaterial *>(other);
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;
188void QSGVideoMaterial::
updateTextures(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates)
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);
207 auto m =
static_cast<QSGVideoMaterial *>(newMaterial);
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)
245 auto m =
static_cast<QSGVideoMaterial *>(newMaterial);
246 *texture = &m->m_textures[binding - 1];
253 setFlag(Blending,
false);
256QSGVideoNode::
QSGVideoNode(QQuickVideoOutput *parent,
const QVideoFrameFormat &videoFormat,
261 new QSGVideoMaterial(videoFormat, rhi),
264 setFlag(QSGNode::OwnsMaterial);
265 setFlag(QSGNode::OwnsGeometry);
266 setMaterial(m_material);
271 delete m_subtitleTextNode;
276 texturePool()->setCurrentFrame(frame);
277 markDirty(DirtyMaterial);
278 updateSubtitle(frame);
283 m_material->setSurfaceFormat(surfaceFormat);
284 markDirty(DirtyMaterial);
287void QSGVideoNode::
setHdrInfo(
const QRhiSwapChainHdrInfo &hdrInfo)
289 m_material->setHdrInfo(hdrInfo);
290 markDirty(DirtyMaterial);
293void QSGVideoNode::updateSubtitle(
const QVideoFrame &frame)
295 QSize subtitleFrameSize = m_rect.size().toSize();
296 if (subtitleFrameSize.isEmpty())
299 subtitleFrameSize = qRotatedFrameSize(subtitleFrameSize, m_videoOutputTransformation.rotation);
301 if (!m_subtitleLayout.update(subtitleFrameSize, frame.subtitleText()))
304 delete m_subtitleTextNode;
305 m_subtitleTextNode =
nullptr;
306 if (frame.subtitleText().isEmpty())
309 QQuickItemPrivate *parent_d = QQuickItemPrivate::get(m_parent);
311 m_subtitleTextNode = parent_d->sceneGraphContext()->createInternalTextNode(parent_d->sceneGraphRenderContext());
312 m_subtitleTextNode->setColor(Qt::white);
313 QColor bgColor = Qt::black;
314 bgColor.setAlpha(128);
315 m_subtitleTextNode->addRectangleNode(m_subtitleLayout.bounds, bgColor,
nullptr);
316 m_subtitleTextNode->addTextLayout(m_subtitleLayout.layout.position(),
317 &m_subtitleLayout.layout,
nullptr);
318 appendChildNode(m_subtitleTextNode);
319 setSubtitleGeometry();
322void QSGVideoNode::setSubtitleGeometry()
324 if (!m_subtitleTextNode)
328 updateSubtitle(texturePool()->currentFrame());
330 float rotate = -1.f * qToUnderlying(m_videoOutputTransformation.rotation);
331 float yTranslate = 0;
332 float xTranslate = 0;
333 if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise90) {
334 yTranslate = m_rect.height();
335 }
else if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise180) {
336 yTranslate = m_rect.height();
337 xTranslate = m_rect.width();
338 }
else if (m_videoOutputTransformation.rotation == QtVideo::Rotation::Clockwise270) {
339 xTranslate = m_rect.width();
342 QMatrix4x4 transform;
343 transform.translate(m_rect.x() + xTranslate, m_rect.y() + yTranslate);
344 transform.rotate(rotate, 0, 0, 1);
349 m_subtitleTextNode->setMatrix(transform);
350 m_subtitleTextNode->markDirty(DirtyGeometry);
355 VideoTransformation videoOutputTransformation)
357 const VideoTransformation currentFrameTransformation = qNormalizedFrameTransformation(
358 m_material ? texturePool()->currentFrame() : QVideoFrame{}, videoOutputTransformation);
360 if (rect == m_rect && textureRect == m_textureRect
361 && videoOutputTransformation == m_videoOutputTransformation
362 && currentFrameTransformation == m_frameTransformation)
366 m_textureRect = textureRect;
367 m_videoOutputTransformation = videoOutputTransformation;
368 m_frameTransformation = currentFrameTransformation;
370 QSGGeometry *g = geometry();
373 g =
new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
375 QSGGeometry::TexturedPoint2D *v = g->vertexDataAsTexturedPoint2D();
383 qSetGeom(v + 0, rect.topLeft());
384 qSetGeom(v + 1, rect.bottomLeft());
385 qSetGeom(v + 2, rect.topRight());
386 qSetGeom(v + 3, rect.bottomRight());
389 switch (currentFrameTransformation.rotation) {
392 qSetTex(v + 0, textureRect.topLeft());
393 qSetTex(v + 1, textureRect.bottomLeft());
394 qSetTex(v + 2, textureRect.topRight());
395 qSetTex(v + 3, textureRect.bottomRight());
398 case QtVideo::Rotation::Clockwise90:
400 qSetTex(v + 0, textureRect.bottomLeft());
401 qSetTex(v + 1, textureRect.bottomRight());
402 qSetTex(v + 2, textureRect.topLeft());
403 qSetTex(v + 3, textureRect.topRight());
406 case QtVideo::Rotation::Clockwise180:
408 qSetTex(v + 0, textureRect.bottomRight());
409 qSetTex(v + 1, textureRect.topRight());
410 qSetTex(v + 2, textureRect.bottomLeft());
411 qSetTex(v + 3, textureRect.topLeft());
414 case QtVideo::Rotation::Clockwise270:
416 qSetTex(v + 0, textureRect.topRight());
417 qSetTex(v + 1, textureRect.topLeft());
418 qSetTex(v + 2, textureRect.bottomRight());
419 qSetTex(v + 3, textureRect.bottomLeft());
423 if (m_frameTransformation.mirroredHorizontallyAfterRotation) {
424 qSwapTex(v + 0, v + 2);
425 qSwapTex(v + 1, v + 3);
431 markDirty(DirtyGeometry);
433 setSubtitleGeometry();
438 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
const QVideoFrameFormat m_videoFormat
void setSurfaceFormat(const QRhiSwapChain::Format surfaceFormat)
QSGVideoMaterial(const QVideoFrameFormat &videoFormat, QRhi *rhi)
QRhiSwapChain::Format m_surfaceFormat
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)