Repository navigation
Expand file tree
/
Copy pathframecache.cpp
More file actions
64 lines (56 loc) · 1.43 KB
/
Copy pathframecache.cpp
File metadata and controls
64 lines (56 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "framecache.h"
#include <utility>
FrameCache::FrameCache(std::size_t limit_bytes) : limit_bytes_(limit_bytes) {}
void FrameCache::SetLimitBytes(std::size_t limit_bytes) {
limit_bytes_ = limit_bytes;
Evict();
}
void FrameCache::Clear() {
entries_.clear();
lru_.clear();
bytes_ = 0;
}
const CachedFrame* FrameCache::Find(int index) {
auto it = entries_.find(index);
if (it == entries_.end()) {
misses_++;
return nullptr;
}
hits_++;
lru_.erase(it->lru);
lru_.push_front(index);
it->lru = lru_.begin();
return &it->frame;
}
void FrameCache::Insert(int index, const QImage& image, double pts_sec) {
if (image.isNull()) {
return;
}
const std::size_t image_bytes = (std::size_t)image.sizeInBytes();
auto it = entries_.find(index);
if (it != entries_.end()) {
bytes_ -= (std::size_t)it->frame.image.sizeInBytes();
lru_.erase(it->lru);
entries_.erase(it);
}
Entry entry;
entry.frame.index = index;
entry.frame.image = image;
entry.frame.pts_sec = pts_sec;
lru_.push_front(index);
entry.lru = lru_.begin();
entries_.insert(index, entry);
bytes_ += image_bytes;
Evict();
}
void FrameCache::Evict() {
while (bytes_ > limit_bytes_ && !lru_.empty()) {
const int oldest = lru_.back();
lru_.pop_back();
auto it = entries_.find(oldest);
if (it != entries_.end()) {
bytes_ -= (std::size_t)it->frame.image.sizeInBytes();
entries_.erase(it);
}
}
}