summaryrefslogtreecommitdiffstats
path: root/video/vdpau.h
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-05-09 21:49:42 +0200
committerwm4 <wm4@nowhere>2014-05-10 10:44:16 +0200
commitbc9a86c3923ce6d293db45100e5314d9c5241e12 (patch)
tree2e9be6f3a9f91b4160ada8601f734067932e1ca8 /video/vdpau.h
parent280e7e171a0798dd2605863b114982ae9b5cef39 (diff)
downloadmpv-bc9a86c3923ce6d293db45100e5314d9c5241e12.tar.bz2
mpv-bc9a86c3923ce6d293db45100e5314d9c5241e12.tar.xz
vdpau: make mp_vdpau_ctx thread-safe
Preparation so that various things related to video can run in different threads. One part to this is making the video surface pool safe. Another issue is the preemption mechanism, which continues to give us endless pain. In theory, it's probably impossible to handle preemption 100% correctly and race-condition free, unless _every_ API user in the same process uses a central, shared mutex to protect every vdpau API call. Otherwise, it could happen that one thread recovering from preemption allocates a vdpau object, and then another thread (which hasn't recovered yet) happens to free the object for some reason. This is because objects are referenced by integer IDs, and vdpau will reuse IDs invalidated by preemption after preemption. Since this is unreasonable, we're as lazy as possible when it comes to handling preemption. We don't do any locking around the mp_vdpau_ctx fields that are normally immutable, and only can change when recovering from preemption. In practice, this will work, because it doesn't matter whether not-yet-recovered components use the old or new vdpau function pointers or device ID. Code calls mp_vdpau_handle_preemption() anyway to check for the preemption event and possibly to recover, and that function acquires the lock protecting the preemption state. Another possible source of potential grandiose fuckup is the fact that the vdpau library is in fact only a tiny wrapper, and the real driver lives in a shared object dlopen()ed by the wrapper. The wrapper also calls dlclose() on the loaded shared object in some situations. One possible danger is that failing to recreate a vdpau device could trigger a dlclose() call, and that glibc might unload it. Currently, glibc implements full unloading of shared objects on the last dlclose() call, and if that happens, calls to function pointers pointing into the shared object would obviously crash. Fortunately, it seems the existing vdpau wrapper won't trigger this case and never unloads the driver once it's successfully loaded. To make it short, vdpau preemption opens up endless depths of WTFs. Another issue is that any participating thread might do the preemption recovery (whichever comes first). This is easier to implement. The implication is that we need threadsafe xlib. We just hope and pray that this will actually work. This also means that once vdpau code is actually involved in a multithreaded scenario, we have to add XInitThreads() to the X11 code.
Diffstat (limited to 'video/vdpau.h')
-rw-r--r--video/vdpau.h17
1 files changed, 12 insertions, 5 deletions
diff --git a/video/vdpau.h b/video/vdpau.h
index 38e679db25..acf4280266 100644
--- a/video/vdpau.h
+++ b/video/vdpau.h
@@ -4,6 +4,8 @@
#include <stdbool.h>
#include <inttypes.h>
+#include <pthread.h>
+
#include <vdpau/vdpau.h>
#include <vdpau/vdpau_x11.h>
@@ -35,26 +37,31 @@ struct vdp_functions {
// Shared state. Objects created from different VdpDevices are often (always?)
// incompatible to each other, so all code must use a shared VdpDevice.
struct mp_vdpau_ctx {
+ struct mp_log *log;
+ struct vo_x11_state *x11;
+
+ // These are mostly immutable, except on preemption. We don't really care
+ // to synchronize the preemption case fully correctly, because it's an
+ // extremely obscure corner case, and basically a vdpau API design bug.
+ // What we do will sort-of work anyway (no memory errors are possible).
struct vdp_functions vdp;
VdpGetProcAddress *get_proc_address;
VdpDevice vdp_device;
+
+ pthread_mutex_t preempt_lock;
bool is_preempted; // set to true during unavailability
uint64_t preemption_counter; // incremented after _restoring_
-
bool preemption_user_notified;
double last_preemption_retry_fail;
- struct vo_x11_state *x11;
-
// Surface pool
+ pthread_mutex_t pool_lock;
struct surface_entry {
VdpVideoSurface surface;
int w, h;
VdpChromaType chroma;
bool in_use;
} video_surfaces[MAX_VIDEO_SURFACES];
-
- struct mp_log *log;
};
struct mp_vdpau_ctx *mp_vdpau_create_device_x11(struct mp_log *log,