Skip to content

Commit

Permalink
impl bitmap_create_ex with bitmap_init_ex
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Nov 28, 2024
1 parent f310109 commit 8f3e1c9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 40 deletions.
17 changes: 9 additions & 8 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,16 +24,16 @@
* fix memory error whene change theme.

2024/11/17
* 完善 TK_STRINGIZE(感谢兆坤提供补丁)
* 完善 TK\_STRINGIZE(感谢兆坤提供补丁)
* value完善调试log(感谢兆坤提供补丁)
* 修复拼写错误(感谢兆坤提供补丁)
* 增加原子操作(感谢兆坤提供补丁)

2024/11/15
* 修复拼写错误(感谢兆坤提供补丁)
* 修复 demoui dialog(感谢兆坤提供补丁)
* 增加tk_strs_bsearch
* 修复text_selector测试用例报错问题(感谢颖健提供补丁)
* 增加tk\_strs\_bsearch
* 修复text\_selector测试用例报错问题(感谢颖健提供补丁)
* 修复控件有offset时更新指针icon没更新的问题(感谢颖健提供补丁)

2024/11/14
Expand Down
55 changes: 27 additions & 28 deletions src/base/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand Down
36 changes: 32 additions & 4 deletions tests/bitmap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#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++) {
bitmap_t* b = bitmap_create_ex(i + 1, i + 1, 0, BITMAP_FMT_BGRA8888);
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);
}

Expand All @@ -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);
Expand Down

0 comments on commit 8f3e1c9

Please sign in to comment.