1 /*
2  * Copyright (c) 2018-2019 Jan Van Winkel <jan.van_winkel@dxplore.eu>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/init.h>
8 #include <zephyr/kernel.h>
9 #include <lvgl.h>
10 #include "lvgl_display.h"
11 #include "lvgl_common_input.h"
12 #include "lvgl_zephyr.h"
13 #ifdef CONFIG_LV_Z_USE_FILESYSTEM
14 #include "lvgl_fs.h"
15 #endif
16 #ifdef CONFIG_LV_Z_MEM_POOL_SYS_HEAP
17 #include "lvgl_mem.h"
18 #endif
19 #include LV_STDLIB_INCLUDE
20 
21 #include <zephyr/logging/log.h>
22 LOG_MODULE_REGISTER(lvgl, CONFIG_LV_Z_LOG_LEVEL);
23 
24 static lv_display_t *display;
25 struct lvgl_disp_data disp_data = {
26 	.blanking_on = false,
27 };
28 
29 #define DISPLAY_NODE          DT_CHOSEN(zephyr_display)
30 #define IS_MONOCHROME_DISPLAY ((CONFIG_LV_Z_BITS_PER_PIXEL == 1) || (CONFIG_LV_COLOR_DEPTH_1 == 1))
31 #define ALLOC_MONOCHROME_CONV_BUFFER                                                               \
32 	((IS_MONOCHROME_DISPLAY == 1) && (CONFIG_LV_Z_MONOCHROME_CONVERSION_BUFFER == 1))
33 
34 #ifdef CONFIG_LV_Z_BUFFER_ALLOC_STATIC
35 
36 #define DISPLAY_WIDTH  DT_PROP(DISPLAY_NODE, width)
37 #define DISPLAY_HEIGHT DT_PROP(DISPLAY_NODE, height)
38 
39 #if IS_MONOCHROME_DISPLAY
40 /* monochrome buffers are expected to have 8 preceding bytes for the color palette */
41 #define BUFFER_SIZE                                                                                \
42 	(((CONFIG_LV_Z_VDB_SIZE * ROUND_UP(DISPLAY_WIDTH, 8) * ROUND_UP(DISPLAY_HEIGHT, 8)) /      \
43 	  100) / 8 +                                                                               \
44 	 8)
45 #else
46 #define BUFFER_SIZE                                                                                \
47 	(CONFIG_LV_Z_BITS_PER_PIXEL *                                                              \
48 	 ((CONFIG_LV_Z_VDB_SIZE * DISPLAY_WIDTH * DISPLAY_HEIGHT) / 100) / 8)
49 #endif /* IS_MONOCHROME_DISPLAY */
50 
51 /* NOTE: depending on chosen color depth buffer may be accessed using uint8_t *,
52  * uint16_t * or uint32_t *, therefore buffer needs to be aligned accordingly to
53  * prevent unaligned memory accesses.
54  */
55 static uint8_t buf0[BUFFER_SIZE]
56 #ifdef CONFIG_LV_Z_VBD_CUSTOM_SECTION
57 	Z_GENERIC_SECTION(.lvgl_buf)
58 #endif
59 		__aligned(CONFIG_LV_Z_VDB_ALIGN);
60 
61 #ifdef CONFIG_LV_Z_DOUBLE_VDB
62 static uint8_t buf1[BUFFER_SIZE]
63 #ifdef CONFIG_LV_Z_VBD_CUSTOM_SECTION
64 	Z_GENERIC_SECTION(.lvgl_buf)
65 #endif
66 		__aligned(CONFIG_LV_Z_VDB_ALIGN);
67 #endif /* CONFIG_LV_Z_DOUBLE_VDB */
68 
69 #if ALLOC_MONOCHROME_CONV_BUFFER
70 static uint8_t mono_vtile_buf[BUFFER_SIZE]
71 #ifdef CONFIG_LV_Z_VBD_CUSTOM_SECTION
72 	Z_GENERIC_SECTION(.lvgl_buf)
73 #endif
74 		__aligned(CONFIG_LV_Z_VDB_ALIGN);
75 #endif /* ALLOC_MONOCHROME_CONV_BUFFER */
76 
77 #endif /* CONFIG_LV_Z_BUFFER_ALLOC_STATIC */
78 
79 #if CONFIG_LV_Z_LOG_LEVEL != 0
lvgl_log(lv_log_level_t level,const char * buf)80 static void lvgl_log(lv_log_level_t level, const char *buf)
81 {
82 	switch (level) {
83 	case LV_LOG_LEVEL_ERROR:
84 		LOG_ERR("%s", buf + (sizeof("[Error] ") - 1));
85 		break;
86 	case LV_LOG_LEVEL_WARN:
87 		LOG_WRN("%s", buf + (sizeof("[Warn] ") - 1));
88 		break;
89 	case LV_LOG_LEVEL_INFO:
90 		LOG_INF("%s", buf + (sizeof("[Info] ") - 1));
91 		break;
92 	case LV_LOG_LEVEL_TRACE:
93 		LOG_DBG("%s", buf + (sizeof("[Trace] ") - 1));
94 		break;
95 	case LV_LOG_LEVEL_USER:
96 		LOG_INF("%s", buf + (sizeof("[User] ") - 1));
97 		break;
98 	}
99 }
100 #endif
101 
102 #ifdef CONFIG_LV_Z_BUFFER_ALLOC_STATIC
103 
lvgl_allocate_rendering_buffers(lv_display_t * display)104 static int lvgl_allocate_rendering_buffers(lv_display_t *display)
105 {
106 	int err = 0;
107 
108 #ifdef CONFIG_LV_Z_DOUBLE_VDB
109 	lv_display_set_buffers(display, &buf0, &buf1, BUFFER_SIZE, LV_DISPLAY_RENDER_MODE_PARTIAL);
110 #else
111 	lv_display_set_buffers(display, &buf0, NULL, BUFFER_SIZE, LV_DISPLAY_RENDER_MODE_PARTIAL);
112 #endif /* CONFIG_LV_Z_DOUBLE_VDB  */
113 
114 #if ALLOC_MONOCHROME_CONV_BUFFER
115 	lvgl_set_mono_conversion_buffer(mono_vtile_buf, BUFFER_SIZE);
116 #endif /* ALLOC_MONOCHROME_CONV_BUFFER */
117 
118 	return err;
119 }
120 
121 #else
122 
lvgl_allocate_rendering_buffers(lv_display_t * display)123 static int lvgl_allocate_rendering_buffers(lv_display_t *display)
124 {
125 	void *buf0 = NULL;
126 	void *buf1 = NULL;
127 	uint16_t buf_nbr_pixels;
128 	uint32_t buf_size;
129 	struct lvgl_disp_data *data = (struct lvgl_disp_data *)lv_display_get_user_data(display);
130 	uint16_t hor_res = lv_display_get_horizontal_resolution(display);
131 	uint16_t ver_res = lv_display_get_vertical_resolution(display);
132 
133 	buf_nbr_pixels = (CONFIG_LV_Z_VDB_SIZE * hor_res * ver_res) / 100;
134 	/* one horizontal line is the minimum buffer requirement for lvgl */
135 	if (buf_nbr_pixels < hor_res) {
136 		buf_nbr_pixels = hor_res;
137 	}
138 
139 	switch (data->cap.current_pixel_format) {
140 	case PIXEL_FORMAT_ARGB_8888:
141 		buf_size = 4 * buf_nbr_pixels;
142 		break;
143 	case PIXEL_FORMAT_RGB_888:
144 		buf_size = 3 * buf_nbr_pixels;
145 		break;
146 	case PIXEL_FORMAT_RGB_565:
147 		buf_size = 2 * buf_nbr_pixels;
148 		break;
149 	case PIXEL_FORMAT_MONO01:
150 	case PIXEL_FORMAT_MONO10:
151 		buf_size = buf_nbr_pixels / 8 + 8;
152 		buf_size += (buf_nbr_pixels % 8) == 0 ? 0 : 1;
153 		break;
154 	default:
155 		return -ENOTSUP;
156 	}
157 
158 	buf0 = lv_malloc(buf_size);
159 	if (buf0 == NULL) {
160 		LOG_ERR("Failed to allocate memory for rendering buffer");
161 		return -ENOMEM;
162 	}
163 
164 #ifdef CONFIG_LV_Z_DOUBLE_VDB
165 	buf1 = lv_malloc(buf_size);
166 	if (buf1 == NULL) {
167 		lv_free(buf0);
168 		LOG_ERR("Failed to allocate memory for rendering buffer");
169 		return -ENOMEM;
170 	}
171 #endif
172 
173 #if ALLOC_MONOCHROME_CONV_BUFFER
174 	void *vtile_buf = lv_malloc(buf_size);
175 
176 	if (vtile_buf == NULL) {
177 		lv_free(buf0);
178 		lv_free(buf1);
179 		LOG_ERR("Failed to allocate memory for vtile buffer");
180 		return -ENOMEM;
181 	}
182 	lvgl_set_mono_conversion_buffer(vtile_buf, buf_size);
183 #endif /* ALLOC_MONOCHROME_CONV_BUFFER */
184 
185 	lv_display_set_buffers(display, buf0, buf1, buf_size, LV_DISPLAY_RENDER_MODE_PARTIAL);
186 	return 0;
187 }
188 #endif /* CONFIG_LV_Z_BUFFER_ALLOC_STATIC */
189 
lv_mem_init(void)190 void lv_mem_init(void)
191 {
192 #ifdef CONFIG_LV_Z_MEM_POOL_SYS_HEAP
193 	lvgl_heap_init();
194 #endif /* CONFIG_LV_Z_MEM_POOL_SYS_HEAP */
195 }
196 
lv_mem_deinit(void)197 void lv_mem_deinit(void)
198 {
199 	/* Reinitializing the heap clears all allocations, no action needed */
200 }
201 
lv_mem_monitor_core(lv_mem_monitor_t * mon_p)202 void lv_mem_monitor_core(lv_mem_monitor_t *mon_p)
203 {
204 	memset(mon_p, 0, sizeof(lv_mem_monitor_t));
205 
206 #if CONFIG_LV_Z_MEM_POOL_SYS_HEAP
207 	struct sys_memory_stats stats;
208 
209 	lvgl_heap_stats(&stats);
210 	mon_p->used_pct =
211 		(stats.allocated_bytes * 100) / (stats.allocated_bytes + stats.free_bytes);
212 	mon_p->max_used = stats.max_allocated_bytes;
213 #else
214 	LOG_WRN_ONCE("Memory statistics only supported for CONFIG_LV_Z_MEM_POOL_SYS_HEAP");
215 #endif /* CONFIG_LV_Z_MEM_POOL_SYS_HEAP */
216 }
217 
lv_mem_test_core(void)218 lv_result_t lv_mem_test_core(void)
219 {
220 	/* Not supported for now */
221 	return LV_RESULT_OK;
222 }
223 
lvgl_init(void)224 int lvgl_init(void)
225 {
226 	const struct device *display_dev = DEVICE_DT_GET(DISPLAY_NODE);
227 
228 	int err = 0;
229 
230 	if (!device_is_ready(display_dev)) {
231 		LOG_ERR("Display device not ready.");
232 		return -ENODEV;
233 	}
234 
235 #if CONFIG_LV_Z_LOG_LEVEL != 0
236 	lv_log_register_print_cb(lvgl_log);
237 #endif
238 
239 	lv_init();
240 	lv_tick_set_cb(k_uptime_get_32);
241 
242 #ifdef CONFIG_LV_Z_USE_FILESYSTEM
243 	lvgl_fs_init();
244 #endif
245 
246 	disp_data.display_dev = display_dev;
247 	display_get_capabilities(display_dev, &disp_data.cap);
248 
249 	display = lv_display_create(disp_data.cap.x_resolution, disp_data.cap.y_resolution);
250 	if (!display) {
251 		return -ENOMEM;
252 	}
253 	lv_display_set_user_data(display, &disp_data);
254 
255 	if (set_lvgl_rendering_cb(display) != 0) {
256 		LOG_ERR("Display not supported.");
257 		return -ENOTSUP;
258 	}
259 
260 	err = lvgl_allocate_rendering_buffers(display);
261 	if (err != 0) {
262 		return err;
263 	}
264 
265 #ifdef CONFIG_LV_Z_FULL_REFRESH
266 	lv_display_set_render_mode(display, LV_DISPLAY_RENDER_MODE_FULL);
267 #endif
268 
269 	err = lvgl_init_input_devices();
270 	if (err < 0) {
271 		LOG_ERR("Failed to initialize input devices.");
272 		return err;
273 	}
274 
275 	return 0;
276 }
277 
278 #ifdef CONFIG_LV_Z_AUTO_INIT
279 SYS_INIT(lvgl_init, APPLICATION, CONFIG_LV_Z_INIT_PRIORITY);
280 #endif /* CONFIG_LV_Z_AUTO_INIT */
281