Sunshine master
Self-hosted game stream host for Moonlight.
kmsgrab.cpp File Reference

Definitions for KMS screen capture. More...

#include <errno.h>
#include <fcntl.h>
#include <filesystem>
#include <thread>
#include <unistd.h>
#include <drm_fourcc.h>
#include <linux/dma-buf.h>
#include <sys/capability.h>
#include <sys/mman.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include "cuda.h"
#include "graphics.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/round_robin.h"
#include "src/utility.h"
#include "src/video.h"
#include "vaapi.h"
#include "vulkan_encode.h"
#include "wayland.h"
Include dependency graph for kmsgrab.cpp:

Classes

class  platf::kms::cap_sys_admin
 Temporarily owns CAP_SYS_ADMIN while opening DRM capture resources. More...
 
struct  platf::kms::card_descriptor_t
 DRM card, device path, and render-node metadata. More...
 
class  platf::kms::card_t
 DRM card, render node, and plane metadata used for KMS capture. More...
 
struct  platf::kms::connector_t
 DRM connector and mode information for one display. More...
 
struct  platf::kms::cursor_t
 Cursor position and visibility for the current capture frame. More...
 
class  platf::kms::display_ram_t
 KMS capture backend that copies frames into system memory. More...
 
class  platf::kms::display_t
 Base KMS display capture backend shared by RAM and VRAM paths. More...
 
class  platf::kms::display_vram_t
 KMS capture backend that exports frames as GPU resources. More...
 
struct  platf::kms::kms_img_t
 Multi-source KMS image assembled for encoding. More...
 
struct  platf::kms::monitor_t
 KMS monitor capture state and DRM resources. More...
 
class  platf::kms::plane_it_t
 Iterator over DRM planes and their associated properties. More...
 
class  platf::kms::wrapper_fb
 RAII wrapper for DRM framebuffer metadata and GEM handles. More...
 

Typedefs

using platf::kms::conn_type_count_t = std::map<std::uint32_t, std::uint32_t>
 Counter map keyed by DRM connector type.
 
using platf::kms::crtc_t = util::safe_ptr<drmModeCrtc, drmModeFreeCrtc>
 DRM CRTC pointer released with drmModeFreeCrtc.
 
using platf::kms::encoder_t = util::safe_ptr<drmModeEncoder, drmModeFreeEncoder>
 DRM encoder pointer released with drmModeFreeEncoder.
 
using platf::kms::fb_t = std::unique_ptr<wrapper_fb>
 DRM framebuffer pointer released with drmModeFreeFB2.
 
using platf::kms::obj_prop_t = util::safe_ptr<drmModeObjectProperties, drmModeFreeObjectProperties>
 DRM object-property list released with drmModeFreeObjectProperties.
 
using platf::kms::plane_res_t = util::safe_ptr<drmModePlaneRes, drmModeFreePlaneResources>
 DRM plane resource list released with drmModeFreePlaneResources.
 
using platf::kms::plane_t = util::safe_ptr<drmModePlane, drmModeFreePlane>
 DRM plane pointer released with drmModeFreePlane.
 
using platf::kms::prop_blob_t = util::safe_ptr<drmModePropertyBlobRes, drmModeFreePropertyBlob>
 DRM property-blob pointer released with drmModeFreePropertyBlob.
 
using platf::kms::prop_t = util::safe_ptr<drmModePropertyRes, drmModeFreeProperty>
 DRM property pointer released with drmModeFreeProperty.
 
using platf::kms::res_t = util::safe_ptr<drmModeRes, drmModeFreeResources>
 DRM resource list released with drmModeFreeResources.
 
using platf::kms::version_t = util::safe_ptr<drmVersion, drmFreeVersion>
 DRM version pointer released with drmFreeVersion.
 

Functions

void platf::correlate_to_wayland (std::vector< kms::card_descriptor_t > &cds)
 
std::shared_ptr< display_tplatf::kms_display (mem_type_e hwdevice_type, const std::string &display_name, const ::video::config_t &config)
 Create a KMS display capture backend.
 
std::vector< std::string > platf::kms_display_names (mem_type_e hwdevice_type)
 Enumerate display names accepted by the KMS backend.
 
std::map< std::uint32_t, monitor_tplatf::kms::map_crtc_to_monitor (const std::vector< connector_t > &connectors)
 Build a lookup from DRM CRTC IDs to monitor descriptors.
 
std::string_view platf::kms::plane_type (std::uint64_t val)
 Convert a DRM plane type value to a diagnostic string.
 
void platf::kms::print (plane_t::pointer plane, fb_t::pointer fb, crtc_t::pointer crtc)
 Write a debug log representation of the input packet.
 

Detailed Description

Definitions for KMS screen capture.

Function Documentation

◆ correlate_to_wayland()

void platf::correlate_to_wayland ( std::vector< kms::card_descriptor_t > & cds)

On Wayland, it's not possible to determine the position of the monitor on the desktop with KMS. Wayland does allow applications to query attached monitors on the desktop, however, the naming scheme is not standardized across implementations.

As a result, correlating the KMS output to the wayland outputs is guess work at best. But, it's necessary for absolute mouse coordinates to work.

This is an ugly hack :(

Parameters
cdsCapture display state used by the KMS backend.

◆ kms_display()

std::shared_ptr< display_t > platf::kms_display ( mem_type_e hwdevice_type,
const std::string & display_name,
const ::video::config_t & config )

Create a KMS display capture backend.

Parameters
hwdevice_typeHardware device type requested for capture or encode.
display_nameDisplay name.
configConfiguration values to apply.
Returns
KMS display backend, or nullptr when initialization fails.

◆ kms_display_names()

std::vector< std::string > platf::kms_display_names ( mem_type_e hwdevice_type)

Enumerate display names accepted by the KMS backend.

Parameters
hwdevice_typeHardware device type requested for capture or encode.
Returns
KMS display names, or an empty list when KMS capture is unavailable.

◆ map_crtc_to_monitor()

std::map< std::uint32_t, monitor_t > platf::kms::map_crtc_to_monitor ( const std::vector< connector_t > & connectors)

Build a lookup from DRM CRTC IDs to monitor descriptors.

Parameters
connectorsDRM connector list being mapped to monitors.
Returns
Map keyed by CRTC ID.

◆ plane_type()

std::string_view platf::kms::plane_type ( std::uint64_t val)

Convert a DRM plane type value to a diagnostic string.

Parameters
valValue assigned to the synchronized object.
Returns
Static text for the DRM plane type.

◆ print()

void platf::kms::print ( plane_t::pointer plane,
fb_t::pointer fb,
crtc_t::pointer crtc )

Write a debug log representation of the input packet.

Parameters
planeDRM or EGL plane index being described.
fbFramebuffer object to bind or update.
crtcDRM CRTC identifier to map.