vdpau_wrapper.c 6.58 KB
Newer Older
Aaron Plattner's avatar
Aaron Plattner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Copyright (c) 2008-2009 NVIDIA, Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

24
25
26
27
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Aaron Plattner's avatar
Aaron Plattner committed
28
#include <dlfcn.h>
29
#include <limits.h>
Aaron Plattner's avatar
Aaron Plattner committed
30
31
32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Aaron Plattner's avatar
Aaron Plattner committed
33
#include <vdpau/vdpau_x11.h>
34
35
36
37
#if DRI2
#include "mesa_dri2.h"
#include <X11/Xlib.h>
#endif
Aaron Plattner's avatar
Aaron Plattner committed
38
39
40
41
42

typedef void SetDllHandle(
    void * driver_dll_handle
);

43
44
45
46
47
48
49
50
51
52
53
static void * _vdp_backend_dll;
static void * _vdp_trace_dll;
static void * _vdp_driver_dll;
static VdpDeviceCreateX11 * _vdp_imp_device_create_x11_proc;

#if defined(__GNUC__)

static void _vdp_close_driver(void) __attribute__((destructor));

#endif

Aaron Plattner's avatar
Aaron Plattner committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#if DEBUG

static void _vdp_wrapper_error_breakpoint(char const * file, int line, char const * function)
{
    fprintf(stderr, "VDPAU wrapper: Error detected at %s:%d %s()\n", file, line, function);
}

#define _VDP_ERROR_BREAKPOINT() _vdp_wrapper_error_breakpoint(__FILE__, __LINE__, __FUNCTION__)

#else

#define _VDP_ERROR_BREAKPOINT()

#endif

69
#define DRIVER_LIB_FORMAT "%slibvdpau_%s.so%s"
Aaron Plattner's avatar
Aaron Plattner committed
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
static char * _vdp_get_driver_name_from_dri2(
    Display *             display,
    int                   screen
)
{
    char * driver_name = NULL;
#if DRI2
    Window root = RootWindow(display, screen);
    int event_base, error_base;
    int major, minor;
    char * device_name;

    if (!_vdp_DRI2QueryExtension(display, &event_base, &error_base)) {
        return NULL;
    }

    if (!_vdp_DRI2QueryVersion(display, &major, &minor) ||
            (major < 1 || (major == 1 && minor < 2))) {
        return NULL;
    }

    if (!_vdp_DRI2Connect(display, root, &driver_name, &device_name)) {
        return NULL;
    }

    XFree(device_name);
#endif /* DRI2 */
    return driver_name;
}

101
static VdpStatus _vdp_open_driver(
Aaron Plattner's avatar
Aaron Plattner committed
102
    Display *             display,
103
    int                   screen)
Aaron Plattner's avatar
Aaron Plattner committed
104
105
{
    char const * vdpau_driver;
106
    char * vdpau_driver_dri2 = NULL;
107
    char         vdpau_driver_lib[PATH_MAX];
Aaron Plattner's avatar
Aaron Plattner committed
108
109
110
111
    char const * vdpau_trace;
    char const * func_name;

    vdpau_driver = getenv("VDPAU_DRIVER");
112
113
114
115
    if (!vdpau_driver) {
        vdpau_driver = vdpau_driver_dri2 =
            _vdp_get_driver_name_from_dri2(display, screen);
    }
Aaron Plattner's avatar
Aaron Plattner committed
116
117
118
119
    if (!vdpau_driver) {
        vdpau_driver = "nvidia";
    }

120
121
122
123
    if (snprintf(vdpau_driver_lib, sizeof(vdpau_driver_lib), DRIVER_LIB_FORMAT,
                 VDPAU_MODULEDIR "/", vdpau_driver, ".1") >=
            sizeof(vdpau_driver_lib)) {
        fprintf(stderr, "Failed to construct driver path: path too long\n");
124
125
126
127
        if (vdpau_driver_dri2) {
            XFree(vdpau_driver_dri2);
            vdpau_driver_dri2 = NULL;
        }
Aaron Plattner's avatar
Aaron Plattner committed
128
        _VDP_ERROR_BREAKPOINT();
129
        return VDP_STATUS_NO_IMPLEMENTATION;
Aaron Plattner's avatar
Aaron Plattner committed
130
131
    }

132
133
    _vdp_driver_dll = dlopen(vdpau_driver_lib, RTLD_NOW | RTLD_GLOBAL);
    if (!_vdp_driver_dll) {
134
135
136
137
        /* Try again using the old path, which is guaranteed to fit in PATH_MAX
         * if the complete path fit above. */
        snprintf(vdpau_driver_lib, sizeof(vdpau_driver_lib), DRIVER_LIB_FORMAT,
                 "", vdpau_driver, "");
138
        _vdp_driver_dll = dlopen(vdpau_driver_lib, RTLD_NOW | RTLD_GLOBAL);
139
140
    }

141
142
143
144
145
    if (vdpau_driver_dri2) {
        XFree(vdpau_driver_dri2);
        vdpau_driver_dri2 = NULL;
    }

146
    if (!_vdp_driver_dll) {
147
        fprintf(stderr, "Failed to open VDPAU backend %s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
148
149
150
151
        _VDP_ERROR_BREAKPOINT();
        return VDP_STATUS_NO_IMPLEMENTATION;
    }

152
153
    _vdp_backend_dll = _vdp_driver_dll;

Aaron Plattner's avatar
Aaron Plattner committed
154
155
156
157
    vdpau_trace = getenv("VDPAU_TRACE");
    if (vdpau_trace && atoi(vdpau_trace)) {
        SetDllHandle * set_dll_handle;

158
159
160
        _vdp_trace_dll = dlopen(VDPAU_MODULEDIR "/libvdpau_trace.so.1",
                                RTLD_NOW | RTLD_GLOBAL);
        if (!_vdp_trace_dll) {
161
            fprintf(stderr, "Failed to open VDPAU trace library %s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
162
163
164
165
166
            _VDP_ERROR_BREAKPOINT();
            return VDP_STATUS_NO_IMPLEMENTATION;
        }

        set_dll_handle = (SetDllHandle*)dlsym(
167
            _vdp_trace_dll,
Aaron Plattner's avatar
Aaron Plattner committed
168
169
170
            "vdp_trace_set_backend_handle"
        );
        if (!set_dll_handle) {
171
            fprintf(stderr, "%s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
172
173
174
175
            _VDP_ERROR_BREAKPOINT();
            return VDP_STATUS_NO_IMPLEMENTATION;
        }

176
        set_dll_handle(_vdp_backend_dll);
Aaron Plattner's avatar
Aaron Plattner committed
177

178
        _vdp_backend_dll = _vdp_trace_dll;
Aaron Plattner's avatar
Aaron Plattner committed
179
180
181
182
183
184
185

        func_name = "vdp_trace_device_create_x11";
    }
    else {
        func_name = "vdp_imp_device_create_x11";
    }

186
187
    _vdp_imp_device_create_x11_proc = (VdpDeviceCreateX11*)dlsym(
        _vdp_backend_dll,
Aaron Plattner's avatar
Aaron Plattner committed
188
189
        func_name
    );
190
    if (!_vdp_imp_device_create_x11_proc) {
191
        fprintf(stderr, "%s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
192
193
194
195
        _VDP_ERROR_BREAKPOINT();
        return VDP_STATUS_NO_IMPLEMENTATION;
    }

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
    return VDP_STATUS_OK;
}

static void _vdp_close_driver(void)
{
    if (_vdp_driver_dll) {
        dlclose(_vdp_driver_dll);
        _vdp_driver_dll = NULL;
    }
    if (_vdp_trace_dll) {
        dlclose(_vdp_trace_dll);
        _vdp_trace_dll = NULL;
    }
    _vdp_backend_dll = NULL;
    _vdp_imp_device_create_x11_proc = NULL;
}

VdpStatus vdp_device_create_x11(
    Display *             display,
    int                   screen,
    /* output parameters follow */
    VdpDevice *           device,
    VdpGetProcAddress * * get_proc_address
)
{
    VdpStatus status;

    if (!_vdp_imp_device_create_x11_proc) {
        status = _vdp_open_driver(display, screen);
        if (status != VDP_STATUS_OK) {
            _vdp_close_driver();
            return status;
        }
    }

    return _vdp_imp_device_create_x11_proc(
Aaron Plattner's avatar
Aaron Plattner committed
232
233
234
235
236
237
        display,
        screen,
        device,
        get_proc_address
    );
}