gles-display.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2018 Alyssa Rosenzweig <alyssa@rosenzweig.io>
  3. *
  4. * Copyright (c) 2012 Rob Clark <robdclark@gmail.com>
  5. * Copyright (c) 2011-2012 Luc Verhaegen <libv@codethink.co.uk>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <EGL/egl.h>
  29. #include <GLES3/gl3.h>
  30. #include <GLES3/gl3ext.h>
  31. static EGLint const config_attribute_list[] = {
  32. EGL_RED_SIZE, 8,
  33. EGL_GREEN_SIZE, 8,
  34. EGL_BLUE_SIZE, 8,
  35. EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
  36. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  37. EGL_DEPTH_SIZE, 8,
  38. EGL_NONE
  39. };
  40. static const EGLint context_attribute_list[] = {
  41. EGL_CONTEXT_CLIENT_VERSION, 2,
  42. EGL_NONE
  43. };
  44. static EGLDisplay display;
  45. static EGLConfig config;
  46. static EGLint num_config;
  47. static EGLContext context;
  48. static GLuint program;
  49. const char *vertex_shader_source =
  50. "#version 300 es \n"
  51. "in vec4 in_position; \n"
  52. "\n"
  53. "out vec4 vTexCoord; \n"
  54. " \n"
  55. "void main() \n"
  56. "{ \n"
  57. " gl_Position = in_position;\n"
  58. " vTexCoord = ((in_position.xyxy) + 1.0) * vec4(0.5, -0.5, 1.0, 1.0); \n"
  59. "} \n";
  60. const char *fragment_shader_source =
  61. "#version 300 es \n"
  62. "precision highp float; \n"
  63. " \n"
  64. "uniform highp sampler2D uTexture; \n"
  65. "uniform sampler2D uTexture2; \n"
  66. "in vec4 vTexCoord; \n"
  67. "out vec4 FragColor; \n"
  68. " \n"
  69. "void main() \n"
  70. "{ \n"
  71. //" FragColor = texture(uTexture, vTexCoord.xy*1.1) + texture(uTexture, vTexCoord.xy) + texture(uTexture, vTexCoord.xy*1.2) + texture(uTexture, vTexCoord.xy*1.3);\n"
  72. //" FragColor = texture(uTexture, mod(vTexCoord.xy * 2.0 * vec2(1.0, -1.0), 1.0));\n"
  73. " float y = texture(uTexture, mod(vTexCoord.xy - vec2(0.01, 0.01), 1.0)).r;\n"
  74. " FragColor = vec4(vec3(y), 1.0);"
  75. "} \n";
  76. # include <X11/Xlib.h>
  77. # include <X11/Xutil.h>
  78. # include <X11/keysym.h>
  79. static EGLNativeDisplayType native_dpy;
  80. static EGLDisplay
  81. get_display(void)
  82. {
  83. EGLDisplay display;
  84. EGLint egl_major, egl_minor;
  85. native_dpy = XOpenDisplay(NULL);
  86. display = eglGetDisplay(native_dpy);
  87. eglInitialize(display, &egl_major, &egl_minor);
  88. return display;
  89. }
  90. EGLSurface surface;
  91. static EGLSurface make_window(EGLDisplay display, EGLConfig config, int width, int height)
  92. {
  93. XVisualInfo *visInfo, visTemplate;
  94. int num_visuals;
  95. Window root, xwin;
  96. XSetWindowAttributes attr;
  97. unsigned long mask;
  98. EGLint vid;
  99. const char *title = "egl";
  100. eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &vid);
  101. /* The X window visual must match the EGL config */
  102. visTemplate.visualid = vid;
  103. visInfo = XGetVisualInfo(native_dpy, VisualIDMask, &visTemplate, &num_visuals);
  104. if (!visInfo) {
  105. exit(-1);
  106. }
  107. root = RootWindow(native_dpy, DefaultScreen(native_dpy));
  108. /* window attributes */
  109. attr.background_pixel = 0;
  110. attr.border_pixel = 0;
  111. attr.colormap = XCreateColormap(native_dpy,
  112. root, visInfo->visual, AllocNone);
  113. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  114. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  115. xwin = XCreateWindow(native_dpy, root, 0, 0, width, height,
  116. 0, visInfo->depth, InputOutput, visInfo->visual, mask, &attr);
  117. if (!xwin) {
  118. exit (-1);
  119. }
  120. XFree(visInfo);
  121. /* set hints and properties */
  122. {
  123. XSizeHints sizehints;
  124. sizehints.x = 0;
  125. sizehints.y = 0;
  126. sizehints.width = width;
  127. sizehints.height = height;
  128. sizehints.flags = USSize | USPosition;
  129. XSetNormalHints(native_dpy, xwin, &sizehints);
  130. XSetStandardProperties(native_dpy, xwin,
  131. title, title, None, (char **) NULL, 0, &sizehints);
  132. }
  133. XMapWindow(native_dpy, xwin);
  134. surface = eglCreateWindowSurface(display, config, xwin, NULL);
  135. return surface;
  136. }
  137. static GLuint
  138. get_program(const char *vertex_shader_source, const char *fragment_shader_source)
  139. {
  140. GLuint vertex_shader, fragment_shader, program;
  141. GLint ret;
  142. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  143. glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
  144. glCompileShader(vertex_shader);
  145. glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &ret);
  146. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  147. glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
  148. glCompileShader(fragment_shader);
  149. glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &ret);
  150. program = glCreateProgram();
  151. glAttachShader(program, vertex_shader);
  152. glAttachShader(program, fragment_shader);
  153. return program;
  154. }
  155. static void
  156. link_program(GLuint program)
  157. {
  158. GLint ret, len;
  159. GLenum binary_format;
  160. void *binary;
  161. glLinkProgram(program);
  162. glGetProgramiv(program, GL_LINK_STATUS, &ret);
  163. glUseProgram(program);
  164. }
  165. GLfloat vVertices[] = {
  166. // front
  167. -1.00, -1.00, 0.0,
  168. 1.00, -1.00, 0.0,
  169. -1.00, 1.00, 0.0,
  170. 1.00, 1.00, 0.0
  171. };
  172. unsigned width;
  173. unsigned height;
  174. void
  175. gles_display_init(unsigned _width, unsigned _height)
  176. {
  177. GLint gwidth, gheight;
  178. GLuint textures[2], texture_handle;
  179. width = _width;
  180. height = _height;
  181. EGLSurface surface;
  182. display = get_display();
  183. eglChooseConfig(display, config_attribute_list, &config, 1, &num_config);
  184. context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribute_list);
  185. surface = make_window(display, config, 1366, 768);
  186. eglQuerySurface(display, surface, EGL_WIDTH, &gwidth);
  187. eglQuerySurface(display, surface, EGL_HEIGHT, &gheight);
  188. /* connect the context to the surface */
  189. eglMakeCurrent(display, surface, surface, context);
  190. program = get_program(vertex_shader_source, fragment_shader_source);
  191. glBindAttribLocation(program, 0, "in_position");
  192. link_program(program);
  193. glViewport(0, 0, 1366, 768);
  194. glGenTextures(1, &textures);
  195. glActiveTexture(GL_TEXTURE0);
  196. glBindTexture(GL_TEXTURE_2D, textures[0]);
  197. texture_handle = glGetUniformLocation(program, "uTexture");
  198. glUniform1i(texture_handle, 0); /* '0' refers to texture unit 0. */
  199. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  200. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  202. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  203. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  204. #if 0
  205. eglDestroySurface(display, surface);
  206. eglTerminate(display);
  207. #endif
  208. printf("OK\n");
  209. return;
  210. }
  211. void
  212. gles_display_yuv(
  213. uint8_t *plane_y, size_t pitch_y,
  214. uint8_t *plane_u, size_t pitch_u,
  215. uint8_t *plane_v, size_t pitch_v)
  216. {
  217. glClearColor(0.5, 0.5, 0.5, 1.0);
  218. glClear(GL_COLOR_BUFFER_BIT);
  219. glTexImage2D(
  220. GL_TEXTURE_2D, 0, GL_RED,
  221. width, height, 0,
  222. GL_RED, GL_UNSIGNED_BYTE, plane_y);
  223. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
  224. glEnableVertexAttribArray(0);
  225. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  226. eglSwapBuffers(display, surface);
  227. }