/* * Copyright © 2011 Benjamin Franzke * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that copyright * notice and this permission notice appear in supporting documentation, and * that the name of the copyright holders not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. The copyright holders make no representations * about the suitability of this software for any purpose. It is provided "as * is" without express or implied warranty. * * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. */ /* * compile using: * gcc -o va-egl va-egl.c -I /path/to/libva/test \ * `pkg-config --libs --cflags wayland-{client,egl} egl glesv2 libva{,-wayland}` */ #include #include #include #include #include #include #include #include #include #define EGL_EGLEXT_PROTOTYPES #include #include #include #include #define CHECK_VASTATUS(va_status,func) \ if (va_status != VA_STATUS_SUCCESS) { \ fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \ exit(1); \ } #include "loadsurface.h" struct display { struct wl_display *display; struct wl_visual *premultiplied_argb_visual; struct wl_compositor *compositor; struct wl_shell *shell; struct { EGLDisplay dpy; EGLContext ctx; EGLConfig conf; } egl; struct { VADisplay dpy; } va; uint32_t mask; }; struct window { struct display *display; struct { int width, height; } geometry; struct { GLuint program; GLuint rotation_uniform; GLuint tex_uniform; GLuint pos; GLuint texcoord; } gl; struct { #define SURFACE_NUM 16 VASurfaceID surface_id[SURFACE_NUM]; int width, height; int index; struct wl_egl_pixmap *pixmap; EGLImageKHR image; int texture; } va; struct wl_egl_window *native; struct wl_surface *surface; EGLSurface egl_surface; }; static const char *vert_shader_text = "uniform mat4 rotation;\n" "attribute vec2 pos;\n" "attribute vec2 texcoord;\n" "varying vec2 v_texcoord;\n" "void main() {\n" " gl_Position = rotation * vec4(pos, 0.0, 1.0);\n" " v_texcoord = texcoord;\n" "}\n"; static const char texture_frag_shader[] = "precision mediump float;\n" "varying vec2 v_texcoord;\n" "uniform sampler2D tex;\n" "void main()\n" "{\n" " gl_FragColor = texture2D(tex, v_texcoord)\n;" "}\n"; static int init_va(struct display *display) { int major, minor; display->va.dpy = vaGetDisplay(display->display); assert(display->va.dpy); vaInitialize(display->va.dpy, &major, &minor); } static void init_egl(struct display *display) { static const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; static const EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 1, EGL_GREEN_SIZE, 1, EGL_BLUE_SIZE, 1, EGL_ALPHA_SIZE, 1, EGL_DEPTH_SIZE, 1, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint major, minor, n; EGLBoolean ret; setenv("EGL_PLATFORM", "wayland", 1); display->egl.dpy = eglGetDisplay(display->display); assert(display->egl.dpy); ret = eglInitialize(display->egl.dpy, &major, &minor); assert(ret == EGL_TRUE); ret = eglBindAPI(EGL_OPENGL_ES_API); assert(ret == EGL_TRUE); assert(eglChooseConfig(display->egl.dpy, config_attribs, &display->egl.conf, 1, &n) && n == 1); display->egl.ctx = eglCreateContext(display->egl.dpy, display->egl.conf, EGL_NO_CONTEXT, context_attribs); assert(display->egl.ctx); } static GLuint create_shader(struct window *window, const char *source, GLenum shader_type) { GLuint shader; GLint status; shader = glCreateShader(shader_type); assert(shader != 0); glShaderSource(shader, 1, (const char **) &source, NULL); glCompileShader(shader); glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (!status) { char log[1000]; GLsizei len; glGetShaderInfoLog(shader, 1000, &len, log); fprintf(stderr, "Error: compiling %s: %*s\n", shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", len, log); exit(1); } return shader; } static void init_gl(struct window *window) { GLuint frag, vert; GLint status; glViewport(0, 0, window->geometry.width, window->geometry.height); frag = create_shader(window, texture_frag_shader, GL_FRAGMENT_SHADER); vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); window->gl.program = glCreateProgram(); glAttachShader(window->gl.program, frag); glAttachShader(window->gl.program, vert); glLinkProgram(window->gl.program); glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status); if (!status) { char log[1000]; GLsizei len; glGetProgramInfoLog(window->gl.program, 1000, &len, log); fprintf(stderr, "Error: linking:\n%*s\n", len, log); exit(1); } glUseProgram(window->gl.program); window->gl.pos = 0; window->gl.texcoord = 1; glBindAttribLocation(window->gl.program, window->gl.pos, "pos"); glBindAttribLocation(window->gl.program, window->gl.texcoord, "texcoord"); glLinkProgram(window->gl.program); window->gl.rotation_uniform = glGetUniformLocation(window->gl.program, "rotation"); window->gl.tex_uniform = glGetUniformLocation(window->gl.program, "tex"); glUniform1i(window->gl.tex_uniform, 0); } static void sync_callback(void *data) { int *done = data; *done = 1; } static void create_surface(struct window *window) { struct display *display = window->display; struct wl_visual *visual; EGLBoolean ret; int done = 0; if (!display->premultiplied_argb_visual) { wl_display_sync_callback(display->display, sync_callback, &done); while (!done) wl_display_iterate(display->display, display->mask); if (!display->premultiplied_argb_visual) { fprintf(stderr, "premultiplied argb visual missing\n"); exit(1); } } window->surface = wl_compositor_create_surface(display->compositor); visual = display->premultiplied_argb_visual; window->native = wl_egl_window_create(window->surface, window->geometry.width, window->geometry.height, visual); window->egl_surface = eglCreateWindowSurface(display->egl.dpy, display->egl.conf, window->native, NULL); wl_shell_set_toplevel(display->shell, window->surface); ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, window->egl_surface, window->display->egl.ctx); assert(ret == EGL_TRUE); } static VASurfaceID get_next_free_surface(struct window *window) { struct display *display = window->display; int i; i = window->va.index; i++; if (i == SURFACE_NUM) i = 0; window->va.index = i; return window->va.surface_id[i]; } static void redraw_va_surf(struct window *window, uint32_t time) { struct display *display = window->display; int width = window->geometry.width; int height = window->geometry.height; VAStatus vaStatus; VASurfaceID surface_id = VA_INVALID_SURFACE; while (surface_id == VA_INVALID_SURFACE) surface_id = get_next_free_surface(window); vaStatus = vaPutSurface(display->va.dpy, surface_id, window->va.pixmap, 0, 0, window->va.width, window->va.height, 0, 0, window->va.width, window->va.height, NULL, 0, VA_TOP_FIELD); CHECK_VASTATUS(vaStatus, "vaPutSurface"); } static void redraw(struct wl_surface *surface, void *data, uint32_t time) { struct window *window = data; GLfloat v[16] = { 1, 1, 0, 0, 1, -1, 0, 1, -1, 1, 1, 0, -1, -1, 1, 1, }; int p[6] = { 2, 1, 3, 0, 1, 2, }; GLfloat angle; GLfloat rotation[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; static const int32_t speed_div = 5; static uint32_t start_time = 0; redraw_va_surf(window, time); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); if (start_time == 0) start_time = time; angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; rotation[0][0] = cos(angle); rotation[0][2] = sin(angle); rotation[2][0] = -sin(angle); rotation[2][2] = cos(angle); glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, (GLfloat *) rotation); glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]); glVertexAttribPointer(window->gl.texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]); glEnableVertexAttribArray(window->gl.pos); glEnableVertexAttribArray(window->gl.texcoord); glDrawElements(GL_TRIANGLES, 1 * 6, GL_UNSIGNED_INT, p); glDisableVertexAttribArray(window->gl.pos); glDisableVertexAttribArray(window->gl.texcoord); eglSwapBuffers(window->display->egl.dpy, window->egl_surface); wl_display_frame_callback(window->display->display, window->surface, redraw, window); } static void compositor_handle_visual(void *data, struct wl_compositor *compositor, uint32_t id, uint32_t token) { struct display *d = data; switch (token) { case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32: d->premultiplied_argb_visual = wl_visual_create(d->display, id, 1); break; } } static const struct wl_compositor_listener compositor_listener = { compositor_handle_visual, }; static void display_handle_global(struct wl_display *display, uint32_t id, const char *interface, uint32_t version, void *data) { struct display *d = data; if (strcmp(interface, "wl_compositor") == 0) { d->compositor = wl_compositor_create(display, id, 1); wl_compositor_add_listener(d->compositor, &compositor_listener, d); } else if (strcmp(interface, "wl_shell") == 0) { d->shell = wl_shell_create(display, id, 1); } } static int event_mask_update(uint32_t mask, void *data) { struct display *d = data; d->mask = mask; return 0; } static void upload_source_YUV_once_for_all(struct window *window) { int box_width_loc = 8, row_shift_loc = 0; int i; for (i = 0; i < SURFACE_NUM; i++) { upload_surface(window->display->va.dpy, window->va.surface_id[i], box_width_loc, row_shift_loc, 0); row_shift_loc++; if (row_shift_loc == (2*box_width_loc)) row_shift_loc= 0; } } static int create_va_surfaces(struct window *window) { struct display *d = window->display; VAStatus status; window->va.width = 352; window->va.height = 288; status = vaCreateSurfaces(d->va.dpy, window->va.width, window->va.height, VA_RT_FORMAT_YUV420, SURFACE_NUM, window->va.surface_id); upload_source_YUV_once_for_all(window); window->va.pixmap = wl_egl_pixmap_create(352, 288, d->premultiplied_argb_visual, 0); window->va.image = eglCreateImageKHR(d->egl.dpy, d->egl.ctx, EGL_NATIVE_PIXMAP_KHR, window->va.pixmap, NULL); glGenTextures(1, &window->va.texture); glBindTexture(GL_TEXTURE_2D, window->va.texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, window->va.image); } int main(int argc, char **argv) { struct display display = { 0 }; struct window window = { 0 }; memset(&display, 0, sizeof display); memset(&window, 0, sizeof window); window.display = &display; window.geometry.width = 250; window.geometry.height = 250; display.display = wl_display_connect(NULL); assert(display.display); wl_display_add_global_listener(display.display, display_handle_global, &display); wl_display_get_fd(display.display, event_mask_update, &display); init_va(&display); init_egl(&display); create_surface(&window); create_va_surfaces(&window); init_gl(&window); redraw(window.surface, &window, 1); while (true) wl_display_iterate(display.display, display.mask); return 0; }