Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Jan 16, 2019
1 parent 2e845ba commit 6829ec7
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 178 deletions.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* 增加darray类,逐步替换以前的array类。
* 增加widget pool用于控件的缓存和重复利用,可以通过WITH\_WIDGET\_POOL启用。
* 增加widget\_create函数,简化子类控件的创建函数,同时让通用控件本身的由widget pool集中管理。
* 使用array的地方替换为darray。

* 2019/01/15
* 整理API文档:progress\_circle/rich\_text/slide\_menu/slide\_view/svg\_image/switch
Expand Down
39 changes: 20 additions & 19 deletions src/base/assets_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
#include "base/system_info.h"
#include "base/assets_manager.h"

static int asset_cache_cmp_type(const void* a, const void* b) {
const asset_info_t* aa = (const asset_info_t*)a;
const asset_info_t* bb = (const asset_info_t*)b;

if (aa->is_in_rom) {
return -1;
}

return aa->type - bb->type;
}

static assets_manager_t* s_assets_manager = NULL;

#ifdef WITH_FS_RES
Expand Down Expand Up @@ -280,7 +291,8 @@ assets_manager_t* assets_manager_create(uint32_t init_nr) {
assets_manager_t* assets_manager_init(assets_manager_t* rm, uint32_t init_nr) {
return_value_if_fail(rm != NULL, NULL);

array_init(&(rm->assets), init_nr);
darray_init(&(rm->assets), init_nr, (tk_destroy_t)asset_info_unref,
(tk_compare_t)asset_cache_cmp_type);

return rm;
}
Expand All @@ -300,8 +312,7 @@ ret_t assets_manager_add(assets_manager_t* rm, const void* info) {
return_value_if_fail(rm != NULL && info != NULL, RET_BAD_PARAMS);

asset_info_ref((asset_info_t*)r);

return array_push(&(rm->assets), (void*)r);
return darray_push(&(rm->assets), (void*)r);
}

const asset_info_t* assets_manager_find_in_cache(assets_manager_t* rm, asset_type_t type,
Expand Down Expand Up @@ -347,35 +358,25 @@ ret_t assets_manager_unref(assets_manager_t* rm, const asset_info_t* info) {
if (!(info->is_in_rom)) {
bool_t remove = info->refcount <= 1;

asset_info_unref((asset_info_t*)info);
if (remove) {
array_remove(&(rm->assets), NULL, (void*)info, NULL);
tk_compare_t cmp = rm->assets.compare;
rm->assets.compare = pointer_compare;
darray_remove(&(rm->assets), (void*)info);
rm->assets.compare = cmp;
}
}

return RET_OK;
}

static int asset_cache_cmp_type(const void* a, const void* b) {
const asset_info_t* aa = (const asset_info_t*)a;
const asset_info_t* bb = (const asset_info_t*)b;

if (aa->is_in_rom) {
return -1;
}

return aa->type - bb->type;
}

ret_t assets_manager_clear_cache(assets_manager_t* rm, asset_type_t type) {
asset_info_t info;

memset(&info, 0x00, sizeof(info));
info.type = type;
return_value_if_fail(rm != NULL, RET_BAD_PARAMS);

return array_remove_all(&(rm->assets), asset_cache_cmp_type, &info,
(tk_destroy_t)asset_info_unref);
return darray_remove_all(&(rm->assets), &info);
}

ret_t assets_manager_deinit(assets_manager_t* rm) {
Expand All @@ -392,7 +393,7 @@ ret_t assets_manager_deinit(assets_manager_t* rm) {
}

TKMEM_FREE(rm->res_root);
array_deinit(&(rm->assets));
darray_deinit(&(rm->assets));

return RET_OK;
}
Expand Down
4 changes: 2 additions & 2 deletions src/base/assets_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#ifndef TK_ASSETS_MANAGER_H
#define TK_ASSETS_MANAGER_H

#include "tkc/array.h"
#include "tkc/darray.h"

BEGIN_C_DECLS

Expand Down Expand Up @@ -205,7 +205,7 @@ typedef struct _asset_info_t {
*
*/
typedef struct _assets_manager_t {
array_t assets;
darray_t assets;

char* res_root;
} assets_manager_t;
Expand Down
48 changes: 18 additions & 30 deletions src/base/font_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@

static font_manager_t* s_font_manager = NULL;

typedef struct _font_cmp_info_t {
const char* name;
uint32_t size;
} font_cmp_info_t;

static int32_t font_cmp(font_t* font, font_cmp_info_t* info) {
if (font_match(font, info->name, info->size)) {
return 0;
}

return -1;
}

font_manager_t* font_manager(void) {
return s_font_manager;
}
Expand All @@ -42,7 +55,7 @@ font_manager_t* font_manager_create(font_loader_t* loader) {

font_manager_t* font_manager_init(font_manager_t* fm, font_loader_t* loader) {
return_value_if_fail(fm != NULL, NULL);
array_init(&(fm->fonts), 2);
darray_init(&(fm->fonts), 2, (tk_destroy_t)font_destroy, (tk_compare_t)font_cmp);

fm->loader = loader;

Expand All @@ -60,27 +73,14 @@ ret_t font_manager_set_assets_manager(font_manager_t* fm, assets_manager_t* am)
ret_t font_manager_add_font(font_manager_t* fm, font_t* font) {
return_value_if_fail(fm != NULL && font != NULL, RET_BAD_PARAMS);

return array_push(&(fm->fonts), font);
return darray_push(&(fm->fonts), font);
}

static font_t* font_manager_lookup(font_manager_t* fm, const char* name, font_size_t size) {
uint32_t i = 0;
uint32_t nr = 0;
font_t** fonts = NULL;
font_cmp_info_t info = {name, size};
return_value_if_fail(fm != NULL, NULL);

nr = fm->fonts.size;
fonts = (font_t**)fm->fonts.elms;
return_value_if_fail(nr > 0, NULL);

for (i = 0; i < nr; i++) {
font_t* iter = fonts[i];
if (font_match(iter, name, size)) {
return iter;
}
}

return NULL;
return darray_find(&(fm->fonts), &info);
}

font_t* font_manager_load(font_manager_t* fm, const char* name, uint32_t size) {
Expand Down Expand Up @@ -123,21 +123,9 @@ font_t* font_manager_get_font(font_manager_t* fm, const char* name, font_size_t
}

ret_t font_manager_deinit(font_manager_t* fm) {
uint32_t i = 0;
uint32_t nr = 0;
font_t** fonts = NULL;
return_value_if_fail(fm != NULL, RET_BAD_PARAMS);

nr = fm->fonts.size;
fonts = (font_t**)fm->fonts.elms;
for (i = 0; i < nr; i++) {
font_t* iter = fonts[i];
font_destroy(iter);
}

array_deinit(&(fm->fonts));

return RET_OK;
return darray_deinit(&(fm->fonts));
}

ret_t font_manager_destroy(font_manager_t* fm) {
Expand Down
4 changes: 2 additions & 2 deletions src/base/font_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#ifndef TK_FONT_MANAGER_H
#define TK_FONT_MANAGER_H

#include "tkc/array.h"
#include "tkc/darray.h"
#include "base/font_loader.h"
#include "base/assets_manager.h"

Expand All @@ -34,7 +34,7 @@ BEGIN_C_DECLS
* (如果使用nanovg,字体由nanovg内部管理)
*/
typedef struct _font_manager_t {
array_t fonts;
darray_t fonts;

/**
* @property {font_loader_t*} loader
Expand Down
104 changes: 50 additions & 54 deletions src/base/image_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ typedef struct _bitmap_cache_t {
uint32_t last_access_time;
} bitmap_cache_t;

static int bitmap_cache_cmp_time(bitmap_cache_t* a, bitmap_cache_t* b) {
return (a->last_access_time <= b->last_access_time) ? 0 : -1;
}

static int bitmap_cache_cmp_name(bitmap_cache_t* a, bitmap_cache_t* b) {
return strcmp(a->name, b->name);
}

static int bitmap_cache_cmp_data(bitmap_cache_t* a, bitmap_cache_t* b) {
return (char*)(a->image.data) - (char*)(b->image.data);
}

static ret_t bitmap_cache_destroy(bitmap_cache_t* cache) {
return_value_if_fail(cache != NULL, RET_BAD_PARAMS);

bitmap_destroy(&(cache->image));
TKMEM_FREE(cache);

return RET_OK;
}

static image_manager_t* s_image_manager = NULL;
image_manager_t* image_manager() {
return s_image_manager;
Expand All @@ -54,7 +75,7 @@ image_manager_t* image_manager_init(image_manager_t* imm, image_loader_t* loader
return_value_if_fail(imm != NULL, NULL);

imm->loader = loader;
array_init(&(imm->images), 0);
darray_init(&(imm->images), 0, (tk_destroy_t)bitmap_cache_destroy, NULL);
imm->assets_manager = assets_manager();

return imm;
Expand All @@ -76,53 +97,49 @@ ret_t image_manager_add(image_manager_t* imm, const char* name, const bitmap_t*
cache->last_access_time = cache->created_time;
cache->image.name = cache->name;

return array_push(&(imm->images), cache);
return darray_push(&(imm->images), cache);
}

ret_t image_manager_lookup(image_manager_t* imm, const char* name, bitmap_t* image) {
uint32_t i = 0;
uint32_t nr = 0;
bitmap_cache_t info;
bitmap_cache_t* iter = NULL;
bitmap_cache_t** all = NULL;
return_value_if_fail(imm != NULL && name != NULL && image != NULL, RET_BAD_PARAMS);

all = (bitmap_cache_t**)(imm->images.elms);
for (i = 0, nr = imm->images.size; i < nr; i++) {
iter = all[i];
if (strcmp(name, iter->name) == 0) {
*image = iter->image;
image->destroy = NULL;
image->specific_destroy = NULL;
image->should_free_data = FALSE;
tk_strncpy(info.name, name, TK_NAME_LEN);
imm->images.compare = (tk_compare_t)bitmap_cache_cmp_name;
iter = darray_find(&(imm->images), &info);

iter->access_count++;
iter->last_access_time = time_now_s();
if (iter != NULL) {
*image = iter->image;
image->destroy = NULL;
image->specific_destroy = NULL;
image->should_free_data = FALSE;

return RET_OK;
}
iter->access_count++;
iter->last_access_time = time_now_s();

return RET_OK;
}

return RET_NOT_FOUND;
}

ret_t image_manager_update_specific(image_manager_t* imm, bitmap_t* image) {
uint32_t i = 0;
uint32_t nr = 0;
bitmap_cache_t info;
bitmap_cache_t* iter = NULL;
bitmap_cache_t** all = NULL;
return_value_if_fail(imm != NULL && image != NULL, RET_BAD_PARAMS);

all = (bitmap_cache_t**)(imm->images.elms);
for (i = 0, nr = imm->images.size; i < nr; i++) {
iter = all[i];
if (image->data == iter->image.data) {
iter->image.flags = image->flags;
iter->image.specific = image->specific;
iter->image.specific_ctx = image->specific_ctx;
iter->image.specific_destroy = image->specific_destroy;
info.image.data = image->data;
imm->images.compare = (tk_compare_t)bitmap_cache_cmp_data;
iter = darray_find(&(imm->images), &info);

return RET_OK;
}
if (iter != NULL) {
iter->image.flags = image->flags;
iter->image.specific = image->specific;
iter->image.specific_ctx = image->specific_ctx;
iter->image.specific_destroy = image->specific_destroy;

return RET_OK;
}

return RET_NOT_FOUND;
Expand Down Expand Up @@ -176,41 +193,20 @@ ret_t image_manager_set_assets_manager(image_manager_t* imm, assets_manager_t* a
return RET_OK;
}

static int bitmap_cache_cmp_time(bitmap_cache_t* a, bitmap_cache_t* b) {
return (a->last_access_time <= b->last_access_time) ? 0 : -1;
}

static ret_t bitmap_cache_destroy(bitmap_cache_t* cache) {
return_value_if_fail(cache != NULL, RET_BAD_PARAMS);

bitmap_destroy(&(cache->image));
TKMEM_FREE(cache);

return RET_OK;
}

ret_t image_manager_unload_unused(image_manager_t* imm, uint32_t time_delta_s) {
bitmap_cache_t b;
b.last_access_time = time_now_s() - time_delta_s;
return_value_if_fail(imm != NULL && imm->loader != NULL, RET_BAD_PARAMS);

return array_remove_all(&(imm->images), (tk_compare_t)bitmap_cache_cmp_time, &b,
(tk_destroy_t)bitmap_cache_destroy);
imm->images.compare = (tk_compare_t)bitmap_cache_cmp_time;
return darray_remove_all(&(imm->images), &b);
}

ret_t image_manager_deinit(image_manager_t* imm) {
uint32_t i = 0;
uint32_t nr = 0;
bitmap_cache_t** all = NULL;
return_value_if_fail(imm != NULL && imm->loader != NULL, RET_BAD_PARAMS);

all = (bitmap_cache_t**)(imm->images.elms);
for (i = 0, nr = imm->images.size; i < nr; i++) {
bitmap_cache_destroy(all[i]);
}

array_deinit(&(imm->images));
imm->loader = NULL;
darray_deinit(&(imm->images));

return RET_OK;
}
Expand Down
6 changes: 3 additions & 3 deletions src/base/image_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#ifndef TK_IMAGE_MANAGER_H
#define TK_IMAGE_MANAGER_H

#include "tkc/array.h"
#include "tkc/darray.h"
#include "base/image_loader.h"
#include "base/assets_manager.h"

Expand All @@ -46,11 +46,11 @@ typedef struct _bitmap_header_t {
*/
typedef struct _image_manager_t {
/**
* @property {array_t} images
* @property {darray_t} images
* @annotation ["private"]
* 缓存的图片。
*/
array_t images;
darray_t images;

/**
* @property {image_loader_t*} loader
Expand Down
Loading

0 comments on commit 6829ec7

Please sign in to comment.