native_wayland.c revision c79a5a706727c40a856e36c043da608c825390a2
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
48static struct native_event_handler *wayland_event_handler;
49
50static void
51sync_callback(void *data)
52{
53   int *done = data;
54
55   *done = 1;
56}
57
58static void
59force_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
69static const struct native_config **
70wayland_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_B8G8R8A8_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
104static int
105wayland_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
122static boolean
123wayland_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
132static void
133wayland_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
147static struct wl_buffer *
148wayland_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
169static void
170wayland_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
183static void
184wayland_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
205static void
206wayland_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
225static boolean
226wayland_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
253static void
254wayland_frame_callback(void *data, uint32_t time)
255{
256   struct wayland_surface *surface = data;
257
258   surface->block_swap_buffers = FALSE;
259}
260
261static INLINE void
262wayland_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
271static boolean
272wayland_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
311static boolean
312wayland_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
344static void
345wayland_surface_wait(struct native_surface *nsurf)
346{
347   /* no-op */
348}
349
350static void
351wayland_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
365static struct native_surface *
366wayland_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
382   surface->type = WL_PIXMAP_SURFACE;
383   surface->pix = egl_pixmap;
384
385   if (surface->pix->visual == wl_display_get_rgb_visual(display->dpy->display))
386      surface->color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
387   else
388      surface->color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
389
390   surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT);
391
392   surface->rsurf = resource_surface_create(display->base.screen,
393           surface->color_format,
394           PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
395           PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT);
396
397   if (!surface->rsurf) {
398       FREE(surface);
399       return NULL;
400   }
401
402   resource_surface_set_size(surface->rsurf,
403                             egl_pixmap->width, egl_pixmap->height);
404
405   /* the pixmap is already allocated, so import it */
406   if (surface->pix->name > 0)
407      resource_surface_import_resource(surface->rsurf, natt,
408                                       surface->pix->driver_private);
409
410   surface->base.destroy = wayland_surface_destroy;
411   surface->base.present = wayland_surface_present;
412   surface->base.validate = wayland_surface_validate;
413   surface->base.wait = wayland_surface_wait;
414
415   return &surface->base;
416}
417
418static struct native_surface *
419wayland_create_window_surface(struct native_display *ndpy,
420                              EGLNativeWindowType win,
421                              const struct native_config *nconf)
422{
423   struct wayland_display *display = wayland_display(ndpy);
424   struct wayland_config *config = wayland_config(nconf);
425   struct wayland_surface *surface;
426
427   surface = CALLOC_STRUCT(wayland_surface);
428   if (!surface)
429      return NULL;
430
431   surface->display = display;
432   surface->color_format = config->base.color_format;
433
434   surface->win = (struct wl_egl_window *) win;
435
436   surface->block_swap_buffers = FALSE;
437   surface->type = WL_WINDOW_SURFACE;
438
439   surface->buffer[WL_BUFFER_FRONT] = NULL;
440   surface->buffer[WL_BUFFER_BACK] = NULL;
441   surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
442                              (1 << NATIVE_ATTACHMENT_BACK_LEFT);
443
444   surface->rsurf = resource_surface_create(display->base.screen,
445           surface->color_format,
446           PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
447           PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT);
448
449   if (!surface->rsurf) {
450       FREE(surface);
451       return NULL;
452   }
453
454   surface->base.destroy = wayland_surface_destroy;
455   surface->base.present = wayland_surface_present;
456   surface->base.validate = wayland_surface_validate;
457   surface->base.wait = wayland_surface_wait;
458
459   return &surface->base;
460}
461
462static const char *
463get_drm_screen_name(int fd, drmVersionPtr version)
464{
465   const char *name = version->name;
466
467   if (name && !strcmp(name, "radeon")) {
468      int chip_id;
469      struct drm_radeon_info info;
470
471      memset(&info, 0, sizeof(info));
472      info.request = RADEON_INFO_DEVICE_ID;
473      info.value = pointer_to_intptr(&chip_id);
474      if (drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info)) != 0)
475         return NULL;
476
477      name = is_r3xx(chip_id) ? "r300" : "r600";
478   }
479
480   return name;
481}
482
483static boolean
484wayland_display_init_screen(struct native_display *ndpy)
485{
486   struct wayland_display *display = wayland_display(ndpy);
487   drmVersionPtr version;
488   const char *driver_name;
489
490   if (display->dpy->fd == -1)
491	   force_roundtrip(display->dpy->display);
492   if (display->dpy->fd == -1)
493	   return FALSE;
494
495   if (!display->dpy->authenticated)
496	   force_roundtrip(display->dpy->display);
497   if (!display->dpy->authenticated)
498	   return FALSE;
499
500   version = drmGetVersion(display->dpy->fd);
501   if (!version) {
502      _eglLog(_EGL_WARNING, "invalid fd %d", display->dpy->fd);
503      return FALSE;
504   }
505
506   /* FIXME: share this with native_drm or egl_dri2 */
507   driver_name = get_drm_screen_name(display->dpy->fd, version);
508
509   display->base.screen =
510      wayland_event_handler->new_drm_screen(&display->base,
511            driver_name, display->dpy->fd);
512   drmFreeVersion(version);
513
514   if (!display->base.screen) {
515      _eglLog(_EGL_WARNING, "failed to create DRM screen");
516      return FALSE;
517   }
518
519   return TRUE;
520}
521
522
523static void
524wayland_set_event_handler(struct native_event_handler *event_handler)
525{
526   wayland_event_handler = event_handler;
527}
528
529static struct pipe_resource *
530wayland_display_import_buffer(struct native_display *ndpy,
531                              const struct pipe_resource *templ,
532                              void *buf)
533{
534   return ndpy->screen->resource_from_handle(ndpy->screen,
535         templ, (struct winsys_handle *) buf);
536}
537
538static boolean
539wayland_display_export_buffer(struct native_display *ndpy,
540                              struct pipe_resource *res,
541                              void *buf)
542{
543   return ndpy->screen->resource_get_handle(ndpy->screen,
544         res, (struct winsys_handle *) buf);
545}
546
547static struct native_display_buffer wayland_display_buffer = {
548   wayland_display_import_buffer,
549   wayland_display_export_buffer
550};
551
552static struct native_display *
553wayland_display_create(void *dpy, boolean use_sw, void *user_data)
554{
555   struct wayland_display *display;
556
557   display = CALLOC_STRUCT(wayland_display);
558   if (!display)
559      return NULL;
560
561   display->base.user_data = user_data;
562
563   display->dpy = dpy;
564   if (!display->dpy->display) {
565      wayland_display_destroy(&display->base);
566      return NULL;
567   }
568
569   if (!wayland_display_init_screen(&display->base)) {
570      wayland_display_destroy(&display->base);
571      return NULL;
572   }
573
574   display->base.destroy = wayland_display_destroy;
575   display->base.get_param = wayland_display_get_param;
576   display->base.get_configs = wayland_display_get_configs;
577   display->base.is_pixmap_supported = wayland_display_is_pixmap_supported;
578   display->base.create_window_surface = wayland_create_window_surface;
579   display->base.create_pixmap_surface = wayland_create_pixmap_surface;
580   display->base.buffer = &wayland_display_buffer;
581
582   return &display->base;
583}
584
585static const struct native_platform wayland_platform = {
586   "wayland", /* name */
587   wayland_set_event_handler,
588   wayland_display_create
589};
590
591const struct native_platform *
592native_get_wayland_platform(void)
593{
594   return &wayland_platform;
595}
596