slight optimization

This commit is contained in:
Jorijn van der Graaf 2025-11-25 02:10:18 +01:00
commit 4793d6f26a
6 changed files with 39 additions and 25 deletions

View file

@ -41,17 +41,3 @@ 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) { 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);
}
}

View file

@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
module Crafter.Graphics:UiElementBufferBuffer_impl; module Crafter.Graphics:UiElementBufferBuffer_impl;
import :UiElement; import :UiElement;
import :Window;
import std; import std;
using namespace Crafter; using namespace Crafter;
@ -31,3 +32,21 @@ 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){ 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);
}
}

View file

@ -1,12 +1,11 @@
/* /*
Crafter®.Graphics Crafter®.Graphics
Copyright (C) 2025 Catcrafts® Copyright (C) 2025 Catcrafts®
Catcrafts.net catcrafts.net
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License version 3.0 as published by the Free Software Foundation;
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of

View file

@ -70,9 +70,7 @@ void UiElementTextBuffer::Render(const std::string_view text, float size, Pixel_
for (int j = 0; j < h; j++) { for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) { for (int i = 0; i < w; i++) {
int bufIndex = ((baseline + j + c_y1) * bufferWidth + (x + i + c_x1)); buffer[(baseline + j + c_y1) * bufferWidth + (x + i + c_x1)] = {color.r, color.g, color.b, bitmap[j * w + i]};
unsigned char val = bitmap[j * w + i];
buffer[bufIndex] = {color.r, color.g, color.b, val};
} }
} }

View file

@ -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); xdg_wm_base_pong(xdg_wm_base, serial);
} }
std::chrono::time_point<std::chrono::high_resolution_clock> framEnd;
void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) 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<std::chrono::milliseconds>(start - framEnd);
wl_callback_destroy(cb); wl_callback_destroy(cb);
WindowWayland* window = reinterpret_cast<WindowWayland*>(data); WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
auto start = std::chrono::high_resolution_clock::now();
if(window->updating) { if(window->updating) {
cb = wl_surface_frame(window->surface); cb = wl_surface_frame(window->surface);
wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window); 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}); 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<std::chrono::milliseconds>(endUpdate - startUpdate), duration_cast<std::chrono::milliseconds>(endRender - startRender), vblank, duration_cast<std::chrono::milliseconds>((endUpdate - startUpdate)+(endRender - startRender)+vblank)) << std::endl;
} }
window->Render();
framEnd = std::chrono::high_resolution_clock::now();
window->lastFrameEnd = start; window->lastFrameEnd = start;
} }

View file

@ -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(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(UiElement&) = delete;
UiElement& operator=(UiElement&) = delete; UiElement& operator=(UiElement&) = delete;
void UpdatePosition(WindowFramebuffer& window); virtual void UpdatePosition(WindowFramebuffer& window) = 0;
void UpdatePosition(WindowFramebuffer& window, UiElement& parent); virtual void UpdatePosition(WindowFramebuffer& window, UiElement& parent) = 0;
}; };
class UiElementMouse { class UiElementMouse {
@ -91,6 +91,7 @@ export namespace Crafter {
class UiElementBufferBufferBase : public UiElementBuffer { class UiElementBufferBufferBase : public UiElementBuffer {
public: public:
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer; std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
std::vector<Pixel_BU8_GU8_RU8_AU8> scaled;
UiElementBufferBufferBase() = default; UiElementBufferBufferBase() = default;
UiElementBufferBufferBase(std::uint_fast32_t width, std::uint_fast32_t height); UiElementBufferBufferBase(std::uint_fast32_t width, std::uint_fast32_t height);
void Create(std::uint_fast32_t width, std::uint_fast32_t height) override; void Create(std::uint_fast32_t width, std::uint_fast32_t height) override;
@ -114,6 +115,8 @@ export namespace Crafter {
public: 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::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); 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;
}; };