lvgl V7中lv_canvas_clean函数实现
时间: 2023-08-05 15:03:35 浏览: 151
在LVGL V7中,`lv_canvas_clean()`函数的实现如下所示:
```c
void lv_canvas_clean(lv_obj_t * canvas)
{
LV_ASSERT_OBJ(canvas, LV_OBJX_NAME);
lv_canvas_ext_t * ext = lv_obj_get_ext_attr(canvas);
if(ext->dsc.header.always_zero == 0) return;
lv_color_t color = lv_color_hex(ext->dsc.header.color);
lv_disp_t * disp = lv_obj_get_disp(canvas);
lv_disp_buf_t * draw_buf = lv_disp_get_draw_buf(disp);
uint32_t w = lv_obj_get_width(canvas);
uint32_t h = lv_obj_get_height(canvas);
uint32_t x = lv_obj_get_x(canvas);
uint32_t y = lv_obj_get_y(canvas);
if(ext->clip_area.width != 0 && ext->clip_area.height != 0) {
lv_area_t rel_area;
lv_obj_get_coords(canvas, &rel_area);
lv_area_intersect(&rel_area, &ext->clip_area, &rel_area);
if(lv_area_get_width(&rel_area) == 0 || lv_area_get_height(&rel_area) == 0) return;
lv_area_t abs_area;
lv_obj_get_abs_coords(canvas, &abs_area);
lv_disp_drv_t * drv = lv_disp_get_drv(disp);
uint32_t y1 = abs_area.y1 + rel_area.y1;
uint32_t x1 = abs_area.x1 + rel_area.x1;
uint32_t len = lv_area_get_width(&rel_area);
uint32_t px_offset = (y1 * draw_buf->w + x1);
for(uint32_t i = 0; i < lv_area_get_height(&rel_area); i++) {
lv_disp_flush_dsc_t flush_dsc;
lv_disp_flush_dsc_init(&flush_dsc);
flush_dsc.area.x1 = x1;
flush_dsc.area.x2 = x1 + len - 1;
flush_dsc.area.y1 = y1 + i;
flush_dsc.area.y2 = y1 + i;
lv_disp_flush_drv_t flush_drv;
lv_disp_flush_drv_init(&flush_drv);
flush_drv.draw_buf = draw_buf;
flush_drv.flush_cb = drv->flush_cb;
flush_drv.user_data = drv->user_data;
flush_drv.wait_cb = drv->wait_cb;
flush_drv.monitor_cb = drv->monitor_cb;
flush_drv.hor_res = drv->hor_res;
flush_drv.ver_res = drv->ver_res;
/* Fill the line */
lv_memset_00(&draw_buf->buf_act[px_offset], len * sizeof(lv_color_t));
lv_disp_flush(&flush_drv, &flush_dsc);
}
} else {
lv_disp_fill(&color, NULL, w * h, x, y);
}
}
```
该函数主要有以下几个步骤:
1. 获取canvas控件对象的扩展属性`ext`。
2. 判断`ext->dsc.header.always_zero`是否为0。如果为0,表示该canvas是不透明的,已经被完全覆盖,无需再次清空。如果不为0,则需要进行清空操作。
3. 获取canvas的宽度`w`、高度`h`、x坐标`x`和y坐标`y`。
4. 如果canvas有剪切区域,则获取剪切区域的相对位置和绝对位置,并且遍历剪切区域的每一行,将该行的像素点设置为背景色。
5. 如果canvas没有剪切区域,则直接使用`lv_disp_fill()`函数进行填充。
总的来说,`lv_canvas_clean()`函数的实现比较简单,主要是对canvas的背景色进行填充,以达到清空canvas的效果。
阅读全文