From c36426754ac9052fbb3006cdbb24e24b8783e029 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 20 Nov 2026 10:47:06 -0600 Subject: [PATCH] feat(display): allocate the draw buffers aligned to LV_DRAW_BUF_ALIGN LVGL's lv_display_set_buffers() refuses buffers that are not aligned to LV_DRAW_BUF_ALIGN, and the display then draws nothing. Display allocated its draw buffers with plain heap_caps_malloc(), so raising LV_DRAW_BUF_ALIGN (LVGL's PPA draw unit on the ESP32-P4 requires the data cache line size, 128) broke every board whose BSP lets Display allocate them. - The DynamicMemoryConfig buffers come from heap_caps_aligned_alloc() at vram_alignment() (LV_DRAW_BUF_ALIGN, at least a pointer), their size rounded up to a multiple of it, so no other allocation shares their last cache line. - A StaticMemoryConfig buffer that is not aligned is logged as an error instead of failing silently. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/display/README.md | 8 ++++ components/display/include/display.hpp | 52 ++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/components/display/README.md b/components/display/README.md index 48e3ef3dba..977dad2cbe 100644 --- a/components/display/README.md +++ b/components/display/README.md @@ -5,3 +5,11 @@ The `display` component is responsible for managing the memory associated the display buffer / video RAM (vram). It ensures that the LVGL subsystem is configured appropriately. + +The display buffers it allocates (`DynamicMemoryConfig`) are aligned to +`LV_DRAW_BUF_ALIGN`, and their size is rounded up to a multiple of it. LVGL +refuses display buffers that are not aligned to it, so buffers you provide +yourself (`StaticMemoryConfig`) must be aligned too; the display logs an error +if they are not. A raised `LV_DRAW_BUF_ALIGN` is needed, for example, by LVGL's +PPA draw unit on the ESP32-P4 (`LV_USE_PPA`), which requires it to equal the +data cache line size (`CONFIG_CACHE_L2_CACHE_LINE_SIZE`, 128 by default). diff --git a/components/display/include/display.hpp b/components/display/include/display.hpp index b90bb018c7..64a0d7cc9b 100644 --- a/components/display/include/display.hpp +++ b/components/display/include/display.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -7,6 +8,7 @@ #include #include +#include #include "base_component.hpp" #include "led.hpp" @@ -116,6 +118,8 @@ template class Display : public BaseComponent { * @brief Used if you want to manage allocation / lifecycle of the display * buffer memory separately from this class. This structure allows you to * configure the Display with up to two display buffers. + * \note The buffers must be aligned to LV_DRAW_BUF_ALIGN (see + * vram_alignment()), or LVGL will not use them. */ struct StaticMemoryConfig { size_t pixel_buffer_size; /**< Size of the display buffer in pixels. */ @@ -217,8 +221,8 @@ template class Display : public BaseComponent { */ ~Display() { if (created_vram_) { - free(vram_0_); - free(vram_1_); + heap_caps_free(vram_0_); + heap_caps_free(vram_1_); } } @@ -298,6 +302,18 @@ template class Display : public BaseComponent { */ size_t vram_size_bytes() const { return display_buffer_px_size_ * sizeof(Pixel); } + /** + * @brief The alignment of the display buffers: LV_DRAW_BUF_ALIGN, which LVGL + * requires of the buffers given to lv_display_set_buffers() (it + * refuses others, and the display then draws nothing). LVGL's PPA + * draw unit on the ESP32-P4 (LV_USE_PPA), for example, needs it to be + * the data cache line size. + */ + static constexpr size_t vram_alignment() { + // (heap_caps_aligned_alloc() takes a power of two of at least a pointer) + return LV_DRAW_BUF_ALIGN < sizeof(void *) ? sizeof(void *) : LV_DRAW_BUF_ALIGN; + } + /** * @brief Set the rotation of the display. * @note This function is called by the event handler when the display @@ -406,7 +422,16 @@ template class Display : public BaseComponent { // access it in the flush callback lv_display_set_user_data(display_, this); - // Configure the lvgl display buffer with our pixel buffers + // Configure the lvgl display buffer with our pixel buffers. LVGL refuses + // buffers that are not aligned to LV_DRAW_BUF_ALIGN (the ones allocated + // here are; a StaticMemoryConfig's may not be), and then draws nothing. + for (const Pixel *buf : {vram_0_, vram_1_}) { + if (buf != nullptr && reinterpret_cast(buf) % LV_DRAW_BUF_ALIGN != 0) { + logger_.error("Display buffer {} is not aligned to LV_DRAW_BUF_ALIGN ({} bytes): LVGL " + "will not use it", + static_cast(buf), LV_DRAW_BUF_ALIGN); + } + } lv_display_set_buffers(display_, vram_0_, vram_1_, vram_size_bytes(), LV_DISPLAY_RENDER_MODE_PARTIAL); lv_display_set_flush_cb(display_, Display::flush_cb); @@ -443,16 +468,29 @@ template class Display : public BaseComponent { } /** - * @brief Allocate the display buffer(s). + * @brief Allocate one display buffer, aligned to vram_alignment(), its size + * rounded up to a multiple of it (so no other allocation shares its + * last cache line: DMA engines and cache syncs work in whole lines). + * @param allocation_flags Flags passed to heap_caps_aligned_alloc. + * @return The buffer, or nullptr if it could not be allocated. + */ + Pixel *allocate_vram(const uint32_t allocation_flags) const { + constexpr size_t align = vram_alignment(); + const size_t bytes = (vram_size_bytes() + align - 1) / align * align; + return static_cast(heap_caps_aligned_alloc(align, bytes, allocation_flags)); + } + + /** + * @brief Allocate the display buffer(s), aligned to vram_alignment(). * @param double_buffered Whether to use double buffered rendering or not, will allocate a second * buffer if true. - * @param allocation_flags Flags passed to heap_caps_malloc for memory allocation. + * @param allocation_flags Flags passed to heap_caps_aligned_alloc for memory allocation. */ void init_memory(const bool double_buffered, const uint32_t allocation_flags) { - vram_0_ = static_cast(heap_caps_malloc(vram_size_bytes(), allocation_flags)); + vram_0_ = allocate_vram(allocation_flags); assert(vram_0_ != NULL && "Failed to allocate display buffer vram_0"); if (double_buffered) { - vram_1_ = static_cast(heap_caps_malloc(vram_size_bytes(), allocation_flags)); + vram_1_ = allocate_vram(allocation_flags); assert(vram_1_ != NULL && "Failed to allocate display buffer vram_1"); } created_vram_ = true;