|
| 1 | +#include <assert.h> |
| 2 | +#include <math.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <X11/Xlib.h> |
| 7 | +#include <X11/Xutil.h> |
| 8 | +#include <X11/keysym.h> |
| 9 | +#include <GLES2/gl2.h> /* use OpenGL ES 2.x */ |
| 10 | +#include <EGL/egl.h> |
| 11 | + |
| 12 | +void run_tests(void); |
| 13 | + |
| 14 | +static void reshape(int width, int height) |
| 15 | +{ |
| 16 | + glViewport(0, 0, (GLint) width, (GLint) height); |
| 17 | +} |
| 18 | + |
| 19 | +static void |
| 20 | +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, |
| 21 | + const char *name, |
| 22 | + int x, int y, int width, int height, |
| 23 | + Window *winRet, |
| 24 | + EGLContext *ctxRet, |
| 25 | + EGLSurface *surfRet) |
| 26 | +{ |
| 27 | + static const EGLint attribs[] = { |
| 28 | + EGL_RED_SIZE, 1, |
| 29 | + EGL_GREEN_SIZE, 1, |
| 30 | + EGL_BLUE_SIZE, 1, |
| 31 | +/* EGL_DEPTH_SIZE, 1, */ |
| 32 | + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 33 | + EGL_NONE |
| 34 | + }; |
| 35 | + static const EGLint ctx_attribs[] = { |
| 36 | + EGL_CONTEXT_CLIENT_VERSION, 2, |
| 37 | + EGL_NONE |
| 38 | + }; |
| 39 | + |
| 40 | + int scrnum; |
| 41 | + XSetWindowAttributes attr; |
| 42 | + unsigned long mask; |
| 43 | + Window root; |
| 44 | + Window win; |
| 45 | + XVisualInfo *visInfo, visTemplate; |
| 46 | + int num_visuals; |
| 47 | + EGLContext ctx; |
| 48 | + EGLConfig config; |
| 49 | + EGLint num_configs; |
| 50 | + EGLint vid; |
| 51 | + |
| 52 | + scrnum = DefaultScreen( x_dpy ); |
| 53 | + root = RootWindow( x_dpy, scrnum ); |
| 54 | + |
| 55 | + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { |
| 56 | + printf("Error: couldn't get an EGL visual config\n"); |
| 57 | + exit(1); |
| 58 | + } |
| 59 | + |
| 60 | + assert(config); |
| 61 | + assert(num_configs > 0); |
| 62 | + |
| 63 | + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { |
| 64 | + printf("Error: eglGetConfigAttrib() failed\n"); |
| 65 | + exit(1); |
| 66 | + } |
| 67 | + |
| 68 | + /* The X window visual must match the EGL config */ |
| 69 | + visTemplate.visualid = vid; |
| 70 | + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); |
| 71 | + if (!visInfo) { |
| 72 | + printf("Error: couldn't get X visual\n"); |
| 73 | + exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + /* window attributes */ |
| 77 | + attr.background_pixel = 0; |
| 78 | + attr.border_pixel = 0; |
| 79 | + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); |
| 80 | + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; |
| 81 | + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; |
| 82 | + |
| 83 | + win = XCreateWindow( x_dpy, root, 0, 0, width, height, |
| 84 | + 0, visInfo->depth, InputOutput, |
| 85 | + visInfo->visual, mask, &attr ); |
| 86 | + |
| 87 | + /* set hints and properties */ |
| 88 | + { |
| 89 | + XSizeHints sizehints; |
| 90 | + sizehints.x = x; |
| 91 | + sizehints.y = y; |
| 92 | + sizehints.width = width; |
| 93 | + sizehints.height = height; |
| 94 | + sizehints.flags = USSize | USPosition; |
| 95 | + XSetNormalHints(x_dpy, win, &sizehints); |
| 96 | + XSetStandardProperties(x_dpy, win, name, name, |
| 97 | + None, (char **)NULL, 0, &sizehints); |
| 98 | + } |
| 99 | + |
| 100 | + eglBindAPI(EGL_OPENGL_ES_API); |
| 101 | + |
| 102 | + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs ); |
| 103 | + if (!ctx) { |
| 104 | + printf("Error: eglCreateContext failed\n"); |
| 105 | + exit(1); |
| 106 | + } |
| 107 | + |
| 108 | + /* test eglQueryContext() */ |
| 109 | + { |
| 110 | + EGLint val; |
| 111 | + eglQueryContext(egl_dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &val); |
| 112 | + assert(val == 2); |
| 113 | + } |
| 114 | + |
| 115 | + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); |
| 116 | + if (!*surfRet) { |
| 117 | + printf("Error: eglCreateWindowSurface failed\n"); |
| 118 | + exit(1); |
| 119 | + } |
| 120 | + |
| 121 | + /* sanity checks */ |
| 122 | + { |
| 123 | + EGLint val; |
| 124 | + eglQuerySurface(egl_dpy, *surfRet, EGL_WIDTH, &val); |
| 125 | + assert(val == width); |
| 126 | + eglQuerySurface(egl_dpy, *surfRet, EGL_HEIGHT, &val); |
| 127 | + assert(val == height); |
| 128 | + assert(eglGetConfigAttrib(egl_dpy, config, EGL_SURFACE_TYPE, &val)); |
| 129 | + assert(val & EGL_WINDOW_BIT); |
| 130 | + } |
| 131 | + |
| 132 | + XFree(visInfo); |
| 133 | + |
| 134 | + *winRet = win; |
| 135 | + *ctxRet = ctx; |
| 136 | +} |
| 137 | + |
| 138 | +static void |
| 139 | +usage(void) |
| 140 | +{ |
| 141 | + printf("Usage:\n"); |
| 142 | + printf(" -display <displayname> set the display to run on\n"); |
| 143 | + printf(" -info display OpenGL renderer info\n"); |
| 144 | +} |
| 145 | + |
| 146 | +EGLDisplay egl_dpy; |
| 147 | +EGLSurface egl_surf; |
| 148 | + |
| 149 | +void present(void) |
| 150 | +{ |
| 151 | + eglSwapBuffers(egl_dpy, egl_surf); |
| 152 | +} |
| 153 | + |
| 154 | +int |
| 155 | +main(int argc, char *argv[]) |
| 156 | +{ |
| 157 | + const int winWidth = 32, winHeight = 32; |
| 158 | + Display *x_dpy; |
| 159 | + Window win; |
| 160 | + EGLContext egl_ctx; |
| 161 | + char *dpyName = NULL; |
| 162 | + EGLint egl_major, egl_minor; |
| 163 | + int i; |
| 164 | + const char *s; |
| 165 | + |
| 166 | + x_dpy = XOpenDisplay(dpyName); |
| 167 | + if (!x_dpy) { |
| 168 | + printf("Error: couldn't open display %s\n", |
| 169 | + dpyName ? dpyName : getenv("DISPLAY")); |
| 170 | + return -1; |
| 171 | + } |
| 172 | + |
| 173 | + egl_dpy = eglGetDisplay(x_dpy); |
| 174 | + if (!egl_dpy) { |
| 175 | + printf("Error: eglGetDisplay() failed\n"); |
| 176 | + return -1; |
| 177 | + } |
| 178 | + |
| 179 | + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { |
| 180 | + printf("Error: eglInitialize() failed\n"); |
| 181 | + return -1; |
| 182 | + } |
| 183 | + |
| 184 | + s = eglQueryString(egl_dpy, EGL_VERSION); |
| 185 | + printf("EGL_VERSION = %s\n", s); |
| 186 | + |
| 187 | + s = eglQueryString(egl_dpy, EGL_VENDOR); |
| 188 | + printf("EGL_VENDOR = %s\n", s); |
| 189 | + |
| 190 | + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); |
| 191 | + printf("EGL_EXTENSIONS = %s\n", s); |
| 192 | + |
| 193 | + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); |
| 194 | + printf("EGL_CLIENT_APIS = %s\n", s); |
| 195 | + |
| 196 | + make_x_window(x_dpy, egl_dpy, |
| 197 | + "OpenGL ES 2.x tri", 0, 0, winWidth, winHeight, |
| 198 | + &win, &egl_ctx, &egl_surf); |
| 199 | + |
| 200 | + XMapWindow(x_dpy, win); |
| 201 | + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { |
| 202 | + printf("Error: eglMakeCurrent() failed\n"); |
| 203 | + return -1; |
| 204 | + } |
| 205 | + |
| 206 | + run_tests(); |
| 207 | + |
| 208 | + eglDestroyContext(egl_dpy, egl_ctx); |
| 209 | + eglDestroySurface(egl_dpy, egl_surf); |
| 210 | + eglTerminate(egl_dpy); |
| 211 | + |
| 212 | + |
| 213 | + XDestroyWindow(x_dpy, win); |
| 214 | + XCloseDisplay(x_dpy); |
| 215 | + |
| 216 | + return 0; |
| 217 | +} |
0 commit comments