diff options
Diffstat (limited to 'video/out/dr_helper.c')
-rw-r--r-- | video/out/dr_helper.c | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/video/out/dr_helper.c b/video/out/dr_helper.c index 78d4633efb..ac440a745d 100644 --- a/video/out/dr_helper.c +++ b/video/out/dr_helper.c @@ -1,26 +1,26 @@ -#include <stdlib.h> #include <assert.h> -#include <pthread.h> +#include <stdatomic.h> +#include <stdlib.h> #include <libavutil/buffer.h> -#include "mpv_talloc.h" #include "misc/dispatch.h" -#include "osdep/atomic.h" +#include "mpv_talloc.h" +#include "osdep/threads.h" #include "video/mp_image.h" #include "dr_helper.h" struct dr_helper { - pthread_mutex_t thread_lock; - pthread_t thread; - bool thread_valid; // (POSIX defines no "unset" pthread_t value yet) + mp_mutex thread_lock; + mp_thread_id thread_id; + bool thread_valid; // (POSIX defines no "unset" mp_thread value yet) struct mp_dispatch_queue *dispatch; atomic_ullong dr_in_flight; struct mp_image *(*get_image)(void *ctx, int imgfmt, int w, int h, - int stride_align); + int stride_align, int flags); void *get_image_ctx; }; @@ -32,43 +32,43 @@ static void dr_helper_destroy(void *ptr) // dangling pointers. assert(atomic_load(&dr->dr_in_flight) == 0); - pthread_mutex_destroy(&dr->thread_lock); + mp_mutex_destroy(&dr->thread_lock); } struct dr_helper *dr_helper_create(struct mp_dispatch_queue *dispatch, struct mp_image *(*get_image)(void *ctx, int imgfmt, int w, int h, - int stride_align), + int stride_align, int flags), void *get_image_ctx) { struct dr_helper *dr = talloc_ptrtype(NULL, dr); talloc_set_destructor(dr, dr_helper_destroy); *dr = (struct dr_helper){ .dispatch = dispatch, - .dr_in_flight = ATOMIC_VAR_INIT(0), + .dr_in_flight = 0, .get_image = get_image, .get_image_ctx = get_image_ctx, }; - pthread_mutex_init(&dr->thread_lock, NULL); + mp_mutex_init(&dr->thread_lock); return dr; } void dr_helper_acquire_thread(struct dr_helper *dr) { - pthread_mutex_lock(&dr->thread_lock); + mp_mutex_lock(&dr->thread_lock); assert(!dr->thread_valid); // fails on API user errors dr->thread_valid = true; - dr->thread = pthread_self(); - pthread_mutex_unlock(&dr->thread_lock); + dr->thread_id = mp_thread_current_id(); + mp_mutex_unlock(&dr->thread_lock); } void dr_helper_release_thread(struct dr_helper *dr) { - pthread_mutex_lock(&dr->thread_lock); + mp_mutex_lock(&dr->thread_lock); // Fails on API user errors. assert(dr->thread_valid); - assert(pthread_equal(dr->thread, pthread_self())); + assert(mp_thread_id_equal(dr->thread_id, mp_thread_current_id())); dr->thread_valid = false; - pthread_mutex_unlock(&dr->thread_lock); + mp_mutex_unlock(&dr->thread_lock); } struct free_dr_context { @@ -92,10 +92,10 @@ static void free_dr_buffer_on_dr_thread(void *opaque, uint8_t *data) struct free_dr_context *ctx = opaque; struct dr_helper *dr = ctx->dr; - pthread_mutex_lock(&dr->thread_lock); + mp_mutex_lock(&dr->thread_lock); bool on_this_thread = - dr->thread_valid && pthread_equal(ctx->dr->thread, pthread_self()); - pthread_mutex_unlock(&dr->thread_lock); + dr->thread_valid && mp_thread_id_equal(ctx->dr->thread_id, mp_thread_current_id()); + mp_mutex_unlock(&dr->thread_lock); // The image could be unreffed even on the DR thread. In practice, this // matters most on DR destruction. @@ -108,7 +108,7 @@ static void free_dr_buffer_on_dr_thread(void *opaque, uint8_t *data) struct get_image_cmd { struct dr_helper *dr; - int imgfmt, w, h, stride_align; + int imgfmt, w, h, stride_align, flags; struct mp_image *res; }; @@ -118,7 +118,7 @@ static void sync_get_image(void *ptr) struct dr_helper *dr = cmd->dr; cmd->res = dr->get_image(dr->get_image_ctx, cmd->imgfmt, cmd->w, cmd->h, - cmd->stride_align); + cmd->stride_align, cmd->flags); if (!cmd->res) return; @@ -140,8 +140,7 @@ static void sync_get_image(void *ptr) AVBufferRef *new_ref = av_buffer_create(ctx->ref->data, ctx->ref->size, free_dr_buffer_on_dr_thread, ctx, 0); - if (!new_ref) - abort(); // tiny malloc OOM + MP_HANDLE_OOM(new_ref); cmd->res->bufs[0] = new_ref; @@ -149,11 +148,14 @@ static void sync_get_image(void *ptr) } struct mp_image *dr_helper_get_image(struct dr_helper *dr, int imgfmt, - int w, int h, int stride_align) + int w, int h, int stride_align, int flags) { struct get_image_cmd cmd = { .dr = dr, - .imgfmt = imgfmt, .w = w, .h = h, .stride_align = stride_align, + .imgfmt = imgfmt, + .w = w, .h = h, + .stride_align = stride_align, + .flags = flags, }; mp_dispatch_run(dr->dispatch, sync_get_image, &cmd); return cmd.res; |