Skip to content

Commit 1ba9e4d

Browse files
faxe1008kartben
authored andcommitted
drivers: display: sdl: Add support for L8 pixel format
Adds support for the 8bit grayscale pixel format to the SDL display driver. Signed-off-by: Fabian Blatz <fabianblatz@gmail.com>
1 parent bd27851 commit 1ba9e4d

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

drivers/display/Kconfig.sdl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ choice SDL_DISPLAY_DEFAULT_PIXEL_FORMAT
3737
config SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_BGR_565
3838
bool "BGR 565"
3939

40+
config SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_L_8
41+
bool "Grayscale 8bit"
42+
4043
endchoice
4144

4245
config SDL_DISPLAY_ZOOM_PCT

drivers/display/display_sdl.c

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ static int sdl_display_init(const struct device *dev)
6969
PIXEL_FORMAT_RGB_565
7070
#elif defined(CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_BGR_565)
7171
PIXEL_FORMAT_BGR_565
72-
#else /* SDL_DISPLAY_DEFAULT_PIXEL_FORMAT */
72+
#elif defined(CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_L_8)
73+
PIXEL_FORMAT_L_8
74+
#else /* SDL_DISPLAY_DEFAULT_PIXEL_FORMAT */
7375
PIXEL_FORMAT_ARGB_8888
7476
#endif /* SDL_DISPLAY_DEFAULT_PIXEL_FORMAT */
7577
;
@@ -223,6 +225,26 @@ static void sdl_display_write_mono(uint8_t *disp_buf,
223225
}
224226
}
225227

228+
static void sdl_display_write_l8(uint8_t *disp_buf, const struct display_buffer_descriptor *desc,
229+
const void *buf)
230+
{
231+
uint32_t w_idx;
232+
uint32_t h_idx;
233+
uint32_t pixel;
234+
const uint8_t *byte_ptr;
235+
236+
__ASSERT((desc->pitch * desc->height) <= desc->buf_size, "Input buffer too small");
237+
238+
for (h_idx = 0U; h_idx < desc->height; ++h_idx) {
239+
for (w_idx = 0U; w_idx < desc->width; ++w_idx) {
240+
byte_ptr = (const uint8_t *)buf + ((h_idx * desc->pitch) + w_idx);
241+
pixel = *byte_ptr;
242+
*((uint32_t *)disp_buf) = pixel | (pixel << 8) | (pixel << 16) | 0xFF000000;
243+
disp_buf += 4;
244+
}
245+
}
246+
}
247+
226248
static int sdl_display_write(const struct device *dev, const uint16_t x,
227249
const uint16_t y,
228250
const struct display_buffer_descriptor *desc,
@@ -262,6 +284,8 @@ static int sdl_display_write(const struct device *dev, const uint16_t x,
262284
sdl_display_write_rgb565(disp_data->buf, desc, buf);
263285
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_BGR_565) {
264286
sdl_display_write_bgr565(disp_data->buf, desc, buf);
287+
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_L_8) {
288+
sdl_display_write_l8(disp_data->buf, desc, buf);
265289
}
266290

267291
sdl_display_write_bottom(desc->height, desc->width, x, y, disp_data->renderer,
@@ -388,10 +412,29 @@ static void sdl_display_read_mono(const uint8_t *read_buf,
388412
}
389413
}
390414

391-
static int sdl_display_read(const struct device *dev, const uint16_t x,
392-
const uint16_t y,
393-
const struct display_buffer_descriptor *desc,
394-
void *buf)
415+
static void sdl_display_read_l8(const uint8_t *read_buf,
416+
const struct display_buffer_descriptor *desc, void *buf)
417+
{
418+
uint32_t w_idx;
419+
uint32_t h_idx;
420+
uint8_t *buf8;
421+
const uint32_t *pix_ptr;
422+
423+
__ASSERT((desc->pitch * desc->height) <= desc->buf_size, "Read buffer is too small");
424+
425+
for (h_idx = 0U; h_idx < desc->height; ++h_idx) {
426+
buf8 = ((uint8_t *)buf) + desc->pitch * h_idx;
427+
428+
for (w_idx = 0U; w_idx < desc->width; ++w_idx) {
429+
pix_ptr = (const uint32_t *)read_buf + ((h_idx * desc->pitch) + w_idx);
430+
*buf8 = *pix_ptr & 0xFF;
431+
buf8 += 1;
432+
}
433+
}
434+
}
435+
436+
static int sdl_display_read(const struct device *dev, const uint16_t x, const uint16_t y,
437+
const struct display_buffer_descriptor *desc, void *buf)
395438
{
396439
struct sdl_display_data *disp_data = dev->data;
397440
int err;
@@ -423,6 +466,8 @@ static int sdl_display_read(const struct device *dev, const uint16_t x,
423466
sdl_display_read_rgb565(disp_data->read_buf, desc, buf);
424467
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_BGR_565) {
425468
sdl_display_read_bgr565(disp_data->read_buf, desc, buf);
469+
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_L_8) {
470+
sdl_display_read_l8(disp_data->read_buf, desc, buf);
426471
}
427472

428473
return 0;
@@ -507,7 +552,8 @@ static void sdl_display_get_capabilities(
507552
PIXEL_FORMAT_MONO01 |
508553
PIXEL_FORMAT_MONO10 |
509554
PIXEL_FORMAT_RGB_565 |
510-
PIXEL_FORMAT_BGR_565;
555+
PIXEL_FORMAT_BGR_565 |
556+
PIXEL_FORMAT_L_8;
511557
capabilities->current_pixel_format = disp_data->current_pixel_format;
512558
capabilities->screen_info = SCREEN_INFO_MONO_VTILED |
513559
(IS_ENABLED(CONFIG_SDL_DISPLAY_MONO_MSB_FIRST) ? SCREEN_INFO_MONO_MSB_FIRST : 0);
@@ -525,6 +571,7 @@ static int sdl_display_set_pixel_format(const struct device *dev,
525571
case PIXEL_FORMAT_MONO10:
526572
case PIXEL_FORMAT_RGB_565:
527573
case PIXEL_FORMAT_BGR_565:
574+
case PIXEL_FORMAT_L_8:
528575
disp_data->current_pixel_format = pixel_format;
529576
return 0;
530577
default:

0 commit comments

Comments
 (0)