native_wayland.c revision 34fd282b270dbaf0ce87e342b3183eb3a4bf4a44
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#include "egllog.h"
35
36#include "native_wayland.h"
37
38static struct native_event_handler *wayland_event_handler;
39
40static void
41sync_callback(void *data)
42{
43   int *done = data;
44
45   *done = 1;
46}
47
48static void
49force_roundtrip(struct wl_display *display)
50{
51   int done = 0;
52
53   wl_display_sync_callback(display, sync_callback, &done);
54   wl_display_iterate(display, WL_DISPLAY_WRITABLE);
55   while (!done)
56      wl_display_iterate(display, WL_DISPLAY_READABLE);
57}
58
59static const struct native_config **
60wayland_display_get_configs (struct native_display *ndpy, int *num_configs)
61{
62   struct wayland_display *display = wayland_display(ndpy);
63   const struct native_config **configs;
64
65   if (!display->config) {
66      struct native_config *nconf;
67      enum pipe_format format;
68      display->config = CALLOC(1, sizeof(*display->config));
69      if (!display->config)
70         return NULL;
71      nconf = &display->config->base;
72
73      nconf->buffer_mask =
74         (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
75         (1 << NATIVE_ATTACHMENT_BACK_LEFT);
76
77      format = PIPE_FORMAT_B8G8R8A8_UNORM;
78
79      nconf->color_format = format;
80      nconf->window_bit = TRUE;
81      nconf->pixmap_bit = TRUE;
82   }
83
84   configs = MALLOC(sizeof(*configs));
85   if (configs) {
86      configs[0] = &display->config->base;
87      if (num_configs)
88         *num_configs = 1;
89   }
90
91   return configs;
92}
93
94static int
95wayland_display_get_param(struct native_display *ndpy,
96                          enum native_param_type param)
97{
98   int val;
99
100   switch (param) {
101   case NATIVE_PARAM_USE_NATIVE_BUFFER:
102   case NATIVE_PARAM_PRESERVE_BUFFER:
103   case NATIVE_PARAM_MAX_SWAP_INTERVAL:
104   default:
105      val = 0;
106      break;
107   }
108
109   return val;
110}
111
112static boolean
113wayland_display_is_pixmap_supported(struct native_display *ndpy,
114                                    EGLNativePixmapType pix,
115                                    const struct native_config *nconf)
116{
117   /* all wl_egl_pixmaps are supported */
118
119   return TRUE;
120}
121
122static void
123wayland_pixmap_destroy(struct wl_egl_pixmap *egl_pixmap)
124{
125   struct pipe_resource *resource = egl_pixmap->driver_private;
126
127   assert(resource);
128
129   pipe_resource_reference(&resource, NULL);
130   if (egl_pixmap->buffer) {
131      wl_buffer_destroy(egl_pixmap->buffer);
132      egl_pixmap->buffer = NULL;
133   }
134
135   egl_pixmap->driver_private = NULL;
136   egl_pixmap->destroy = NULL;
137}
138
139static void
140wayland_pixmap_surface_initialize(struct wayland_surface *surface)
141{
142   struct wayland_display *display = wayland_display(&surface->display->base);
143   const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
144
145   if (surface->pix->buffer != NULL)
146      return;
147
148   surface->pix->buffer  = display->create_buffer(display, surface, front_natt);
149   surface->pix->destroy = wayland_pixmap_destroy;
150   surface->pix->driver_private =
151      resource_surface_get_single_resource(surface->rsurf, front_natt);
152}
153
154static void
155wayland_release_pending_resource(void *data)
156{
157   struct wayland_surface *surface = data;
158
159   /* FIXME: print internal error */
160   if (!surface->pending_resource)
161      return;
162
163   pipe_resource_reference(&surface->pending_resource, NULL);
164}
165
166static void
167wayland_window_surface_handle_resize(struct wayland_surface *surface)
168{
169   struct wayland_display *display = surface->display;
170   struct pipe_resource *front_resource;
171   const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
172   int i;
173
174   front_resource = resource_surface_get_single_resource(surface->rsurf,
175                                                         front_natt);
176   if (resource_surface_set_size(surface->rsurf,
177                                 surface->win->width, surface->win->height)) {
178
179      if (surface->pending_resource)
180         force_roundtrip(display->dpy);
181
182      if (front_resource) {
183         surface->pending_resource = front_resource;
184         front_resource = NULL;
185         wl_display_sync_callback(display->dpy,
186                                  wayland_release_pending_resource, surface);
187      }
188
189      for (i = 0; i < WL_BUFFER_COUNT; ++i) {
190         if (surface->buffer[i])
191            wl_buffer_destroy(surface->buffer[i]);
192         surface->buffer[i] = NULL;
193      }
194   }
195   pipe_resource_reference(&front_resource, NULL);
196
197   surface->dx = surface->win->dx;
198   surface->dy = surface->win->dy;
199   surface->win->dx = 0;
200   surface->win->dy = 0;
201}
202
203static boolean
204wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask,
205                         unsigned int *seq_num, struct pipe_resource **textures,
206                         int *width, int *height)
207{
208   struct wayland_surface *surface = wayland_surface(nsurf);
209
210   if (surface->type == WL_WINDOW_SURFACE)
211      wayland_window_surface_handle_resize(surface);
212
213   if (!resource_surface_add_resources(surface->rsurf, attachment_mask |
214                                       surface->attachment_mask))
215      return FALSE;
216
217   if (textures)
218      resource_surface_get_resources(surface->rsurf, textures, attachment_mask);
219
220   if (seq_num)
221      *seq_num = surface->sequence_number;
222
223   resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height);
224
225   if (surface->type == WL_PIXMAP_SURFACE)
226      wayland_pixmap_surface_initialize(surface);
227
228   return TRUE;
229}
230
231static void
232wayland_frame_callback(struct wl_surface *surf, void *data, uint32_t time)
233{
234   struct wayland_surface *surface = data;
235
236   surface->block_swap_buffers = FALSE;
237}
238
239static INLINE void
240wayland_buffers_swap(struct wl_buffer **buffer,
241                     enum wayland_buffer_type buf1,
242                     enum wayland_buffer_type buf2)
243{
244   struct wl_buffer *tmp = buffer[buf1];
245   buffer[buf1] = buffer[buf2];
246   buffer[buf2] = tmp;
247}
248
249static boolean
250wayland_surface_swap_buffers(struct native_surface *nsurf)
251{
252   struct wayland_surface *surface = wayland_surface(nsurf);
253   struct wayland_display *display = surface->display;
254
255   while (surface->block_swap_buffers)
256      wl_display_iterate(display->dpy, WL_DISPLAY_READABLE);
257
258   surface->block_swap_buffers = TRUE;
259   wl_display_frame_callback(display->dpy, surface->win->surface,
260                             wayland_frame_callback, surface);
261
262   if (surface->type == WL_WINDOW_SURFACE) {
263      resource_surface_swap_buffers(surface->rsurf,
264                                    NATIVE_ATTACHMENT_FRONT_LEFT,
265                                    NATIVE_ATTACHMENT_BACK_LEFT, FALSE);
266
267      wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK);
268
269      if (surface->buffer[WL_BUFFER_FRONT] == NULL)
270         surface->buffer[WL_BUFFER_FRONT] =
271            display->create_buffer(display, surface,
272                                   NATIVE_ATTACHMENT_FRONT_LEFT);
273
274      wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT],
275                        surface->dx, surface->dy);
276
277      resource_surface_get_size(surface->rsurf,
278                                (uint *) &surface->win->attached_width,
279                                (uint *) &surface->win->attached_height);
280      surface->dx = 0;
281      surface->dy = 0;
282   }
283
284   surface->sequence_number++;
285   wayland_event_handler->invalid_surface(&display->base,
286                                          &surface->base,
287                                          surface->sequence_number);
288
289   return TRUE;
290}
291
292static boolean
293wayland_surface_present(struct native_surface *nsurf,
294                        enum native_attachment natt,
295                        boolean preserve,
296                        uint swap_interval)
297{
298   struct wayland_surface *surface = wayland_surface(nsurf);
299   uint width, height;
300   boolean ret;
301
302   if (preserve || swap_interval)
303      return FALSE;
304
305   switch (natt) {
306   case NATIVE_ATTACHMENT_FRONT_LEFT:
307      ret = TRUE;
308      break;
309   case NATIVE_ATTACHMENT_BACK_LEFT:
310      ret = wayland_surface_swap_buffers(nsurf);
311      break;
312   default:
313      ret = FALSE;
314      break;
315   }
316
317   if (surface->type == WL_WINDOW_SURFACE) {
318      resource_surface_get_size(surface->rsurf, &width, &height);
319      wl_buffer_damage(surface->buffer[WL_BUFFER_FRONT], 0, 0, width, height);
320      wl_surface_damage(surface->win->surface, 0, 0, width, height);
321   }
322
323   return ret;
324}
325
326static void
327wayland_surface_wait(struct native_surface *nsurf)
328{
329   /* no-op */
330}
331
332static void
333wayland_surface_destroy(struct native_surface *nsurf)
334{
335   struct wayland_surface *surface = wayland_surface(nsurf);
336   enum wayland_buffer_type buffer;
337
338   for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) {
339      if (surface->buffer[buffer])
340         wl_buffer_destroy(surface->buffer[buffer]);
341   }
342
343   resource_surface_destroy(surface->rsurf);
344   FREE(surface);
345}
346
347
348
349static struct native_surface *
350wayland_create_pixmap_surface(struct native_display *ndpy,
351                              EGLNativePixmapType pix,
352                              const struct native_config *nconf)
353{
354   struct wayland_display *display = wayland_display(ndpy);
355   struct wayland_surface *surface;
356   struct wl_egl_pixmap *egl_pixmap = (struct wl_egl_pixmap *) pix;
357   enum native_attachment natt = NATIVE_ATTACHMENT_FRONT_LEFT;
358   uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
359      PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT;
360
361   surface = CALLOC_STRUCT(wayland_surface);
362   if (!surface)
363      return NULL;
364
365   surface->display = display;
366
367   surface->pending_resource = NULL;
368   surface->type = WL_PIXMAP_SURFACE;
369   surface->pix = egl_pixmap;
370
371   if (surface->pix->visual == wl_display_get_rgb_visual(display->dpy))
372      surface->color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
373   else
374      surface->color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
375
376   surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT);
377
378   surface->rsurf = resource_surface_create(display->base.screen,
379                                            surface->color_format, bind);
380
381   if (!surface->rsurf) {
382      FREE(surface);
383      return NULL;
384   }
385
386   resource_surface_set_size(surface->rsurf,
387                             egl_pixmap->width, egl_pixmap->height);
388
389   /* the pixmap is already allocated, so import it */
390   if (surface->pix->buffer != NULL)
391      resource_surface_import_resource(surface->rsurf, natt,
392                                       surface->pix->driver_private);
393
394   surface->base.destroy = wayland_surface_destroy;
395   surface->base.present = wayland_surface_present;
396   surface->base.validate = wayland_surface_validate;
397   surface->base.wait = wayland_surface_wait;
398
399   return &surface->base;
400}
401
402
403static struct native_surface *
404wayland_create_window_surface(struct native_display *ndpy,
405                              EGLNativeWindowType win,
406                              const struct native_config *nconf)
407{
408   struct wayland_display *display = wayland_display(ndpy);
409   struct wayland_config *config = wayland_config(nconf);
410   struct wayland_surface *surface;
411   uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
412      PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT;
413
414   surface = CALLOC_STRUCT(wayland_surface);
415   if (!surface)
416      return NULL;
417
418   surface->display = display;
419   surface->color_format = config->base.color_format;
420
421   surface->win = (struct wl_egl_window *) win;
422
423   surface->pending_resource = NULL;
424   surface->block_swap_buffers = FALSE;
425   surface->type = WL_WINDOW_SURFACE;
426
427   surface->buffer[WL_BUFFER_FRONT] = NULL;
428   surface->buffer[WL_BUFFER_BACK] = NULL;
429   surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
430      (1 << NATIVE_ATTACHMENT_BACK_LEFT);
431
432   surface->rsurf = resource_surface_create(display->base.screen,
433                                            surface->color_format, bind);
434
435   if (!surface->rsurf) {
436      FREE(surface);
437      return NULL;
438   }
439
440   surface->base.destroy = wayland_surface_destroy;
441   surface->base.present = wayland_surface_present;
442   surface->base.validate = wayland_surface_validate;
443   surface->base.wait = wayland_surface_wait;
444
445   return &surface->base;
446}
447
448static void
449native_set_event_handler(struct native_event_handler *event_handler)
450{
451   wayland_event_handler = event_handler;
452}
453
454static struct native_display *
455native_create_display(void *dpy, boolean use_sw, void *user_data)
456{
457   struct wayland_display *display = NULL;
458
459   display = wayland_create_drm_display((struct wl_display *) dpy,
460                                        wayland_event_handler,
461                                        user_data);
462   if (!display)
463      return NULL;
464
465   display->base.get_param = wayland_display_get_param;
466   display->base.get_configs = wayland_display_get_configs;
467   display->base.is_pixmap_supported = wayland_display_is_pixmap_supported;
468   display->base.create_window_surface = wayland_create_window_surface;
469   display->base.create_pixmap_surface = wayland_create_pixmap_surface;
470
471   return &display->base;
472}
473
474static const struct native_platform wayland_platform = {
475   "wayland", /* name */
476   native_set_event_handler,
477   native_create_display
478};
479
480const struct native_platform *
481native_get_wayland_platform(void)
482{
483   return &wayland_platform;
484}
485
486/* vim: set sw=3 ts=8 sts=3 expandtab: */
487