2013-12-01 19:28:57 +00:00
|
|
|
/*
|
2020-08-02 22:22:19 +00: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 19:28:57 +00:00
|
|
|
|
|
|
|
#include "contrast.h"
|
|
|
|
#include "contrastshader.h"
|
|
|
|
// KConfigSkeleton
|
|
|
|
|
|
|
|
#include <QMatrix4x4>
|
2019-01-13 16:50:32 +00:00
|
|
|
#include <QWindow>
|
2013-12-01 19:28:57 +00:00
|
|
|
|
2020-04-29 15:18:41 +00:00
|
|
|
#include <KWaylandServer/surface_interface.h>
|
|
|
|
#include <KWaylandServer/contrast_interface.h>
|
|
|
|
#include <KWaylandServer/display.h>
|
2015-09-03 15:00:24 +00:00
|
|
|
|
2013-12-01 19:28:57 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2014-03-16 09:16:52 +00:00
|
|
|
static const QByteArray s_contrastAtomName = QByteArrayLiteral("_KDE_NET_WM_BACKGROUND_CONTRAST_REGION");
|
|
|
|
|
2013-12-01 19:28:57 +00:00
|
|
|
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()) {
|
2014-03-16 09:16:52 +00:00
|
|
|
net_wm_contrast_region = effects->announceSupportProperty(s_contrastAtomName, this);
|
2020-04-29 15:18:41 +00:00
|
|
|
KWaylandServer::Display *display = effects->waylandDisplay();
|
2015-09-29 08:21:24 +00:00
|
|
|
if (display) {
|
2020-12-09 21:24:41 +00:00
|
|
|
m_contrastManager = new KWaylandServer::ContrastManagerInterface(display, this);
|
2015-09-29 08:21:24 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-01-01 20:48:53 +00: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 14:51:18 +00:00
|
|
|
connect(effects, &EffectsHandler::xcbConnectionChanged, this,
|
|
|
|
[this] {
|
|
|
|
if (shader && shader->isValid()) {
|
|
|
|
net_wm_contrast_region = effects->announceSupportProperty(s_contrastAtomName, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2013-12-01 19:28:57 +00:00
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
// Fetch the contrast regions for all windows
|
2017-09-29 12:30:42 +00:00
|
|
|
for (EffectWindow *window: effects->stackingOrder()) {
|
2013-12-01 19:28:57 +00:00
|
|
|
updateContrastRegion(window);
|
2017-09-29 12:30:42 +00:00
|
|
|
}
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ContrastEffect::~ContrastEffect()
|
|
|
|
{
|
|
|
|
delete shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::slotScreenGeometryChanged()
|
|
|
|
{
|
2017-09-29 12:30:42 +00:00
|
|
|
effects->makeOpenGLContextCurrent();
|
|
|
|
if (!supported()) {
|
|
|
|
effects->reloadEffect(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (EffectWindow *window: effects->stackingOrder()) {
|
|
|
|
updateContrastRegion(window);
|
|
|
|
}
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::reconfigure(ReconfigureFlags flags)
|
|
|
|
{
|
|
|
|
Q_UNUSED(flags)
|
|
|
|
|
2014-01-06 20:43:03 +00:00
|
|
|
if (shader)
|
|
|
|
shader->init();
|
|
|
|
|
2015-09-29 08:21:24 +00:00
|
|
|
if (!shader || !shader->isValid()) {
|
2014-03-16 09:16:52 +00:00
|
|
|
effects->removeSupportProperty(s_contrastAtomName, this);
|
2015-09-29 08:21:24 +00:00
|
|
|
delete m_contrastManager;
|
|
|
|
m_contrastManager = nullptr;
|
|
|
|
}
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 11:02:22 +00:00
|
|
|
void ContrastEffect::updateContrastRegion(EffectWindow *w)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
QRegion region;
|
2014-01-05 17:28:31 +00:00
|
|
|
float colorTransform[16];
|
2017-09-10 14:51:18 +00: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 19:28:57 +00:00
|
|
|
|
2017-09-10 14:51:18 +00:00
|
|
|
for (unsigned int j = 0; j < 16; ++j) {
|
|
|
|
colorTransform[j] = floatCardinals[i + j];
|
|
|
|
}
|
2014-01-06 20:43:03 +00:00
|
|
|
|
2017-09-10 14:51:18 +00:00
|
|
|
QMatrix4x4 colorMatrix(colorTransform);
|
|
|
|
m_colorMatrices[w] = colorMatrix;
|
2014-01-22 20:46:56 +00:00
|
|
|
}
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 15:18:41 +00:00
|
|
|
KWaylandServer::SurfaceInterface *surf = w->surface();
|
2015-09-03 15:00:24 +00:00
|
|
|
|
|
|
|
if (surf && surf->contrast()) {
|
|
|
|
region = surf->contrast()->region();
|
2017-03-15 11:02:22 +00:00
|
|
|
m_colorMatrices[w] = colorMatrix(surf->contrast()->contrast(), surf->contrast()->intensity(), surf->contrast()->saturation());
|
2015-09-03 15:00:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 16:50:32 +00: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 15:00:24 +00: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 19:28:57 +00: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 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)
|
|
|
|
{
|
2020-04-29 15:18:41 +00:00
|
|
|
KWaylandServer::SurfaceInterface *surf = w->surface();
|
2015-09-03 15:00:24 +00:00
|
|
|
|
|
|
|
if (surf) {
|
2020-04-29 15:18:41 +00:00
|
|
|
m_contrastChangedConnections[w] = connect(surf, &KWaylandServer::SurfaceInterface::contrastChanged, this, [this, w] () {
|
2015-09-03 15:00:24 +00:00
|
|
|
|
|
|
|
if (w) {
|
|
|
|
updateContrastRegion(w);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-01-13 16:50:32 +00:00
|
|
|
|
|
|
|
if (auto internal = w->internalWindow()) {
|
|
|
|
internal->installEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2013-12-01 19:28:57 +00:00
|
|
|
updateContrastRegion(w);
|
|
|
|
}
|
|
|
|
|
2019-01-13 16:50:32 +00: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 15:00:24 +00:00
|
|
|
void ContrastEffect::slotWindowDeleted(EffectWindow *w)
|
|
|
|
{
|
|
|
|
if (m_contrastChangedConnections.contains(w)) {
|
|
|
|
disconnect(m_contrastChangedConnections[w]);
|
|
|
|
m_contrastChangedConnections.remove(w);
|
2017-03-15 11:02:22 +00:00
|
|
|
m_colorMatrices.remove(w);
|
2015-09-03 15:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 20:43:03 +00:00
|
|
|
void ContrastEffect::slotPropertyNotify(EffectWindow *w, long atom)
|
|
|
|
{
|
2017-09-10 14:51:18 +00:00
|
|
|
if (w && atom == net_wm_contrast_region && net_wm_contrast_region != XCB_ATOM_NONE) {
|
2014-01-06 20:43:03 +00: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 19:28:57 +00:00
|
|
|
bool ContrastEffect::enabledByDefault()
|
|
|
|
{
|
|
|
|
GLPlatform *gl = GLPlatform::instance();
|
|
|
|
|
|
|
|
if (gl->isIntel() && gl->chipClass() < SandyBridge)
|
|
|
|
return false;
|
2016-08-09 12:52:14 +00:00
|
|
|
if (gl->isSoftwareEmulation()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContrastEffect::supported()
|
|
|
|
{
|
2014-11-22 14:53:15 +00:00
|
|
|
bool supported = effects->isOpenGLCompositing() && GLRenderTarget::supported();
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
if (supported) {
|
|
|
|
int maxTexSize;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
|
|
|
|
|
2014-02-24 15:13:30 +00:00
|
|
|
const QSize screenSize = effects->virtualScreenSize();
|
|
|
|
if (screenSize.width() > maxTexSize || screenSize.height() > maxTexSize)
|
2013-12-01 19:28:57 +00:00
|
|
|
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()) {
|
|
|
|
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-22 20:46:56 +00:00
|
|
|
region = w->decorationInnerRect();
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContrastEffect::uploadRegion(QVector2D *&map, const QRegion ®ion)
|
|
|
|
{
|
2017-12-02 16:38:14 +00:00
|
|
|
for (const QRect &r : region) {
|
2013-12-01 19:28:57 +00: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 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;
|
2015-05-19 12:35:12 +00:00
|
|
|
if (!vertexCount)
|
|
|
|
return;
|
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));
|
|
|
|
}
|
|
|
|
|
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 ContrastEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::milliseconds presentTime)
|
2014-01-06 20:43:03 +00:00
|
|
|
{
|
|
|
|
m_paintedArea = QRegion();
|
|
|
|
m_currentContrast = QRegion();
|
|
|
|
|
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);
|
2014-01-06 20:43:03 +00:00
|
|
|
}
|
|
|
|
|
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 ContrastEffect::prePaintWindow(EffectWindow* w, WindowPrePaintData& data, std::chrono::milliseconds presentTime)
|
2014-01-06 20:43:03 +00:00
|
|
|
{
|
|
|
|
// this effect relies on prePaintWindow being called in the bottom to top order
|
|
|
|
|
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->prePaintWindow(w, data, presentTime);
|
2014-01-06 20:43:03 +00:00
|
|
|
|
|
|
|
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 15:13:30 +00:00
|
|
|
const QRect screen = effects->virtualScreenGeometry();
|
2014-01-06 20:43:03 +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 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 19:28:57 +00:00
|
|
|
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();
|
|
|
|
|
2016-02-25 11:45:19 +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;
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:04:15 +00:00
|
|
|
void ContrastEffect::drawWindow(EffectWindow *w, int mask, const QRegion ®ion, WindowPaintData &data)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
2016-10-18 14:33:20 +00:00
|
|
|
const QRect screen = GLRenderTarget::virtualScreenGeometry();
|
2013-12-01 19:28:57 +00: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 11:09:01 +00: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 20:08:47 +00:00
|
|
|
QRegion scaledShape;
|
|
|
|
for (QRect r : shape) {
|
2016-02-09 11:09:01 +00: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 20:08:47 +00:00
|
|
|
scaledShape |= r;
|
2016-02-09 11:09:01 +00:00
|
|
|
}
|
2019-07-09 20:08:47 +00:00
|
|
|
shape = scaledShape & region;
|
2016-02-09 11:09:01 +00:00
|
|
|
|
|
|
|
//Only translated, not scaled
|
|
|
|
} else if (translated) {
|
2013-12-01 19:28:57 +00:00
|
|
|
shape = shape.translated(data.xTranslation(), data.yTranslation());
|
|
|
|
shape = shape & region;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shape.isEmpty()) {
|
2017-03-15 11:02:22 +00:00
|
|
|
doContrast(w, shape, screen, data.opacity(), data.screenProjectionMatrix());
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 17:28:31 +00:00
|
|
|
// Draw the window over the contrast area
|
2013-12-01 19:28:57 +00:00
|
|
|
effects->drawWindow(w, mask, region, data);
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:04:15 +00:00
|
|
|
void ContrastEffect::paintEffectFrame(EffectFrame *frame, const QRegion ®ion, double opacity, double frameOpacity)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
2014-01-23 09:54:40 +00:00
|
|
|
//FIXME: this is a no-op for now, it should figure out the right contrast, intensity, saturation
|
2013-12-01 19:28:57 +00:00
|
|
|
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
|
|
|
|
}
|
|
|
|
|
2017-03-15 11:02:22 +00:00
|
|
|
void ContrastEffect::doContrast(EffectWindow *w, const QRegion& shape, const QRect& screen, const float opacity, const QMatrix4x4 &screenProjection)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
const QRegion actualShape = shape & screen;
|
|
|
|
const QRect r = actualShape.boundingRect();
|
|
|
|
|
2017-03-06 00:39:12 +00:00
|
|
|
qreal scale = GLRenderTarget::virtualScreenScale();
|
|
|
|
|
2013-12-01 19:28:57 +00:00
|
|
|
// Upload geometry for the horizontal and vertical passes
|
|
|
|
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
|
2019-02-15 11:31:51 +00:00
|
|
|
vbo->reset();
|
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
|
2017-03-06 00:39:12 +00:00
|
|
|
GLTexture scratch(GL_RGBA8, r.width() * scale, r.height() * scale);
|
2013-12-01 19:28:57 +00:00
|
|
|
scratch.setFilter(GL_LINEAR);
|
|
|
|
scratch.setWrapMode(GL_CLAMP_TO_EDGE);
|
|
|
|
scratch.bind();
|
|
|
|
|
2016-10-18 14:33:20 +00:00
|
|
|
const QRect sg = GLRenderTarget::virtualScreenGeometry();
|
2018-05-17 09:51:50 +00: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 00:39:12 +00:00
|
|
|
scratch.width(), scratch.height());
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
// Draw the texture on the offscreen framebuffer object, while blurring it horizontally
|
|
|
|
|
2017-03-15 11:02:22 +00:00
|
|
|
shader->setColorMatrix(m_colorMatrices.value(w));
|
2013-12-01 19:28:57 +00:00
|
|
|
shader->bind();
|
|
|
|
|
2014-01-23 13:33:19 +00:00
|
|
|
|
|
|
|
shader->setOpacity(opacity);
|
2013-12-01 19:28:57 +00:00
|
|
|
// Set up the texture matrix to transform from screen coordinates
|
|
|
|
// to texture coordinates.
|
|
|
|
QMatrix4x4 textureMatrix;
|
2017-03-06 00:39:12 +00:00
|
|
|
textureMatrix.scale(1.0 / r.width(), -1.0 / r.height(), 1);
|
|
|
|
textureMatrix.translate(-r.x(), -r.height() - r.y(), 0);
|
2013-12-01 19:28:57 +00:00
|
|
|
shader->setTextureMatrix(textureMatrix);
|
2016-10-18 14:33:20 +00:00
|
|
|
shader->setModelViewProjectionMatrix(screenProjection);
|
2013-12-01 19:28:57 +00: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 18:11:06 +00:00
|
|
|
bool ContrastEffect::isActive() const
|
|
|
|
{
|
|
|
|
return !effects->isScreenLocked();
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:26:43 +00:00
|
|
|
bool ContrastEffect::blocksDirectScanout() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-01 19:28:57 +00:00
|
|
|
} // namespace KWin
|
|
|
|
|