]> git.sesse.net Git - vlc/blob - modules/video_output/wayland/shm.c
Wayland/SHM: fix memory leak
[vlc] / modules / video_output / wayland / shm.c
1 /**
2  * @file shm.c
3  * @brief Wayland shared memory video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2014 Rémi Denis-Courmont
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <assert.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <sys/mman.h>
35 #include <unistd.h>
36
37 #include <wayland-client.h>
38 #include "scaler-client-protocol.h"
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_vout_display.h>
43 #include <vlc_picture_pool.h>
44
45 #define MAX_PICTURES 4
46
47 struct vout_display_sys_t
48 {
49     vout_window_t *embed; /* VLC window */
50     struct wl_event_queue *eventq;
51     struct wl_shm *shm;
52     struct wl_scaler *scaler;
53     struct wl_viewport *viewport;
54
55     picture_pool_t *pool; /* picture pool */
56
57     int x;
58     int y;
59     bool use_buffer_transform;
60 };
61
62 static void PictureDestroy(picture_t *pic)
63 {
64     struct wl_buffer *buf = (struct wl_buffer *)pic->p_sys;
65     const long pagemask = sysconf(_SC_PAGE_SIZE) - 1;
66     size_t picsize = pic->p[0].i_pitch * pic->p[0].i_lines;
67
68     munmap(pic->p[0].p_pixels, (picsize + pagemask) & ~pagemask);
69     wl_buffer_destroy(buf); /* XXX: what if wl_display is already gone? */
70     free(pic);
71 }
72
73 static void buffer_release_cb(void *data, struct wl_buffer *buffer)
74 {
75     picture_t *pic = data;
76
77     picture_Release(pic);
78     (void) buffer;
79 }
80
81 static const struct wl_buffer_listener buffer_cbs =
82 {
83     buffer_release_cb,
84 };
85
86 static picture_pool_t *Pool(vout_display_t *vd, unsigned req)
87 {
88     vout_display_sys_t *sys = vd->sys;
89
90     if (sys->pool != NULL)
91         return sys->pool;
92
93     if (req > MAX_PICTURES)
94         req = MAX_PICTURES;
95
96     char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
97     int fd = mkostemp(bufpath, O_CLOEXEC);
98     if (fd == -1)
99     {
100         msg_Err(vd, "cannot create buffers: %s", vlc_strerror_c(errno));
101         return NULL;
102     }
103     unlink(bufpath);
104
105     /* We need one extra line to cover for horizontal crop offset */
106     unsigned stride = 4 * ((vd->fmt.i_width + 31) & ~31);
107     unsigned lines = (vd->fmt.i_height + 31 + (sys->viewport == NULL)) & ~31;
108     const long pagemask = sysconf(_SC_PAGE_SIZE) - 1;
109     size_t picsize = ((stride * lines) + pagemask) & ~pagemask;
110     size_t length = picsize * req;
111
112     if (ftruncate(fd, length))
113     {
114         msg_Err(vd, "cannot allocate buffers: %s", vlc_strerror_c(errno));
115         close(fd);
116         return NULL;
117     }
118
119     void *base = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
120     if (base == MAP_FAILED)
121     {
122         msg_Err(vd, "cannot map buffers: %s", vlc_strerror_c(errno));
123         close(fd);
124         return NULL;
125     }
126 #ifndef NDEBUG
127     memset(base, 0x80, length); /* gray fill */
128 #endif
129
130     struct wl_shm_pool *shm_pool = wl_shm_create_pool(sys->shm, fd, length);
131     close(fd);
132     if (shm_pool == NULL)
133     {
134         munmap(base, length);
135         return NULL;
136     }
137
138     picture_t *pics[MAX_PICTURES];
139     picture_resource_t res = {
140         .pf_destroy = PictureDestroy,
141         .p = {
142             [0] = {
143                 .i_lines = lines,
144                 .i_pitch = stride,
145             },
146         },
147     };
148     size_t offset = 0;
149     unsigned width = vd->fmt.i_visible_width;
150     unsigned height = vd->fmt.i_visible_height;
151     unsigned count = 0;
152
153     if (sys->viewport == NULL) /* Poor man's crop */
154         offset += 4 * vd->fmt.i_x_offset + stride * vd->fmt.i_y_offset;
155
156     while (count < req)
157     {
158         struct wl_buffer *buf;
159
160         buf = wl_shm_pool_create_buffer(shm_pool, offset, width, height,
161                                         stride, WL_SHM_FORMAT_XRGB8888);
162         if (buf == NULL)
163             break;
164
165         res.p_sys = (picture_sys_t *)buf;
166         res.p[0].p_pixels = base;
167         base = ((char *)base) + picsize;
168         offset += picsize;
169         length -= picsize;
170
171         picture_t *pic = picture_NewFromResource(&vd->fmt, &res);
172         if (unlikely(pic == NULL))
173         {
174             wl_buffer_destroy(buf);
175             break;
176         }
177
178         wl_buffer_add_listener(buf, &buffer_cbs, pic);
179         pics[count++] = pic;
180     }
181
182     wl_shm_pool_destroy(shm_pool);
183     wl_display_flush(sys->embed->display.wl);
184
185     if (length > 0)
186         munmap(base, length); /* Left-over buffers */
187     if (count == 0)
188         return NULL;
189
190     sys->pool = picture_pool_New (count, pics);
191     if (unlikely(sys->pool == NULL))
192     {
193         while (count > 0)
194             picture_Release(pics[--count]);
195         return NULL;
196     }
197     return sys->pool;
198 }
199
200 static void Prepare(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
201 {
202     vout_display_sys_t *sys = vd->sys;
203     struct wl_display *display = sys->embed->display.wl;
204     struct wl_surface *surface = sys->embed->handle.wl;
205     struct wl_buffer *buf = (struct wl_buffer *)pic->p_sys;
206
207     wl_surface_attach(surface, buf, sys->x, sys->y);
208     wl_surface_damage(surface, 0, 0,
209                       vd->cfg->display.width, vd->cfg->display.height);
210     wl_display_flush(display);
211
212     sys->x = 0;
213     sys->y = 0;
214
215     (void) subpic;
216 }
217
218 static void Display(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
219 {
220     vout_display_sys_t *sys = vd->sys;
221     struct wl_display *display = sys->embed->display.wl;
222     struct wl_surface *surface = sys->embed->handle.wl;
223
224     wl_surface_commit(surface);
225     wl_display_roundtrip_queue(display, sys->eventq);
226
227     (void) pic; (void) subpic;
228 }
229
230 static void ResetPictures(vout_display_t *vd)
231 {
232     vout_display_sys_t *sys = vd->sys;
233
234     if (sys->pool == NULL)
235         return;
236
237     picture_pool_Delete(sys->pool);
238     sys->pool = NULL;
239 }
240
241 static int Control(vout_display_t *vd, int query, va_list ap)
242 {
243     vout_display_sys_t *sys = vd->sys;
244
245     switch (query)
246     {
247         case VOUT_DISPLAY_HIDE_MOUSE:
248             /* TODO */
249             return VLC_EGENERIC;
250
251         case VOUT_DISPLAY_RESET_PICTURES:
252         {
253             vout_display_place_t place;
254             video_format_t src;
255
256             assert(sys->viewport == NULL);
257
258             vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
259             video_format_ApplyRotation(&src, &vd->source);
260
261             vd->fmt.i_width  = src.i_width  * place.width
262                                             / src.i_visible_width;
263             vd->fmt.i_height = src.i_height * place.height
264                                             / src.i_visible_height;
265             vd->fmt.i_visible_width  = place.width;
266             vd->fmt.i_visible_height = place.height;
267             vd->fmt.i_x_offset = src.i_x_offset * place.width
268                                                 / src.i_visible_width;
269             vd->fmt.i_y_offset = src.i_y_offset * place.height
270                                                 / src.i_visible_height;
271             ResetPictures(vd);
272             break;
273         }
274
275         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
276         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
277         case VOUT_DISPLAY_CHANGE_ZOOM:
278         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
279         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
280         {
281             const vout_display_cfg_t *cfg;
282             const video_format_t *src;
283
284             if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
285              || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
286             {
287                 src = va_arg(ap, const video_format_t *);
288                 cfg = vd->cfg;
289             }
290             else
291             {
292                 src = &vd->source;
293                 cfg = va_arg(ap, const vout_display_cfg_t *);
294             }
295
296             vout_display_place_t place;
297
298             vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
299             sys->x += place.width / 2;
300             sys->y += place.height / 2;
301
302             vout_display_PlacePicture(&place, src, cfg, false);
303             sys->x -= place.width / 2;
304             sys->y -= place.height / 2;
305
306             if (sys->viewport != NULL)
307             {
308                 video_format_t fmt;
309
310                 video_format_ApplyRotation(&fmt, src);
311                 wl_viewport_set(sys->viewport,
312                                 wl_fixed_from_int(fmt.i_x_offset),
313                                 wl_fixed_from_int(fmt.i_y_offset),
314                                 wl_fixed_from_int(fmt.i_visible_width),
315                                 wl_fixed_from_int(fmt.i_visible_height),
316                                 place.width, place.height);
317             }
318             else
319                 vout_display_SendEventPicturesInvalid(vd);
320             break;
321         }
322         default:
323              msg_Err(vd, "unknown request %d", query);
324              return VLC_EGENERIC;
325     }
326     return VLC_SUCCESS;
327 }
328
329 static void shm_format_cb(void *data, struct wl_shm *shm, uint32_t format)
330 {
331     vout_display_t *vd = data;
332     char str[4];
333
334     memcpy(str, &format, sizeof (str));
335
336     if (format >= 0x20202020)
337         msg_Dbg(vd, "format %.4s (0x%08"PRIx32")", str, format);
338     else
339         msg_Dbg(vd, "format %4"PRIu32" (0x%08"PRIx32")", format, format);
340     (void) shm;
341 }
342
343 static const struct wl_shm_listener shm_cbs =
344 {
345     shm_format_cb,
346 };
347
348 static void registry_global_cb(void *data, struct wl_registry *registry,
349                                uint32_t name, const char *iface, uint32_t vers)
350 {
351     vout_display_t *vd = data;
352     vout_display_sys_t *sys = vd->sys;
353
354     msg_Dbg(vd, "global %3"PRIu32": %s version %"PRIu32, name, iface, vers);
355
356     if (!strcmp(iface, "wl_shm"))
357         sys->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
358     else
359     if (!strcmp(iface, "wl_scaler"))
360         sys->scaler = wl_registry_bind(registry, name, &wl_scaler_interface,
361                                        1);
362     else
363     if (!strcmp(iface, "wl_compositor"))
364         sys->use_buffer_transform = vers >= 2;
365 }
366
367 static void registry_global_remove_cb(void *data, struct wl_registry *registry,
368                                       uint32_t name)
369 {
370     vout_display_t *vd = data;
371
372     msg_Dbg(vd, "global remove %3"PRIu32, name);
373     (void) registry;
374 }
375
376 static const struct wl_registry_listener registry_cbs =
377 {
378     registry_global_cb,
379     registry_global_remove_cb,
380 };
381
382 static int Open(vlc_object_t *obj)
383 {
384     vout_display_t *vd = (vout_display_t *)obj;
385     vout_display_sys_t *sys = malloc(sizeof (*sys));
386     if (unlikely(sys == NULL))
387         return VLC_ENOMEM;
388
389     vd->sys = sys;
390     sys->embed = NULL;
391     sys->eventq = NULL;
392     sys->shm = NULL;
393     sys->scaler = NULL;
394     sys->pool = NULL;
395     sys->x = 0;
396     sys->y = 0;
397     sys->use_buffer_transform = false;
398
399     /* Get window */
400     sys->embed = vout_display_NewWindow(vd, VOUT_WINDOW_TYPE_WAYLAND);
401     if (sys->embed == NULL)
402         goto error;
403
404     struct wl_display *display = sys->embed->display.wl;
405
406     sys->eventq = wl_display_create_queue(display);
407     if (sys->eventq == NULL)
408         goto error;
409
410     struct wl_registry *registry = wl_display_get_registry(display);
411     if (registry == NULL)
412         goto error;
413
414     wl_proxy_set_queue((struct wl_proxy *)registry, sys->eventq);
415     wl_registry_add_listener(registry, &registry_cbs, vd);
416     wl_display_roundtrip_queue(display, sys->eventq);
417     wl_registry_destroy(registry);
418
419     if (sys->shm == NULL)
420         goto error;
421
422     wl_shm_add_listener(sys->shm, &shm_cbs, vd);
423     wl_display_roundtrip_queue(display, sys->eventq);
424
425     struct wl_surface *surface = sys->embed->handle.wl;
426     if (sys->scaler != NULL)
427         sys->viewport = wl_scaler_get_viewport(sys->scaler, surface);
428     else
429         sys->viewport = NULL;
430
431     /* Determine our pixel format */
432     static const enum wl_output_transform transforms[8] = {
433         [ORIENT_TOP_LEFT] = WL_OUTPUT_TRANSFORM_NORMAL,
434         [ORIENT_TOP_RIGHT] = WL_OUTPUT_TRANSFORM_FLIPPED,
435         [ORIENT_BOTTOM_LEFT] = WL_OUTPUT_TRANSFORM_FLIPPED_180,
436         [ORIENT_BOTTOM_RIGHT] = WL_OUTPUT_TRANSFORM_180,
437         [ORIENT_LEFT_TOP] = WL_OUTPUT_TRANSFORM_FLIPPED_270,
438         [ORIENT_LEFT_BOTTOM] = WL_OUTPUT_TRANSFORM_90,
439         [ORIENT_RIGHT_TOP] = WL_OUTPUT_TRANSFORM_270,
440         [ORIENT_RIGHT_BOTTOM] = WL_OUTPUT_TRANSFORM_FLIPPED_90,
441     };
442
443     if (sys->use_buffer_transform)
444     {
445         wl_surface_set_buffer_transform(surface,
446                                         transforms[vd->fmt.orientation]);
447     }
448     else
449     {
450         video_format_t fmt = vd->fmt;
451         video_format_ApplyRotation(&vd->fmt, &fmt);
452     }
453
454     vd->fmt.i_chroma = VLC_CODEC_RGB32;
455
456     vd->info.has_pictures_invalid = sys->viewport == NULL;
457     vd->info.has_event_thread = true;
458
459     vd->pool = Pool;
460     vd->prepare = Prepare;
461     vd->display = Display;
462     vd->control = Control;
463     vd->manage = NULL;
464
465     return VLC_SUCCESS;
466
467 error:
468     if (sys->eventq != NULL)
469         wl_event_queue_destroy(sys->eventq);
470     if (sys->embed != NULL)
471         vout_display_DeleteWindow(vd, sys->embed);
472     free(sys);
473     return VLC_EGENERIC;
474 }
475
476 static void Close(vlc_object_t *obj)
477 {
478     vout_display_t *vd = (vout_display_t *)obj;
479     vout_display_sys_t *sys = vd->sys;
480
481     ResetPictures(vd);
482
483     if (sys->viewport != NULL)
484         wl_viewport_destroy(sys->viewport);
485     if (sys->scaler != NULL)
486         wl_scaler_destroy(sys->scaler);
487     wl_shm_destroy(sys->shm);
488     wl_display_flush(sys->embed->display.wl);
489     wl_event_queue_destroy(sys->eventq);
490     vout_display_DeleteWindow(vd, sys->embed);
491     free(sys);
492 }
493
494 vlc_module_begin()
495     set_shortname(N_("WL SHM"))
496     set_description(N_("Wayland shared memory video output"))
497     set_category(CAT_VIDEO)
498     set_subcategory(SUBCAT_VIDEO_VOUT)
499     set_capability("vout display", 120)
500     set_callbacks(Open, Close)
501     add_shortcut("wl")
502 vlc_module_end()