New to LVGL. It is going well, but I am starting to get a bad feeling about all the memory allocations going on. Is it actually required to allocate an object just to draw a rectangle from a custom widget? I tried to do that and it insisted on filling in the background. I then turned the rectangle into four sides to draw individually and it still filled in the background. Is there an interface that allows primitive drawing similar to windows device context or stemwin?
There is not. an lv_obj_t needs to be allocated in order to draw primitives. This is done by use of the LVGL canvas widget and it’s associated functions/structures.
And the canvas widget needs to allocate the entire pixel area if I read that correctly. As I am in the early stages of picking a graphics system, does anyone know of a similar free system with a more traditional approach of using clipping regions?
It really depends on what you are exactly trying to do…
There is no need to allocate a buffer that is the entire size of the display. while you will have to provide some kind of a buffer to LVGL for the frame buffer and also for the canvas widget itself it doesn’t have to be a whole display sized buffer.
LVGL is a complete UI framework. It was not written to be a drawing/rendering framework. I don’t know of any framework that only provides basic drawing features like circles, rectangles, polygons, lines, text, gradients, masking, etc… Not saying that one doesn’t exist but typically a very basic drawing type of framework is usually provided along with display driver libraries.
if you are not able to find a rendering/drawing framework that has all of the things you want I know that you should be able to locate code examples of how to do specific portions so you could hobble together something that would do what you want ti to do.
what you looking for is a low level c framework for rendering/drawing primitives.
Ideally, I would be looking for a C++ framework because C++ done in “C” is a bit awkward. My only fear of C++ is sometimes developers forget they are in the embedded world and they assume that a massive amount of memory allocations for objects are going to come without a performance penalty. It would have the ability for parent and child windows and understand that a window being drawn might be partially obscured. And it would expose the lower layers so that it would be easy to write custom widgets. Some basic ability to integrate with hardware acceleration features would be nice. For example, an SPI or parallel LCD will have a hardware scroll feature allowing a region of the screen to be scrolled. It would be nice to make use of that for a scrolling chart display rather than have to redraw the entire region. In other words, I do want “a full package”, but part of being full should be access to lower-layer drawing routines.
I’m still thinking that it is somewhere here. For example the label drawing object works with a clipping area. I will keep trying, but if anyone has any advice on how to reach the drawing layers, it would be appreciated.
Hi @JK232
You can draw primitives either using the canvas or using object’s draw main task:
Draw main event example
void draw_rect_event_cb(lv_event_t * e)
{
lv_obj_t * obj = lv_event_get_target_obj(e);
lv_layer_t * layer = lv_event_get_layer(e);
lv_draw_rect_dsc_t dsc;
lv_draw_rect_dsc_init(&dsc);
dsc.bg_color = lv_palette_main(LV_PALETTE_RED);
lv_area_t obj_coords;
lv_obj_get_coords(obj, &obj_coords);
lv_draw_rect(layer, &dsc, &obj_coords);
}
void example_rectangle(void)
{
lv_obj_t * obj = lv_obj_create(lv_screen_active());
lv_obj_set_size(obj, LV_PCT(20), LV_PCT(20));
lv_obj_center(obj);
lv_obj_add_event_cb(obj, draw_rect_event_cb, LV_EVENT_DRAW_MAIN, NULL);
lv_obj_set_style_radius(obj, 0, LV_PART_MAIN);
}
The same thing can be done for lines, triangles, … other draw primitives
Canvas example
The second option is using the canvas widget, in this case you need to provide a draw buffer so the canvas can render the necessary primitives, there’s a lot of examples here: Canvas (lv_canvas) - LVGL 9.5 documentation
The draw buffer provided to the canvas can be some static memory which will reduce memory allocations
Note that memory is allocated from an static memory arena and the size is set by the user so you can control how much memory LVGL has access to
I also thought it would be worth to point out that we’re also working on LVGL Safe, an UI library for safety critical applications which doesn’t use memory allocations, this might be a good fit for your company
