summaryrefslogtreecommitdiffstatsci
path: root/src/surface-cairo-egl.cpp
blob: 83d14be0bbd86c81632d1ea0d9b48785e4c8a2eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <err.h>
#include <stdlib.h>
#include <cairo.h>
#include <cairo-gl.h>

#include <EGL/egl.h>
#include <GLES3/gl3.h>

#include "egl.h"

static Display* getDisplay()
{
  static Display* display = NULL;
  if (!display)
    display = XOpenDisplay(NULL);
  return display;
}

static bool
createWindow(egl_state_t *state, int width, int height)
{
  state->dpy = XOpenDisplay (NULL);
  if (state->dpy == NULL) {
    warnx ("Failed to open display: %s\n", XDisplayName (0));
    return false;
  }

  state->window = XCreateSimpleWindow (state->dpy, DefaultRootWindow (state->dpy),
				       0, 0,
				       width, height,
				       0,
				       BlackPixel (state->dpy, DefaultRootWindow (state->dpy)),
				       BlackPixel (state->dpy, DefaultRootWindow (state->dpy)));
  XMapWindow (state->dpy, state->window);
  return true;
}

static void
cleanup (void *data)
{
  egl_state_t *state = (egl_state_t*)data;

  eglDestroyContext (state->egl_display, state->egl_context);
  eglDestroySurface (state->egl_display, state->egl_surface);
  eglTerminate (state->egl_display);

  XCloseDisplay (state->dpy);

  free (state);
}

cairo_surface_t *
create_cairo_surface_egl (int width, int height)
{
  egl_state_t *state;
  cairo_device_t *cairo_device;
  cairo_surface_t *cairo_surface;

  state = (egl_state_t*) malloc (sizeof (egl_state_t));
  if (!state) {
    warnx ("Out of memory\n");
    return NULL;
  }

  if (!createWindow(state, width, height)) {
    return NULL;
  }

  if (!initializeEGL(state)) {
    cleanup(state);
    return NULL;
  }

  if (!createEGLContextWithWindow(state)) {
    cleanup(state);
    return NULL;
  }

  cairo_device = cairo_egl_device_create (state->egl_display, state->egl_context);
  cairo_gl_device_set_thread_aware (cairo_device, 0);
  cairo_surface = cairo_gl_surface_create_for_egl (cairo_device,
						   state->egl_surface,
						   width, height);

  if (cairo_device_set_user_data (cairo_device,
                                  (cairo_user_data_key_t *) cleanup,
                                  state,
                                  cleanup))
    {
      warnx ("Failed to setup cleanup callback closure\n");
      cleanup (state);
      return NULL;
    }

  cairo_device_destroy (cairo_device);

  return cairo_surface;
}