LVGL V8 性能优化
3 Mar 2025
Read time: 4 minute(s)
设置图片缓存张数
当新的图片被加载时,如果当前缓存未达到设定的上限,该图片会被添加到缓存中。在内存有限的情况下,可以通过以下方式进一步优化:
- 根据实际需求,动态调整图片缓存的大小。例如,在需要频繁切换的场景中,可以适当增加缓存数量以提高响应速度;而在其它场景中,则可以减少缓存数量以节省内存。
-
释放不必要的缓存。在确定某些图片不再需要时,及时释放其缓存可以有效节省内存。例如,在场景切换时,可以调用lv_img_cache_invalidate_src(NULL)来释放所有缓存。
// 释放对象 img_obj 的图片缓存 lv_img_cache_invalidate_src(lv_img_get_src(img_obj)); // 释放所有的图片缓存,在场景切换时候,确定不需要已有的图片缓存可以这么调用 lv_img_cache_invalidate_src(NULL);
如下配置中设置了八张图片缓存:Application options ---> *** Filesystem related *** [*] Using File System Image 0 ---> [ ] Using File System Image 1 ---- *** lvgl demo select related *** -*- LVGL (official): powerful and easy-to-use embedded GUI library ---> -*- ArtInChip lvgl demo select lvgl demo (lvgl demo with basic function) ---> (X) lvgl demo with basic function ( ) lvgl demo of meter (16) LVGL color depth(32/16) (8) LVGL image cached number (/rodata/lvgl_data) LVGL Resource Directory
采用 rgb565 格式
选择合适的颜色深度和图片格式可以有效减少处理的数据量,例如 rgb565 格式,从而提升 UI 的处理速度。
显示采用 rgb565 格式和 argb8888 相比减少了处理的数据量,可以提升 UI 的处理速度。以下示例中,LVGL color depth 为 16,并且配置
framebuffer 为
rgb565:
Application options ---> *** Filesystem related *** [*] Using File System Image 0 ---> [ ] Using File System Image 1 ---- *** lvgl demo select related *** -*- LVGL (official): powerful and easy-to-use embedded GUI library ---> -*- ArtInChip lvgl demo select lvgl demo (lvgl demo with basic function) ---> (X) lvgl demo with basic function ( ) lvgl demo of meter (16) LVGL color depth(32/16) (8) LVGL image cached number (/rodata/lvgl_data) LVGL Resource Directory
Board options ---> [*] Using Display Engine (DE) Display Parameter ---> select framebuffer format (rgb565) ---> [*] Support double framebuffer [*] Enable Display Dither
设置 FreeType 字体缓存个数
在 LVGL 中,FreeType 字体缓存的配置可以通过修改头文件 lv_conf.h 中的宏定义来实现。
宏定义参数 | 描述 |
---|---|
LV_FREETYPE_CACHE_SIZE | 设置 FreeType 缓存的总大小(以字节为单位)
|
LV_FREETYPE_SBIT_CACHE: | 选择位图缓存的类型。
|
LV_FREETYPE_CACHE_FT_FACES | 设置缓存管理的 FT_Face 对象的最大数量。
|
LV_FREETYPE_CACHE_FT_SIZES | 设置缓存管理的 FT_Size 对象的最大数量。
|
#define LV_USE_FREETYPE 1 #if LV_USE_FREETYPE // Memory used by FreeType to cache characters [bytes] #define LV_FREETYPE_CACHE_SIZE (128 * 1024) #if LV_FREETYPE_CACHE_SIZE >= 0 // 1: bitmap cache use the sbit cache, 0:bitmap cache use the image cache. // sbit cache:it is much more memory efficient for small bitmaps(font size < 256) // if font size >= 256, must be configured as image cache */ #define LV_FREETYPE_SBIT_CACHE 0 // Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. // (0:use system defaults) #define LV_FREETYPE_CACHE_FT_FACES 0 #define LV_FREETYPE_CACHE_FT_SIZES 0 #endif #endif
其他优化方式
-
对于不透明的图片,可以选择采用 JPEG 格式。JPEG 图片的解码速度通常比 PNG 速度快。
-
在定时器或动画回调函数中,避免执行耗时过长的函数,以免影响 UI 的流畅性。可以考虑以下方法:
-
LVGL 的 UI 绘制是单线程的,将耗时操作放到后台线程中执行,避免阻塞主线程。
-
将大任务拆分成多个小任务,通过多次调用来完成,每次只处理一小部分工作。
-