2013-01-31 12:31:49 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2013 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
|
|
|
|
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. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#include "screenedgeeffect.h"
|
|
|
|
// KWin
|
|
|
|
#include <kwinglutils.h>
|
|
|
|
#include <kwingltexture.h>
|
2013-02-13 09:25:33 +00:00
|
|
|
#include <kwinxrenderutils.h>
|
2013-01-31 12:31:49 +00:00
|
|
|
// KDE
|
|
|
|
#include <Plasma/Svg>
|
|
|
|
// Qt
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QPainter>
|
2013-02-26 07:02:27 +00:00
|
|
|
#include <QVector4D>
|
2013-02-13 09:25:33 +00:00
|
|
|
// xcb
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
#include <xcb/render.h>
|
|
|
|
#endif
|
2013-01-31 12:31:49 +00:00
|
|
|
|
|
|
|
namespace KWin {
|
|
|
|
|
|
|
|
ScreenEdgeEffect::ScreenEdgeEffect()
|
|
|
|
: Effect()
|
|
|
|
, m_glow(new Plasma::Svg(this))
|
|
|
|
, m_cleanupTimer(new QTimer(this))
|
|
|
|
{
|
2013-07-23 05:02:52 +00:00
|
|
|
m_glow->setImagePath(QStringLiteral("widgets/glowbar"));
|
2013-01-31 12:31:49 +00:00
|
|
|
connect(effects, SIGNAL(screenEdgeApproaching(ElectricBorder,qreal,QRect)), SLOT(edgeApproaching(ElectricBorder,qreal,QRect)));
|
|
|
|
m_cleanupTimer->setInterval(5000);
|
|
|
|
m_cleanupTimer->setSingleShot(true);
|
|
|
|
connect(m_cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenEdgeEffect::~ScreenEdgeEffect()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenEdgeEffect::cleanup()
|
|
|
|
{
|
|
|
|
for (QHash<ElectricBorder, Glow*>::iterator it = m_borders.begin();
|
|
|
|
it != m_borders.end();
|
|
|
|
++it) {
|
|
|
|
effects->addRepaint((*it)->geometry);
|
|
|
|
}
|
|
|
|
qDeleteAll(m_borders);
|
|
|
|
m_borders.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenEdgeEffect::prePaintScreen(ScreenPrePaintData &data, int time)
|
|
|
|
{
|
|
|
|
effects->prePaintScreen(data, time);
|
|
|
|
for (QHash<ElectricBorder, Glow*>::iterator it = m_borders.begin();
|
|
|
|
it != m_borders.end();
|
|
|
|
++it) {
|
|
|
|
if ((*it)->strength == 0.0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
data.paint += (*it)->geometry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenEdgeEffect::paintScreen(int mask, QRegion region, ScreenPaintData &data)
|
|
|
|
{
|
|
|
|
effects->paintScreen(mask, region, data);
|
|
|
|
for (QHash<ElectricBorder, Glow*>::iterator it = m_borders.begin();
|
|
|
|
it != m_borders.end();
|
|
|
|
++it) {
|
|
|
|
const qreal opacity = (*it)->strength;
|
|
|
|
if (opacity == 0.0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (effects->isOpenGLCompositing()) {
|
|
|
|
GLTexture *texture = (*it)->texture.data();
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
texture->bind();
|
2014-02-25 10:02:32 +00:00
|
|
|
ShaderBinder binder(ShaderManager::SimpleShader);
|
|
|
|
const QVector4D constant(opacity, opacity, opacity, opacity);
|
|
|
|
binder.shader()->setUniform(GLShader::ModulationConstant, constant);
|
|
|
|
texture->render(infiniteRegion(), (*it)->geometry);
|
2013-01-31 12:31:49 +00:00
|
|
|
texture->unbind();
|
|
|
|
glDisable(GL_BLEND);
|
2013-02-13 09:25:33 +00:00
|
|
|
} else if (effects->compositingType() == XRenderCompositing) {
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
const QRect &rect = (*it)->geometry;
|
|
|
|
const QSize &size = (*it)->pictureSize;
|
|
|
|
int x = rect.x();
|
|
|
|
int y = rect.y();
|
|
|
|
int width = rect.width();
|
|
|
|
int height = rect.height();
|
|
|
|
switch ((*it)->border) {
|
|
|
|
case ElectricTopRight:
|
|
|
|
x = rect.x() + rect.width() - size.width();
|
|
|
|
break;
|
|
|
|
case ElectricBottomRight:
|
|
|
|
x = rect.x() + rect.width() - size.width();
|
|
|
|
y = rect.y() + rect.height() - size.height();
|
|
|
|
break;
|
|
|
|
case ElectricBottomLeft:
|
|
|
|
y = rect.y() + rect.height() - size.height();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xcb_render_composite(connection(), XCB_RENDER_PICT_OP_OVER, *(*it)->picture.data(),
|
|
|
|
xRenderBlendPicture(opacity), effects->xrenderBufferPicture(),
|
|
|
|
0, 0, 0, 0, x, y, width, height);
|
|
|
|
#endif
|
2013-06-21 13:34:40 +00:00
|
|
|
} else if (effects->compositingType() == QPainterCompositing) {
|
|
|
|
QImage tmp((*it)->image->size(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
tmp.fill(Qt::transparent);
|
|
|
|
QPainter p(&tmp);
|
|
|
|
p.drawImage(0, 0, *(*it)->image.data());
|
|
|
|
QColor color(Qt::transparent);
|
|
|
|
color.setAlphaF(opacity);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
p.fillRect(QRect(QPoint(0, 0), tmp.size()), color);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
QPainter *painter = effects->scenePainter();
|
|
|
|
const QRect &rect = (*it)->geometry;
|
|
|
|
const QSize &size = (*it)->pictureSize;
|
|
|
|
int x = rect.x();
|
|
|
|
int y = rect.y();
|
|
|
|
switch ((*it)->border) {
|
|
|
|
case ElectricTopRight:
|
|
|
|
x = rect.x() + rect.width() - size.width();
|
|
|
|
break;
|
|
|
|
case ElectricBottomRight:
|
|
|
|
x = rect.x() + rect.width() - size.width();
|
|
|
|
y = rect.y() + rect.height() - size.height();
|
|
|
|
break;
|
|
|
|
case ElectricBottomLeft:
|
|
|
|
y = rect.y() + rect.height() - size.height();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
painter->drawImage(QPoint(x, y), tmp);
|
2013-01-31 12:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenEdgeEffect::edgeApproaching(ElectricBorder border, qreal factor, const QRect &geometry)
|
|
|
|
{
|
|
|
|
QHash<ElectricBorder, Glow*>::iterator it = m_borders.find(border);
|
|
|
|
if (it != m_borders.end()) {
|
|
|
|
// need to update
|
|
|
|
effects->addRepaint((*it)->geometry);
|
|
|
|
(*it)->strength = factor;
|
|
|
|
if ((*it)->geometry != geometry) {
|
|
|
|
(*it)->geometry = geometry;
|
|
|
|
effects->addRepaint((*it)->geometry);
|
|
|
|
if (border == ElectricLeft || border == ElectricRight || border == ElectricTop || border == ElectricBottom) {
|
2013-02-13 09:25:33 +00:00
|
|
|
if (effects->isOpenGLCompositing()) {
|
|
|
|
(*it)->texture.reset(createEdgeGlow<GLTexture>(border, geometry.size()));
|
|
|
|
} else if (effects->compositingType() == XRenderCompositing) {
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
(*it)->picture.reset(createEdgeGlow<XRenderPicture>(border, geometry.size()));
|
|
|
|
#endif
|
2013-06-21 13:34:40 +00:00
|
|
|
} else if (effects->compositingType() == QPainterCompositing) {
|
|
|
|
(*it)->image.reset(createEdgeGlow<QImage>(border, geometry.size()));
|
2013-02-13 09:25:33 +00:00
|
|
|
}
|
2013-01-31 12:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (factor == 0.0) {
|
|
|
|
m_cleanupTimer->start();
|
|
|
|
} else {
|
|
|
|
m_cleanupTimer->stop();
|
|
|
|
}
|
|
|
|
} else if (factor != 0.0) {
|
|
|
|
// need to generate new Glow
|
|
|
|
Glow *glow = createGlow(border, factor, geometry);
|
|
|
|
if (glow) {
|
|
|
|
m_borders.insert(border, glow);
|
|
|
|
effects->addRepaint(glow->geometry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Glow *ScreenEdgeEffect::createGlow(ElectricBorder border, qreal factor, const QRect &geometry)
|
|
|
|
{
|
|
|
|
Glow *glow = new Glow();
|
2013-02-13 09:25:33 +00:00
|
|
|
glow->border = border;
|
2013-01-31 12:31:49 +00:00
|
|
|
glow->strength = factor;
|
|
|
|
glow->geometry = geometry;
|
|
|
|
|
|
|
|
// render the glow image
|
2013-02-13 09:25:33 +00:00
|
|
|
if (effects->isOpenGLCompositing()) {
|
Better handling for making the compositing OpenGL context current
With QtQuick2 it's possible that the scene graph rendering context either
lives in an own thread or uses the main GUI thread. In the latter case
it's the same thread as our compositing OpenGL context lives in. This
means our basic assumption that between two rendering passes the context
stays current does not hold.
The code already ensured that before we start a rendering pass the
context is made current, but there are many more possible cases. If we
use OpenGL in areas not triggered by the rendering loop but in response
to other events the context needs to be made current. This includes the
loading and unloading of effects (some effects use OpenGL in the static
effect check, in the ctor and dtor), background loading of texture data,
lazy loading after first usage invoked by shortcut, etc. etc.
To properly handle these cases new methods are added to EffectsHandler
to make the compositing OpenGL context current. These calls delegate down
into the scene. On non-OpenGL scenes they are noop, but on OpenGL they go
into the backend and make the context current. In addition they ensure
that Qt doesn't think that it's QOpenGLContext is current by calling
doneCurrent() on the QOpenGLContext::currentContext(). This unfortunately
causes an additional call to makeCurrent with a null context, but there
is no other way to tell Qt - it doesn't notice when a different context
is made current with low level API calls. In the multi-threaded
architecture this doesn't matter as ::currentContext() returns null.
A short evaluation showed that a transition to QOpenGLContext doesn't
seem feasible. Qt only supports either GLX or EGL while KWin supports
both and when entering the transition phase for Wayland, it would become
extremely tricky if our native platform is X11, but we want a Wayland
EGL context. A future solution might be to have a "KWin-QPA plugin" which
uses either xcb or Wayland and hides everything from Qt.
The API documentation is extended to describe when the effects-framework
ensures that an OpenGL context is current. The effects are changed to
make the context current in cases where it's not guaranteed. This has
been done by looking for creation or deletion of GLTextures and Shaders.
If there are other OpenGL usages outside the rendering loop, ctor/dtor
this needs to be changed, too.
2013-11-22 14:05:36 +00:00
|
|
|
effects->makeOpenGLContextCurrent();
|
2013-02-13 09:25:33 +00:00
|
|
|
if (border == ElectricTopLeft || border == ElectricTopRight || border == ElectricBottomRight || border == ElectricBottomLeft) {
|
|
|
|
glow->texture.reset(createCornerGlow<GLTexture>(border));
|
|
|
|
} else {
|
|
|
|
glow->texture.reset(createEdgeGlow<GLTexture>(border, geometry.size()));
|
|
|
|
}
|
|
|
|
if (!glow->texture.isNull()) {
|
|
|
|
glow->texture->setWrapMode(GL_CLAMP_TO_EDGE);
|
|
|
|
}
|
2013-02-21 17:21:46 +00:00
|
|
|
if (glow->texture.isNull()) {
|
|
|
|
delete glow;
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-02-13 09:25:33 +00:00
|
|
|
} else if (effects->compositingType() == XRenderCompositing) {
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
if (border == ElectricTopLeft || border == ElectricTopRight || border == ElectricBottomRight || border == ElectricBottomLeft) {
|
|
|
|
glow->pictureSize = cornerGlowSize(border);
|
|
|
|
glow->picture.reset(createCornerGlow<XRenderPicture>(border));
|
|
|
|
} else {
|
|
|
|
glow->pictureSize = geometry.size();
|
|
|
|
glow->picture.reset(createEdgeGlow<XRenderPicture>(border, geometry.size()));
|
|
|
|
}
|
2013-02-21 17:21:46 +00:00
|
|
|
if (glow->picture.isNull()) {
|
|
|
|
delete glow;
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-02-13 09:25:33 +00:00
|
|
|
#endif
|
2013-06-21 13:34:40 +00:00
|
|
|
} else if (effects->compositingType() == QPainterCompositing) {
|
|
|
|
if (border == ElectricTopLeft || border == ElectricTopRight || border == ElectricBottomRight || border == ElectricBottomLeft) {
|
|
|
|
glow->image.reset(createCornerGlow<QImage>(border));
|
|
|
|
glow->pictureSize = cornerGlowSize(border);
|
|
|
|
} else {
|
|
|
|
glow->image.reset(createEdgeGlow<QImage>(border, geometry.size()));
|
|
|
|
glow->pictureSize = geometry.size();
|
|
|
|
}
|
|
|
|
if (glow->image.isNull()) {
|
|
|
|
delete glow;
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-31 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return glow;
|
|
|
|
}
|
|
|
|
|
2013-02-13 09:25:33 +00:00
|
|
|
template <typename T>
|
|
|
|
T *ScreenEdgeEffect::createCornerGlow(ElectricBorder border)
|
2013-01-31 12:31:49 +00:00
|
|
|
{
|
|
|
|
switch (border) {
|
|
|
|
case ElectricTopLeft:
|
2013-08-13 05:19:09 +00:00
|
|
|
return new T(m_glow->pixmap(QStringLiteral("bottomright")).toImage());
|
2013-01-31 12:31:49 +00:00
|
|
|
case ElectricTopRight:
|
2013-08-13 05:19:09 +00:00
|
|
|
return new T(m_glow->pixmap(QStringLiteral("bottomleft")).toImage());
|
2013-01-31 12:31:49 +00:00
|
|
|
case ElectricBottomRight:
|
2013-08-13 05:19:09 +00:00
|
|
|
return new T(m_glow->pixmap(QStringLiteral("topleft")).toImage());
|
2013-01-31 12:31:49 +00:00
|
|
|
case ElectricBottomLeft:
|
2013-08-13 05:19:09 +00:00
|
|
|
return new T(m_glow->pixmap(QStringLiteral("topright")).toImage());
|
2013-01-31 12:31:49 +00:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 09:25:33 +00:00
|
|
|
QSize ScreenEdgeEffect::cornerGlowSize(ElectricBorder border) const
|
|
|
|
{
|
|
|
|
switch (border) {
|
|
|
|
case ElectricTopLeft:
|
2013-07-23 05:02:52 +00:00
|
|
|
return m_glow->elementSize(QStringLiteral("bottomright"));
|
2013-02-13 09:25:33 +00:00
|
|
|
case ElectricTopRight:
|
2013-07-23 05:02:52 +00:00
|
|
|
return m_glow->elementSize(QStringLiteral("bottomleft"));
|
2013-02-13 09:25:33 +00:00
|
|
|
case ElectricBottomRight:
|
2013-07-23 05:02:52 +00:00
|
|
|
return m_glow->elementSize(QStringLiteral("topleft"));
|
2013-02-13 09:25:33 +00:00
|
|
|
case ElectricBottomLeft:
|
2013-07-23 05:02:52 +00:00
|
|
|
return m_glow->elementSize(QStringLiteral("topright"));
|
2013-02-13 09:25:33 +00:00
|
|
|
default:
|
|
|
|
return QSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T *ScreenEdgeEffect::createEdgeGlow(ElectricBorder border, const QSize &size)
|
2013-01-31 12:31:49 +00:00
|
|
|
{
|
|
|
|
QPoint pixmapPosition(0, 0);
|
|
|
|
QPixmap l, r, c;
|
|
|
|
switch (border) {
|
|
|
|
case ElectricTop:
|
2013-07-23 05:02:52 +00:00
|
|
|
l = m_glow->pixmap(QStringLiteral("bottomleft"));
|
|
|
|
r = m_glow->pixmap(QStringLiteral("bottomright"));
|
|
|
|
c = m_glow->pixmap(QStringLiteral("bottom"));
|
2013-01-31 12:31:49 +00:00
|
|
|
break;
|
|
|
|
case ElectricBottom:
|
2013-07-23 05:02:52 +00:00
|
|
|
l = m_glow->pixmap(QStringLiteral("topleft"));
|
|
|
|
r = m_glow->pixmap(QStringLiteral("topright"));
|
|
|
|
c = m_glow->pixmap(QStringLiteral("top"));
|
2013-01-31 12:31:49 +00:00
|
|
|
pixmapPosition = QPoint(0, size.height() - c.height());
|
|
|
|
break;
|
|
|
|
case ElectricLeft:
|
2013-07-23 05:02:52 +00:00
|
|
|
l = m_glow->pixmap(QStringLiteral("topright"));
|
|
|
|
r = m_glow->pixmap(QStringLiteral("bottomright"));
|
|
|
|
c = m_glow->pixmap(QStringLiteral("right"));
|
2013-01-31 12:31:49 +00:00
|
|
|
break;
|
|
|
|
case ElectricRight:
|
2013-07-23 05:02:52 +00:00
|
|
|
l = m_glow->pixmap(QStringLiteral("topleft"));
|
|
|
|
r = m_glow->pixmap(QStringLiteral("bottomleft"));
|
|
|
|
c = m_glow->pixmap(QStringLiteral("left"));
|
2013-01-31 12:31:49 +00:00
|
|
|
pixmapPosition = QPoint(size.width() - c.width(), 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-02-13 09:25:33 +00:00
|
|
|
QPixmap image(size);
|
2013-01-31 12:31:49 +00:00
|
|
|
image.fill(Qt::transparent);
|
|
|
|
QPainter p;
|
|
|
|
p.begin(&image);
|
|
|
|
if (border == ElectricBottom || border == ElectricTop) {
|
|
|
|
p.drawPixmap(pixmapPosition, l);
|
|
|
|
p.drawTiledPixmap(QRect(l.width(), pixmapPosition.y(), size.width() - l.width() - r.width(), c.height()), c);
|
|
|
|
p.drawPixmap(QPoint(size.width() - r.width(), pixmapPosition.y()), r);
|
|
|
|
} else {
|
|
|
|
p.drawPixmap(pixmapPosition, l);
|
|
|
|
p.drawTiledPixmap(QRect(pixmapPosition.x(), l.height(), c.width(), size.height() - l.height() - r.height()), c);
|
|
|
|
p.drawPixmap(QPoint(pixmapPosition.x(), size.height() - r.height()), r);
|
|
|
|
}
|
|
|
|
p.end();
|
2013-08-13 05:19:09 +00:00
|
|
|
return new T(image.toImage());
|
2013-01-31 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScreenEdgeEffect::isActive() const
|
|
|
|
{
|
|
|
|
return !m_borders.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|