video/out/gpu/context: convert --gpu-context to use obj_settings_list

Since the list of available GPU contexts is a compile time constant,
use obj_settings_list instead of opt_string_validate for GPU contexts.
This has several advantages:

- Aligns with the existing usage of vo, ao, and filter lists.
- Allows custom probing order.
- Allows list option suffixes. (--gpu-context-append, etc.)
- Allows autocomplete in console.lua.
- Uses the standard obj_settings_list help printing, so the custom
  help printing function is no longer needed.

This also deduplicates some context creation code for ra_ctx_create
and ra_ctx_create_by_name.
This commit is contained in:
nanahi 2024-04-15 22:47:20 -04:00 committed by Kacper Michajłow
parent d5ec069829
commit 96e1f1dfa5
3 changed files with 109 additions and 98 deletions

View File

@ -7306,7 +7306,7 @@ void mp_option_change_callback(void *ctx, struct m_config_option *co, int flags,
} }
if (opt_ptr == &opts->vo->video_driver_list || if (opt_ptr == &opts->vo->video_driver_list ||
opt_ptr == &opts->ra_ctx_opts->context_name || opt_ptr == &opts->ra_ctx_opts->context_list ||
opt_ptr == &opts->ra_ctx_opts->context_type) { opt_ptr == &opts->ra_ctx_opts->context_type) {
struct track *track = mpctx->current_track[0][STREAM_VIDEO]; struct track *track = mpctx->current_track[0][STREAM_VIDEO];
uninit_video_out(mpctx); uninit_video_out(mpctx);

View File

@ -119,6 +119,38 @@ static const struct ra_ctx_fns *no_api_contexts[] = {
#endif #endif
}; };
static bool get_desc(struct m_obj_desc *dst, int index)
{
if (index >= MP_ARRAY_SIZE(contexts))
return false;
const struct ra_ctx_fns *ctx = contexts[index];
*dst = (struct m_obj_desc) {
.name = ctx->name,
};
return true;
}
static bool check_unknown_entry(const char *name)
{
struct bstr param = bstr0(name);
if (bstr_equals0(param, "auto"))
return true;
for (int i = 0; i < MP_ARRAY_SIZE(contexts); i++) {
if (bstr_equals0(param, contexts[i]->name))
return true;
}
return false;
}
const struct m_obj_list ra_ctx_obj_list = {
.get_desc = get_desc,
.check_unknown_entry = check_unknown_entry,
.description = "GPU contexts",
.allow_trailer = true,
.disallow_positional_parameters = true,
.use_global_options = true,
};
static int ra_ctx_api_help(struct mp_log *log, const struct m_option *opt, static int ra_ctx_api_help(struct mp_log *log, const struct m_option *opt,
struct bstr name) struct bstr name)
{ {
@ -142,93 +174,36 @@ static inline OPT_STRING_VALIDATE_FUNC(ra_ctx_validate_api)
return M_OPT_INVALID; return M_OPT_INVALID;
} }
static int ra_ctx_context_help(struct mp_log *log, const struct m_option *opt, #define OPT_BASE_STRUCT struct ra_ctx_opts
struct bstr name) const struct m_sub_options ra_ctx_conf = {
{ .opts = (const m_option_t[]) {
mp_info(log, "GPU contexts (APIs):\n"); {"gpu-context",
mp_info(log, " auto (autodetect)\n"); OPT_SETTINGSLIST(context_list, &ra_ctx_obj_list)},
for (int n = 0; n < MP_ARRAY_SIZE(contexts); n++) { {"gpu-api",
mp_info(log, " %s (%s)\n", contexts[n]->name, contexts[n]->type); OPT_STRING_VALIDATE(context_type, ra_ctx_validate_api),
} .help = ra_ctx_api_help},
return M_OPT_EXIT; {"gpu-debug", OPT_BOOL(debug)},
} {"gpu-sw", OPT_BOOL(allow_sw)},
{0}
},
.size = sizeof(struct ra_ctx_opts),
};
static inline OPT_STRING_VALIDATE_FUNC(ra_ctx_validate_context) static struct ra_ctx *create_in_contexts(struct vo *vo, const char *name, bool api_auto,
{
struct bstr param = bstr0(*value);
if (bstr_equals0(param, "auto"))
return 1;
for (int i = 0; i < MP_ARRAY_SIZE(contexts); i++) {
if (bstr_equals0(param, contexts[i]->name))
return 1;
}
return M_OPT_INVALID;
}
// Create a VO window and create a RA context on it.
// vo_flags: passed to the backend's create window function
struct ra_ctx *ra_ctx_create(struct vo *vo, struct ra_ctx_opts opts)
{
bool api_auto = !opts.context_type || strcmp(opts.context_type, "auto") == 0;
bool ctx_auto = !opts.context_name || strcmp(opts.context_name, "auto") == 0;
if (ctx_auto) {
MP_VERBOSE(vo, "Probing for best GPU context.\n");
opts.probing = true;
}
// Hack to silence backend (X11/Wayland/etc.) errors. Kill it once backends
// are separate from `struct vo`
bool old_probing = vo->probing;
vo->probing = opts.probing;
for (int i = 0; i < MP_ARRAY_SIZE(contexts); i++) {
if (!opts.probing && strcmp(contexts[i]->name, opts.context_name) != 0)
continue;
if (!api_auto && strcmp(contexts[i]->type, opts.context_type) != 0)
continue;
struct ra_ctx *ctx = talloc_ptrtype(NULL, ctx);
*ctx = (struct ra_ctx) {
.vo = vo,
.global = vo->global,
.log = mp_log_new(ctx, vo->log, contexts[i]->type),
.opts = opts,
.fns = contexts[i],
};
MP_VERBOSE(ctx, "Initializing GPU context '%s'\n", ctx->fns->name);
if (contexts[i]->init(ctx)) {
vo->probing = old_probing;
vo->context_name = ctx->fns->name;
return ctx;
}
talloc_free(ctx);
}
vo->probing = old_probing;
// If we've reached this point, then none of the contexts matched the name
// requested, or the backend creation failed for all of them.
if (!vo->probing)
MP_ERR(vo, "Failed initializing any suitable GPU context!\n");
return NULL;
}
static struct ra_ctx *create_in_contexts(struct vo *vo, const char *name,
const struct ra_ctx_fns *ctxs[], const struct ra_ctx_fns *ctxs[],
size_t size) size_t size, struct ra_ctx_opts opts)
{ {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (strcmp(name, ctxs[i]->name) != 0) if (strcmp(name, ctxs[i]->name) != 0)
continue; continue;
if (!api_auto && strcmp(ctxs[i]->type, opts.context_type) != 0)
continue;
struct ra_ctx *ctx = talloc_ptrtype(NULL, ctx); struct ra_ctx *ctx = talloc_ptrtype(NULL, ctx);
*ctx = (struct ra_ctx) { *ctx = (struct ra_ctx) {
.vo = vo, .vo = vo,
.global = vo->global, .global = vo->global,
.log = mp_log_new(ctx, vo->log, ctxs[i]->type), .log = mp_log_new(ctx, vo->log, ctxs[i]->type),
.opts = opts,
.fns = ctxs[i], .fns = ctxs[i],
}; };
@ -242,12 +217,64 @@ static struct ra_ctx *create_in_contexts(struct vo *vo, const char *name,
struct ra_ctx *ra_ctx_create_by_name(struct vo *vo, const char *name) struct ra_ctx *ra_ctx_create_by_name(struct vo *vo, const char *name)
{ {
struct ra_ctx *ctx = create_in_contexts(vo, name, contexts, struct ra_ctx_opts dummy = {0};
MP_ARRAY_SIZE(contexts)); struct ra_ctx *ctx = create_in_contexts(vo, name, true, contexts,
MP_ARRAY_SIZE(contexts), dummy);
if (ctx) if (ctx)
return ctx; return ctx;
return create_in_contexts(vo, name, no_api_contexts, return create_in_contexts(vo, name, true, no_api_contexts,
MP_ARRAY_SIZE(no_api_contexts)); MP_ARRAY_SIZE(no_api_contexts), dummy);
}
// Create a VO window and create a RA context on it.
// vo_flags: passed to the backend's create window function
struct ra_ctx *ra_ctx_create(struct vo *vo, struct ra_ctx_opts opts)
{
bool api_auto = !opts.context_type || strcmp(opts.context_type, "auto") == 0;
bool ctx_auto = !opts.context_list ||
(opts.context_list[0].name &&
strcmp(opts.context_list[0].name, "auto") == 0);
if (ctx_auto) {
MP_VERBOSE(vo, "Probing for best GPU context.\n");
opts.probing = true;
}
// Hack to silence backend (X11/Wayland/etc.) errors. Kill it once backends
// are separate from `struct vo`
bool old_probing = vo->probing;
vo->probing = opts.probing;
struct ra_ctx *ctx = NULL;
if (opts.probing) {
for (int i = 0; i < MP_ARRAY_SIZE(contexts); i++) {
ctx = create_in_contexts(vo, contexts[i]->name, api_auto,
contexts, MP_ARRAY_SIZE(contexts), opts);
if (ctx)
goto done;
}
}
for (int i = 0; opts.context_list && opts.context_list[i].name; i++) {
ctx = create_in_contexts(vo, opts.context_list[i].name, api_auto,
contexts, MP_ARRAY_SIZE(contexts), opts);
if (ctx)
goto done;
}
done:
if (ctx) {
vo->probing = old_probing;
vo->context_name = ctx->fns->name;
return ctx;
}
vo->probing = old_probing;
// If we've reached this point, then none of the contexts matched the name
// requested, or the backend creation failed for all of them.
if (!vo->probing)
MP_ERR(vo, "Failed initializing any suitable GPU context!\n");
return NULL;
} }
void ra_ctx_destroy(struct ra_ctx **ctx_ptr) void ra_ctx_destroy(struct ra_ctx **ctx_ptr)
@ -264,19 +291,3 @@ void ra_ctx_destroy(struct ra_ctx **ctx_ptr)
*ctx_ptr = NULL; *ctx_ptr = NULL;
} }
#define OPT_BASE_STRUCT struct ra_ctx_opts
const struct m_sub_options ra_ctx_conf = {
.opts = (const m_option_t[]) {
{"gpu-context",
OPT_STRING_VALIDATE(context_name, ra_ctx_validate_context),
.help = ra_ctx_context_help},
{"gpu-api",
OPT_STRING_VALIDATE(context_type, ra_ctx_validate_api),
.help = ra_ctx_api_help},
{"gpu-debug", OPT_BOOL(debug)},
{"gpu-sw", OPT_BOOL(allow_sw)},
{0}
},
.size = sizeof(struct ra_ctx_opts),
};

View File

@ -10,7 +10,7 @@ struct ra_ctx_opts {
bool want_alpha; // create an alpha framebuffer if possible bool want_alpha; // create an alpha framebuffer if possible
bool debug; // enable debugging layers/callbacks etc. bool debug; // enable debugging layers/callbacks etc.
bool probing; // the backend was auto-probed bool probing; // the backend was auto-probed
char *context_name; // filter by `ra_ctx_fns.name` struct m_obj_settings *context_list; // list of `ra_ctx_fns.name` to probe
char *context_type; // filter by `ra_ctx_fns.type` char *context_type; // filter by `ra_ctx_fns.type`
}; };