blur.h 3.91 KB
Newer Older
1
/*
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
2
3
4
5
6
    SPDX-FileCopyrightText: 2010 Fredrik Höglund <fredrik@kde.org>
    SPDX-FileCopyrightText: 2018 Alex Nemeth <alex.nemeth329@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
7
8

#ifndef BLUR_H
9
#define BLUR_H
10
11

#include <kwineffects.h>
12
#include <kwinglplatform.h>
13
14
15
16
#include <kwinglutils.h>

#include <QVector>
#include <QVector2D>
17
#include <QStack>
18

19
namespace KWaylandServer
20
21
22
23
{
class BlurManagerInterface;
}

24
25
26
namespace KWin
{

27
28
static const int borderSize = 5;

29
30
31
32
class BlurShader;

class BlurEffect : public KWin::Effect
{
33
    Q_OBJECT
34

35
36
public:
    BlurEffect();
37
    ~BlurEffect() override;
38
39

    static bool supported();
40
    static bool enabledByDefault();
41

42
43
44
    void reconfigure(ReconfigureFlags flags) override;
    void prePaintScreen(ScreenPrePaintData &data, int time) override;
    void prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time) override;
Albert Astals Cid's avatar
Albert Astals Cid committed
45
46
    void drawWindow(EffectWindow *w, int mask, const QRegion &region, WindowPaintData &data) override;
    void paintEffectFrame(EffectFrame *frame, const QRegion &region, double opacity, double frameOpacity) override;
47

48
    bool provides(Feature feature) override;
49
    bool isActive() const override;
50

51
52
53
54
    int requestedEffectChainPosition() const override {
        return 75;
    }

55
56
    bool eventFilter(QObject *watched, QEvent *event) override;

57
public Q_SLOTS:
58
59
60
    void slotWindowAdded(KWin::EffectWindow *w);
    void slotWindowDeleted(KWin::EffectWindow *w);
    void slotPropertyNotify(KWin::EffectWindow *w, long atom);
61
    void slotScreenGeometryChanged();
62

63
64
65
private:
    QRect expand(const QRect &rect) const;
    QRegion expand(const QRegion &region) const;
66
67
68
69
    bool renderTargetsValid() const;
    void deleteFBOs();
    void initBlurStrengthValues();
    void updateTexture();
70
    QRegion blurRegion(const EffectWindow *w) const;
71
    bool shouldBlur(const EffectWindow *w, int mask, const WindowPaintData &data) const;
72
    void updateBlurRegion(EffectWindow *w) const;
Alex Nemeth's avatar
Alex Nemeth committed
73
    void doBlur(const QRegion &shape, const QRect &screen, const float opacity, const QMatrix4x4 &screenProjection, bool isDock, QRect windowRect);
74
75
    void uploadRegion(QVector2D *&map, const QRegion &region, const int downSampleIterations);
    void uploadGeometry(GLVertexBuffer *vbo, const QRegion &blurRegion, const QRegion &windowRegion);
Alex Nemeth's avatar
Alex Nemeth committed
76
    void generateNoiseTexture();
77

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
78
    void upscaleRenderToScreen(GLVertexBuffer *vbo, int vboStart, int blurRectCount, QMatrix4x4 screenProjection, QPoint windowPosition);
79
80
    void downSampleTexture(GLVertexBuffer *vbo, int blurRectCount);
    void upSampleTexture(GLVertexBuffer *vbo, int blurRectCount);
Alex Nemeth's avatar
Alex Nemeth committed
81
    void copyScreenSampleTexture(GLVertexBuffer *vbo, int blurRectCount, QRegion blurShape, QMatrix4x4 screenProjection);
82
83

private:
84
85
86
87
    BlurShader *m_shader;
    QVector <GLRenderTarget*> m_renderTargets;
    QVector <GLTexture> m_renderTextures;
    QStack <GLRenderTarget*> m_renderTargetStack;
Alex Nemeth's avatar
Alex Nemeth committed
88
89
90

    GLTexture m_noiseTexture;

91
    bool m_renderTargetsValid;
92
    long net_wm_blur_region;
93
    QRegion m_paintedArea; // keeps track of all painted areas (from bottom to top)
94
95
96
97
98
    QRegion m_currentBlur; // keeps track of the currently blured area of the windows(from bottom to top)

    int m_downSampleIterations; // number of times the texture will be downsized to half size
    int m_offset;
    int m_expandSize;
Alex Nemeth's avatar
Alex Nemeth committed
99
100
    int m_noiseStrength;
    int m_scalingFactor;
101
102
103
104
105
106
107
108
109
110
111
112

    struct OffsetStruct {
        float minOffset;
        float maxOffset;
        int expandSize;
    };

    QVector <OffsetStruct> blurOffsets;

    struct BlurValuesStruct {
        int iteration;
        float offset;
113
114
    };

115
116
117
    QVector <BlurValuesStruct> blurStrengthValues;

    QMap <EffectWindow*, QMetaObject::Connection> windowBlurChangedConnections;
118
    KWaylandServer::BlurManagerInterface *m_blurManager = nullptr;
119
120
};

121
122
123
124
125
126
127
128
129
130
inline
bool BlurEffect::provides(Effect::Feature feature)
{
    if (feature == Blur) {
        return true;
    }
    return KWin::Effect::provides(feature);
}


131
132
133
134
} // namespace KWin

#endif