File indexing completed on 2024-04-14 04:53:33

0001 /* This file is part of the KDE project
0002    Copyright (C) 2009 Collabora Ltd <info@collabora.co.uk>
0003     @author George Goldberg <george.goldberg@collabora.co.uk>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "framebuffermanager.h"
0022 
0023 #include "framebufferplugin.h"
0024 #include "krfbconfig.h"
0025 #include "krfbdebug.h"
0026 
0027 #include <QGuiApplication>
0028 #include <QGlobalStatic>
0029 
0030 #include <KPluginFactory>
0031 #include <KPluginMetaData>
0032 
0033 
0034 class FrameBufferManagerStatic
0035 {
0036 public:
0037     FrameBufferManager instance;
0038 };
0039 
0040 Q_GLOBAL_STATIC(FrameBufferManagerStatic, frameBufferManagerStatic)
0041 
0042 FrameBufferManager::FrameBufferManager()
0043 {
0044     const auto platformFilter = [] (const KPluginMetaData &pluginData) {
0045         return pluginData.value(QStringLiteral("X-KDE-OnlyShowOnQtPlatforms"), QStringList()).contains(QGuiApplication::platformName());
0046     };
0047     const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("krfb/framebuffer"), platformFilter, KPluginMetaData::AllowEmptyMetaData);
0048     for (const KPluginMetaData &data : plugins) {
0049         const KPluginFactory::Result<FrameBufferPlugin> result = KPluginFactory::instantiatePlugin<FrameBufferPlugin>(data);
0050         if (result.plugin) {
0051             m_plugins.insert(data.pluginId(), result.plugin);
0052             qCDebug(KRFB) << "Loaded plugin with name " << data.pluginId();
0053         } else {
0054             qCDebug(KRFB) << "unable to load plugin for " << data.fileName() << result.errorString;
0055         }
0056     }
0057 }
0058 
0059 FrameBufferManager::~FrameBufferManager()
0060 {
0061 }
0062 
0063 FrameBufferManager *FrameBufferManager::instance()
0064 {
0065     return &frameBufferManagerStatic->instance;
0066 }
0067 
0068 QSharedPointer<FrameBuffer> FrameBufferManager::frameBuffer(WId id, const QVariantMap &args)
0069 {
0070     // See if there is still an existing framebuffer to this WId.
0071     if (m_frameBuffers.contains(id)) {
0072         QWeakPointer<FrameBuffer> weakFrameBuffer = m_frameBuffers.value(id);
0073 
0074         if (weakFrameBuffer) {
0075             //qDebug() << "Found cached frame buffer.";
0076             return weakFrameBuffer.toStrongRef();
0077         } else {
0078             //qDebug() << "Found deleted cached frame buffer. Don't use.";
0079             m_frameBuffers.remove(id);
0080         }
0081     }
0082 
0083     if (auto preferredPlugin = m_plugins.value(KrfbConfig::preferredFrameBufferPlugin())) {
0084         if (auto frameBuffer = QSharedPointer<FrameBuffer>(preferredPlugin->frameBuffer(args))) {
0085             qCDebug(KRFB) << "Using FrameBuffer:" << KrfbConfig::preferredFrameBufferPlugin();
0086             m_frameBuffers.insert(id, frameBuffer.toWeakRef());
0087             return frameBuffer;
0088         }
0089     }
0090 
0091     // We don't already have that frame buffer.
0092     for (auto it = m_plugins.cbegin(); it != m_plugins.constEnd(); it++) {
0093         QSharedPointer<FrameBuffer> frameBuffer(it.value()->frameBuffer(args));
0094         if (frameBuffer) {
0095             qCDebug(KRFB) << "Using FrameBuffer:" << it.key();
0096             m_frameBuffers.insert(id, frameBuffer.toWeakRef());
0097             return frameBuffer;
0098         }
0099     }
0100 
0101     // No valid framebuffer plugin found.
0102     qCDebug(KRFB) << "No valid framebuffer found. returning null.";
0103     return QSharedPointer<FrameBuffer>();
0104 }