File indexing completed on 2024-04-28 05:27:34

0001 /*
0002     SPDX-FileCopyrightText: 2023 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include "pipewiresourcestream.h"
0010 #include <QByteArray>
0011 #include <epoxy/egl.h>
0012 #include <kpipewire_export.h>
0013 
0014 typedef unsigned int GLenum;
0015 
0016 /**
0017  * The @class PipeWireFrameCleanupFunction is used to track the lifetime of a pipewire frame.
0018  *
0019  * It is used to have a ref-counted class that will call the cleanup function when
0020  * it's left with no references.
0021  * This is useful so that it can be passed to QImage() if necessary without having to
0022  * track if the QImage itself outlives the buffer.
0023  */
0024 class PipeWireFrameCleanupFunction
0025 {
0026     Q_DISABLE_COPY(PipeWireFrameCleanupFunction)
0027 public:
0028     PipeWireFrameCleanupFunction(std::function<void()> cleanup)
0029         : m_ref(0)
0030         , m_cleanup(cleanup)
0031     {
0032     }
0033 
0034     void ref()
0035     {
0036         m_ref++;
0037     }
0038     static void unref(void *x)
0039     {
0040         if (!x) {
0041             return;
0042         }
0043         auto self = static_cast<PipeWireFrameCleanupFunction *>(x);
0044         self->m_ref--;
0045         if (self->m_ref == 0) {
0046             self->m_cleanup();
0047             delete self;
0048         }
0049     }
0050 
0051 private:
0052     QAtomicInt m_ref;
0053     std::function<void()> m_cleanup;
0054 };
0055 
0056 namespace PWHelpers
0057 {
0058 
0059 KPIPEWIRE_EXPORT QImage
0060 SpaBufferToQImage(const uchar *data, int width, int height, qsizetype bytesPerLine, spa_video_format format, PipeWireFrameCleanupFunction *cleanup);
0061 }