2013-12-01 19:28:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Fredrik Höglund <fredrik@kde.org>
|
|
|
|
* Copyright © 2011 Philipp Knechtges <philipp-dev@knechtges.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contrast.h"
|
|
|
|
#include "contrastshader.h"
|
|
|
|
// KConfigSkeleton
|
|
|
|
#include "contrastconfig.h"
|
|
|
|
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
|
|
|
|
#include <QMatrix4x4>
|
|
|
|
#include <QLinkedList>
|
|
|
|
#include <KDebug>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
KWIN_EFFECT(contrast, ContrastEffect)
|
|
|
|
KWIN_EFFECT_SUPPORTED(contrast, ContrastEffect::supported())
|
|
|
|
KWIN_EFFECT_ENABLEDBYDEFAULT(contrast, ContrastEffect::enabledByDefault())
|
|
|
|
|
|
|
|
ContrastEffect::ContrastEffect()
|
|
|
|
{
|
|
|
|
shader = ContrastShader::create();
|
|
|
|
|
|
|
|
reconfigure(ReconfigureAll);
|
|
|
|
|
|
|
|
// ### Hackish way to announce support.
|
|
|
|
// Should be included in _NET_SUPPORTED instead.
|
2013-12-03 19:20:45 +00:00
|
|
|
if (shader && shader->isValid()) {
|
|
|
|
net_wm_contrast_region = effects->announceSupportProperty("_KDE_NET_WM_BACKGROUND_CONTRAST_REGION", this);
|
2013-12-01 19:28:57 +00:00
|
|
|
} else {
|
2013-12-03 19:20:45 +00:00
|
|
|
net_wm_contrast_region = 0;
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(effects, SIGNAL(windowAdded(KWin::EffectWindow*)), this, SLOT(slotWindowAdded(KWin::EffectWindow*)));
|
|
|
|
connect(effects, SIGNAL(windowDeleted(KWin::EffectWindow*)), this, SLOT(slotWindowDeleted(KWin::EffectWindow*)));
|
|
|
|
connect(effects, SIGNAL(propertyNotify(KWin::EffectWindow*,long)), this, SLOT(slotPropertyNotify(KWin::EffectWindow*,long)));
|
|
|
|
connect(effects, SIGNAL(screenGeometryChanged(QSize)), this, SLOT(slotScreenGeometryChanged()));
|
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
// Fetch the contrast regions for all windows
|
2013-12-01 19:28:57 +00:00
|
|
|
foreach (EffectWindow *window, effects->stackingOrder())
|
|
|
|
updateContrastRegion(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
ContrastEffect::~ContrastEffect()
|
|
|
|
{
|
|
|
|
delete shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::slotScreenGeometryChanged()
|
|
|
|
{
|
|
|
|
effects->reloadEffect(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::reconfigure(ReconfigureFlags flags)
|
|
|
|
{
|
|
|
|
Q_UNUSED(flags)
|
|
|
|
|
|
|
|
ContrastConfig::self()->readConfig();
|
|
|
|
int strength = 4;
|
|
|
|
if (shader)
|
|
|
|
shader->setStrength(strength);
|
|
|
|
|
|
|
|
if (!shader || !shader->isValid())
|
2013-12-03 19:20:45 +00:00
|
|
|
XDeleteProperty(display(), rootWindow(), net_wm_contrast_region);
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::updateContrastRegion(EffectWindow *w) const
|
|
|
|
{
|
|
|
|
QRegion region;
|
2013-12-03 19:20:45 +00:00
|
|
|
QVector<float> colorTransform = QVector<float>(16);
|
2013-12-01 19:28:57 +00:00
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
const QByteArray value = w->readProperty(net_wm_contrast_region, XA_CARDINAL, 32);
|
|
|
|
if (value.size() > 0 && !((value.size() - (16 * sizeof(float))) % ((4 * sizeof(long))))) {
|
|
|
|
const long *cardinals = reinterpret_cast<const long*>(value.constData());
|
|
|
|
unsigned int i = 0;
|
|
|
|
for (; i < ((value.size() - (16 * sizeof(long)))) / sizeof(long);) {
|
2013-12-01 19:28:57 +00:00
|
|
|
int x = cardinals[i++];
|
|
|
|
int y = cardinals[i++];
|
|
|
|
int w = cardinals[i++];
|
|
|
|
int h = cardinals[i++];
|
|
|
|
region += QRect(x, y, w, h);
|
|
|
|
}
|
2013-12-03 19:20:45 +00:00
|
|
|
for (unsigned int j = 0; j < 16; ++j) {
|
|
|
|
colorTransform[j] = (float)cardinals[i + j] / 100;
|
|
|
|
}
|
|
|
|
QMatrix4x4 colorMatrix(colorTransform.constData());
|
|
|
|
shader->setColorMatrix(colorMatrix);
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (region.isEmpty() && !value.isNull()) {
|
|
|
|
// 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 19:20:45 +00:00
|
|
|
w->setData(WindowBackgroundContrastRole, 1);
|
2013-12-01 19:28:57 +00:00
|
|
|
} else
|
2013-12-03 19:20:45 +00:00
|
|
|
w->setData(WindowBackgroundContrastRole, region);
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::slotWindowAdded(EffectWindow *w)
|
|
|
|
{
|
|
|
|
updateContrastRegion(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContrastEffect::enabledByDefault()
|
|
|
|
{
|
|
|
|
GLPlatform *gl = GLPlatform::instance();
|
|
|
|
|
|
|
|
if (gl->isIntel() && gl->chipClass() < SandyBridge)
|
|
|
|
return false;
|
|
|
|
if (gl->driver() == Driver_Catalyst && effects->compositingType() == OpenGL1Compositing) {
|
|
|
|
// fglrx supports only ARB shaders and those tend to crash KWin (see Bug #270818 and #286795)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContrastEffect::supported()
|
|
|
|
{
|
|
|
|
bool supported = GLRenderTarget::supported() && GLTexture::NPOTTextureSupported() && GLSLContrastShader::supported();
|
|
|
|
|
|
|
|
if (supported) {
|
|
|
|
int maxTexSize;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
|
|
|
|
|
|
|
|
if (displayWidth() > maxTexSize || displayHeight() > maxTexSize)
|
|
|
|
supported = false;
|
|
|
|
}
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion ContrastEffect::contrastRegion(const EffectWindow *w) const
|
|
|
|
{
|
|
|
|
QRegion region;
|
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
const QVariant value = w->data(WindowBackgroundContrastRole);
|
2013-12-01 19:28:57 +00:00
|
|
|
if (value.isValid()) {
|
|
|
|
const QRegion appRegion = qvariant_cast<QRegion>(value);
|
|
|
|
if (!appRegion.isEmpty()) {
|
|
|
|
if (w->decorationHasAlpha() && effects->decorationSupportsBlurBehind()) {
|
|
|
|
region = w->shape();
|
|
|
|
region -= w->decorationInnerRect();
|
|
|
|
}
|
|
|
|
region |= appRegion.translated(w->contentsRect().topLeft()) &
|
|
|
|
w->decorationInnerRect();
|
|
|
|
} else {
|
|
|
|
// An empty region means that the blur effect should be enabled
|
|
|
|
// for the whole window.
|
|
|
|
region = w->shape();
|
|
|
|
}
|
|
|
|
} else if (w->decorationHasAlpha() && effects->decorationSupportsBlurBehind()) {
|
|
|
|
// If the client hasn't specified a blur region, we'll only enable
|
|
|
|
// the effect behind the decoration.
|
|
|
|
region = w->shape();
|
|
|
|
region -= w->decorationInnerRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::uploadRegion(QVector2D *&map, const QRegion ®ion)
|
|
|
|
{
|
|
|
|
foreach (const QRect &r, region.rects()) {
|
|
|
|
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 19:20:45 +00:00
|
|
|
void ContrastEffect::uploadGeometry(GLVertexBuffer *vbo, const QRegion ®ion)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
2013-12-03 19:20:45 +00:00
|
|
|
const int vertexCount = region.rectCount() * 6;
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
QVector2D *map = (QVector2D *) vbo->map(vertexCount * sizeof(QVector2D));
|
2013-12-03 19:20:45 +00:00
|
|
|
uploadRegion(map, region);
|
2013-12-01 19:28:57 +00:00
|
|
|
vbo->unmap();
|
|
|
|
|
|
|
|
const GLVertexAttrib layout[] = {
|
|
|
|
{ VA_Position, 2, GL_FLOAT, 0 },
|
|
|
|
{ VA_TexCoord, 2, GL_FLOAT, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
vbo->setAttribLayout(layout, 2, sizeof(QVector2D));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::prePaintScreen(ScreenPrePaintData &data, int time)
|
|
|
|
{
|
|
|
|
m_damagedArea = QRegion();
|
|
|
|
m_paintedArea = QRegion();
|
2013-12-03 19:20:45 +00:00
|
|
|
m_currentContrast = QRegion();
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
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
|
2013-12-03 19:20:45 +00:00
|
|
|
m_currentContrast -= data.clip;
|
2013-12-01 19:28:57 +00:00
|
|
|
// 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
|
2013-12-03 19:20:45 +00:00
|
|
|
if ((data.paint-data.clip).intersects(m_currentContrast)) {
|
|
|
|
data.paint |= m_currentContrast;
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// in case this window has regions to be blurred
|
|
|
|
const QRect screen(0, 0, displayWidth(), displayHeight());
|
2013-12-03 19:20:45 +00: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 keep track of the "damage propagation"
|
|
|
|
m_damagedArea |= 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;
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
m_currentContrast |= contrastArea;
|
|
|
|
|
|
|
|
|
2013-12-01 19:28:57 +00:00
|
|
|
// we don't consider damaged areas which are occluded and are not
|
|
|
|
// explicitly damaged by this window
|
|
|
|
m_damagedArea -= data.clip;
|
|
|
|
m_damagedArea |= oldPaint;
|
|
|
|
|
|
|
|
// in contrast to m_damagedArea does m_paintedArea keep track of all repainted areas
|
|
|
|
m_paintedArea -= data.clip;
|
|
|
|
m_paintedArea |= data.paint;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContrastEffect::shouldContrast(const EffectWindow *w, int mask, const WindowPaintData &data) const
|
|
|
|
{
|
2013-12-03 19:20:45 +00:00
|
|
|
if (!shader || !shader->isValid())
|
2013-12-01 19:28:57 +00:00
|
|
|
return false;
|
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
if (effects->activeFullScreenEffect() && !w->data(WindowForceBackgroundContrastRole).toBool())
|
2013-12-01 19:28:57 +00: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();
|
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
if (scaled || ((translated || (mask & PAINT_WINDOW_TRANSFORMED)) && !w->data(WindowForceBackgroundContrastRole).toBool()))
|
2013-12-01 19:28:57 +00:00
|
|
|
return false;
|
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
if (!w->hasAlpha())
|
2013-12-01 19:28:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::drawWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data)
|
|
|
|
{
|
|
|
|
const QRect screen(0, 0, displayWidth(), displayHeight());
|
|
|
|
if (shouldContrast(w, mask, data)) {
|
|
|
|
QRegion shape = region & contrastRegion(w).translated(w->pos()) & screen;
|
|
|
|
|
|
|
|
const bool translated = data.xTranslation() || data.yTranslation();
|
|
|
|
// let's do the evil parts - someone wants to blur behind a transformed window
|
|
|
|
if (translated) {
|
|
|
|
shape = shape.translated(data.xTranslation(), data.yTranslation());
|
|
|
|
shape = shape & region;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shape.isEmpty()) {
|
2013-12-03 19:20:45 +00:00
|
|
|
doContrast(shape, screen, data.opacity());
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the window over the blurred area
|
|
|
|
effects->drawWindow(w, mask, region, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::paintEffectFrame(EffectFrame *frame, QRegion region, double opacity, double frameOpacity)
|
|
|
|
{
|
|
|
|
const QRect screen(0, 0, displayWidth(), displayHeight());
|
2013-12-03 19:20:45 +00:00
|
|
|
bool valid = shader && shader->isValid();
|
2013-12-01 19:28:57 +00:00
|
|
|
QRegion shape = frame->geometry().adjusted(-5, -5, 5, 5) & screen;
|
|
|
|
if (valid && !shape.isEmpty() && region.intersects(shape.boundingRect()) && frame->style() != EffectFrameNone) {
|
|
|
|
doContrast(shape, screen, opacity * frameOpacity);
|
|
|
|
}
|
|
|
|
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::doContrast(const QRegion& shape, const QRect& screen, const float opacity)
|
|
|
|
{
|
|
|
|
const QRegion actualShape = shape & screen;
|
|
|
|
const QRect r = actualShape.boundingRect();
|
|
|
|
|
|
|
|
// Upload geometry for the horizontal and vertical passes
|
|
|
|
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
|
2013-12-03 19:20:45 +00:00
|
|
|
uploadGeometry(vbo, actualShape);
|
2013-12-01 19:28:57 +00:00
|
|
|
vbo->bindArrays();
|
|
|
|
|
|
|
|
// Create a scratch texture and copy the area in the back buffer that we're
|
|
|
|
// going to blur into it
|
|
|
|
GLTexture scratch(r.width(), r.height());
|
|
|
|
scratch.setFilter(GL_LINEAR);
|
|
|
|
scratch.setWrapMode(GL_CLAMP_TO_EDGE);
|
|
|
|
scratch.bind();
|
|
|
|
|
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r.x(), displayHeight() - r.y() - r.height(),
|
|
|
|
r.width(), r.height());
|
|
|
|
|
|
|
|
// Draw the texture on the offscreen framebuffer object, while blurring it horizontally
|
|
|
|
|
|
|
|
shader->bind();
|
|
|
|
|
|
|
|
// Set up the texture matrix to transform from screen coordinates
|
|
|
|
// to texture coordinates.
|
|
|
|
#ifdef KWIN_HAVE_OPENGL_1
|
|
|
|
if (effects->compositingType() == OpenGL1Compositing) {
|
|
|
|
glMatrixMode(GL_TEXTURE);
|
|
|
|
pushMatrix();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
QMatrix4x4 textureMatrix;
|
|
|
|
textureMatrix.scale(1.0 / scratch.width(), -1.0 / scratch.height(), 1);
|
|
|
|
textureMatrix.translate(-r.x(), -scratch.height() - r.y(), 0);
|
|
|
|
loadMatrix(textureMatrix);
|
|
|
|
shader->setTextureMatrix(textureMatrix);
|
|
|
|
|
|
|
|
vbo->draw(GL_TRIANGLES, 0, actualShape.rectCount() * 6);
|
|
|
|
|
|
|
|
scratch.unbind();
|
|
|
|
scratch.discard();
|
|
|
|
|
|
|
|
vbo->unbindArrays();
|
|
|
|
|
|
|
|
#ifdef KWIN_HAVE_OPENGL_1
|
|
|
|
if (effects->compositingType() == OpenGL1Compositing) {
|
|
|
|
popMatrix();
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (opacity < 1.0) {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
shader->unbind();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace KWin
|
|
|
|
|