Skip to content

Commit

Permalink
player: use XDG_CACHE_HOME by default
Browse files Browse the repository at this point in the history
This adds cache as a possible path for mpv to interally pick
(~/.cache/mpv for non-darwin unix-like systems, the usual config
directory for everyone else). Internally, --cache-dir has been reworked
to be a singular, global option that controls where mpv may put any
cache. This means --gpu-shader-cache-dir and --icc-cache-dir are both
obsolete options now (they just map to --cache-dir). This is essentially
the same as --config-dir but instead for --cache-dir (to force a
specific directory for only cache files). If --cache-dir isn't set, then
the cache directory is XDG_CACHE_HOME. This does mean that it is no
longer possible to have separate directories for the different kinds of
cache files mpv can create (demuxer cache, gpu shader cache, and icc
cache), but most likely nobody seriously does that. Cache is cache and
it makes more sense for it to be in one directory and controlled by the
same option.
  • Loading branch information
Dudemanguy committed Apr 29, 2023
1 parent e63a7b7 commit 59ac119
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 33 deletions.
7 changes: 7 additions & 0 deletions DOCS/interface-changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Interface changes
- `--save-position-on-quit` and its associated commands now store state files in
the XDG_STATE_HOME directory by default. This only has an effect on linux/bsd
systems.
- add `--icc-cache` and `--gpu-shader-cache` options
- mpv now implictly saves cache files in XDG_CACHE_HOME by default. This only has
an effect if the user enables options that would lead to cache being stored.
- ``--cache-dir`` now controls the location of the cache directory for all
mpv-related cache (demuxer cache, gpu shader cache, and icc cache).
- deprecate ``--icc-cache-dir`` and alias it to ``--cache-dir``
- deprecate ``--gpu-shader-cache-dir`` and alias it to ``--cache-dir``
--- mpv 0.35.0 ---
- add the `--vo=gpu-next` video output driver, as well as the options
`--allow-delayed-peak-detect`, `--builtin-scalers`,
Expand Down
2 changes: 2 additions & 0 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ Name Meaning
``~~desktop/`` the path to the desktop (win32, macOS)
``~~exe_dir/`` win32 only: the path to the directory containing the exe (for
config file purposes; ``$MPV_HOME`` overrides it)
``~~cache/`` the path to application cache data (``~/.cache/mpv/``)
On some platforms, this will be the same as ``~~home/``.
``~~state/`` the path to application state data (``~/.local/state/mpv/``)
On some platforms, this will be the same as ``~~home/``.
``~~old_home/`` do not use
Expand Down
25 changes: 12 additions & 13 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4770,8 +4770,6 @@ Cache
This makes sense only with ``--cache``. If the normal cache is disabled,
this option is ignored.

You need to set ``--cache-dir`` to use this.

The cache file is append-only. Even if the player appears to prune data, the
file space freed by it is not reused. The cache file is deleted when
playback is closed.
Expand All @@ -4794,9 +4792,9 @@ Cache
continue to use the cache file that was opened first.

``--cache-dir=<path>``
Directory where to create temporary files (default: none).

Currently, this is used for ``--cache-on-disk`` only.
Directory where to create temporary files (usually ``~/.cache/mpv``).
This is used for ``--cache-on-disk``, ``--icc-cache``, and
``--gpu-shader-cache``.

``--cache-pause=<yes|no>``
Whether the player should automatically pause when the cache runs out of
Expand Down Expand Up @@ -6633,11 +6631,12 @@ them.
Applications using libmpv with the render API need to provide the ICC
profile via ``MPV_RENDER_PARAM_ICC_PROFILE``.

``--icc-cache-dir=<dirname>``
Store and load the 3D LUTs created from the ICC profile in this directory.
This can be used to speed up loading, since LittleCMS 2 can take a while to
create a 3D LUT. Note that these files contain uncompressed LUTs. Their
size depends on the ``--icc-3dlut-size``, and can be very big.
``--icc-cache``
Store and load 3D LUTs created from the ICC profile on disk in the
cache directory. This can be used to speed up loading, since LittleCMS
2 can take a while to create a 3D LUT. Note that these files contain
uncompressed LUTs. Their size depends on the ``--icc-3dlut-size``, and
can be very big.

NOTE: This is not cleaned automatically, so old, unused cache files may
stick around indefinitely.
Expand Down Expand Up @@ -6774,9 +6773,9 @@ them.

This option might be silently removed in the future.

``--gpu-shader-cache-dir=<dirname>``
Store and load compiled GLSL shaders in this directory. Normally, shader
compilation is very fast, so this is usually not needed. It mostly matters
``--gpu-shader-cache``
Store and load compiled GLSL shaders in the cache directory. Normally, shader
compilation is very fast, so this is not usually needed. It mostly matters
for GPU APIs that require internally recompiling shaders to other languages,
for example anything based on ANGLE or Vulkan. Enabling this can improve
startup performance on these platforms.
Expand Down
2 changes: 2 additions & 0 deletions common/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ struct mpv_global {
struct mp_client_api *client_api;
struct stats_base *stats;

bool no_cachedir;
bool no_statedir;
char *cachedir;
char *configdir;
char *statedir;
};
Expand Down
10 changes: 3 additions & 7 deletions demux/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@
#include "osdep/io.h"

struct demux_cache_opts {
char *cache_dir;
int unlink_files;
};

#define OPT_BASE_STRUCT struct demux_cache_opts

const struct m_sub_options demux_cache_conf = {
.opts = (const struct m_option[]){
{"cache-dir", OPT_STRING(cache_dir), .flags = M_OPT_FILE},
{"cache-unlink-files", OPT_CHOICE(unlink_files,
{"immediate", 2}, {"whendone", 1}, {"no", 0}),
},
Expand Down Expand Up @@ -99,11 +97,9 @@ struct demux_cache *demux_cache_create(struct mpv_global *global,
cache->log = log;
cache->fd = -1;

char *cache_dir = cache->opts->cache_dir;
if (!(cache_dir && cache_dir[0])) {
MP_ERR(cache, "No cache data directory supplied.\n");
goto fail;
}
char *cache_dir = mp_find_user_file(NULL, global, "cache", "");
if (cache_dir && cache_dir[0])
mp_mkdirp(cache_dir);

cache->filename = mp_path_join(cache, cache_dir, "mpv-cache-XXXXXX.dat");
cache->fd = mp_mkostemps(cache->filename, 4, O_CLOEXEC);
Expand Down
2 changes: 2 additions & 0 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ static const m_option_t mp_opts[] = {
{"config", OPT_BOOL(load_config), .flags = CONF_PRE_PARSE},
{"config-dir", OPT_STRING(force_configdir),
.flags = CONF_NOCFG | CONF_PRE_PARSE | M_OPT_FILE},
{"cache-dir", OPT_STRING(force_cachedir),
.flags = CONF_PRE_PARSE | M_OPT_FILE},
{"reset-on-next-file", OPT_STRINGLIST(reset_options)},

#if HAVE_LUA || HAVE_JAVASCRIPT || HAVE_CPLUGINS
Expand Down
1 change: 1 addition & 0 deletions options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ typedef struct MPOpts {
bool quiet;
bool load_config;
char *force_configdir;
char *force_cachedir;
bool use_filedir_conf;
int hls_bitrate;
int edition_id;
Expand Down
14 changes: 14 additions & 0 deletions options/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@ static const char *mp_get_platform_path(void *talloc_ctx,
}
}

if (global->cachedir && global->cachedir[0] && strcmp(type, "cache") == 0)
return global->cachedir;

if (global->statedir && global->statedir[0] && strcmp(type, "state") == 0)
return global->statedir;

// Return the native config path if the platform doesn't support the
// type we are trying to fetch.
if (strcmp(type, "cache") == 0 && global->no_cachedir)
type = "home";
if (strcmp(type, "state") == 0 && global->no_statedir)
type = "home";

Expand All @@ -104,28 +109,37 @@ static const char *mp_get_platform_path(void *talloc_ctx,
void mp_init_paths(struct mpv_global *global, struct MPOpts *opts)
{
TA_FREEP(&global->configdir);
TA_FREEP(&global->cachedir);
TA_FREEP(&global->statedir);

// Check if the platform has unique directories that differ from
// the standard config directory.
void *tmp = talloc_new(NULL);
const char *cache = mp_get_platform_path(tmp, global, "cache");
const char *state = mp_get_platform_path(tmp, global, "state");
if (!cache)
global->no_cachedir = true;
if (!state)
global->no_statedir = true;
talloc_free(tmp);

const char *force_configdir = getenv("MPV_HOME");
const char *force_cachedir = "";
const char *force_statedir = "";
if (opts->force_configdir && opts->force_configdir[0])
force_configdir = opts->force_configdir;
if (opts->force_cachedir && opts->force_cachedir[0])
force_cachedir = opts->force_cachedir;
if (opts->watch_later_directory && opts->watch_later_directory[0])
force_statedir = opts->watch_later_directory;
if (!opts->load_config) {
force_configdir = "";
force_cachedir = "";
force_statedir = "";
}

global->configdir = talloc_strdup(global, force_configdir);
global->cachedir = talloc_strdup(global, force_cachedir);
global->statedir = talloc_strdup(global, force_statedir);
}

Expand Down
11 changes: 11 additions & 0 deletions osdep/path-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ static pthread_once_t path_init_once = PTHREAD_ONCE_INIT;

static char mpv_home[512];
static char old_home[512];
static char mpv_cache[512];
static char mpv_state[512];

static void path_init(void)
{
char *home = getenv("HOME");
char *xdg_cache = getenv("XDG_CACHE_HOME");
char *xdg_config = getenv("XDG_CONFIG_HOME");
char *xdg_state = getenv("XDG_STATE_HOME");

Expand All @@ -45,6 +47,12 @@ static void path_init(void)
if (home && home[0])
snprintf(old_home, sizeof(old_home), "%s/.mpv", home);

if (xdg_cache && xdg_cache[0]) {
snprintf(mpv_cache, sizeof(mpv_cache), "%s/mpv", xdg_cache);
} else if (home && home[0]) {
snprintf(mpv_cache, sizeof(mpv_cache), "%s/.cache/mpv", home);
}

if (xdg_state && xdg_state[0]) {
snprintf(mpv_state, sizeof(mpv_state), "%s/mpv", xdg_state);
} else if (home && home[0]) {
Expand All @@ -55,6 +63,7 @@ static void path_init(void)
// config dir only. Also do not use any other XDG directories.
if (mp_path_exists(old_home) && !mp_path_exists(mpv_home)) {
snprintf(mpv_home, sizeof(mpv_home), "%s", old_home);
snprintf(mpv_cache, sizeof(mpv_cache), "%s", old_home);
snprintf(mpv_state, sizeof(mpv_state), "%s", old_home);
old_home[0] = '\0';
}
Expand All @@ -67,6 +76,8 @@ const char *mp_get_platform_path_unix(void *talloc_ctx, const char *type)
return mpv_home;
if (strcmp(type, "old_home") == 0)
return old_home;
if (strcmp(type, "cache") == 0)
return mpv_cache;
if (strcmp(type, "state") == 0)
return mpv_state;
if (strcmp(type, "global") == 0)
Expand Down
1 change: 1 addition & 0 deletions osdep/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// implemented on every platform. Unlike some other type values that are
// platform specific (like "osxbundle"), the value of "home" is returned
// instead if these types are not explicitly defined.
// "cache" the native mpv-specific user cache dir
// "state" the native mpv-specific user state dir
//
// It is allowed to return a static string, so the caller must set talloc_ctx
Expand Down
7 changes: 4 additions & 3 deletions video/out/gpu/lcms.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d,
cmsContext cms = NULL;

char *cache_file = NULL;
if (p->opts->cache_dir && p->opts->cache_dir[0]) {
if (p->opts->cache) {
// Gamma is included in the header to help uniquely identify it,
// because we may change the parameter in the future or make it
// customizable, same for the primaries.
Expand All @@ -352,7 +352,7 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d,
av_sha_final(sha, hash);
av_free(sha);

char *cache_dir = mp_get_user_path(tmp, p->global, p->opts->cache_dir);
char *cache_dir = mp_find_user_file(NULL, p->global, "cache", "");
cache_file = talloc_strdup(tmp, "");
for (int i = 0; i < sizeof(hash); i++)
cache_file = talloc_asprintf_append(cache_file, "%02X", hash[i]);
Expand Down Expand Up @@ -494,13 +494,14 @@ const struct m_sub_options mp_icc_conf = {
{"use-embedded-icc-profile", OPT_BOOL(use_embedded)},
{"icc-profile", OPT_STRING(profile), .flags = M_OPT_FILE},
{"icc-profile-auto", OPT_BOOL(profile_auto)},
{"icc-cache", OPT_BOOL(cache)},
{"icc-cache-dir", OPT_STRING(cache_dir), .flags = M_OPT_FILE},
{"icc-intent", OPT_INT(intent)},
{"icc-force-contrast", OPT_CHOICE(contrast, {"no", 0}, {"inf", -1}),
M_RANGE(0, 1000000)},
{"icc-3dlut-size", OPT_STRING_VALIDATE(size_str, validate_3dlut_size_opt)},
{"3dlut-size", OPT_REPLACED("icc-3dlut-size")},
{"icc-cache", OPT_REMOVED("see icc-cache-dir")},
{"icc-cache-dir", OPT_REPLACED("cache-dir")},
{"icc-contrast", OPT_REMOVED("see icc-force-contrast")},
{0}
},
Expand Down
1 change: 1 addition & 0 deletions video/out/gpu/lcms.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct mp_icc_opts {
bool use_embedded;
char *profile;
bool profile_auto;
bool cache;
char *cache_dir;
char *size_str;
int intent;
Expand Down
6 changes: 4 additions & 2 deletions video/out/gpu/shader_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,12 @@ static void update_uniform(struct gl_shader_cache *sc, struct sc_entry *e,
}
}

void gl_sc_set_cache_dir(struct gl_shader_cache *sc, const char *dir)
void gl_sc_set_cache_dir(struct gl_shader_cache *sc)
{
talloc_free(sc->cache_dir);
sc->cache_dir = talloc_strdup(sc, dir);
char *cache_dir = mp_find_user_file(NULL, sc->global, "cache", "");
mp_mkdirp(cache_dir);
sc->cache_dir = talloc_strdup(sc, cache_dir);
}

static bool create_pass(struct gl_shader_cache *sc, struct sc_entry *entry)
Expand Down
2 changes: 1 addition & 1 deletion video/out/gpu/shader_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ struct mp_pass_perf gl_sc_dispatch_compute(struct gl_shader_cache *sc,
// The application can call this on errors, to reset the current shader. This
// is normally done implicitly by gl_sc_dispatch_*
void gl_sc_reset(struct gl_shader_cache *sc);
void gl_sc_set_cache_dir(struct gl_shader_cache *sc, const char *dir);
void gl_sc_set_cache_dir(struct gl_shader_cache *sc);
8 changes: 5 additions & 3 deletions video/out/gpu/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,16 @@ const struct m_sub_options gl_video_conf = {
{"gpu-tex-pad-x", OPT_INT(tex_pad_x), M_RANGE(0, 4096)},
{"gpu-tex-pad-y", OPT_INT(tex_pad_y), M_RANGE(0, 4096)},
{"", OPT_SUBSTRUCT(icc_opts, mp_icc_conf)},
{"gpu-shader-cache-dir", OPT_STRING(shader_cache_dir), .flags = M_OPT_FILE},
{"gpu-shader-cache", OPT_BOOL(shader_cache)},
{"gpu-hwdec-interop",
OPT_STRING_VALIDATE(hwdec_interop, ra_hwdec_validate_opt)},
{"gpu-shader-cache-dir", OPT_REPLACED("cache-dir")},
{"opengl-hwdec-interop", OPT_REPLACED("gpu-hwdec-interop")},
{"hwdec-preload", OPT_REPLACED("opengl-hwdec-interop")},
{"hdr-tone-mapping", OPT_REPLACED("tone-mapping")},
{"opengl-shaders", OPT_REPLACED("glsl-shaders")},
{"opengl-shader", OPT_REPLACED("glsl-shader")},
{"opengl-shader-cache-dir", OPT_REPLACED("gpu-shader-cache-dir")},
{"opengl-shader-cache-dir", OPT_REPLACED("cache-dir")},
{"opengl-tex-pad-x", OPT_REPLACED("gpu-tex-pad-x")},
{"opengl-tex-pad-y", OPT_REPLACED("gpu-tex-pad-y")},
{"opengl-fbo-format", OPT_REPLACED("fbo-format")},
Expand Down Expand Up @@ -4099,7 +4100,8 @@ static void reinit_from_options(struct gl_video *p)

check_gl_features(p);
uninit_rendering(p);
gl_sc_set_cache_dir(p->sc, p->opts.shader_cache_dir);
if (p->opts.shader_cache)
gl_sc_set_cache_dir(p->sc);
p->ra->use_pbo = p->opts.pbo;
gl_video_setup_hooks(p);
reinit_osd(p);
Expand Down
1 change: 1 addition & 0 deletions video/out/gpu/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ struct gl_video_opts {
int tex_pad_x, tex_pad_y;
struct mp_icc_opts *icc_opts;
int early_flush;
bool shader_cache;
char *shader_cache_dir;
char *hwdec_interop;
};
Expand Down
8 changes: 4 additions & 4 deletions video/out/vo_gpu_next.c
Original file line number Diff line number Diff line change
Expand Up @@ -1348,10 +1348,10 @@ static void wait_events(struct vo *vo, int64_t until_time_us)
static char *get_cache_file(struct priv *p)
{
struct gl_video_opts *opts = p->opts_cache->opts;
if (!opts->shader_cache_dir || !opts->shader_cache_dir[0])
if (!opts->shader_cache)
return NULL;

char *dir = mp_get_user_path(NULL, p->global, opts->shader_cache_dir);
char *dir = mp_find_user_file(NULL, p->global, "cache", "");
char *file = mp_path_join(NULL, dir, "libplacebo.cache");
mp_mkdirp(dir);
talloc_free(dir);
Expand Down Expand Up @@ -1575,7 +1575,7 @@ static const struct pl_hook *load_hook(struct priv *p, const char *path)
static stream_t *icc_open_cache(struct priv *p, uint64_t sig, int flags)
{
const struct gl_video_opts *opts = p->opts_cache->opts;
if (!opts->icc_opts->cache_dir || !opts->icc_opts->cache_dir[0])
if (!opts->icc_opts->cache)
return NULL;

char cache_name[16+1];
Expand All @@ -1585,7 +1585,7 @@ static stream_t *icc_open_cache(struct priv *p, uint64_t sig, int flags)
}
cache_name[16] = '\0';

char *cache_dir = mp_get_user_path(NULL, p->global, opts->icc_opts->cache_dir);
char *cache_dir = mp_find_user_file(NULL, p->global, "cache", "");
char *path = mp_path_join(NULL, cache_dir, cache_name);

stream_t *stream = NULL;
Expand Down

0 comments on commit 59ac119

Please sign in to comment.