19#include <drm_fourcc.h>
20#include <linux/dma-buf.h>
21#include <linux/sync_file.h>
23#include <sys/capability.h>
27#include <xf86drmMode.h>
45using namespace std::literals;
46namespace fs = std::filesystem;
55 inline void accumulate_capture_metric(
const char *label,
long long ns) {
56 static thread_local long long sum = 0;
57 static thread_local long long peak = 0;
58 static thread_local int count = 0;
60 peak = std::max(peak, ns);
62 BOOST_LOG(info) <<
"[capture-metric/" << label <<
"] avg="
63 << (sum / count) / 1000.0 <<
"us peak=" << peak / 1000.0
64 <<
"us over " << count <<
" frames";
71 enum class hermes_fence_wait_e {
81 inline std::uint32_t hermes_timeout_ms(std::chrono::milliseconds timeout) {
86 constexpr auto max_poll_timeout = std::chrono::milliseconds {
87 std::numeric_limits<int>::max()
89 if (timeout >= max_poll_timeout) {
90 return static_cast<std::uint32_t
>(std::numeric_limits<int>::max());
92 return static_cast<std::uint32_t
>(timeout.count());
98 inline hermes_fence_wait_e wait_hermes_frame_fence(
100 std::chrono::steady_clock::time_point deadline
103 const auto now = std::chrono::steady_clock::now();
105 if (now < deadline) {
106 const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now);
107 wait_ms =
static_cast<int>(std::max<std::int64_t>(1, remaining.count()));
110 pollfd fence_poll {fd, POLLIN, 0};
111 const int poll_result = ::poll(&fence_poll, 1, wait_ms);
112 if (poll_result < 0) {
113 if (errno == EINTR) {
116 return hermes_fence_wait_e::error;
118 if (poll_result == 0) {
120 return hermes_fence_wait_e::timeout;
122 if (fence_poll.revents & POLLNVAL) {
124 return hermes_fence_wait_e::error;
127 sync_file_info info {};
130 info_result = ::ioctl(fd, SYNC_IOC_FILE_INFO, &info);
131 }
while (info_result < 0 && errno == EINTR);
132 if (info_result < 0) {
133 return hermes_fence_wait_e::error;
135 if (info.status < 0) {
136 errno = info.status >= -4095 ? -info.status : EIO;
137 return hermes_fence_wait_e::failed;
139 if (info.status > 0) {
140 return hermes_fence_wait_e::ready;
146 return hermes_fence_wait_e::error;
150 inline capture_e hermes_acquire_failure(
const char *path,
int acquire_errno) {
151 if (acquire_errno == ETIMEDOUT || acquire_errno == EAGAIN) {
152 return capture_e::timeout;
154 if (acquire_errno == EINTR) {
155 return capture_e::interrupted;
158 if (!acquire_errno) {
159 acquire_errno = EPROTO;
161 BOOST_LOG(error) <<
"Hermes-KMS "sv << path
162 <<
" capture failed; reinitializing: "sv
163 << strerror(acquire_errno);
164 return capture_e::reinit;
167 inline int hermes_dma_buf_sync(
int fd, std::uint64_t flags) {
168 dma_buf_sync sync {};
173 result = ::ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
174 }
while (result < 0 && errno == EINTR);
178 class cap_sys_admin {
181 caps = cap_get_proc();
183 cap_value_t sys_admin = CAP_SYS_ADMIN;
184 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &sys_admin, CAP_SET) || cap_set_proc(caps)) {
185 BOOST_LOG(error) <<
"Failed to gain CAP_SYS_ADMIN";
190 cap_value_t sys_admin = CAP_SYS_ADMIN;
191 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &sys_admin, CAP_CLEAR) || cap_set_proc(caps)) {
192 BOOST_LOG(error) <<
"Failed to drop CAP_SYS_ADMIN";
202 wrapper_fb(drmModeFB *fb):
206 height {fb->height} {
207 pixel_format = DRM_FORMAT_XRGB8888;
208 modifier = DRM_FORMAT_MOD_INVALID;
209 std::fill_n(handles, 4, 0);
210 std::fill_n(pitches, 4, 0);
211 std::fill_n(offsets, 4, 0);
212 handles[0] = fb->handle;
213 pitches[0] = fb->pitch;
216 wrapper_fb(drmModeFB2 *fb2):
220 height {fb2->height} {
221 pixel_format = fb2->pixel_format;
222 modifier = (fb2->flags & DRM_MODE_FB_MODIFIERS) ? fb2->modifier : DRM_FORMAT_MOD_INVALID;
224 memcpy(handles, fb2->handles,
sizeof(handles));
225 memcpy(pitches, fb2->pitches,
sizeof(pitches));
226 memcpy(offsets, fb2->offsets,
sizeof(offsets));
237 drmModeFB *fb =
nullptr;
238 drmModeFB2 *fb2 =
nullptr;
242 uint32_t pixel_format;
249 using plane_res_t = util::safe_ptr<drmModePlaneRes, drmModeFreePlaneResources>;
250 using encoder_t = util::safe_ptr<drmModeEncoder, drmModeFreeEncoder>;
251 using res_t = util::safe_ptr<drmModeRes, drmModeFreeResources>;
252 using plane_t = util::safe_ptr<drmModePlane, drmModeFreePlane>;
253 using fb_t = std::unique_ptr<wrapper_fb>;
254 using crtc_t = util::safe_ptr<drmModeCrtc, drmModeFreeCrtc>;
255 using obj_prop_t = util::safe_ptr<drmModeObjectProperties, drmModeFreeObjectProperties>;
256 using prop_t = util::safe_ptr<drmModePropertyRes, drmModeFreeProperty>;
257 using prop_blob_t = util::safe_ptr<drmModePropertyBlobRes, drmModeFreePropertyBlob>;
258 using version_t = util::safe_ptr<drmVersion, drmFreeVersion>;
260 using conn_type_count_t = std::map<std::uint32_t, std::uint32_t>;
262 static int env_width;
263 static int env_height;
265 std::string_view plane_type(std::uint64_t val) {
267 case DRM_PLANE_TYPE_OVERLAY:
268 return "DRM_PLANE_TYPE_OVERLAY"sv;
269 case DRM_PLANE_TYPE_PRIMARY:
270 return "DRM_PLANE_TYPE_PRIMARY"sv;
271 case DRM_PLANE_TYPE_CURSOR:
272 return "DRM_PLANE_TYPE_CURSOR"sv;
283 std::uint32_t crtc_id;
289 std::uint32_t connector_id;
300 std::uint32_t monitor_index;
308 std::map<std::uint32_t, monitor_t> crtc_to_monitor;
311 static std::vector<card_descriptor_t> card_descriptors;
318 static std::string kernel_connector_name(std::uint32_t type, std::uint32_t type_id) {
319 static constexpr std::array<const char *, 21> names {
320 "Unknown",
"VGA",
"DVI-I",
"DVI-D",
"DVI-A",
"Composite",
"SVIDEO",
"LVDS",
"Component",
"DIN",
321 "DP",
"HDMI-A",
"HDMI-B",
"TV",
"eDP",
"Virtual",
"DSI",
"DPI",
"Writeback",
"SPI",
"USB",
323 if (type >= names.size() || type_id == 0) {
326 return std::string {names[type]} +
'-' + std::to_string(type_id);
329 static std::uint32_t from_view(
const std::string_view &
string) {
330#define _CONVERT(x, y) \
332 return DRM_MODE_CONNECTOR_##y
338 _CONVERT(
"VGA"sv, VGA);
339 _CONVERT(
"DVII"sv, DVII);
340 _CONVERT(
"DVI-I"sv, DVII);
341 _CONVERT(
"DVID"sv, DVID);
342 _CONVERT(
"DVI-D"sv, DVID);
343 _CONVERT(
"DVIA"sv, DVIA);
344 _CONVERT(
"DVI-A"sv, DVIA);
345 _CONVERT(
"Composite"sv, Composite);
346 _CONVERT(
"SVIDEO"sv, SVIDEO);
347 _CONVERT(
"S-Video"sv, SVIDEO);
348 _CONVERT(
"LVDS"sv, LVDS);
349 _CONVERT(
"Component"sv, Component);
350 _CONVERT(
"9PinDIN"sv, 9PinDIN);
351 _CONVERT(
"DIN"sv, 9PinDIN);
352 _CONVERT(
"DisplayPort"sv, DisplayPort);
353 _CONVERT(
"DP"sv, DisplayPort);
354 _CONVERT(
"HDMIA"sv, HDMIA);
355 _CONVERT(
"HDMI-A"sv, HDMIA);
356 _CONVERT(
"HDMI"sv, HDMIA);
357 _CONVERT(
"HDMIB"sv, HDMIB);
358 _CONVERT(
"HDMI-B"sv, HDMIB);
359 _CONVERT(
"TV"sv, TV);
360 _CONVERT(
"eDP"sv, eDP);
361 _CONVERT(
"VIRTUAL"sv, VIRTUAL);
362 _CONVERT(
"Virtual"sv, VIRTUAL);
363 _CONVERT(
"DSI"sv, DSI);
364 _CONVERT(
"DPI"sv, DPI);
365 _CONVERT(
"WRITEBACK"sv, WRITEBACK);
366 _CONVERT(
"Writeback"sv, WRITEBACK);
367 _CONVERT(
"SPI"sv, SPI);
368#ifdef DRM_MODE_CONNECTOR_USB
369 _CONVERT(
"USB"sv, USB);
374 if (
string.
find(
"Unknown"sv) == 0) {
376 std::string null_terminated_string {
string};
377 if (std::sscanf(null_terminated_string.c_str(),
"Unknown%u", &type) == 1) {
382 BOOST_LOG(error) <<
"Unknown Monitor connector type ["sv <<
string <<
"]: Please report this to the GitHub issue tracker"sv;
383 return DRM_MODE_CONNECTOR_Unknown;
388 plane_it_t(
int fd, std::uint32_t *plane_p, std::uint32_t *end):
392 load_next_valid_plane();
395 plane_it_t(
int fd, std::uint32_t *end):
401 void load_next_valid_plane() {
404 for (; plane_p != end; ++plane_p) {
405 plane_t plane = drmModeGetPlane(fd, *plane_p);
407 BOOST_LOG(error) <<
"Couldn't get drm plane ["sv << (end - plane_p) <<
"]: "sv << strerror(errno);
411 this->plane = util::make_shared<plane_t>(plane.release());
418 load_next_valid_plane();
421 bool eq(
const plane_it_t &other)
const {
422 return plane_p == other.plane_p;
425 plane_t::pointer get() {
430 std::uint32_t *plane_p;
433 util::shared_t<plane_t> plane;
438 bool visible =
false;
440 std::uint32_t dst_w, dst_h;
441 std::uint32_t src_w, src_h;
442 std::vector<std::uint8_t> pixels;
443 unsigned long serial;
446 std::uint64_t prop_src_x, prop_src_y, prop_src_w, prop_src_h;
452 std::chrono::steady_clock::time_point deadline,
454 std::uint64_t &last_cursor_sequence
459 if (errno == ESTALE || errno == EAGAIN) {
462 return hermes_acquire_failure(
"cursor", errno);
464 auto cursor_guard = util::fail_guard([&cursor]() { cursor.
close(); });
466 if (!cursor.visible) {
467 captured_cursor.visible =
false;
468 last_cursor_sequence = cursor.sequence;
469 return capture_e::ok;
472 constexpr std::uint32_t fixed_fraction_mask = (1U << 16) - 1U;
473 const bool integral_source =
475 const std::uint32_t src_x = cursor.
src_x >> 16;
476 const std::uint32_t src_y = cursor.
src_y >> 16;
477 const std::uint32_t src_w = cursor.
src_w >> 16;
478 const std::uint32_t src_h = cursor.
src_h >> 16;
479 const bool supported_layout =
480 cursor.position_valid && cursor.geometry_valid && cursor.buffer_valid &&
481 cursor.plane_count == 1 && cursor.dma_buf_fd[0] >= 0 && cursor.sync_file_fd >= 0 &&
482 cursor.fourcc == DRM_FORMAT_ARGB8888 &&
483 (cursor.modifier == DRM_FORMAT_MOD_LINEAR || cursor.modifier == DRM_FORMAT_MOD_INVALID) &&
484 integral_source && src_w && src_h && cursor.crtc_w == src_w && cursor.crtc_h == src_h &&
485 src_x <= cursor.width && src_w <= cursor.width - src_x &&
486 src_y <= cursor.height && src_h <= cursor.height - src_y;
487 if (!supported_layout) {
488 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: unsupported or malformed cursor layout."sv;
489 return capture_e::reinit;
492 const auto fence_status = wait_hermes_frame_fence(cursor.sync_file_fd, deadline);
493 if (fence_status == hermes_fence_wait_e::timeout) {
494 BOOST_LOG(warning) <<
"Hermes-KMS cursor capture: producer fence timed out."sv;
495 return capture_e::timeout;
497 if (fence_status == hermes_fence_wait_e::failed) {
498 const int fence_errno = errno;
499 BOOST_LOG(warning) <<
"Hermes-KMS cursor capture: dropping update "sv
500 << cursor.sequence <<
" after producer fence failure: "sv
501 << strerror(fence_errno);
502 captured_cursor.visible =
false;
503 last_cursor_sequence = cursor.sequence;
504 return capture_e::ok;
506 if (fence_status == hermes_fence_wait_e::error) {
507 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: invalid sync file: "sv << strerror(errno);
508 return capture_e::reinit;
511 const std::size_t pitch = cursor.pitch[0];
512 const std::size_t offset = cursor.offset[0];
513 const std::size_t source_x_bytes =
static_cast<std::size_t
>(src_x) * 4U;
514 const std::size_t row_bytes =
static_cast<std::size_t
>(src_w) * 4U;
515 const std::size_t last_source_row =
static_cast<std::size_t
>(src_y) + src_h - 1U;
516 if (pitch < source_x_bytes || row_bytes > pitch - source_x_bytes ||
517 last_source_row > (std::numeric_limits<std::size_t>::max() - offset) / pitch) {
518 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: cursor row layout overflows size_t."sv;
519 return capture_e::reinit;
521 const std::size_t last_row_offset = offset + last_source_row * pitch;
522 if (source_x_bytes > std::numeric_limits<std::size_t>::max() - last_row_offset ||
523 row_bytes > std::numeric_limits<std::size_t>::max() - last_row_offset - source_x_bytes) {
524 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: cursor mapping length overflows size_t."sv;
525 return capture_e::reinit;
527 const std::size_t map_len = last_row_offset + source_x_bytes + row_bytes;
528 if (
static_cast<std::size_t
>(src_h) >
529 std::numeric_limits<std::size_t>::max() / row_bytes) {
530 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: cursor image size overflows size_t."sv;
531 return capture_e::reinit;
534 struct stat dmabuf_stat {};
535 if (::fstat(cursor.dma_buf_fd[0], &dmabuf_stat) < 0 || dmabuf_stat.st_size <= 0 ||
536 static_cast<std::uintmax_t
>(map_len) >
static_cast<std::uintmax_t
>(dmabuf_stat.st_size)) {
537 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: cursor metadata exceeds its DMA-BUF."sv;
538 return capture_e::reinit;
542 next_cursor.pixels.resize(
static_cast<std::size_t
>(src_h) * row_bytes);
543 void *map = ::mmap(
nullptr, map_len, PROT_READ, MAP_SHARED, cursor.dma_buf_fd[0], 0);
544 if (map == MAP_FAILED) {
545 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: mmap failed: "sv << strerror(errno);
546 return capture_e::reinit;
548 if (hermes_dma_buf_sync(cursor.dma_buf_fd[0], DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ) < 0) {
549 const int sync_errno = errno;
550 ::munmap(map, map_len);
551 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: DMA_BUF_SYNC_START failed: "sv
552 << strerror(sync_errno);
553 return capture_e::reinit;
556 const auto *source =
static_cast<const std::uint8_t *
>(map) + offset +
557 static_cast<std::size_t
>(src_y) * pitch + source_x_bytes;
558 for (std::size_t y = 0; y < src_h; ++y) {
559 std::memcpy(next_cursor.pixels.data() + y * row_bytes, source + y * pitch, row_bytes);
562 const int sync_end_result = hermes_dma_buf_sync(
563 cursor.dma_buf_fd[0],
564 DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ
566 const int sync_end_errno = errno;
567 const int unmap_result = ::munmap(map, map_len);
568 const int unmap_errno = errno;
569 if (sync_end_result < 0 || unmap_result < 0) {
570 BOOST_LOG(error) <<
"Hermes-KMS cursor capture: cleanup failed: "sv
571 << strerror(sync_end_result < 0 ? sync_end_errno : unmap_errno);
572 return capture_e::reinit;
575 next_cursor.visible =
true;
576 next_cursor.x = cursor.crtc_x;
577 next_cursor.y = cursor.crtc_y;
578 next_cursor.dst_w = cursor.crtc_w;
579 next_cursor.dst_h = cursor.crtc_h;
580 next_cursor.src_w = src_w;
581 next_cursor.src_h = src_h;
582 next_cursor.serial = captured_cursor.serial + 1;
583 next_cursor.prop_src_x = cursor.
src_x;
584 next_cursor.prop_src_y = cursor.
src_y;
585 next_cursor.prop_src_w = cursor.
src_w;
586 next_cursor.prop_src_h = cursor.
src_h;
587 captured_cursor = std::move(next_cursor);
588 last_cursor_sequence = cursor.sequence;
589 return capture_e::ok;
592 inline void attach_hermes_cursor(
593 egl::img_descriptor_t &img,
597 if (!requested || !captured_cursor.visible) {
602 if (img.serial != captured_cursor.serial) {
603 img.buffer = captured_cursor.pixels;
604 img.serial = captured_cursor.serial;
606 img.cursor_premultiplied =
true;
607 img.x = captured_cursor.x;
608 img.y = captured_cursor.y;
609 img.src_w = captured_cursor.src_w;
610 img.src_h = captured_cursor.src_h;
611 img.width = captured_cursor.dst_w;
612 img.height = captured_cursor.dst_h;
614 img.row_pitch = img.pixel_pitch * img.src_w;
615 img.data = img.buffer.data();
621 for (
unsigned int i = 0; i < 4; ++i) {
622 const auto c =
static_cast<char>((fourcc >> (8 * i)) & 0xff);
623 if (c < 0x20 || c > 0x7e) {
625 std::snprintf(hex,
sizeof(hex),
"0x%08x", fourcc);
635 return fourcc == DRM_FORMAT_XRGB2101010 || fourcc == DRM_FORMAT_ARGB2101010 ||
636 fourcc == DRM_FORMAT_XBGR2101010 || fourcc == DRM_FORMAT_ABGR2101010;
642 return fourcc == DRM_FORMAT_XRGB8888 || fourcc == DRM_FORMAT_ARGB8888 ||
hermes_ten_bit_format(fourcc);
652 if (!cursor.visible || !img.data || img.pixel_pitch != 4 || img.row_pitch <= 0 ||
653 img.width <= 0 || img.height <= 0 || !cursor.src_w || !cursor.src_h ||
654 cursor.src_w > std::numeric_limits<std::size_t>::max() / 4U) {
658 const std::int32_t left = std::max<std::int32_t>(0, cursor.x);
659 const std::int32_t top = std::max<std::int32_t>(0, cursor.y);
660 const std::int64_t cursor_right =
static_cast<std::int64_t
>(cursor.x) + cursor.src_w;
661 const std::int64_t cursor_bottom =
static_cast<std::int64_t
>(cursor.y) + cursor.src_h;
662 const std::int32_t right =
static_cast<std::int32_t
>(
663 std::min<std::int64_t>(img.width, cursor_right)
665 const std::int32_t bottom =
static_cast<std::int32_t
>(
666 std::min<std::int64_t>(img.height, cursor_bottom)
668 if (left >= right || top >= bottom) {
672 const std::size_t cursor_pitch =
static_cast<std::size_t
>(cursor.src_w) * 4U;
673 if (cursor.src_h > std::numeric_limits<std::size_t>::max() / cursor_pitch) {
676 const std::size_t required_cursor_bytes =
static_cast<std::size_t
>(cursor.src_h) * cursor_pitch;
677 if (cursor.pixels.size() < required_cursor_bytes ||
678 static_cast<std::size_t
>(img.row_pitch) <
static_cast<std::size_t
>(img.width) * 4U) {
682 const std::size_t source_x =
static_cast<std::size_t
>(left - cursor.x);
683 const std::size_t source_y =
static_cast<std::size_t
>(top - cursor.y);
686 const bool red_high = fourcc == DRM_FORMAT_XRGB2101010 || fourcc == DRM_FORMAT_ARGB2101010;
687 const unsigned int shifts[3] {red_high ? 0U : 20U, 10U, red_high ? 20U : 0U};
688 for (std::int32_t y = top; y < bottom; ++y) {
689 const auto *source = cursor.pixels.data() +
690 (source_y +
static_cast<std::size_t
>(y - top)) * cursor_pitch +
692 auto *destination = img.data +
static_cast<std::size_t
>(y) * img.row_pitch +
693 static_cast<std::size_t
>(left) * 4U;
694 for (std::int32_t x = left; x < right; ++x, source += 4, destination += 4) {
695 const unsigned int alpha = source[3];
701 std::memcpy(&word, destination,
sizeof(word));
702 std::uint32_t blended = word & 0xc0000000U;
703 for (
unsigned int channel = 0; channel < 3; ++channel) {
704 const unsigned int below = (word >> shifts[channel]) & 1023U;
705 const unsigned int value = (source[channel] * 1023U + below * (255U - alpha) + 127U) / 255U;
706 blended |= std::min(value, 1023U) << shifts[channel];
708 std::memcpy(destination, &blended,
sizeof(blended));
709 }
else if (alpha == 255U) {
710 std::memcpy(destination, source, 4);
713 destination[0] =
static_cast<std::uint8_t
>(
714 source[0] + (destination[0] * (255U - alpha) + 127U) / 255U
716 destination[1] =
static_cast<std::uint8_t
>(
717 source[1] + (destination[1] * (255U - alpha) + 127U) / 255U
719 destination[2] =
static_cast<std::uint8_t
>(
720 source[2] + (destination[2] * (255U - alpha) + 127U) / 255U
728 void blend_hermes_cursor_for_test(
734 std::uint32_t height,
735 const std::vector<std::uint8_t> &pixels,
739 cursor.visible = visible;
742 cursor.src_w = width;
743 cursor.src_h = height;
744 cursor.pixels = pixels;
745 blend_hermes_cursor(img, cursor, fourcc);
751 using connector_interal_t = util::safe_ptr<drmModeConnector, drmModeFreeConnector>;
753 int init(
const char *path) {
758 BOOST_LOG(error) <<
"Couldn't open: "sv << path <<
": "sv << strerror(errno);
762 version_t ver {drmGetVersion(fd.el)};
763 BOOST_LOG(info) << path <<
" -> "sv << ((ver && ver->name) ? ver->name :
"UNKNOWN");
767 char *rendernode_path = drmGetRenderDeviceNameFromFd(fd.el);
768 if (rendernode_path) {
769 BOOST_LOG(debug) <<
"Opening render node: "sv << rendernode_path;
770 render_fd.el = open(rendernode_path, O_RDWR);
771 if (render_fd.el < 0) {
775 const bool hermes_kms = errno == EACCES && ver && ver->name && ver->name ==
"hermes-kms"sv;
776 BOOST_LOG(hermes_kms ? debug : warning) <<
"Couldn't open render node: "sv << rendernode_path <<
": "sv << strerror(errno);
777 render_fd.el = dup(fd.el);
779 free(rendernode_path);
781 BOOST_LOG(warning) <<
"No render device name for: "sv << path;
782 render_fd.el = dup(fd.el);
785 if (drmSetClientCap(fd.el, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) {
786 BOOST_LOG(error) <<
"GPU driver doesn't support universal planes: "sv << path;
790 if (drmSetClientCap(fd.el, DRM_CLIENT_CAP_ATOMIC, 1)) {
791 BOOST_LOG(warning) <<
"GPU driver doesn't support atomic mode-setting: "sv << path;
792#if defined(HERMES_BUILD_X11)
796 BOOST_LOG(info) <<
"Avoiding KMS capture under X11 due to lack of atomic mode-setting"sv;
800 BOOST_LOG(warning) <<
"Cursor capture may fail without atomic mode-setting support!"sv;
803 plane_res.reset(drmModeGetPlaneResources(fd.el));
805 BOOST_LOG(error) <<
"Couldn't get drm plane resources"sv;
812 fb_t fb(plane_t::pointer plane) {
815 auto fb2 = drmModeGetFB2(fd.el, plane->fb_id);
817 return std::make_unique<wrapper_fb>(fb2);
820 auto fb = drmModeGetFB(fd.el, plane->fb_id);
822 return std::make_unique<wrapper_fb>(fb);
828 crtc_t crtc(std::uint32_t
id) {
829 return drmModeGetCrtc(fd.el,
id);
832 encoder_t encoder(std::uint32_t
id) {
833 return drmModeGetEncoder(fd.el,
id);
837 return drmModeGetResources(fd.el);
841 version_t ver {drmGetVersion(fd.el)};
842 return ver && ver->name && strncmp(ver->name,
"nvidia-drm", 10) == 0;
845 bool is_cursor(std::uint32_t plane_id) {
846 auto props = plane_props(plane_id);
847 for (
auto &[prop, val] : props) {
848 if (prop->name ==
"type"sv) {
849 if (val == DRM_PLANE_TYPE_CURSOR) {
860 std::optional<std::uint64_t> prop_value_by_name(
const std::vector<std::pair<prop_t, std::uint64_t>> &props, std::string_view name) {
861 for (
auto &[prop, val] : props) {
862 if (prop->name == name) {
869 std::uint32_t get_panel_orientation(std::uint32_t plane_id) {
870 auto props = plane_props(plane_id);
871 auto value = prop_value_by_name(props,
"rotation"sv);
876 BOOST_LOG(error) <<
"Failed to determine panel orientation, defaulting to landscape.";
877 return DRM_MODE_ROTATE_0;
880 int get_crtc_index_by_id(std::uint32_t crtc_id) {
881 auto resources = res();
882 for (
int i = 0; i < resources->count_crtcs; i++) {
883 if (resources->crtcs[i] == crtc_id) {
890 connector_interal_t connector(std::uint32_t
id) {
891 return drmModeGetConnector(fd.el,
id);
894 std::vector<connector_t> monitors(conn_type_count_t &conn_type_count) {
895 auto resources = res();
897 BOOST_LOG(error) <<
"Couldn't get connector resources"sv;
901 std::vector<connector_t> monitors;
902 std::for_each_n(resources->connectors, resources->count_connectors, [
this, &conn_type_count, &monitors](std::uint32_t
id) {
903 auto conn = connector(id);
905 std::uint32_t crtc_id = 0;
907 if (conn->encoder_id) {
908 auto enc = encoder(conn->encoder_id);
910 crtc_id = enc->crtc_id;
914 auto index = ++conn_type_count[conn->connector_type];
917 conn->connector_type,
921 conn->connection == DRM_MODE_CONNECTED,
928 file_t handleFD(std::uint32_t handle) {
931 auto status = drmPrimeHandleToFD(fd.el, handle, 0 , &fb_fd.el);
939 std::vector<std::pair<prop_t, std::uint64_t>> props(std::uint32_t
id, std::uint32_t type) {
940 obj_prop_t obj_prop = drmModeObjectGetProperties(fd.el,
id, type);
945 std::vector<std::pair<prop_t, std::uint64_t>> props;
946 props.reserve(obj_prop->count_props);
948 for (
auto x = 0; x < obj_prop->count_props; ++x) {
949 props.emplace_back(drmModeGetProperty(fd.el, obj_prop->props[x]), obj_prop->prop_values[x]);
955 std::vector<std::pair<prop_t, std::uint64_t>> plane_props(std::uint32_t
id) {
956 return props(
id, DRM_MODE_OBJECT_PLANE);
959 std::vector<std::pair<prop_t, std::uint64_t>> crtc_props(std::uint32_t
id) {
960 return props(
id, DRM_MODE_OBJECT_CRTC);
963 std::vector<std::pair<prop_t, std::uint64_t>> connector_props(std::uint32_t
id) {
964 return props(
id, DRM_MODE_OBJECT_CONNECTOR);
967 plane_t operator[](std::uint32_t index) {
968 return drmModeGetPlane(fd.el, plane_res->planes[index]);
971 std::uint32_t count() {
972 return plane_res->count_planes;
976 return plane_it_t {fd.el, plane_res->planes, plane_res->planes + plane_res->count_planes};
980 return plane_it_t {fd.el, plane_res->planes + plane_res->count_planes};
985 plane_res_t plane_res;
988 std::map<std::uint32_t, monitor_t> map_crtc_to_monitor(
const std::vector<connector_t> &connectors) {
989 std::map<std::uint32_t, monitor_t> result;
991 for (
auto &connector : connectors) {
992 result.emplace(connector.crtc_id, monitor_t {
1008#ifdef HERMES_BUILD_CUDA
1010 struct cuda_host_img_t:
public img_t {
1011 ~cuda_host_img_t()
override {
1012 cuda::free_host(data);
1020 case DRM_FORMAT_XRGB2101010:
1021 case DRM_FORMAT_ARGB2101010:
1023 case DRM_FORMAT_XBGR2101010:
1024 case DRM_FORMAT_ABGR2101010:
1032 void print(plane_t::pointer plane, fb_t::pointer fb, crtc_t::pointer crtc) {
1034 BOOST_LOG(debug) <<
"crtc("sv << crtc->x <<
", "sv << crtc->y <<
')';
1035 BOOST_LOG(debug) <<
"crtc("sv << crtc->width <<
", "sv << crtc->height <<
')';
1036 BOOST_LOG(debug) <<
"plane->possible_crtcs == "sv << plane->possible_crtcs;
1040 <<
"x("sv << plane->x
1041 <<
") y("sv << plane->y
1042 <<
") crtc_x("sv << plane->crtc_x
1043 <<
") crtc_y("sv << plane->crtc_y
1044 <<
") crtc_id("sv << plane->crtc_id
1048 <<
"Resolution: "sv << fb->width <<
'x' << fb->height
1049 <<
": Pitch: "sv << fb->pitches[0]
1050 <<
": Offset: "sv << fb->offsets[0];
1052 std::stringstream ss;
1055 std::for_each_n(plane->formats, plane->count_formats - 1, [&ss](
auto format) {
1056 ss << util::view(format) <<
", "sv;
1059 ss << util::view(plane->formats[plane->count_formats - 1]) <<
']';
1061 BOOST_LOG(debug) << ss.str();
1068 mem_type {mem_type} {
1071 int init(
const std::string &display_name, const ::video::config_t &config) {
1072 delay = std::chrono::nanoseconds {1s} / config.framerate;
1076 int monitor_index = 0;
1077 std::string virtual_card;
1078 constexpr std::string_view virtual_card_prefix {
"VIRTUAL-card"};
1081 if (hermes_card_index >= 0) {
1082 virtual_card =
"card" + std::to_string(hermes_card_index);
1083 }
else if (evdi_card_index >= 0) {
1084 virtual_card =
"card" + std::to_string(evdi_card_index);
1085 }
else if (display_name.rfind(virtual_card_prefix, 0) == 0) {
1086 const auto card_index = display_name.substr(virtual_card_prefix.size());
1087 if (card_index.empty() || card_index.find_first_not_of(
"0123456789") != std::string::npos) {
1088 BOOST_LOG(error) <<
"Invalid virtual display name ["sv << display_name <<
']';
1091 virtual_card =
"card" + card_index;
1092 }
else if (display_name.rfind(
"VIRTUAL-", 0) != 0) {
1094 monitor_index = util::from_view(display_name);
1096 BOOST_LOG(error) <<
"Virtual display ["sv << display_name <<
"] has no DRM card mapping"sv;
1101 fs::path card_dir {
"/dev/dri"sv};
1102 for (
auto &entry : fs::directory_iterator {card_dir}) {
1103 auto file = entry.path().filename();
1105 auto filestring = file.generic_string();
1106 if (filestring.size() < 4 || std::string_view {filestring}.substr(0, 4) !=
"card"sv) {
1109 if (!virtual_card.empty() && filestring != virtual_card) {
1114 if (card.init(entry.path().c_str())) {
1120 if (virtual_card.empty() && mem_type ==
mem_type_e::cuda && !card.is_nvidia()) {
1121 BOOST_LOG(debug) << file <<
" is not a CUDA device"sv;
1122 if (config::video.encoder !=
"nvenc") {
1127 auto end = std::end(card);
1128 for (
auto plane = std::begin(card); plane != end; ++plane) {
1130 if (!plane->fb_id) {
1134 if (card.is_cursor(plane->plane_id)) {
1138 if (monitor != monitor_index) {
1143 auto fb = card.fb(plane.get());
1145 BOOST_LOG(error) <<
"Couldn't get drm fb for plane ["sv << plane->fb_id <<
"]: "sv << strerror(errno);
1149 if (!fb->handles[0]) {
1150 BOOST_LOG(error) <<
"Couldn't get handle for DRM Framebuffer ["sv << plane->fb_id <<
"]: Probably not permitted"sv;
1154 for (
int i = 0; i < 4; ++i) {
1155 if (!fb->handles[i]) {
1159 auto fb_fd = card.handleFD(fb->handles[i]);
1161 BOOST_LOG(error) <<
"Couldn't get primary file descriptor for Framebuffer ["sv << fb->fb_id <<
"]: "sv << strerror(errno);
1166 auto crtc = card.crtc(plane->crtc_id);
1168 BOOST_LOG(error) <<
"Couldn't get CRTC info: "sv << strerror(errno);
1172 BOOST_LOG(info) <<
"Found monitor for DRM screencasting"sv;
1175 auto pos = std::find_if(std::begin(card_descriptors), std::end(card_descriptors), [&](
card_descriptor_t &cd) {
1176 return cd.path == filestring;
1179 if (pos == std::end(card_descriptors) && virtual_card.empty()) {
1182 BOOST_LOG(error) <<
"Couldn't find ["sv << entry.path() <<
"]: This shouldn't have happened :/"sv;
1188 kms::print(plane.get(), fb.get(), crtc.get());
1190 img_width = fb->width;
1191 img_height = fb->height;
1192 img_offset_x = crtc->x;
1193 img_offset_y = crtc->y;
1195 this->env_width = ::platf::kms::env_width;
1196 this->env_height = ::platf::kms::env_height;
1198 if (pos != std::end(card_descriptors) &&
1199 pos->crtc_to_monitor.find(plane->crtc_id) != std::end(pos->crtc_to_monitor)) {
1200 auto monitor = pos->crtc_to_monitor.find(plane->crtc_id);
1201 auto &viewport = monitor->second.viewport;
1203 width = viewport.width;
1204 height = viewport.height;
1206 switch (card.get_panel_orientation(plane->plane_id)) {
1207 case DRM_MODE_ROTATE_270:
1208 BOOST_LOG(debug) <<
"Detected panel orientation at 90, swapping width and height.";
1209 width = viewport.height;
1210 height = viewport.width;
1212 case DRM_MODE_ROTATE_90:
1213 case DRM_MODE_ROTATE_180:
1214 BOOST_LOG(warning) <<
"Panel orientation is unsupported, screen capture may not work correctly.";
1218 offset_x = viewport.offset_x;
1219 offset_y = viewport.offset_y;
1227 BOOST_LOG(virtual_card.empty() ? warning : debug)
1228 <<
"Using CRTC geometry because no monitor descriptor is available."sv;
1229 width = crtc->width;
1230 height = crtc->height;
1233 if (!virtual_card.empty()) {
1237 this->env_width = width;
1238 this->env_height = height;
1242 plane_id = plane->plane_id;
1243 crtc_id = plane->crtc_id;
1244 crtc_index = card.get_crtc_index_by_id(plane->crtc_id);
1247 kms::conn_type_count_t conn_type_count;
1248 for (
auto &connector : card.monitors(conn_type_count)) {
1249 if (connector.crtc_id == crtc_id) {
1250 BOOST_LOG(info) <<
"Found connector ID ["sv << connector.connector_id <<
']';
1252 connector_id = connector.connector_id;
1256 if (virtual_card.empty()) {
1257 auto conn = card.connector(connector.connector_id);
1259 const auto name = kernel_connector_name(conn->connector_type, conn->connector_type_id);
1260 if (!name.empty()) {
1261 VDISPLAY::notePhysicalCaptureOutput(name);
1266 auto connector_props = card.connector_props(*connector_id);
1267 hdr_metadata_blob_id = card.prop_value_by_name(connector_props,
"HDR_OUTPUT_METADATA"sv);
1271 this->card = std::move(card);
1276 BOOST_LOG(error) <<
"Couldn't find monitor ["sv << monitor_index <<
']';
1283 cursor_plane_id = -1;
1284 auto end = std::end(card);
1285 for (
auto plane = std::begin(card); plane != end; ++plane) {
1286 if (!card.is_cursor(plane->plane_id)) {
1293 if (!(plane->possible_crtcs & (1 << crtc_index))) {
1296 }
else if (plane->possible_crtcs != (1 << crtc_index)) {
1300 BOOST_LOG(warning) <<
"Cursor plane spans multiple CRTCs!"sv;
1303 BOOST_LOG(info) <<
"Found cursor plane ["sv << plane->plane_id <<
']';
1304 cursor_plane_id = plane->plane_id;
1308 if (cursor_plane_id < 0) {
1309 BOOST_LOG(warning) <<
"No KMS cursor plane found. Cursor may not be displayed while streaming!"sv;
1316 if (!hdr_metadata_blob_id || *hdr_metadata_blob_id == 0) {
1320 prop_blob_t hdr_metadata_blob = drmModeGetPropertyBlob(card.fd.el, *hdr_metadata_blob_id);
1321 if (hdr_metadata_blob ==
nullptr) {
1322 BOOST_LOG(error) <<
"Unable to get HDR metadata blob: "sv << strerror(errno);
1326 if (hdr_metadata_blob->length <
sizeof(uint32_t) +
sizeof(hdr_metadata_infoframe)) {
1327 BOOST_LOG(error) <<
"HDR metadata blob is too small: "sv << hdr_metadata_blob->length;
1331 auto raw_metadata = (hdr_output_metadata *) hdr_metadata_blob->data;
1332 if (raw_metadata->metadata_type != 0) {
1333 BOOST_LOG(error) <<
"Unknown HDMI_STATIC_METADATA_TYPE value: "sv << raw_metadata->metadata_type;
1337 if (raw_metadata->hdmi_metadata_type1.metadata_type != 0) {
1338 BOOST_LOG(error) <<
"Unknown secondary metadata type value: "sv << raw_metadata->hdmi_metadata_type1.metadata_type;
1344 switch (raw_metadata->hdmi_metadata_type1.eotf) {
1348 BOOST_LOG(warning) <<
"Unsupported HDR EOTF: Traditional Gamma"sv;
1353 BOOST_LOG(warning) <<
"Unsupported HDR EOTF: HLG"sv;
1356 BOOST_LOG(warning) <<
"Unsupported HDR EOTF: "sv << raw_metadata->hdmi_metadata_type1.eotf;
1365 bool get_hdr_metadata(SS_HDR_METADATA &metadata) {
1371 prop_blob_t hdr_metadata_blob = drmModeGetPropertyBlob(card.fd.el, *hdr_metadata_blob_id);
1372 if (hdr_metadata_blob ==
nullptr) {
1373 BOOST_LOG(error) <<
"Unable to get HDR metadata blob: "sv << strerror(errno);
1377 auto raw_metadata = (hdr_output_metadata *) hdr_metadata_blob->data;
1379 for (
int i = 0; i < 3; i++) {
1380 metadata.displayPrimaries[i].x = raw_metadata->hdmi_metadata_type1.display_primaries[i].x;
1381 metadata.displayPrimaries[i].y = raw_metadata->hdmi_metadata_type1.display_primaries[i].y;
1384 metadata.whitePoint.x = raw_metadata->hdmi_metadata_type1.white_point.x;
1385 metadata.whitePoint.y = raw_metadata->hdmi_metadata_type1.white_point.y;
1386 metadata.maxDisplayLuminance = raw_metadata->hdmi_metadata_type1.max_display_mastering_luminance;
1387 metadata.minDisplayLuminance = raw_metadata->hdmi_metadata_type1.min_display_mastering_luminance;
1388 metadata.maxContentLightLevel = raw_metadata->hdmi_metadata_type1.max_cll;
1389 metadata.maxFrameAverageLightLevel = raw_metadata->hdmi_metadata_type1.max_fall;
1394 void update_cursor() {
1395 if (cursor_plane_id < 0) {
1399 plane_t plane = drmModeGetPlane(card.fd.el, cursor_plane_id);
1405 BOOST_LOG(error) <<
"Couldn't get drm cursor plane ["sv << cursor_plane_id <<
"]: "sv << strerror(errno);
1406 cursor_plane_id = -1;
1407 captured_cursor.visible =
false;
1411 std::optional<std::int32_t> prop_crtc_x;
1412 std::optional<std::int32_t> prop_crtc_y;
1413 std::optional<std::uint32_t> prop_crtc_w;
1414 std::optional<std::uint32_t> prop_crtc_h;
1416 std::optional<std::uint64_t> prop_src_x;
1417 std::optional<std::uint64_t> prop_src_y;
1418 std::optional<std::uint64_t> prop_src_w;
1419 std::optional<std::uint64_t> prop_src_h;
1421 auto props = card.plane_props(cursor_plane_id);
1422 for (
auto &[prop, val] : props) {
1423 if (prop->name ==
"CRTC_X"sv) {
1425 }
else if (prop->name ==
"CRTC_Y"sv) {
1427 }
else if (prop->name ==
"CRTC_W"sv) {
1429 }
else if (prop->name ==
"CRTC_H"sv) {
1431 }
else if (prop->name ==
"SRC_X"sv) {
1433 }
else if (prop->name ==
"SRC_Y"sv) {
1435 }
else if (prop->name ==
"SRC_W"sv) {
1437 }
else if (prop->name ==
"SRC_H"sv) {
1442 if (!prop_crtc_w || !prop_crtc_h || !prop_crtc_x || !prop_crtc_y) {
1443 BOOST_LOG(error) <<
"Cursor plane is missing required plane CRTC properties!"sv;
1444 BOOST_LOG(error) <<
"Atomic mode-setting must be enabled to capture the cursor!"sv;
1445 cursor_plane_id = -1;
1446 captured_cursor.visible =
false;
1449 if (!prop_src_x || !prop_src_y || !prop_src_w || !prop_src_h) {
1450 BOOST_LOG(error) <<
"Cursor plane is missing required plane SRC properties!"sv;
1451 BOOST_LOG(error) <<
"Atomic mode-setting must be enabled to capture the cursor!"sv;
1452 cursor_plane_id = -1;
1453 captured_cursor.visible =
false;
1458 captured_cursor.x = *prop_crtc_x;
1459 captured_cursor.y = *prop_crtc_y;
1460 captured_cursor.dst_w = *prop_crtc_w;
1461 captured_cursor.dst_h = *prop_crtc_h;
1466 bool cursor_dirty =
false;
1468 if (!plane->fb_id) {
1469 captured_cursor.visible =
false;
1470 captured_cursor.fb_id = 0;
1471 }
else if (plane->fb_id != captured_cursor.fb_id) {
1472 BOOST_LOG(debug) <<
"Refreshing cursor image after FB changed"sv;
1473 cursor_dirty =
true;
1474 }
else if (*prop_src_x != captured_cursor.prop_src_x ||
1475 *prop_src_y != captured_cursor.prop_src_y ||
1476 *prop_src_w != captured_cursor.prop_src_w ||
1477 *prop_src_h != captured_cursor.prop_src_h) {
1478 BOOST_LOG(debug) <<
"Refreshing cursor image after source dimensions changed"sv;
1479 cursor_dirty =
true;
1484 auto fb = card.fb(plane.get());
1485 if (!fb || !fb->handles[0]) {
1487 captured_cursor.visible =
false;
1492 if (fb->pixel_format != DRM_FORMAT_ARGB8888) {
1493 BOOST_LOG(error) <<
"Unsupported non-ARGB8888 cursor format: "sv << fb->pixel_format;
1494 captured_cursor.visible =
false;
1495 cursor_plane_id = -1;
1500 if (fb->modifier != DRM_FORMAT_MOD_LINEAR && fb->modifier != DRM_FORMAT_MOD_INVALID) {
1501 BOOST_LOG(error) <<
"Unsupported non-linear cursor modifier: "sv << fb->modifier;
1502 captured_cursor.visible =
false;
1503 cursor_plane_id = -1;
1508 auto src_x = *prop_src_x >> 16;
1509 auto src_y = *prop_src_y >> 16;
1510 auto src_w = *prop_src_w >> 16;
1511 auto src_h = *prop_src_h >> 16;
1514 if (src_x + src_w > fb->width || src_y + src_h > fb->height) {
1515 BOOST_LOG(error) <<
"Illegal source size: ["sv << src_x + src_w <<
',' << src_y + src_h <<
"] > ["sv << fb->width <<
',' << fb->height <<
']';
1516 captured_cursor.visible =
false;
1520 file_t plane_fd = card.handleFD(fb->handles[0]);
1521 if (plane_fd.el < 0) {
1522 captured_cursor.visible =
false;
1527 size_t mapped_size = ((size_t) fb->pitches[0]) * fb->height;
1528 void *mapped_data = mmap(
nullptr, mapped_size, PROT_READ, MAP_SHARED, plane_fd.el, fb->offsets[0]);
1531 if (mapped_data == MAP_FAILED && errno == ENOSYS) {
1532 drm_mode_map_dumb map = {};
1533 map.handle = fb->handles[0];
1534 if (drmIoctl(card.fd.el, DRM_IOCTL_MODE_MAP_DUMB, &map) < 0) {
1535 BOOST_LOG(error) <<
"Failed to map cursor FB as dumb buffer: "sv << strerror(errno);
1536 captured_cursor.visible =
false;
1540 mapped_data = mmap(
nullptr, mapped_size, PROT_READ, MAP_SHARED, card.fd.el, map.offset);
1543 if (mapped_data == MAP_FAILED) {
1544 BOOST_LOG(error) <<
"Failed to mmap cursor FB: "sv << strerror(errno);
1545 captured_cursor.visible =
false;
1549 captured_cursor.pixels.resize(src_w * src_h * 4);
1552 struct dma_buf_sync sync;
1553 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ;
1554 drmIoctl(plane_fd.el, DMA_BUF_IOCTL_SYNC, &sync);
1557 if (fb->pitches[0] == src_w * 4 && src_x == 0) {
1558 memcpy(captured_cursor.pixels.data(), &((std::uint8_t *) mapped_data)[src_y * fb->pitches[0]], src_h * fb->pitches[0]);
1561 auto pixel_dst = captured_cursor.pixels.data();
1562 for (
int y = 0; y < src_h; y++) {
1563 memcpy(&pixel_dst[y * (src_w * 4)], &((std::uint8_t *) mapped_data)[(y + src_y) * fb->pitches[0] + (src_x * 4)], src_w * 4);
1568 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
1569 drmIoctl(plane_fd.el, DMA_BUF_IOCTL_SYNC, &sync);
1571 munmap(mapped_data, mapped_size);
1573 captured_cursor.visible =
true;
1574 captured_cursor.src_w = src_w;
1575 captured_cursor.src_h = src_h;
1576 captured_cursor.prop_src_x = *prop_src_x;
1577 captured_cursor.prop_src_y = *prop_src_y;
1578 captured_cursor.prop_src_w = *prop_src_w;
1579 captured_cursor.prop_src_h = *prop_src_h;
1580 captured_cursor.fb_id = plane->fb_id;
1581 ++captured_cursor.serial;
1585 inline capture_e refresh(file_t *file, egl::surface_descriptor_t *sd, std::optional<std::chrono::steady_clock::time_point> &frame_timestamp) {
1588 auto connector_props = card.connector_props(*connector_id);
1589 if (hdr_metadata_blob_id != card.prop_value_by_name(connector_props,
"HDR_OUTPUT_METADATA"sv)) {
1590 BOOST_LOG(info) <<
"Reinitializing capture after HDR metadata change"sv;
1591 return capture_e::reinit;
1595 plane_t plane = drmModeGetPlane(card.fd.el, plane_id);
1596 frame_timestamp = std::chrono::steady_clock::now();
1603 BOOST_LOG(warning) <<
"Couldn't get drm plane ["sv << plane_id <<
"]: "sv << strerror(errno);
1604 return capture_e::reinit;
1607 auto fb = card.fb(plane.get());
1613 BOOST_LOG(warning) <<
"Couldn't get drm fb for plane ["sv << plane->fb_id <<
"]: "sv << strerror(errno)
1614 <<
"; waiting for the display to show a picture again"sv;
1617 return capture_e::timeout;
1620 BOOST_LOG(info) <<
"The display shows a picture again; capture resumed"sv;
1624 if (!fb->handles[0]) {
1625 BOOST_LOG(error) <<
"Couldn't get handle for DRM Framebuffer ["sv << plane->fb_id <<
"]: Probably not permitted"sv;
1626 return capture_e::error;
1629 for (
int y = 0; y < 4; ++y) {
1630 if (!fb->handles[y]) {
1640 file[y] = card.handleFD(fb->handles[y]);
1641 if (file[y].el < 0) {
1642 BOOST_LOG(error) <<
"Couldn't get primary file descriptor for Framebuffer ["sv << fb->fb_id <<
"]: "sv << strerror(errno);
1643 return capture_e::error;
1646 sd->fds[y] = file[y].el;
1647 sd->offsets[y] = fb->offsets[y];
1648 sd->pitches[y] = fb->pitches[y];
1651 sd->width = fb->width;
1652 sd->height = fb->height;
1653 sd->modifier = fb->modifier;
1654 sd->fourcc = fb->pixel_format;
1657 fb->width != img_width ||
1658 fb->height != img_height
1660 return capture_e::reinit;
1665 return capture_e::ok;
1670 std::chrono::nanoseconds delay;
1672 int img_width, img_height;
1673 int img_offset_x, img_offset_y;
1680 std::optional<uint32_t> connector_id;
1681 std::optional<uint64_t> hdr_metadata_blob_id;
1683 int cursor_plane_id;
1689 class display_ram_t:
public display_t {
1692 display_t(mem_type) {
1695 int init(
const std::string &display_name, const ::video::config_t &config) {
1696 if (!gbm::create_device) {
1697 BOOST_LOG(warning) <<
"libgbm not initialized"sv;
1701 if (display_t::init(display_name, config)) {
1705 gbm.reset(gbm::create_device(card.fd.el));
1707 BOOST_LOG(error) <<
"Couldn't create GBM device: ["sv << util::hex(eglGetError()).to_string_view() <<
']';
1711 display = egl::make_display(gbm.get());
1716 auto ctx_opt = egl::make_ctx(display.get());
1721 ctx = std::move(*ctx_opt);
1727 auto next_frame = std::chrono::steady_clock::now();
1729 sleep_overshoot_logger.reset();
1732 auto now = std::chrono::steady_clock::now();
1734 if (next_frame > now) {
1735 std::this_thread::sleep_for(next_frame - now);
1736 sleep_overshoot_logger.first_point(next_frame);
1737 sleep_overshoot_logger.second_point_now_and_log();
1740 next_frame += delay;
1741 if (next_frame < now) {
1742 next_frame = now + delay;
1745 std::shared_ptr<platf::img_t> img_out;
1746 auto status = snapshot(pull_free_image_cb, img_out, 1000ms, *cursor);
1753 if (!push_captured_image_cb(std::move(img_out),
false)) {
1758 if (!push_captured_image_cb(std::move(img_out),
true)) {
1763 BOOST_LOG(error) <<
"Unrecognized capture status ["sv << (int) status <<
']';
1772 std::unique_ptr<avcodec_encode_device_t> make_pyrowave_encode_device()
override {
1773 return pyrowave::make_ram_encode_device(width, height);
1777 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e pix_fmt)
override {
1778#ifdef HERMES_BUILD_VAAPI
1780 return va::make_avcodec_encode_device(width, height,
false);
1784#ifdef HERMES_BUILD_CUDA
1785 if (mem_type == mem_type_e::cuda) {
1786 return cuda::make_avcodec_encode_device(width, height,
false);
1790 return std::make_unique<avcodec_encode_device_t>();
1793 void blend_cursor(img_t &img) {
1796 auto pixels = (
int *) img.data;
1798 int32_t screen_height = img.height;
1799 int32_t screen_width = img.width;
1802 auto cursor_x = std::max<int32_t>(0, captured_cursor.x - img_offset_x);
1803 auto cursor_y = std::max<int32_t>(0, captured_cursor.y - img_offset_y);
1807 auto cursor_delta_x = cursor_x - std::max<int32_t>(-captured_cursor.src_w, captured_cursor.x - img_offset_x);
1808 auto cursor_delta_y = cursor_y - std::max<int32_t>(-captured_cursor.src_h, captured_cursor.y - img_offset_y);
1810 auto delta_height = std::min<uint32_t>(captured_cursor.src_h, std::max<int32_t>(0, screen_height - cursor_y)) - cursor_delta_y;
1811 auto delta_width = std::min<uint32_t>(captured_cursor.src_w, std::max<int32_t>(0, screen_width - cursor_x)) - cursor_delta_x;
1812 for (
auto y = 0; y < delta_height; ++y) {
1819 auto cursor_begin = (uint32_t *) &captured_cursor.pixels.data()[((y + cursor_delta_y) * captured_cursor.src_w + cursor_delta_x) * 4];
1820 auto cursor_end = (uint32_t *) &captured_cursor.pixels.data()[((y + cursor_delta_y) * captured_cursor.src_w + delta_width + cursor_delta_x) * 4];
1822 auto pixels_begin = &pixels[(y + cursor_y) * (img.row_pitch / img.pixel_pitch) + cursor_x];
1824 std::for_each(cursor_begin, cursor_end, [&](uint32_t cursor_pixel) {
1825 auto colors_in = (uint8_t *) pixels_begin;
1827 auto alpha = (*(uint *) &cursor_pixel) >> 24u;
1829 *pixels_begin = cursor_pixel;
1831 auto colors_out = (uint8_t *) &cursor_pixel;
1832 colors_in[0] = colors_out[0] + (colors_in[0] * (255 - alpha) + 255 / 2) / 255;
1833 colors_in[1] = colors_out[1] + (colors_in[1] * (255 - alpha) + 255 / 2) / 255;
1834 colors_in[2] = colors_out[2] + (colors_in[2] * (255 - alpha) + 255 / 2) / 255;
1841 capture_e snapshot(
const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds timeout,
bool cursor) {
1844 egl::surface_descriptor_t sd;
1846 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
1847 auto status = refresh(fb_fd, &sd, frame_timestamp);
1848 if (status != capture_e::ok) {
1852 auto rgb_opt = egl::import_source(
display.get(), sd);
1855 return capture_e::error;
1858 auto &rgb = *rgb_opt;
1860 gl::ctx.BindTexture(GL_TEXTURE_2D, rgb->tex[0]);
1864 gl::ctx.GetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
1865 gl::ctx.GetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
1866 BOOST_LOG(debug) <<
"width and height: w "sv << w <<
" h "sv << h;
1868 if (!pull_free_image_cb(img_out)) {
1869 return platf::capture_e::interrupted;
1872 gl::ctx.GetTextureSubImage(rgb->tex[0], 0, img_offset_x, img_offset_y, 0, width, height, 1, GL_BGRA, GL_UNSIGNED_BYTE, img_out->height * img_out->row_pitch, img_out->data);
1874 img_out->frame_timestamp = frame_timestamp;
1876 if (cursor && captured_cursor.visible) {
1877 blend_cursor(*img_out);
1880 return capture_e::ok;
1883 std::shared_ptr<img_t> alloc_img()
override {
1884 auto img = std::make_shared<kms_img_t>();
1886 img->height = height;
1887 img->pixel_pitch = 4;
1888 img->row_pitch = img->pixel_pitch * width;
1889 img->data =
new std::uint8_t[height * img->row_pitch];
1894 int dummy_img(platf::img_t *img)
override {
1908 explicit display_evdi_t(
mem_type_e mem_type): mem_type {mem_type} {}
1910 int init(
const std::string &display_name, const ::video::config_t &config) {
1913 BOOST_LOG(error) <<
"No EVDI pixel buffer is available for " << display_name;
1916 delay = std::chrono::nanoseconds {1s} / config.framerate;
1917 width =
static_cast<int>(buffer->width());
1918 height =
static_cast<int>(buffer->height());
1922 env_height = height;
1929 auto next_frame = std::chrono::steady_clock::now();
1930 auto last_frame = buffer->frame_number();
1932 bool copied_any =
false;
1933 std::uint64_t copied_frame = 0;
1936 const auto now = std::chrono::steady_clock::now();
1937 if (next_frame > now) {
1938 const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(next_frame - now);
1939 last_frame = buffer->wait_for_update(last_frame, std::max(remaining, std::chrono::milliseconds {1}));
1941 next_frame += delay;
1942 if (next_frame < now) {
1943 next_frame = now + delay;
1946 std::shared_ptr<platf::img_t> img_out;
1947 if (!pull_free_image_cb(img_out)) {
1954 if (copied_any && buffer->frame_number() == copied_frame) {
1955 if (!push_captured_image_cb(std::move(img_out),
false)) {
1960 const auto t0 = std::chrono::steady_clock::now();
1961 copied_frame = buffer->copy_to(img_out->data, img_out->row_pitch);
1963 const auto t1 = std::chrono::steady_clock::now();
1964 accumulate_capture_metric(
"evdi", std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count());
1965 img_out->frame_timestamp = std::chrono::steady_clock::now();
1966 if (!push_captured_image_cb(std::move(img_out),
true)) {
1973 std::unique_ptr<avcodec_encode_device_t> make_pyrowave_encode_device()
override {
1974 return pyrowave::make_ram_encode_device(width, height);
1978 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e)
override {
1979#ifdef HERMES_BUILD_VAAPI
1981 return va::make_avcodec_encode_device(width, height,
false);
1984#ifdef HERMES_BUILD_CUDA
1985 if (mem_type == mem_type_e::cuda) {
1986 return cuda::make_avcodec_encode_device(width, height,
false);
1989 return std::make_unique<avcodec_encode_device_t>();
1992 std::shared_ptr<img_t> alloc_img()
override {
1993 auto img = std::make_shared<kms_img_t>();
1995 img->height = height;
1996 img->pixel_pitch = 4;
1997 img->row_pitch = img->pixel_pitch * width;
1998 img->data =
new std::uint8_t[height * img->row_pitch];
2002 int dummy_img(platf::img_t *)
override {
return 0; }
2006 std::chrono::nanoseconds delay;
2007 std::shared_ptr<VDISPLAY::EvdiBuffer> buffer;
2010 class display_vram_t:
public display_t {
2013 display_t(mem_type) {
2018 return pyrowave::make_gpu_encode_device(width, height, dup(card.render_fd.el), img_offset_x, img_offset_y);
2022 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e pix_fmt)
override {
2023#ifdef HERMES_BUILD_VAAPI
2025 return va::make_avcodec_encode_device(width, height, dup(card.render_fd.el), img_offset_x, img_offset_y,
true);
2029#ifdef HERMES_BUILD_CUDA
2035 BOOST_LOG(error) <<
"Unsupported pixel format for egl::display_vram_t: "sv << platf::from_pix_fmt(pix_fmt);
2039 std::shared_ptr<img_t> alloc_img()
override {
2040 auto img = std::make_shared<egl::img_descriptor_t>();
2043 img->height = height;
2044 img->serial = std::numeric_limits<
decltype(img->serial)>::max();
2045 img->data =
nullptr;
2046 img->pixel_pitch = 4;
2049 std::fill_n(img->sd.fds, 4, -1);
2060 auto next_frame = std::chrono::steady_clock::now();
2062 sleep_overshoot_logger.reset();
2065 auto now = std::chrono::steady_clock::now();
2067 if (next_frame > now) {
2068 std::this_thread::sleep_for(next_frame - now);
2069 sleep_overshoot_logger.first_point(next_frame);
2070 sleep_overshoot_logger.second_point_now_and_log();
2073 next_frame += delay;
2074 if (next_frame < now) {
2075 next_frame = now + delay;
2078 std::shared_ptr<platf::img_t> img_out;
2079 auto status = snapshot(pull_free_image_cb, img_out, 1000ms, *cursor);
2086 if (!push_captured_image_cb(std::move(img_out),
false)) {
2091 if (!push_captured_image_cb(std::move(img_out),
true)) {
2096 BOOST_LOG(error) <<
"Unrecognized capture status ["sv << (int) status <<
']';
2104 capture_e snapshot(
const pull_free_image_cb_t &pull_free_image_cb, std::shared_ptr<platf::img_t> &img_out, std::chrono::milliseconds ,
bool cursor) {
2107 if (!pull_free_image_cb(img_out)) {
2113 auto status = refresh(fb_fd, &img->sd, img->frame_timestamp);
2118 img->sequence = ++sequence;
2120 if (cursor && captured_cursor.visible) {
2122 if (img->serial != captured_cursor.serial) {
2123 img->buffer = captured_cursor.pixels;
2124 img->serial = captured_cursor.serial;
2127 img->x = captured_cursor.x;
2128 img->y = captured_cursor.y;
2129 img->src_w = captured_cursor.src_w;
2130 img->src_h = captured_cursor.src_h;
2131 img->width = captured_cursor.dst_w;
2132 img->height = captured_cursor.dst_h;
2133 img->pixel_pitch = 4;
2134 img->row_pitch = img->pixel_pitch * img->width;
2135 img->data = img->buffer.data();
2137 img->data =
nullptr;
2140 for (
auto x = 0; x < 4; ++x) {
2143 return capture_e::ok;
2146 int init(
const std::string &display_name, const ::video::config_t &config) {
2147 if (display_t::init(display_name, config)) {
2151#ifdef HERMES_BUILD_VAAPI
2152 if (mem_type == mem_type_e::vaapi && !va::validate(card.render_fd.el)) {
2153 BOOST_LOG(warning) <<
"Monitor "sv << display_name <<
" doesn't support hardware encoding. Reverting back to GPU -> RAM -> GPU"sv;
2158#ifndef HERMES_BUILD_CUDA
2159 if (mem_type == mem_type_e::cuda) {
2160 BOOST_LOG(warning) <<
"Attempting to use NVENC without CUDA support. Reverting back to GPU -> RAM -> GPU"sv;
2168 std::uint64_t sequence {};
2185 if (current == recorded) {
2189 BOOST_LOG(info) <<
"Hermes-KMS capture: the desktop layout changed; reinitialising to re-resolve "
2190 "the input geometry."sv;
2208 const std::string &display_name,
2219 env_height = height;
2230 int desktop_width = 0;
2231 int desktop_height = 0;
2239 BOOST_LOG(warning) <<
"Could not resolve compositor geometry for ["sv << display_name
2240 <<
"]; absolute input will use output-local coordinates, which are only "
2241 "correct while the virtual display is the whole desktop."sv;
2248 if (desktop_x < 0 || desktop_y < 0 || desktop_width <= 0 || desktop_height <= 0 ||
2249 desktop_x >= desktop_width || desktop_y >= desktop_height) {
2250 BOOST_LOG(warning) <<
"Compositor reported an unusable desktop geometry for ["sv << display_name
2251 <<
"]: "sv << width <<
'x' << height <<
" at "sv << desktop_x <<
',' << desktop_y
2252 <<
" in "sv << desktop_width <<
'x' << desktop_height
2253 <<
"; keeping output-local coordinates."sv;
2257 offset_x = desktop_x;
2258 offset_y = desktop_y;
2259 env_width = desktop_width;
2260 env_height = desktop_height;
2261 BOOST_LOG(info) <<
"Hermes-KMS input geometry for ["sv << display_name <<
"]: "
2262 << offset_x <<
',' << offset_y <<
" in "
2263 << env_width <<
'x' << env_height;
2266 bool wait_hermes_capture_size(
int fd,
int &width,
int &height) {
2269 constexpr uint32_t timeout_ms = 1000;
2273 const int capture_error = errno;
2274 if (capture_error == ETIMEDOUT) {
2275 BOOST_LOG(error) <<
"Hermes-KMS capture: timed out after "sv << timeout_ms
2276 <<
"ms waiting for active scanout and its framebuffer."sv;
2278 BOOST_LOG(
error) <<
"Hermes-KMS capture: GET_STATUS failed: "sv
2279 << std::strerror(capture_error) <<
" (errno="sv << capture_error <<
')';
2291 class display_hermes_vram_t:
public display_vram_t {
2294 display_vram_t(mem_type) {
2297 bool is_hdr()
override {
2298 return source_color.pq();
2301 bool get_hdr_metadata(SS_HDR_METADATA &metadata)
override {
2302 return source_color.copy_hdr_metadata(metadata);
2306 bool probe_only {
false};
2307 uint32_t source_fourcc {};
2309 ~display_hermes_vram_t()
override {
2310 if (hermes_fd >= 0) {
2316 int init(
const std::string &display_name, const ::video::config_t &config) {
2317 probe_only = config.encoder_probe;
2318 BOOST_LOG(info) <<
"Hermes-KMS zero-copy capture path selected for ["sv << display_name <<
']';
2321 if (hermes_fd < 0) {
2327 if (!wait_hermes_capture_size(hermes_fd, w, h)) {
2332 BOOST_LOG(error) <<
"Hermes-KMS: cannot acquire initial colour state: " << strerror(errno);
2335 if (source_color.pq() && !config.dynamicRange && !config.encoder_probe) {
2336 BOOST_LOG(error) <<
"Hermes-KMS output is HDR but the client requested SDR. Disable HDR on this KDE output or enable HDR in the client.";
2339 if (source_color.known() && source_color.colorspace == VDISPLAY::hermes_kms::colorspace_bt2020_rgb && !source_color.pq()) {
2340 BOOST_LOG(error) <<
"Hermes-KMS: BT.2020 SDR capture requires a matching SDR colour conversion path.";
2344 width = img_width = w;
2345 height = img_height = h;
2355 if (encode_render_fd.el < 0) {
2356 BOOST_LOG(error) <<
"Hermes-KMS capture: no render GPU found for encoding."sv;
2360#ifdef HERMES_BUILD_VAAPI
2362 BOOST_LOG(warning) <<
"Encoding GPU doesn't support VAAPI."sv;
2367#ifndef HERMES_BUILD_CUDA
2369 BOOST_LOG(warning) <<
"Hermes-KMS NVENC path requested but Hermes was built without CUDA support."sv;
2374 BOOST_LOG(info) <<
"Hermes-KMS zero-copy capture ready: "sv << w <<
'x' << h;
2382 return pyrowave::make_gpu_encode_device(width, height, dup(encode_render_fd.el), img_offset_x, img_offset_y);
2386 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e pix_fmt)
override {
2387#ifdef HERMES_BUILD_VAAPI
2390 return va::make_avcodec_encode_device(width, height, dup(encode_render_fd.el), img_offset_x, img_offset_y,
true);
2393#ifdef HERMES_BUILD_CUDA
2407 BOOST_LOG(error) <<
"Hermes-KMS capture supports VAAPI and NVENC(CUDA) encoding only."sv;
2412 if (!pull_free_image_cb(img_out)) {
2422 const auto timeout_ms = hermes_timeout_ms(
timeout);
2423 const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds {timeout_ms};
2424 const bool cursor_mode_changed =
2425 !last_cursor_requested || *last_cursor_requested != cursor_requested;
2426 bool cursor_ready =
false;
2428 if (cursor_requested && !cursor_mode_changed) {
2433 last_cursor_sequence,
2436 return hermes_acquire_failure(
"zero-copy update wait", errno);
2438 cursor_ready = update.cursor_ready;
2439 }
else if (cursor_requested) {
2440 cursor_ready =
true;
2442 if (!VDISPLAY::hermesKmsAcquireFrame(
2445 (cursor_requested || cursor_mode_changed) ? 0U : timeout_ms,
2446 frame, source_color.known())) {
2449 return hermes_acquire_failure(
"zero-copy", errno);
2454 accumulate_capture_metric(
"hermes-kms", frame.
acquire_ns);
2456 if (frame.width != img_width || frame.height != img_height ||
2457 frame.fourcc != source_fourcc || !(frame.color == source_color)) {
2465 bool valid_layout = frame.fourcc != 0;
2466 for (std::uint32_t i = 0; i < frame.plane_count; ++i) {
2467 valid_layout = valid_layout && frame.dma_buf_fd[i] >= 0 && frame.pitch[i] > 0;
2469 if (!valid_layout) {
2470 BOOST_LOG(error) <<
"Hermes-KMS zero-copy capture: invalid DMA-BUF plane metadata."sv;
2475 if (frame.sync_file_fd >= 0) {
2476 const auto fence_status = wait_hermes_frame_fence(frame.sync_file_fd, deadline);
2477 if (fence_status == hermes_fence_wait_e::timeout) {
2478 BOOST_LOG(warning) <<
"Hermes-KMS zero-copy capture: frame fence timed out."sv;
2482 if (fence_status == hermes_fence_wait_e::failed) {
2483 const int fence_errno = errno;
2484 BOOST_LOG(warning) <<
"Hermes-KMS zero-copy capture: dropping frame "sv
2485 << frame.sequence <<
" after producer fence failure: "sv
2486 << strerror(fence_errno);
2487 last_sequence = frame.sequence;
2491 if (fence_status == hermes_fence_wait_e::error) {
2492 const int fence_errno = errno;
2493 BOOST_LOG(error) <<
"Hermes-KMS zero-copy capture: invalid sync file: "sv
2494 << strerror(fence_errno);
2500 std::optional<cursor_t> pending_cursor;
2501 uint64_t pending_cursor_sequence = last_cursor_sequence;
2503 pending_cursor = captured_cursor;
2504 const auto cursor_status = refresh_hermes_cursor(
2508 pending_cursor_sequence
2512 return cursor_status;
2520 img->sd.width = frame.width;
2521 img->sd.height = frame.height;
2522 img->sd.fourcc = frame.fourcc;
2523 img->sd.modifier = frame.modifier;
2524 std::fill_n(img->sd.offsets, 4, 0);
2525 std::fill_n(img->sd.pitches, 4, 0);
2526 for (std::uint32_t i = 0; i < frame.plane_count; ++i) {
2527 img->sd.fds[i] = frame.dma_buf_fd[i];
2528 img->sd.offsets[i] = frame.offset[i];
2529 img->sd.pitches[i] = frame.pitch[i];
2530 frame.dma_buf_fd[i] = -1;
2534 const bool primary_advanced = frame.sequence != last_sequence;
2535 last_sequence = frame.sequence;
2536 if (primary_advanced || !sequence) {
2539 img->sequence = sequence;
2540 img->frame_timestamp = std::chrono::steady_clock::now();
2541 attach_hermes_cursor(
2544 pending_cursor ? *pending_cursor : captured_cursor
2546 if (pending_cursor) {
2547 captured_cursor = std::move(*pending_cursor);
2548 last_cursor_sequence = pending_cursor_sequence;
2550 last_cursor_requested = cursor_requested;
2562 std::shared_ptr<platf::img_t> img_out;
2563 auto status = snapshot(pull_free_image_cb, img_out, 200ms, *cursor);
2570 if (!push_captured_image_cb(std::move(img_out),
false)) {
2575 if (!push_captured_image_cb(std::move(img_out),
true)) {
2580 BOOST_LOG(error) <<
"Unrecognized capture status ["sv << (int) status <<
']';
2590 file_t encode_render_fd;
2591 uint64_t last_sequence {0};
2592 uint64_t last_cursor_sequence {0};
2593 std::optional<bool> last_cursor_requested;
2605 class display_hermes_ram_t:
public display_t {
2608 display_t(mem_type) {
2611 bool is_hdr()
override {
2612 return source_color.pq();
2615 bool get_hdr_metadata(SS_HDR_METADATA &metadata)
override {
2616 return source_color.copy_hdr_metadata(metadata);
2620 bool probe_only {
false};
2621 uint32_t source_fourcc {};
2623 ~display_hermes_ram_t()
override {
2624 if (hermes_fd >= 0) {
2630 int init(
const std::string &display_name, const ::video::config_t &config) {
2631 probe_only = config.encoder_probe;
2632 BOOST_LOG(info) <<
"Hermes-KMS CPU-copy capture path selected for ["sv << display_name <<
']';
2635 if (hermes_fd < 0) {
2641 if (!wait_hermes_capture_size(hermes_fd, w, h)) {
2646 BOOST_LOG(error) <<
"Hermes-KMS: cannot acquire initial colour state: " << strerror(errno);
2649 if (source_color.pq() && !config.dynamicRange && !config.encoder_probe) {
2650 BOOST_LOG(error) <<
"Hermes-KMS output is HDR but the client requested SDR. Disable HDR on this KDE output or enable HDR in the client.";
2653 if (source_color.known() && source_color.colorspace == VDISPLAY::hermes_kms::colorspace_bt2020_rgb && !source_color.pq()) {
2654 BOOST_LOG(error) <<
"Hermes-KMS: BT.2020 SDR capture requires a matching SDR colour conversion path.";
2658 constexpr auto bytes_per_pixel = std::size_t {4};
2659 if (
static_cast<std::size_t
>(w) >
2660 static_cast<std::size_t
>(std::numeric_limits<std::int32_t>::max()) / bytes_per_pixel ||
2661 static_cast<std::size_t
>(h) >
2662 std::numeric_limits<std::size_t>::max() / (
static_cast<std::size_t
>(w) * bytes_per_pixel)) {
2663 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: scanout geometry is too large: "sv
2669 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: scanout format "sv <<
hermes_fourcc_name(source_fourcc)
2670 <<
" is not supported; the compositor must scan out XRGB8888, ARGB8888 or packed 10-bit RGB."sv;
2674 width = img_width = w;
2675 height = img_height = h;
2681 BOOST_LOG(info) <<
"Hermes-KMS CPU-copy capture ready: "sv << w <<
'x' << h <<
' '
2686 std::shared_ptr<img_t> alloc_img()
override {
2687 const auto row_pitch =
static_cast<std::size_t
>(width) * std::size_t {4};
2688 const auto size =
static_cast<std::size_t
>(height) * row_pitch;
2689 const auto describe = [&](
img_t &img) {
2691 img.height = height;
2692 img.pixel_pitch = 4;
2693 img.row_pitch =
static_cast<std::int32_t
>(row_pitch);
2696#ifdef HERMES_BUILD_CUDA
2698 if (
auto *pinned =
static_cast<std::uint8_t *
>(cuda::alloc_host(size))) {
2699 std::memset(pinned, 0, size);
2700 auto img = std::make_shared<cuda_host_img_t>();
2705 if (!warned_pageable) {
2706 BOOST_LOG(warning) <<
"Hermes-KMS CPU capture: page-locked frames unavailable; uploads use pageable memory."sv;
2707 warned_pageable =
true;
2712 auto img = std::make_shared<kms_img_t>();
2714 img->data =
new std::uint8_t[size] {};
2732 std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e pix_fmt)
override {
2733#ifdef HERMES_BUILD_CUDA
2738 return cuda::make_avcodec_encode_device(width, height,
false, hermes_cuda_layout(source_fourcc));
2741#ifdef HERMES_BUILD_VAAPI
2746 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: the VAAPI upload supports 8-bit scanout only, not "sv
2750 return va::make_avcodec_encode_device(width, height,
false);
2753 return std::make_unique<avcodec_encode_device_t>();
2760 if (!pull_free_image_cb(img_out)) {
2769 const auto timeout_ms = hermes_timeout_ms(
timeout);
2770 const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds {timeout_ms};
2771 const bool cursor_mode_changed =
2772 !last_cursor_requested || *last_cursor_requested != cursor_requested;
2773 bool cursor_ready =
false;
2775 if (cursor_requested && !cursor_mode_changed) {
2780 last_cursor_sequence,
2783 return hermes_acquire_failure(
"CPU-copy update wait", errno);
2785 cursor_ready = update.cursor_ready;
2786 }
else if (cursor_requested) {
2787 cursor_ready =
true;
2789 if (!VDISPLAY::hermesKmsAcquireFrame(
2792 (cursor_requested || cursor_mode_changed) ? 0U : timeout_ms,
2793 frame, source_color.known())) {
2794 return hermes_acquire_failure(
"CPU-copy", errno);
2796 accumulate_capture_metric(
"hermes-kms-cpu", frame.
acquire_ns);
2801 if (frame.width != img_width || frame.height != img_height ||
2802 frame.fourcc != source_fourcc || !(frame.color == source_color)) {
2803 if (frame.fourcc != source_fourcc) {
2804 BOOST_LOG(info) <<
"Hermes-KMS CPU capture: scanout format changed from "sv
2811 const auto frame_height =
static_cast<std::size_t
>(frame.height);
2812 const auto row_bytes =
static_cast<std::size_t
>(img_width) * std::size_t {4};
2813 const bool valid_layout =
2814 frame.plane_count == 1 &&
2815 frame.dma_buf_fd[0] >= 0 &&
2817 (frame.modifier == DRM_FORMAT_MOD_LINEAR || frame.modifier == DRM_FORMAT_MOD_INVALID) &&
2818 static_cast<std::size_t
>(frame.pitch[0]) >= row_bytes;
2819 const bool valid_destination =
2820 img_out->data && img_out->width == img_width && img_out->height == img_height &&
2821 img_out->pixel_pitch == 4 && img_out->row_pitch > 0 &&
2822 static_cast<std::size_t
>(img_out->row_pitch) >= row_bytes;
2823 if (!valid_layout || !valid_destination) {
2824 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: unexpected frame layout (planes="sv << frame.plane_count
2825 <<
", fourcc=0x"sv << std::hex << frame.fourcc << std::dec
2826 <<
", modifier=0x"sv << std::hex << frame.modifier << std::dec
2827 <<
", pitch="sv << frame.pitch[0] <<
')';
2836 if (frame.sync_file_fd >= 0) {
2837 const auto fence_status = wait_hermes_frame_fence(frame.sync_file_fd, deadline);
2838 if (fence_status == hermes_fence_wait_e::timeout) {
2839 BOOST_LOG(warning) <<
"Hermes-KMS CPU capture: frame fence timed out."sv;
2843 if (fence_status == hermes_fence_wait_e::failed) {
2844 const int fence_errno = errno;
2845 BOOST_LOG(warning) <<
"Hermes-KMS CPU capture: dropping frame "sv
2846 << frame.sequence <<
" after producer fence failure: "sv
2847 << strerror(fence_errno);
2848 last_sequence = frame.sequence;
2852 if (fence_status == hermes_fence_wait_e::error) {
2853 const int fence_errno = errno;
2854 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: invalid sync file: "sv
2855 << strerror(fence_errno);
2864 const auto frame_ready = std::chrono::steady_clock::now();
2866 std::optional<cursor_t> pending_cursor;
2867 uint64_t pending_cursor_sequence = last_cursor_sequence;
2869 pending_cursor = captured_cursor;
2870 const auto cursor_status = refresh_hermes_cursor(
2874 pending_cursor_sequence
2878 return cursor_status;
2882 const auto source_pitch =
static_cast<std::size_t
>(frame.pitch[0]);
2883 const auto source_offset =
static_cast<std::size_t
>(frame.offset[0]);
2884 const auto last_row = frame_height - 1;
2885 if (last_row > (std::numeric_limits<std::size_t>::max() - source_offset) / source_pitch) {
2886 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: DMA-BUF row offset overflows size_t."sv;
2890 const auto last_row_offset = source_offset + last_row * source_pitch;
2891 if (row_bytes > std::numeric_limits<std::size_t>::max() - last_row_offset) {
2892 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: DMA-BUF mapping length overflows size_t."sv;
2896 const auto map_len = last_row_offset + row_bytes;
2900 const std::uint8_t *map =
mappings.map(frame.dma_buf_fd[0], map_len);
2902 const int map_errno = errno;
2903 if (map_errno == EINVAL) {
2904 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: frame metadata exceeds the DMA-BUF (needs "sv
2905 << map_len <<
" bytes)"sv;
2907 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: could not map the DMA-BUF: "sv
2908 << strerror(map_errno);
2915 if (hermes_dma_buf_sync(frame.dma_buf_fd[0], DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ) < 0) {
2916 const int sync_errno = errno;
2917 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: DMA_BUF_SYNC_START failed: "sv
2918 << strerror(sync_errno);
2925 static_cast<std::size_t
>(img_out->row_pitch),
2926 map + frame.offset[0],
2932 if (hermes_dma_buf_sync(frame.dma_buf_fd[0], DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ) < 0) {
2933 const int sync_errno = errno;
2934 BOOST_LOG(error) <<
"Hermes-KMS CPU capture: DMA_BUF_SYNC_END failed: "sv
2935 << strerror(sync_errno);
2940 const cursor_t &cursor_for_frame = pending_cursor ? *pending_cursor : captured_cursor;
2941 if (cursor_requested && cursor_for_frame.visible) {
2945 last_sequence = frame.sequence;
2947 if (pending_cursor) {
2948 captured_cursor = std::move(*pending_cursor);
2949 last_cursor_sequence = pending_cursor_sequence;
2951 last_cursor_requested = cursor_requested;
2953 img_out->frame_timestamp = frame_ready;
2962 std::shared_ptr<platf::img_t> img_out;
2963 auto status = snapshot(pull_free_image_cb, img_out, 200ms, *cursor);
2970 if (!push_captured_image_cb(std::move(img_out),
false)) {
2975 if (!push_captured_image_cb(std::move(img_out),
true)) {
2980 BOOST_LOG(error) <<
"Unrecognized capture status ["sv << (int) status <<
']';
2990 uint64_t last_sequence {0};
2991 uint64_t last_cursor_sequence {0};
2992 std::optional<bool> last_cursor_requested;
2995 bool warned_pageable {
false};
3000 std::shared_ptr<display_t> kms_display(mem_type_e hwdevice_type,
const std::string &display_name, const ::video::config_t &config) {
3003 auto display = std::make_shared<kms::display_evdi_t>(hwdevice_type);
3004 return display->init(display_name, config) ? nullptr : display;
3011 if (hermes_kms_display) {
3012 if (hwdevice_type != mem_type_e::vaapi && hwdevice_type != mem_type_e::cuda) {
3013 BOOST_LOG(error) <<
"Hermes-KMS capture requires VAAPI or NVENC(CUDA); "sv
3014 <<
"the selected encoder is not supported."sv;
3026 const char *force_cpu_copy = std::getenv(
"HERMES_KMS_FORCE_CPU_COPY");
3027 const bool forced_cpu_copy = force_cpu_copy && !std::strcmp(force_cpu_copy,
"1");
3028 if (forced_cpu_copy && hwdevice_type != mem_type_e::cuda) {
3029 BOOST_LOG(warning) <<
"HERMES_KMS_FORCE_CPU_COPY=1: using the CPU-copy capture path instead of zero-copy."sv;
3031 if (hwdevice_type == mem_type_e::cuda || forced_cpu_copy) {
3032 auto disp = std::make_shared<kms::display_hermes_ram_t>(hwdevice_type);
3033 if (!disp->init(display_name, config)) {
3036 BOOST_LOG(error) <<
"Hermes-KMS CPU-copy capture failed; refusing physical-display fallback."sv;
3040 auto disp = std::make_shared<kms::display_hermes_vram_t>(hwdevice_type);
3041 if (!disp->init(display_name, config)) {
3044 BOOST_LOG(error) <<
"Hermes-KMS zero-copy capture failed; refusing physical-display fallback."sv;
3048 if (hwdevice_type == mem_type_e::vaapi || hwdevice_type == mem_type_e::cuda) {
3049 auto disp = std::make_shared<kms::display_vram_t>(hwdevice_type);
3051 if (!disp->init(display_name, config)) {
3056 if (hermes_kms_display) {
3057 BOOST_LOG(error) <<
"Hermes-KMS RAM capture is not supported; no active scanout framebuffer was found."sv;
3061 auto disp = std::make_shared<kms::display_ram_t>(hwdevice_type);
3063 if (disp->init(display_name, config)) {
3081 auto monitors = wl::monitors();
3083 BOOST_LOG(info) <<
"-------- Start of KMS monitor list --------"sv;
3085 for (
auto &monitor : monitors) {
3086 std::string_view name = monitor->name;
3091 auto index_begin = name.find_last_of(
'-');
3093 std::uint32_t index;
3094 if (index_begin == std::string_view::npos) {
3097 index = std::max<int64_t>(1, util::from_view(name.substr(index_begin + 1)));
3100 auto type = kms::from_view(name.substr(0, index_begin));
3113 for (
auto &card_descriptor : cds) {
3114 for (
auto &[_, monitor_descriptor] : card_descriptor.crtc_to_monitor) {
3115 if (monitor_descriptor.index != index || monitor_descriptor.type != type) {
3119 if (monitor_descriptor.viewport.width == monitor->viewport.width &&
3120 monitor_descriptor.viewport.height == monitor->viewport.height) {
3121 chosen = &monitor_descriptor;
3126 fallback = &monitor_descriptor;
3140 chosen->viewport.offset_x = monitor->viewport.offset_x;
3141 chosen->viewport.offset_y = monitor->viewport.offset_y;
3145 chosen->viewport.width != monitor->viewport.width ||
3146 chosen->viewport.height != monitor->viewport.height
3149 <<
"Mismatch on expected Resolution compared to actual resolution: "sv
3150 << chosen->viewport.width <<
'x' << chosen->viewport.height
3152 << monitor->viewport.width <<
'x' << monitor->viewport.height;
3155 BOOST_LOG(info) <<
"Monitor " << chosen->monitor_index <<
" is "sv << name <<
": "sv << monitor->description;
3158 BOOST_LOG(verbose) <<
"Reduced to name: "sv << name <<
": "sv << index;
3161 BOOST_LOG(info) <<
"--------- End of KMS monitor list ---------"sv;
3165 std::vector<std::string> kms_display_names(mem_type_e hwdevice_type) {
3168 if (!fs::exists(
"/dev/dri")) {
3169 BOOST_LOG(warning) <<
"Couldn't find /dev/dri, kmsgrab won't be enabled"sv;
3173 if (!gbm::create_device) {
3174 BOOST_LOG(warning) <<
"libgbm not initialized"sv;
3178 kms::conn_type_count_t conn_type_count;
3180 std::vector<kms::card_descriptor_t> cds;
3181 std::vector<std::string> display_names;
3182 std::map<std::string, int> index_by_connector;
3184 fs::path card_dir {
"/dev/dri"sv};
3185 for (
auto &entry : fs::directory_iterator {card_dir}) {
3186 auto file = entry.path().filename();
3188 auto filestring = file.generic_string();
3189 if (std::string_view {filestring}.substr(0, 4) !=
"card"sv) {
3194 if (card.init(entry.path().c_str())) {
3200 if (hwdevice_type == mem_type_e::cuda && !card.is_nvidia()) {
3201 BOOST_LOG(debug) << file <<
" is not a CUDA device"sv;
3202 if (config::video.encoder ==
"nvenc") {
3203 BOOST_LOG(warning) <<
"Using NVENC with your display connected to a different GPU may not work properly!"sv;
3209 const auto monitors = card.monitors(conn_type_count);
3210 auto crtc_to_monitor = kms::map_crtc_to_monitor(monitors);
3213 std::map<std::uint32_t, std::string> connector_by_crtc;
3214 for (
const auto &monitor : monitors) {
3215 if (
auto conn = card.connector(monitor.connector_id)) {
3216 connector_by_crtc.emplace(monitor.crtc_id, kms::kernel_connector_name(conn->connector_type, conn->connector_type_id));
3220 auto end = std::end(card);
3221 for (
auto plane = std::begin(card); plane != end; ++plane) {
3223 if (!plane->fb_id) {
3227 if (card.is_cursor(plane->plane_id)) {
3231 auto fb = card.fb(plane.get());
3233 BOOST_LOG(error) <<
"Couldn't get drm fb for plane ["sv << plane->fb_id <<
"]: "sv << strerror(errno);
3237 if (!fb->handles[0]) {
3238 BOOST_LOG(error) <<
"Couldn't get handle for DRM Framebuffer ["sv << plane->fb_id <<
"]: Probably not permitted"sv;
3239 BOOST_LOG((window_system !=
window_system_e::X11 || config::video.capture ==
"kms") ? fatal : error)
3240 <<
"You must run [sudo setcap cap_sys_admin+p $(readlink -f $(which hermes))] for KMS display capture to work!\n"sv
3241 <<
"The packages set this in their post-install script; a binary you built yourself needs it applied by hand.\n"sv
3242 <<
"See https://github.com/MrOz59/Hermes/blob/main/docs/troubleshooting.md"sv;
3247 auto crtc = card.crtc(plane->crtc_id);
3249 BOOST_LOG(error) <<
"Couldn't get CRTC info: "sv << strerror(errno);
3253 auto it = crtc_to_monitor.find(plane->crtc_id);
3254 if (it != std::end(crtc_to_monitor)) {
3261 it->second.monitor_index = count;
3264 if (
const auto name = connector_by_crtc.find(plane->crtc_id); name != connector_by_crtc.end() && !name->second.empty()) {
3265 index_by_connector.emplace(name->second, count);
3268 kms::env_width = std::max(kms::env_width, (
int) (crtc->x + crtc->width));
3269 kms::env_height = std::max(kms::env_height, (
int) (crtc->y + crtc->height));
3271 kms::print(plane.get(), fb.get(), crtc.get());
3273 display_names.emplace_back(std::to_string(count++));
3276 cds.emplace_back(kms::card_descriptor_t {
3278 std::move(crtc_to_monitor),
3288 kms::env_height = 0;
3290 for (
auto &card_descriptor : cds) {
3291 for (
auto &[_, monitor_descriptor] : card_descriptor.crtc_to_monitor) {
3292 BOOST_LOG(debug) <<
"Monitor description"sv;
3293 BOOST_LOG(debug) <<
"Resolution: "sv << monitor_descriptor.viewport.width <<
'x' << monitor_descriptor.viewport.height;
3294 BOOST_LOG(debug) <<
"Offset: "sv << monitor_descriptor.viewport.offset_x <<
'x' << monitor_descriptor.viewport.offset_y;
3296 kms::env_width = std::max(kms::env_width, (
int) (monitor_descriptor.viewport.offset_x + monitor_descriptor.viewport.width));
3297 kms::env_height = std::max(kms::env_height, (
int) (monitor_descriptor.viewport.offset_y + monitor_descriptor.viewport.height));
3301 BOOST_LOG(debug) <<
"Desktop resolution: "sv << kms::env_width <<
'x' << kms::env_height;
3303 kms::card_descriptors = std::move(cds);
3311 const auto it = index_by_connector.find(primary);
3312 if (it == index_by_connector.end()) {
3313 BOOST_LOG(info) <<
"The desktop's primary output "sv <<
primary
3314 <<
" is dark (switched off or asleep), so it can't be the default display yet."sv;
3316 const auto pos = std::find(display_names.begin(), display_names.end(), std::to_string(it->second));
3317 if (pos != display_names.end() && pos != display_names.begin()) {
3318 std::rotate(display_names.begin(), pos, pos + 1);
3319 BOOST_LOG(info) <<
"Monitor "sv << it->second <<
" ("sv <<
primary
3320 <<
") is the desktop's primary output; it is streamed when no display is configured."sv;
3330 if (std::find(display_names.begin(), display_names.end(), name) == display_names.end()) {
3331 display_names.emplace_back(std::move(name));
3335 return display_names;
std::function< bool(std::shared_ptr< img_t > &&img, bool frame_captured)> push_captured_image_cb_t
Callback for when a new image is ready. When display has a new image ready or a timeout occurs,...
std::function< bool(std::shared_ptr< img_t > &img_out)> pull_free_image_cb_t
Get free image from pool. Calls must be synchronized. Blocks until there is free image in the pool or...
virtual std::unique_ptr< avcodec_encode_device_t > make_pyrowave_encode_device()
Make a device that converts captured frames into PyroWave's GPU planes.
capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *) override
Capture a frame.
std::uint64_t layout_generation
The desktop layout this capture resolved its geometry against.
platf::dmabuf::mapping_cache_t mappings
CPU mappings of the scanout buffers the compositor rotates through.
capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override
Capture a frame.
std::uint64_t layout_generation
The desktop layout this capture resolved its geometry against.
capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor)
Capture a frame.
capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override
Capture a frame.
bool plane_lost
The plane had no framebuffer at the last refresh().
bool requires_hdr_for_hdr_output() const override
Whether this capture backend requires an HDR encoder when the display is in HDR mode.
capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor)
Capture a frame.
Declarations for common platform specific utilities.
std::shared_ptr< display_t > display(mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config)
Get the display_t instance for the given hwdevice_type. If display_name is empty, use the first monit...
@ reinit
Need to reinitialize.
@ interrupted
Capture was interrupted.
Declarations for the configuration of Sunshine.
std::unique_ptr< platf::avcodec_encode_device_t > make_avcodec_gl_encode_device(int width, int height, int offset_x, int offset_y)
Create a GL->CUDA encoding device for consuming captured dmabufs.
Definitions for CUDA implementation.
layout_e
How the 32-bit little-endian words of a CPU-copied frame hold their channels.
@ bgra8
DRM XRGB8888 / ARGB8888: bytes B, G, R, X.
@ bgr10
DRM XBGR2101010 / ABGR2101010: B in bits 29:20, G in 19:10, R in 9:0.
@ rgb10
DRM XRGB2101010 / ARGB2101010: R in bits 29:20, G in 19:10, B in 9:0.
std::optional< client_t > find(const std::filesystem::path &proc, uid_t uid, const std::filesystem::path &pid_file, std::string_view own_cgroup)
The user's running Steam client.
std::unique_ptr< platf::deinit_t > init(const std::filesystem::path &persistence_filepath, const config::video_t &video_config)
Initialize the implementation and perform the initial state recovery (if needed).
Persistent CPU mappings of DMA-BUFs, and a copy suited to reading them.
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)
int open_encode_render_node(bool nvenc)
Open the render node of the best GPU to encode a stream on.
Declarations for graphics related functions.
std::string hermes_fourcc_name(uint32_t fourcc)
"XR30" for a printable fourcc, its hex value otherwise.
bool hermes_layout_changed(std::uint64_t &recorded)
void blend_hermes_cursor(img_t &img, const cursor_t &cursor, uint32_t fourcc=DRM_FORMAT_ARGB8888)
void correlate_to_wayland(std::vector< kms::card_descriptor_t > &cds)
constexpr bool hermes_cpu_copy_format(uint32_t fourcc)
void resolve_hermes_input_geometry(const std::string &display_name, int width, int height, int &offset_x, int &offset_y, int &env_width, int &env_height)
constexpr bool hermes_ten_bit_format(uint32_t fourcc)
A frame word packing 10-bit R, G and B, in either channel order.
Declarations for logging related functions.
int open_drm_node(const char *path, int flags)
Open a DRM device node without becoming its DRM master.
Miscellaneous declarations for Linux.
GPU conversion for the experimental PyroWave codec.
Declarations for a round-robin iterator.
uint32_t src_x
DRM 16.16 fixed point.
uint32_t src_h
DRM 16.16 fixed point.
void close()
Close all owned fds (dma_buf_fd[] and sync_file_fd).
uint32_t src_w
DRM 16.16 fixed point.
uint32_t src_y
DRM 16.16 fixed point.
long long acquire_ns
Time spent in the ACQUIRE_FRAME ioctl only.
void close()
Close all owned fds (dma_buf_fd[] and sync_file_fd).
Declarations for utility functions.
Declarations for VA-API hardware accelerated capture.
bool hermesKmsAcquireCursor(int render_fd, bool request_buffer, HermesKmsCursor &out)
int getEvdiCardIndex(const std::string &displayName)
Get the DRM card index for an EVDI display.
bool isEvdiDisplay(const std::string &displayName)
Check if a display name is an EVDI virtual display.
std::vector< std::string > listHermesKmsDisplayNames()
uint64_t displayLayoutGeneration()
A counter bumped whenever the desktop layout changes underneath a running session.
int hermesKmsOpenCapture(const std::string &display_name)
bool hermesKmsWaitUpdate(int render_fd, uint64_t after_frame_sequence, uint64_t after_cursor_sequence, uint32_t timeout_ms, HermesKmsUpdate &out)
int getHermesKmsCardIndex(const std::string &displayName)
bool getHermesKmsDisplayGeometry(const std::string &display_name, int &offset_x, int &offset_y, int &environment_width, int &environment_height)
bool hermesKmsCaptureColor(int render_fd, hermes_kms::color_t &color, uint32_t &fourcc)
bool hermesKmsCaptureSize(int render_fd, int &width, int &height, uint32_t timeout_ms)
std::shared_ptr< EvdiBuffer > getEvdiBuffer(const std::string &display_name)
bool isHermesKmsDisplay(const std::string &displayName)
std::string primaryPhysicalOutputName()
The desktop's primary physical output ("DP-2"), or empty.
std::string getHermesKmsSeatName(const std::string &displayName)
Virtual display declarations for Linux.
@ primary
Side-by-side like extend, with the virtual output as the primary one.
Declarations for Wayland capture.