Hermes
0.7.0
Linux game streaming host
Toggle main menu visibility
Loading...
Searching...
No Matches
dmabuf_mapping.h
Go to the documentation of this file.
1
5
#pragma once
6
7
#include <algorithm>
8
#include <cerrno>
9
#include <cstddef>
10
#include <cstdint>
11
#include <cstring>
12
#include <vector>
13
14
#include <sys/mman.h>
15
#include <sys/stat.h>
16
#include <sys/types.h>
17
18
namespace
platf::dmabuf {
19
36
class
mapping_cache_t {
37
public
:
38
explicit
mapping_cache_t(std::size_t capacity = 4):
39
capacity {std::max<std::size_t>(capacity, 1)} {
40
}
41
42
~mapping_cache_t() {
43
clear
();
44
}
45
46
mapping_cache_t(
const
mapping_cache_t &) =
delete
;
47
mapping_cache_t &operator=(
const
mapping_cache_t &) =
delete
;
48
56
const
std::uint8_t *
map
(
int
fd, std::size_t required) {
57
struct
stat st {};
58
if
(::fstat(fd, &st) < 0) {
59
return
nullptr
;
60
}
61
if
(st.st_size <= 0 ||
62
static_cast<
std::uintmax_t
>
(required) >
static_cast<
std::uintmax_t
>
(st.st_size)) {
63
errno = EINVAL;
64
return
nullptr
;
65
}
66
const
auto
length =
static_cast<
std::size_t
>
(st.st_size);
67
68
++clock;
69
for
(
auto
&entry : entries) {
70
if
(entry.device == st.st_dev && entry.inode == st.st_ino && entry.length == length) {
71
entry.last_use = clock;
72
++hit_count;
73
return
entry.address;
74
}
75
}
76
77
void
*address = ::mmap(
nullptr
, length, PROT_READ, MAP_SHARED, fd, 0);
78
if
(address == MAP_FAILED) {
79
return
nullptr
;
80
}
81
if
(entries.size() >= capacity) {
82
auto
oldest = std::min_element(entries.begin(), entries.end(), [](
const
entry_t &a,
const
entry_t &b) {
83
return a.last_use < b.last_use;
84
});
85
::munmap(oldest->address, oldest->length);
86
entries.erase(oldest);
87
}
88
entries.push_back({st.st_dev, st.st_ino,
static_cast<
std::uint8_t *
>
(address), length, clock});
89
++miss_count;
90
return
static_cast<
std::uint8_t *
>
(address);
91
}
92
94
void
clear
() {
95
for
(
const
auto
&entry : entries) {
96
::munmap(entry.address, entry.length);
97
}
98
entries.clear();
99
}
100
101
std::size_t size()
const
{
102
return
entries.size();
103
}
104
105
std::uint64_t hits()
const
{
106
return
hit_count;
107
}
108
109
std::uint64_t misses()
const
{
110
return
miss_count;
111
}
112
113
private
:
114
struct
entry_t {
115
dev_t device;
116
ino_t inode;
117
std::uint8_t *address;
118
std::size_t length;
119
std::uint64_t last_use;
120
};
121
122
std::size_t capacity;
123
std::vector<entry_t> entries;
124
std::uint64_t clock {0};
125
std::uint64_t hit_count {0};
126
std::uint64_t miss_count {0};
127
};
128
136
constexpr
std::size_t
copy_block_bytes
= std::size_t {2} << 20;
137
142
inline
void
copy_rows
(
143
std::uint8_t *dst,
144
std::size_t dst_pitch,
145
const
std::uint8_t *src,
146
std::size_t src_pitch,
147
std::size_t row_bytes,
148
std::size_t rows,
149
std::size_t block_bytes =
copy_block_bytes
150
) {
151
if
(src_pitch == row_bytes && dst_pitch == row_bytes) {
152
const
std::size_t total = row_bytes * rows;
153
block_bytes = std::max<std::size_t>(block_bytes, 1);
154
for
(std::size_t offset = 0; offset < total; offset += block_bytes) {
155
std::memcpy(dst + offset, src + offset, std::min(block_bytes, total - offset));
156
}
157
return
;
158
}
159
for
(std::size_t y = 0; y < rows; ++y) {
160
std::memcpy(dst + y * dst_pitch, src + y * src_pitch, row_bytes);
161
}
162
}
163
164
}
// namespace platf::dmabuf
platf::dmabuf::mapping_cache_t::clear
void clear()
Definition
dmabuf_mapping.h:94
platf::dmabuf::mapping_cache_t::map
const std::uint8_t * map(int fd, std::size_t required)
Definition
dmabuf_mapping.h:56
platf::dmabuf::copy_block_bytes
constexpr std::size_t copy_block_bytes
Definition
dmabuf_mapping.h:136
platf::dmabuf::copy_rows
void copy_rows(std::uint8_t *dst, std::size_t dst_pitch, const std::uint8_t *src, std::size_t src_pitch, std::size_t row_bytes, std::size_t rows, std::size_t block_bytes=copy_block_bytes)
Definition
dmabuf_mapping.h:142
src
platform
linux
dmabuf_mapping.h
Generated by
1.18.0