2 #include <score/tools/Debug.hpp>
4 #include <ossia/detail/hash.hpp>
5 #include <ossia/detail/hash_map.hpp>
6 #include <ossia/detail/mutex.hpp>
18 class hash<std::pair<int, int>>
21 std::size_t operator()(
const std::pair<int, int>& p)
const
24 ossia::hash_combine(seed, p.first);
25 ossia::hash_combine(seed, p.second);
31 namespace Media::Sound
37 QVector<QImage*> images;
38 std::chrono::steady_clock::time_point last_touched;
41 static const constexpr
int max_count = 100;
42 using pool_t = ossia::hash_map<std::pair<int, int>,
Images>;
43 pool_t pool TS_GUARDED_BY(m_mtx);
54 for(
auto& pair : pool)
56 for(QImage* img : pair.second.images)
63 static inline int hit = 0;
64 static inline int miss = 0;
65 QImage* request(
int w,
int h)
67 std::lock_guard _{m_mtx};
69 auto it = pool.find({w, h});
72 auto& vec = it->second.images;
75 auto img = vec.front();
77 img->fill(Qt::transparent);
78 it->second.last_touched = std::chrono::steady_clock::now();
84 auto img =
new QImage(w, h, QImage::Format_ARGB32_Premultiplied);
85 img->fill(Qt::transparent);
90 void giveBack(
const QVector<QImage*>& imgs)
94 QVector<QImage*> to_delete;
97 std::lock_guard _{m_mtx};
99 Images& images = pool[std::make_pair(img->width(), img->height())];
100 SCORE_ASSERT(!images.images.contains(img));
101 images.images.push_back(img);
102 images.last_touched = std::chrono::steady_clock::now();
105 for(
auto img : to_delete)
110 QVector<QImage*> gc()
116 auto oldest = pool.begin();
117 auto oldest_t = oldest->second.last_touched;
118 for(
auto it = oldest; it != pool.end(); ++it)
120 if(it->second.last_touched < oldest->second.last_touched)
123 oldest_t = it->second.last_touched;
125 count += it->second.images.size();
128 if(count < max_count)
130 auto res = std::move(oldest->second.images);
131 SCORE_ASSERT(oldest->second.images.isEmpty());
132 oldest->second.last_touched = std::chrono::steady_clock::now();
138 std::size_t bytes = 0;
140 for(
auto& pair : pool)
142 for(QImage* img : pair.second.images)
144 bytes += img->sizeInBytes();
149 qDebug() << QString(
"%1 images: %2 megabytes ; hit/miss ratio : %3 / %4 = %5")
151 .arg(bytes / (1024 * 1024))
154 .arg(
double(hit) / miss);