print the GL version in use
[gltest.git] / glxbackend.cpp
1 /* gltest - small OpenGL tearing test program
2  * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "glxbackend.h"
20
21 #include <stdio.h>
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <GL/glxext.h>
25 #include <string>
26
27 #if !defined(CON_GL1)
28 #error "Valid GL contexts for GLX are: GL1"
29 #endif
30
31 // attributes for a double buffered visual in RGBA format with at least 4 bits per color
32 static int attrList[] =                                             
33 {
34         GLX_RGBA, GLX_DOUBLEBUFFER,
35         GLX_RED_SIZE, 4,
36         GLX_GREEN_SIZE, 4,
37         GLX_BLUE_SIZE, 4,
38         None
39 };
40
41 VisualID GLXBackend::initialize(Display *display)
42 {
43         if (this->display == NULL) { // this implies that the context is also unitialized
44                 this->display = display;
45                 // debugging: show version information
46                 int glxMajor, glxMinor;
47                 glXQueryVersion(display, &glxMajor, &glxMinor);
48                 printf("Using GLX version: %d.%d\n", glxMajor, glxMinor);
49                 // check for swap control functions
50                 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
51                 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
52                         funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
53                         if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
54                 }
55                 else {
56                         funSwapIntervalMesa = 0;
57                 }
58                 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
59                         funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
60                         if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
61                 }
62                 else {
63                         funSwapIntervalExt = 0;
64                 }
65                 // get the visual
66                 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
67         }
68         return vi->visualid;
69 }
70
71 void GLXBackend::createContext(Window window)
72 {
73         assert(display != NULL && context == None);
74         this->window = window;
75         // create context with that window
76         context = glXCreateContext(display, vi, 0, GL_TRUE);                             
77         glXMakeCurrent(display, window, context);                                      
78         assert(glXIsDirect(display, context));
79 }
80
81 GLXBackend::~GLXBackend()
82 {
83         if (display != NULL) {
84                 if (context != None) {
85                         glXMakeCurrent(display, None, NULL);
86                         glXDestroyContext(display, context);
87                 }
88                 XFree(vi);
89         }
90 }
91
92 void GLXBackend::swapBuffers() const
93 {
94         assert(context != None); // this implies the display is also initialized
95         glXSwapBuffers(display, window);
96 }
97
98 void GLXBackend::setSwapInterval(int i) const
99 {
100         assert(context != None);
101         if (funSwapIntervalExt)
102                 funSwapIntervalExt(display, window, i);
103         else if (funSwapIntervalMesa)
104                 funSwapIntervalMesa(i);
105         else {
106                 fprintf(stderr, "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system\n");
107                 abort();
108         }
109 }