From 829e0ffc3ba4f8c51d567db53863dca80e1895af Mon Sep 17 00:00:00 2001 From: haorui wang <56127613+HR1025@users.noreply.github.com> Date: Thu, 10 Nov 2022 17:43:07 +0800 Subject: [PATCH] chore(wayland) : accelerate damage updates, reduce unnecessary cycles (#249) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(wayland) : accelerate damage updates, reduce unnecessary cycles * chore(wayland) : Increase code robustness * chore(wayland) : only use `lv_memcpy` when lv color depth equal to shm color depth Co-authored-by: 王浩锐 --- wayland/wayland.c | 91 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 22 deletions(-) diff --git a/wayland/wayland.c b/wayland/wayland.c index 7cbff15..b29ac15 100644 --- a/wayland/wayland.c +++ b/wayland/wayland.c @@ -39,9 +39,6 @@ /********************* * DEFINES *********************/ - -#define BYTES_PER_PIXEL ((LV_COLOR_DEPTH + 7) / 8) - #if LV_WAYLAND_CLIENT_SIDE_DECORATIONS #define TITLE_BAR_HEIGHT 24 #define BORDER_SIZE 2 @@ -232,6 +229,7 @@ struct window int width; int height; + int bits_per_pixel; bool resize_pending; int resize_width; @@ -299,6 +297,38 @@ static void shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) } } +static int get_shm_format_bits_per_pixel(uint32_t format) +{ + int bits_per_pixel = 0; + switch (format) + { +#if (LV_COLOR_DEPTH == 32) + case WL_SHM_FORMAT_ARGB8888: + bits_per_pixel = 32; + break; + case WL_SHM_FORMAT_XRGB8888: + bits_per_pixel = 32; + break; +#elif (LV_COLOR_DEPTH == 16) + case WL_SHM_FORMAT_RGB565: + bits_per_pixel = 16; + break; +#elif (LV_COLOR_DEPTH == 8) + case WL_SHM_FORMAT_RGB332: + bits_per_pixel = 8; + break; +#elif (LV_COLOR_DEPTH == 1) + case WL_SHM_FORMAT_RGB332: + bits_per_pixel = 8; + break; +#endif + default: + break; + } + + return bits_per_pixel; +} + static const struct wl_shm_listener shm_listener = { shm_format }; @@ -1275,7 +1305,9 @@ static bool initialize_buffer(struct window *window, struct buffer_hdl *buffer_h int ret; long sz = sysconf(_SC_PAGESIZE); - buffer_hdl->size = (((width * height * BYTES_PER_PIXEL) + sz - 1) / sz) * sz; + window->bits_per_pixel = get_shm_format_bits_per_pixel(app->format); + + buffer_hdl->size = (((width * height * window->bits_per_pixel / 8) + sz - 1) / sz) * sz; LV_LOG_TRACE("initializing buffer %dx%d (alloc size: %d)", width, height, buffer_hdl->size); @@ -1335,7 +1367,7 @@ static bool initialize_buffer(struct window *window, struct buffer_hdl *buffer_h buffer_hdl->wl_buffer = wl_shm_pool_create_buffer(allocator->shm_pool, allocator->shm_mem_size - allocator->shm_file_free_size, width, height, - width * BYTES_PER_PIXEL, + width * window->bits_per_pixel / 8, app->format); if (!buffer_hdl->wl_buffer) { @@ -1956,31 +1988,46 @@ static void _lv_wayland_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv lv_disp_flush_ready(disp_drv); return; } + + if (LV_COLOR_DEPTH == window->bits_per_pixel && LV_COLOR_DEPTH != 1 && LV_COLOR_DEPTH != 8) + { + int32_t bytes_pre_pixel = window->bits_per_pixel / 8; + int32_t x1 = area->x1, x2 = area->x2 <= disp_drv->hor_res - 1 ? area->x2 : disp_drv->hor_res - 1; + int32_t y1 = area->y1, y2 = area->y2 <= disp_drv->ver_res - 1 ? area->y2 : disp_drv->ver_res - 1; + int32_t act_w = x2 - x1 + 1; - int32_t x; - int32_t y; - - for (y = area->y1; y <= area->y2 && y < disp_drv->ver_res; y++) + for (int y = y1; y <= y2; y++) + { + lv_memcpy((uint8_t *)buffer->base + ((y * disp_drv->hor_res + x1) * bytes_pre_pixel), color_p, act_w * bytes_pre_pixel); + color_p += act_w; + } + } + else { - for (x = area->x1; x <= area->x2 && x < disp_drv->hor_res; x++) + int32_t x; + int32_t y; + for (y = area->y1; y <= area->y2 && y < disp_drv->ver_res; y++) { - int offset = (y * disp_drv->hor_res) + x; + for (x = area->x1; x <= area->x2 && x < disp_drv->hor_res; x++) + { + int offset = (y * disp_drv->hor_res) + x; #if (LV_COLOR_DEPTH == 32) - uint32_t * const buf = (uint32_t *)buffer->base + offset; - *buf = color_p->full; + uint32_t * const buf = (uint32_t *)buffer->base + offset; + *buf = color_p->full; #elif (LV_COLOR_DEPTH == 16) - uint16_t * const buf = (uint16_t *)buffer->base + offset; - *buf = color_p->full; + uint16_t * const buf = (uint16_t *)buffer->base + offset; + *buf = color_p->full; #elif (LV_COLOR_DEPTH == 8) - uint8_t * const buf = (uint8_t *)buffer->base + offset; - *buf = color_p->full; + uint8_t * const buf = (uint8_t *)buffer->base + offset; + *buf = color_p->full; #elif (LV_COLOR_DEPTH == 1) - uint8_t * const buf = (uint8_t *)buffer->base + offset; - *buf = ((0x07 * color_p->ch.red) << 5) | - ((0x07 * color_p->ch.green) << 2) | - ((0x03 * color_p->ch.blue) << 0); + uint8_t * const buf = (uint8_t *)buffer->base + offset; + *buf = ((0x07 * color_p->ch.red) << 5) | + ((0x07 * color_p->ch.green) << 2) | + ((0x03 * color_p->ch.blue) << 0); #endif - color_p++; + color_p++; + } } }