src/gui/egl/qegl_x11.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
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 QtGui 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 
       
    42 #include <QtCore/qdebug.h>
       
    43 
       
    44 #include <private/qt_x11_p.h>
       
    45 #include <QtGui/qx11info_x11.h>
       
    46 #include <private/qpixmapdata_p.h>
       
    47 #include <private/qpixmap_x11_p.h>
       
    48 
       
    49 #include <QtGui/qpaintdevice.h>
       
    50 #include <QtGui/qpixmap.h>
       
    51 #include <QtGui/qwidget.h>
       
    52 #include "qegl_p.h"
       
    53 
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 EGLSurface QEglContext::createSurface(QPaintDevice *device, const QEglProperties *properties)
       
    58 {
       
    59     // Create the native drawable for the paint device.
       
    60     int devType = device->devType();
       
    61     EGLNativePixmapType pixmapDrawable = 0;
       
    62     EGLNativeWindowType windowDrawable = 0;
       
    63     bool ok;
       
    64     if (devType == QInternal::Pixmap) {
       
    65         pixmapDrawable = (EGLNativePixmapType)(static_cast<QPixmap *>(device))->handle();
       
    66         ok = (pixmapDrawable != 0);
       
    67     } else if (devType == QInternal::Widget) {
       
    68         windowDrawable = (EGLNativeWindowType)(static_cast<QWidget *>(device))->winId();
       
    69         ok = (windowDrawable != 0);
       
    70     } else {
       
    71         ok = false;
       
    72     }
       
    73     if (!ok) {
       
    74         qWarning("QEglContext::createSurface(): Cannot create the native EGL drawable");
       
    75         return EGL_NO_SURFACE;
       
    76     }
       
    77 
       
    78     // Create the EGL surface to draw into, based on the native drawable.
       
    79     const int *props;
       
    80     if (properties)
       
    81         props = properties->properties();
       
    82     else
       
    83         props = 0;
       
    84     EGLSurface surf;
       
    85     if (devType == QInternal::Widget)
       
    86         surf = eglCreateWindowSurface(dpy, cfg, windowDrawable, props);
       
    87     else
       
    88         surf = eglCreatePixmapSurface(dpy, cfg, pixmapDrawable, props);
       
    89     if (surf == EGL_NO_SURFACE) {
       
    90         qWarning() << "QEglContext::createSurface(): Unable to create EGL surface:"
       
    91                    << errorString(eglGetError());
       
    92     }
       
    93     return surf;
       
    94 }
       
    95 
       
    96 EGLDisplay QEglContext::getDisplay(QPaintDevice *device)
       
    97 {
       
    98     Q_UNUSED(device);
       
    99     Display *xdpy = QX11Info::display();
       
   100     if (!xdpy) {
       
   101         qWarning("QEglContext::getDisplay(): X11 display is not open");
       
   102         return EGL_NO_DISPLAY;
       
   103     }
       
   104     return eglGetDisplay(EGLNativeDisplayType(xdpy));
       
   105 }
       
   106 
       
   107 static int countBits(unsigned long mask)
       
   108 {
       
   109     int count = 0;
       
   110     while (mask != 0) {
       
   111         if (mask & 1)
       
   112             ++count;
       
   113         mask >>= 1;
       
   114     }
       
   115     return count;
       
   116 }
       
   117 
       
   118 // Set the pixel format parameters from the visual in "xinfo".
       
   119 void QEglProperties::setVisualFormat(const QX11Info *xinfo)
       
   120 {
       
   121     if (!xinfo)
       
   122         return;
       
   123     Visual *visual = (Visual*)xinfo->visual();
       
   124     if (!visual)
       
   125         return;
       
   126     if (visual->c_class != TrueColor && visual->c_class != DirectColor)
       
   127         return;
       
   128     setValue(EGL_RED_SIZE, countBits(visual->red_mask));
       
   129     setValue(EGL_GREEN_SIZE, countBits(visual->green_mask));
       
   130     setValue(EGL_BLUE_SIZE, countBits(visual->blue_mask));
       
   131 
       
   132     EGLint alphaBits = 0;
       
   133 #if !defined(QT_NO_XRENDER)
       
   134     XRenderPictFormat *format;
       
   135     format = XRenderFindVisualFormat(xinfo->display(), visual);
       
   136     if (format && (format->type == PictTypeDirect) && format->direct.alphaMask) {
       
   137         alphaBits = countBits(format->direct.alphaMask);
       
   138         qDebug("QEglProperties::setVisualFormat() - visual's alphaMask is %d", alphaBits);
       
   139     }
       
   140 #endif
       
   141     setValue(EGL_ALPHA_SIZE, alphaBits);
       
   142 }
       
   143 
       
   144 extern const QX11Info *qt_x11Info(const QPaintDevice *pd);
       
   145 
       
   146 // Set pixel format and other properties based on a paint device.
       
   147 void QEglProperties::setPaintDeviceFormat(QPaintDevice *dev)
       
   148 {
       
   149     if (!dev)
       
   150         return;
       
   151     if (dev->devType() == QInternal::Image)
       
   152         setPixelFormat(static_cast<QImage *>(dev)->format());
       
   153     else
       
   154         setVisualFormat(qt_x11Info(dev));
       
   155 }
       
   156 
       
   157 QT_END_NAMESPACE