1 /*****************************************************************************
2 * instance.c: VDPAU instance management for VLC
3 *****************************************************************************
4 * Copyright (C) 2013 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
30 #include <vlc_common.h>
31 #include "vlc_vdpau.h"
33 #pragma GCC visibility push(default)
35 typedef struct vdp_instance
41 int num; /**< X11 screen number */
42 char *name; /**< X11 display name */
44 uintptr_t refs; /**< Reference count */
45 struct vdp_instance *next;
48 static VdpStatus vdp_instance_create(const char *name, int num,
51 size_t namelen = (name != NULL) ? (strlen(name) + 1) : 0;
52 vdp_instance_t *vi = malloc(sizeof (*vi) + namelen);
54 if (unlikely(vi == NULL))
55 return VDP_STATUS_RESOURCES;
57 vi->display = XOpenDisplay(name);
58 if (vi->display == NULL)
61 return VDP_STATUS_ERROR;
67 vi->name = (void *)(vi + 1);
68 memcpy(vi->name, name, namelen);
75 vi->num = XDefaultScreen(vi->display);
78 VdpStatus err = vdp_create_x11(vi->display, vi->num,
79 &vi->vdp, &vi->device);
80 if (err != VDP_STATUS_OK)
82 XCloseDisplay(vi->display);
90 static void vdp_instance_destroy(vdp_instance_t *vi)
92 vdp_device_destroy(vi->vdp, vi->device);
93 vdp_destroy_x11(vi->vdp);
94 XCloseDisplay(vi->display);
98 /** Compares two string pointers that might be NULL */
99 static int strnullcmp(const char *a, const char *b)
108 static int vicmp(const char *name, int num, const vdp_instance_t *vi)
110 int val = strnullcmp(name, vi->name);
115 num = XDefaultScreen(vi->display);
116 return num - vi->num;
119 static vdp_instance_t *list = NULL;
121 static vdp_instance_t *vdp_instance_lookup(const char *name, int num)
123 vdp_instance_t *vi = NULL;
125 for (vi = list; vi != NULL; vi = vi->next)
127 int val = vicmp(name, num, vi);
130 assert(vi->refs < UINTPTR_MAX);
138 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
140 /** Finds an existing VDPAU instance for the given X11 display and screen.
141 * If not found, (try to) create a new one.
142 * @param display_name X11 display string, NULL for default
143 * @param snum X11 screen number, strictly negative for default
145 VdpStatus vdp_get_x11(const char *display_name, int snum,
146 vdp_t **restrict vdpp, VdpDevice *restrict devicep)
148 vdp_instance_t *vi, *vi2;
149 VdpStatus err = VDP_STATUS_RESOURCES;
151 if (display_name == NULL)
153 display_name = getenv("DISPLAY");
154 if (display_name == NULL)
155 return VDP_STATUS_ERROR;
158 pthread_mutex_lock(&lock);
159 vi = vdp_instance_lookup(display_name, snum);
160 pthread_mutex_unlock(&lock);
164 err = vdp_instance_create(display_name, snum, &vi);
165 if (err != VDP_STATUS_OK)
168 pthread_mutex_lock(&lock);
169 vi2 = vdp_instance_lookup(display_name, snum);
170 if (unlikely(vi2 != NULL))
171 { /* Another thread created the instance (race condition corner case) */
172 pthread_mutex_unlock(&lock);
173 vdp_instance_destroy(vi);
180 pthread_mutex_unlock(&lock);
184 *devicep = vi->device;
185 return VDP_STATUS_OK;
188 vdp_t *vdp_hold_x11(vdp_t *vdp, VdpDevice *restrict devp)
190 vdp_instance_t *vi, **pp = &list;
192 pthread_mutex_lock(&lock);
202 assert(vi->refs < UINTPTR_MAX);
204 pthread_mutex_unlock(&lock);
211 void vdp_release_x11(vdp_t *vdp)
213 vdp_instance_t *vi, **pp = &list;
215 pthread_mutex_lock(&lock);
225 assert(vi->refs > 0);
228 vi = NULL; /* Keep the instance for now */
230 *pp = vi->next; /* Unlink the instance */
231 pthread_mutex_unlock(&lock);
234 vdp_instance_destroy(vi);