File: instance.c

package info (click to toggle)
vlc 2.2.7-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 191,124 kB
  • sloc: ansic: 356,116; cpp: 94,295; objc: 34,063; sh: 6,765; makefile: 4,272; xml: 1,538; asm: 1,251; python: 240; perl: 77; sed: 16
file content (235 lines) | stat: -rw-r--r-- 5,945 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*****************************************************************************
 * instance.c: VDPAU instance management for VLC
 *****************************************************************************
 * Copyright (C) 2013 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <pthread.h>
#include <X11/Xlib.h>
#include <vlc_common.h>
#include "vlc_vdpau.h"

#pragma GCC visibility push(default)

typedef struct vdp_instance
{
    Display *display;
    vdp_t *vdp;
    VdpDevice device;

    int num; /**< X11 screen number */
    char *name; /**< X11 display name */

    uintptr_t refs; /**< Reference count */
    struct vdp_instance *next;
} vdp_instance_t;

static VdpStatus vdp_instance_create(const char *name, int num,
                                     vdp_instance_t **pp)
{
    size_t namelen = (name != NULL) ? (strlen(name) + 1) : 0;
    vdp_instance_t *vi = malloc(sizeof (*vi) + namelen);

    if (unlikely(vi == NULL))
        return VDP_STATUS_RESOURCES;

    vi->display = XOpenDisplay(name);
    if (vi->display == NULL)
    {
        free(vi);
        return VDP_STATUS_ERROR;
    }

    vi->next = NULL;
    if (name != NULL)
    {
        vi->name = (void *)(vi + 1);
        memcpy(vi->name, name, namelen);
    }
    else
        vi->name = NULL;
    if (num >= 0)
        vi->num = num;
    else
        vi->num = XDefaultScreen(vi->display);
    vi->refs = 1;

    VdpStatus err = vdp_create_x11(vi->display, vi->num,
                                   &vi->vdp, &vi->device);
    if (err != VDP_STATUS_OK)
    {
        XCloseDisplay(vi->display);
        free(vi);
        return err;
    }
    *pp = vi;
    return VDP_STATUS_OK;
}

static void vdp_instance_destroy(vdp_instance_t *vi)
{
    vdp_device_destroy(vi->vdp, vi->device);
    vdp_destroy_x11(vi->vdp);
    XCloseDisplay(vi->display);
    free(vi);
}

/** Compares two string pointers that might be NULL */
static int strnullcmp(const char *a, const char *b)
{
    if (b == NULL)
        return a != NULL;
    if (a == NULL)
        return -1;
    return strcmp(a, b);
}

static int vicmp(const char *name, int num, const vdp_instance_t *vi)
{
    int val = strnullcmp(name, vi->name);
    if (val)
        return val;

    if (num < 0)
        num = XDefaultScreen(vi->display);
    return num - vi->num;
}

static vdp_instance_t *list = NULL;

static vdp_instance_t *vdp_instance_lookup(const char *name, int num)
{
    vdp_instance_t *vi = NULL;

    for (vi = list; vi != NULL; vi = vi->next)
    {
        int val = vicmp(name, num, vi);
        if (val == 0)
        {
            assert(vi->refs < UINTPTR_MAX);
            vi->refs++;
            break;
        }
    }
    return vi;
}

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

/** Finds an existing VDPAU instance for the given X11 display and screen.
 * If not found, (try to) create a new one.
 * @param display_name X11 display string, NULL for default
 * @param snum X11 screen number, strictly negative for default
 **/
VdpStatus vdp_get_x11(const char *display_name, int snum,
                      vdp_t **restrict vdpp, VdpDevice *restrict devicep)
{
    vdp_instance_t *vi, *vi2;
    VdpStatus err = VDP_STATUS_RESOURCES;

    if (display_name == NULL)
    {
        display_name = getenv("DISPLAY");
        if (display_name == NULL)
            return VDP_STATUS_ERROR;
    }

    pthread_mutex_lock(&lock);
    vi = vdp_instance_lookup(display_name, snum);
    pthread_mutex_unlock(&lock);
    if (vi != NULL)
        goto found;

    err = vdp_instance_create(display_name, snum, &vi);
    if (err != VDP_STATUS_OK)
        return err;

    pthread_mutex_lock(&lock);
    vi2 = vdp_instance_lookup(display_name, snum);
    if (unlikely(vi2 != NULL))
    {   /* Another thread created the instance (race condition corner case) */
        pthread_mutex_unlock(&lock);
        vdp_instance_destroy(vi);
        vi = vi2;
    }
    else
    {
        vi->next = list;
        list = vi;
        pthread_mutex_unlock(&lock);
    }
found:
    *vdpp = vi->vdp;
    *devicep = vi->device;
    return VDP_STATUS_OK;
}

vdp_t *vdp_hold_x11(vdp_t *vdp, VdpDevice *restrict devp)
{
    vdp_instance_t *vi, **pp = &list;

    pthread_mutex_lock(&lock);
    for (;;)
    {
        vi = *pp;
        assert(vi != NULL);
        if (vi->vdp == vdp)
            break;
        pp = &vi->next;
    }

    assert(vi->refs < UINTPTR_MAX);
    vi->refs++;
    pthread_mutex_unlock(&lock);

    if (devp != NULL)
        *devp = vi->device;
   return vdp;
}

void vdp_release_x11(vdp_t *vdp)
{
    vdp_instance_t *vi, **pp = &list;

    pthread_mutex_lock(&lock);
    for (;;)
    {
        vi = *pp;
        assert(vi != NULL);
        if (vi->vdp == vdp)
            break;
        pp = &vi->next;
    }

    assert(vi->refs > 0);
    vi->refs--;
    if (vi->refs > 0)
        vi = NULL; /* Keep the instance for now */
    else
        *pp = vi->next; /* Unlink the instance */
    pthread_mutex_unlock(&lock);

    if (vi != NULL)
        vdp_instance_destroy(vi);
}