File indexing completed on 2024-05-19 05:31:43

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include "kwin_export.h"
0013 
0014 #include <QObject>
0015 #include <QRect>
0016 #include <QUrl>
0017 
0018 #include <memory>
0019 
0020 class QKeyEvent;
0021 class QMouseEvent;
0022 
0023 class QMouseEvent;
0024 class QKeyEvent;
0025 
0026 class QQmlContext;
0027 class QQuickItem;
0028 class QQuickWindow;
0029 
0030 namespace KWin
0031 {
0032 class GLTexture;
0033 
0034 class OffscreenQuickView;
0035 
0036 /**
0037  * @brief The KwinQuickView class provides a convenient API for exporting
0038  * QtQuick scenes as buffers that can be composited in any other fashion.
0039  *
0040  * Contents can be fetched as a GL Texture or as a QImage
0041  * If data is to be fetched as an image, it should be specified upfront as
0042  * blitting is performed when we update our FBO to keep kwin's render loop
0043  * as fast as possible.
0044  */
0045 class KWIN_EXPORT OffscreenQuickView : public QObject
0046 {
0047     Q_OBJECT
0048 
0049 public:
0050     enum class ExportMode {
0051         /** The contents will be available as a texture in the shared contexts. Image will be blank*/
0052         Texture,
0053         /** The contents will be blit during the update into a QImage buffer. */
0054         Image
0055     };
0056 
0057     /**
0058      * Construct a new KWinQuickView explicitly stating an export mode. \a alpha indicates
0059      * whether the view is translucent or not.
0060      */
0061     explicit OffscreenQuickView(ExportMode exportMode = ExportMode::Texture, bool alpha = true);
0062 
0063     /**
0064      * Note that this may change the current GL Context
0065      */
0066     ~OffscreenQuickView();
0067 
0068     QSize size() const;
0069 
0070     /**
0071      * The geometry of the current view
0072      * This may be out of sync with the current buffer size if an update is pending
0073      */
0074     void setGeometry(const QRect &rect);
0075     QRect geometry() const;
0076 
0077     void setOpacity(qreal opacity);
0078     qreal opacity() const;
0079     bool hasAlphaChannel() const;
0080 
0081     /**
0082      * Render the current scene graph into the FBO.
0083      * This is typically done automatically when the scene changes
0084      * albeit deffered by a timer
0085      *
0086      * It can be manually invoked to update the contents immediately.
0087      * Note this will change the GL context
0088      */
0089     void update();
0090 
0091     /** The invisble root item of the window*/
0092     QQuickItem *contentItem() const;
0093     QQuickWindow *window() const;
0094 
0095     /**
0096      * @brief Marks the window as visible/invisible
0097      * This can be used to release resources used by the window
0098      * The default is true.
0099      */
0100     void setVisible(bool visible);
0101     bool isVisible() const;
0102 
0103     void show();
0104     void hide();
0105 
0106     bool automaticRepaint() const;
0107     void setAutomaticRepaint(bool set);
0108 
0109     /**
0110      * Returns the current output of the scene graph
0111      * @note The render context must valid at the time of calling
0112      */
0113     GLTexture *bufferAsTexture();
0114 
0115     /**
0116      * Returns the current output of the scene graph
0117      */
0118     QImage bufferAsImage() const;
0119 
0120     /**
0121      * Inject any mouse event into the QQuickWindow.
0122      * Local co-ordinates are transformed
0123      * If it is handled the event will be accepted
0124      */
0125     void forwardMouseEvent(QEvent *mouseEvent);
0126     /**
0127      * Inject a key event into the window.
0128      * If it is handled the event will be accepted
0129      */
0130     void forwardKeyEvent(QKeyEvent *keyEvent);
0131 
0132     bool forwardTouchDown(qint32 id, const QPointF &pos, std::chrono::microseconds time);
0133     bool forwardTouchMotion(qint32 id, const QPointF &pos, std::chrono::microseconds time);
0134     bool forwardTouchUp(qint32 id, std::chrono::microseconds time);
0135 
0136 Q_SIGNALS:
0137     /**
0138      * The frame buffer has changed, contents need re-rendering on screen
0139      */
0140     void repaintNeeded();
0141     void geometryChanged(const QRect &oldGeometry, const QRect &newGeometry);
0142     void renderRequested();
0143     void sceneChanged();
0144 
0145 private:
0146     void handleRenderRequested();
0147     void handleSceneChanged();
0148 
0149     class Private;
0150     std::unique_ptr<Private> d;
0151 };
0152 
0153 /**
0154  * The KWinQuickScene class extends KWinQuickView
0155  * adding QML support. This will represent a context
0156  * powered by an engine
0157  */
0158 class KWIN_EXPORT OffscreenQuickScene : public OffscreenQuickView
0159 {
0160 public:
0161     explicit OffscreenQuickScene(ExportMode exportMode = ExportMode::Texture, bool alpha = true);
0162     ~OffscreenQuickScene();
0163 
0164     /** top level item in the given source*/
0165     QQuickItem *rootItem() const;
0166 
0167     void setSource(const QUrl &source);
0168     void setSource(const QUrl &source, const QVariantMap &initialProperties);
0169 
0170 private:
0171     class Private;
0172     std::unique_ptr<Private> d;
0173 };
0174 
0175 }