How to avoid a refresh problem?


When an img widget showing after it hidden very short term may cause the some content missing.

What MCU/Processor/Board and compiler are you using?


What LVGL version are you using?


What do you want to achieve?

Hide an img widget, and subsquently update an image to it and show it, correct content is displayed. Any solutions or suggetions would be appreciated.

What have you tried so far?

  1. canvas widget also has the same problem.
  2. The problem is caused by the mechanism of refreshing:
    When lv_img_set_src is called, nothing redrawing is happened until lv_task_handler is called.
  1. While showing the img widget after a short term, seems it’s states are confused?
  2. lv_refr_now can sync the image content, but need to mutex between lv_task_handler, howerver it’s a good method, and lv_refr_now may cause crash( seems by timers’ list competition) .

Code to reproduce

hide an display a gray image.

  const int SCR_WIDTH = 800;
  const int SCR_HEIGHT = 1280;
  uint8_t* img_data = (uint8_t*)malloc(SCR_WIDTH * SCR_HEIGHT * 4);

  memset(img_data, 0x88, SCR_WIDTH * SCR_HEIGHT * 4);
  lv_img_dsc_t img_dsc;

  memset(&img_dsc, 0x0, sizeof(img_dsc));
  img_dsc.header.w = SCR_WIDTH;
  img_dsc.header.h = SCR_HEIGHT;
  img_dsc.header.always_zero = 0; = LV_IMG_CF_TRUE_COLOR; = img_data;
  img_dsc.data_size = SCR_WIDTH * SCR_HEIGHT * 4;

  lv_obj_t* img_widget = lv_img_create(lv_scr_act());
  while (1) {
    lv_obj_add_flag(img_widget, LV_OBJ_FLAG_HIDDEN);
    usleep(10 * 1000);
    lv_img_set_src(img_widget, &img_dsc);
    lv_obj_clear_flag(img_widget, LV_OBJ_FLAG_HIDDEN);

Screenshot and/or video

Partition of the image can be display while sleeping 2 seconds.

If you are using multiple threads, you need to ensure that all LVGL API calls are guarded with a mutex, including lv_task_handler.

One thread should, in a loop: lock a mutex, run lv_task_handler, unlock the mutex, and sleep for 15-30ms.

The other thread also needs to lock & unlock the mutex whenever an LVGL API is used.

I have just updated the documentation on this with an example, it should be visible in a couple minutes.

It’s important to conform this point. Thank you very much:)