Edit online

LVGL V9 性能优化

3 Mar 2025
Read time: 6 minute(s)

设置图片缓存张数

当新的图片被加载时,如果当前缓存未达到设定的上限,该图片会被添加到缓存中。在内存有限的情况下,可以通过以下方式进一步优化:

  • 根据实际需求,动态调整图片缓存的大小。例如,在需要频繁切换的场景中,可以适当增加缓存数量以提高响应速度;而在其它场景中,则可以减少缓存数量以节省内存。
  • 释放不必要的缓存。在确定某些图片不再需要时,及时释放其缓存可以有效节省内存。例如,在场景切换时,可以调用lv_img_cache_invalidate_src(NULL)来释放所有缓存。
    // 释放对象 img_obj 的图片缓存
    lv_image_cache_drop(lv_image_get_src(img_obj));
    
    // 释放所有的图片缓存,在场景切换时候,确定不需要已有的图片缓存可以这么调用
    lv_image_cache_drop(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

设置图片缓存大小

设置图片的缓存 size,缓存越大,运行速度越快,但是占用的内存也会越多。
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
        (0x800000) LVGL image cached size
        (20)  LVGL image header cached number
    (/rodata/lvgl_data) LVGL Resource Directory

设置图片头信息缓存个数

对图片的头信息进行缓存,图片的头信息一般占用内存不多,但是如果图像的头信息需要频繁的从文件系统中去读取,也会影响运行速度, 用户可以根据需要,设置合适的图片头信息缓存个数。
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
        (0x800000) LVGL image cached size
        (20)  LVGL image header 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 中的宏定义来实现。
1. lv_conf.h 中的宏定义
宏定义参数 描述
LV_FREETYPE_USE_LVGL_PORT 决定是否让 FreeType 使用 LVGL 的内存和文件接口。
  • 设置为 0 时,表示不使用 LVGL 的内存和文件接口。
LV_FREETYPE_CACHE_FT_GLYPH_CNT 设置 FreeType 缓存的字形数量。
  • 数值越大,运行速度越快,但占用的内存也会越多。
#define LV_USE_FREETYPE 1
#if LV_USE_FREETYPE
    /*Let FreeType to use LVGL memory and file porting*/
    #define LV_FREETYPE_USE_LVGL_PORT 0

    /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached.
    *The higher the value, the more memory will be used.*/
    #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256
#endif

其他优化方式

  1. 对于不透明的图片,可以选择采用 JPEG 格式。JPEG 图片的解码速度通常比 PNG 速度快。
  2. 在定时器或动画回调函数中,避免执行耗时过长的函数,以免影响 UI 的流畅性。可以考虑以下方法:
    • LVGL 的 UI 绘制是单线程的,将耗时操作放到后台线程中执行,避免阻塞主线程。
    • 将大任务拆分成多个小任务,通过多次调用来完成,每次只处理一小部分工作。

[1]Yan Wang速度优化中和上面的步骤是不是重叠?2025/07/0108:48:28+00:00速度优化

  1. 多利用G2D模块进行绘制d
    1. 绘制一个复杂的背景,不如将其换成图片。
    2. 如果文字内容相对固定,不如换成图片。
    3. 绘制大块颜色,使用GE渲染函数
    4. 绘制JEPG的图片比PNG快,底图推荐使用JPEG
  2. 减少重复的绘制

    减少重复的绘制:重复绘制的部分透明度设为 LV_OPA_0.

    不需要绘制的内容透明度设为LV_OPA_0,LVGL 绘制的时候就会忽略它,就不会绘制这个部分。

  3. 简化流程:通过整合减少对象的绘制
  4. 如果内存充裕,增大缓存空间

    增大图片缓存,减少了加载和解码的时间.

  5. 如果内存充裕,增大缓存空间

    增大字体缓存,减少了加载和内存申请的时间。

Internal

[2]Yan Wang是不是和上面的步骤有重叠2025/07/0108:48:39+00:00内存优化

  1. 优化显示内存占用
    2. 显示内存占用
    格式 双framebuffer 屏幕是否旋转 内存占用
    1024 600 ARGB8888 1024x600x4x2 = 4.8MB
    1024 600 RGB565 1024x600x2x2 = 2.4MB
    1024 600 ARGB8888 1024x600x4x2 +1024x600x4 = 7.2MB
    1024 600 RGB565 1024x600x2x2 + 1024x600x2= 3.6 MB

    对显示效果没有非常高的要求,framebuffer选择rgb565[3]Yan Wanginsert2025/07/0203:26:40+00:00

    不开启旋转或者选择竖屏竖用,横屏横用的显示屏[4]Yan Wanginsert2025/07/0203:26:39+00:00

  2. 优化解码内存占用
    1. 如果不是必须包含透明度,使用[5]Yan Wanginsert2025/07/0203:26:46+00:00 JPEG[6]Yan Wanginsert2025/07/0203:26:44+00:00 图片替换[7]Yan Wanginsert2025/07/0203:26:46+00:00 PNG[8]Yan Wanginsert2025/07/0203:26:47+00:00 图片[9]Yan Wanginsert2025/07/0203:26:49+00:00。。
      格式 解码内存占用
      1024 600 PNG 1024x600x4 = 2.4MB
      1024 600 JPEG 1024x600x2 = 1.2MB
    2. 当内存比较紧张的时候,需要对图片缓存的释放进行控制[10]Yan Wanginsert2025/07/0203:26:51+00:00
Internal