Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- #include <wayland-client.h>
- // EGL
- #include <wayland-egl.h>
- #include <EGL/egl.h>
- // Cairo
- #include <cairo.h>
- #include <cairo-gl.h>
- struct display {
- struct wl_display *display;
- struct wl_compositor *compositor;
- struct wl_registry *registry;
- struct wl_shell *shell;
- };
- struct window {
- // EGL
- struct wl_egl_window *ewindow;
- struct wl_surface *surface;
- struct wl_shell_surface *shell_surface;
- EGLDisplay edpy;
- EGLContext ectx;
- EGLConfig ecfg;
- EGLSurface esrf;
- // Cairo
- cairo_device_t *crd;
- cairo_surface_t *crs;
- //
- struct wl_callback *callback;
- int width;
- int height;
- };
- static void
- registry_handle_global (void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version)
- {
- struct display *d = data;
- if (strcmp (interface, "wl_compositor") == 0) {
- d-> compositor = wl_registry_bind (registry, id, &wl_compositor_interface, 1);
- } else if (strcmp(interface, "wl_shell") == 0) {
- d->shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
- }
- }
- static void
- registry_handle_global_remove (void *data, struct wl_registry *registry, uint32_t name)
- {
- }
- static const struct wl_registry_listener registry_listener = {
- registry_handle_global,
- registry_handle_global_remove
- };
- static void
- handle_ping(void *data, struct wl_shell_surface *shell_surface, uint32_t serial)
- {
- wl_shell_surface_pong (shell_surface, serial);
- }
- static const struct wl_shell_surface_listener shell_surface_listener = {
- handle_ping
- };
- void init_egl (struct display *display, struct window *window)
- {
- static const EGLint config_attrs[] = {
- EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
- EGL_RED_SIZE, 1,
- EGL_GREEN_SIZE, 1,
- EGL_BLUE_SIZE, 1,
- EGL_ALPHA_SIZE, 0,
- //EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
- EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
- EGL_NONE
- };
- static const EGLint context_attrs[] = {
- EGL_CONTEXT_CLIENT_VERSION, 2,
- EGL_NONE
- };
- window->edpy = eglGetDisplay ((EGLNativeDisplayType) display->display);
- assert (window->edpy != NULL);
- //
- EGLint major, minor, n;
- EGLBoolean result;
- result = eglInitialize (window->edpy, &major, &minor);
- assert (result == EGL_TRUE);
- result = eglBindAPI (EGL_OPENGL_ES_API);
- assert (result == EGL_TRUE);
- result = eglChooseConfig (window->edpy, config_attrs, &window->ecfg, 1, &n);
- assert (result && n == 1);
- window->ectx = eglCreateContext (window->edpy, window->ecfg, EGL_NO_CONTEXT, context_attrs);
- assert (window->ectx != NULL);
- // CAIRO !
- window->crd = cairo_egl_device_create (window->edpy, window->ectx);
- }
- static const struct wl_callback_listener frame_listener;
- static void
- redraw(void *data, struct wl_callback *callback, uint32_t time)
- {
- struct window *window = data;
- // CAIRO !
- cairo_t *cr;
- cr = cairo_create (window->crs);
- //
- cairo_set_source_rgb (cr, 1, 0, 0);
- cairo_rectangle (cr, 0, 0, 320, 240);
- cairo_fill (cr);
- //
- cairo_surface_t *image;
- image = cairo_image_surface_create_from_png ("monimage.png");
- cairo_set_source_surface (cr, image, 0, 0);
- cairo_paint (cr);
- cairo_surface_destroy (image);
- //
- cairo_destroy (cr);
- if (callback)
- wl_callback_destroy(callback);
- window->callback = wl_surface_frame(window->surface);
- wl_callback_add_listener(window->callback, &frame_listener, window);
- cairo_gl_surface_swapbuffers (window->crs);
- //eglSwapBuffers (window->edpy, window->esrf);
- }
- static const struct wl_callback_listener frame_listener = {
- redraw
- };
- int main ()
- {
- struct display *display;
- display = malloc (sizeof *display);
- display->display = wl_display_connect (NULL);
- assert (display->display != NULL);
- display->registry = wl_display_get_registry (display->display);
- wl_registry_add_listener (display->registry, ®istry_listener, display);
- wl_display_roundtrip (display->display);
- wl_display_roundtrip (display->display);
- wl_display_get_fd (display->display);
- struct window *window;
- window = malloc (sizeof *window);
- // EGL/CAIRO !
- init_egl (display, window);
- window->surface = wl_compositor_create_surface (display->compositor);
- window->shell_surface = wl_shell_get_shell_surface (display->shell, window->surface);
- wl_shell_surface_add_listener(window->shell_surface, &shell_surface_listener, window);
- // EGL/CAIRO !
- window->ewindow = wl_egl_window_create (window->surface, 320, 240);
- window->esrf = eglCreateWindowSurface (window->edpy, window->ecfg, window->ewindow, NULL);
- wl_shell_surface_set_title (window->shell_surface, "Exemple EGL");
- wl_shell_surface_set_toplevel (window->shell_surface);
- // EGL/CAIRO !
- EGLBoolean result;
- result = eglMakeCurrent (window->edpy, window->esrf, window->esrf, window->ectx);
- assert (result == EGL_TRUE);
- //
- window->crs = cairo_gl_surface_create_for_egl (window->crd, window->esrf, 320, 240);
- redraw (window, NULL, 0);
- int finboucle = 0;
- while (finboucle != -1)
- finboucle = wl_display_dispatch (display->display);
- cairo_surface_destroy (window->crs);
- cairo_device_finish (window->crd);
- cairo_device_destroy (window->crd);
- eglDestroyContext (window->edpy, window->ectx);
- eglTerminate (window->edpy);
- eglReleaseThread ();
- wl_registry_destroy (display->registry);
- wl_display_flush (display->display);
- wl_display_disconnect (display->display);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement