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#pragma once
30
31#include "common/memory_arena.h"
32#include "common/atomic.h"
33#include "develop/format.h"
34#include <inttypes.h>
35#include <glib.h>
36#include <stddef.h>
37
39struct dt_iop_module_t;
40struct dt_iop_roi_t;
41
42#define DT_PIXELPIPE_CACHE_HASH_INVALID ((uint64_t)-1)
43
44
56{
57 GHashTable *entries;
58 // External (temporary) buffers keyed by address hash, separate from pipeline cache entries.
59 GHashTable *external_entries;
63 size_t max_memory;
65 dt_pthread_mutex_t lock; // mutex to protect the cache entries
68
76
82
83/* Public for by-value snapshots in pipeline pieces (for example realtime
84 * output cacheline reuse/rekey). Ownership still belongs to pixelpipe_cache.
85 * External code must treat this as metadata only and never free internals. */
87{
88 uint64_t hash; // unique identifier of the entry
89 uint64_t serial; // stable identity across rekeys, changes on fresh allocations
90 void *data; // buffer holding pixels... or anything else
91 size_t size; // size of the data buffer
92 int64_t age; // timestamp of creation. Oldest entry will be the first freed if it's not locked
93 char *name; // name of the cache entry, for debugging
94 int id; // id of the pipeline owning this entry. Used when flushing, a pipe can only flush its own.
95 dt_atomic_int refcount; // reference count for the cache entry, to avoid freeing it while still in use
96 dt_pthread_rwlock_t lock; // read/write lock to avoid threads conflicts
97 gboolean auto_destroy; // TRUE for auto-destruction the next time it's used. Used for short-lived entries (transient states).
98 gboolean external_alloc; // TRUE for external buffers tracked in the cache
99 int hits; // number of times this entry was hit (utility score)
100 dt_dev_pixelpipe_cache_t *cache; // reference to parent cache object
101 GList *cl_mem_list; // reusable OpenCL pinned buffers tied to this entry
102 dt_pthread_mutex_t cl_mem_lock;
104
111const char *dt_pixelpipe_cache_set_current_module(const char *module);
112
123 const uint64_t hash);
124
125/*
126 * @brief Find a cache entry that holds the exact data buffer pointer `data`.
127 *
128 * This searches both regular and external cache tables under the cache mutex.
129 * It does not change refcounts or locks on the returned entry; the caller must
130 * manage lifetime if needed.
131 */
133 void *data);
134
135
160 const char *name, const int id, const gboolean alloc,
161 void **data,
162 struct dt_pixel_cache_entry_t **entry);
163
203 const size_t size, const char *name, const int id,
204 const gboolean alloc, const gboolean allow_rekey_reuse,
205 const struct dt_pixel_cache_entry_t *reuse_hint,
206 void **data,
207 struct dt_pixel_cache_entry_t **entry);
208
227 int devid, int width, int height, int bpp);
228
236
246
269 struct dt_pixel_cache_entry_t *entry,
270 int preferred_devid, void **data);
271
295 struct dt_pixel_cache_entry_t *entry_hint, int devid,
296 int width, int height, int bpp, int flags,
297 gboolean *out_reused);
298
313 struct dt_pixel_cache_entry_t *entry_hint, void **mem);
314
331 struct dt_pixel_cache_entry_t *entry_hint, int devid);
332
354void *dt_dev_pixelpipe_cache_get_cl_buffer(int devid, void *host_ptr, const struct dt_iop_roi_t *roi,
355 size_t bpp, struct dt_iop_module_t *module,
356 const char *message, struct dt_pixel_cache_entry_t *entry,
357 gboolean *out_reused, void *keep);
358
370void *dt_dev_pixelpipe_cache_alloc_cl_device_buffer(int devid, const struct dt_iop_roi_t *roi, size_t bpp,
371 const struct dt_iop_module_t *module,
372 const char *message, void *keep);
373
386void dt_dev_pixelpipe_cache_release_cl_buffer(void **cl_mem_buffer, struct dt_pixel_cache_entry_t *entry,
387 void *host_ptr, gboolean cache_device);
388
402int dt_dev_pixelpipe_cache_sync_cl_buffer(int devid, void *host_ptr, void *cl_mem_buffer,
403 const struct dt_iop_roi_t *roi, int cl_mode, size_t bpp,
404 struct dt_iop_module_t *module, const char *message);
405
420 void *cl_mem_input, const struct dt_iop_roi_t *roi_in,
421 struct dt_iop_module_t *module, size_t in_bpp,
422 struct dt_pixel_cache_entry_t *input_entry,
423 const char *message);
424
448 struct dt_iop_module_t *module,
449 float *input, void **cl_mem_input,
450 const struct dt_iop_roi_t *roi_in, size_t in_bpp,
451 struct dt_pixel_cache_entry_t *input_entry,
452 struct dt_pixel_cache_entry_t **locked_input_entry,
453 void *keep);
454
472 void *host_ptr);
473
494 const uint64_t hash,
495 void **data,
496 struct dt_pixel_cache_entry_t **entry);
497
500
511
512
523
535 const char *name);
536
543void dt_pixelpipe_cache_free_align_cache(dt_dev_pixelpipe_cache_t *cache, void **mem, const char *message);
544
560 struct dt_pixel_cache_entry_t **entry, const int preferred_devid,
561 void **cl_mem_output);
562
571
588 const uint64_t *hashes,
589 const size_t count);
590
607
615
616
626 struct dt_pixel_cache_entry_t *entry);
627
628
631
636
648 struct dt_pixel_cache_entry_t *entry);
649
657 struct dt_pixel_cache_entry_t *entry);
658
659
668 struct dt_pixel_cache_entry_t *entry);
669
670
681 struct dt_pixel_cache_entry_t *entry);
682
694 struct dt_pixel_cache_entry_t *entry);
695
703
725 const uint64_t new_hash, struct dt_pixel_cache_entry_t *entry);
726
727// clang-format off
728// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
729// vim: shiftwidth=2 expandtab tabstop=2 cindent
730// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
731// clang-format on
atomic_int dt_atomic_int
Definition atomic.h:66
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
char * name
#define dt_pthread_rwlock_t
Definition dtpthread.h:389
int bpp
size_t size
Definition mipmap_cache.c:3
dt_mipmap_buffer_dsc_flags flags
Definition mipmap_cache.c:4
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.
void dt_dev_pixelpipe_cache_wrlock_entry(dt_dev_pixelpipe_cache_t *cache, gboolean lock, struct dt_pixel_cache_entry_t *entry)
Lock or release the write lock on the entry.
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.
struct dt_pixel_cache_entry_t * dt_dev_pixelpipe_cache_get_entry(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash)
Get an internal reference to the cache entry matching hash. If you are going to access this entry mor...
void dt_pixelpipe_cache_free_align_cache(dt_dev_pixelpipe_cache_t *cache, void **mem, const char *message)
Free aligned memory allocated with dt_pixelpipe_cache_alloc_align_cache.
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(dt_dev_pixelpipe_cache_t *cache)
int dt_dev_pixelpipe_cache_invalidate_hashes(dt_dev_pixelpipe_cache_t *cache, const uint64_t *hashes, const size_t count)
Invalidate cache lines matching an explicit list of hashes.
struct dt_pixel_cache_entry_t * dt_dev_pixelpipe_cache_get_entry_by_data(dt_dev_pixelpipe_cache_t *cache, void *data)
void dt_dev_pixelpipe_cache_auto_destroy_apply(dt_dev_pixelpipe_cache_t *cache, 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()....
dt_dev_pixelpipe_cache_t * dt_dev_pixelpipe_cache_init(size_t max_memory)
void dt_dev_pixelpipe_cache_print(dt_dev_pixelpipe_cache_t *cache)
dt_dev_pixelpipe_cache_writable_status_t dt_dev_pixelpipe_cache_get_writable(dt_dev_pixelpipe_cache_t *cache, 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.
void dt_dev_pixelpipe_cache_flush(dt_dev_pixelpipe_cache_t *cache, 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_unref_hash(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash)
Find the entry matching hash, and decrease its ref_count if found.
void dt_dev_pixelpipe_cache_flush_clmem_for_pipe(dt_dev_pixelpipe_cache_t *cache, const int devid)
Like dt_dev_pixelpipe_cache_flush_clmem(), for callers that do not hold darktable....
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_flag_auto_destroy(dt_dev_pixelpipe_cache_t *cache, struct dt_pixel_cache_entry_t *entry)
Flag the cache entry as "auto_destroy". This is useful for short-lived/disposable cache entries,...
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.
int dt_dev_pixel_pipe_cache_remove_lru(dt_dev_pixelpipe_cache_t *cache)
gboolean dt_dev_pixelpipe_cache_ref_entry_by_hash(dt_dev_pixelpipe_cache_t *cache, 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_put_pinned_image(dt_dev_pixelpipe_cache_t *cache, 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().
void * dt_pixel_cache_entry_get_data(struct dt_pixel_cache_entry_t *entry)
int dt_dev_pixelpipe_cache_remove(dt_dev_pixelpipe_cache_t *cache, 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_get_pinned_image(dt_dev_pixelpipe_cache_t *cache, 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.
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.
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_pixel_cache_alloc(dt_dev_pixelpipe_cache_t *cache, 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_rdlock_entry(dt_dev_pixelpipe_cache_t *cache, gboolean lock, struct dt_pixel_cache_entry_t *entry)
Lock or release the read lock on the entry.
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_flush_host_pinned_image(dt_dev_pixelpipe_cache_t *cache, 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_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.
gboolean dt_dev_pixelpipe_cache_peek(dt_dev_pixelpipe_cache_t *cache, 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_pixelpipe_cache_alloc_align_cache_impl(dt_dev_pixelpipe_cache_t *cache, 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_ref_count_entry(dt_dev_pixelpipe_cache_t *cache, gboolean lock, struct dt_pixel_cache_entry_t *entry)
Increase/Decrease the reference count on the cache line as to prevent LRU item removal....
struct dt_pixel_cache_entry_t * dt_dev_pixelpipe_cache_ref_entry_for_host_ptr(dt_dev_pixelpipe_cache_t *cache, void *host_ptr)
Resolve and retain the cache entry owning a host pointer.
void dt_dev_pixelpipe_cache_flush_clmem(dt_dev_pixelpipe_cache_t *cache, const int devid)
Release cached OpenCL buffers for a single device.
gboolean dt_dev_pixelpipe_cache_restore_host_payload(dt_dev_pixelpipe_cache_t *cache, 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.
int dt_dev_pixelpipe_cache_rekey(dt_dev_pixelpipe_cache_t *cache, 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...
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
int dt_dev_pixelpipe_cache_get(dt_dev_pixelpipe_cache_t *cache, 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.
const char * dt_pixelpipe_cache_set_current_module(const char *module)
Set the current module name for cache diagnostics (thread-local).
unsigned __int64 uint64_t
Definition strptime.c:75
dt_pthread_mutex_t lock
Region of interest passed through the pixelpipe.
Definition imageop.h:72
uint64_t hash
gboolean auto_destroy
dt_atomic_int refcount
gboolean external_alloc
void * data
size_t size
int64_t age
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