Qt 4.8
qglxconvenience.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qglxconvenience.h"
43 
44 #include <QtCore/QVector>
45 
46 #ifndef QT_NO_XRENDER
47 #include <X11/extensions/Xrender.h>
48 #endif
49 
50 enum {
51  XFocusOut = FocusOut,
52  XFocusIn = FocusIn,
53  XKeyPress = KeyPress,
54  XKeyRelease = KeyRelease,
56  XRevertToParent = RevertToParent,
57  XGrayScale = GrayScale,
59 };
60 #undef FocusOut
61 #undef FocusIn
62 #undef KeyPress
63 #undef KeyRelease
64 #undef None
65 #undef RevertToParent
66 #undef GrayScale
67 #undef CursorShape
68 
69 #ifdef FontChange
70 #undef FontChange
71 #endif
72 
74 {
75  QVector<int> spec(48);
76  int i = 0;
77 
78  spec[i++] = GLX_LEVEL;
79  spec[i++] = 0;
80  spec[i++] = GLX_DRAWABLE_TYPE; spec[i++] = drawableBit;
81 
82  if (format.rgba()) {
83  spec[i++] = GLX_RENDER_TYPE; spec[i++] = GLX_RGBA_BIT;
84  spec[i++] = GLX_RED_SIZE; spec[i++] = (format.redBufferSize() == -1) ? 1 : format.redBufferSize();
85  spec[i++] = GLX_GREEN_SIZE; spec[i++] = (format.greenBufferSize() == -1) ? 1 : format.greenBufferSize();
86  spec[i++] = GLX_BLUE_SIZE; spec[i++] = (format.blueBufferSize() == -1) ? 1 : format.blueBufferSize();
87  if (format.alpha()) {
88  spec[i++] = GLX_ALPHA_SIZE; spec[i++] = (format.alphaBufferSize() == -1) ? 1 : format.alphaBufferSize();
89  }
90 
91  if (format.accum()) {
92  spec[i++] = GLX_ACCUM_RED_SIZE; spec[i++] = (format.accumBufferSize() == -1) ? 1 : format.accumBufferSize();
93  spec[i++] = GLX_ACCUM_GREEN_SIZE; spec[i++] = (format.accumBufferSize() == -1) ? 1 : format.accumBufferSize();
94  spec[i++] = GLX_ACCUM_BLUE_SIZE; spec[i++] = (format.accumBufferSize() == -1) ? 1 : format.accumBufferSize();
95 
96  if (format.alpha()) {
97  spec[i++] = GLX_ACCUM_ALPHA_SIZE; spec[i++] = (format.accumBufferSize() == -1) ? 1 : format.accumBufferSize();
98  }
99  }
100  } else {
101  spec[i++] = GLX_RENDER_TYPE; spec[i++] = GLX_COLOR_INDEX_BIT; //I'm really not sure if this works....
102  spec[i++] = GLX_BUFFER_SIZE; spec[i++] = 8;
103  }
104 
105  spec[i++] = GLX_DOUBLEBUFFER; spec[i++] = format.doubleBuffer() ? True : False;
106  spec[i++] = GLX_STEREO; spec[i++] = format.stereo() ? True : False;
107 
108  if (format.depth()) {
109  spec[i++] = GLX_DEPTH_SIZE; spec[i++] = (format.depthBufferSize() == -1) ? 1 : format.depthBufferSize();
110  }
111 
112  if (format.stencil()) {
113  spec[i++] = GLX_STENCIL_SIZE; spec[i++] = (format.stencilBufferSize() == -1) ? 1 : format.stencilBufferSize();
114  }
115  if (format.sampleBuffers()) {
116  spec[i++] = GLX_SAMPLE_BUFFERS_ARB;
117  spec[i++] = 1;
118  spec[i++] = GLX_SAMPLES_ARB;
119  spec[i++] = format.samples() == -1 ? 4 : format.samples();
120  }
121 
122  spec[i++] = XNone;
123  return spec;
124 }
125 
126 GLXFBConfig qglx_findConfig(Display *display, int screen , const QPlatformWindowFormat &format, int drawableBit)
127 {
128  bool reduced = true;
129  GLXFBConfig chosenConfig = 0;
130  QPlatformWindowFormat reducedFormat = format;
131  while (!chosenConfig && reduced) {
132  QVector<int> spec = qglx_buildSpec(reducedFormat, drawableBit);
133  int confcount = 0;
134  GLXFBConfig *configs;
135  configs = glXChooseFBConfig(display, screen,spec.constData(),&confcount);
136  if (confcount)
137  {
138  for (int i = 0; i < confcount; i++) {
139  chosenConfig = configs[i];
140  // Make sure we try to get an ARGB visual if the format asked for an alpha:
141  if (reducedFormat.alpha()) {
142  int alphaSize;
143  glXGetFBConfigAttrib(display,configs[i],GLX_ALPHA_SIZE,&alphaSize);
144  if (alphaSize > 0) {
145  XVisualInfo *visual = glXGetVisualFromFBConfig(display, chosenConfig);
146 #if !defined(QT_NO_XRENDER)
147  XRenderPictFormat *pictFormat = XRenderFindVisualFormat(display, visual->visual);
148  if (pictFormat->direct.alphaMask > 0)
149  break;
150 #else
151  if (visual->depth == 32)
152  break;
153 #endif
154  }
155  } else {
156  break; // Just choose the first in the list if there's no alpha requested
157  }
158  }
159 
160  XFree(configs);
161  }
162  reducedFormat = qglx_reducePlatformWindowFormat(reducedFormat,&reduced);
163  }
164 
165  if (!chosenConfig)
166  qWarning("Warning: no suitable glx confiuration found");
167 
168  return chosenConfig;
169 }
170 
172 {
173  GLXFBConfig config = qglx_findConfig(display,screen,format);
174  XVisualInfo *visualInfo = glXGetVisualFromFBConfig(display,config);
175  return visualInfo;
176 }
177 
179 {
181  int redSize = 0;
182  int greenSize = 0;
183  int blueSize = 0;
184  int alphaSize = 0;
185  int depthSize = 0;
186  int stencilSize = 0;
187  int sampleBuffers = 0;
188  int sampleCount = 0;
189  int level = 0;
190  int rgba = 0;
191  int stereo = 0;
192  int accumSizeA = 0;
193  int accumSizeR = 0;
194  int accumSizeG = 0;
195  int accumSizeB = 0;
196 
197  XVisualInfo *vi = glXGetVisualFromFBConfig(display,config);
198  glXGetConfig(display,vi,GLX_RGBA,&rgba);
199  XFree(vi);
200  glXGetFBConfigAttrib(display, config, GLX_RED_SIZE, &redSize);
201  glXGetFBConfigAttrib(display, config, GLX_GREEN_SIZE, &greenSize);
202  glXGetFBConfigAttrib(display, config, GLX_BLUE_SIZE, &blueSize);
203  glXGetFBConfigAttrib(display, config, GLX_ALPHA_SIZE, &alphaSize);
204  glXGetFBConfigAttrib(display, config, GLX_DEPTH_SIZE, &depthSize);
205  glXGetFBConfigAttrib(display, config, GLX_STENCIL_SIZE, &stencilSize);
206  glXGetFBConfigAttrib(display, config, GLX_SAMPLES, &sampleBuffers);
207  glXGetFBConfigAttrib(display, config, GLX_LEVEL, &level);
208  glXGetFBConfigAttrib(display, config, GLX_STEREO, &stereo);
209  glXGetFBConfigAttrib(display, config, GLX_ACCUM_ALPHA_SIZE, &accumSizeA);
210  glXGetFBConfigAttrib(display, config, GLX_ACCUM_RED_SIZE, &accumSizeR);
211  glXGetFBConfigAttrib(display, config, GLX_ACCUM_GREEN_SIZE, &accumSizeG);
212  glXGetFBConfigAttrib(display, config, GLX_ACCUM_BLUE_SIZE, &accumSizeB);
213 
214  format.setRedBufferSize(redSize);
215  format.setGreenBufferSize(greenSize);
216  format.setBlueBufferSize(blueSize);
217  format.setAlphaBufferSize(alphaSize);
218  format.setDepthBufferSize(depthSize);
219  format.setStencilBufferSize(stencilSize);
220  format.setSampleBuffers(sampleBuffers);
221  if (format.sampleBuffers()) {
222  glXGetFBConfigAttrib(display, config, GLX_SAMPLES_ARB, &sampleCount);
223  format.setSamples(sampleCount);
224  }
225 
226  format.setDirectRendering(glXIsDirect(display, ctx));
227  format.setRgba(rgba);
228  format.setStereo(stereo);
229  format.setAccumBufferSize(accumSizeB);
230 
231  return format;
232 }
233 
235 {
236  QPlatformWindowFormat retFormat = format;
237  *reduced = true;
238 
239  if (retFormat.sampleBuffers()) {
240  retFormat.setSampleBuffers(false);
241  } else if (retFormat.stereo()) {
242  retFormat.setStereo(false);
243  } else if (retFormat.accum()) {
244  retFormat.setAccum(false);
245  }else if (retFormat.stencil()) {
246  retFormat.setStencil(false);
247  }else if (retFormat.alpha()) {
248  retFormat.setAlpha(false);
249  }else if (retFormat.depth()) {
250  retFormat.setDepth(false);
251  }else if (retFormat.doubleBuffer()) {
252  retFormat.setDoubleBuffer(false);
253  }else{
254  *reduced = false;
255  }
256  return retFormat;
257 }
int greenBufferSize() const
Returns the green buffer size.
bool rgba() const
Returns true if RGBA color mode is set.
void setDepthBufferSize(int size)
Set the minimum depth buffer size to size.
#define GLX_RGBA_BIT
void setStereo(bool enable)
If enable is true enables stereo buffering; otherwise disables stereo buffering.
QVector< int > qglx_buildSpec(const QPlatformWindowFormat &format, int drawableBit)
QPlatformWindowFormat qglx_platformWindowFromGLXFBConfig(Display *display, GLXFBConfig config, GLXContext ctx)
void setDirectRendering(bool enable)
If enable is true enables direct rendering; otherwise disables direct rendering.
int samples() const
Returns the number of samples per pixel when multisampling is enabled.
The QPlatformWindowFormat class specifies the display format of an OpenGL rendering context and if po...
#define glXGetFBConfigAttrib
void setBlueBufferSize(int size)
Set the preferred blue buffer size to size.
void setStencilBufferSize(int size)
Set the preferred stencil buffer size to size.
int stencilBufferSize() const
Returns the stencil buffer size.
bool doubleBuffer() const
Returns true if double buffering is enabled; otherwise returns false.
#define glXChooseFBConfig
bool stereo() const
Returns true if stereo buffering is enabled; otherwise returns false.
#define GLX_SAMPLE_BUFFERS_ARB
Definition: qgl_x11.cpp:96
#define GLX_DRAWABLE_TYPE
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
#define GLX_RENDER_TYPE
void setAccumBufferSize(int size)
Set the preferred accumulation buffer size, where size is the bit depth for each RGBA component...
void setAlphaBufferSize(int size)
Set the preferred alpha buffer size to size.
QPlatformWindowFormat qglx_reducePlatformWindowFormat(const QPlatformWindowFormat &format, bool *reduced)
Q_CORE_EXPORT void qWarning(const char *,...)
int depthBufferSize() const
Returns the depth buffer size.
int alphaBufferSize() const
Returns the alpha buffer size.
int accumBufferSize() const
Returns the accumulation buffer size.
void setSamples(int numSamples)
Set the preferred number of samples per pixel when multisampling is enabled to numSamples.
bool depth() const
Returns true if the depth buffer is enabled; otherwise returns false.
void setDoubleBuffer(bool enable)
If enable is true sets double buffering; otherwise sets single buffering.
void setRgba(bool enable)
If enable is true sets RGBA mode.
#define None
GLXFBConfig qglx_findConfig(Display *display, int screen, const QPlatformWindowFormat &format, int drawableBit)
bool accum() const
Returns true if the accumulation buffer is enabled; otherwise returns false.
#define GLX_SAMPLES_ARB
Definition: qgl_x11.cpp:97
struct _XDisplay Display
Definition: qwindowdefs.h:115
void setAlpha(bool enable)
If enable is true enables the alpha buffer; otherwise disables the alpha buffer.
CursorShape
Definition: qnamespace.h:1262
#define ctx
Definition: qgl.cpp:6094
void setSampleBuffers(bool enable)
If enable is true, a GL context with multisample buffer support is picked; otherwise ignored...
bool stencil() const
Returns true if the stencil buffer is enabled; otherwise returns false.
int blueBufferSize() const
Returns the blue buffer size.
void setStencil(bool enable)
If enable is true enables the stencil buffer; otherwise disables the stencil buffer.
int redBufferSize() const
Returns the red buffer size.
void setRedBufferSize(int size)
Set the preferred red buffer size to size.
XVisualInfo * qglx_findVisualInfo(Display *display, int screen, const QPlatformWindowFormat &format)
bool alpha() const
Returns true if the alpha buffer in the framebuffer is enabled; otherwise returns false...
const T * constData() const
Returns a const pointer to the data stored in the vector.
Definition: qvector.h:154
bool sampleBuffers() const
Returns true if multisample buffer support is enabled; otherwise returns false.
void setAccum(bool enable)
If enable is true enables the accumulation buffer; otherwise disables the accumulation buffer...
void setGreenBufferSize(int size)
Set the preferred green buffer size to size.
void setDepth(bool enable)
If enable is true enables the depth buffer; otherwise disables the depth buffer.