kwin/paintredirector.cpp

516 lines
15 KiB
C++
Raw Normal View History

/*****************************************************************
This file is part of the KDE project.
Copyright (C) 2009 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2012 Martin Gräßlin <mgraesslin@kde.org>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
******************************************************************/
#include "paintredirector.h"
#include "client.h"
#include "deleted.h"
#include "effects.h"
#include <kwinglplatform.h>
#include <kwinglutils.h>
#include <kwinxrenderutils.h>
#include <QPaintEngine>
#include <qevent.h>
#include <qpainter.h>
#include <qmath.h>
namespace KWin
{
PaintRedirector *PaintRedirector::create(Client *c, KDecoration *deco)
{
if (effects->isOpenGLCompositing()) {
return new OpenGLPaintRedirector(c, deco);
} else if (effects->compositingType() == XRenderCompositing) {
return new RasterXRenderPaintRedirector(c, deco);
} else if (effects->compositingType() == QPainterCompositing) {
return new QImagePaintRedirector(c, deco);
}
return NULL;
}
PaintRedirector::PaintRedirector(Client *c, KDecoration *deco)
: QObject(deco)
, widget(deco->widget())
2011-01-30 14:34:42 +00:00
, recursionCheck(false)
, m_client(c)
, m_decoration(deco)
, m_requiresRepaint(false)
2011-01-30 14:34:42 +00:00
{
added(deco->widget());
}
PaintRedirector::~PaintRedirector()
{
}
void PaintRedirector::reparent(Deleted *d)
{
setParent(d);
widget = NULL;
m_client = NULL;
m_decoration = NULL;
2011-01-30 14:34:42 +00:00
}
static int align(int value, int align)
{
return (value + align - 1) & ~(align - 1);
}
void PaintRedirector::performPendingPaint()
2011-01-30 14:34:42 +00:00
{
if (!widget && !m_decoration->window()) {
return;
}
//qDebug() << "### performing paint, pending:" << pending.boundingRect();
const QSize size = pending.boundingRect().size();
QPaintDevice *scratch = this->scratch();
if (scratch->width() < size.width() || scratch->height() < size.height()) {
int w = align(size.width(), 128);
int h = align(size.height(), 128);
scratch = recreateScratch(QSize(qMax(scratch->width(), w), qMax(scratch->height(), h)));
2011-01-30 14:34:42 +00:00
}
fillScratch(Qt::transparent);
recursionCheck = true;
m_decoration->render(scratch, pending.boundingRect());
recursionCheck = false;
cleanupTimer.start(2000, this);
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
bool PaintRedirector::isToolTip(QWidget *object) const
{
// ### We need a more reliable way of doing this
return object->windowFlags() & Qt::ToolTip;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
bool PaintRedirector::eventFilter(QObject* o, QEvent* e)
{
if (!widget || !m_client) {
return false;
}
2011-01-30 14:34:42 +00:00
switch(e->type()) {
case QEvent::ChildAdded: {
QChildEvent* c = static_cast< QChildEvent* >(e);
if (c->child()->isWidgetType() && !isToolTip(static_cast< QWidget* >(c->child())))
added(static_cast< QWidget* >(c->child()));
break;
}
case QEvent::ChildRemoved: {
QChildEvent* c = static_cast< QChildEvent* >(e);
if (c->child()->isWidgetType())
removed(static_cast< QWidget* >(c->child()));
break;
}
case QEvent::Paint: {
if (!recursionCheck) {
QPaintEvent* pe = static_cast< QPaintEvent* >(e);
QWidget* w = static_cast< QWidget* >(o);
pending |= pe->region().translated(w->mapTo(widget, QPoint(0, 0)));
scheduled = pending;
// schedule repaint
const int paddingLeft = m_client->paddingLeft();
const int paddingTop = m_client->paddingTop();
const bool needsTranslate = (paddingLeft != 0 || paddingTop != 0);
m_client->addRepaint(needsTranslate ? pending.translated(-paddingLeft, -paddingTop) : pending);
m_requiresRepaint = true;
2011-01-30 14:34:42 +00:00
return true; // filter out
}
}
2011-01-30 14:34:42 +00:00
default:
break;
}
return false;
}
void PaintRedirector::addRepaint(const QRegion &region)
{
pending |= region;
scheduled = pending;
// schedule repaint
const int paddingLeft = m_client->paddingLeft();
const int paddingTop = m_client->paddingTop();
const bool needsTranslate = (paddingLeft != 0 || paddingTop != 0);
m_client->addRepaint(needsTranslate ? pending.translated(-paddingLeft, -paddingTop) : pending);
m_requiresRepaint = true;
}
QRegion PaintRedirector::pendingRegion() const
2011-01-30 14:34:42 +00:00
{
return pending;
2011-01-30 14:34:42 +00:00
}
QRegion PaintRedirector::scheduledRepaintRegion()
2011-01-30 14:34:42 +00:00
{
QRegion tempRegion = scheduled;
scheduled = QRegion();
return tempRegion;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void PaintRedirector::added(QWidget* w)
{
if (!w) {
return;
}
2011-01-30 14:34:42 +00:00
w->installEventFilter(this);
foreach (QObject * o, w->children()) {
if (o->isWidgetType() && !isToolTip(static_cast< QWidget* >(o)))
added(static_cast< QWidget* >(o));
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void PaintRedirector::removed(QWidget* w)
{
foreach (QObject * o, w->children()) {
if (o->isWidgetType())
removed(static_cast< QWidget* >(o));
}
2011-01-30 14:34:42 +00:00
w->installEventFilter(this);
}
2011-01-30 14:34:42 +00:00
void PaintRedirector::timerEvent(QTimerEvent* event)
{
if (event->timerId() == cleanupTimer.timerId()) {
cleanupTimer.stop();
discardScratch();
}
2011-01-30 14:34:42 +00:00
}
void PaintRedirector::ensurePixmapsPainted()
{
if (pending.isEmpty() || !m_client)
return;
performPendingPaint();
QRect rects[PixmapCount];
m_client->layoutDecorationRects(rects[LeftPixmap], rects[TopPixmap], rects[RightPixmap], rects[BottomPixmap], Client::DecorationRelative);
updatePixmaps(rects, pending);
pending = QRegion();
scheduled = QRegion();
xcb_flush(connection());
}
void PaintRedirector::updatePixmaps(const QRect *rects, const QRegion &region)
{
for (int i = 0; i < PixmapCount; ++i) {
if (!rects[i].isValid())
continue;
const QRect bounding = region.boundingRect();
const QRegion reg = region & rects[i];
if (reg.isEmpty())
continue;
paint(DecorationPixmap(i), rects[i], bounding, reg);
}
}
void PaintRedirector::preparePaint(const QPixmap &pending)
{
Q_UNUSED(pending)
}
void PaintRedirector::resizePixmaps()
{
QRect rects[PixmapCount];
m_client->layoutDecorationRects(rects[LeftPixmap], rects[TopPixmap], rects[RightPixmap], rects[BottomPixmap], Client::DecorationRelative);
resizePixmaps(rects);
// repaint
if (widget) {
widget->update();
}
}
void PaintRedirector::resizePixmaps(const QRect *rects)
{
for (int i = 0; i < PixmapCount; ++i) {
resize(DecorationPixmap(i), rects[i].size());
}
}
GLTexture *PaintRedirector::texture(PaintRedirector::DecorationPixmap border) const
{
Q_UNUSED(border)
return NULL;
}
xcb_render_picture_t PaintRedirector::picture(PaintRedirector::DecorationPixmap border) const
{
Q_UNUSED(border)
return XCB_RENDER_PICTURE_NONE;
}
const QImage *PaintRedirector::image(PaintRedirector::DecorationPixmap border) const
{
Q_UNUSED(border)
return NULL;
}
void PaintRedirector::resize(DecorationPixmap border, const QSize &size)
{
Q_UNUSED(border)
Q_UNUSED(size)
}
void PaintRedirector::paint(DecorationPixmap border, const QRect& r, const QRect &b, const QRegion &reg)
{
Q_UNUSED(border)
Q_UNUSED(r)
Q_UNUSED(b)
Q_UNUSED(reg)
}
// ------------------------------------------------------------------
ImageBasedPaintRedirector::ImageBasedPaintRedirector(Client *c, KDecoration *deco)
: PaintRedirector(c, deco)
{
}
ImageBasedPaintRedirector::~ImageBasedPaintRedirector()
{
}
QPaintDevice *ImageBasedPaintRedirector::recreateScratch(const QSize &size)
{
m_scratchImage = QImage(size, QImage::Format_ARGB32_Premultiplied);
return &m_scratchImage;
}
QPaintDevice *ImageBasedPaintRedirector::scratch()
{
return &m_scratchImage;
}
void ImageBasedPaintRedirector::fillScratch(Qt::GlobalColor color)
{
m_scratchImage.fill(color);
}
void ImageBasedPaintRedirector::discardScratch()
{
m_scratchImage = QImage();
}
// ------------------------------------------------------------------
OpenGLPaintRedirector::OpenGLPaintRedirector(Client *c, KDecoration *deco)
: ImageBasedPaintRedirector(c, deco)
{
for (int i = 0; i < TextureCount; ++i)
m_textures[i] = NULL;
PaintRedirector::resizePixmaps();
}
OpenGLPaintRedirector::~OpenGLPaintRedirector()
{
for (int i = 0; i < TextureCount; ++i)
delete m_textures[i];
}
void OpenGLPaintRedirector::resizePixmaps(const QRect *rects)
{
QSize size[2];
size[LeftRight] = QSize(rects[LeftPixmap].width() + rects[RightPixmap].width(),
align(qMax(rects[LeftPixmap].height(), rects[RightPixmap].height()), 128));
size[TopBottom] = QSize(align(qMax(rects[TopPixmap].width(), rects[BottomPixmap].width()), 128),
rects[TopPixmap].height() + rects[BottomPixmap].height());
Better handling for making the compositing OpenGL context current With QtQuick2 it's possible that the scene graph rendering context either lives in an own thread or uses the main GUI thread. In the latter case it's the same thread as our compositing OpenGL context lives in. This means our basic assumption that between two rendering passes the context stays current does not hold. The code already ensured that before we start a rendering pass the context is made current, but there are many more possible cases. If we use OpenGL in areas not triggered by the rendering loop but in response to other events the context needs to be made current. This includes the loading and unloading of effects (some effects use OpenGL in the static effect check, in the ctor and dtor), background loading of texture data, lazy loading after first usage invoked by shortcut, etc. etc. To properly handle these cases new methods are added to EffectsHandler to make the compositing OpenGL context current. These calls delegate down into the scene. On non-OpenGL scenes they are noop, but on OpenGL they go into the backend and make the context current. In addition they ensure that Qt doesn't think that it's QOpenGLContext is current by calling doneCurrent() on the QOpenGLContext::currentContext(). This unfortunately causes an additional call to makeCurrent with a null context, but there is no other way to tell Qt - it doesn't notice when a different context is made current with low level API calls. In the multi-threaded architecture this doesn't matter as ::currentContext() returns null. A short evaluation showed that a transition to QOpenGLContext doesn't seem feasible. Qt only supports either GLX or EGL while KWin supports both and when entering the transition phase for Wayland, it would become extremely tricky if our native platform is X11, but we want a Wayland EGL context. A future solution might be to have a "KWin-QPA plugin" which uses either xcb or Wayland and hides everything from Qt. The API documentation is extended to describe when the effects-framework ensures that an OpenGL context is current. The effects are changed to make the context current in cases where it's not guaranteed. This has been done by looking for creation or deletion of GLTextures and Shaders. If there are other OpenGL usages outside the rendering loop, ctor/dtor this needs to be changed, too.
2013-11-22 14:05:36 +00:00
effects->makeOpenGLContextCurrent();
if (!GLTexture::NPOTTextureSupported()) {
for (int i = 0; i < 2; i++) {
size[i].rwidth() = nearestPowerOfTwo(size[i].width());
size[i].rheight() = nearestPowerOfTwo(size[i].height());
}
}
for (int i = 0; i < 2; i++) {
if (m_textures[i] && m_textures[i]->size() == size[i])
continue;
delete m_textures[i];
m_textures[i] = NULL;
if (size[i].isEmpty())
continue;
m_textures[i] = new GLTexture(size[i].width(), size[i].height());
m_textures[i]->setYInverted(true);
m_textures[i]->setWrapMode(GL_CLAMP_TO_EDGE);
m_textures[i]->clear();
}
}
void OpenGLPaintRedirector::updatePixmaps(const QRect *rects, const QRegion &region)
{
const QImage &image = scratchImage();
const QRect bounding = region.boundingRect();
const int leftWidth = rects[LeftPixmap].width();
const int topHeight = rects[TopPixmap].height();
// Top, Right, Bottom, Left
GLTexture *textures[4] = { m_textures[TopBottom], m_textures[LeftRight], m_textures[TopBottom], m_textures[LeftRight] };
QPoint offsets[4] = { QPoint(0, 0), QPoint(leftWidth, 0), QPoint(0, topHeight), QPoint(0, 0) };
for (int i = 0; i < 4; i++) {
const QRect dirty = (region & rects[i]).boundingRect();
if (!textures[i] || dirty.isEmpty())
continue;
const QPoint dst = dirty.topLeft() - rects[i].topLeft() + offsets[i];
const QRect src(dirty.topLeft() - bounding.topLeft(), dirty.size());
textures[i]->update(image, dst, src);
}
}
// ------------------------------------------------------------------
RasterXRenderPaintRedirector::RasterXRenderPaintRedirector(Client *c, KDecoration *deco)
: ImageBasedPaintRedirector(c, deco)
, m_gc(0)
{
for (int i=0; i<PixmapCount; ++i) {
m_pixmaps[i] = XCB_PIXMAP_NONE;
m_pictures[i] = NULL;
}
resizePixmaps();
}
RasterXRenderPaintRedirector::~RasterXRenderPaintRedirector()
{
for (int i=0; i<PixmapCount; ++i) {
if (m_pixmaps[i] != XCB_PIXMAP_NONE) {
xcb_free_pixmap(connection(), m_pixmaps[i]);
}
delete m_pictures[i];
}
if (m_gc != 0) {
xcb_free_gc(connection(), m_gc);
}
}
xcb_render_picture_t RasterXRenderPaintRedirector::picture(PaintRedirector::DecorationPixmap border) const
{
return *m_pictures[border];
}
void RasterXRenderPaintRedirector::resize(PaintRedirector::DecorationPixmap border, const QSize &size)
{
if (m_sizes[border] != size) {
if (m_pixmaps[border] != XCB_PIXMAP_NONE) {
xcb_free_pixmap(connection(), m_pixmaps[border]);
}
m_pixmaps[border] = xcb_generate_id(connection());
xcb_create_pixmap(connection(), 32, m_pixmaps[border], rootWindow(), size.width(), size.height());
delete m_pictures[border];
m_pictures[border] = new XRenderPicture(m_pixmaps[border], 32);
}
// fill transparent
xcb_rectangle_t rect = {0, 0, uint16_t(size.width()), uint16_t(size.height())};
xcb_render_fill_rectangles(connection(), XCB_RENDER_PICT_OP_SRC, *m_pictures[border], preMultiply(Qt::transparent), 1, &rect);
}
void RasterXRenderPaintRedirector::preparePaint(const QPixmap &pending)
{
m_tempImage = pending.toImage();
}
void RasterXRenderPaintRedirector::paint(PaintRedirector::DecorationPixmap border, const QRect &r, const QRect &b, const QRegion &reg)
{
// clip the sub area
const QRect bounding = reg.boundingRect();
const QPoint offset = bounding.topLeft() - r.topLeft();
if (m_gc == 0) {
m_gc = xcb_generate_id(connection());
xcb_create_gc(connection(), m_gc, m_pixmaps[border], 0, NULL);
}
const QImage img(scratchImage().copy(QRect(bounding.topLeft() - b.topLeft(), bounding.size())));
xcb_put_image(connection(), XCB_IMAGE_FORMAT_Z_PIXMAP, m_pixmaps[border], m_gc,
img.width(), img.height(), offset.x(), offset.y(), 0, 32, img.byteCount(), img.constBits());
}
QImagePaintRedirector::QImagePaintRedirector(Client *c, KDecoration *deco)
: ImageBasedPaintRedirector(c, deco)
{
resizePixmaps();
}
QImagePaintRedirector::~QImagePaintRedirector()
{
}
void QImagePaintRedirector::paint(PaintRedirector::DecorationPixmap border, const QRect &r, const QRect &b, const QRegion &reg)
{
// clip the sub area
const QRect bounding = reg.boundingRect();
const QPoint offset = bounding.topLeft() - r.topLeft();
QPainter painter(&m_images[border]);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.drawImage(offset, scratchImage(), QRect(bounding.topLeft() - b.topLeft(), bounding.size()));
}
void QImagePaintRedirector::resize(PaintRedirector::DecorationPixmap border, const QSize &size)
{
m_images[border] = QImage(size, QImage::Format_ARGB32_Premultiplied);
m_images[border].fill(Qt::transparent);
}
const QImage *QImagePaintRedirector::image(PaintRedirector::DecorationPixmap border) const
{
return &m_images[border];
}
} // namespace
#include "paintredirector.moc"