
Recall first from Styles, fonts, and images
- The default LVGL 9 theme provides Material-style widget defaults that a program can override property by property.
- State selectors determine which styles apply, and inheritable properties such as text font can fall back to a parent.
- A converted font's size depends on its glyph set, pixel size, bitmap depth, and supporting tables.
Budget memory for the display and widgets
3 min readThe last lesson's fonts and images consume memory; this lesson covers LVGL's memory model and touch input.
A touchscreen can report every tap correctly while a button still reacts late. The Linux input driver reports contact and position through evdev. LVGL must then process the input, draw the changed pixels and submit them to the display. The kernel's input event documentation describes the first step. Here you will follow the remaining work and measure it on the running UI.
LVGL's lv_mem subsystem provides allocation for internal objects and temporary data. In LVGL 9, lv_malloc() and lv_free() are the public allocation functions. The built-in allocator uses a pool whose initial size is set by LV_MEM_SIZE. A build can select a different allocation backend through LV_USE_STDLIB_MALLOC. With the built-in allocator, deleting a widget returns its allocated storage to the pool. Other allocations can reuse it, and the backing storage can remain resident in the Linux process. With many allocations of different sizes, free space can also be split into blocks too small for the next large request.
The display port supplies draw buffers, where LVGL writes pixels before it calls the display's flush callback. A static buffer, such as a file-scope array, has a fixed capacity and lives for the duration of the program. It gives you a predictable allocation and must meet the port's alignment requirements. A dynamically allocated buffer lets the port choose a size at startup. Check the allocation before registering it. Keep the storage valid while LVGL or the transfer code can access it. If you allocate it through lv_malloc() with the built-in allocator, it consumes pool capacity. A static array or memory allocated separately by the display backend can sit outside that pool. Count backend-owned display storage separately when it occupies a separate allocation.
For a tightly packed RGB565 buffer, each pixel takes two bytes. On a hypothetical 320 × 240 display, a full screen takes 320 × 240 × 2 = 153,600 bytes. A buffer that holds 24 full-width rows takes 320 × 24 × 2 = 15,360 bytes. Two buffers of that size take 30,720 bytes of pixel storage. If the port pads scanlines, use the actual stride in bytes multiplied by the number of rows. These calculations give the buffer capacity before any separate backend or allocator costs.
In LVGL 9, lv_display_set_buffers() takes a size in bytes per buffer. Use LV_DISPLAY_RENDER_MODE_PARTIAL with a display port that can flush rectangles. LVGL can then render an update larger than the buffer in several pieces. With one buffer, pass NULL for the second address. With two, give LVGL two distinct buffers of the registered capacity. The flush callback receives the rectangle and pixel data. Call lv_display_flush_ready() when the backend is finished reading that buffer. For a synchronous copy, this can be when the copy returns. For an asynchronous transfer, wait for completion. Signalling early can let LVGL overwrite pixels still in use.
Connect evdev touch to LVGL
3 min readOn Linux, a touchscreen driver exposes input through a character device under /dev/input, commonly /dev/input/eventN, where N is assigned by the system. In evdev, an absolute axis reports a position in the device's coordinate range. A simple touchscreen can report ABS_X, ABS_Y and BTN_TOUCH. Events delimited by SYN_REPORT form a report, so an X update and a Y update in that report belong together.
Use evtest from the Ubuntu package of the same name to identify the device. Run it on the machine that sees the touchscreen in /dev/input. Its interactive list lets you select a device and inspect its advertised capabilities and live events. Touch the panel, drag and lift. Look for absolute position changes and a release. If you can read events with elevated privileges but the application account cannot open the device, check the node's permissions and give that account access through the system's device access policy. Event numbers can change. Use a suitable stable device symlink if the system provides one. A desktop pointer inside WSL2 or a VM only serves this lab if the environment exposes a suitable evdev device to the program.
The lab uses the LVGL 9 Linux evdev driver. Enable LV_USE_EVDEV in the lv_conf.h used by your build and rebuild LVGL and the program with that configuration. After creating the display, call lv_evdev_create(LV_INDEV_TYPE_POINTER, path) with the node you identified. It returns an lv_indev_t *. Check for NULL and report the failing path if creation fails. Associate the returned input device with the display using lv_indev_set_display(). Keep the existing tick source and regular lv_timer_handler() calls running so LVGL services the input and updates the widgets.
Verify the position and the press state separately. Put a button near one corner and tap it, then test targets near the other corners. If taps land along the wrong axis, check axis swap and display rotation. If they align near one edge and drift toward the other, compare the absolute axis ranges with the display's logical size and check calibration. Apply the coordinate transform in one place so rotation or scaling is performed once. Then drag a slider and lift your finger. It should follow the drag and stop responding when contact ends. A pointer that moves correctly but stays pressed after lift needs investigation of the release report or its translation.
A multi-touch panel can report several contacts through the same event node. In the slot-based Linux protocol, ABS_MT_SLOT selects a slot, ABS_MT_TRACKING_ID identifies the contact or ends it with -1, and ABS_MT_POSITION_X and ABS_MT_POSITION_Y carry that contact's position. Two fingers can therefore produce separate coordinates in the same report. A standard LVGL pointer sample contains a single position and press state. For a button-and-slider UI, verify that ordinary one-finger input works on the chosen controller. For pinch or simultaneous actions, check what your pinned LVGL version and input backend expose. If they expose only one pointer position, use an input path that retains the contacts and interprets them before sending the resulting action to the application. Mapping a primary contact yourself requires keeping the same contact selected throughout a drag and handling its release explicitly.
Measure frame rate and RAM
3 min readLVGL 9 provides performance and memory monitors through its system monitor (LV_USE_SYSMON) configuration. Enable LV_USE_SYSMON and LV_USE_PERF_MONITOR to show performance statistics. Enable LV_USE_MEM_MONITOR with the built-in allocator to show its memory statistics. Rebuild and leave the monitor configuration identical across comparisons. The overlay itself allocates memory and draws pixels, so enabling it for only one run changes the workload.
Record an idle baseline after the screen has settled. Then repeat a fixed interaction, such as dragging the same slider back and forth for the same elapsed time. Record the displayed FPS and memory usage during that interval, along with the buffer count and byte capacity. FPS here describes LVGL's refresh activity under the workload. The panel can continue scanning out an unchanged image while LVGL has little to draw, and an LVGL refresh can include several flush calls. Treat the FPS reading as a comparison between runs of the same test.
For process RAM, read VmRSS in /proc/PID/status, substituting the PID of your running application. This is Linux's reported resident set size. It can include resident code, libraries, the allocator pool and draw buffers. Device or kernel allocations can fall outside it. A larger static buffer may have little effect on the allocator reading and still increase RSS once the application touches those pages. Take samples at comparable points in each run and record the configured buffer bytes even when RSS differences are small.
Going deeperWhy redraw area and transfer time matter
When a widget changes, LVGL marks affected rectangles as invalid, often called dirty areas. At refresh time it can merge regions and redraw the objects that contribute pixels inside them, including underlying content where needed. With partial rendering, a large region may be split into strips. Some drawing operations, such as applying opacity to an object and its children as a group, render into a temporary layer before compositing it into the target. That intermediate storage can raise peak allocator usage even when the registered display buffers stay the same size.
A smaller draw buffer can increase the number of flush submissions for a large redraw. A larger one may reduce per-submission overhead, though it cannot remove the cost of transferring the pixels themselves. With two buffers and asynchronous transfer, LVGL can render into one buffer while the backend reads the other. A blocking flush leaves little opportunity for that overlap. Compare configurations using both a small widget update and a repeatable large redraw, since a buffer capacity that changes the second workload may have no measurable effect on the first.
Use the practice pack to connect touch, verify interaction and compare the measured cost of two buffer configurations. The next lesson, “The Linux media stack and V4L2”, explains how Linux supplies frames from cameras and other video devices, extending the display work here to pixels produced outside the UI.
Key takeaways
- LVGL allocator usage and application-owned draw buffers can contribute separately to the RAM budget.
- Partial rendering can use buffers smaller than the screen, and double buffering can overlap rendering with asynchronous transfer.
- A standard LVGL pointer reports one position and press state, while Linux evdev can report several simultaneous contacts.
- LVGL's system monitor reports UI performance and allocator usage, while Linux can report resident memory for the process.
Reflect
- Explain to a colleague, in two sentences: calculate draw-buffer capacity from pixel format, stride and row count.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Kernel documentationLinux input event codes
- Kernel documentationLinux multi-touch protocol
- Man pageproc_pid_status(5): process status and resident memory
- LVGL documentationLVGL evdev driver
- LVGL documentationLVGL input devices
Official documentation first. Links checked when the lesson was published.