diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 68ed3e641..0379150ab 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -131,7 +131,7 @@ resort suggestion. ### Hardware Encoders throttle/drop FPS during high GPU load Capture methods (`wlgrab`) or encoders (`nvenc`, `vaapi`) that utilize EGL contexts may exhibit FPS drops in conjunction with a Sunshine installation that runs in a sandboxed or reduced permissions state -(Flatpak, AppImage, or when using Portal capture) due to the lack of active CAP_SYS_NICE process permissions +(Flatpak, AppImage packages) due to the lack of active CAP_SYS_NICE process permissions needed to set up high priority EGL contexts. To check if you are affected by this issue, look out for this message in your Sunshine log: @@ -142,12 +142,12 @@ Warning: EGL: context priority set to HIGH but CAP_SYS_NICE capability is missin > [!IMPORTANT] > Switching to Vulkan encoding should resolve the issue for the majority of configurations, but refer to this > table for recommended configurations (especially if Vulkan encoding is not supported on your system): -> | Desktop Environment | Vulkan Supported? | Recommended Sunshine Install Type | Recommended Capture & Encoder Configuration | -> |:--------------------|-------------------|-----------------------------------|--------------------------------------------------:| -> | KDE Plasma | Yes | Any | `portal` or `kwin` capture with `vulkan` encoding | -> | KDE Plasma | No | Non-Sandboxed | `kwin` capture with `vaapi`/`nvenc` encoding | -> | GNOME / other | Yes | Any | `portal` capture with `vulkan` encoding | -> | GNOME / other | No | Non-Sandboxed | `kms` capture with `vaapi`/`nvenc` encoding | +> | Desktop Environment | Vulkan Supported? | Recommended Sunshine Install Type | Recommended Capture & Encoder Configuration | +> |:--------------------|-------------------|-----------------------------------|---------------------------------------------------------:| +> | KDE Plasma | Yes | Any | `portal` or `kwin` capture with `vulkan` encoding | +> | KDE Plasma | No | Non-Sandboxed | `portal` or `kwin` capture with `vaapi`/`nvenc` encoding | +> | GNOME / other | Yes | Any | `portal` capture with `vulkan` encoding | +> | GNOME / other | No | Non-Sandboxed | `portal` capture with `vaapi`/`nvenc` encoding | ### Hardware Encoding fails Due to legal concerns, Mesa has disabled hardware decoding and encoding by default. diff --git a/src/main.cpp b/src/main.cpp index ff0f015e6..f349a233d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,12 @@ #include #endif #ifdef __linux__ + #include "platform/linux/graphics.h" + #include + #if defined(SUNSHINE_BUILD_DRM) + #include "platform/linux/misc.h" + #endif #endif // lib includes @@ -215,6 +220,13 @@ int main(int argc, char *argv[]) { if (platf::has_elevated_privileges(false)) { platf::drop_elevated_privileges(false); } + + // Next, initialize privileged EGL worker thread + egl::ensure_privileged_egl_worker_started(); + // Finally, check and drop all capabilities via 'true', which includes CAP_SYS_NICE. + if (platf::has_elevated_privileges(true)) { + platf::drop_elevated_privileges(true); + } #endif lifetime::argv = argv; diff --git a/src/platform/linux/graphics.cpp b/src/platform/linux/graphics.cpp index 4a65c69b3..1ada5a0c9 100644 --- a/src/platform/linux/graphics.cpp +++ b/src/platform/linux/graphics.cpp @@ -4,6 +4,8 @@ */ // standard includes #include +#include +#include // local includes #include "graphics.h" @@ -334,6 +336,153 @@ namespace gbm { namespace egl { + namespace { // Keep privileged implementation details anonymous/local to this translation unit + +#if !defined(__FreeBSD__) + /** + * @brief Temporarily owns CAP_SYS_NICE while creating EGL contexts. + */ + class cap_sys_nice { + public: + static bool verify_cap_sys_nice() { + cap_t caps = cap_get_proc(); + cap_value_t sys_nice = CAP_SYS_NICE; + cap_flag_value_t value; + cap_get_flag(caps, sys_nice, CAP_EFFECTIVE, &value); + cap_free(caps); + + if (value != CAP_SET) { + BOOST_LOG(debug) << "Failed to verify CAP_SYS_NICE effective capability"sv; + return false; + } + + return true; + } + + cap_sys_nice() { + caps = cap_get_proc(); + + cap_value_t sys_nice = CAP_SYS_NICE; + if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &sys_nice, CAP_SET) || cap_set_proc(caps)) { + BOOST_LOG(debug) << "Failed to gain CAP_SYS_NICE"; + } + } + + ~cap_sys_nice() { + cap_value_t sys_nice = CAP_SYS_NICE; + if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &sys_nice, CAP_CLEAR) || cap_set_proc(caps)) { + BOOST_LOG(debug) << "Failed to drop CAP_SYS_NICE"; + } + cap_free(caps); + } + + cap_t caps; ///< Caps + }; +#endif + + /** + * @brief Reports that the privileged EGL worker rejected a task. + */ + class privileged_egl_worker_stopped final: public std::runtime_error { + public: + using std::runtime_error::runtime_error; ///< Inherit standard runtime error constructors. + }; + + class privileged_egl_worker { + public: + static void ensure_started() { + instance(); + } + + static EGLContext eglCreateContext_privileged(EGLDisplay display, EGLConfig config, EGLContext share_context, EGLint const *attrib_list, bool &nice_warning) { + try { + return instance().run([display, config, share_context, attrib_list, &nice_warning] { + nice_warning = false; +#if !defined(__FreeBSD__) + nice_warning = !egl::cap_sys_nice::verify_cap_sys_nice(); +#endif + + if (!eglBindAPI(EGL_OPENGL_API)) { + BOOST_LOG(error) << "Couldn't bind API in privileged thread: ["sv << util::hex(eglGetError()).to_string_view() << ']'; + return EGL_NO_CONTEXT; + } + EGLContext privileged_ctx = eglCreateContext(display, config, share_context, attrib_list); + if (privileged_ctx == EGL_NO_CONTEXT) { + BOOST_LOG(error) << "Couldn't create EGL context in privileged thread: ["sv << util::hex(eglGetError()).to_string_view() << ']'; + } + return privileged_ctx; + }); + } catch (const privileged_egl_worker_stopped &e) { + BOOST_LOG(error) << "Couldn't execute privileged EGL context creation: "sv << e.what(); + return EGL_NO_CONTEXT; + } + } + + private: + static privileged_egl_worker &instance() { + static privileged_egl_worker w; + return w; + } + + privileged_egl_worker(): + thread_ {[this] { + sigset_t all; + sigfillset(&all); + if (pthread_sigmask(SIG_BLOCK, &all, nullptr) != 0) { + BOOST_LOG(error) << "Failed to block signals in egl_worker"sv; + queue_.stop(); + return; + } + + platf::set_thread_name("egl_worker"); + for (;;) { + auto task = queue_.pop(); + if (!task) { + break; + } + (*task)(); + } + }} { + } + + ~privileged_egl_worker() { + queue_.stop(); + } + + template + auto run(F &&f) -> std::invoke_result_t { + using R = std::invoke_result_t; + auto task = std::make_shared>( + [f = std::forward(f)]() mutable -> R { +#if !defined(__FreeBSD__) + cap_sys_nice nice; +#endif + return f(); + } + ); + auto fut = task->get_future(); + + if (!queue_.raise([task]() mutable { + (*task)(); + })) { + throw privileged_egl_worker_stopped {"privileged_egl_worker: task rejected (worker stopping)"}; + } + + return fut.get(); + } + + safe::queue_t> queue_ {32, safe::queue_t>::overflow_policy_e::reject}; + std::jthread thread_; + }; + } // namespace + + /** + * @brief Allows the EGL privileged worker thread to be constructed early. + */ + void ensure_privileged_egl_worker_started() { + privileged_egl_worker::ensure_started(); + } + /** * @brief Log EGL failure details and return an error code. */ @@ -421,16 +570,6 @@ namespace egl { */ std::optional make_ctx(display_t::pointer display) { bool nice_warning = false; -#if !defined(__FreeBSD__) - cap_t caps = cap_get_proc(); - - cap_value_t sys_nice = CAP_SYS_NICE; - if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &sys_nice, CAP_SET) || cap_set_proc(caps)) { - BOOST_LOG(debug) << "Failed to gain CAP_SYS_NICE"sv; - nice_warning = true; - } - cap_free(caps); -#endif constexpr int conf_attr[] { EGL_RENDERABLE_TYPE, @@ -464,9 +603,8 @@ namespace egl { } attr.push_back(EGL_NONE); - EGLContext raw_ctx = eglCreateContext(display, conf, EGL_NO_CONTEXT, attr.data()); + EGLContext raw_ctx = egl::privileged_egl_worker::eglCreateContext_privileged(display, conf, EGL_NO_CONTEXT, attr.data(), nice_warning); if (raw_ctx == EGL_NO_CONTEXT) { - BOOST_LOG(error) << "Couldn't create EGL context: ["sv << util::hex(eglGetError()).to_string_view() << ']'; return std::nullopt; } @@ -520,14 +658,6 @@ namespace egl { gl::ctx.PixelStorei(GL_UNPACK_ALIGNMENT, 1); -#if !defined(__FreeBSD__) - caps = cap_get_proc(); - if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &sys_nice, CAP_CLEAR) || cap_set_proc(caps)) { - BOOST_LOG(debug) << "Failed to drop CAP_SYS_NICE"sv; - } - cap_free(caps); -#endif - return ctx; } diff --git a/src/platform/linux/graphics.h b/src/platform/linux/graphics.h index 2a2a06429..60c0a1dfc 100644 --- a/src/platform/linux/graphics.h +++ b/src/platform/linux/graphics.h @@ -518,6 +518,11 @@ namespace egl { */ std::optional make_ctx(display_t::pointer display); + /** + * @brief Allows the EGL privileged worker thread to be constructed early. + */ + void ensure_privileged_egl_worker_started(); + /** * @brief Import an RGB source surface. * diff --git a/src/platform/linux/kwingrab.cpp b/src/platform/linux/kwingrab.cpp index 934318713..e82e58090 100644 --- a/src/platform/linux/kwingrab.cpp +++ b/src/platform/linux/kwingrab.cpp @@ -345,14 +345,6 @@ namespace kwin { return 0; } - /** - * @brief Check if kwin screencasting is currently available - * @return true if screencast can be started, false otherwise - */ - bool is_kwin_screencasting_available() const { - return kde_screencast_v1_ != nullptr; - } - /** * @brief Generate a sorted list of known output names. * @return List of strings with output names to pass to start() @@ -674,20 +666,6 @@ namespace kwin { if (screencast->init(true) < 0) { return -1; } -#if !defined(__FreeBSD__) - // Check if KWin screencasting extension is accessible after first init attempt - if (!screencast->is_kwin_screencasting_available()) { - // KWin screencasting extension was not found. Drop ALL elevated privileges in case KWin is missing CAP_SYS_NICE - BOOST_LOG(warning) << "[kwingrab] KWin screencasting unavailable after init. Trying again after dropping ALL elevated privileges."sv; - platf::drop_elevated_privileges(true); - // Retry screencast session init after privilege drop - screencast.reset(); // Cleanup current screencast instance - screencast = std::make_unique(); // Create new screencast instance - if (screencast->init(true) < 0) { - return -1; - } - } -#endif if (screencast->start(display_name) < 0) { return -1; } diff --git a/src/platform/linux/portalgrab.cpp b/src/platform/linux/portalgrab.cpp index 5b8802aef..8e8c25d34 100644 --- a/src/platform/linux/portalgrab.cpp +++ b/src/platform/linux/portalgrab.cpp @@ -805,11 +805,6 @@ namespace platf { return nullptr; } - // Drop CAP_SYS_ADMIN, CAP_SYS_NICE and set DUMPABLE flag to allow XDG /root access - if (has_elevated_privileges(true)) { - drop_elevated_privileges(true); - } - auto portal = std::make_shared(); if (portal->init(hwdevice_type, display_name, config)) { return nullptr;