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>
35 # define shmdt(mem) free(mem)
41 #include <vlc_common.h>
42 #include <vlc_vout_display.h>
47 /** Check MIT-SHM shared memory support */
48 bool XCB_shm_Check (vlc_object_t *obj, xcb_connection_t *conn)
51 xcb_shm_query_version_cookie_t ck;
52 xcb_shm_query_version_reply_t *r;
54 ck = xcb_shm_query_version (conn);
55 r = xcb_shm_query_version_reply (conn, ck, NULL);
61 msg_Err (obj, "shared memory (MIT-SHM) not available");
62 msg_Warn (obj, "display will be slow");
64 msg_Warn (obj, "shared memory (MIT-SHM) not implemented");
71 * Release picture private data: detach the shared memory segment.
73 static void XCB_picture_Destroy (picture_t *pic)
75 shmdt (pic->p[0].p_pixels);
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).
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)
89 /* Allocate shared memory segment */
90 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | S_IRWXU);
93 msg_Err (vd, "shared memory allocation error: %m");
97 /* Attach the segment to VLC */
98 void *shm = shmat (id, NULL, 0 /* read/write */);
99 if (-1 == (intptr_t)shm)
101 msg_Err (vd, "shared memory attachment error: %m");
102 shmctl (id, IPC_RMID, 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",
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?");
134 shmctl (id, IPC_RMID, NULL);
136 assert (segment == 0);
138 /* XXX: align on 32 bytes for VLC chroma filters */
139 void *shm = malloc (size);
140 if (unlikely(shm == NULL))
143 res->p_sys = (void *)(uintptr_t)segment;
144 res->pf_destroy = XCB_picture_Destroy;
145 res->p[0].p_pixels = shm;
149 picture_t *XCB_picture_NewFromResource (const video_format_t *restrict fmt,
150 const picture_resource_t *restrict res)
152 picture_t *pic = picture_NewFromResource (fmt, res);
153 if (unlikely(pic == NULL))
154 shmdt (res->p[0].p_pixels);