From 4793d6f26a0567f46c3f51dc6734b76dadc27af5 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Tue, 25 Nov 2025 02:10:18 +0100 Subject: [PATCH] slight optimization --- .../Crafter.Graphics-UiElement.cpp | 14 -------------- ...Crafter.Graphics-UiElementBufferBuffer.cpp | 19 +++++++++++++++++++ ...ter.Graphics-UiElementBufferBufferBase.cpp | 5 ++--- .../Crafter.Graphics-UiElementTextBuffer.cpp | 4 +--- .../Crafter.Graphics-Window_wayland.cpp | 15 ++++++++++++--- interfaces/Crafter.Graphics-UiElement.cppm | 7 +++++-- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/implementations/Crafter.Graphics-UiElement.cpp b/implementations/Crafter.Graphics-UiElement.cpp index 5b064da..7f539eb 100644 --- a/implementations/Crafter.Graphics-UiElement.cpp +++ b/implementations/Crafter.Graphics-UiElement.cpp @@ -40,18 +40,4 @@ UiElementMouse::UiElementMouse(std::int_fast32_t anchorX, std::int_fast32_t anch UiElementBuffer::UiElementBuffer(std::uint_fast32_t width, std::uint_fast32_t height) : width(width), height(height) { -} - -void UiElement::UpdatePosition(WindowFramebuffer& window) { - window.ScaleElement(transform); - for(UiElement* child : children) { - child->UpdatePosition(window, *this); - } -} - -void UiElement::UpdatePosition(WindowFramebuffer& window, UiElement& parent) { - window.ScaleElement(transform, parent.transform); - for(UiElement* child : children) { - UpdatePosition(window, *child); - } } \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementBufferBuffer.cpp b/implementations/Crafter.Graphics-UiElementBufferBuffer.cpp index 3e9cdc3..e1eafdf 100644 --- a/implementations/Crafter.Graphics-UiElementBufferBuffer.cpp +++ b/implementations/Crafter.Graphics-UiElementBufferBuffer.cpp @@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA module Crafter.Graphics:UiElementBufferBuffer_impl; import :UiElement; +import :Window; import std; using namespace Crafter; @@ -30,4 +31,22 @@ UiElementBufferBuffer::UiElementBufferBuffer(std::int_fast32_t anchorX, std::int UiElementBufferBuffer::UiElementBufferBuffer(std::uint_fast32_t width, std::uint_fast32_t height, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : UiElement(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementBufferBufferBase(width, height){ +} + +void UiElementBufferBuffer::UpdatePosition(WindowFramebuffer& window) { + window.ScaleElement(transform); + scaled.resize(transform.scaled.width*transform.scaled.height); + CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height); + for(UiElement* child : children) { + child->UpdatePosition(window, *this); + } +} + +void UiElementBufferBuffer::UpdatePosition(WindowFramebuffer& window, UiElement& parent) { + window.ScaleElement(transform, parent.transform); + scaled.resize(transform.scaled.width*transform.scaled.height); + CopyNearestNeighbour(scaled.data(),transform.scaled.width, transform.scaled.height); + for(UiElement* child : children) { + UpdatePosition(window, *child); + } } \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementBufferBufferBase.cpp b/implementations/Crafter.Graphics-UiElementBufferBufferBase.cpp index 3f8e946..2cc63b4 100644 --- a/implementations/Crafter.Graphics-UiElementBufferBufferBase.cpp +++ b/implementations/Crafter.Graphics-UiElementBufferBufferBase.cpp @@ -1,12 +1,11 @@ /* Crafter®.Graphics Copyright (C) 2025 Catcrafts® -Catcrafts.net +catcrafts.net This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 3.0 of the License, or (at your option) any later version. +License version 3.0 as published by the Free Software Foundation; This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of diff --git a/implementations/Crafter.Graphics-UiElementTextBuffer.cpp b/implementations/Crafter.Graphics-UiElementTextBuffer.cpp index 2740106..5a8ff4f 100644 --- a/implementations/Crafter.Graphics-UiElementTextBuffer.cpp +++ b/implementations/Crafter.Graphics-UiElementTextBuffer.cpp @@ -70,9 +70,7 @@ void UiElementTextBuffer::Render(const std::string_view text, float size, Pixel_ for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { - int bufIndex = ((baseline + j + c_y1) * bufferWidth + (x + i + c_x1)); - unsigned char val = bitmap[j * w + i]; - buffer[bufIndex] = {color.r, color.g, color.b, val}; + buffer[(baseline + j + c_y1) * bufferWidth + (x + i + c_x1)] = {color.r, color.g, color.b, bitmap[j * w + i]}; } } diff --git a/implementations/Crafter.Graphics-Window_wayland.cpp b/implementations/Crafter.Graphics-Window_wayland.cpp index cac0a38..6ecd299 100644 --- a/implementations/Crafter.Graphics-Window_wayland.cpp +++ b/implementations/Crafter.Graphics-Window_wayland.cpp @@ -252,21 +252,30 @@ void WindowWayland::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base xdg_wm_base_pong(xdg_wm_base, serial); } +std::chrono::time_point framEnd; + void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) { + auto start = std::chrono::high_resolution_clock::now(); + auto vblank = duration_cast(start - framEnd); wl_callback_destroy(cb); WindowWayland* window = reinterpret_cast(data); - auto start = std::chrono::high_resolution_clock::now(); - if(window->updating) { cb = wl_surface_frame(window->surface); wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window); + auto startUpdate = std::chrono::high_resolution_clock::now(); window->onUpdate.Invoke({start, start-window->lastFrameEnd}); + auto endUpdate = std::chrono::high_resolution_clock::now(); + auto startRender = std::chrono::high_resolution_clock::now(); + window->Render(); + auto endRender = std::chrono::high_resolution_clock::now(); + auto end = std::chrono::high_resolution_clock::now(); + std::cout << std::format("Update: {}, Render: {}, Vblank: {}, Total: {}", duration_cast(endUpdate - startUpdate), duration_cast(endRender - startRender), vblank, duration_cast((endUpdate - startUpdate)+(endRender - startRender)+vblank)) << std::endl; } - window->Render(); + framEnd = std::chrono::high_resolution_clock::now(); window->lastFrameEnd = start; } diff --git a/interfaces/Crafter.Graphics-UiElement.cppm b/interfaces/Crafter.Graphics-UiElement.cppm index bbe3eae..1166dd8 100644 --- a/interfaces/Crafter.Graphics-UiElement.cppm +++ b/interfaces/Crafter.Graphics-UiElement.cppm @@ -46,8 +46,8 @@ export namespace Crafter { UiElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); UiElement(UiElement&) = delete; UiElement& operator=(UiElement&) = delete; - void UpdatePosition(WindowFramebuffer& window); - void UpdatePosition(WindowFramebuffer& window, UiElement& parent); + virtual void UpdatePosition(WindowFramebuffer& window) = 0; + virtual void UpdatePosition(WindowFramebuffer& window, UiElement& parent) = 0; }; class UiElementMouse { @@ -91,6 +91,7 @@ export namespace Crafter { class UiElementBufferBufferBase : public UiElementBuffer { public: std::vector buffer; + std::vector scaled; UiElementBufferBufferBase() = default; UiElementBufferBufferBase(std::uint_fast32_t width, std::uint_fast32_t height); void Create(std::uint_fast32_t width, std::uint_fast32_t height) override; @@ -114,6 +115,8 @@ export namespace Crafter { public: UiElementBufferBuffer(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); UiElementBufferBuffer(std::uint_fast32_t width, std::uint_fast32_t height, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + void UpdatePosition(WindowFramebuffer& window) override; + void UpdatePosition(WindowFramebuffer& window, UiElement& parent) override; };