st/egl: Add wayland platform
[mesa.git] / src / gallium / state_trackers / egl / wayland / native_wayland.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.11
4 *
5 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "util/u_memory.h"
27 #include "util/u_inlines.h"
28
29 #include "pipe/p_compiler.h"
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 #include "state_tracker/drm_driver.h"
34
35 #include "egllog.h"
36
37 #include "native_wayland.h"
38
39 /* see get_drm_screen_name */
40 #include <radeon_drm.h>
41 #include "radeon/drm/radeon_drm_public.h"
42
43 #include <wayland-client.h>
44 #include "wayland-egl-priv.h"
45
46 #include <xf86drm.h>
47
48 static struct native_event_handler *wayland_event_handler;
49
50 static void
51 sync_callback(void *data)
52 {
53 int *done = data;
54
55 *done = 1;
56 }
57
58 static void
59 force_roundtrip(struct wl_display *display)
60 {
61 int done = 0;
62
63 wl_display_sync_callback(display, sync_callback, &done);
64 wl_display_iterate(display, WL_DISPLAY_WRITABLE);
65 while (!done)
66 wl_display_iterate(display, WL_DISPLAY_READABLE);
67 }
68
69 static const struct native_config **
70 wayland_display_get_configs (struct native_display *ndpy, int *num_configs)
71 {
72 struct wayland_display *display = wayland_display(ndpy);
73 const struct native_config **configs;
74
75 if (!display->config) {
76 struct native_config *nconf;
77 enum pipe_format format;
78 display->config = CALLOC(1, sizeof(*display->config));
79 if (!display->config)
80 return NULL;
81 nconf = &display->config->base;
82
83 nconf->buffer_mask =
84 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
85 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
86
87 format = PIPE_FORMAT_B8G8R8X8_UNORM;
88
89 nconf->color_format = format;
90 nconf->window_bit = TRUE;
91 nconf->pixmap_bit = TRUE;
92 }
93
94 configs = MALLOC(sizeof(*configs));
95 if (configs) {
96 configs[0] = &display->config->base;
97 if (num_configs)
98 *num_configs = 1;
99 }
100
101 return configs;
102 }
103
104 static int
105 wayland_display_get_param(struct native_display *ndpy,
106 enum native_param_type param)
107 {
108 int val;
109
110 switch (param) {
111 case NATIVE_PARAM_USE_NATIVE_BUFFER:
112 case NATIVE_PARAM_PRESERVE_BUFFER:
113 case NATIVE_PARAM_MAX_SWAP_INTERVAL:
114 default:
115 val = 0;
116 break;
117 }
118
119 return val;
120 }
121
122 static boolean
123 wayland_display_is_pixmap_supported(struct native_display *ndpy,
124 EGLNativePixmapType pix,
125 const struct native_config *nconf)
126 {
127 /* all wl_egl_pixmaps are supported */
128
129 return TRUE;
130 }
131
132 static void
133 wayland_display_destroy(struct native_display *ndpy)
134 {
135 struct wayland_display *display = wayland_display(ndpy);
136
137 if (display->config)
138 FREE(display->config);
139
140 if (display->base.screen)
141 display->base.screen->destroy(display->base.screen);
142
143 FREE(display);
144 }
145
146
147 static struct wl_buffer *
148 wayland_create_buffer(struct wayland_surface *surface,
149 enum native_attachment attachment)
150 {
151 struct wayland_display *display = surface->display;
152 struct pipe_resource *resource;
153 struct winsys_handle wsh;
154 uint width, height;
155
156 resource = resource_surface_get_single_resource(surface->rsurf, attachment);
157 resource_surface_get_size(surface->rsurf, &width, &height);
158
159 wsh.type = DRM_API_HANDLE_TYPE_SHARED;
160 display->base.screen->resource_get_handle(display->base.screen, resource, &wsh);
161
162 pipe_resource_reference(&resource, NULL);
163
164 return wl_drm_create_buffer(display->dpy->drm, wsh.handle,
165 width, height,
166 wsh.stride, surface->win->visual);
167 }
168
169 static void
170 wayland_pixmap_destroy(struct wl_egl_pixmap *egl_pixmap)
171 {
172 struct pipe_resource *resource = egl_pixmap->driver_private;
173
174 assert(resource);
175
176 pipe_resource_reference(&resource, NULL);
177
178 egl_pixmap->driver_private = NULL;
179 egl_pixmap->destroy = NULL;
180 egl_pixmap->name = 0;
181 }
182
183 static void
184 wayland_pixmap_surface_intialize(struct wayland_surface *surface)
185 {
186 struct native_display *ndpy = &surface->display->base;
187 struct pipe_resource *resource;
188 struct winsys_handle wsh;
189 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
190
191 if (surface->pix->name > 0)
192 return;
193
194 resource = resource_surface_get_single_resource(surface->rsurf, front_natt);
195
196 wsh.type = DRM_API_HANDLE_TYPE_SHARED;
197 ndpy->screen->resource_get_handle(ndpy->screen, resource, &wsh);
198
199 surface->pix->name = wsh.handle;
200 surface->pix->stride = wsh.stride;
201 surface->pix->destroy = wayland_pixmap_destroy;
202 surface->pix->driver_private = resource;
203 }
204
205 static void
206 wayland_window_surface_handle_resize(struct wayland_surface *surface)
207 {
208 int i;
209
210 if (resource_surface_set_size(surface->rsurf,
211 surface->win->width, surface->win->height)) {
212 for (i = 0; i < WL_BUFFER_COUNT; ++i) {
213 if (surface->buffer[i])
214 wl_buffer_destroy(surface->buffer[i]);
215 surface->buffer[i] = NULL;
216 }
217 }
218
219 surface->dx = surface->win->dx;
220 surface->dy = surface->win->dy;
221 surface->win->dx = 0;
222 surface->win->dy = 0;
223 }
224
225 static boolean
226 wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask,
227 unsigned int *seq_num, struct pipe_resource **textures,
228 int *width, int *height)
229 {
230 struct wayland_surface *surface = wayland_surface(nsurf);
231
232 if (surface->type == WL_WINDOW_SURFACE)
233 wayland_window_surface_handle_resize(surface);
234
235 if (!resource_surface_add_resources(surface->rsurf, attachment_mask |
236 surface->attachment_mask))
237 return FALSE;
238
239 if (textures)
240 resource_surface_get_resources(surface->rsurf, textures, attachment_mask);
241
242 if (seq_num)
243 *seq_num = surface->sequence_number;
244
245 resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height);
246
247 if (surface->type == WL_PIXMAP_SURFACE)
248 wayland_pixmap_surface_intialize(surface);
249
250 return TRUE;
251 }
252
253 static void
254 wayland_frame_callback(void *data, uint32_t time)
255 {
256 struct wayland_surface *surface = data;
257
258 surface->block_swap_buffers = FALSE;
259 }
260
261 static INLINE void
262 wayland_buffers_swap(struct wl_buffer **buffer,
263 enum wayland_buffer_type buf1,
264 enum wayland_buffer_type buf2)
265 {
266 struct wl_buffer *tmp = buffer[buf1];
267 buffer[buf1] = buffer[buf2];
268 buffer[buf2] = tmp;
269 }
270
271 static boolean
272 wayland_surface_swap_buffers(struct native_surface *nsurf)
273 {
274 struct wayland_surface *surface = wayland_surface(nsurf);
275 struct wayland_display *display = surface->display;
276
277 while (surface->block_swap_buffers)
278 wl_display_iterate(display->dpy->display, WL_DISPLAY_READABLE);
279
280 surface->block_swap_buffers = TRUE;
281 wl_display_frame_callback(display->dpy->display, wayland_frame_callback,
282 surface);
283
284 if (surface->type == WL_WINDOW_SURFACE) {
285 resource_surface_swap_buffers(surface->rsurf,
286 NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, FALSE);
287
288 wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK);
289
290 if (surface->buffer[WL_BUFFER_FRONT] == NULL)
291 surface->buffer[WL_BUFFER_FRONT] =
292 wayland_create_buffer(surface, NATIVE_ATTACHMENT_FRONT_LEFT);
293
294 wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT],
295 surface->dx, surface->dy);
296
297 resource_surface_get_size(surface->rsurf,
298 (uint *) &surface->win->attached_width,
299 (uint *) &surface->win->attached_height);
300 surface->dx = 0;
301 surface->dy = 0;
302 }
303
304 surface->sequence_number++;
305 wayland_event_handler->invalid_surface(&display->base,
306 &surface->base, surface->sequence_number);
307
308 return TRUE;
309 }
310
311 static boolean
312 wayland_surface_present(struct native_surface *nsurf,
313 enum native_attachment natt,
314 boolean preserve,
315 uint swap_interval)
316 {
317 struct wayland_surface *surface = wayland_surface(nsurf);
318 uint width, height;
319 boolean ret;
320
321 if (preserve || swap_interval)
322 return FALSE;
323
324 switch (natt) {
325 case NATIVE_ATTACHMENT_FRONT_LEFT:
326 ret = TRUE;
327 break;
328 case NATIVE_ATTACHMENT_BACK_LEFT:
329 ret = wayland_surface_swap_buffers(nsurf);
330 break;
331 default:
332 ret = FALSE;
333 break;
334 }
335
336 if (surface->type == WL_WINDOW_SURFACE) {
337 resource_surface_get_size(surface->rsurf, &width, &height);
338 wl_surface_damage(surface->win->surface, 0, 0, width, height);
339 }
340
341 return ret;
342 }
343
344 static void
345 wayland_surface_wait(struct native_surface *nsurf)
346 {
347 /* no-op */
348 }
349
350 static void
351 wayland_surface_destroy(struct native_surface *nsurf)
352 {
353 struct wayland_surface *surface = wayland_surface(nsurf);
354 enum wayland_buffer_type buffer;
355
356 for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) {
357 if (surface->buffer[buffer])
358 wl_buffer_destroy(surface->buffer[buffer]);
359 }
360
361 resource_surface_destroy(surface->rsurf);
362 FREE(surface);
363 }
364
365 static struct native_surface *
366 wayland_create_pixmap_surface(struct native_display *ndpy,
367 EGLNativePixmapType pix,
368 const struct native_config *nconf)
369 {
370 struct wayland_display *display = wayland_display(ndpy);
371 struct wayland_config *config = wayland_config(nconf);
372 struct wayland_surface *surface;
373 struct wl_egl_pixmap *egl_pixmap = (struct wl_egl_pixmap *) pix;
374 enum native_attachment natt = NATIVE_ATTACHMENT_FRONT_LEFT;
375
376 surface = CALLOC_STRUCT(wayland_surface);
377 if (!surface)
378 return NULL;
379
380 surface->display = display;
381 if (config)
382 surface->color_format = config->base.color_format;
383 else
384 surface->color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
385
386 surface->type = WL_PIXMAP_SURFACE;
387 surface->pix = egl_pixmap;
388
389 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT);
390
391 surface->rsurf = resource_surface_create(display->base.screen,
392 surface->color_format,
393 PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
394 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT);
395
396 if (!surface->rsurf) {
397 FREE(surface);
398 return NULL;
399 }
400
401 resource_surface_set_size(surface->rsurf,
402 egl_pixmap->width, egl_pixmap->height);
403
404 /* the pixmap is already allocated, so import it */
405 if (surface->pix->name > 0)
406 resource_surface_import_resource(surface->rsurf, natt,
407 surface->pix->driver_private);
408
409 surface->base.destroy = wayland_surface_destroy;
410 surface->base.present = wayland_surface_present;
411 surface->base.validate = wayland_surface_validate;
412 surface->base.wait = wayland_surface_wait;
413
414 return &surface->base;
415 }
416
417 static struct native_surface *
418 wayland_create_window_surface(struct native_display *ndpy,
419 EGLNativeWindowType win,
420 const struct native_config *nconf)
421 {
422 struct wayland_display *display = wayland_display(ndpy);
423 struct wayland_config *config = wayland_config(nconf);
424 struct wayland_surface *surface;
425
426 surface = CALLOC_STRUCT(wayland_surface);
427 if (!surface)
428 return NULL;
429
430 surface->display = display;
431 surface->color_format = config->base.color_format;
432
433 surface->win = (struct wl_egl_window *) win;
434
435 surface->block_swap_buffers = FALSE;
436 surface->type = WL_WINDOW_SURFACE;
437
438 surface->buffer[WL_BUFFER_FRONT] = NULL;
439 surface->buffer[WL_BUFFER_BACK] = NULL;
440 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
441 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
442
443 surface->rsurf = resource_surface_create(display->base.screen,
444 surface->color_format,
445 PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
446 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT);
447
448 if (!surface->rsurf) {
449 FREE(surface);
450 return NULL;
451 }
452
453 surface->base.destroy = wayland_surface_destroy;
454 surface->base.present = wayland_surface_present;
455 surface->base.validate = wayland_surface_validate;
456 surface->base.wait = wayland_surface_wait;
457
458 return &surface->base;
459 }
460
461 static const char *
462 get_drm_screen_name(int fd, drmVersionPtr version)
463 {
464 const char *name = version->name;
465
466 if (name && !strcmp(name, "radeon")) {
467 int chip_id;
468 struct drm_radeon_info info;
469
470 memset(&info, 0, sizeof(info));
471 info.request = RADEON_INFO_DEVICE_ID;
472 info.value = pointer_to_intptr(&chip_id);
473 if (drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info)) != 0)
474 return NULL;
475
476 name = is_r3xx(chip_id) ? "r300" : "r600";
477 }
478
479 return name;
480 }
481
482 static boolean
483 wayland_display_init_screen(struct native_display *ndpy)
484 {
485 struct wayland_display *display = wayland_display(ndpy);
486 drmVersionPtr version;
487 const char *driver_name;
488
489 if (display->dpy->fd == -1)
490 force_roundtrip(display->dpy->display);
491 if (display->dpy->fd == -1)
492 return FALSE;
493
494 if (!display->dpy->authenticated)
495 force_roundtrip(display->dpy->display);
496 if (!display->dpy->authenticated)
497 return FALSE;
498
499 version = drmGetVersion(display->dpy->fd);
500 if (!version) {
501 _eglLog(_EGL_WARNING, "invalid fd %d", display->dpy->fd);
502 return FALSE;
503 }
504
505 /* FIXME: share this with native_drm or egl_dri2 */
506 driver_name = get_drm_screen_name(display->dpy->fd, version);
507
508 display->base.screen =
509 wayland_event_handler->new_drm_screen(&display->base,
510 driver_name, display->dpy->fd);
511 drmFreeVersion(version);
512
513 if (!display->base.screen) {
514 _eglLog(_EGL_WARNING, "failed to create DRM screen");
515 return FALSE;
516 }
517
518 return TRUE;
519 }
520
521
522 static void
523 wayland_set_event_handler(struct native_event_handler *event_handler)
524 {
525 wayland_event_handler = event_handler;
526 }
527
528 static struct pipe_resource *
529 wayland_display_import_buffer(struct native_display *ndpy,
530 const struct pipe_resource *templ,
531 void *buf)
532 {
533 return ndpy->screen->resource_from_handle(ndpy->screen,
534 templ, (struct winsys_handle *) buf);
535 }
536
537 static boolean
538 wayland_display_export_buffer(struct native_display *ndpy,
539 struct pipe_resource *res,
540 void *buf)
541 {
542 return ndpy->screen->resource_get_handle(ndpy->screen,
543 res, (struct winsys_handle *) buf);
544 }
545
546 static struct native_display_buffer wayland_display_buffer = {
547 wayland_display_import_buffer,
548 wayland_display_export_buffer
549 };
550
551 static struct native_display *
552 wayland_display_create(void *dpy, boolean use_sw, void *user_data)
553 {
554 struct wayland_display *display;
555
556 display = CALLOC_STRUCT(wayland_display);
557 if (!display)
558 return NULL;
559
560 display->base.user_data = user_data;
561
562 display->dpy = dpy;
563 if (!display->dpy->display) {
564 wayland_display_destroy(&display->base);
565 return NULL;
566 }
567
568 if (!wayland_display_init_screen(&display->base)) {
569 wayland_display_destroy(&display->base);
570 return NULL;
571 }
572
573 display->base.destroy = wayland_display_destroy;
574 display->base.get_param = wayland_display_get_param;
575 display->base.get_configs = wayland_display_get_configs;
576 display->base.is_pixmap_supported = wayland_display_is_pixmap_supported;
577 display->base.create_window_surface = wayland_create_window_surface;
578 display->base.create_pixmap_surface = wayland_create_pixmap_surface;
579 display->base.buffer = &wayland_display_buffer;
580
581 return &display->base;
582 }
583
584 static const struct native_platform wayland_platform = {
585 "wayland", /* name */
586 wayland_set_event_handler,
587 wayland_display_create
588 };
589
590 const struct native_platform *
591 native_get_wayland_platform(void)
592 {
593 return &wayland_platform;
594 }