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