2008-05-07 14:43:13 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2008 Lubos Lunak <l.lunak@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 "kwinxrenderutils.h"
|
2015-07-31 10:07:53 +00:00
|
|
|
#include "logging_p.h"
|
2008-05-07 14:43:13 +00:00
|
|
|
|
2012-03-04 08:52:32 +00:00
|
|
|
#include <QStack>
|
2008-06-15 18:49:48 +00:00
|
|
|
#include <QPixmap>
|
2014-08-28 18:19:26 +00:00
|
|
|
#include <QGlobalStatic>
|
2008-05-07 14:43:13 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2014-03-16 14:06:04 +00:00
|
|
|
namespace XRenderUtils
|
|
|
|
{
|
|
|
|
static xcb_connection_t *s_connection = nullptr;
|
|
|
|
static xcb_window_t s_rootWindow = XCB_WINDOW_NONE;
|
|
|
|
|
|
|
|
void init(xcb_connection_t *connection, xcb_window_t rootWindow)
|
|
|
|
{
|
|
|
|
s_connection = connection;
|
|
|
|
s_rootWindow = rootWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2008-06-06 09:11:52 +00:00
|
|
|
// adapted from Qt, because this really sucks ;)
|
2013-02-12 09:57:27 +00:00
|
|
|
xcb_render_color_t preMultiply(const QColor &c, float opacity)
|
2008-06-06 09:11:52 +00:00
|
|
|
{
|
2013-02-12 09:57:27 +00:00
|
|
|
xcb_render_color_t color;
|
2008-06-06 09:11:52 +00:00
|
|
|
const uint A = c.alpha() * opacity,
|
|
|
|
R = c.red(),
|
|
|
|
G = c.green(),
|
|
|
|
B = c.blue();
|
|
|
|
color.alpha = (A | A << 8);
|
|
|
|
color.red = (R | R << 8) * color.alpha / 0x10000;
|
|
|
|
color.green = (G | G << 8) * color.alpha / 0x10000;
|
|
|
|
color.blue = (B | B << 8) * color.alpha / 0x10000;
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2013-02-12 09:57:27 +00:00
|
|
|
XRenderPicture xRenderFill(const xcb_render_color_t &c)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_pixmap_t pixmap = xcb_generate_id(XRenderUtils::s_connection);
|
|
|
|
xcb_create_pixmap(XRenderUtils::s_connection, 32, pixmap, XRenderUtils::s_rootWindow, 1, 1);
|
2011-01-30 14:34:42 +00:00
|
|
|
XRenderPicture fill(pixmap, 32);
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_free_pixmap(XRenderUtils::s_connection, pixmap);
|
2013-02-12 09:57:27 +00:00
|
|
|
|
|
|
|
uint32_t values[] = {true};
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_render_change_picture(XRenderUtils::s_connection, fill, XCB_RENDER_CP_REPEAT, values);
|
2013-02-12 09:57:27 +00:00
|
|
|
|
|
|
|
xcb_rectangle_t rect = {0, 0, 1, 1};
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_render_fill_rectangles(XRenderUtils::s_connection, XCB_RENDER_PICT_OP_SRC, fill, c, 1, &rect);
|
2008-06-06 09:11:52 +00:00
|
|
|
return fill;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-06-06 09:11:52 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
XRenderPicture xRenderFill(const QColor &c)
|
|
|
|
{
|
2013-02-12 09:57:27 +00:00
|
|
|
return xRenderFill(preMultiply(c));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-06-06 09:11:52 +00:00
|
|
|
|
2009-05-12 14:12:46 +00:00
|
|
|
XRenderPicture xRenderBlendPicture(double opacity)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-02-12 09:57:27 +00:00
|
|
|
static XRenderPicture s_blendPicture(XCB_RENDER_PICTURE_NONE);
|
|
|
|
static xcb_render_color_t s_blendColor = {0, 0, 0, 0};
|
|
|
|
s_blendColor.alpha = uint16_t(opacity * 0xffff);
|
|
|
|
if (s_blendPicture == XCB_RENDER_PICTURE_NONE) {
|
|
|
|
s_blendPicture = xRenderFill(s_blendColor);
|
|
|
|
} else {
|
|
|
|
xcb_rectangle_t rect = {0, 0, 1, 1};
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_render_fill_rectangles(XRenderUtils::s_connection, XCB_RENDER_PICT_OP_SRC, s_blendPicture, s_blendColor, 1, &rect);
|
2013-02-12 09:57:27 +00:00
|
|
|
}
|
|
|
|
return s_blendPicture;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-05-12 14:12:46 +00:00
|
|
|
|
2013-02-12 08:16:42 +00:00
|
|
|
static xcb_render_picture_t createPicture(xcb_pixmap_t pix, int depth)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-02-12 08:16:42 +00:00
|
|
|
if (pix == XCB_PIXMAP_NONE)
|
|
|
|
return XCB_RENDER_PICTURE_NONE;
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_connection_t *c = XRenderUtils::s_connection;
|
2013-02-12 08:54:25 +00:00
|
|
|
static QHash<int, xcb_render_pictformat_t> s_renderFormats;
|
|
|
|
if (!s_renderFormats.contains(depth)) {
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_render_query_pict_formats_reply_t *formats = xcb_render_query_pict_formats_reply(c, xcb_render_query_pict_formats_unchecked(c), nullptr);
|
2013-02-12 08:54:25 +00:00
|
|
|
if (!formats) {
|
|
|
|
return XCB_RENDER_PICTURE_NONE;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2013-02-12 08:54:25 +00:00
|
|
|
for (xcb_render_pictforminfo_iterator_t it = xcb_render_query_pict_formats_formats_iterator(formats);
|
|
|
|
it.rem;
|
|
|
|
xcb_render_pictforminfo_next(&it)) {
|
|
|
|
if (it.data->depth == depth) {
|
|
|
|
s_renderFormats.insert(depth, it.data->id);
|
|
|
|
break;
|
|
|
|
}
|
2008-05-08 10:08:10 +00:00
|
|
|
}
|
2013-02-12 08:54:25 +00:00
|
|
|
free(formats);
|
|
|
|
}
|
|
|
|
QHash<int, xcb_render_pictformat_t>::const_iterator it = s_renderFormats.constFind(depth);
|
|
|
|
if (it == s_renderFormats.constEnd()) {
|
2015-07-31 10:07:53 +00:00
|
|
|
qCWarning(LIBKWINXRENDERUTILS) << "Could not find XRender format for depth" << depth;
|
2013-02-12 08:54:25 +00:00
|
|
|
return XCB_RENDER_PICTURE_NONE;
|
2008-05-08 10:08:10 +00:00
|
|
|
}
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_render_picture_t pic = xcb_generate_id(c);
|
|
|
|
xcb_render_create_picture(c, pic, pix, it.value(), 0, nullptr);
|
2013-02-12 08:54:25 +00:00
|
|
|
return pic;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-05-08 10:08:10 +00:00
|
|
|
|
2013-08-04 12:50:25 +00:00
|
|
|
XRenderPicture::XRenderPicture(const QImage &img)
|
2013-08-25 14:17:20 +00:00
|
|
|
{
|
|
|
|
fromImage(img);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XRenderPicture::fromImage(const QImage &img)
|
2013-08-04 12:50:25 +00:00
|
|
|
{
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_connection_t *c = XRenderUtils::s_connection;
|
2013-02-12 07:16:27 +00:00
|
|
|
const int depth = img.depth();
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_pixmap_t xpix = xcb_generate_id(c);
|
|
|
|
xcb_create_pixmap(c, depth, xpix, XRenderUtils::s_rootWindow, img.width(), img.height());
|
2013-02-12 07:16:27 +00:00
|
|
|
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_gcontext_t cid = xcb_generate_id(c);
|
|
|
|
xcb_create_gc(c, cid, xpix, 0, nullptr);
|
|
|
|
xcb_put_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, xpix, cid, img.width(), img.height(),
|
2013-02-12 07:16:27 +00:00
|
|
|
0, 0, 0, depth, img.byteCount(), img.constBits());
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_free_gc(c, cid);
|
2013-02-12 07:16:27 +00:00
|
|
|
|
|
|
|
d = new XRenderPictureData(createPicture(xpix, depth));
|
2014-03-16 14:06:04 +00:00
|
|
|
xcb_free_pixmap(c, xpix);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-05-08 10:08:10 +00:00
|
|
|
|
2013-02-12 08:16:42 +00:00
|
|
|
XRenderPicture::XRenderPicture(xcb_pixmap_t pix, int depth)
|
2011-01-30 14:34:42 +00:00
|
|
|
: d(new XRenderPictureData(createPicture(pix, depth)))
|
|
|
|
{
|
|
|
|
}
|
2008-05-08 10:08:10 +00:00
|
|
|
|
2014-03-16 14:06:04 +00:00
|
|
|
XRenderPictureData::~XRenderPictureData()
|
|
|
|
{
|
|
|
|
if (picture != XCB_RENDER_PICTURE_NONE)
|
|
|
|
xcb_render_free_picture(XRenderUtils::s_connection, picture);
|
|
|
|
}
|
|
|
|
|
|
|
|
XFixesRegion::XFixesRegion(const QRegion ®ion)
|
|
|
|
{
|
|
|
|
m_region = xcb_generate_id(XRenderUtils::s_connection);
|
|
|
|
QVector< QRect > rects = region.rects();
|
|
|
|
QVector< xcb_rectangle_t > xrects(rects.count());
|
|
|
|
for (int i = 0;
|
|
|
|
i < rects.count();
|
|
|
|
++i) {
|
|
|
|
const QRect &rect = rects.at(i);
|
|
|
|
xcb_rectangle_t xrect;
|
|
|
|
xrect.x = rect.x();
|
|
|
|
xrect.y = rect.y();
|
|
|
|
xrect.width = rect.width();
|
|
|
|
xrect.height = rect.height();
|
|
|
|
xrects[i] = xrect;
|
|
|
|
}
|
|
|
|
xcb_xfixes_create_region(XRenderUtils::s_connection, m_region, xrects.count(), xrects.constData());
|
|
|
|
}
|
|
|
|
|
|
|
|
XFixesRegion::~XFixesRegion()
|
|
|
|
{
|
|
|
|
xcb_xfixes_destroy_region(XRenderUtils::s_connection, m_region);
|
|
|
|
}
|
|
|
|
|
2013-02-11 20:01:41 +00:00
|
|
|
static xcb_render_picture_t s_offscreenTarget = XCB_RENDER_PICTURE_NONE;
|
2012-03-04 08:52:32 +00:00
|
|
|
static QStack<XRenderPicture*> s_scene_offscreenTargetStack;
|
|
|
|
static int s_renderOffscreen = 0;
|
|
|
|
|
2013-02-11 20:01:41 +00:00
|
|
|
void scene_setXRenderOffscreenTarget(xcb_render_picture_t pix)
|
2012-03-04 08:52:32 +00:00
|
|
|
{
|
|
|
|
s_offscreenTarget = pix;
|
|
|
|
}
|
|
|
|
|
|
|
|
XRenderPicture *scene_xRenderOffscreenTarget()
|
|
|
|
{
|
2014-01-08 10:43:02 +00:00
|
|
|
return s_scene_offscreenTargetStack.isEmpty() ? nullptr : s_scene_offscreenTargetStack.top();
|
2012-03-04 08:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setXRenderOffscreen(bool b)
|
|
|
|
{
|
|
|
|
b ? ++s_renderOffscreen : --s_renderOffscreen;
|
|
|
|
if (s_renderOffscreen < 0) {
|
|
|
|
s_renderOffscreen = 0;
|
2015-07-31 10:07:53 +00:00
|
|
|
qCWarning(LIBKWINXRENDERUTILS) << "*** SOMETHING IS MESSED UP WITH YOUR setXRenderOffscreen() USAGE ***";
|
2012-03-04 08:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void xRenderPushTarget(XRenderPicture *pic)
|
|
|
|
{
|
|
|
|
s_scene_offscreenTargetStack.push(pic);
|
|
|
|
++s_renderOffscreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xRenderPopTarget()
|
|
|
|
{
|
|
|
|
s_scene_offscreenTargetStack.pop();
|
|
|
|
--s_renderOffscreen;
|
|
|
|
if (s_renderOffscreen < 0) {
|
|
|
|
s_renderOffscreen = 0;
|
2015-07-31 10:07:53 +00:00
|
|
|
qCWarning(LIBKWINXRENDERUTILS) << "*** SOMETHING IS MESSED UP WITH YOUR xRenderPopTarget() USAGE ***";
|
2012-03-04 08:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool xRenderOffscreen()
|
|
|
|
{
|
|
|
|
return s_renderOffscreen;
|
|
|
|
}
|
|
|
|
|
2013-02-11 20:01:41 +00:00
|
|
|
xcb_render_picture_t xRenderOffscreenTarget()
|
2012-03-04 08:52:32 +00:00
|
|
|
{
|
|
|
|
return s_offscreenTarget;
|
|
|
|
}
|
|
|
|
|
2014-08-28 18:19:26 +00:00
|
|
|
namespace XRenderUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
struct PictFormatData
|
|
|
|
{
|
|
|
|
PictFormatData() {
|
|
|
|
// Fetch the render pict formats
|
|
|
|
reply = xcb_render_query_pict_formats_reply(s_connection,
|
|
|
|
xcb_render_query_pict_formats_unchecked(s_connection), nullptr);
|
|
|
|
|
|
|
|
// Init the visual ID -> format ID hash table
|
|
|
|
for (auto screens = xcb_render_query_pict_formats_screens_iterator(reply); screens.rem; xcb_render_pictscreen_next(&screens)) {
|
|
|
|
for (auto depths = xcb_render_pictscreen_depths_iterator(screens.data); depths.rem; xcb_render_pictdepth_next(&depths)) {
|
|
|
|
const xcb_render_pictvisual_t *visuals = xcb_render_pictdepth_visuals(depths.data);
|
|
|
|
const int len = xcb_render_pictdepth_visuals_length(depths.data);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
visualHash.insert(visuals[i].visual, visuals[i].format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the format ID -> xcb_render_directformat_t* hash table
|
|
|
|
const xcb_render_pictforminfo_t *formats = xcb_render_query_pict_formats_formats(reply);
|
|
|
|
const int len = xcb_render_query_pict_formats_formats_length(reply);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if (formats[i].type == XCB_RENDER_PICT_TYPE_DIRECT)
|
|
|
|
formatInfoHash.insert(formats[i].id, &formats[i].direct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~PictFormatData() {
|
|
|
|
free(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_render_query_pict_formats_reply_t *reply;
|
|
|
|
QHash<xcb_visualid_t, xcb_render_pictformat_t> visualHash;
|
|
|
|
QHash<xcb_render_pictformat_t, const xcb_render_directformat_t *> formatInfoHash;
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_GLOBAL_STATIC(PictFormatData, g_pictFormatData)
|
|
|
|
|
|
|
|
xcb_render_pictformat_t findPictFormat(xcb_visualid_t visual)
|
|
|
|
{
|
|
|
|
PictFormatData *d = g_pictFormatData;
|
|
|
|
return d->visualHash.value(visual);
|
|
|
|
}
|
|
|
|
|
|
|
|
const xcb_render_directformat_t *findPictFormatInfo(xcb_render_pictformat_t format)
|
|
|
|
{
|
|
|
|
PictFormatData *d = g_pictFormatData;
|
|
|
|
return d->formatInfoHash.value(format);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace XRenderUtils
|
|
|
|
|
|
|
|
} // namespace KWin
|