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
+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);
}