blur.h 4.67 KB
Newer Older
1 2
/*
 *   Copyright © 2010 Fredrik Höglund <fredrik@kde.org>
3
 *   Copyright © 2018 Alex Nemeth <alex.nemeth329@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; see the file COPYING.  if not, write to
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *   Boston, MA 02110-1301, USA.
 */

#ifndef BLUR_H
22
#define BLUR_H
23 24

#include <kwineffects.h>
25
#include <kwinglplatform.h>
26 27 28 29
#include <kwinglutils.h>

#include <QVector>
#include <QVector2D>
30
#include <QStack>
31

32 33 34 35 36 37 38 39
namespace KWayland
{
namespace Server
{
class BlurManagerInterface;
}
}

40 41 42
namespace KWin
{

43 44
static const int borderSize = 5;

45 46 47 48
class BlurShader;

class BlurEffect : public KWin::Effect
{
49
    Q_OBJECT
50

51 52
public:
    BlurEffect();
53
    ~BlurEffect() override;
54 55

    static bool supported();
56
    static bool enabledByDefault();
57

58 59 60 61 62
    void reconfigure(ReconfigureFlags flags) override;
    void prePaintScreen(ScreenPrePaintData &data, int time) override;
    void prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time) override;
    void drawWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data) override;
    void paintEffectFrame(EffectFrame *frame, QRegion region, double opacity, double frameOpacity) override;
63

64
    bool provides(Feature feature) override;
65

66 67 68 69
    int requestedEffectChainPosition() const override {
        return 75;
    }

70 71
    bool eventFilter(QObject *watched, QEvent *event) override;

72
public Q_SLOTS:
73 74 75
    void slotWindowAdded(KWin::EffectWindow *w);
    void slotWindowDeleted(KWin::EffectWindow *w);
    void slotPropertyNotify(KWin::EffectWindow *w, long atom);
76
    void slotScreenGeometryChanged();
77

78 79 80
private:
    QRect expand(const QRect &rect) const;
    QRegion expand(const QRegion &region) const;
81 82 83 84
    bool renderTargetsValid() const;
    void deleteFBOs();
    void initBlurStrengthValues();
    void updateTexture();
85
    QRegion blurRegion(const EffectWindow *w) const;
86
    bool shouldBlur(const EffectWindow *w, int mask, const WindowPaintData &data) const;
87
    void updateBlurRegion(EffectWindow *w) const;
Alex Nemeth's avatar
Alex Nemeth committed
88
    void doBlur(const QRegion &shape, const QRect &screen, const float opacity, const QMatrix4x4 &screenProjection, bool isDock, QRect windowRect);
89 90
    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
91
    void generateNoiseTexture();
92

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
93
    void upscaleRenderToScreen(GLVertexBuffer *vbo, int vboStart, int blurRectCount, QMatrix4x4 screenProjection, QPoint windowPosition);
94 95
    void downSampleTexture(GLVertexBuffer *vbo, int blurRectCount);
    void upSampleTexture(GLVertexBuffer *vbo, int blurRectCount);
Alex Nemeth's avatar
Alex Nemeth committed
96
    void copyScreenSampleTexture(GLVertexBuffer *vbo, int blurRectCount, QRegion blurShape, QMatrix4x4 screenProjection);
97 98

private:
99 100 101 102
    BlurShader *m_shader;
    QVector <GLRenderTarget*> m_renderTargets;
    QVector <GLTexture> m_renderTextures;
    QStack <GLRenderTarget*> m_renderTargetStack;
Alex Nemeth's avatar
Alex Nemeth committed
103 104 105

    GLTexture m_noiseTexture;

106
    bool m_renderTargetsValid;
107
    long net_wm_blur_region;
108
    QRegion m_damagedArea; // keeps track of the area which has been damaged (from bottom to top)
109
    QRegion m_paintedArea; // actually painted area which is greater than m_damagedArea
110 111 112 113 114
    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
115 116
    int m_noiseStrength;
    int m_scalingFactor;
117 118 119 120 121 122 123 124 125 126 127 128

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

    QVector <OffsetStruct> blurOffsets;

    struct BlurValuesStruct {
        int iteration;
        float offset;
129 130
    };

131 132 133
    QVector <BlurValuesStruct> blurStrengthValues;

    QMap <EffectWindow*, QMetaObject::Connection> windowBlurChangedConnections;
134
    KWayland::Server::BlurManagerInterface *m_blurManager = nullptr;
135 136
};

137 138 139 140 141 142 143 144 145 146
inline
bool BlurEffect::provides(Effect::Feature feature)
{
    if (feature == Blur) {
        return true;
    }
    return KWin::Effect::provides(feature);
}


147 148 149 150
} // namespace KWin

#endif