test-util-3d.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2012 Rob Clark <robdclark@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #ifndef TEST_UTIL_3D_H_
  24. #define TEST_UTIL_3D_H_
  25. #include <EGL/egl.h>
  26. #ifndef GL_ES_VERSION_2_0
  27. #include <GLES2/gl2.h>
  28. #include <GLES2/gl2ext.h>
  29. #endif
  30. #include <ctype.h>
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. # include <X11/Xlib.h>
  37. # include <X11/Xutil.h>
  38. # include <X11/keysym.h>
  39. static EGLNativeDisplayType native_dpy;
  40. static EGLDisplay
  41. get_display(void)
  42. {
  43. EGLDisplay display;
  44. EGLint egl_major, egl_minor;
  45. #ifdef BIONIC
  46. native_dpy = EGL_DEFAULT_DISPLAY;
  47. #else
  48. native_dpy = XOpenDisplay(NULL);
  49. #endif
  50. display = eglGetDisplay(native_dpy);
  51. if (display == EGL_NO_DISPLAY) {
  52. exit(-1);
  53. }
  54. eglInitialize(display, &egl_major, &egl_minor);
  55. return display;
  56. }
  57. static EGLSurface make_window(EGLDisplay display, EGLConfig config, int width, int height)
  58. {
  59. EGLSurface surface;
  60. XVisualInfo *visInfo, visTemplate;
  61. int num_visuals;
  62. Window root, xwin;
  63. XSetWindowAttributes attr;
  64. unsigned long mask;
  65. EGLint vid;
  66. const char *title = "egl";
  67. eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &vid);
  68. /* The X window visual must match the EGL config */
  69. visTemplate.visualid = vid;
  70. visInfo = XGetVisualInfo(native_dpy, VisualIDMask, &visTemplate, &num_visuals);
  71. if (!visInfo) {
  72. exit(-1);
  73. }
  74. root = RootWindow(native_dpy, DefaultScreen(native_dpy));
  75. /* window attributes */
  76. attr.background_pixel = 0;
  77. attr.border_pixel = 0;
  78. attr.colormap = XCreateColormap(native_dpy,
  79. root, visInfo->visual, AllocNone);
  80. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  81. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  82. xwin = XCreateWindow(native_dpy, root, 0, 0, width, height,
  83. 0, visInfo->depth, InputOutput, visInfo->visual, mask, &attr);
  84. if (!xwin) {
  85. exit (-1);
  86. }
  87. XFree(visInfo);
  88. /* set hints and properties */
  89. {
  90. XSizeHints sizehints;
  91. sizehints.x = 0;
  92. sizehints.y = 0;
  93. sizehints.width = width;
  94. sizehints.height = height;
  95. sizehints.flags = USSize | USPosition;
  96. XSetNormalHints(native_dpy, xwin, &sizehints);
  97. XSetStandardProperties(native_dpy, xwin,
  98. title, title, None, (char **) NULL, 0, &sizehints);
  99. }
  100. XMapWindow(native_dpy, xwin);
  101. surface = eglCreateWindowSurface(display, config, xwin, NULL);
  102. return surface;
  103. }
  104. static GLuint
  105. get_shader(GLenum stage, const char *stage_name, const char *source)
  106. {
  107. GLuint shader;
  108. GLint ret;
  109. shader = glCreateShader(stage);
  110. glShaderSource(shader, 1, &source, NULL);
  111. glCompileShader(shader);
  112. glGetShaderiv(shader, GL_COMPILE_STATUS, &ret);
  113. if (!ret) {
  114. char *log;
  115. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &ret);
  116. if (ret > 1) {
  117. log = malloc(ret);
  118. glGetShaderInfoLog(shader, ret, NULL, log);
  119. printf("%s", log);
  120. }
  121. exit(-1);
  122. }
  123. return shader;
  124. }
  125. static GLuint
  126. get_program(const char *vertex_shader_source, const char *fragment_shader_source)
  127. {
  128. GLuint vertex_shader, fragment_shader, program;
  129. vertex_shader = get_shader(GL_VERTEX_SHADER, "vertex", vertex_shader_source);
  130. fragment_shader = get_shader(GL_FRAGMENT_SHADER, "fragment", fragment_shader_source);
  131. program = glCreateProgram();
  132. glAttachShader(program, vertex_shader);
  133. glAttachShader(program, fragment_shader);
  134. return program;
  135. }
  136. static void
  137. link_program(GLuint program)
  138. {
  139. GLint ret, len;
  140. GLenum binary_format;
  141. void *binary;
  142. glLinkProgram(program);
  143. glGetProgramiv(program, GL_LINK_STATUS, &ret);
  144. if (!ret) {
  145. char *log;
  146. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &ret);
  147. if (ret > 1) {
  148. log = malloc(ret);
  149. glGetProgramInfoLog(program, ret, NULL, log);
  150. printf("%s", log);
  151. }
  152. exit(-1);
  153. }
  154. glUseProgram(program);
  155. }
  156. #endif /* TEST_UTIL_3D_H_ */