~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kwin/effects/screenshot/screenshot.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
 KWin - the KDE window manager
 
3
 This file is part of the KDE project.
 
4
 
 
5
 Copyright (C) 2010 Martin Gräßlin <kde@martin-graesslin.com>
 
6
 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
 
7
 
 
8
This program is free software; you can redistribute it and/or modify
 
9
it under the terms of the GNU General Public License as published by
 
10
the Free Software Foundation; either version 2 of the License, or
 
11
(at your option) any later version.
 
12
 
 
13
This program is distributed in the hope that it will be useful,
 
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
GNU General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU General Public License
 
19
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*********************************************************************/
 
21
#include "screenshot.h"
 
22
#include <kwinglutils.h>
 
23
#include <KDE/KDebug>
 
24
#include <QtDBus/QDBusConnection>
 
25
#include <QtCore/QVarLengthArray>
 
26
#include <QtGui/QPainter>
 
27
 
 
28
#include <X11/extensions/Xfixes.h>
 
29
#include <QX11Info>
 
30
 
 
31
namespace KWin
 
32
{
 
33
 
 
34
KWIN_EFFECT(screenshot, ScreenShotEffect)
 
35
KWIN_EFFECT_SUPPORTED(screenshot, ScreenShotEffect::supported())
 
36
 
 
37
bool ScreenShotEffect::supported()
 
38
{
 
39
    return effects->compositingType() == KWin::OpenGLCompositing && GLRenderTarget::supported();
 
40
}
 
41
 
 
42
ScreenShotEffect::ScreenShotEffect()
 
43
    : m_scheduledScreenshot(0)
 
44
{
 
45
    QDBusConnection::sessionBus().registerObject("/Screenshot", this, QDBusConnection::ExportScriptableContents);
 
46
    QDBusConnection::sessionBus().registerService("org.kde.kwin.Screenshot");
 
47
}
 
48
 
 
49
ScreenShotEffect::~ScreenShotEffect()
 
50
{
 
51
    QDBusConnection::sessionBus().unregisterObject("/Screenshot");
 
52
    QDBusConnection::sessionBus().unregisterService("org.kde.kwin.Screenshot");
 
53
}
 
54
void ScreenShotEffect::postPaintScreen()
 
55
{
 
56
    effects->postPaintScreen();
 
57
    if (m_scheduledScreenshot) {
 
58
        int w = displayWidth();
 
59
        int h = displayHeight();
 
60
        if (!GLTexture::NPOTTextureSupported()) {
 
61
            w = nearestPowerOfTwo(w);
 
62
            h = nearestPowerOfTwo(h);
 
63
        }
 
64
        GLTexture* offscreenTexture = new GLTexture(w, h);
 
65
        offscreenTexture->setFilter(GL_LINEAR);
 
66
        offscreenTexture->setWrapMode(GL_CLAMP_TO_EDGE);
 
67
        GLRenderTarget* target = new GLRenderTarget(offscreenTexture);
 
68
        if (target->valid()) {
 
69
            WindowPaintData d(m_scheduledScreenshot);
 
70
            double left = 0;
 
71
            double top = 0;
 
72
            double right = m_scheduledScreenshot->width();
 
73
            double bottom = m_scheduledScreenshot->height();
 
74
            if (m_scheduledScreenshot->hasDecoration() && m_type & INCLUDE_DECORATION) {
 
75
                foreach (const WindowQuad & quad, d.quads) {
 
76
                    // we need this loop to include the decoration padding
 
77
                    left   = qMin(left, quad.left());
 
78
                    top    = qMin(top, quad.top());
 
79
                    right  = qMax(right, quad.right());
 
80
                    bottom = qMax(bottom, quad.bottom());
 
81
                }
 
82
            } else if (m_scheduledScreenshot->hasDecoration()) {
 
83
                WindowQuadList newQuads;
 
84
                left = m_scheduledScreenshot->width();
 
85
                top = m_scheduledScreenshot->height();
 
86
                right = 0;
 
87
                bottom = 0;
 
88
                foreach (const WindowQuad & quad, d.quads) {
 
89
                    if (quad.type() == WindowQuadContents) {
 
90
                        newQuads << quad;
 
91
                        left   = qMin(left, quad.left());
 
92
                        top    = qMin(top, quad.top());
 
93
                        right  = qMax(right, quad.right());
 
94
                        bottom = qMax(bottom, quad.bottom());
 
95
                    }
 
96
                }
 
97
                d.quads = newQuads;
 
98
            }
 
99
            int width = right - left;
 
100
            int height = bottom - top;
 
101
            d.xTranslate = -m_scheduledScreenshot->x() - left;
 
102
            d.yTranslate = -m_scheduledScreenshot->y() - top;
 
103
            // render window into offscreen texture
 
104
            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
            ScreenShotEffect::convertFromGLImage(img, width, height);
 
115
            if (m_type & INCLUDE_CURSOR) {
 
116
                grabPointerImage(img, m_scheduledScreenshot->x() + left, m_scheduledScreenshot->y() + top);
 
117
            }
 
118
            m_lastScreenshot = QPixmap::fromImage(img);
 
119
            if (m_lastScreenshot.handle() == 0) {
 
120
                Pixmap xpix = XCreatePixmap(display(), rootWindow(), m_lastScreenshot.width(),
 
121
                                            m_lastScreenshot.height(), XDefaultDepth(display(), QX11Info::appScreen()));
 
122
                m_lastScreenshot = QPixmap::fromX11Pixmap(xpix, QPixmap::ExplicitlyShared);
 
123
                QPainter p(&m_lastScreenshot);
 
124
                p.drawImage(QPoint(0, 0), img);
 
125
            }
 
126
            emit screenshotCreated(m_lastScreenshot.handle());
 
127
        }
 
128
        delete offscreenTexture;
 
129
        delete target;
 
130
        m_scheduledScreenshot = NULL;
 
131
    }
 
132
}
 
133
 
 
134
void ScreenShotEffect::screenshotWindowUnderCursor(int mask)
 
135
{
 
136
    m_type = (ScreenShotType)mask;
 
137
    const QPoint cursor = effects->cursorPos();
 
138
    foreach (EffectWindow * w, effects->stackingOrder()) {
 
139
        if (w->geometry().contains(cursor) && w->isOnCurrentDesktop() && !w->isMinimized()) {
 
140
            m_scheduledScreenshot = w;
 
141
        }
 
142
    }
 
143
    if (m_scheduledScreenshot) {
 
144
        m_scheduledScreenshot->addRepaintFull();
 
145
    }
 
146
}
 
147
 
 
148
void ScreenShotEffect::grabPointerImage(QImage& snapshot, int offsetx, int offsety)
 
149
// Uses the X11_EXTENSIONS_XFIXES_H extension to grab the pointer image, and overlays it onto the snapshot.
 
150
{
 
151
    XFixesCursorImage *xcursorimg = XFixesGetCursorImage(QX11Info::display());
 
152
    if (!xcursorimg)
 
153
        return;
 
154
 
 
155
    //Annoyingly, xfixes specifies the data to be 32bit, but places it in an unsigned long *
 
156
    //which can be 64 bit.  So we need to iterate over a 64bit structure to put it in a 32bit
 
157
    //structure.
 
158
    QVarLengthArray< quint32 > pixels(xcursorimg->width * xcursorimg->height);
 
159
    for (int i = 0; i < xcursorimg->width * xcursorimg->height; ++i)
 
160
        pixels[i] = xcursorimg->pixels[i] & 0xffffffff;
 
161
 
 
162
    QImage qcursorimg((uchar *) pixels.data(), xcursorimg->width, xcursorimg->height,
 
163
                      QImage::Format_ARGB32_Premultiplied);
 
164
 
 
165
    QPainter painter(&snapshot);
 
166
    painter.drawImage(QPointF(xcursorimg->x - xcursorimg->xhot - offsetx, xcursorimg->y - xcursorimg ->yhot - offsety), qcursorimg);
 
167
 
 
168
    XFree(xcursorimg);
 
169
}
 
170
 
 
171
void ScreenShotEffect::convertFromGLImage(QImage &img, int w, int h)
 
172
{
 
173
    // from QtOpenGL/qgl.cpp
 
174
    // Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
 
175
    // see http://qt.gitorious.org/qt/qt/blobs/master/src/opengl/qgl.cpp
 
176
    if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
 
177
        // OpenGL gives RGBA; Qt wants ARGB
 
178
        uint *p = (uint*)img.bits();
 
179
        uint *end = p + w * h;
 
180
        while (p < end) {
 
181
            uint a = *p << 24;
 
182
            *p = (*p >> 8) | a;
 
183
            p++;
 
184
        }
 
185
    } else {
 
186
        // OpenGL gives ABGR (i.e. RGBA backwards); Qt wants ARGB
 
187
        for (int y = 0; y < h; y++) {
 
188
            uint *q = (uint*)img.scanLine(y);
 
189
            for (int x = 0; x < w; ++x) {
 
190
                const uint pixel = *q;
 
191
                *q = ((pixel << 16) & 0xff0000) | ((pixel >> 16) & 0xff)
 
192
                     | (pixel & 0xff00ff00);
 
193
 
 
194
                q++;
 
195
            }
 
196
        }
 
197
 
 
198
    }
 
199
    img = img.mirrored();
 
200
}
 
201
 
 
202
} // namespace