Skip to content

Commit

Permalink
protocols: add hyprland_surface_v1 implementation (#8877)
Browse files Browse the repository at this point in the history
  • Loading branch information
outfoxxed authored Jan 1, 2025
1 parent 9f3c9ac commit dde3e08
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 15 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protocols/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 4 additions & 1 deletion src/desktop/WLSurface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,4 +119,4 @@ class CWLSurface {
} listeners;

friend class CPointerConstraint;
};
};
4 changes: 4 additions & 0 deletions src/managers/ProtocolManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -159,6 +160,7 @@ CProtocolManager::CProtocolManager() {
PROTO::singlePixel = std::make_unique<CSinglePixelProtocol>(&wp_single_pixel_buffer_manager_v1_interface, 1, "SinglePixel");
PROTO::securityContext = std::make_unique<CSecurityContextProtocol>(&wp_security_context_manager_v1_interface, 1, "SecurityContext");
PROTO::ctm = std::make_unique<CHyprlandCTMControlProtocol>(&hyprland_ctm_control_manager_v1_interface, 1, "CTMControl");
PROTO::hyprlandSurface = std::make_unique<CHyprlandSurfaceProtocol>(&hyprland_surface_manager_v1_interface, 1, "HyprlandSurface");

for (auto const& b : g_pCompositor->m_pAqBackend->getImplementations()) {
if (b->type() != Aquamarine::AQ_BACKEND_DRM)
Expand Down Expand Up @@ -232,6 +234,7 @@ CProtocolManager::~CProtocolManager() {
PROTO::singlePixel.reset();
PROTO::securityContext.reset();
PROTO::ctm.reset();
PROTO::hyprlandSurface.reset();

PROTO::lease.reset();
PROTO::sync.reset();
Expand Down Expand Up @@ -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,
Expand Down
111 changes: 111 additions & 0 deletions src/protocols/HyprlandSurface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "HyprlandSurface.hpp"
#include "../desktop/WLSurface.hpp"
#include "../render/Renderer.hpp"
#include "core/Compositor.hpp"
#include "hyprland-surface-v1.hpp"

CHyprlandSurface::CHyprlandSurface(SP<CHyprlandSurfaceV1> resource, SP<CWLSurfaceResource> surface) : m_pSurface(surface) {
setResource(std::move(resource));
}

bool CHyprlandSurface::good() const {
return m_pResource->resource();
}

void CHyprlandSurface::setResource(SP<CHyprlandSurfaceV1> resource) {
m_pResource = std::move(resource);

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);

if (!m_pResource)
PROTO::hyprlandSurface->destroySurface(this);
}
});

listeners.surfaceDestroyed = m_pSurface->events.destroy.registerListener([this](std::any data) {
if (!m_pResource)
PROTO::hyprlandSurface->destroySurface(this);
});
}

void CHyprlandSurface::destroy() {
m_pResource.reset();
m_fOpacity = 1.F;

if (!m_pSurface)
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<CHyprlandSurfaceManagerV1>(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<CWLSurfaceResource> surface) {
CHyprlandSurface* hyprlandSurface = nullptr;
auto iter = std::find_if(m_mSurfaces.begin(), m_mSurfaces.end(), [&](const auto& entry) { return entry.second->m_pSurface == surface; });

if (iter != m_mSurfaces.end()) {
if (iter->second->m_pResource) {
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;
} else {
iter->second->setResource(makeShared<CHyprlandSurfaceV1>(manager->client(), manager->version(), id));
hyprlandSurface = iter->second.get();
}
} else {
hyprlandSurface = m_mSurfaces.emplace(surface, std::make_unique<CHyprlandSurface>(makeShared<CHyprlandSurfaceV1>(manager->client(), manager->version(), id), surface))
.first->second.get();
}

if (!hyprlandSurface->good()) {
manager->noMemory();
m_mSurfaces.erase(surface);
}
}
54 changes: 54 additions & 0 deletions src/protocols/HyprlandSurface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <memory>
#include <vector>
#include <unordered_map>
#include "WaylandProtocol.hpp"
#include "hyprland-surface-v1.hpp"
#include "../helpers/signal/Signal.hpp"

class CWLSurfaceResource;
class CHyprlandSurfaceProtocol;

class CHyprlandSurface {
public:
CHyprlandSurface(SP<CHyprlandSurfaceV1> resource, SP<CWLSurfaceResource> surface);

bool good() const;
void setResource(SP<CHyprlandSurfaceV1> resource);

private:
SP<CHyprlandSurfaceV1> m_pResource;
WP<CWLSurfaceResource> m_pSurface;
float m_fOpacity = 1.0;

void destroy();

struct {
CHyprSignalListener surfaceCommitted;
CHyprSignalListener surfaceDestroyed;
} listeners;

friend class CHyprlandSurfaceProtocol;
};

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<CWLSurfaceResource> surface);

std::vector<UP<CHyprlandSurfaceManagerV1>> m_vManagers;
std::unordered_map<WP<CWLSurfaceResource>, UP<CHyprlandSurface>> m_mSurfaces;

friend class CHyprlandSurface;
};

namespace PROTO {
inline UP<CHyprlandSurfaceProtocol> hyprlandSurface;
}
7 changes: 4 additions & 3 deletions src/render/OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,8 @@ bool CHyprOpenGLImpl::shouldUseNewBlurOptimizations(PHLLS pLayer, PHLWINDOW pWin
return false;
}

void CHyprOpenGLImpl::renderTextureWithBlur(SP<CTexture> tex, CBox* pBox, float a, SP<CWLSurfaceResource> pSurface, int round, bool blockBlurOptimization, float blurA) {
void CHyprOpenGLImpl::renderTextureWithBlur(SP<CTexture> tex, CBox* pBox, float a, SP<CWLSurfaceResource> 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<Hyprlang::INT>("decoration:no_blur_on_oversized");
Expand Down Expand Up @@ -2126,7 +2127,7 @@ void CHyprOpenGLImpl::renderTextureWithBlur(SP<CTexture> 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);
Expand All @@ -2137,7 +2138,7 @@ void CHyprOpenGLImpl::renderTextureWithBlur(SP<CTexture> 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);
Expand Down
3 changes: 2 additions & 1 deletion src/render/OpenGL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ class CHyprOpenGLImpl {
void renderTexture(SP<CTexture>, CBox*, float a, int round = 0, bool discardActive = false, bool allowCustomUV = false);
void renderTextureWithDamage(SP<CTexture>, CBox*, const CRegion& damage, float a, int round = 0, bool discardActive = false, bool allowCustomUV = false,
SP<CSyncTimeline> waitTimeline = nullptr, uint64_t waitPoint = 0);
void renderTextureWithBlur(SP<CTexture>, CBox*, float a, SP<CWLSurfaceResource> pSurface, int round = 0, bool blockBlurOptimization = false, float blurA = 1.f);
void renderTextureWithBlur(SP<CTexture>, CBox*, float a, SP<CWLSurfaceResource> 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 */);
Expand Down
13 changes: 7 additions & 6 deletions src/render/pass/SurfacePassElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion subprojects/hyprland-protocols

0 comments on commit dde3e08

Please sign in to comment.