60class QOhosTextureVideoBuffer
final :
public QHwVideoBuffer
64 std::weak_ptr<QRhi> producerRhi, QPointer<QObject> producer,
65 QPointer<QOpenGLContext> producerContext)
77 if (m_mapMode != QVideoFrame::NotMapped || mode != QVideoFrame::ReadOnly)
79 m_mapMode = QVideoFrame::ReadOnly;
81 m_image = readbackOnProducerThread();
82 if (m_image.isNull()) {
83 m_mapMode = QVideoFrame::NotMapped;
87 data.bytesPerLine[0] = m_image.bytesPerLine();
88 data.dataSize[0] =
static_cast<
int>(m_image.sizeInBytes());
89 data.data[0] = m_image.bits();
96 m_mapMode = QVideoFrame::NotMapped;
106 if (!isCompatibleRhi(rhi))
109 m_tex->nativeTexture().object);
113 bool isCompatibleRhi(QRhi &rhi)
const
115 if (rhi.backend() != QRhi::OpenGLES2)
117 if (!m_producerContext)
119 const auto *handles =
120 static_cast<
const QRhiGles2NativeHandles *>(rhi.nativeHandles());
121 if (!handles || !handles->context)
123 return handles->context->shareGroup() == m_producerContext->shareGroup();
126 QImage readbackOnProducerThread()
const
128 auto producerRhi = m_producerRhi.lock();
129 if (!producerRhi || !m_producer)
132 QRhi *rhi = producerRhi.get();
133 QRhiTexture *tex = m_tex.get();
134 QMetaObject::invokeMethod(
137 QRhiReadbackResult result;
139 result.completed = [&done] { done =
true; };
140 QRhiCommandBuffer *cb =
nullptr;
141 if (rhi->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
143 QRhiResourceUpdateBatch *rub = rhi->nextResourceUpdateBatch();
144 rub->readBackTexture({ tex }, &result);
145 cb->resourceUpdate(rub);
146 rhi->endOffscreenFrame();
147 if (!done || result.data.isEmpty())
149 QImage img(
reinterpret_cast<
const uchar *>(result.data.constData()),
150 result.pixelSize.width(), result.pixelSize.height(),
151 result.pixelSize.width() * 4, QImage::Format_RGBA8888);
154 Qt::BlockingQueuedConnection);
159 std::unique_ptr<QRhiTexture> m_tex;
161 QVideoFrame::MapMode m_mapMode = QVideoFrame::NotMapped;
162 std::weak_ptr<QRhi> m_producerRhi;
163 QPointer<QObject> m_producer;
164 QPointer<QOpenGLContext> m_producerContext;
172 m_vertexBuffer.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer,
174 m_vertexBuffer->create();
176 m_uniformBuffer.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic,
177 QRhiBuffer::UniformBuffer, 160));
178 m_uniformBuffer->create();
180 m_sampler.reset(m_rhi->newSampler(QRhiSampler::Nearest, QRhiSampler::Nearest,
181 QRhiSampler::None, QRhiSampler::ClampToEdge,
182 QRhiSampler::ClampToEdge));
185 m_srb.reset(m_rhi->newShaderResourceBindings());
187 QRhiShaderResourceBinding::uniformBuffer(
189 QRhiShaderResourceBinding::VertexStage
190 | QRhiShaderResourceBinding::FragmentStage,
191 m_uniformBuffer.get()),
192 QRhiShaderResourceBinding::sampledTexture(
193 1, QRhiShaderResourceBinding::FragmentStage, externalTex, m_sampler.get())
197 m_vertexShader = loadShader(
198 QStringLiteral(
":/qt-project.org/multimedia/shaders/externalsampler.vert.qsb"));
199 m_fragmentShader = loadShader(
200 QStringLiteral(
":/qt-project.org/multimedia/shaders/externalsampler.frag.qsb"));
206 static QShader loadShader(
const QString &name)
209 if (f.open(QIODevice::ReadOnly))
210 return QShader::fromSerialized(f.readAll());
214 QRhi *m_rhi{
nullptr };
215 std::unique_ptr<QRhiBuffer> m_vertexBuffer;
216 std::unique_ptr<QRhiBuffer> m_uniformBuffer;
217 std::unique_ptr<QRhiSampler> m_sampler;
218 std::unique_ptr<QRhiShaderResourceBindings> m_srb;
219 QShader m_vertexShader;
220 QShader m_fragmentShader;
224 QRhiShaderResourceBindings *srb,
225 QRhiRenderPassDescriptor *rpd,
226 QShader vs, QShader fs)
228 std::unique_ptr<QRhiGraphicsPipeline> gp(rhi->newGraphicsPipeline());
229 gp->setTopology(QRhiGraphicsPipeline::TriangleFan);
230 gp->setShaderStages({
231 { QRhiShaderStage::Vertex, vs },
232 { QRhiShaderStage::Fragment, fs }
235 layout.setBindings({ { 4 *
sizeof(
float) } });
236 layout.setAttributes({
237 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
238 { 0, 1, QRhiVertexInputAttribute::Float2, 2 *
sizeof(
float) }
240 gp->setVertexInputLayout(layout);
241 gp->setShaderResourceBindings(srb);
242 gp->setRenderPassDescriptor(rpd);
250 std::unique_ptr<QRhiTexture> tex(
251 m_rhi->newTexture(QRhiTexture::RGBA8, size, 1, QRhiTexture::RenderTarget));
252 if (!tex->create()) {
253 qCWarning(qLcOhosMediaPlugin) <<
"Failed to create frame texture";
257 std::unique_ptr<QRhiTextureRenderTarget> renderTarget(
258 m_rhi->newTextureRenderTarget({ { tex.get() } }));
259 std::unique_ptr<QRhiRenderPassDescriptor> rpd(
260 renderTarget->newCompatibleRenderPassDescriptor());
261 renderTarget->setRenderPassDescriptor(rpd.get());
262 renderTarget->create();
264 QRhiResourceUpdateBatch *rub = m_rhi->nextResourceUpdateBatch();
265 rub->uploadStaticBuffer(m_vertexBuffer.get(),
g_quad);
267 const QMatrix4x4 identity;
268 char *p = m_uniformBuffer->beginFullDynamicBufferUpdateForCurrentFrame();
269 memcpy(p, identity.constData(), 64);
270 memcpy(p + 64, externalTexMatrix.constData(), 64);
271 const float opacity = 1.0f;
272 memcpy(p + 64 + 64, &opacity, 4);
273 m_uniformBuffer->endFullDynamicBufferUpdateForCurrentFrame();
275 auto pipeline = newGraphicsPipeline(m_rhi, m_srb.get(), rpd.get(), m_vertexShader,
278 const QRhiCommandBuffer::VertexInput vbufBinding(m_vertexBuffer.get(), 0);
279 QRhiCommandBuffer *cb =
nullptr;
280 if (m_rhi->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
283 cb->beginPass(renderTarget.get(), Qt::transparent, { 1.0f, 0 }, rub);
284 cb->setGraphicsPipeline(pipeline.get());
285 cb->setViewport({ 0, 0,
float(size.width()),
float(size.height()) });
286 cb->setShaderResources(m_srb.get());
287 cb->setVertexInput(0, 1, &vbufBinding);
290 m_rhi->endOffscreenFrame();
370 0.0f, -1.0f, 0.0f, 1.0f,
371 0.0f, 0.0f, 1.0f, 0.0f,
372 0.0f, 0.0f, 0.0f, 1.0f);
399 OHNativeWindow *ensureSurface(QRhi *rhi)
401 const auto *nativeHandles =
402 rhi ?
static_cast<
const QRhiGles2NativeHandles *>(rhi->nativeHandles())
404 QOpenGLContext *shareContext = nativeHandles ? nativeHandles->context :
nullptr;
409 const bool reuse = m_surfaceImage && m_surfaceImage->isValid()
410 && (rhi ? !m_isHeadless && m_shareContext == shareContext : m_isHeadless);
412 return m_surfaceImage->nativeWindow();
421 QRhiGles2InitParams params;
422 params.shareContext = shareContext;
423 params.fallbackSurface = QRhiGles2InitParams::newFallbackSurface();
424 m_isHeadless = (rhi ==
nullptr);
425 m_shareContext = shareContext;
426 m_rhi.reset(QRhi::create(QRhi::OpenGLES2, ¶ms));
428 qCWarning(qLcOhosMediaPlugin) <<
"Failed to create offscreen GLES2 RHI";
432 m_externalTexture.reset(
433 m_rhi->newTexture(QRhiTexture::RGBA8, m_size.isEmpty() ? QSize{ 1, 1 } : m_size, 1,
434 QRhiTexture::ExternalOES));
435 if (!m_externalTexture->create()) {
436 qCWarning(qLcOhosMediaPlugin) <<
"External OES texture create failed";
437 m_externalTexture.reset();
442 const auto nativeTex = m_externalTexture->nativeTexture();
443 m_surfaceImage = std::make_unique<QOhosSurfaceImage>(uint32_t(nativeTex.object));
444 if (!m_surfaceImage->isValid()) {
445 m_surfaceImage.reset();
446 m_externalTexture.reset();
451 const quint64 index = m_surfaceImage->index();
452 connect(m_surfaceImage.get(), &QOhosSurfaceImage::frameAvailable,
this,
453 [
this, index]() { onFrameAvailable(index); }, Qt::QueuedConnection);
455 m_textureCopy = std::make_unique<TextureCopy>(m_rhi.get(), m_externalTexture.get());
456 return m_surfaceImage->nativeWindow();
459 std::shared_ptr<QRhi> m_rhi;
460 std::unique_ptr<QRhiTexture> m_externalTexture;
461 std::unique_ptr<QOhosSurfaceImage> m_surfaceImage;
462 std::unique_ptr<TextureCopy> m_textureCopy;
463 QSize m_size{ 1, 1 };
465 QPointer<QOpenGLContext> m_shareContext;
466 bool m_isHeadless{
false };
473 m_textureThread = std::make_shared<QOhosTextureThread>();
474 connect(m_textureThread.get(), &QOhosTextureThread::newFrame,
this,
475 &QOhosVideoOutput::onNewFrame, Qt::QueuedConnection);
476 m_textureThread->launch();
478 if (
auto *p = sink ? sink->platformVideoSink() :
nullptr) {
479 connect(p, &QPlatformVideoSink::rhiChanged,
this, &
QOhosVideoOutput::onRhiChanged);
482 if (m_contentSource == ContentSource::Camera) {
483 if (QScreen *screen = QGuiApplication::primaryScreen()) {
484 connect(screen, &QScreen::orientationChanged,
this,
487 updateDisplayRotation();
QOhosTextureVideoBuffer(std::unique_ptr< QRhiTexture > tex, const QSize &size, std::weak_ptr< QRhi > producerRhi, QPointer< QObject > producer, QPointer< QOpenGLContext > producerContext)
QVideoFrameTexturesUPtr mapTextures(QRhi &rhi) override
void unmap() override
Releases the memory mapped by the map() function.
MapData map(QVideoFrame::MapMode mode) override
Maps the planes of a video buffer to memory.
QRhiTexture * texture(uint plane) const override
QOhosVideoFrameTextures(QRhi *rhi, QSize size, quint64 handle)
std::unique_ptr< QRhiTexture > copyExternalTexture(QSize size, const QMatrix4x4 &externalTexMatrix)
TextureCopy(QRhi *rhi, QRhiTexture *externalTex)