Compare commits

..

45 Commits
4.3.2 ... 5.0.2

Author SHA1 Message Date
dec05eba
802067d1df -overlapping-replay > -overlap-replay 2025-01-24 00:29:57 +01:00
dec05eba
b55096544b Add version to --info output 2025-01-23 23:56:19 +01:00
dec05eba
e87ade6ee3 Add -overlapping-replay option to clear replay buffer after a save 2025-01-23 23:52:31 +01:00
dec05eba
832052a012 m 2025-01-19 00:32:20 +01:00
dec05eba
bae0fdd949 Mesa check version 24.3.6 2025-01-18 21:59:29 +01:00
dec05eba
bcaf312bca 5.0.1 2025-01-18 21:38:39 +01:00
dec05eba
b559c56d80 Mention amd recording performance issue 2025-01-18 21:38:17 +01:00
dec05eba
59df69bf6a amd: disable vaapi surface copy unless mesa 2.3.4 is used, which fixes a performance issue. Otherwise we get stutter in some games 2025-01-18 21:12:28 +01:00
dec05eba
b68400ca20 Add -gl-debug option to make it easier to debug user issues that cant easily be reproduced 2025-01-16 22:55:20 +01:00
dec05eba
4211dfa2f8 Mention OpenMandriva package 2025-01-13 21:29:22 +01:00
dec05eba
5baa4b82e3 Fix possibility of monitor captured changing on wayland when monitors are reconfigured 2025-01-13 00:42:00 +01:00
dec05eba
3a200a4c9f Workaround teamspeak crashing when recording app audio 2025-01-10 23:59:15 +01:00
dec05eba
f6f8f20686 Update TODO 2025-01-08 17:28:17 +01:00
dec05eba
43d353b7b4 Unset DRI_PRIME as well when gpu offloading cant be used 2025-01-08 17:17:33 +01:00
dec05eba
621f253f00 Minor change 2025-01-03 17:14:30 +01:00
dec05eba
68a7dc1b7f README: mention icc profile 2025-01-02 10:42:27 +01:00
dec05eba
fbaa73bfc7 Prefix program arguments error with error: 2024-12-31 10:40:07 +01:00
dec05eba
01e3a7a8cd Use poll instead of select 2024-12-30 05:24:40 +01:00
dec05eba
027b29cb6e 5.0.0 2024-12-29 21:13:10 +01:00
David Rosca
954f9abe2a kms_server: Use MOD_INVALID when modifiers are not supported
Fixes VAAPI import on older AMD cards that doesn't support modifiers.
2024-12-28 10:39:25 +01:00
dec05eba
2c51e8630d Exit with exit code 50 if invalid audio device. Exit with exit code 51 if invalid monitor 2024-12-26 15:21:47 +01:00
dec05eba
c1048a3d20 Make '-w screen' capture the first monitor on nvidia x11 as well to make it work like amd, intel and nvidia wayland. Keep screen-direct for all monitors, that is gsync compatible 2024-12-26 13:49:20 +01:00
dec05eba
a006261ade Fix --list-capture-options with card path not working on x11 nvidia because x11 nvidia doesn't use dri 2024-12-09 01:57:10 +01:00
dec05eba
3af2ea52c2 4.3.5 - fix build on some distros with older spa version 2024-12-08 23:33:48 +01:00
dec05eba
da08e318a6 Remove shit 2024-12-08 23:26:14 +01:00
dec05eba
10dd7f1c64 Yeet the unecessary properties error location code, it fails to build on some versions of spa 2024-12-08 23:24:36 +01:00
dec05eba
fc45439b31 Attempt to fix build on some distros 2024-12-08 23:01:29 +01:00
dec05eba
0b9fe9a897 4.3.4 - minor fixes and refactoring 2024-12-08 03:15:18 +01:00
dec05eba
a25cbc1ef6 Fix window creation on wayland 2024-12-08 03:14:00 +01:00
dec05eba
d138a1fe73 Add card path to --info, add card path and vendor to --list-capture-options if known to speed up the command 2024-12-08 02:52:58 +01:00
dec05eba
c259a19b9d Refactor windowing from egl to window_x11/window_wayland, yolo 2024-12-08 02:17:41 +01:00
dec05eba
655fd3756b Comment out unused egl priority for now because amd error is misleading 2024-12-07 22:39:03 +01:00
dec05eba
175ed79b06 Fix virtual sink not destroyed if gsr is forcefully killed (use pipewire to create the virtual sink instead of pulseaudio) 2024-12-06 12:53:44 +01:00
dec05eba
1b40947884 Add error output when reaching max ports/stream nodes/links 2024-12-05 19:14:00 +01:00
dec05eba
8941e12c61 Increase pipewire audio max nodes/ports limit 2024-12-05 19:08:01 +01:00
dec05eba
00b132c7b4 Remove incorrect references to -aa and -aai 2024-12-05 17:48:21 +01:00
dec05eba
508934b14a Mention fedora package, remove old and likely broken gentoo ebuild 2024-11-30 13:24:30 +01:00
dec05eba
51265d3536 --help improvement 2024-11-29 18:36:11 +01:00
dec05eba
0ae3c1f827 Repurpose '/' in audio argument to set the audio track name, not the recording node name in pulseaudio/pipewire. This also now allows setting audio track name when recording application audio 2024-11-28 11:42:39 +01:00
dec05eba
c2cd9c3473 Print --help to stdout instead of stderr 2024-11-28 10:42:23 +01:00
dec05eba
d472d8d41e Dont allow running the program as the root user 2024-11-24 22:52:12 +01:00
dec05eba
8644c72ac9 4.3.3 - fix startup on linux mint 2024-11-22 18:56:45 +01:00
dec05eba
41b3136797 Only report monitors when /dev/dri/cardN is available (its not available on linux mint for some reason with prime setup) 2024-11-22 18:13:01 +01:00
dec05eba
8e94d6a696 Dont run any pipewire if audio server is not pipewire (fix freeze on linux mint 21.3 pulseaudio system in flatpak) 2024-11-22 17:36:23 +01:00
dec05eba
dcc1bf1e2a Remove GSR_APP_AUDIO macro use when not needed 2024-11-22 00:14:33 +01:00
29 changed files with 1485 additions and 1052 deletions

View File

@@ -23,9 +23,8 @@ Supported audio codecs:
## Note
This software works on X11 and Wayland on AMD, Intel and NVIDIA.
### TEMPORARY ISSUES
1) screen-direct capture has been temporary disabled as it causes issues with stuttering. This might be a nvfbc bug.
2) Videos are in variable framerate format. Use MPV to play such videos, otherwise you might experience stuttering in the video if you are using a buggy video player. You can try saving the video into a .mkv file instead as some software may have better support for .mkv files (such as kdenlive). You can use the "-fm cfr" option to to use constant framerate mode.
3) FLAC audio codec is disabled at the moment because of temporary issues.
1) Videos are in variable framerate format. Use MPV to play such videos, otherwise you might experience stuttering in the video if you are using a buggy video player. You can try saving the video into a .mkv file instead as some software may have better support for .mkv files (such as kdenlive). You can use the "-fm cfr" option to to use constant framerate mode.
2) FLAC audio codec is disabled at the moment because of temporary issues.
### AMD/Intel/Wayland root permission
When recording a window or when using the `-w portal` option under AMD/Intel no special user permission is required,
however when recording a monitor (or when using wayland) the program needs root permission (to access KMS).\
@@ -37,14 +36,15 @@ When recording Legend of Zelda Breath of the Wild at 4k, fps drops from 30 to 7
When recording GTA V at 4k on highest settings, fps drops from 60 to 23 when using obs-nvfbc + nvenc, however when using this screen recorder the fps only drops to 58.\
GPU Screen Recorder also produces much smoother videos than OBS when GPU utilization is close to 100%, see comparison here: [https://www.youtube.com/watch?v=zfj4sNVLLLg](https://www.youtube.com/watch?v=zfj4sNVLLLg).\
GPU Screen Recorder has much better performance than OBS Studio even with version 30.2 that does "zero-copy" recording and encoding, see: [https://www.youtube.com/watch?v=jdroRjibsDw](https://www.youtube.com/watch?v=jdroRjibsDw).\
It is recommended to save the video to a SSD because of the large file size, which a slow HDD might not be fast enough to handle. Using variable framerate mode (-fm vfr) which is the default is also recommended as this reduces encoding load. Ultra quality is also overkill most of the time, very high (the default) or lower quality is usually enough.
It is recommended to save the video to a SSD because of the large file size, which a slow HDD might not be fast enough to handle. Using variable framerate mode (-fm vfr) which is the default is also recommended as this reduces encoding load. Ultra quality is also overkill most of the time, very high (the default) or lower quality is usually enough.\
Note that recording on AMD can have some performance issues on Wayland in the recording itself when recording without desktop portal unless your mesa version is 25.0.0 or greater.
## Note about optimal performance on NVIDIA
NVIDIA driver has a "feature" (read: bug) where it will downclock memory transfer rate when a program uses cuda (or nvenc, which uses cuda), such as GPU Screen Recorder. To work around this bug, GPU Screen Recorder can overclock your GPU memory transfer rate to it's normal optimal level.\
To enable overclocking for optimal performance use the `-oc` option when running GPU Screen Recorder. You also need to have "Coolbits" NVIDIA X setting set to "12" to enable overclocking. You can automatically add this option if you run `sudo nvidia-xconfig --cool-bits=12` and then reboot your computer.\
Note that this only works when Xorg server is running as root, and using this option will only give you a performance boost if the game you are recording is bottlenecked by your GPU.\
Note! use at your own risk!
# VRR/G-SYNC
This should work fine on AMD/Intel X11 or Wayland. On Nvidia X11 G-SYNC only works with the -w screen-direct-force option, but because of bugs in the Nvidia driver this option is not always recommended.
This should work fine on AMD/Intel X11 or Wayland. On Nvidia X11 G-SYNC only works with the -w screen-direct option, but because of bugs in the Nvidia driver this option is not always recommended.
For example it can cause your computer to freeze when recording certain games.
# Installation
@@ -62,7 +62,8 @@ Here are some known unofficial packages:
* Debian/Ubuntu: [Pacstall](https://pacstall.dev/packages/gpu-screen-recorder)
* Nix: [NixOS wiki](https://wiki.nixos.org/wiki/Gpu-screen-recorder)
* openSUSE: [openSUSE software repository](https://software.opensuse.org/package/gpu-screen-recorder)
* Gentoo: [Guru](https://github.com/gentoo/guru/blob/master/media-video/gpu-screen-recorder/gpu-screen-recorder-9999.ebuild)
* Fedora: [Copr](https://copr.fedorainfracloud.org/coprs/brycensranch/gpu-screen-recorder-git/)
* OpenMandriva: [gpu-screen-recorder](https://github.com/OpenMandrivaAssociation/gpu-screen-recorder/tree/master)
# Dependencies
GPU Screen Recorder uses meson build system so you need to install `meson` to build GPU Screen Recorder.
@@ -179,3 +180,5 @@ You have to either record in hdr mode (-k `hevc_hdr` or -k `av1_hdr` option) to
You can record with desktop portal option (`-w portal`) instead which ignores night light, if you are ok with recording without HDR.
## Kdenlive says that the video is not usable for editing because it has variable frame rate
To fix this you can either record the video in .mkv format or constant frame rate (-fm cfr).
## Colors look incorrect when recording HDR (with hevc_hdr/av1_hdr) or using an ICC profile
The latest version of KDE Plasma breaks HDR and ICC profiles for recording applications. Wayland in general doesn't properly support recording HDR yet. Use desktop portal option (`-w portal`) for now to turn HDR recording into SDR and to be able to record with correct colors when using an ICC profile.

32
TODO
View File

@@ -69,8 +69,6 @@ Exit if X11/Wayland killed (if drm plane dead or something?)
Use SRC_W and SRC_H for screen plane instead of crtc_w and crtc_h.
Make it possible to select which /dev/dri/card* to use, but that requires opengl to also use the same card. Not sure if that is possible for amd, intel and nvidia without using vulkan instead.
Test if p2 state can be worked around by using pure nvenc api and overwriting cuInit/cuCtxCreate* to not do anything. Cuda might be loaded when using nvenc but it might not be used, with certain record options? (such as h264 p5).
nvenc uses cuda when using b frames and rgb->yuv conversion, so convert the image ourselves instead.-
@@ -193,3 +191,33 @@ Cleanup pipewire_audio.c (proper error handling and memory cleanup of proxies).
Hide application audio module-null-sink by using sink_properties=media.class="Audio/Sink/Internal".
Improve software encoding performance.
Add option to record audio from the recorded window only.
Add option to automatically select best video codec available. Add -k best, -k best_10bit and -k best_hdr.
HDR is broken on kde plasma > 6.2 because of change to how HDR metadata works. See https://github.com/dec05eba/gpu-screen-recorder-issues/issues/60.
Use wayland color management protocol when it's available: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/14.
Use different exit codes for different errors. Use one for invalid -w option, another one for invalid -a option for audio devices, etc. This is to make UI error reporting better.
Document these exit codes in an exit code .md file, or finally create a manpage where this can be documented.
Ffmpeg fixed black bars in videos on amd when using hevc and when recording at some resolutions, such as 1080p:
https://github.com/FFmpeg/FFmpeg/commit/bcfbf2bac8f9eeeedc407b40596f5c7aaa0d5b47
https://github.com/FFmpeg/FFmpeg/commit/d0facac679faf45d3356dff2e2cb382580d7a521
Disable gpu screen recorder black bar handling when using hevc on amd when the libavcodec version is the one that comes after those commits.
Use opengl compute shader instead of graphics shader. This might allow for better performance when games are using 100% of graphics unit which might fix issue with 100% gpu usage causing gpu screen recorder to run slow when not using vaapi to convert rgb to nv12(?).
Always disable prime run/dri prime and list all monitors to record from from all cards.
Do this instead of adding an option to choose which gpu to use.
On X11 the primary gpu will always have the framebuffer for all monitors combined.
Use randr to list all monitors and always record and encode with the primary gpu.
On Wayland each gpu will have its own list of monitors with framebuffers.
Iterate through all cards with drm and list all monitors with associated framebuffers and when choosing a monitor to record
automatically use the associated gpu card.
Allow flv av1 if recent ffmpeg version and streaming to youtube (and twitch?) and for custom services.
Use explicit sync in pipewire video code: https://docs.pipewire.org/page_dma_buf.html.
Support vaapi rotation. Support for it is added in mesa here: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32919.

View File

@@ -17,6 +17,7 @@ typedef enum {
typedef struct {
gsr_egl *egl;
Display *display;
bool track_cursor;
gsr_damage_track_type track_type;

View File

@@ -10,6 +10,8 @@
#include "vec2.h"
#include "defs.h"
typedef struct gsr_window gsr_window;
#ifdef _WIN64
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
@@ -152,54 +154,11 @@ typedef int (*FUNC_eglQueryDisplayAttribEXT)(EGLDisplay dpy, int32_t attribute,
typedef const char* (*FUNC_eglQueryDeviceStringEXT)(void *device, int32_t name);
typedef int (*FUNC_eglQueryDmaBufModifiersEXT)(EGLDisplay dpy, int32_t format, int32_t max_modifiers, uint64_t *modifiers, int *external_only, int32_t *num_modifiers);
#define GSR_MAX_OUTPUTS 32
typedef struct {
char *name;
vec2i pos;
vec2i size;
uint32_t connector_id;
gsr_monitor_rotation rotation;
uint32_t monitor_identifier; /* crtc id */
} gsr_x11_output;
typedef struct {
Display *dpy;
Window window;
gsr_x11_output outputs[GSR_MAX_OUTPUTS];
int num_outputs;
XEvent xev;
} gsr_x11;
typedef struct {
uint32_t wl_name;
void *output;
vec2i pos;
vec2i size;
int32_t transform;
char *name;
} gsr_wayland_output;
typedef struct {
void *dpy;
void *window;
void *registry;
void *surface;
void *compositor;
gsr_wayland_output outputs[GSR_MAX_OUTPUTS];
int num_outputs;
} gsr_wayland;
typedef enum {
GSR_GL_CONTEXT_TYPE_EGL,
GSR_GL_CONTEXT_TYPE_GLX
} gsr_gl_context_type;
typedef enum {
GSR_DISPLAY_SERVER_X11,
GSR_DISPLAY_SERVER_WAYLAND
} gsr_display_server;
typedef struct gsr_egl gsr_egl;
struct gsr_egl {
void *egl_library;
@@ -207,6 +166,7 @@ struct gsr_egl {
void *gl_library;
gsr_gl_context_type context_type;
gsr_window *window;
EGLDisplay egl_display;
EGLSurface egl_surface;
@@ -218,8 +178,6 @@ struct gsr_egl {
gsr_gpu_info gpu_info;
gsr_x11 x11;
gsr_wayland wayland;
char card_path[128];
int32_t (*eglGetError)(void);
@@ -319,15 +277,10 @@ struct gsr_egl {
unsigned char (*glUnmapBuffer)(unsigned int target);
};
bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland, bool is_monitor_capture);
bool gsr_egl_load(gsr_egl *self, gsr_window *window, bool is_monitor_capture, bool enable_debug);
void gsr_egl_unload(gsr_egl *self);
/* Returns true if an event is available */
bool gsr_egl_process_event(gsr_egl *self);
/* Does opengl swap with egl or glx, depending on which one is active */
void gsr_egl_swap_buffers(gsr_egl *self);
gsr_display_server gsr_egl_get_display_server(const gsr_egl *self);
XEvent* gsr_egl_get_event_data(gsr_egl *self);
#endif /* GSR_EGL_H */

View File

@@ -9,8 +9,9 @@
#include <stdbool.h>
#define GSR_PIPEWIRE_AUDIO_MAX_STREAM_NODES 128
#define GSR_PIPEWIRE_AUDIO_MAX_PORTS 128
#define GSR_PIPEWIRE_AUDIO_MAX_PORTS 256
#define GSR_PIPEWIRE_AUDIO_MAX_REQUESTED_LINKS 32
#define GSR_PIPEWIRE_AUDIO_MAX_VIRTUAL_SINKS 32
typedef enum {
GSR_PIPEWIRE_AUDIO_NODE_TYPE_STREAM_OUTPUT, /* Application audio */
@@ -67,11 +68,16 @@ typedef struct {
gsr_pipewire_audio_requested_link requested_links[GSR_PIPEWIRE_AUDIO_MAX_REQUESTED_LINKS];
int num_requested_links;
struct pw_proxy *virtual_sink_proxies[GSR_PIPEWIRE_AUDIO_MAX_VIRTUAL_SINKS];
int num_virtual_sink_proxies;
} gsr_pipewire_audio;
bool gsr_pipewire_audio_init(gsr_pipewire_audio *self);
void gsr_pipewire_audio_deinit(gsr_pipewire_audio *self);
bool gsr_pipewire_audio_create_virtual_sink(gsr_pipewire_audio *self, const char *name);
/*
This function links audio source outputs from applications that match the name |app_names| to the input
that matches the name |stream_name_input|.

View File

@@ -26,6 +26,17 @@ typedef struct {
unsigned int frames;
} SoundDevice;
struct AudioDevice {
std::string name;
std::string description;
};
struct AudioDevices {
std::string default_output;
std::string default_input;
std::vector<AudioDevice> audio_inputs;
};
enum class AudioInputType {
DEVICE,
APPLICATION
@@ -33,18 +44,12 @@ enum class AudioInputType {
struct AudioInput {
std::string name;
std::string description;
AudioInputType type = AudioInputType::DEVICE;
bool inverted = false;
};
struct AudioDevices {
std::string default_output;
std::string default_input;
std::vector<AudioInput> audio_inputs;
};
struct MergedAudioInputs {
std::string track_name;
std::vector<AudioInput> audio_inputs;
};
@@ -59,11 +64,6 @@ typedef enum {
Returns 0 on success, or a negative value on failure.
*/
int sound_device_get_by_name(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format);
/*
Creates a module-combine-sink and connects to it for recording, returning the device into the |device| parameter.
Returns 0 on success, or a negative value on failure.
*/
int sound_device_create_combined_sink_connect(SoundDevice *device, const char *combined_sink_name, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format);
void sound_device_close(SoundDevice *device);

View File

@@ -7,6 +7,8 @@
#include <stdbool.h>
#include <stdint.h>
#define CONNECTOR_TYPE_COUNTS 32
typedef struct AVCodecContext AVCodecContext;
typedef struct AVFrame AVFrame;
@@ -27,20 +29,30 @@ typedef struct {
bool found_monitor;
} get_monitor_by_name_userdata;
typedef struct {
int type;
int count;
int count_active;
} drm_connector_type_count;
double clock_get_monotonic_seconds(void);
bool generate_random_characters(char *buffer, int buffer_size, const char *alphabet, size_t alphabet_size);
bool generate_random_characters_standard_alphabet(char *buffer, int buffer_size);
typedef void (*active_monitor_callback)(const gsr_monitor *monitor, void *userdata);
void for_each_active_monitor_output_x11_not_cached(Display *display, active_monitor_callback callback, void *userdata);
void for_each_active_monitor_output_x11(const gsr_egl *egl, active_monitor_callback callback, void *userdata);
void for_each_active_monitor_output(const gsr_egl *egl, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata);
void for_each_active_monitor_output(const gsr_window *window, const char *card_path, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata);
bool get_monitor_by_name(const gsr_egl *egl, gsr_connection_type connection_type, const char *name, gsr_monitor *monitor);
gsr_monitor_rotation drm_monitor_get_display_server_rotation(const gsr_egl *egl, const gsr_monitor *monitor);
gsr_monitor_rotation drm_monitor_get_display_server_rotation(const gsr_window *window, const gsr_monitor *monitor);
int get_connector_type_by_name(const char *name);
drm_connector_type_count* drm_connector_types_get_index(drm_connector_type_count *type_counts, int *num_type_counts, int connector_type);
uint32_t monitor_identifier_from_type_and_count(int monitor_type_index, int monitor_type_count);
bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info);
bool gl_driver_version_greater_than(const gsr_egl *egl, int major, int minor, int patch);
bool gl_driver_version_greater_than(const gsr_gpu_info *gpu_info, int major, int minor, int patch);
bool try_card_has_valid_plane(const char *card_path);
/* |output| should be at least 128 bytes in size */
bool gsr_get_valid_card_path(gsr_egl *egl, char *output, bool is_monitor_capture);
/* |render_path| should be at least 128 bytes in size */

37
include/window/window.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef GSR_WINDOW_H
#define GSR_WINDOW_H
#include "../utils.h"
#include <stdbool.h>
typedef union _XEvent XEvent;
typedef struct gsr_window gsr_window;
typedef enum {
GSR_DISPLAY_SERVER_X11,
GSR_DISPLAY_SERVER_WAYLAND
} gsr_display_server;
struct gsr_window {
void (*destroy)(gsr_window *self);
/* Returns true if an event is available */
bool (*process_event)(gsr_window *self);
XEvent* (*get_event_data)(gsr_window *self); /* can be NULL */
gsr_display_server (*get_display_server)(void);
void* (*get_display)(gsr_window *self);
void* (*get_window)(gsr_window *self);
void (*for_each_active_monitor_output_cached)(const gsr_window *self, active_monitor_callback callback, void *userdata);
void *priv;
};
void gsr_window_destroy(gsr_window *self);
/* Returns true if an event is available */
bool gsr_window_process_event(gsr_window *self);
XEvent* gsr_window_get_event_data(gsr_window *self);
gsr_display_server gsr_window_get_display_server(const gsr_window *self);
void* gsr_window_get_display(gsr_window *self);
void* gsr_window_get_window(gsr_window *self);
void gsr_window_for_each_active_monitor_output_cached(const gsr_window *self, active_monitor_callback callback, void *userdata);
#endif /* GSR_WINDOW_H */

View File

@@ -0,0 +1,8 @@
#ifndef GSR_WINDOW_WAYLAND_H
#define GSR_WINDOW_WAYLAND_H
#include "window.h"
gsr_window* gsr_window_wayland_create(void);
#endif /* GSR_WINDOW_WAYLAND_H */

View File

@@ -0,0 +1,10 @@
#ifndef GSR_WINDOW_X11_H
#define GSR_WINDOW_X11_H
#include "window.h"
typedef struct _XDisplay Display;
gsr_window* gsr_window_x11_create(Display *display);
#endif /* GSR_WINDOW_X11_H */

View File

@@ -11,6 +11,7 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <poll.h>
#include <sys/stat.h>
#include <sys/capability.h>
@@ -318,17 +319,14 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
}
fprintf(stderr, "gsr info: gsr_kms_client_init: waiting for server to connect\n");
struct pollfd poll_fd = {
.fd = self->initial_socket_fd,
.events = POLLIN,
.revents = 0
};
for(;;) {
struct timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(self->initial_socket_fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 100 * 1000; // 100 ms
int select_res = select(1 + self->initial_socket_fd, &rfds, NULL, NULL, &tv);
if(select_res > 0) {
int poll_res = poll(&poll_fd, 1, 100);
if(poll_res > 0 && (poll_fd.revents & POLLIN)) {
socklen_t sock_len = 0;
self->initial_client_fd = accept(self->initial_socket_fd, (struct sockaddr*)&remote_addr, &sock_len);
if(self->initial_client_fd == -1) {

View File

@@ -19,6 +19,7 @@
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_mode.h>
#include <drm_fourcc.h>
#define MAX_CONNECTORS 32
@@ -362,7 +363,7 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response, connector_to_crt
response->items[item_index].width = drmfb->width;
response->items[item_index].height = drmfb->height;
response->items[item_index].pixel_format = drmfb->pixel_format;
response->items[item_index].modifier = drmfb->modifier;
response->items[item_index].modifier = drmfb->flags & DRM_MODE_FB_MODIFIERS ? drmfb->modifier : DRM_FORMAT_MOD_INVALID;
response->items[item_index].connector_id = crtc_pair ? crtc_pair->connector_id : 0;
response->items[item_index].is_cursor = property_mask & PLANE_PROPERTY_IS_CURSOR;
if(property_mask & PLANE_PROPERTY_IS_CURSOR) {

View File

@@ -1,4 +1,4 @@
project('gpu-screen-recorder', ['c', 'cpp'], version : '4.3.2', default_options : ['warning_level=2'])
project('gpu-screen-recorder', ['c', 'cpp'], version : '5.0.2', default_options : ['warning_level=2'])
add_project_arguments('-Wshadow', language : ['c', 'cpp'])
if get_option('buildtype') == 'debug'
@@ -21,6 +21,9 @@ src = [
'src/codec_query/nvenc.c',
'src/codec_query/vaapi.c',
'src/codec_query/vulkan.c',
'src/window/window.c',
'src/window/window_x11.c',
'src/window/window_wayland.c',
'src/egl.c',
'src/cuda.c',
'src/xnvctrl.c',

View File

@@ -1,5 +1,5 @@
option('systemd', type : 'boolean', value : true, description : 'Install systemd service file')
option('capabilities', type : 'boolean', value : true, description : 'Set binary admin capability to remove password prompt when recording monitor (without desktop portal option) on amd/intel or nvidia wayland')
option('capabilities', type : 'boolean', value : true, description : 'Set binary admin capability on gsr-kms-server binary to remove password prompt when recording monitor (without desktop portal option) on amd/intel or nvidia wayland')
option('nvidia_suspend_fix', type : 'boolean', value : true, description : 'Install nvidia modprobe config file to tell nvidia driver to preserve video memory on suspend. This is a workaround for an nvidia driver bug that breaks cuda (and gpu screen recorder) on suspend')
option('portal', type : 'boolean', value : true, description : 'Build with support for xdg desktop portal ScreenCast capture (wayland only) (-w portal option)')
option('app_audio', type : 'boolean', value : true, description : 'Build with support for recording a single audio source (-aa option). Requires pipewire')
option('app_audio', type : 'boolean', value : true, description : 'Build with support for recording a single audio source (-aa option). Requires pipewire')

View File

@@ -1,7 +1,7 @@
[package]
name = "gpu-screen-recorder"
type = "executable"
version = "4.3.2"
version = "5.0.2"
platforms = ["posix"]
[config]

View File

@@ -2,6 +2,7 @@
#include "../../include/utils.h"
#include "../../include/color_conversion.h"
#include "../../include/cursor.h"
#include "../../include/window/window.h"
#include "../../kms/client/kms_client.h"
#include <stdlib.h>
@@ -17,6 +18,8 @@
#include <libavutil/mastering_display_metadata.h>
#include <libavformat/avformat.h>
#define FIND_CRTC_BY_NAME_TIMEOUT_SECONDS 2.0
#define HDMI_STATIC_METADATA_TYPE1 0
#define HDMI_EOTF_SMPTE_ST2084 2
@@ -55,6 +58,7 @@ typedef struct {
AVCodecContext *video_codec_context;
bool performance_error_shown;
bool fast_path_failed;
bool mesa_supports_compute_only_vaapi_copy;
//int drm_fd;
//uint64_t prev_sequence;
@@ -62,6 +66,8 @@ typedef struct {
vec2i prev_target_pos;
vec2i prev_plane_size;
double last_time_monitor_check;
} gsr_capture_kms;
static void gsr_capture_kms_cleanup_kms_fds(gsr_capture_kms *self) {
@@ -183,17 +189,19 @@ static int gsr_capture_kms_start(gsr_capture *cap, AVCodecContext *video_codec_c
if(kms_init_res != 0)
return kms_init_res;
self->is_x11 = gsr_egl_get_display_server(self->params.egl) == GSR_DISPLAY_SERVER_X11;
self->is_x11 = gsr_window_get_display_server(self->params.egl->window) == GSR_DISPLAY_SERVER_X11;
const gsr_connection_type connection_type = self->is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
if(self->is_x11)
gsr_cursor_init(&self->x11_cursor, self->params.egl, self->params.egl->x11.dpy);
if(self->is_x11) {
Display *display = gsr_window_get_display(self->params.egl->window);
gsr_cursor_init(&self->x11_cursor, self->params.egl, display);
}
MonitorCallbackUserdata monitor_callback_userdata = {
&self->monitor_id,
self->params.display_to_capture, strlen(self->params.display_to_capture),
0,
};
for_each_active_monitor_output(self->params.egl, connection_type, monitor_callback, &monitor_callback_userdata);
for_each_active_monitor_output(self->params.egl->window, self->params.egl->card_path, connection_type, monitor_callback, &monitor_callback_userdata);
if(!get_monitor_by_name(self->params.egl, connection_type, self->params.display_to_capture, &monitor)) {
fprintf(stderr, "gsr error: gsr_capture_kms_start: failed to find monitor by name \"%s\"\n", self->params.display_to_capture);
@@ -202,7 +210,7 @@ static int gsr_capture_kms_start(gsr_capture *cap, AVCodecContext *video_codec_c
}
monitor.name = self->params.display_to_capture;
self->monitor_rotation = drm_monitor_get_display_server_rotation(self->params.egl, &monitor);
self->monitor_rotation = drm_monitor_get_display_server_rotation(self->params.egl->window, &monitor);
self->capture_pos = monitor.pos;
/* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */
@@ -224,14 +232,17 @@ static int gsr_capture_kms_start(gsr_capture *cap, AVCodecContext *video_codec_c
video_codec_context->height = FFALIGN(self->params.output_resolution.y, 2);
}
self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(self->params.egl, 24, 0, 9);
self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(&self->params.egl->gpu_info, 24, 0, 9);
if(self->fast_path_failed)
fprintf(stderr, "gsr warning: gsr_capture_kms_start: your amd driver (mesa) version is known to be buggy (<= version 24.0.9), falling back to opengl copy\n");
self->mesa_supports_compute_only_vaapi_copy = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && gl_driver_version_greater_than(&self->params.egl->gpu_info, 24, 3, 6);
frame->width = video_codec_context->width;
frame->height = video_codec_context->height;
self->video_codec_context = video_codec_context;
self->last_time_monitor_check = clock_get_monotonic_seconds();
return 0;
}
@@ -240,7 +251,7 @@ static void gsr_capture_kms_on_event(gsr_capture *cap, gsr_egl *egl) {
if(!self->is_x11)
return;
XEvent *xev = gsr_egl_get_event_data(egl);
XEvent *xev = gsr_window_get_event_data(egl->window);
gsr_cursor_on_event(&self->x11_cursor, xev);
}
@@ -425,7 +436,7 @@ static gsr_kms_response_item* find_monitor_drm(gsr_capture_kms *self, bool *capt
}
// Will never happen on wayland unless the target monitor has been disconnected
if(!drm_fd) {
if(!drm_fd && self->is_x11) {
drm_fd = find_largest_drm(&self->kms_response);
*capture_is_combined_plane = true;
}
@@ -525,7 +536,8 @@ static void render_x11_cursor(gsr_capture_kms *self, gsr_color_conversion *color
self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y
};
gsr_cursor_tick(&self->x11_cursor, DefaultRootWindow(self->params.egl->x11.dpy));
Display *display = gsr_window_get_display(self->params.egl->window);
gsr_cursor_tick(&self->x11_cursor, DefaultRootWindow(display));
const vec2i cursor_pos = {
target_pos.x + (self->x11_cursor.position.x - self->x11_cursor.hotspot.x - capture_pos.x) * scale.x,
@@ -551,6 +563,55 @@ static void gsr_capture_kms_update_capture_size_change(gsr_capture_kms *self, gs
}
}
static void gsr_capture_kms_update_connector_ids(gsr_capture_kms *self) {
const double now = clock_get_monotonic_seconds();
if(now - self->last_time_monitor_check < FIND_CRTC_BY_NAME_TIMEOUT_SECONDS)
return;
self->last_time_monitor_check = now;
/* TODO: Assume for now that there is only 1 framebuffer for all monitors and it doesn't change */
if(self->is_x11)
return;
self->monitor_id.num_connector_ids = 0;
const gsr_connection_type connection_type = self->is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
// MonitorCallbackUserdata monitor_callback_userdata = {
// &self->monitor_id,
// self->params.display_to_capture, strlen(self->params.display_to_capture),
// 0,
// };
// for_each_active_monitor_output(self->params.egl->window, self->params.egl->card_path, connection_type, monitor_callback, &monitor_callback_userdata);
gsr_monitor monitor;
if(!get_monitor_by_name(self->params.egl, connection_type, self->params.display_to_capture, &monitor)) {
fprintf(stderr, "gsr error: gsr_capture_kms_update_connector_ids: failed to find monitor by name \"%s\"\n", self->params.display_to_capture);
return;
}
self->monitor_id.num_connector_ids = 1;
self->monitor_id.connector_ids[0] = monitor.connector_id;
monitor.name = self->params.display_to_capture;
self->monitor_rotation = drm_monitor_get_display_server_rotation(self->params.egl->window, &monitor);
self->capture_pos = monitor.pos;
/* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */
if(self->is_x11)
self->capture_size = monitor.size;
else
self->capture_size = rotate_capture_size_if_rotated(self, monitor.size);
}
static void gsr_capture_kms_fail_fast_path_if_not_fast(gsr_capture_kms *self, uint32_t pixel_format) {
const uint8_t pixel_format_color_depth_1 = (pixel_format >> 16) & 0xFF;
if(!self->fast_path_failed && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !self->mesa_supports_compute_only_vaapi_copy && (pixel_format_color_depth_1 == '3' || pixel_format_color_depth_1 == '4')) {
self->fast_path_failed = true;
fprintf(stderr, "gsr warning: gsr_capture_kms_capture: the monitor you are recording is in 10/12-bit color format and your mesa version is <= 24.3.6, composition will be used."
" If you experience performance problems in the video then record on a single window on X11 or use portal capture option instead or disable 10/12-bit color option in your desktop environment settings,"
" or try to record the monitor on X11 instead (if you aren't already doing that) or update your mesa version.\n");
}
}
static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) {
gsr_capture_kms *self = cap->priv;
@@ -570,6 +631,8 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c
return -1;
}
gsr_capture_kms_update_connector_ids(self);
bool capture_is_combined_plane = false;
const gsr_kms_response_item *drm_fd = find_monitor_drm(self, &capture_is_combined_plane);
if(!drm_fd) {
@@ -582,10 +645,13 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c
if(!self->performance_error_shown && self->monitor_rotation != GSR_MONITOR_ROT_0 && video_codec_context_is_vaapi(self->video_codec_context) && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD) {
self->performance_error_shown = true;
fprintf(stderr,"gsr warning: gsr_capture_kms_capture: the monitor you are recording is rotated, composition will have to be used."
" If you are experience performance problems in the video then record a single window on X11 or use portal capture option instead\n");
self->fast_path_failed = true;
fprintf(stderr, "gsr warning: gsr_capture_kms_capture: the monitor you are recording is rotated, composition will have to be used."
" If you experience performance problems in the video then record a single window on X11 or use portal capture option instead\n");
}
gsr_capture_kms_fail_fast_path_if_not_fast(self, drm_fd->pixel_format);
self->capture_size = rotate_capture_size_if_rotated(self, (vec2i){ drm_fd->src_w, drm_fd->src_h });
const bool is_scaled = self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0;

View File

@@ -3,12 +3,14 @@
#include "../../include/egl.h"
#include "../../include/utils.h"
#include "../../include/color_conversion.h"
#include "../../include/window/window.h"
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <X11/Xlib.h>
#include <libavcodec/avcodec.h>
@@ -136,7 +138,10 @@ static void set_vertical_sync_enabled(gsr_egl *egl, int enabled) {
int result = 0;
if(egl->glXSwapIntervalEXT) {
egl->glXSwapIntervalEXT(egl->x11.dpy, egl->x11.window, enabled ? 1 : 0);
assert(gsr_window_get_display_server(egl->window) == GSR_DISPLAY_SERVER_X11);
Display *display = gsr_window_get_display(egl->window);
const Window window = (Window)gsr_window_get_window(egl->window);
egl->glXSwapIntervalEXT(display, window, enabled ? 1 : 0);
} else if(egl->glXSwapIntervalMESA) {
result = egl->glXSwapIntervalMESA(enabled ? 1 : 0);
} else if(egl->glXSwapIntervalSGI) {
@@ -219,8 +224,11 @@ static int gsr_capture_nvfbc_setup_handle(gsr_capture_nvfbc *self) {
goto error_cleanup;
}
self->tracking_width = XWidthOfScreen(DefaultScreenOfDisplay(self->params.egl->x11.dpy));
self->tracking_height = XHeightOfScreen(DefaultScreenOfDisplay(self->params.egl->x11.dpy));
assert(gsr_window_get_display_server(self->params.egl->window) == GSR_DISPLAY_SERVER_X11);
Display *display = gsr_window_get_display(self->params.egl->window);
self->tracking_width = XWidthOfScreen(DefaultScreenOfDisplay(display));
self->tracking_height = XHeightOfScreen(DefaultScreenOfDisplay(display));
self->tracking_type = strcmp(self->params.display_to_capture, "screen") == 0 ? NVFBC_TRACKING_SCREEN : NVFBC_TRACKING_OUTPUT;
if(self->tracking_type == NVFBC_TRACKING_OUTPUT) {
if(!status_params.bXRandRAvailable) {

View File

@@ -27,6 +27,7 @@ typedef struct {
AVCodecContext *video_codec_context;
bool fast_path_failed;
bool mesa_supports_compute_only_vaapi_copy;
} gsr_capture_portal;
static void gsr_capture_portal_cleanup_plane_fds(gsr_capture_portal *self) {
@@ -310,10 +311,12 @@ static int gsr_capture_portal_start(gsr_capture *cap, AVCodecContext *video_code
video_codec_context->height = FFALIGN(self->params.output_resolution.y, 2);
}
self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(self->params.egl, 24, 0, 9);
self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(&self->params.egl->gpu_info, 24, 0, 9);
if(self->fast_path_failed)
fprintf(stderr, "gsr warning: gsr_capture_kms_start: your amd driver (mesa) version is known to be buggy (<= version 24.0.9), falling back to opengl copy\n");
self->mesa_supports_compute_only_vaapi_copy = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && gl_driver_version_greater_than(&self->params.egl->gpu_info, 24, 3, 6);
frame->width = video_codec_context->width;
frame->height = video_codec_context->height;
@@ -325,6 +328,16 @@ static int max_int(int a, int b) {
return a > b ? a : b;
}
static void gsr_capture_portal_fail_fast_path_if_not_fast(gsr_capture_portal *self, uint32_t pixel_format) {
const uint8_t pixel_format_color_depth_1 = (pixel_format >> 16) & 0xFF;
if(!self->fast_path_failed && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !self->mesa_supports_compute_only_vaapi_copy && (pixel_format_color_depth_1 == '3' || pixel_format_color_depth_1 == '4')) {
self->fast_path_failed = true;
fprintf(stderr, "gsr warning: gsr_capture_kms_capture: the monitor you are recording is in 10/12-bit color format and your mesa version is <= 24.3.6, composition will be used."
" If you experience performance problems in the video then record on a single window on X11 instead or disable 10/12-bit color option in your desktop environment settings,"
" or try to record the monitor on X11 instead (if you aren't already doing that) or update your mesa version.\n");
}
}
static int gsr_capture_portal_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) {
(void)frame;
(void)color_conversion;
@@ -346,6 +359,8 @@ static int gsr_capture_portal_capture(gsr_capture *cap, AVFrame *frame, gsr_colo
return 0;
}
gsr_capture_portal_fail_fast_path_if_not_fast(self, pipewire_fourcc);
const bool is_scaled = self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0;
vec2i output_size = is_scaled ? self->params.output_resolution : self->capture_size;
output_size = scale_keep_aspect_ratio(self->capture_size, output_size);

View File

@@ -3,6 +3,7 @@
#include "../../include/utils.h"
#include "../../include/cursor.h"
#include "../../include/color_conversion.h"
#include "../../include/window/window.h"
#include <stdlib.h>
#include <stdio.h>
@@ -16,6 +17,7 @@
typedef struct {
gsr_capture_xcomposite_params params;
Display *display;
bool should_stop;
bool stop_is_error;
@@ -66,12 +68,12 @@ static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_
gsr_capture_xcomposite *self = cap->priv;
if(self->params.follow_focused) {
self->net_active_window_atom = XInternAtom(self->params.egl->x11.dpy, "_NET_ACTIVE_WINDOW", False);
self->net_active_window_atom = XInternAtom(self->display, "_NET_ACTIVE_WINDOW", False);
if(!self->net_active_window_atom) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start failed: failed to get _NET_ACTIVE_WINDOW atom\n");
return -1;
}
self->window = get_focused_window(self->params.egl->x11.dpy, self->net_active_window_atom);
self->window = get_focused_window(self->display, self->net_active_window_atom);
} else {
self->window = self->params.window;
}
@@ -79,7 +81,7 @@ static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_
/* TODO: Do these in tick, and allow error if follow_focused */
XWindowAttributes attr;
if(!XGetWindowAttributes(self->params.egl->x11.dpy, self->window, &attr) && !self->params.follow_focused) {
if(!XGetWindowAttributes(self->display, self->window, &attr) && !self->params.follow_focused) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start failed: invalid window id: %lu\n", self->window);
return -1;
}
@@ -88,19 +90,19 @@ static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_
self->window_size.y = max_int(attr.height, 0);
if(self->params.follow_focused)
XSelectInput(self->params.egl->x11.dpy, DefaultRootWindow(self->params.egl->x11.dpy), PropertyChangeMask);
XSelectInput(self->display, DefaultRootWindow(self->display), PropertyChangeMask);
// TODO: Get select and add these on top of it and then restore at the end. Also do the same in other xcomposite
XSelectInput(self->params.egl->x11.dpy, self->window, StructureNotifyMask | ExposureMask);
XSelectInput(self->display, self->window, StructureNotifyMask | ExposureMask);
/* Disable vsync */
self->params.egl->eglSwapInterval(self->params.egl->egl_display, 0);
if(window_texture_init(&self->window_texture, self->params.egl->x11.dpy, self->window, self->params.egl) != 0 && !self->params.follow_focused) {
if(window_texture_init(&self->window_texture, self->display, self->window, self->params.egl) != 0 && !self->params.follow_focused) {
fprintf(stderr, "gsr error: gsr_capture_xcomposite_start: failed to get window texture for window %ld\n", self->window);
return -1;
}
if(gsr_cursor_init(&self->cursor, self->params.egl, self->params.egl->x11.dpy) != 0) {
if(gsr_cursor_init(&self->cursor, self->params.egl, self->display) != 0) {
gsr_capture_xcomposite_stop(self);
return -1;
}
@@ -122,7 +124,7 @@ static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_
video_codec_context->height = FFALIGN(self->params.output_resolution.y, 2);
}
self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(self->params.egl, 24, 0, 9);
self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(&self->params.egl->gpu_info, 24, 0, 9);
if(self->fast_path_failed)
fprintf(stderr, "gsr warning: gsr_capture_kms_start: your amd driver (mesa) version is known to be buggy (<= version 24.0.9), falling back to opengl copy\n");
@@ -143,24 +145,24 @@ static void gsr_capture_xcomposite_tick(gsr_capture *cap) {
if(self->init_new_window) {
self->init_new_window = false;
Window focused_window = get_focused_window(self->params.egl->x11.dpy, self->net_active_window_atom);
Window focused_window = get_focused_window(self->display, self->net_active_window_atom);
if(focused_window != self->window || !self->follow_focused_initialized) {
self->follow_focused_initialized = true;
XSelectInput(self->params.egl->x11.dpy, self->window, 0);
XSelectInput(self->display, self->window, 0);
self->window = focused_window;
XSelectInput(self->params.egl->x11.dpy, self->window, StructureNotifyMask | ExposureMask);
XSelectInput(self->display, self->window, StructureNotifyMask | ExposureMask);
XWindowAttributes attr;
attr.width = 0;
attr.height = 0;
if(!XGetWindowAttributes(self->params.egl->x11.dpy, self->window, &attr))
if(!XGetWindowAttributes(self->display, self->window, &attr))
fprintf(stderr, "gsr error: gsr_capture_xcomposite_tick failed: invalid window id: %lu\n", self->window);
self->window_size.x = max_int(attr.width, 0);
self->window_size.y = max_int(attr.height, 0);
window_texture_deinit(&self->window_texture);
window_texture_init(&self->window_texture, self->params.egl->x11.dpy, self->window, self->params.egl); // TODO: Do not do the below window_texture_on_resize after this
window_texture_init(&self->window_texture, self->display, self->window, self->params.egl); // TODO: Do not do the below window_texture_on_resize after this
self->texture_size.x = 0;
self->texture_size.y = 0;
@@ -200,7 +202,7 @@ static void gsr_capture_xcomposite_tick(gsr_capture *cap) {
static void gsr_capture_xcomposite_on_event(gsr_capture *cap, gsr_egl *egl) {
gsr_capture_xcomposite *self = cap->priv;
XEvent *xev = gsr_egl_get_event_data(egl);
XEvent *xev = gsr_window_get_event_data(egl->window);
switch(xev->type) {
case DestroyNotify: {
/* Window died (when not following focused window), so we stop recording */
@@ -350,6 +352,7 @@ gsr_capture* gsr_capture_xcomposite_create(const gsr_capture_xcomposite_params *
}
cap_xcomp->params = *params;
cap_xcomp->display = gsr_window_get_display(params->egl->window);
*cap = (gsr_capture) {
.start = gsr_capture_xcomposite_start,

View File

@@ -1,5 +1,6 @@
#include "../include/damage.h"
#include "../include/utils.h"
#include "../include/window/window.h"
#include <stdio.h>
#include <string.h>
@@ -30,33 +31,34 @@ bool gsr_damage_init(gsr_damage *self, gsr_egl *egl, bool track_cursor) {
self->egl = egl;
self->track_cursor = track_cursor;
if(gsr_egl_get_display_server(egl) != GSR_DISPLAY_SERVER_X11) {
if(gsr_window_get_display_server(egl->window) != GSR_DISPLAY_SERVER_X11) {
fprintf(stderr, "gsr warning: gsr_damage_init: damage tracking is not supported on wayland\n");
return false;
}
self->display = gsr_window_get_display(egl->window);
if(!XDamageQueryExtension(self->egl->x11.dpy, &self->damage_event, &self->damage_error)) {
if(!XDamageQueryExtension(self->display, &self->damage_event, &self->damage_error)) {
fprintf(stderr, "gsr warning: gsr_damage_init: XDamage is not supported by your X11 server\n");
gsr_damage_deinit(self);
return false;
}
if(!XRRQueryExtension(self->egl->x11.dpy, &self->randr_event, &self->randr_error)) {
if(!XRRQueryExtension(self->display, &self->randr_event, &self->randr_error)) {
fprintf(stderr, "gsr warning: gsr_damage_init: XRandr is not supported by your X11 server\n");
gsr_damage_deinit(self);
return false;
}
if(!xrandr_is_supported(self->egl->x11.dpy)) {
if(!xrandr_is_supported(self->display)) {
fprintf(stderr, "gsr warning: gsr_damage_init: your X11 randr version is too old\n");
gsr_damage_deinit(self);
return false;
}
if(self->track_cursor)
self->track_cursor = gsr_cursor_init(&self->cursor, self->egl, self->egl->x11.dpy) == 0;
self->track_cursor = gsr_cursor_init(&self->cursor, self->egl, self->display) == 0;
XRRSelectInput(self->egl->x11.dpy, DefaultRootWindow(self->egl->x11.dpy), RRScreenChangeNotifyMask | RRCrtcChangeNotifyMask | RROutputChangeNotifyMask);
XRRSelectInput(self->display, DefaultRootWindow(self->display), RRScreenChangeNotifyMask | RRCrtcChangeNotifyMask | RROutputChangeNotifyMask);
self->damaged = true;
return true;
@@ -64,7 +66,7 @@ bool gsr_damage_init(gsr_damage *self, gsr_egl *egl, bool track_cursor) {
void gsr_damage_deinit(gsr_damage *self) {
if(self->damage) {
XDamageDestroy(self->egl->x11.dpy, self->damage);
XDamageDestroy(self->display, self->damage);
self->damage = None;
}
@@ -85,22 +87,22 @@ bool gsr_damage_set_target_window(gsr_damage *self, uint64_t window) {
return true;
if(self->damage) {
XDamageDestroy(self->egl->x11.dpy, self->damage);
XDamageDestroy(self->display, self->damage);
self->damage = None;
}
if(self->window)
XSelectInput(self->egl->x11.dpy, self->window, 0);
XSelectInput(self->display, self->window, 0);
self->window = window;
XSelectInput(self->egl->x11.dpy, self->window, StructureNotifyMask | ExposureMask);
XSelectInput(self->display, self->window, StructureNotifyMask | ExposureMask);
XWindowAttributes win_attr;
win_attr.x = 0;
win_attr.y = 0;
win_attr.width = 0;
win_attr.height = 0;
if(!XGetWindowAttributes(self->egl->x11.dpy, self->window, &win_attr))
if(!XGetWindowAttributes(self->display, self->window, &win_attr))
fprintf(stderr, "gsr warning: gsr_damage_set_target_window failed: failed to get window attributes: %ld\n", (long)self->window);
//self->window_pos.x = win_attr.x;
@@ -109,9 +111,9 @@ bool gsr_damage_set_target_window(gsr_damage *self, uint64_t window) {
self->window_size.x = win_attr.width;
self->window_size.y = win_attr.height;
self->damage = XDamageCreate(self->egl->x11.dpy, window, XDamageReportNonEmpty);
self->damage = XDamageCreate(self->display, window, XDamageReportNonEmpty);
if(self->damage) {
XDamageSubtract(self->egl->x11.dpy, self->damage, None, None);
XDamageSubtract(self->display, self->damage, None, None);
self->damaged = true;
self->track_type = GSR_DAMAGE_TRACK_WINDOW;
return true;
@@ -130,23 +132,23 @@ bool gsr_damage_set_target_monitor(gsr_damage *self, const char *monitor_name) {
return true;
if(self->damage) {
XDamageDestroy(self->egl->x11.dpy, self->damage);
XDamageDestroy(self->display, self->damage);
self->damage = None;
}
memset(&self->monitor, 0, sizeof(self->monitor));
if(strcmp(monitor_name, "screen") != 0 && strcmp(monitor_name, "screen-direct") != 0 && strcmp(monitor_name, "screen-direct-force") != 0) {
if(strcmp(monitor_name, "screen-direct") != 0 && strcmp(monitor_name, "screen-direct-force") != 0) {
if(!get_monitor_by_name(self->egl, GSR_CONNECTION_X11, monitor_name, &self->monitor))
fprintf(stderr, "gsr warning: gsr_damage_set_target_monitor: failed to find monitor: %s\n", monitor_name);
}
if(self->window)
XSelectInput(self->egl->x11.dpy, self->window, 0);
XSelectInput(self->display, self->window, 0);
self->window = DefaultRootWindow(self->egl->x11.dpy);
self->damage = XDamageCreate(self->egl->x11.dpy, self->window, XDamageReportNonEmpty);
self->window = DefaultRootWindow(self->display);
self->damage = XDamageCreate(self->display, self->window, XDamageReportNonEmpty);
if(self->damage) {
XDamageSubtract(self->egl->x11.dpy, self->damage, None, None);
XDamageSubtract(self->display, self->damage, None, None);
self->damaged = true;
snprintf(self->monitor_name, sizeof(self->monitor_name), "%s", monitor_name);
self->track_type = GSR_DAMAGE_TRACK_MONITOR;
@@ -184,14 +186,14 @@ static void gsr_damage_on_output_change(gsr_damage *self, XEvent *xev) {
if(!rr_output_change_event->output || self->monitor.monitor_identifier == 0)
return;
XRRScreenResources *screen_res = XRRGetScreenResources(self->egl->x11.dpy, DefaultRootWindow(self->egl->x11.dpy));
XRRScreenResources *screen_res = XRRGetScreenResources(self->display, DefaultRootWindow(self->display));
if(!screen_res)
return;
// TODO: What about scaled output? look at for_each_active_monitor_output_x11_not_cached
XRROutputInfo *out_info = XRRGetOutputInfo(self->egl->x11.dpy, screen_res, rr_output_change_event->output);
XRROutputInfo *out_info = XRRGetOutputInfo(self->display, screen_res, rr_output_change_event->output);
if(out_info && out_info->crtc && out_info->crtc == self->monitor.monitor_identifier) {
XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(self->egl->x11.dpy, screen_res, out_info->crtc);
XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(self->display, screen_res, out_info->crtc);
if(crtc_info && (crtc_info->x != self->monitor.pos.x || crtc_info->y != self->monitor.pos.y ||
(int)crtc_info->width != self->monitor.size.x || (int)crtc_info->height != self->monitor.size.y))
{
@@ -226,15 +228,15 @@ static void gsr_damage_on_randr_event(gsr_damage *self, XEvent *xev) {
static void gsr_damage_on_damage_event(gsr_damage *self, XEvent *xev) {
const XDamageNotifyEvent *de = (XDamageNotifyEvent*)xev;
XserverRegion region = XFixesCreateRegion(self->egl->x11.dpy, NULL, 0);
XserverRegion region = XFixesCreateRegion(self->display, NULL, 0);
/* Subtract all the damage, repairing the window */
XDamageSubtract(self->egl->x11.dpy, de->damage, None, region);
XDamageSubtract(self->display, de->damage, None, region);
if(self->track_type == GSR_DAMAGE_TRACK_WINDOW || (self->track_type == GSR_DAMAGE_TRACK_MONITOR && self->monitor.connector_id == 0)) {
self->damaged = true;
} else {
int num_rectangles = 0;
XRectangle *rectangles = XFixesFetchRegion(self->egl->x11.dpy, region, &num_rectangles);
XRectangle *rectangles = XFixesFetchRegion(self->display, region, &num_rectangles);
if(rectangles) {
const gsr_rectangle monitor_region = { self->monitor.pos, self->monitor.size };
for(int i = 0; i < num_rectangles; ++i) {
@@ -247,8 +249,8 @@ static void gsr_damage_on_damage_event(gsr_damage *self, XEvent *xev) {
}
}
XFixesDestroyRegion(self->egl->x11.dpy, region);
XFlush(self->egl->x11.dpy);
XFixesDestroyRegion(self->display, region);
XFlush(self->display);
}
static void gsr_damage_on_tick_cursor(gsr_damage *self) {

425
src/egl.c
View File

@@ -1,4 +1,5 @@
#include "../include/egl.h"
#include "../include/window/window.h"
#include "../include/library_loader.h"
#include "../include/utils.h"
@@ -10,118 +11,28 @@
#include <unistd.h>
#include <sys/capability.h>
#include <wayland-client.h>
#include <wayland-egl.h>
// TODO: rename gsr_egl to something else since this includes both egl and glx and in the future maybe vulkan too
// TODO: Move this shit to a separate wayland file, and have a separate file for x11.
#define GLX_DRAWABLE_TYPE 0x8010
#define GLX_RENDER_TYPE 0x8011
#define GLX_RGBA_BIT 0x00000001
#define GLX_WINDOW_BIT 0x00000001
#define GLX_PIXMAP_BIT 0x00000002
#define GLX_BIND_TO_TEXTURE_RGBA_EXT 0x20D1
#define GLX_BIND_TO_TEXTURE_TARGETS_EXT 0x20D3
#define GLX_TEXTURE_2D_BIT_EXT 0x00000002
#define GLX_DOUBLEBUFFER 5
#define GLX_RED_SIZE 8
#define GLX_GREEN_SIZE 9
#define GLX_BLUE_SIZE 10
#define GLX_ALPHA_SIZE 11
#define GLX_DEPTH_SIZE 12
#define GLX_RGBA_TYPE 0x8014
static void output_handle_geometry(void *data, struct wl_output *wl_output,
int32_t x, int32_t y, int32_t phys_width, int32_t phys_height,
int32_t subpixel, const char *make, const char *model,
int32_t transform) {
(void)wl_output;
(void)phys_width;
(void)phys_height;
(void)subpixel;
(void)make;
(void)model;
gsr_wayland_output *gsr_output = data;
gsr_output->pos.x = x;
gsr_output->pos.y = y;
gsr_output->transform = transform;
}
static void output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
(void)wl_output;
(void)flags;
(void)refresh;
gsr_wayland_output *gsr_output = data;
gsr_output->size.x = width;
gsr_output->size.y = height;
}
static void output_handle_done(void *data, struct wl_output *wl_output) {
(void)data;
(void)wl_output;
}
static void output_handle_scale(void* data, struct wl_output *wl_output, int32_t factor) {
(void)data;
(void)wl_output;
(void)factor;
}
static void output_handle_name(void *data, struct wl_output *wl_output, const char *name) {
(void)wl_output;
gsr_wayland_output *gsr_output = data;
if(gsr_output->name) {
free(gsr_output->name);
gsr_output->name = NULL;
}
gsr_output->name = strdup(name);
}
static void output_handle_description(void *data, struct wl_output *wl_output, const char *description) {
(void)data;
(void)wl_output;
(void)description;
}
static const struct wl_output_listener output_listener = {
.geometry = output_handle_geometry,
.mode = output_handle_mode,
.done = output_handle_done,
.scale = output_handle_scale,
.name = output_handle_name,
.description = output_handle_description,
};
static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
(void)version;
gsr_egl *egl = data;
if (strcmp(interface, "wl_compositor") == 0) {
if(egl->wayland.compositor) {
wl_compositor_destroy(egl->wayland.compositor);
egl->wayland.compositor = NULL;
}
egl->wayland.compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
} else if(strcmp(interface, wl_output_interface.name) == 0) {
if(version < 4) {
fprintf(stderr, "gsr warning: wl output interface version is < 4, expected >= 4 to capture a monitor. Using KMS capture instead\n");
return;
}
if(egl->wayland.num_outputs == GSR_MAX_OUTPUTS) {
fprintf(stderr, "gsr warning: reached maximum outputs (%d), ignoring output %u\n", GSR_MAX_OUTPUTS, name);
return;
}
gsr_wayland_output *gsr_output = &egl->wayland.outputs[egl->wayland.num_outputs];
egl->wayland.num_outputs++;
*gsr_output = (gsr_wayland_output) {
.wl_name = name,
.output = wl_registry_bind(registry, name, &wl_output_interface, 4),
.pos = { .x = 0, .y = 0 },
.size = { .x = 0, .y = 0 },
.transform = 0,
.name = NULL,
};
wl_output_add_listener(gsr_output->output, &output_listener, gsr_output);
}
}
static void registry_remove_object(void *data, struct wl_registry *registry, uint32_t name) {
(void)data;
(void)registry;
(void)name;
}
static struct wl_registry_listener registry_listener = {
.global = registry_add_object,
.global_remove = registry_remove_object,
};
#define GLX_CONTEXT_PRIORITY_LEVEL_EXT 0x3100
#define GLX_CONTEXT_PRIORITY_HIGH_EXT 0x3101
#define GLX_CONTEXT_PRIORITY_MEDIUM_EXT 0x3102
#define GLX_CONTEXT_PRIORITY_LOW_EXT 0x3103
static void reset_cap_nice(void) {
cap_t caps = cap_get_proc();
@@ -135,75 +46,8 @@ static void reset_cap_nice(void) {
cap_free(caps);
}
static void store_x11_monitor(const gsr_monitor *monitor, void *userdata) {
gsr_egl *egl = userdata;
if(egl->x11.num_outputs == GSR_MAX_OUTPUTS) {
fprintf(stderr, "gsr warning: reached maximum outputs (%d), ignoring output %s\n", GSR_MAX_OUTPUTS, monitor->name);
return;
}
char *monitor_name = strdup(monitor->name);
if(!monitor_name)
return;
const int index = egl->x11.num_outputs;
egl->x11.outputs[index].name = monitor_name;
egl->x11.outputs[index].pos = monitor->pos;
egl->x11.outputs[index].size = monitor->size;
egl->x11.outputs[index].connector_id = monitor->connector_id;
egl->x11.outputs[index].rotation = monitor->rotation;
egl->x11.outputs[index].monitor_identifier = monitor->monitor_identifier;
++egl->x11.num_outputs;
}
#define GLX_DRAWABLE_TYPE 0x8010
#define GLX_RENDER_TYPE 0x8011
#define GLX_RGBA_BIT 0x00000001
#define GLX_WINDOW_BIT 0x00000001
#define GLX_PIXMAP_BIT 0x00000002
#define GLX_BIND_TO_TEXTURE_RGBA_EXT 0x20D1
#define GLX_BIND_TO_TEXTURE_TARGETS_EXT 0x20D3
#define GLX_TEXTURE_2D_BIT_EXT 0x00000002
#define GLX_DOUBLEBUFFER 5
#define GLX_RED_SIZE 8
#define GLX_GREEN_SIZE 9
#define GLX_BLUE_SIZE 10
#define GLX_ALPHA_SIZE 11
#define GLX_DEPTH_SIZE 12
#define GLX_RGBA_TYPE 0x8014
#define GLX_CONTEXT_PRIORITY_LEVEL_EXT 0x3100
#define GLX_CONTEXT_PRIORITY_HIGH_EXT 0x3101
#define GLX_CONTEXT_PRIORITY_MEDIUM_EXT 0x3102
#define GLX_CONTEXT_PRIORITY_LOW_EXT 0x3103
static GLXFBConfig glx_fb_config_choose(gsr_egl *self) {
const int glx_visual_attribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
// TODO:
//GLX_BIND_TO_TEXTURE_RGBA_EXT, 1,
//GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
GLX_DOUBLEBUFFER, True,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 0,
GLX_DEPTH_SIZE, 0,
None, None
};
// TODO: Cleanup
int c = 0;
GLXFBConfig *fb_configs = self->glXChooseFBConfig(self->x11.dpy, DefaultScreen(self->x11.dpy), glx_visual_attribs, &c);
if(c == 0 || !fb_configs)
return NULL;
return fb_configs[0];
}
// TODO: Create egl context without surface (in other words, x11/wayland agnostic, doesn't require x11/wayland dependency)
static bool gsr_egl_create_window(gsr_egl *self, bool wayland) {
static bool gsr_egl_create_window(gsr_egl *self) {
EGLConfig ecfg;
int32_t num_config = 0;
@@ -216,43 +60,14 @@ static bool gsr_egl_create_window(gsr_egl *self, bool wayland) {
const int32_t ctxattr[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG, /* requires cap_sys_nice, ignored otherwise */
//EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG, /* requires cap_sys_nice, ignored otherwise */
EGL_NONE, EGL_NONE
};
if(wayland) {
self->wayland.dpy = wl_display_connect(NULL);
if(!self->wayland.dpy) {
fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to connect to the Wayland server\n");
goto fail;
}
self->wayland.registry = wl_display_get_registry(self->wayland.dpy); // TODO: Error checking
wl_registry_add_listener(self->wayland.registry, &registry_listener, self); // TODO: Error checking
// Fetch globals
wl_display_roundtrip(self->wayland.dpy);
// Fetch wl_output
wl_display_roundtrip(self->wayland.dpy);
if(!self->wayland.compositor) {
fprintf(stderr, "gsr error: gsr_gl_create_window failed: failed to find compositor\n");
goto fail;
}
} else {
self->x11.window = XCreateWindow(self->x11.dpy, DefaultRootWindow(self->x11.dpy), 0, 0, 16, 16, 0, CopyFromParent, InputOutput, CopyFromParent, 0, NULL);
if(!self->x11.window) {
fprintf(stderr, "gsr error: gsr_gl_create_window failed: failed to create gl window\n");
goto fail;
}
}
// TODO: Use EGL_OPENGL_ES_API as amd requires that for external texture, but that breaks software encoding
self->eglBindAPI(EGL_OPENGL_API);
self->egl_display = self->eglGetDisplay(self->wayland.dpy ? (EGLNativeDisplayType)self->wayland.dpy : (EGLNativeDisplayType)self->x11.dpy);
self->egl_display = self->eglGetDisplay((EGLNativeDisplayType)gsr_window_get_display(self->window));
if(!self->egl_display) {
fprintf(stderr, "gsr error: gsr_egl_create_window failed: eglGetDisplay failed\n");
goto fail;
@@ -274,15 +89,7 @@ static bool gsr_egl_create_window(gsr_egl *self, bool wayland) {
goto fail;
}
if(wayland) {
// TODO: Error check?
self->wayland.surface = wl_compositor_create_surface(self->wayland.compositor);
self->wayland.window = wl_egl_window_create(self->wayland.surface, 16, 16);
self->egl_surface = self->eglCreateWindowSurface(self->egl_display, ecfg, (EGLNativeWindowType)self->wayland.window, NULL);
} else {
self->egl_surface = self->eglCreateWindowSurface(self->egl_display, ecfg, (EGLNativeWindowType)self->x11.window, NULL);
}
self->egl_surface = self->eglCreateWindowSurface(self->egl_display, ecfg, (EGLNativeWindowType)gsr_window_get_window(self->window), NULL);
if(!self->egl_surface) {
fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to create window surface\n");
goto fail;
@@ -293,11 +100,6 @@ static bool gsr_egl_create_window(gsr_egl *self, bool wayland) {
goto fail;
}
if(!wayland) {
self->x11.num_outputs = 0;
for_each_active_monitor_output_x11_not_cached(self->x11.dpy, store_x11_monitor, self);
}
reset_cap_nice();
return true;
@@ -307,8 +109,36 @@ static bool gsr_egl_create_window(gsr_egl *self, bool wayland) {
return false;
}
static GLXFBConfig glx_fb_config_choose(gsr_egl *self, Display *display) {
const int glx_visual_attribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
// TODO:
//GLX_BIND_TO_TEXTURE_RGBA_EXT, 1,
//GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
GLX_DOUBLEBUFFER, True,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 0,
GLX_DEPTH_SIZE, 0,
None, None
};
// TODO: Cleanup
int c = 0;
GLXFBConfig *fb_configs = self->glXChooseFBConfig(display, DefaultScreen(display), glx_visual_attribs, &c);
if(c == 0 || !fb_configs)
return NULL;
return fb_configs[0];
}
static bool gsr_egl_switch_to_glx_context(gsr_egl *self) {
// TODO: Cleanup
assert(gsr_window_get_display_server(self->window) == GSR_DISPLAY_SERVER_X11);
Display *display = gsr_window_get_display(self->window);
const Window window = (Window)gsr_window_get_window(self->window);
if(self->egl_context) {
self->eglMakeCurrent(self->egl_display, NULL, NULL, NULL);
@@ -326,21 +156,21 @@ static bool gsr_egl_switch_to_glx_context(gsr_egl *self) {
self->egl_display = NULL;
}
self->glx_fb_config = glx_fb_config_choose(self);
self->glx_fb_config = glx_fb_config_choose(self, display);
if(!self->glx_fb_config) {
fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to find a suitable fb config\n");
goto fail;
}
// TODO:
//self->glx_context = self->glXCreateContextAttribsARB(self->x11.dpy, self->glx_fb_config, NULL, True, context_attrib_list);
self->glx_context = self->glXCreateNewContext(self->x11.dpy, self->glx_fb_config, GLX_RGBA_TYPE, NULL, True);
//self->glx_context = self->glXCreateContextAttribsARB(display, self->glx_fb_config, NULL, True, context_attrib_list);
self->glx_context = self->glXCreateNewContext(display, self->glx_fb_config, GLX_RGBA_TYPE, NULL, True);
if(!self->glx_context) {
fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to create glx context\n");
goto fail;
}
if(!self->glXMakeContextCurrent(self->x11.dpy, self->x11.window, self->x11.window, self->glx_context)) {
if(!self->glXMakeContextCurrent(display, window, window, self->glx_context)) {
fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to make glx context current\n");
goto fail;
}
@@ -349,8 +179,8 @@ static bool gsr_egl_switch_to_glx_context(gsr_egl *self) {
fail:
if(self->glx_context) {
self->glXMakeContextCurrent(self->x11.dpy, None, None, NULL);
self->glXDestroyContext(self->x11.dpy, self->glx_context);
self->glXMakeContextCurrent(display, None, None, NULL);
self->glXDestroyContext(display, self->glx_context);
self->glx_context = NULL;
self->glx_fb_config = NULL;
}
@@ -514,28 +344,21 @@ static bool gsr_egl_load_gl(gsr_egl *self, void *library) {
return true;
}
// #define GL_DEBUG_TYPE_ERROR 0x824C
// static void debug_callback( unsigned int source,
// unsigned int type,
// unsigned int id,
// unsigned int severity,
// int length,
// const char* message,
// const void* userParam )
// {
// (void)source;
// (void)id;
// (void)length;
// (void)userParam;
// fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
// ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
// type, severity, message );
// }
#define GL_DEBUG_TYPE_ERROR 0x824C
#define GL_DEBUG_SEVERITY_NOTIFICATION 0x826B
static void debug_callback(unsigned int source, unsigned int type, unsigned int id, unsigned int severity, int length, const char* message, const void* userParam) {
(void)source;
(void)id;
(void)length;
(void)userParam;
if(severity != GL_DEBUG_SEVERITY_NOTIFICATION)
fprintf(stderr, "gsr info: gl callback: %s type = 0x%x, severity = 0x%x, message = %s\n", type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "", type, severity, message);
}
bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland, bool is_monitor_capture) {
bool gsr_egl_load(gsr_egl *self, gsr_window *window, bool is_monitor_capture, bool enable_debug) {
memset(self, 0, sizeof(gsr_egl));
self->x11.dpy = dpy;
self->context_type = GSR_GL_CONTEXT_TYPE_EGL;
self->window = window;
dlerror(); /* clear */
self->egl_library = dlopen("libEGL.so.1", RTLD_LAZY);
@@ -547,7 +370,7 @@ bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland, bool is_monitor_cap
self->glx_library = dlopen("libGLX.so.0", RTLD_LAZY);
self->gl_library = dlopen("libGL.so.1", RTLD_LAZY);
if(!self->egl_library) {
if(!self->gl_library) {
fprintf(stderr, "gsr error: gsr_egl_load: failed to load libGL.so.1, error: %s\n", dlerror());
goto fail;
}
@@ -565,7 +388,7 @@ bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland, bool is_monitor_cap
if(!gsr_egl_proc_load_egl(self))
goto fail;
if(!gsr_egl_create_window(self, wayland))
if(!gsr_egl_create_window(self))
goto fail;
if(!gl_get_gpu_info(self, &self->gpu_info))
@@ -578,7 +401,7 @@ bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland, bool is_monitor_cap
}
/* Nvfbc requires glx */
if(!wayland && is_monitor_capture && self->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA) {
if(gsr_window_get_display_server(self->window) == GSR_DISPLAY_SERVER_X11 && is_monitor_capture && self->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA) {
self->context_type = GSR_GL_CONTEXT_TYPE_GLX;
self->dri_card_path = NULL;
if(!gsr_egl_switch_to_glx_context(self))
@@ -588,8 +411,10 @@ bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland, bool is_monitor_cap
self->glEnable(GL_BLEND);
self->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//self->glEnable(GL_DEBUG_OUTPUT);
//self->glDebugMessageCallback(debug_callback, NULL);
if(enable_debug) {
self->glEnable(GL_DEBUG_OUTPUT);
self->glDebugMessageCallback(debug_callback, NULL);
}
return true;
@@ -616,63 +441,14 @@ void gsr_egl_unload(gsr_egl *self) {
}
if(self->glx_context) {
self->glXMakeContextCurrent(self->x11.dpy, None, None, NULL);
self->glXDestroyContext(self->x11.dpy, self->glx_context);
assert(gsr_window_get_display_server(self->window) == GSR_DISPLAY_SERVER_X11);
Display *display = gsr_window_get_display(self->window);
self->glXMakeContextCurrent(display, None, None, NULL);
self->glXDestroyContext(display, self->glx_context);
self->glx_context = NULL;
self->glx_fb_config = NULL;
}
if(self->x11.window) {
XDestroyWindow(self->x11.dpy, self->x11.window);
self->x11.window = None;
}
for(int i = 0; i < self->x11.num_outputs; ++i) {
if(self->x11.outputs[i].name) {
free(self->x11.outputs[i].name);
self->x11.outputs[i].name = NULL;
}
}
self->x11.num_outputs = 0;
if(self->wayland.window) {
wl_egl_window_destroy(self->wayland.window);
self->wayland.window = NULL;
}
if(self->wayland.surface) {
wl_surface_destroy(self->wayland.surface);
self->wayland.surface = NULL;
}
for(int i = 0; i < self->wayland.num_outputs; ++i) {
if(self->wayland.outputs[i].output) {
wl_output_destroy(self->wayland.outputs[i].output);
self->wayland.outputs[i].output = NULL;
}
if(self->wayland.outputs[i].name) {
free(self->wayland.outputs[i].name);
self->wayland.outputs[i].name = NULL;
}
}
self->wayland.num_outputs = 0;
if(self->wayland.compositor) {
wl_compositor_destroy(self->wayland.compositor);
self->wayland.compositor = NULL;
}
if(self->wayland.registry) {
wl_registry_destroy(self->wayland.registry);
self->wayland.registry = NULL;
}
if(self->wayland.dpy) {
wl_display_disconnect(self->wayland.dpy);
self->wayland.dpy = NULL;
}
if(self->egl_library) {
dlclose(self->egl_library);
self->egl_library = NULL;
@@ -691,25 +467,6 @@ void gsr_egl_unload(gsr_egl *self) {
memset(self, 0, sizeof(gsr_egl));
}
bool gsr_egl_process_event(gsr_egl *self) {
switch(gsr_egl_get_display_server(self)) {
case GSR_DISPLAY_SERVER_X11: {
if(XPending(self->x11.dpy)) {
XNextEvent(self->x11.dpy, &self->x11.xev);
return true;
}
return false;
}
case GSR_DISPLAY_SERVER_WAYLAND: {
// TODO: pselect on wl_display_get_fd before doing dispatch
const bool events_available = wl_display_dispatch_pending(self->wayland.dpy) > 0;
wl_display_flush(self->wayland.dpy);
return events_available;
}
}
return false;
}
void gsr_egl_swap_buffers(gsr_egl *self) {
/* This uses less cpu than swap buffer on nvidia */
// TODO: Do these and remove swap
@@ -717,21 +474,9 @@ void gsr_egl_swap_buffers(gsr_egl *self) {
//self->glFinish();
if(self->egl_display) {
self->eglSwapBuffers(self->egl_display, self->egl_surface);
} else if(self->x11.window) {
self->glXSwapBuffers(self->x11.dpy, self->x11.window);
} else if(gsr_window_get_display_server(self->window) == GSR_DISPLAY_SERVER_X11) {
Display *display = gsr_window_get_display(self->window);
const Window window = (Window)gsr_window_get_window(self->window);
self->glXSwapBuffers(display, window);
}
}
gsr_display_server gsr_egl_get_display_server(const gsr_egl *self) {
if(self->wayland.dpy)
return GSR_DISPLAY_SERVER_WAYLAND;
else
return GSR_DISPLAY_SERVER_X11;
}
XEvent* gsr_egl_get_event_data(gsr_egl *self) {
if(gsr_egl_get_display_server(self) == GSR_DISPLAY_SERVER_X11)
return &self->x11.xev;
else
return NULL;
}

View File

@@ -1,6 +1,7 @@
#include "../../../include/encoder/video/nvenc.h"
#include "../../../include/egl.h"
#include "../../../include/cuda.h"
#include "../../../include/window/window.h"
#include <libavcodec/avcodec.h>
#include <libavutil/hwcontext_cuda.h>
@@ -128,8 +129,10 @@ static void gsr_video_encoder_nvenc_stop(gsr_video_encoder_nvenc *self, AVCodecC
static bool gsr_video_encoder_nvenc_start(gsr_video_encoder *encoder, AVCodecContext *video_codec_context, AVFrame *frame) {
gsr_video_encoder_nvenc *self = encoder->priv;
const bool overclock = gsr_egl_get_display_server(self->params.egl) == GSR_DISPLAY_SERVER_X11 ? self->params.overclock : false;
if(!gsr_cuda_load(&self->cuda, self->params.egl->x11.dpy, overclock)) {
const bool is_x11 = gsr_window_get_display_server(self->params.egl->window) == GSR_DISPLAY_SERVER_X11;
const bool overclock = is_x11 ? self->params.overclock : false;
Display *display = is_x11 ? gsr_window_get_display(self->params.egl->window) : NULL;
if(!gsr_cuda_load(&self->cuda, display, overclock)) {
fprintf(stderr, "gsr error: gsr_video_encoder_nvenc_start: failed to load cuda\n");
gsr_video_encoder_nvenc_stop(self, video_codec_context);
return false;

File diff suppressed because it is too large Load Diff

View File

@@ -186,6 +186,8 @@ static void registry_event_global(void *data, uint32_t id, uint32_t permissions,
gsr_pipewire_audio_create_links(self);
}
} else if(self->num_stream_nodes >= GSR_PIPEWIRE_AUDIO_MAX_STREAM_NODES) {
fprintf(stderr, "gsr error: reached the maximum amount of audio stream nodes\n");
}
} else if(strcmp(type, PW_TYPE_INTERFACE_Port) == 0) {
const char *port_name = spa_dict_lookup(props, PW_KEY_PORT_NAME);
@@ -212,6 +214,8 @@ static void registry_event_global(void *data, uint32_t id, uint32_t permissions,
gsr_pipewire_audio_create_links(self);
}
} else if(self->num_ports >= GSR_PIPEWIRE_AUDIO_MAX_PORTS) {
fprintf(stderr, "gsr error: reached the maximum amount of audio ports\n");
}
}
}
@@ -273,20 +277,20 @@ bool gsr_pipewire_audio_init(gsr_pipewire_audio *self) {
self->thread_loop = pw_thread_loop_new("gsr screen capture", NULL);
if(!self->thread_loop) {
fprintf(stderr, "gsr error: gsr_pipewire_video_setup_stream: failed to create pipewire thread\n");
fprintf(stderr, "gsr error: gsr_pipewire_audio_init: failed to create pipewire thread\n");
gsr_pipewire_audio_deinit(self);
return false;
}
self->context = pw_context_new(pw_thread_loop_get_loop(self->thread_loop), NULL, 0);
if(!self->context) {
fprintf(stderr, "gsr error: gsr_pipewire_video_setup_stream: failed to create pipewire context\n");
fprintf(stderr, "gsr error: gsr_pipewire_audio_init: failed to create pipewire context\n");
gsr_pipewire_audio_deinit(self);
return false;
}
if(pw_thread_loop_start(self->thread_loop) < 0) {
fprintf(stderr, "gsr error: gsr_pipewire_video_setup_stream: failed to start thread\n");
fprintf(stderr, "gsr error: gsr_pipewire_audio_init: failed to start thread\n");
gsr_pipewire_audio_deinit(self);
return false;
}
@@ -308,7 +312,6 @@ bool gsr_pipewire_audio_init(gsr_pipewire_audio *self) {
self->server_version_sync = pw_core_sync(self->core, PW_ID_CORE, 0);
pw_thread_loop_wait(self->thread_loop);
pw_thread_loop_unlock(self->thread_loop);
return true;
}
@@ -319,6 +322,14 @@ void gsr_pipewire_audio_deinit(gsr_pipewire_audio *self) {
pw_thread_loop_stop(self->thread_loop);
}
for(int i = 0; i < self->num_virtual_sink_proxies; ++i) {
if(self->virtual_sink_proxies[i]) {
pw_proxy_destroy(self->virtual_sink_proxies[i]);
self->virtual_sink_proxies[i] = NULL;
}
}
self->num_virtual_sink_proxies = 0;
if(self->core) {
pw_core_disconnect(self->core);
self->core = NULL;
@@ -358,6 +369,54 @@ void gsr_pipewire_audio_deinit(gsr_pipewire_audio *self) {
#endif
}
static struct pw_properties* gsr_pipewire_create_null_audio_sink(const char *name) {
char props_str[512];
snprintf(props_str, sizeof(props_str), "{ factory.name=support.null-audio-sink node.name=\"%s\" media.class=Audio/Sink object.linger=false audio.position=[FL FR] monitor.channel-volumes=true monitor.passthrough=true adjust_time=0 node.description=gsr-app-sink slaves=\"\" }", name);
struct pw_properties *props = pw_properties_new_string(props_str);
if(!props) {
fprintf(stderr, "gsr error: gsr_pipewire_create_null_audio_sink: failed to create virtual sink properties\n");
return NULL;
}
return props;
}
bool gsr_pipewire_audio_create_virtual_sink(gsr_pipewire_audio *self, const char *name) {
if(self->num_virtual_sink_proxies == GSR_PIPEWIRE_AUDIO_MAX_VIRTUAL_SINKS) {
fprintf(stderr, "gsr error: gsr_pipewire_audio_create_virtual_sink: reached max number of virtual sinks\n");
return false;
}
pw_thread_loop_lock(self->thread_loop);
struct pw_properties *virtual_sink_props = gsr_pipewire_create_null_audio_sink(name);
if(!virtual_sink_props) {
pw_thread_loop_unlock(self->thread_loop);
return false;
}
struct pw_proxy *virtual_sink_proxy = pw_core_create_object(self->core, "adapter", PW_TYPE_INTERFACE_Node, PW_VERSION_NODE, &virtual_sink_props->dict, 0);
// TODO:
// If these are done then the above needs sizeof(*self) as the last argument
//pw_proxy_add_object_listener(virtual_sink_proxy, &pd->object_listener, &node_events, self);
//pw_proxy_add_listener(virtual_sink_proxy, &pd->proxy_listener, &proxy_events, self);
// TODO: proxy
pw_properties_free(virtual_sink_props);
if(!virtual_sink_proxy) {
fprintf(stderr, "gsr error: gsr_pipewire_audio_create_virtual_sink: failed to create virtual sink\n");
pw_thread_loop_unlock(self->thread_loop);
return false;
}
self->server_version_sync = pw_core_sync(self->core, PW_ID_CORE, self->server_version_sync);
pw_thread_loop_wait(self->thread_loop);
pw_thread_loop_unlock(self->thread_loop);
self->virtual_sink_proxies[self->num_virtual_sink_proxies] = virtual_sink_proxy;
++self->num_virtual_sink_proxies;
return true;
}
static bool string_remove_suffix(char *str, const char *suffix) {
int str_len = strlen(str);
int suffix_len = strlen(suffix);
@@ -370,8 +429,10 @@ static bool string_remove_suffix(char *str, const char *suffix) {
}
static bool gsr_pipewire_audio_add_link_from_apps_to_output(gsr_pipewire_audio *self, const char **output_names, int num_output_names, const char *input_name, gsr_pipewire_audio_node_type output_type, gsr_pipewire_audio_link_input_type input_type, bool inverted) {
if(self->num_requested_links >= GSR_PIPEWIRE_AUDIO_MAX_REQUESTED_LINKS)
if(self->num_requested_links >= GSR_PIPEWIRE_AUDIO_MAX_REQUESTED_LINKS) {
fprintf(stderr, "gsr error: reached the maximum amount of audio links\n");
return false;
}
char **output_names_copy = calloc(num_output_names, sizeof(char*));
if(!output_names_copy)
@@ -381,6 +442,9 @@ static bool gsr_pipewire_audio_add_link_from_apps_to_output(gsr_pipewire_audio *
if(!input_name_copy)
goto error;
if(input_type == GSR_PIPEWIRE_AUDIO_LINK_INPUT_TYPE_SINK)
string_remove_suffix(input_name_copy, ".monitor");
for(int i = 0; i < num_output_names; ++i) {
output_names_copy[i] = strdup(output_names[i]);
if(!output_names_copy[i])

View File

@@ -42,30 +42,11 @@ struct pa_handle {
int operation_success;
double latency_seconds;
uint32_t combined_sink_module_index;
};
static void destroy_combined_sink(pa_handle *p) {
// TODO: error handling
pa_operation *module_pa = pa_context_unload_module(p->context, p->combined_sink_module_index, NULL, NULL);
for(;;) {
if(pa_operation_get_state(module_pa) == PA_OPERATION_DONE) {
pa_operation_unref(module_pa);
break;
}
pa_mainloop_iterate(p->mainloop, 1, NULL);
}
}
static void pa_sound_device_free(pa_handle *p) {
assert(p);
if(p->combined_sink_module_index != PA_INVALID_INDEX) {
destroy_combined_sink(p);
p->combined_sink_module_index = PA_INVALID_INDEX;
}
if (p->stream) {
pa_stream_unref(p->stream);
p->stream = NULL;
@@ -90,31 +71,10 @@ static void pa_sound_device_free(pa_handle *p) {
pa_xfree(p);
}
static void module_index_callback(pa_context*, uint32_t idx, void *userdata) {
pa_handle *p = (pa_handle*)userdata;
p->combined_sink_module_index = idx;
}
static bool create_null_sink(pa_handle *p, const char *null_sink_name) {
// TODO: Error handling
char module_argument[256];
snprintf(module_argument, sizeof(module_argument), "sink_name=\"%s\" slaves= adjust_time=0", null_sink_name);
pa_operation *module_pa = pa_context_load_module(p->context, "module-null-sink", module_argument, module_index_callback, p);
for(;;) {
if(pa_operation_get_state(module_pa) == PA_OPERATION_DONE) {
pa_operation_unref(module_pa);
break;
}
pa_mainloop_iterate(p->mainloop, 1, NULL);
}
return p->combined_sink_module_index != PA_INVALID_INDEX;
}
static pa_handle* pa_sound_device_new(const char *server,
const char *name,
const char *dev,
const char *stream_name,
const char *combined_sink_name,
const pa_sample_spec *ss,
const pa_buffer_attr *attr,
int *rerror) {
@@ -122,7 +82,6 @@ static pa_handle* pa_sound_device_new(const char *server,
int error = PA_ERR_INTERNAL, r;
p = pa_xnew0(pa_handle, 1);
p->combined_sink_module_index = PA_INVALID_INDEX;
const int buffer_size = attr->fragsize;
void *buffer = malloc(buffer_size);
@@ -161,23 +120,12 @@ static pa_handle* pa_sound_device_new(const char *server,
pa_mainloop_iterate(p->mainloop, 1, NULL);
}
char device_to_record[256];
if(combined_sink_name) {
if(!create_null_sink(p, combined_sink_name)) {
fprintf(stderr, "gsr error: pa_sound_device_new: failed to create module-combine-sink\n");
goto fail;
}
snprintf(device_to_record, sizeof(device_to_record), "%s.monitor", combined_sink_name);
} else {
snprintf(device_to_record, sizeof(device_to_record), "%s", dev);
}
if (!(p->stream = pa_stream_new(p->context, stream_name, ss, NULL))) {
error = pa_context_errno(p->context);
goto fail;
}
r = pa_stream_connect_record(p->stream, device_to_record, attr,
r = pa_stream_connect_record(p->stream, dev, attr,
(pa_stream_flags_t)(PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_ADJUST_LATENCY|PA_STREAM_AUTO_TIMING_UPDATE));
if (r < 0) {
@@ -312,7 +260,7 @@ static int audio_format_to_get_bytes_per_sample(AudioFormat audio_format) {
return 2;
}
static int sound_device_setup_record(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format, const char *combined_sink_name) {
int sound_device_get_by_name(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format) {
pa_sample_spec ss;
ss.format = audio_format_to_pulse_audio_format(audio_format);
ss.rate = 48000;
@@ -326,9 +274,9 @@ static int sound_device_setup_record(SoundDevice *device, const char *device_nam
buffer_attr.maxlength = buffer_attr.fragsize;
int error = 0;
pa_handle *handle = pa_sound_device_new(nullptr, description, device_name, description, combined_sink_name, &ss, &buffer_attr, &error);
pa_handle *handle = pa_sound_device_new(nullptr, description, device_name, description, &ss, &buffer_attr, &error);
if(!handle) {
fprintf(stderr, "pa_sound_device_new() failed: %s. Audio input device %s might not be valid\n", pa_strerror(error), description);
fprintf(stderr, "pa_sound_device_new() failed: %s. Audio input device %s might not be valid\n", pa_strerror(error), device_name);
return -1;
}
@@ -337,14 +285,6 @@ static int sound_device_setup_record(SoundDevice *device, const char *device_nam
return 0;
}
int sound_device_get_by_name(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format) {
return sound_device_setup_record(device, device_name, description, num_channels, period_frame_size, audio_format, NULL);
}
int sound_device_create_combined_sink_connect(SoundDevice *device, const char *combined_sink_name, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format) {
return sound_device_setup_record(device, "gpu-screen-recorder", "gpu-screen-recorder", num_channels, period_frame_size, audio_format, combined_sink_name);
}
void sound_device_close(SoundDevice *device) {
if(device->handle)
pa_sound_device_free((pa_handle*)device->handle);

View File

@@ -1,4 +1,5 @@
#include "../include/utils.h"
#include "../include/window/window.h"
#include <time.h>
#include <string.h>
@@ -47,16 +48,6 @@ bool generate_random_characters_standard_alphabet(char *buffer, int buffer_size)
return generate_random_characters(buffer, buffer_size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62);
}
static gsr_monitor_rotation wayland_transform_to_gsr_rotation(int32_t rot) {
switch(rot) {
case 0: return GSR_MONITOR_ROT_0;
case 1: return GSR_MONITOR_ROT_90;
case 2: return GSR_MONITOR_ROT_180;
case 3: return GSR_MONITOR_ROT_270;
}
return GSR_MONITOR_ROT_0;
}
static const XRRModeInfo* get_mode_info(const XRRScreenResources *sr, RRMode id) {
for(int i = 0; i < sr->nmode; ++i) {
if(sr->modes[i].id == id)
@@ -146,31 +137,22 @@ void for_each_active_monitor_output_x11_not_cached(Display *display, active_moni
XRRFreeScreenResources(screen_res);
}
void for_each_active_monitor_output_x11(const gsr_egl *egl, active_monitor_callback callback, void *userdata) {
for(int i = 0; i < egl->x11.num_outputs; ++i) {
const gsr_x11_output *output = &egl->x11.outputs[i];
const gsr_monitor monitor = {
.name = output->name,
.name_len = strlen(output->name),
.pos = output->pos,
.size = output->size,
.connector_id = output->connector_id,
.rotation = output->rotation,
.monitor_identifier = output->monitor_identifier
};
callback(&monitor, userdata);
}
/* TODO: Support more connector types */
int get_connector_type_by_name(const char *name) {
int len = strlen(name);
if(len >= 5 && strncmp(name, "HDMI-", 5) == 0)
return 1;
else if(len >= 3 && strncmp(name, "DP-", 3) == 0)
return 2;
else if(len >= 12 && strncmp(name, "DisplayPort-", 12) == 0)
return 3;
else if(len >= 4 && strncmp(name, "eDP-", 4) == 0)
return 4;
else
return -1;
}
typedef struct {
int type;
int count;
int count_active;
} drm_connector_type_count;
#define CONNECTOR_TYPE_COUNTS 32
static drm_connector_type_count* drm_connector_types_get_index(drm_connector_type_count *type_counts, int *num_type_counts, int connector_type) {
drm_connector_type_count* drm_connector_types_get_index(drm_connector_type_count *type_counts, int *num_type_counts, int connector_type) {
for(int i = 0; i < *num_type_counts; ++i) {
if(type_counts[i].type == connector_type)
return &type_counts[i];
@@ -187,6 +169,10 @@ static drm_connector_type_count* drm_connector_types_get_index(drm_connector_typ
return &type_counts[index];
}
uint32_t monitor_identifier_from_type_and_count(int monitor_type_index, int monitor_type_count) {
return ((uint32_t)monitor_type_index << 16) | ((uint32_t)monitor_type_count);
}
static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, const char *name, uint64_t *result) {
for(int i = 0; i < props->count_props; ++i) {
drmModePropertyPtr prop = drmModeGetProperty(drmfd, props->props[i]);
@@ -202,61 +188,12 @@ static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props,
return false;
}
/* TODO: Support more connector types */
static int get_connector_type_by_name(const char *name) {
int len = strlen(name);
if(len >= 5 && strncmp(name, "HDMI-", 5) == 0)
return 1;
else if(len >= 3 && strncmp(name, "DP-", 3) == 0)
return 2;
else if(len >= 12 && strncmp(name, "DisplayPort-", 12) == 0)
return 3;
else if(len >= 4 && strncmp(name, "eDP-", 4) == 0)
return 4;
else
return -1;
}
static uint32_t monitor_identifier_from_type_and_count(int monitor_type_index, int monitor_type_count) {
return ((uint32_t)monitor_type_index << 16) | ((uint32_t)monitor_type_count);
}
static void for_each_active_monitor_output_wayland(const gsr_egl *egl, active_monitor_callback callback, void *userdata) {
drm_connector_type_count type_counts[CONNECTOR_TYPE_COUNTS];
int num_type_counts = 0;
for(int i = 0; i < egl->wayland.num_outputs; ++i) {
const gsr_wayland_output *output = &egl->wayland.outputs[i];
if(!output->name)
continue;
const int connector_type_index = get_connector_type_by_name(output->name);
drm_connector_type_count *connector_type = NULL;
if(connector_type_index != -1)
connector_type = drm_connector_types_get_index(type_counts, &num_type_counts, connector_type_index);
if(connector_type) {
++connector_type->count;
++connector_type->count_active;
}
const gsr_monitor monitor = {
.name = output->name,
.name_len = strlen(output->name),
.pos = { .x = output->pos.x, .y = output->pos.y },
.size = { .x = output->size.x, .y = output->size.y },
.connector_id = 0,
.rotation = wayland_transform_to_gsr_rotation(output->transform),
.monitor_identifier = connector_type ? monitor_identifier_from_type_and_count(connector_type_index, connector_type->count_active) : 0
};
callback(&monitor, userdata);
}
}
static void for_each_active_monitor_output_drm(const gsr_egl *egl, active_monitor_callback callback, void *userdata) {
int fd = open(egl->card_path, O_RDONLY);
if(fd == -1)
static void for_each_active_monitor_output_drm(const char *card_path, active_monitor_callback callback, void *userdata) {
int fd = open(card_path, O_RDONLY);
if(fd == -1) {
fprintf(stderr, "gsr error: for_each_active_monitor_output_drm failed, failed to open \"%s\", error: %s\n", card_path, strerror(errno));
return;
}
drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1);
@@ -315,16 +252,14 @@ static void for_each_active_monitor_output_drm(const gsr_egl *egl, active_monito
close(fd);
}
void for_each_active_monitor_output(const gsr_egl *egl, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata) {
void for_each_active_monitor_output(const gsr_window *window, const char *card_path, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata) {
switch(connection_type) {
case GSR_CONNECTION_X11:
for_each_active_monitor_output_x11(egl, callback, userdata);
break;
case GSR_CONNECTION_WAYLAND:
for_each_active_monitor_output_wayland(egl, callback, userdata);
gsr_window_for_each_active_monitor_output_cached(window, callback, userdata);
break;
case GSR_CONNECTION_DRM:
for_each_active_monitor_output_drm(egl, callback, userdata);
for_each_active_monitor_output_drm(card_path, callback, userdata);
break;
}
}
@@ -347,7 +282,7 @@ bool get_monitor_by_name(const gsr_egl *egl, gsr_connection_type connection_type
userdata.name_len = strlen(name);
userdata.monitor = monitor;
userdata.found_monitor = false;
for_each_active_monitor_output(egl, connection_type, get_monitor_by_name_callback, &userdata);
for_each_active_monitor_output(egl->window, egl->card_path, connection_type, get_monitor_by_name_callback, &userdata);
return userdata.found_monitor;
}
@@ -379,14 +314,14 @@ static void get_monitor_by_connector_id_callback(const gsr_monitor *monitor, voi
}
}
gsr_monitor_rotation drm_monitor_get_display_server_rotation(const gsr_egl *egl, const gsr_monitor *monitor) {
if(gsr_egl_get_display_server(egl) == GSR_DISPLAY_SERVER_WAYLAND) {
gsr_monitor_rotation drm_monitor_get_display_server_rotation(const gsr_window *window, const gsr_monitor *monitor) {
if(gsr_window_get_display_server(window) == GSR_DISPLAY_SERVER_WAYLAND) {
{
get_monitor_by_connector_id_userdata userdata;
userdata.monitor = monitor;
userdata.rotation = GSR_MONITOR_ROT_0;
userdata.match_found = false;
for_each_active_monitor_output_wayland(egl, get_monitor_by_name_and_size_callback, &userdata);
gsr_window_for_each_active_monitor_output_cached(window, get_monitor_by_name_and_size_callback, &userdata);
if(userdata.match_found)
return userdata.rotation;
}
@@ -395,7 +330,7 @@ gsr_monitor_rotation drm_monitor_get_display_server_rotation(const gsr_egl *egl,
userdata.monitor = monitor;
userdata.rotation = GSR_MONITOR_ROT_0;
userdata.match_found = false;
for_each_active_monitor_output_wayland(egl, get_monitor_by_connector_id_callback, &userdata);
gsr_window_for_each_active_monitor_output_cached(window, get_monitor_by_connector_id_callback, &userdata);
return userdata.rotation;
}
} else {
@@ -403,7 +338,7 @@ gsr_monitor_rotation drm_monitor_get_display_server_rotation(const gsr_egl *egl,
userdata.monitor = monitor;
userdata.rotation = GSR_MONITOR_ROT_0;
userdata.match_found = false;
for_each_active_monitor_output_x11(egl, get_monitor_by_connector_id_callback, &userdata);
gsr_window_for_each_active_monitor_output_cached(window, get_monitor_by_connector_id_callback, &userdata);
return userdata.rotation;
}
@@ -482,11 +417,11 @@ static bool version_greater_than(int major, int minor, int patch, int other_majo
return (major > other_major) || (major == other_major && minor > other_minor) || (major == other_major && minor == other_minor && patch > other_patch);
}
bool gl_driver_version_greater_than(const gsr_egl *egl, int major, int minor, int patch) {
return version_greater_than(egl->gpu_info.driver_major, egl->gpu_info.driver_minor, egl->gpu_info.driver_patch, major, minor, patch);
bool gl_driver_version_greater_than(const gsr_gpu_info *gpu_info, int major, int minor, int patch) {
return version_greater_than(gpu_info->driver_major, gpu_info->driver_minor, gpu_info->driver_patch, major, minor, patch);
}
static bool try_card_has_valid_plane(const char *card_path) {
bool try_card_has_valid_plane(const char *card_path) {
drmVersion *ver = NULL;
drmModePlaneResPtr planes = NULL;
bool found_screen_card = false;

30
src/window/window.c Normal file
View File

@@ -0,0 +1,30 @@
#include "../../include/window/window.h"
#include <stddef.h>
void gsr_window_destroy(gsr_window *self);
bool gsr_window_process_event(gsr_window *self) {
return self->process_event(self);
}
XEvent* gsr_window_get_event_data(gsr_window *self) {
if(self->get_event_data)
return self->get_event_data(self);
return NULL;
}
gsr_display_server gsr_window_get_display_server(const gsr_window *self) {
return self->get_display_server();
}
void* gsr_window_get_display(gsr_window *self) {
return self->get_display(self);
}
void* gsr_window_get_window(gsr_window *self) {
return self->get_window(self);
}
void gsr_window_for_each_active_monitor_output_cached(const gsr_window *self, active_monitor_callback callback, void *userdata) {
self->for_each_active_monitor_output_cached(self, callback, userdata);
}

321
src/window/window_wayland.c Normal file
View File

@@ -0,0 +1,321 @@
#include "../../include/window/window_wayland.h"
#include "../../include/vec2.h"
#include "../../include/defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#define GSR_MAX_OUTPUTS 32
typedef struct {
uint32_t wl_name;
void *output;
vec2i pos;
vec2i size;
int32_t transform;
char *name;
} gsr_wayland_output;
typedef struct {
void *display;
void *window;
void *registry;
void *surface;
void *compositor;
gsr_wayland_output outputs[GSR_MAX_OUTPUTS];
int num_outputs;
} gsr_window_wayland;
static void output_handle_geometry(void *data, struct wl_output *wl_output,
int32_t x, int32_t y, int32_t phys_width, int32_t phys_height,
int32_t subpixel, const char *make, const char *model,
int32_t transform) {
(void)wl_output;
(void)phys_width;
(void)phys_height;
(void)subpixel;
(void)make;
(void)model;
gsr_wayland_output *gsr_output = data;
gsr_output->pos.x = x;
gsr_output->pos.y = y;
gsr_output->transform = transform;
}
static void output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
(void)wl_output;
(void)flags;
(void)refresh;
gsr_wayland_output *gsr_output = data;
gsr_output->size.x = width;
gsr_output->size.y = height;
}
static void output_handle_done(void *data, struct wl_output *wl_output) {
(void)data;
(void)wl_output;
}
static void output_handle_scale(void* data, struct wl_output *wl_output, int32_t factor) {
(void)data;
(void)wl_output;
(void)factor;
}
static void output_handle_name(void *data, struct wl_output *wl_output, const char *name) {
(void)wl_output;
gsr_wayland_output *gsr_output = data;
if(gsr_output->name) {
free(gsr_output->name);
gsr_output->name = NULL;
}
gsr_output->name = strdup(name);
}
static void output_handle_description(void *data, struct wl_output *wl_output, const char *description) {
(void)data;
(void)wl_output;
(void)description;
}
static const struct wl_output_listener output_listener = {
.geometry = output_handle_geometry,
.mode = output_handle_mode,
.done = output_handle_done,
.scale = output_handle_scale,
.name = output_handle_name,
.description = output_handle_description,
};
static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
(void)version;
gsr_window_wayland *window_wayland = data;
if (strcmp(interface, "wl_compositor") == 0) {
if(window_wayland->compositor) {
wl_compositor_destroy(window_wayland->compositor);
window_wayland->compositor = NULL;
}
window_wayland->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
} else if(strcmp(interface, wl_output_interface.name) == 0) {
if(version < 4) {
fprintf(stderr, "gsr warning: wl output interface version is < 4, expected >= 4 to capture a monitor. Using KMS capture instead\n");
return;
}
if(window_wayland->num_outputs == GSR_MAX_OUTPUTS) {
fprintf(stderr, "gsr warning: reached maximum outputs (%d), ignoring output %u\n", GSR_MAX_OUTPUTS, name);
return;
}
gsr_wayland_output *gsr_output = &window_wayland->outputs[window_wayland->num_outputs];
window_wayland->num_outputs++;
*gsr_output = (gsr_wayland_output) {
.wl_name = name,
.output = wl_registry_bind(registry, name, &wl_output_interface, 4),
.pos = { .x = 0, .y = 0 },
.size = { .x = 0, .y = 0 },
.transform = 0,
.name = NULL,
};
wl_output_add_listener(gsr_output->output, &output_listener, gsr_output);
}
}
static void registry_remove_object(void *data, struct wl_registry *registry, uint32_t name) {
(void)data;
(void)registry;
(void)name;
}
static struct wl_registry_listener registry_listener = {
.global = registry_add_object,
.global_remove = registry_remove_object,
};
static void gsr_window_wayland_deinit(gsr_window_wayland *self) {
if(self->window) {
wl_egl_window_destroy(self->window);
self->window = NULL;
}
if(self->surface) {
wl_surface_destroy(self->surface);
self->surface = NULL;
}
for(int i = 0; i < self->num_outputs; ++i) {
if(self->outputs[i].output) {
wl_output_destroy(self->outputs[i].output);
self->outputs[i].output = NULL;
}
if(self->outputs[i].name) {
free(self->outputs[i].name);
self->outputs[i].name = NULL;
}
}
self->num_outputs = 0;
if(self->compositor) {
wl_compositor_destroy(self->compositor);
self->compositor = NULL;
}
if(self->registry) {
wl_registry_destroy(self->registry);
self->registry = NULL;
}
if(self->display) {
wl_display_disconnect(self->display);
self->display = NULL;
}
}
static bool gsr_window_wayland_init(gsr_window_wayland *self) {
self->display = wl_display_connect(NULL);
if(!self->display) {
fprintf(stderr, "gsr error: gsr_window_wayland_init failed: failed to connect to the Wayland server\n");
goto fail;
}
self->registry = wl_display_get_registry(self->display); // TODO: Error checking
wl_registry_add_listener(self->registry, &registry_listener, self); // TODO: Error checking
// Fetch globals
wl_display_roundtrip(self->display);
// Fetch wl_output
wl_display_roundtrip(self->display);
if(!self->compositor) {
fprintf(stderr, "gsr error: gsr_window_wayland_init failed: failed to find compositor\n");
goto fail;
}
self->surface = wl_compositor_create_surface(self->compositor);
if(!self->surface) {
fprintf(stderr, "gsr error: gsr_window_wayland_init failed: failed to create surface\n");
goto fail;
}
self->window = wl_egl_window_create(self->surface, 16, 16);
if(!self->window) {
fprintf(stderr, "gsr error: gsr_window_wayland_init failed: failed to create window\n");
goto fail;
}
return true;
fail:
gsr_window_wayland_deinit(self);
return false;
}
static void gsr_window_wayland_destroy(gsr_window *window) {
gsr_window_wayland *self = window->priv;
gsr_window_wayland_deinit(self);
free(self);
free(window);
}
static bool gsr_window_wayland_process_event(gsr_window *window) {
gsr_window_wayland *self = window->priv;
// TODO: pselect on wl_display_get_fd before doing dispatch
const bool events_available = wl_display_dispatch_pending(self->display) > 0;
wl_display_flush(self->display);
return events_available;
}
static gsr_display_server gsr_wayland_get_display_server(void) {
return GSR_DISPLAY_SERVER_WAYLAND;
}
static void* gsr_window_wayland_get_display(gsr_window *window) {
gsr_window_wayland *self = window->priv;
return self->display;
}
static void* gsr_window_wayland_get_window(gsr_window *window) {
gsr_window_wayland *self = window->priv;
return self->window;
}
static gsr_monitor_rotation wayland_transform_to_gsr_rotation(int32_t rot) {
switch(rot) {
case 0: return GSR_MONITOR_ROT_0;
case 1: return GSR_MONITOR_ROT_90;
case 2: return GSR_MONITOR_ROT_180;
case 3: return GSR_MONITOR_ROT_270;
}
return GSR_MONITOR_ROT_0;
}
static void gsr_window_wayland_for_each_active_monitor_output_cached(const gsr_window *window, active_monitor_callback callback, void *userdata) {
const gsr_window_wayland *self = window->priv;
drm_connector_type_count type_counts[CONNECTOR_TYPE_COUNTS];
int num_type_counts = 0;
for(int i = 0; i < self->num_outputs; ++i) {
const gsr_wayland_output *output = &self->outputs[i];
if(!output->name)
continue;
const int connector_type_index = get_connector_type_by_name(output->name);
drm_connector_type_count *connector_type = NULL;
if(connector_type_index != -1)
connector_type = drm_connector_types_get_index(type_counts, &num_type_counts, connector_type_index);
if(connector_type) {
++connector_type->count;
++connector_type->count_active;
}
const gsr_monitor monitor = {
.name = output->name,
.name_len = strlen(output->name),
.pos = { .x = output->pos.x, .y = output->pos.y },
.size = { .x = output->size.x, .y = output->size.y },
.connector_id = 0,
.rotation = wayland_transform_to_gsr_rotation(output->transform),
.monitor_identifier = connector_type ? monitor_identifier_from_type_and_count(connector_type_index, connector_type->count_active) : 0
};
callback(&monitor, userdata);
}
}
gsr_window* gsr_window_wayland_create(void) {
gsr_window *window = calloc(1, sizeof(gsr_window));
if(!window)
return window;
gsr_window_wayland *window_wayland = calloc(1, sizeof(gsr_window_wayland));
if(!window_wayland) {
free(window);
return NULL;
}
if(!gsr_window_wayland_init(window_wayland)) {
free(window_wayland);
free(window);
return NULL;
}
*window = (gsr_window) {
.destroy = gsr_window_wayland_destroy,
.process_event = gsr_window_wayland_process_event,
.get_event_data = NULL,
.get_display_server = gsr_wayland_get_display_server,
.get_display = gsr_window_wayland_get_display,
.get_window = gsr_window_wayland_get_window,
.for_each_active_monitor_output_cached = gsr_window_wayland_for_each_active_monitor_output_cached,
.priv = window_wayland
};
return window;
}

162
src/window/window_x11.c Normal file
View File

@@ -0,0 +1,162 @@
#include "../../include/window/window_x11.h"
#include "../../include/vec2.h"
#include "../../include/defs.h"
#include "../../include/utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <X11/Xlib.h>
#define GSR_MAX_OUTPUTS 32
typedef struct {
char *name;
vec2i pos;
vec2i size;
uint32_t connector_id;
gsr_monitor_rotation rotation;
uint32_t monitor_identifier; /* crtc id */
} gsr_x11_output;
typedef struct {
Display *display;
Window window;
gsr_x11_output outputs[GSR_MAX_OUTPUTS];
int num_outputs;
XEvent xev;
} gsr_window_x11;
static void store_x11_monitor(const gsr_monitor *monitor, void *userdata) {
gsr_window_x11 *window_x11 = userdata;
if(window_x11->num_outputs == GSR_MAX_OUTPUTS) {
fprintf(stderr, "gsr warning: reached maximum outputs (%d), ignoring output %s\n", GSR_MAX_OUTPUTS, monitor->name);
return;
}
char *monitor_name = strdup(monitor->name);
if(!monitor_name)
return;
const int index = window_x11->num_outputs;
window_x11->outputs[index].name = monitor_name;
window_x11->outputs[index].pos = monitor->pos;
window_x11->outputs[index].size = monitor->size;
window_x11->outputs[index].connector_id = monitor->connector_id;
window_x11->outputs[index].rotation = monitor->rotation;
window_x11->outputs[index].monitor_identifier = monitor->monitor_identifier;
++window_x11->num_outputs;
}
static void gsr_window_x11_deinit(gsr_window_x11 *self) {
if(self->window) {
XDestroyWindow(self->display, self->window);
self->window = None;
}
for(int i = 0; i < self->num_outputs; ++i) {
if(self->outputs[i].name) {
free(self->outputs[i].name);
self->outputs[i].name = NULL;
}
}
self->num_outputs = 0;
}
static bool gsr_window_x11_init(gsr_window_x11 *self) {
self->window = XCreateWindow(self->display, DefaultRootWindow(self->display), 0, 0, 16, 16, 0, CopyFromParent, InputOutput, CopyFromParent, 0, NULL);
if(!self->window) {
fprintf(stderr, "gsr error: gsr_window_x11_init failed: failed to create gl window\n");
return false;
}
self->num_outputs = 0;
for_each_active_monitor_output_x11_not_cached(self->display, store_x11_monitor, self);
return true;
}
static void gsr_window_x11_destroy(gsr_window *window) {
gsr_window_x11 *self = window->priv;
gsr_window_x11_deinit(self);
free(self);
free(window);
}
static bool gsr_window_x11_process_event(gsr_window *window) {
gsr_window_x11 *self = window->priv;
if(XPending(self->display)) {
XNextEvent(self->display, &self->xev);
return true;
}
return false;
}
static XEvent* gsr_window_x11_get_event_data(gsr_window *window) {
gsr_window_x11 *self = window->priv;
return &self->xev;
}
static gsr_display_server gsr_window_x11_get_display_server(void) {
return GSR_DISPLAY_SERVER_X11;
}
static void* gsr_window_x11_get_display(gsr_window *window) {
gsr_window_x11 *self = window->priv;
return self->display;
}
static void* gsr_window_x11_get_window(gsr_window *window) {
gsr_window_x11 *self = window->priv;
return (void*)self->window;
}
static void gsr_window_x11_for_each_active_monitor_output_cached(const gsr_window *window, active_monitor_callback callback, void *userdata) {
const gsr_window_x11 *self = window->priv;
for(int i = 0; i < self->num_outputs; ++i) {
const gsr_x11_output *output = &self->outputs[i];
const gsr_monitor monitor = {
.name = output->name,
.name_len = strlen(output->name),
.pos = output->pos,
.size = output->size,
.connector_id = output->connector_id,
.rotation = output->rotation,
.monitor_identifier = output->monitor_identifier
};
callback(&monitor, userdata);
}
}
gsr_window* gsr_window_x11_create(Display *display) {
gsr_window *window = calloc(1, sizeof(gsr_window));
if(!window)
return window;
gsr_window_x11 *window_x11 = calloc(1, sizeof(gsr_window_x11));
if(!window_x11) {
free(window);
return NULL;
}
window_x11->display = display;
if(!gsr_window_x11_init(window_x11)) {
free(window_x11);
free(window);
return NULL;
}
*window = (gsr_window) {
.destroy = gsr_window_x11_destroy,
.process_event = gsr_window_x11_process_event,
.get_event_data = gsr_window_x11_get_event_data,
.get_display_server = gsr_window_x11_get_display_server,
.get_display = gsr_window_x11_get_display,
.get_window = gsr_window_x11_get_window,
.for_each_active_monitor_output_cached = gsr_window_x11_for_each_active_monitor_output_cached,
.priv = window_x11
};
return window;
}