Skip to content

Commit

Permalink
improve to support linux g2d
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Dec 28, 2024
1 parent 00c2878 commit b97a166
Show file tree
Hide file tree
Showing 23 changed files with 590 additions and 77 deletions.
3 changes: 3 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 最新动态

2024/12/28
* 重构以支持Linux G2D。

2024/12/27
* 修复fdb资源泄露的问题(感谢智明提供补丁)

Expand Down
8 changes: 4 additions & 4 deletions src/awtk_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ ret_t tk_init(wh_t w, wh_t h, app_type_t app_type, const char* app_name, const c
ENSURE(system_info_init(app_type, app_name, app_root) == RET_OK);
return_value_if_fail(tk_init_internal() == RET_OK, RET_FAIL);

#ifdef WITH_G2D
tk_g2d_init();
#endif /*WITH_G2D*/

if (APP_CONSOLE == system_info()->app_type) {
#if WITH_MAIN_LOOP_CONSOLE
loop = (main_loop_t*)main_loop_console_init();
Expand All @@ -345,10 +349,6 @@ ret_t tk_init(wh_t w, wh_t h, app_type_t app_type, const char* app_name, const c
tk_socket_init();
#endif /*WITH_SOCKET*/

#ifdef WITH_G2D
tk_g2d_init();
#endif /*WITH_G2D*/

return RET_OK;
}

Expand Down
48 changes: 35 additions & 13 deletions src/base/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ static ret_t bitmap_platform_create(bitmap_t* bitmap) {
return_value_if_fail(format == BITMAP_FMT_RGBA8888, RET_BAD_PARAMS);

data = bitmap_lock_buffer_for_write(bitmap);
id = EM_ASM_INT(
{ return VGCanvas.createMutableImage($0, $1, $2, $3, $4); }, data, w, h, line_length, format);
id = EM_ASM_INT({ return VGCanvas.createMutableImage($0, $1, $2, $3, $4); }, data, w, h,
line_length, format);
bitmap->specific = tk_pointer_from_int(id);
bitmap->specific_destroy = bitmap_web_destroy;
bitmap_unlock_buffer(bitmap);
Expand All @@ -161,7 +161,7 @@ static ret_t bitmap_platform_create(bitmap_t* bitmap) {
#endif /*AWTK_WEB*/

bitmap_t* bitmap_create_ex(uint32_t w, uint32_t h, uint32_t line_length, bitmap_format_t format) {
bitmap_t* bitmap = TKMEM_ZALLOC(bitmap_t);
bitmap_t* bitmap = bitmap_create();
return_value_if_fail(bitmap != NULL, NULL);

bitmap_init_ex(bitmap, w, h, line_length, format, NULL);
Expand All @@ -170,6 +170,22 @@ bitmap_t* bitmap_create_ex(uint32_t w, uint32_t h, uint32_t line_length, bitmap_
return bitmap;
}

bitmap_t* bitmap_create_ex3(uint32_t w, uint32_t h, uint32_t line_length, bitmap_format_t format,
uint8_t* data, uint8_t* physical_data_addr, bool_t should_free_data) {
bitmap_t* bitmap = bitmap_create();
return_value_if_fail(bitmap != NULL, NULL);

bitmap_init_ex2(bitmap, w, h, line_length, format, data, physical_data_addr, should_free_data);
bitmap->should_free_handle = TRUE;

return bitmap;
}

bitmap_t* bitmap_create_ex2(uint32_t w, uint32_t h, uint32_t line_length, bitmap_format_t format,
uint8_t* data, bool_t should_free_data) {
return bitmap_create_ex3(w, h, line_length, format, data, NULL, should_free_data);
}

ret_t bitmap_get_pixel(bitmap_t* bitmap, uint32_t x, uint32_t y, rgba_t* rgba) {
ret_t ret = RET_OK;
const uint8_t* data = NULL;
Expand Down Expand Up @@ -588,22 +604,23 @@ ret_t bitmap_init_from_rgba(bitmap_t* bitmap, uint32_t w, uint32_t h, bitmap_for
}

ret_t bitmap_init(bitmap_t* bitmap, uint32_t w, uint32_t h, bitmap_format_t format, uint8_t* data) {
uint32_t line_length = 0;
return bitmap_init_ex(bitmap, w, h, 0, format, data);
}

ret_t bitmap_init_ex2(bitmap_t* bitmap, uint32_t w, uint32_t h, uint32_t line_length,
bitmap_format_t format, uint8_t* data, uint8_t* physical_data_addr,
bool_t should_free_data) {
uint32_t bpp = bitmap_get_bpp_of_format(format);
return_value_if_fail(bitmap != NULL, RET_BAD_PARAMS);

bitmap->should_free_data = should_free_data;
if (bitmap->format == BITMAP_FMT_MONO) {
line_length = TK_BITMAP_MONO_LINE_LENGTH(w);
} else {
uint32_t bpp = bitmap_get_bpp_of_format(format);
line_length = tk_max(w * bpp, line_length);
}

return bitmap_init_ex(bitmap, w, h, line_length, format, data);
}

ret_t bitmap_init_ex(bitmap_t* bitmap, uint32_t w, uint32_t h, uint32_t line_length,
bitmap_format_t format, uint8_t* data) {
uint32_t bpp = bitmap_get_bpp_of_format(format);
return_value_if_fail(bitmap != NULL, RET_BAD_PARAMS);

memset(bitmap, 0x00, sizeof(bitmap_t));

bitmap->w = w;
Expand All @@ -617,7 +634,7 @@ ret_t bitmap_init_ex(bitmap_t* bitmap, uint32_t w, uint32_t h, uint32_t line_len
if (data == NULL) {
bitmap_alloc_data(bitmap);
} else {
bitmap->buffer = GRAPHIC_BUFFER_CREATE_WITH_DATA(data, w, h, format);
bitmap->buffer = GRAPHIC_BUFFER_CREATE_WITH_DATA_EX(data, physical_data_addr, w, h, line_length, format);
bitmap->should_free_data = TRUE;
}

Expand All @@ -628,6 +645,11 @@ ret_t bitmap_init_ex(bitmap_t* bitmap, uint32_t w, uint32_t h, uint32_t line_len
return bitmap->buffer != NULL ? RET_OK : RET_OOM;
}

ret_t bitmap_init_ex(bitmap_t* bitmap, uint32_t w, uint32_t h, uint32_t line_length,
bitmap_format_t format, uint8_t* data) {
return bitmap_init_ex2(bitmap, w, h, line_length, format, data, NULL, FALSE);
}

ret_t bitmap_set_line_length(bitmap_t* bitmap, uint32_t line_length) {
return_value_if_fail(bitmap != NULL, RET_BAD_PARAMS);

Expand Down
49 changes: 49 additions & 0 deletions src/base/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,55 @@ bitmap_t* bitmap_create(void);
*/
bitmap_t* bitmap_create_ex(uint32_t w, uint32_t h, uint32_t line_length, bitmap_format_t format);

/**
* @method bitmap_create_ex2
* 创建图片对象。
* @annotation ["constructor", "scriptable", "gc"]
* @param {uint32_t} w 宽度。
* @param {uint32_t} h 高度。
* @param {uint32_t} line_length line_length。
* @param {bitmap_format_t} format 格式。
* @param {uint8_t*} data 图像数据。
* @param {bool_t} should_free_data 是否释放数据。
*
* @return {bitmap_t*} 返回bitmap对象。
*/
bitmap_t* bitmap_create_ex2(uint32_t w, uint32_t h, uint32_t line_length, bitmap_format_t format,
uint8_t* data, bool_t should_free_data);

/**
* @method bitmap_create_ex3
* 创建图片对象。
* @annotation ["constructor", "scriptable", "gc"]
* @param {uint32_t} w 宽度。
* @param {uint32_t} h 高度。
* @param {uint32_t} line_length line_length。
* @param {bitmap_format_t} format 格式。
* @param {uint8_t*} data 图像数据。
* @param {uint8_t*} physical_data_addr 物理地址(部分硬件加速需要)。
* @param {bool_t} should_free_data 是否释放数据。
*
* @return {bitmap_t*} 返回bitmap对象。
*/
bitmap_t* bitmap_create_ex3(uint32_t w, uint32_t h, uint32_t line_length, bitmap_format_t format,
uint8_t* data, uint8_t* physical_data_addr, bool_t should_free_data);
/**
* @method bitmap_init_ex2
* 创建图片对象。
* @annotation ["constructor", "scriptable", "gc"]
* @param {uint32_t} w 宽度。
* @param {uint32_t} h 高度。
* @param {uint32_t} line_length line_length。
* @param {bitmap_format_t} format 格式。
* @param {uint8_t*} data 图像数据。
* @param {uint8_t*} physical_data_addr 物理地址(部分硬件加速需要)。
* @param {bool_t} should_free_data 是否释放数据。
*
* @return {bitmap_t*} 返回bitmap对象。
*/
ret_t bitmap_init_ex2(bitmap_t* bitmap, uint32_t w, uint32_t h, uint32_t line_length,
bitmap_format_t format, uint8_t* data, uint8_t* physical_data_addr,
bool_t should_free_data);
/**
* @method bitmap_get_bpp
* 获取图片一个像素占用的字节数。
Expand Down
12 changes: 6 additions & 6 deletions src/base/canvas_offline.inc
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ canvas_t* canvas_offline_create(uint32_t w, uint32_t h, bitmap_format_t format)
buff = bitmap_lock_buffer_for_write(bitmap);
return_value_if_fail(buff != NULL, NULL);
if (bitmap->format == BITMAP_FMT_RGBA8888) {
lcd = lcd_mem_rgba8888_create_single_fb(bitmap->w, bitmap->h, buff);
lcd = lcd_mem_rgba8888_create_single_fb_bitmap(bitmap);
} else if (bitmap->format == BITMAP_FMT_BGRA8888) {
lcd = lcd_mem_bgra8888_create_single_fb(bitmap->w, bitmap->h, buff);
lcd = lcd_mem_bgra8888_create_single_fb_bitmap(bitmap);
} else if (bitmap->format == BITMAP_FMT_BGR565) {
lcd = lcd_mem_bgr565_create_single_fb(bitmap->w, bitmap->h, buff);
lcd = lcd_mem_bgr565_create_single_fb_bitmap(bitmap);
} else if (bitmap->format == BITMAP_FMT_RGB565) {
lcd = lcd_mem_rgb565_create_single_fb(bitmap->w, bitmap->h, buff);
lcd = lcd_mem_rgb565_create_single_fb_bitmap(bitmap);
}
#ifdef WITH_LCD_RGB888
else if (bitmap->format == BITMAP_FMT_RGB888) {
lcd = lcd_mem_rgb888_create_single_fb(bitmap->w, bitmap->h, buff);
lcd = lcd_mem_rgb888_create_single_fb_bitmap(bitmap);
} else if (bitmap->format == BITMAP_FMT_BGR888) {
lcd = lcd_mem_bgr888_create_single_fb(bitmap->w, bitmap->h, buff);
lcd = lcd_mem_bgr888_create_single_fb_bitmap(bitmap);
}
#endif /*WITH_LCD_RGB888*/

Expand Down
20 changes: 20 additions & 0 deletions src/base/graphic_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ ret_t graphic_buffer_create_for_bitmap(bitmap_t* bitmap);
graphic_buffer_t* graphic_buffer_create_with_data(const uint8_t* data, uint32_t w, uint32_t h,
bitmap_format_t format);

/**
* @method graphic_buffer_create_with_data_ex
* 创建缓冲区。
*
* > 为了兼容raw图像。
* @param {const uint8_t*} virtual_data 数据虚拟地址。
* @param {const uint8_t*} physical_data 数据物理地址。
* @param {uint32_t} w 宽度。
* @param {uint32_t} h 宽度。
* @param {uint32_t} line_length 每行宽度(字节数)
* @param {bitmap_format_t} format 格式。
*
* @return {graphic_buffer_t*} 返回缓存区。
*/
graphic_buffer_t* graphic_buffer_create_with_data_ex(const uint8_t* virtual_data,
const uint8_t* physical_data, uint32_t w,
uint32_t h, uint32_t line_length,
bitmap_format_t format);
/**
* @method graphic_buffer_lock_for_read
* 为读取数据而锁定缓冲区。
Expand Down Expand Up @@ -198,6 +216,8 @@ ret_t graphic_buffer_destroy(graphic_buffer_t* buffer);
#define GRAPHIC_BUFFER_CREATE_WITH_DATA(data, w, h, format) \
graphic_buffer_create_with_data(data, w, h, format)

#define GRAPHIC_BUFFER_CREATE_WITH_DATA_EX(data, physical_data_addr, w, h, line_length, format) \
graphic_buffer_create_with_data_ex(data, physical_data_addr, w, h, line_length, format)
END_C_DECLS

#endif /*TK_GRAPHIC_BUFFER_H*/
3 changes: 2 additions & 1 deletion src/blend/image_g2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ ret_t image_rotate_ex(bitmap_t* dst, bitmap_t* src, const rect_t* src_r, xy_t dx
ret_t image_rotate_blend(bitmap_t* dst, bitmap_t* src, const rectf_t* dst_r, const rectf_t* src_r,
uint8_t global_alpha, lcd_orientation_t o);

ret_t image_g2d_benchmark(void);
#endif

ret_t image_g2d_benchmark(void);

END_C_DECLS

#endif /*TK_IMAGE_G2D_H*/
8 changes: 6 additions & 2 deletions src/blend/soft_g2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ ret_t soft_fill_rect(bitmap_t* dst, const rect_t* dst_r, color_t c) {
return fill_abgr8888_rect(dst, dst_r, c);
}
#endif /*LCD_BGR565_LITE*/
default:
default: {
log_debug("not support format: w=%u h=%u format=%u\n", dst->w, dst->h, dst->format);
break;
}
}

assert(!"not supported format");
Expand Down Expand Up @@ -219,8 +221,10 @@ ret_t soft_rotate_image(bitmap_t* dst, bitmap_t* src, const rect_t* src_r, lcd_o
return rotate_bgra8888_image(dst, src, src_r, o);
}
#endif /*LCD_BGR565_LITE*/
default:
default: {
log_debug("not support format: w=%u h=%u format=%u\n", dst->w, dst->h, dst->format);
break;
}
}

assert(!"not supported format");
Expand Down
7 changes: 7 additions & 0 deletions src/graphic_buffer/graphic_buffer_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ graphic_buffer_t* graphic_buffer_create_with_data(const uint8_t* data, uint32_t
return GRAPHIC_BUFFER(buffer);
}

graphic_buffer_t* graphic_buffer_create_with_data_ex(const uint8_t* virtual_data,
const uint8_t* physical_data, uint32_t w,
uint32_t h, uint32_t line_length,
bitmap_format_t format) {
return graphic_buffer_create_with_data(virtual_data, w, h, format);
}

ret_t graphic_buffer_create_for_bitmap(bitmap_t* bitmap) {
uint32_t line_length = bitmap_get_line_length(bitmap);
return_value_if_fail(bitmap != NULL && bitmap->buffer == NULL, RET_BAD_PARAMS);
Expand Down
69 changes: 60 additions & 9 deletions src/lcd/lcd_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ typedef struct _lcd_mem_t {
uint32_t online_line_length;
uint32_t line_length;
bitmap_format_t format;
bool_t own_offline_fb;

graphic_buffer_t* online_gb;
graphic_buffer_t* offline_gb;

bitmap_t* fb_bitmaps[3];
bool_t should_destroy_fb_bitmaps;
lcd_fb_dirty_rects_t fb_dirty_rects_list;

/*VBI: vertical blank interrupt。用于2fb等待当前显示完成,以便把下一帧的数据从offline fb拷贝到online fb,从而避免因为同时访问online fb数据造成闪烁。*/
Expand Down Expand Up @@ -84,6 +82,7 @@ static inline ret_t lcd_mem_set_wait_vbi(lcd_t* lcd, lcd_mem_wait_vbi_t wait_vbi
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
*/
static inline ret_t lcd_mem_deinit(lcd_mem_t* mem) {
uint32_t i = 0;
return_value_if_fail(mem != NULL && mem->base.begin_frame != NULL, RET_BAD_PARAMS);

lcd_fb_dirty_rects_deinit(&(mem->fb_dirty_rects_list));
Expand All @@ -92,13 +91,13 @@ static inline ret_t lcd_mem_deinit(lcd_mem_t* mem) {
mem->vgcanvas = NULL;
}

if (mem->own_offline_fb) {
TKMEM_FREE(mem->offline_fb);
if (mem->should_destroy_fb_bitmaps) {
for (i = 0; i < ARRAY_SIZE(mem->fb_bitmaps); i++) {
bitmap_destroy(mem->fb_bitmaps[i]);
mem->fb_bitmaps[i] = NULL;
}
}

graphic_buffer_destroy(mem->online_gb);
graphic_buffer_destroy(mem->offline_gb);

return RET_OK;
}

Expand Down Expand Up @@ -186,6 +185,58 @@ static inline uint8_t* lcd_mem_get_next_fb(lcd_mem_t* lcd) {
return lcd->next_fb;
}

static inline ret_t lcd_mem_set_single_fb_bitmap(lcd_t* lcd, bitmap_t* offline_fb_bitmap) {
lcd_mem_t* mem = (lcd_mem_t*)lcd;
return_value_if_fail(mem != NULL, RET_BAD_PARAMS);
return_value_if_fail(offline_fb_bitmap != NULL, RET_BAD_PARAMS);

lcd_mem_set_offline_fb(mem, bitmap_lock_buffer_for_write(offline_fb_bitmap));
mem->fb_bitmaps[0] = offline_fb_bitmap;

return RET_OK;
}

static inline ret_t lcd_mem_set_double_fb_bitmap(lcd_t* lcd, bitmap_t* online_fb_bitmap, bitmap_t* offline_fb_bitmap) {
lcd_mem_t* mem = (lcd_mem_t*)lcd;
return_value_if_fail(mem != NULL, RET_BAD_PARAMS);
return_value_if_fail(online_fb_bitmap != NULL && offline_fb_bitmap != NULL, RET_BAD_PARAMS);

assert(online_fb_bitmap->w == offline_fb_bitmap->w);
assert(online_fb_bitmap->h == offline_fb_bitmap->h);
assert(online_fb_bitmap->format == offline_fb_bitmap->format);

lcd_mem_set_online_fb(mem, bitmap_lock_buffer_for_write(online_fb_bitmap));
lcd_mem_set_offline_fb(mem, bitmap_lock_buffer_for_write(offline_fb_bitmap));

mem->fb_bitmaps[0] = offline_fb_bitmap;
mem->fb_bitmaps[1] = online_fb_bitmap;

return RET_OK;
}

static inline ret_t lcd_mem_set_three_fb_bitmap(lcd_t* lcd, bitmap_t* online_fb_bitmap, bitmap_t* offline_fb_bitmap,
bitmap_t* next_fb_bitmap) {
lcd_mem_t* mem = (lcd_mem_t*)lcd;
return_value_if_fail(mem != NULL, RET_BAD_PARAMS);
return_value_if_fail(online_fb_bitmap != NULL && offline_fb_bitmap != NULL && next_fb_bitmap != NULL, RET_BAD_PARAMS);

assert(online_fb_bitmap->w == next_fb_bitmap->w);
assert(online_fb_bitmap->h == next_fb_bitmap->h);
assert(online_fb_bitmap->w == offline_fb_bitmap->w);
assert(online_fb_bitmap->h == offline_fb_bitmap->h);
assert(online_fb_bitmap->format == offline_fb_bitmap->format);

lcd_mem_set_next_fb(mem, bitmap_lock_buffer_for_write(next_fb_bitmap));
lcd_mem_set_online_fb(mem, bitmap_lock_buffer_for_write(online_fb_bitmap));
lcd_mem_set_offline_fb(mem, bitmap_lock_buffer_for_write(offline_fb_bitmap));

mem->fb_bitmaps[0] = offline_fb_bitmap;
mem->fb_bitmaps[1] = online_fb_bitmap;
mem->fb_bitmaps[2] = next_fb_bitmap;

return RET_OK;
}

END_C_DECLS

#endif /*TK_LCD_MEM_H*/
Loading

0 comments on commit b97a166

Please sign in to comment.