src/opengl/qglpixelbuffer_x11.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <qlibrary.h>
       
    42 #include <qdebug.h>
       
    43 #include <private/qgl_p.h>
       
    44 #include <private/qt_x11_p.h>
       
    45 #include <private/qpaintengine_opengl_p.h>
       
    46 
       
    47 #include <qx11info_x11.h>
       
    48 #include <GL/glx.h>
       
    49 #include <qimage.h>
       
    50 
       
    51 #include "qglpixelbuffer.h"
       
    52 #include "qglpixelbuffer_p.h"
       
    53 
       
    54 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
       
    55 #include <dlfcn.h>
       
    56 #endif
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 #ifndef GLX_VERSION_1_3
       
    61 #define GLX_RGBA_BIT            0x00000002
       
    62 #define GLX_PBUFFER_BIT         0x00000004
       
    63 #define GLX_DRAWABLE_TYPE       0x8010
       
    64 #define GLX_RENDER_TYPE         0x8011
       
    65 #define GLX_RGBA_TYPE           0x8014
       
    66 #define GLX_PBUFFER_HEIGHT      0x8040
       
    67 #define GLX_PBUFFER_WIDTH       0x8041
       
    68 #endif
       
    69 
       
    70 #ifndef GLX_ARB_multisample
       
    71 #define GLX_SAMPLE_BUFFERS_ARB  100000
       
    72 #define GLX_SAMPLES_ARB         100001
       
    73 #endif
       
    74 
       
    75 typedef GLXFBConfig* (*_glXChooseFBConfig) (Display *dpy, int screen, const int *attrib_list, int *nelements);
       
    76 typedef int (*_glXGetFBConfigAttrib) (Display *dpy, GLXFBConfig config, int attribute, int *value);
       
    77 typedef GLXPbuffer (*_glXCreatePbuffer) (Display *dpy, GLXFBConfig config, const int *attrib_list);
       
    78 typedef void (*_glXDestroyPbuffer) (Display *dpy, GLXPbuffer pbuf);
       
    79 typedef GLXContext (*_glXCreateNewContext) (Display *dpy, GLXFBConfig config, int render_type, GLXContext share_list, Bool direct);
       
    80 typedef Bool (*_glXMakeContextCurrent) (Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx);
       
    81 
       
    82 static _glXChooseFBConfig qt_glXChooseFBConfig = 0;
       
    83 static _glXCreateNewContext qt_glXCreateNewContext = 0;
       
    84 static _glXCreatePbuffer qt_glXCreatePbuffer = 0;
       
    85 static _glXDestroyPbuffer qt_glXDestroyPbuffer = 0;
       
    86 static _glXGetFBConfigAttrib qt_glXGetFBConfigAttrib = 0;
       
    87 static _glXMakeContextCurrent qt_glXMakeContextCurrent = 0;
       
    88 
       
    89 #define glXChooseFBConfig qt_glXChooseFBConfig
       
    90 #define glXCreateNewContext qt_glXCreateNewContext
       
    91 #define glXCreatePbuffer qt_glXCreatePbuffer
       
    92 #define glXDestroyPbuffer qt_glXDestroyPbuffer
       
    93 #define glXGetFBConfigAttrib qt_glXGetFBConfigAttrib
       
    94 #define glXMakeContextCurrent qt_glXMakeContextCurrent
       
    95 
       
    96 extern void* qglx_getProcAddress(const char* procName); // in qgl_x11.cpp
       
    97 
       
    98 static bool qt_resolve_pbuffer_extensions()
       
    99 {
       
   100     static int resolved = false;
       
   101     if (resolved && qt_glXMakeContextCurrent)
       
   102         return true;
       
   103     else if (resolved)
       
   104         return false;
       
   105 
       
   106     qt_glXChooseFBConfig = (_glXChooseFBConfig) qglx_getProcAddress("glXChooseFBConfig");
       
   107     qt_glXCreateNewContext = (_glXCreateNewContext) qglx_getProcAddress("glXCreateNewContext");
       
   108     qt_glXCreatePbuffer = (_glXCreatePbuffer) qglx_getProcAddress("glXCreatePbuffer");
       
   109     qt_glXDestroyPbuffer = (_glXDestroyPbuffer) qglx_getProcAddress("glXDestroyPbuffer");
       
   110     qt_glXGetFBConfigAttrib = (_glXGetFBConfigAttrib) qglx_getProcAddress("glXGetFBConfigAttrib");
       
   111     qt_glXMakeContextCurrent = (_glXMakeContextCurrent) qglx_getProcAddress("glXMakeContextCurrent");
       
   112 
       
   113     resolved = qt_glXMakeContextCurrent ? true : false;
       
   114     return resolved;
       
   115 }
       
   116 
       
   117 static void qt_format_to_attrib_list(const QGLFormat &f, int attribs[])
       
   118 {
       
   119     int i = 0;
       
   120     attribs[i++] = GLX_RENDER_TYPE;
       
   121     attribs[i++] = GLX_RGBA_BIT;
       
   122     attribs[i++] = GLX_DRAWABLE_TYPE;
       
   123     attribs[i++] = GLX_PBUFFER_BIT;
       
   124     attribs[i++] = GLX_RED_SIZE;
       
   125     attribs[i++] = f.redBufferSize() == -1 ? 1 : f.redBufferSize();
       
   126     attribs[i++] = GLX_GREEN_SIZE;
       
   127     attribs[i++] = f.greenBufferSize() == -1 ? 1 : f.greenBufferSize();
       
   128     attribs[i++] = GLX_BLUE_SIZE;
       
   129     attribs[i++] = f.blueBufferSize() == -1 ? 1 : f.blueBufferSize();
       
   130     if (f.doubleBuffer()) {
       
   131         attribs[i++] = GLX_DOUBLEBUFFER;
       
   132         attribs[i++] = true;
       
   133     }
       
   134     if (f.depth()) {
       
   135         attribs[i++] = GLX_DEPTH_SIZE;
       
   136         attribs[i++] = f.depthBufferSize() == -1 ? 1 : f.depthBufferSize();
       
   137     }
       
   138     if (f.stereo()) {
       
   139         attribs[i++] = GLX_STEREO;
       
   140         attribs[i++] = true;
       
   141     }
       
   142     if (f.stencil()) {
       
   143         attribs[i++] = GLX_STENCIL_SIZE;
       
   144         attribs[i++] = f.stencilBufferSize() == -1 ? 1 : f.stencilBufferSize();
       
   145     }
       
   146     if (f.alpha()) {
       
   147         attribs[i++] = GLX_ALPHA_SIZE;
       
   148         attribs[i++] = f.alphaBufferSize() == -1 ? 1 : f.alphaBufferSize();
       
   149     }
       
   150     if (f.accum()) {
       
   151         attribs[i++] = GLX_ACCUM_RED_SIZE;
       
   152         attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
       
   153         attribs[i++] = GLX_ACCUM_GREEN_SIZE;
       
   154         attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
       
   155         attribs[i++] = GLX_ACCUM_BLUE_SIZE;
       
   156         attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
       
   157         if (f.alpha()) {
       
   158             attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
       
   159             attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
       
   160         }
       
   161     }
       
   162     if (f.sampleBuffers()) {
       
   163         attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
       
   164         attribs[i++] = 1;
       
   165         attribs[i++] = GLX_SAMPLES_ARB;
       
   166         attribs[i++] = f.samples() == -1 ? 4 : f.samples();
       
   167     }
       
   168 
       
   169     attribs[i] = XNone;
       
   170 }
       
   171 
       
   172 bool QGLPixelBufferPrivate::init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget)
       
   173 {
       
   174     if (!qt_resolve_pbuffer_extensions()) {
       
   175         qWarning("QGLPixelBuffer: pbuffers are not supported on this system.");
       
   176         return false;
       
   177     }
       
   178 
       
   179     int attribs[40];
       
   180     int num_configs = 0;
       
   181 
       
   182     qt_format_to_attrib_list(f, attribs);
       
   183 
       
   184     GLXFBConfig *configs = glXChooseFBConfig(X11->display, X11->defaultScreen, attribs, &num_configs);
       
   185     if (configs && num_configs) {
       
   186         int res;
       
   187         glXGetFBConfigAttrib(X11->display, configs[0], GLX_LEVEL, &res);
       
   188         format.setPlane(res);
       
   189         glXGetFBConfigAttrib(X11->display, configs[0], GLX_DOUBLEBUFFER, &res);
       
   190         format.setDoubleBuffer(res);
       
   191         glXGetFBConfigAttrib(X11->display, configs[0], GLX_DEPTH_SIZE, &res);
       
   192         format.setDepth(res);
       
   193         if (format.depth())
       
   194             format.setDepthBufferSize(res);
       
   195         glXGetFBConfigAttrib(X11->display, configs[0], GLX_RGBA, &res);
       
   196         format.setRgba(res);
       
   197         glXGetFBConfigAttrib(X11->display, configs[0], GLX_RED_SIZE, &res);
       
   198         format.setRedBufferSize(res);
       
   199         glXGetFBConfigAttrib(X11->display, configs[0], GLX_GREEN_SIZE, &res);
       
   200         format.setGreenBufferSize(res);
       
   201         glXGetFBConfigAttrib(X11->display, configs[0], GLX_BLUE_SIZE, &res);
       
   202         format.setBlueBufferSize(res);
       
   203         glXGetFBConfigAttrib(X11->display, configs[0], GLX_ALPHA_SIZE, &res);
       
   204         format.setAlpha(res);
       
   205         if (format.alpha())
       
   206             format.setAlphaBufferSize(res);
       
   207         glXGetFBConfigAttrib(X11->display, configs[0], GLX_ACCUM_RED_SIZE, &res);
       
   208         format.setAccum(res);
       
   209         if (format.accum())
       
   210             format.setAccumBufferSize(res);
       
   211         glXGetFBConfigAttrib(X11->display, configs[0], GLX_STENCIL_SIZE, &res);
       
   212         format.setStencil(res);
       
   213         if (format.stencil())
       
   214             format.setStencilBufferSize(res);
       
   215         glXGetFBConfigAttrib(X11->display, configs[0], GLX_STEREO, &res);
       
   216         format.setStereo(res);
       
   217         glXGetFBConfigAttrib(X11->display, configs[0], GLX_SAMPLE_BUFFERS_ARB, &res);
       
   218         format.setSampleBuffers(res);
       
   219         if (format.sampleBuffers()) {
       
   220             glXGetFBConfigAttrib(X11->display, configs[0], GLX_SAMPLES_ARB, &res);
       
   221             format.setSamples(res);
       
   222         }
       
   223 
       
   224         int pb_attribs[] = {GLX_PBUFFER_WIDTH, size.width(), GLX_PBUFFER_HEIGHT, size.height(), XNone};
       
   225         GLXContext shareContext = 0;
       
   226         if (shareWidget && shareWidget->d_func()->glcx)
       
   227             shareContext = (GLXContext) shareWidget->d_func()->glcx->d_func()->cx;
       
   228 
       
   229         pbuf = glXCreatePbuffer(QX11Info::display(), configs[0], pb_attribs);
       
   230         ctx = glXCreateNewContext(QX11Info::display(), configs[0], GLX_RGBA_TYPE, shareContext, true);
       
   231 
       
   232         XFree(configs);
       
   233         if (!pbuf || !ctx) {
       
   234             qWarning("QGLPixelBuffer: Unable to create a pbuffer/context - giving up.");
       
   235             return false;
       
   236         }
       
   237         return true;
       
   238     } else {
       
   239         qWarning("QGLPixelBuffer: Unable to find a context/format match - giving up.");
       
   240         return false;
       
   241     }
       
   242 }
       
   243 
       
   244 bool QGLPixelBufferPrivate::cleanup()
       
   245 {
       
   246     glXDestroyPbuffer(QX11Info::display(), pbuf);
       
   247     return true;
       
   248 }
       
   249 
       
   250 bool QGLPixelBuffer::bindToDynamicTexture(GLuint)
       
   251 {
       
   252     return false;
       
   253 }
       
   254 
       
   255 void QGLPixelBuffer::releaseFromDynamicTexture()
       
   256 {
       
   257 }
       
   258 
       
   259 bool QGLPixelBuffer::hasOpenGLPbuffers()
       
   260 {
       
   261     bool ret = qt_resolve_pbuffer_extensions();
       
   262 
       
   263     if (!ret)
       
   264 	return false;
       
   265 
       
   266     int attribs[40];
       
   267     int num_configs = 0;
       
   268 
       
   269     qt_format_to_attrib_list(QGLFormat::defaultFormat(), attribs);
       
   270 
       
   271     GLXFBConfig *configs = glXChooseFBConfig(X11->display, X11->defaultScreen, attribs, &num_configs);
       
   272     GLXPbuffer pbuf = 0;
       
   273     GLXContext ctx = 0;
       
   274 
       
   275     if (configs && num_configs) {
       
   276         int pb_attribs[] = {GLX_PBUFFER_WIDTH, 128, GLX_PBUFFER_HEIGHT, 128, XNone};
       
   277         pbuf = glXCreatePbuffer(X11->display, configs[0], pb_attribs);
       
   278         ctx = glXCreateNewContext(X11->display, configs[0], GLX_RGBA_TYPE, 0, true);
       
   279         XFree(configs);
       
   280 	glXDestroyContext(X11->display, ctx);
       
   281 	glXDestroyPbuffer(X11->display, pbuf);
       
   282     }
       
   283     return pbuf && ctx;
       
   284 }
       
   285 
       
   286 QT_END_NAMESPACE