diff --git a/CMakeLists.txt b/CMakeLists.txt index 3441039e10f..9d5c58b7c07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -318,6 +318,7 @@ protocolnew("${HYPRLAND_PROTOCOLS}/protocols" "hyprland-focus-grab-v1" true) protocolnew("protocols" "wlr-layer-shell-unstable-v1" true) protocolnew("protocols" "wayland-drm" true) protocolnew("${HYPRLAND_PROTOCOLS}/protocols" "hyprland-ctm-control-v1" true) +protocolnew("${HYPRLAND_PROTOCOLS}/protocols" "hyprland-surface-v1" true) protocolnew("staging/tearing-control" "tearing-control-v1" false) protocolnew("staging/fractional-scale" "fractional-scale-v1" false) diff --git a/protocols/meson.build b/protocols/meson.build index 2c0d06d1221..a39602b7e31 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -37,6 +37,7 @@ protocols = [ hyprland_protocol_dir / 'protocols/hyprland-toplevel-export-v1.xml', hyprland_protocol_dir / 'protocols/hyprland-focus-grab-v1.xml', hyprland_protocol_dir / 'protocols/hyprland-ctm-control-v1.xml', + hyprland_protocol_dir / 'protocols/hyprland-surface-v1.xml', wayland_protocol_dir / 'staging/tearing-control/tearing-control-v1.xml', wayland_protocol_dir / 'staging/fractional-scale/fractional-scale-v1.xml', wayland_protocol_dir / 'unstable/xdg-output/xdg-output-unstable-v1.xml', diff --git a/src/desktop/WLSurface.hpp b/src/desktop/WLSurface.hpp index a962b882c21..346fc935bcf 100644 --- a/src/desktop/WLSurface.hpp +++ b/src/desktop/WLSurface.hpp @@ -86,6 +86,9 @@ class CWLSurface { // used by the alpha-modifier protocol float m_pAlphaModifier = 1.F; + // used by the hyprland-surface protocol + float m_fOverallOpacity = 1.F; + struct { CSignal destroy; } events; @@ -116,4 +119,4 @@ class CWLSurface { } listeners; friend class CPointerConstraint; -}; \ No newline at end of file +}; diff --git a/src/managers/ProtocolManager.cpp b/src/managers/ProtocolManager.cpp index cf517b22a44..0e1c21f51ef 100644 --- a/src/managers/ProtocolManager.cpp +++ b/src/managers/ProtocolManager.cpp @@ -47,6 +47,7 @@ #include "../protocols/SinglePixel.hpp" #include "../protocols/SecurityContext.hpp" #include "../protocols/CTMControl.hpp" +#include "../protocols/HyprlandSurface.hpp" #include "../protocols/core/Seat.hpp" #include "../protocols/core/DataDevice.hpp" @@ -159,6 +160,7 @@ CProtocolManager::CProtocolManager() { PROTO::singlePixel = std::make_unique(&wp_single_pixel_buffer_manager_v1_interface, 1, "SinglePixel"); PROTO::securityContext = std::make_unique(&wp_security_context_manager_v1_interface, 1, "SecurityContext"); PROTO::ctm = std::make_unique(&hyprland_ctm_control_manager_v1_interface, 1, "CTMControl"); + PROTO::hyprlandSurface = std::make_unique(&hyprland_surface_manager_v1_interface, 1, "HyprlandSurface"); for (auto const& b : g_pCompositor->m_pAqBackend->getImplementations()) { if (b->type() != Aquamarine::AQ_BACKEND_DRM) @@ -232,6 +234,7 @@ CProtocolManager::~CProtocolManager() { PROTO::singlePixel.reset(); PROTO::securityContext.reset(); PROTO::ctm.reset(); + PROTO::hyprlandSurface.reset(); PROTO::lease.reset(); PROTO::sync.reset(); @@ -282,6 +285,7 @@ bool CProtocolManager::isGlobalPrivileged(const wl_global* global) { PROTO::xdgDialog->getGlobal(), PROTO::singlePixel->getGlobal(), PROTO::primarySelection->getGlobal(), + PROTO::hyprlandSurface->getGlobal(), PROTO::sync ? PROTO::sync->getGlobal() : nullptr, PROTO::mesaDRM ? PROTO::mesaDRM->getGlobal() : nullptr, PROTO::linuxDma ? PROTO::linuxDma->getGlobal() : nullptr, diff --git a/src/protocols/HyprlandSurface.cpp b/src/protocols/HyprlandSurface.cpp new file mode 100644 index 00000000000..f9009d7dfd3 --- /dev/null +++ b/src/protocols/HyprlandSurface.cpp @@ -0,0 +1,97 @@ +#include "HyprlandSurface.hpp" +#include "../desktop/WLSurface.hpp" +#include "../render/Renderer.hpp" +#include "core/Compositor.hpp" +#include "hyprland-surface-v1.hpp" + +CHyprlandSurface::CHyprlandSurface(SP resource, SP surface) : m_pResource(resource), m_pSurface(surface) { + if (!m_pResource->resource()) + return; + + m_pResource->setDestroy([this](CHyprlandSurfaceV1* resource) { destroy(); }); + m_pResource->setOnDestroy([this](CHyprlandSurfaceV1* resource) { destroy(); }); + + m_pResource->setSetOpacity([this](CHyprlandSurfaceV1* resource, uint32_t opacity) { + if (!m_pSurface) { + m_pResource->error(HYPRLAND_SURFACE_V1_ERROR_NO_SURFACE, "set_opacity called for destroyed wl_surface"); + return; + } + + auto fOpacity = wl_fixed_to_double(opacity); + if (fOpacity < 0.0 || fOpacity > 1.0) { + m_pResource->error(HYPRLAND_SURFACE_V1_ERROR_OUT_OF_RANGE, "set_opacity called with an opacity value larger than 1.0 or smaller than 0.0."); + return; + } + + m_fOpacity = fOpacity; + }); + + listeners.surfaceCommitted = m_pSurface->events.commit.registerListener([this](std::any data) { + auto surface = CWLSurface::fromResource(m_pSurface.lock()); + + if (surface && surface->m_fOverallOpacity != m_fOpacity) { + surface->m_fOverallOpacity = m_fOpacity; + auto box = surface->getSurfaceBoxGlobal(); + + if (box.has_value()) + g_pHyprRenderer->damageBox(&*box); + } + }); + + listeners.surfaceDestroyed = m_pSurface->events.destroy.registerListener([this](std::any data) { m_pSurface.reset(); }); +} + +bool CHyprlandSurface::good() const { + return m_pResource->resource(); +} + +void CHyprlandSurface::destroy() { + auto surface = CWLSurface::fromResource(m_pSurface.lock()); + + if (surface) { + surface->m_fOverallOpacity = 1.F; + auto box = surface->getSurfaceBoxGlobal(); + + if (box.has_value()) + g_pHyprRenderer->damageBox(&*box); + } + + PROTO::hyprlandSurface->destroySurface(this); +} + +CHyprlandSurfaceProtocol::CHyprlandSurfaceProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { + ; +} + +void CHyprlandSurfaceProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { + auto manager = m_vManagers.emplace_back(std::make_unique(client, ver, id)).get(); + manager->setOnDestroy([this](CHyprlandSurfaceManagerV1* manager) { destroyManager(manager); }); + + manager->setDestroy([this](CHyprlandSurfaceManagerV1* manager) { destroyManager(manager); }); + manager->setGetHyprlandSurface( + [this](CHyprlandSurfaceManagerV1* manager, uint32_t id, wl_resource* surface) { getSurface(manager, id, CWLSurfaceResource::fromResource(surface)); }); +} + +void CHyprlandSurfaceProtocol::destroyManager(CHyprlandSurfaceManagerV1* manager) { + std::erase_if(m_vManagers, [&](const auto& p) { return p.get() == manager; }); +} + +void CHyprlandSurfaceProtocol::destroySurface(CHyprlandSurface* surface) { + std::erase_if(m_mSurfaces, [&](const auto& entry) { return entry.second.get() == surface; }); +} + +void CHyprlandSurfaceProtocol::getSurface(CHyprlandSurfaceManagerV1* manager, uint32_t id, SP surface) { + if (m_mSurfaces.contains(surface)) { + LOGM(ERR, "HyprlandSurface already present for surface {:x}", (uintptr_t)surface.get()); + manager->error(HYPRLAND_SURFACE_MANAGER_V1_ERROR_ALREADY_CONSTRUCTED, "HyprlandSurface already present"); + return; + } + + auto hyprlandSurface = + m_mSurfaces.emplace(surface, std::make_unique(makeShared(manager->client(), manager->version(), id), surface)).first->second.get(); + + if (!hyprlandSurface->good()) { + manager->noMemory(); + m_mSurfaces.erase(surface); + } +} diff --git a/src/protocols/HyprlandSurface.hpp b/src/protocols/HyprlandSurface.hpp new file mode 100644 index 00000000000..ad9073f84f7 --- /dev/null +++ b/src/protocols/HyprlandSurface.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include +#include "WaylandProtocol.hpp" +#include "hyprland-surface-v1.hpp" +#include "../helpers/signal/Signal.hpp" + +class CWLSurfaceResource; + +class CHyprlandSurface { + public: + CHyprlandSurface(SP resource, SP surface); + + bool good() const; + + private: + SP m_pResource; + WP m_pSurface; + float m_fOpacity = 1.0; + + void destroy(); + + struct { + CHyprSignalListener surfaceCommitted; + CHyprSignalListener surfaceDestroyed; + } listeners; +}; + +class CHyprlandSurfaceProtocol : public IWaylandProtocol { + public: + CHyprlandSurfaceProtocol(const wl_interface* iface, const int& ver, const std::string& name); + + virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id); + + private: + void destroyManager(CHyprlandSurfaceManagerV1* res); + void destroySurface(CHyprlandSurface* surface); + void getSurface(CHyprlandSurfaceManagerV1* manager, uint32_t id, SP surface); + + std::vector> m_vManagers; + std::unordered_map, UP> m_mSurfaces; + + friend class CHyprlandSurface; +}; + +namespace PROTO { + inline UP hyprlandSurface; +} diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index d9f19feaa45..93bd129affb 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -2045,7 +2045,8 @@ bool CHyprOpenGLImpl::shouldUseNewBlurOptimizations(PHLLS pLayer, PHLWINDOW pWin return false; } -void CHyprOpenGLImpl::renderTextureWithBlur(SP tex, CBox* pBox, float a, SP pSurface, int round, bool blockBlurOptimization, float blurA) { +void CHyprOpenGLImpl::renderTextureWithBlur(SP tex, CBox* pBox, float a, SP pSurface, int round, bool blockBlurOptimization, float blurA, + float overallA) { RASSERT(m_RenderData.pMonitor, "Tried to render texture with blur without begin()!"); static auto PNOBLUROVERSIZED = CConfigValue("decoration:no_blur_on_oversized"); @@ -2126,7 +2127,7 @@ void CHyprOpenGLImpl::renderTextureWithBlur(SP tex, CBox* pBox, float setMonitorTransformEnabled(true); if (!USENEWOPTIMIZE) setRenderModifEnabled(false); - renderTextureInternalWithDamage(POUTFB->getTexture(), &MONITORBOX, *PBLURIGNOREOPACITY ? blurA : a * blurA, texDamage, 0, false, false, false); + renderTextureInternalWithDamage(POUTFB->getTexture(), &MONITORBOX, (*PBLURIGNOREOPACITY ? blurA : a * blurA) * overallA, texDamage, 0, false, false, false); if (!USENEWOPTIMIZE) setRenderModifEnabled(true); setMonitorTransformEnabled(false); @@ -2137,7 +2138,7 @@ void CHyprOpenGLImpl::renderTextureWithBlur(SP tex, CBox* pBox, float // draw window glDisable(GL_STENCIL_TEST); - renderTextureInternalWithDamage(tex, pBox, a, texDamage, round, false, false, true, true); + renderTextureInternalWithDamage(tex, pBox, a * overallA, texDamage, round, false, false, true, true); glStencilMask(0xFF); glStencilFunc(GL_ALWAYS, 1, 0xFF); diff --git a/src/render/OpenGL.hpp b/src/render/OpenGL.hpp index 0b72d4383e4..4586d6bc125 100644 --- a/src/render/OpenGL.hpp +++ b/src/render/OpenGL.hpp @@ -177,7 +177,8 @@ class CHyprOpenGLImpl { void renderTexture(SP, CBox*, float a, int round = 0, bool discardActive = false, bool allowCustomUV = false); void renderTextureWithDamage(SP, CBox*, const CRegion& damage, float a, int round = 0, bool discardActive = false, bool allowCustomUV = false, SP waitTimeline = nullptr, uint64_t waitPoint = 0); - void renderTextureWithBlur(SP, CBox*, float a, SP pSurface, int round = 0, bool blockBlurOptimization = false, float blurA = 1.f); + void renderTextureWithBlur(SP, CBox*, float a, SP pSurface, int round = 0, bool blockBlurOptimization = false, float blurA = 1.f, + float overallA = 1.f); void renderRoundedShadow(CBox*, int round, int range, const CHyprColor& color, float a = 1.0); void renderBorder(CBox*, const CGradientValueData&, int round, int borderSize, float a = 1.0, int outerRound = -1 /* use round */); void renderBorder(CBox*, const CGradientValueData&, const CGradientValueData&, float lerp, int round, int borderSize, float a = 1.0, int outerRound = -1 /* use round */); diff --git a/src/render/pass/SurfacePassElement.cpp b/src/render/pass/SurfacePassElement.cpp index 38de521080b..89d4301c88d 100644 --- a/src/render/pass/SurfacePassElement.cpp +++ b/src/render/pass/SurfacePassElement.cpp @@ -58,8 +58,9 @@ void CSurfacePassElement::draw(const CRegion& damage) { auto PSURFACE = CWLSurface::fromResource(data.surface); - const float ALPHA = data.alpha * data.fadeAlpha * (PSURFACE ? PSURFACE->m_pAlphaModifier : 1.F); - const bool BLUR = data.blur && (!TEXTURE->m_bOpaque || ALPHA < 1.F); + const float ALPHA = data.alpha * data.fadeAlpha * (PSURFACE ? PSURFACE->m_pAlphaModifier : 1.F); + const float OVERALL_ALPHA = PSURFACE ? PSURFACE->m_fOverallOpacity : 1.F; + const bool BLUR = data.blur && (!TEXTURE->m_bOpaque || ALPHA < 1.F || OVERALL_ALPHA < 1.F); auto windowBox = getTexBox(); @@ -107,14 +108,14 @@ void CSurfacePassElement::draw(const CRegion& damage) { // to what we do for misaligned surfaces (blur the entire thing and then render shit without blur) if (data.surfaceCounter == 0 && !data.popup) { if (BLUR) - g_pHyprOpenGL->renderTextureWithBlur(TEXTURE, &windowBox, ALPHA, data.surface, rounding, data.blockBlurOptimization, data.fadeAlpha); + g_pHyprOpenGL->renderTextureWithBlur(TEXTURE, &windowBox, ALPHA, data.surface, rounding, data.blockBlurOptimization, data.fadeAlpha, OVERALL_ALPHA); else - g_pHyprOpenGL->renderTexture(TEXTURE, &windowBox, ALPHA, rounding, false, true); + g_pHyprOpenGL->renderTexture(TEXTURE, &windowBox, ALPHA * OVERALL_ALPHA, rounding, false, true); } else { if (BLUR && data.popup) - g_pHyprOpenGL->renderTextureWithBlur(TEXTURE, &windowBox, ALPHA, data.surface, rounding, true, data.fadeAlpha); + g_pHyprOpenGL->renderTextureWithBlur(TEXTURE, &windowBox, ALPHA, data.surface, rounding, true, data.fadeAlpha, OVERALL_ALPHA); else - g_pHyprOpenGL->renderTexture(TEXTURE, &windowBox, ALPHA, rounding, false, true); + g_pHyprOpenGL->renderTexture(TEXTURE, &windowBox, ALPHA * OVERALL_ALPHA, rounding, false, true); } if (!g_pHyprRenderer->m_bBlockSurfaceFeedback) diff --git a/subprojects/hyprland-protocols b/subprojects/hyprland-protocols index c7c3f4cd0fa..c9eb0c7972d 160000 --- a/subprojects/hyprland-protocols +++ b/subprojects/hyprland-protocols @@ -1 +1 @@ -Subproject commit c7c3f4cd0faed21fc90ba6bd06fe4f3e0e057ea8 +Subproject commit c9eb0c7972d8f760ac3e68fa32cbd1ddeeac4c78