From 82190233ca63e07cd2fc15f225e598c6cf3c624f Mon Sep 17 00:00:00 2001 From: lixianjing Date: Thu, 14 Nov 2019 14:53:25 +0800 Subject: [PATCH] refactor lcd mem --- docs/changes.md | 2 + src/base/graphic_buffer.c | 9 ++- src/base/graphic_buffer.h | 12 +++ src/graphic_buffer/graphic_buffer_default.c | 14 +++- src/lcd/lcd_mem.h | 2 +- src/lcd/lcd_mem.inc | 87 ++++----------------- 6 files changed, 52 insertions(+), 74 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 961f8aa7e..77b7e7cef 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -4,6 +4,8 @@ * 完善进度条(感谢朝泽提供补丁)。 * 增加键值TK\_KEY\_BACK。 * 增加lcd\_mem\_fragment。 + * 增加graphic\_buffer\_attach。 + * 重构lcd\_mem * 2019/11/13 * 把部分变量修改为常量,以减少内存使用。 diff --git a/src/base/graphic_buffer.c b/src/base/graphic_buffer.c index f0225c37e..399dc086c 100644 --- a/src/base/graphic_buffer.c +++ b/src/base/graphic_buffer.c @@ -1,4 +1,4 @@ -/** +/** * File: graphic_buffer.c * Author: AWTK Develop Team * Brief: graphic_buffer @@ -44,6 +44,13 @@ ret_t graphic_buffer_unlock(graphic_buffer_t* buffer) { return buffer->vt->unlock(buffer); } +ret_t graphic_buffer_attach(graphic_buffer_t* buffer, void* data) { + return_value_if_fail(buffer != NULL && buffer->vt != NULL && buffer->vt->attach != NULL, + RET_BAD_PARAMS); + + return buffer->vt->attach(buffer, data); +} + ret_t graphic_buffer_destroy(graphic_buffer_t* buffer) { return_value_if_fail(buffer != NULL && buffer->vt != NULL && buffer->vt->destroy != NULL, RET_BAD_PARAMS); diff --git a/src/base/graphic_buffer.h b/src/base/graphic_buffer.h index 3b1795810..cba77e337 100644 --- a/src/base/graphic_buffer.h +++ b/src/base/graphic_buffer.h @@ -32,6 +32,7 @@ typedef struct _graphic_buffer_t graphic_buffer_t; typedef uint8_t* (*graphic_buffer_lock_for_read_t)(graphic_buffer_t* buffer); typedef uint8_t* (*graphic_buffer_lock_for_write_t)(graphic_buffer_t* buffer); typedef ret_t (*graphic_buffer_unlock_t)(graphic_buffer_t* buffer); +typedef ret_t (*graphic_buffer_attach_t)(graphic_buffer_t* buffer, void* data); typedef ret_t (*graphic_buffer_destroy_t)(graphic_buffer_t* buffer); typedef enum _graphic_buffer_options_t { @@ -57,6 +58,7 @@ typedef struct _graphic_buffer_vtable_t { graphic_buffer_lock_for_read_t lock_for_read; graphic_buffer_lock_for_write_t lock_for_write; graphic_buffer_unlock_t unlock; + graphic_buffer_attach_t attach; graphic_buffer_destroy_t destroy; } graphic_buffer_vtable_t; @@ -119,6 +121,16 @@ uint8_t* graphic_buffer_lock_for_write(graphic_buffer_t* buffer); */ ret_t graphic_buffer_unlock(graphic_buffer_t* buffer); +/** + * @method graphic_buffer_attach + * 附件到指定的内存。 + * @param {graphic_buffer_t*} buffer 图像缓冲区对象。 + * @param {void*} data 内存数据。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t graphic_buffer_attach(graphic_buffer_t* buffer, void* data); + /** * @method graphic_buffer_destroy * 销毁缓冲区。 diff --git a/src/graphic_buffer/graphic_buffer_default.c b/src/graphic_buffer/graphic_buffer_default.c index 66a2c8af8..1dd75ff51 100644 --- a/src/graphic_buffer/graphic_buffer_default.c +++ b/src/graphic_buffer/graphic_buffer_default.c @@ -1,4 +1,4 @@ -/** +/** * File: graphic_buffer_default.c * Author: AWTK Develop Team * Brief: graphic_buffer default @@ -54,6 +54,16 @@ static ret_t graphic_buffer_default_unlock(graphic_buffer_t* buffer) { return RET_OK; } +static ret_t graphic_buffer_default_attach(graphic_buffer_t* buffer, void* data) { + graphic_buffer_default_t* b = GRAPHIC_BUFFER_DEFAULT(buffer); + return_value_if_fail(b != NULL, RET_BAD_PARAMS); + return_value_if_fail(b->data_head == NULL, RET_NOT_IMPL); + + b->data = data; + + return RET_OK; +} + static ret_t graphic_buffer_default_destroy(graphic_buffer_t* buffer) { graphic_buffer_default_t* b = GRAPHIC_BUFFER_DEFAULT(buffer); return_value_if_fail(b != NULL, RET_BAD_PARAMS); @@ -68,6 +78,7 @@ static const graphic_buffer_vtable_t s_graphic_buffer_default_vtable = { .lock_for_read = graphic_buffer_default_lock_for_read, .lock_for_write = graphic_buffer_default_lock_for_write, .unlock = graphic_buffer_default_unlock, + .attach = graphic_buffer_default_attach, .destroy = graphic_buffer_default_destroy}; static graphic_buffer_t* graphic_buffer_default_create(uint32_t w, uint32_t h, @@ -107,7 +118,6 @@ static graphic_buffer_t* graphic_buffer_default_create(uint32_t w, uint32_t h, graphic_buffer_t* graphic_buffer_create_with_data(const uint8_t* data, uint32_t w, uint32_t h, bitmap_format_t format) { graphic_buffer_default_t* buffer = NULL; - return_value_if_fail(data != NULL, NULL); buffer = TKMEM_ZALLOC(graphic_buffer_default_t); return_value_if_fail(buffer != NULL, NULL); diff --git a/src/lcd/lcd_mem.h b/src/lcd/lcd_mem.h index 6c68b82b3..d60ec581f 100644 --- a/src/lcd/lcd_mem.h +++ b/src/lcd/lcd_mem.h @@ -1,7 +1,7 @@ /** * File: lcd_mem.h * Author: AWTK Develop Team - * Brief: memory implemented lcd interface/ + * Brief: memory implemented lcd interface * * Copyright (c) 2018 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd. * diff --git a/src/lcd/lcd_mem.inc b/src/lcd/lcd_mem.inc index e43d330f8..693dd1e71 100644 --- a/src/lcd/lcd_mem.inc +++ b/src/lcd/lcd_mem.inc @@ -1,7 +1,7 @@ -/** +/** * File: lcd_mem.c * Author: Li XianJing - * Brief: mem implemented lcd interface/ + * Brief: mem implemented lcd interface * * Copyright (c) 2018 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd. * @@ -26,31 +26,6 @@ #include "blend/image_g2d.h" #include "base/system_info.h" -#define LCD_MEM_GRAPHIC_BUFFER_CACHE_MAX_NUMBER 5 - -typedef struct _lcd_mem_graphic_buffer_cache_t { - uint8_t* fb; - graphic_buffer_t* gb; -} lcd_mem_graphic_buffer_cache_t; - -static darray_t lcd_mem_graphic_buffer_cache; - -static int lcd_mem_graphic_buffer_cache_cmp(const lcd_mem_graphic_buffer_cache_t* a, - const lcd_mem_graphic_buffer_cache_t* b) { - if (a->fb == b->fb) { - return 0; - } - return 1; -} - -static ret_t lcd_mem_graphic_buffer_cache_destroy(lcd_mem_graphic_buffer_cache_t* cache) { - if (cache->gb != NULL) { - graphic_buffer_destroy(cache->gb); - } - TKMEM_FREE(cache); - return RET_OK; -} - static uint32_t lcd_mem_get_line_length(lcd_mem_t* mem) { uint32_t bpp = bitmap_get_bpp_of_format(LCD_FORMAT); return tk_max(mem->base.w * bpp, mem->line_length); @@ -65,8 +40,9 @@ static pixel_t* lcd_mem_init_drawing_fb(lcd_t* lcd, bitmap_t* fb) { fb->w = lcd->w; fb->h = lcd->h; - fb->format = mem->format; fb->buffer = mem->offline_gb; + fb->format = mem->format; + graphic_buffer_attach(mem->offline_gb, mem->offline_fb); bitmap_set_line_length(fb, lcd_mem_get_line_length(mem)); } @@ -89,47 +65,15 @@ static bitmap_t* lcd_mem_init_online_fb(lcd_t* lcd, bitmap_t* fb, lcd_orientatio memset(fb, 0x00, sizeof(bitmap_t)); fb->w = w; fb->h = h; - fb->format = mem->format; fb->buffer = mem->online_gb; + fb->format = mem->format; + graphic_buffer_attach(mem->online_gb, mem->online_fb); bitmap_set_line_length(fb, lcd_mem_get_line_length(mem)); return fb; } -static inline graphic_buffer_t* lcd_mem_get_graphic_buffer(uint8_t* fb, uint32_t w, uint32_t h, - bitmap_format_t format) { - graphic_buffer_t* gb = NULL; - lcd_mem_graphic_buffer_cache_t tmp; - lcd_mem_graphic_buffer_cache_t* cache = NULL; - - return_value_if_fail(fb != NULL, NULL); - - tmp.fb = fb; - cache = darray_find(&lcd_mem_graphic_buffer_cache, &tmp); - if (cache == NULL) { - cache = (lcd_mem_graphic_buffer_cache_t*)TKMEM_ZALLOC(lcd_mem_graphic_buffer_cache_t); - gb = graphic_buffer_create_with_data(fb, w, h, format); - - cache->fb = fb; - cache->gb = gb; - darray_push(&lcd_mem_graphic_buffer_cache, cache); - } else { - gb = cache->gb; - } - return gb; -} - static ret_t lcd_mem_begin_frame(lcd_t* lcd, rect_t* dirty_rect) { - lcd_mem_t* mem = (lcd_mem_t*)lcd; - - if (mem->online_fb != NULL) { - mem->online_gb = lcd_mem_get_graphic_buffer(mem->online_fb, lcd->w, lcd->h, mem->format); - } - - if (mem->offline_fb != NULL) { - mem->offline_gb = lcd_mem_get_graphic_buffer(mem->offline_fb, lcd->w, lcd->h, mem->format); - } - return RET_OK; } @@ -198,12 +142,15 @@ static ret_t lcd_mem_draw_points(lcd_t* lcd, point_t* points, uint32_t nr) { } static color_t lcd_mem_get_point_color(lcd_t* lcd, xy_t x, xy_t y) { + color_t c; uint32_t line_length = lcd_mem_get_line_length((lcd_mem_t*)lcd); uint8_t* fbuff = (uint8_t*)lcd_mem_init_drawing_fb(lcd, NULL); pixel_t p = *((pixel_t*)(fbuff + y * line_length) + x); + rgba_t rgba = pixel_to_rgba(p); + c.rgba = rgba; - return color_init(p.r, p.g, p.b, 0xff); + return c; } static ret_t lcd_mem_draw_glyph(lcd_t* lcd, glyph_t* glyph, rect_t* src, xy_t x, xy_t y) { @@ -356,7 +303,8 @@ static ret_t lcd_mem_destroy(lcd_t* lcd) { TKMEM_FREE(mem->offline_fb); } - darray_deinit(&lcd_mem_graphic_buffer_cache); + graphic_buffer_destroy(mem->online_gb); + graphic_buffer_destroy(mem->offline_gb); TKMEM_FREE(lcd); @@ -412,25 +360,24 @@ static lcd_t* lcd_mem_create(wh_t w, wh_t h, bool_t alloc) { base->destroy = lcd_mem_destroy; base->resize = lcd_mem_resize; base->flush = lcd_mem_flush; + base->w = w; base->h = h; base->ratio = 1; - base->type = LCD_FRAMEBUFFER; base->global_alpha = 0xff; + base->type = LCD_FRAMEBUFFER; base->support_dirty_rect = TRUE; - lcd->line_length = w * bpp; lcd->format = LCD_FORMAT; + lcd->line_length = w * bpp; + lcd->online_gb = graphic_buffer_create_with_data(NULL, w, h, LCD_FORMAT); + lcd->offline_gb = graphic_buffer_create_with_data(NULL, w, h, LCD_FORMAT); system_info_set_lcd_w(info, base->w); system_info_set_lcd_h(info, base->h); system_info_set_lcd_type(info, base->type); system_info_set_device_pixel_ratio(info, 1); - darray_init(&lcd_mem_graphic_buffer_cache, LCD_MEM_GRAPHIC_BUFFER_CACHE_MAX_NUMBER, - (tk_destroy_t)lcd_mem_graphic_buffer_cache_destroy, - (tk_compare_t)lcd_mem_graphic_buffer_cache_cmp); - return base; }