1 /*
2     SPDX-FileCopyrightText: 2011 Martin Gräßlin <mgraesslin@kde.org>
3     SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #pragma once
9 
10 #include <QQuickItem>
11 #include <QUuid>
12 
13 #include <epoxy/gl.h>
14 
15 namespace KWin
16 {
17 class AbstractClient;
18 class GLRenderTarget;
19 class GLTexture;
20 class ThumbnailTextureProvider;
21 
22 class ThumbnailItemBase : public QQuickItem
23 {
24     Q_OBJECT
25     Q_PROPERTY(QSize sourceSize READ sourceSize WRITE setSourceSize NOTIFY sourceSizeChanged)
26     /**
27      * TODO Plasma 6: Remove.
28      * @deprecated use a shader effect to change the brightness
29      */
30     Q_PROPERTY(qreal brightness READ brightness WRITE setBrightness NOTIFY brightnessChanged)
31     /**
32      * TODO Plasma 6: Remove.
33      * @deprecated use a shader effect to change color saturation
34      */
35     Q_PROPERTY(qreal saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
36     /**
37      * TODO Plasma 6: Remove.
38      * @deprecated clipTo has no replacement
39      */
40     Q_PROPERTY(QQuickItem *clipTo READ clipTo WRITE setClipTo NOTIFY clipToChanged)
41 
42 public:
43     explicit ThumbnailItemBase(QQuickItem *parent = nullptr);
44     ~ThumbnailItemBase() override;
45 
brightness()46     qreal brightness() const { return 1; }
47     void setBrightness(qreal brightness);
48 
saturation()49     qreal saturation() const { return 1; }
50     void setSaturation(qreal saturation);
51 
clipTo()52     QQuickItem *clipTo() const { return nullptr; }
53     void setClipTo(QQuickItem *clip);
54 
55     QSize sourceSize() const;
56     void setSourceSize(const QSize &sourceSize);
57 
58     QSGTextureProvider *textureProvider() const override;
59     bool isTextureProvider() const override;
60     QSGNode *updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *) override;
61 
62 Q_SIGNALS:
63     void brightnessChanged();
64     void saturationChanged();
65     void clipToChanged();
66     void sourceSizeChanged();
67 
68 protected:
69     void releaseResources() override;
70 
71     virtual QImage fallbackImage() const = 0;
72     virtual QRectF paintedRect() const = 0;
73     virtual void invalidateOffscreenTexture() = 0;
74     virtual void updateOffscreenTexture() = 0;
75     void destroyOffscreenTexture();
76 
77     mutable ThumbnailTextureProvider *m_provider = nullptr;
78     QSharedPointer<GLTexture> m_offscreenTexture;
79     QScopedPointer<GLRenderTarget> m_offscreenTarget;
80     GLsync m_acquireFence = 0;
81     qreal m_devicePixelRatio = 1;
82 
83 private:
84     void updateFrameRenderingConnection();
85     QMetaObject::Connection m_frameRenderingConnection;
86 
87     QSize m_sourceSize;
88 };
89 
90 class WindowThumbnailItem : public ThumbnailItemBase
91 {
92     Q_OBJECT
93     Q_PROPERTY(QUuid wId READ wId WRITE setWId NOTIFY wIdChanged)
94     Q_PROPERTY(KWin::AbstractClient *client READ client WRITE setClient NOTIFY clientChanged)
95 
96 public:
97     explicit WindowThumbnailItem(QQuickItem *parent = nullptr);
98 
99     QUuid wId() const;
100     void setWId(const QUuid &wId);
101 
102     AbstractClient *client() const;
103     void setClient(AbstractClient *client);
104 
105 Q_SIGNALS:
106     void wIdChanged();
107     void clientChanged();
108 
109 protected:
110     QImage fallbackImage() const override;
111     QRectF paintedRect() const override;
112     void invalidateOffscreenTexture() override;
113     void updateOffscreenTexture() override;
114 
115 private:
116     QUuid m_wId;
117     QPointer<AbstractClient> m_client;
118     bool m_dirty = false;
119 };
120 
121 class DesktopThumbnailItem : public ThumbnailItemBase
122 {
123     Q_OBJECT
124     Q_PROPERTY(int desktop READ desktop WRITE setDesktop NOTIFY desktopChanged)
125 
126 public:
127     explicit DesktopThumbnailItem(QQuickItem *parent = nullptr);
128 
129     int desktop() const;
130     void setDesktop(int desktop);
131 
132 Q_SIGNALS:
133     void desktopChanged();
134 
135 protected:
136     QImage fallbackImage() const override;
137     QRectF paintedRect() const override;
138     void invalidateOffscreenTexture() override;
139     void updateOffscreenTexture() override;
140 
141 private:
142     int m_desktop = 1;
143 };
144 
145 } // namespace KWin
146