2013-12-01 23:28:57 +04:00
|
|
|
/*
|
2020-08-03 01:22:19 +03:00
|
|
|
SPDX-FileCopyrightText: 2010 Fredrik Höglund <fredrik@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2011 Philipp Knechtges <philipp-dev@knechtges.com>
|
|
|
|
SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2013-12-01 23:28:57 +04:00
|
|
|
|
|
|
|
#include "contrast.h"
|
|
|
|
#include "contrastshader.h"
|
|
|
|
// KConfigSkeleton
|
|
|
|
|
|
|
|
#include <QMatrix4x4>
|
2019-01-13 19:50:32 +03:00
|
|
|
#include <QWindow>
|
2013-12-01 23:28:57 +04:00
|
|
|
|
2020-04-29 18:18:41 +03:00
|
|
|
#include <KWaylandServer/surface_interface.h>
|
|
|
|
#include <KWaylandServer/contrast_interface.h>
|
|
|
|
#include <KWaylandServer/display.h>
|
2015-09-03 18:00:24 +03:00
|
|
|
|
2013-12-01 23:28:57 +04:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2014-03-16 13:16:52 +04:00
|
|
|
static const QByteArray s_contrastAtomName = QByteArrayLiteral("_KDE_NET_WM_BACKGROUND_CONTRAST_REGION");
|
|
|
|
|
2013-12-01 23:28:57 +04:00
|
|
|
ContrastEffect::ContrastEffect()
|
|
|
|
{
|
|
|
|
shader = ContrastShader::create();
|
|
|
|
|
|
|
|
reconfigure(ReconfigureAll);
|
|
|
|
|
|
|
|
// ### Hackish way to announce support.
|
|
|
|
// Should be included in _NET_SUPPORTED instead.
|
2013-12-03 23:20:45 +04:00
|
|
|
if (shader && shader->isValid()) {
|
2014-03-16 13:16:52 +04:00
|
|
|
net_wm_contrast_region = effects->announceSupportProperty(s_contrastAtomName, this);
|
2020-04-29 18:18:41 +03:00
|
|
|
KWaylandServer::Display *display = effects->waylandDisplay();
|
2015-09-29 11:21:24 +03:00
|
|
|
if (display) {
|
|
|
|
m_contrastManager = display->createContrastManager(this);
|
|
|
|
}
|
2013-12-01 23:28:57 +04:00
|
|
|
} else {
|
2013-12-03 23:20:45 +04:00
|
|
|
net_wm_contrast_region = 0;
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
|
2019-01-01 23:48:53 +03:00
|
|
|
connect(effects, &EffectsHandler::windowAdded, this, &ContrastEffect::slotWindowAdded);
|
|
|
|
connect(effects, &EffectsHandler::windowDeleted, this, &ContrastEffect::slotWindowDeleted);
|
|
|
|
connect(effects, &EffectsHandler::propertyNotify, this, &ContrastEffect::slotPropertyNotify);
|
|
|
|
connect(effects, &EffectsHandler::screenGeometryChanged, this, &ContrastEffect::slotScreenGeometryChanged);
|
2017-09-10 17:51:18 +03:00
|
|
|
connect(effects, &EffectsHandler::xcbConnectionChanged, this,
|
|
|
|
[this] {
|
|
|
|
if (shader && shader->isValid()) {
|
|
|
|
net_wm_contrast_region = effects->announceSupportProperty(s_contrastAtomName, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2013-12-01 23:28:57 +04:00
|
|
|
|
2013-12-03 23:20:45 +04:00
|
|
|
// Fetch the contrast regions for all windows
|
2017-09-29 15:30:42 +03:00
|
|
|
for (EffectWindow *window: effects->stackingOrder()) {
|
2013-12-01 23:28:57 +04:00
|
|
|
updateContrastRegion(window);
|
2017-09-29 15:30:42 +03:00
|
|
|
}
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ContrastEffect::~ContrastEffect()
|
|
|
|
{
|
|
|
|
delete shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::slotScreenGeometryChanged()
|
|
|
|
{
|
2017-09-29 15:30:42 +03:00
|
|
|
effects->makeOpenGLContextCurrent();
|
|
|
|
if (!supported()) {
|
|
|
|
effects->reloadEffect(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (EffectWindow *window: effects->stackingOrder()) {
|
|
|
|
updateContrastRegion(window);
|
|
|
|
}
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::reconfigure(ReconfigureFlags flags)
|
|
|
|
{
|
|
|
|
Q_UNUSED(flags)
|
|
|
|
|
2014-01-07 00:43:03 +04:00
|
|
|
if (shader)
|
|
|
|
shader->init();
|
|
|
|
|
2015-09-29 11:21:24 +03:00
|
|
|
if (!shader || !shader->isValid()) {
|
2014-03-16 13:16:52 +04:00
|
|
|
effects->removeSupportProperty(s_contrastAtomName, this);
|
2015-09-29 11:21:24 +03:00
|
|
|
delete m_contrastManager;
|
|
|
|
m_contrastManager = nullptr;
|
|
|
|
}
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
|
2017-03-15 14:02:22 +03:00
|
|
|
void ContrastEffect::updateContrastRegion(EffectWindow *w)
|
2013-12-01 23:28:57 +04:00
|
|
|
{
|
|
|
|
QRegion region;
|
2014-01-05 21:28:31 +04:00
|
|
|
float colorTransform[16];
|
2017-09-10 17:51:18 +03:00
|
|
|
QByteArray value;
|
|
|
|
|
|
|
|
if (net_wm_contrast_region != XCB_ATOM_NONE) {
|
|
|
|
value = w->readProperty(net_wm_contrast_region, net_wm_contrast_region, 32);
|
|
|
|
|
|
|
|
if (value.size() > 0 && !((value.size() - (16 * sizeof(uint32_t))) % ((4 * sizeof(uint32_t))))) {
|
|
|
|
const uint32_t *cardinals = reinterpret_cast<const uint32_t*>(value.constData());
|
|
|
|
const float *floatCardinals = reinterpret_cast<const float*>(value.constData());
|
|
|
|
unsigned int i = 0;
|
|
|
|
for (; i < ((value.size() - (16 * sizeof(uint32_t)))) / sizeof(uint32_t);) {
|
|
|
|
int x = cardinals[i++];
|
|
|
|
int y = cardinals[i++];
|
|
|
|
int w = cardinals[i++];
|
|
|
|
int h = cardinals[i++];
|
|
|
|
region += QRect(x, y, w, h);
|
|
|
|
}
|
2013-12-01 23:28:57 +04:00
|
|
|
|
2017-09-10 17:51:18 +03:00
|
|
|
for (unsigned int j = 0; j < 16; ++j) {
|
|
|
|
colorTransform[j] = floatCardinals[i + j];
|
|
|
|
}
|
2014-01-07 00:43:03 +04:00
|
|
|
|
2017-09-10 17:51:18 +03:00
|
|
|
QMatrix4x4 colorMatrix(colorTransform);
|
|
|
|
m_colorMatrices[w] = colorMatrix;
|
2014-01-23 00:46:56 +04:00
|
|
|
}
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:18:41 +03:00
|
|
|
KWaylandServer::SurfaceInterface *surf = w->surface();
|
2015-09-03 18:00:24 +03:00
|
|
|
|
|
|
|
if (surf && surf->contrast()) {
|
|
|
|
region = surf->contrast()->region();
|
2017-03-15 14:02:22 +03:00
|
|
|
m_colorMatrices[w] = colorMatrix(surf->contrast()->contrast(), surf->contrast()->intensity(), surf->contrast()->saturation());
|
2015-09-03 18:00:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 19:50:32 +03:00
|
|
|
if (auto internal = w->internalWindow()) {
|
|
|
|
const auto property = internal->property("kwin_background_region");
|
|
|
|
if (property.isValid()) {
|
|
|
|
region = property.value<QRegion>();
|
|
|
|
bool ok = false;
|
|
|
|
qreal contrast = internal->property("kwin_background_contrast").toReal(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
contrast = 1.0;
|
|
|
|
}
|
|
|
|
qreal intensity = internal->property("kwin_background_intensity").toReal(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
intensity = 1.0;
|
|
|
|
}
|
|
|
|
qreal saturation = internal->property("kwin_background_saturation").toReal(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
saturation = 1.0;
|
|
|
|
}
|
|
|
|
m_colorMatrices[w] = colorMatrix(contrast, intensity, saturation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:00:24 +03:00
|
|
|
//!value.isNull() full window in X11 case, surf->contrast()
|
|
|
|
//valid, full window in wayland case
|
|
|
|
if (region.isEmpty() && (!value.isNull() || (surf && surf->contrast()))) {
|
2013-12-01 23:28:57 +04:00
|
|
|
// Set the data to a dummy value.
|
|
|
|
// This is needed to be able to distinguish between the value not
|
|
|
|
// being set, and being set to an empty region.
|
2013-12-03 23:20:45 +04:00
|
|
|
w->setData(WindowBackgroundContrastRole, 1);
|
2013-12-01 23:28:57 +04:00
|
|
|
} else
|
2013-12-03 23:20:45 +04:00
|
|
|
w->setData(WindowBackgroundContrastRole, region);
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::slotWindowAdded(EffectWindow *w)
|
|
|
|
{
|
2020-04-29 18:18:41 +03:00
|
|
|
KWaylandServer::SurfaceInterface *surf = w->surface();
|
2015-09-03 18:00:24 +03:00
|
|
|
|
|
|
|
if (surf) {
|
2020-04-29 18:18:41 +03:00
|
|
|
m_contrastChangedConnections[w] = connect(surf, &KWaylandServer::SurfaceInterface::contrastChanged, this, [this, w] () {
|
2015-09-03 18:00:24 +03:00
|
|
|
|
|
|
|
if (w) {
|
|
|
|
updateContrastRegion(w);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-01-13 19:50:32 +03:00
|
|
|
|
|
|
|
if (auto internal = w->internalWindow()) {
|
|
|
|
internal->installEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2013-12-01 23:28:57 +04:00
|
|
|
updateContrastRegion(w);
|
|
|
|
}
|
|
|
|
|
2019-01-13 19:50:32 +03:00
|
|
|
bool ContrastEffect::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
auto internal = qobject_cast<QWindow*>(watched);
|
|
|
|
if (internal && event->type() == QEvent::DynamicPropertyChange) {
|
|
|
|
QDynamicPropertyChangeEvent *pe = static_cast<QDynamicPropertyChangeEvent*>(event);
|
|
|
|
if (pe->propertyName() == "kwin_background_region" ||
|
|
|
|
pe->propertyName() == "kwin_background_contrast" ||
|
|
|
|
pe->propertyName() == "kwin_background_intensity" ||
|
|
|
|
pe->propertyName() == "kwin_background_saturation") {
|
|
|
|
if (auto w = effects->findWindow(internal)) {
|
|
|
|
updateContrastRegion(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-03 18:00:24 +03:00
|
|
|
void ContrastEffect::slotWindowDeleted(EffectWindow *w)
|
|
|
|
{
|
|
|
|
if (m_contrastChangedConnections.contains(w)) {
|
|
|
|
disconnect(m_contrastChangedConnections[w]);
|
|
|
|
m_contrastChangedConnections.remove(w);
|
2017-03-15 14:02:22 +03:00
|
|
|
m_colorMatrices.remove(w);
|
2015-09-03 18:00:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 00:43:03 +04:00
|
|
|
void ContrastEffect::slotPropertyNotify(EffectWindow *w, long atom)
|
|
|
|
{
|
2017-09-10 17:51:18 +03:00
|
|
|
if (w && atom == net_wm_contrast_region && net_wm_contrast_region != XCB_ATOM_NONE) {
|
2014-01-07 00:43:03 +04:00
|
|
|
updateContrastRegion(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMatrix4x4 ContrastEffect::colorMatrix(qreal contrast, qreal intensity, qreal saturation)
|
|
|
|
{
|
|
|
|
QMatrix4x4 satMatrix; //saturation
|
|
|
|
QMatrix4x4 intMatrix; //intensity
|
|
|
|
QMatrix4x4 contMatrix; //contrast
|
|
|
|
|
|
|
|
//Saturation matrix
|
|
|
|
if (!qFuzzyCompare(saturation, 1.0)) {
|
|
|
|
const qreal rval = (1.0 - saturation) * .2126;
|
|
|
|
const qreal gval = (1.0 - saturation) * .7152;
|
|
|
|
const qreal bval = (1.0 - saturation) * .0722;
|
|
|
|
|
|
|
|
satMatrix = QMatrix4x4(rval + saturation, rval, rval, 0.0,
|
|
|
|
gval, gval + saturation, gval, 0.0,
|
|
|
|
bval, bval, bval + saturation, 0.0,
|
|
|
|
0, 0, 0, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//IntensityMatrix
|
|
|
|
if (!qFuzzyCompare(intensity, 1.0)) {
|
|
|
|
intMatrix.scale(intensity, intensity, intensity);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Contrast Matrix
|
|
|
|
if (!qFuzzyCompare(contrast, 1.0)) {
|
|
|
|
const float transl = (1.0 - contrast) / 2.0;
|
|
|
|
|
|
|
|
contMatrix = QMatrix4x4(contrast, 0, 0, 0.0,
|
|
|
|
0, contrast, 0, 0.0,
|
|
|
|
0, 0, contrast, 0.0,
|
|
|
|
transl, transl, transl, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QMatrix4x4 colorMatrix = contMatrix * satMatrix * intMatrix;
|
|
|
|
//colorMatrix = colorMatrix.transposed();
|
|
|
|
|
|
|
|
return colorMatrix;
|
|
|
|
}
|
|
|
|
|
2013-12-01 23:28:57 +04:00
|
|
|
bool ContrastEffect::enabledByDefault()
|
|
|
|
{
|
|
|
|
GLPlatform *gl = GLPlatform::instance();
|
|
|
|
|
|
|
|
if (gl->isIntel() && gl->chipClass() < SandyBridge)
|
|
|
|
return false;
|
2016-08-09 15:52:14 +03:00
|
|
|
if (gl->isSoftwareEmulation()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-01 23:28:57 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContrastEffect::supported()
|
|
|
|
{
|
2014-11-22 17:53:15 +03:00
|
|
|
bool supported = effects->isOpenGLCompositing() && GLRenderTarget::supported();
|
2013-12-01 23:28:57 +04:00
|
|
|
|
|
|
|
if (supported) {
|
|
|
|
int maxTexSize;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
|
|
|
|
|
2014-02-24 19:13:30 +04:00
|
|
|
const QSize screenSize = effects->virtualScreenSize();
|
|
|
|
if (screenSize.width() > maxTexSize || screenSize.height() > maxTexSize)
|
2013-12-01 23:28:57 +04:00
|
|
|
supported = false;
|
|
|
|
}
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion ContrastEffect::contrastRegion(const EffectWindow *w) const
|
|
|
|
{
|
|
|
|
QRegion region;
|
|
|
|
|
2013-12-03 23:20:45 +04:00
|
|
|
const QVariant value = w->data(WindowBackgroundContrastRole);
|
2013-12-01 23:28:57 +04:00
|
|
|
if (value.isValid()) {
|
|
|
|
const QRegion appRegion = qvariant_cast<QRegion>(value);
|
|
|
|
if (!appRegion.isEmpty()) {
|
|
|
|
region |= appRegion.translated(w->contentsRect().topLeft()) &
|
|
|
|
w->decorationInnerRect();
|
|
|
|
} else {
|
|
|
|
// An empty region means that the blur effect should be enabled
|
|
|
|
// for the whole window.
|
2014-01-23 00:46:56 +04:00
|
|
|
region = w->decorationInnerRect();
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::uploadRegion(QVector2D *&map, const QRegion ®ion)
|
|
|
|
{
|
2017-12-02 19:38:14 +03:00
|
|
|
for (const QRect &r : region) {
|
2013-12-01 23:28:57 +04:00
|
|
|
const QVector2D topLeft(r.x(), r.y());
|
|
|
|
const QVector2D topRight(r.x() + r.width(), r.y());
|
|
|
|
const QVector2D bottomLeft(r.x(), r.y() + r.height());
|
|
|
|
const QVector2D bottomRight(r.x() + r.width(), r.y() + r.height());
|
|
|
|
|
|
|
|
// First triangle
|
|
|
|
*(map++) = topRight;
|
|
|
|
*(map++) = topLeft;
|
|
|
|
*(map++) = bottomLeft;
|
|
|
|
|
|
|
|
// Second triangle
|
|
|
|
*(map++) = bottomLeft;
|
|
|
|
*(map++) = bottomRight;
|
|
|
|
*(map++) = topRight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 23:20:45 +04:00
|
|
|
void ContrastEffect::uploadGeometry(GLVertexBuffer *vbo, const QRegion ®ion)
|
2013-12-01 23:28:57 +04:00
|
|
|
{
|
2013-12-03 23:20:45 +04:00
|
|
|
const int vertexCount = region.rectCount() * 6;
|
2015-05-19 15:35:12 +03:00
|
|
|
if (!vertexCount)
|
|
|
|
return;
|
2013-12-01 23:28:57 +04:00
|
|
|
|
|
|
|
QVector2D *map = (QVector2D *) vbo->map(vertexCount * sizeof(QVector2D));
|
2013-12-03 23:20:45 +04:00
|
|
|
uploadRegion(map, region);
|
2013-12-01 23:28:57 +04:00
|
|
|
vbo->unmap();
|
|
|
|
|
|
|
|
const GLVertexAttrib layout[] = {
|
|
|
|
{ VA_Position, 2, GL_FLOAT, 0 },
|
|
|
|
{ VA_TexCoord, 2, GL_FLOAT, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
vbo->setAttribLayout(layout, 2, sizeof(QVector2D));
|
|
|
|
}
|
|
|
|
|
2014-01-07 00:43:03 +04:00
|
|
|
void ContrastEffect::prePaintScreen(ScreenPrePaintData &data, int time)
|
|
|
|
{
|
|
|
|
m_paintedArea = QRegion();
|
|
|
|
m_currentContrast = QRegion();
|
|
|
|
|
|
|
|
effects->prePaintScreen(data, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time)
|
|
|
|
{
|
|
|
|
// this effect relies on prePaintWindow being called in the bottom to top order
|
|
|
|
|
|
|
|
effects->prePaintWindow(w, data, time);
|
|
|
|
|
|
|
|
if (!w->isPaintingEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!shader || !shader->isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QRegion oldPaint = data.paint;
|
|
|
|
|
|
|
|
// we don't have to blur a region we don't see
|
|
|
|
m_currentContrast -= data.clip;
|
|
|
|
// if we have to paint a non-opaque part of this window that intersects with the
|
|
|
|
// currently blurred region (which is not cached) we have to redraw the whole region
|
|
|
|
if ((data.paint-data.clip).intersects(m_currentContrast)) {
|
|
|
|
data.paint |= m_currentContrast;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case this window has regions to be blurred
|
2014-02-24 19:13:30 +04:00
|
|
|
const QRect screen = effects->virtualScreenGeometry();
|
2014-01-07 00:43:03 +04:00
|
|
|
const QRegion contrastArea = contrastRegion(w).translated(w->pos()) & screen;
|
|
|
|
|
|
|
|
// we are not caching the window
|
|
|
|
|
|
|
|
// if this window or an window underneath the modified area is painted again we have to
|
|
|
|
// do everything
|
|
|
|
if (m_paintedArea.intersects(contrastArea) || data.paint.intersects(contrastArea)) {
|
|
|
|
data.paint |= contrastArea;
|
|
|
|
|
|
|
|
// we have to check again whether we do not damage a blurred area
|
|
|
|
// of a window we do not cache
|
|
|
|
if (contrastArea.intersects(m_currentContrast)) {
|
|
|
|
data.paint |= m_currentContrast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_currentContrast |= contrastArea;
|
|
|
|
|
|
|
|
|
|
|
|
// m_paintedArea keep track of all repainted areas
|
|
|
|
m_paintedArea -= data.clip;
|
|
|
|
m_paintedArea |= data.paint;
|
|
|
|
}
|
|
|
|
|
2013-12-01 23:28:57 +04:00
|
|
|
bool ContrastEffect::shouldContrast(const EffectWindow *w, int mask, const WindowPaintData &data) const
|
|
|
|
{
|
2013-12-03 23:20:45 +04:00
|
|
|
if (!shader || !shader->isValid())
|
2013-12-01 23:28:57 +04:00
|
|
|
return false;
|
|
|
|
|
2013-12-03 23:20:45 +04:00
|
|
|
if (effects->activeFullScreenEffect() && !w->data(WindowForceBackgroundContrastRole).toBool())
|
2013-12-01 23:28:57 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (w->isDesktop())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool scaled = !qFuzzyCompare(data.xScale(), 1.0) && !qFuzzyCompare(data.yScale(), 1.0);
|
|
|
|
bool translated = data.xTranslation() || data.yTranslation();
|
|
|
|
|
2016-02-25 14:45:19 +03:00
|
|
|
if ((scaled || (translated || (mask & PAINT_WINDOW_TRANSFORMED))) && !w->data(WindowForceBackgroundContrastRole).toBool())
|
2013-12-01 23:28:57 +04:00
|
|
|
return false;
|
|
|
|
|
2013-12-03 23:20:45 +04:00
|
|
|
if (!w->hasAlpha())
|
2013-12-01 23:28:57 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:04:15 +03:00
|
|
|
void ContrastEffect::drawWindow(EffectWindow *w, int mask, const QRegion ®ion, WindowPaintData &data)
|
2013-12-01 23:28:57 +04:00
|
|
|
{
|
2016-10-18 17:33:20 +03:00
|
|
|
const QRect screen = GLRenderTarget::virtualScreenGeometry();
|
2013-12-01 23:28:57 +04:00
|
|
|
if (shouldContrast(w, mask, data)) {
|
|
|
|
QRegion shape = region & contrastRegion(w).translated(w->pos()) & screen;
|
|
|
|
|
|
|
|
// let's do the evil parts - someone wants to blur behind a transformed window
|
2016-02-09 14:09:01 +03:00
|
|
|
const bool translated = data.xTranslation() || data.yTranslation();
|
|
|
|
const bool scaled = data.xScale() != 1 || data.yScale() != 1;
|
|
|
|
if (scaled) {
|
|
|
|
QPoint pt = shape.boundingRect().topLeft();
|
2019-07-09 23:08:47 +03:00
|
|
|
QRegion scaledShape;
|
|
|
|
for (QRect r : shape) {
|
2016-02-09 14:09:01 +03:00
|
|
|
r.moveTo(pt.x() + (r.x() - pt.x()) * data.xScale() + data.xTranslation(),
|
|
|
|
pt.y() + (r.y() - pt.y()) * data.yScale() + data.yTranslation());
|
|
|
|
r.setWidth(r.width() * data.xScale());
|
|
|
|
r.setHeight(r.height() * data.yScale());
|
2019-07-09 23:08:47 +03:00
|
|
|
scaledShape |= r;
|
2016-02-09 14:09:01 +03:00
|
|
|
}
|
2019-07-09 23:08:47 +03:00
|
|
|
shape = scaledShape & region;
|
2016-02-09 14:09:01 +03:00
|
|
|
|
|
|
|
//Only translated, not scaled
|
|
|
|
} else if (translated) {
|
2013-12-01 23:28:57 +04:00
|
|
|
shape = shape.translated(data.xTranslation(), data.yTranslation());
|
|
|
|
shape = shape & region;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shape.isEmpty()) {
|
2017-03-15 14:02:22 +03:00
|
|
|
doContrast(w, shape, screen, data.opacity(), data.screenProjectionMatrix());
|
2013-12-01 23:28:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 21:28:31 +04:00
|
|
|
// Draw the window over the contrast area
|
2013-12-01 23:28:57 +04:00
|
|
|
effects->drawWindow(w, mask, region, data);
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:04:15 +03:00
|
|
|
void ContrastEffect::paintEffectFrame(EffectFrame *frame, const QRegion ®ion, double opacity, double frameOpacity)
|
2013-12-01 23:28:57 +04:00
|
|
|
{
|
2014-01-23 13:54:40 +04:00
|
|
|
//FIXME: this is a no-op for now, it should figure out the right contrast, intensity, saturation
|
2013-12-01 23:28:57 +04:00
|
|
|
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
|
|
|
|
}
|
|
|
|
|
2017-03-15 14:02:22 +03:00
|
|
|
void ContrastEffect::doContrast(EffectWindow *w, const QRegion& shape, const QRect& screen, const float opacity, const QMatrix4x4 &screenProjection)
|
2013-12-01 23:28:57 +04:00
|
|
|
{
|
|
|
|
const QRegion actualShape = shape & screen;
|
|
|
|
const QRect r = actualShape.boundingRect();
|
|
|
|
|
2017-03-06 03:39:12 +03:00
|
|
|
qreal scale = GLRenderTarget::virtualScreenScale();
|
|
|
|
|
2013-12-01 23:28:57 +04:00
|
|
|
// Upload geometry for the horizontal and vertical passes
|
|
|
|
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
|
2019-02-15 14:31:51 +03:00
|
|
|
vbo->reset();
|
2013-12-03 23:20:45 +04:00
|
|
|
uploadGeometry(vbo, actualShape);
|
2013-12-01 23:28:57 +04:00
|
|
|
vbo->bindArrays();
|
|
|
|
|
|
|
|
// Create a scratch texture and copy the area in the back buffer that we're
|
|
|
|
// going to blur into it
|
2017-03-06 03:39:12 +03:00
|
|
|
GLTexture scratch(GL_RGBA8, r.width() * scale, r.height() * scale);
|
2013-12-01 23:28:57 +04:00
|
|
|
scratch.setFilter(GL_LINEAR);
|
|
|
|
scratch.setWrapMode(GL_CLAMP_TO_EDGE);
|
|
|
|
scratch.bind();
|
|
|
|
|
2016-10-18 17:33:20 +03:00
|
|
|
const QRect sg = GLRenderTarget::virtualScreenGeometry();
|
2018-05-17 12:51:50 +03:00
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, (r.x() - sg.x()) * scale, (sg.height() - (r.y() - sg.y() + r.height())) * scale,
|
2017-03-06 03:39:12 +03:00
|
|
|
scratch.width(), scratch.height());
|
2013-12-01 23:28:57 +04:00
|
|
|
|
|
|
|
// Draw the texture on the offscreen framebuffer object, while blurring it horizontally
|
|
|
|
|
2017-03-15 14:02:22 +03:00
|
|
|
shader->setColorMatrix(m_colorMatrices.value(w));
|
2013-12-01 23:28:57 +04:00
|
|
|
shader->bind();
|
|
|
|
|
2014-01-23 17:33:19 +04:00
|
|
|
|
|
|
|
shader->setOpacity(opacity);
|
2013-12-01 23:28:57 +04:00
|
|
|
// Set up the texture matrix to transform from screen coordinates
|
|
|
|
// to texture coordinates.
|
|
|
|
QMatrix4x4 textureMatrix;
|
2017-03-06 03:39:12 +03:00
|
|
|
textureMatrix.scale(1.0 / r.width(), -1.0 / r.height(), 1);
|
|
|
|
textureMatrix.translate(-r.x(), -r.height() - r.y(), 0);
|
2013-12-01 23:28:57 +04:00
|
|
|
shader->setTextureMatrix(textureMatrix);
|
2016-10-18 17:33:20 +03:00
|
|
|
shader->setModelViewProjectionMatrix(screenProjection);
|
2013-12-01 23:28:57 +04:00
|
|
|
|
|
|
|
vbo->draw(GL_TRIANGLES, 0, actualShape.rectCount() * 6);
|
|
|
|
|
|
|
|
scratch.unbind();
|
|
|
|
scratch.discard();
|
|
|
|
|
|
|
|
vbo->unbindArrays();
|
|
|
|
|
|
|
|
if (opacity < 1.0) {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
shader->unbind();
|
|
|
|
}
|
|
|
|
|
2020-08-31 21:11:06 +03:00
|
|
|
bool ContrastEffect::isActive() const
|
|
|
|
{
|
|
|
|
return !effects->isScreenLocked();
|
|
|
|
}
|
|
|
|
|
2013-12-01 23:28:57 +04:00
|
|
|
} // namespace KWin
|
|
|
|
|