Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 5 additions & 11 deletions src/layout/Positioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void CPositioner::position(SP<IElement> element, const CBox& box, const Hyprutil
}

void CPositioner::positionChildren(SP<IElement> 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
Expand Down Expand Up @@ -80,15 +80,9 @@ void CPositioner::positionChildren(SP<IElement> element, const SRepositionData&

void CPositioner::repositionNeeded(SP<IElement> 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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/window/EmbeddedSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion src/window/IWaylandWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <hyprtoolkit/element/Null.hpp>

#include "../element/Element.hpp"
#include "../layout/Positioner.hpp"
#include "../core/platforms/WaylandPlatform.hpp"
#include "../core/InternalBackend.hpp"
#include "../core/BackendContext.hpp"
Expand Down Expand Up @@ -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) {
Expand Down
92 changes: 92 additions & 0 deletions tests/EmbeddedRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <hyprtoolkit/core/Timer.hpp>
#include <hyprtoolkit/element/Rectangle.hpp>
#include <hyprtoolkit/window/EmbeddedSurface.hpp>
#include <hyprutils/utils/ScopeGuard.hpp>

#include "window/EmbeddedSurface.hpp"

#include <array>
#include <cmath>

using namespace Hyprtoolkit;
using namespace Hyprutils::Memory;
Expand Down Expand Up @@ -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<CTestEventLoop>();
auto backend = IEmbeddedBackend::create(creationData);
ASSERT_TRUE(backend);
auto surface = backend->createSurface();
ASSERT_TRUE(surface);
auto window = dynamicPointerCast<CEmbeddedSurface>(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<GLubyte, 4> 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<GLubyte, 4> 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);
}
}
}
}
26 changes: 26 additions & 0 deletions tests/unit/window/EmbeddedSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading