From 992d6be6ebae56f2e5830abb2f6276fb34fd5bc3 Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Wed, 1 Dec 2021 16:02:54 +0100 Subject: [PATCH] Ensure we do not render before the framebuffer copy completes --- platform/drivers/display/display_libSDL.c | 25 +++++++++++--------- platform/targets/linux/emulator/sdl_engine.c | 8 +++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/platform/drivers/display/display_libSDL.c b/platform/drivers/display/display_libSDL.c index 4c0653e3..a3fd7815 100644 --- a/platform/drivers/display/display_libSDL.c +++ b/platform/drivers/display/display_libSDL.c @@ -130,20 +130,23 @@ void display_renderRows(uint8_t startRow, uint8_t endRow) if(sdl_ready) { + // receive a texture pixel map + void *fb; + chan_recv(&fb_sync, &fb); #ifdef PIX_FMT_RGB565 - chan_send(&fb_sync, frameBuffer); + memcpy(fb, frameBuffer, sizeof(PIXEL_SIZE) * SCREEN_HEIGHT * SCREEN_WIDTH); #else - //TODO free - PIXEL_SIZE *pixels = malloc(sizeof(uint16_t) * SCREEN_HEIGHT * SCREEN_WIDTH); - for (unsigned int x = 0; x < SCREEN_WIDTH; x++) - { - for (unsigned int y = startRow; y < endRow; y++) - { - pixels[x + y * SCREEN_WIDTH] = fetchPixelFromFb(x, y); - } - } - chan_send(&fb_sync, (void *)pixels); + for (unsigned int x = 0; x < SCREEN_WIDTH; x++) + { + for (unsigned int y = startRow; y < endRow; y++) + { + pixels[x + y * SCREEN_WIDTH] = fetchPixelFromFb(x, y); + } + } #endif + // signal the SDL main loop to proceed with rendering + void *done = {0}; + chan_send(&fb_sync, done); } inProgress = false; diff --git a/platform/targets/linux/emulator/sdl_engine.c b/platform/targets/linux/emulator/sdl_engine.c index 26d20efd..83e211c1 100644 --- a/platform/targets/linux/emulator/sdl_engine.c +++ b/platform/targets/linux/emulator/sdl_engine.c @@ -278,7 +278,8 @@ void sdl_task() } } - if (chan_can_recv(&fb_sync)) + // we update the window only if there is a something ready to render + if (chan_can_send(&fb_sync)) { PIXEL_SIZE *pixels; int pitch = 0; @@ -287,9 +288,8 @@ void sdl_task() SDL_Log("SDL_lock failed: %s", SDL_GetError()); } - void *fb; - chan_recv(&fb_sync, &fb); - memcpy(pixels, fb, sizeof(PIXEL_SIZE) * SCREEN_HEIGHT * SCREEN_WIDTH); + chan_send(&fb_sync, pixels); + chan_recv(&fb_sync, NULL); SDL_UnlockTexture(displayTexture); SDL_RenderCopy(renderer, displayTexture, NULL, NULL);