diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2011-02-11 23:40:36 +0100 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2011-02-11 23:40:36 +0100 |
commit | 069c6045212222bd4ea6b82daf110d6c754473a2 (patch) | |
tree | b9dac754603d94e8b0a206e2b7374a7768bc1157 | |
parent | 0f49000905ff66d3b85598ad0e117ceebac229ad (diff) |
Add egl-wayland-window-legacy.c
Legacy means clients not using the frame callback,
so need to be blocked in eglSwapBuffers.
-rw-r--r-- | egl-wayland-window-legacy.c | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/egl-wayland-window-legacy.c b/egl-wayland-window-legacy.c new file mode 100644 index 0000000..2385854 --- /dev/null +++ b/egl-wayland-window-legacy.c @@ -0,0 +1,279 @@ +#include <stdio.h> +#include <string.h> +#include <stdbool.h> +#include <assert.h> + +#include <sys/time.h> + +#include <wayland-client.h> +#include <wayland-egl.h> + +#include <GL/gl.h> +#include <EGL/egl.h> + +struct display { + struct wl_display *display; + struct wl_egl_display *egl_display; + struct { + struct wl_compositor *compositor; + struct wl_shell *shell; + } interface; + + struct { + EGLDisplay dpy; + EGLConfig conf; + EGLContext ctx; + } egl; + + uint32_t mask; +}; + +struct window { + struct display *display; + struct wl_surface *surface; + struct wl_egl_window *egl_window; + + int width, height; + + struct { + EGLSurface surf; + } egl; +}; + +static void +init_egl(struct display *display) +{ + EGLint major, minor; + EGLint num_config; + EGLint config_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_NONE + }; + + display->egl.dpy = eglGetDisplay(display->egl_display); + assert(display->egl.dpy); + + assert(eglInitialize(display->egl.dpy, &major, &minor)); + + assert(eglBindAPI(EGL_OPENGL_API)); + assert(eglChooseConfig(display->egl.dpy, config_attribs, + &display->egl.conf, 1, &num_config)); + assert(num_config == 1); + + display->egl.ctx = eglCreateContext(display->egl.dpy, display->egl.conf, + EGL_NO_CONTEXT, NULL); + assert(display->egl.ctx); +} + +static void +create_egl_window(struct display *display, struct window *window) +{ + struct wl_visual *visual; + + visual = wl_display_get_premultiplied_argb_visual(display->display); + window->egl_window = wl_egl_window_create(display->egl_display, + window->surface, + window->width, + window->height, + visual); + + window->egl.surf = + eglCreateWindowSurface(display->egl.dpy, display->egl.conf, + window->egl_window, NULL); + assert(window->egl.surf); + assert(eglMakeCurrent(display->egl.dpy, + window->egl.surf, window->egl.surf, + display->egl.ctx)); +} + +static void +init_gl(struct window *window) +{ + GLfloat ar; + + glViewport(0, 0, window->width, window->height); + ar = (GLfloat)window->width / (GLfloat)window->height; + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-ar, ar, -1, 1, 5.0, 60.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -10.0); +} + +static void +redraw(void *data, uint32_t time) +{ + struct window *window = data; + static const GLfloat verts[3][2] = { + { -1, -1 }, + { 1, -1 }, + { 0, 1 } + }; + static const GLfloat colors[3][3] = { + { 1, 0, 1 }, + { 0, 1, 0 }, + { 0, 0, 1 } + }; + static GLfloat speed = 0.2; + static uint32_t start_time = 0; + static uint32_t last_time = 0; + static int frames = 0; + + if (start_time == 0) + start_time = time; + + ++frames; + if ((time - last_time) >= 5000) { + printf("fps: %d\n", frames / 5); + last_time = time; + frames = 0; + } + + glClearColor(0.0, 0.0, 0.0, 0.5); + glClear(GL_COLOR_BUFFER_BIT); + + GLint foo; + glGetIntegerv(GL_DRAW_BUFFER, &foo), + + glPushMatrix(); + glRotatef(speed * (time-start_time), 0, 1, 0); + + glVertexPointer(2, GL_FLOAT, 0, verts); + glColorPointer(3, GL_FLOAT, 0, colors); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + + glPopMatrix(); + + eglSwapBuffers(window->display->egl.dpy, window->egl.surf); +} + +enum window_location { + WINDOW_INTERIOR = 0, + WINDOW_RESIZING_TOP = 1, + WINDOW_RESIZING_BOTTOM = 2, + WINDOW_RESIZING_LEFT = 4, + WINDOW_RESIZING_TOP_LEFT = 5, + WINDOW_RESIZING_BOTTOM_LEFT = 6, + WINDOW_RESIZING_RIGHT = 8, + WINDOW_RESIZING_TOP_RIGHT = 9, + WINDOW_RESIZING_BOTTOM_RIGHT = 10, + WINDOW_RESIZING_MASK = 15, + WINDOW_EXTERIOR = 16, + WINDOW_TITLEBAR = 17, + WINDOW_CLIENT_AREA = 18, +}; +static void +handle_configure(void *data, struct wl_shell *shell, + uint32_t time, uint32_t edges, + struct wl_surface *surface, + int32_t width, int32_t height) +{ + struct window *window = wl_surface_get_user_data(surface); + int dx = 0; + int dy = 0; + int a_width, a_height; + + wl_egl_window_get_attached_size(window->egl_window, + &a_width, &a_height); + + if (edges & WINDOW_RESIZING_LEFT) + dx = a_width - width; + + if (edges & WINDOW_RESIZING_TOP) + dy = a_height - height; + + wl_egl_window_resize(window->egl_window, width, height, dx, dy); + + window->width = width; + window->height = height; + + glViewport(0, 0, window->width, window->height); +} + +static const struct wl_shell_listener wayland_shell_listener = { + handle_configure, +}; + +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, "compositor") == 0) { + d->interface.compositor = wl_compositor_create(display, id); + } else if (strcmp(interface, "shell") == 0) { + d->interface.shell = wl_shell_create(display, id); + wl_shell_add_listener(d->interface.shell, &wayland_shell_listener, d); + } +} + +static int +event_mask_update(uint32_t mask, void *data) +{ + struct display *d = data; + + d->mask = mask; + + return 0; +} + +int +main(int argc, char **argv) +{ + struct display display = { 0 }; + struct window window = { 0 }; + + window.display = &display; + window.width = 250; + window.height = 250; + + display.display = wl_display_connect(NULL); + assert(display.display); + + display.egl_display = wl_egl_display_create(display.display); + assert(display.egl_display); + + wl_display_add_global_listener(display.display, + display_handle_global, &display); + /* process connection events */ + wl_display_iterate(display.display, WL_DISPLAY_READABLE); + + init_egl(&display); + window.surface = + wl_compositor_create_surface(display.interface.compositor); + wl_surface_set_user_data(window.surface, &window); + create_egl_window(&display, &window); + + init_gl(&window); + + wl_surface_map_toplevel(window.surface); + + wl_display_get_fd(display.display, event_mask_update, &display); + + while (true) { + struct timeval tv; + uint32_t msec; + + gettimeofday(&tv, NULL); + redraw(&window, tv.tv_sec * 1000 + tv.tv_usec / 1000); + + wl_display_iterate(display.display, display.mask); + } + + return 0; +} |