With LVGL, you don't need to draw anything manually. Just create objects (like buttons, labels, arc, etc.), move and change them, and LVGL will refresh and redraw what is required.
However, it can be useful to have a basic understanding of how drawing happens in LVGL to add customization, make it easier to find bugs or just out of curiosity.
The basic concept is to not draw directly onto the display but rather to first draw on an internal draw buffer. When a drawing (rendering) is ready that buffer is copied to the display.
The draw buffer can be smaller than a display's size. LVGL will simply render in "tiles" that fit into the given draw buffer.
This approach has two main advantages compared to directly drawing to the display:
Note that this concept is different from "traditional" double buffering where there are two display sized frame buffers: one holds the current image to show on the display, and rendering happens to the other (inactive) frame buffer, and they are swapped when the rendering is finished. The main difference is that with LVGL you don't have to store two frame buffers (which usually requires external RAM) but only smaller draw buffer(s) that can easily fit into internal RAM.
Be sure to get familiar with the Buffering modes of LVGL first.
LVGL refreshes the screen in the following steps:
LV_DISP_DEF_REFR_PERIOD
(set in lv_conf.h
) the following happens:
flush_cb
from the display driver to refresh the display.When an area is redrawn the library searches the top-most object which covers that area and starts drawing from that object. For example, if a button's label has changed, the library will see that it's enough to draw the button under the text and it's not necessary to redraw the display under the rest of the button too.
The difference between buffering modes regarding the drawing mechanism is the following:
lv_disp_flush_ready()
(called from flush_cb
) before starting to redraw the next part.flush_cb
because the flushing should be done by DMA (or similar hardware) in the background.flush_cb
should only swap the addresses of the frame buffers.Masking is the basic concept of LVGL's draw engine. To use LVGL it's not required to know about the mechanisms described here but you might find interesting to know how drawing works under hood. Knowing about masking comes in handy if you want to customize drawing.
To learn about masking let's see the steps of drawing first. LVGL performs the following steps to render any shape, image or text. It can be considered as a drawing pipeline.
lv_draw_rect_dsc_t
). This gives us the parameters for drawing, for example colors, widths, opacity, fonts, radius, etc.lv_draw_rect()
). It will render the primitive shape to the current draw buffer.LVGL has the following built-in mask types which can be calculated and applied real-time:
LV_DRAW_MASK_TYPE_LINE
Removes a side from a line (top, bottom, left or right). lv_draw_line
uses four instances of it.
Essentially, every (skew) line is bounded with four line masks forming a rectangle.LV_DRAW_MASK_TYPE_RADIUS
Removes the inner or outer corners of a rectangle with a radiused transition. It's also used to create circles by setting the radius to large value (LV_RADIUS_CIRCLE
)LV_DRAW_MASK_TYPE_ANGLE
Removes a circular sector. It is used by lv_draw_arc
to remove the "empty" sector.LV_DRAW_MASK_TYPE_FADE
Create a vertical fade (change opacity)LV_DRAW_MASK_TYPE_MAP
The mask is stored in a bitmap array and the necessary parts are appliedMasks are used to create almost every basic primitive:
Every mask type has a related parameter structure to describe the mask's data. The following parameter types exist:
lv_draw_mask_line_param_t
lv_draw_mask_radius_param_t
lv_draw_mask_angle_param_t
lv_draw_mask_fade_param_t
lv_draw_mask_map_param_t
lv_draw_mask_<type>_init
. See lv_draw_mask.h
for the whole API.int16_t mask_id = lv_draw_mask_add(¶m, ptr)
. ptr
can be any pointer to identify the mask, (NULL
if unused).lv_draw_mask_remove_id(mask_id)
or lv_draw_mask_remove_custom(ptr)
.lv_draw_mask_free_param(¶m)
.A parameter can be added and removed any number of times, but it needs to be freed when not required anymore.
lv_draw_mask_add
saves only the pointer of the mask so the parameter needs to be valid while in use.
Although widgets can be easily customized by styles there might be cases when something more custom is required. To ensure a great level of flexibility LVGL sends a lot of events during drawing with parameters that tell what LVGL is about to draw. Some fields of these parameters can be modified to draw something else or any custom drawing operations can be added manually.
A good use case for this is the Button matrix widget. By default, its buttons can be styled in different states, but you can't style the buttons one by one. However, an event is sent for every button and you can, for example, tell LVGL to use different colors on a specific button or to manually draw an image on some buttons.
Each of these events is described in detail below.
These events are related to the actual drawing of an object. E.g. the drawing of buttons, texts, etc. happens here.
lv_event_get_clip_area(event)
can be used to get the current clip area. The clip area is required in draw functions to make them draw only on a limited area.
Sent before starting to draw an object. This is a good place to add masks manually. E.g. add a line mask that "removes" the right side of an object.
The actual drawing of an object happens in this event. E.g. a rectangle for a button is drawn here. First, the widgets' internal events are called to perform drawing and after that you can draw anything on top of them. For example you can add a custom text or an image.
Called when the main drawing is finished. You can draw anything here as well and it's also a good place to remove any masks created in LV_EVENT_DRAW_MAIN_BEGIN
.
Post drawing events are called when all the children of an object are drawn. For example LVGL use the post drawing phase to draw scrollbars because they should be above all of the children.
lv_event_get_clip_area(event)
can be used to get the current clip area.
Sent before starting the post draw phase. Masks can be added here too to mask out the post drawn content.
The actual drawing should happen here.
Called when post drawing has finished. If masks were not removed in LV_EVENT_DRAW_MAIN_END
they should be removed here.
When LVGL draws a part of an object (e.g. a slider's indicator, a table's cell or a button matrix's button) it sends events before and after drawing that part with some context of the drawing. This allows changing the parts on a very low level with masks, extra drawing, or changing the parameters that LVGL is planning to use for drawing.
In these events an lv_obj_draw_part_t
structure is used to describe the context of the drawing. Not all fields are set for every part and widget.
To see which fields are set for a widget refer to the widget's documentation.
lv_obj_draw_part_t
has the following fields:
// Always set
const lv_area_t * clip_area; // The current clip area, required if you need to draw something in the event
uint32_t part; // The current part for which the event is sent
uint32_t id; // The index of the part. E.g. a button's index on button matrix or table cell index.
// Draw desciptors, set only if related
lv_draw_rect_dsc_t * rect_dsc; // A draw descriptor that can be modified to changed what LVGL will draw. Set only for rectangle-like parts
lv_draw_label_dsc_t * label_dsc; // A draw descriptor that can be modified to changed what LVGL will draw. Set only for text-like parts
lv_draw_line_dsc_t * line_dsc; // A draw descriptor that can be modified to changed what LVGL will draw. Set only for line-like parts
lv_draw_img_dsc_t * img_dsc; // A draw descriptor that can be modified to changed what LVGL will draw. Set only for image-like parts
lv_draw_arc_dsc_t * arc_dsc; // A draw descriptor that can be modified to changed what LVGL will draw. Set only for arc-like parts
// Other parameters
lv_area_t * draw_area; // The area of the part being drawn
const lv_point_t * p1; // A point calculated during drawing. E.g. a point of a chart or the center of an arc.
const lv_point_t * p2; // A point calculated during drawing. E.g. a point of a chart.
char text[16]; // A text calculated during drawing. Can be modified. E.g. tick labels on a chart axis.
lv_coord_t radius; // E.g. the radius of an arc (not the corner radius).
int32_t value; // A value calculated during drawing. E.g. Chart's tick line value.
const void * sub_part_ptr; // A pointer the identifies something in the part. E.g. chart series.
lv_event_get_draw_part_dsc(event)
can be used to get a pointer to lv_obj_draw_part_t
.
Start the drawing of a part. This is a good place to modify the draw descriptors (e.g. rect_dsc
), or add masks.
Finish the drawing of a part. This is a good place to draw extra content on the part or remove masks added in LV_EVENT_DRAW_PART_BEGIN
.
This event is used to check whether an object fully covers an area or not.
lv_event_get_cover_area(event)
returns a pointer to an area to check and lv_event_set_cover_res(event, res)
can be used to set one of these results:
LV_COVER_RES_COVER
the area is fully covered by the objectLV_COVER_RES_NOT_COVER
the area is not covered by the objectLV_COVER_RES_MASKED
there is a mask on the object, so it does not fully cover the areaHere are some reasons why an object would be unable to fully cover an area:
In short if for any reason the area below an object is visible than the object doesn't cover that area.
Before sending this event LVGL checks if at least the widget's coordinates fully cover the area or not. If not the event is not called.
You need to check only the drawing you have added. The existing properties known by a widget are handled in its internal events.
E.g. if a widget has > 0 radius it might not cover an area, but you need to handle radius
only if you will modify it and the widget won't know about it.
If you need to draw outside a widget, LVGL needs to know about it to provide extra space for drawing. Let's say you create an event which writes the current value of a slider above its knob. In this case LVGL needs to know that the slider's draw area should be larger with the size required for the text.
You can simply set the required draw area with lv_event_set_ext_draw_size(e, size)
.