screenshot.cpp 7.61 KB
Newer Older
Martin Flöser's avatar
Martin Flöser committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

 Copyright (C) 2010 Martin Gräßlin <kde@martin-graesslin.com>
 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)

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 "screenshot.h"
#include <kwinglutils.h>
23 24 25 26 27 28 29
#include <KDE/KDebug>
#include <QtDBus/QDBusConnection>
#include <QtCore/QVarLengthArray>
#include <QtGui/QPainter>

#include <X11/extensions/Xfixes.h>
#include <QX11Info>
Martin Flöser's avatar
Martin Flöser committed
30 31 32 33

namespace KWin
{

34 35
KWIN_EFFECT(screenshot, ScreenShotEffect)
KWIN_EFFECT_SUPPORTED(screenshot, ScreenShotEffect::supported())
Martin Flöser's avatar
Martin Flöser committed
36 37

bool ScreenShotEffect::supported()
38
{
Martin Flöser's avatar
Martin Flöser committed
39
    return effects->compositingType() == KWin::OpenGLCompositing && GLRenderTarget::supported();
40
}
Martin Flöser's avatar
Martin Flöser committed
41 42

ScreenShotEffect::ScreenShotEffect()
43 44 45 46 47
    : m_scheduledScreenshot(0)
{
    QDBusConnection::sessionBus().registerObject("/Screenshot", this, QDBusConnection::ExportScriptableContents);
    QDBusConnection::sessionBus().registerService("org.kde.kwin.Screenshot");
}
Martin Flöser's avatar
Martin Flöser committed
48 49

ScreenShotEffect::~ScreenShotEffect()
50 51 52 53
{
    QDBusConnection::sessionBus().unregisterObject("/Screenshot");
    QDBusConnection::sessionBus().unregisterService("org.kde.kwin.Screenshot");
}
Martin Flöser's avatar
Martin Flöser committed
54
void ScreenShotEffect::postPaintScreen()
55
{
Martin Flöser's avatar
Martin Flöser committed
56
    effects->postPaintScreen();
57
    if (m_scheduledScreenshot) {
Martin Flöser's avatar
Martin Flöser committed
58 59
        int w = displayWidth();
        int h = displayHeight();
60 61 62 63 64 65 66 67 68 69
        if (!GLTexture::NPOTTextureSupported()) {
            w = nearestPowerOfTwo(w);
            h = nearestPowerOfTwo(h);
        }
        GLTexture* offscreenTexture = new GLTexture(w, h);
        offscreenTexture->setFilter(GL_LINEAR);
        offscreenTexture->setWrapMode(GL_CLAMP_TO_EDGE);
        GLRenderTarget* target = new GLRenderTarget(offscreenTexture);
        if (target->valid()) {
            WindowPaintData d(m_scheduledScreenshot);
Martin Flöser's avatar
Martin Flöser committed
70 71 72 73
            double left = 0;
            double top = 0;
            double right = m_scheduledScreenshot->width();
            double bottom = m_scheduledScreenshot->height();
74 75
            if (m_scheduledScreenshot->hasDecoration() && m_type & INCLUDE_DECORATION) {
                foreach (const WindowQuad & quad, d.quads) {
76 77 78 79 80 81
                    // we need this loop to include the decoration padding
                    left   = qMin(left, quad.left());
                    top    = qMin(top, quad.top());
                    right  = qMax(right, quad.right());
                    bottom = qMax(bottom, quad.bottom());
                }
82
            } else if (m_scheduledScreenshot->hasDecoration()) {
83 84 85 86 87
                WindowQuadList newQuads;
                left = m_scheduledScreenshot->width();
                top = m_scheduledScreenshot->height();
                right = 0;
                bottom = 0;
88 89
                foreach (const WindowQuad & quad, d.quads) {
                    if (quad.type() == WindowQuadContents) {
90 91 92 93 94 95
                        newQuads << quad;
                        left   = qMin(left, quad.left());
                        top    = qMin(top, quad.top());
                        right  = qMax(right, quad.right());
                        bottom = qMax(bottom, quad.bottom());
                    }
Martin Flöser's avatar
Martin Flöser committed
96
                }
97 98
                d.quads = newQuads;
            }
Martin Flöser's avatar
Martin Flöser committed
99 100 101 102 103 104
            int width = right - left;
            int height = bottom - top;
            d.xTranslate = -m_scheduledScreenshot->x() - left;
            d.yTranslate = -m_scheduledScreenshot->y() - top;
            // render window into offscreen texture
            int mask = PAINT_WINDOW_TRANSFORMED | PAINT_WINDOW_TRANSLUCENT;
105
            GLRenderTarget::pushRenderTarget(target);
106
            glClearColor(0.0, 0.0, 0.0, 0.0);
107
            glClear(GL_COLOR_BUFFER_BIT);
108
            glClearColor(0.0, 0.0, 0.0, 1.0);
109
            effects->drawWindow(m_scheduledScreenshot, mask, QRegion(0, 0, width, height), d);
110
            // copy content from framebuffer into image
111
            QImage img(QSize(width, height), QImage::Format_ARGB32);
112
            glReadPixels(0, offscreenTexture->height() - height, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)img.bits());
113
            GLRenderTarget::popRenderTarget();
114 115 116
            ScreenShotEffect::convertFromGLImage(img, width, height);
            if (m_type & INCLUDE_CURSOR) {
                grabPointerImage(img, m_scheduledScreenshot->x() + left, m_scheduledScreenshot->y() + top);
Martin Flöser's avatar
Martin Flöser committed
117
            }
118 119 120
            m_lastScreenshot = QPixmap::fromImage(img);
            emit screenshotCreated(m_lastScreenshot.handle());
        }
Martin Flöser's avatar
Martin Flöser committed
121 122 123 124
        delete offscreenTexture;
        delete target;
        m_scheduledScreenshot = NULL;
    }
125
}
Martin Flöser's avatar
Martin Flöser committed
126

127
void ScreenShotEffect::screenshotWindowUnderCursor(int mask)
128
{
129 130
    m_type = (ScreenShotType)mask;
    const QPoint cursor = effects->cursorPos();
131 132
    foreach (EffectWindow * w, effects->stackingOrder()) {
        if (w->geometry().contains(cursor) && w->isOnCurrentDesktop() && !w->isMinimized()) {
133 134
            m_scheduledScreenshot = w;
        }
135 136
    }
    if (m_scheduledScreenshot) {
137
        m_scheduledScreenshot->addRepaintFull();
Martin Flöser's avatar
Martin Flöser committed
138
    }
139
}
Martin Flöser's avatar
Martin Flöser committed
140

141
void ScreenShotEffect::grabPointerImage(QImage& snapshot, int offsetx, int offsety)
142 143
// Uses the X11_EXTENSIONS_XFIXES_H extension to grab the pointer image, and overlays it onto the snapshot.
{
144 145 146
    XFixesCursorImage *xcursorimg = XFixesGetCursorImage(QX11Info::display());
    if (!xcursorimg)
        return;
147 148 149 150

    //Annoyingly, xfixes specifies the data to be 32bit, but places it in an unsigned long *
    //which can be 64 bit.  So we need to iterate over a 64bit structure to put it in a 32bit
    //structure.
151
    QVarLengthArray< quint32 > pixels(xcursorimg->width * xcursorimg->height);
152 153 154 155
    for (int i = 0; i < xcursorimg->width * xcursorimg->height; ++i)
        pixels[i] = xcursorimg->pixels[i] & 0xffffffff;

    QImage qcursorimg((uchar *) pixels.data(), xcursorimg->width, xcursorimg->height,
156
                      QImage::Format_ARGB32_Premultiplied);
157 158 159 160 161 162 163

    QPainter painter(&snapshot);
    painter.drawImage(QPointF(xcursorimg->x - xcursorimg->xhot - offsetx, xcursorimg->y - xcursorimg ->yhot - offsety), qcursorimg);

    XFree(xcursorimg);
}

Martin Flöser's avatar
Martin Flöser committed
164 165 166 167 168 169 170 171
void ScreenShotEffect::convertFromGLImage(QImage &img, int w, int h)
{
    // from QtOpenGL/qgl.cpp
    // Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
    // see http://qt.gitorious.org/qt/qt/blobs/master/src/opengl/qgl.cpp
    if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
        // OpenGL gives RGBA; Qt wants ARGB
        uint *p = (uint*)img.bits();
172
        uint *end = p + w * h;
Martin Flöser's avatar
Martin Flöser committed
173 174 175 176 177 178 179 180 181
        while (p < end) {
            uint a = *p << 24;
            *p = (*p >> 8) | a;
            p++;
        }
    } else {
        // OpenGL gives ABGR (i.e. RGBA backwards); Qt wants ARGB
        for (int y = 0; y < h; y++) {
            uint *q = (uint*)img.scanLine(y);
182
            for (int x = 0; x < w; ++x) {
Martin Flöser's avatar
Martin Flöser committed
183 184
                const uint pixel = *q;
                *q = ((pixel << 16) & 0xff0000) | ((pixel >> 16) & 0xff)
185
                     | (pixel & 0xff00ff00);
Martin Flöser's avatar
Martin Flöser committed
186 187 188 189 190 191 192 193 194 195

                q++;
            }
        }

    }
    img = img.mirrored();
}

} // namespace