1 /**
2  * @file lv_init.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "misc/lv_timer_private.h"
10 #include "misc/lv_profiler_builtin_private.h"
11 #include "misc/lv_anim_private.h"
12 #include "draw/lv_image_decoder_private.h"
13 #include "draw/lv_draw_buf_private.h"
14 #include "core/lv_refr_private.h"
15 #include "core/lv_obj_style_private.h"
16 #include "core/lv_group_private.h"
17 #include "lv_init.h"
18 #include "core/lv_global.h"
19 #include "core/lv_obj.h"
20 #include "display/lv_display_private.h"
21 #include "indev/lv_indev_private.h"
22 #include "layouts/lv_layout_private.h"
23 #include "libs/bin_decoder/lv_bin_decoder.h"
24 #include "libs/bmp/lv_bmp.h"
25 #include "libs/ffmpeg/lv_ffmpeg.h"
26 #include "libs/freetype/lv_freetype.h"
27 #include "libs/fsdrv/lv_fsdrv.h"
28 #include "libs/gif/lv_gif.h"
29 #include "libs/tjpgd/lv_tjpgd.h"
30 #include "libs/libjpeg_turbo/lv_libjpeg_turbo.h"
31 #include "libs/lodepng/lv_lodepng.h"
32 #include "libs/libpng/lv_libpng.h"
33 #include "libs/tiny_ttf/lv_tiny_ttf.h"
34 #include "draw/lv_draw.h"
35 #include "misc/lv_async.h"
36 #include "misc/lv_fs_private.h"
37 #include "widgets/span/lv_span.h"
38 #include "themes/simple/lv_theme_simple.h"
39 #include "misc/lv_fs.h"
40 #include "osal/lv_os_private.h"
41 #include "others/sysmon/lv_sysmon_private.h"
42 #include "others/xml/lv_xml.h"
43 
44 #if LV_USE_NEMA_GFX
45     #include "draw/nema_gfx/lv_draw_nema_gfx.h"
46 #endif
47 #if LV_USE_DRAW_VGLITE
48     #include "draw/nxp/vglite/lv_draw_vglite.h"
49 #endif
50 #if LV_USE_PXP
51     #if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP
52         #include "draw/nxp/pxp/lv_draw_pxp.h"
53     #endif
54 #endif
55 #if LV_USE_DRAW_DAVE2D
56     #include "draw/renesas/dave2d/lv_draw_dave2d.h"
57 #endif
58 #if LV_USE_DRAW_SDL
59     #include "draw/sdl/lv_draw_sdl.h"
60 #endif
61 #if LV_USE_DRAW_VG_LITE
62     #include "draw/vg_lite/lv_draw_vg_lite.h"
63 #endif
64 #if LV_USE_DRAW_DMA2D
65     #include "draw/dma2d/lv_draw_dma2d.h"
66 #endif
67 #if LV_USE_DRAW_OPENGLES
68     #include "draw/opengles/lv_draw_opengles.h"
69 #endif
70 #if LV_USE_WINDOWS
71     #include "drivers/windows/lv_windows_context.h"
72 #endif
73 #if LV_USE_UEFI
74     #include "drivers/uefi/lv_uefi_context.h"
75 #endif
76 #if LV_USE_EVDEV
77     #include "drivers/evdev/lv_evdev_private.h"
78 #endif
79 
80 /*********************
81  *      DEFINES
82  *********************/
83 #define lv_initialized  LV_GLOBAL_DEFAULT()->inited
84 #define lv_deinit_in_progress  LV_GLOBAL_DEFAULT()->deinit_in_progress
85 
86 /**********************
87  *      TYPEDEFS
88  **********************/
89 
90 /**********************
91  *  STATIC PROTOTYPES
92  **********************/
93 
94 /**********************
95  *  STATIC VARIABLES
96  **********************/
97 #if LV_ENABLE_GLOBAL_CUSTOM == 0
98     lv_global_t lv_global;
99 #endif
100 
101 /**********************
102  *      MACROS
103  **********************/
104 
105 #ifndef LV_GLOBAL_INIT
106     #define LV_GLOBAL_INIT(__GLOBAL_PTR)    lv_global_init((lv_global_t *)(__GLOBAL_PTR))
107 #endif
108 
109 /**********************
110  *   GLOBAL FUNCTIONS
111  **********************/
lv_global_init(lv_global_t * global)112 static inline void lv_global_init(lv_global_t * global)
113 {
114     LV_ASSERT_NULL(global);
115 
116     if(global == NULL) {
117         LV_LOG_ERROR("lv_global cannot be null");
118         return;
119     }
120 
121     lv_memzero(global, sizeof(lv_global_t));
122 
123     lv_ll_init(&(global->disp_ll), sizeof(lv_display_t));
124     lv_ll_init(&(global->indev_ll), sizeof(lv_indev_t));
125 
126     global->memory_zero = ZERO_MEM_SENTINEL;
127     global->style_refresh = true;
128     global->layout_count = LV_LAYOUT_LAST;
129     global->style_last_custom_prop_id = (uint32_t)LV_STYLE_LAST_BUILT_IN_PROP;
130     global->event_last_register_id = LV_EVENT_LAST;
131     lv_rand_set_seed(0x1234ABCD);
132 
133 #ifdef LV_LOG_PRINT_CB
134     void LV_LOG_PRINT_CB(lv_log_level_t, const char * txt);
135     global->custom_log_print_cb = LV_LOG_PRINT_CB;
136 #endif
137 
138 #if defined(LV_DRAW_SW_SHADOW_CACHE_SIZE) && LV_DRAW_SW_SHADOW_CACHE_SIZE > 0
139     global->sw_shadow_cache.cache_size = -1;
140     global->sw_shadow_cache.cache_r = -1;
141 #endif
142 }
143 
lv_cleanup_devices(lv_global_t * global)144 static inline void lv_cleanup_devices(lv_global_t * global)
145 {
146     LV_ASSERT_NULL(global);
147 
148     if(global) {
149         /* cleanup indev and display */
150         lv_ll_clear_custom(&(global->indev_ll), (void (*)(void *)) lv_indev_delete);
151         lv_ll_clear_custom(&(global->disp_ll), (void (*)(void *)) lv_display_delete);
152     }
153 }
154 
lv_is_initialized(void)155 bool lv_is_initialized(void)
156 {
157 #if LV_ENABLE_GLOBAL_CUSTOM
158     if(LV_GLOBAL_DEFAULT()) return lv_initialized;
159     else return false;
160 #else
161     return lv_initialized;
162 #endif
163 }
164 
lv_init(void)165 void lv_init(void)
166 {
167     /*First initialize Garbage Collection if needed*/
168 #ifdef LV_GC_INIT
169     LV_GC_INIT();
170 #endif
171 
172     /*Do nothing if already initialized*/
173     if(lv_initialized) {
174         LV_LOG_WARN("lv_init: already initialized");
175         return;
176     }
177 
178     LV_LOG_INFO("begin");
179 
180     /*Initialize members of static variable lv_global */
181     LV_GLOBAL_INIT(LV_GLOBAL_DEFAULT());
182 
183     lv_mem_init();
184 
185     lv_draw_buf_init_handlers();
186 
187 #if LV_USE_SPAN != 0
188     lv_span_stack_init();
189 #endif
190 
191 #if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN
192     lv_profiler_builtin_config_t profiler_config;
193     lv_profiler_builtin_config_init(&profiler_config);
194     lv_profiler_builtin_init(&profiler_config);
195 #endif
196 
197     lv_os_init();
198 
199     lv_timer_core_init();
200 
201     lv_fs_init();
202 
203     lv_layout_init();
204 
205     lv_anim_core_init();
206 
207     lv_group_init();
208 
209 #if LV_USE_FREETYPE
210     /* Since the drawing unit needs to register the freetype event,
211      * initialize the freetype module first
212      */
213     lv_freetype_init(LV_FREETYPE_CACHE_FT_GLYPH_CNT);
214 #endif
215 
216     lv_draw_init();
217 
218 #if LV_USE_DRAW_SW
219     lv_draw_sw_init();
220 #endif
221 
222 #if LV_USE_NEMA_GFX
223     lv_draw_nema_gfx_init();
224 #endif
225 
226 #if LV_USE_DRAW_VGLITE
227     lv_draw_vglite_init();
228 #endif
229 
230 #if LV_USE_PXP
231 #if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP
232     lv_draw_pxp_init();
233 #endif
234 #endif
235 
236 #if LV_USE_DRAW_DAVE2D
237     lv_draw_dave2d_init();
238 #endif
239 
240 #if LV_USE_DRAW_SDL
241     lv_draw_sdl_init();
242 #endif
243 
244 #if LV_USE_DRAW_DMA2D
245     lv_draw_dma2d_init();
246 #endif
247 
248 #if LV_USE_DRAW_OPENGLES
249     lv_draw_opengles_init();
250 #endif
251 
252 #if LV_USE_WINDOWS
253     lv_windows_platform_init();
254 #endif
255 
256 #if LV_USE_UEFI
257     lv_uefi_platform_init();
258 #endif
259 
260     lv_obj_style_init();
261 
262     /*Initialize the screen refresh system*/
263     lv_refr_init();
264 
265 #if LV_USE_SYSMON
266     lv_sysmon_builtin_init();
267 #endif
268 
269     lv_image_decoder_init(LV_CACHE_DEF_SIZE, LV_IMAGE_HEADER_CACHE_DEF_CNT);
270     lv_bin_decoder_init();  /*LVGL built-in binary image decoder*/
271 
272 #if LV_USE_DRAW_VG_LITE
273     lv_draw_vg_lite_init();
274 #endif
275 
276     /*Test if the IDE has UTF-8 encoding*/
277     const char * txt = "Á";
278 
279     uint8_t * txt_u8 = (uint8_t *)txt;
280     if(txt_u8[0] != 0xc3 || txt_u8[1] != 0x81 || txt_u8[2] != 0x00) {
281         LV_LOG_WARN("The strings have no UTF-8 encoding. Non-ASCII characters won't be displayed.");
282     }
283 
284     uint32_t endianness_test = 0x11223344;
285     uint8_t * endianness_test_p = (uint8_t *) &endianness_test;
286     bool big_endian = endianness_test_p[0] == 0x11;
287 
288     if(big_endian) {
289         LV_ASSERT_MSG(LV_BIG_ENDIAN_SYSTEM == 1,
290                       "It's a big endian system but LV_BIG_ENDIAN_SYSTEM is not enabled in lv_conf.h");
291     }
292     else {
293         LV_ASSERT_MSG(LV_BIG_ENDIAN_SYSTEM == 0,
294                       "It's a little endian system but LV_BIG_ENDIAN_SYSTEM is enabled in lv_conf.h");
295     }
296 
297 #if LV_USE_ASSERT_MEM_INTEGRITY
298     LV_LOG_WARN("Memory integrity checks are enabled via LV_USE_ASSERT_MEM_INTEGRITY which makes LVGL much slower");
299 #endif
300 
301 #if LV_USE_ASSERT_OBJ
302     LV_LOG_WARN("Object sanity checks are enabled via LV_USE_ASSERT_OBJ which makes LVGL much slower");
303 #endif
304 
305 #if LV_USE_ASSERT_STYLE
306     LV_LOG_WARN("Style sanity checks are enabled that uses more RAM");
307 #endif
308 
309 #if LV_LOG_LEVEL == LV_LOG_LEVEL_TRACE
310     LV_LOG_WARN("Log level is set to 'Trace' which makes LVGL much slower");
311 #endif
312 
313 #if LV_USE_FS_FATFS != '\0'
314     lv_fs_fatfs_init();
315 #endif
316 
317 #if LV_USE_FS_STDIO != '\0'
318     lv_fs_stdio_init();
319 #endif
320 
321 #if LV_USE_FS_POSIX != '\0'
322     lv_fs_posix_init();
323 #endif
324 
325 #if LV_USE_FS_WIN32 != '\0'
326     lv_fs_win32_init();
327 #endif
328 
329 #if LV_USE_FS_MEMFS
330     lv_fs_memfs_init();
331 #endif
332 
333 #if LV_USE_FS_LITTLEFS
334     lv_fs_littlefs_init();
335 #endif
336 
337 #if LV_USE_FS_ARDUINO_ESP_LITTLEFS
338     lv_fs_arduino_esp_littlefs_init();
339 #endif
340 
341 #if LV_USE_FS_ARDUINO_SD
342     lv_fs_arduino_sd_init();
343 #endif
344 
345 #if LV_USE_FS_UEFI
346     lv_fs_uefi_init();
347 #endif
348 
349 #if LV_USE_LODEPNG
350     lv_lodepng_init();
351 #endif
352 
353 #if LV_USE_LIBPNG
354     lv_libpng_init();
355 #endif
356 
357 #if LV_USE_TJPGD
358     lv_tjpgd_init();
359 #endif
360 
361 #if LV_USE_LIBJPEG_TURBO
362     lv_libjpeg_turbo_init();
363 #endif
364 
365 #if LV_USE_BMP
366     lv_bmp_init();
367 #endif
368 
369     /*Make FFMPEG last because the last converter will be checked first and
370      *it's superior to any other */
371 #if LV_USE_FFMPEG
372     lv_ffmpeg_init();
373 #endif
374 
375 #if LV_USE_XML
376     lv_xml_init();
377 #endif
378 
379     lv_initialized = true;
380 
381     LV_LOG_TRACE("finished");
382 }
383 
lv_deinit(void)384 void lv_deinit(void)
385 {
386     /*Do nothing if already deinit*/
387     if(!lv_initialized) {
388         LV_LOG_WARN("lv_deinit: already deinit!");
389         return;
390     }
391 
392     if(lv_deinit_in_progress) return;
393 
394     lv_deinit_in_progress = true;
395 
396 #if LV_USE_SYSMON
397     lv_sysmon_builtin_deinit();
398 #endif
399 
400     lv_display_set_default(NULL);
401 
402     lv_cleanup_devices(LV_GLOBAL_DEFAULT());
403 
404 #if LV_USE_EVDEV
405     lv_evdev_deinit();
406 #endif
407 
408 #if LV_USE_SPAN != 0
409     lv_span_stack_deinit();
410 #endif
411 
412 #if LV_USE_FREETYPE
413     lv_freetype_uninit();
414 #endif
415 
416 #if LV_USE_THEME_DEFAULT
417     lv_theme_default_deinit();
418 #endif
419 
420 #if LV_USE_THEME_SIMPLE
421     lv_theme_simple_deinit();
422 #endif
423 
424 #if LV_USE_THEME_MONO
425     lv_theme_mono_deinit();
426 #endif
427 
428     lv_image_decoder_deinit();
429 
430     lv_refr_deinit();
431 
432     lv_obj_style_deinit();
433 
434 #if LV_USE_UEFI
435     lv_uefi_platform_deinit();
436 #endif
437 
438 #if LV_USE_PXP
439 #if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP
440     lv_draw_pxp_deinit();
441 #endif
442 #endif
443 
444 #if LV_USE_DRAW_VGLITE
445     lv_draw_vglite_deinit();
446 #endif
447 
448 #if LV_USE_DRAW_VG_LITE
449     lv_draw_vg_lite_deinit();
450 #endif
451 
452 #if LV_USE_DRAW_DMA2D
453     lv_draw_dma2d_deinit();
454 #endif
455 
456 #if LV_USE_DRAW_OPENGLES
457     lv_draw_opengles_deinit();
458 #endif
459 
460 #if LV_USE_DRAW_SW
461     lv_draw_sw_deinit();
462 #endif
463 
464     lv_draw_deinit();
465 
466     lv_group_deinit();
467 
468     lv_anim_core_deinit();
469 
470     lv_layout_deinit();
471 
472     lv_fs_deinit();
473 
474     lv_timer_core_deinit();
475 
476 #if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN
477     lv_profiler_builtin_uninit();
478 #endif
479 
480 #if LV_USE_OBJ_ID && LV_USE_OBJ_ID_BUILTIN
481     lv_objid_builtin_destroy();
482 #endif
483 
484     lv_mem_deinit();
485 
486     lv_initialized = false;
487 
488     LV_LOG_INFO("lv_deinit done");
489 
490 #if LV_USE_LOG
491     lv_log_register_print_cb(NULL);
492 #endif
493 
494 }
495 
496 /**********************
497  *   STATIC FUNCTIONS
498  **********************/
499