forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_sdl.c
443 lines (379 loc) · 12.3 KB
/
display_sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Copyright (c) 2018 Jan Van Winkel <[email protected]>
* Copyright (c) 2021 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT zephyr_sdl_dc
#include <zephyr/drivers/display.h>
#include <SDL.h>
#include <string.h>
#include <soc.h>
#include <zephyr/sys/byteorder.h>
#define LOG_LEVEL CONFIG_DISPLAY_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(display_sdl);
struct sdl_display_config {
uint16_t height;
uint16_t width;
};
struct sdl_display_data {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
bool display_on;
enum display_pixel_format current_pixel_format;
uint8_t *buf;
};
static int sdl_display_init(const struct device *dev)
{
const struct sdl_display_config *config = dev->config;
struct sdl_display_data *disp_data = dev->data;
LOG_DBG("Initializing display driver");
disp_data->current_pixel_format =
#if defined(CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_RGB_888)
PIXEL_FORMAT_RGB_888
#elif defined(CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_MONO01)
PIXEL_FORMAT_MONO01
#elif defined(CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_MONO10)
PIXEL_FORMAT_MONO10
#elif defined(CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_RGB_565)
PIXEL_FORMAT_RGB_565
#elif defined(CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_BGR_565)
PIXEL_FORMAT_BGR_565
#else /* SDL_DISPLAY_DEFAULT_PIXEL_FORMAT */
PIXEL_FORMAT_ARGB_8888
#endif /* SDL_DISPLAY_DEFAULT_PIXEL_FORMAT */
;
disp_data->window =
SDL_CreateWindow("Zephyr Display", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, config->width,
config->height, SDL_WINDOW_SHOWN);
if (disp_data->window == NULL) {
LOG_ERR("Failed to create SDL window: %s", SDL_GetError());
return -EIO;
}
disp_data->renderer =
SDL_CreateRenderer(disp_data->window, -1, SDL_RENDERER_ACCELERATED);
if (disp_data->renderer == NULL) {
LOG_ERR("Failed to create SDL renderer: %s",
SDL_GetError());
return -EIO;
}
disp_data->texture = SDL_CreateTexture(
disp_data->renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, config->width,
config->height);
if (disp_data->texture == NULL) {
LOG_ERR("Failed to create SDL texture: %s", SDL_GetError());
return -EIO;
}
disp_data->display_on = false;
SDL_SetRenderDrawColor(disp_data->renderer, 0, 0, 0, 0xFF);
SDL_RenderClear(disp_data->renderer);
SDL_RenderPresent(disp_data->renderer);
return 0;
}
static void sdl_display_write_argb8888(void *disp_buf,
const struct display_buffer_descriptor *desc, const void *buf)
{
__ASSERT((desc->pitch * 4U * desc->height) <= desc->buf_size,
"Input buffer to small");
memcpy(disp_buf, buf, desc->pitch * 4U * desc->height);
}
static void sdl_display_write_rgb888(uint8_t *disp_buf,
const struct display_buffer_descriptor *desc, const void *buf)
{
uint32_t w_idx;
uint32_t h_idx;
uint32_t pixel;
const uint8_t *byte_ptr;
__ASSERT((desc->pitch * 3U * desc->height) <= desc->buf_size,
"Input buffer to small");
for (h_idx = 0U; h_idx < desc->height; ++h_idx) {
for (w_idx = 0U; w_idx < desc->width; ++w_idx) {
byte_ptr = (const uint8_t *)buf +
((h_idx * desc->pitch) + w_idx) * 3U;
pixel = *byte_ptr << 16;
pixel |= *(byte_ptr + 1) << 8;
pixel |= *(byte_ptr + 2);
*((uint32_t *)disp_buf) = pixel;
disp_buf += 4;
}
}
}
static void sdl_display_write_rgb565(uint8_t *disp_buf,
const struct display_buffer_descriptor *desc, const void *buf)
{
uint32_t w_idx;
uint32_t h_idx;
uint32_t pixel;
const uint16_t *pix_ptr;
uint16_t rgb565;
__ASSERT((desc->pitch * 2U * desc->height) <= desc->buf_size,
"Input buffer to small");
for (h_idx = 0U; h_idx < desc->height; ++h_idx) {
for (w_idx = 0U; w_idx < desc->width; ++w_idx) {
pix_ptr = (const uint16_t *)buf +
((h_idx * desc->pitch) + w_idx);
rgb565 = sys_be16_to_cpu(*pix_ptr);
pixel = (((rgb565 >> 11) & 0x1F) * 255 / 31) << 16;
pixel |= (((rgb565 >> 5) & 0x3F) * 255 / 63) << 8;
pixel |= (rgb565 & 0x1F) * 255 / 31;
*((uint32_t *)disp_buf) = pixel;
disp_buf += 4;
}
}
}
static void sdl_display_write_bgr565(uint8_t *disp_buf,
const struct display_buffer_descriptor *desc, const void *buf)
{
uint32_t w_idx;
uint32_t h_idx;
uint32_t pixel;
const uint16_t *pix_ptr;
__ASSERT((desc->pitch * 2U * desc->height) <= desc->buf_size,
"Input buffer to small");
for (h_idx = 0U; h_idx < desc->height; ++h_idx) {
for (w_idx = 0U; w_idx < desc->width; ++w_idx) {
pix_ptr = (const uint16_t *)buf +
((h_idx * desc->pitch) + w_idx);
pixel = (((*pix_ptr >> 11) & 0x1F) * 255 / 31) << 16;
pixel |= (((*pix_ptr >> 5) & 0x3F) * 255 / 63) << 8;
pixel |= (*pix_ptr & 0x1F) * 255 / 31;
*((uint32_t *)disp_buf) = pixel;
disp_buf += 4;
}
}
}
static void sdl_display_write_mono(uint8_t *disp_buf,
const struct display_buffer_descriptor *desc, const void *buf,
const bool one_is_black)
{
uint32_t w_idx;
uint32_t h_idx;
uint32_t tile_idx;
uint32_t pixel;
const uint8_t *byte_ptr;
uint32_t one_color;
uint8_t *disp_buf_start;
__ASSERT((desc->pitch * desc->height) <= (desc->buf_size * 8U),
"Input buffer to small");
__ASSERT((desc->height % 8) == 0U,
"Input buffer height not aligned per 8 pixels");
if (one_is_black) {
one_color = 0U;
} else {
one_color = 0x00FFFFFF;
}
for (tile_idx = 0U; tile_idx < desc->height/8U; ++tile_idx) {
for (w_idx = 0U; w_idx < desc->width; ++w_idx) {
byte_ptr = (const uint8_t *)buf +
((tile_idx * desc->pitch) + w_idx);
disp_buf_start = disp_buf;
for (h_idx = 0U; h_idx < 8; ++h_idx) {
if ((*byte_ptr & BIT(7-h_idx)) != 0U) {
pixel = one_color;
} else {
pixel = (~one_color) & 0x00FFFFFF;
}
*((uint32_t *)disp_buf) = pixel;
disp_buf += (desc->width * 4U);
}
disp_buf = disp_buf_start;
disp_buf += 4;
}
disp_buf += 7 * (desc->width * 4U);
}
}
static int sdl_display_write(const struct device *dev, const uint16_t x,
const uint16_t y,
const struct display_buffer_descriptor *desc,
const void *buf)
{
const struct sdl_display_config *config = dev->config;
struct sdl_display_data *disp_data = dev->data;
SDL_Rect rect;
LOG_DBG("Writing %dx%d (w,h) bitmap @ %dx%d (x,y)", desc->width,
desc->height, x, y);
__ASSERT(desc->width <= desc->pitch, "Pitch is smaller then width");
__ASSERT(desc->pitch <= config->width,
"Pitch in descriptor is larger than screen size");
__ASSERT(desc->height <= config->height,
"Height in descriptor is larger than screen size");
__ASSERT(x + desc->pitch <= config->width,
"Writing outside screen boundaries in horizontal direction");
__ASSERT(y + desc->height <= config->height,
"Writing outside screen boundaries in vertical direction");
if (desc->width > desc->pitch ||
x + desc->pitch > config->width ||
y + desc->height > config->height) {
return -EINVAL;
}
if (disp_data->current_pixel_format == PIXEL_FORMAT_ARGB_8888) {
sdl_display_write_argb8888(disp_data->buf, desc, buf);
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_RGB_888) {
sdl_display_write_rgb888(disp_data->buf, desc, buf);
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_MONO10) {
sdl_display_write_mono(disp_data->buf, desc, buf, true);
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_MONO01) {
sdl_display_write_mono(disp_data->buf, desc, buf, false);
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_RGB_565) {
sdl_display_write_rgb565(disp_data->buf, desc, buf);
} else if (disp_data->current_pixel_format == PIXEL_FORMAT_BGR_565) {
sdl_display_write_bgr565(disp_data->buf, desc, buf);
}
rect.x = x;
rect.y = y;
rect.w = desc->width;
rect.h = desc->height;
SDL_UpdateTexture(disp_data->texture, &rect, disp_data->buf,
4 * rect.w);
if (disp_data->display_on) {
SDL_RenderClear(disp_data->renderer);
SDL_RenderCopy(disp_data->renderer, disp_data->texture, NULL,
NULL);
SDL_RenderPresent(disp_data->renderer);
}
return 0;
}
static int sdl_display_read(const struct device *dev, const uint16_t x,
const uint16_t y,
const struct display_buffer_descriptor *desc,
void *buf)
{
struct sdl_display_data *disp_data = dev->data;
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = desc->width;
rect.h = desc->height;
LOG_DBG("Reading %dx%d (w,h) bitmap @ %dx%d (x,y)", desc->width,
desc->height, x, y);
__ASSERT(desc->width <= desc->pitch, "Pitch is smaller then width");
__ASSERT((desc->pitch * 3U * desc->height) <= desc->buf_size,
"Input buffer to small");
return SDL_RenderReadPixels(disp_data->renderer, &rect, 0, buf,
desc->pitch * 4U);
}
static void *sdl_display_get_framebuffer(const struct device *dev)
{
return NULL;
}
static int sdl_display_blanking_off(const struct device *dev)
{
struct sdl_display_data *disp_data = dev->data;
LOG_DBG("Turning display blacking off");
disp_data->display_on = true;
SDL_RenderClear(disp_data->renderer);
SDL_RenderCopy(disp_data->renderer, disp_data->texture, NULL, NULL);
SDL_RenderPresent(disp_data->renderer);
return 0;
}
static int sdl_display_blanking_on(const struct device *dev)
{
struct sdl_display_data *disp_data = dev->data;
LOG_DBG("Turning display blanking on");
disp_data->display_on = false;
SDL_RenderClear(disp_data->renderer);
SDL_RenderPresent(disp_data->renderer);
return 0;
}
static int sdl_display_set_brightness(const struct device *dev,
const uint8_t brightness)
{
return -ENOTSUP;
}
static int sdl_display_set_contrast(const struct device *dev,
const uint8_t contrast)
{
return -ENOTSUP;
}
static void sdl_display_get_capabilities(
const struct device *dev, struct display_capabilities *capabilities)
{
const struct sdl_display_config *config = dev->config;
struct sdl_display_data *disp_data = dev->data;
memset(capabilities, 0, sizeof(struct display_capabilities));
capabilities->x_resolution = config->width;
capabilities->y_resolution = config->height;
capabilities->supported_pixel_formats = PIXEL_FORMAT_ARGB_8888 |
PIXEL_FORMAT_RGB_888 |
PIXEL_FORMAT_MONO01 |
PIXEL_FORMAT_MONO10 |
PIXEL_FORMAT_RGB_565 |
PIXEL_FORMAT_BGR_565;
capabilities->current_pixel_format = disp_data->current_pixel_format;
capabilities->screen_info = SCREEN_INFO_MONO_VTILED |
SCREEN_INFO_MONO_MSB_FIRST;
}
static int sdl_display_set_pixel_format(const struct device *dev,
const enum display_pixel_format pixel_format)
{
struct sdl_display_data *disp_data = dev->data;
switch (pixel_format) {
case PIXEL_FORMAT_ARGB_8888:
case PIXEL_FORMAT_RGB_888:
case PIXEL_FORMAT_MONO01:
case PIXEL_FORMAT_MONO10:
case PIXEL_FORMAT_RGB_565:
case PIXEL_FORMAT_BGR_565:
disp_data->current_pixel_format = pixel_format;
return 0;
default:
LOG_ERR("Pixel format not supported");
return -ENOTSUP;
}
}
static void sdl_display_cleanup(struct sdl_display_data *disp_data)
{
if (disp_data->texture != NULL) {
SDL_DestroyTexture(disp_data->texture);
disp_data->texture = NULL;
}
if (disp_data->renderer != NULL) {
SDL_DestroyRenderer(disp_data->renderer);
disp_data->renderer = NULL;
}
if (disp_data->window != NULL) {
SDL_DestroyWindow(disp_data->window);
disp_data->window = NULL;
}
}
static const struct display_driver_api sdl_display_api = {
.blanking_on = sdl_display_blanking_on,
.blanking_off = sdl_display_blanking_off,
.write = sdl_display_write,
.read = sdl_display_read,
.get_framebuffer = sdl_display_get_framebuffer,
.set_brightness = sdl_display_set_brightness,
.set_contrast = sdl_display_set_contrast,
.get_capabilities = sdl_display_get_capabilities,
.set_pixel_format = sdl_display_set_pixel_format,
};
#define DISPLAY_SDL_DEFINE(n) \
static const struct sdl_display_config sdl_config_##n = { \
.height = DT_INST_PROP(n, height), \
.width = DT_INST_PROP(n, width), \
}; \
\
static uint8_t sdl_buf_##n[4 * DT_INST_PROP(n, height) \
* DT_INST_PROP(n, width)]; \
static struct sdl_display_data sdl_data_##n = { \
.buf = sdl_buf_##n, \
}; \
\
DEVICE_DT_INST_DEFINE(n, &sdl_display_init, NULL, \
&sdl_data_##n, \
&sdl_config_##n, \
APPLICATION, \
CONFIG_DISPLAY_INIT_PRIORITY, \
&sdl_display_api); \
\
static void sdl_display_cleanup_##n(void) \
{ \
sdl_display_cleanup(&sdl_data_##n); \
} \
\
NATIVE_TASK(sdl_display_cleanup_##n, ON_EXIT, 1);
DT_INST_FOREACH_STATUS_OKAY(DISPLAY_SDL_DEFINE)