Files
hypr-chrome/src/ChromePassElement.cpp
T
darmanandClaude Opus 5 74ec7c89f0 Stop position-only animations from invalidating the texture caches
ChromePassElement::draw() rounded its device-space box with CBox::round(),
which derives the size from the two rounded corners - round(x + w) -
round(x) - making the rounded width and height a function of the
*position's* fractional part. A box merely sliding at a constant size
therefore has its size flip by a pixel every few frames.

Every one of those flips misses the cachedTexSize check in
GetBorderTexture, GetShadowTexture and GetTitleTexture alike, and each
miss is a full cairo re-render plus a fresh GPU texture allocation and
upload for content that did not change appearance at all. That is exactly
what a workspace switch, a window move, or any other position-only
animation does - every frame, for every window on screen - and it is why
those animations stutter. Measured against hyprutils, a sliding window
resized nothing yet rebuilt its textures on 18-40% of frames at scale
1.0/1.25/1.5/1.6 (integer scales happened to be stable); rounding the
size on its own takes all of those to zero.

What round()'s coupling buys is a far edge landing on the same device
pixel as an adjacent box's near edge. Nothing abuts this box - it is a
free-floating decoration drawn over everything - so there is no seam here
to keep closed.

damageEntire() gains a one-pixel margin to match: with position and size
now rounded separately, the drawn box's far edge can land up to a device
pixel past where the logical damage box scales to.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-02 14:16:26 +02:00

163 lines
8.1 KiB
C++

#include "ChromePassElement.hpp"
#include "Globals.hpp"
#include "ChromeDecoration.hpp"
#include "ChromeDecorationGeometry.hpp"
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/desktop/view/Window.hpp>
#include <hyprland/src/render/Renderer.hpp>
#include <hyprland/src/render/Texture.hpp>
#include <hyprland/src/render/pass/TexPassElement.hpp>
ChromePassElement::ChromePassElement(const SData& data_) : data(data_) {}
std::vector<UP<IPassElement>> ChromePassElement::draw() {
const auto monitor = g_pHyprRenderer->m_renderData.pMonitor.lock();
if (!monitor)
return {};
const auto window = data.decoration->GetOwner();
if (!window)
return {};
auto box = data.decoration->FullDecorationExtentGlobal();
box.translate(-monitor->m_position).scale(monitor->m_scale);
// Round the position and the size independently, rather than via
// CBox::round(). That derives the size from the two *rounded corners*
// (round(x + w) - round(x)), which makes it a function of the position's
// fractional part - so a box that is merely sliding, at a perfectly
// constant size, has its rounded w/h flip by a pixel every few frames.
// Every one of those flips misses the caches in GetBorderTexture /
// GetShadowTexture / GetTitleTexture, each miss being a full cairo
// re-render plus a GPU re-upload of a texture that didn't actually change
// appearance - which is precisely what a workspace switch, a window move,
// or any other position-only animation does, every frame, for every window
// on screen. Rounded on its own, the size stays a pure function of the
// window's own size and can't be perturbed by translation at all.
//
// What round()'s coupling buys is a far edge that lands on the same device
// pixel as an adjacent box's near edge. Nothing abuts this box - it's a
// free-floating decoration drawn over everything - so there is no seam here
// to keep closed.
box = CBox{box.pos().round(), box.size().round()};
if (box.w < 1 || box.h < 1)
return {};
// Chamfer amount is inferred from the window's own border rounding, in
// device pixels, so the frame's cut corners track the window's rounding
// live (config reload, per-window rules, animations).
const float chamferPx = window->rounding() * monitor->m_scale;
const float extentPx = static_cast<float>(PluginState->config.extent->value()) * monitor->m_scale;
const bool isActive = g_pCompositor->isWindowActive(window);
// When follow_hyprland_border_color is set, track Hyprland's own
// general:col.active_border / col.inactive_border (already resolved per
// focus state and animated by the compositor), stops and angle included.
// Otherwise (or if Hyprland reports no border color at all) fall back to
// our own active_color/inactive_color config, which take the same
// gradient syntax.
const auto& hyprlandBorder = window->m_realBorderColor;
const bool followHyprlandColor = PluginState->config.followHyprlandBorderColor->value() && !hyprlandBorder.m_colors.empty();
const auto gradient = ChromeGradient::From(followHyprlandColor
? hyprlandBorder
: (isActive ? PluginState->config.activeColor->value() : PluginState->config.inactiveColor->value()));
const float borderAlpha = gradient.FirstAlpha();
const float glowPx = static_cast<float>(PluginState->config.glowSize->value()) * monitor->m_scale;
const float glowStrength = static_cast<float>(PluginState->config.glowStrength->value());
const float titleBarHeightPx = static_cast<float>(PluginState->config.titlebarHeight->value()) * monitor->m_scale;
const float textSizePx = static_cast<float>(PluginState->config.titlebarTextSize->value()) * monitor->m_scale;
const std::string fontFamily = PluginState->config.titlebarFont->value();
// Base geometry (everything but the title bar plateau's own width - that
// depends on the rendered title text, which can only be measured by
// actually rendering it). MaxTitleBarWidth() bounds that render so a long
// title gets ellipsized rather than growing the plateau into the
// self-intersection GetBorderTexture's cairo path would otherwise risk.
const auto baseGeo = ChromeDecorationGeometry::ComputeBase({box.w, box.h}, extentPx, chamferPx, titleBarHeightPx);
// Padding around the title text, inside the plateau.
const float titlePadding = textSizePx * 1.2F;
const int measureMaxWidthPx = static_cast<int>(baseGeo.MaxTitleBarWidth() - baseGeo.barX0 - titlePadding * 2.F);
const auto titleTex = measureMaxWidthPx > 0
? data.decoration->GetTitleTexture(window->m_title, textSizePx, borderAlpha, fontFamily, measureMaxWidthPx, isActive)
: nullptr;
const float texW = (titleTex && titleTex->ok()) ? static_cast<float>(titleTex->m_size.x) : 0.F;
// Plateau grows to fit the measured text + padding, floored at 20% of the
// total width regardless of how short (or absent) the title is.
const float desiredTitleBarWidthPx = baseGeo.barX0 + texW + titlePadding * 2.F;
const auto geo = baseGeo.WithTitleBarWidth(desiredTitleBarWidthPx);
// In fullSpan mode the outer path's shape is fixed regardless of text
// width (see GetBorderTexture), so pass a value that doesn't fluctuate
// with title length - otherwise every title change would needlessly
// regenerate an identical border texture.
const float borderTitleBarWidthPx = geo.fullSpan ? geo.MaxTitleBarWidth() : geo.titleBarWidth;
const float outlinePx = static_cast<float>(PluginState->config.outlineSize->value()) * monitor->m_scale;
const CHyprColor outlineColor{static_cast<uint64_t>(PluginState->config.outlineColor->value())};
const auto tex = data.decoration->GetBorderTexture({box.w, box.h}, extentPx, chamferPx, gradient, titleBarHeightPx, borderTitleBarWidthPx, glowPx, glowStrength, outlinePx, outlineColor);
if (!tex || !tex->ok())
return {};
std::vector<UP<IPassElement>> children;
// Emitted first so it lands under the frame - which matters, because the
// shadow does run under the frame's own outline (only the window's interior
// is cleared out of it), and the frame is what hides that cut.
const float shadowSizePx = static_cast<float>(PluginState->config.shadowSize->value()) * monitor->m_scale;
const auto configOffset = PluginState->config.shadowOffset->value();
const Vector2D shadowOffsetPx = {configOffset.x * monitor->m_scale, configOffset.y * monitor->m_scale};
const CHyprColor shadowColor{static_cast<uint64_t>(PluginState->config.shadowColor->value())};
if (const auto shadowTex = data.decoration->GetShadowTexture({box.w, box.h}, extentPx, chamferPx, titleBarHeightPx, borderTitleBarWidthPx, shadowSizePx, shadowColor, shadowOffsetPx);
shadowTex && shadowTex->ok()) {
const float margin = ChromeDecoration::ShadowMarginPx(shadowSizePx, shadowOffsetPx);
CTexPassElement::SRenderData shadowData;
shadowData.tex = shadowTex;
shadowData.box = CBox{
box.x - margin + shadowOffsetPx.x, box.y - margin + shadowOffsetPx.y,
box.w + 2.0 * margin, box.h + 2.0 * margin,
};
shadowData.a = data.alpha;
children.emplace_back(makeUnique<CTexPassElement>(shadowData));
}
CTexPassElement::SRenderData texData;
texData.tex = tex;
texData.box = box;
texData.a = data.alpha;
children.emplace_back(makeUnique<CTexPassElement>(texData));
if (titleTex && titleTex->ok() && titleTex->m_size.x > 0 && titleTex->m_size.y > 0) {
const float texH = static_cast<float>(titleTex->m_size.y);
const float localX = geo.barX0 + titlePadding;
const float localY = (geo.barBottom - texH) / 2.F;
CTexPassElement::SRenderData titleData;
titleData.tex = titleTex;
titleData.box = CBox{box.x + localX, box.y + localY, texW, texH};
titleData.a = data.alpha;
children.emplace_back(makeUnique<CTexPassElement>(titleData));
}
return children;
}
bool ChromePassElement::needsLiveBlur() { return false; }
bool ChromePassElement::needsPrecomputeBlur() { return false; }
std::optional<CBox> ChromePassElement::boundingBox() {
const auto monitor = g_pHyprRenderer->m_renderData.pMonitor.lock();
if (!monitor)
return std::nullopt;
return data.decoration->FullDecorationExtentGlobal()
.translate(-monitor->m_position)
.expand(4 + ChromeDecoration::ShadowMarginLogical());
}