kwin/compositingprefs.cpp

358 lines
11 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
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 "compositingprefs.h"
#include "kwinglobals.h"
#include "kwinglplatform.h"
#include <kdebug.h>
#include <kxerrorhandler.h>
#include <klocale.h>
#include <kdeversion.h>
#include <kstandarddirs.h>
#include <qprocess.h>
namespace KWin
{
CompositingPrefs::CompositingPrefs()
2011-01-30 14:34:42 +00:00
: mRecommendCompositing(false)
, mEnableVSync(true)
, mEnableDirectRendering(true)
, mStrictBinding(true)
{
}
CompositingPrefs::~CompositingPrefs()
2011-01-30 14:34:42 +00:00
{
}
bool CompositingPrefs::recommendCompositing() const
2011-01-30 14:34:42 +00:00
{
return mRecommendCompositing;
2011-01-30 14:34:42 +00:00
}
bool CompositingPrefs::compositingPossible()
2011-01-30 14:34:42 +00:00
{
#ifdef KWIN_HAVE_COMPOSITING
Extensions::init();
2011-01-30 14:34:42 +00:00
if (!Extensions::compositeAvailable()) {
kDebug(1212) << "No composite extension available";
return false;
2011-01-30 14:34:42 +00:00
}
if (!Extensions::damageAvailable()) {
kDebug(1212) << "No damage extension available";
return false;
2011-01-30 14:34:42 +00:00
}
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
2011-01-30 14:34:42 +00:00
if (Extensions::glxAvailable())
return true;
#endif
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
2011-01-30 14:34:42 +00:00
if (Extensions::renderAvailable() && Extensions::fixesAvailable())
return true;
2010-12-12 11:08:23 +00:00
#endif
#ifdef KWIN_HAVE_OPENGLES
return true;
#endif
2011-01-30 14:34:42 +00:00
kDebug(1212) << "No OpenGL or XRender/XFixes support";
return false;
#else
return false;
#endif
2011-01-30 14:34:42 +00:00
}
QString CompositingPrefs::compositingNotPossibleReason()
2011-01-30 14:34:42 +00:00
{
#ifdef KWIN_HAVE_COMPOSITING
Extensions::init();
2011-01-30 14:34:42 +00:00
if (!Extensions::compositeAvailable() || !Extensions::damageAvailable()) {
return i18n("Required X extensions (XComposite and XDamage) are not available.");
2011-01-30 14:34:42 +00:00
}
#if defined( KWIN_HAVE_OPENGL_COMPOSITING ) && !defined( KWIN_HAVE_XRENDER_COMPOSITING )
2011-01-30 14:34:42 +00:00
if (!Extensions::glxAvailable())
return i18n("GLX/OpenGL are not available and only OpenGL support is compiled.");
#elif !defined( KWIN_HAVE_OPENGL_COMPOSITING ) && defined( KWIN_HAVE_XRENDER_COMPOSITING )
2011-01-30 14:34:42 +00:00
if (!(Extensions::renderAvailable() && Extensions::fixesAvailable()))
return i18n("XRender/XFixes extensions are not available and only XRender support"
" is compiled.");
#else
2011-01-30 14:34:42 +00:00
if (!(Extensions::glxAvailable()
|| (Extensions::renderAvailable() && Extensions::fixesAvailable()))) {
return i18n("GLX/OpenGL and XRender/XFixes are not available.");
}
#endif
return QString();
#else
return i18n("Compositing was disabled at compile time.\n"
2011-01-30 14:34:42 +00:00
"It is likely Xorg development headers were not installed.");
#endif
2011-01-30 14:34:42 +00:00
}
void CompositingPrefs::detect()
2011-01-30 14:34:42 +00:00
{
if (!compositingPossible()) {
return;
2011-01-30 14:34:42 +00:00
}
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
2010-12-12 11:08:23 +00:00
#ifdef KWIN_HAVE_OPENGLES
bool haveContext = false;
bool canDetect = false;
EGLDisplay dpy = eglGetCurrentDisplay();
if (dpy != EGL_NO_DISPLAY) {
EGLContext ctx = eglGetCurrentContext();
if (ctx != EGL_NO_CONTEXT) {
haveContext = true;
canDetect = true;
}
}
if (!haveContext) {
canDetect = initEGLContext();
}
if (canDetect) {
detectDriverAndVersion();
applyDriverSpecificOptions();
}
if (!haveContext) {
deleteEGLContext();
}
#else
// HACK: This is needed for AIGLX
2011-01-30 14:34:42 +00:00
if (qstrcmp(qgetenv("KWIN_DIRECT_GL"), "1") != 0) {
// Start an external helper program that initializes GLX and returns
// 0 if we can use direct rendering, and 1 otherwise.
// The reason we have to use an external program is that after GLX
// has been initialized, it's too late to set the LIBGL_ALWAYS_INDIRECT
// environment variable.
// Direct rendering is preferred, since not all OpenGL extensions are
// available with indirect rendering.
2011-01-30 14:34:42 +00:00
const QString opengl_test = KStandardDirs::findExe("kwin_opengl_test");
if (QProcess::execute(opengl_test) != 0)
setenv("LIBGL_ALWAYS_INDIRECT", "1", true);
}
if (!Extensions::glxAvailable()) {
kDebug(1212) << "No GLX available";
return;
2011-01-30 14:34:42 +00:00
}
int glxmajor, glxminor;
2011-01-30 14:34:42 +00:00
glXQueryVersion(display(), &glxmajor, &glxminor);
kDebug(1212) << "glx version is " << glxmajor << "." << glxminor;
bool hasglx13 = (glxmajor > 1 || (glxmajor == 1 && glxminor >= 3));
// remember and later restore active context
GLXContext oldcontext = glXGetCurrentContext();
GLXDrawable olddrawable = glXGetCurrentDrawable();
GLXDrawable oldreaddrawable = None;
2011-01-30 14:34:42 +00:00
if (hasglx13)
oldreaddrawable = glXGetCurrentReadDrawable();
2011-01-30 14:34:42 +00:00
if (initGLXContext()) {
detectDriverAndVersion();
applyDriverSpecificOptions();
2011-01-30 14:34:42 +00:00
}
if (hasglx13)
glXMakeContextCurrent(display(), olddrawable, oldreaddrawable, oldcontext);
else
2011-01-30 14:34:42 +00:00
glXMakeCurrent(display(), olddrawable, oldcontext);
deleteGLXContext();
2010-11-20 12:33:24 +00:00
#endif
#endif
2011-01-30 14:34:42 +00:00
}
bool CompositingPrefs::initGLXContext()
{
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
2010-11-20 12:33:24 +00:00
#ifndef KWIN_HAVE_OPENGLES
mGLContext = NULL;
KXErrorHandler handler;
// Most of this code has been taken from glxinfo.c
QVector<int> attribs;
attribs << GLX_RGBA;
attribs << GLX_RED_SIZE << 1;
attribs << GLX_GREEN_SIZE << 1;
attribs << GLX_BLUE_SIZE << 1;
attribs << None;
2011-01-30 14:34:42 +00:00
XVisualInfo* visinfo = glXChooseVisual(display(), DefaultScreen(display()), attribs.data());
if (!visinfo) {
attribs.last() = GLX_DOUBLEBUFFER;
attribs << None;
2011-01-30 14:34:42 +00:00
visinfo = glXChooseVisual(display(), DefaultScreen(display()), attribs.data());
if (!visinfo) {
kDebug(1212) << "Error: couldn't find RGB GLX visual";
return false;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
mGLContext = glXCreateContext(display(), visinfo, NULL, True);
if (!mGLContext) {
kDebug(1212) << "glXCreateContext failed";
XDestroyWindow(display(), mGLWindow);
return false;
}
XSetWindowAttributes attr;
attr.background_pixel = 0;
attr.border_pixel = 0;
2011-01-30 14:34:42 +00:00
attr.colormap = XCreateColormap(display(), rootWindow(), visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask | ExposureMask;
unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
int width = 100, height = 100;
2011-01-30 14:34:42 +00:00
mGLWindow = XCreateWindow(display(), rootWindow(), 0, 0, width, height,
0, visinfo->depth, InputOutput,
visinfo->visual, mask, &attr);
2011-01-30 14:34:42 +00:00
return glXMakeCurrent(display(), mGLWindow, mGLContext) && !handler.error(true);
2010-11-20 12:33:24 +00:00
#else
return false;
#endif
#else
2011-01-30 14:34:42 +00:00
return false;
#endif
}
void CompositingPrefs::deleteGLXContext()
{
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
2010-11-20 12:33:24 +00:00
#ifndef KWIN_HAVE_OPENGLES
2011-01-30 14:34:42 +00:00
if (mGLContext == NULL)
return;
2011-01-30 14:34:42 +00:00
glXDestroyContext(display(), mGLContext);
XDestroyWindow(display(), mGLWindow);
#endif
2010-11-20 12:33:24 +00:00
#endif
}
2010-12-12 11:08:23 +00:00
bool CompositingPrefs::initEGLContext()
{
#ifdef KWIN_HAVE_OPENGLES
2011-01-30 14:34:42 +00:00
mEGLDisplay = eglGetDisplay(display());
2010-12-12 11:08:23 +00:00
if (mEGLDisplay == EGL_NO_DISPLAY) {
return false;
}
2011-01-30 14:34:42 +00:00
if (eglInitialize(mEGLDisplay, 0, 0) == EGL_FALSE) {
2010-12-12 11:08:23 +00:00
return false;
}
2011-01-30 14:34:42 +00:00
eglBindAPI(EGL_OPENGL_ES_API);
2010-12-12 11:08:23 +00:00
const EGLint config_attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_ALPHA_SIZE, 0,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_CONFIG_CAVEAT, EGL_NONE,
EGL_NONE,
};
EGLint count;
EGLConfig configs[1024];
eglChooseConfig(mEGLDisplay, config_attribs, configs, 1024, &count);
EGLint visualId = XVisualIDFromVisual((Visual*)QX11Info::appVisual());
EGLConfig config = configs[0];
for (int i = 0; i < count; i++) {
EGLint val;
eglGetConfigAttrib(mEGLDisplay, configs[i], EGL_NATIVE_VISUAL_ID, &val);
if (visualId == val) {
config = configs[i];
break;
}
}
XSetWindowAttributes attr;
attr.background_pixel = 0;
attr.border_pixel = 0;
2011-01-30 14:34:42 +00:00
attr.colormap = XCreateColormap(display(), rootWindow(), (Visual*)QX11Info::appVisual(), AllocNone);
2010-12-12 11:08:23 +00:00
attr.event_mask = StructureNotifyMask | ExposureMask;
unsigned long mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
int width = 100, height = 100;
2011-01-30 14:34:42 +00:00
mGLWindow = XCreateWindow(display(), rootWindow(), 0, 0, width, height,
0, QX11Info::appDepth(), InputOutput,
(Visual*)QX11Info::appVisual(), mask, &attr);
2010-12-12 11:08:23 +00:00
2011-01-30 14:34:42 +00:00
mEGLSurface = eglCreateWindowSurface(mEGLDisplay, config, mGLWindow, 0);
2010-12-12 11:08:23 +00:00
const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
2011-01-30 14:34:42 +00:00
mEGLContext = eglCreateContext(mEGLDisplay, config, EGL_NO_CONTEXT, context_attribs);
2010-12-12 11:08:23 +00:00
if (mEGLContext == EGL_NO_CONTEXT) {
return false;
}
2011-01-30 14:34:42 +00:00
if (eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext) == EGL_FALSE) {
2010-12-12 11:08:23 +00:00
return false;
}
EGLint error = eglGetError();
if (error != EGL_SUCCESS) {
return false;
}
return true;
#else
return false;
#endif
}
void CompositingPrefs::deleteEGLContext()
{
#ifdef KWIN_HAVE_OPENGLES
eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(mEGLDisplay, mEGLContext);
eglDestroySurface(mEGLDisplay, mEGLSurface);
eglTerminate(mEGLDisplay);
eglReleaseThread();
2011-01-30 14:34:42 +00:00
XDestroyWindow(display(), mGLWindow);
2010-12-12 11:08:23 +00:00
#endif
}
void CompositingPrefs::detectDriverAndVersion()
2011-01-30 14:34:42 +00:00
{
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
GLPlatform *gl = GLPlatform::instance();
gl->detect();
gl->printResults();
#endif
2011-01-30 14:34:42 +00:00
}
// See http://techbase.kde.org/Projects/KWin/HW for a list of some cards that are known to work.
void CompositingPrefs::applyDriverSpecificOptions()
2011-01-30 14:34:42 +00:00
{
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
// Always recommend
mRecommendCompositing = true;
GLPlatform *gl = GLPlatform::instance();
2011-01-30 14:34:42 +00:00
mStrictBinding = !gl->supports(LooseBinding);
if (gl->driver() == Driver_Intel)
mEnableVSync = false;
2011-01-01 13:34:55 +00:00
#endif
2011-01-30 14:34:42 +00:00
}
} // namespace