Crafter.Graphics/implementations/Crafter.Graphics-WindowFramebuffer_wayland.cpp
2025-11-22 20:58:42 +01:00

107 lines
No EOL
4 KiB
C++

/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
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 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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#include <wayland-client.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
module Crafter.Graphics:WindowFramebuffer_wayland_impl;
import :WindowFramebuffer;
import :Window;
import :UiElement;
import std;
import :Types;
import :Shm;
import Crafter.Event;
namespace Crafter {
void WindowFramebuffer::Create(std::uint_fast32_t width, std::uint_fast32_t height) {
this->width = width;
this->height = height;
int stride = width * 4;
int size = stride * height;
int fd = create_shm_file(size);
if (fd < 0) {
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
}
framebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (framebuffer == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
}
wl_shm_pool *pool = wl_shm_create_pool(window.shm, fd, size);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool);
close(fd);
if (buffer == NULL) {
exit(EXIT_FAILURE);
}
wl_surface_attach(window.surface, buffer, 0, 0);
wl_surface_commit(window.surface);
}
void WindowFramebuffer::Resize(std::uint_fast32_t width, std::uint_fast32_t height) {
}
void WindowFramebuffer::Destroy() {
wl_buffer_destroy(buffer);
}
void WindowFramebuffer::Render() {
std::vector<const UiElementBuffer*> drawOrder;
drawOrder.reserve(elements.size());
for (const UiElementBuffer* e : elements) drawOrder.push_back(e);
std::sort(drawOrder.begin(), drawOrder.end(), [](const UiElementBuffer* a, const UiElementBuffer* b){ return a->parent.z < b->parent.z; });
for(const UiElementBuffer* element : drawOrder) {
ScaleData data = ScaleElement(element->parent);
std::vector<Pixel_BU8_GU8_RU8_AU8> scaled(data.width*data.height);
element->DrawNearestNeighbour(scaled.data(), data.width, data.height);
for (std::int32_t x = data.x; x - data.x < data.width; x++) {
for (std::int32_t y = data.y; y - data.y < data.height; y++) {
if (x >= 0 && x < width && y >= 0 && y < height) {
Pixel_BU8_GU8_RU8_AU8& dst = framebuffer[y * width + x];
const Pixel_BU8_GU8_RU8_AU8& src = scaled[(y - data.y) * data.width + (x - data.x)];
float srcA = src.a / 255.0f;
float dstA = dst.a / 255.0f;
float outA = srcA + dstA * (1.0f - srcA);
if (outA > 0.0f) {
dst.r = static_cast<uint8_t>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA);
dst.g = static_cast<uint8_t>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA);
dst.b = static_cast<uint8_t>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA);
dst.a = static_cast<uint8_t>(outA * 255);
}
}
}
}
//RenderElements(window, *element, data);
}
}
}