Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfRenderingContext_GLX.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "glfRenderingContext_GLX.hh"
26 #include <OpenGLFramework/Base/glfException.hh>
27 
28 using namespace BIAS;
29 using namespace std;
30 
32 {
33  display_ = NULL;
34  drawable_ = None;
35  context_ = NULL;
36 }
37 
39 {
40  Destroy();
41  // make sure Destroy was called by now.
42  BIASASSERT(display_ == NULL);
43  BIASASSERT(drawable_ == None);
44  BIASASSERT(context_ == NULL);
45 }
46 
48 {
49  Destroy();
50 
51  // open connect to the X server
52  display_ = XOpenDisplay(NULL);
53  if (display_ == NULL) {
54  GLF_THROW_EXCEPTION("Failed to connect to the X server");
55  }
56 
57  // query GLX version
58  int major, minor;
59  if (glXQueryVersion(display_, &major, &minor) == False) {
60  GLF_THROW_EXCEPTION("Failed to query GLX version");
61  }
62  // get framebuffer configurations
63  int attribs[] = {
64  GLX_DRAWABLE_TYPE, GetDrawableType(),
65  None
66  };
67  int numFbConfigs;
68  GLXFBConfig* fbConfigs =
69  glXChooseFBConfig(display_, DefaultScreen(display_),
70  attribs, &numFbConfigs);
71  if (fbConfigs == NULL || numFbConfigs == 0) {
72  GLF_THROW_EXCEPTION("Failed to get GLX framebuffer configurations");
73  }
74 
75  // choose framebuffer configuration
76  GLXFBConfig fbConfig = None;
77  /*
78  cout<<"Config:"<<endl;
79  cout<<"doubleBuffer:"<<config.doubleBuffer<<endl;
80  cout<<"redSize:"<<config.redSize<<endl;
81  cout<<"greenSize:"<<config.greenSize<<endl;
82  cout<<"blueSize:"<<config.blueSize<<endl;
83  cout<<"alphaSize:"<<config.alphaSize<<endl;
84  cout<<"depthSize:"<<config.depthSize<<endl;
85  cout<<"stencilSize:"<<config.stencilSize<<endl<<endl;
86  cout<<"numFbConfigs:"<<numFbConfigs<<endl;
87  */
88  for (int i = 0; i < numFbConfigs; i++) {
89  int doubleBuffer, redSize, greenSize, blueSize, alphaSize, depthSize, stencilSize;
90  glXGetFBConfigAttrib(display_, fbConfigs[i], GLX_DOUBLEBUFFER, &doubleBuffer);
91  glXGetFBConfigAttrib(display_, fbConfigs[i], GLX_RED_SIZE, &redSize);
92  glXGetFBConfigAttrib(display_, fbConfigs[i], GLX_GREEN_SIZE, &greenSize);
93  glXGetFBConfigAttrib(display_, fbConfigs[i], GLX_BLUE_SIZE, &blueSize);
94  glXGetFBConfigAttrib(display_, fbConfigs[i], GLX_ALPHA_SIZE, &alphaSize);
95  glXGetFBConfigAttrib(display_, fbConfigs[i], GLX_DEPTH_SIZE, &depthSize);
96  glXGetFBConfigAttrib(display_, fbConfigs[i], GLX_STENCIL_SIZE, &stencilSize);
97 
98  /*
99  cout<<"doubleBuffer:"<<doubleBuffer<<endl;
100  cout<<"redSize:"<<redSize<<endl;
101  cout<<"greenSize:"<<greenSize<<endl;
102  cout<<"blueSize:"<<blueSize<<endl;
103  cout<<"alphaSize:"<<alphaSize<<endl;
104  cout<<"depthSize:"<<depthSize<<endl;
105  cout<<"stencilSize:"<<stencilSize<<endl<<endl;
106  */
107  // if (config.doubleBuffer == (doubleBuffer == True) &&
108  if (config.doubleBuffer == doubleBuffer &&
109  config.redSize == redSize &&
110  config.greenSize == greenSize &&
111  config.blueSize == blueSize &&
112  config.alphaSize == alphaSize &&
113  config.depthSize == depthSize &&
114  config.stencilSize == stencilSize) {
115  fbConfig = fbConfigs[i];
116  break;
117  }
118  }
119  if (fbConfig == None) {
120  GLF_THROW_EXCEPTION("Rendering context configuration is unsupported");
121  }
122 
123  // create drawable
124  drawable_ = CreateDrawable(config.width, config.height, fbConfig);
125  BIASASSERT(drawable_ != None);
126 
127  // create context
128  context_ = glXCreateNewContext(display_, fbConfig,
129  GLX_RGBA_TYPE, NULL, True);
130  if (context_ == NULL) {
131  GLF_THROW_EXCEPTION("Failed to create GLX context");
132  }
133  if(!glXIsDirect(display_, context_)){
134  GLF_THROW_EXCEPTION("Failed to create a direct GLX context");
135  }
136  // init GLEW
137  MakeCurrent();
138  InitGlew();
139 }
140 
142 {
143  if (context_ != NULL) {
144  if (glXGetCurrentContext() == context_) {
145  glXMakeCurrent(display_, None, NULL);
146  }
147  glXDestroyContext(display_, context_);
148  context_ = NULL;
149  }
150 
151  // DestroyDrawable() is called in destructor of glfBuffer_GLX
152  if (display_ != NULL) {
153  XCloseDisplay(display_);
154  display_ = NULL;
155  }
156  drawable_ = None;
157 }
158 
160 {
161  BIASASSERT(display_ != NULL && drawable_ != None && context_ != NULL);
162 
163  if (glXMakeCurrent(display_, drawable_, context_) == False) {
164  GLF_THROW_EXCEPTION("Failed to set current GLX context");
165  }
166 }
167 
169 {
170  BIASASSERT(display_ != NULL && drawable_ != None && context_ != NULL);
171 
172  if (glXMakeCurrent(display_, None, NULL) == False) {
173  GLF_THROW_EXCEPTION("Failed to release current GLX context");
174  }
175 }
176 
178 {
179  BIASASSERT(display_ != NULL && drawable_ != None && context_ != NULL);
180 
181  // query size
182  unsigned int width, height;
183  glXQueryDrawable(display_, drawable_, GLX_WIDTH, &width);
184  glXQueryDrawable(display_, drawable_, GLX_HEIGHT, &height);
185  config.width = width;
186  config.height = height;
187 
188  // get framebuffer config XID
189  unsigned int fbConfigId;
190  glXQueryDrawable(display_, drawable_, GLX_FBCONFIG_ID, &fbConfigId);
191 
192  // get framebuffer config
193  int signedFbConfigId = (int)fbConfigId;
194  int attribs[] = {
195  GLX_FBCONFIG_ID, signedFbConfigId,
196  None
197  };
198  int numFbConfigs;
199  GLXFBConfig* fbConfig =
200  glXChooseFBConfig(display_, DefaultScreen(display_), attribs, &numFbConfigs);
201  if (fbConfig == NULL || numFbConfigs == 0) {
202  GLF_THROW_EXCEPTION("Failed to get framebuffer config");
203  }
204 
205  // get framebuffer config attributes
206  int tmp = int(config.doubleBuffer);
207  glXGetFBConfigAttrib(display_, *fbConfig, GLX_DOUBLEBUFFER, &tmp);
208  glXGetFBConfigAttrib(display_, *fbConfig, GLX_RED_SIZE, &config.redSize);
209  glXGetFBConfigAttrib(display_, *fbConfig, GLX_GREEN_SIZE, &config.greenSize);
210  glXGetFBConfigAttrib(display_, *fbConfig, GLX_BLUE_SIZE, &config.blueSize);
211  glXGetFBConfigAttrib(display_, *fbConfig, GLX_ALPHA_SIZE, &config.alphaSize);
212  glXGetFBConfigAttrib(display_, *fbConfig, GLX_DEPTH_SIZE, &config.depthSize);
213  glXGetFBConfigAttrib(display_, *fbConfig, GLX_STENCIL_SIZE, &config.stencilSize);
214  config.doubleBuffer = double(tmp);
215 }
virtual void MakeCurrent()
Makes this context the current target for OpenGL calls.
virtual void Destroy()
Uninitializes the rendering context.
virtual void GetConfig(glfRenderingContextConfig &config)
Gets the currently used config of the rendering context.
virtual void Init(const glfRenderingContextConfig &config)
Initializes the rendering context with the given configuration.
virtual void DoneCurrent()
Removes the current state of this context.
Configuration for a rendering context.