From 36fde146178ea3d4e9830f738cb454c910c49562 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sun, 15 Nov 2026 19:42:13 +0100 Subject: [PATCH] window: preserve fractional drawable bounds during layout Size Wayland roots from the allocated pixel extent without rounding back to whole logical coordinates. Record configured root bounds through the positioner and reuse them during reflow, preserving explicit embedded surface bounds as well. Add layout and framebuffer regressions covering fractional scales, resize, repeated reflow, and partial edge damage. Verified all 98 tests and hyprlauncher borders at 2880x1920 with 4/3 scaling. --- CMakeLists.txt | 1 + src/layout/Positioner.cpp | 16 ++--- src/window/EmbeddedSurface.cpp | 3 +- src/window/IWaylandWindow.cpp | 4 +- tests/EmbeddedRenderer.cpp | 92 +++++++++++++++++++++++++++ tests/unit/window/EmbeddedSurface.cpp | 26 ++++++++ 6 files changed, 129 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 540b8e6..a296c88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -211,6 +211,7 @@ if(BUILD_TESTING) gtest_discover_tests(hyprtoolkit_tests) add_executable(embeddedRendererTest "tests/EmbeddedRenderer.cpp") + target_include_directories(embeddedRendererTest PRIVATE "./src" "./protocols") target_link_libraries(embeddedRendererTest PRIVATE hyprtoolkit GTest::gtest_main OpenGL::EGL OpenGL::OpenGL PkgConfig::deps) gtest_discover_tests(embeddedRendererTest) diff --git a/src/layout/Positioner.cpp b/src/layout/Positioner.cpp index cfdf22e..c3b90d5 100644 --- a/src/layout/Positioner.cpp +++ b/src/layout/Positioner.cpp @@ -26,8 +26,8 @@ void CPositioner::position(SP element, const CBox& box, const Hyprutil } void CPositioner::positionChildren(SP element, const SRepositionData& data) { - const auto C = element->impl->children; - const auto BOX = element->impl->position.copy().translate(data.offset); + const auto C = element->impl->children; + const auto BOX = element->impl->position.copy().translate(data.offset); const auto CLIP_BOX = CBox{BOX.pos(), {data.growX ? 99999999 : BOX.width, data.growY ? 99999999 : BOX.height}}; // position children according to how they wanna be positioned @@ -80,15 +80,9 @@ void CPositioner::positionChildren(SP element, const SRepositionData& void CPositioner::repositionNeeded(SP element, bool force) { if (!element->impl->parent) { - // root el likely, check - if (!element->impl->window || element != element->impl->window->m_rootElement) { - initElementIfNeeded(element); - position(element, element->impl->positionerData->baseBox); - return; - } - - // otherwise, max box - position(element, {{}, (element->impl->window->pixelSize() / element->impl->window->scale()).round()}); + // Reuse the configured bounds, including fractional drawable extents. + initElementIfNeeded(element); + position(element, element->impl->positionerData->baseBox); return; } diff --git a/src/window/EmbeddedSurface.cpp b/src/window/EmbeddedSurface.cpp index fc8b4f8..428522a 100644 --- a/src/window/EmbeddedSurface.cpp +++ b/src/window/EmbeddedSurface.cpp @@ -7,6 +7,7 @@ #include "../core/BackendContext.hpp" #include "../core/EmbeddedBackend.hpp" #include "../element/Element.hpp" +#include "../layout/Positioner.hpp" #include "../renderer/Renderer.hpp" using namespace Hyprtoolkit; @@ -65,7 +66,7 @@ void CEmbeddedSurface::resize(const Vector2D& logicalSize, const Vector2D& pixel m_pixelSize = pixelSize; m_scale = scale; m_damageRing.setSize(pixelSize); - m_rootElement->reposition({{}, logicalSize}); + g_positioner->position(m_rootElement, {{}, logicalSize}); damageEntire(); } diff --git a/src/window/IWaylandWindow.cpp b/src/window/IWaylandWindow.cpp index 99cc888..3e700b5 100644 --- a/src/window/IWaylandWindow.cpp +++ b/src/window/IWaylandWindow.cpp @@ -4,6 +4,7 @@ #include #include "../element/Element.hpp" +#include "../layout/Positioner.hpp" #include "../core/platforms/WaylandPlatform.hpp" #include "../core/InternalBackend.hpp" #include "../core/BackendContext.hpp" @@ -67,7 +68,8 @@ void IWaylandWindow::configure(const Vector2D& size, uint32_t serial) { resizeSwapchain(m_waylandState.size); damageEntire(); - m_rootElement->reposition({0, 0, m_waylandState.logicalSize.x, m_waylandState.logicalSize.y}); + // Keep the drawable extent fractional so pixel rounding cannot put borders outside the buffer. + g_positioner->position(m_rootElement, {{}, m_waylandState.size / m_fractionalScale}); } void IWaylandWindow::resizeSwapchain(const Vector2D& pixelSize) { diff --git a/tests/EmbeddedRenderer.cpp b/tests/EmbeddedRenderer.cpp index da0798b..89f3445 100644 --- a/tests/EmbeddedRenderer.cpp +++ b/tests/EmbeddedRenderer.cpp @@ -8,6 +8,12 @@ #include #include #include +#include + +#include "window/EmbeddedSurface.hpp" + +#include +#include using namespace Hyprtoolkit; using namespace Hyprutils::Memory; @@ -182,3 +188,89 @@ TEST_F(CEmbeddedRendererTest, rendersAndRestoresHostState) { backend->destroy(); backend.reset(); } + +TEST_F(CEmbeddedRendererTest, fractionalBordersSurviveReflowAndEdgeDamage) { + using namespace Hyprutils::Math; + + IEmbeddedBackend::SCreationData creationData; + creationData.eventLoop = makeShared(); + auto backend = IEmbeddedBackend::create(creationData); + ASSERT_TRUE(backend); + auto surface = backend->createSurface(); + ASSERT_TRUE(surface); + auto window = dynamicPointerCast(surface); + ASSERT_TRUE(window); + + GLuint framebuffer = 0, texture = 0; + glGenFramebuffers(1, &framebuffer); + glGenTextures(1, &texture); + Hyprutils::Utils::CScopeGuard cleanup{[&] { + glDeleteFramebuffers(1, &framebuffer); + glDeleteTextures(1, &texture); + window.reset(); + surface.reset(); + backend->destroy(); + }}; + + surface->rootElement()->addChild(CRectangleBuilder::begin() + ->color([] { return CHyprColor{1.F, 0.F, 0.F, 1.F}; }) + ->borderColor([] { return CHyprColor{0.F, 1.F, 0.F, 1.F}; }) + ->borderThickness(1) + ->rounding(8) + ->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_PERCENT, {1.F, 1.F}}) + ->commence()); + + for (const float scale : {1.F, 1.25F, 4.F / 3.F, 1.5F, 2.F}) { + for (const auto size : {Vector2D{400, 260}, Vector2D{401, 259}, Vector2D{398, 261}}) { + SCOPED_TRACE(::testing::Message() << scale << ", " << size.x << "x" << size.y); + const auto pixels = (size * scale).floor(); + const auto logical = pixels / scale; + const int width = pixels.x, height = pixels.y; + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); + ASSERT_EQ(glCheckFramebufferStatus(GL_FRAMEBUFFER), GL_FRAMEBUFFER_COMPLETE); + surface->resize(logical, pixels, scale); + + for (int frame = 0; frame < 3; ++frame) { + SCOPED_TRACE(frame); + if (frame == 1) + surface->rootElement()->forceReposition(); + else if (frame == 2) { + // Erase the framebuffer edges, then request only logical edge damage. + glEnable(GL_SCISSOR_TEST); + glClearColor(0.F, 0.F, 0.F, 0.F); + for (const auto box : {CBox{0, 0, pixels.x, 1}, CBox{0, pixels.y - 1, pixels.x, 1}, CBox{0, 0, 1, pixels.y}, CBox{pixels.x - 1, 0, 1, pixels.y}}) { + glScissor(box.x, box.y, box.w, box.h); + glClear(GL_COLOR_BUFFER_BIT); + } + glDisable(GL_SCISSOR_TEST); + + const auto w = std::ceil(logical.x), h = std::ceil(logical.y); + CRegion edges{CBox{0, 0, w, h}}; + edges.subtract(CBox{2, 2, w - 4, h - 4}); + window->damage(std::move(edges)); + } + + surface->render(frame == 0 ? 0 : 1); + + for (const auto point : {Vector2D{width / 2, 0}, Vector2D{width / 2, height - 1}, Vector2D{0, height / 2}, Vector2D{width - 1, height / 2}}) { + SCOPED_TRACE(::testing::Message() << point.x << ", " << point.y); + std::array pixel = {}; + glReadPixels(point.x, point.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel.data()); + EXPECT_LT(pixel[0], 10); + EXPECT_GT(pixel[1], 240); + EXPECT_LT(pixel[2], 10); + EXPECT_GT(pixel[3], 240); + } + + std::array center = {}; + glReadPixels(width / 2, height / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, center.data()); + EXPECT_GT(center[0], 240); + EXPECT_LT(center[1], 10); + EXPECT_GT(center[3], 240); + } + } + } +} diff --git a/tests/unit/window/EmbeddedSurface.cpp b/tests/unit/window/EmbeddedSurface.cpp index f1339e6..feec881 100644 --- a/tests/unit/window/EmbeddedSurface.cpp +++ b/tests/unit/window/EmbeddedSurface.cpp @@ -75,6 +75,32 @@ TEST_F(CEmbeddedSurfaceTest, frameRequestsAreCoalesced) { EXPECT_EQ(m_surface->scale(), 2.F); } +TEST_F(CEmbeddedSurfaceTest, rootReflowPreservesConfiguredBounds) { + const auto child = CNullBuilder::begin()->size({CDynamicSize::HT_SIZE_PERCENT, CDynamicSize::HT_SIZE_PERCENT, {1, 1}})->commence(); + m_surface->rootElement()->addChild(child); + + for (const float scale : {1.F, 1.25F, 4.F / 3.F, 1.5F, 2.F}) { + for (const auto size : {Vector2D{400, 260}, Vector2D{401, 259}, Vector2D{398, 261}}) { + const auto pixels = (size * scale).floor(); + // Embedded hosts supply logical bounds explicitly; Wayland uses the drawable extent. + for (const auto logical : {size, pixels / scale}) { + SCOPED_TRACE(::testing::Message() << scale << ", " << logical.x << "x" << logical.y); + m_surface->resize(logical, pixels, scale); + EXPECT_EQ(child->size(), logical); + + for (int i = 0; i < 3; ++i) { + m_surface->rootElement()->forceReposition(); + EXPECT_EQ(m_surface->rootElement()->size(), logical); + EXPECT_EQ(child->size(), logical); + if (logical != pixels / scale) + continue; + EXPECT_EQ((CBox{{}, child->size()}.scale(scale).round().size()), pixels); + } + } + } + } +} + TEST_F(CEmbeddedSurfaceTest, mouseInputCanBeDisabled) { const auto element = CEmbeddedTestElement::create(); int moves = 0;