Advertisement
Guest User

Untitled

a guest
Sep 20th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3.  
  4. #include <wayland-client.h>
  5.  
  6.  // EGL
  7. #include <wayland-egl.h>
  8. #include <EGL/egl.h>
  9.  
  10.  // Cairo
  11. #include <cairo.h>
  12. #include <cairo-gl.h>
  13.  
  14.  
  15. struct display {
  16.     struct wl_display *display;
  17.     struct wl_compositor *compositor;
  18.     struct wl_registry *registry;
  19.     struct wl_shell *shell;
  20. };
  21.  
  22. struct window {
  23.         // EGL
  24.     struct wl_egl_window *ewindow;
  25.     struct wl_surface *surface;
  26.     struct wl_shell_surface *shell_surface;
  27.     EGLDisplay edpy;
  28.     EGLContext ectx;
  29.     EGLConfig ecfg;
  30.     EGLSurface esrf;
  31.         // Cairo
  32.     cairo_device_t *crd;
  33.     cairo_surface_t *crs;
  34.         //
  35.     struct wl_callback *callback;
  36.     int width;
  37.     int height;
  38. };
  39.  
  40.  
  41.  
  42. static void
  43. registry_handle_global (void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version)
  44. {
  45.     struct display *d = data;
  46.  
  47.     if (strcmp (interface, "wl_compositor") == 0) {
  48.         d-> compositor = wl_registry_bind (registry, id, &wl_compositor_interface, 1);
  49.     } else if (strcmp(interface, "wl_shell") == 0) {
  50.         d->shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
  51.     }
  52. }
  53.  
  54. static void
  55. registry_handle_global_remove (void *data, struct wl_registry *registry, uint32_t name)
  56. {
  57. }
  58.  
  59. static const struct wl_registry_listener registry_listener = {
  60.     registry_handle_global,
  61.     registry_handle_global_remove
  62. };
  63.  
  64.  
  65. static void
  66. handle_ping(void *data, struct wl_shell_surface *shell_surface, uint32_t serial)
  67. {
  68.     wl_shell_surface_pong (shell_surface, serial);
  69. }
  70.  
  71. static const struct wl_shell_surface_listener shell_surface_listener = {
  72.     handle_ping
  73. };
  74.  
  75.  
  76.  
  77. void init_egl (struct display *display, struct window *window)
  78. {
  79.     static const EGLint config_attrs[] = {
  80.         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  81.         EGL_RED_SIZE, 1,
  82.         EGL_GREEN_SIZE, 1,
  83.         EGL_BLUE_SIZE, 1,
  84.         EGL_ALPHA_SIZE, 0,
  85.         //EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
  86.         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  87.         EGL_NONE
  88.     };
  89.  
  90.     static const EGLint context_attrs[] = {
  91.         EGL_CONTEXT_CLIENT_VERSION, 2,
  92.         EGL_NONE
  93.     };
  94.  
  95.     window->edpy = eglGetDisplay ((EGLNativeDisplayType) display->display);
  96.     assert (window->edpy != NULL);
  97.      //
  98.     EGLint major, minor, n;
  99.     EGLBoolean result;
  100.     result = eglInitialize (window->edpy, &major, &minor);
  101.     assert (result == EGL_TRUE);
  102.  
  103.     result = eglBindAPI (EGL_OPENGL_ES_API);
  104.     assert (result == EGL_TRUE);
  105.  
  106.     result = eglChooseConfig (window->edpy, config_attrs, &window->ecfg, 1, &n);
  107.     assert (result && n == 1);
  108.  
  109.     window->ectx = eglCreateContext (window->edpy, window->ecfg, EGL_NO_CONTEXT, context_attrs);
  110.     assert (window->ectx != NULL);
  111.  
  112.     // CAIRO !
  113.     window->crd = cairo_egl_device_create (window->edpy, window->ectx);
  114. }
  115.  
  116.  
  117. static const struct wl_callback_listener frame_listener;
  118.  
  119. static void
  120. redraw(void *data, struct wl_callback *callback, uint32_t time)
  121. {
  122.     struct window *window = data;
  123.  
  124.     // CAIRO !
  125.     cairo_t *cr;
  126.     cr = cairo_create (window->crs);
  127.      //
  128.     cairo_set_source_rgb (cr, 1, 0, 0);
  129.     cairo_rectangle (cr, 0, 0, 320, 240);
  130.     cairo_fill (cr);
  131.      //
  132.     cairo_surface_t *image;
  133.     image = cairo_image_surface_create_from_png ("monimage.png");
  134.     cairo_set_source_surface (cr, image, 0, 0);
  135.     cairo_paint (cr);
  136.     cairo_surface_destroy (image);
  137.      //
  138.     cairo_destroy (cr);
  139.  
  140.     if (callback)
  141.         wl_callback_destroy(callback);
  142.     window->callback = wl_surface_frame(window->surface);
  143.     wl_callback_add_listener(window->callback, &frame_listener, window);
  144.  
  145.     cairo_gl_surface_swapbuffers (window->crs);
  146.     //eglSwapBuffers (window->edpy, window->esrf);
  147. }
  148.  
  149. static const struct wl_callback_listener frame_listener = {
  150.     redraw
  151. };
  152.  
  153.  
  154.  
  155.  
  156. int main ()
  157. {
  158.     struct display *display;
  159.     display = malloc (sizeof *display);
  160.  
  161.     display->display = wl_display_connect (NULL);
  162.     assert (display->display != NULL);
  163.  
  164.     display->registry = wl_display_get_registry (display->display);
  165.     wl_registry_add_listener (display->registry, &registry_listener, display);
  166.  
  167.     wl_display_roundtrip (display->display);
  168.     wl_display_roundtrip (display->display);
  169.     wl_display_get_fd (display->display);
  170.  
  171.  
  172.     struct window *window;
  173.     window = malloc (sizeof *window);
  174.  
  175.  
  176.      // EGL/CAIRO !
  177.     init_egl (display, window);
  178.  
  179.  
  180.     window->surface =  wl_compositor_create_surface (display->compositor);
  181.     window->shell_surface = wl_shell_get_shell_surface (display->shell, window->surface);
  182.     wl_shell_surface_add_listener(window->shell_surface, &shell_surface_listener, window);
  183.  
  184.      // EGL/CAIRO !
  185.     window->ewindow = wl_egl_window_create (window->surface, 320, 240);
  186.     window->esrf = eglCreateWindowSurface (window->edpy, window->ecfg, window->ewindow, NULL);
  187.  
  188.  
  189.     wl_shell_surface_set_title (window->shell_surface, "Exemple EGL");
  190.     wl_shell_surface_set_toplevel (window->shell_surface);
  191.  
  192.      // EGL/CAIRO !
  193.      EGLBoolean result;
  194.      result = eglMakeCurrent (window->edpy, window->esrf, window->esrf, window->ectx);
  195.     assert (result == EGL_TRUE);
  196.      //
  197.     window->crs = cairo_gl_surface_create_for_egl (window->crd, window->esrf, 320, 240);
  198.     redraw (window, NULL, 0);
  199.  
  200.  
  201.     int finboucle = 0;
  202.     while (finboucle != -1)
  203.         finboucle = wl_display_dispatch (display->display);
  204.  
  205.  
  206.     cairo_surface_destroy (window->crs);
  207.     cairo_device_finish (window->crd);
  208.     cairo_device_destroy (window->crd);
  209.  
  210.     eglDestroyContext (window->edpy, window->ectx);
  211.     eglTerminate (window->edpy);
  212.     eglReleaseThread ();
  213.  
  214.     wl_registry_destroy (display->registry);
  215.     wl_display_flush (display->display);
  216.     wl_display_disconnect (display->display);
  217.  
  218.  
  219.     return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement