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"
|
2011-06-11 12:48:16 +00:00
|
|
|
#include "kwinglobals.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>
|
2008-05-08 10:08:10 +00:00
|
|
|
#include <kdebug.h>
|
2008-05-07 14:43:13 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2008-06-06 09:11:52 +00:00
|
|
|
// adapted from Qt, because this really sucks ;)
|
|
|
|
XRenderColor preMultiply(const QColor &c, float opacity)
|
|
|
|
{
|
|
|
|
XRenderColor color;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
XRenderPicture xRenderFill(const XRenderColor *xc)
|
|
|
|
{
|
|
|
|
Pixmap pixmap = XCreatePixmap(display(), rootWindow(), 1, 1, 32);
|
2008-06-06 09:11:52 +00:00
|
|
|
XRenderPictureAttributes pa; pa.repeat = True;
|
2011-01-30 14:34:42 +00:00
|
|
|
XRenderPicture fill(pixmap, 32);
|
|
|
|
XFreePixmap(display(), pixmap);
|
|
|
|
XRenderChangePicture(display(), fill, CPRepeat, &pa);
|
|
|
|
XRenderFillRectangle(display(), PictOpSrc, fill, xc, 0, 0, 1, 1);
|
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)
|
|
|
|
{
|
2008-06-06 09:11:52 +00:00
|
|
|
XRenderColor xc = preMultiply(c);
|
2011-01-30 14:34:42 +00:00
|
|
|
return xRenderFill(&xc);
|
|
|
|
}
|
2008-06-06 09:11:52 +00:00
|
|
|
|
2012-12-29 06:34:38 +00:00
|
|
|
static XRenderPicture _blendPicture(X::None);
|
2009-05-12 14:12:46 +00:00
|
|
|
static XRenderColor _blendColor;
|
|
|
|
XRenderPicture xRenderBlendPicture(double opacity)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2009-05-12 14:12:46 +00:00
|
|
|
_blendColor.alpha = ushort(opacity * 0xffff);
|
|
|
|
if (_blendPicture == X::None)
|
|
|
|
_blendPicture = xRenderFill(&_blendColor);
|
|
|
|
else
|
|
|
|
XRenderFillRectangle(display(), PictOpSrc, _blendPicture, &_blendColor, 0, 0, 1, 1);
|
|
|
|
return _blendPicture;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-05-12 14:12:46 +00:00
|
|
|
|
2008-05-08 10:08:10 +00:00
|
|
|
// XRenderFind(Standard)Format() is a roundtrip, so cache the results
|
|
|
|
static XRenderPictFormat* renderformats[ 33 ];
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
static Picture createPicture(Pixmap pix, int depth)
|
|
|
|
{
|
|
|
|
if (pix == None)
|
2008-05-08 10:08:10 +00:00
|
|
|
return None;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (renderformats[ depth ] == NULL) {
|
|
|
|
switch(depth) {
|
|
|
|
case 1:
|
|
|
|
renderformats[ 1 ] = XRenderFindStandardFormat(display(), PictStandardA1);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
renderformats[ 8 ] = XRenderFindStandardFormat(display(), PictStandardA8);
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
renderformats[ 24 ] = XRenderFindStandardFormat(display(), PictStandardRGB24);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
renderformats[ 32 ] = XRenderFindStandardFormat(display(), PictStandardARGB32);
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
XRenderPictFormat req;
|
|
|
|
long mask = PictFormatType | PictFormatDepth;
|
|
|
|
req.type = PictTypeDirect;
|
|
|
|
req.depth = depth;
|
|
|
|
renderformats[ depth ] = XRenderFindFormat(display(), mask, &req, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (renderformats[ depth ] == NULL) {
|
|
|
|
kWarning(1212) << "Could not find XRender format for depth" << depth;
|
2008-05-08 10:08:10 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return XRenderCreatePicture(display(), pix, renderformats[ depth ], 0, NULL);
|
|
|
|
}
|
2008-05-08 10:08:10 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
XRenderPicture::XRenderPicture(QPixmap pix)
|
|
|
|
{
|
2013-02-12 07:16:27 +00:00
|
|
|
if (!Extensions::nonNativePixmaps()) {
|
2011-05-12 16:52:38 +00:00
|
|
|
d = new XRenderPictureData(createPicture(pix.handle(), pix.depth()));
|
2013-02-12 07:16:27 +00:00
|
|
|
return;
|
2011-05-12 16:52:38 +00:00
|
|
|
}
|
2013-02-12 07:16:27 +00:00
|
|
|
QImage img(pix.toImage());
|
|
|
|
const int depth = img.depth();
|
|
|
|
xcb_pixmap_t xpix = xcb_generate_id(connection());
|
|
|
|
xcb_create_pixmap(connection(), depth, xpix, rootWindow(), img.width(), img.height());
|
|
|
|
|
|
|
|
xcb_gcontext_t cid = xcb_generate_id(connection());
|
|
|
|
xcb_create_gc(connection(), cid, xpix, 0, NULL);
|
|
|
|
xcb_put_image(connection(), XCB_IMAGE_FORMAT_Z_PIXMAP, xpix, cid, img.width(), img.height(),
|
|
|
|
0, 0, 0, depth, img.byteCount(), img.constBits());
|
|
|
|
xcb_free_gc(connection(), cid);
|
|
|
|
|
|
|
|
d = new XRenderPictureData(createPicture(xpix, depth));
|
|
|
|
xcb_free_pixmap(connection(), xpix);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-05-08 10:08:10 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
XRenderPicture::XRenderPicture(Pixmap pix, int depth)
|
|
|
|
: d(new XRenderPictureData(createPicture(pix, depth)))
|
|
|
|
{
|
|
|
|
}
|
2008-05-08 10:08:10 +00:00
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return s_scene_offscreenTargetStack.isEmpty() ? 0 : s_scene_offscreenTargetStack.top();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setXRenderOffscreen(bool b)
|
|
|
|
{
|
|
|
|
b ? ++s_renderOffscreen : --s_renderOffscreen;
|
|
|
|
if (s_renderOffscreen < 0) {
|
|
|
|
s_renderOffscreen = 0;
|
|
|
|
qWarning("*** SOMETHING IS MESSED UP WITH YOUR setXRenderOffscreen() USAGE ***");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
qWarning("*** SOMETHING IS MESSED UP WITH YOUR xRenderPopTarget() USAGE ***");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-05-07 14:43:13 +00:00
|
|
|
} // namespace
|