File indexing completed on 2024-05-19 16:34:35

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