1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
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.
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.
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.
19 #include "glxbackend.h"
25 #include <GL/glxext.h>
29 #error "Valid GL contexts for GLX are: GL1"
32 // attributes for a double buffered visual in RGBA format with at least 4 bits per color
33 static int attrList[] =
35 GLX_RGBA, GLX_DOUBLEBUFFER,
42 VisualID GLXBackend::initialize(Display *display)
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");
57 funSwapIntervalMesa = 0;
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");
64 funSwapIntervalExt = 0;
67 vi = glXChooseVisual(display, DefaultScreen(display), attrList);
72 void GLXBackend::createContext(Window window)
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);
85 GLXBackend::~GLXBackend()
87 if (display != NULL) {
88 if (context != None) {
89 glXMakeCurrent(display, None, NULL);
90 glXDestroyContext(display, context);
96 void GLXBackend::swapBuffers() const
98 assert(context != None); // this implies the display is also initialized
99 glXSwapBuffers(display, window);
102 void GLXBackend::setSwapInterval(int i) const
104 assert(context != None);
105 if (funSwapIntervalExt)
106 funSwapIntervalExt(display, window, i);
107 else if (funSwapIntervalMesa)
108 funSwapIntervalMesa(i);
110 fprintf(stderr, "At least one of glXSwapIntervalMESA, glXSwapIntervalEXT must be provided by the system\n");