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>
44 /** Check MIT-SHM shared memory support */
45 bool XCB_shm_Check (vlc_object_t *obj, xcb_connection_t *conn)
48 xcb_shm_query_version_cookie_t ck;
49 xcb_shm_query_version_reply_t *r;
51 ck = xcb_shm_query_version (conn);
52 r = xcb_shm_query_version_reply (conn, ck, NULL);
58 msg_Err (obj, "shared memory (MIT-SHM) not available");
59 msg_Warn (obj, "display will be slow");
61 msg_Warn (obj, "shared memory (MIT-SHM) not implemented");
68 * Initialize a picture buffer as shared memory, according to the video output
69 * format. If a attach is true, the segment is attached to
70 * the X server (MIT-SHM extension).
72 int XCB_pictures_Alloc (vout_display_t *vd, picture_resource_t *res,
73 size_t size, xcb_connection_t *conn, bool attach)
75 res->p_sys = malloc (sizeof(*res->p_sys));
80 /* Allocate shared memory segment */
81 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | S_IRWXU);
84 msg_Err (vd, "shared memory allocation error: %m");
89 /* Attach the segment to VLC */
90 void *shm = shmat (id, NULL, 0 /* read/write */);
91 if (-1 == (intptr_t)shm)
93 msg_Err (vd, "shared memory attachment error: %m");
94 shmctl (id, IPC_RMID, 0);
99 xcb_shm_seg_t segment;
102 /* Attach the segment to X */
103 xcb_void_cookie_t ck;
105 segment = xcb_generate_id (conn);
106 ck = xcb_shm_attach_checked (conn, segment, id, 1);
108 switch (XCB_error_Check (vd, conn, "shared memory server-side error",
117 /* Retry with promiscuous permissions */
118 shmctl (id, IPC_STAT, &buf);
119 buf.shm_perm.mode |= S_IRGRP|S_IROTH;
120 shmctl (id, IPC_SET, &buf);
121 ck = xcb_shm_attach_checked (conn, segment, id, 1);
122 if (XCB_error_Check (vd, conn, "same error on retry", ck) == 0)
128 msg_Info (vd, "using buggy X11 server - SSH proxying?");
135 shmctl (id, IPC_RMID, NULL);
136 res->p_sys->segment = segment;
137 res->p->p_pixels = shm;
140 res->p_sys->segment = 0;
142 /* XXX: align on 32 bytes for VLC chroma filters */
143 res->p->p_pixels = malloc (size);
144 if (unlikely(res->p->p_pixels == NULL))
154 * Release picture private data: detach the shared memory segment.
156 void XCB_pictures_Free (picture_resource_t *res, xcb_connection_t *conn)
158 #ifdef HAVE_SYS_SHM_H
159 xcb_shm_seg_t segment = res->p_sys->segment;
161 if (conn != NULL && segment != 0)
162 xcb_shm_detach (conn, segment);
163 shmdt (res->p->p_pixels);
165 free (res->p->p_pixels);