lcd driver: use LVGL direct mode or rotation in driver

see https://forum.lvgl.io/t/v8-display-driver-double-buffer-low-fps-high-cpu/6901/19
This commit is contained in:
Raphael Coeffic
2022-06-11 15:38:08 +02:00
committed by Raphael Coeffic
parent e255b40b1f
commit 40eb89e6ed
4 changed files with 320 additions and 92 deletions
+62 -42
View File
@@ -58,51 +58,41 @@ void lcdSetFlushCb(void (*cb)(lv_disp_drv_t *, uint16_t*, const rect_t&))
static lv_disp_drv_t* refr_disp = nullptr;
#if defined (LCD_VERTICAL_INVERT)
static void buf_rotate_180(lv_area_t * area, uint16_t* color_p)
#if !defined(LCD_VERTICAL_INVERT)
// TODO: DMA copy would be possible (use function from draw_ctx???
static void _copy_screen_area(uint16_t* dst, uint16_t* src, const lv_area_t& copy_area)
{
lv_coord_t area_w = lv_area_get_width(area);
lv_coord_t area_h = lv_area_get_height(area);
uint32_t total = area_w * area_h;
/*Swap the beginning and end values*/
lv_coord_t x1 = copy_area.x1;
lv_coord_t y1 = copy_area.y1;
lv_coord_t area_w = copy_area.x2 - copy_area.x1 + 1;
uint32_t tmp;
uint32_t* c_tail = (uint32_t*)(color_p + (total & 1));
uint32_t* c_head = (uint32_t*)color_p;
uint32_t i = total/2 - 1, j = 0;
while(i > j) {
tmp = c_tail[i];
c_tail[i] = ((c_head[j] & 0xFFFF0000) >> 16) | ((c_head[j] & 0xFFFF) << 16);
c_head[j] = ((tmp & 0xFFFF0000) >> 16) | ((tmp & 0xFFFF) << 16);
i--;
j++;
}
if (total & 1) {
uint16_t tmp = color_p[i * 2 + 1];
color_p[j * 2] = color_p[i * 2 + 1];
color_p[i * 2 + 1] = tmp;
}
auto offset = y1 * LCD_W + x1;
auto px_src = src + offset;
auto px_dst = dst + offset;
lv_coord_t tmp_coord;
tmp_coord = area->y2;
area->y2 = LCD_H - area->y1 - 1;
area->y1 = LCD_H - tmp_coord - 1;
tmp_coord = area->x2;
area->x2 = LCD_W - area->x1 - 1;
area->x1 = LCD_W - tmp_coord - 1;
for (auto line = copy_area.y1; line <= copy_area.y2; line++) {
memcpy(px_dst, px_src, area_w * sizeof(uint16_t));
px_dst += LCD_W;
px_src += LCD_W;
}
}
#endif
static void flushLcd(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
lv_area_t refr_area;
lv_area_copy(&refr_area, area);
#if !defined(LCD_VERTICAL_INVERT)
// we're only interested in the last flush in direct mode
if (!lv_disp_flush_is_last(disp_drv)) {
lv_disp_flush_ready(disp_drv);
return;
}
#endif
#if defined(DEBUG_WINDOWS)
if (refr_area.x1 != 0 || refr_area.x2 != LCD_W-1 || refr_area.y1 != 0 ||
refr_area.y2 != LCD_H-1) {
TRACE("partial refresh @ 0x%p {%d,%d,%d,%d}", color_p, refr_area.x1,
refr_area.y1, refr_area.x2, refr_area.y2);
if (area->x1 != 0 || area->x2 != LCD_W-1 || area->y1 != 0 ||
area->y2 != LCD_H-1) {
TRACE("partial refresh @ 0x%p {%d,%d,%d,%d}", color_p, area->x1,
area->y1, area->x2, area->y2);
} else {
TRACE("full refresh @ 0x%p", color_p);
}
@@ -111,15 +101,40 @@ static void flushLcd(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_
if (lcd_flush_cb) {
refr_disp = disp_drv;
#if defined (LCD_VERTICAL_INVERT)
buf_rotate_180(&refr_area, (uint16_t*)color_p);
#endif
rect_t copy_area = {refr_area.x1, refr_area.y1,
refr_area.x2 - refr_area.x1 + 1,
refr_area.y2 - refr_area.y1 + 1};
rect_t copy_area = {area->x1, area->y1,
area->x2 - area->x1 + 1,
area->y2 - area->y1 + 1};
lcd_flush_cb(disp_drv, (uint16_t*)color_p, copy_area);
#if !defined(LCD_VERTICAL_INVERT)
uint16_t* src = (uint16_t*)color_p;
uint16_t* dst = nullptr;
if ((uint16_t*)color_p == LCD_FIRST_FRAME_BUFFER)
dst = LCD_SECOND_FRAME_BUFFER;
else
dst = LCD_FIRST_FRAME_BUFFER;
lv_disp_t* disp = _lv_refr_get_disp_refreshing();
for(int i = 0; i < disp->inv_p; i++) {
if(disp->inv_area_joined[i]) continue;
const lv_area_t& refr_area = disp->inv_areas[i];
TRACE("{%d,%d,%d,%d}", refr_area.x1,
refr_area.y1, refr_area.x2, refr_area.y2);
auto area_w = refr_area.x2 - refr_area.x1 + 1;
auto area_h = refr_area.y2 - refr_area.y1 + 1;
DMACopyBitmap(dst, LCD_W, LCD_H, refr_area.x1, refr_area.y1,
src, LCD_W, LCD_H, refr_area.x1, refr_area.y1,
area_w, area_h);
}
TRACE("#############################");
lv_disp_flush_ready(disp_drv);
#endif
} else {
lv_disp_flush_ready(disp_drv);
}
@@ -143,7 +158,12 @@ void lcdInitDisplayDriver()
disp_drv.hor_res = LCD_W; /*Set the horizontal resolution in pixels*/
disp_drv.ver_res = LCD_H; /*Set the vertical resolution in pixels*/
disp_drv.full_refresh = 0;
#if !defined(LCD_VERTICAL_INVERT)
disp_drv.direct_mode = 1;
#else
disp_drv.direct_mode = 0;
#endif
// Register the driver and save the created display object
disp = lv_disp_drv_register(&disp_drv);
+128 -21
View File
@@ -22,6 +22,7 @@
#include "opentx_types.h"
#include "libopenui_config.h"
#include "lcd.h"
#include <lvgl/lvgl.h>
#if defined(RADIO_T18)
#define HBP 43
@@ -43,21 +44,133 @@
#define VFP 2
#endif
static pixel_t LCD_FRAME_BUFFER[DISPLAY_BUFFER_SIZE] __SDRAM;
#if defined(LCD_VERTICAL_INVERT)
static pixel_t _LCD_BUF_1[DISPLAY_BUFFER_SIZE] __SDRAM;
static pixel_t _LCD_BUF_2[DISPLAY_BUFFER_SIZE] __SDRAM;
static uint16_t* next_frame_buffer;
static rect_t next_frame_area;
static pixel_t _line_buffer[LCD_W];
static uint16_t* _front_buffer = _LCD_BUF_1;
static uint16_t* _back_buffer = _LCD_BUF_2;
// Copy 2 pixels at once to speed up a little
static void _copy_rotate_180(uint16_t* dst, uint16_t* src, const rect_t& copy_area)
{
coord_t x1 = LCD_W - copy_area.w - copy_area.x;
coord_t y1 = LCD_H - copy_area.h - copy_area.y;
auto total = copy_area.w * copy_area.h;
uint16_t* px_src = src + total - 2;
dst += y1 * LCD_W + x1;
for (auto line = 0; line < copy_area.h; line++) {
// invert line into _line_buffer first (SRAM)
auto px_dst = _line_buffer;
auto line_end = px_dst + (copy_area.w & ~1);
while (px_dst != line_end) {
uint32_t* px2_src = (uint32_t*)px_src;
uint32_t* px2_dst = (uint32_t*)px_dst;
uint32_t px = ((*px2_src & 0xFFFF0000) >> 16) | ((*px2_src & 0xFFFF) << 16);
*px2_dst = px;
px_src -= 2;
px_dst += 2;
}
if (copy_area.w & 1) {
*px_dst = *(px_src+1);
px_src--;
}
// ... and DMA back into SDRAM
DMACopyBitmap(dst, copy_area.w, 1, 0, 0,
_line_buffer, copy_area.w, 1, 0, 0,
copy_area.w, 1);
dst += LCD_W;
}
}
static void _rotate_area_180(lv_area_t& area)
{
lv_coord_t tmp_coord;
tmp_coord = area.y2;
area.y2 = LCD_H - area.y1 - 1;
area.y1 = LCD_H - tmp_coord - 1;
tmp_coord = area.x2;
area.x2 = LCD_W - area.x1 - 1;
area.x1 = LCD_W - tmp_coord - 1;
}
#endif
static volatile uint8_t _frame_addr_reloaded = 0;
static void _update_frame_buffer_addr(uint16_t* addr)
{
LTDC_Layer1->CFBAR = (uint32_t)addr;
// reload shadow registers on vertical blank
_frame_addr_reloaded = 0;
LTDC->SRCR = LTDC_SRCR_VBR;
// wait for reload
// TODO: replace through some smarter mechanism without busy wait
while(_frame_addr_reloaded == 0);
}
static void startLcdRefresh(lv_disp_drv_t *disp_drv, uint16_t *buffer,
const rect_t &copy_area)
{
(void)disp_drv;
#if defined(LCD_VERTICAL_INVERT)
_copy_rotate_180(_back_buffer, buffer, copy_area);
next_frame_buffer = buffer;
next_frame_area = copy_area;
// Enable line IRQ
LTDC_ITConfig(LTDC_IER_LIE, ENABLE);
if (lv_disp_flush_is_last(disp_drv)) {
// swap back/front
if (_front_buffer == _LCD_BUF_1) {
_front_buffer = _LCD_BUF_2;
_back_buffer = _LCD_BUF_1;
} else {
_front_buffer = _LCD_BUF_1;
_back_buffer = _LCD_BUF_2;
}
// Trigger async refresh
_update_frame_buffer_addr(_front_buffer);
// Copy refreshed & rotated areas into new back buffer
uint16_t* src = _front_buffer;
uint16_t* dst = _back_buffer;
lv_disp_t* disp = _lv_refr_get_disp_refreshing();
for(int i = 0; i < disp->inv_p; i++) {
if(disp->inv_area_joined[i]) continue;
lv_area_t refr_area;
lv_area_copy(&refr_area, &disp->inv_areas[i]);
TRACE("{%d,%d,%d,%d}", refr_area.x1,
refr_area.y1, refr_area.x2, refr_area.y2);
_rotate_area_180(refr_area);
auto area_w = refr_area.x2 - refr_area.x1 + 1;
auto area_h = refr_area.y2 - refr_area.y1 + 1;
DMACopyBitmap(dst, LCD_W, LCD_H, refr_area.x1, refr_area.y1,
src, LCD_W, LCD_H, refr_area.x1, refr_area.y1,
area_w, area_h);
}
}
lv_disp_flush_ready(disp_drv);
#else
// Direct mode
_update_frame_buffer_addr(buffer);
#endif
}
inline void LCD_NRST_LOW()
@@ -296,7 +409,7 @@ void LCD_LayerInit()
LTDC_Layer_InitStruct.LTDC_CFBLineNumber = LCD_PHYS_H;
/* Start Address configuration : the LCD Frame buffer is defined on SDRAM w/ Offset */
LTDC_Layer_InitStruct.LTDC_CFBStartAdress = (uint32_t)LCD_FRAME_BUFFER;
LTDC_Layer_InitStruct.LTDC_CFBStartAdress = (uint32_t)lcdFront->getData();
/* Initialize LTDC layer 1 */
LTDC_LayerInit(LTDC_Layer1, &LTDC_Layer_InitStruct);
@@ -327,8 +440,11 @@ void LCD_Init(void)
void lcdInit()
{
#if defined(LCD_VERTICAL_INVERT)
// Clear buffer first
memset(LCD_FRAME_BUFFER, 0, sizeof(LCD_FRAME_BUFFER));
memset(_LCD_BUF_1, 0, sizeof(_LCD_BUF_1));
memset(_LCD_BUF_2, 0, sizeof(_LCD_BUF_2));
#endif
// Initialize the LCD
LCD_Init();
@@ -504,15 +620,6 @@ extern "C" void LTDC_IRQHandler(void)
{
// clear interrupt flag
LTDC->ICR = LTDC_ICR_CLIF;
LTDC_ITConfig(LTDC_IER_LIE, DISABLE);
// TODO: use modified version with "Transfer Complete" IRQ
DMACopyBitmap((uint16_t *)LCD_FRAME_BUFFER, LCD_PHYS_W, LCD_PHYS_H,
next_frame_area.x, next_frame_area.y, next_frame_buffer,
next_frame_area.w, next_frame_area.h, 0, 0,
next_frame_area.w, next_frame_area.h);
// TODO: call on "Transfer Complete" IRQ
lcdFlushed();
_frame_addr_reloaded = 1;
}
+126 -29
View File
@@ -25,8 +25,6 @@
#include <string.h>
#include <utility>
pixel_t simuLcdBuf[DISPLAY_BUFFER_SIZE];
bool simuLcdRefresh = true;
void toplcdOff() {}
@@ -35,13 +33,10 @@ void toplcdOff() {}
void lcdOff() {}
#endif
void lcdCopy(void *dest, void *src)
{
memcpy(dest, src, DISPLAY_BUFFER_SIZE * sizeof(pixel_t));
}
#if !defined(COLORLCD)
pixel_t simuLcdBuf[DISPLAY_BUFFER_SIZE];
void lcdInit() {}
void lcdRefresh()
@@ -54,44 +49,144 @@ void lcdRefresh()
#else
#if defined(LCD_VERTICAL_INVERT)
static pixel_t _LCD_BUF1[DISPLAY_BUFFER_SIZE] __SDRAM;
static pixel_t _LCD_BUF2[DISPLAY_BUFFER_SIZE] __SDRAM;
#endif
pixel_t* simuLcdBuf = _LCD_BUF1;
pixel_t* simuLcdBackBuf = _LCD_BUF1;
#include <lvgl/lvgl.h>
static void simuRefreshLcd(lv_disp_drv_t * disp_drv, uint16_t *buffer, const rect_t& copy_area)
// Copy 2 pixels at once to speed up a little
static void _copy_rotate_180(uint16_t* dst, uint16_t* src, const rect_t& copy_area)
{
#if defined(LCD_VERTICAL_INVERT)
coord_t x1 = LCD_W - copy_area.w - copy_area.x;
coord_t y1 = LCD_H - copy_area.h - copy_area.y;
auto total = copy_area.w * copy_area.h;
auto src = buffer + total - 1;
#else
coord_t x1 = copy_area.x;
coord_t y1 = copy_area.y;
uint16_t* px_src = src + total - 2;
auto src = buffer;
#endif
auto dst = simuLcdBuf + y1 * LCD_W + x1;
auto px_dst = dst + y1 * LCD_W + x1;
for (auto line = 0; line < copy_area.h; line++) {
auto line_end = dst + copy_area.w;
while (dst != line_end) {
#if defined(LCD_VERTICAL_INVERT)
*(dst++) = *(src--);
#else
*(dst++) = *(src++);
#endif
auto line_end = px_dst + (copy_area.w & ~1);
while (px_dst != line_end) {
uint32_t* px2_src = (uint32_t*)px_src;
uint32_t* px2_dst = (uint32_t*)px_dst;
uint32_t px = ((*px2_src & 0xFFFF0000) >> 16) | ((*px2_src & 0xFFFF) << 16);
*px2_dst = px;
px_src -= 2;
px_dst += 2;
}
dst += LCD_W - copy_area.w;
}
if (copy_area.w & 1) {
*(px_dst++) = *(px_src+1);
px_src--;
}
px_dst += LCD_W - copy_area.w;
}
}
static void _rotate_area_180(lv_area_t& area)
{
lv_coord_t tmp_coord;
tmp_coord = area.y2;
area.y2 = LCD_H - area.y1 - 1;
area.y1 = LCD_H - tmp_coord - 1;
tmp_coord = area.x2;
area.x2 = LCD_W - area.x1 - 1;
area.x1 = LCD_W - tmp_coord - 1;
}
static void _copy_screen_area(uint16_t* dst, uint16_t* src, const lv_area_t& copy_area)
{
lv_coord_t x1 = copy_area.x1;
lv_coord_t y1 = copy_area.y1;
lv_coord_t area_w = copy_area.x2 - copy_area.x1 + 1;
auto offset = y1 * LCD_W + x1;
auto px_src = src + offset;
auto px_dst = dst + offset;
for (auto line = copy_area.y1; line <= copy_area.y2; line++) {
memcpy(px_dst, px_src, area_w * sizeof(uint16_t));
px_dst += LCD_W;
px_src += LCD_W;
}
}
static void _copy_area(uint16_t* dst, uint16_t* src, const rect_t& copy_area)
{
lv_coord_t x1 = copy_area.x;
lv_coord_t y1 = copy_area.y;
auto offset = y1 * LCD_W + x1;
auto px_src = src;
auto px_dst = dst + offset;
for (auto line = 0; line < copy_area.h; line++) {
memcpy(px_dst, px_src, copy_area.w * sizeof(uint16_t));
px_dst += LCD_W;
px_src += copy_area.w;
}
}
static void simuRefreshLcd(lv_disp_drv_t * disp_drv, uint16_t *buffer, const rect_t& copy_area)
{
#if !defined(LCD_VERTICAL_INVERT) // rename into "Use direct mode" ???
// Direct mode: driver flush is called on final LVGL flush
// simply set LVGL's buffer as our current frame buffer
simuLcdBuf = buffer;
// Trigger async refresh
simuLcdRefresh = true;
#else
// // copy / rotate current area
// _copy_rotate_180(simuLcdBackBuf, buffer, copy_area);
_copy_area(simuLcdBackBuf, buffer, copy_area);
if (lv_disp_flush_is_last(disp_drv)) {
// Mark screen dirty for async refresh
// swap back/front
if (simuLcdBuf == _LCD_BUF1) {
simuLcdBuf = _LCD_BUF2;
simuLcdBackBuf = _LCD_BUF1;
} else {
simuLcdBuf = _LCD_BUF1;
simuLcdBackBuf = _LCD_BUF2;
}
// Trigger async refresh
simuLcdRefresh = true;
// Copy refreshed & rotated areas into new back buffer
uint16_t* src = simuLcdBuf;
uint16_t* dst = simuLcdBackBuf;
lv_disp_t* disp = _lv_refr_get_disp_refreshing();
for(int i = 0; i < disp->inv_p; i++) {
if(disp->inv_area_joined[i]) continue;
lv_area_t refr_area;
lv_area_copy(&refr_area, &disp->inv_areas[i]);
TRACE("{%d,%d,%d,%d}", refr_area.x1,
refr_area.y1, refr_area.x2, refr_area.y2);
// _rotate_area_180(refr_area);
_copy_screen_area(dst, src, refr_area);
}
} else {
lv_disp_flush_ready(disp_drv);
}
}
#endif
}
extern bool simu_shutdown;
@@ -105,7 +200,9 @@ static void simuLcdExitHandler(lv_disp_drv_t* disp_drv)
void lcdInit()
{
memset(simuLcdBuf, 0, sizeof(simuLcdBuf));
memset(_LCD_BUF1, 0, sizeof(_LCD_BUF1));
memset(_LCD_BUF2, 0, sizeof(_LCD_BUF2));
lcdSetWaitCb(simuLcdExitHandler);
lcdSetFlushCb(simuRefreshLcd);
}
+4
View File
@@ -28,7 +28,11 @@
extern int g_snapshot_idx;
extern bool simuLcdRefresh;
#if defined(COLORLCD)
extern pixel_t* simuLcdBuf;
#else
extern pixel_t simuLcdBuf[DISPLAY_BUFFER_SIZE];
extern pixel_t displayBuf[DISPLAY_BUFFER_SIZE];
#endif
#endif // _SIMULCD_H_