Skip to content

Commit

Permalink
drivers: display: display_sdl: implement display_show
Browse files Browse the repository at this point in the history
Adds frame synchronization to every frame.
This prevents frame tearing.

Signed-off-by: Martin Stumpf <[email protected]>
  • Loading branch information
Finomnis authored and mstumpf-vected committed Nov 11, 2024
1 parent c108daa commit 56465bb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
2 changes: 2 additions & 0 deletions doc/releases/release-notes-4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Drivers and Sensors
* Added API function :c:func:`display_show` and display capability
:c:enum:`SCREEN_INFO_REQUIRES_SHOW` for displays that allow tearing prevention of segmented
updates. (:github:`79936`)
* Implemented :c:func:`display_show` for SDL display driver (:dtcompatible:`zephyr,sdl-dc`)
and added display capability :c:enum:`SCREEN_INFO_REQUIRES_SHOW`. (:github:`79936`)

* Ethernet

Expand Down
28 changes: 21 additions & 7 deletions drivers/display/display_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,20 @@ static int sdl_display_write(const struct device *dev, const uint16_t x,
sdl_display_write_bgr565(disp_data->buf, desc, buf);
}

sdl_display_write_bottom(desc->height, desc->width, x, y,
disp_data->renderer, disp_data->mutex, disp_data->texture,
disp_data->buf, disp_data->display_on);
sdl_display_write_bottom(desc->height, desc->width, x, y, disp_data->renderer,
disp_data->mutex, disp_data->texture, disp_data->buf);

return 0;
}

static int sdl_display_show(const struct device *dev)
{
struct sdl_display_data *disp_data = dev->data;

LOG_DBG("Displaying frame buffer content");

sdl_display_show_bottom(disp_data->renderer, disp_data->texture, disp_data->mutex,
disp_data->display_on);

return 0;
}
Expand Down Expand Up @@ -431,7 +442,7 @@ static int sdl_display_blanking_off(const struct device *dev)

disp_data->display_on = true;

sdl_display_blanking_off_bottom(disp_data->renderer, disp_data->texture);
sdl_display_blanking_off_bottom(disp_data->renderer, disp_data->texture, disp_data->mutex);

return 0;
}
Expand All @@ -444,7 +455,7 @@ static int sdl_display_blanking_on(const struct device *dev)

disp_data->display_on = false;

sdl_display_blanking_on_bottom(disp_data->renderer);
sdl_display_blanking_on_bottom(disp_data->renderer, disp_data->mutex);
return 0;
}

Expand All @@ -464,8 +475,10 @@ static void sdl_display_get_capabilities(
PIXEL_FORMAT_RGB_565 |
PIXEL_FORMAT_BGR_565;
capabilities->current_pixel_format = disp_data->current_pixel_format;
capabilities->screen_info = SCREEN_INFO_MONO_VTILED |
(IS_ENABLED(CONFIG_SDL_DISPLAY_MONO_MSB_FIRST) ? SCREEN_INFO_MONO_MSB_FIRST : 0);
capabilities->screen_info =
SCREEN_INFO_MONO_VTILED |
(IS_ENABLED(CONFIG_SDL_DISPLAY_MONO_MSB_FIRST) ? SCREEN_INFO_MONO_MSB_FIRST : 0) |
SCREEN_INFO_REQUIRES_SHOW;
}

static int sdl_display_set_pixel_format(const struct device *dev,
Expand Down Expand Up @@ -499,6 +512,7 @@ static const struct display_driver_api sdl_display_api = {
.blanking_off = sdl_display_blanking_off,
.write = sdl_display_write,
.read = sdl_display_read,
.show = sdl_display_show,
.get_capabilities = sdl_display_get_capabilities,
.set_pixel_format = sdl_display_set_pixel_format,
};
Expand Down
52 changes: 37 additions & 15 deletions drivers/display/display_sdl_bottom.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "display_sdl_bottom.h"

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
Expand Down Expand Up @@ -64,10 +66,9 @@ int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
return 0;
}

void sdl_display_write_bottom(const uint16_t height, const uint16_t width,
const uint16_t x, const uint16_t y,
void *renderer, void *mutex, void *texture,
uint8_t *buf, bool display_on)
void sdl_display_write_bottom(const uint16_t height, const uint16_t width, const uint16_t x,
const uint16_t y, void *renderer, void *mutex, void *texture,
uint8_t *buf)
{
SDL_Rect rect;
int err;
Expand All @@ -85,12 +86,6 @@ void sdl_display_write_bottom(const uint16_t height, const uint16_t width,

SDL_UpdateTexture(texture, &rect, buf, 4 * rect.w);

if (display_on) {
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}

SDL_UnlockMutex(mutex);
}

Expand Down Expand Up @@ -126,17 +121,44 @@ int sdl_display_read_bottom(const uint16_t height, const uint16_t width,
return err;
}

void sdl_display_blanking_off_bottom(void *renderer, void *texture)
void sdl_display_show_bottom(void *renderer, void *texture, void *mutex, bool display_on)
{
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
int err = 0;

if (display_on) {
err = SDL_TryLockMutex(mutex);
if (err) {
nsi_print_warning("Failed to lock SDL mutex: %s", SDL_GetError());
return;
}

SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);

SDL_UnlockMutex(mutex);
}
}

void sdl_display_blanking_off_bottom(void *renderer, void *texture, void *mutex)
{
sdl_display_show_bottom(renderer, texture, mutex, true);
}

void sdl_display_blanking_on_bottom(void *renderer)
void sdl_display_blanking_on_bottom(void *renderer, void *mutex)
{
int err;

err = SDL_TryLockMutex(mutex);
if (err) {
nsi_print_warning("Failed to lock SDL mutex: %s", SDL_GetError());
return;
}

SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);

SDL_UnlockMutex(mutex);
}

void sdl_display_cleanup_bottom(void **window, void **renderer, void **mutex, void **texture,
Expand Down
19 changes: 9 additions & 10 deletions drivers/display/display_sdl_bottom.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ extern "C" {
int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
bool use_accelerator, void **window, void **renderer, void **mutex,
void **texture, void **read_texture);
void sdl_display_write_bottom(const uint16_t height, const uint16_t width,
const uint16_t x, const uint16_t y,
void *renderer, void *mutex, void *texture,
uint8_t *buf, bool display_on);
int sdl_display_read_bottom(const uint16_t height, const uint16_t width,
const uint16_t x, const uint16_t y,
void *renderer, void *buf, uint16_t pitch,
void *mutex, void *texture, void **read_texture);
void sdl_display_blanking_off_bottom(void *renderer, void *texture);
void sdl_display_blanking_on_bottom(void *renderer);
void sdl_display_write_bottom(const uint16_t height, const uint16_t width, const uint16_t x,
const uint16_t y, void *renderer, void *mutex, void *texture,
uint8_t *buf);
void sdl_display_show_bottom(void *renderer, void *texture, void *mutex, bool display_on);
int sdl_display_read_bottom(const uint16_t height, const uint16_t width, const uint16_t x,
const uint16_t y, void *renderer, void *buf, uint16_t pitch,
void *mutex, void *texture, void *read_texture);
void sdl_display_blanking_off_bottom(void *renderer, void *texture, void *mutex);
void sdl_display_blanking_on_bottom(void *renderer, void *mutex);
void sdl_display_cleanup_bottom(void **window, void **renderer, void **mutex, void **texture,
void **read_texture);

Expand Down

0 comments on commit 56465bb

Please sign in to comment.