font_mempool.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #include <os_common_api.h>
  2. #include <mem_manager.h>
  3. #include <ui_mem.h>
  4. #include <string.h>
  5. #include "bitmap_font_api.h"
  6. #ifdef CONFIG_SIMULATOR
  7. #include <kheap.h>
  8. #define ALLOC_NO_WAIT OS_NO_WAIT
  9. #else
  10. #define ALLOC_NO_WAIT K_NO_WAIT
  11. #endif
  12. typedef struct _cache_mem_info
  13. {
  14. void* ptr;
  15. size_t size;
  16. struct _cache_mem_info* next;
  17. }cache_mem_info_t;
  18. #define CMAP_CACHE_SIZE 1024*8
  19. #ifdef CONFIG_BITMAP_FONT_USE_HIGH_FREQ_CACHE
  20. #ifdef CONFIG_BITMAP_FONT_HIGH_FREQ_CACHE_SIZE
  21. #define BITMAP_FONT_HIGH_FREQ_CACHE_SIZE CONFIG_BITMAP_FONT_HIGH_FREQ_CACHE_SIZE
  22. #else
  23. #define BITMAP_FONT_HIGH_FREQ_CACHE_SIZE 1500*1024
  24. #endif //CONFIG_BITMAP_FONT_HIGH_FREQ_CACHE_SIZE
  25. #else
  26. #define BITMAP_FONT_HIGH_FREQ_CACHE_SIZE 0
  27. #endif //CONFIG_BITMAP_FONT_USE_HIGH_FREQ_CACHE
  28. #ifdef CONFIG_BITMAP_PER_FONT_CACHE_SIZE
  29. #define BITMAP_FONT_CACHE_SIZE CONFIG_BITMAP_PER_FONT_CACHE_SIZE
  30. #else
  31. #define BITMAP_FONT_CACHE_SIZE 1024*64
  32. #endif //CONFIG_BITMAP_PER_FONT_CACHE_SIZE
  33. #ifdef CONFIG_BITMAP_FONT_MAX_OPENED_FONT
  34. #define MAX_OPEND_FONT CONFIG_BITMAP_FONT_MAX_OPENED_FONT
  35. #else
  36. #define MAX_OPEND_FONT 2
  37. #endif //CONFIG_BITMAP_FONT_MAX_OPENED_FONT
  38. #ifdef CONFIG_BITMAP_FONT_SUPPORT_EMOJI
  39. #define MAX_EMOJI_FONT 1
  40. #define MAX_EMOJI_NUM 100
  41. #else
  42. #define MAX_EMOJI_FONT 0
  43. #define MAX_EMOJI_NUM 0
  44. #endif //CONFIG_BITMAP_FONT_SUPPORT_EMOJI
  45. //#define BITMAP_FONT_PSRAM_SIZE (MAX_OPEND_FONT+MAX_EMOJI_FONT)*(BITMAP_FONT_CACHE_SIZE+CMAP_CACHE_SIZE)+MAX_EMOJI_NUM*sizeof(emoji_font_entry_t)+BITMAP_FONT_HIGH_FREQ_CACHE_SIZE
  46. #ifdef CONFIG_BITMAP_FONT_CACHE_POOL_SIZE
  47. #define BITMAP_FONT_PSRAM_SIZE CONFIG_BITMAP_FONT_CACHE_POOL_SIZE
  48. #else
  49. #define BITMAP_FONT_PSRAM_SIZE 360*1024
  50. #endif
  51. #define DEBUG_FONT_GLYPH 0
  52. #define PRINT_FONT_GLYPH_ERR 0
  53. #ifdef CONFIG_FREETYPE_FONT_MAX_FACES
  54. #define FREETYPE_FONT_MAX_FACES CONFIG_FREETYPE_FONT_MAX_FACES
  55. #else
  56. #define FREETYPE_FONT_MAX_FACES 2
  57. #endif
  58. #ifdef CONFIG_FREETYPE_FONT_MAX_SIZES
  59. #define FREETYPE_FONT_MAX_SIZES CONFIG_FREETYPE_FONT_MAX_SIZES
  60. #else
  61. #define FREETYPE_FONT_MAX_SIZES 3
  62. #endif
  63. #ifdef CONFIG_FREETYPE_FONT_MAX_SUBCACHE_BYTES
  64. #define FREETYPE_FONT_MAX_SUBCACHE_BYTES CONFIG_FREETYPE_FONT_MAX_SUBCACHE_BYTES
  65. #else
  66. #define FREETYPE_FONT_MAX_SUBCACHE_BYTES 150*1024
  67. #endif
  68. #if CONFIG_FREETYPE_FONT_SHAPE_CACHE_SIZE > 0
  69. static char __aligned(4) shape_cache_buffer[CONFIG_FREETYPE_FONT_SHAPE_CACHE_SIZE] __in_section_unique(font.bss.cache);
  70. static struct k_heap shape_cache_pool = {
  71. .heap = {
  72. .init_mem = shape_cache_buffer,
  73. .init_bytes = CONFIG_FREETYPE_FONT_SHAPE_CACHE_SIZE,
  74. },
  75. };
  76. #endif
  77. static char __aligned(4) bmp_font_info_buffer[12 * 1024] __in_section_unique(font.bss.cache);
  78. static struct k_heap font_info_pool = {
  79. .heap = {
  80. .init_mem = bmp_font_info_buffer,
  81. .init_bytes = 12 * 1024,
  82. },
  83. };
  84. #if defined(CONFIG_BITMAP_FONT_CACHE_POOL_SIZE) && CONFIG_BITMAP_FONT_CACHE_POOL_SIZE >= 12 * 1024
  85. static char __aligned(4) bmp_font_cache_buffer[BITMAP_FONT_PSRAM_SIZE - 12 * 1024] __in_section_unique(font.bss.cache);
  86. static struct k_heap font_mem_pool = {
  87. .heap = {
  88. .init_mem = bmp_font_cache_buffer,
  89. .init_bytes = BITMAP_FONT_PSRAM_SIZE - 12 * 1024,
  90. },
  91. };
  92. #endif
  93. static int font_cache_peak_size = 0;
  94. static int font_cache_total_size = 0;
  95. static int font_cache_used_size = 0;
  96. static int font_cahce_item_num = 0;
  97. static cache_mem_info_t* cache_mem_info;
  98. static int font_cache_inited = 0;
  99. void bitmap_font_cache_init(void)
  100. {
  101. if(font_cache_inited == 0)
  102. {
  103. font_cache_total_size = BITMAP_FONT_PSRAM_SIZE;
  104. font_cache_used_size = 0;
  105. os_printk("bitmap_font_cache_init\n");
  106. k_heap_init(&font_info_pool, font_info_pool.heap.init_mem, font_info_pool.heap.init_bytes);
  107. #if defined(CONFIG_BITMAP_FONT_CACHE_POOL_SIZE) && CONFIG_BITMAP_FONT_CACHE_POOL_SIZE >= 10000
  108. k_heap_init(&font_mem_pool, font_mem_pool.heap.init_mem, font_mem_pool.heap.init_bytes);
  109. #endif
  110. #if CONFIG_FREETYPE_FONT_SHAPE_CACHE_SIZE > 0
  111. k_heap_init(&shape_cache_pool, shape_cache_pool.heap.init_mem, shape_cache_pool.heap.init_bytes);
  112. #endif
  113. font_cache_inited = 1;
  114. }
  115. }
  116. static void _add_cache_mem_info(void* ptr, uint32_t size)
  117. {
  118. cache_mem_info_t* item=NULL;
  119. //item = mem_malloc(sizeof(cache_mem_info_t));
  120. item = k_heap_alloc(&font_info_pool, sizeof(cache_mem_info_t), ALLOC_NO_WAIT);
  121. if(item == NULL)
  122. {
  123. SYS_LOG_INF("cache mem info malloc failed %d, %d\n", font_cache_used_size, font_cahce_item_num);
  124. return;
  125. }
  126. item->ptr = ptr;
  127. item->size = size;
  128. item->next = NULL;
  129. if(cache_mem_info==NULL)
  130. {
  131. cache_mem_info = item;
  132. }
  133. else
  134. {
  135. cache_mem_info_t* pos = cache_mem_info;
  136. cache_mem_info_t* prev = cache_mem_info;
  137. while(pos)
  138. {
  139. prev = pos;
  140. pos = pos->next;
  141. }
  142. prev->next = item;
  143. }
  144. font_cache_used_size += size;
  145. font_cahce_item_num++;
  146. if(font_cache_peak_size < font_cache_used_size)
  147. {
  148. font_cache_peak_size = font_cache_used_size;
  149. //SYS_LOG_INF("font_cache_peak_size %d\n", font_cache_peak_size);
  150. }
  151. //SYS_LOG_INF("font_cache_used_size %d, font_cahce_item_num %d\n", font_cache_used_size, font_cahce_item_num);
  152. //SYS_LOG_INF("font cache mem add %d, total %d, peak size %d\n", size, font_cache_used_size, font_cache_peak_size);
  153. }
  154. static void _remove_cache_mem_info(void* ptr)
  155. {
  156. cache_mem_info_t* item;
  157. cache_mem_info_t* prev;
  158. item = cache_mem_info;
  159. prev = NULL;
  160. while(item)
  161. {
  162. if(item->ptr == ptr)
  163. {
  164. break;
  165. }
  166. prev = item;
  167. item = item->next;
  168. }
  169. if(item == NULL)
  170. {
  171. SYS_LOG_INF("cache mem info not found %p\n", ptr);
  172. return;
  173. }
  174. if(prev)
  175. {
  176. prev->next = item->next;
  177. }
  178. if(item == cache_mem_info)
  179. {
  180. cache_mem_info = item->next;
  181. }
  182. font_cahce_item_num--;
  183. font_cache_used_size -= item->size;
  184. //SYS_LOG_INF("font cache mem remove %d, total %d\n", item->size, font_cache_used_size);
  185. k_heap_free(&font_info_pool, item);
  186. }
  187. uint32_t _get_cache_mem_info(void* ptr)
  188. {
  189. cache_mem_info_t* item;
  190. item = cache_mem_info;
  191. while(item)
  192. {
  193. if(item->ptr == ptr)
  194. {
  195. break;
  196. }
  197. item = item->next;
  198. }
  199. if (item) {
  200. return item->size;
  201. }
  202. return 0;
  203. }
  204. void* bitmap_font_cache_malloc(uint32_t size)
  205. {
  206. void *ptr;
  207. if(size % 4 != 0)
  208. {
  209. size = (size/4 + 1)*4;
  210. }
  211. #if defined(CONFIG_BITMAP_FONT_CACHE_POOL_SIZE) && CONFIG_BITMAP_FONT_CACHE_POOL_SIZE >= 10000
  212. ptr = k_heap_alloc(&font_mem_pool, size, ALLOC_NO_WAIT);
  213. #else
  214. ptr = ui_mem_gui_alloc(size);
  215. #endif
  216. if (ptr == NULL) {
  217. SYS_LOG_ERR("font cache heap alloc failed, size %d\n", size);
  218. return ptr;
  219. }
  220. _add_cache_mem_info(ptr, size);
  221. return ptr;
  222. }
  223. void bitmap_font_cache_free(void* ptr)
  224. {
  225. #if defined(CONFIG_BITMAP_FONT_CACHE_POOL_SIZE) && CONFIG_BITMAP_FONT_CACHE_POOL_SIZE >= 10000
  226. k_heap_free(&font_mem_pool, ptr);
  227. #else
  228. ui_mem_gui_free(ptr);
  229. #endif
  230. _remove_cache_mem_info(ptr);
  231. }
  232. uint32_t bitmap_font_cache_get_size(void* ptr)
  233. {
  234. return _get_cache_mem_info(ptr);
  235. }
  236. uint32_t bitmap_font_get_max_fonts_num(void)
  237. {
  238. return (MAX_OPEND_FONT+MAX_EMOJI_FONT);
  239. }
  240. //use as default size if not configd for current font size
  241. uint32_t bitmap_font_get_font_cache_size(void)
  242. {
  243. return BITMAP_FONT_CACHE_SIZE;
  244. }
  245. uint32_t bitmap_font_get_max_emoji_num(void)
  246. {
  247. return MAX_EMOJI_NUM;
  248. }
  249. uint32_t bitmap_font_get_cmap_cache_size(void)
  250. {
  251. return CMAP_CACHE_SIZE;
  252. }
  253. void bitmap_font_cache_dump_info(void)
  254. {
  255. SYS_LOG_INF("bitmap font cache info dump: total used %d, peak size %d, max size %d\n", font_cache_used_size, font_cache_peak_size, font_cache_total_size);
  256. }
  257. int bitmap_font_glyph_debug_is_on(void)
  258. {
  259. #if DEBUG_FONT_GLYPH==1
  260. return 1;
  261. #else
  262. return 0;
  263. #endif
  264. }
  265. int bitmap_font_glyph_err_print_is_on(void)
  266. {
  267. #if PRINT_FONT_GLYPH_ERR==1
  268. return 1;
  269. #else
  270. return 0;
  271. #endif
  272. }
  273. int bitmap_font_get_high_freq_enabled(void)
  274. {
  275. #ifdef CONFIG_BITMAP_FONT_USE_HIGH_FREQ_CACHE
  276. return 1;
  277. #else
  278. return 0;
  279. #endif
  280. }
  281. void bitmap_font_get_decompress_param(int bmp_size, int font_size, int* in_size, int* line_size)
  282. {
  283. *in_size = bmp_size*3/2;
  284. *line_size = ((font_size+3)/4)*4*2;
  285. }
  286. int freetype_font_get_font_fixed_bpp(void)
  287. {
  288. #ifdef CONFIG_FREETYPE_FONT_BITMAP_BPP
  289. return CONFIG_FREETYPE_FONT_BITMAP_BPP;
  290. #else
  291. return 2;
  292. #endif
  293. }
  294. int freetype_font_get_max_face_num(void)
  295. {
  296. return FREETYPE_FONT_MAX_FACES;
  297. }
  298. int freetype_font_get_max_size_num(void)
  299. {
  300. return FREETYPE_FONT_MAX_SIZES;
  301. }
  302. uint32_t freetype_font_get_font_cache_size(void)
  303. {
  304. #ifdef CONFIG_FREETYPE_PER_FONT_CACHE_SIZE
  305. return CONFIG_FREETYPE_PER_FONT_CACHE_SIZE;
  306. #else
  307. return 65536;
  308. #endif
  309. }
  310. int freetype_font_get_max_ftccache_bytes(void)
  311. {
  312. return FREETYPE_FONT_MAX_SUBCACHE_BYTES;
  313. }
  314. int freetype_font_get_memory_face_enabled(void)
  315. {
  316. #ifndef CONFIG_SIMULATOR
  317. #ifdef CONFIG_FREETYPE_FONT_ENABLE_MEMORY_FACE
  318. return 1;
  319. #else
  320. return 0;
  321. #endif
  322. #else
  323. return 0;
  324. #endif
  325. }
  326. int freetype_font_use_svg_path(void)
  327. {
  328. #ifdef CONFIG_FREETYPE_FONT_ENABLE_SVG_PATH
  329. return 1;
  330. #else
  331. return 0;
  332. #endif
  333. }
  334. int freetype_font_get_max_vertices(void)
  335. {
  336. #ifdef CONFIG_FREETYPE_FONT_ENABLE_SVG_PATH
  337. return 256;
  338. #else
  339. return 1;
  340. #endif
  341. }
  342. int freetype_font_enable_subpixel(void)
  343. {
  344. #ifdef ENABLE_SUBPIX
  345. return 1;
  346. #else
  347. return 0;
  348. #endif
  349. }
  350. int emoji_font_use_mmap(void)
  351. {
  352. #ifdef CONFIG_EMOJI_FONT_USE_MMAP
  353. return 1;
  354. #else
  355. return 0;
  356. #endif
  357. }
  358. void* freetype_font_shape_cache_malloc(uint32_t size)
  359. {
  360. #if CONFIG_FREETYPE_FONT_SHAPE_CACHE_SIZE > 0
  361. void *ptr;
  362. ptr = k_heap_alloc(&shape_cache_pool, size, ALLOC_NO_WAIT);
  363. if (ptr == NULL) {
  364. SYS_LOG_ERR("shape cache heap alloc failed, size %d\n", size);
  365. return ptr;
  366. }
  367. return ptr;
  368. #else
  369. return NULL;
  370. #endif
  371. }
  372. void freetype_font_shape_cache_free(void* ptr)
  373. {
  374. #if CONFIG_FREETYPE_FONT_SHAPE_CACHE_SIZE > 0
  375. k_heap_free(&shape_cache_pool, ptr);
  376. #else
  377. return;
  378. #endif
  379. }
  380. int freetype_font_get_shape_info_size(void)
  381. {
  382. #if CONFIG_FREETYPE_FONT_SHAPE_CACHE_SIZE > 0
  383. return CONFIG_FREETYPE_SHAPE_INFO_CACHE_SIZE;
  384. #else
  385. return 0;
  386. #endif
  387. }