Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions components/display/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
52 changes: 45 additions & 7 deletions components/display/include/display.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include <cstdint>
#include <functional>
#include <vector>

#include <lvgl.h>
#include <sdkconfig.h>

#include <driver/gpio.h>
#include <esp_heap_caps.h>

#include "base_component.hpp"
#include "led.hpp"
Expand Down Expand Up @@ -116,6 +118,8 @@ template <typename Pixel> 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. */
Expand Down Expand Up @@ -217,8 +221,8 @@ template <typename Pixel> class Display : public BaseComponent {
*/
~Display() {
if (created_vram_) {
free(vram_0_);
free(vram_1_);
heap_caps_free(vram_0_);
heap_caps_free(vram_1_);
}
}

Expand Down Expand Up @@ -298,6 +302,18 @@ template <typename Pixel> 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
Expand Down Expand Up @@ -406,7 +422,16 @@ template <typename Pixel> 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<uintptr_t>(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<const void *>(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);
Expand Down Expand Up @@ -443,16 +468,29 @@ template <typename Pixel> 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<Pixel *>(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<Pixel *>(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<Pixel *>(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;
Expand Down
Loading