Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
wlgrab.cpp File Reference

Definitions for wlgrab capture. More...

#include <thread>
#include "cuda.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/video.h"
#include "vaapi.h"
#include "vulkan_encode.h"
#include "wayland.h"
Include dependency graph for wlgrab.cpp:

Classes

struct  wl::img_t
 Captured frame buffer shared between capture and encode stages. More...
class  wl::wlr_ram_t
 Wayland screencopy backend that copies frames into system memory. More...
class  wl::wlr_t
 Wayland screencopy capture backend shared by RAM and VRAM paths. More...
class  wl::wlr_vram_t
 Wayland screencopy backend that exports frames as GPU resources. More...

Functions

std::chrono::steady_clock::time_point wl::damage_frame_time (std::optional< std::chrono::steady_clock::time_point > compositor_time, std::chrono::steady_clock::time_point now, std::chrono::nanoseconds delay)
 Pick the time a captured frame is charged to the damage-driven budget at.
std::chrono::steady_clock::time_point wl::next_damage_request (std::chrono::steady_clock::time_point next_request, std::chrono::steady_clock::time_point frame_time, std::chrono::nanoseconds delay)
 Move the earliest time of the next damage-driven request on by one captured frame.
screencopy_request_e wl::next_screencopy_request (bool pending, bool requested_cursor, bool cursor, bool event_driven, bool have_frame)
 Decide which screencopy request a capture waits on next.
bool wl::should_wait_for_damage_request (std::chrono::steady_clock::time_point next_request, std::chrono::steady_clock::time_point now, std::chrono::nanoseconds delay)
 Determine whether a damage-driven request should wait for its budget.
bool wl::use_damage_capture (std::uint32_t screencopy_version, double stream_fps, std::int32_t refresh_mhz)
 Determine whether wlroots capture should ask for frames with copy_with_damage.
bool wl::use_vram_capture (platf::mem_type_e hwdevice_type)
 Determine whether wlroots capture should keep frames in VRAM for the requested memory type.
std::shared_ptr< display_t > platf::wl_display (mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config)
 Create a Wayland display capture backend.
std::vector< std::string > platf::wl_display_names ()
 Enumerate displays available through the Wayland capture backend.

Detailed Description

Definitions for wlgrab capture.

Function Documentation

◆ damage_frame_time()

std::chrono::steady_clock::time_point wl::damage_frame_time ( std::optional< std::chrono::steady_clock::time_point > compositor_time,
std::chrono::steady_clock::time_point now,
std::chrono::nanoseconds delay )

Pick the time a captured frame is charged to the damage-driven budget at.

The compositor's own timestamp is the better one, it does not carry the delay of getting the frame to Sunshine. The screencopy protocol does not say which clock it is on, though. A time ahead of now, or more than one interval behind it, is not from the clock the budget runs on and would leave the budget permanently due or permanently out of reach, so the frame is charged at now instead.

Parameters
compositor_timeTime the compositor gave for the frame, if any.
nowTime the frame was received, on Sunshine's clock.
delayFrame interval of the stream.
Returns
compositor_time when it lies within one interval before now, otherwise now.

◆ next_damage_request()

std::chrono::steady_clock::time_point wl::next_damage_request ( std::chrono::steady_clock::time_point next_request,
std::chrono::steady_clock::time_point frame_time,
std::chrono::nanoseconds delay )

Move the earliest time of the next damage-driven request on by one captured frame.

Every captured frame costs one frame interval of budget, which holds an output that changes faster than the stream to the stream's rate. A frame that arrives after the budget ran out resets it to a quarter interval before the frame's own time, so a source running at the stream's rate is asked for with room to spare.

Parameters
next_requestEarliest request time before this frame.
frame_timeTime the compositor presented the captured frame.
delayFrame interval of the stream.
Returns
Earliest time the next frame may be asked for.

◆ next_screencopy_request()

screencopy_request_e wl::next_screencopy_request ( bool pending,
bool requested_cursor,
bool cursor,
bool event_driven,
bool have_frame )

Decide which screencopy request a capture waits on next.

A request that outlived a timeout is kept: with copy_with_damage a still picture never answers, and asking again on every timeout would pile requests up in the compositor. It is replaced when the cursor setting changed since it was made. The setting is fixed when a request is created and changing it does not damage the output, so on a still picture the old request would go on answering with the cursor as it was. The replacement is a plain copy, which brings the picture up to date at once.

The same holds when no request is out. The last frame was captured with the setting of the last request, so if the setting changed since then, the picture the stream is showing is out of date and the next request is a plain copy as well. This covers a change between two requests, and a request that could not be withdrawn and was answered with the old setting.

Parameters
pendingWhether a request is still waiting for the compositor.
requested_cursorCursor setting the last request was made with, pending or answered.
cursorCursor setting wanted now.
event_drivenWhether frames are requested with copy_with_damage.
have_frameWhether this capture has delivered a frame yet.
Returns
The request to wait on.

◆ should_wait_for_damage_request()

bool wl::should_wait_for_damage_request ( std::chrono::steady_clock::time_point next_request,
std::chrono::steady_clock::time_point now,
std::chrono::nanoseconds delay )

Determine whether a damage-driven request should wait for its budget.

Parameters
next_requestEarliest time the next frame may be asked for.
nowCurrent time.
delayFrame interval of the stream.
Returns
true when the caller should sleep until next_request. A time further off than one interval is never waited for. With frame times from damage_frame_time the budget does not get that far ahead; the bound keeps a wrong budget from stalling capture.

◆ use_damage_capture()

bool wl::use_damage_capture ( std::uint32_t screencopy_version,
double stream_fps,
std::int32_t refresh_mhz )

Determine whether wlroots capture should ask for frames with copy_with_damage.

With copy_with_damage the compositor answers when the output has a new picture, so capture follows the compositor's frames instead of sampling them on a clock of Sunshine's own. That needs version 2 of the screencopy protocol and an output that refreshes at least half again as fast as the stream. On an output running at the stream's own rate, the commits a paced copy forces are part of what keeps the output's clients on time.

Parameters
screencopy_versionVersion of the screencopy global the compositor offered.
stream_fpsFrame rate requested for the stream.
refresh_mhzRefresh rate of the captured output in mHz, 0 when the compositor did not say.
Returns
true when frames should be asked for with copy_with_damage.

◆ use_vram_capture()

bool wl::use_vram_capture ( platf::mem_type_e hwdevice_type)

Determine whether wlroots capture should keep frames in VRAM for the requested memory type.

Parameters
hwdevice_typeHardware device type requested for capture or encode.
Returns
true when the requested memory type should use the wlroots VRAM path.

◆ wl_display()

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

Create a Wayland display capture backend.

Create a Wayland capture backend for the requested memory type.

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

◆ wl_display_names()

std::vector< std::string > platf::wl_display_names ( )

Enumerate displays available through the Wayland capture backend.

Enumerate capture display names reported by the Wayland compositor.

Returns
Wayland display names, or an empty list when discovery fails.