Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lion.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <VG/openvg.h>
  10. #include <GLES/egl.h>
  11. #include "lion-render.h"
  12. static VGint width, height;
  13. struct lion *lion = 0;
  14. VGfloat angle = 0;
  15. static void
  16. draw(void)
  17. {
  18. vgClear(0, 0, width, height);
  19. vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
  20. vgLoadIdentity();
  21. vgTranslate(width/2, height/2);
  22. vgRotate(angle);
  23. vgTranslate(-width/2, -height/2);
  24. lion_render(lion);
  25. ++angle;
  26. }
  27. /* new window size or exposure */
  28. static void
  29. reshape(int w, int h)
  30. {
  31. width = w;
  32. height = h;
  33. }
  34. static void
  35. init(void)
  36. {
  37. float clear_color[4] = {1.0, 1.0, 1.0, 1.0};
  38. vgSetfv(VG_CLEAR_COLOR, 4, clear_color);
  39. lion = lion_create();
  40. }
  41. /*
  42. * Create an RGB, double-buffered X window.
  43. * Return the window and context handles.
  44. */
  45. static void
  46. make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
  47. const char *name,
  48. int x, int y, int width, int height,
  49. Window *winRet,
  50. EGLContext *ctxRet,
  51. EGLSurface *surfRet)
  52. {
  53. static const EGLint attribs[] = {
  54. EGL_RED_SIZE, 1,
  55. EGL_GREEN_SIZE, 1,
  56. EGL_BLUE_SIZE, 1,
  57. EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
  58. EGL_NONE
  59. };
  60. int scrnum;
  61. XSetWindowAttributes attr;
  62. unsigned long mask;
  63. Window root;
  64. Window win;
  65. XVisualInfo *visInfo, visTemplate;
  66. int num_visuals;
  67. EGLContext ctx;
  68. EGLConfig config;
  69. EGLint num_configs;
  70. EGLint vid;
  71. scrnum = DefaultScreen( x_dpy );
  72. root = RootWindow( x_dpy, scrnum );
  73. if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
  74. printf("Error: couldn't get an EGL visual config\n");
  75. exit(1);
  76. }
  77. assert(config);
  78. assert(num_configs > 0);
  79. if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
  80. printf("Error: eglGetConfigAttrib() failed\n");
  81. exit(1);
  82. }
  83. /* The X window visual must match the EGL config */
  84. visTemplate.visualid = vid;
  85. visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
  86. if (!visInfo) {
  87. printf("Error: couldn't get X visual\n");
  88. exit(1);
  89. }
  90. /* window attributes */
  91. attr.background_pixel = 0;
  92. attr.border_pixel = 0;
  93. attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
  94. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  95. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  96. win = XCreateWindow( x_dpy, root, 0, 0, width, height,
  97. 0, visInfo->depth, InputOutput,
  98. visInfo->visual, mask, &attr );
  99. /* set hints and properties */
  100. {
  101. XSizeHints sizehints;
  102. sizehints.x = x;
  103. sizehints.y = y;
  104. sizehints.width = width;
  105. sizehints.height = height;
  106. sizehints.flags = USSize | USPosition;
  107. XSetNormalHints(x_dpy, win, &sizehints);
  108. XSetStandardProperties(x_dpy, win, name, name,
  109. None, (char **)NULL, 0, &sizehints);
  110. }
  111. eglBindAPI(EGL_OPENVG_API);
  112. ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
  113. if (!ctx) {
  114. printf("Error: eglCreateContext failed\n");
  115. exit(1);
  116. }
  117. *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
  118. if (!*surfRet) {
  119. printf("Error: eglCreateWindowSurface failed\n");
  120. exit(1);
  121. }
  122. XFree(visInfo);
  123. *winRet = win;
  124. *ctxRet = ctx;
  125. }
  126. static void
  127. event_loop(Display *dpy, Window win,
  128. EGLDisplay egl_dpy, EGLSurface egl_surf)
  129. {
  130. while (1) {
  131. XEvent event;
  132. while (XPending(dpy) > 0) {
  133. XNextEvent(dpy, &event);
  134. switch (event.type) {
  135. case Expose:
  136. break;
  137. case ConfigureNotify:
  138. reshape(event.xconfigure.width, event.xconfigure.height);
  139. break;
  140. case KeyPress:
  141. {
  142. char buffer[10];
  143. int r, code;
  144. code = XLookupKeysym(&event.xkey, 0);
  145. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  146. NULL, NULL);
  147. if (buffer[0] == 27) {
  148. /* escape */
  149. return;
  150. }
  151. }
  152. break;
  153. default:
  154. ; /*no-op*/
  155. }
  156. }
  157. draw();
  158. eglSwapBuffers(egl_dpy, egl_surf);
  159. }
  160. }
  161. static void
  162. usage(void)
  163. {
  164. printf("Usage:\n");
  165. printf(" -display <displayname> set the display to run on\n");
  166. printf(" -info display OpenGL renderer info\n");
  167. }
  168. int
  169. main(int argc, char *argv[])
  170. {
  171. const int winWidth = 350, winHeight = 450;
  172. Display *x_dpy;
  173. Window win;
  174. EGLSurface egl_surf;
  175. EGLContext egl_ctx;
  176. EGLDisplay egl_dpy;
  177. char *dpyName = NULL;
  178. GLboolean printInfo = GL_FALSE;
  179. EGLint egl_major, egl_minor;
  180. int i;
  181. const char *s;
  182. for (i = 1; i < argc; i++) {
  183. if (strcmp(argv[i], "-display") == 0) {
  184. dpyName = argv[i+1];
  185. i++;
  186. }
  187. else if (strcmp(argv[i], "-info") == 0) {
  188. printInfo = GL_TRUE;
  189. }
  190. else {
  191. usage();
  192. return -1;
  193. }
  194. }
  195. x_dpy = XOpenDisplay(dpyName);
  196. if (!x_dpy) {
  197. printf("Error: couldn't open display %s\n",
  198. dpyName ? dpyName : getenv("DISPLAY"));
  199. return -1;
  200. }
  201. egl_dpy = eglGetDisplay(x_dpy);
  202. if (!egl_dpy) {
  203. printf("Error: eglGetDisplay() failed\n");
  204. return -1;
  205. }
  206. if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
  207. printf("Error: eglInitialize() failed\n");
  208. return -1;
  209. }
  210. s = eglQueryString(egl_dpy, EGL_VERSION);
  211. printf("EGL_VERSION = %s\n", s);
  212. make_x_window(x_dpy, egl_dpy,
  213. "Lion Example", 0, 0, winWidth, winHeight,
  214. &win, &egl_ctx, &egl_surf);
  215. XMapWindow(x_dpy, win);
  216. if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
  217. printf("Error: eglMakeCurrent() failed\n");
  218. return -1;
  219. }
  220. if (printInfo) {
  221. printf("VG_RENDERER = %s\n", (char *) vgGetString(VG_RENDERER));
  222. printf("VG_VERSION = %s\n", (char *) vgGetString(VG_VERSION));
  223. printf("VG_VENDOR = %s\n", (char *) vgGetString(VG_VENDOR));
  224. }
  225. init();
  226. /* Set initial projection/viewing transformation.
  227. * We can't be sure we'll get a ConfigureNotify event when the window
  228. * first appears.
  229. */
  230. reshape(winWidth, winHeight);
  231. event_loop(x_dpy, win, egl_dpy, egl_surf);
  232. eglDestroyContext(egl_dpy, egl_ctx);
  233. eglDestroySurface(egl_dpy, egl_surf);
  234. eglTerminate(egl_dpy);
  235. XDestroyWindow(x_dpy, win);
  236. XCloseDisplay(x_dpy);
  237. return 0;
  238. }