Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
pixelpipe_cache.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2009-2010 johannes hanika.
4 Copyright (C) 2010-2011 Henrik Andersson.
5 Copyright (C) 2012 Richard Wonka.
6 Copyright (C) 2012, 2014, 2016 Tobias Ellinghaus.
7 Copyright (C) 2014 Ulrich Pegelow.
8 Copyright (C) 2016 Roman Lebedev.
9 Copyright (C) 2020 Pascal Obry.
10 Copyright (C) 2020 Ralf Brown.
11 Copyright (C) 2022 Hanno Schwalm.
12 Copyright (C) 2022 Martin Bařinka.
13 Copyright (C) 2023, 2025-2026 Aurélien PIERRE.
14
15 darktable is free software: you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19
20 darktable is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with darktable. If not, see <http://www.gnu.org/licenses/>.
27*/
28
29#ifndef DT_CACHES_PIXELPIPE_CACHE_H
30#define DT_CACHES_PIXELPIPE_CACHE_H
31
32#include "system/memory_arena.h"
33#include "system/atomic.h"
34#include "pixel/format.h"
35#include <inttypes.h>
36#include <glib.h>
37#include <stddef.h>
38
39/* Consumed by C++ translation units (iop/lens.cc, iop/bilateral.cc, ...) through
40 * develop/pixelpipe_cache_alloc.h. Without this guard those TUs mangle the declarations
41 * as C++ and fail to link against the C definitions -- invisible on ELF, which permits
42 * undefined symbols in shared objects, but fatal on Mach-O. */
43#ifdef __cplusplus
44extern "C" {
45#endif
46
48struct dt_iop_module_t;
49struct dt_iop_roi_t;
50
51#define DT_PIXELPIPE_CACHE_HASH_INVALID ((uint64_t)-1)
52
53
64/* Opaque, and there is no accessor: no function below takes a cache handle. */
66
74
92gboolean dt_dev_pixelpipe_cache_init(size_t max_memory, const gboolean verbose,
93 const gboolean verbose_detail);
94
103
104// One pipeline-cache entry, for the GUI memory view.
106{
108 size_t size; // host (RAM) bytes
110 int hits;
111 int cl_count; // number of OpenCL device buffers attached to this entry
112 size_t cl_bytes; // their total vRAM bytes (0 without OpenCL)
113 char name[64];
115
116// Current/max bytes used by the pipeline cache (host RAM).
117void dt_dev_pixelpipe_cache_get_usage(size_t *current, size_t *max);
118
119/* Largest contiguous free run in the arena (bytes): the real upper bound on
120 * what an allocation — and, transitively, a tiled module's working set — can
121 * get, as opposed to the byte headroom max - current. */
123
124// Total device memory across enabled OpenCL devices (0 if OpenCL is off/absent),
125// for the vRAM usage bar's denominator.
127
128// Snapshot of all live entries (newly-allocated GArray of
129// dt_pixel_cache_stats_entry_t; free with g_array_free()).
131
132/* Public for by-value snapshots in pipeline pieces (for example realtime
133 * output cacheline reuse/rekey). Ownership still belongs to pixelpipe_cache.
134 * External code must treat this as metadata only and never free internals. */
135/* PUBLIC, and staying that way: develop/pixelpipe_hb.h embeds one BY VALUE in
136 * dt_dev_pixelpipe_iop_t, as a snapshot of the last reusable cacheline's metadata. That makes
137 * it a value type the pipeline carries, like dt_mipmap_buffer_t -- not the cache's internal
138 * bookkeeping, which is dt_cache_entry_t and is private to this module. */
140{
141 uint64_t hash; // unique identifier of the entry
142 uint64_t serial; // stable identity across rekeys, changes on fresh allocations
143 void *data; // buffer holding pixels... or anything else
144 size_t size; // size of the data buffer
145 /* MRU timestamp, in `g_get_monotonic_time()` microseconds. Set when the entry is created AND
146 * refreshed on every hit, so the eviction sweeps age an entry by its LAST USE rather than by
147 * its creation -- otherwise a cacheline consumed on every frame looks like the oldest thing in
148 * the cache and gets evicted first. Written from paths that hold no cache lock (finalize, the
149 * OpenCL payload reuse under `cl_mem_lock` only) and read by the sweeps under `cache->lock`,
150 * hence atomic. Touched exclusively through `_pixel_cache_touch()` in pixelpipe_cache.c. */
151 dt_atomic_uint64 age;
152 char *name; // name of the cache entry, for debugging
153 int id; // id of the pipeline owning this entry. Used when flushing, a pipe can only flush its own.
154 uint64_t producer_node_key; // stable identity of the pipeline node that produced this output
155 // (dt_supervisor_node_key(pipe_type, op, multi_priority)), or INVALID.
156 // Travels with DT_SIGNAL_CACHELINE_READY so GUI waiters can match by
157 // producer node even when the exact output hash drifted. See
158 // doc/pipeline-cache.md §8.
159 dt_atomic_int refcount; // reference count for the cache entry, to avoid freeing it while still in use
160 dt_pthread_rwlock_t lock; // read/write lock to avoid threads conflicts
161 gboolean auto_destroy; // TRUE for auto-destruction the next time it's used. Used for short-lived entries (transient states).
162 gboolean external_alloc; // TRUE for external buffers tracked in the cache
163 /* Number of ASYNCHRONOUS REUSES of this entry: how many times a lookup found content someone
164 * else had already published, in another run or another pipe. The intra-run producer->consumer
165 * handoff is NOT counted -- a module publishing its output with a reference reserved for the
166 * next module, which then reopens and releases it, is plain pipeline passthrough, not a reuse.
167 * Written only under `cache->lock`, alongside the cache-wide `hits`/`queries` rate. Distinct
168 * from `age`, which every use refreshes: see `_pixel_cache_touch()` in pixelpipe_cache.c. */
169 int hits;
170 dt_dev_pixelpipe_cache_t *cache; // reference to parent cache object
171 GList *cl_mem_list; // reusable OpenCL pinned buffers tied to this entry
172 dt_pthread_mutex_t cl_mem_lock;
174
181const char *dt_pixelpipe_cache_set_current_module(const char *module);
182
193
194/*
195 * @brief Find a cache entry that holds the exact data buffer pointer `data`.
196 *
197 * This searches both regular and external cache tables under the cache mutex.
198 * It does not change refcounts or locks on the returned entry; the caller must
199 * manage lifetime if needed.
200 */
202
203
227int dt_dev_pixelpipe_cache_get(const uint64_t hash, const size_t size,
228 const char *name, const int id, const gboolean alloc,
229 void **data,
230 struct dt_pixel_cache_entry_t **entry);
231
271 const size_t size, const char *name, const int id,
272 const gboolean alloc, const gboolean allow_rekey_reuse,
273 const struct dt_pixel_cache_entry_t *reuse_hint,
274 void **data,
275 struct dt_pixel_cache_entry_t **entry);
276
295 int devid, int width, int height, int bpp);
296
304
314
337 int preferred_devid, void **data);
338
361void *dt_dev_pixelpipe_cache_get_pinned_image(void *host_ptr,
362 struct dt_pixel_cache_entry_t *entry_hint, int devid,
363 int width, int height, int bpp, int flags,
364 gboolean *out_reused);
365
380 struct dt_pixel_cache_entry_t *entry_hint, void **mem);
381
398 struct dt_pixel_cache_entry_t *entry_hint, int devid);
399
421void *dt_dev_pixelpipe_cache_get_cl_buffer(int devid, void *host_ptr, const struct dt_iop_roi_t *roi,
422 size_t bpp, struct dt_iop_module_t *module,
423 const char *message, struct dt_pixel_cache_entry_t *entry,
424 gboolean *out_reused, void *keep);
425
437void *dt_dev_pixelpipe_cache_alloc_cl_device_buffer(int devid, const struct dt_iop_roi_t *roi, size_t bpp,
438 const struct dt_iop_module_t *module,
439 const char *message, void *keep);
440
453void dt_dev_pixelpipe_cache_release_cl_buffer(void **cl_mem_buffer, struct dt_pixel_cache_entry_t *entry,
454 void *host_ptr, gboolean cache_device);
455
469int dt_dev_pixelpipe_cache_sync_cl_buffer(int devid, void *host_ptr, void *cl_mem_buffer,
470 const struct dt_iop_roi_t *roi, int cl_mode, size_t bpp,
471 struct dt_iop_module_t *module, const char *message);
472
487 void *cl_mem_input, const struct dt_iop_roi_t *roi_in,
488 struct dt_iop_module_t *module, size_t in_bpp,
489 struct dt_pixel_cache_entry_t *input_entry,
490 const char *message);
491
515 struct dt_iop_module_t *module,
516 float *input, void **cl_mem_input,
517 const struct dt_iop_roi_t *roi_in, size_t in_bpp,
518 struct dt_pixel_cache_entry_t *input_entry,
519 struct dt_pixel_cache_entry_t **locked_input_entry,
520 void *keep);
521
539
560 void **data,
561 struct dt_pixel_cache_entry_t **entry);
562
587 void **data,
588 struct dt_pixel_cache_entry_t **entry);
589
592
603
604
615
627 const char *name);
628
635void dt_pixelpipe_cache_free_align_cache(void **mem, const char *message);
636
637
638
653gboolean dt_dev_pixelpipe_cache_peek(const uint64_t hash, void **data,
654 struct dt_pixel_cache_entry_t **entry, const int preferred_devid,
655 void **cl_mem_output);
656
664void dt_dev_pixelpipe_cache_flush(const int id);
665
682 const size_t count);
683
699void dt_dev_pixelpipe_cache_flush_clmem(const int devid);
700
708
709
718int dt_dev_pixelpipe_cache_remove(const gboolean force,
719 struct dt_pixel_cache_entry_t *entry);
720
721
724
729
741 struct dt_pixel_cache_entry_t *entry);
742
750 struct dt_pixel_cache_entry_t *entry);
751
752
761 struct dt_pixel_cache_entry_t *entry);
762
763
774
786
794
815int dt_dev_pixelpipe_cache_rekey(const uint64_t old_hash,
816 const uint64_t new_hash, struct dt_pixel_cache_entry_t *entry);
817
818/* --- Telling the rest of the application things ---------------------------
819 *
820 * The cache has three reasons to speak upward: warn the user that it is full, announce that a
821 * cacheline became ready so a waiter can stop waiting, and feed the supervisor its bookkeeping.
822 * Every one of those used to be a direct call -- dt_control_log(), a raised
823 * DT_SIGNAL_CACHELINE_READY, dt_supervisor_*() -- which put control/ and develop/ headers in a
824 * module that is otherwise pure storage, and made the cache depend on the application rather
825 * than the other way round.
826 *
827 * They are handlers now, installed once by the orchestrator. Unset handlers are simply not
828 * called, so the cache works in a build or a test that installs none. Same shape as
829 * dt_colorspaces_set_profile_changed_handler().
830 */
831
834typedef void (*dt_pixelpipe_cache_warn_handler_t)(const char *message);
835
839
843{
844 gboolean (*active)(void);
846 void (*cacheline_delete)(uint64_t hash, size_t size, int owner_pipe_id, const char *name);
847 void (*rekey)(uint64_t old_hash, uint64_t new_hash);
849
858 const dt_pixelpipe_cache_observer_t *observer);
859
860
861#ifdef __cplusplus
862}
863#endif
864
865#endif // DT_CACHES_PIXELPIPE_CACHE_H
866
867// clang-format off
868// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
869// vim: shiftwidth=2 expandtab tabstop=2 cindent
870// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
871// clang-format on
atomic_int dt_atomic_int
Definition atomic.h:68
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
const float max
int bpp
uint32_t width
Definition mipmap_cache.c:0
uint32_t height
Definition mipmap_cache.c:1
size_t size
Definition mipmap_cache.c:3
dt_mipmap_buffer_dsc_flags flags
Definition mipmap_cache.c:4
const char * name
Definition pdf.h:90
gboolean dt_dev_pixelpipe_cache_flush_host_pinned_image(void *host_ptr, struct dt_pixel_cache_entry_t *entry_hint, int devid)
Drop cached pinned OpenCL images associated with a given host buffer.
void dt_dev_pixelpipe_cache_ref_count_entry(gboolean lock, struct dt_pixel_cache_entry_t *entry)
Increase/Decrease the reference count on the cache line as to prevent LRU item removal....
void * dt_dev_pixelpipe_cache_get_cl_buffer(int devid, void *host_ptr, const struct dt_iop_roi_t *roi, size_t bpp, struct dt_iop_module_t *module, const char *message, struct dt_pixel_cache_entry_t *entry, gboolean *out_reused, void *keep)
Allocate or reuse an OpenCL buffer for one cache entry payload.
GArray * dt_dev_pixelpipe_cache_get_entries_stats(void)
int dt_dev_pixelpipe_cache_sync_cl_buffer(int devid, void *host_ptr, void *cl_mem_buffer, const struct dt_iop_roi_t *roi, int cl_mode, size_t bpp, struct dt_iop_module_t *module, const char *message)
Synchronize between host memory and a pinned OpenCL image.
int dt_dev_pixelpipe_cache_invalidate_hashes(const uint64_t *hashes, const size_t count)
Invalidate cache lines matching an explicit list of hashes.
size_t dt_dev_pixelpipe_cache_get_vram_total(void)
gboolean dt_dev_pixelpipe_cache_peek(const uint64_t hash, void **data, struct dt_pixel_cache_entry_t **entry, const int preferred_devid, void **cl_mem_output)
Non-owning lookup of an existing cache line.
void * dt_dev_pixelpipe_cache_borrow_cl_payload(struct dt_pixel_cache_entry_t *entry, int devid, int width, int height, int bpp)
Borrow a cached OpenCL payload attached to a cache entry.
void dt_dev_pixelpipe_cache_cleanup(void)
void * dt_pixelpipe_cache_alloc_align_cache_impl(size_t size, int id, const char *name)
Allocate aligned memory tracked by the pixelpipe cache. This allows LRU cache entries to be evicted i...
void dt_dev_pixelpipe_cache_auto_destroy_apply(struct dt_pixel_cache_entry_t *entry)
Free the entry if it has the flag "auto_destroy". See dt_dev_pixelpipe_cache_flag_auto_destroy()....
struct dt_pixel_cache_entry_t * dt_dev_pixelpipe_cache_get_entry_by_data(void *data)
int dt_dev_pixelpipe_cache_remove(const gboolean force, struct dt_pixel_cache_entry_t *entry)
Arbitrarily remove the cache entry matching hash. Entries having a reference count > 0 (inter-thread ...
void dt_dev_pixelpipe_cache_flush_clmem(const int devid)
Release cached OpenCL buffers for a single device.
int dt_dev_pixel_pipe_cache_remove_lru(void)
void(* dt_pixelpipe_cache_warn_handler_t)(const char *message)
Tell the user something went wrong. Called with an already-translated, already-formatted string; the ...
void dt_dev_pixelpipe_cache_wrlock_entry(gboolean lock, struct dt_pixel_cache_entry_t *entry)
Lock or release the write lock on the entry.
void dt_dev_pixelpipe_cache_put_pinned_image(void *host_ptr, struct dt_pixel_cache_entry_t *entry_hint, void **mem)
Release or cache a pinned OpenCL image acquired with dt_dev_pixelpipe_cache_get_pinned_image().
int dt_dev_pixelpipe_cache_prepare_cl_input(struct dt_dev_pixelpipe_t *pipe, struct dt_iop_module_t *module, float *input, void **cl_mem_input, const struct dt_iop_roi_t *roi_in, size_t in_bpp, struct dt_pixel_cache_entry_t *input_entry, struct dt_pixel_cache_entry_t **locked_input_entry, void *keep)
Prepare the OpenCL input image corresponding to one cache-backed module input.
float * dt_dev_pixelpipe_cache_restore_cl_buffer(struct dt_dev_pixelpipe_t *pipe, float *input, void *cl_mem_input, const struct dt_iop_roi_t *roi_in, struct dt_iop_module_t *module, size_t in_bpp, struct dt_pixel_cache_entry_t *input_entry, const char *message)
Resynchronize one OpenCL input payload back into its cache-backed host buffer.
void dt_dev_pixelpipe_cache_flush_clmem_for_pipe(const int devid)
Like dt_dev_pixelpipe_cache_flush_clmem(), for callers that do not hold the device lock (dt_opencl_re...
void dt_dev_pixelpipe_cache_flush_entry_clmem(struct dt_pixel_cache_entry_t *entry)
Flush all reusable OpenCL payloads cached on one cache entry.
struct dt_pixel_cache_entry_t * dt_dev_pixelpipe_cache_ref_entry_for_host_ptr(void *host_ptr)
Resolve and retain the cache entry owning a host pointer.
void dt_dev_pixelpipe_cache_flush(const int id)
Remove cache lines matching id. Entries locked in read/write or having reference count greater than 0...
void dt_dev_pixelpipe_cache_print(void)
size_t dt_pixelpipe_cache_get_largest_free_run(void)
void * dt_pixel_cache_entry_get_data(struct dt_pixel_cache_entry_t *entry)
void * dt_dev_pixelpipe_cache_get_pinned_image(void *host_ptr, struct dt_pixel_cache_entry_t *entry_hint, int devid, int width, int height, int bpp, int flags, gboolean *out_reused)
Acquire a pinned OpenCL image for a host buffer tracked by the pixelpipe cache.
gboolean dt_dev_pixelpipe_cache_ref_host_entry_by_hash(const uint64_t hash, void **data, struct dt_pixel_cache_entry_t **entry)
Resolve and retain an existing cacheline that already holds HOST pixels.
size_t dt_pixel_cache_entry_get_size(struct dt_pixel_cache_entry_t *entry)
Peek the size (in bytes) reserved for the host buffer of a cache entry.
gboolean dt_dev_pixelpipe_cache_ref_entry_by_hash(const uint64_t hash, void **data, struct dt_pixel_cache_entry_t **entry)
Resolve and retain an existing cache entry by hash.
void dt_dev_pixelpipe_cache_release_cl_buffer(void **cl_mem_buffer, struct dt_pixel_cache_entry_t *entry, void *host_ptr, gboolean cache_device)
Release or cache an OpenCL image associated with one cache entry.
void dt_dev_pixelpipe_cache_get_usage(size_t *current, size_t *max)
void(* dt_pixelpipe_cache_ready_handler_t)(uint64_t hash, uint64_t producer_node_key)
A cacheline finished and is readable.
void dt_dev_pixelpipe_cache_rdlock_entry(gboolean lock, struct dt_pixel_cache_entry_t *entry)
Lock or release the read lock on the entry.
void * dt_pixel_cache_alloc(struct dt_pixel_cache_entry_t *entry)
Actually allocate the memory buffer attached to the cache entry once you create it with dt_dev_pixelp...
void dt_dev_pixelpipe_cache_return_cl_payload(struct dt_pixel_cache_entry_t *entry, void *mem)
Return a borrowed cached OpenCL payload to its cache entry.
gboolean dt_dev_pixelpipe_cache_init(size_t max_memory, const gboolean verbose, const gboolean verbose_detail)
Reserve the cache's arena and start it.
struct dt_pixel_cache_entry_t * dt_dev_pixelpipe_cache_get_entry(const uint64_t hash)
Get an internal reference to the cache entry matching hash. If you are going to access this entry mor...
int dt_dev_pixelpipe_cache_rekey(const uint64_t old_hash, const uint64_t new_hash, struct dt_pixel_cache_entry_t *entry)
Change the hash/key of an existing cache line in place, without freeing, reallocating or invalidating...
void dt_dev_pixelpipe_cache_unref_hash(const uint64_t hash)
Find the entry matching hash, and decrease its ref_count if found.
void * dt_dev_pixelpipe_cache_alloc_cl_device_buffer(int devid, const struct dt_iop_roi_t *roi, size_t bpp, const struct dt_iop_module_t *module, const char *message, void *keep)
Allocate a temporary device-only OpenCL image, retrying once after cache flush.
void dt_pixelpipe_cache_free_align_cache(void **mem, const char *message)
Free aligned memory allocated with dt_pixelpipe_cache_alloc_align_cache.
dt_dev_pixelpipe_cache_writable_status_t dt_dev_pixelpipe_cache_get_writable(const uint64_t hash, const size_t size, const char *name, const int id, const gboolean alloc, const gboolean allow_rekey_reuse, const struct dt_pixel_cache_entry_t *reuse_hint, void **data, struct dt_pixel_cache_entry_t **entry)
Acquire a writable cache line for module output.
gboolean dt_dev_pixelpipe_cache_restore_host_payload(struct dt_pixel_cache_entry_t *entry, int preferred_devid, void **data)
Materialize a host payload for a live cache entry from its cached device payload.
void dt_dev_pixelpipe_cache_set_handlers(dt_pixelpipe_cache_warn_handler_t warn, dt_pixelpipe_cache_ready_handler_t ready, const dt_pixelpipe_cache_observer_t *observer)
Install the handlers. Call once, from the orchestrator, before any pipe runs.
void dt_dev_pixelpipe_cache_flag_auto_destroy(struct dt_pixel_cache_entry_t *entry)
Flag the cache entry as "auto_destroy". This is useful for short-lived/disposable cache entries,...
int dt_dev_pixelpipe_cache_get(const uint64_t hash, const size_t size, const char *name, const int id, const gboolean alloc, void **data, struct dt_pixel_cache_entry_t **entry)
Get a cache line from the cache.
dt_dev_pixelpipe_cache_writable_status_t
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_REKEYED
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_ERROR
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_CREATED
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_EXACT_HIT
const char * dt_pixelpipe_cache_set_current_module(const char *module)
Set the current module name for cache diagnostics (thread-local).
gboolean dt_dev_pixelpipe_cache_is_ready(void)
Has the pixelpipe cache been initialised? Callers that run before dt_dev_pixelpipe_cache_init() succe...
unsigned __int64 uint64_t
Definition strptime.c:75
Region of interest passed through the pixelpipe.
Definition format.h:49
uint64_t hash
gboolean auto_destroy
dt_atomic_int refcount
gboolean external_alloc
void * data
dt_atomic_uint64 age
size_t size
uint64_t serial
dt_dev_pixelpipe_cache_t * cache
dt_pthread_rwlock_t lock
dt_pthread_mutex_t cl_mem_lock
GList * cl_mem_list
char * name
int hits
int id
uint64_t producer_node_key
int refcount
char name[64]
int hits
size_t size
size_t cl_bytes
uint64_t hash
int cl_count
The supervisor's view of the cache. All four may be NULL; active gates the other three so the cache p...
void(* cacheline_delete)(uint64_t hash, size_t size, int owner_pipe_id, const char *name)
void(* cacheline_read)(uint64_t hash, size_t size)
void(* rekey)(uint64_t old_hash, uint64_t new_hash)
dt_pthread_mutex_t lock
Definition supervisor.c:123