2021-02-04 09:07:20 +00:00
|
|
|
/*
|
2021-04-26 16:38:40 +00:00
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2021-02-04 09:07:20 +00:00
|
|
|
SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "decorationitem.h"
|
2021-04-26 16:38:40 +00:00
|
|
|
#include "abstract_client.h"
|
2021-11-26 09:18:19 +00:00
|
|
|
#include "abstract_output.h"
|
2021-04-26 16:38:40 +00:00
|
|
|
#include "composite.h"
|
|
|
|
#include "decorations/decoratedclient.h"
|
2021-05-21 10:51:23 +00:00
|
|
|
#include "deleted.h"
|
2021-04-26 16:38:40 +00:00
|
|
|
#include "scene.h"
|
2022-01-18 08:35:52 +00:00
|
|
|
#include "utils/common.h"
|
2021-02-04 09:07:20 +00:00
|
|
|
|
2021-12-16 16:36:47 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2021-02-04 09:07:20 +00:00
|
|
|
#include <KDecoration2/Decoration>
|
2021-04-26 16:38:40 +00:00
|
|
|
#include <KDecoration2/DecoratedClient>
|
|
|
|
|
|
|
|
#include <QPainter>
|
2021-02-04 09:07:20 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2021-04-26 16:38:40 +00:00
|
|
|
DecorationRenderer::DecorationRenderer(Decoration::DecoratedClientImpl *client)
|
|
|
|
: m_client(client)
|
|
|
|
, m_imageSizesDirty(true)
|
|
|
|
{
|
|
|
|
connect(client->decoration(), &KDecoration2::Decoration::damaged,
|
|
|
|
this, &DecorationRenderer::addDamage);
|
|
|
|
|
|
|
|
connect(client->decoration(), &KDecoration2::Decoration::bordersChanged,
|
|
|
|
this, &DecorationRenderer::invalidate);
|
|
|
|
connect(client->decoratedClient(), &KDecoration2::DecoratedClient::sizeChanged,
|
|
|
|
this, &DecorationRenderer::invalidate);
|
|
|
|
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
Decoration::DecoratedClientImpl *DecorationRenderer::client() const
|
|
|
|
{
|
|
|
|
return m_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationRenderer::invalidate()
|
|
|
|
{
|
2021-11-05 22:54:48 +00:00
|
|
|
if (m_client) {
|
|
|
|
addDamage(m_client->client()->rect());
|
|
|
|
}
|
2021-04-26 16:38:40 +00:00
|
|
|
m_imageSizesDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion DecorationRenderer::damage() const
|
|
|
|
{
|
|
|
|
return m_damage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationRenderer::addDamage(const QRegion ®ion)
|
|
|
|
{
|
|
|
|
m_damage += region;
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT damaged(region);
|
2021-04-26 16:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationRenderer::resetDamage()
|
|
|
|
{
|
|
|
|
m_damage = QRegion();
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:36:25 +00:00
|
|
|
qreal DecorationRenderer::effectiveDevicePixelRatio() const
|
|
|
|
{
|
|
|
|
// QPainter won't let paint with a device pixel ratio less than 1.
|
|
|
|
return std::max(qreal(1.0), devicePixelRatio());
|
|
|
|
}
|
|
|
|
|
2021-11-26 09:18:19 +00:00
|
|
|
qreal DecorationRenderer::devicePixelRatio() const
|
|
|
|
{
|
|
|
|
return m_devicePixelRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationRenderer::setDevicePixelRatio(qreal dpr)
|
|
|
|
{
|
|
|
|
if (m_devicePixelRatio != dpr) {
|
|
|
|
m_devicePixelRatio = dpr;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:38:40 +00:00
|
|
|
QImage DecorationRenderer::renderToImage(const QRect &geo)
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_client);
|
|
|
|
|
|
|
|
// Guess the pixel format of the X pixmap into which the QImage will be copied.
|
|
|
|
QImage::Format format;
|
|
|
|
const int depth = client()->client()->depth();
|
|
|
|
switch (depth) {
|
|
|
|
case 30:
|
|
|
|
format = QImage::Format_A2RGB30_Premultiplied;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
case 32:
|
|
|
|
format = QImage::Format_ARGB32_Premultiplied;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qCCritical(KWIN_CORE) << "Unsupported client depth" << depth;
|
|
|
|
format = QImage::Format_ARGB32_Premultiplied;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2021-11-26 09:18:19 +00:00
|
|
|
QImage image(geo.width() * m_devicePixelRatio, geo.height() * m_devicePixelRatio, format);
|
|
|
|
image.setDevicePixelRatio(m_devicePixelRatio);
|
2021-04-26 16:38:40 +00:00
|
|
|
image.fill(Qt::transparent);
|
|
|
|
QPainter p(&image);
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
2022-02-07 10:36:25 +00:00
|
|
|
p.setWindow(QRect(geo.topLeft(), geo.size() * effectiveDevicePixelRatio()));
|
2021-04-26 16:38:40 +00:00
|
|
|
p.setClipRect(geo);
|
|
|
|
renderToPainter(&p, geo);
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationRenderer::renderToPainter(QPainter *painter, const QRect &rect)
|
|
|
|
{
|
|
|
|
client()->decoration()->paint(painter, rect);
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:07:38 +00:00
|
|
|
DecorationItem::DecorationItem(KDecoration2::Decoration *decoration, AbstractClient *window, Item *parent)
|
|
|
|
: Item(parent)
|
|
|
|
, m_window(window)
|
2021-02-04 09:07:20 +00:00
|
|
|
{
|
2021-08-12 09:07:38 +00:00
|
|
|
m_renderer.reset(Compositor::self()->scene()->createDecorationRenderer(window->decoratedClient()));
|
2021-02-04 09:07:20 +00:00
|
|
|
|
2021-08-12 09:07:38 +00:00
|
|
|
connect(window, &Toplevel::frameGeometryChanged,
|
2021-02-04 09:07:20 +00:00
|
|
|
this, &DecorationItem::handleFrameGeometryChanged);
|
2021-08-12 09:07:38 +00:00
|
|
|
connect(window, &Toplevel::windowClosed,
|
|
|
|
this, &DecorationItem::handleWindowClosed);
|
2021-11-26 09:18:19 +00:00
|
|
|
connect(window, &Toplevel::screenChanged,
|
|
|
|
this, &DecorationItem::handleOutputChanged);
|
2021-02-04 09:07:20 +00:00
|
|
|
|
|
|
|
connect(decoration, &KDecoration2::Decoration::bordersChanged,
|
|
|
|
this, &DecorationItem::discardQuads);
|
|
|
|
|
2021-04-26 16:38:40 +00:00
|
|
|
connect(renderer(), &DecorationRenderer::damaged,
|
|
|
|
this, &DecorationItem::scheduleRepaint);
|
|
|
|
|
2021-08-12 09:07:38 +00:00
|
|
|
setSize(window->size());
|
2021-11-26 09:18:19 +00:00
|
|
|
handleOutputChanged();
|
2021-04-26 16:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationItem::preprocess()
|
|
|
|
{
|
|
|
|
const QRegion damage = m_renderer->damage();
|
|
|
|
if (!damage.isEmpty()) {
|
|
|
|
m_renderer->render(damage);
|
|
|
|
m_renderer->resetDamage();
|
|
|
|
}
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 09:18:19 +00:00
|
|
|
void DecorationItem::handleOutputChanged()
|
|
|
|
{
|
|
|
|
if (m_output) {
|
|
|
|
disconnect(m_output, &AbstractOutput::scaleChanged, this, &DecorationItem::handleOutputScaleChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_output = m_window->output();
|
|
|
|
|
|
|
|
if (m_output) {
|
|
|
|
handleOutputScaleChanged();
|
|
|
|
connect(m_output, &AbstractOutput::scaleChanged, this, &DecorationItem::handleOutputScaleChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationItem::handleOutputScaleChanged()
|
|
|
|
{
|
|
|
|
const qreal dpr = m_output->scale();
|
|
|
|
if (m_renderer->devicePixelRatio() != dpr) {
|
|
|
|
m_renderer->setDevicePixelRatio(dpr);
|
|
|
|
discardQuads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:07:20 +00:00
|
|
|
void DecorationItem::handleFrameGeometryChanged()
|
|
|
|
{
|
2021-08-12 09:07:38 +00:00
|
|
|
setSize(m_window->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecorationItem::handleWindowClosed(Toplevel *original, Deleted *deleted)
|
|
|
|
{
|
|
|
|
Q_UNUSED(original)
|
|
|
|
m_window = deleted;
|
|
|
|
|
|
|
|
// If the decoration is about to be destroyed, render the decoration for the last time.
|
|
|
|
preprocess();
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 16:38:40 +00:00
|
|
|
DecorationRenderer *DecorationItem::renderer() const
|
|
|
|
{
|
|
|
|
return m_renderer.data();
|
|
|
|
}
|
|
|
|
|
2021-12-16 16:36:47 +00:00
|
|
|
WindowQuad buildQuad(const QRect &partRect, const QPoint &textureOffset,
|
|
|
|
const qreal devicePixelRatio, bool rotated)
|
|
|
|
{
|
|
|
|
const QRect &r = partRect;
|
|
|
|
const int p = DecorationRenderer::TexturePad;
|
|
|
|
|
|
|
|
const int x0 = r.x();
|
|
|
|
const int y0 = r.y();
|
|
|
|
const int x1 = r.x() + r.width();
|
|
|
|
const int y1 = r.y() + r.height();
|
|
|
|
|
|
|
|
int u0 = textureOffset.x() + p;
|
|
|
|
int v0 = textureOffset.y() + p;
|
|
|
|
int u1 = textureOffset.x() + p + (r.width() * devicePixelRatio);
|
|
|
|
int v1 = textureOffset.y() + p + (r.height() * devicePixelRatio);
|
|
|
|
|
|
|
|
if (rotated) {
|
|
|
|
u0 = textureOffset.x() + p;
|
|
|
|
v0 = textureOffset.y() + p + (r.width() * devicePixelRatio);
|
|
|
|
u1 = textureOffset.x() + p + (r.height() * devicePixelRatio);
|
|
|
|
v1 = textureOffset.y() + p;
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowQuad quad;
|
|
|
|
quad[0] = WindowVertex(x0, y0, u0, v0); // Top-left
|
|
|
|
quad[1] = WindowVertex(x1, y0, u1, v0); // Top-right
|
|
|
|
quad[2] = WindowVertex(x1, y1, u1, v1); // Bottom-right
|
|
|
|
quad[3] = WindowVertex(x0, y1, u0, v1); // Bottom-left
|
|
|
|
return quad;
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:51:23 +00:00
|
|
|
WindowQuadList DecorationItem::buildQuads() const
|
|
|
|
{
|
2021-08-12 09:07:38 +00:00
|
|
|
if (m_window->frameMargins().isNull()) {
|
2021-05-21 10:51:23 +00:00
|
|
|
return WindowQuadList();
|
|
|
|
}
|
|
|
|
|
2021-12-16 16:36:47 +00:00
|
|
|
QRect left, top, right, bottom;
|
2022-02-07 10:36:25 +00:00
|
|
|
const qreal devicePixelRatio = m_renderer->effectiveDevicePixelRatio();
|
2021-12-16 16:36:47 +00:00
|
|
|
const int texturePad = DecorationRenderer::TexturePad;
|
2021-05-21 10:51:23 +00:00
|
|
|
|
2021-08-12 09:07:38 +00:00
|
|
|
if (const AbstractClient *client = qobject_cast<const AbstractClient *>(m_window)) {
|
2021-12-16 16:36:47 +00:00
|
|
|
client->layoutDecorationRects(left, top, right, bottom);
|
2021-08-12 09:07:38 +00:00
|
|
|
} else if (const Deleted *deleted = qobject_cast<const Deleted *>(m_window)) {
|
2021-12-16 16:36:47 +00:00
|
|
|
deleted->layoutDecorationRects(left, top, right, bottom);
|
2021-05-21 10:51:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 16:36:47 +00:00
|
|
|
const int topHeight = std::ceil(top.height() * devicePixelRatio);
|
|
|
|
const int bottomHeight = std::ceil(bottom.height() * devicePixelRatio);
|
|
|
|
const int leftWidth = std::ceil(left.width() * devicePixelRatio);
|
2021-05-21 10:51:23 +00:00
|
|
|
|
2021-12-16 16:36:47 +00:00
|
|
|
const QPoint topPosition(0, 0);
|
|
|
|
const QPoint bottomPosition(0, topPosition.y() + topHeight + (2 * texturePad));
|
|
|
|
const QPoint leftPosition(0, bottomPosition.y() + bottomHeight + (2 * texturePad));
|
|
|
|
const QPoint rightPosition(0, leftPosition.y() + leftWidth + (2 * texturePad));
|
2021-05-21 10:51:23 +00:00
|
|
|
|
|
|
|
WindowQuadList list;
|
2021-12-16 16:36:47 +00:00
|
|
|
if (left.isValid()) {
|
|
|
|
list.append(buildQuad(left, leftPosition, devicePixelRatio, true));
|
|
|
|
}
|
|
|
|
if (top.isValid()) {
|
|
|
|
list.append(buildQuad(top, topPosition, devicePixelRatio, false));
|
|
|
|
}
|
|
|
|
if (right.isValid()) {
|
|
|
|
list.append(buildQuad(right, rightPosition, devicePixelRatio, true));
|
|
|
|
}
|
|
|
|
if (bottom.isValid()) {
|
|
|
|
list.append(buildQuad(bottom, bottomPosition, devicePixelRatio, false));
|
2021-05-21 10:51:23 +00:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:07:20 +00:00
|
|
|
} // namespace KWin
|