Merge branch '7.8'
[mesa.git] / progs / egl / opengles1 / es1_info.c
1 /*
2 * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved.
3 */
4
5 /*
6 * List OpenGL ES extensions.
7 * Print ES 1 or ES 2 extensions depending on which library we're
8 * linked with: libGLESv1_CM.so vs libGLESv2.so
9 */
10
11 #define GL_GLEXT_PROTOTYPES
12
13 #include <assert.h>
14 #include <math.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <X11/Xlib.h>
19 #include <X11/Xutil.h>
20 #include <X11/keysym.h>
21 #include <GLES/gl.h>
22 #include <GLES/glext.h>
23 #include <EGL/egl.h>
24
25
26 /*
27 * Print a list of extensions, with word-wrapping.
28 */
29 static void
30 print_extension_list(const char *ext)
31 {
32 const char *indentString = " ";
33 const int indent = 4;
34 const int max = 79;
35 int width, i, j;
36
37 if (!ext || !ext[0])
38 return;
39
40 width = indent;
41 printf(indentString);
42 i = j = 0;
43 while (1) {
44 if (ext[j] == ' ' || ext[j] == 0) {
45 /* found end of an extension name */
46 const int len = j - i;
47 if (width + len > max) {
48 /* start a new line */
49 printf("\n");
50 width = indent;
51 printf(indentString);
52 }
53 /* print the extension name between ext[i] and ext[j] */
54 while (i < j) {
55 printf("%c", ext[i]);
56 i++;
57 }
58 /* either we're all done, or we'll continue with next extension */
59 width += len + 1;
60 if (ext[j] == 0) {
61 break;
62 }
63 else {
64 i++;
65 j++;
66 if (ext[j] == 0)
67 break;
68 printf(", ");
69 width += 2;
70 }
71 }
72 j++;
73 }
74 printf("\n");
75 }
76
77
78 static void
79 info(EGLDisplay egl_dpy)
80 {
81 const char *s;
82
83 s = eglQueryString(egl_dpy, EGL_VERSION);
84 printf("EGL_VERSION = %s\n", s);
85
86 s = eglQueryString(egl_dpy, EGL_VENDOR);
87 printf("EGL_VENDOR = %s\n", s);
88
89 s = eglQueryString(egl_dpy, EGL_EXTENSIONS);
90 printf("EGL_EXTENSIONS = %s\n", s);
91
92 s = eglQueryString(egl_dpy, EGL_CLIENT_APIS);
93 printf("EGL_CLIENT_APIS = %s\n", s);
94
95 printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
96 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
97 printf("GL_EXTENSIONS:\n");
98 print_extension_list((char *) glGetString(GL_EXTENSIONS));
99 }
100
101
102 /*
103 * Create an RGB, double-buffered X window.
104 * Return the window and context handles.
105 */
106 static void
107 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
108 const char *name,
109 int x, int y, int width, int height, int es_ver,
110 Window *winRet,
111 EGLContext *ctxRet,
112 EGLSurface *surfRet)
113 {
114 EGLint attribs[] = {
115 EGL_RENDERABLE_TYPE, 0x0,
116 EGL_RED_SIZE, 1,
117 EGL_GREEN_SIZE, 1,
118 EGL_BLUE_SIZE, 1,
119 EGL_NONE
120 };
121 EGLint ctx_attribs[] = {
122 EGL_CONTEXT_CLIENT_VERSION, 0,
123 EGL_NONE
124 };
125
126 int scrnum;
127 XSetWindowAttributes attr;
128 unsigned long mask;
129 Window root;
130 Window win;
131 XVisualInfo *visInfo, visTemplate;
132 int num_visuals;
133 EGLContext ctx;
134 EGLConfig config;
135 EGLint num_configs;
136 EGLint vid;
137
138 scrnum = DefaultScreen( x_dpy );
139 root = RootWindow( x_dpy, scrnum );
140
141 if (es_ver == 1)
142 attribs[1] = EGL_OPENGL_ES_BIT;
143 else
144 attribs[1] = EGL_OPENGL_ES2_BIT;
145 ctx_attribs[1] = es_ver;
146
147 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
148 printf("Error: couldn't get an EGL visual config\n");
149 exit(1);
150 }
151
152 assert(config);
153 assert(num_configs > 0);
154
155 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
156 printf("Error: eglGetConfigAttrib() failed\n");
157 exit(1);
158 }
159
160 /* The X window visual must match the EGL config */
161 visTemplate.visualid = vid;
162 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
163 if (!visInfo) {
164 printf("Error: couldn't get X visual\n");
165 exit(1);
166 }
167
168 /* window attributes */
169 attr.background_pixel = 0;
170 attr.border_pixel = 0;
171 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
172 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
173 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
174
175 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
176 0, visInfo->depth, InputOutput,
177 visInfo->visual, mask, &attr );
178
179 /* set hints and properties */
180 {
181 XSizeHints sizehints;
182 sizehints.x = x;
183 sizehints.y = y;
184 sizehints.width = width;
185 sizehints.height = height;
186 sizehints.flags = USSize | USPosition;
187 XSetNormalHints(x_dpy, win, &sizehints);
188 XSetStandardProperties(x_dpy, win, name, name,
189 None, (char **)NULL, 0, &sizehints);
190 }
191
192 eglBindAPI(EGL_OPENGL_ES_API);
193
194 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs );
195 if (!ctx) {
196 printf("Error: eglCreateContext failed\n");
197 exit(1);
198 }
199
200 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
201
202 if (!*surfRet) {
203 printf("Error: eglCreateWindowSurface failed\n");
204 exit(1);
205 }
206
207 XFree(visInfo);
208
209 *winRet = win;
210 *ctxRet = ctx;
211 }
212
213
214 static void
215 usage(void)
216 {
217 printf("Usage:\n");
218 printf(" -display <displayname> set the display to run on\n");
219 }
220
221
222 int
223 main(int argc, char *argv[])
224 {
225 const int winWidth = 400, winHeight = 300;
226 Display *x_dpy;
227 Window win;
228 EGLSurface egl_surf;
229 EGLContext egl_ctx;
230 EGLDisplay egl_dpy;
231 char *dpyName = NULL;
232 EGLint egl_major, egl_minor, es_ver;
233 int i;
234
235 for (i = 1; i < argc; i++) {
236 if (strcmp(argv[i], "-display") == 0) {
237 dpyName = argv[i+1];
238 i++;
239 }
240 else {
241 usage();
242 return -1;
243 }
244 }
245
246 x_dpy = XOpenDisplay(dpyName);
247 if (!x_dpy) {
248 printf("Error: couldn't open display %s\n",
249 dpyName ? dpyName : getenv("DISPLAY"));
250 return -1;
251 }
252
253 egl_dpy = eglGetDisplay(x_dpy);
254 if (!egl_dpy) {
255 printf("Error: eglGetDisplay() failed\n");
256 return -1;
257 }
258
259 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
260 printf("Error: eglInitialize() failed\n");
261 return -1;
262 }
263
264 es_ver = 1;
265 /* decide the version from the executable's name */
266 if (argc > 0 && argv[0] && strstr(argv[0], "es2"))
267 es_ver = 2;
268 make_x_window(x_dpy, egl_dpy,
269 "ES info", 0, 0, winWidth, winHeight, es_ver,
270 &win, &egl_ctx, &egl_surf);
271
272 /*XMapWindow(x_dpy, win);*/
273 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
274 printf("Error: eglMakeCurrent() failed\n");
275 return -1;
276 }
277
278 info(egl_dpy);
279
280 eglDestroyContext(egl_dpy, egl_ctx);
281 eglDestroySurface(egl_dpy, egl_surf);
282 eglTerminate(egl_dpy);
283
284
285 XDestroyWindow(x_dpy, win);
286 XCloseDisplay(x_dpy);
287
288 return 0;
289 }