3 * @brief Pictures management code for XCB video output plugins
5 /*****************************************************************************
6 * Copyright © 2009-2013 Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
30 #include <sys/types.h>
33 # include <sys/stat.h>
39 #include <vlc_common.h>
40 #include <vlc_vout_display.h>
45 /** Check MIT-SHM shared memory support */
46 bool XCB_shm_Check (vlc_object_t *obj, xcb_connection_t *conn)
49 xcb_shm_query_version_cookie_t ck;
50 xcb_shm_query_version_reply_t *r;
52 ck = xcb_shm_query_version (conn);
53 r = xcb_shm_query_version_reply (conn, ck, NULL);
59 msg_Err (obj, "shared memory (MIT-SHM) not available");
60 msg_Warn (obj, "display will be slow");
62 msg_Warn (obj, "shared memory (MIT-SHM) not implemented");
69 * Initialize a picture buffer as shared memory, according to the video output
70 * format. If a attach is true, the segment is attached to
71 * the X server (MIT-SHM extension).
73 int XCB_pictures_Alloc (vout_display_t *vd, picture_resource_t *res,
74 size_t size, xcb_connection_t *conn, bool attach)
76 res->p_sys = malloc (sizeof(*res->p_sys));
81 /* Allocate shared memory segment */
82 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | S_IRWXU);
85 msg_Err (vd, "shared memory allocation error: %m");
90 /* Attach the segment to VLC */
91 void *shm = shmat (id, NULL, 0 /* read/write */);
92 if (-1 == (intptr_t)shm)
94 msg_Err (vd, "shared memory attachment error: %m");
95 shmctl (id, IPC_RMID, 0);
100 xcb_shm_seg_t segment;
103 /* Attach the segment to X */
104 xcb_void_cookie_t ck;
106 segment = xcb_generate_id (conn);
107 ck = xcb_shm_attach_checked (conn, segment, id, 1);
109 switch (XCB_error_Check (vd, conn, "shared memory server-side error",
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)
129 msg_Info (vd, "using buggy X11 server - SSH proxying?");
136 shmctl (id, IPC_RMID, NULL);
137 res->p_sys->segment = segment;
138 res->p->p_pixels = shm;
141 res->p_sys->segment = 0;
143 /* XXX: align on 32 bytes for VLC chroma filters */
144 res->p->p_pixels = malloc (size);
145 if (unlikely(res->p->p_pixels == NULL))
155 * Release picture private data: detach the shared memory segment.
157 void XCB_pictures_Free (picture_resource_t *res, xcb_connection_t *conn)
159 #ifdef HAVE_SYS_SHM_H
160 xcb_shm_seg_t segment = res->p_sys->segment;
162 if (conn != NULL && segment != 0)
163 xcb_shm_detach (conn, segment);
164 shmdt (res->p->p_pixels);
166 free (res->p->p_pixels);