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

« back to all changes in this revision

Viewing changes to kwin/compositingprefs.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) 2007 Rivo Laks <rivolaks@hot.ee>
 
6
 
 
7
This program is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation; either version 2 of the License, or
 
10
(at your option) any later version.
 
11
 
 
12
This program is distributed in the hope that it will be useful,
 
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
*********************************************************************/
 
20
 
 
21
#include "compositingprefs.h"
 
22
 
 
23
#include "kwinglobals.h"
 
24
#include "kwinglplatform.h"
 
25
 
 
26
#include <kconfiggroup.h>
 
27
#include <kdebug.h>
 
28
#include <kxerrorhandler.h>
 
29
#include <klocale.h>
 
30
#include <kdeversion.h>
 
31
#include <ksharedconfig.h>
 
32
#include <kstandarddirs.h>
 
33
 
 
34
#include <qprocess.h>
 
35
 
 
36
 
 
37
namespace KWin
 
38
{
 
39
 
 
40
CompositingPrefs::CompositingPrefs()
 
41
    : mRecommendCompositing(false)
 
42
    , mEnableVSync(true)
 
43
    , mEnableDirectRendering(true)
 
44
    , mStrictBinding(true)
 
45
{
 
46
}
 
47
 
 
48
CompositingPrefs::~CompositingPrefs()
 
49
{
 
50
}
 
51
 
 
52
bool CompositingPrefs::recommendCompositing() const
 
53
{
 
54
    return mRecommendCompositing;
 
55
}
 
56
 
 
57
bool CompositingPrefs::openGlIsBroken()
 
58
{
 
59
    KSharedConfigPtr config = KSharedConfig::openConfig("kwinrc");
 
60
    return KConfigGroup(config, "Compositing").readEntry("OpenGLIsUnsafe", false);
 
61
}
 
62
 
 
63
bool CompositingPrefs::compositingPossible()
 
64
{
 
65
    // first off, check whether we figured that we'll crash on detection because of a buggy driver
 
66
    KSharedConfigPtr config = KSharedConfig::openConfig("kwinrc");
 
67
    KConfigGroup gl_workaround_group(config, "Compositing");
 
68
    if (gl_workaround_group.readEntry("Backend", "OpenGL") == "OpenGL" &&
 
69
        gl_workaround_group.readEntry("OpenGLIsUnsafe", false))
 
70
        return false;
 
71
 
 
72
#ifdef KWIN_HAVE_COMPOSITING
 
73
    Extensions::init();
 
74
    if (!Extensions::compositeAvailable()) {
 
75
        kDebug(1212) << "No composite extension available";
 
76
        return false;
 
77
    }
 
78
    if (!Extensions::damageAvailable()) {
 
79
        kDebug(1212) << "No damage extension available";
 
80
        return false;
 
81
    }
 
82
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
 
83
    if (Extensions::glxAvailable())
 
84
        return true;
 
85
#endif
 
86
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
 
87
    if (Extensions::renderAvailable() && Extensions::fixesAvailable())
 
88
        return true;
 
89
#endif
 
90
#ifdef KWIN_HAVE_OPENGLES
 
91
    return true;
 
92
#endif
 
93
    kDebug(1212) << "No OpenGL or XRender/XFixes support";
 
94
    return false;
 
95
#else
 
96
    return false;
 
97
#endif
 
98
}
 
99
 
 
100
QString CompositingPrefs::compositingNotPossibleReason()
 
101
{
 
102
#ifdef KWIN_HAVE_COMPOSITING
 
103
    // first off, check whether we figured that we'll crash on detection because of a buggy driver
 
104
    KSharedConfigPtr config = KSharedConfig::openConfig("kwinrc");
 
105
    KConfigGroup gl_workaround_group(config, "Compositing");
 
106
    if (gl_workaround_group.readEntry("Backend", "OpenGL") == "OpenGL" &&
 
107
        gl_workaround_group.readEntry("OpenGLIsUnsafe", false))
 
108
        return i18n("<b>OpenGL compositing (the default) has crashed KWin in the past.</b><br>"
 
109
                    "This was most likely due to a driver bug."
 
110
                    "<p>If you think that you have meanwhile upgraded to a stable driver,<br>"
 
111
                    "you can reset this protection but <b>be aware that this might result in an immediate crash!</b></p>"
 
112
                    "<p>Alternatively, you might want to use the XRender backend instead.</p>");
 
113
 
 
114
    Extensions::init();
 
115
    if (!Extensions::compositeAvailable() || !Extensions::damageAvailable()) {
 
116
        return i18n("Required X extensions (XComposite and XDamage) are not available.");
 
117
    }
 
118
#if defined( KWIN_HAVE_OPENGL_COMPOSITING ) && !defined( KWIN_HAVE_XRENDER_COMPOSITING )
 
119
    if (!Extensions::glxAvailable())
 
120
        return i18n("GLX/OpenGL are not available and only OpenGL support is compiled.");
 
121
#elif !defined( KWIN_HAVE_OPENGL_COMPOSITING ) && defined( KWIN_HAVE_XRENDER_COMPOSITING )
 
122
    if (!(Extensions::renderAvailable() && Extensions::fixesAvailable()))
 
123
        return i18n("XRender/XFixes extensions are not available and only XRender support"
 
124
                    " is compiled.");
 
125
#else
 
126
    if (!(Extensions::glxAvailable()
 
127
            || (Extensions::renderAvailable() && Extensions::fixesAvailable()))) {
 
128
        return i18n("GLX/OpenGL and XRender/XFixes are not available.");
 
129
    }
 
130
#endif
 
131
    return QString();
 
132
#else
 
133
    return i18n("Compositing was disabled at compile time.\n"
 
134
                "It is likely Xorg development headers were not installed.");
 
135
#endif
 
136
}
 
137
 
 
138
void CompositingPrefs::detect()
 
139
{
 
140
    if (!compositingPossible() || openGlIsBroken()) {
 
141
        return;
 
142
    }
 
143
 
 
144
    // NOTICE: this is intended to workaround broken GL implementations that successfully segfault
 
145
    // on glXQuery :-(
 
146
    // we tag GL as unsafe. It *must* be reset before every return, and in case we "unexpectedly"
 
147
    // end (aka "segfaulted") we know that we shall not try again
 
148
    KSharedConfigPtr config = KSharedConfig::openConfig("kwinrc");
 
149
    KConfigGroup gl_workaround_config = KConfigGroup(config, "Compositing");
 
150
    gl_workaround_config.writeEntry("OpenGLIsUnsafe", true);
 
151
    gl_workaround_config.sync();
 
152
 
 
153
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
 
154
#ifdef KWIN_HAVE_OPENGLES
 
155
    bool haveContext = false;
 
156
    bool canDetect = false;
 
157
    EGLDisplay dpy = eglGetCurrentDisplay();
 
158
    if (dpy != EGL_NO_DISPLAY) {
 
159
        EGLContext ctx = eglGetCurrentContext();
 
160
        if (ctx != EGL_NO_CONTEXT) {
 
161
            haveContext = true;
 
162
            canDetect = true;
 
163
        }
 
164
    }
 
165
    if (!haveContext) {
 
166
        canDetect = initEGLContext();
 
167
    }
 
168
    if (canDetect) {
 
169
        detectDriverAndVersion();
 
170
        applyDriverSpecificOptions();
 
171
    }
 
172
    if (!haveContext) {
 
173
        deleteEGLContext();
 
174
    }
 
175
#else
 
176
    // HACK: This is needed for AIGLX
 
177
    if (qstrcmp(qgetenv("KWIN_DIRECT_GL"), "1") != 0) {
 
178
        // Start an external helper program that initializes GLX and returns
 
179
        // 0 if we can use direct rendering, and 1 otherwise.
 
180
        // The reason we have to use an external program is that after GLX
 
181
        // has been initialized, it's too late to set the LIBGL_ALWAYS_INDIRECT
 
182
        // environment variable.
 
183
        // Direct rendering is preferred, since not all OpenGL extensions are
 
184
        // available with indirect rendering.
 
185
        const QString opengl_test = KStandardDirs::findExe("kwin_opengl_test");
 
186
        if (QProcess::execute(opengl_test) != 0)
 
187
            setenv("LIBGL_ALWAYS_INDIRECT", "1", true);
 
188
    }
 
189
    if (!Extensions::glxAvailable()) {
 
190
        kDebug(1212) << "No GLX available";
 
191
        gl_workaround_config.writeEntry("OpenGLIsUnsafe", false);
 
192
        gl_workaround_config.sync();
 
193
        return;
 
194
    }
 
195
    int glxmajor, glxminor;
 
196
    glXQueryVersion(display(), &glxmajor, &glxminor);
 
197
    kDebug(1212) << "glx version is " << glxmajor << "." << glxminor;
 
198
    bool hasglx13 = (glxmajor > 1 || (glxmajor == 1 && glxminor >= 3));
 
199
 
 
200
    // remember and later restore active context
 
201
    GLXContext oldcontext = glXGetCurrentContext();
 
202
    GLXDrawable olddrawable = glXGetCurrentDrawable();
 
203
    GLXDrawable oldreaddrawable = None;
 
204
    if (hasglx13)
 
205
        oldreaddrawable = glXGetCurrentReadDrawable();
 
206
 
 
207
    if (initGLXContext()) {
 
208
        detectDriverAndVersion();
 
209
        applyDriverSpecificOptions();
 
210
    }
 
211
    if (hasglx13)
 
212
        glXMakeContextCurrent(display(), olddrawable, oldreaddrawable, oldcontext);
 
213
    else
 
214
        glXMakeCurrent(display(), olddrawable, oldcontext);
 
215
    deleteGLXContext();
 
216
#endif
 
217
    gl_workaround_config.writeEntry("OpenGLIsUnsafe", false);
 
218
    gl_workaround_config.sync();
 
219
#endif
 
220
}
 
221
 
 
222
bool CompositingPrefs::initGLXContext()
 
223
{
 
224
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
 
225
#ifndef KWIN_HAVE_OPENGLES
 
226
    mGLContext = NULL;
 
227
    KXErrorHandler handler;
 
228
    // Most of this code has been taken from glxinfo.c
 
229
    QVector<int> attribs;
 
230
    attribs << GLX_RGBA;
 
231
    attribs << GLX_RED_SIZE << 1;
 
232
    attribs << GLX_GREEN_SIZE << 1;
 
233
    attribs << GLX_BLUE_SIZE << 1;
 
234
    attribs << None;
 
235
 
 
236
    XVisualInfo* visinfo = glXChooseVisual(display(), DefaultScreen(display()), attribs.data());
 
237
    if (!visinfo) {
 
238
        attribs.last() = GLX_DOUBLEBUFFER;
 
239
        attribs << None;
 
240
        visinfo = glXChooseVisual(display(), DefaultScreen(display()), attribs.data());
 
241
        if (!visinfo) {
 
242
            kDebug(1212) << "Error: couldn't find RGB GLX visual";
 
243
            return false;
 
244
        }
 
245
    }
 
246
 
 
247
    mGLContext = glXCreateContext(display(), visinfo, NULL, True);
 
248
    if (!mGLContext) {
 
249
        kDebug(1212) << "glXCreateContext failed";
 
250
        XDestroyWindow(display(), mGLWindow);
 
251
        return false;
 
252
    }
 
253
 
 
254
    XSetWindowAttributes attr;
 
255
    attr.background_pixel = 0;
 
256
    attr.border_pixel = 0;
 
257
    attr.colormap = XCreateColormap(display(), rootWindow(), visinfo->visual, AllocNone);
 
258
    attr.event_mask = StructureNotifyMask | ExposureMask;
 
259
    unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
 
260
    int width = 100, height = 100;
 
261
    mGLWindow = XCreateWindow(display(), rootWindow(), 0, 0, width, height,
 
262
                              0, visinfo->depth, InputOutput,
 
263
                              visinfo->visual, mask, &attr);
 
264
 
 
265
    return glXMakeCurrent(display(), mGLWindow, mGLContext) && !handler.error(true);
 
266
#else
 
267
    return false;
 
268
#endif
 
269
#else
 
270
    return false;
 
271
#endif
 
272
}
 
273
 
 
274
void CompositingPrefs::deleteGLXContext()
 
275
{
 
276
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
 
277
#ifndef KWIN_HAVE_OPENGLES
 
278
    if (mGLContext == NULL)
 
279
        return;
 
280
    glXDestroyContext(display(), mGLContext);
 
281
    XDestroyWindow(display(), mGLWindow);
 
282
#endif
 
283
#endif
 
284
}
 
285
 
 
286
bool CompositingPrefs::initEGLContext()
 
287
{
 
288
#ifdef KWIN_HAVE_OPENGLES
 
289
    mEGLDisplay = eglGetDisplay(display());
 
290
    if (mEGLDisplay == EGL_NO_DISPLAY) {
 
291
        return false;
 
292
    }
 
293
    if (eglInitialize(mEGLDisplay, 0, 0) == EGL_FALSE) {
 
294
        return false;
 
295
    }
 
296
    eglBindAPI(EGL_OPENGL_ES_API);
 
297
 
 
298
    const EGLint config_attribs[] = {
 
299
        EGL_SURFACE_TYPE,         EGL_WINDOW_BIT,
 
300
        EGL_RED_SIZE,             1,
 
301
        EGL_GREEN_SIZE,           1,
 
302
        EGL_BLUE_SIZE,            1,
 
303
        EGL_ALPHA_SIZE,           0,
 
304
        EGL_RENDERABLE_TYPE,      EGL_OPENGL_ES2_BIT,
 
305
        EGL_CONFIG_CAVEAT,        EGL_NONE,
 
306
        EGL_NONE,
 
307
    };
 
308
 
 
309
    EGLint count;
 
310
    EGLConfig configs[1024];
 
311
    eglChooseConfig(mEGLDisplay, config_attribs, configs, 1024, &count);
 
312
 
 
313
    EGLint visualId = XVisualIDFromVisual((Visual*)QX11Info::appVisual());
 
314
 
 
315
    EGLConfig config = configs[0];
 
316
    for (int i = 0; i < count; i++) {
 
317
        EGLint val;
 
318
        eglGetConfigAttrib(mEGLDisplay, configs[i], EGL_NATIVE_VISUAL_ID, &val);
 
319
        if (visualId == val) {
 
320
            config = configs[i];
 
321
            break;
 
322
        }
 
323
    }
 
324
 
 
325
    XSetWindowAttributes attr;
 
326
    attr.background_pixel = 0;
 
327
    attr.border_pixel = 0;
 
328
    attr.colormap = XCreateColormap(display(), rootWindow(), (Visual*)QX11Info::appVisual(), AllocNone);
 
329
    attr.event_mask = StructureNotifyMask | ExposureMask;
 
330
    unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
 
331
    int width = 100, height = 100;
 
332
    mGLWindow = XCreateWindow(display(), rootWindow(), 0, 0, width, height,
 
333
                              0, QX11Info::appDepth(), InputOutput,
 
334
                              (Visual*)QX11Info::appVisual(), mask, &attr);
 
335
 
 
336
    mEGLSurface = eglCreateWindowSurface(mEGLDisplay, config, mGLWindow, 0);
 
337
 
 
338
    const EGLint context_attribs[] = {
 
339
        EGL_CONTEXT_CLIENT_VERSION, 2,
 
340
        EGL_NONE
 
341
    };
 
342
 
 
343
    mEGLContext = eglCreateContext(mEGLDisplay, config, EGL_NO_CONTEXT, context_attribs);
 
344
    if (mEGLContext == EGL_NO_CONTEXT) {
 
345
        return false;
 
346
    }
 
347
    if (eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext) == EGL_FALSE) {
 
348
        return false;
 
349
    }
 
350
    EGLint error = eglGetError();
 
351
    if (error != EGL_SUCCESS) {
 
352
        return false;
 
353
    }
 
354
    return true;
 
355
#else
 
356
    return false;
 
357
#endif
 
358
}
 
359
 
 
360
void CompositingPrefs::deleteEGLContext()
 
361
{
 
362
#ifdef KWIN_HAVE_OPENGLES
 
363
    eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
 
364
    eglDestroyContext(mEGLDisplay, mEGLContext);
 
365
    eglDestroySurface(mEGLDisplay, mEGLSurface);
 
366
    eglTerminate(mEGLDisplay);
 
367
    eglReleaseThread();
 
368
    XDestroyWindow(display(), mGLWindow);
 
369
#endif
 
370
}
 
371
 
 
372
void CompositingPrefs::detectDriverAndVersion()
 
373
{
 
374
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
 
375
    GLPlatform *gl = GLPlatform::instance();
 
376
    gl->detect();
 
377
    gl->printResults();
 
378
#endif
 
379
}
 
380
 
 
381
// See http://techbase.kde.org/Projects/KWin/HW for a list of some cards that are known to work.
 
382
void CompositingPrefs::applyDriverSpecificOptions()
 
383
{
 
384
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
 
385
    // Always recommend
 
386
    mRecommendCompositing = true;
 
387
 
 
388
    GLPlatform *gl = GLPlatform::instance();
 
389
    mStrictBinding = !gl->supports(LooseBinding);
 
390
    if (gl->driver() == Driver_Intel)
 
391
        mEnableVSync = false;
 
392
#endif
 
393
}
 
394
 
 
395
} // namespace
 
396