17 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
19 struct egl_compositor {
20 struct wl_compositor base;
24 struct wl_display *wl_display;
30 int32_t width, height, stride;
38 struct egl_compositor *ec = data;
39 struct wl_surface_iterator *iterator;
40 struct wl_surface *surface;
41 struct surface_data *sd;
43 GLint tex_coords[8] = { 1, 0, 1, 1, 0, 1, 0, 0 };
44 GLuint indices[4] = { 0, 1, 2, 3 };
46 /* This part is where we actually copy the buffer to screen.
47 * Needs to be part of the repaint loop, not called from the
48 * notify_map handler. */
50 glClear(GL_COLOR_BUFFER_BIT);
52 iterator = wl_surface_iterator_create(ec->wl_display, 0);
53 while (wl_surface_iterator_next(iterator, &surface)) {
54 sd = wl_surface_get_data(surface);
58 vertices[0] = sd->map.x;
59 vertices[1] = sd->map.y;
62 vertices[3] = sd->map.x;
63 vertices[4] = sd->map.y + sd->map.height;
66 vertices[6] = sd->map.x + sd->map.width;
67 vertices[7] = sd->map.y + sd->map.height;
70 vertices[9] = sd->map.x + sd->map.width;
71 vertices[10] = sd->map.y;
74 glBindTexture(GL_TEXTURE_2D, sd->texture);
75 glEnable(GL_TEXTURE_2D);
77 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
79 glEnableClientState(GL_VERTEX_ARRAY);
80 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
81 glVertexPointer(3, GL_INT, 0, vertices);
82 glTexCoordPointer(2, GL_INT, 0, tex_coords);
83 glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indices);
85 wl_surface_iterator_destroy(iterator);
89 eglSwapBuffers(ec->display, ec->surface);
92 static void schedule_repaint(struct egl_compositor *ec)
94 struct wl_event_loop *loop;
96 loop = wl_display_get_event_loop(ec->wl_display);
97 wl_event_loop_add_idle(loop, repaint, ec);
100 void notify_surface_create(struct wl_compositor *compositor,
101 struct wl_surface *surface)
103 struct surface_data *sd;
105 sd = malloc(sizeof *sd);
110 wl_surface_set_data(surface, sd);
112 glGenTextures(1, &sd->texture);
115 void notify_surface_destroy(struct wl_compositor *compositor,
116 struct wl_surface *surface)
118 struct egl_compositor *ec = (struct egl_compositor *) compositor;
119 struct surface_data *sd;
120 struct drm_gem_close close_arg;
123 sd = wl_surface_get_data(surface);
124 if (sd == NULL || sd->handle == 0)
127 close_arg.handle = sd->handle;
128 ret = ioctl(ec->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
130 fprintf(stderr, "failed to gem_close handle %d: %m\n", sd->handle);
133 glDeleteTextures(1, &sd->texture);
137 schedule_repaint(ec);
140 void notify_surface_attach(struct wl_compositor *compositor,
141 struct wl_surface *surface, uint32_t name,
142 uint32_t width, uint32_t height, uint32_t stride)
144 struct egl_compositor *ec = (struct egl_compositor *) compositor;
145 struct surface_data *sd;
146 struct drm_gem_open open_arg;
147 struct drm_gem_close close_arg;
148 struct drm_i915_gem_pread pread;
153 sd = wl_surface_get_data(surface);
157 if (sd->handle != 0) {
158 close_arg.handle = sd->handle;
159 ret = ioctl(ec->gem_fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
161 fprintf(stderr, "failed to gem_close name %d: %m\n", name);
165 open_arg.name = name;
166 ret = ioctl(ec->gem_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
168 fprintf(stderr, "failed to gem_open name %d, fd=%d: %m\n", name, ec->gem_fd);
172 sd->handle = open_arg.handle;
177 size = sd->height * sd->stride;
180 fprintf(stderr, "swap buffers malloc failed\n");
184 pread.handle = sd->handle;
188 pread.data_ptr = (long) data;
190 if (ioctl(ec->gem_fd, DRM_IOCTL_I915_GEM_PREAD, &pread)) {
191 fprintf(stderr, "gem pread failed");
195 glBindTexture(GL_TEXTURE_2D, sd->texture);
196 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
197 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
198 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
199 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
200 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
201 GL_BGRA, GL_UNSIGNED_BYTE, data);
205 schedule_repaint(ec);
208 void notify_surface_map(struct wl_compositor *compositor,
209 struct wl_surface *surface, struct wl_map *map)
211 struct egl_compositor *ec = (struct egl_compositor *) compositor;
212 struct surface_data *sd;
214 sd = wl_surface_get_data(surface);
220 schedule_repaint(ec);
223 struct wl_compositor_interface interface = {
224 notify_surface_create,
225 notify_surface_destroy,
226 notify_surface_attach,
230 static const char gem_device[] = "/dev/dri/card0";
232 struct wl_compositor *
233 wl_compositor_create(struct wl_display *display)
235 EGLConfig configs[64];
236 EGLint major, minor, count;
237 struct egl_compositor *ec;
240 ec = malloc(sizeof *ec);
244 ec->base.interface = &interface;
245 ec->wl_display = display;
247 ec->display = eglCreateDisplay(gem_device, "i965");
248 if (ec->display == NULL) {
249 fprintf(stderr, "failed to create display\n");
253 if (!eglInitialize(ec->display, &major, &minor)) {
254 fprintf(stderr, "failed to initialize display\n");
258 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
259 fprintf(stderr, "failed to get configs\n");
263 ec->surface = eglGetFullscreenSurface(ec->display, configs[24], &width, &height);
264 if (ec->surface == NULL) {
265 fprintf(stderr, "failed to create surface\n");
269 ec->context = eglCreateContext(ec->display, configs[24], NULL, NULL);
270 if (ec->context == NULL) {
271 fprintf(stderr, "failed to create context\n");
275 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
276 fprintf(stderr, "failed to make context current\n");
280 glViewport(0, 0, width, height);
281 glMatrixMode(GL_PROJECTION);
283 glOrtho(0, width, height, 0, 0, 1000.0);
284 glMatrixMode(GL_MODELVIEW);
285 glClearColor(0.0, 0.1, 0.3, 0.0);
287 ec->gem_fd = open(gem_device, O_RDWR);
288 if (ec->gem_fd < 0) {
289 fprintf(stderr, "failed to open drm device\n");
293 schedule_repaint(ec);