Skip to content

Commit 02d562e

Browse files
Finomnisnashif
authored andcommitted
drivers: display_sdl: add alpha support
While the driver was already capable of processing `ARGB8888` data, it did not actually show the alpha value in any way. This change adds a checkerboard background that shows transparent regions. Signed-off-by: Martin Stumpf <[email protected]>
1 parent 86a126d commit 02d562e

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

drivers/display/Kconfig.sdl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,22 @@ config SDL_DISPLAY_MONO_MSB_FIRST
5959
If selected, set the MSB to represent the first pixel.
6060
This applies when the pixel format is MONO01/MONO10.
6161

62+
config SDL_DISPLAY_TRANSPARENCY_GRID_CELL_SIZE
63+
int "Transparency grid cell size"
64+
default 8
65+
help
66+
The size of the checkerboard pattern squares, in pixels.
67+
68+
config SDL_DISPLAY_TRANSPARENCY_GRID_CELL_COLOR_1
69+
hex "Transparency grid cell color 1"
70+
default 0xcccccc
71+
help
72+
The color of the odd cells in the transparency grid.
73+
74+
config SDL_DISPLAY_TRANSPARENCY_GRID_CELL_COLOR_2
75+
hex "Transparency grid cell color 2"
76+
default 0xbbbbbb
77+
help
78+
The color of the even cells in the transparency grid.
79+
6280
endif # SDL_DISPLAY

drivers/display/display_sdl.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct sdl_display_data {
3333
void *mutex;
3434
void *texture;
3535
void *read_texture;
36+
void *background_texture;
3637
bool display_on;
3738
enum display_pixel_format current_pixel_format;
3839
uint8_t *buf;
@@ -80,7 +81,10 @@ static int sdl_display_init(const struct device *dev)
8081
int rc = sdl_display_init_bottom(config->height, config->width, sdl_display_zoom_pct,
8182
use_accelerator, &disp_data->window, &disp_data->renderer,
8283
&disp_data->mutex, &disp_data->texture,
83-
&disp_data->read_texture);
84+
&disp_data->read_texture, &disp_data->background_texture,
85+
CONFIG_SDL_DISPLAY_TRANSPARENCY_GRID_CELL_COLOR_1,
86+
CONFIG_SDL_DISPLAY_TRANSPARENCY_GRID_CELL_COLOR_2,
87+
CONFIG_SDL_DISPLAY_TRANSPARENCY_GRID_CELL_SIZE);
8488

8589
if (rc != 0) {
8690
LOG_ERR("Failed to create SDL display");
@@ -261,7 +265,8 @@ static int sdl_display_write(const struct device *dev, const uint16_t x,
261265
}
262266

263267
sdl_display_write_bottom(desc->height, desc->width, x, y, disp_data->renderer,
264-
disp_data->mutex, disp_data->texture, disp_data->buf,
268+
disp_data->mutex, disp_data->texture,
269+
disp_data->background_texture, disp_data->buf,
265270
disp_data->display_on, desc->frame_incomplete);
266271

267272
return 0;
@@ -431,7 +436,8 @@ static int sdl_display_blanking_off(const struct device *dev)
431436

432437
disp_data->display_on = true;
433438

434-
sdl_display_blanking_off_bottom(disp_data->renderer, disp_data->texture);
439+
sdl_display_blanking_off_bottom(disp_data->renderer, disp_data->texture,
440+
disp_data->background_texture);
435441

436442
return 0;
437443
}
@@ -491,7 +497,8 @@ static int sdl_display_set_pixel_format(const struct device *dev,
491497
static void sdl_display_cleanup(struct sdl_display_data *disp_data)
492498
{
493499
sdl_display_cleanup_bottom(&disp_data->window, &disp_data->renderer, &disp_data->mutex,
494-
&disp_data->texture, &disp_data->read_texture);
500+
&disp_data->texture, &disp_data->read_texture,
501+
&disp_data->background_texture);
495502
}
496503

497504
static const struct display_driver_api sdl_display_api = {

drivers/display/display_sdl_bottom.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
1717
bool use_accelerator, void **window, void **renderer, void **mutex,
18-
void **texture, void **read_texture)
18+
void **texture, void **read_texture, void **background_texture,
19+
uint32_t transparency_grid_color1, uint32_t transparency_grid_color2,
20+
uint16_t transparency_grid_cell_size)
1921
{
2022
*window = SDL_CreateWindow("Zephyr Display", SDL_WINDOWPOS_UNDEFINED,
2123
SDL_WINDOWPOS_UNDEFINED, width * zoom_pct / 100,
@@ -51,6 +53,7 @@ int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
5153
nsi_print_warning("Failed to create SDL texture: %s", SDL_GetError());
5254
return -1;
5355
}
56+
SDL_SetTextureBlendMode(*texture, SDL_BLENDMODE_BLEND);
5457

5558
*read_texture = SDL_CreateTexture(*renderer, SDL_PIXELFORMAT_ARGB8888,
5659
SDL_TEXTUREACCESS_TARGET, width, height);
@@ -59,16 +62,50 @@ int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
5962
return -1;
6063
}
6164

65+
*background_texture = SDL_CreateTexture(*renderer, SDL_PIXELFORMAT_ARGB8888,
66+
SDL_TEXTUREACCESS_STREAMING, width, height);
67+
if (*background_texture == NULL) {
68+
nsi_print_warning("Failed to create SDL texture: %s", SDL_GetError());
69+
return -1;
70+
}
71+
72+
void *background_data;
73+
int background_pitch;
74+
int err;
75+
76+
err = SDL_LockTexture(*background_texture, NULL, &background_data, &background_pitch);
77+
if (err != 0) {
78+
nsi_print_warning("Failed to lock background texture: %d", err);
79+
return -1;
80+
}
81+
for (int y = 0; y < height; y++) {
82+
uint32_t *row = (uint32_t *)((uint8_t *)background_data + background_pitch * y);
83+
84+
for (int x = 0; x < width; x++) {
85+
bool x_cell_even = ((x / transparency_grid_cell_size) % 2) == 0;
86+
bool y_cell_even = ((y / transparency_grid_cell_size) % 2) == 0;
87+
88+
if (x_cell_even == y_cell_even) {
89+
row[x] = transparency_grid_color1 | 0xff000000;
90+
} else {
91+
row[x] = transparency_grid_color2 | 0xff000000;
92+
}
93+
}
94+
}
95+
SDL_UnlockTexture(*background_texture);
96+
6297
SDL_SetRenderDrawColor(*renderer, 0, 0, 0, 0xFF);
6398
SDL_RenderClear(*renderer);
99+
SDL_RenderCopy(*renderer, *background_texture, NULL, NULL);
64100
SDL_RenderPresent(*renderer);
65101

66102
return 0;
67103
}
68104

69105
void sdl_display_write_bottom(const uint16_t height, const uint16_t width, const uint16_t x,
70106
const uint16_t y, void *renderer, void *mutex, void *texture,
71-
uint8_t *buf, bool display_on, bool frame_incomplete)
107+
void *background_texture, uint8_t *buf, bool display_on,
108+
bool frame_incomplete)
72109
{
73110
SDL_Rect rect;
74111
int err;
@@ -88,6 +125,7 @@ void sdl_display_write_bottom(const uint16_t height, const uint16_t width, const
88125

89126
if (display_on && !frame_incomplete) {
90127
SDL_RenderClear(renderer);
128+
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
91129
SDL_RenderCopy(renderer, texture, NULL, NULL);
92130
SDL_RenderPresent(renderer);
93131
}
@@ -127,9 +165,10 @@ int sdl_display_read_bottom(const uint16_t height, const uint16_t width,
127165
return err;
128166
}
129167

130-
void sdl_display_blanking_off_bottom(void *renderer, void *texture)
168+
void sdl_display_blanking_off_bottom(void *renderer, void *texture, void *background_texture)
131169
{
132170
SDL_RenderClear(renderer);
171+
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
133172
SDL_RenderCopy(renderer, texture, NULL, NULL);
134173
SDL_RenderPresent(renderer);
135174
}
@@ -141,8 +180,13 @@ void sdl_display_blanking_on_bottom(void *renderer)
141180
}
142181

143182
void sdl_display_cleanup_bottom(void **window, void **renderer, void **mutex, void **texture,
144-
void **read_texture)
183+
void **read_texture, void **background_texture)
145184
{
185+
if (*background_texture != NULL) {
186+
SDL_DestroyTexture(*background_texture);
187+
*background_texture = NULL;
188+
}
189+
146190
if (*read_texture != NULL) {
147191
SDL_DestroyTexture(*read_texture);
148192
*read_texture = NULL;

drivers/display/display_sdl_bottom.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ extern "C" {
2222

2323
int sdl_display_init_bottom(uint16_t height, uint16_t width, uint16_t zoom_pct,
2424
bool use_accelerator, void **window, void **renderer, void **mutex,
25-
void **texture, void **read_texture);
25+
void **texture, void **read_texture, void **background_texture,
26+
uint32_t transparency_grid_color1, uint32_t transparency_grid_color2,
27+
uint16_t transparency_grid_cell_size);
2628
void sdl_display_write_bottom(const uint16_t height, const uint16_t width, const uint16_t x,
2729
const uint16_t y, void *renderer, void *mutex, void *texture,
28-
uint8_t *buf, bool display_on, bool frame_incomplete);
30+
void *background_texture, uint8_t *buf, bool display_on,
31+
bool frame_incomplete);
2932
int sdl_display_read_bottom(const uint16_t height, const uint16_t width, const uint16_t x,
3033
const uint16_t y, void *renderer, void *buf, uint16_t pitch,
3134
void *mutex, void *texture, void *read_texture);
32-
void sdl_display_blanking_off_bottom(void *renderer, void *texture);
35+
void sdl_display_blanking_off_bottom(void *renderer, void *texture, void *background_texture);
3336
void sdl_display_blanking_on_bottom(void *renderer);
3437
void sdl_display_cleanup_bottom(void **window, void **renderer, void **mutex, void **texture,
35-
void **read_texture);
38+
void **read_texture, void **background_texture);
3639

3740
#ifdef __cplusplus
3841
}

0 commit comments

Comments
 (0)