diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | TODO | 3 | ||||
-rwxr-xr-x | build.sh | 24 | ||||
-rw-r--r-- | external/wlr-export-dmabuf-unstable-v1.xml | 203 | ||||
-rwxr-xr-x | install.sh | 4 | ||||
-rw-r--r-- | src/egl.c | 368 | ||||
-rw-r--r-- | src/egl.h | 54 | ||||
-rw-r--r-- | src/main.cpp | 93 | ||||
-rwxr-xr-x | uninstall.sh | 11 |
10 files changed, 496 insertions, 269 deletions
@@ -4,6 +4,9 @@ compile_commands.json tests/sibs-build/ tests/compile_commands.json +external/wlr-export-dmabuf-unstable-v1-client-protocol.h +external/wlr-export-dmabuf-unstable-v1-protocol.c + *.o .clangd/ @@ -27,7 +27,7 @@ Performance is the same when recording a single window or the monitor, however i ## Installation This program depends on [gpu-screen-recorder](https://git.dec05eba.com/gpu-screen-recorder/) which needs to be installed first.\ Run `sudo ./install.sh` or if you are running Arch Linux, then you can find gpu screen recorder gtk on aur under the name gpu-screen-recorder-gtk-git (`yay -S gpu-screen-recorder-gtk-git`).\ -Dependencies needed when building using `build.sh` or `install.sh`: `gtk3 libx11 libxrandr libpulse libdrm wayland-client wayland-egl`.\ +Dependencies needed when building using `build.sh` or `install.sh`: `gtk3 libx11 libxrandr libpulse libdrm wayland-client`.\ You can also install gpu screen recorder (the gtk gui version) from [flathub](https://flathub.org/apps/details/com.dec05eba.gpu_screen_recorder). This flatpak includes gpu-screen-recorder so no need to install that first.\ Note that if you use the flatpak version then you wont be able to use overclocking unless you set "Coolbits" NVIDIA X setting. See https://git.dec05eba.com/gpu-screen-recorder/about/ for more information and how to overcome this. @@ -4,4 +4,5 @@ Add list of windows to select from. This makes it easier to select another windo Disable overclocking and show some kind of sign that overclocking is not possible (if coolbits not set). Button (in the field) to remove hotkey. Implement global hotkeys on wayland. -If wlroots, use wayland instead of drm.
\ No newline at end of file +Add pipewire capture option. +Error if polkit agent is not running (pkexec --help may work but the agent might not be running anyways).
\ No newline at end of file @@ -9,9 +9,21 @@ CXX=${CXX:-g++} opts="-O2 -g0 -DNDEBUG -Wall -Wextra -Werror -s" [ -n "$DEBUG" ] && opts="-O0 -g3 -Wall -Wextra -Werror"; -dependencies="gtk+-3.0 x11 xrandr libpulse libdrm wayland-egl wayland-client" -includes="$(pkg-config --cflags $dependencies)" -libs="$(pkg-config --libs $dependencies) -ldl" -gcc -c src/egl.c $opts $includes -g++ -c src/main.cpp $opts $includes -g++ -o gpu-screen-recorder-gtk egl.o main.o $libs $opts +build_wayland_protocol() { + wayland-scanner private-code external/wlr-export-dmabuf-unstable-v1.xml external/wlr-export-dmabuf-unstable-v1-protocol.c + wayland-scanner client-header external/wlr-export-dmabuf-unstable-v1.xml external/wlr-export-dmabuf-unstable-v1-client-protocol.h +} + +build_gsr_gtk() { + dependencies="gtk+-3.0 x11 xrandr libpulse libdrm wayland-egl wayland-client" + includes="$(pkg-config --cflags $dependencies)" + libs="$(pkg-config --libs $dependencies) -ldl" + gcc -c src/egl.c $opts $includes + $CC -c external/wlr-export-dmabuf-unstable-v1-protocol.c $opts $includes + g++ -c src/main.cpp $opts $includes + g++ -o gpu-screen-recorder-gtk egl.o wlr-export-dmabuf-unstable-v1-protocol.o main.o $libs $opts +} + +build_wayland_protocol +build_gsr_gtk +echo "Successfully built gpu-screen-recorder-gtk"
\ No newline at end of file diff --git a/external/wlr-export-dmabuf-unstable-v1.xml b/external/wlr-export-dmabuf-unstable-v1.xml new file mode 100644 index 0000000..2614065 --- /dev/null +++ b/external/wlr-export-dmabuf-unstable-v1.xml @@ -0,0 +1,203 @@ +<?xml version="1.0" encoding="UTF-8"?> +<protocol name="wlr_export_dmabuf_unstable_v1"> + <copyright> + Copyright © 2018 Rostislav Pehlivanov + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + </copyright> + + <description summary="a protocol for low overhead screen content capturing"> + An interface to capture surfaces in an efficient way by exporting DMA-BUFs. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + </description> + + <interface name="zwlr_export_dmabuf_manager_v1" version="1"> + <description summary="manager to inform clients and begin capturing"> + This object is a manager with which to start capturing from sources. + </description> + + <request name="capture_output"> + <description summary="capture a frame from an output"> + Capture the next frame of a an entire output. + </description> + <arg name="frame" type="new_id" interface="zwlr_export_dmabuf_frame_v1"/> + <arg name="overlay_cursor" type="int" + summary="include custom client hardware cursor on top of the frame"/> + <arg name="output" type="object" interface="wl_output"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy the manager"> + All objects created by the manager will still remain valid, until their + appropriate destroy request has been called. + </description> + </request> + </interface> + + <interface name="zwlr_export_dmabuf_frame_v1" version="1"> + <description summary="a DMA-BUF frame"> + This object represents a single DMA-BUF frame. + + If the capture is successful, the compositor will first send a "frame" + event, followed by one or several "object". When the frame is available + for readout, the "ready" event is sent. + + If the capture failed, the "cancel" event is sent. This can happen anytime + before the "ready" event. + + Once either a "ready" or a "cancel" event is received, the client should + destroy the frame. Once an "object" event is received, the client is + responsible for closing the associated file descriptor. + + All frames are read-only and may not be written into or altered. + </description> + + <enum name="flags"> + <description summary="frame flags"> + Special flags that should be respected by the client. + </description> + <entry name="transient" value="0x1" + summary="clients should copy frame before processing"/> + </enum> + + <event name="frame"> + <description summary="a frame description"> + Main event supplying the client with information about the frame. If the + capture didn't fail, this event is always emitted first before any other + events. + + This event is followed by a number of "object" as specified by the + "num_objects" argument. + </description> + <arg name="width" type="uint" + summary="frame width in pixels"/> + <arg name="height" type="uint" + summary="frame height in pixels"/> + <arg name="offset_x" type="uint" + summary="crop offset for the x axis"/> + <arg name="offset_y" type="uint" + summary="crop offset for the y axis"/> + <arg name="buffer_flags" type="uint" + summary="flags which indicate properties (invert, interlacing), + has the same values as zwp_linux_buffer_params_v1:flags"/> + <arg name="flags" type="uint" enum="flags" + summary="indicates special frame features"/> + <arg name="format" type="uint" + summary="format of the frame (DRM_FORMAT_*)"/> + <arg name="mod_high" type="uint" + summary="drm format modifier, high"/> + <arg name="mod_low" type="uint" + summary="drm format modifier, low"/> + <arg name="num_objects" type="uint" + summary="indicates how many objects (FDs) the frame has (max 4)"/> + </event> + + <event name="object"> + <description summary="an object description"> + Event which serves to supply the client with the file descriptors + containing the data for each object. + + After receiving this event, the client must always close the file + descriptor as soon as they're done with it and even if the frame fails. + </description> + <arg name="index" type="uint" + summary="index of the current object"/> + <arg name="fd" type="fd" + summary="fd of the current object"/> + <arg name="size" type="uint" + summary="size in bytes for the current object"/> + <arg name="offset" type="uint" + summary="starting point for the data in the object's fd"/> + <arg name="stride" type="uint" + summary="line size in bytes"/> + <arg name="plane_index" type="uint" + summary="index of the the plane the data in the object applies to"/> + </event> + + <event name="ready"> + <description summary="indicates frame is available for reading"> + This event is sent as soon as the frame is presented, indicating it is + available for reading. This event includes the time at which + presentation happened at. + + The timestamp is expressed as tv_sec_hi, tv_sec_lo, tv_nsec triples, + each component being an unsigned 32-bit value. Whole seconds are in + tv_sec which is a 64-bit value combined from tv_sec_hi and tv_sec_lo, + and the additional fractional part in tv_nsec as nanoseconds. Hence, + for valid timestamps tv_nsec must be in [0, 999999999]. The seconds part + may have an arbitrary offset at start. + + After receiving this event, the client should destroy this object. + </description> + <arg name="tv_sec_hi" type="uint" + summary="high 32 bits of the seconds part of the timestamp"/> + <arg name="tv_sec_lo" type="uint" + summary="low 32 bits of the seconds part of the timestamp"/> + <arg name="tv_nsec" type="uint" + summary="nanoseconds part of the timestamp"/> + </event> + + <enum name="cancel_reason"> + <description summary="cancel reason"> + Indicates reason for cancelling the frame. + </description> + <entry name="temporary" value="0" + summary="temporary error, source will produce more frames"/> + <entry name="permanent" value="1" + summary="fatal error, source will not produce frames"/> + <entry name="resizing" value="2" + summary="temporary error, source will produce more frames"/> + </enum> + + <event name="cancel"> + <description summary="indicates the frame is no longer valid"> + If the capture failed or if the frame is no longer valid after the + "frame" event has been emitted, this event will be used to inform the + client to scrap the frame. + + If the failure is temporary, the client may capture again the same + source. If the failure is permanent, any further attempts to capture the + same source will fail again. + + After receiving this event, the client should destroy this object. + </description> + <arg name="reason" type="uint" enum="cancel_reason" + summary="indicates a reason for cancelling this frame capture"/> + </event> + + <request name="destroy" type="destructor"> + <description summary="delete this object, used or not"> + Unreferences the frame. This request must be called as soon as its no + longer used. + + It can be called at any time by the client. The client will still have + to close any FDs it has been given. + </description> + </request> + </interface> +</protocol>
\ No newline at end of file @@ -11,4 +11,6 @@ install -Dm755 "gpu-screen-recorder-gtk" "/usr/bin/gpu-screen-recorder-gtk" install -Dm644 "gpu-screen-recorder-gtk.desktop" "/usr/share/applications/com.dec05eba.gpu_screen_recorder.desktop" install -Dm644 com.dec05eba.gpu_screen_recorder.appdata.xml "/usr/share/metainfo/com.dec05eba.gpu_screen_recorder.appdata.xml" install -Dm644 icons/hicolor/64x64/apps/com.dec05eba.gpu_screen_recorder.png "/usr/share/icons/hicolor/64x64/apps/com.dec05eba.gpu_screen_recorder.png" -install -Dm644 icons/hicolor/128x128/apps/com.dec05eba.gpu_screen_recorder.png "/usr/share/icons/hicolor/128x128/apps/com.dec05eba.gpu_screen_recorder.png"
\ No newline at end of file +install -Dm644 icons/hicolor/128x128/apps/com.dec05eba.gpu_screen_recorder.png "/usr/share/icons/hicolor/128x128/apps/com.dec05eba.gpu_screen_recorder.png" + +echo "Successfully installed gpu-screen-recorder-gtk"
\ No newline at end of file @@ -2,157 +2,131 @@ #include "library_loader.h" #include <string.h> #include <stdio.h> +#include <stdlib.h> #include <dlfcn.h> #include <assert.h> #include <wayland-client.h> #include <wayland-egl.h> -//#include "../external/wlr-export-dmabuf-unstable-v1-client-protocol.h" +#include "../external/wlr-export-dmabuf-unstable-v1-client-protocol.h" #include <unistd.h> -#if 0 -static struct wl_compositor *compositor = NULL; -static struct wl_output *output = NULL; -static struct zwlr_export_dmabuf_manager_v1 *export_manager = NULL; -static struct zwlr_export_dmabuf_frame_v1 *current_frame = NULL; -//static struct wl_shell *shell = NULL; - -struct window { - EGLContext egl_context; - struct wl_surface *surface; - //struct wl_shell_surface *shell_surface; - struct wl_egl_window *egl_window; - EGLSurface egl_surface; -}; - 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) { - fprintf(stderr, "output geometry, make: %s, model: %s\n", make, model); + 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; + (void)transform; + gsr_wayland_output *gsr_output = data; + gsr_output->pos.x = x; + gsr_output->pos.y = y; } -static void output_handle_mode(void *data, struct wl_output *wl_output, - uint32_t flags, int32_t width, int32_t height, int32_t refresh) { - +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) { - /* Nothing to do */ +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) { - /* Nothing to do */ +static void output_handle_scale(void* data, struct wl_output *wl_output, int32_t factor) { + (void)data; + (void)wl_output; + (void)factor; } -static const struct wl_output_listener output_listener = { - .geometry = output_handle_geometry, - .mode = output_handle_mode, - .done = output_handle_done, - .scale = output_handle_scale, -}; -#endif - -static void registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { - (void)version; - struct wl_compositor **wayland_compositor = data; - if (strcmp(interface, "wl_compositor") == 0) { - if(*wayland_compositor) { - wl_compositor_destroy(*wayland_compositor); - *wayland_compositor = NULL; - } - *wayland_compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1); - }/* else if(strcmp(interface, wl_output_interface.name) == 0) { - fprintf(stderr, "wayland output, name: %u\n", name); - output = wl_registry_bind(registry, name, &wl_output_interface, 1); - wl_output_add_listener(output, &output_listener, NULL); - } else if(strcmp(interface, zwlr_export_dmabuf_manager_v1_interface.name) == 0) { - export_manager = wl_registry_bind(registry, name, &zwlr_export_dmabuf_manager_v1_interface, 1); - }*/ - //fprintf(stderr, "interface: %s\n", interface); +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 registry_remove_object (void *data, struct wl_registry *registry, uint32_t name) { - (void)data; - (void)registry; - (void)name; +static void output_handle_description(void *data, struct wl_output *wl_output, const char *description) { + (void)data; + (void)wl_output; + (void)description; } -static struct wl_registry_listener registry_listener = {®istry_add_object, ®istry_remove_object}; - -#if 0 -static void register_cb(gsr_egl *egl); - -static void frame_start(void *data, struct zwlr_export_dmabuf_frame_v1 *frame, - uint32_t width, uint32_t height, uint32_t offset_x, uint32_t offset_y, - uint32_t buffer_flags, uint32_t flags, uint32_t format, - uint32_t mod_high, uint32_t mod_low, uint32_t num_objects) { - gsr_egl *egl = data; - //fprintf(stderr, "frame start, width: %u, height: %u, offset x: %u, offset y: %u, format: %u, num objects: %u\n", width, height, offset_x, offset_y, format, num_objects); - egl->width = width; - egl->height = height; - egl->pixel_format = format; - egl->modifier = ((uint64_t)mod_high << 32) | mod_low; - current_frame = frame; -} +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 frame_object(void *data, struct zwlr_export_dmabuf_frame_v1 *frame, - uint32_t index, int32_t fd, uint32_t size, uint32_t offset, - uint32_t stride, uint32_t plane_index) { - // TODO: What if we get multiple objects? then we get multiple fd per frame +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; - //egl->fd = fd; - egl->pitch = stride; - egl->offset = offset; - //fprintf(stderr, "new frame, fd: %d, index: %u, size: %u, offset: %u, stride: %u, plane_index: %u\n", fd, index, size, offset, stride, plane_index); - close(fd); -} + 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 (32), ignoring output %u\n", name); + return; + } -static void frame_ready(void *data, struct zwlr_export_dmabuf_frame_v1 *frame, - uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec) { - register_cb(data); + 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 }, + .name = NULL, + }; + wl_output_add_listener(gsr_output->output, &output_listener, gsr_output); + } else if(strcmp(interface, zwlr_export_dmabuf_manager_v1_interface.name) == 0) { + if(egl->wayland.export_manager) { + zwlr_export_dmabuf_manager_v1_destroy(egl->wayland.export_manager); + egl->wayland.export_manager = NULL; + } + egl->wayland.export_manager = wl_registry_bind(registry, name, &zwlr_export_dmabuf_manager_v1_interface, 1); + } } -static void frame_cancel(void *data, struct zwlr_export_dmabuf_frame_v1 *frame, - uint32_t reason) { - register_cb(data); +static void registry_remove_object(void *data, struct wl_registry *registry, uint32_t name) { + (void)data; + (void)registry; + (void)name; } - -static const struct zwlr_export_dmabuf_frame_v1_listener frame_listener = { - .frame = frame_start, - .object = frame_object, - .ready = frame_ready, - .cancel = frame_cancel, +static struct wl_registry_listener registry_listener = { + .global = registry_add_object, + .global_remove = registry_remove_object, }; -static struct zwlr_export_dmabuf_frame_v1 *frame_callback = NULL; -static void register_cb(gsr_egl *egl) { - bool with_cursor = false; - frame_callback = zwlr_export_dmabuf_manager_v1_capture_output(export_manager, with_cursor, output); - zwlr_export_dmabuf_frame_v1_add_listener(frame_callback, &frame_listener, egl); -} -#endif - // 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) { EGLConfig ecfg; int32_t num_config = 0; - EGLDisplay egl_display = NULL; - EGLSurface egl_surface = NULL; - EGLContext egl_context = NULL; - - Window x11_window = None; - - struct wl_registry *wayland_registry = NULL; - struct wl_compositor *wayland_compositor = NULL; - struct wl_surface *wayland_surface = NULL; - void *wayland_dpy = NULL; - void *wayland_window = NULL; - const int32_t attr[] = { EGL_BUFFER_SIZE, 24, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, @@ -165,118 +139,80 @@ static bool gsr_egl_create_window(gsr_egl *self, bool wayland) { }; if(wayland) { - wayland_dpy = wl_display_connect(NULL); - if(!wayland_dpy) { + self->wayland.dpy = wl_display_connect(NULL); + if(!self->wayland.dpy) { fprintf(stderr, "gsr error: gsr_egl_create_window failed: wl_display_connect failed\n"); goto fail; } - wayland_registry = wl_display_get_registry(wayland_dpy); // TODO: Error checking - wl_registry_add_listener(wayland_registry, ®istry_listener, &wayland_compositor); // TODO: Error checking + self->wayland.registry = wl_display_get_registry(self->wayland.dpy); // TODO: Error checking + wl_registry_add_listener(self->wayland.registry, ®istry_listener, self); // TODO: Error checking // Fetch globals - wl_display_roundtrip(wayland_dpy); + wl_display_roundtrip(self->wayland.dpy); // fetch wl_output - wl_display_roundtrip(wayland_dpy); + wl_display_roundtrip(self->wayland.dpy); - if(!wayland_compositor) { + if(!self->wayland.compositor) { fprintf(stderr, "gsr error: gsr_gl_create_window failed: failed to find compositor\n"); goto fail; } - - /*if(!output) { - fprintf(stderr, "gsr error: gsr_gl_create_window failed: failed to find output\n"); - goto fail; - } - - if(!export_manager) { - fprintf(stderr, "gsr error: gsr_gl_create_window failed: failed to find export manager\n"); - goto fail; - }*/ } else { - x11_window = XCreateWindow(self->x11_dpy, DefaultRootWindow(self->x11_dpy), 0, 0, 16, 16, 0, CopyFromParent, InputOutput, CopyFromParent, 0, NULL); + self->x11.window = XCreateWindow(self->x11.dpy, DefaultRootWindow(self->x11.dpy), 0, 0, 16, 16, 0, CopyFromParent, InputOutput, CopyFromParent, 0, NULL); - if(!x11_window) { + if(!self->x11.window) { fprintf(stderr, "gsr error: gsr_gl_create_window failed: failed to create gl window\n"); goto fail; } } - self->eglBindAPI(EGL_OPENGL_ES_API); + self->eglBindAPI(EGL_OPENGL_ES2_BIT); - egl_display = self->eglGetDisplay(wayland_dpy ? (EGLNativeDisplayType)wayland_dpy : (EGLNativeDisplayType)self->x11_dpy); - if(!egl_display) { + self->egl_display = self->eglGetDisplay(self->wayland.dpy ? (EGLNativeDisplayType)self->wayland.dpy : (EGLNativeDisplayType)self->x11.dpy); + if(!self->egl_display) { fprintf(stderr, "gsr error: gsr_egl_create_window failed: eglGetDisplay failed\n"); goto fail; } - if(!self->eglInitialize(egl_display, NULL, NULL)) { + if(!self->eglInitialize(self->egl_display, NULL, NULL)) { fprintf(stderr, "gsr error: gsr_egl_create_window failed: eglInitialize failed\n"); goto fail; } - if(!self->eglChooseConfig(egl_display, attr, &ecfg, 1, &num_config) || num_config != 1) { + if(!self->eglChooseConfig(self->egl_display, attr, &ecfg, 1, &num_config) || num_config != 1) { fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to find a matching config\n"); goto fail; } - egl_context = self->eglCreateContext(egl_display, ecfg, NULL, ctxattr); - if(!egl_context) { + self->egl_context = self->eglCreateContext(self->egl_display, ecfg, NULL, ctxattr); + if(!self->egl_context) { fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to create egl context\n"); goto fail; } if(wayland) { - wayland_surface = wl_compositor_create_surface(wayland_compositor); - wayland_window = wl_egl_window_create(wayland_surface, 16, 16); - egl_surface = self->eglCreateWindowSurface(egl_display, ecfg, (EGLNativeWindowType)wayland_window, NULL); + 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 { - egl_surface = self->eglCreateWindowSurface(egl_display, ecfg, (EGLNativeWindowType)x11_window, NULL); + self->egl_surface = self->eglCreateWindowSurface(self->egl_display, ecfg, (EGLNativeWindowType)self->x11.window, NULL); } - if(!egl_surface) { + if(!self->egl_surface) { fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to create window surface\n"); goto fail; } - if(!self->eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)) { + if(!self->eglMakeCurrent(self->egl_display, self->egl_surface, self->egl_surface, self->egl_context)) { fprintf(stderr, "gsr error: gsr_egl_create_window failed: failed to make context current\n"); goto fail; } - self->egl_display = egl_display; - self->egl_surface = egl_surface; - self->egl_context = egl_context; - - self->x11_window = x11_window; - - self->wayland_dpy = wayland_dpy; - self->wayland_window = wayland_window; - self->wayland_surface = wayland_surface; - self->wayland_compositor = wayland_compositor; - self->wayland_registry = wayland_registry; return true; fail: - if(egl_context) - self->eglDestroyContext(egl_display, egl_context); - if(egl_surface) - self->eglDestroySurface(egl_display, egl_surface); - if(egl_display) - self->eglTerminate(egl_display); - if(x11_window) - XDestroyWindow(self->x11_dpy, x11_window); - if(wayland_window) - wl_egl_window_destroy(wayland_window); - if(wayland_surface) - wl_surface_destroy(wayland_surface); - if(wayland_compositor) - wl_compositor_destroy(wayland_compositor); - if(wayland_registry) - wl_registry_destroy(wayland_registry); - if(wayland_dpy) - wl_display_disconnect(wayland_dpy); + gsr_egl_unload(self); return false; } @@ -321,7 +257,7 @@ static bool gsr_egl_load_gl(gsr_egl *self, void *library) { bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland) { memset(self, 0, sizeof(gsr_egl)); - self->x11_dpy = dpy; + self->x11.dpy = dpy; void *egl_lib = NULL; void *gl_lib = NULL; @@ -377,34 +313,52 @@ void gsr_egl_unload(gsr_egl *self) { self->egl_display = NULL; } - if(self->x11_window) { - XDestroyWindow(self->x11_dpy, self->x11_window); - self->x11_window = None; + if(self->x11.window) { + XDestroyWindow(self->x11.dpy, self->x11.window); + self->x11.window = None; } - if(self->wayland_window) { - wl_egl_window_destroy(self->wayland_window); - self->wayland_window = NULL; + if(self->wayland.export_manager) { + zwlr_export_dmabuf_manager_v1_destroy(self->wayland.export_manager); + self->wayland.export_manager = NULL; } - if(self->wayland_surface) { - wl_surface_destroy(self->wayland_surface); - self->wayland_surface = NULL; + if(self->wayland.window) { + wl_egl_window_destroy(self->wayland.window); + self->wayland.window = NULL; } - if(self->wayland_compositor) { - wl_compositor_destroy(self->wayland_compositor); - self->wayland_compositor = NULL; + if(self->wayland.surface) { + wl_surface_destroy(self->wayland.surface); + self->wayland.surface = NULL; } - if(self->wayland_registry) { - wl_registry_destroy(self->wayland_registry); - self->wayland_registry = 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->wayland.dpy) { + wl_display_disconnect(self->wayland.dpy); + self->wayland.dpy = NULL; } if(self->egl_library) { @@ -420,24 +374,6 @@ void gsr_egl_unload(gsr_egl *self) { memset(self, 0, sizeof(gsr_egl)); } -void gsr_egl_update(gsr_egl *self) { - if(!self->wayland_dpy) - return; - - wl_display_dispatch(self->wayland_dpy); -} - -void gsr_egl_cleanup_frame(gsr_egl *self) { - if(!self->wayland_dpy) - return; - - if(self->fd > 0) { - close(self->fd); - self->fd = 0; - } - - /*if(current_frame) { - zwlr_export_dmabuf_frame_v1_destroy(current_frame); - current_frame = NULL; - }*/ +bool gsr_egl_supports_wayland_capture(gsr_egl *self) { + return !!self->wayland.export_manager && self->wayland.num_outputs > 0; } @@ -8,6 +8,10 @@ #include <stdbool.h> #include <stdint.h> +typedef struct { + int x, y; +} vec2i; + #ifdef _WIN64 typedef signed long long int khronos_intptr_t; typedef unsigned long long int khronos_uintptr_t; @@ -33,7 +37,6 @@ typedef void* EGLImageKHR; typedef void *GLeglImageOES; typedef void (*__eglMustCastToProperFunctionPointerType)(void); -#define EGL_OPENGL_ES_API 0x30A0 #define EGL_BUFFER_SIZE 0x3020 #define EGL_RENDERABLE_TYPE 0x3040 #define EGL_OPENGL_ES2_BIT 0x0004 @@ -43,6 +46,33 @@ typedef void (*__eglMustCastToProperFunctionPointerType)(void); #define GL_VENDOR 0x1F00 #define GL_RENDERER 0x1F01 +#define GSR_MAX_OUTPUTS 32 + +typedef struct { + Display *dpy; + Window window; +} gsr_x11; + +typedef struct { + uint32_t wl_name; + void *output; + vec2i pos; + vec2i size; + char *name; +} gsr_wayland_output; + +typedef struct { + void *dpy; + void *window; + void *registry; + void *surface; + void *compositor; + void *export_manager; + gsr_wayland_output outputs[GSR_MAX_OUTPUTS]; + int num_outputs; + gsr_wayland_output *output_to_capture; +} gsr_wayland; + typedef struct { void *egl_library; void *gl_library; @@ -51,22 +81,8 @@ typedef struct { EGLSurface egl_surface; EGLContext egl_context; - Display *x11_dpy; - Window x11_window; - - void *wayland_dpy; - void *wayland_window; - void *wayland_registry; - void *wayland_surface; - void *wayland_compositor; - - int fd; - uint32_t width; - uint32_t height; - uint32_t pitch; - uint32_t offset; - uint32_t pixel_format; - uint64_t modifier; + gsr_x11 x11; + gsr_wayland wayland; EGLDisplay (*eglGetDisplay)(EGLNativeDisplayType display_id); unsigned int (*eglInitialize)(EGLDisplay dpy, int32_t *major, int32_t *minor); @@ -85,7 +101,7 @@ typedef struct { bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland); void gsr_egl_unload(gsr_egl *self); -void gsr_egl_update(gsr_egl *self); -void gsr_egl_cleanup_frame(gsr_egl *self); +/* wayland protocol capture, does not include kms capture */ +bool gsr_egl_supports_wayland_capture(gsr_egl *self); #endif /* GSR_EGL_H */ diff --git a/src/main.cpp b/src/main.cpp index ec6e2db..c7ad5b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,6 +98,7 @@ static bool nvfbc_installed = false; static bool wayland = false; char drm_card_path[128]; +static gsr_egl egl; enum class HotkeyMode { NoAction, @@ -456,10 +457,6 @@ static void save_configs() { } typedef struct { - int x, y; -} vec2i; - -typedef struct { const char *name; int name_len; vec2i pos; @@ -558,6 +555,25 @@ static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, return false; } +static void for_each_active_monitor_output_wayland(gsr_egl *egl, active_monitor_callback callback, void *userdata) { + if(!gsr_egl_supports_wayland_capture(egl)) + return; + + for(int i = 0; i < egl->wayland.num_outputs; ++i) { + if(!egl->wayland.outputs[i].name) + continue; + + gsr_monitor monitor; + monitor.name = egl->wayland.outputs[i].name; + monitor.name_len = strlen(egl->wayland.outputs[i].name); + monitor.pos = { egl->wayland.outputs[i].pos.x, egl->wayland.outputs[i].pos.y }; + monitor.size = { egl->wayland.outputs[i].size.x, egl->wayland.outputs[i].size.y }; + monitor.crt_info = NULL; + monitor.connector_id = 0; + callback(&monitor, userdata); + } +} + static void for_each_active_monitor_output_drm(const char *drm_card_path, active_monitor_callback callback, void *userdata) { int fd = open(drm_card_path, O_RDONLY); if(fd == -1) @@ -620,7 +636,7 @@ static void for_each_active_monitor_output(void *connection, gsr_connection_type for_each_active_monitor_output_x11((Display*)connection, callback, userdata); break; case GSR_CONNECTION_WAYLAND: - // TODO: use gsr_egl here (connection) + for_each_active_monitor_output_wayland((gsr_egl*)connection, callback, userdata); break; case GSR_CONNECTION_DRM: for_each_active_monitor_output_drm((const char*)connection, callback, userdata); @@ -1079,7 +1095,7 @@ static HotkeyResult replace_grabbed_keys_depending_on_active_page() { static bool show_pkexec_flatpak_error_if_needed() { std::string window_str = gtk_combo_box_get_active_id(GTK_COMBO_BOX(record_area_selection_menu)); - if((wayland || gpu_inf.vendor != GPU_VENDOR_NVIDIA) && window_str != "window" && window_str != "focused") { + if(!gsr_egl_supports_wayland_capture(&egl) && (wayland || gpu_inf.vendor != GPU_VENDOR_NVIDIA) && window_str != "window" && window_str != "focused") { if(!is_pkexec_installed()) { GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "pkexec needs to be installed to record a monitor with an AMD/Intel GPU. Please install and run polkit. Alternatively, record a single window which doesn't require root access."); @@ -1989,6 +2005,21 @@ static bool audio_inputs_contains(const std::vector<AudioInput> &audio_inputs, c return false; } +static void get_connection_by_active_type(void **connection, gsr_connection_type *connection_type) { + if(wayland) { + if(gsr_egl_supports_wayland_capture(&egl)) { + *connection = &egl; + *connection_type = GSR_CONNECTION_WAYLAND; + } else { + *connection = (void*)drm_card_path; + *connection_type = GSR_CONNECTION_DRM; + } + } else { + *connection = gdk_x11_get_default_xdisplay(); + *connection_type = GSR_CONNECTION_X11; + } +} + static GtkWidget* create_common_settings_page(GtkStack *stack, GtkApplication *app, const gpu_info &gpu_inf) { GtkGrid *grid = GTK_GRID(gtk_grid_new()); gtk_stack_add_named(stack, GTK_WIDGET(grid), "common-settings"); @@ -2041,8 +2072,9 @@ static GtkWidget* create_common_settings_page(GtkStack *stack, GtkApplication *a gtk_combo_box_text_append(record_area_selection_menu, "screen-direct-force", "All monitors (for VRR. No cursor, may have driver issues. Only use with VRR monitors!)"); } - void *connection = wayland ? (void*)drm_card_path : gdk_x11_get_default_xdisplay(); - const gsr_connection_type connection_type = wayland ? GSR_CONNECTION_DRM : GSR_CONNECTION_X11; + void *connection = NULL; + gsr_connection_type connection_type = GSR_CONNECTION_DRM; + get_connection_by_active_type(&connection, &connection_type); for_each_active_monitor_output(connection, connection_type, [&](const gsr_monitor *monitor, void*) { std::string label = "Monitor "; @@ -2051,10 +2083,12 @@ static GtkWidget* create_common_settings_page(GtkStack *stack, GtkApplication *a label += std::to_string(monitor->size.x); label += "x"; label += std::to_string(monitor->size.y); - if(wayland) - label += ", requires root access"; - else if(gpu_inf.vendor != GPU_VENDOR_NVIDIA) + if(wayland) { + if(!gsr_egl_supports_wayland_capture(&egl)) + label += ", requires root access"; + } else if(gpu_inf.vendor != GPU_VENDOR_NVIDIA) { label += ", requires root access, may perform better"; + } label += ")"; // Leak on purpose, what are you gonna do? stab me? @@ -2606,8 +2640,9 @@ static void load_config(const gpu_info &gpu_inf) { } else if(!wayland && (strcmp(config.main_config.record_area_option.c_str(), "screen") == 0 || strcmp(config.main_config.record_area_option.c_str(), "screen-direct-force") == 0)) { // } else { - void *connection = wayland ? (void*)drm_card_path : gdk_x11_get_default_xdisplay(); - const gsr_connection_type connection_type = wayland ? GSR_CONNECTION_DRM : GSR_CONNECTION_X11; + void *connection = NULL; + gsr_connection_type connection_type = GSR_CONNECTION_DRM; + get_connection_by_active_type(&connection, &connection_type); bool found_monitor = false; int monitor_name_size = strlen(config.main_config.record_area_option.c_str()); @@ -2728,16 +2763,10 @@ static void load_config(const gpu_info &gpu_inf) { enable_stream_record_button_if_info_filled(); } -static bool gl_get_gpu_info(Display *dpy, gpu_info *info, bool wayland) { - gsr_egl gl; - if(!gsr_egl_load(&gl, dpy, wayland)) { - fprintf(stderr, "Error: failed to load opengl\n"); - return false; - } - +static bool gl_get_gpu_info(gsr_egl *egl, gpu_info *info) { bool supported = true; - const unsigned char *gl_vendor = gl.glGetString(GL_VENDOR); - const unsigned char *gl_renderer = gl.glGetString(GL_RENDERER); + const unsigned char *gl_vendor = egl->glGetString(GL_VENDOR); + const unsigned char *gl_renderer = egl->glGetString(GL_RENDERER); info->gpu_version = 0; @@ -2747,6 +2776,12 @@ static bool gl_get_gpu_info(Display *dpy, gpu_info *info, bool wayland) { goto end; } + if(gl_renderer && strstr((const char*)gl_renderer, "llvmpipe")) { + fprintf(stderr, "Error: your opengl environment is not properly setup. It's using llvmpipe (cpu fallback) for opengl instead of your graphics card\n"); + supported = false; + goto end; + } + if(strstr((const char*)gl_vendor, "AMD")) info->vendor = GPU_VENDOR_AMD; else if(strstr((const char*)gl_vendor, "Intel")) @@ -2765,7 +2800,6 @@ static bool gl_get_gpu_info(Display *dpy, gpu_info *info, bool wayland) { } end: - gsr_egl_unload(&gl); return supported; } @@ -2801,7 +2835,16 @@ static void activate(GtkApplication *app, gpointer) { nvfbc_installed = !wayland && is_nv_fbc_installed(); - if(wayland) { + if(!gsr_egl_load(&egl, dpy, wayland)) { + GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, + "Failed to load OpenGL. Make sure your GPU drivers are properly installed."); + gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); + g_application_quit(G_APPLICATION(app)); + return; + } + + if(wayland && !gsr_egl_supports_wayland_capture(&egl)) { if(!gsr_get_valid_card_path(drm_card_path)) { GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Failed to find a valid DRM card."); @@ -2814,7 +2857,7 @@ static void activate(GtkApplication *app, gpointer) { drm_card_path[0] = '\0'; } - if(!gl_get_gpu_info(gdk_x11_get_default_xdisplay(), &gpu_inf, wayland)) { + if(!gl_get_gpu_info(&egl, &gpu_inf)) { GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Failed to get OpenGL information. Make sure your GPU drivers are properly installed."); gtk_dialog_run(GTK_DIALOG(dialog)); diff --git a/uninstall.sh b/uninstall.sh new file mode 100755 index 0000000..2cf9c8a --- /dev/null +++ b/uninstall.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +[ $(id -u) -ne 0 ] && echo "You need root privileges to run the uninstall script" && exit 1 + +rm -f "/usr/bin/gpu-screen-recorder-gtk" +rm -f "/usr/share/applications/com.dec05eba.gpu_screen_recorder.desktop" +rm -f "/usr/share/metainfo/com.dec05eba.gpu_screen_recorder.appdata.xml" +rm -f "/usr/share/icons/hicolor/64x64/apps/com.dec05eba.gpu_screen_recorder.png" +rm -f "/usr/share/icons/hicolor/128x128/apps/com.dec05eba.gpu_screen_recorder.png" + +echo "Successfully uninstalled gpu-screen-recorder-gtk"
\ No newline at end of file |