2013-06-21 08:03:31 +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 "scene_qpainter.h"
|
|
|
|
// KWin
|
|
|
|
#include "client.h"
|
|
|
|
#include "composite.h"
|
|
|
|
#include "deleted.h"
|
|
|
|
#include "effects.h"
|
2013-06-25 07:53:45 +00:00
|
|
|
#include "main.h"
|
2013-06-21 08:03:31 +00:00
|
|
|
#include "toplevel.h"
|
2014-03-13 15:21:04 +00:00
|
|
|
#if HAVE_WAYLAND
|
2013-06-21 08:03:31 +00:00
|
|
|
#include "wayland_backend.h"
|
2015-03-23 13:29:07 +00:00
|
|
|
#include "wayland_server.h"
|
2015-03-19 07:29:34 +00:00
|
|
|
#include "x11windowed_backend.h"
|
2014-09-19 11:59:51 +00:00
|
|
|
#include <KWayland/Client/buffer.h>
|
|
|
|
#include <KWayland/Client/shm_pool.h>
|
|
|
|
#include <KWayland/Client/surface.h>
|
2015-02-09 17:56:05 +00:00
|
|
|
#include <KWayland/Server/buffer_interface.h>
|
|
|
|
#include <KWayland/Server/surface_interface.h>
|
2013-06-21 08:03:31 +00:00
|
|
|
#endif
|
|
|
|
#include "xcbutils.h"
|
2014-07-22 11:11:19 +00:00
|
|
|
#include "decorations/decoratedclient.h"
|
2013-06-21 08:03:31 +00:00
|
|
|
// Qt
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QPainter>
|
2014-07-22 11:11:19 +00:00
|
|
|
#include <KDecoration2/Decoration>
|
2013-06-21 08:03:31 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
//****************************************
|
|
|
|
// QPainterBackend
|
|
|
|
//****************************************
|
|
|
|
QPainterBackend::QPainterBackend()
|
|
|
|
: m_failed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterBackend::~QPainterBackend()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OverlayWindow* QPainterBackend::overlayWindow()
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPainterBackend::showOverlay()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPainterBackend::screenGeometryChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
Q_UNUSED(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPainterBackend::setFailed(const QString &reason)
|
|
|
|
{
|
2014-12-05 10:42:15 +00:00
|
|
|
qCWarning(KWIN_CORE) << "Creating the XRender backend failed: " << reason;
|
2013-06-21 08:03:31 +00:00
|
|
|
m_failed = true;
|
|
|
|
}
|
|
|
|
|
2014-03-13 15:21:04 +00:00
|
|
|
#if HAVE_WAYLAND
|
2013-06-21 08:03:31 +00:00
|
|
|
//****************************************
|
|
|
|
// WaylandQPainterBackend
|
|
|
|
//****************************************
|
|
|
|
|
2015-03-23 13:29:07 +00:00
|
|
|
WaylandQPainterBackend::WaylandQPainterBackend(Wayland::WaylandBackend *b)
|
2013-06-21 08:03:31 +00:00
|
|
|
: QPainterBackend()
|
2015-03-23 13:29:07 +00:00
|
|
|
, m_backend(b)
|
2013-06-21 08:03:31 +00:00
|
|
|
, m_needsFullRepaint(true)
|
2014-09-19 13:01:59 +00:00
|
|
|
, m_backBuffer(QImage(QSize(), QImage::Format_RGB32))
|
2014-09-22 07:59:58 +00:00
|
|
|
, m_buffer()
|
2013-06-21 08:03:31 +00:00
|
|
|
{
|
2015-03-23 13:29:07 +00:00
|
|
|
connect(b->shmPool(), SIGNAL(poolResized()), SLOT(remapBuffer()));
|
|
|
|
connect(b, &Wayland::WaylandBackend::shellSurfaceSizeChanged,
|
2013-07-05 08:37:21 +00:00
|
|
|
this, &WaylandQPainterBackend::screenGeometryChanged);
|
2015-03-23 13:29:07 +00:00
|
|
|
connect(b->surface(), &KWayland::Client::Surface::frameRendered,
|
2014-08-27 16:24:43 +00:00
|
|
|
Compositor::self(), &Compositor::bufferSwapComplete);
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WaylandQPainterBackend::~WaylandQPainterBackend()
|
|
|
|
{
|
|
|
|
if (m_buffer) {
|
2014-09-22 07:59:58 +00:00
|
|
|
m_buffer.toStrongRef()->setUsed(false);
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaylandQPainterBackend::usesOverlayWindow() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandQPainterBackend::present(int mask, const QRegion &damage)
|
|
|
|
{
|
|
|
|
Q_UNUSED(mask)
|
|
|
|
if (m_backBuffer.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-27 16:24:43 +00:00
|
|
|
Compositor::self()->aboutToSwapBuffers();
|
2013-06-21 08:03:31 +00:00
|
|
|
m_needsFullRepaint = false;
|
2015-03-23 13:29:07 +00:00
|
|
|
auto s = m_backend->surface();
|
2014-09-22 07:59:58 +00:00
|
|
|
s->attachBuffer(m_buffer);
|
2014-08-20 12:06:58 +00:00
|
|
|
s->damage(damage);
|
|
|
|
s->commit();
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandQPainterBackend::screenGeometryChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
Q_UNUSED(size)
|
2014-08-18 18:07:15 +00:00
|
|
|
if (!m_buffer) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-22 07:59:58 +00:00
|
|
|
m_buffer.toStrongRef()->setUsed(false);
|
|
|
|
m_buffer.clear();
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage *WaylandQPainterBackend::buffer()
|
|
|
|
{
|
|
|
|
return &m_backBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandQPainterBackend::prepareRenderingFrame()
|
|
|
|
{
|
|
|
|
if (m_buffer) {
|
2014-09-22 07:59:58 +00:00
|
|
|
auto b = m_buffer.toStrongRef();
|
|
|
|
if (b->isReleased()) {
|
2013-06-21 08:03:31 +00:00
|
|
|
// we can re-use this buffer
|
2014-09-22 07:59:58 +00:00
|
|
|
b->setReleased(false);
|
2013-06-21 08:03:31 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// buffer is still in use, get a new one
|
2014-09-22 07:59:58 +00:00
|
|
|
b->setUsed(false);
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 07:59:58 +00:00
|
|
|
m_buffer.clear();
|
2015-03-23 13:29:07 +00:00
|
|
|
const QSize size(m_backend->shellSurfaceSize());
|
|
|
|
m_buffer = m_backend->shmPool()->getBuffer(size, size.width() * 4);
|
2013-06-21 08:03:31 +00:00
|
|
|
if (!m_buffer) {
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Did not get a new Buffer from Shm Pool";
|
2013-06-21 08:03:31 +00:00
|
|
|
m_backBuffer = QImage();
|
|
|
|
return;
|
|
|
|
}
|
2014-09-22 07:59:58 +00:00
|
|
|
auto b = m_buffer.toStrongRef();
|
|
|
|
b->setUsed(true);
|
|
|
|
m_backBuffer = QImage(b->address(), size.width(), size.height(), QImage::Format_RGB32);
|
2013-06-21 08:03:31 +00:00
|
|
|
m_backBuffer.fill(Qt::transparent);
|
|
|
|
m_needsFullRepaint = true;
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Created a new back buffer";
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandQPainterBackend::remapBuffer()
|
|
|
|
{
|
2014-09-22 07:59:58 +00:00
|
|
|
if (!m_buffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto b = m_buffer.toStrongRef();
|
|
|
|
if (!b->isUsed()){
|
2013-06-21 08:03:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const QSize size = m_backBuffer.size();
|
2014-09-22 07:59:58 +00:00
|
|
|
m_backBuffer = QImage(b->address(), size.width(), size.height(), QImage::Format_RGB32);
|
2014-12-05 10:42:15 +00:00
|
|
|
qCDebug(KWIN_CORE) << "Remapped our back buffer";
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WaylandQPainterBackend::needsFullRepaint() const
|
|
|
|
{
|
|
|
|
return m_needsFullRepaint;
|
|
|
|
}
|
|
|
|
|
2015-03-19 07:29:34 +00:00
|
|
|
//****************************************
|
|
|
|
// X11WindowedBackend
|
|
|
|
//****************************************
|
2015-03-23 13:29:07 +00:00
|
|
|
X11WindowedQPainterBackend::X11WindowedQPainterBackend(X11WindowedBackend *backend)
|
2015-03-19 07:29:34 +00:00
|
|
|
: QPainterBackend()
|
2015-03-23 13:29:07 +00:00
|
|
|
, m_backBuffer(backend->size(), QImage::Format_RGB32)
|
|
|
|
, m_backend(backend)
|
2015-03-19 07:29:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
X11WindowedQPainterBackend::~X11WindowedQPainterBackend()
|
|
|
|
{
|
|
|
|
if (m_gc) {
|
2015-03-23 13:29:07 +00:00
|
|
|
xcb_free_gc(m_backend->connection(), m_gc);
|
2015-03-19 07:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage *X11WindowedQPainterBackend::buffer()
|
|
|
|
{
|
|
|
|
return &m_backBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X11WindowedQPainterBackend::needsFullRepaint() const
|
|
|
|
{
|
|
|
|
return m_needsFullRepaint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void X11WindowedQPainterBackend::prepareRenderingFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void X11WindowedQPainterBackend::screenGeometryChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
if (m_backBuffer.size() != size) {
|
|
|
|
m_backBuffer = QImage(size, QImage::Format_RGB32);
|
|
|
|
m_backBuffer.fill(Qt::black);
|
|
|
|
m_needsFullRepaint = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void X11WindowedQPainterBackend::present(int mask, const QRegion &damage)
|
|
|
|
{
|
|
|
|
Q_UNUSED(mask)
|
|
|
|
Q_UNUSED(damage)
|
2015-03-23 13:29:07 +00:00
|
|
|
xcb_connection_t *c = m_backend->connection();
|
|
|
|
const xcb_window_t window = m_backend->window();
|
2015-03-19 07:29:34 +00:00
|
|
|
if (m_gc == XCB_NONE) {
|
|
|
|
m_gc = xcb_generate_id(c);
|
|
|
|
xcb_create_gc(c, m_gc, window, 0, nullptr);
|
|
|
|
}
|
|
|
|
// TODO: only update changes?
|
|
|
|
xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, window, m_gc,
|
|
|
|
m_backBuffer.width(), m_backBuffer.height(), 0, 0, 0, 24,
|
|
|
|
m_backBuffer.byteCount(), m_backBuffer.constBits());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool X11WindowedQPainterBackend::usesOverlayWindow() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-21 08:03:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//****************************************
|
|
|
|
// SceneQPainter
|
|
|
|
//****************************************
|
2015-02-23 13:41:45 +00:00
|
|
|
SceneQPainter *SceneQPainter::createScene(QObject *parent)
|
2013-06-21 08:03:31 +00:00
|
|
|
{
|
|
|
|
QScopedPointer<QPainterBackend> backend;
|
2014-03-13 15:21:04 +00:00
|
|
|
#if HAVE_WAYLAND
|
2013-06-25 07:53:45 +00:00
|
|
|
if (kwinApp()->shouldUseWaylandForCompositing()) {
|
2015-03-23 13:29:07 +00:00
|
|
|
if (X11WindowedBackend *b = dynamic_cast<X11WindowedBackend*>(waylandServer()->backend())) {
|
|
|
|
backend.reset(new X11WindowedQPainterBackend(b));
|
|
|
|
} else if (Wayland::WaylandBackend *b = dynamic_cast<Wayland::WaylandBackend*>(waylandServer()->backend())) {
|
|
|
|
backend.reset(new WaylandQPainterBackend(b));
|
2015-03-19 07:29:34 +00:00
|
|
|
}
|
2013-06-21 08:03:31 +00:00
|
|
|
if (backend->isFailed()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-02-23 13:41:45 +00:00
|
|
|
return new SceneQPainter(backend.take(), parent);
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:41:45 +00:00
|
|
|
SceneQPainter::SceneQPainter(QPainterBackend *backend, QObject *parent)
|
|
|
|
: Scene(parent)
|
2013-06-21 08:03:31 +00:00
|
|
|
, m_backend(backend)
|
|
|
|
, m_painter(new QPainter())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SceneQPainter::~SceneQPainter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositingType SceneQPainter::compositingType() const
|
|
|
|
{
|
|
|
|
return QPainterCompositing;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SceneQPainter::initFailed() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneQPainter::paintGenericScreen(int mask, ScreenPaintData data)
|
|
|
|
{
|
|
|
|
m_painter->save();
|
|
|
|
m_painter->translate(data.xTranslation(), data.yTranslation());
|
|
|
|
m_painter->scale(data.xScale(), data.yScale());
|
|
|
|
Scene::paintGenericScreen(mask, data);
|
|
|
|
m_painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 SceneQPainter::paint(QRegion damage, ToplevelList toplevels)
|
|
|
|
{
|
|
|
|
QElapsedTimer renderTimer;
|
|
|
|
renderTimer.start();
|
|
|
|
|
|
|
|
createStackingOrder(toplevels);
|
|
|
|
|
|
|
|
int mask = 0;
|
|
|
|
m_backend->prepareRenderingFrame();
|
|
|
|
m_painter->begin(m_backend->buffer());
|
|
|
|
if (m_backend->needsFullRepaint()) {
|
|
|
|
mask |= Scene::PAINT_SCREEN_BACKGROUND_FIRST;
|
|
|
|
damage = QRegion(0, 0, displayWidth(), displayHeight());
|
|
|
|
}
|
|
|
|
QRegion updateRegion, validRegion;
|
|
|
|
paintScreen(&mask, damage, QRegion(), &updateRegion, &validRegion);
|
|
|
|
|
|
|
|
m_backend->showOverlay();
|
|
|
|
|
|
|
|
m_painter->end();
|
|
|
|
m_backend->present(mask, updateRegion);
|
|
|
|
// do cleanup
|
|
|
|
clearStackingOrder();
|
|
|
|
|
|
|
|
return renderTimer.nsecsElapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneQPainter::paintBackground(QRegion region)
|
|
|
|
{
|
|
|
|
m_painter->setBrush(Qt::black);
|
|
|
|
m_painter->drawRects(region.rects());
|
|
|
|
}
|
|
|
|
|
|
|
|
Scene::Window *SceneQPainter::createWindow(Toplevel *toplevel)
|
|
|
|
{
|
|
|
|
return new SceneQPainter::Window(this, toplevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scene::EffectFrame *SceneQPainter::createEffectFrame(EffectFrameImpl *frame)
|
|
|
|
{
|
|
|
|
return new QPainterEffectFrame(frame, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Shadow *SceneQPainter::createShadow(Toplevel *toplevel)
|
|
|
|
{
|
|
|
|
return new SceneQPainterShadow(toplevel);
|
|
|
|
}
|
|
|
|
|
2015-03-19 07:29:34 +00:00
|
|
|
void SceneQPainter::screenGeometryChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
Scene::screenGeometryChanged(size);
|
|
|
|
m_backend->screenGeometryChanged(size);
|
|
|
|
}
|
|
|
|
|
2013-06-21 08:03:31 +00:00
|
|
|
//****************************************
|
|
|
|
// SceneQPainter::Window
|
|
|
|
//****************************************
|
|
|
|
SceneQPainter::Window::Window(SceneQPainter *scene, Toplevel *c)
|
|
|
|
: Scene::Window(c)
|
|
|
|
, m_scene(scene)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SceneQPainter::Window::~Window()
|
|
|
|
{
|
|
|
|
discardShape();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneQPainter::Window::performPaint(int mask, QRegion region, WindowPaintData data)
|
|
|
|
{
|
|
|
|
if (!(mask & (PAINT_WINDOW_TRANSFORMED | PAINT_SCREEN_TRANSFORMED)))
|
|
|
|
region &= toplevel->visibleRect();
|
|
|
|
|
|
|
|
if (region.isEmpty())
|
|
|
|
return;
|
|
|
|
QPainterWindowPixmap *pixmap = windowPixmap<QPainterWindowPixmap>();
|
|
|
|
if (!pixmap || !pixmap->isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!toplevel->damage().isEmpty()) {
|
|
|
|
pixmap->update(toplevel->damage());
|
|
|
|
toplevel->resetDamage();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainter *scenePainter = m_scene->painter();
|
|
|
|
QPainter *painter = scenePainter;
|
|
|
|
painter->setClipRegion(region);
|
|
|
|
painter->setClipping(true);
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
painter->translate(x(), y());
|
|
|
|
if (mask & PAINT_WINDOW_TRANSFORMED) {
|
|
|
|
painter->translate(data.xTranslation(), data.yTranslation());
|
|
|
|
painter->scale(data.xScale(), data.yScale());
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool opaque = qFuzzyCompare(1.0, data.opacity());
|
|
|
|
QImage tempImage;
|
|
|
|
QPainter tempPainter;
|
|
|
|
if (!opaque) {
|
|
|
|
// need a temp render target which we later on blit to the screen
|
|
|
|
tempImage = QImage(toplevel->visibleRect().size(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
tempImage.fill(Qt::transparent);
|
|
|
|
tempPainter.begin(&tempImage);
|
|
|
|
tempPainter.save();
|
|
|
|
tempPainter.translate(toplevel->geometry().topLeft() - toplevel->visibleRect().topLeft());
|
|
|
|
painter = &tempPainter;
|
|
|
|
}
|
|
|
|
renderShadow(painter);
|
|
|
|
renderWindowDecorations(painter);
|
|
|
|
|
|
|
|
// render content
|
|
|
|
const QRect src = QRect(toplevel->clientPos(), toplevel->clientSize());
|
|
|
|
painter->drawImage(toplevel->clientPos(), pixmap->image(), src);
|
|
|
|
|
|
|
|
if (!opaque) {
|
|
|
|
tempPainter.restore();
|
|
|
|
tempPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
QColor translucent(Qt::transparent);
|
|
|
|
translucent.setAlphaF(data.opacity());
|
|
|
|
tempPainter.fillRect(QRect(QPoint(0, 0), toplevel->visibleRect().size()), translucent);
|
|
|
|
tempPainter.end();
|
|
|
|
painter = scenePainter;
|
|
|
|
painter->drawImage(toplevel->visibleRect().topLeft() - toplevel->geometry().topLeft(), tempImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
|
|
|
|
painter->setClipRegion(QRegion());
|
|
|
|
painter->setClipping(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneQPainter::Window::renderShadow(QPainter* painter)
|
|
|
|
{
|
|
|
|
if (!toplevel->shadow()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SceneQPainterShadow *shadow = static_cast<SceneQPainterShadow *>(toplevel->shadow());
|
|
|
|
const QPixmap &topLeft = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementTopLeft);
|
|
|
|
const QPixmap &top = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementTop);
|
|
|
|
const QPixmap &topRight = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementTopRight);
|
|
|
|
const QPixmap &bottomLeft = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementBottomLeft);
|
|
|
|
const QPixmap &bottom = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementBottom);
|
|
|
|
const QPixmap &bottomRight = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementBottomRight);
|
|
|
|
const QPixmap &left = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementLeft);
|
|
|
|
const QPixmap &right = shadow->shadowPixmap(SceneQPainterShadow::ShadowElementRight);
|
|
|
|
|
|
|
|
const int leftOffset = shadow->leftOffset();
|
|
|
|
const int topOffset = shadow->topOffset();
|
|
|
|
const int rightOffset = shadow->rightOffset();
|
|
|
|
const int bottomOffset = shadow->bottomOffset();
|
|
|
|
|
|
|
|
// top left
|
|
|
|
painter->drawPixmap(-leftOffset, -topOffset, topLeft);
|
|
|
|
// top right
|
|
|
|
painter->drawPixmap(toplevel->width() - topRight.width() + rightOffset, -topOffset, topRight);
|
|
|
|
// bottom left
|
|
|
|
painter->drawPixmap(-leftOffset, toplevel->height() - bottomLeft.height() + bottomOffset, bottomLeft);
|
|
|
|
// bottom right
|
|
|
|
painter->drawPixmap(toplevel->width() - bottomRight.width() + rightOffset,
|
|
|
|
toplevel->height() - bottomRight.height() + bottomOffset,
|
|
|
|
bottomRight);
|
|
|
|
// top
|
|
|
|
painter->drawPixmap(topLeft.width() - leftOffset, -topOffset,
|
|
|
|
toplevel->width() - topLeft.width() - topRight.width() + leftOffset + rightOffset,
|
|
|
|
top.height(),
|
|
|
|
top);
|
|
|
|
// left
|
|
|
|
painter->drawPixmap(-leftOffset, topLeft.height() - topOffset, left.width(),
|
|
|
|
toplevel->height() - topLeft.height() - bottomLeft.height() + topOffset + bottomOffset,
|
|
|
|
left);
|
|
|
|
// right
|
|
|
|
painter->drawPixmap(toplevel->width() - right.width() + rightOffset,
|
|
|
|
topRight.height() - topOffset,
|
|
|
|
right.width(),
|
|
|
|
toplevel->height() - topRight.height() - bottomRight.height() + topOffset + bottomOffset,
|
|
|
|
right);
|
|
|
|
// bottom
|
|
|
|
painter->drawPixmap(bottomLeft.width() - leftOffset,
|
|
|
|
toplevel->height() - bottom.height() + bottomOffset,
|
|
|
|
toplevel->width() - bottomLeft.width() - bottomRight.width() + leftOffset + rightOffset,
|
|
|
|
bottom.height(),
|
|
|
|
bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneQPainter::Window::renderWindowDecorations(QPainter *painter)
|
|
|
|
{
|
|
|
|
// TODO: custom decoration opacity
|
|
|
|
Client *client = dynamic_cast<Client*>(toplevel);
|
|
|
|
Deleted *deleted = dynamic_cast<Deleted*>(toplevel);
|
|
|
|
if (!client && !deleted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool noBorder = true;
|
2014-07-23 07:20:28 +00:00
|
|
|
const SceneQPainterDecorationRenderer *renderer = nullptr;
|
2013-06-21 08:03:31 +00:00
|
|
|
QRect dtr, dlr, drr, dbr;
|
|
|
|
if (client && !client->noBorder()) {
|
2014-10-21 05:46:44 +00:00
|
|
|
if (client->isDecorated()) {
|
|
|
|
if (SceneQPainterDecorationRenderer *r = static_cast<SceneQPainterDecorationRenderer *>(client->decoratedClient()->renderer())) {
|
2014-07-23 07:20:28 +00:00
|
|
|
r->render();
|
2014-07-22 11:11:19 +00:00
|
|
|
renderer = r;
|
|
|
|
}
|
|
|
|
}
|
2014-07-25 10:55:28 +00:00
|
|
|
client->layoutDecorationRects(dlr, dtr, drr, dbr);
|
2013-06-21 08:03:31 +00:00
|
|
|
noBorder = false;
|
|
|
|
} else if (deleted && !deleted->noBorder()) {
|
|
|
|
noBorder = false;
|
|
|
|
deleted->layoutDecorationRects(dlr, dtr, drr, dbr);
|
2014-07-23 07:20:28 +00:00
|
|
|
renderer = static_cast<const SceneQPainterDecorationRenderer *>(deleted->decorationRenderer());
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
2014-07-22 11:11:19 +00:00
|
|
|
if (noBorder || !renderer) {
|
2013-06-21 08:03:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-22 11:11:19 +00:00
|
|
|
painter->drawImage(dtr, renderer->image(SceneQPainterDecorationRenderer::DecorationPart::Top));
|
|
|
|
painter->drawImage(dlr, renderer->image(SceneQPainterDecorationRenderer::DecorationPart::Left));
|
|
|
|
painter->drawImage(drr, renderer->image(SceneQPainterDecorationRenderer::DecorationPart::Right));
|
|
|
|
painter->drawImage(dbr, renderer->image(SceneQPainterDecorationRenderer::DecorationPart::Bottom));
|
2013-06-21 08:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowPixmap *SceneQPainter::Window::createWindowPixmap()
|
|
|
|
{
|
|
|
|
return new QPainterWindowPixmap(this);
|
|
|
|
}
|
|
|
|
|
2014-07-22 11:11:19 +00:00
|
|
|
Decoration::Renderer *SceneQPainter::createDecorationRenderer(Decoration::DecoratedClientImpl *impl)
|
|
|
|
{
|
|
|
|
return new SceneQPainterDecorationRenderer(impl);
|
|
|
|
}
|
|
|
|
|
2013-06-21 08:03:31 +00:00
|
|
|
//****************************************
|
|
|
|
// QPainterWindowPixmap
|
|
|
|
//****************************************
|
|
|
|
QPainterWindowPixmap::QPainterWindowPixmap(Scene::Window *window)
|
|
|
|
: WindowPixmap(window)
|
2015-02-09 17:56:05 +00:00
|
|
|
, m_shm(kwinApp()->shouldUseWaylandForCompositing() ? nullptr : new Xcb::Shm)
|
2013-06-21 08:03:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterWindowPixmap::~QPainterWindowPixmap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPainterWindowPixmap::create()
|
|
|
|
{
|
2015-02-09 17:56:05 +00:00
|
|
|
if (isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!kwinApp()->shouldUseWaylandForCompositing() && !m_shm->isValid()) {
|
2013-06-21 08:03:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
KWin::WindowPixmap::create();
|
|
|
|
if (!isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-09 17:56:05 +00:00
|
|
|
#if HAVE_WAYLAND
|
|
|
|
if (kwinApp()->shouldUseWaylandForCompositing()) {
|
|
|
|
// performing deep copy, this could probably be improved
|
|
|
|
m_image = buffer()->data().copy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-06-21 08:03:31 +00:00
|
|
|
m_image = QImage((uchar*)m_shm->buffer(), size().width(), size().height(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QPainterWindowPixmap::update(const QRegion &damage)
|
|
|
|
{
|
2015-02-09 17:56:05 +00:00
|
|
|
#if HAVE_WAYLAND
|
|
|
|
if (kwinApp()->shouldUseWaylandForCompositing()) {
|
|
|
|
const auto oldBuffer = buffer();
|
|
|
|
updateBuffer();
|
|
|
|
const auto &b = buffer();
|
|
|
|
if (b == oldBuffer || b.isNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QPainter p(&m_image);
|
|
|
|
const QImage &data = b->data();
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
for (const QRect &rect : damage.rects()) {
|
|
|
|
p.drawImage(rect, data, rect);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-21 08:03:31 +00:00
|
|
|
if (!m_shm->isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: optimize by only updating the damaged areas
|
|
|
|
xcb_shm_get_image_cookie_t cookie = xcb_shm_get_image_unchecked(connection(), pixmap(),
|
|
|
|
0, 0, size().width(), size().height(),
|
|
|
|
~0, XCB_IMAGE_FORMAT_Z_PIXMAP, m_shm->segment(), 0);
|
|
|
|
|
|
|
|
ScopedCPointer<xcb_shm_get_image_reply_t> image(xcb_shm_get_image_reply(connection(), cookie, NULL));
|
|
|
|
if (image.isNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterEffectFrame::QPainterEffectFrame(EffectFrameImpl *frame, SceneQPainter *scene)
|
|
|
|
: Scene::EffectFrame(frame)
|
|
|
|
, m_scene(scene)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterEffectFrame::~QPainterEffectFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QPainterEffectFrame::render(QRegion region, double opacity, double frameOpacity)
|
|
|
|
{
|
|
|
|
Q_UNUSED(region)
|
|
|
|
Q_UNUSED(opacity)
|
|
|
|
// TODO: adjust opacity
|
|
|
|
if (m_effectFrame->geometry().isEmpty()) {
|
|
|
|
return; // Nothing to display
|
|
|
|
}
|
|
|
|
QPainter *painter = m_scene->painter();
|
|
|
|
|
|
|
|
|
|
|
|
// Render the actual frame
|
|
|
|
if (m_effectFrame->style() == EffectFrameUnstyled) {
|
|
|
|
painter->save();
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
QColor color(Qt::black);
|
|
|
|
color.setAlphaF(frameOpacity);
|
|
|
|
painter->setBrush(color);
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter->drawRoundedRect(m_effectFrame->geometry().adjusted(-5, -5, 5, 5), 5.0, 5.0);
|
|
|
|
painter->restore();
|
|
|
|
} else if (m_effectFrame->style() == EffectFrameStyled) {
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
m_effectFrame->frame().getMargins(left, top, right, bottom); // m_geometry is the inner geometry
|
|
|
|
QRect geom = m_effectFrame->geometry().adjusted(-left, -top, right, bottom);
|
|
|
|
painter->drawPixmap(geom, m_effectFrame->frame().framePixmap());
|
|
|
|
}
|
|
|
|
if (!m_effectFrame->selection().isNull()) {
|
|
|
|
painter->drawPixmap(m_effectFrame->selection(), m_effectFrame->selectionFrame().framePixmap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render icon
|
|
|
|
if (!m_effectFrame->icon().isNull() && !m_effectFrame->iconSize().isEmpty()) {
|
|
|
|
const QPoint topLeft(m_effectFrame->geometry().x(),
|
|
|
|
m_effectFrame->geometry().center().y() - m_effectFrame->iconSize().height() / 2);
|
|
|
|
|
|
|
|
const QRect geom = QRect(topLeft, m_effectFrame->iconSize());
|
|
|
|
painter->drawPixmap(geom, m_effectFrame->icon().pixmap(m_effectFrame->iconSize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render text
|
|
|
|
if (!m_effectFrame->text().isEmpty()) {
|
|
|
|
// Determine position on texture to paint text
|
|
|
|
QRect rect(QPoint(0, 0), m_effectFrame->geometry().size());
|
|
|
|
if (!m_effectFrame->icon().isNull() && !m_effectFrame->iconSize().isEmpty()) {
|
|
|
|
rect.setLeft(m_effectFrame->iconSize().width());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If static size elide text as required
|
|
|
|
QString text = m_effectFrame->text();
|
|
|
|
if (m_effectFrame->isStatic()) {
|
|
|
|
QFontMetrics metrics(m_effectFrame->text());
|
|
|
|
text = metrics.elidedText(text, Qt::ElideRight, rect.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
painter->setFont(m_effectFrame->font());
|
|
|
|
if (m_effectFrame->style() == EffectFrameStyled) {
|
|
|
|
painter->setPen(m_effectFrame->styledTextColor());
|
|
|
|
} else {
|
|
|
|
// TODO: What about no frame? Custom color setting required
|
|
|
|
painter->setPen(Qt::white);
|
|
|
|
}
|
|
|
|
painter->drawText(rect.translated(m_effectFrame->geometry().topLeft()), m_effectFrame->alignment(), text);
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//****************************************
|
|
|
|
// QPainterShadow
|
|
|
|
//****************************************
|
|
|
|
SceneQPainterShadow::SceneQPainterShadow(Toplevel* toplevel)
|
|
|
|
: Shadow(toplevel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SceneQPainterShadow::~SceneQPainterShadow()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SceneQPainterShadow::prepareBackend()
|
|
|
|
{
|
2014-07-24 06:39:25 +00:00
|
|
|
if (hasDecorationShadow()) {
|
|
|
|
// TODO: implement for QPainter
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-21 08:03:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-22 11:11:19 +00:00
|
|
|
//****************************************
|
|
|
|
// QPainterDecorationRenderer
|
|
|
|
//****************************************
|
|
|
|
SceneQPainterDecorationRenderer::SceneQPainterDecorationRenderer(Decoration::DecoratedClientImpl *client)
|
|
|
|
: Renderer(client)
|
|
|
|
{
|
|
|
|
connect(this, &Renderer::renderScheduled, client->client(), static_cast<void (Client::*)(const QRect&)>(&Client::addRepaint));
|
|
|
|
}
|
|
|
|
|
|
|
|
SceneQPainterDecorationRenderer::~SceneQPainterDecorationRenderer() = default;
|
|
|
|
|
|
|
|
QImage SceneQPainterDecorationRenderer::image(SceneQPainterDecorationRenderer::DecorationPart part) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(part != DecorationPart::Count);
|
|
|
|
return m_images[int(part)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneQPainterDecorationRenderer::render()
|
|
|
|
{
|
|
|
|
const QRegion scheduled = getScheduled();
|
|
|
|
if (scheduled.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (areImageSizesDirty()) {
|
|
|
|
resizeImages();
|
|
|
|
resetImageSizesDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
const QRect top(QPoint(0, 0), m_images[int(DecorationPart::Top)].size());
|
|
|
|
const QRect left(QPoint(0, top.height()), m_images[int(DecorationPart::Left)].size());
|
|
|
|
const QRect right(QPoint(top.width() - m_images[int(DecorationPart::Right)].size().width(), top.height()), m_images[int(DecorationPart::Right)].size());
|
|
|
|
const QRect bottom(QPoint(0, left.y() + left.height()), m_images[int(DecorationPart::Bottom)].size());
|
|
|
|
|
|
|
|
const QRect geometry = scheduled.boundingRect();
|
|
|
|
auto renderPart = [this](const QRect &rect, const QRect &partRect, int index) {
|
|
|
|
if (rect.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPainter painter(&m_images[index]);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setWindow(partRect);
|
|
|
|
painter.setClipRect(rect);
|
|
|
|
painter.save();
|
|
|
|
// clear existing part
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
painter.fillRect(rect, Qt::red);
|
|
|
|
painter.restore();
|
2014-11-11 08:39:45 +00:00
|
|
|
client()->decoration()->paint(&painter, rect);
|
2014-07-22 11:11:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
renderPart(left.intersected(geometry), left, int(DecorationPart::Left));
|
|
|
|
renderPart(top.intersected(geometry), top, int(DecorationPart::Top));
|
|
|
|
renderPart(right.intersected(geometry), right, int(DecorationPart::Right));
|
|
|
|
renderPart(bottom.intersected(geometry), bottom, int(DecorationPart::Bottom));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneQPainterDecorationRenderer::resizeImages()
|
|
|
|
{
|
|
|
|
QRect left, top, right, bottom;
|
2014-07-25 10:55:28 +00:00
|
|
|
client()->client()->layoutDecorationRects(left, top, right, bottom);
|
2014-07-22 11:11:19 +00:00
|
|
|
|
|
|
|
auto checkAndCreate = [this](int index, const QSize &size) {
|
|
|
|
if (m_images[index].size() != size) {
|
|
|
|
m_images[index] = QImage(size, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
m_images[index].fill(Qt::transparent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
checkAndCreate(int(DecorationPart::Left), left.size());
|
|
|
|
checkAndCreate(int(DecorationPart::Right), right.size());
|
|
|
|
checkAndCreate(int(DecorationPart::Top), top.size());
|
|
|
|
checkAndCreate(int(DecorationPart::Bottom), bottom.size());
|
|
|
|
}
|
|
|
|
|
2014-07-23 07:20:28 +00:00
|
|
|
void SceneQPainterDecorationRenderer::reparent(Deleted *deleted)
|
|
|
|
{
|
|
|
|
render();
|
|
|
|
Renderer::reparent(deleted);
|
|
|
|
}
|
|
|
|
|
2013-06-21 08:03:31 +00:00
|
|
|
} // KWin
|