69QVideoWindowPrivate::QVideoWindowPrivate(QVideoWindow *q) : q(q), m_sink(
new QVideoSink)
73 if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::RhiBasedRendering)) {
74 if (g_graphicsApiOverride) {
75 m_graphicsApi = *g_graphicsApiOverride;
76 q->setSurfaceType(surfaceTypeFor(m_graphicsApi));
78 auto surfaceType = ::platformSurfaceType();
79 q->setSurfaceType(surfaceType);
80 switch (surfaceType) {
81 case QSurface::RasterSurface:
82 case QSurface::OpenVGSurface:
85 m_graphicsApi = QRhi::Null;
87 case QSurface::OpenGLSurface:
88 m_graphicsApi = QRhi::OpenGLES2;
90 case QSurface::VulkanSurface:
91 m_graphicsApi = QRhi::Vulkan;
93 case QSurface::MetalSurface:
94 m_graphicsApi = QRhi::Metal;
96 case QSurface::Direct3DSurface:
97 m_graphicsApi = QRhi::D3D11;
103 QObject::connect(m_sink.get(), &QVideoSink::videoFrameChanged, q, &QVideoWindow::setVideoFrame);
146void QVideoWindowPrivate::initRhi()
148 if (m_graphicsApi == QRhi::Null)
151 [[maybe_unused]] QRhi::Flags rhiFlags = {};
154 if (m_graphicsApi == QRhi::OpenGLES2) {
155 m_fallbackSurface.reset(QRhiGles2InitParams::newFallbackSurface(q->format()));
156 QRhiGles2InitParams params;
157 params.fallbackSurface = m_fallbackSurface.get();
159 params.format = q->format();
160 m_rhi.reset(QRhi::create(QRhi::OpenGLES2, ¶ms, rhiFlags));
165 if (m_graphicsApi == QRhi::Vulkan) {
166 QRhiVulkanInitParams params;
167 params.inst = q->vulkanInstance();
169 m_rhi.reset(QRhi::create(QRhi::Vulkan, ¶ms, rhiFlags));
174 if (m_graphicsApi == QRhi::D3D11) {
175 QRhiD3D11InitParams params;
176 params.enableDebugLayer =
true;
177 m_rhi.reset(QRhi::create(QRhi::D3D11, ¶ms, rhiFlags));
179 if (m_graphicsApi == QRhi::D3D12) {
180 QRhiD3D12InitParams params;
181 m_rhi.reset(QRhi::create(QRhi::D3D12, ¶ms, rhiFlags));
186 if (m_graphicsApi == QRhi::Metal) {
187 QRhiMetalInitParams params;
188 m_rhi.reset(QRhi::create(QRhi::Metal, ¶ms, rhiFlags));
194 m_swapChain.reset(m_rhi->newSwapChain());
195 m_swapChain->setWindow(q);
196 m_renderPass.reset(m_swapChain->newCompatibleRenderPassDescriptor());
197 m_swapChain->setRenderPassDescriptor(m_renderPass.get());
199 m_vertexBuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer,
sizeof(g_vw_quad)));
200 m_vertexBuf->create();
201 m_vertexBufReady =
false;
203 m_uniformBuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
sizeof(QVideoTextureHelper::UniformData)));
204 m_uniformBuf->create();
206 m_textureSampler.reset(m_rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
207 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
208 m_textureSampler->create();
210 m_shaderResourceBindings.reset(m_rhi->newShaderResourceBindings());
211 m_subtitleResourceBindings.reset(m_rhi->newShaderResourceBindings());
213 m_subtitleUniformBuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
sizeof(QVideoTextureHelper::UniformData)));
214 m_subtitleUniformBuf->create();
217void QVideoWindowPrivate::setupGraphicsPipeline(QRhiGraphicsPipeline *pipeline, QRhiShaderResourceBindings *bindings,
const QVideoFrameFormat &fmt)
220 pipeline->setTopology(QRhiGraphicsPipeline::TriangleStrip);
221 QShader vs = vwGetShader(QVideoTextureHelper::vertexShaderFileName(fmt));
222 Q_ASSERT(vs.isValid());
223 QShader fs = vwGetShader(QVideoTextureHelper::fragmentShaderFileName(
224 fmt, m_rhi.get(), m_swapChain->format()));
225 Q_ASSERT(fs.isValid());
226 pipeline->setShaderStages({
227 { QRhiShaderStage::Vertex, vs },
228 { QRhiShaderStage::Fragment, fs }
230 QRhiVertexInputLayout inputLayout;
231 inputLayout.setBindings({
232 { 4 *
sizeof(
float) }
234 inputLayout.setAttributes({
235 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
236 { 0, 1, QRhiVertexInputAttribute::Float2, 2 *
sizeof(
float) }
238 pipeline->setVertexInputLayout(inputLayout);
239 pipeline->setShaderResourceBindings(bindings);
240 pipeline->setRenderPassDescriptor(m_renderPass.get());
244void QVideoWindowPrivate::updateTextures(QRhiResourceUpdateBatch *rub)
247 if (!m_texturePool.currentFrame().isValid())
248 m_texturePool.setCurrentFrame(QVideoFramePrivate::createFrame(
249 std::make_unique<QMemoryVideoBuffer>(QByteArray{ 4, 0 }, 4),
250 QVideoFrameFormat(QSize(1, 1), QVideoFrameFormat::Format_RGBA8888)));
252 if (!m_texturePool.texturesDirty())
255 QVideoFrameTextures *textures = m_texturePool.updateTextures(*m_rhi, *rub);
259 QRhiShaderResourceBinding bindings[4];
261 *(b++) = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
264 auto fmt = m_texturePool.currentFrame().surfaceFormat();
265 auto textureDesc = QVideoTextureHelper::textureDescription(fmt.pixelFormat());
267 for (
int i = 0; i < textureDesc->nplanes; ++i)
268 (*b++) = QRhiShaderResourceBinding::sampledTexture(
269 i + 1, QRhiShaderResourceBinding::FragmentStage, textures->texture(i),
270 m_textureSampler.get());
271 m_shaderResourceBindings->setBindings(bindings, b);
272 m_shaderResourceBindings->create();
276 if (!m_graphicsPipeline)
277 m_graphicsPipeline.reset(m_rhi->newGraphicsPipeline());
279 setupGraphicsPipeline(m_graphicsPipeline.get(), m_shaderResourceBindings.get(), format);
283void QVideoWindowPrivate::updateSubtitle(QRhiResourceUpdateBatch *rub,
const QSize &frameSize)
285 m_subtitleDirty =
false;
286 m_hasSubtitle = !m_texturePool.currentFrame().subtitleText().isEmpty();
290 m_subtitleLayout.update(frameSize, m_texturePool.currentFrame().subtitleText());
291 QSize size = m_subtitleLayout.bounds.size().toSize();
293 QImage img = m_subtitleLayout.toImage();
295 m_subtitleTexture.reset(m_rhi->newTexture(QRhiTexture::RGBA8, size));
296 m_subtitleTexture->create();
297 rub->uploadTexture(m_subtitleTexture.get(), img);
299 QRhiShaderResourceBinding bindings[2];
301 bindings[0] = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
302 m_subtitleUniformBuf.get());
304 bindings[1] = QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage,
305 m_subtitleTexture.get(), m_textureSampler.get());
306 m_subtitleResourceBindings->setBindings(bindings, bindings + 2);
307 m_subtitleResourceBindings->create();
309 if (!m_subtitlePipeline) {
310 m_subtitlePipeline.reset(m_rhi->newGraphicsPipeline());
312 QRhiGraphicsPipeline::TargetBlend blend;
314 m_subtitlePipeline->setTargetBlends({ blend });
315 setupGraphicsPipeline(m_subtitlePipeline.get(), m_subtitleResourceBindings.get(), QVideoFrameFormat(QSize(1, 1), QVideoFrameFormat::Format_RGBA8888));
346void QVideoWindowPrivate::render()
351 if (!q->isExposed() || !isExposed)
354 QRect rect(0, 0, q->width(), q->height());
357 if (backingStore->size() != q->size())
358 backingStore->resize(q->size());
360 backingStore->beginPaint(rect);
362 QPaintDevice *device = backingStore->paintDevice();
365 QPainter painter(device);
367 QVideoFrame frame = m_texturePool.currentFrame();
368 frame.paint(&painter, rect, { Qt::black, aspectRatioMode });
371 backingStore->endPaint();
372 backingStore->flush(rect);
376 const VideoTransformation frameTransformation =
377 qNormalizedFrameTransformation(m_texturePool.currentFrame().surfaceFormat());
378 const QSize frameSize = qRotatedFramePresentationSize(m_texturePool.currentFrame());
379 const QSize scaled = frameSize.scaled(rect.size(), aspectRatioMode);
380 QRect videoRect = QRect(QPoint(0, 0), scaled);
381 videoRect.moveCenter(rect.center());
382 QRect subtitleRect = videoRect.intersected(rect);
384 if (!m_hasSwapChain || (m_swapChain->currentPixelSize() != m_swapChain->surfacePixelSize()))
387 const auto requiredSwapChainFormat =
388 qGetRequiredSwapChainFormat(m_texturePool.currentFrame().surfaceFormat());
389 if (qShouldUpdateSwapChainFormat(m_swapChain.get(), requiredSwapChainFormat)) {
391 m_swapChain->setFormat(requiredSwapChainFormat);
398 QRhi::FrameOpResult r = m_rhi->beginFrame(m_swapChain.get());
400 if (r == QRhi::FrameOpSwapChainOutOfDate) {
404 r = m_rhi->beginFrame(m_swapChain.get());
406 if (r != QRhi::FrameOpSuccess) {
407 qWarning(
"beginFrame failed with %d, retry", r);
412 QRhiResourceUpdateBatch *rub = m_rhi->nextResourceUpdateBatch();
414 if (!m_vertexBufReady) {
415 m_vertexBufReady =
true;
416 rub->uploadStaticBuffer(m_vertexBuf.get(), g_vw_quad);
421 if (m_subtitleDirty || m_subtitleLayout.videoSize != subtitleRect.size())
422 updateSubtitle(rub, subtitleRect.size());
424 const float mirrorFrame = frameTransformation.mirroredHorizontallyAfterRotation ? -1.f : 1.f;
425 const float xscale = mirrorFrame *
float(videoRect.width()) /
float(rect.width());
426 const float yscale = -1.f *
float(videoRect.height()) /
float(rect.height());
428 QMatrix4x4 transform;
429 transform.scale(xscale, yscale);
432 if (m_swapChain->format() == QRhiSwapChain::HDRExtendedSrgbLinear) {
433 auto info = m_swapChain->hdrInfo();
434 if (info.limitsType == QRhiSwapChainHdrInfo::ColorComponentValue)
435 maxNits = 100 * info.limits.colorComponentValue.maxColorComponentValue;
437 maxNits = info.limits.luminanceInNits.maxLuminance;
440 QByteArray uniformData;
441 QVideoTextureHelper::updateUniformData(&uniformData, m_rhi.get(),
442 m_texturePool.currentFrame().surfaceFormat(),
443 m_texturePool.currentFrame(), transform, 1.f, maxNits);
444 rub->updateDynamicBuffer(m_uniformBuf.get(), 0, uniformData.size(), uniformData.constData());
448 st.translate(0, -2.f * (
float(m_subtitleLayout.bounds.center().y()) +
float(subtitleRect.top()))/
float(rect.height()) + 1.f);
449 st.scale(
float(m_subtitleLayout.bounds.width())/
float(rect.width()),
450 -1.f *
float(m_subtitleLayout.bounds.height())/
float(rect.height()));
452 QByteArray uniformData;
453 QVideoFrameFormat fmt(m_subtitleLayout.bounds.size().toSize(), QVideoFrameFormat::Format_ARGB8888);
454 QVideoTextureHelper::updateUniformData(&uniformData, m_rhi.get(), fmt, QVideoFrame(), st,
456 rub->updateDynamicBuffer(m_subtitleUniformBuf.get(), 0, uniformData.size(), uniformData.constData());
459 QRhiCommandBuffer *cb = m_swapChain->currentFrameCommandBuffer();
460 cb->beginPass(m_swapChain->currentFrameRenderTarget(), Qt::black, { 1.0f, 0 }, rub);
461 cb->setGraphicsPipeline(m_graphicsPipeline.get());
462 auto size = m_swapChain->currentPixelSize();
463 cb->setViewport({ 0, 0,
float(size.width()),
float(size.height()) });
464 cb->setShaderResources(m_shaderResourceBindings.get());
466 const quint32 vertexOffset = quint32(
sizeof(
float)) * 16 * frameTransformation.rotationIndex();
467 const QRhiCommandBuffer::VertexInput vbufBinding(m_vertexBuf.get(), vertexOffset);
468 cb->setVertexInput(0, 1, &vbufBinding);
472 cb->setGraphicsPipeline(m_subtitlePipeline.get());
473 cb->setShaderResources(m_subtitleResourceBindings.get());
474 const QRhiCommandBuffer::VertexInput vbufBinding(m_vertexBuf.get(), 0);
475 cb->setVertexInput(0, 1, &vbufBinding);
481 if (m_postRenderHookForTests)
482 m_postRenderHookForTests(*m_rhi, *cb);
484 m_rhi->endFrame(m_swapChain.get());
486 m_texturePool.onFrameEndInvoked();