This commit is contained in:
Jorijn van der Graaf 2025-11-16 20:42:48 +01:00
commit e795ab880c
6 changed files with 89 additions and 24 deletions

View file

@ -121,26 +121,32 @@ WindowWaylandWayland::WindowWaylandWayland(std::string name, std::uint32_t width
}
// Map the shared memory file
framebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (framebuffer == MAP_FAILED) {
frontFramebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (frontFramebuffer == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
}
backFramebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (backFramebuffer == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
}
// Create a wl_buffer from our shared memory file descriptor
wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
frontBuffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
backBuffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool);
// Now that we've mapped the file and created the wl_buffer, we no longer
// need to keep file descriptor opened
close(fd);
if (buffer == NULL) {
if (frontBuffer == NULL || backBuffer == NULL) {
exit(EXIT_FAILURE);
}
wl_surface_attach(surface, buffer, 0, 0);
wl_surface_attach(surface, frontBuffer, 0, 0);
wl_surface_commit(surface);
}
@ -155,20 +161,42 @@ void WindowWaylandWayland::StartAsync() {
void WindowWaylandWayland::StartSync() {
while (open && wl_display_dispatch(display) != -1) {
wl_surface_attach(surface, buffer, 0, 0);
for(const UiElement& element : elements) {
ScaleData data = ScaleElement(element);
std::vector<UiElement*> drawOrder;
drawOrder.reserve(elements.size());
for (UiElement& e : elements) drawOrder.push_back(&e);
std::sort(drawOrder.begin(), drawOrder.end(), [](UiElement* a, UiElement* b){ return a->z < b->z; });
for(const UiElement* element : drawOrder) {
std::cout << element->bufferWidth << std::endl;
ScaleData data = ScaleElement(*element);
std::vector<Pixel_BU8_GU8_RU8_AU8> scaled(data.width*data.height);
ScaleBitmapR8G8B8(scaled.data(), element.buffer.data(), element.bufferWidth, element.bufferHeight, 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) {
framebuffer[y*width+x] = scaled[(y-data.y)*data.width+(x-data.x)];
ScaleBitmapR8G8B8(scaled.data(), element->buffer.data(), element->bufferWidth, element->bufferHeight, 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 = backFramebuffer[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);
}
}
}
}
wl_surface_damage(surface, data.x, data.y, data.width, data.height);
}
wl_surface_attach(surface, backBuffer, 0, 0);
wl_surface_damage(surface, 0, 0, width, height);
wl_surface_commit(surface);
std::swap(frontFramebuffer, backFramebuffer);
std::swap(frontBuffer, backBuffer);
}
}