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

Definitions for graphics related functions. More...

#include <fcntl.h>
#include "graphics.h"
#include "src/file_handler.h"
#include "src/logging.h"
#include "src/video.h"
#include <sys/capability.h>
#include <libavutil/pixdesc.h>
Include dependency graph for graphics.cpp:

Classes

struct  egl::plane_attr_t
 EGL attribute pair describing one DMA-BUF plane. More...
 

Macros

#define DRM_FORMAT_MOD_INVALID   fourcc_mod_code(0, ((1ULL << 56) - 1))
 Macro for DRM FORMAT MOD INVALID.
 
#define fourcc_code(a, b, c, d)
 Macro for fourcc code.
 
#define fourcc_mod_code(vendor, val)
 Macro for fourcc mod code.
 
#define SUNSHINE_SHADERS_DIR   SUNSHINE_ASSETS_DIR "/shaders/opengl"
 Macro for SUNSHINE SHADERS DIR.
 

Functions

int egl::configure_sws_pipeline (sws_t &sws, const video::color_t *color_p, gl::tex_t &&tex, bool is_yuv444)
 Configure the EGL/OpenGL scaling and colorspace conversion pipeline.
 
rgb_t egl::create_blank (platf::img_t &img)
 Create a black RGB texture of the specified image size.
 
std::optional< nv12_t > egl::create_nv12_target (int width, int height, AVPixelFormat format)
 Create biplanar YUV textures to render into.
 
std::optional< yuv444_t > egl::create_yuv444_target (int width, int height, AVPixelFormat format)
 Create YUV444 target.
 
void gl::drain_errors (const std::string_view &prefix)
 Drain and log pending OpenGL errors.
 
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC gl::egl_image_target_texture_2d ()
 Bind an EGL image to the current OpenGL texture target.
 
bool egl::fail ()
 Log EGL failure details and return an error code.
 
void free_frame (AVFrame *frame)
 Release an FFmpeg frame allocated by the capture or conversion backend.
 
plane_attr_t egl::get_plane (std::uint32_t plane_indice)
 Build EGL attributes for one DMA-BUF plane.
 
std::optional< rgb_t > egl::import_source (display_t::pointer egl_display, const surface_descriptor_t &xrgb)
 Import the source frame texture for EGL/OpenGL conversion.
 
std::optional< nv12_t > egl::import_target (display_t::pointer egl_display, std::array< file_t, nv12_img_t::num_fds > &&fds, const surface_descriptor_t &y, const surface_descriptor_t &uv)
 Import the output frame target for EGL/OpenGL conversion.
 
std::optional< yuv444_t > egl::import_target_yuv444 (display_t::pointer egl_display, std::array< file_t, yuv444_img_t::num_fds > &&fds, const surface_descriptor_t &y, const surface_descriptor_t &u, const surface_descriptor_t &v)
 Import a YUV444 target image from DMA-BUF descriptors.
 
int gbm::init ()
 Load GBM symbols required for EGL device creation.
 
std::optional< ctx_t > egl::make_ctx (display_t::pointer display)
 Create an EGL/OpenGL context for capture or conversion.
 
display_t egl::make_display (std::variant< gbm::gbm_t::pointer, wl_display *, _XDisplay * > native_display)
 Open and initialize the display connection used for capture.
 
void egl::nv12_bind_framebuffers (nv12_t &nv12)
 Bind NV12 target framebuffers to their Y and UV plane textures.
 
std::vector< EGLAttrib > egl::surface_descriptor_to_egl_attribs (const surface_descriptor_t &surface)
 Get EGL attributes for eglCreateImage() to import the provided surface.
 
void egl::yuv44_bind_framebuffers (yuv444_t &yuv444)
 Bind YUV444 target framebuffers to their Y, U, and V plane textures.
 

Variables

create_device_fn gbm::create_device
 Create device.
 
GladGLContext gl::ctx
 Loaded OpenGL function table for the active context.
 
device_destroy_fn gbm::device_destroy
 Device destroy.
 
const float egl::uv_black [] = {0.5f, 0.5f, 0.5f, 0.5f}
 Uv black.
 
const float egl::y_black [] = {0.0f, 0.0f, 0.0f, 0.0f}
 Y black.
 

Detailed Description

Definitions for graphics related functions.

Macro Definition Documentation

◆ fourcc_code

#define fourcc_code ( a,
b,
c,
d )
Value:
((std::uint32_t) (a) | ((std::uint32_t) (b) << 8) | ((std::uint32_t) (c) << 16) | ((std::uint32_t) (d) << 24))

Macro for fourcc code.

◆ fourcc_mod_code

#define fourcc_mod_code ( vendor,
val )
Value:
((((uint64_t) vendor) << 56) | ((val) & 0x00ffffffffffffffULL))

Macro for fourcc mod code.

Function Documentation

◆ configure_sws_pipeline()

int egl::configure_sws_pipeline ( sws_t & sws,
const video::color_t * color_p,
gl::tex_t && tex,
bool is_yuv444 )

Configure the EGL/OpenGL scaling and colorspace conversion pipeline.

Parameters
swsSoftware-scaling pipeline to configure.
color_pColor p.
texTexture resource used by the converter.
is_yuv444Is YUV444.
Returns
0 when shaders, framebuffers, and color uniforms are ready; nonzero on failure.

◆ create_blank()

rgb_t egl::create_blank ( platf::img_t & img)

Create a black RGB texture of the specified image size.

Create a blank RGB texture for an image.

Parameters
imgThe image to use for texture sizing.
Returns
The new RGB texture.
Blank RGB image.

◆ create_nv12_target()

std::optional< nv12_t > egl::create_nv12_target ( int width,
int height,
AVPixelFormat format )

Create biplanar YUV textures to render into.

Creates biplanar YUV textures to render into.

Parameters
widthWidth of the target frame.
heightHeight of the target frame.
formatFormat of the target frame.
Returns
The new RGB texture.

◆ create_yuv444_target()

std::optional< yuv444_t > egl::create_yuv444_target ( int width,
int height,
AVPixelFormat format )

Create YUV444 target.

Parameters
widthFrame or display width in pixels.
heightFrame or display height in pixels.
formatPixel, audio, or protocol format being converted.
Returns
Created YUV444 target object or status.

◆ drain_errors()

void gl::drain_errors ( const std::string_view & prefix)

Drain and log pending OpenGL errors.

Parameters
prefixText prefix used when formatting the message.

◆ egl_image_target_texture_2d()

PFNGLEGLIMAGETARGETTEXTURE2DOESPROC gl::egl_image_target_texture_2d ( )

Bind an EGL image to the current OpenGL texture target.

Resolve the GLES extension used to bind EGL images as textures.

Returns
Extension function pointer, or nullptr when the driver does not expose it.

◆ fail()

bool egl::fail ( )

Log EGL failure details and return an error code.

Returns
False after logging the EGL failure.

◆ free_frame()

void free_frame ( AVFrame * frame)

Release an FFmpeg frame allocated by the capture or conversion backend.

Parameters
frameVideo or graphics frame being processed.

◆ get_plane()

plane_attr_t egl::get_plane ( std::uint32_t plane_indice)
inline

Build EGL attributes for one DMA-BUF plane.

Parameters
plane_indiceZero-based plane index in the DMA-BUF descriptor.
Returns
EGL attribute keys for that plane's file descriptor, offset, pitch, and modifier.

◆ import_source()

std::optional< rgb_t > egl::import_source ( display_t::pointer egl_display,
const surface_descriptor_t & xrgb )

Import the source frame texture for EGL/OpenGL conversion.

Import an RGB source surface.

Parameters
egl_displayEGL display used to create the image.
xrgbXRGB surface descriptor to import.
Returns
Imported RGB image, or empty when import fails.
Imported RGB image, or std::nullopt on failure.

◆ import_target()

std::optional< nv12_t > egl::import_target ( display_t::pointer egl_display,
std::array< file_t, nv12_img_t::num_fds > && fds,
const surface_descriptor_t & y,
const surface_descriptor_t & uv )

Import the output frame target for EGL/OpenGL conversion.

Import an NV12 target surface.

Parameters
egl_displayEGL display used to create the images.
fdsFile descriptors backing the NV12 image.
yLuma plane descriptor.
uvChroma plane descriptor.
Returns
Imported NV12 image, or empty when import fails.
Imported NV12 image, or std::nullopt on failure.

◆ import_target_yuv444()

std::optional< yuv444_t > egl::import_target_yuv444 ( display_t::pointer egl_display,
std::array< file_t, yuv444_img_t::num_fds > && fds,
const surface_descriptor_t & y,
const surface_descriptor_t & u,
const surface_descriptor_t & v )

Import a YUV444 target image from DMA-BUF descriptors.

Parameters
egl_displayEGL display used to create the images.
fdsFile descriptors backing the YUV444 image.
yLuma plane descriptor.
uU chroma plane descriptor.
vV chroma plane descriptor.
Returns
Imported YUV444 image, or empty when import fails.

◆ init()

int gbm::init ( )

Load GBM symbols required for EGL device creation.

Returns
0 on success; nonzero or negative platform status on failure.

◆ make_ctx()

std::optional< ctx_t > egl::make_ctx ( display_t::pointer display)

Create an EGL/OpenGL context for capture or conversion.

Parameters
displayDisplay object or identifier associated with the operation.
Returns
EGL context wrapper, or std::nullopt when context creation fails.

◆ make_display()

display_t egl::make_display ( std::variant< gbm::gbm_t::pointer, wl_display *, _XDisplay * > native_display)

Open and initialize the display connection used for capture.

Parameters
native_displayNative display.
Returns
Constructed display object.

◆ nv12_bind_framebuffers()

void egl::nv12_bind_framebuffers ( nv12_t & nv12)

Bind NV12 target framebuffers to their Y and UV plane textures.

Parameters
nv12Imported NV12 target whose textures receive rendered output.

◆ surface_descriptor_to_egl_attribs()

std::vector< EGLAttrib > egl::surface_descriptor_to_egl_attribs ( const surface_descriptor_t & surface)

Get EGL attributes for eglCreateImage() to import the provided surface.

Parameters
surfaceThe surface descriptor.
Returns
Vector of EGL attributes.

◆ yuv44_bind_framebuffers()

void egl::yuv44_bind_framebuffers ( yuv444_t & yuv444)

Bind YUV444 target framebuffers to their Y, U, and V plane textures.

Parameters
yuv444Imported YUV444 target whose textures receive rendered output.