From 8f3e1c97732b698083e8a650955a17517026a58f Mon Sep 17 00:00:00 2001 From: lixianjing Date: Thu, 28 Nov 2024 08:37:46 +0800 Subject: [PATCH] impl bitmap_create_ex with bitmap_init_ex --- docs/changes.md | 17 +++++++------- src/base/bitmap.c | 55 ++++++++++++++++++++++---------------------- tests/bitmap_test.cc | 36 +++++++++++++++++++++++++---- 3 files changed, 68 insertions(+), 40 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 39df937d2..f999e0c62 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,20 +1,21 @@ # 最新动态 2024/11/28 - * 增加函数 bitmap_set_dirty/bitmap_is_dirty + * 增加函数 bitmap\_set\_dirty/bitmap\_is\_dirty + * 重构bitmap,用bitmap\_init\_ex 实现bitmap\_create\_ex 2024/11/26 - * 增加tab_button_group删除tab_button和对应page的函数(感谢智明提供补丁) + * 增加tab\_button\_group删除tab\_button和对应page的函数(感谢智明提供补丁) 2024/11/22 * 修复 mledit编译警告(感谢泽武提供补丁) 2024/11/21 - * 完善conf_json(感谢兆坤提供补丁) + * 完善conf\_json(感谢兆坤提供补丁) 2024/11/19 - * 增加函数 str_append_format_padding - * 增加 atomic_compare_exchange(感谢兆坤提供补丁) + * 增加函数 str\_append\_format\_padding + * 增加 atomic\_compare\_exchange(感谢兆坤提供补丁) * 完善 atomic(感谢兆坤提供补丁) 2024/11/18 @@ -23,7 +24,7 @@ * fix memory error whene change theme. 2024/11/17 - * 完善 TK_STRINGIZE(感谢兆坤提供补丁) + * 完善 TK\_STRINGIZE(感谢兆坤提供补丁) * value完善调试log(感谢兆坤提供补丁) * 修复拼写错误(感谢兆坤提供补丁) * 增加原子操作(感谢兆坤提供补丁) @@ -31,8 +32,8 @@ 2024/11/15 * 修复拼写错误(感谢兆坤提供补丁) * 修复 demoui dialog(感谢兆坤提供补丁) - * 增加tk_strs_bsearch - * 修复text_selector测试用例报错问题(感谢颖健提供补丁) + * 增加tk\_strs\_bsearch + * 修复text\_selector测试用例报错问题(感谢颖健提供补丁) * 修复控件有offset时更新指针icon没更新的问题(感谢颖健提供补丁) 2024/11/14 diff --git a/src/base/bitmap.c b/src/base/bitmap.c index 924a498b0..a4be64ec0 100644 --- a/src/base/bitmap.c +++ b/src/base/bitmap.c @@ -124,41 +124,35 @@ static ret_t bitmap_web_destroy(bitmap_t* bitmap) { return RET_OK; } +static ret_t bitmap_platform_create(bitmap_t* bitmap) { + uint32_t w = bitmap->w; + uint32_t h = bitmap->h; + uint32_t line_length = bitmap_get_physical_line_length(bitmap); + bitmap_format_t format = bitmap->format; + + uint8_t* data = bitmap_lock_buffer_for_write(bitmap); + return_value_if_fail(format == BITMAP_FMT_RGBA8888, NULL); + int32_t 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); + + return RET_OK; +} +#else +static ret_t bitmap_platform_create(bitmap_t* bitmap) { + return RET_OK; +} #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); - uint32_t bpp = bitmap_get_bpp_of_format(format); - return_value_if_fail(bitmap != NULL, NULL); - - bitmap->w = w; - bitmap->h = h; - bitmap->format = format; + + bitmap_init_ex(bitmap, w, h, line_length, format, NULL); bitmap->should_free_handle = TRUE; - if (bpp < 4) { - bitmap->flags = BITMAP_FLAG_OPAQUE; - } - - bitmap_set_line_length(bitmap, line_length); - - bitmap_alloc_data(bitmap); - if (bitmap->buffer == NULL) { - TKMEM_FREE(bitmap); - bitmap = NULL; - } else { -#ifdef AWTK_WEB - uint8_t* data = bitmap_lock_buffer_for_write(bitmap); - return_value_if_fail(format == BITMAP_FMT_RGBA8888, NULL); - int32_t 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); -#endif /*AWTK_WEB*/ - } - return bitmap; } @@ -587,6 +581,7 @@ ret_t bitmap_init(bitmap_t* bitmap, uint32_t w, uint32_t h, bitmap_format_t form 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); } @@ -612,6 +607,10 @@ ret_t bitmap_init_ex(bitmap_t* bitmap, uint32_t w, uint32_t h, uint32_t line_len bitmap->should_free_data = TRUE; } + if (bitmap->buffer == NULL) { + bitmap_platform_create(bitmap); + } + return bitmap->buffer != NULL ? RET_OK : RET_OOM; } diff --git a/tests/bitmap_test.cc b/tests/bitmap_test.cc index e1d924ecb..44b46e3b8 100644 --- a/tests/bitmap_test.cc +++ b/tests/bitmap_test.cc @@ -2,7 +2,7 @@ #include "base/bitmap.h" #include "gtest/gtest.h" -TEST(Bitmap, basic) { +TEST(Bitmap, basic1) { uint32_t n = 100; uint32_t i = 0; for (i = 0; i < n; i++) { @@ -10,10 +10,10 @@ TEST(Bitmap, basic) { uint8_t* bdata = bitmap_lock_buffer_for_write(b); ASSERT_EQ(((intptr_t)(bdata)) % BITMAP_ALIGN_SIZE, (intptr_t)0); ASSERT_EQ(bitmap_get_line_length(b), b->w * 4u); - ASSERT_EQ(bitmap_is_dirty(b), TRUE); - bitmap_unlock_buffer(b); ASSERT_EQ(bitmap_is_dirty(b), FALSE); - + bitmap_unlock_buffer(b); + ASSERT_EQ(bitmap_is_dirty(b), TRUE); + ASSERT_EQ(b->should_free_handle, TRUE); bitmap_destroy(b); } @@ -23,10 +23,38 @@ TEST(Bitmap, basic) { ASSERT_EQ(((intptr_t)(bdata)) % BITMAP_ALIGN_SIZE, (intptr_t)0); ASSERT_EQ(bitmap_get_line_length(b), b->w * 2u); bitmap_unlock_buffer(b); + ASSERT_EQ(b->should_free_handle, TRUE); bitmap_destroy(b); } } +TEST(Bitmap, basic2) { + bitmap_t bitmap; + uint32_t n = 100; + uint32_t i = 0; + bitmap_t* b = &bitmap; + + for (i = 0; i < n; i++) { + bitmap_init_ex(b, i + 1, i + 1, 0, BITMAP_FMT_BGRA8888, NULL); + uint8_t* bdata = bitmap_lock_buffer_for_write(b); + ASSERT_EQ(((intptr_t)(bdata)) % BITMAP_ALIGN_SIZE, (intptr_t)0); + ASSERT_EQ(bitmap_get_line_length(b), b->w * 4u); + ASSERT_EQ(bitmap_is_dirty(b), FALSE); + bitmap_unlock_buffer(b); + ASSERT_EQ(bitmap_is_dirty(b), TRUE); + ASSERT_EQ(b->should_free_handle, FALSE); + } + + for (i = 0; i < n; i++) { + bitmap_init_ex(b, i + 1, i + 1, 0, BITMAP_FMT_BGR565, NULL); + uint8_t* bdata = bitmap_lock_buffer_for_write(b); + ASSERT_EQ(((intptr_t)(bdata)) % BITMAP_ALIGN_SIZE, (intptr_t)0); + ASSERT_EQ(bitmap_get_line_length(b), b->w * 2u); + bitmap_unlock_buffer(b); + ASSERT_EQ(b->should_free_handle, FALSE); + } +} + TEST(Bitmap, row_size) { ASSERT_EQ(TK_BITMAP_MONO_LINE_LENGTH(8), 2); ASSERT_EQ(TK_BITMAP_MONO_LINE_LENGTH(9), 2);