1 /* Copyright © 2013, Intel Corporation
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 /* Perform some simple drawing via OpenGL as follows:
24 * 1. Using EGL to construct an OpenGL context
25 * 2. By using dlopen to dynamically load libGL.so
26 * 3. By using eglGetProcAddress to lookup OpenGL functions
29 #define GL_GLEXT_PROTOTYPES
39 EGLBoolean (*my_eglBindAPI)(EGLenum);
40 EGLBoolean (*my_eglChooseConfig)(EGLDisplay, EGLint const *, EGLConfig *, EGLint, EGLint *);
41 EGLContext (*my_eglCreateContext)(EGLDisplay, EGLConfig, EGLContext, EGLint const *);
42 EGLSurface (*my_eglCreateWindowSurface)(EGLDisplay, EGLConfig, NativeWindowType, EGLint const *);
43 EGLBoolean (*my_eglGetConfigAttrib)(EGLDisplay, EGLConfig, EGLint, EGLint *);
44 EGLDisplay (*my_eglGetDisplay)(NativeDisplayType);
45 void * (*my_eglGetProcAddress)(char const *);
46 EGLBoolean (*my_eglInitialize)(EGLDisplay, EGLint *, EGLint *);
47 EGLBoolean (*my_eglMakeCurrent)(EGLDisplay, EGLSurface, EGLSurface, EGLContext);
48 EGLBoolean (*my_eglSwapBuffers)(EGLDisplay, EGLSurface);
50 /* The OpenGL header files give typedefs for the types of all
51 * functions provided by etensisons. Here, though, we're using
52 * glxGetProcAddress to lookup core functions, so we provide our own
54 typedef void (*FIPS_GLCLEAR_FN)(GLbitfield);
55 FIPS_GLCLEAR_FN my_glClear;
57 typedef void (*FIPS_GLCLEARCOLOR_FN)(GLclampf, GLclampf, GLclampf, GLclampf);
58 FIPS_GLCLEARCOLOR_FN my_glClearColor;
60 typedef void (*FIPS_GLVIEWPORT_FN)(GLint, GLint, GLsizei, GLsizei);
61 FIPS_GLVIEWPORT_FN my_glViewport;
63 #define COMMON_USE_EGL
64 #define COMMON_GL_PREFIX my_
65 #define COMMON_EGL_PREFIX my_
69 resolve_symbols (void)
71 void *egl_handle, *gl_handle;
74 egl_handle = dlopen ("libEGL.so", RTLD_LAZY);
75 if (egl_handle == NULL) {
76 fprintf (stderr, "Error: Failed to open libEGL.so\n");
80 gl_handle = dlopen ("libGL.so", RTLD_LAZY);
81 if (gl_handle == NULL) {
82 fprintf (stderr, "Error: Failed to open libGL.so\n");
89 my_eglBindAPI = dlsym (egl_handle, "eglBindAPI");
92 fprintf (stderr, "Failed to dlsym eglBindAPI: %s\n", error);
96 my_eglChooseConfig = dlsym (egl_handle, "eglChooseConfig");
99 fprintf (stderr, "Failed to dlsym eglChooseConfig: %s\n", error);
103 my_eglCreateContext = dlsym (egl_handle, "eglCreateContext");
106 fprintf (stderr, "Failed to dlsym eglCreateContext: %s\n", error);
110 my_eglCreateWindowSurface = dlsym (egl_handle, "eglCreateWindowSurface");
113 fprintf (stderr, "Failed to dlsym eglCreateWindowSurface: %s\n", error);
117 my_eglGetConfigAttrib = dlsym (egl_handle, "eglGetConfigAttrib");
120 fprintf (stderr, "Failed to dlsym eglGetConfigAttrib: %s\n", error);
124 my_eglGetDisplay = dlsym (egl_handle, "eglGetDisplay");
127 fprintf (stderr, "Failed to dlsym eglGetDisplay: %s\n", error);
131 my_eglGetProcAddress = dlsym (egl_handle, "eglGetProcAddress");
134 fprintf (stderr, "Failed to dlsym eglGetProcAddress: %s\n", error);
138 my_eglInitialize = dlsym (egl_handle, "eglInitialize");
141 fprintf (stderr, "Failed to dlsym eglInitialize: %s\n", error);
145 my_eglMakeCurrent = dlsym (egl_handle, "eglMakeCurrent");
148 fprintf (stderr, "Failed to dlsym eglMakeCurrent: %s\n", error);
152 my_eglSwapBuffers = dlsym (egl_handle, "eglSwapBuffers");
155 fprintf (stderr, "Failed to dlsym eglSwapBuffers: %s\n", error);
159 my_glClear = (FIPS_GLCLEAR_FN) my_eglGetProcAddress ("glClear");
160 if (my_glClear == NULL) {
161 fprintf (stderr, "Failed to eglGetProcAddress glClear\n");
165 my_glClearColor = (FIPS_GLCLEARCOLOR_FN) my_eglGetProcAddress ("glClearColor");
166 if (my_glClearColor == NULL) {
167 fprintf (stderr, "Failed to eglGetProcAddress glClearColor\n");
171 my_glViewport = (FIPS_GLVIEWPORT_FN) my_eglGetProcAddress ("glViewport");
172 if (my_glViewport == NULL) {
173 fprintf (stderr, "Failed to eglGetProcAddress glViewport\n");
187 XVisualInfo *visual_info;
191 dpy = util_x11_init_display ();
193 common_create_egl_context (dpy, EGL_OPENGL_API, &egl_dpy,
194 &ctx, &config, &visual_info);
196 window = util_x11_init_window (dpy, visual_info);
198 surface = my_eglCreateWindowSurface (egl_dpy, config, window, NULL);
200 common_make_current (egl_dpy, ctx, surface);
202 common_handle_events (dpy, egl_dpy, surface);
204 util_x11_fini_window (dpy, window);
206 util_x11_fini_display (dpy);