kwin/effects/screenedge/screenedgeeffect.cpp

363 lines
13 KiB
C++
Raw Normal View History

2020-08-02 22:22:19 +00:00
/*
KWin - the KDE window manager
This file is part of the KDE project.
2020-08-02 22:22:19 +00:00
SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
2020-08-02 22:22:19 +00:00
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "screenedgeeffect.h"
// KWin
#include <kwinglutils.h>
#include <kwingltexture.h>
#include <kwinxrenderutils.h>
// KDE
#include <Plasma/Svg>
// Qt
#include <QTimer>
#include <QPainter>
#include <QVector4D>
// xcb
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
#include <xcb/render.h>
#endif
namespace KWin {
ScreenEdgeEffect::ScreenEdgeEffect()
: Effect()
, m_cleanupTimer(new QTimer(this))
{
connect(effects, &EffectsHandler::screenEdgeApproaching, this, &ScreenEdgeEffect::edgeApproaching);
m_cleanupTimer->setInterval(5000);
m_cleanupTimer->setSingleShot(true);
connect(m_cleanupTimer, &QTimer::timeout, this, &ScreenEdgeEffect::cleanup);
connect(effects, &EffectsHandler::screenLockingChanged, this,
[this] (bool locked) {
if (locked) {
cleanup();
}
}
);
}
ScreenEdgeEffect::~ScreenEdgeEffect()
{
cleanup();
}
void ScreenEdgeEffect::ensureGlowSvg()
{
if (!m_glow) {
m_glow = new Plasma::Svg(this);
m_glow->setImagePath(QStringLiteral("widgets/glowbar"));
}
}
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();
}
Provide expected presentation time to effects Effects are given the interval between two consecutive frames. The main flaw of this approach is that if the Compositor transitions from the idle state to "active" state, i.e. when there is something to repaint, effects may see a very large interval between the last painted frame and the current. In order to address this issue, the Scene invalidates the timer that is used to measure time between consecutive frames before the Compositor is about to become idle. While this works perfectly fine with Xinerama-style rendering, with per screen rendering, determining whether the compositor is about to idle is rather a tedious task mostly because a single output can't be used for the test. Furthermore, since the Compositor schedules pointless repaints just to ensure that it's idle, it might take several attempts to figure out whether the scene timer must be invalidated if you use (true) per screen rendering. Ideally, all effects should use a timeline helper that is aware of the underlying render loop and its timings. However, this option is off the table because it will involve a lot of work to implement it. Alternative and much simpler option is to pass the expected presentation time to effects rather than time between consecutive frames. This means that effects are responsible for determining how much animation timelines have to be advanced. Typically, an effect would have to store the presentation timestamp provided in either prePaint{Screen,Window} and use it in the subsequent prePaint{Screen,Window} call to estimate the amount of time passed between the next and the last frames. Unfortunately, this is an API incompatible change. However, it shouldn't take a lot of work to port third-party binary effects, which don't use the AnimationEffect class, to the new API. On the bright side, we no longer need to be concerned about the Compositor getting idle. We do still try to determine whether the Compositor is about to idle, primarily, because the OpenGL render backend swaps buffers on present, but that will change with the ongoing compositing timing rework.
2020-11-20 15:44:04 +00:00
void ScreenEdgeEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::milliseconds presentTime)
{
Provide expected presentation time to effects Effects are given the interval between two consecutive frames. The main flaw of this approach is that if the Compositor transitions from the idle state to "active" state, i.e. when there is something to repaint, effects may see a very large interval between the last painted frame and the current. In order to address this issue, the Scene invalidates the timer that is used to measure time between consecutive frames before the Compositor is about to become idle. While this works perfectly fine with Xinerama-style rendering, with per screen rendering, determining whether the compositor is about to idle is rather a tedious task mostly because a single output can't be used for the test. Furthermore, since the Compositor schedules pointless repaints just to ensure that it's idle, it might take several attempts to figure out whether the scene timer must be invalidated if you use (true) per screen rendering. Ideally, all effects should use a timeline helper that is aware of the underlying render loop and its timings. However, this option is off the table because it will involve a lot of work to implement it. Alternative and much simpler option is to pass the expected presentation time to effects rather than time between consecutive frames. This means that effects are responsible for determining how much animation timelines have to be advanced. Typically, an effect would have to store the presentation timestamp provided in either prePaint{Screen,Window} and use it in the subsequent prePaint{Screen,Window} call to estimate the amount of time passed between the next and the last frames. Unfortunately, this is an API incompatible change. However, it shouldn't take a lot of work to port third-party binary effects, which don't use the AnimationEffect class, to the new API. On the bright side, we no longer need to be concerned about the Compositor getting idle. We do still try to determine whether the Compositor is about to idle, primarily, because the OpenGL render backend swaps buffers on present, but that will change with the ongoing compositing timing rework.
2020-11-20 15:44:04 +00:00
effects->prePaintScreen(data, presentTime);
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, const 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_ONE, GL_ONE_MINUS_SRC_ALPHA);
texture->bind();
ShaderBinder binder(ShaderTrait::MapTexture | ShaderTrait::Modulate);
const QVector4D constant(opacity, opacity, opacity, opacity);
binder.shader()->setUniform(GLShader::ModulationConstant, constant);
QMatrix4x4 mvp = data.projectionMatrix();
mvp.translate((*it)->geometry.x(), (*it)->geometry.y());
binder.shader()->setUniform(GLShader::ModelViewProjectionMatrix, mvp);
texture->render(infiniteRegion(), (*it)->geometry);
texture->unbind();
glDisable(GL_BLEND);
} 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(xcbConnection(), XCB_RENDER_PICT_OP_OVER, *(*it)->picture.data(),
xRenderBlendPicture(opacity), effects->xrenderBufferPicture(),
0, 0, 0, 0, x, y, width, height);
#endif
} 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);
}
}
}
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) {
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
} else if (effects->compositingType() == QPainterCompositing) {
(*it)->image.reset(createEdgeGlow<QImage>(border, geometry.size()));
}
}
}
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();
glow->border = border;
glow->strength = factor;
glow->geometry = geometry;
// render the glow image
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();
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);
}
if (glow->texture.isNull()) {
delete glow;
return nullptr;
}
} 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()));
}
if (glow->picture.isNull()) {
delete glow;
return nullptr;
}
#endif
} 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 nullptr;
}
}
return glow;
}
template <typename T>
T *ScreenEdgeEffect::createCornerGlow(ElectricBorder border)
{
ensureGlowSvg();
switch (border) {
case ElectricTopLeft:
return new T(m_glow->pixmap(QStringLiteral("bottomright")).toImage());
case ElectricTopRight:
return new T(m_glow->pixmap(QStringLiteral("bottomleft")).toImage());
case ElectricBottomRight:
return new T(m_glow->pixmap(QStringLiteral("topleft")).toImage());
case ElectricBottomLeft:
return new T(m_glow->pixmap(QStringLiteral("topright")).toImage());
default:
return nullptr;
}
}
QSize ScreenEdgeEffect::cornerGlowSize(ElectricBorder border)
{
ensureGlowSvg();
switch (border) {
case ElectricTopLeft:
return m_glow->elementSize(QStringLiteral("bottomright"));
case ElectricTopRight:
return m_glow->elementSize(QStringLiteral("bottomleft"));
case ElectricBottomRight:
return m_glow->elementSize(QStringLiteral("topleft"));
case ElectricBottomLeft:
return m_glow->elementSize(QStringLiteral("topright"));
default:
return QSize();
}
}
template <typename T>
T *ScreenEdgeEffect::createEdgeGlow(ElectricBorder border, const QSize &size)
{
ensureGlowSvg();
const bool stretchBorder = m_glow->hasElement(QStringLiteral("hint-stretch-borders"));
QPoint pixmapPosition(0, 0);
QPixmap l, r, c;
switch (border) {
case ElectricTop:
l = m_glow->pixmap(QStringLiteral("bottomleft"));
r = m_glow->pixmap(QStringLiteral("bottomright"));
c = m_glow->pixmap(QStringLiteral("bottom"));
break;
case ElectricBottom:
l = m_glow->pixmap(QStringLiteral("topleft"));
r = m_glow->pixmap(QStringLiteral("topright"));
c = m_glow->pixmap(QStringLiteral("top"));
pixmapPosition = QPoint(0, size.height() - c.height());
break;
case ElectricLeft:
l = m_glow->pixmap(QStringLiteral("topright"));
r = m_glow->pixmap(QStringLiteral("bottomright"));
c = m_glow->pixmap(QStringLiteral("right"));
break;
case ElectricRight:
l = m_glow->pixmap(QStringLiteral("topleft"));
r = m_glow->pixmap(QStringLiteral("bottomleft"));
c = m_glow->pixmap(QStringLiteral("left"));
pixmapPosition = QPoint(size.width() - c.width(), 0);
break;
default:
return nullptr;
}
QPixmap image(size);
image.fill(Qt::transparent);
QPainter p;
p.begin(&image);
if (border == ElectricBottom || border == ElectricTop) {
p.drawPixmap(pixmapPosition, l);
const QRect cRect(l.width(), pixmapPosition.y(), size.width() - l.width() - r.width(), c.height());
if (stretchBorder) {
p.drawPixmap(cRect, c);
} else {
p.drawTiledPixmap(cRect, c);
}
p.drawPixmap(QPoint(size.width() - r.width(), pixmapPosition.y()), r);
} else {
p.drawPixmap(pixmapPosition, l);
const QRect cRect(pixmapPosition.x(), l.height(), c.width(), size.height() - l.height() - r.height());
if (stretchBorder) {
p.drawPixmap(cRect, c);
} else {
p.drawTiledPixmap(cRect, c);
}
p.drawPixmap(QPoint(pixmapPosition.x(), size.height() - r.height()), r);
}
p.end();
return new T(image.toImage());
}
bool ScreenEdgeEffect::isActive() const
{
return !m_borders.isEmpty() && !effects->isScreenLocked();
}
} // namespace