27#define SUNSHINE_STRINGIFY_HELPER(x) #x
32#define SUNSHINE_STRINGIFY(x) SUNSHINE_STRINGIFY_HELPER(x)
37#define gl_drain_errors_helper(x) gl::drain_errors(x)
42#define gl_drain_errors gl_drain_errors_helper(__FILE__ ":" SUNSHINE_STRINGIFY(__LINE__))
53extern "C" struct _XDisplay;
69 extern GladGLContext ctx;
152 inline void bind(std::nullptr_t, std::nullptr_t) {
154 for (
auto fb : (*
this)) {
155 ctx.BindFramebuffer(GL_FRAMEBUFFER, fb);
156 ctx.FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, 0, 0);
170 void bind(It it_begin, It it_end) {
171 using namespace std::literals;
172 if (std::distance(it_begin, it_end) >
size()) {
173 BOOST_LOG(
warning) <<
"To many elements to bind"sv;
178 std::for_each(it_begin, it_end, [&](
auto tex) {
179 ctx.BindFramebuffer(GL_FRAMEBUFFER, (*
this)[x]);
180 ctx.BindTexture(GL_TEXTURE_2D, tex);
182 ctx.FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, tex, 0);
198 void copy(
int id,
int texture,
int offset_x,
int offset_y,
int width,
int height);
205 KITTY_USING_MOVE_T(shader_internal_t, GLuint, std::numeric_limits<GLuint>::max(), {
206 if (el != std::numeric_limits<GLuint>::max()) {
207 ctx.DeleteShader(el);
236 shader_internal_t _shader;
243 KITTY_USING_MOVE_T(buffer_internal_t, GLuint, std::numeric_limits<GLuint>::max(), {
244 if (el != std::numeric_limits<GLuint>::max()) {
245 ctx.DeleteBuffers(1, &el);
272 const char *
block()
const;
280 void update(
const std::string_view &view, std::size_t offset = 0);
288 void update(std::string_view *members, std::size_t count, std::size_t offset = 0);
297 buffer_internal_t _buffer;
304 KITTY_USING_MOVE_T(program_internal_t, GLuint, std::numeric_limits<GLuint>::max(), {
305 if (el != std::numeric_limits<GLuint>::max()) {
306 ctx.DeleteProgram(el);
342 std::optional<buffer_t>
uniform(
const char *block, std::pair<const char *, std::string_view> *members, std::size_t count);
352 program_internal_t _program;
414 std::array<file_t, num_fds>
fds;
431 std::array<file_t, num_fds>
fds;
437 eglDestroyImage(el.display, el.xrgb8);
443 eglDestroyImage(el.display, el.r8);
447 eglDestroyImage(el.display, el.bg88);
453 eglDestroyImage(el.display, el.r8);
457 eglDestroyImage(el.display, el.g8);
461 eglDestroyImage(el.display, el.b8);
468 eglMakeCurrent(disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
469 eglDestroyContext(disp, ctx);
548 std::array<file_t, nv12_img_t::num_fds> &&fds,
565 std::array<file_t, yuv444_img_t::num_fds> &&fds,
578 std::optional<nv12_t>
create_nv12_target(
int width,
int height, AVPixelFormat format);
619 for (
auto fd_index = 0; fd_index < 4; ++fd_index) {
620 if (
sd.
fds[fd_index] >= 0) {
623 sd.
fds[fd_index] = -1;
637 std::optional<uint64_t>
pts;
638 std::optional<uint64_t>
seq;
726 int blank(
gl::frame_buf_t &fb,
int offsetX_,
int offsetY_,
int width,
int height,
bool is_yuv444);
util::safe_ptr< CodedBitstreamContext, &close > ctx_t
Owning coded bitstream context pointer that calls ff_cbs_close.
Definition cbs.cpp:35
Cursor image and hotspot metadata captured from the window system.
Definition graphics.h:593
std::vector< std::uint8_t > buffer
Cursor image pixels.
Definition graphics.h:602
unsigned long serial
X11 cursor serial used to detect cursor image changes.
Definition graphics.h:600
int y
Cursor hotspot or surface Y coordinate.
Definition graphics.h:596
int x
Cursor hotspot or surface X coordinate.
Definition graphics.h:595
int src_w
Cursor source image width in pixels.
Definition graphics.h:597
int src_h
Cursor source image height in pixels.
Definition graphics.h:598
Captured image descriptor shared by EGL conversion paths.
Definition graphics.h:609
std::optional< bool > pw_damage
Whether PipeWire damage tracking should be used.
Definition graphics.h:639
std::optional< uint32_t > pw_flags
PipeWire frame flags reported with the buffer.
Definition graphics.h:640
std::optional< uint64_t > seq
PipeWire frame sequence number.
Definition graphics.h:638
std::uint64_t sequence
Monotonic value used to detect when GL resources must be recreated.
Definition graphics.h:631
surface_descriptor_t sd
DMA-BUF surface descriptor for the captured image.
Definition graphics.h:628
void reset()
Reset the object to its initial empty state.
Definition graphics.h:618
bool y_invert
Whether the shader should invert the Y axis.
Definition graphics.h:634
std::optional< uint64_t > pts
PipeWire presentation timestamp.
Definition graphics.h:637
EGL/OpenGL scaler and colorspace conversion pipeline.
Definition graphics.h:646
gl::tex_t tex
Source and cursor textures used by the conversion pipeline.
Definition graphics.h:755
int offsetY
Offset y.
Definition graphics.h:771
int convert_yuv444(gl::frame_buf_t &fb)
Convert the loaded source image into YUV444 output planes.
Definition graphics.cpp:1510
int in_width
In width.
Definition graphics.h:768
int in_height
In height.
Definition graphics.h:769
gl::frame_buf_t cursor_framebuffer
Cursor framebuffer.
Definition graphics.h:758
static std::optional< sws_t > make(int in_width, int in_height, int out_width, int out_height, AVPixelFormat format, bool is_yuv444)
Allocate the underlying object and wrap it in the owning handle.
Definition graphics.cpp:1360
int out_width
Out width.
Definition graphics.h:766
int convert_nv12(gl::frame_buf_t &fb)
Convert the loaded source image into NV12 output planes.
Definition graphics.cpp:1488
static std::optional< sws_t > make_nv12(int in_width, int in_height, int out_width, int out_height, gl::tex_t &&tex)
Create a software-scaling pipeline that renders to NV12 planes.
Definition graphics.cpp:1133
void apply_colorspace(const video::sunshine_colorspace_t &colorspace, bool is_yuv444)
Apply the configured colorspace metadata to the active frame.
Definition graphics.cpp:1071
int blank(gl::frame_buf_t &fb, int offsetX_, int offsetY_, int width, int height, bool is_yuv444)
Clear the render target to a blank frame.
Definition graphics.cpp:1344
int offsetX
Offset x.
Definition graphics.h:770
gl::buffer_t color_matrix
Color matrix.
Definition graphics.h:764
gl::frame_buf_t copy_framebuffer
Copy framebuffer.
Definition graphics.h:759
void load_ram(platf::img_t &img)
Load ram data from the backing API or store.
Definition graphics.cpp:1397
static std::optional< sws_t > make_yuv444(int in_width, int in_height, int out_width, int out_height, gl::tex_t &&tex)
Create a software-scaling pipeline that renders to YUV444 planes.
Definition graphics.cpp:1239
void load_vram(img_descriptor_t &img, int offset_x, int offset_y, int texture, bool is_yuv444)
Load vram data from the backing API or store.
Definition graphics.cpp:1404
int loaded_texture
Loaded texture.
Definition graphics.h:774
int out_height
Out height.
Definition graphics.h:767
std::uint64_t serial
Serial.
Definition graphics.h:777
int draw_programs_to_buffers(GLenum attachments[], gl::frame_buf_t &fb, int count, bool is_yuv444)
Render the loaded source texture into output framebuffers.
Definition graphics.cpp:1466
std::array< gl::program_t, 4 > program
Program.
Definition graphics.h:763
EGL image buffer with plane descriptors and imported GL textures.
Definition graphics.h:242
void update(const std::string_view &view, std::size_t offset=0)
Update one uniform member in the block buffer.
Definition graphics.cpp:184
static buffer_t make(util::buffer_t< GLint > &&offsets, const char *block, const std::string_view &data)
Allocate the underlying object and wrap it in the owning handle.
Definition graphics.cpp:163
const char * block() const
Query a uniform block index from an OpenGL program.
Definition graphics.cpp:180
GLuint handle() const
Return the native handle owned by the wrapper.
Definition graphics.cpp:176
OpenGL framebuffer handle wrapper.
Definition graphics.h:124
void bind(std::nullptr_t, std::nullptr_t)
Bind each framebuffer and clear its color attachment.
Definition graphics.h:152
void bind(It it_begin, It it_end)
Bind textures to this object's framebuffers as color attachments.
Definition graphics.h:170
static frame_buf_t make(std::size_t count)
Allocate the underlying object and wrap it in the owning handle.
Definition graphics.cpp:110
frame_buf_t(frame_buf_t &&)=default
Move ownership of OpenGL framebuffer object names.
frame_buf_t & operator=(frame_buf_t &&)=default
Assign state from another instance while preserving ownership semantics.
void copy(int id, int texture, int offset_x, int offset_y, int width, int height)
Definition graphics.cpp:118
OpenGL shader program with attached shader stages.
Definition graphics.h:303
std::optional< buffer_t > uniform(const char *block, std::pair< const char *, std::string_view > *members, std::size_t count)
Query a uniform location from an OpenGL program.
Definition graphics.cpp:249
GLuint handle() const
Return the native handle owned by the wrapper.
Definition graphics.cpp:297
std::string err_str()
Read the program linker log.
Definition graphics.cpp:201
static util::Either< program_t, std::string > link(const shader_t &vert, const shader_t &frag)
Link an OpenGL program from compiled shaders.
Definition graphics.cpp:215
void bind(const buffer_t &buffer)
Bind this program and attach a uniform buffer block.
Definition graphics.cpp:242
OpenGL shader object that compiles GLSL source.
Definition graphics.h:204
static util::Either< shader_t, std::string > compile(const std::string_view &source, GLenum type)
Compile an OpenGL shader and report compiler errors.
Definition graphics.cpp:139
std::string err_str()
Read the shader compiler log.
Definition graphics.cpp:125
GLuint handle() const
Return the native handle owned by the wrapper.
Definition graphics.cpp:159
OpenGL texture handle wrapper.
Definition graphics.h:95
tex_t(tex_t &&)=default
Move ownership of OpenGL texture object names.
static tex_t make(std::size_t count)
Allocate the underlying object and wrap it in the owning handle.
Definition graphics.cpp:85
tex_t & operator=(tex_t &&)=default
Assign state from another instance while preserving ownership semantics.
Tagged storage for one of two possible value types.
Definition utility.h:750
Owning contiguous buffer with an explicit logical element count.
Definition utility.h:1398
size_t size() const
Definition utility.h:1485
element_type * pointer
Definition utility.h:823
Declarations for common platform specific utilities.
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC egl_image_target_texture_2d()
Bind an EGL image to the current OpenGL texture target.
Definition graphics.cpp:65
GladGLContext ctx
Loaded OpenGL function table for the active context.
Definition graphics.cpp:58
std::optional< rgb_t > import_source(display_t::pointer egl_display, const surface_descriptor_t &xrgb)
Import the source frame texture for EGL/OpenGL conversion.
Definition graphics.cpp:772
void free_frame(AVFrame *frame)
Release an FFmpeg frame allocated by the capture or conversion backend.
Definition graphics.cpp:1539
std::optional< ctx_t > make_ctx(display_t::pointer display)
Create an EGL/OpenGL context for capture or conversion.
Definition graphics.cpp:573
bool fail()
Log EGL failure details and return an error code.
Definition graphics.cpp:491
std::optional< nv12_t > create_nv12_target(int width, int height, AVPixelFormat format)
Create biplanar YUV textures to render into.
Definition graphics.cpp:981
int init()
Load GBM symbols required for EGL device creation.
Definition graphics.cpp:310
rgb_t create_blank(platf::img_t &img)
Create a black RGB texture of the specified image size.
Definition graphics.cpp:806
device_destroy_fn device_destroy
Device destroy.
Definition graphics.cpp:304
void ensure_privileged_egl_worker_started()
Allows the EGL privileged worker thread to be constructed early.
Definition graphics.cpp:484
display_t make_display(std::variant< gbm::gbm_t::pointer, wl_display *, _XDisplay * > native_display)
Open and initialize the display connection used for capture.
Definition graphics.cpp:495
std::optional< nv12_t > 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.
Definition graphics.cpp:881
void drain_errors(const std::string_view &prefix)
Drain and log pending OpenGL errors.
Definition graphics.cpp:72
create_device_fn create_device
Create device.
Definition graphics.cpp:305
std::optional< yuv444_t > create_yuv444_target(int width, int height, AVPixelFormat format)
Create YUV444 target.
Definition graphics.cpp:1024
void(*)(device *gbm) device_destroy_fn
Function pointer used to destroy a GBM device.
Definition graphics.h:361
int close(int __fd)
Release the native resource held by the RAII wrapper.
device *(*)(int fd) create_device_fn
Function pointer used to create a GBM device from a file descriptor.
Definition graphics.h:365
util::safe_ptr< AVFrame, &free_frame > frame_t
Owning pointer for an EGL frame object.
Definition graphics.h:66
util::dyn_safe_ptr< device, &device_destroy > gbm_t
Owning GBM device pointer released with the GBM destroy callback.
Definition graphics.h:373
util::dyn_safe_ptr_v2< void, EGLBoolean, &eglTerminate > display_t
Owning pointer for an EGL display connection.
Definition graphics.h:388
void(*)(GLenum target, void *image) PFNGLEGLIMAGETARGETTEXTURE2DOESPROC
Function pointer type for glEGLImageTargetTexture2DOES.
Definition graphics.h:77
Miscellaneous declarations for Linux.
bl::sources::severity_logger< int > warning
Strange events.
Definition logging.cpp:45
Declarations for logging related functions.
NV12 capture image backed by EGL and OpenGL resources.
Definition graphics.h:403
EGLImage bg88
EGL image for the NV12 interleaved chroma plane.
Definition graphics.h:406
static constexpr std::size_t num_fds
Maximum number of DMA-BUF plane file descriptors exported by VAAPI.
Definition graphics.h:412
gl::tex_t tex
Textures containing the imported Y and UV planes.
Definition graphics.h:408
display_t::pointer display
EGL display that owns the imported planes.
Definition graphics.h:404
gl::frame_buf_t buf
OpenGL framebuffer object used for rendering.
Definition graphics.h:409
std::array< file_t, num_fds > fds
DMA-BUF file descriptors for each exported plane.
Definition graphics.h:414
EGLImage r8
EGL image for the NV12 luma plane.
Definition graphics.h:405
RGB capture image backed by EGL and OpenGL resources.
Definition graphics.h:393
display_t::pointer display
EGL display that owns the imported image.
Definition graphics.h:394
EGLImage xrgb8
EGL image for the imported XRGB plane.
Definition graphics.h:395
gl::tex_t tex
Texture containing the imported RGB plane.
Definition graphics.h:397
EGL surface descriptor used to import a captured DMA-BUF.
Definition graphics.h:497
std::array< std::uint32_t, 4 > pitches
Row stride in bytes for each DMA-BUF plane.
Definition graphics.h:503
std::uint32_t fourcc
DRM fourcc pixel format for the buffer.
Definition graphics.h:501
std::array< std::uint32_t, 4 > offsets
Byte offset to the first pixel for each DMA-BUF plane.
Definition graphics.h:504
std::uint64_t modifier
DRM format modifier describing the buffer layout.
Definition graphics.h:502
std::array< int, 4 > fds
DMA-BUF file descriptors for up to four planes.
Definition graphics.h:500
int height
Frame or display height in pixels.
Definition graphics.h:499
int width
Frame or display width in pixels.
Definition graphics.h:498
YUV 4:4:4 capture image backed by EGL and OpenGL resources.
Definition graphics.h:420
EGLImage b8
EGL image for the V plane.
Definition graphics.h:424
display_t::pointer display
EGL display that owns the imported planes.
Definition graphics.h:421
EGLImage r8
EGL image for the Y plane.
Definition graphics.h:422
std::array< file_t, num_fds > fds
DMA-BUF file descriptors for each exported plane.
Definition graphics.h:431
EGLImage g8
EGL image for the U plane.
Definition graphics.h:423
gl::frame_buf_t buf
OpenGL framebuffer object used for rendering.
Definition graphics.h:427
static constexpr std::size_t num_fds
Num fds.
Definition graphics.h:429
gl::tex_t tex
Textures containing the imported Y, U, and V planes.
Definition graphics.h:426
Captured frame buffer shared between capture and encode stages.
Definition common.h:530
Sunshine colorimetry values derived from stream configuration.
Definition video_colorspace.h:36
Declarations for utility functions.
safe_ptr< T, dynamic< T, ReturnType, function > > dyn_safe_ptr_v2
Safe pointer wrapper for runtime-resolved release functions with status returns.
Definition utility.h:1629
#define TUPLE_2D_REF(a, b, expr)
Bind both elements of an existing 2-tuple expression to local references.
Definition utility.h:165
uniq_ptr< T, Destroy< T *, void, function > > safe_ptr
Unique pointer using a compile-time C-style destroy function.
Definition utility.h:1590
#define KITTY_USING_MOVE_T(move_t, t, init_val, z)
Define a move-only RAII wrapper with caller-provided initial value and destructor body.
Definition utility.h:51
safe_ptr< T, dynamic< T, void, function > > dyn_safe_ptr
Unique pointer using a runtime-provided destroy function.
Definition utility.h:1623
Declarations for colorspace functions.