1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6
7 #include <GL/gl.h>
8 #include <GL/glx.h>
9
10 #include <X11/Xlib.h>
11 #include <X11/Xutil.h>
12
13 #include <string>
14
InitGraphics(Display ** display,Window * window,GLXContext * context)15 bool InitGraphics(Display** display,
16 Window* window,
17 GLXContext* context) {
18 const int kWindowWidth = 100;
19 const int kWindowHeight = 100;
20
21 *display = XOpenDisplay(NULL);
22 if (*display == NULL) {
23 printf("ERROR: XOpenDisplay failed\n");
24 return false;
25 }
26
27 Window root_window = DefaultRootWindow(*display);
28 GLint att[] = { GLX_RGBA,
29 GLX_DEPTH_SIZE,
30 24,
31 None };
32 XVisualInfo* vi = glXChooseVisual(*display, 0, att);
33 if (vi == NULL) {
34 printf("ERROR: glXChooseVisual failed\n");
35 return false;
36 }
37
38 XSetWindowAttributes swa;
39 swa.colormap = XCreateColormap(*display,
40 root_window,
41 vi->visual,
42 AllocNone);
43 *window = XCreateWindow(*display, root_window,
44 0, 0, kWindowWidth, kWindowHeight,
45 0, vi->depth, InputOutput, vi->visual,
46 CWColormap,
47 &swa);
48 XMapWindow(*display, *window);
49
50 *context = glXCreateContext(*display, vi, NULL, GL_TRUE);
51 if (*context == NULL) {
52 printf("ERROR: glXCreateContext failed\n");
53 } else {
54 glXMakeCurrent(*display, *window, *context);
55 }
56
57 XFree(vi);
58 return (*context != NULL);
59 }
60
ExitGraphics(Display * display,Window window,GLXContext context)61 void ExitGraphics(Display* display,
62 Window window,
63 GLXContext context) {
64 if (display != NULL) {
65 glXMakeCurrent(display, None, NULL);
66 if (context != NULL)
67 glXDestroyContext(display, context);
68 XDestroyWindow(display, window);
69 XCloseDisplay(display);
70 }
71 }
72
GetGLVersion()73 bool GetGLVersion() {
74 const GLubyte* version_string = glGetString(GL_VERSION);
75 if (version_string == NULL) {
76 printf("ERROR: glGetString(GL_VERSION) failed\n");
77 return false;
78 }
79 printf("GL_VERSION = %s\n", version_string);
80 return true;
81 }
82
GetGLExtensions()83 bool GetGLExtensions() {
84 const GLubyte* ext_string = glGetString(GL_EXTENSIONS);
85 if (ext_string == NULL) {
86 printf("ERROR: glGetString(GL_EXTENSIONS) failed\n");
87 return false;
88 }
89 printf("GL_EXTENSIONS = %s\n", ext_string);
90 return true;
91 }
92
GetGLXExtensions(Display * display)93 bool GetGLXExtensions(Display* display) {
94 const char* ext_string = glXQueryExtensionsString(display, 0);
95 if (ext_string == NULL) {
96 printf("ERROR: glXQueryExtensionsString failed\n");
97 return false;
98 }
99 printf("GLX_EXTENSIONS = %s\n", ext_string);
100 return true;
101 }
102
GetXExtensions(Display * display)103 bool GetXExtensions(Display* display) {
104 int ext_num;
105 char** ext_list = XListExtensions(display, &ext_num);
106 printf("X_EXTENSIONS =");
107 for (int i = 0; i < ext_num; ++i) {
108 printf(" %s", ext_list[i]);
109 }
110 printf("\n");
111 XFreeExtensionList(ext_list);
112 return true;
113 }
114
main(int argc,char * argv[])115 int main(int argc, char* argv[]) {
116 // Initialize graphics.
117 Display* display = NULL;
118 Window window = 0;
119 GLXContext context = NULL;
120 bool rt_code = InitGraphics(&display, &window, &context);
121
122 // Get OpenGL major/minor version number.
123 if (rt_code)
124 rt_code = GetGLVersion();
125
126 // Get OpenGL extentions.
127 if (rt_code)
128 rt_code = GetGLExtensions();
129
130 // Get GLX extensions.
131 if (rt_code)
132 rt_code = GetGLXExtensions(display);
133
134 // Get X11 extensions.
135 if (rt_code)
136 rt_code = GetXExtensions(display);
137
138 ExitGraphics(display, window, context);
139 printf("SUCCEED: run to the end\n");
140 return 0;
141 }
142
143