9f8ed3e257d5aac8ec10239ecd33b9934332028b
[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 /* attributes for a double buffered visual in RGBA format with at least
28 * 4 bits per color */                       
29 static int attrList[] =                                             
30 {                                                                      
31         GLX_RGBA, GLX_DOUBLEBUFFER,
32         GLX_RED_SIZE, 4,                                                   
33         GLX_GREEN_SIZE, 4,                                                 
34         GLX_BLUE_SIZE, 4,
35         None                                                               
36 };
37
38 VisualID GLXBackend::initialize(Display *display)
39 {
40         if (this->display == NULL) { // this implies that the context is also unitialized
41                 this->display = display;
42                 // debugging: show version information
43                 int glxMajor, glxMinor;
44                 glXQueryVersion(display, &glxMajor, &glxMinor);
45                 printf("Using GLX version %d.%d\n", glxMajor, glxMinor);
46                 // check for swap control functions
47                 const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
48                 if (std::string(extensions).find("GLX_MESA_swap_control") != std::string::npos) {
49                         funSwapIntervalMesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalMESA");
50                         if (funSwapIntervalMesa) printf("glXSwapIntervalMESA is present\n");
51                 }
52                 else {
53                         funSwapIntervalMesa = 0;
54                 }
55                 if (std::string(extensions).find("GLX_EXT_swap_control") != std::string::npos) {
56                         funSwapIntervalExt = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
57                         if (funSwapIntervalExt) printf("glXSwapIntervalEXT is present\n");
58                 }
59                 else {
60                         funSwapIntervalExt = 0;
61                 }
62                 // get the visual
63                 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
64         }
65         return vi->visualid;
66 }
67
68 void GLXBackend::createContext(Window window)
69 {
70         assert(display != NULL && context == None);
71         this->window = window;
72         // create context with that window
73         context = glXCreateContext(display, vi, 0, GL_TRUE);                             
74         glXMakeCurrent(display, window, context);                                      
75         assert(glXIsDirect(display, context));
76 }
77
78 GLXBackend::~GLXBackend()
79 {
80         if (display != NULL) {
81                 if (context != None) {
82                         glXMakeCurrent(display, None, NULL);
83                         glXDestroyContext(display, context);
84                 }
85                 XFree(vi);
86         }
87 }
88
89 void GLXBackend::swapBuffers() const
90 {
91         assert(context != None); // this implies the display is also initialized
92         glXSwapBuffers(display, window);
93 }
94
95 void GLXBackend::setSwapInterval(int i) const
96 {
97         assert(context != None);
98         if (funSwapIntervalExt)
99                 funSwapIntervalExt(display, window, i);
100         else if (funSwapIntervalMesa)
101                 funSwapIntervalMesa(i);
102         else {
103                 fprintf(stderr, "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system\n");
104                 abort();
105         }
106 }