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 *****************************************************************************/
31 #include <sys/types.h>
34 # include <sys/stat.h>
36 # define shmdt(mem) free(mem)
42 #include <vlc_common.h>
43 #include <vlc_vout_display.h>
48 /** Check MIT-SHM shared memory support */
49 bool XCB_shm_Check (vlc_object_t *obj, xcb_connection_t *conn)
52 xcb_shm_query_version_cookie_t ck;
53 xcb_shm_query_version_reply_t *r;
55 ck = xcb_shm_query_version (conn);
56 r = xcb_shm_query_version_reply (conn, ck, NULL);
62 msg_Err (obj, "shared memory (MIT-SHM) not available");
63 msg_Warn (obj, "display will be slow");
65 msg_Warn (obj, "shared memory (MIT-SHM) not implemented");
72 * Release picture private data: detach the shared memory segment.
74 static void XCB_picture_Destroy (picture_t *pic)
76 shmdt (pic->p[0].p_pixels);
81 * Initialize a picture buffer as shared memory, according to the video output
82 * format. If a attach is true, the segment is attached to
83 * the X server (MIT-SHM extension).
85 int XCB_picture_Alloc (vout_display_t *vd, picture_resource_t *res,
86 size_t size, xcb_connection_t *conn,
87 xcb_shm_seg_t segment)
90 /* Allocate shared memory segment */
91 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | S_IRWXU);
94 msg_Err (vd, "shared memory allocation error: %s",
95 vlc_strerror_c(errno));
99 /* Attach the segment to VLC */
100 void *shm = shmat (id, NULL, 0 /* read/write */);
101 if (-1 == (intptr_t)shm)
103 msg_Err (vd, "shared memory attachment error: %s",
104 vlc_strerror_c(errno));
105 shmctl (id, IPC_RMID, 0);
110 { /* Attach the segment to X */
111 xcb_void_cookie_t ck = xcb_shm_attach_checked (conn, segment, id, 1);
112 switch (XCB_error_Check (vd, conn, "shared memory server-side error",
121 /* Retry with promiscuous permissions */
122 shmctl (id, IPC_STAT, &buf);
123 buf.shm_perm.mode |= S_IRGRP|S_IROTH;
124 shmctl (id, IPC_SET, &buf);
125 ck = xcb_shm_attach_checked (conn, segment, id, 1);
126 if (XCB_error_Check (vd, conn, "same error on retry", ck) == 0)
132 msg_Info (vd, "using buggy X11 server - SSH proxying?");
137 shmctl (id, IPC_RMID, NULL);
139 assert (segment == 0);
141 /* XXX: align on 32 bytes for VLC chroma filters */
142 void *shm = malloc (size);
143 if (unlikely(shm == NULL))
146 res->p_sys = (void *)(uintptr_t)segment;
147 res->pf_destroy = XCB_picture_Destroy;
148 res->p[0].p_pixels = shm;
152 picture_t *XCB_picture_NewFromResource (const video_format_t *restrict fmt,
153 const picture_resource_t *restrict res)
155 picture_t *pic = picture_NewFromResource (fmt, res);
156 if (unlikely(pic == NULL))
157 shmdt (res->p[0].p_pixels);