vdpau_wrapper.c 11.3 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>
Emil Velikov's avatar
Emil Velikov committed
33
#include <unistd.h>
Aaron Plattner's avatar
Aaron Plattner committed
34
#include <vdpau/vdpau_x11.h>
35
36
37
38
#if DRI2
#include "mesa_dri2.h"
#include <X11/Xlib.h>
#endif
Aaron Plattner's avatar
Aaron Plattner committed
39
40
41
42
43

typedef void SetDllHandle(
    void * driver_dll_handle
);

44
45
46
47
48
49
50
51
52
53
54
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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

Emil Velikov's avatar
Emil Velikov committed
70
71
#define DRIVER_FALLBACK_LIB_FORMAT "libvdpau_%s.so"
#define DRIVER_LIB_FORMAT "%s/libvdpau_%s.so.1"
Aaron Plattner's avatar
Aaron Plattner committed
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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))) {
91
        _vdp_DRI2RemoveExtension(display);
92
93
94
95
        return NULL;
    }

    if (!_vdp_DRI2Connect(display, root, &driver_name, &device_name)) {
96
        _vdp_DRI2RemoveExtension(display);
97
98
99
100
        return NULL;
    }

    XFree(device_name);
101
    _vdp_DRI2RemoveExtension(display);
102
103
104
105
#endif /* DRI2 */
    return driver_name;
}

106
static VdpStatus _vdp_open_driver(
Aaron Plattner's avatar
Aaron Plattner committed
107
    Display *             display,
108
    int                   screen)
Aaron Plattner's avatar
Aaron Plattner committed
109
110
{
    char const * vdpau_driver;
111
    char * vdpau_driver_dri2 = NULL;
Emil Velikov's avatar
Emil Velikov committed
112
    const char * vdpau_driver_path = NULL;
113
    char         vdpau_driver_lib[PATH_MAX];
Aaron Plattner's avatar
Aaron Plattner committed
114
115
116
117
    char const * vdpau_trace;
    char const * func_name;

    vdpau_driver = getenv("VDPAU_DRIVER");
118
119
120
121
    if (!vdpau_driver) {
        vdpau_driver = vdpau_driver_dri2 =
            _vdp_get_driver_name_from_dri2(display, screen);
    }
Aaron Plattner's avatar
Aaron Plattner committed
122
123
124
125
    if (!vdpau_driver) {
        vdpau_driver = "nvidia";
    }

Emil Velikov's avatar
Emil Velikov committed
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
    if (geteuid() == getuid()) {
        /* don't allow setuid apps to use VDPAU_DRIVER_PATH */
        vdpau_driver_path = getenv("VDPAU_DRIVER_PATH");
        if (vdpau_driver_path &&
            snprintf(vdpau_driver_lib, sizeof(vdpau_driver_lib),
                     DRIVER_LIB_FORMAT, vdpau_driver_path, vdpau_driver) <
                sizeof(vdpau_driver_lib)) {
            _vdp_driver_dll = dlopen(vdpau_driver_lib, RTLD_NOW | RTLD_GLOBAL);
        }
    }

    /* Fallback to VDPAU_MODULEDIR when VDPAU_DRIVER_PATH is not set,
     * or if we fail to create the driver path/dlopen the library. */
    if (!_vdp_driver_dll) {
        if (snprintf(vdpau_driver_lib, sizeof(vdpau_driver_lib),
                     DRIVER_LIB_FORMAT, VDPAU_MODULEDIR, vdpau_driver) >=
                sizeof(vdpau_driver_lib)) {
            fprintf(stderr, "Failed to construct driver path: path too long\n");
            if (vdpau_driver_dri2) {
                XFree(vdpau_driver_dri2);
                vdpau_driver_dri2 = NULL;
            }
            _VDP_ERROR_BREAKPOINT();
            return VDP_STATUS_NO_IMPLEMENTATION;
        }
        else {
            _vdp_driver_dll = dlopen(vdpau_driver_lib, RTLD_NOW | RTLD_GLOBAL);
153
        }
Aaron Plattner's avatar
Aaron Plattner committed
154
155
    }

156
    if (!_vdp_driver_dll) {
157
158
        /* Try again using the old path, which is guaranteed to fit in PATH_MAX
         * if the complete path fit above. */
Emil Velikov's avatar
Emil Velikov committed
159
160
        snprintf(vdpau_driver_lib, sizeof(vdpau_driver_lib),
                 DRIVER_FALLBACK_LIB_FORMAT, vdpau_driver);
161
        _vdp_driver_dll = dlopen(vdpau_driver_lib, RTLD_NOW | RTLD_GLOBAL);
162
163
    }

164
165
166
167
168
    if (vdpau_driver_dri2) {
        XFree(vdpau_driver_dri2);
        vdpau_driver_dri2 = NULL;
    }

169
    if (!_vdp_driver_dll) {
170
        fprintf(stderr, "Failed to open VDPAU backend %s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
171
172
173
174
        _VDP_ERROR_BREAKPOINT();
        return VDP_STATUS_NO_IMPLEMENTATION;
    }

175
176
    _vdp_backend_dll = _vdp_driver_dll;

Aaron Plattner's avatar
Aaron Plattner committed
177
178
179
180
    vdpau_trace = getenv("VDPAU_TRACE");
    if (vdpau_trace && atoi(vdpau_trace)) {
        SetDllHandle * set_dll_handle;

181
182
183
        _vdp_trace_dll = dlopen(VDPAU_MODULEDIR "/libvdpau_trace.so.1",
                                RTLD_NOW | RTLD_GLOBAL);
        if (!_vdp_trace_dll) {
184
            fprintf(stderr, "Failed to open VDPAU trace library %s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
185
186
187
188
189
            _VDP_ERROR_BREAKPOINT();
            return VDP_STATUS_NO_IMPLEMENTATION;
        }

        set_dll_handle = (SetDllHandle*)dlsym(
190
            _vdp_trace_dll,
Aaron Plattner's avatar
Aaron Plattner committed
191
192
193
            "vdp_trace_set_backend_handle"
        );
        if (!set_dll_handle) {
194
            fprintf(stderr, "%s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
195
196
197
198
            _VDP_ERROR_BREAKPOINT();
            return VDP_STATUS_NO_IMPLEMENTATION;
        }

199
        set_dll_handle(_vdp_backend_dll);
Aaron Plattner's avatar
Aaron Plattner committed
200

201
        _vdp_backend_dll = _vdp_trace_dll;
Aaron Plattner's avatar
Aaron Plattner committed
202
203
204
205
206
207
208

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

209
210
    _vdp_imp_device_create_x11_proc = (VdpDeviceCreateX11*)dlsym(
        _vdp_backend_dll,
Aaron Plattner's avatar
Aaron Plattner committed
211
212
        func_name
    );
213
    if (!_vdp_imp_device_create_x11_proc) {
214
        fprintf(stderr, "%s\n", dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
215
216
217
218
        _VDP_ERROR_BREAKPOINT();
        return VDP_STATUS_NO_IMPLEMENTATION;
    }

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    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;
}

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
static VdpGetProcAddress * _imp_get_proc_address;
static VdpVideoSurfacePutBitsYCbCr * _imp_vid_put_bits_y_cb_cr;
static VdpPresentationQueueSetBackgroundColor * _imp_pq_set_bg_color;
static int _inited_fixes;
static int _running_under_flash;
static int _enable_flash_uv_swap = 1;
static int _disable_flash_pq_bg_color = 1;

static VdpStatus vid_put_bits_y_cb_cr_swapped(
    VdpVideoSurface      surface,
    VdpYCbCrFormat       source_ycbcr_format,
    void const * const * source_data,
    uint32_t const *     source_pitches
)
{
    void const * data_reordered[3];
    void const * const * data;

    if (source_ycbcr_format == VDP_YCBCR_FORMAT_YV12) {
        data_reordered[0] = source_data[0];
        data_reordered[1] = source_data[2];
        data_reordered[2] = source_data[1];
        /*
         * source_pitches[1] and source_pitches[2] should be equal,
         * so no need to re-order.
         */
        data = data_reordered;
    }
    else {
        data = source_data;
    }

    return _imp_vid_put_bits_y_cb_cr(
        surface,
        source_ycbcr_format,
        data,
        source_pitches
    );
}

static VdpStatus pq_set_bg_color_noop(
    VdpPresentationQueue presentation_queue,
    VdpColor * const     background_color
)
{
    return VDP_STATUS_OK;
}

static VdpStatus vdp_wrapper_get_proc_address(
    VdpDevice device,
    VdpFuncId function_id,
    /* output parameters follow */
    void * *  function_pointer
)
{
    VdpStatus status;

    status = _imp_get_proc_address(device, function_id, function_pointer);
    if (status != VDP_STATUS_OK) {
        return status;
    }

    if (_running_under_flash) {
        switch (function_id) {
        case VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR:
            if (_enable_flash_uv_swap) {
                _imp_vid_put_bits_y_cb_cr = *function_pointer;
                *function_pointer = vid_put_bits_y_cb_cr_swapped;
            }
            break;
        case VDP_FUNC_ID_PRESENTATION_QUEUE_SET_BACKGROUND_COLOR:
            if (_disable_flash_pq_bg_color) {
                _imp_pq_set_bg_color = *function_pointer;
                *function_pointer = pq_set_bg_color_noop;
            }
            break;
        default:
            break;
        }
    }

    return VDP_STATUS_OK;
}

static void init_running_under_flash(void)
{
    FILE *fp;
    char buffer[1024];
    int ret, i;

    fp = fopen("/proc/self/cmdline", "r");
    if (!fp) {
        return;
    }
    ret = fread(buffer, 1, sizeof(buffer) - 1, fp);
    fclose(fp);
    if (ret < 0) {
        return;
    }
    /*
     * Sometimes the file contains null between arguments. Wipe these out so
     * strstr doesn't stop early.
     */
    for (i = 0; i < ret; i++) {
        if (buffer[i] == '\0') {
            buffer[i] = 'x';
        }
    }
    buffer[ret] = '\0';

    if (strstr(buffer, "libflashplayer") != NULL) {
        _running_under_flash = 1;
    }
}

351
static void init_config(void)
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{
    FILE *fp;
    char buffer[1024];

    fp = fopen(VDPAU_SYSCONFDIR "/vdpau_wrapper.cfg", "r");
    if (!fp) {
        return;
    }

    while (fgets(buffer, sizeof(buffer), fp) != NULL) {
        char * equals = strchr(buffer, '=');
        char * param;

        if (equals == NULL) {
            continue;
        }

        *equals = '\0';
        param = equals + 1;

        if (!strcmp(buffer, "enable_flash_uv_swap")) {
            _enable_flash_uv_swap = atoi(param);
        }
        else if (!strcmp(buffer, "disable_flash_pq_bg_color")) {
            _disable_flash_pq_bg_color = atoi(param);
        }
    }
379
380

    fclose(fp);
381
382
}

383
static void init_fixes(void)
384
385
386
387
388
389
390
391
392
393
{
    if (_inited_fixes) {
        return;
    }
    _inited_fixes = 1;

    init_running_under_flash();
    init_config();
}

394
395
396
397
398
399
400
401
402
403
VdpStatus vdp_device_create_x11(
    Display *             display,
    int                   screen,
    /* output parameters follow */
    VdpDevice *           device,
    VdpGetProcAddress * * get_proc_address
)
{
    VdpStatus status;

404
405
    init_fixes();

406
407
408
409
410
411
412
413
    if (!_vdp_imp_device_create_x11_proc) {
        status = _vdp_open_driver(display, screen);
        if (status != VDP_STATUS_OK) {
            _vdp_close_driver();
            return status;
        }
    }

414
    status = _vdp_imp_device_create_x11_proc(
Aaron Plattner's avatar
Aaron Plattner committed
415
416
417
        display,
        screen,
        device,
418
        &_imp_get_proc_address
Aaron Plattner's avatar
Aaron Plattner committed
419
    );
420
421
422
423
424
425
426
    if (status != VDP_STATUS_OK) {
        return status;
    }

    *get_proc_address = vdp_wrapper_get_proc_address;

    return VDP_STATUS_OK;
Aaron Plattner's avatar
Aaron Plattner committed
427
}