]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/pictures.c
f4311d1e2586aec19dc15c5f363f4210aa053bb5
[vlc] / modules / video_output / xcb / pictures.c
1 /**
2  * @file pictures.c
3  * @brief Pictures management code for XCB video output plugins
4  */
5 /*****************************************************************************
6  * Copyright © 2009-2013 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 <stdlib.h>
28 #include <assert.h>
29
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SHM_H
32 # include <sys/shm.h>
33 # include <sys/stat.h>
34 #else
35 # define shmdt(mem) free(mem)
36 #endif
37
38 #include <xcb/xcb.h>
39 #include <xcb/shm.h>
40
41 #include <vlc_common.h>
42 #include <vlc_vout_display.h>
43
44 #include "pictures.h"
45 #include "events.h"
46
47 /** Check MIT-SHM shared memory support */
48 bool XCB_shm_Check (vlc_object_t *obj, xcb_connection_t *conn)
49 {
50 #ifdef HAVE_SYS_SHM_H
51     xcb_shm_query_version_cookie_t ck;
52     xcb_shm_query_version_reply_t *r;
53
54     ck = xcb_shm_query_version (conn);
55     r = xcb_shm_query_version_reply (conn, ck, NULL);
56     if (r != NULL)
57     {
58         free (r);
59         return true;
60     }
61     msg_Err (obj, "shared memory (MIT-SHM) not available");
62     msg_Warn (obj, "display will be slow");
63 #else
64     msg_Warn (obj, "shared memory (MIT-SHM) not implemented");
65     (void) conn;
66 #endif
67     return false;
68 }
69
70 /**
71  * Release picture private data: detach the shared memory segment.
72  */
73 static void XCB_picture_Destroy (picture_t *pic)
74 {
75     shmdt (pic->p[0].p_pixels);
76     free (pic);
77 }
78
79 /**
80  * Initialize a picture buffer as shared memory, according to the video output
81  * format. If a attach is true, the segment is attached to
82  * the X server (MIT-SHM extension).
83  */
84 int XCB_picture_Alloc (vout_display_t *vd, picture_resource_t *res,
85                        size_t size, xcb_connection_t *conn,
86                        xcb_shm_seg_t segment)
87 {
88 #ifdef HAVE_SYS_SHM_H
89     /* Allocate shared memory segment */
90     int id = shmget (IPC_PRIVATE, size, IPC_CREAT | S_IRWXU);
91     if (id == -1)
92     {
93         msg_Err (vd, "shared memory allocation error: %m");
94         return -1;
95     }
96
97     /* Attach the segment to VLC */
98     void *shm = shmat (id, NULL, 0 /* read/write */);
99     if (-1 == (intptr_t)shm)
100     {
101         msg_Err (vd, "shared memory attachment error: %m");
102         shmctl (id, IPC_RMID, 0);
103         return -1;
104     }
105
106     if (segment != 0)
107     {   /* Attach the segment to X */
108         xcb_void_cookie_t ck = xcb_shm_attach_checked (conn, segment, id, 1);
109         switch (XCB_error_Check (vd, conn, "shared memory server-side error",
110                                  ck))
111         {
112             case 0:
113                 break;
114
115             case XCB_ACCESS:
116             {
117                 struct shmid_ds buf;
118                 /* Retry with promiscuous permissions */
119                 shmctl (id, IPC_STAT, &buf);
120                 buf.shm_perm.mode |= S_IRGRP|S_IROTH;
121                 shmctl (id, IPC_SET, &buf);
122                 ck = xcb_shm_attach_checked (conn, segment, id, 1);
123                 if (XCB_error_Check (vd, conn, "same error on retry", ck) == 0)
124                     break;
125                 /* fall through */
126             }
127
128             default:
129                 msg_Info (vd, "using buggy X11 server - SSH proxying?");
130                 segment = 0;
131         }
132     }
133
134     shmctl (id, IPC_RMID, NULL);
135 #else
136     assert (segment == 0);
137
138     /* XXX: align on 32 bytes for VLC chroma filters */
139     void *shm = malloc (size);
140     if (unlikely(shm == NULL))
141         return -1;
142 #endif
143     res->p_sys = (void *)(uintptr_t)segment;
144     res->pf_destroy = XCB_picture_Destroy;
145     res->p[0].p_pixels = shm;
146     return 0;
147 }
148
149 picture_t *XCB_picture_NewFromResource (const video_format_t *restrict fmt,
150                                         const picture_resource_t *restrict res)
151 {
152     picture_t *pic = picture_NewFromResource (fmt, res);
153     if (unlikely(pic == NULL))
154         shmdt (res->p[0].p_pixels);
155     return pic;
156 }