Commit 6a7e780d authored by Vlad Zahorodnii's avatar Vlad Zahorodnii
Browse files

[effects] Rewrite the Glide effect

Summary:
There are several reasons why I "re-wrote" the Glide effect:
* it doesn't work correctly because it suffers from undesired perspective distortions: {F5914378}
   The worst part is that windows are distorted so much on multiple monitor setups that it's hard to say whether that's glide animation.
* window close animation is not quite intuitive: if the close button is
  located at the top and I click it, I would expect that window is
  rotated around the bottom edge, not the top; (IMHO)
* it's too much distracting when working on something for quite good
  amount of time: e.g. when editing photos, which involves a big number
  of different dialogs;
* there are issues with deletion of QTimeLine;
* windows are not gracefully released if some other effect grabs them;
* its code doesn't follow common coding style in KWin.

So, the "new" Glide effect is more subtle, it's possible to have
different rotation edges for window open/close animations, it doesn't
animate special windows(like audio volume feedback), the code is simpler
and readable. Yet, there are some issues with QTimeLine, which are
common to all effects in KWin anyway.

### Demos

{F5889803}
//Window Open Animation//

{F5889804}
//Window Close Animation//

{F5889805, layout=center, size=full}
//KCM//

CCBUG: 394245

Test Plan:
* Enabled the Glide effect
* Closed System Settings
* Opened it again

Reviewers: #kwin, #plasma, #vdg, davidedmundson

Reviewed By: #kwin, #plasma, #vdg, davidedmundson

Subscribers: ngraham, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D13338
parent df802d49
......@@ -5,6 +5,7 @@
Copyright (C) 2007 Philip Falkner <philip.falkner@gmail.com>
Copyright (C) 2009 Martin Gräßlin <mgraesslin@kde.org>
Copyright (C) 2010 Alexandre Pereira <pereira.alex@gmail.com>
Copyright (C) 2018 Vlad Zagorodniy <vladzzag@gmail.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
......@@ -20,246 +21,293 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
// own
#include "glide.h"
// KConfigSkeleton
#include "glideconfig.h"
// Qt
#include <QMatrix4x4>
#include <QSet>
#include <QString>
#include <QTimeLine>
// Effect is based on fade effect by Philip Falkner
namespace KWin
{
static const int IsGlideWindow = 0x22A982D4;
static const QSet<QString> s_blacklist {
"ksmserver ksmserver",
"ksplashx ksplashx",
"ksplashsimple ksplashsimple",
"ksplashqml ksplashqml"
QStringLiteral("ksmserver ksmserver"),
QStringLiteral("ksplashqml ksplashqml"),
QStringLiteral("ksplashsimple ksplashsimple"),
QStringLiteral("ksplashx ksplashx")
};
GlideEffect::GlideEffect()
: Effect()
{
initConfig<GlideConfig>();
reconfigure(ReconfigureAll);
connect(effects, SIGNAL(windowAdded(KWin::EffectWindow*)), this, SLOT(slotWindowAdded(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowClosed(KWin::EffectWindow*)), this, SLOT(slotWindowClosed(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowDeleted(KWin::EffectWindow*)), this, SLOT(slotWindowDeleted(KWin::EffectWindow*)));
connect(effects, &EffectsHandler::windowDataChanged, this, &GlideEffect::cancelWindowGrab);
connect(effects, &EffectsHandler::windowAdded, this, &GlideEffect::windowAdded);
connect(effects, &EffectsHandler::windowClosed, this, &GlideEffect::windowClosed);
connect(effects, &EffectsHandler::windowDeleted, this, &GlideEffect::windowDeleted);
connect(effects, &EffectsHandler::windowDataChanged, this, &GlideEffect::windowDataChanged);
}
GlideEffect::~GlideEffect() = default;
bool GlideEffect::supported()
void GlideEffect::reconfigure(ReconfigureFlags flags)
{
return effects->isOpenGLCompositing() && effects->animationsSupported();
}
Q_UNUSED(flags)
void GlideEffect::reconfigure(ReconfigureFlags)
{
// Fetch config with KConfigXT
GlideConfig::self()->read();
duration = animationTime<GlideConfig>(350);
effect = (EffectStyle) GlideConfig::glideEffect();
angle = GlideConfig::glideAngle();
m_duration = std::chrono::milliseconds(animationTime<GlideConfig>(160));
m_inParams.edge = static_cast<RotationEdge>(GlideConfig::inRotationEdge());
m_inParams.angle.from = GlideConfig::inRotationAngle();
m_inParams.angle.to = 0.0;
m_inParams.distance.from = GlideConfig::inDistance();
m_inParams.distance.to = 0.0;
m_inParams.opacity.from = GlideConfig::inOpacity();
m_inParams.opacity.to = 1.0;
m_outParams.edge = static_cast<RotationEdge>(GlideConfig::outRotationEdge());
m_outParams.angle.from = 0.0;
m_outParams.angle.to = GlideConfig::outRotationAngle();
m_outParams.distance.from = 0.0;
m_outParams.distance.to = GlideConfig::outDistance();
m_outParams.opacity.from = 1.0;
m_outParams.opacity.to = GlideConfig::outOpacity();
}
void GlideEffect::prePaintScreen(ScreenPrePaintData& data, int time)
void GlideEffect::prePaintScreen(ScreenPrePaintData &data, int time)
{
if (!windows.isEmpty())
data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
const std::chrono::milliseconds delta(time);
auto animationIt = m_animations.begin();
while (animationIt != m_animations.end()) {
(*animationIt).update(delta);
++animationIt;
}
data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
effects->prePaintScreen(data, time);
}
void GlideEffect::prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time)
void GlideEffect::prePaintWindow(EffectWindow *w, WindowPrePaintData &data, int time)
{
InfoHash::iterator info = windows.find(w);
if (info != windows.end()) {
if (m_animations.contains(w)) {
data.setTransformed();
if (info->added)
info->timeLine->setCurrentTime(info->timeLine->currentTime() + time);
else if (info->closed) {
info->timeLine->setCurrentTime(info->timeLine->currentTime() - time);
if (info->deleted)
w->enablePainting(EffectWindow::PAINT_DISABLED_BY_DELETE);
}
w->enablePainting(EffectWindow::PAINT_DISABLED_BY_DELETE);
}
effects->prePaintWindow(w, data, time);
// if the window isn't to be painted, then let's make sure
// to track its progress
if (info != windows.end() && !w->isPaintingEnabled() && !effects->activeFullScreenEffect())
w->addRepaintFull();
}
void GlideEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
void GlideEffect::paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data)
{
InfoHash::const_iterator info = windows.constFind(w);
if (info != windows.constEnd()) {
const double progress = info->timeLine->currentValue();
auto animationIt = m_animations.constFind(w);
if (animationIt == m_animations.constEnd()) {
effects->paintWindow(w, mask, region, data);
return;
}
// Perspective projection distorts objects near edges
// of the viewport. This is critical because distortions
// near edges of the viewport are not desired with this effect.
// To fix this, the center of the window will be moved to the origin,
// after applying perspective projection, the center is moved back
// to its "original" projected position. Overall, this is how the window
// will be transformed:
// [move to the origin] -> [rotate] -> [translate] ->
// -> [perspective projection] -> [reverse "move to the origin"]
const QMatrix4x4 oldProjMatrix = data.screenProjectionMatrix();
const QRectF windowGeo = w->geometry();
const QVector3D invOffset = oldProjMatrix.map(QVector3D(windowGeo.center()));
QMatrix4x4 invOffsetMatrix;
invOffsetMatrix.translate(invOffset.x(), invOffset.y());
data.setProjectionMatrix(invOffsetMatrix * oldProjMatrix);
// Move the center of the window to the origin.
const QRectF screenGeo = effects->virtualScreenGeometry();
const QPointF offset = screenGeo.center() - windowGeo.center();
data.translate(offset.x(), offset.y());
const GlideParams params = w->isDeleted() ? m_outParams : m_inParams;
const qreal t = (*animationIt).value();
switch (params.edge) {
case RotationEdge::Top:
data.setRotationAxis(Qt::XAxis);
data.setRotationAngle(angle * (1 - progress));
data.multiplyOpacity(progress);
switch(effect) {
default:
case GlideInOut:
if (info->added)
glideIn(w, data, info);
else if (info->closed)
glideOut(w, data, info);
break;
case GlideOutIn:
if (info->added)
glideOut(w, data, info);
if (info->closed)
glideIn(w, data, info);
break;
case GlideIn: glideIn(w, data, info); break;
case GlideOut: glideOut(w, data, info); break;
}
data.setRotationOrigin(QVector3D(0, 0, 0));
data.setRotationAngle(-interpolate(params.angle.from, params.angle.to, t));
break;
case RotationEdge::Right:
data.setRotationAxis(Qt::YAxis);
data.setRotationOrigin(QVector3D(w->width(), 0, 0));
data.setRotationAngle(-interpolate(params.angle.from, params.angle.to, t));
break;
case RotationEdge::Bottom:
data.setRotationAxis(Qt::XAxis);
data.setRotationOrigin(QVector3D(0, w->height(), 0));
data.setRotationAngle(interpolate(params.angle.from, params.angle.to, t));
break;
case RotationEdge::Left:
data.setRotationAxis(Qt::YAxis);
data.setRotationOrigin(QVector3D(0, 0, 0));
data.setRotationAngle(interpolate(params.angle.from, params.angle.to, t));
break;
default:
// Fallback to Top.
data.setRotationAxis(Qt::XAxis);
data.setRotationOrigin(QVector3D(0, 0, 0));
data.setRotationAngle(-interpolate(params.angle.from, params.angle.to, t));
break;
}
data.setZTranslation(-interpolate(params.distance.from, params.distance.to, t));
data.multiplyOpacity(interpolate(params.opacity.from, params.opacity.to, t));
effects->paintWindow(w, mask, region, data);
}
void GlideEffect::glideIn(EffectWindow* w, WindowPaintData& data, const InfoHash::const_iterator &info)
void GlideEffect::postPaintScreen()
{
const double progress = info->timeLine->currentValue();
data *= progress;
data.translate(int(w->width() / 2 * (1 - progress)), int(w->height() / 2 * (1 - progress)));
auto animationIt = m_animations.begin();
while (animationIt != m_animations.end()) {
if ((*animationIt).done()) {
EffectWindow *w = animationIt.key();
if (w->isDeleted()) {
w->unrefWindow();
}
animationIt = m_animations.erase(animationIt);
} else {
++animationIt;
}
}
effects->addRepaintFull();
effects->postPaintScreen();
}
void GlideEffect::glideOut(EffectWindow* w, WindowPaintData& data, const InfoHash::const_iterator &info)
bool GlideEffect::isActive() const
{
const double progress = info->timeLine->currentValue();
data *= (2 - progress);
data.translate(- int(w->width() / 2 * (1 - progress)), - int(w->height() / 2 * (1 - progress)));
return !m_animations.isEmpty();
}
void GlideEffect::postPaintWindow(EffectWindow* w)
bool GlideEffect::supported()
{
InfoHash::iterator info = windows.find(w);
if (info != windows.end()) {
if (info->added && info->timeLine->currentValue() == 1.0) {
windows.remove(w);
effects->addRepaintFull();
} else if (info->closed && info->timeLine->currentValue() == 0.0) {
info->closed = false;
if (info->deleted) {
windows.remove(w);
w->unrefWindow();
}
effects->addRepaintFull();
}
if (info->added || info->closed)
w->addRepaintFull();
}
effects->postPaintWindow(w);
return effects->isOpenGLCompositing()
&& effects->animationsSupported();
}
void GlideEffect::slotWindowAdded(EffectWindow* w)
void GlideEffect::windowAdded(EffectWindow *w)
{
if (!isGlideWindow(w))
if (effects->activeFullScreenEffect()) {
return;
}
if (!isGlideWindow(w)) {
return;
w->setData(IsGlideWindow, true);
}
if (!w->isVisible()) {
return;
}
const void *addGrab = w->data(WindowAddedGrabRole).value<void*>();
if (addGrab && addGrab != this)
if (addGrab && addGrab != this) {
return;
}
w->setData(WindowAddedGrabRole, QVariant::fromValue(static_cast<void*>(this)));
InfoHash::iterator it = windows.find(w);
WindowInfo *info = (it == windows.end()) ? &windows[w] : &it.value();
info->added = true;
info->closed = false;
info->deleted = false;
delete info->timeLine;
info->timeLine = new QTimeLine(duration);
info->timeLine->setCurveShape(QTimeLine::EaseOutCurve);
w->addRepaintFull();
TimeLine &timeLine = m_animations[w];
timeLine.reset();
timeLine.setDirection(TimeLine::Forward);
timeLine.setDuration(m_duration);
timeLine.setEasingCurve(QEasingCurve::InCurve);
effects->addRepaintFull();
}
void GlideEffect::slotWindowClosed(EffectWindow* w)
void GlideEffect::windowClosed(EffectWindow *w)
{
if (!isGlideWindow(w))
if (effects->activeFullScreenEffect()) {
return;
}
if (!isGlideWindow(w)) {
return;
}
if (!w->isVisible()) {
return;
}
const void *closeGrab = w->data(WindowClosedGrabRole).value<void*>();
if (closeGrab && closeGrab != this)
if (closeGrab && closeGrab != this) {
return;
}
w->refWindow();
w->setData(WindowClosedGrabRole, QVariant::fromValue(static_cast<void*>(this)));
InfoHash::iterator it = windows.find(w);
WindowInfo *info = (it == windows.end()) ? &windows[w] : &it.value();
info->added = false;
info->closed = true;
info->deleted = true;
delete info->timeLine;
info->timeLine = new QTimeLine(duration);
info->timeLine->setCurveShape(QTimeLine::EaseInCurve);
info->timeLine->setCurrentTime(info->timeLine->duration());
w->addRepaintFull();
}
TimeLine &timeLine = m_animations[w];
timeLine.reset();
timeLine.setDirection(TimeLine::Forward);
timeLine.setDuration(m_duration);
timeLine.setEasingCurve(QEasingCurve::OutCurve);
void GlideEffect::slotWindowDeleted(EffectWindow* w)
{
windows.remove(w);
effects->addRepaintFull();
}
bool GlideEffect::isGlideWindow(EffectWindow* w)
void GlideEffect::windowDeleted(EffectWindow *w)
{
if (effects->activeFullScreenEffect())
return false;
if (!w->isVisible())
return false;
if (s_blacklist.contains(w->windowClass()))
return false;
if (w->data(IsGlideWindow).toBool())
return true;
if (w->hasDecoration())
return true;
if (!w->isManaged() || w->isMenu() || w->isNotification() || w->isDesktop() ||
w->isDock() || w->isSplash() || w->isToolbar())
return false;
return true;
m_animations.remove(w);
}
bool GlideEffect::isActive() const
void GlideEffect::windowDataChanged(EffectWindow *w, int role)
{
return !windows.isEmpty();
}
if (role != WindowAddedGrabRole && role != WindowClosedGrabRole) {
return;
}
void GlideEffect::cancelWindowGrab(EffectWindow *w, int grabRole)
{
if (grabRole != WindowAddedGrabRole && grabRole != WindowClosedGrabRole) {
if (w->data(role).value<void*>() == this) {
return;
}
if (!w->data(IsGlideWindow).toBool()) {
auto animationIt = m_animations.find(w);
if (animationIt == m_animations.end()) {
return;
}
if (w->data(grabRole).value<void*>() != this) {
windows.remove(w);
w->setData(IsGlideWindow, false);
if (w->isDeleted() && role == WindowClosedGrabRole) {
w->unrefWindow();
}
}
GlideEffect::WindowInfo::WindowInfo()
: deleted(false)
, added(false)
, closed(false)
, timeLine(0)
{
m_animations.erase(animationIt);
}
GlideEffect::WindowInfo::~WindowInfo()
bool GlideEffect::isGlideWindow(EffectWindow *w) const
{
delete timeLine;
if (s_blacklist.contains(w->windowClass())) {
return false;
}
if (w->hasDecoration()) {
return true;
}
if (!w->isManaged()) {
return false;
}
return w->isNormalWindow()
|| w->isDialog();
}
} // namespace
} // namespace KWin
......@@ -5,6 +5,7 @@
Copyright (C) 2007 Philip Falkner <philip.falkner@gmail.com>
Copyright (C) 2009 Martin Gräßlin <mgraesslin@kde.org>
Copyright (C) 2010 Alexandre Pereira <pereira.alex@gmail.com>
Copyright (C) 2018 Vlad Zagorodniy <vladzzag@gmail.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
......@@ -23,81 +24,133 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef KWIN_GLIDE_H
#define KWIN_GLIDE_H
// kwineffects
#include <kwineffects.h>
class QTimeLine;
namespace KWin
{
class GlideEffect
: public Effect
class GlideEffect : public Effect
{
Q_OBJECT
Q_PROPERTY(int duration READ configuredDuration)
Q_PROPERTY(int effect READ configuredEffect)
Q_PROPERTY(int angle READ configuredAngle)
Q_PROPERTY(int duration READ duration)
Q_PROPERTY(RotationEdge inRotationEdge READ inRotationEdge)
Q_PROPERTY(qreal inRotationAngle READ inRotationAngle)
Q_PROPERTY(qreal inDistance READ inDistance)
Q_PROPERTY(qreal inOpacity READ inOpacity)
Q_PROPERTY(RotationEdge outRotationEdge READ outRotationEdge)
Q_PROPERTY(qreal outRotationAngle READ outRotationAngle)
Q_PROPERTY(qreal outDistance READ outDistance)
Q_PROPERTY(qreal outOpacity READ outOpacity)
public:
GlideEffect();
~GlideEffect();
virtual void reconfigure(ReconfigureFlags);
virtual void prePaintScreen(ScreenPrePaintData& data, int time);
virtual void prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time);
virtual void paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data);
virtual void postPaintWindow(EffectWindow* w);
virtual bool isActive() const;
int requestedEffectChainPosition() const override {
return 50;
}
~GlideEffect() override;
void reconfigure(ReconfigureFlags flags) override;
void prePaintScreen(ScreenPrePaintData &data, int time) override;
void prePaintWindow(EffectWindow *w, WindowPrePaintData &data, int time) override;
void paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data) override;
void postPaintScreen() override;
bool isActive() const override;
int requestedEffectChainPosition() const override;
static bool supported();
// for properties
int configuredDuration() const {
return duration;
}
int configuredEffect() const {
return effect;
}
int configuredAngle() const {
return angle;
}
public Q_SLOTS:
void slotWindowAdded(KWin::EffectWindow* c);
void slotWindowClosed(KWin::EffectWindow *c);
void slotWindowDeleted(KWin::EffectWindow *w);
enum RotationEdge {
Top = 0,
Right = 1,
Bottom = 2,
Left = 3
};
Q_ENUM(RotationEdge)
int duration() const;
RotationEdge inRotationEdge() const;
qreal inRotationAngle() const;
qreal inDistance() const;
qreal inOpacity() const;
RotationEdge outRotationEdge() const;
qreal outRotationAngle() const;
qreal outDistance() const;
qreal outOpacity() const;
private Q_SLOTS:
void windowAdded(EffectWindow *w);
void windowClosed(EffectWindow *w);
void windowDeleted(EffectWindow *w);
void windowDataChanged(EffectWindow *w, int role);
private:
class WindowInfo;
typedef QMap< const EffectWindow*, WindowInfo > InfoHash;
void glideIn(EffectWindow* w, WindowPaintData& data, const InfoHash::const_iterator &info);
void glideOut(EffectWindow* w, WindowPaintData& data, const InfoHash::const_iterator &info);
bool isGlideWindow(EffectWindow* w);
void cancelWindowGrab(EffectWindow *w, int grabRole);
InfoHash windows;
float duration;
int angle;
enum EffectStyle {
GlideIn = 0,
GlideInOut = 1,
GlideOutIn = 2,
GlideOut = 3
bool isGlideWindow(EffectWindow *w) const;
std::chrono::milliseconds m_duration;
QHash<EffectWindow*, TimeLine> m_animations;
struct GlideParams {
RotationEdge edge;
struct {
qreal from;
qreal to;
} angle, distance, opacity;
};
EffectStyle effect;
GlideParams m_inParams;
GlideParams m_outParams;
};
class GlideEffect::WindowInfo
inline int GlideEffect::requestedEffectChainPosition() const
{
public: