Coverage Report

Created: 2026-11-30 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/video/out/vo_libmpv.c
Line
Count
Source
1
#include <assert.h>
2
#include <limits.h>
3
#include <math.h>
4
#include <stdatomic.h>
5
#include <stdbool.h>
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
10
#include "mpv_talloc.h"
11
#include "common/common.h"
12
#include "misc/bstr.h"
13
#include "misc/dispatch.h"
14
#include "common/msg.h"
15
#include "options/m_config.h"
16
#include "options/options.h"
17
#include "aspect.h"
18
#include "dr_helper.h"
19
#include "vo.h"
20
#include "video/mp_image.h"
21
#include "sub/osd.h"
22
#include "osdep/threads.h"
23
#include "osdep/timer.h"
24
25
#include "common/global.h"
26
#include "player/client.h"
27
28
#include "libmpv.h"
29
30
#if HAVE_MACOS_COCOA_CB
31
#include "osdep/mac/app_bridge.h"
32
#endif
33
34
/*
35
 * mpv_render_context is managed by the host application - the host application
36
 * can access it any time, even if the VO is destroyed (or not created yet).
37
 *
38
 * - the libmpv user can mix render API and normal API; thus render API
39
 *   functions can wait on the core, but not the reverse
40
 * - the core does blocking calls into the VO thread, thus the VO functions
41
 *   can't wait on the user calling the API functions
42
 * - to make video timing work like it should, the VO thread waits on the
43
 *   render API user anyway, and the (unlikely) deadlock is avoided with
44
 *   a timeout
45
 *
46
 *  Locking:  mpv core > VO > mpv_render_context.lock > mp_client_api.lock
47
 *              > mpv_render_context.update_lock
48
 *  And: render thread > VO (wait for present)
49
 *       VO > render thread (wait for present done, via timeout)
50
 *
51
 *  Locking gets more complex with advanced_control enabled. Use
52
 *  mpv_render_context.dispatch with care; synchronous calls can add lock
53
 *  dependencies.
54
 */
55
56
struct vo_priv {
57
    struct mpv_render_context *ctx; // immutable after init
58
};
59
60
struct mpv_render_context {
61
    struct mp_log *log;
62
    struct mpv_global *global;
63
    struct mp_client_api *client_api;
64
65
    atomic_bool in_use;
66
67
    // --- Immutable after init
68
    struct mp_dispatch_queue *dispatch;
69
    bool advanced_control;
70
    struct dr_helper *dr;           // NULL if advanced_control disabled
71
72
    mp_mutex control_lock;
73
    // --- Protected by control_lock
74
    mp_render_cb_control_fn control_cb;
75
    void *control_cb_ctx;
76
77
    mp_mutex update_lock;
78
    mp_cond update_cond;     // paired with update_lock
79
80
    // --- Protected by update_lock
81
    mpv_render_update_fn update_cb;
82
    void *update_cb_ctx;
83
84
    mp_mutex lock;
85
    mp_cond video_wait;      // paired with lock
86
87
    // --- Protected by lock
88
    struct vo_frame *next_frame;    // next frame to draw
89
    int64_t present_count;          // incremented when next frame can be shown
90
    int64_t expected_flip_count;    // next vsync event for next_frame
91
    bool redrawing;                 // next_frame was a redraw request
92
    int64_t flip_count;
93
    struct vo_frame *cur_frame;
94
    struct mp_image_params img_params;
95
    int vp_w, vp_h;
96
    bool flip;
97
    bool imgfmt_supported[IMGFMT_END - IMGFMT_START];
98
    bool need_reconfig;
99
    bool need_resize;
100
    bool need_reset;
101
    bool need_update_external;
102
    bool paused;
103
    struct vo *vo;
104
    uint64_t frame_id_offset;       // added to the frame IDs of the VO
105
    uint64_t last_frame_id;         // highest ID a frame was passed on with
106
107
    // --- Mostly immutable after init.
108
    struct mp_hwdec_devices *hwdec_devs;
109
110
    // --- All of these can only be accessed from mpv_render_*() API, for
111
    //     which the user makes sure they're called synchronized.
112
    struct render_backend *renderer;
113
    struct m_config_cache *vo_opts_cache;
114
    struct mp_vo_opts *vo_opts;
115
};
116
117
const struct render_backend_fns *render_backends[] = {
118
    &render_backend_gpu_next,
119
    &render_backend_gpu,
120
    &render_backend_sw,
121
    NULL
122
};
123
124
static void update(struct mpv_render_context *ctx)
125
0
{
126
0
    mp_mutex_lock(&ctx->update_lock);
127
0
    if (ctx->update_cb)
128
0
        ctx->update_cb(ctx->update_cb_ctx);
129
130
0
    mp_cond_broadcast(&ctx->update_cond);
131
0
    mp_mutex_unlock(&ctx->update_lock);
132
0
}
133
134
void *get_mpv_render_param(mpv_render_param *params, mpv_render_param_type type,
135
                           void *def)
136
0
{
137
0
    for (int n = 0; params && params[n].type; n++) {
138
0
        if (params[n].type == type)
139
0
            return params[n].data;
140
0
    }
141
0
    return def;
142
0
}
143
144
static void forget_frames(struct mpv_render_context *ctx, bool all)
145
0
{
146
0
    mp_cond_broadcast(&ctx->video_wait);
147
0
    if (all) {
148
0
        talloc_free(ctx->cur_frame);
149
0
        ctx->cur_frame = NULL;
150
0
    }
151
0
}
152
153
static void dispatch_wakeup(void *ptr)
154
0
{
155
0
    struct mpv_render_context *ctx = ptr;
156
157
0
    update(ctx);
158
0
}
159
160
static struct mp_image *render_get_image(void *ptr, int imgfmt, int w, int h,
161
                                         int stride_align, int flags)
162
0
{
163
0
    struct mpv_render_context *ctx = ptr;
164
165
0
    return ctx->renderer->fns->get_image(ctx->renderer, imgfmt, w, h, stride_align, flags);
166
0
}
167
168
int mpv_render_context_create(mpv_render_context **res, mpv_handle *mpv,
169
                              mpv_render_param *params)
170
0
{
171
0
    mpv_render_context *ctx = talloc_zero(NULL, mpv_render_context);
172
0
    mp_mutex_init(&ctx->control_lock);
173
0
    mp_mutex_init(&ctx->lock);
174
0
    mp_mutex_init(&ctx->update_lock);
175
0
    mp_cond_init(&ctx->update_cond);
176
0
    mp_cond_init(&ctx->video_wait);
177
178
0
    ctx->global = mp_client_get_global(mpv);
179
0
    ctx->client_api = ctx->global->client_api;
180
0
    ctx->log = mp_log_new(ctx, ctx->global->log, "libmpv_render");
181
182
0
    ctx->vo_opts_cache = m_config_cache_alloc(ctx, ctx->global, &vo_sub_opts);
183
0
    ctx->vo_opts = ctx->vo_opts_cache->opts;
184
185
0
    ctx->dispatch = mp_dispatch_create(ctx);
186
0
    mp_dispatch_set_wakeup_fn(ctx->dispatch, dispatch_wakeup, ctx);
187
188
0
    if (GET_MPV_RENDER_PARAM(params, MPV_RENDER_PARAM_ADVANCED_CONTROL, int, 0))
189
0
        ctx->advanced_control = true;
190
191
0
    char *renderer = get_mpv_render_param(params, MPV_RENDER_PARAM_RENDERER, NULL);
192
193
0
    int err = MPV_ERROR_NOT_IMPLEMENTED;
194
0
    for (int n = 0; render_backends[n]; n++) {
195
0
        const char *name = render_backends[n]->renderer;
196
0
        if (name && renderer && strcmp(name, renderer) != 0)
197
0
            continue;
198
0
        ctx->renderer = talloc_zero(NULL, struct render_backend);
199
0
        *ctx->renderer = (struct render_backend){
200
0
            .global = ctx->global,
201
0
            .log = ctx->log,
202
0
            .fns = render_backends[n],
203
0
        };
204
0
        err = ctx->renderer->fns->init(ctx->renderer, params);
205
0
        if (err >= 0)
206
0
            break;
207
0
        ctx->renderer->fns->destroy(ctx->renderer);
208
0
        talloc_free(ctx->renderer->priv);
209
0
        TA_FREEP(&ctx->renderer);
210
0
        if (err != MPV_ERROR_NOT_IMPLEMENTED)
211
0
            break;
212
0
    }
213
214
0
    if (err < 0) {
215
0
        mpv_render_context_free(ctx);
216
0
        return err;
217
0
    }
218
219
0
    if (ctx->renderer->fns->renderer)
220
0
        MP_VERBOSE(ctx, "Using the %s renderer.\n", ctx->renderer->fns->renderer);
221
222
0
    ctx->hwdec_devs = ctx->renderer->hwdec_devs;
223
224
0
    for (int n = IMGFMT_START; n < IMGFMT_END; n++) {
225
0
        ctx->imgfmt_supported[n - IMGFMT_START] =
226
0
            ctx->renderer->fns->check_format(ctx->renderer, n);
227
0
    }
228
229
0
    if (ctx->renderer->fns->get_image && ctx->advanced_control)
230
0
        ctx->dr = dr_helper_create(ctx->dispatch, render_get_image, ctx);
231
232
0
    if (!mp_set_main_render_context(ctx->client_api, ctx, true)) {
233
0
        MP_ERR(ctx, "There is already a mpv_render_context set.\n");
234
0
        mpv_render_context_free(ctx);
235
0
        return MPV_ERROR_GENERIC;
236
0
    }
237
238
0
    *res = ctx;
239
0
    return 0;
240
0
}
241
242
void mpv_render_context_set_update_callback(mpv_render_context *ctx,
243
                                            mpv_render_update_fn callback,
244
                                            void *callback_ctx)
245
0
{
246
0
    mp_mutex_lock(&ctx->update_lock);
247
0
    ctx->update_cb = callback;
248
0
    ctx->update_cb_ctx = callback_ctx;
249
0
    if (ctx->update_cb)
250
0
        ctx->update_cb(ctx->update_cb_ctx);
251
0
    mp_mutex_unlock(&ctx->update_lock);
252
0
}
253
254
void mp_render_context_set_control_callback(mpv_render_context *ctx,
255
                                            mp_render_cb_control_fn callback,
256
                                            void *callback_ctx)
257
0
{
258
0
    mp_mutex_lock(&ctx->control_lock);
259
0
    ctx->control_cb = callback;
260
0
    ctx->control_cb_ctx = callback_ctx;
261
0
    mp_mutex_unlock(&ctx->control_lock);
262
0
}
263
264
void mpv_render_context_free(mpv_render_context *ctx)
265
0
{
266
0
    if (!ctx)
267
0
        return;
268
269
    // From here on, ctx becomes invisible and cannot be newly acquired. Only
270
    // a VO could still hold a reference.
271
0
    mp_set_main_render_context(ctx->client_api, ctx, false);
272
273
0
    if (atomic_load(&ctx->in_use)) {
274
        // Start destroy the VO, and also bring down the decoder etc., which
275
        // still might be using the hwdec context or use DR images. The above
276
        // mp_set_main_render_context() call guarantees it can't come back (so
277
        // ctx->vo can't change to non-NULL).
278
        // In theory, this races with vo_libmpv exiting and another VO being
279
        // used, which is a harmless grotesque corner case.
280
0
        kill_video_async(ctx->client_api);
281
282
0
        while (atomic_load(&ctx->in_use)) {
283
            // As a nasty detail, we need to wait until the VO is released, but
284
            // also need to react to update() calls during it (the update calls
285
            // are supposed to trigger processing ctx->dispatch). We solve this
286
            // by making the VO uninit function call mp_dispatch_interrupt().
287
            //
288
            // Other than that, processing ctx->dispatch is needed to serve the
289
            // video decoder, which might still not be fully destroyed, and e.g.
290
            // performs calls to release DR images (or, as a grotesque corner
291
            // case may even try to allocate new ones).
292
            //
293
            // Once the VO is released, ctx->dispatch becomes truly inactive.
294
            // (The libmpv API user could call mpv_render_context_update() while
295
            // mpv_render_context_free() is being called, but of course this is
296
            // invalid.)
297
0
            mp_dispatch_queue_process(ctx->dispatch, INFINITY);
298
0
        }
299
0
    }
300
301
0
    mp_mutex_lock(&ctx->lock);
302
    // Barrier - guarantee uninit() has left the lock region. It will access ctx
303
    // until the lock has been released, so we must not proceed with destruction
304
    // before we can acquire the lock. (The opposite, uninit() acquiring the
305
    // lock, can not happen anymore at this point - we've waited for VO uninit,
306
    // and prevented that new VOs can be created.)
307
0
    mp_mutex_unlock(&ctx->lock);
308
309
0
    mp_assert(!atomic_load(&ctx->in_use));
310
0
    mp_assert(!ctx->vo);
311
312
    // With the dispatch queue not being served anymore, allow frame free
313
    // requests from this thread to be served directly.
314
0
    if (ctx->dr)
315
0
        dr_helper_acquire_thread(ctx->dr);
316
317
    // Possibly remaining outstanding work.
318
0
    mp_dispatch_queue_process(ctx->dispatch, 0);
319
320
0
    forget_frames(ctx, true);
321
322
0
    if (ctx->renderer) {
323
0
        ctx->renderer->fns->destroy(ctx->renderer);
324
0
        talloc_free(ctx->renderer->priv);
325
0
        talloc_free(ctx->renderer);
326
0
    }
327
0
    talloc_free(ctx->dr);
328
0
    talloc_free(ctx->dispatch);
329
330
0
    mp_cond_destroy(&ctx->update_cond);
331
0
    mp_cond_destroy(&ctx->video_wait);
332
0
    mp_mutex_destroy(&ctx->update_lock);
333
0
    mp_mutex_destroy(&ctx->lock);
334
0
    mp_mutex_destroy(&ctx->control_lock);
335
336
0
    talloc_free(ctx);
337
0
}
338
339
// Try to mark the context as "in exclusive use" (e.g. by a VO).
340
// Note: the function must not acquire any locks, because it's called with an
341
// external leaf lock held.
342
bool mp_render_context_acquire(mpv_render_context *ctx)
343
0
{
344
0
    bool prev = false;
345
0
    return atomic_compare_exchange_strong(&ctx->in_use, &prev, true);
346
0
}
347
348
int mpv_render_context_render(mpv_render_context *ctx, mpv_render_param *params)
349
0
{
350
0
    mp_mutex_lock(&ctx->lock);
351
352
0
    int do_render =
353
0
        !GET_MPV_RENDER_PARAM(params, MPV_RENDER_PARAM_SKIP_RENDERING, int, 0);
354
355
0
    if (do_render) {
356
0
        int vp_w, vp_h;
357
0
        int err = ctx->renderer->fns->get_target_size(ctx->renderer, params,
358
0
                                                    &vp_w, &vp_h);
359
0
        if (err < 0) {
360
0
            mp_mutex_unlock(&ctx->lock);
361
0
            return err;
362
0
        }
363
364
0
        if (ctx->vo && (ctx->vp_w != vp_w || ctx->vp_h != vp_h ||
365
0
                        ctx->need_resize))
366
0
        {
367
0
            ctx->vp_w = vp_w;
368
0
            ctx->vp_h = vp_h;
369
370
0
            m_config_cache_update(ctx->vo_opts_cache);
371
372
0
            struct mp_rect src, dst;
373
0
            struct mp_osd_res osd;
374
0
            mp_get_src_dst_rects(ctx->log, ctx->vo_opts, ctx->renderer->driver_caps,
375
0
                                &ctx->img_params, vp_w, abs(vp_h),
376
0
                                1.0, &src, &dst, &osd);
377
378
0
            ctx->renderer->fns->resize(ctx->renderer, &src, &dst, &osd);
379
0
        }
380
0
        ctx->need_resize = false;
381
0
    }
382
383
0
    if (ctx->need_reconfig)
384
0
        ctx->renderer->fns->reconfig(ctx->renderer, &ctx->img_params);
385
0
    ctx->need_reconfig = false;
386
387
0
    if (ctx->renderer->fns->set_paused)
388
0
        ctx->renderer->fns->set_paused(ctx->renderer, ctx->paused);
389
390
0
    if (ctx->need_update_external)
391
0
        ctx->renderer->fns->update_external(ctx->renderer, ctx->vo);
392
0
    ctx->need_update_external = false;
393
394
0
    if (ctx->need_reset) {
395
0
        ctx->renderer->fns->reset(ctx->renderer);
396
0
        if (ctx->cur_frame)
397
0
            ctx->cur_frame->still = true;
398
0
    }
399
0
    ctx->need_reset = false;
400
401
0
    struct vo_frame *frame = ctx->next_frame;
402
0
    int64_t wait_present_count = ctx->present_count;
403
0
    if (frame) {
404
0
        ctx->next_frame = NULL;
405
0
        if (!(frame->redraw || !frame->current))
406
0
            wait_present_count += 1;
407
0
        mp_cond_broadcast(&ctx->video_wait);
408
0
        talloc_free(ctx->cur_frame);
409
0
        ctx->cur_frame = vo_frame_ref(frame);
410
0
    } else {
411
0
        frame = vo_frame_ref(ctx->cur_frame);
412
0
        if (frame)
413
0
            frame->redraw = true;
414
0
        MP_STATS(ctx, "glcb-noframe");
415
0
    }
416
0
    struct vo_frame dummy = {0};
417
0
    if (!frame)
418
0
        frame = &dummy;
419
420
0
    mp_mutex_unlock(&ctx->lock);
421
422
0
    MP_STATS(ctx, "glcb-render");
423
424
0
    int err = 0;
425
426
0
    if (do_render)
427
0
        err = ctx->renderer->fns->render(ctx->renderer, params, frame);
428
429
0
    if (frame != &dummy)
430
0
        talloc_free(frame);
431
432
0
    if (GET_MPV_RENDER_PARAM(params, MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME,
433
0
                             int, 1))
434
0
    {
435
0
        mp_mutex_lock(&ctx->lock);
436
0
        while (wait_present_count > ctx->present_count)
437
0
            mp_cond_wait(&ctx->video_wait, &ctx->lock);
438
0
        mp_mutex_unlock(&ctx->lock);
439
0
    }
440
441
0
    return err;
442
0
}
443
444
void mpv_render_context_report_swap(mpv_render_context *ctx)
445
0
{
446
0
    MP_STATS(ctx, "glcb-reportflip");
447
448
0
    mp_mutex_lock(&ctx->lock);
449
0
    ctx->flip_count += 1;
450
0
    mp_cond_broadcast(&ctx->video_wait);
451
0
    mp_mutex_unlock(&ctx->lock);
452
0
}
453
454
uint64_t mpv_render_context_update(mpv_render_context *ctx)
455
0
{
456
0
    uint64_t res = 0;
457
458
0
    mp_dispatch_queue_process(ctx->dispatch, 0);
459
460
0
    mp_mutex_lock(&ctx->lock);
461
0
    if (ctx->next_frame)
462
0
        res |= MPV_RENDER_UPDATE_FRAME;
463
0
    mp_mutex_unlock(&ctx->lock);
464
0
    return res;
465
0
}
466
467
int mpv_render_context_set_parameter(mpv_render_context *ctx,
468
                                     mpv_render_param param)
469
0
{
470
0
    return ctx->renderer->fns->set_parameter(ctx->renderer, param);
471
0
}
472
473
int mpv_render_context_get_info(mpv_render_context *ctx,
474
                                mpv_render_param param)
475
0
{
476
0
    int res = MPV_ERROR_NOT_IMPLEMENTED;
477
0
    mp_mutex_lock(&ctx->lock);
478
479
0
    switch (param.type) {
480
0
    case MPV_RENDER_PARAM_NEXT_FRAME_INFO: {
481
0
        mpv_render_frame_info *info = param.data;
482
0
        *info = (mpv_render_frame_info){0};
483
0
        struct vo_frame *frame = ctx->next_frame;
484
0
        if (frame) {
485
0
            info->flags =
486
0
                MPV_RENDER_FRAME_INFO_PRESENT |
487
0
                (frame->redraw ? MPV_RENDER_FRAME_INFO_REDRAW : 0) |
488
0
                (frame->repeat ? MPV_RENDER_FRAME_INFO_REPEAT : 0) |
489
0
                (frame->display_synced && !frame->redraw ?
490
0
                    MPV_RENDER_FRAME_INFO_BLOCK_VSYNC : 0);
491
0
            info->target_time = frame->pts;
492
0
        }
493
0
        res = 0;
494
0
        break;
495
0
    }
496
0
    default:;
497
0
    }
498
499
0
    mp_mutex_unlock(&ctx->lock);
500
0
    return res;
501
0
}
502
503
static bool draw_frame(struct vo *vo, struct vo_frame *frame)
504
0
{
505
0
    struct vo_priv *p = vo->priv;
506
0
    struct mpv_render_context *ctx = p->ctx;
507
508
0
    mp_mutex_lock(&ctx->lock);
509
0
    mp_assert(!ctx->next_frame);
510
0
    ctx->next_frame = vo_frame_ref(frame);
511
0
    if (frame->num_frames) {
512
0
        ctx->next_frame->frame_id += ctx->frame_id_offset;
513
0
        ctx->last_frame_id = ctx->next_frame->frame_id + frame->num_frames - 1;
514
0
    }
515
0
    ctx->expected_flip_count = ctx->flip_count + 1;
516
0
    ctx->redrawing = frame->redraw || !frame->current;
517
0
    mp_mutex_unlock(&ctx->lock);
518
519
0
    update(ctx);
520
0
    return VO_TRUE;
521
0
}
522
523
static void flip_page(struct vo *vo)
524
0
{
525
0
    struct vo_priv *p = vo->priv;
526
0
    struct mpv_render_context *ctx = p->ctx;
527
0
    int64_t until = mp_time_ns() + MP_TIME_MS_TO_NS(200);
528
529
0
    mp_mutex_lock(&ctx->lock);
530
531
    // Wait until frame was rendered
532
0
    while (ctx->next_frame) {
533
0
        if (mp_cond_timedwait_until(&ctx->video_wait, &ctx->lock, until)) {
534
0
            if (ctx->next_frame) {
535
0
                MP_VERBOSE(vo, "mpv_render_context_render() not being called "
536
0
                           "or stuck.\n");
537
0
                goto done;
538
0
            }
539
0
        }
540
0
    }
541
542
    // Unblock mpv_render_context_render().
543
0
    ctx->present_count += 1;
544
0
    mp_cond_broadcast(&ctx->video_wait);
545
546
0
    if (ctx->redrawing)
547
0
        goto done; // do not block for redrawing
548
549
    // Wait until frame was presented
550
0
    while (ctx->expected_flip_count > ctx->flip_count) {
551
        // mpv_render_report_swap() is declared as optional API.
552
        // Assume the user calls it consistently _if_ it's called at all.
553
0
        if (!ctx->flip_count)
554
0
            break;
555
0
        if (mp_cond_timedwait_until(&ctx->video_wait, &ctx->lock, until)) {
556
0
            MP_VERBOSE(vo, "mpv_render_report_swap() not being called.\n");
557
0
            goto done;
558
0
        }
559
0
    }
560
561
0
done:
562
563
    // Cleanup after the API user is not reacting, or is being unusually slow.
564
0
    if (ctx->next_frame) {
565
0
        talloc_free(ctx->cur_frame);
566
0
        ctx->cur_frame = ctx->next_frame;
567
0
        ctx->next_frame = NULL;
568
0
        ctx->present_count += 2;
569
0
        mp_cond_signal(&ctx->video_wait);
570
0
        vo_increment_drop_count(vo, 1);
571
0
    }
572
573
0
    mp_mutex_unlock(&ctx->lock);
574
0
}
575
576
static int query_format(struct vo *vo, int format)
577
0
{
578
0
    struct vo_priv *p = vo->priv;
579
0
    struct mpv_render_context *ctx = p->ctx;
580
581
0
    bool ok = false;
582
0
    mp_mutex_lock(&ctx->lock);
583
0
    if (format >= IMGFMT_START && format < IMGFMT_END)
584
0
        ok = ctx->imgfmt_supported[format - IMGFMT_START];
585
0
    mp_mutex_unlock(&ctx->lock);
586
0
    return ok;
587
0
}
588
589
static void run_control_on_render_thread(void *p)
590
0
{
591
0
    void **args = p;
592
0
    struct mpv_render_context *ctx = args[0];
593
0
    int request = (intptr_t)args[1];
594
0
    void *data = args[2];
595
0
    int ret = VO_NOTIMPL;
596
597
0
    switch (request) {
598
0
    case VOCTRL_SCREENSHOT: {
599
0
        mp_mutex_lock(&ctx->lock);
600
0
        struct vo_frame *frame = vo_frame_ref(ctx->cur_frame);
601
0
        mp_mutex_unlock(&ctx->lock);
602
0
        if (frame && ctx->renderer->fns->screenshot)
603
0
            ctx->renderer->fns->screenshot(ctx->renderer, frame, data);
604
0
        talloc_free(frame);
605
0
        break;
606
0
    }
607
0
    case VOCTRL_PERFORMANCE_DATA: {
608
0
        if (ctx->renderer->fns->perfdata) {
609
0
            ctx->renderer->fns->perfdata(ctx->renderer, data);
610
0
            ret = VO_TRUE;
611
0
        }
612
0
        break;
613
0
    }
614
0
    }
615
616
0
    *(int *)args[3] = ret;
617
0
}
618
619
static int control(struct vo *vo, uint32_t request, void *data)
620
0
{
621
0
    struct vo_priv *p = vo->priv;
622
0
    struct mpv_render_context *ctx = p->ctx;
623
624
0
    switch (request) {
625
0
    case VOCTRL_RESET:
626
0
        mp_mutex_lock(&ctx->lock);
627
0
        forget_frames(ctx, false);
628
0
        ctx->need_reset = true;
629
0
        mp_mutex_unlock(&ctx->lock);
630
0
        vo->want_redraw = true;
631
0
        return VO_TRUE;
632
0
    case VOCTRL_PAUSE:
633
0
        mp_mutex_lock(&ctx->lock);
634
0
        ctx->paused = true;
635
0
        mp_mutex_unlock(&ctx->lock);
636
0
        vo->want_redraw = true;
637
0
        return VO_TRUE;
638
0
    case VOCTRL_RESUME:
639
0
        mp_mutex_lock(&ctx->lock);
640
0
        ctx->paused = false;
641
0
        mp_mutex_unlock(&ctx->lock);
642
0
        return VO_TRUE;
643
0
    case VOCTRL_SET_PANSCAN:
644
0
        mp_mutex_lock(&ctx->lock);
645
0
        ctx->need_resize = true;
646
0
        mp_mutex_unlock(&ctx->lock);
647
0
        vo->want_redraw = true;
648
0
        return VO_TRUE;
649
0
    case VOCTRL_UPDATE_RENDER_OPTS:
650
0
        mp_mutex_lock(&ctx->lock);
651
0
        ctx->need_update_external = true;
652
0
        mp_mutex_unlock(&ctx->lock);
653
0
        vo->want_redraw = true;
654
0
        return VO_TRUE;
655
0
    }
656
657
    // VOCTRLs to be run on the renderer thread (if possible at all).
658
0
    if (ctx->advanced_control) {
659
0
        switch (request) {
660
0
        case VOCTRL_SCREENSHOT:
661
0
        case VOCTRL_PERFORMANCE_DATA: {
662
0
            int ret;
663
0
            void *args[] = {ctx, (void *)(intptr_t)request, data, &ret};
664
0
            mp_dispatch_run(ctx->dispatch, run_control_on_render_thread, args);
665
0
            return ret;
666
0
        }
667
0
        }
668
0
    }
669
670
0
    int r = VO_NOTIMPL;
671
0
    mp_mutex_lock(&ctx->control_lock);
672
0
    if (ctx->control_cb) {
673
0
        int events = 0;
674
0
        r = p->ctx->control_cb(vo, p->ctx->control_cb_ctx,
675
0
                               &events, request, data);
676
0
        vo_event(vo, events);
677
0
    }
678
0
    mp_mutex_unlock(&ctx->control_lock);
679
680
0
    return r;
681
0
}
682
683
static struct mp_image *get_image(struct vo *vo, int imgfmt, int w, int h,
684
                                  int stride_align, int flags)
685
0
{
686
0
    struct vo_priv *p = vo->priv;
687
0
    struct mpv_render_context *ctx = p->ctx;
688
689
0
    if (ctx->dr)
690
0
        return dr_helper_get_image(ctx->dr, imgfmt, w, h, stride_align, flags);
691
692
0
    return NULL;
693
0
}
694
695
static int reconfig(struct vo *vo, struct mp_image_params *params)
696
0
{
697
0
    struct vo_priv *p = vo->priv;
698
0
    struct mpv_render_context *ctx = p->ctx;
699
700
0
    mp_mutex_lock(&ctx->lock);
701
0
    forget_frames(ctx, true);
702
0
    ctx->img_params = *params;
703
0
    ctx->need_reconfig = true;
704
0
    ctx->need_resize = true;
705
0
    mp_mutex_unlock(&ctx->lock);
706
707
0
    control(vo, VOCTRL_RECONFIG, NULL);
708
709
0
    return 0;
710
0
}
711
712
static void uninit(struct vo *vo)
713
0
{
714
0
    struct vo_priv *p = vo->priv;
715
0
    struct mpv_render_context *ctx = p->ctx;
716
717
0
    control(vo, VOCTRL_UNINIT, NULL);
718
719
0
    mp_mutex_lock(&ctx->lock);
720
721
0
    forget_frames(ctx, true);
722
0
    ctx->img_params = (struct mp_image_params){0};
723
0
    ctx->need_reconfig = true;
724
0
    ctx->need_resize = true;
725
0
    ctx->need_update_external = true;
726
0
    ctx->need_reset = true;
727
0
    ctx->paused = false;
728
0
    ctx->vo = NULL;
729
0
    ctx->frame_id_offset = ctx->last_frame_id;
730
731
    // The following do not normally need ctx->lock, however, ctx itself may
732
    // become invalid once we release ctx->lock.
733
0
    bool prev_in_use = atomic_exchange(&ctx->in_use, false);
734
0
    mp_assert(prev_in_use); // obviously must have been set
735
0
    mp_dispatch_interrupt(ctx->dispatch);
736
737
0
    mp_mutex_unlock(&ctx->lock);
738
0
}
739
740
static int preinit(struct vo *vo)
741
0
{
742
#if HAVE_MACOS_COCOA_CB
743
    cocoa_init_cocoa_cb();
744
#else
745
0
    if (vo->probing)
746
0
        return -1;
747
0
#endif
748
749
0
    struct vo_priv *p = vo->priv;
750
751
0
    struct mpv_render_context *ctx =
752
0
        mp_client_api_acquire_render_context(vo->global->client_api);
753
0
    p->ctx = ctx;
754
755
0
    if (!ctx) {
756
0
        if (!vo->probing)
757
0
            MP_FATAL(vo, "No render context set.\n");
758
0
        return -1;
759
0
    }
760
761
0
    mp_mutex_lock(&ctx->lock);
762
0
    ctx->vo = vo;
763
0
    ctx->need_resize = true;
764
0
    ctx->need_update_external = true;
765
0
    mp_mutex_unlock(&ctx->lock);
766
767
0
    vo->hwdec_devs = ctx->hwdec_devs;
768
0
    control(vo, VOCTRL_PREINIT, NULL);
769
770
0
    return 0;
771
0
}
772
773
const struct vo_driver video_out_libmpv = {
774
    .description = "render API for libmpv",
775
    .name = "libmpv",
776
    .caps = VO_CAP_ROTATE90 | VO_CAP_VFLIP,
777
    .preinit = preinit,
778
    .query_format = query_format,
779
    .reconfig = reconfig,
780
    .control = control,
781
    .get_image_ts = get_image,
782
    .draw_frame = draw_frame,
783
    .flip_page = flip_page,
784
    .uninit = uninit,
785
    .priv_size = sizeof(struct vo_priv),
786
};