File indexing completed on 2024-05-26 05:33:23

0001 /*
0002     SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "outputscreencastsource.h"
0008 #include "screencastutils.h"
0009 
0010 #include "compositor.h"
0011 #include "core/output.h"
0012 #include "core/renderloop.h"
0013 #include "opengl/gltexture.h"
0014 #include "opengl/glutils.h"
0015 #include "scene/workspacescene.h"
0016 
0017 #include <drm_fourcc.h>
0018 
0019 namespace KWin
0020 {
0021 
0022 OutputScreenCastSource::OutputScreenCastSource(Output *output, QObject *parent)
0023     : ScreenCastSource(parent)
0024     , m_output(output)
0025 {
0026     connect(m_output, &QObject::destroyed, this, &ScreenCastSource::closed);
0027     connect(m_output, &Output::enabledChanged, this, [this] {
0028         if (!m_output->isEnabled()) {
0029             Q_EMIT closed();
0030         }
0031     });
0032 }
0033 
0034 bool OutputScreenCastSource::hasAlphaChannel() const
0035 {
0036     return true;
0037 }
0038 
0039 quint32 OutputScreenCastSource::drmFormat() const
0040 {
0041     return DRM_FORMAT_ARGB8888;
0042 }
0043 
0044 QSize OutputScreenCastSource::textureSize() const
0045 {
0046     return m_output->pixelSize();
0047 }
0048 
0049 void OutputScreenCastSource::render(spa_data *spa, spa_video_format format)
0050 {
0051     const auto [outputTexture, colorDescription] = Compositor::self()->scene()->textureForOutput(m_output);
0052     if (outputTexture) {
0053         grabTexture(outputTexture.get(), spa, format);
0054     }
0055 }
0056 
0057 void OutputScreenCastSource::render(GLFramebuffer *target)
0058 {
0059     const auto [outputTexture, colorDescription] = Compositor::self()->scene()->textureForOutput(m_output);
0060     if (!outputTexture) {
0061         return;
0062     }
0063 
0064     ShaderBinder shaderBinder(ShaderTrait::MapTexture | ShaderTrait::TransformColorspace);
0065     QMatrix4x4 projectionMatrix;
0066     projectionMatrix.scale(1, -1);
0067     projectionMatrix.ortho(QRect(QPoint(), textureSize()));
0068     shaderBinder.shader()->setUniform(GLShader::Mat4Uniform::ModelViewProjectionMatrix, projectionMatrix);
0069     shaderBinder.shader()->setColorspaceUniformsToSRGB(colorDescription);
0070 
0071     GLFramebuffer::pushFramebuffer(target);
0072     outputTexture->render(textureSize());
0073     GLFramebuffer::popFramebuffer();
0074 }
0075 
0076 std::chrono::nanoseconds OutputScreenCastSource::clock() const
0077 {
0078     return m_output->renderLoop()->lastPresentationTimestamp();
0079 }
0080 
0081 uint OutputScreenCastSource::refreshRate() const
0082 {
0083     return m_output->refreshRate();
0084 }
0085 
0086 } // namespace KWin
0087 
0088 #include "moc_outputscreencastsource.cpp"