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

Declarations for graphics related functions. More...

#include <optional>
#include <string_view>
#include <glad/egl.h>
#include <glad/gl.h>
#include "misc.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/utility.h"
#include "src/video_colorspace.h"
Include dependency graph for graphics.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  gl::buffer_t
 EGL image buffer with plane descriptors and imported GL textures. More...
 
class  egl::cursor_t
 Cursor image and hotspot metadata captured from the window system. More...
 
class  gl::frame_buf_t
 OpenGL framebuffer handle wrapper. More...
 
class  egl::img_descriptor_t
 Captured image descriptor shared by EGL conversion paths. More...
 
struct  egl::nv12_img_t
 NV12 capture image backed by EGL and OpenGL resources. More...
 
class  gl::program_t
 OpenGL shader program with attached shader stages. More...
 
struct  egl::rgb_img_t
 RGB capture image backed by EGL and OpenGL resources. More...
 
class  gl::shader_t
 OpenGL shader object that compiles GLSL source. More...
 
struct  egl::surface_descriptor_t
 EGL surface descriptor used to import a captured DMA-BUF. More...
 
class  egl::sws_t
 EGL/OpenGL scaler and colorspace conversion pipeline. More...
 
class  gl::tex_t
 OpenGL texture handle wrapper. More...
 
struct  egl::yuv444_img_t
 YUV 4:4:4 capture image backed by EGL and OpenGL resources. More...
 

Macros

#define gl_drain_errors   gl_drain_errors_helper(__FILE__ ":" SUNSHINE_STRINGIFY(__LINE__))
 Macro for gl drain errors.
 
#define gl_drain_errors_helper(x)
 Macro for gl drain errors helper.
 
#define SUNSHINE_STRINGIFY(x)
 Macro for SUNSHINE STRINGIFY.
 
#define SUNSHINE_STRINGIFY_HELPER(x)
 Macro for SUNSHINE STRINGIFY HELPER.
 

Typedefs

typedef device *(* gbm::create_device_fn) (int fd)
 Function pointer used to create a GBM device from a file descriptor.
 
typedef void(* gbm::device_destroy_fn) (device *gbm)
 Function pointer used to destroy a GBM device.
 
using egl::display_t = util::dyn_safe_ptr_v2<void, EGLBoolean, &eglTerminate>
 Owning pointer for an EGL display connection.
 
using frame_t = util::safe_ptr<AVFrame, free_frame>
 Owning pointer for an EGL frame object.
 
using gbm::gbm_t = util::dyn_safe_ptr<device, &device_destroy>
 Owning GBM device pointer released with the GBM destroy callback.
 
using gl::PFNGLEGLIMAGETARGETTEXTURE2DOESPROC = void (*)(GLenum target, void *image)
 Function pointer type for glEGLImageTargetTexture2DOES.
 

Functions

int close (int __fd)
 Release the native resource held by the RAII wrapper.
 
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.
 
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 (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 surface.
 
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.
 

Detailed Description

Declarations for graphics related functions.

Macro Definition Documentation

◆ gl_drain_errors_helper

#define gl_drain_errors_helper ( x)
Value:
gl::drain_errors(x)

Macro for gl drain errors helper.

◆ SUNSHINE_STRINGIFY

#define SUNSHINE_STRINGIFY ( x)
Value:
#define SUNSHINE_STRINGIFY_HELPER(x)
Macro for SUNSHINE STRINGIFY HELPER.
Definition graphics.h:26

Macro for SUNSHINE STRINGIFY.

◆ SUNSHINE_STRINGIFY_HELPER

#define SUNSHINE_STRINGIFY_HELPER ( x)
Value:
#x

Macro for SUNSHINE STRINGIFY HELPER.

Function Documentation

◆ close()

int close ( int __fd)

Release the native resource held by the RAII wrapper.

Parameters
__fdFile descriptor owned by the RAII wrapper.
Returns
0 on success, or -1 with errno set by the system close call.

◆ 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.

◆ 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() [1/2]

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() [2/2]

std::optional< yuv444_t > egl::import_target ( 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 surface.

Parameters
egl_displayEGL display.
fdsTarget plane file descriptors.
yY plane descriptor.
uU plane descriptor.
vV plane descriptor.
Returns
Imported YUV444 image, or std::nullopt on failure.

◆ 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.