optimization

This commit is contained in:
Jorijn van der Graaf 2025-11-26 00:00:50 +01:00
commit bf6793e41d
5 changed files with 51 additions and 43 deletions

View file

@ -31,16 +31,16 @@ import std;
using namespace Crafter;
RenderingElement::RenderingElement() : Transform() {
RenderingElement::RenderingElement(bool opaque) : Transform(), opaque(opaque) {
}
RenderingElement::RenderingElement(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) : Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
RenderingElement::RenderingElement(bool opaque, 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) : Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), opaque(opaque) {
}
RenderingElement::RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, 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) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
RenderingElement::RenderingElement(bool opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, 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) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), opaque(opaque) {
}
@ -61,16 +61,26 @@ void RenderingElement::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uin
}
void RenderingElement::UpdatePosition(Window& window) {
std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this);
bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
if(oldWidth != scaled.width || oldHeight && scaled.height) {
bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
}
for(Transform* child : children) {
child->UpdatePosition(window, *this);
}
}
void RenderingElement::UpdatePosition(Window& window, Transform& parent) {
std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this, parent);
if(oldWidth != scaled.width || oldHeight && scaled.height) {
bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
}
bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
for(Transform* child : children) {

View file

@ -178,22 +178,37 @@ void WindowWayland::RenderElement(Transform* transform) {
std::uint_fast32_t src_width = element->scaled.width;
std::uint_fast32_t src_height = element->scaled.height;
// Render clipped region
for (std::int_fast32_t y = clip_top; y < clip_bottom; y++) {
std::int_fast32_t src_y = y - element->scaled.y;
for (std::int_fast32_t x = clip_left; x < clip_right; x++) {
std::int_fast32_t src_x = x - element->scaled.x;
// If element is opaque, we can simply copy pixels without blending
if (element->opaque) {
// Render clipped region
for (std::int_fast32_t y = clip_top; y < clip_bottom; y++) {
std::int_fast32_t src_y = y - element->scaled.y;
// Bounds check for source buffer
if (src_x >= 0 && src_x < static_cast<std::int_fast32_t>(src_width) && src_y >= 0 && src_y < static_cast<std::int_fast32_t>(src_height)) {
for (std::int_fast32_t x = clip_left; x < clip_right; x++) {
std::int_fast32_t src_x = x - element->scaled.x;
// Get pixel indices
std::uint_fast32_t dst_idx = y * width + x;
std::uint_fast32_t src_idx = src_y * src_width + src_x;
// Bounds check for source buffer
if (src_x >= 0 && src_x < static_cast<std::int_fast32_t>(src_width) && src_y >= 0 && src_y < static_cast<std::int_fast32_t>(src_height)) {
// Direct copy for opaque elements (skip blending)
framebuffer[y * width + x] = src_buffer[src_y * src_width + src_x];
}
}
}
} else {
// Render clipped region with blending for non-opaque elements
for (std::int_fast32_t y = clip_top; y < clip_bottom; y++) {
std::int_fast32_t src_y = y - element->scaled.y;
for (std::int_fast32_t x = clip_left; x < clip_right; x++) {
std::int_fast32_t src_x = x - element->scaled.x;
// Blend pixels
blend_pixel_optimized(framebuffer[dst_idx], src_buffer[src_idx]);
// Bounds check for source buffer
if (src_x >= 0 && src_x < static_cast<std::int_fast32_t>(src_width) && src_y >= 0 && src_y < static_cast<std::int_fast32_t>(src_height)) {
// Blend pixels
blend_pixel_optimized(framebuffer[y * width + x], src_buffer[src_y * src_width + src_x]);
}
}
}
}