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_roi_t;
40
41#define DT_PIXELPIPE_CACHE_HASH_INVALID ((uint64_t)-1)
42
43
55{
56 GHashTable *entries;
57 // External (temporary) buffers keyed by address hash, separate from pipeline cache entries.
58 GHashTable *external_entries;
62 size_t max_memory;
64 dt_pthread_mutex_t lock; // mutex to protect the cache entries
67
75
81
82/* Public for by-value snapshots in pipeline pieces (for example realtime
83 * output cacheline reuse/rekey). Ownership still belongs to pixelpipe_cache.
84 * External code must treat this as metadata only and never free internals. */
86{
87 uint64_t hash; // unique identifier of the entry
88 uint64_t serial; // stable identity across rekeys, changes on fresh allocations
89 void *data; // buffer holding pixels... or anything else
90 size_t size; // size of the data buffer
91 int64_t age; // timestamp of creation. Oldest entry will be the first freed if it's not locked
92 char *name; // name of the cache entry, for debugging
93 int id; // id of the pipeline owning this entry. Used when flushing, a pipe can only flush its own.
94 dt_atomic_int refcount; // reference count for the cache entry, to avoid freeing it while still in use
95 dt_pthread_rwlock_t lock; // read/write lock to avoid threads conflicts
96 gboolean auto_destroy; // TRUE for auto-destruction the next time it's used. Used for short-lived entries (transient states).
97 gboolean external_alloc; // TRUE for external buffers tracked in the cache
98 int hits; // number of times this entry was hit (utility score)
99 dt_dev_pixelpipe_cache_t *cache; // reference to parent cache object
100 GList *cl_mem_list; // reusable OpenCL pinned buffers tied to this entry
101 dt_pthread_mutex_t cl_mem_lock;
103
110const char *dt_pixelpipe_cache_set_current_module(const char *module);
111
122 const uint64_t hash);
123
124
149 const char *name, const int id, const gboolean alloc,
150 void **data,
151 struct dt_pixel_cache_entry_t **entry);
152
192 const size_t size, const char *name, const int id,
193 const gboolean alloc, const gboolean allow_rekey_reuse,
194 const struct dt_pixel_cache_entry_t *reuse_hint,
195 void **data,
196 struct dt_pixel_cache_entry_t **entry);
197
199void *dt_pixel_cache_clmem_get(struct dt_pixel_cache_entry_t *entry, void *host_ptr, int devid,
200 int width, int height, int bpp, int flags);
201void *dt_pixel_cache_clmem_ref(struct dt_pixel_cache_entry_t *entry, void *host_ptr, int devid,
202 int width, int height, int bpp, int flags);
203void dt_pixel_cache_clmem_unref(struct dt_pixel_cache_entry_t *entry, void *mem);
204void dt_pixel_cache_clmem_put(struct dt_pixel_cache_entry_t *entry, void *host_ptr, int devid,
205 int width, int height, int bpp, int flags, void *mem);
206void dt_pixel_cache_clmem_remove(struct dt_pixel_cache_entry_t *entry, void *mem);
208
231 struct dt_pixel_cache_entry_t *entry,
232 int preferred_devid, void **data);
233
257 struct dt_pixel_cache_entry_t *entry_hint, int devid,
258 int width, int height, int bpp, int flags,
259 gboolean *out_reused);
260
275 struct dt_pixel_cache_entry_t *entry_hint, void **mem);
276
292 struct dt_pixel_cache_entry_t *entry_hint, int devid);
293
309 struct dt_pixel_cache_entry_t *entry_hint, int devid);
310
328 void *host_ptr);
329
332
343
344
355
367 const char *name);
368
375void dt_pixelpipe_cache_free_align_cache(dt_dev_pixelpipe_cache_t *cache, void **mem, const char *message);
376
392 struct dt_pixel_cache_entry_t **entry, const int preferred_devid,
393 void **cl_mem_output);
394
403
415void dt_dev_pixelpipe_cache_flush_clmem(dt_dev_pixelpipe_cache_t *cache, const int devid, void *keep);
416
417
425
435 struct dt_pixel_cache_entry_t *entry);
436
437
440
445
457 struct dt_pixel_cache_entry_t *entry);
458
466 struct dt_pixel_cache_entry_t *entry);
467
468
477 struct dt_pixel_cache_entry_t *entry);
478
479
490 struct dt_pixel_cache_entry_t *entry);
491
502 struct dt_pixel_cache_entry_t *entry);
503
504
516 struct dt_pixel_cache_entry_t **cache_entry);
517
526 struct dt_pixel_cache_entry_t *cache_entry);
527
548 struct dt_pixel_cache_entry_t **cache_entry);
549
555 struct dt_pixel_cache_entry_t *cache_entry);
556
564
586 const uint64_t new_hash, struct dt_pixel_cache_entry_t *entry);
587
588// clang-format off
589// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
590// vim: shiftwidth=2 expandtab tabstop=2 cindent
591// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
592// clang-format on
atomic_int dt_atomic_int
Definition atomic.h:63
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
char * name
Definition common/metadata.c:61
#define dt_pthread_rwlock_t
Definition dtpthread.h:389
int bpp
Definition imageio/format/pdf.c:88
size_t size
Definition mipmap_cache.c:3
dt_mipmap_buffer_dsc_flags flags
Definition mipmap_cache.c:4
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.
Definition pixelpipe_cache.c:1880
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...
Definition pixelpipe_cache.c:136
void dt_dev_pixelpipe_cache_resync_host_pinned_image(dt_dev_pixelpipe_cache_t *cache, void *host_ptr, struct dt_pixel_cache_entry_t *entry_hint, int devid)
Resynchronize cached pinned OpenCL images from an authoritative host buffer.
Definition pixelpipe_cache.c:927
void dt_pixel_cache_clmem_put(struct dt_pixel_cache_entry_t *entry, void *host_ptr, int devid, int width, int height, int bpp, int flags, void *mem)
Definition pixelpipe_cache.c:665
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.
Definition pixelpipe_cache.c:1172
void dt_dev_pixelpipe_cache_cleanup(dt_dev_pixelpipe_cache_t *cache)
Definition pixelpipe_cache.c:1342
void dt_pixel_cache_clmem_unref(struct dt_pixel_cache_entry_t *entry, void *mem)
Definition pixelpipe_cache.c:641
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()....
Definition pixelpipe_cache.c:1928
dt_dev_pixelpipe_cache_t * dt_dev_pixelpipe_cache_init(size_t max_memory)
Definition pixelpipe_cache.c:1307
void dt_dev_pixelpipe_cache_print(dt_dev_pixelpipe_cache_t *cache)
Definition pixelpipe_cache.c:2116
void dt_dev_pixelpipe_cache_unref_unlocked(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash, struct dt_pixel_cache_entry_t *cache_entry)
Decrease the refcount of an entry previously acquired with the transient realtime getter above.
Definition pixelpipe_cache.c:2008
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...
Definition pixelpipe_cache.c:1807
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.
Definition pixelpipe_cache.c:2016
void * dt_dev_pixelpipe_cache_get_read_only(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash, struct dt_pixel_cache_entry_t **cache_entry)
Find the existing cache entry linked to hash if any, lock it in read mode and increase its ref_count ...
Definition pixelpipe_cache.c:1951
void dt_dev_pixelpipe_cache_flush_clmem(dt_dev_pixelpipe_cache_t *cache, const int devid, void *keep)
Release cached OpenCL buffers for a device (-1 for all).
Definition pixelpipe_cache.c:461
int dt_dev_pixelpipe_cache_flush_old(dt_dev_pixelpipe_cache_t *cache)
Free cache entries older than 3 min, that are not locked and have been used 3 times or less.
Definition pixelpipe_cache.c:1836
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,...
Definition pixelpipe_cache.c:1912
int dt_dev_pixel_pipe_cache_remove_lru(dt_dev_pixelpipe_cache_t *cache)
Definition pixelpipe_cache.c:562
void * dt_pixel_cache_clmem_ref(struct dt_pixel_cache_entry_t *entry, void *host_ptr, int devid, int width, int height, int bpp, int flags)
Definition pixelpipe_cache.c:612
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().
Definition pixelpipe_cache.c:909
void * dt_pixel_cache_entry_get_data(struct dt_pixel_cache_entry_t *entry)
Definition pixelpipe_cache.c:1081
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 ...
Definition pixelpipe_cache.c:209
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.
Definition pixelpipe_cache.c:892
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.
Definition pixelpipe_cache.c:1086
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...
Definition pixelpipe_cache.c:1063
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.
Definition pixelpipe_cache.c:1896
void dt_pixel_cache_clmem_remove(struct dt_pixel_cache_entry_t *entry, void *mem)
Definition pixelpipe_cache.c:710
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.
Definition pixelpipe_cache.c:1704
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...
Definition pixelpipe_cache.c:1124
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....
Definition pixelpipe_cache.c:1871
void * dt_pixel_cache_clmem_get(struct dt_pixel_cache_entry_t *entry, void *host_ptr, int devid, int width, int height, int bpp, int flags)
Definition pixelpipe_cache.c:570
void * dt_dev_pixelpipe_cache_get_ref_unlocked(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash, struct dt_pixel_cache_entry_t **cache_entry)
Find an existing cache entry, synchronize once with a short read lock, then keep only a refcount (no ...
Definition pixelpipe_cache.c:1980
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.
Definition pixelpipe_cache.c:937
void dt_dev_pixelpipe_cache_close_read_only(dt_dev_pixelpipe_cache_t *cache, const uint64_t hash, struct dt_pixel_cache_entry_t *cache_entry)
Decrease the ref_count and release the read lock over cache_entry all at once.
Definition pixelpipe_cache.c:1973
void 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.
Definition pixelpipe_cache.c:918
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.
Definition pixelpipe_cache.c:1676
void dt_pixel_cache_clmem_flush(struct dt_pixel_cache_entry_t *entry)
Definition pixelpipe_cache.c:735
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...
Definition pixelpipe_cache.c:2029
dt_dev_pixelpipe_cache_writable_status_t
Definition pixelpipe_cache.h:69
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_REKEYED
Definition pixelpipe_cache.h:73
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_ERROR
Definition pixelpipe_cache.h:70
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_CREATED
Definition pixelpipe_cache.h:72
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_EXACT_HIT
Definition pixelpipe_cache.h:71
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.
Definition pixelpipe_cache.c:1438
const char * dt_pixelpipe_cache_set_current_module(const char *module)
Set the current module name for cache diagnostics (thread-local).
Definition pixelpipe_cache.c:76
unsigned __int64 uint64_t
Definition strptime.c:74
Definition memory_arena.h:35
Definition pixelpipe_cache.h:55
uint64_t next_serial
Definition pixelpipe_cache.h:59
size_t current_memory
Definition pixelpipe_cache.h:63
dt_cache_arena_t arena
Definition pixelpipe_cache.h:65
GHashTable * entries
Definition pixelpipe_cache.h:56
dt_pthread_mutex_t lock
Definition pixelpipe_cache.h:64
size_t max_memory
Definition pixelpipe_cache.h:62
uint64_t queries
Definition pixelpipe_cache.h:60
GHashTable * external_entries
Definition pixelpipe_cache.h:58
uint64_t hits
Definition pixelpipe_cache.h:61
Definition pixelpipe_hb.h:216
Definition imageop.h:67
Definition pixelpipe_cache.h:86
uint64_t hash
Definition pixelpipe_cache.h:87
gboolean auto_destroy
Definition pixelpipe_cache.h:96
dt_atomic_int refcount
Definition pixelpipe_cache.h:94
gboolean external_alloc
Definition pixelpipe_cache.h:97
void * data
Definition pixelpipe_cache.h:89
size_t size
Definition pixelpipe_cache.h:90
int64_t age
Definition pixelpipe_cache.h:91
uint64_t serial
Definition pixelpipe_cache.h:88
dt_dev_pixelpipe_cache_t * cache
Definition pixelpipe_cache.h:99
dt_pthread_rwlock_t lock
Definition pixelpipe_cache.h:95
dt_pthread_mutex_t cl_mem_lock
Definition pixelpipe_cache.h:101
GList * cl_mem_list
Definition pixelpipe_cache.h:100
char * name
Definition pixelpipe_cache.h:92
int hits
Definition pixelpipe_cache.h:98
int id
Definition pixelpipe_cache.h:93