]> git.sesse.net Git - nageru/blob - futatabi/frame_on_disk.h
Cache computed flow between textures.
[nageru] / futatabi / frame_on_disk.h
1 #ifndef _FRAME_ON_DISK_H
2 #define _FRAME_ON_DISK_H 1
3
4 #include <algorithm>
5 #include <mutex>
6 #include <string>
7 #include <vector>
8
9 #include <stdint.h>
10
11 #include "defs.h"
12
13 extern std::mutex frame_mu;
14 struct FrameOnDisk {
15         int64_t pts = -1;  // -1 means empty.
16         off_t offset;
17         unsigned filename_idx;
18         uint32_t size;  // Not using size_t saves a few bytes; we can have so many frames.
19 };
20 extern std::vector<FrameOnDisk> frames[MAX_STREAMS];  // Under frame_mu.
21 extern std::vector<std::string> frame_filenames;  // Under frame_mu.
22
23 static bool inline operator==(const FrameOnDisk &a, const FrameOnDisk &b)
24 {
25         return a.pts == b.pts &&
26                 a.offset == b.offset &&
27                 a.filename_idx == b.filename_idx &&
28                 a.size == b.size;
29 }
30
31 // A helper class to read frames from disk. It caches the file descriptor
32 // so that the kernel has a better chance of doing readahead when it sees
33 // the sequential reads. (For this reason, each display has a private
34 // FrameReader. Thus, we can easily keep multiple open file descriptors around
35 // for a single .frames file.)
36 class FrameReader {
37 public:
38         FrameReader();
39         ~FrameReader();
40         std::string read_frame(FrameOnDisk frame);
41
42 private:
43         int fd = -1;
44         int last_filename_idx = -1;
45 };
46
47 // Utility functions for dealing with binary search.
48 inline std::vector<FrameOnDisk>::iterator
49 find_last_frame_before(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
50 {
51         return std::lower_bound(frames.begin(), frames.end(), pts_origin,
52                 [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; });
53 }
54
55 inline std::vector<FrameOnDisk>::iterator
56 find_first_frame_at_or_after(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
57 {
58         return std::upper_bound(frames.begin(), frames.end(), pts_origin - 1,
59                 [](int64_t pts, const FrameOnDisk &frame) { return pts < frame.pts; });
60 }
61
62 #endif  // !defined(_FRAME_ON_DISK_H)