26#define SUNSHINE_STRINGIFY_HELPER(x) #x
31#define SUNSHINE_STRINGIFY(x) SUNSHINE_STRINGIFY_HELPER(x)
36#define gl_drain_errors_helper(x) gl::drain_errors(x)
41#define gl_drain_errors gl_drain_errors_helper(__FILE__ ":" SUNSHINE_STRINGIFY(__LINE__))
52extern "C" struct _XDisplay;
68 extern GladGLContext ctx;
151 inline void bind(std::nullptr_t, std::nullptr_t) {
153 for (
auto fb : (*
this)) {
154 ctx.BindFramebuffer(GL_FRAMEBUFFER, fb);
155 ctx.FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, 0, 0);
169 void bind(It it_begin, It it_end) {
170 using namespace std::literals;
171 if (std::distance(it_begin, it_end) >
size()) {
172 BOOST_LOG(
warning) <<
"To many elements to bind"sv;
177 std::for_each(it_begin, it_end, [&](
auto tex) {
178 ctx.BindFramebuffer(GL_FRAMEBUFFER, (*
this)[x]);
179 ctx.BindTexture(GL_TEXTURE_2D, tex);
181 ctx.FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + x, tex, 0);
197 void copy(
int id,
int texture,
int offset_x,
int offset_y,
int width,
int height);
205 if (el != std::numeric_limits<GLuint>::max()) {
206 ctx.DeleteShader(el);
235 shader_internal_t _shader;
243 if (el != std::numeric_limits<GLuint>::max()) {
244 ctx.DeleteBuffers(1, &el);
271 const char *
block()
const;
279 void update(
const std::string_view &view, std::size_t offset = 0);
287 void update(std::string_view *members, std::size_t count, std::size_t offset = 0);
296 buffer_internal_t _buffer;
304 if (el != std::numeric_limits<GLuint>::max()) {
305 ctx.DeleteProgram(el);
341 std::optional<buffer_t>
uniform(
const char *block, std::pair<const char *, std::string_view> *members, std::size_t count);
351 program_internal_t _program;
360 typedef void (*device_destroy_fn)(device *gbm);
364 typedef device *(*create_device_fn)(
int fd);
413 std::array<file_t, num_fds>
fds;
430 std::array<file_t, num_fds>
fds;
436 eglDestroyImage(el.display, el.xrgb8);
442 eglDestroyImage(el.display, el.r8);
446 eglDestroyImage(el.display, el.bg88);
452 eglDestroyImage(el.display, el.r8);
456 eglDestroyImage(el.display, el.g8);
460 eglDestroyImage(el.display, el.b8);
467 eglMakeCurrent(disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
468 eglDestroyContext(disp, ctx);
542 std::array<file_t, nv12_img_t::num_fds> &&fds,
559 std::array<file_t, yuv444_img_t::num_fds> &&fds,
572 std::optional<nv12_t>
create_nv12_target(
int width,
int height, AVPixelFormat format);
613 for (
auto x = 0;
x < 4; ++
x) {
631 std::optional<uint64_t>
pts;
632 std::optional<uint64_t>
seq;
720 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:34
Cursor image and hotspot metadata captured from the window system.
Definition graphics.h:587
std::vector< std::uint8_t > buffer
Cursor image pixels.
Definition graphics.h:596
unsigned long serial
X11 cursor serial used to detect cursor image changes.
Definition graphics.h:594
int y
Cursor hotspot or surface Y coordinate.
Definition graphics.h:590
int x
Cursor hotspot or surface X coordinate.
Definition graphics.h:589
int src_w
Cursor source image width in pixels.
Definition graphics.h:591
int src_h
Cursor source image height in pixels.
Definition graphics.h:592
Captured image descriptor shared by EGL conversion paths.
Definition graphics.h:603
std::optional< bool > pw_damage
Whether PipeWire damage tracking should be used.
Definition graphics.h:633
std::optional< uint32_t > pw_flags
PipeWire frame flags reported with the buffer.
Definition graphics.h:634
std::optional< uint64_t > seq
PipeWire frame sequence number.
Definition graphics.h:632
std::uint64_t sequence
Monotonic value used to detect when GL resources must be recreated.
Definition graphics.h:625
surface_descriptor_t sd
DMA-BUF surface descriptor for the captured image.
Definition graphics.h:622
void reset()
Reset the object to its initial empty state.
Definition graphics.h:612
bool y_invert
Whether the shader should invert the Y axis.
Definition graphics.h:628
std::optional< uint64_t > pts
PipeWire presentation timestamp.
Definition graphics.h:631
EGL/OpenGL scaler and colorspace conversion pipeline.
Definition graphics.h:640
gl::tex_t tex
Source and cursor textures used by the conversion pipeline.
Definition graphics.h:749
int offsetY
Offset y.
Definition graphics.h:765
int convert_yuv444(gl::frame_buf_t &fb)
Convert the loaded source image into YUV444 output planes.
Definition graphics.cpp:1374
int in_width
In width.
Definition graphics.h:762
int in_height
In height.
Definition graphics.h:763
gl::frame_buf_t cursor_framebuffer
Cursor framebuffer.
Definition graphics.h:752
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:1224
int out_width
Out width.
Definition graphics.h:760
int convert_nv12(gl::frame_buf_t &fb)
Convert the loaded source image into NV12 output planes.
Definition graphics.cpp:1352
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:997
void apply_colorspace(const video::sunshine_colorspace_t &colorspace, bool is_yuv444)
Apply the configured colorspace metadata to the active frame.
Definition graphics.cpp:935
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:1208
int offsetX
Offset x.
Definition graphics.h:764
gl::buffer_t color_matrix
Color matrix.
Definition graphics.h:758
gl::frame_buf_t copy_framebuffer
Copy framebuffer.
Definition graphics.h:753
void load_ram(platf::img_t &img)
Load ram data from the backing API or store.
Definition graphics.cpp:1261
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:1103
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:1268
int loaded_texture
Loaded texture.
Definition graphics.h:768
int out_height
Out height.
Definition graphics.h:761
std::uint64_t serial
Serial.
Definition graphics.h:771
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:1330
std::array< gl::program_t, 4 > program
Program.
Definition graphics.h:757
EGL image buffer with plane descriptors and imported GL textures.
Definition graphics.h:241
void update(const std::string_view &view, std::size_t offset=0)
Update one uniform member in the block buffer.
Definition graphics.cpp:180
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:159
const char * block() const
Query a uniform block index from an OpenGL program.
Definition graphics.cpp:176
GLuint handle() const
Return the native handle owned by the wrapper.
Definition graphics.cpp:172
OpenGL framebuffer handle wrapper.
Definition graphics.h:123
void bind(std::nullptr_t, std::nullptr_t)
Bind each framebuffer and clear its color attachment.
Definition graphics.h:151
void bind(It it_begin, It it_end)
Bind textures to this object's framebuffers as color attachments.
Definition graphics.h:169
static frame_buf_t make(std::size_t count)
Allocate the underlying object and wrap it in the owning handle.
Definition graphics.cpp:106
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:114
OpenGL shader program with attached shader stages.
Definition graphics.h:302
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:245
GLuint handle() const
Return the native handle owned by the wrapper.
Definition graphics.cpp:293
std::string err_str()
Read the program linker log.
Definition graphics.cpp:197
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:211
void bind(const buffer_t &buffer)
Bind this program and attach a uniform buffer block.
Definition graphics.cpp:238
OpenGL shader object that compiles GLSL source.
Definition graphics.h:203
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:135
std::string err_str()
Read the shader compiler log.
Definition graphics.cpp:121
GLuint handle() const
Return the native handle owned by the wrapper.
Definition graphics.cpp:155
OpenGL texture handle wrapper.
Definition graphics.h:94
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:81
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:756
Owning contiguous buffer with an explicit logical element count.
Definition utility.h:1394
size_t size() const
Definition utility.h:1481
Unique pointer wrapper with customizable pointer and deleter types.
Definition utility.h:820
element_type * pointer
Pointer type stored by the unique pointer wrapper.
Definition utility.h:829
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:61
GladGLContext ctx
Loaded OpenGL function table for the active context.
Definition graphics.cpp:54
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:640
std::optional< ctx_t > make_ctx(display_t::pointer display)
Create an EGL/OpenGL context for capture or conversion.
Definition graphics.cpp:422
bool fail()
Log EGL failure details and return an error code.
Definition graphics.cpp:340
std::optional< nv12_t > create_nv12_target(int width, int height, AVPixelFormat format)
Create biplanar YUV textures to render into.
Definition graphics.cpp:849
rgb_t create_blank(platf::img_t &img)
Create a black RGB texture of the specified image size.
Definition graphics.cpp:674
device_destroy_fn device_destroy
Device destroy.
Definition graphics.cpp:300
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:344
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:749
void drain_errors(const std::string_view &prefix)
Drain and log pending OpenGL errors.
Definition graphics.cpp:68
create_device_fn create_device
Create device.
Definition graphics.cpp:301
std::optional< yuv444_t > create_yuv444_target(int width, int height, AVPixelFormat format)
Create YUV444 target.
Definition graphics.cpp:890
void free_frame(AVFrame *frame)
Release an FFmpeg frame allocated by the capture or conversion backend.
Definition graphics.cpp:1403
int close(int __fd)
Release the native resource held by the RAII wrapper.
void(* device_destroy_fn)(device *gbm)
Function pointer used to destroy a GBM device.
Definition graphics.h:360
void(*)(GLenum target, void *image) PFNGLEGLIMAGETARGETTEXTURE2DOESPROC
Function pointer type for glEGLImageTargetTexture2DOES.
Definition graphics.h:76
device *(* create_device_fn)(int fd)
Function pointer used to create a GBM device from a file descriptor.
Definition graphics.h:364
Miscellaneous declarations for Linux.
bl::sources::severity_logger< int > warning
Strange events.
Definition logging.cpp:43
Declarations for logging related functions.
NV12 capture image backed by EGL and OpenGL resources.
Definition graphics.h:402
EGLImage bg88
EGL image for the NV12 interleaved chroma plane.
Definition graphics.h:405
static constexpr std::size_t num_fds
Maximum number of DMA-BUF plane file descriptors exported by VAAPI.
Definition graphics.h:411
gl::tex_t tex
Textures containing the imported Y and UV planes.
Definition graphics.h:407
display_t::pointer display
EGL display that owns the imported planes.
Definition graphics.h:403
gl::frame_buf_t buf
OpenGL framebuffer object used for rendering.
Definition graphics.h:408
std::array< file_t, num_fds > fds
DMA-BUF file descriptors for each exported plane.
Definition graphics.h:413
EGLImage r8
EGL image for the NV12 luma plane.
Definition graphics.h:404
RGB capture image backed by EGL and OpenGL resources.
Definition graphics.h:392
display_t::pointer display
EGL display that owns the imported image.
Definition graphics.h:393
EGLImage xrgb8
EGL image for the imported XRGB plane.
Definition graphics.h:394
gl::tex_t tex
Texture containing the imported RGB plane.
Definition graphics.h:396
EGL surface descriptor used to import a captured DMA-BUF.
Definition graphics.h:496
std::uint32_t fourcc
DRM fourcc pixel format for the buffer.
Definition graphics.h:500
int fds[4]
DMA-BUF file descriptors for up to four planes.
Definition graphics.h:499
std::uint32_t pitches[4]
Row stride in bytes for each DMA-BUF plane.
Definition graphics.h:502
std::uint64_t modifier
DRM format modifier describing the buffer layout.
Definition graphics.h:501
int height
Frame or display height in pixels.
Definition graphics.h:498
int width
Frame or display width in pixels.
Definition graphics.h:497
std::uint32_t offsets[4]
Byte offset to the first pixel for each DMA-BUF plane.
Definition graphics.h:503
YUV 4:4:4 capture image backed by EGL and OpenGL resources.
Definition graphics.h:419
EGLImage b8
EGL image for the V plane.
Definition graphics.h:423
display_t::pointer display
EGL display that owns the imported planes.
Definition graphics.h:420
EGLImage r8
EGL image for the Y plane.
Definition graphics.h:421
std::array< file_t, num_fds > fds
DMA-BUF file descriptors for each exported plane.
Definition graphics.h:430
EGLImage g8
EGL image for the U plane.
Definition graphics.h:422
gl::frame_buf_t buf
OpenGL framebuffer object used for rendering.
Definition graphics.h:426
static constexpr std::size_t num_fds
Num fds.
Definition graphics.h:428
gl::tex_t tex
Textures containing the imported Y, U, and V planes.
Definition graphics.h:425
Captured frame buffer shared between capture and encode stages.
Definition common.h:502
Sunshine colorimetry values derived from stream configuration.
Definition video_colorspace.h:26
Declarations for utility functions.
#define TUPLE_2D_REF(a, b, expr)
Bind both elements of an existing 2-tuple expression to local references.
Definition utility.h:162
#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:48
Declarations for colorspace functions.