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