Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
image_cache.hpp
Go to the documentation of this file.
1#pragma once
2
3// Image cache.
4//
5// URI-keyed cache for decoded images consumed by ImageView and related
6// paint paths. The cache is backend-agnostic: callers provide decode and
7// release callbacks, while the cache owns lookup, stats, LRU trimming, and
8// clear-on-memory-pressure behavior.
9//
10// Keys:
11// string URI ("file:///…", "resource://…", "memory://sha256=…")
12// combined with mtime/size for file URIs so a rebuilt image
13// invalidates automatically.
14
15#include <cstddef>
16#include <cstdint>
17#include <functional>
18#include <mutex>
19#include <optional>
20#include <string>
21#include <unordered_map>
22#include <vector>
23
24namespace pulp::view {
25
27 uint32_t width = 0;
28 uint32_t height = 0;
31 void* native_handle = nullptr;
35 std::size_t bytes = 0;
36};
37
39 std::size_t entry_count = 0;
40 std::size_t total_bytes = 0;
41 std::size_t hits = 0;
42 std::size_t misses = 0;
43 std::size_t evictions = 0;
44};
45
49using ImageDecodeFn = std::function<std::optional<DecodedImage>(const std::string& uri)>;
50
53using ImageReleaseFn = std::function<void(DecodedImage&)>;
54
56public:
57 ImageCache() = default;
58
59 void set_decoder(ImageDecodeFn fn) { decoder_ = std::move(fn); }
60 void set_releaser(ImageReleaseFn fn) { releaser_ = std::move(fn); }
61
65 void set_byte_budget(std::size_t bytes) { budget_ = bytes; try_trim_(); }
66
69 const DecodedImage* get(const std::string& uri);
70
73 void clear();
74
76
77private:
78 struct Entry {
79 DecodedImage image;
80 uint64_t last_used = 0;
81 };
82
83 void evict_one_();
84 void try_trim_();
85
86 mutable std::mutex mu_;
87 std::unordered_map<std::string, Entry> entries_;
88 ImageDecodeFn decoder_;
89 ImageReleaseFn releaser_;
90 std::size_t total_bytes_ = 0;
91 std::size_t budget_ = 0;
92 uint64_t tick_ = 0;
93 ImageCacheStats stats_{};
94};
95
96} // namespace pulp::view
void set_releaser(ImageReleaseFn fn)
Definition image_cache.hpp:60
ImageCacheStats stats() const
void set_decoder(ImageDecodeFn fn)
Definition image_cache.hpp:59
void set_byte_budget(std::size_t bytes)
Soft byte cap.
Definition image_cache.hpp:65
void clear()
Drop everything rebuildable.
const DecodedImage * get(const std::string &uri)
Look up (and decode if absent) the image at uri.
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
std::function< std::optional< DecodedImage >(const std::string &uri)> ImageDecodeFn
Pluggable decode callback.
Definition image_cache.hpp:49
std::function< void(DecodedImage &)> ImageReleaseFn
Release the platform handle when an entry is evicted.
Definition image_cache.hpp:53
Definition image_cache.hpp:26
std::size_t bytes
Best-effort estimate of the resident memory this entry costs (e.g.
Definition image_cache.hpp:35
void * native_handle
Platform-owned handle.
Definition image_cache.hpp:31
uint32_t height
Definition image_cache.hpp:28
uint32_t width
Definition image_cache.hpp:27
Definition image_cache.hpp:38
std::size_t evictions
Definition image_cache.hpp:43
std::size_t misses
Definition image_cache.hpp:42
std::size_t entry_count
Definition image_cache.hpp:39
std::size_t hits
Definition image_cache.hpp:41
std::size_t total_bytes
Definition image_cache.hpp:40