lvgl_res_loader.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <os_common_api.h>
  5. #include <res_manager_api.h>
  6. #include <lvgl/lvgl_res_loader.h>
  7. #ifdef CONFIG_UI_MANAGER
  8. #include <ui_manager.h>
  9. #endif
  10. #include "res_mempool.h"
  11. #define RES_PRELOAD_STACKSIZE 1536
  12. typedef enum
  13. {
  14. PRELOAD_TYPE_IMMEDIATE,
  15. PRELOAD_TYPE_NORMAL,
  16. PRELOAD_TYPE_NORMAL_COMPACT,
  17. PRELOAD_TYPE_BEGIN_CALLBACK,
  18. PRELOAD_TYPE_END_CALLBACK,
  19. }preload_type_e;
  20. typedef struct _preload_default_t
  21. {
  22. uint32_t scene_id;
  23. uint32_t* resource_id;
  24. uint32_t resource_num;
  25. void (*callback)(int32_t status, void* param);
  26. char* style_path;
  27. char* picture_path;
  28. char* text_path;
  29. struct _preload_default_t* next;
  30. }preload_default_t;
  31. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  32. #define MAX_BITMAP_CHECKABLE 160
  33. #define MAX_STRING_CHECKABLE 64
  34. typedef struct _bitmap_mem_debug_item
  35. {
  36. uint32_t scene_id;
  37. uint32_t res_id[MAX_BITMAP_CHECKABLE];
  38. uint32_t ref[MAX_BITMAP_CHECKABLE];
  39. uint32_t addr[MAX_BITMAP_CHECKABLE];
  40. struct _bitmap_mem_debug_item* next;
  41. }bitmap_mem_debug_t;
  42. typedef struct _string_mem_debug_item
  43. {
  44. uint32_t scene_id;
  45. uint32_t res_id[MAX_STRING_CHECKABLE];
  46. uint32_t ref[MAX_STRING_CHECKABLE];
  47. uint32_t addr[MAX_STRING_CHECKABLE];
  48. struct _string_mem_debug_item* next;
  49. }string_mem_debug_t;
  50. static bool bitmap_mem_debug_enabled = false;
  51. static uint32_t default_scene_id = 0;
  52. static bitmap_mem_debug_t* bitmap_mem_debug_list = NULL;
  53. static string_mem_debug_t* string_mem_debug_list = NULL;
  54. static void _bitmap_mem_debug_enable(bool enable);
  55. #endif
  56. static os_sem preload_sem;
  57. static os_mutex preload_mutex;
  58. static uint32_t preload_running = 1;
  59. static preload_param_t* param_list = NULL;
  60. static preload_param_t* sync_param_list = NULL;
  61. static lvgl_res_scene_t current_scene;
  62. static lvgl_res_group_t current_group;
  63. static lvgl_res_group_t current_subgrp;
  64. static resource_info_t** res_info;
  65. static int32_t res_manager_inited = 0;
  66. //static int32_t current_preload_count = 0;
  67. static preload_default_t* preload_default_list = NULL;
  68. static int screen_width;
  69. static int screen_height;
  70. static int max_resource_sets;
  71. static uint32_t max_compact_block_size;
  72. static size_t res_mem_align = 4;
  73. #ifndef CONFIG_RES_MANAGER_SKIP_PRELOAD
  74. static char __aligned(ARCH_STACK_PTR_ALIGN) __in_section_unique(ram.noinit.stack)
  75. res_preload_stack[CONFIG_LVGL_RES_PRELOAD_STACKSIZE];
  76. static void _res_preload_thread(void *parama1, void *parama2, void *parama3);
  77. #endif
  78. void lvgl_res_scene_preload_default_cb_for_view(int32_t status, void *view_id);
  79. void lvgl_res_cache_clear(uint32_t force_clear)
  80. {
  81. res_manager_clear_cache(force_clear);
  82. }
  83. int lvgl_res_loader_init(uint32_t screen_w, uint32_t screen_h)
  84. {
  85. res_manager_init();
  86. res_manager_set_screen_size(screen_w, screen_h);
  87. /* create a thread to preload*/
  88. #ifndef CONFIG_RES_MANAGER_SKIP_PRELOAD
  89. os_sem_init(&preload_sem, 0, 1);
  90. os_mutex_init(&preload_mutex);
  91. int tid = os_thread_create(res_preload_stack, RES_PRELOAD_STACKSIZE,
  92. _res_preload_thread,
  93. NULL, NULL, NULL,
  94. CONFIG_LVGL_RES_PRELOAD_PRIORITY, 0, 0);
  95. os_thread_name_set((os_tid_t)tid, "res_preload");
  96. #endif
  97. memset(&current_scene, 0, sizeof(lvgl_res_scene_t));
  98. memset(&current_group, 0, sizeof(lvgl_res_group_t));
  99. memset(&current_subgrp, 0, sizeof(lvgl_res_group_t));
  100. max_resource_sets = res_mem_get_max_resource_sets();
  101. res_info = (resource_info_t**)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(resource_info_t*)*max_resource_sets);
  102. if(!res_info)
  103. {
  104. SYS_LOG_ERR("res_info malloc faild\n");
  105. return -1;
  106. }
  107. memset(res_info, 0, sizeof(resource_info_t*)*max_resource_sets);
  108. screen_width = screen_w;
  109. screen_height = screen_h;
  110. max_compact_block_size = res_mem_get_max_compact_block_size();
  111. res_manager_inited = 1;
  112. res_mem_align = res_mem_get_align();
  113. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  114. _bitmap_mem_debug_enable(true);
  115. #endif
  116. return 0;
  117. }
  118. void lvgl_res_loader_deinit(void)
  119. {
  120. uint32_t i;
  121. if(res_manager_inited == 0)
  122. {
  123. return;
  124. }
  125. for(i=0;i<max_resource_sets;i++)
  126. {
  127. if(res_info[i] != NULL)
  128. {
  129. res_manager_close_resources(res_info[i]);
  130. }
  131. res_info[i] = NULL;
  132. }
  133. res_array_free(res_info);
  134. }
  135. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  136. static void _bitmap_mem_debug_enable(bool enable)
  137. {
  138. bitmap_mem_debug_enabled = enable;
  139. }
  140. static void _bitmap_mem_debug_set_default_scene_id(uint32_t scene_id)
  141. {
  142. bitmap_mem_debug_t* item = NULL;
  143. bitmap_mem_debug_t* prev = NULL;
  144. string_mem_debug_t* sitem = NULL;
  145. string_mem_debug_t* sprev = NULL;
  146. int i;
  147. if(!bitmap_mem_debug_enabled)
  148. {
  149. return;
  150. }
  151. //SYS_LOG_INF("RES LEAK INF: load scene 0x%x\n", scene_id);
  152. default_scene_id = scene_id;
  153. item = bitmap_mem_debug_list;
  154. while(item)
  155. {
  156. if(item->scene_id == scene_id)
  157. {
  158. SYS_LOG_INF("RES LEAK WARNING: scene 0x%x maybe not unloaded\n", item->scene_id);
  159. SYS_LOG_INF("unloaded bitmap ids: \n");
  160. for(i=0;i<MAX_BITMAP_CHECKABLE;i++)
  161. {
  162. if(item->res_id[i] == 0)
  163. {
  164. break;
  165. }
  166. SYS_LOG_INF("res id %d, ref %d\n", item->res_id[i], item->ref[i]);
  167. }
  168. return;
  169. }
  170. prev = item;
  171. item = item->next;
  172. }
  173. if(prev == NULL)
  174. {
  175. bitmap_mem_debug_list = res_mem_alloc(RES_MEM_POOL_BMP, sizeof(bitmap_mem_debug_t));
  176. if(!bitmap_mem_debug_list)
  177. {
  178. SYS_LOG_INF("no memory to debug res mem leak for scene 0x%x\n", scene_id);
  179. }
  180. memset(bitmap_mem_debug_list, 0, sizeof(bitmap_mem_debug_t));
  181. bitmap_mem_debug_list->scene_id = scene_id;
  182. }
  183. else
  184. {
  185. item = res_mem_alloc(RES_MEM_POOL_BMP, sizeof(bitmap_mem_debug_t));
  186. if(!item)
  187. {
  188. SYS_LOG_INF("no memory to debug res mem leak for scene 0x%x\n", scene_id);
  189. }
  190. memset(item, 0, sizeof(bitmap_mem_debug_t));
  191. item->scene_id = scene_id;
  192. prev->next = item;
  193. }
  194. //string resource debug list init
  195. sitem = string_mem_debug_list;
  196. while(sitem)
  197. {
  198. if(sitem->scene_id == scene_id)
  199. {
  200. SYS_LOG_INF("RES LEAK WARNING: scene 0x%x maybe not unloaded\n", sitem->scene_id);
  201. SYS_LOG_INF("unloaded string ids: \n");
  202. for(i=0;i<MAX_STRING_CHECKABLE;i++)
  203. {
  204. if(sitem->res_id[i] == 0)
  205. {
  206. break;
  207. }
  208. SYS_LOG_INF("res id %d, ref %d\n", sitem->res_id[i], sitem->ref[i]);
  209. }
  210. return;
  211. }
  212. sprev = sitem;
  213. sitem = sitem->next;
  214. }
  215. if(sprev == NULL)
  216. {
  217. string_mem_debug_list = res_mem_alloc(RES_MEM_POOL_BMP, sizeof(string_mem_debug_t));
  218. if(!string_mem_debug_list)
  219. {
  220. SYS_LOG_INF("no memory to debug res mem leak for scene 0x%x\n", scene_id);
  221. }
  222. memset(string_mem_debug_list, 0, sizeof(string_mem_debug_t));
  223. string_mem_debug_list->scene_id = scene_id;
  224. }
  225. else
  226. {
  227. sitem = res_mem_alloc(RES_MEM_POOL_BMP, sizeof(string_mem_debug_t));
  228. if(!sitem)
  229. {
  230. SYS_LOG_INF("no memory to debug res mem leak for scene 0x%x\n", scene_id);
  231. }
  232. memset(sitem, 0, sizeof(string_mem_debug_t));
  233. sitem->scene_id = scene_id;
  234. sprev->next = sitem;
  235. }
  236. }
  237. static void _bitmap_mem_debug_add_bitmap_to_scene(uint32_t scene_id, uint32_t id, uint32_t addr)
  238. {
  239. int i;
  240. bitmap_mem_debug_t* item;
  241. if(!bitmap_mem_debug_enabled)
  242. {
  243. return;
  244. }
  245. item = bitmap_mem_debug_list;
  246. while(item)
  247. {
  248. if(item->scene_id == scene_id || item->scene_id == default_scene_id)
  249. {
  250. break;
  251. }
  252. item = item->next;
  253. }
  254. if(!item)
  255. {
  256. SYS_LOG_INF("no mem debug info registered for scene 0x%x\n", scene_id);
  257. return;
  258. }
  259. for(i=0;i<MAX_BITMAP_CHECKABLE;i++)
  260. {
  261. if(item->res_id[i] == id)
  262. {
  263. item->ref[i]++;
  264. if(item->addr[i] != addr && item->ref[i] > 1)
  265. {
  266. SYS_LOG_INF("RES LEAK WARNING: unmatched addr for same res id %d, previous 0x%x, current 0x%x\n",
  267. id, item->addr[i], addr);
  268. }
  269. //update addr
  270. if(item->ref[i] == 1)
  271. {
  272. item->addr[i] = addr;
  273. }
  274. return;
  275. }
  276. else if(item->res_id[i] == 0)
  277. {
  278. break;
  279. }
  280. }
  281. if(i>=MAX_BITMAP_CHECKABLE)
  282. {
  283. SYS_LOG_INF("too many pics to check\n");
  284. return;
  285. }
  286. item->res_id[i] = id;
  287. item->addr[i] = addr;
  288. item->ref[i] = 1;
  289. }
  290. static void _bitmap_mem_debug_add_string_to_scene(uint32_t scene_id, uint32_t id, uint32_t addr)
  291. {
  292. int i;
  293. string_mem_debug_t* item;
  294. if(!bitmap_mem_debug_enabled)
  295. {
  296. return;
  297. }
  298. item = string_mem_debug_list;
  299. while(item)
  300. {
  301. if(item->scene_id == scene_id || item->scene_id == default_scene_id)
  302. {
  303. break;
  304. }
  305. item = item->next;
  306. }
  307. if(!item)
  308. {
  309. SYS_LOG_INF("no mem debug info registered for scene 0x%x\n", scene_id);
  310. return;
  311. }
  312. for(i=0;i<MAX_STRING_CHECKABLE;i++)
  313. {
  314. if(item->res_id[i] == id)
  315. {
  316. item->ref[i]++;
  317. if(item->addr[i] != addr && item->ref[i] > 1)
  318. {
  319. SYS_LOG_INF("RES LEAK WARNING: unmatched addr for same string id %d, previous 0x%x, current 0x%x\n",
  320. id, item->addr[i], addr);
  321. }
  322. //update addr
  323. if(item->ref[i] == 1)
  324. {
  325. item->addr[i] = addr;
  326. }
  327. return;
  328. }
  329. else if(item->res_id[i] == 0)
  330. {
  331. break;
  332. }
  333. }
  334. if(i>=MAX_STRING_CHECKABLE)
  335. {
  336. SYS_LOG_INF("too many strings to check\n");
  337. return;
  338. }
  339. item->res_id[i] = id;
  340. item->addr[i] = addr;
  341. item->ref[i] = 1;
  342. }
  343. static void _bitmap_mem_debug_remove_bitmap_from_scene(uint32_t addr)
  344. {
  345. int i;
  346. bitmap_mem_debug_t* item;
  347. int not_found = 1;
  348. if(!bitmap_mem_debug_enabled)
  349. {
  350. return;
  351. }
  352. item = bitmap_mem_debug_list;
  353. while(item)
  354. {
  355. for(i=0;i < MAX_BITMAP_CHECKABLE;i++)
  356. {
  357. if(item->res_id[i] == 0)
  358. {
  359. break;
  360. }
  361. if(item->addr[i] == addr)
  362. {
  363. not_found = 0;
  364. if(item->ref[i] > 0)
  365. {
  366. item->ref[i]--;
  367. }
  368. break;
  369. }
  370. }
  371. item = item->next;
  372. }
  373. if(not_found == 1)
  374. {
  375. SYS_LOG_INF("RES LEAK WARNING: pic addr 0x%x not found when unload\n", addr);
  376. }
  377. }
  378. static void _bitmap_mem_debug_remove_string_from_scene(uint32_t addr)
  379. {
  380. int i;
  381. string_mem_debug_t* item;
  382. int not_found = 1;
  383. if(!bitmap_mem_debug_enabled)
  384. {
  385. return;
  386. }
  387. item = string_mem_debug_list;
  388. while(item)
  389. {
  390. for(i=0;i < MAX_STRING_CHECKABLE;i++)
  391. {
  392. if(item->res_id[i] == 0)
  393. {
  394. break;
  395. }
  396. if(item->addr[i] == addr)
  397. {
  398. not_found = 0;
  399. if(item->ref[i] > 0)
  400. {
  401. item->ref[i]--;
  402. }
  403. break;
  404. }
  405. }
  406. item = item->next;
  407. }
  408. if(not_found == 1)
  409. {
  410. SYS_LOG_INF("RES LEAK WARNING: string addr 0x%x not found when unload\n", addr);
  411. }
  412. }
  413. static void _bitmap_mem_debug_bitmap_id_check(uint32_t scene_id, bool unload_compact_buffer)
  414. {
  415. int i;
  416. bitmap_mem_debug_t* item;
  417. bitmap_mem_debug_t* prev;
  418. string_mem_debug_t* sitem;
  419. string_mem_debug_t* sprev;
  420. if(!bitmap_mem_debug_enabled)
  421. {
  422. return;
  423. }
  424. //SYS_LOG_INF("RES LEAK INF: unload scene 0x%x, unload compact %d\n", scene_id, unload_compact_buffer);
  425. //check and release bitmap debug item
  426. item = bitmap_mem_debug_list;
  427. prev = NULL;
  428. while(item)
  429. {
  430. if(item->scene_id == scene_id)
  431. {
  432. break;
  433. }
  434. prev = item;
  435. item = item->next;
  436. }
  437. if(!item)
  438. {
  439. SYS_LOG_INF("RES LEAK WARNING: scene to be unload not found 0x%x\n", scene_id);
  440. return;
  441. }
  442. for(i=0;i<MAX_BITMAP_CHECKABLE;i++)
  443. {
  444. if(item->res_id[i]!= 0 && item->ref[i] > 0)
  445. {
  446. SYS_LOG_INF("RES LEAK WARNING: bitmap maybe leaked, scene 0x%x, res id %d, ref %d\n", item->scene_id, item->res_id[i], item->ref[i]);
  447. }
  448. }
  449. if(prev == NULL)
  450. {
  451. bitmap_mem_debug_list = item->next;
  452. }
  453. else
  454. {
  455. prev->next = item->next;
  456. }
  457. res_mem_free(RES_MEM_POOL_BMP, item);
  458. if(unload_compact_buffer)
  459. {
  460. return;
  461. }
  462. //check and release string debug item
  463. sitem = string_mem_debug_list;
  464. sprev = NULL;
  465. while(sitem)
  466. {
  467. if(sitem->scene_id == scene_id)
  468. {
  469. break;
  470. }
  471. sprev = sitem;
  472. sitem = sitem->next;
  473. }
  474. if(!sitem)
  475. {
  476. SYS_LOG_INF("RES LEAK WARNING: scene to be unload not found 0x%x\n", scene_id);
  477. return;
  478. }
  479. for(i=0;i<MAX_STRING_CHECKABLE;i++)
  480. {
  481. if(sitem->res_id[i]!= 0 && sitem->ref[i] > 0)
  482. {
  483. SYS_LOG_INF("RES LEAK WARNING: string maybe leaked, res id %d, ref %d\n", sitem->res_id[i], sitem->ref[i]);
  484. }
  485. }
  486. if(sprev == NULL)
  487. {
  488. string_mem_debug_list = sitem->next;
  489. }
  490. else
  491. {
  492. sprev->next = sitem->next;
  493. }
  494. res_mem_free(RES_MEM_POOL_BMP, sitem);
  495. }
  496. static void _bitmap_mem_debug_compact_buffer_check(uint32_t scene_id)
  497. {
  498. res_manager_compact_buffer_check(scene_id);
  499. }
  500. #endif
  501. void _res_file_close(resource_info_t* info, uint32_t force_close)
  502. {
  503. uint32_t i;
  504. if(info == NULL)
  505. {
  506. SYS_LOG_ERR("null resource info when unload");
  507. return;
  508. }
  509. for(i=0;i<max_resource_sets;i++)
  510. {
  511. if(res_info[i] == info)
  512. {
  513. if(res_info[i]->reference >= 1)
  514. {
  515. res_info[i]->reference--;
  516. }
  517. if(res_info[i]->reference == 0 && force_close)
  518. {
  519. res_info[i] = NULL;
  520. break;
  521. }
  522. else
  523. {
  524. return;
  525. }
  526. }
  527. }
  528. if(i >= max_resource_sets)
  529. {
  530. SYS_LOG_ERR("resource info not found when unload");
  531. return;
  532. }
  533. res_manager_close_resources(info);
  534. }
  535. resource_info_t* _res_file_open(const char* style_path, const char* picture_path, const char* text_path, uint32_t force_ref, uint32_t in_preload)
  536. {
  537. resource_info_t* info;
  538. uint32_t i;
  539. int32_t empty_slot = -1;
  540. int32_t expired_slot = -1;
  541. for(i=0;i<max_resource_sets;i++)
  542. {
  543. if(res_info[i] != NULL)
  544. {
  545. if(strcmp(style_path, res_info[i]->sty_path)==0)
  546. {
  547. if(force_ref)
  548. {
  549. res_info[i]->reference++;
  550. }
  551. if(in_preload == 0 && strcmp(text_path, res_info[i]->str_path) != 0)
  552. {
  553. SYS_LOG_INF("testres: change str file %s to %s\n", res_info[i]->str_path, text_path);
  554. res_manager_set_str_file(res_info[i], text_path);
  555. }
  556. return res_info[i];
  557. }
  558. else if(res_info[i]->reference == 0)
  559. {
  560. expired_slot = i;
  561. }
  562. }
  563. else if(empty_slot == -1)
  564. {
  565. empty_slot = i;
  566. }
  567. }
  568. if(empty_slot == -1 && expired_slot == -1)
  569. {
  570. SYS_LOG_ERR("too many resources sets opened");
  571. for(i=0;i<max_resource_sets;i++)
  572. {
  573. SYS_LOG_ERR("res set %d: %s\n", i, res_info[i]->sty_path);
  574. }
  575. return NULL;
  576. }
  577. info = res_manager_open_resources(style_path, picture_path, text_path);
  578. if(info == NULL)
  579. {
  580. SYS_LOG_ERR("open resources failed");
  581. return NULL;
  582. }
  583. if(force_ref)
  584. {
  585. info->reference = 1;
  586. }
  587. if(empty_slot != -1)
  588. {
  589. res_info[empty_slot] = info;
  590. }
  591. else
  592. {
  593. _res_file_close(res_info[expired_slot], 1);
  594. res_info[expired_slot] = info;
  595. }
  596. return info;
  597. }
  598. //caller to ensure no scene is loaded when close files
  599. int lvgl_res_close_resources(const char* style_path, const char* picture_path, const char* text_path)
  600. {
  601. int slot = -1;
  602. int i;
  603. if(style_path == NULL)
  604. {
  605. SYS_LOG_ERR("null style path to be closed\n");
  606. return -1;
  607. }
  608. for(i=0;i<max_resource_sets;i++)
  609. {
  610. if(res_info[i] != NULL)
  611. {
  612. if(strcmp(style_path, res_info[i]->sty_path)==0)
  613. {
  614. slot = i;
  615. break;
  616. }
  617. }
  618. }
  619. if(slot >= 0)
  620. {
  621. //force close, dont check, but warn.
  622. if(res_info[slot]->reference > 0)
  623. {
  624. SYS_LOG_INF("WARNING: closing referred resources\n");
  625. }
  626. res_manager_close_resources(res_info[slot]);
  627. res_info[slot] = NULL;
  628. preload_default_t* preload = preload_default_list;
  629. preload_default_t* prev = NULL;
  630. while(preload)
  631. {
  632. if((strcmp(preload->style_path, style_path)==0) &&
  633. (strcmp(preload->picture_path, picture_path)==0))
  634. {
  635. break;
  636. }
  637. prev=preload;
  638. preload = preload->next;
  639. }
  640. if(preload == NULL)
  641. {
  642. //not in preload default list
  643. return 0;
  644. }
  645. if(preload == preload_default_list)
  646. {
  647. preload_default_list = preload->next;
  648. }
  649. else
  650. {
  651. prev->next = preload->next;
  652. }
  653. res_mem_free(RES_MEM_POOL_BMP,preload);
  654. preload = NULL;
  655. return 0;
  656. }
  657. else
  658. {
  659. SYS_LOG_ERR("resource file to be closed not found\n");
  660. return -1;
  661. }
  662. }
  663. int lvgl_res_load_scene(uint32_t scene_id, lvgl_res_scene_t* scene, const char* style_path, const char* picture_path, const char* text_path)
  664. {
  665. resource_scene_t* res_scene;
  666. if(scene == NULL)
  667. {
  668. SYS_LOG_ERR("null scene param\n");
  669. return -1;
  670. }
  671. os_strace_u32(SYS_TRACE_ID_RES_SCENE_LOAD, (uint32_t)scene_id);
  672. scene->res_info = _res_file_open(style_path, picture_path, text_path, 1, 0);
  673. if(scene->res_info == NULL)
  674. {
  675. SYS_LOG_ERR("resource info error\n");
  676. return -1;
  677. }
  678. res_scene = res_manager_load_scene(scene->res_info, scene_id);
  679. if(res_scene == NULL)
  680. {
  681. return -1;
  682. }
  683. scene->id = scene_id;
  684. scene->scene_data = res_scene;
  685. scene->background = lv_color_hex(res_scene->background);
  686. scene->x = res_scene->x;
  687. scene->y = res_scene->y;
  688. scene->width = res_scene->width;
  689. scene->height = res_scene->height;
  690. scene->transparence = res_scene->transparence;
  691. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_LOAD, (uint32_t)scene_id);
  692. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  693. _bitmap_mem_debug_set_default_scene_id(scene_id);
  694. _bitmap_mem_debug_compact_buffer_check(scene_id);
  695. #endif
  696. return 0;
  697. }
  698. int32_t lvgl_res_set_pictures_regular(uint32_t scene_id, uint32_t group_id, uint32_t subgroup_id, uint32_t* id, uint32_t num,
  699. const uint8_t* style_path, const char* picture_path, const char* text_path)
  700. {
  701. resource_info_t* info;
  702. int32_t ret;
  703. info = _res_file_open(style_path, picture_path, text_path, 0, 0);
  704. if(info == NULL)
  705. {
  706. SYS_LOG_ERR("open resource info error\n");
  707. return -1;
  708. }
  709. ret = res_manager_set_pictures_regular(info, scene_id, group_id, subgroup_id, id, num);
  710. if(ret < 0)
  711. {
  712. SYS_LOG_ERR("set regular resource failed\n");
  713. return -1;
  714. }
  715. return ret;
  716. }
  717. int32_t lvgl_res_clear_regular_pictures(uint32_t scene_id, const uint8_t* style_path)
  718. {
  719. int32_t ret;
  720. ret = res_manager_clear_regular_pictures(style_path, scene_id);
  721. if(ret < 0)
  722. {
  723. SYS_LOG_ERR("clear resource regular info failed\n");
  724. return -1;
  725. }
  726. return 0;
  727. }
  728. int lvgl_res_load_group_from_scene(lvgl_res_scene_t* scene, uint32_t id, lvgl_res_group_t* group)
  729. {
  730. resource_group_t* res_group;
  731. if(scene == NULL || group == NULL)
  732. {
  733. SYS_LOG_ERR("null scene %p or null group %p\n", scene, group);
  734. return -1;
  735. }
  736. res_group = (resource_group_t*) res_manager_get_scene_child(scene->res_info, scene->scene_data, id);
  737. if(res_group == NULL)
  738. {
  739. return -1;
  740. }
  741. group->id = id;
  742. group->group_data = res_group;
  743. group->x = res_group->sty_data->x;
  744. group->y = res_group->sty_data->y;
  745. group->width = res_group->sty_data->width;
  746. group->height = res_group->sty_data->height;
  747. group->res_info = scene->res_info;
  748. return 0;
  749. }
  750. int lvgl_res_load_group_from_group(lvgl_res_group_t* group, uint32_t id, lvgl_res_group_t* subgroup)
  751. {
  752. resource_group_t* res_group;
  753. if(group == NULL || subgroup == NULL)
  754. {
  755. SYS_LOG_ERR("null scene %p or null group %p\n", group, subgroup);
  756. return -1;
  757. }
  758. res_group = res_manager_get_group_child(group->res_info, group->group_data, id);
  759. if(res_group == NULL)
  760. {
  761. return -1;
  762. }
  763. subgroup->id = id;
  764. subgroup->group_data = res_group;
  765. subgroup->x = res_group->sty_data->x;
  766. subgroup->y = res_group->sty_data->y;
  767. subgroup->width = res_group->sty_data->width;
  768. subgroup->height = res_group->sty_data->height;
  769. subgroup->res_info = group->res_info;
  770. return 0;
  771. }
  772. static int _cvt_bmp2dsc(lv_img_dsc_t *dsc, resource_bitmap_t *bmp)
  773. {
  774. if (bmp->sty_data->width <= 0 || bmp->sty_data->height <= 0)
  775. return -EINVAL;
  776. switch (bmp->sty_data->format) {
  777. case RESOURCE_BITMAP_FORMAT_ARGB8888:
  778. dsc->header.cf = LV_IMG_CF_ARGB_8888;
  779. break;
  780. case RESOURCE_BITMAP_FORMAT_RGB565:
  781. dsc->header.cf = LV_IMG_CF_RGB_565;
  782. break;
  783. case RESOURCE_BITMAP_FORMAT_ARGB8565:
  784. dsc->header.cf = LV_IMG_CF_ARGB_8565;
  785. break;
  786. case RESOURCE_BITMAP_FORMAT_A8:
  787. dsc->header.cf = LV_IMG_CF_ALPHA_8BIT;
  788. break;
  789. case RESOURCE_BITMAP_FORMAT_ARGB6666:
  790. dsc->header.cf = LV_IMG_CF_ARGB_6666;
  791. break;
  792. case RESOURCE_BITMAP_FORMAT_INDEX8:
  793. dsc->header.cf = LV_IMG_CF_INDEXED_8BIT_ACTS;
  794. break;
  795. case RESOURCE_BITMAP_FORMAT_LVGL_INDEX8:
  796. dsc->header.cf = LV_IMG_CF_INDEXED_8BIT;
  797. break;
  798. case RESOURCE_BITMAP_FORMAT_INDEX4:
  799. dsc->header.cf = LV_IMG_CF_INDEXED_4BIT;
  800. break;
  801. case RESOURCE_BITMAP_FORMAT_RAW:
  802. dsc->header.cf = LV_IMG_CF_RAW;
  803. break;
  804. case RESOURCE_BITMAP_FORMAT_JPEG:
  805. #ifdef CONFIG_LVGL_USE_IMG_DECODER_ACTS
  806. dsc->header.cf = LV_IMG_CF_RAW;
  807. #else
  808. #ifdef CONFIG_LV_COLOR_DEPTH_32
  809. dsc->header.cf = LV_IMG_CF_RGB_888;
  810. #else
  811. dsc->header.cf = LV_IMG_CF_RGB_565;
  812. #endif
  813. #endif
  814. break;
  815. case RESOURCE_BITMAP_FORMAT_ARGB1555:
  816. dsc->header.cf = LV_IMG_CF_ARGB_1555;
  817. break;
  818. case RESOURCE_BITMAP_FORMAT_ETC2_EAC:
  819. dsc->header.cf = LV_IMG_CF_ETC2_EAC;
  820. break;
  821. case RESOURCE_BITMAP_FORMAT_RGB888:
  822. default:
  823. return -EINVAL;
  824. }
  825. dsc->header.always_zero = 0;
  826. dsc->header.w = bmp->sty_data->width;
  827. dsc->header.h = bmp->sty_data->height;
  828. dsc->data = bmp->buffer;
  829. if(bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_RAW
  830. || bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX8
  831. || bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX4
  832. #ifdef CONFIG_LVGL_USE_IMG_DECODER_ACTS
  833. || bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_JPEG
  834. #endif
  835. )
  836. {
  837. //what if compress size is not consistent when upgarded?
  838. dsc->data_size = bmp->sty_data->compress_size;
  839. }
  840. else if(bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_ETC2_EAC) {
  841. dsc->data_size = ((dsc->header.w + 0x3) & ~0x3) *
  842. ((dsc->header.h + 0x3) & ~0x3);
  843. }
  844. else if(bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX4) {
  845. dsc->data_size = ((dsc->header.w + 0x1) >> 1) * dsc->header.h + 64;
  846. }
  847. else if(bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX8) {
  848. dsc->data_size = dsc->header.w * dsc->header.h + 1024 + 4;
  849. }
  850. else if(bmp->sty_data->format == RESOURCE_BITMAP_FORMAT_LVGL_INDEX8) {
  851. dsc->data_size = dsc->header.w * dsc->header.h + 1024;
  852. }
  853. else {
  854. dsc->data_size = bmp->sty_data->width * bmp->sty_data->height * bmp->sty_data->bytes_per_pixel;
  855. }
  856. return 0;
  857. }
  858. int lvgl_res_load_pictures_from_scene(lvgl_res_scene_t* scene, const uint32_t* id, lv_img_dsc_t* img, lv_point_t* pt, uint32_t num)
  859. {
  860. resource_bitmap_t* bitmap;
  861. uint32_t i;
  862. if(scene == NULL || id == NULL)
  863. {
  864. SYS_LOG_ERR("null param %p, %p, %p", scene, id, img);
  865. return -1;
  866. }
  867. os_strace_u32(SYS_TRACE_ID_RES_PICS_LOAD, (uint32_t)scene->scene_data);
  868. if(img != NULL)
  869. {
  870. for(i=0;i<num;i++)
  871. {
  872. img[i].data = NULL;
  873. }
  874. }
  875. for(i=0;i<num;i++)
  876. {
  877. bitmap = (resource_bitmap_t*)res_manager_get_scene_child(scene->res_info, scene->scene_data, id[i]);
  878. if(bitmap == NULL)
  879. {
  880. if(img != NULL)
  881. {
  882. lvgl_res_unload_pictures(img, num);
  883. }
  884. return -1;
  885. }
  886. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  887. _bitmap_mem_debug_add_bitmap_to_scene(scene->id, bitmap->sty_data->id, (uint32_t)bitmap->buffer);
  888. #endif
  889. if(img != NULL)
  890. {
  891. _cvt_bmp2dsc(&img[i], bitmap);
  892. }
  893. if(pt != NULL)
  894. {
  895. pt[i].x = bitmap->sty_data->x;
  896. pt[i].y = bitmap->sty_data->y;
  897. }
  898. res_manager_free_resource_structure(bitmap);
  899. }
  900. os_strace_end_call_u32(SYS_TRACE_ID_RES_PICS_LOAD, (uint32_t)scene->scene_data);
  901. return 0;
  902. }
  903. int lvgl_res_load_strings_from_scene(lvgl_res_scene_t* scene, const uint32_t* id, lvgl_res_string_t* str, uint32_t num)
  904. {
  905. resource_text_t* text;
  906. uint32_t i;
  907. if(scene == NULL || id == NULL || str == NULL)
  908. {
  909. SYS_LOG_ERR("null param %p, %p, %p", scene, id, str);
  910. return -1;
  911. }
  912. os_strace_u32(SYS_TRACE_ID_RES_STRS_LOAD, (uint32_t)scene->scene_data);
  913. for(i=0;i<num;i++)
  914. {
  915. str[i].txt = NULL;
  916. }
  917. for(i=0;i<num;i++)
  918. {
  919. text = (resource_text_t*)res_manager_get_scene_child(scene->res_info, scene->scene_data, id[i]);
  920. if(text == NULL)
  921. {
  922. lvgl_res_unload_strings(str, i);
  923. return -1;
  924. }
  925. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  926. _bitmap_mem_debug_add_string_to_scene(scene->id, text->sty_data->id, (uint32_t)text->buffer);
  927. #endif
  928. str[i].txt = text->buffer;
  929. str[i].color = lv_color_hex(text->sty_data->color);
  930. str[i].bgcolor = lv_color_hex(text->sty_data->color);
  931. str[i].x = text->sty_data->x;
  932. str[i].y = text->sty_data->y;
  933. str[i].width = text->sty_data->width;
  934. str[i].height = text->sty_data->height;
  935. uint16_t align = text->sty_data->align;
  936. align = align & 0x3;
  937. switch(align)
  938. {
  939. case 0:
  940. str[i].align = LV_TEXT_ALIGN_LEFT;
  941. break;
  942. case 2:
  943. str[i].align = LV_TEXT_ALIGN_CENTER;
  944. break;
  945. case 1:
  946. str[i].align = LV_TEXT_ALIGN_RIGHT;
  947. break;
  948. default:
  949. str[i].align = LV_TEXT_ALIGN_AUTO;
  950. break;
  951. }
  952. res_manager_free_resource_structure(text);
  953. }
  954. os_strace_end_call_u32(SYS_TRACE_ID_RES_STRS_LOAD, (uint32_t)scene->scene_data);
  955. return 0;
  956. }
  957. int lvgl_res_load_pictures_from_group(lvgl_res_group_t* group, const uint32_t* id, lv_img_dsc_t* img, lv_point_t* pt, uint32_t num)
  958. {
  959. resource_bitmap_t* bitmap = NULL;
  960. uint32_t i;
  961. if(group == NULL || id == NULL )
  962. {
  963. SYS_LOG_ERR("null param %p, %p, %p", group, id, img);
  964. return -1;
  965. }
  966. os_strace_u32(SYS_TRACE_ID_RES_PICS_LOAD, (uint32_t)group->group_data);
  967. if(img != NULL)
  968. {
  969. for(i=0;i<num;i++)
  970. {
  971. img[i].data = NULL;
  972. }
  973. }
  974. for(i=0;i<num;i++)
  975. {
  976. bitmap = (resource_bitmap_t*)res_manager_get_group_child(group->res_info, group->group_data, id[i]);
  977. if(bitmap == NULL)
  978. {
  979. if(img != NULL)
  980. {
  981. lvgl_res_unload_pictures(img, num);
  982. }
  983. return -1;
  984. }
  985. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  986. _bitmap_mem_debug_add_bitmap_to_scene(0, bitmap->sty_data->id, (uint32_t)bitmap->buffer);
  987. #endif
  988. if(img != NULL)
  989. {
  990. _cvt_bmp2dsc(&img[i], bitmap);
  991. }
  992. if(pt != NULL)
  993. {
  994. pt[i].x = bitmap->sty_data->x;
  995. pt[i].y = bitmap->sty_data->y;
  996. }
  997. res_manager_free_resource_structure(bitmap);
  998. bitmap = NULL;
  999. }
  1000. os_strace_end_call_u32(SYS_TRACE_ID_RES_PICS_LOAD, (uint32_t)group->group_data);
  1001. return 0;
  1002. }
  1003. int lvgl_res_load_strings_from_group(lvgl_res_group_t* group, const uint32_t* id, lvgl_res_string_t* str, uint32_t num)
  1004. {
  1005. resource_text_t* text;
  1006. uint32_t i;
  1007. if(group == NULL || id== NULL || str == NULL)
  1008. {
  1009. SYS_LOG_ERR("null param %p, %p, %p", group, id, str);
  1010. return -1;
  1011. }
  1012. os_strace_u32(SYS_TRACE_ID_RES_STRS_LOAD, (uint32_t)group->group_data);
  1013. for(i=0;i<num;i++)
  1014. {
  1015. str[i].txt = NULL;
  1016. }
  1017. for(i=0;i<num;i++)
  1018. {
  1019. text = (resource_text_t*)res_manager_get_group_child(group->res_info, group->group_data, id[i]);
  1020. if(text == NULL)
  1021. {
  1022. lvgl_res_unload_strings(str, i);
  1023. return -1;
  1024. }
  1025. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  1026. _bitmap_mem_debug_add_string_to_scene(0, text->sty_data->id, (uint32_t)text->buffer);
  1027. #endif
  1028. str[i].txt = text->buffer;
  1029. str[i].color = lv_color_hex(text->sty_data->color);
  1030. str[i].bgcolor = lv_color_hex(text->sty_data->bgcolor);
  1031. str[i].x = text->sty_data->x;
  1032. str[i].y = text->sty_data->y;
  1033. str[i].width = text->sty_data->width;
  1034. str[i].height = text->sty_data->height;
  1035. uint16_t align = text->sty_data->align;
  1036. align = align & 0x3;
  1037. switch(align)
  1038. {
  1039. case 0:
  1040. str[i].align = LV_TEXT_ALIGN_LEFT;
  1041. break;
  1042. case 2:
  1043. str[i].align = LV_TEXT_ALIGN_CENTER;
  1044. break;
  1045. case 1:
  1046. str[i].align = LV_TEXT_ALIGN_RIGHT;
  1047. break;
  1048. default:
  1049. str[i].align = LV_TEXT_ALIGN_AUTO;
  1050. break;
  1051. }
  1052. res_manager_free_resource_structure(text);
  1053. }
  1054. os_strace_end_call_u32(SYS_TRACE_ID_RES_STRS_LOAD, (uint32_t)group->group_data);
  1055. return 0;
  1056. }
  1057. void lvgl_res_unload_scene_compact(uint32_t scene_id)
  1058. {
  1059. os_strace_string(SYS_TRACE_ID_RES_UNLOAD, "scene_compact");
  1060. res_manager_unload_scene(scene_id, NULL);
  1061. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  1062. _bitmap_mem_debug_bitmap_id_check(scene_id, true);
  1063. #endif
  1064. os_strace_end_call_u32(SYS_TRACE_ID_RES_UNLOAD, scene_id);
  1065. }
  1066. void lvgl_res_unload_scene(lvgl_res_scene_t * scene)
  1067. {
  1068. if(scene && scene->scene_data)
  1069. {
  1070. os_strace_string(SYS_TRACE_ID_RES_UNLOAD, "scene");
  1071. // res_manager_unload_scene(0, scene->scene_data);
  1072. if(scene->res_info != NULL)
  1073. {
  1074. _res_file_close(scene->res_info, 0);
  1075. }
  1076. os_strace_end_call_u32(SYS_TRACE_ID_RES_UNLOAD, scene->id);
  1077. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  1078. _bitmap_mem_debug_bitmap_id_check(scene->id, false);
  1079. #endif
  1080. scene->id = 0;
  1081. scene->scene_data = NULL;
  1082. }
  1083. }
  1084. void lvgl_res_unload_group(lvgl_res_group_t* group)
  1085. {
  1086. if(group && group->group_data)
  1087. {
  1088. os_strace_string(SYS_TRACE_ID_RES_UNLOAD, "group");
  1089. res_manager_release_resource(group->group_data);
  1090. group->id = 0;
  1091. group->group_data = NULL;
  1092. os_strace_end_call_u32(SYS_TRACE_ID_RES_UNLOAD, 1);
  1093. }
  1094. }
  1095. void lvgl_res_unload_picregion(lvgl_res_picregion_t* picreg)
  1096. {
  1097. if(picreg && picreg->picreg_data)
  1098. {
  1099. os_strace_string(SYS_TRACE_ID_RES_UNLOAD, "picregion");
  1100. res_manager_release_resource(picreg->picreg_data);
  1101. picreg->picreg_data = NULL;
  1102. os_strace_end_call_u32(SYS_TRACE_ID_RES_UNLOAD, 1);
  1103. }
  1104. }
  1105. void lvgl_res_unload_pictures(lv_img_dsc_t* img, uint32_t num)
  1106. {
  1107. uint32_t i;
  1108. os_strace_string(SYS_TRACE_ID_RES_UNLOAD, "pictures");
  1109. for(i=0;i<num;i++)
  1110. {
  1111. if(img[i].data != NULL)
  1112. {
  1113. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  1114. _bitmap_mem_debug_remove_bitmap_from_scene((uint32_t)img[i].data);
  1115. #endif
  1116. res_manager_free_bitmap_data((void *)img[i].data);
  1117. img[i].data = NULL;
  1118. }
  1119. }
  1120. os_strace_end_call_u32(SYS_TRACE_ID_RES_UNLOAD, num);
  1121. }
  1122. void lvgl_res_unload_strings(lvgl_res_string_t* str, uint32_t num)
  1123. {
  1124. uint32_t i;
  1125. os_strace_string(SYS_TRACE_ID_RES_UNLOAD, "strings");
  1126. for(i=0;i<num;i++)
  1127. {
  1128. if(str[i].txt != NULL)
  1129. {
  1130. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  1131. _bitmap_mem_debug_remove_string_from_scene((uint32_t)str[i].txt);
  1132. #endif
  1133. res_manager_free_text_data(str[i].txt);
  1134. str[i].txt = NULL;
  1135. }
  1136. }
  1137. os_strace_end_call_u32(SYS_TRACE_ID_RES_UNLOAD, num);
  1138. }
  1139. int lvgl_res_load_picregion_from_group(lvgl_res_group_t* group, const uint32_t id, lvgl_res_picregion_t* res_picreg)
  1140. {
  1141. resource_picregion_t* picreg;
  1142. if(group == NULL)
  1143. {
  1144. SYS_LOG_ERR("null param %p", group);
  1145. return -1;
  1146. }
  1147. picreg = res_manager_get_group_child(group->res_info, group->group_data, id);
  1148. if(picreg == NULL)
  1149. {
  1150. SYS_LOG_ERR("load pic region faild");
  1151. return -1;
  1152. }
  1153. res_picreg->x = picreg->sty_data->x;
  1154. res_picreg->y = picreg->sty_data->y;
  1155. res_picreg->width = picreg->sty_data->width;
  1156. res_picreg->height = picreg->sty_data->height;
  1157. res_picreg->frames = picreg->sty_data->frames;
  1158. res_picreg->picreg_data = picreg;
  1159. res_picreg->res_info = group->res_info;
  1160. return 0;
  1161. }
  1162. int lvgl_res_load_picregion_from_scene(lvgl_res_scene_t* scene, const uint32_t id, lvgl_res_picregion_t* res_picreg)
  1163. {
  1164. resource_picregion_t* picreg;
  1165. if(scene == NULL)
  1166. {
  1167. SYS_LOG_ERR("null param %p", scene);
  1168. return -1;
  1169. }
  1170. picreg = res_manager_get_scene_child(scene->res_info, scene->scene_data, id);
  1171. if(picreg == NULL)
  1172. {
  1173. SYS_LOG_ERR("load pic region faild");
  1174. return -1;
  1175. }
  1176. res_picreg->x = picreg->sty_data->x;
  1177. res_picreg->y = picreg->sty_data->y;
  1178. res_picreg->width = picreg->sty_data->width;
  1179. res_picreg->height = picreg->sty_data->height;
  1180. res_picreg->frames = (uint32_t)picreg->sty_data->frames;
  1181. res_picreg->picreg_data = picreg;
  1182. res_picreg->res_info = scene->res_info;
  1183. return 0;
  1184. }
  1185. int lvgl_res_load_pictures_from_picregion(lvgl_res_picregion_t* picreg, uint32_t start, uint32_t end, lv_img_dsc_t* img)
  1186. {
  1187. uint32_t i;
  1188. resource_bitmap_t* bitmap;
  1189. if(picreg == NULL || img == NULL ||
  1190. start >= picreg->frames ||
  1191. start > end)
  1192. {
  1193. SYS_LOG_ERR("invalid param %p, %p, %d, %d", picreg, img, start, end);
  1194. return -1;
  1195. }
  1196. os_strace_u32(SYS_TRACE_ID_RES_PICS_LOAD, (uint32_t)picreg->picreg_data);
  1197. for(i=0; i<=end - start; i++)
  1198. {
  1199. img[i].data = NULL;
  1200. }
  1201. if(end >= picreg->frames)
  1202. {
  1203. end = picreg->frames-1;
  1204. }
  1205. for(i=start; i<=end; i++)
  1206. {
  1207. bitmap = res_manager_load_frame_from_picregion(picreg->res_info, picreg->picreg_data, i);
  1208. if (bitmap == NULL)
  1209. {
  1210. lvgl_res_unload_pictures(img, i - start + 1);
  1211. return -1;
  1212. }
  1213. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  1214. _bitmap_mem_debug_add_bitmap_to_scene(0, bitmap->sty_data->id, (uint32_t)bitmap->buffer);
  1215. #endif
  1216. if (_cvt_bmp2dsc(&img[i-start], bitmap))
  1217. {
  1218. lvgl_res_unload_pictures(img, i - start + 1);
  1219. return -1;
  1220. }
  1221. res_manager_free_resource_structure(bitmap);
  1222. }
  1223. os_strace_end_call_u32(SYS_TRACE_ID_RES_PICS_LOAD, (uint32_t)picreg->picreg_data);
  1224. return 0;
  1225. }
  1226. int32_t _check_res_path(uint32_t group_id, uint32_t subgroup_id)
  1227. {
  1228. int32_t ret;
  1229. if(current_group.id != group_id && current_group.id != 0)
  1230. {
  1231. lvgl_res_unload_group(&current_subgrp);
  1232. memset(&current_subgrp, 0, sizeof(lvgl_res_group_t));
  1233. lvgl_res_unload_group(&current_group);
  1234. memset(&current_group, 0, sizeof(lvgl_res_group_t));
  1235. }
  1236. if(current_subgrp.id != subgroup_id && current_subgrp.id != 0)
  1237. {
  1238. lvgl_res_unload_group(&current_subgrp);
  1239. memset(&current_subgrp, 0, sizeof(lvgl_res_group_t));
  1240. }
  1241. if(current_group.id != group_id && group_id != 0 )
  1242. {
  1243. ret = lvgl_res_load_group_from_scene(&current_scene, group_id, &current_group);
  1244. if(ret < 0)
  1245. {
  1246. memset(&current_group, 0, sizeof(lvgl_res_group_t));
  1247. return -1;
  1248. }
  1249. }
  1250. if(current_subgrp.id != subgroup_id && subgroup_id != 0)
  1251. {
  1252. ret = lvgl_res_load_group_from_group(&current_group, subgroup_id, &current_subgrp);
  1253. if(ret < 0)
  1254. {
  1255. memset(&current_subgrp, 0, sizeof(lvgl_res_group_t));
  1256. return -1;
  1257. }
  1258. }
  1259. return 0;
  1260. }
  1261. int lvgl_res_load_pictures(uint32_t group_id, uint32_t subgroup_id, uint32_t* id, lv_img_dsc_t* img, lv_point_t* pt, uint32_t num)
  1262. {
  1263. int32_t ret;
  1264. if(current_scene.id == 0 || current_scene.scene_data == NULL)
  1265. {
  1266. SYS_LOG_ERR("invalid scene 0x%x, %p", current_scene.id, current_scene.scene_data);
  1267. return -1;
  1268. }
  1269. ret = _check_res_path(group_id, subgroup_id);
  1270. if(ret < 0)
  1271. {
  1272. return -1;
  1273. }
  1274. if(current_subgrp.id != 0)
  1275. {
  1276. ret = lvgl_res_load_pictures_from_group(&current_subgrp, id, img, pt, num);
  1277. if(ret < 0)
  1278. {
  1279. return -1;
  1280. }
  1281. }
  1282. else if(current_group.id != 0)
  1283. {
  1284. ret = lvgl_res_load_pictures_from_group(&current_group, id, img, pt, num);
  1285. if(ret < 0)
  1286. {
  1287. return -1;
  1288. }
  1289. }
  1290. else
  1291. {
  1292. ret = lvgl_res_load_pictures_from_scene(&current_scene, id, img, pt, num);
  1293. if(ret < 0)
  1294. {
  1295. return -1;
  1296. }
  1297. }
  1298. return 0;
  1299. }
  1300. int lvgl_res_load_strings(uint32_t group_id, uint32_t subgroup_id, uint32_t* id, lvgl_res_string_t* str, uint32_t num)
  1301. {
  1302. int32_t ret;
  1303. if(current_scene.id == 0 || current_scene.scene_data == NULL)
  1304. {
  1305. SYS_LOG_ERR("invalid scene 0x%x, %p", current_scene.id, current_scene.scene_data);
  1306. return -1;
  1307. }
  1308. ret = _check_res_path(group_id, subgroup_id);
  1309. if(ret < 0)
  1310. {
  1311. return -1;
  1312. }
  1313. if(current_subgrp.id != 0)
  1314. {
  1315. ret = lvgl_res_load_strings_from_group(&current_subgrp, id, str, num);
  1316. if(ret < 0)
  1317. {
  1318. return -1;
  1319. }
  1320. }
  1321. else if(current_group.id != 0)
  1322. {
  1323. ret = lvgl_res_load_strings_from_group(&current_group, id, str, num);
  1324. if(ret < 0)
  1325. {
  1326. return -1;
  1327. }
  1328. }
  1329. else
  1330. {
  1331. ret = lvgl_res_load_strings_from_scene(&current_scene, id, str, num);
  1332. if(ret < 0)
  1333. {
  1334. return -1;
  1335. }
  1336. }
  1337. return 0;
  1338. }
  1339. int lvgl_res_load_picregion(uint32_t group_id, uint32_t subgroup_id, uint32_t picreg_id, lvgl_res_picregion_t* res_picreg)
  1340. {
  1341. int32_t ret;
  1342. if(current_scene.id == 0 || current_scene.scene_data == NULL)
  1343. {
  1344. SYS_LOG_ERR("invalid scene 0x%x, %p", current_scene.id, current_scene.scene_data);
  1345. return -1;
  1346. }
  1347. ret = _check_res_path(group_id, subgroup_id);
  1348. if(ret < 0)
  1349. {
  1350. return -1;
  1351. }
  1352. if(current_subgrp.id != 0)
  1353. {
  1354. ret = lvgl_res_load_picregion_from_group(&current_subgrp, picreg_id, res_picreg);
  1355. if(ret < 0)
  1356. {
  1357. return -1;
  1358. }
  1359. }
  1360. else if(current_group.id != 0)
  1361. {
  1362. ret = lvgl_res_load_picregion_from_group(&current_group, picreg_id, res_picreg);
  1363. if(ret < 0)
  1364. {
  1365. return -1;
  1366. }
  1367. }
  1368. else
  1369. {
  1370. ret = lvgl_res_load_picregion_from_scene(&current_scene, picreg_id, res_picreg);
  1371. if(ret < 0)
  1372. {
  1373. return -1;
  1374. }
  1375. }
  1376. return 0;
  1377. }
  1378. static preload_param_t* _check_item_in_list(preload_param_t* sublist, resource_bitmap_t* bitmap)
  1379. {
  1380. preload_param_t* item;
  1381. if(sublist == NULL)
  1382. {
  1383. SYS_LOG_ERR("shouldnt happen");
  1384. return NULL;
  1385. }
  1386. else
  1387. {
  1388. item = sublist;
  1389. if(item->bitmap && item->bitmap->sty_data->id == bitmap->sty_data->id)
  1390. {
  1391. return NULL;
  1392. }
  1393. while(item->next != NULL)
  1394. {
  1395. if(item->bitmap && item->bitmap->sty_data->id == bitmap->sty_data->id)
  1396. {
  1397. return NULL;
  1398. }
  1399. item = item->next;
  1400. }
  1401. if(item->bitmap && item->bitmap->sty_data->id == bitmap->sty_data->id)
  1402. {
  1403. return NULL;
  1404. }
  1405. //found tail
  1406. return item;
  1407. }
  1408. }
  1409. static void _add_item_to_list(preload_param_t** sublist, preload_param_t* param)
  1410. {
  1411. preload_param_t* item;
  1412. os_strace_u32x4(SYS_TRACE_ID_RES_PRELOAD_ADD, (uint32_t)param->scene_id, (uint32_t)*sublist, (uint32_t)param, (uint32_t)param->next);
  1413. if(*sublist == NULL)
  1414. {
  1415. *sublist = param;
  1416. }
  1417. else
  1418. {
  1419. item = *sublist;
  1420. while(1)
  1421. {
  1422. if(item->next == NULL)
  1423. {
  1424. item->next = param;
  1425. break;
  1426. }
  1427. item = item->next;
  1428. }
  1429. }
  1430. os_strace_end_call_u32(SYS_TRACE_ID_RES_PRELOAD_ADD, (uint32_t)param_list);
  1431. }
  1432. static void _add_item_to_preload_list(preload_param_t* param)
  1433. {
  1434. preload_param_t* item;
  1435. os_mutex_lock(&preload_mutex, OS_FOREVER);
  1436. os_strace_u32x4(SYS_TRACE_ID_RES_PRELOAD_ADD, (uint32_t)param->scene_id, (uint32_t)param_list, (uint32_t)param, (uint32_t)param->next);
  1437. if(param_list == NULL)
  1438. {
  1439. param_list = param;
  1440. os_sem_give(&preload_sem);
  1441. }
  1442. else
  1443. {
  1444. item = param_list;
  1445. while(1)
  1446. {
  1447. if(item->next == NULL)
  1448. {
  1449. item->next = param;
  1450. break;
  1451. }
  1452. item = item->next;
  1453. }
  1454. }
  1455. os_strace_end_call_u32(SYS_TRACE_ID_RES_PRELOAD_ADD, (uint32_t)param_list);
  1456. os_mutex_unlock(&preload_mutex);
  1457. }
  1458. static void _add_item_to_loading_list(preload_param_t* param)
  1459. {
  1460. preload_param_t* item;
  1461. if(sync_param_list == NULL)
  1462. {
  1463. sync_param_list = param;
  1464. }
  1465. else
  1466. {
  1467. item = sync_param_list;
  1468. while(1)
  1469. {
  1470. if(item->next == NULL)
  1471. {
  1472. item->next = param;
  1473. break;
  1474. }
  1475. item = item->next;
  1476. }
  1477. }
  1478. }
  1479. void _clear_preload_list(uint32_t scene_id)
  1480. {
  1481. preload_param_t* item;
  1482. preload_param_t* prev;
  1483. preload_param_t* found;
  1484. os_mutex_lock(&preload_mutex, OS_FOREVER);
  1485. os_strace_u32x2(SYS_TRACE_ID_RES_PRELOAD_CANCEL, (uint32_t)scene_id, (uint32_t)param_list);
  1486. if(param_list == NULL)
  1487. {
  1488. os_strace_end_call_u32(SYS_TRACE_ID_RES_PRELOAD_CANCEL, (uint32_t)param_list);
  1489. os_mutex_unlock(&preload_mutex);
  1490. return;
  1491. }
  1492. item = param_list;
  1493. prev = NULL;
  1494. if(scene_id == 0)
  1495. {
  1496. while(item != NULL)
  1497. {
  1498. param_list = item->next;
  1499. if(item->preload_type == PRELOAD_TYPE_END_CALLBACK)
  1500. {
  1501. if(item->scene_id > 0)
  1502. {
  1503. res_manager_unload_scene(item->scene_id, NULL);
  1504. }
  1505. if (item->callback)
  1506. item->callback(LVGL_RES_PRELOAD_STATUS_CANCELED, item->param);
  1507. }
  1508. else
  1509. {
  1510. res_manager_free_resource_structure(item->bitmap);
  1511. }
  1512. memset(item, 0, sizeof(preload_param_t));
  1513. res_array_free(item);
  1514. item = param_list;
  1515. }
  1516. }
  1517. else
  1518. {
  1519. while(item != NULL)
  1520. {
  1521. if(item->scene_id == scene_id)
  1522. {
  1523. if(item->preload_type == PRELOAD_TYPE_END_CALLBACK)
  1524. {
  1525. if(item->scene_id > 0)
  1526. {
  1527. res_manager_unload_scene(item->scene_id, NULL);
  1528. }
  1529. if (item->callback)
  1530. item->callback(LVGL_RES_PRELOAD_STATUS_CANCELED, item->param);
  1531. }
  1532. else
  1533. {
  1534. res_manager_free_resource_structure(item->bitmap);
  1535. }
  1536. if(param_list == item)
  1537. {
  1538. param_list = item->next;
  1539. }
  1540. if(prev != NULL)
  1541. {
  1542. prev->next = item->next;
  1543. }
  1544. found = item;
  1545. item = item->next;
  1546. memset(found, 0, sizeof(preload_param_t));
  1547. res_array_free(found);
  1548. }
  1549. else
  1550. {
  1551. prev = item;
  1552. item = item->next;
  1553. }
  1554. }
  1555. }
  1556. os_strace_end_call_u32(SYS_TRACE_ID_RES_PRELOAD_CANCEL, (uint32_t)param_list);
  1557. os_mutex_unlock(&preload_mutex);
  1558. }
  1559. void _pause_preload(void)
  1560. {
  1561. preload_running = 2;
  1562. }
  1563. void _resume_preload(void)
  1564. {
  1565. preload_running = 1;
  1566. }
  1567. int lvgl_res_preload_pictures_from_scene(uint32_t scene_id, lvgl_res_scene_t* scene, const uint32_t* id, uint32_t num)
  1568. {
  1569. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  1570. return 0;
  1571. #else
  1572. int32_t i;
  1573. preload_param_t* param;
  1574. resource_bitmap_t* bitmap;
  1575. if(scene == NULL || id == NULL || num == 0)
  1576. {
  1577. SYS_LOG_ERR("invalid param to preload %p, %p, %d", scene, id, num);
  1578. return -1;
  1579. }
  1580. os_strace_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)scene->scene_data);
  1581. for(i=0; i<num; i++)
  1582. {
  1583. bitmap = res_manager_preload_from_scene(scene->res_info, scene->scene_data, id[i]);
  1584. if(bitmap == NULL)
  1585. {
  1586. SYS_LOG_ERR("preload %d bitmap error", id[i]);
  1587. continue;
  1588. }
  1589. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  1590. if(param == NULL)
  1591. {
  1592. SYS_LOG_ERR("no space for param for preload ");
  1593. break;
  1594. }
  1595. if(scene_id > 0)
  1596. {
  1597. param->preload_type = PRELOAD_TYPE_NORMAL_COMPACT;
  1598. param->scene_id = scene_id;
  1599. }
  1600. else
  1601. {
  1602. param->preload_type = PRELOAD_TYPE_NORMAL;
  1603. }
  1604. param->bitmap = bitmap;
  1605. param->next = NULL;
  1606. param->res_info = scene->res_info;
  1607. _add_item_to_preload_list(param);
  1608. }
  1609. os_strace_end_call_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)scene->scene_data);
  1610. return 0;
  1611. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  1612. }
  1613. int lvgl_res_preload_pictures_from_group(uint32_t scene_id, lvgl_res_group_t* group, const uint32_t* id, uint32_t num)
  1614. {
  1615. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  1616. return 0;
  1617. #else
  1618. int32_t i;
  1619. preload_param_t* param;
  1620. resource_bitmap_t* bitmap;
  1621. if(group == NULL || id == NULL || num == 0)
  1622. {
  1623. SYS_LOG_ERR("invalid param to preload %p, %p, %d", group, id, num);
  1624. return -1;
  1625. }
  1626. os_strace_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)group->group_data);
  1627. for(i=0; i<num; i++)
  1628. {
  1629. bitmap = res_manager_preload_from_group(group->res_info, group->group_data, scene_id, id[i]);
  1630. if(bitmap == NULL)
  1631. {
  1632. SYS_LOG_ERR("preload %d bitmap error", id[i]);
  1633. continue;
  1634. }
  1635. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  1636. if(param == NULL)
  1637. {
  1638. SYS_LOG_ERR("no space for param for preload");
  1639. break;
  1640. }
  1641. if(scene_id > 0)
  1642. {
  1643. param->preload_type = PRELOAD_TYPE_NORMAL_COMPACT;
  1644. param->scene_id = scene_id;
  1645. }
  1646. else
  1647. {
  1648. param->preload_type = PRELOAD_TYPE_NORMAL;
  1649. }
  1650. param->bitmap = bitmap;
  1651. param->next = NULL;
  1652. param->res_info = group->res_info;
  1653. _add_item_to_preload_list(param);
  1654. }
  1655. os_strace_end_call_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)group->group_data);
  1656. return 0;
  1657. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  1658. }
  1659. int lvgl_res_preload_pictures_from_picregion(uint32_t scene_id, lvgl_res_picregion_t* picreg, uint32_t start, uint32_t end)
  1660. {
  1661. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  1662. return 0;
  1663. #else
  1664. int32_t i;
  1665. preload_param_t* param;
  1666. resource_bitmap_t* bitmap;
  1667. if(picreg == NULL || start > end)
  1668. {
  1669. SYS_LOG_ERR("invalid param to preload %p, %d, %d", picreg, start, end);
  1670. return -1;
  1671. }
  1672. os_strace_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)picreg->picreg_data);
  1673. for(i=start; i<=end; i++)
  1674. {
  1675. bitmap = res_manager_preload_from_picregion(picreg->res_info, picreg->picreg_data, i);
  1676. if(bitmap == NULL)
  1677. {
  1678. SYS_LOG_ERR("preload %d bitmap error", i);
  1679. continue;
  1680. }
  1681. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  1682. if(param == NULL)
  1683. {
  1684. SYS_LOG_ERR("no space for param for preload");
  1685. break;
  1686. }
  1687. if(scene_id > 0)
  1688. {
  1689. param->preload_type = PRELOAD_TYPE_NORMAL_COMPACT;
  1690. param->scene_id = scene_id;
  1691. }
  1692. else
  1693. {
  1694. param->preload_type = PRELOAD_TYPE_NORMAL;
  1695. }
  1696. param->bitmap = bitmap;
  1697. param->next = NULL;
  1698. param->res_info = picreg->res_info;
  1699. _add_item_to_preload_list(param);
  1700. }
  1701. os_strace_end_call_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)picreg->picreg_data);
  1702. return 0;
  1703. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  1704. }
  1705. #ifndef CONFIG_RES_MANAGER_SKIP_PRELOAD
  1706. static void _res_preload_thread(void *parama1, void *parama2, void *parama3)
  1707. {
  1708. preload_param_t* param_item;
  1709. int32_t ret = 0;
  1710. while(preload_running)
  1711. {
  1712. os_mutex_lock(&preload_mutex, OS_FOREVER);
  1713. if(param_list == NULL)
  1714. {
  1715. os_mutex_unlock(&preload_mutex);
  1716. os_sem_take(&preload_sem, OS_FOREVER);
  1717. os_mutex_lock(&preload_mutex, OS_FOREVER);
  1718. }
  1719. if(param_list == NULL)
  1720. {
  1721. os_mutex_unlock(&preload_mutex);
  1722. continue;
  1723. }
  1724. os_strace_u32x2(SYS_TRACE_ID_RES_SCENE_PRELOAD_0, (uint32_t)param_list, (uint32_t)param_list->next);
  1725. param_item = param_list;
  1726. param_list = param_item->next;
  1727. if(preload_running == 2)
  1728. {
  1729. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_0, (uint32_t)param_list);
  1730. os_mutex_unlock(&preload_mutex);
  1731. continue;
  1732. }
  1733. if(param_item->preload_type == PRELOAD_TYPE_NORMAL)
  1734. {
  1735. ret = res_manager_preload_bitmap(param_item->res_info, param_item->bitmap);
  1736. res_manager_free_resource_structure(param_item->bitmap);
  1737. }
  1738. else if(param_item->preload_type == PRELOAD_TYPE_NORMAL_COMPACT)
  1739. {
  1740. ret = res_manager_preload_bitmap_compact(param_item->scene_id, param_item->res_info, param_item->bitmap);
  1741. res_manager_free_resource_structure(param_item->bitmap);
  1742. }
  1743. else if(param_item->preload_type == PRELOAD_TYPE_BEGIN_CALLBACK)
  1744. {
  1745. if (param_item->callback)
  1746. param_item->callback(LVGL_RES_PRELOAD_STATUS_LOADING, param_item->param);
  1747. }
  1748. else if(param_item->preload_type == PRELOAD_TYPE_END_CALLBACK)
  1749. {
  1750. //user callback ,add at the end of preloaded pics to inform user about preload finish
  1751. //res_manager_preload_finish_check(param_item->scene_id);
  1752. if (param_item->callback)
  1753. param_item->callback(LVGL_RES_PRELOAD_STATUS_FINISHED, param_item->param);
  1754. // _dump_sram_usage();
  1755. }
  1756. else
  1757. {
  1758. //presumably already freed, just continue;
  1759. SYS_LOG_ERR("unknown preload type: %d\n", param_item->preload_type);
  1760. }
  1761. memset(param_item, 0, sizeof(preload_param_t));
  1762. res_array_free(param_item);
  1763. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_0, (uint32_t)param_list);
  1764. os_mutex_unlock(&preload_mutex);
  1765. }
  1766. }
  1767. #endif
  1768. void _dump_preload_list(void)
  1769. {
  1770. preload_param_t* item = param_list;
  1771. while(item)
  1772. {
  1773. printf("preload item scene 0x%x, type %d\n", item->scene_id, item->preload_type);
  1774. item=item->next;
  1775. }
  1776. }
  1777. int lvgl_res_preload_cancel(void)
  1778. {
  1779. #ifndef CONFIG_RES_MANAGER_SKIP_PRELOAD
  1780. _clear_preload_list(0);
  1781. #endif
  1782. return 0;
  1783. }
  1784. int lvgl_res_preload_cancel_scene(uint32_t scene_id)
  1785. {
  1786. #ifndef CONFIG_RES_MANAGER_SKIP_PRELOAD
  1787. _clear_preload_list(scene_id);
  1788. // _dump_preload_list();
  1789. #endif
  1790. return 0;
  1791. }
  1792. int _res_preload_pictures_from_picregion(uint32_t scene_id, lvgl_res_picregion_t* picreg, uint32_t start, uint32_t end, preload_param_t** sublist)
  1793. {
  1794. int32_t i;
  1795. preload_param_t* param;
  1796. preload_param_t* tail;
  1797. resource_bitmap_t* bitmap;
  1798. int preload_count = 0;
  1799. if(picreg == NULL || start > end)
  1800. {
  1801. SYS_LOG_ERR("invalid param to preload %p, %d, %d", picreg, start, end);
  1802. return -1;
  1803. }
  1804. os_strace_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)picreg->picreg_data);
  1805. for(i=start; i<=end; i++)
  1806. {
  1807. bitmap = res_manager_preload_from_picregion(picreg->res_info, picreg->picreg_data, i);
  1808. if(bitmap == NULL)
  1809. {
  1810. SYS_LOG_ERR("preload %d bitmap error", i);
  1811. continue;
  1812. }
  1813. tail = _check_item_in_list(*sublist, bitmap);
  1814. if(tail == NULL)
  1815. {
  1816. //already in sublist, FIXME:only checked sublist for performance
  1817. res_manager_release_resource(bitmap);
  1818. continue;
  1819. }
  1820. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  1821. if(param == NULL)
  1822. {
  1823. SYS_LOG_ERR("no space for param for preload");
  1824. break;
  1825. }
  1826. if(scene_id > 0)
  1827. {
  1828. param->preload_type = PRELOAD_TYPE_NORMAL_COMPACT;
  1829. param->scene_id = scene_id;
  1830. }
  1831. else
  1832. {
  1833. param->preload_type = PRELOAD_TYPE_NORMAL;
  1834. }
  1835. param->bitmap = bitmap;
  1836. param->next = NULL;
  1837. param->res_info = picreg->res_info;
  1838. tail->next = param;
  1839. preload_count++;
  1840. }
  1841. os_strace_end_call_u32(SYS_TRACE_ID_RES_PICS_PRELOAD, (uint32_t)picreg->picreg_data);
  1842. return preload_count;
  1843. }
  1844. void _add_preload_total_size(uint32_t scene_id, uint32_t* ptotal_size, uint32_t size)
  1845. {
  1846. uint32_t total_size;
  1847. if(ptotal_size == NULL)
  1848. {
  1849. return;
  1850. }
  1851. size = ((size+res_mem_align-1)/res_mem_align)*res_mem_align;
  1852. total_size = *ptotal_size;
  1853. if(total_size + size > max_compact_block_size)
  1854. {
  1855. //split block
  1856. SYS_LOG_INF("\n ### scene 0x%x, split total_size %d\n", scene_id, total_size);
  1857. if(total_size > 0)
  1858. {
  1859. res_manager_init_compact_buffer(scene_id, total_size);
  1860. }
  1861. if(size >= max_compact_block_size)
  1862. {
  1863. SYS_LOG_INF("\n ### scene 0x%x, split total_size %d\n", scene_id, size);
  1864. res_manager_init_compact_buffer(scene_id, size);
  1865. total_size = 0;
  1866. }
  1867. else
  1868. {
  1869. total_size = size;
  1870. }
  1871. *ptotal_size = total_size;
  1872. }
  1873. else
  1874. {
  1875. total_size += size;
  1876. *ptotal_size = total_size;
  1877. }
  1878. return;
  1879. }
  1880. int _res_preload_group_compact(resource_info_t* info, uint32_t scene_id, uint32_t pargroup_id, resource_group_t* group, uint32_t* ptotal_size, uint32_t preload, preload_param_t** sublist)
  1881. {
  1882. preload_param_t* param;
  1883. preload_param_t* tail;
  1884. resource_group_t* res_group;
  1885. resource_bitmap_t* bitmap;
  1886. lvgl_res_picregion_t picreg;
  1887. uint32_t count = 0;
  1888. uint32_t offset = 0;
  1889. uint32_t total_size = *ptotal_size;
  1890. uint32_t inc_size = 0;
  1891. uint32_t buf_block_struct_size = res_manager_get_bitmap_buf_block_unit_size();
  1892. int picreg_count = 0;
  1893. if(info == NULL)
  1894. {
  1895. SYS_LOG_ERR("resources not opened\n");
  1896. return -1;
  1897. }
  1898. while(1)
  1899. {
  1900. void* resource = res_manager_preload_next_group_child(info, group, &count, &offset, scene_id, pargroup_id);
  1901. if(resource == NULL)
  1902. {
  1903. if(count >= group->sty_data->resource_sum)
  1904. {
  1905. //reach end of scene
  1906. break;
  1907. }
  1908. //text or invalid param which shouldnt happen here
  1909. continue;
  1910. }
  1911. uint32_t type = *((uint32_t*)(*((uint32_t*)(resource))));
  1912. switch(type)
  1913. {
  1914. case RESOURCE_TYPE_GROUP:
  1915. res_group = (resource_group_t*)resource;
  1916. if(res_group->sty_data->resource_sum > 0)
  1917. {
  1918. if(pargroup_id > 0)
  1919. {
  1920. //regular check only support 2 level depth, dont check regular after that
  1921. _res_preload_group_compact(info, scene_id, 0xffffffff, (resource_group_t*)resource, &total_size, preload, sublist);
  1922. }
  1923. else
  1924. {
  1925. _res_preload_group_compact(info, scene_id, group->sty_data->sty_id, (resource_group_t*)resource, &total_size, preload, sublist);
  1926. }
  1927. }
  1928. res_manager_release_resource(resource);
  1929. break;
  1930. case RESOURCE_TYPE_PICREGION:
  1931. picreg.picreg_data = (resource_picregion_t*)resource;
  1932. picreg.res_info = info;
  1933. if(preload)
  1934. {
  1935. picreg_count = _res_preload_pictures_from_picregion(scene_id, &picreg, 0, picreg.picreg_data->sty_data->frames-1, sublist);
  1936. }
  1937. if(picreg_count > 0 && picreg.picreg_data->regular_info == 0)
  1938. {
  1939. inc_size = (picreg.picreg_data->sty_data->width*picreg.picreg_data->sty_data->height*picreg.picreg_data->sty_data->bytes_per_pixel + buf_block_struct_size);
  1940. inc_size = ((inc_size+res_mem_align-1)/res_mem_align)*res_mem_align;
  1941. inc_size = inc_size*picreg_count;
  1942. _add_preload_total_size(scene_id, &total_size, inc_size);
  1943. //total_size += (picreg.picreg_data->sty_data->width*picreg.picreg_data->sty_data->height*picreg.picreg_data->sty_data->bytes_per_pixel + buf_block_struct_size)*picreg_count;
  1944. }
  1945. res_manager_release_resource(resource);
  1946. break;
  1947. case RESOURCE_TYPE_PICTURE:
  1948. bitmap = (resource_bitmap_t*)resource;
  1949. tail = _check_item_in_list(*sublist, bitmap);
  1950. if(tail == NULL)
  1951. {
  1952. //already in sublist, FIXME:only checked sublist for performance
  1953. res_manager_release_resource(resource);
  1954. continue;
  1955. }
  1956. if(bitmap->regular_info == 0)
  1957. {
  1958. if(bitmap->sty_data->width == screen_width && bitmap->sty_data->height == screen_height && bitmap->sty_data->bytes_per_pixel == 2)
  1959. {
  1960. inc_size = buf_block_struct_size;
  1961. _add_preload_total_size(scene_id, &total_size, inc_size);
  1962. //total_size += buf_block_struct_size;
  1963. }
  1964. else
  1965. {
  1966. inc_size = (bitmap->sty_data->width*bitmap->sty_data->height*bitmap->sty_data->bytes_per_pixel + buf_block_struct_size);
  1967. _add_preload_total_size(scene_id, &total_size, inc_size);
  1968. //total_size += (bitmap->sty_data->width*bitmap->sty_data->height*bitmap->sty_data->bytes_per_pixel + buf_block_struct_size);
  1969. }
  1970. }
  1971. if(preload)
  1972. {
  1973. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  1974. if(param == NULL)
  1975. {
  1976. SYS_LOG_ERR("no space for param for preload");
  1977. break;
  1978. }
  1979. if(scene_id == 0)
  1980. {
  1981. param->preload_type = PRELOAD_TYPE_NORMAL;
  1982. }
  1983. else
  1984. {
  1985. param->preload_type = PRELOAD_TYPE_NORMAL_COMPACT;
  1986. param->scene_id = scene_id;
  1987. }
  1988. param->bitmap = bitmap;
  1989. param->next = NULL;
  1990. param->res_info = info;
  1991. _add_item_to_list(sublist, param);
  1992. }
  1993. break;
  1994. default:
  1995. //ignore text resource
  1996. break;
  1997. }
  1998. }
  1999. *ptotal_size = total_size;
  2000. return 0;
  2001. }
  2002. int _res_preload_scene_compact(uint32_t scene_id, const uint32_t* resource_id, uint32_t resource_num, void (*callback)(int32_t, void *), void* user_data,
  2003. const char* style_path, const char* picture_path, const char* text_path, bool async_preload)
  2004. {
  2005. resource_info_t* info;
  2006. preload_param_t* param;
  2007. preload_param_t* sublist;
  2008. preload_param_t* tail;
  2009. resource_bitmap_t* bitmap;
  2010. resource_scene_t* res_scene;
  2011. resource_group_t* res_group;
  2012. void* resource;
  2013. lvgl_res_picregion_t picreg;
  2014. uint32_t count = 0;
  2015. uint32_t offset = 0;
  2016. uint32_t total_size = 0;
  2017. uint32_t inc_size = 0;
  2018. uint32_t buf_block_struct_size = res_manager_get_bitmap_buf_block_unit_size();
  2019. int picreg_count = 0;
  2020. info = _res_file_open(style_path, picture_path, text_path, 0, 1);
  2021. if(info == NULL)
  2022. {
  2023. SYS_LOG_ERR("resource info error");
  2024. return-1;
  2025. }
  2026. os_strace_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_1, (uint32_t)scene_id);
  2027. res_scene = res_manager_load_scene(info, scene_id);
  2028. if(res_scene == NULL)
  2029. {
  2030. return -1;
  2031. }
  2032. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_1, (uint32_t)scene_id);
  2033. os_strace_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_3, (uint32_t)scene_id);
  2034. sublist = NULL;
  2035. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  2036. if(param == NULL)
  2037. {
  2038. SYS_LOG_ERR("no space for param for preload");
  2039. return -1;
  2040. }
  2041. memset(param, 0, sizeof(preload_param_t));
  2042. param->callback = callback;
  2043. param->param = user_data;
  2044. param->scene_id = scene_id;
  2045. param->preload_type = PRELOAD_TYPE_BEGIN_CALLBACK;
  2046. param->next = NULL;
  2047. param->res_info = NULL;
  2048. _add_item_to_list(&sublist, param);
  2049. //preload group, be sure that scene compact buffer is inited already
  2050. if(resource_id != NULL)
  2051. {
  2052. uint32_t i;
  2053. for(i=0;i<resource_num;i++)
  2054. {
  2055. resource = (void*)res_manager_preload_from_scene(info, res_scene, resource_id[i]);
  2056. if(resource == NULL)
  2057. {
  2058. //somehow not found, just ignore it
  2059. continue;
  2060. }
  2061. uint32_t type = *((uint32_t*)(*((uint32_t*)(resource))));
  2062. switch(type)
  2063. {
  2064. case RESOURCE_TYPE_GROUP:
  2065. res_group = (resource_group_t*)resource;
  2066. if(res_group->sty_data->resource_sum > 0)
  2067. {
  2068. _res_preload_group_compact(info, scene_id, 0, (resource_group_t*)resource, &total_size, 1, &sublist);
  2069. }
  2070. res_manager_release_resource(resource);
  2071. break;
  2072. case RESOURCE_TYPE_PICREGION:
  2073. picreg.picreg_data = (resource_picregion_t*)resource;
  2074. picreg.res_info = info;
  2075. picreg_count = _res_preload_pictures_from_picregion(scene_id, &picreg, 0, picreg.picreg_data->sty_data->frames-1, &sublist);
  2076. if(picreg_count > 0 && picreg.picreg_data->regular_info == 0)
  2077. {
  2078. inc_size = (picreg.picreg_data->sty_data->width*picreg.picreg_data->sty_data->height*picreg.picreg_data->sty_data->bytes_per_pixel + buf_block_struct_size);
  2079. inc_size = ((inc_size+res_mem_align-1)/res_mem_align)*res_mem_align;
  2080. inc_size = inc_size*picreg_count;
  2081. _add_preload_total_size(scene_id, &total_size, inc_size);
  2082. //total_size += (picreg.picreg_data->sty_data->width*picreg.picreg_data->sty_data->height*picreg.picreg_data->sty_data->bytes_per_pixel + buf_block_struct_size)*picreg_count;
  2083. }
  2084. res_manager_release_resource(resource);
  2085. break;
  2086. case RESOURCE_TYPE_PICTURE:
  2087. bitmap = (resource_bitmap_t*)resource;
  2088. tail = _check_item_in_list(sublist, bitmap);
  2089. if(tail == NULL)
  2090. {
  2091. //already in sublist, FIXME:only checked sublist for performance
  2092. res_manager_release_resource(resource);
  2093. continue;
  2094. }
  2095. if(bitmap->regular_info == 0)
  2096. {
  2097. if(bitmap->sty_data->width == screen_width && bitmap->sty_data->height == screen_height && bitmap->sty_data->bytes_per_pixel == 2)
  2098. {
  2099. inc_size = buf_block_struct_size;
  2100. _add_preload_total_size(scene_id, &total_size, inc_size);
  2101. //total_size += buf_block_struct_size;
  2102. }
  2103. else
  2104. {
  2105. inc_size = (bitmap->sty_data->width*bitmap->sty_data->height*bitmap->sty_data->bytes_per_pixel + buf_block_struct_size);
  2106. _add_preload_total_size(scene_id, &total_size, inc_size);
  2107. //total_size += (bitmap->sty_data->width*bitmap->sty_data->height*bitmap->sty_data->bytes_per_pixel + buf_block_struct_size);
  2108. }
  2109. }
  2110. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  2111. if(param == NULL)
  2112. {
  2113. SYS_LOG_ERR("no space for param for preload");
  2114. break;
  2115. }
  2116. param->preload_type = PRELOAD_TYPE_NORMAL_COMPACT;
  2117. param->bitmap = bitmap;
  2118. param->scene_id = scene_id;
  2119. param->next = NULL;
  2120. param->res_info = info;
  2121. tail->next = param;
  2122. break;
  2123. default:
  2124. break;
  2125. }
  2126. }
  2127. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  2128. if(param == NULL)
  2129. {
  2130. SYS_LOG_ERR("no space for param for preload");
  2131. return -1;
  2132. }
  2133. memset(param, 0, sizeof(preload_param_t));
  2134. param->callback = callback;
  2135. param->param = user_data;
  2136. param->scene_id = scene_id;
  2137. param->preload_type = PRELOAD_TYPE_END_CALLBACK;
  2138. param->next = NULL;
  2139. _add_item_to_list(&sublist, param);
  2140. if(total_size > 0)
  2141. {
  2142. res_manager_init_compact_buffer(scene_id, total_size);
  2143. }
  2144. else
  2145. {
  2146. //for scene without pic resources save a node for status check
  2147. res_manager_init_compact_buffer(scene_id, buf_block_struct_size);
  2148. }
  2149. if(async_preload)
  2150. {
  2151. _add_item_to_preload_list(sublist);
  2152. }
  2153. else
  2154. {
  2155. _add_item_to_loading_list(sublist);
  2156. }
  2157. // _dump_sram_usage();
  2158. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_3, (uint32_t)scene_id);
  2159. SYS_LOG_INF("\n #### scene res 0x%x, total_size %d\n", scene_id, total_size);
  2160. return 0;
  2161. }
  2162. //calculate scene bitmap buffer
  2163. // _pause_preload();
  2164. //fill preload list
  2165. while(1)
  2166. {
  2167. void* resource = res_manager_preload_next_scene_child(info, res_scene, &count, &offset);
  2168. if(resource == NULL)
  2169. {
  2170. if(count >= res_scene->resource_sum)
  2171. {
  2172. //reach end of scene
  2173. break;
  2174. }
  2175. //text or invalid param which shouldnt happen here
  2176. continue;
  2177. }
  2178. uint32_t type = *((uint32_t*)(*((uint32_t*)(resource))));
  2179. SYS_LOG_DBG("scene 0x%x, resource type %d, count %d\n",scene_id, type, count);
  2180. switch(type)
  2181. {
  2182. case RESOURCE_TYPE_GROUP:
  2183. res_group = (resource_group_t*)resource;
  2184. if(res_group->sty_data->resource_sum > 0)
  2185. {
  2186. _res_preload_group_compact(info, scene_id, 0, (resource_group_t*)resource, &total_size, 1, &sublist);
  2187. }
  2188. res_manager_release_resource(resource);
  2189. break;
  2190. case RESOURCE_TYPE_PICREGION:
  2191. picreg.picreg_data = (resource_picregion_t*)resource;
  2192. picreg.res_info = info;
  2193. picreg_count = _res_preload_pictures_from_picregion(scene_id, &picreg, 0, picreg.picreg_data->sty_data->frames-1, &sublist);
  2194. if(picreg_count > 0 && picreg.picreg_data->regular_info == 0)
  2195. {
  2196. inc_size = (picreg.picreg_data->sty_data->width*picreg.picreg_data->sty_data->height*picreg.picreg_data->sty_data->bytes_per_pixel + buf_block_struct_size);
  2197. inc_size = ((inc_size+res_mem_align-1)/res_mem_align)*res_mem_align;
  2198. inc_size = inc_size*picreg_count;
  2199. _add_preload_total_size(scene_id, &total_size, inc_size);
  2200. //total_size += (picreg.picreg_data->sty_data->width*picreg.picreg_data->sty_data->height*picreg.picreg_data->sty_data->bytes_per_pixel + buf_block_struct_size)*picreg_count;
  2201. }
  2202. res_manager_release_resource(resource);
  2203. break;
  2204. case RESOURCE_TYPE_PICTURE:
  2205. bitmap = (resource_bitmap_t*)resource;
  2206. tail = _check_item_in_list(sublist, bitmap);
  2207. if(tail == NULL)
  2208. {
  2209. //already in sublist, FIXME:only checked sublist for performance
  2210. res_manager_release_resource(resource);
  2211. continue;
  2212. }
  2213. if(bitmap->regular_info == 0)
  2214. {
  2215. if(bitmap->sty_data->width == screen_width && bitmap->sty_data->height == screen_height && bitmap->sty_data->bytes_per_pixel == 2)
  2216. {
  2217. inc_size = buf_block_struct_size;
  2218. _add_preload_total_size(scene_id, &total_size, inc_size);
  2219. //total_size += buf_block_struct_size;
  2220. }
  2221. else
  2222. {
  2223. inc_size = (bitmap->sty_data->width*bitmap->sty_data->height*bitmap->sty_data->bytes_per_pixel + buf_block_struct_size);
  2224. _add_preload_total_size(scene_id, &total_size, inc_size);
  2225. //total_size += (bitmap->sty_data->width*bitmap->sty_data->height*bitmap->sty_data->bytes_per_pixel + buf_block_struct_size);
  2226. }
  2227. }
  2228. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  2229. if(param == NULL)
  2230. {
  2231. SYS_LOG_ERR("no space for param for preload");
  2232. break;
  2233. }
  2234. param->preload_type = PRELOAD_TYPE_NORMAL_COMPACT;
  2235. param->bitmap = bitmap;
  2236. param->scene_id = scene_id;
  2237. param->next = NULL;
  2238. param->res_info = info;
  2239. tail->next = param;
  2240. break;
  2241. default:
  2242. //ignore text resource
  2243. break;
  2244. }
  2245. }
  2246. param = (preload_param_t*)res_array_alloc(RES_MEM_SIMPLE_PRELOAD, sizeof(preload_param_t));
  2247. if(param == NULL)
  2248. {
  2249. SYS_LOG_ERR("no space for param for preload");
  2250. return -1;
  2251. }
  2252. memset(param, 0, sizeof(preload_param_t));
  2253. param->callback = callback;
  2254. param->param = user_data;
  2255. param->scene_id = scene_id;
  2256. param->preload_type = PRELOAD_TYPE_END_CALLBACK;
  2257. param->next = NULL;
  2258. _add_item_to_list(&sublist, param);
  2259. if(total_size > 0)
  2260. {
  2261. res_manager_init_compact_buffer(scene_id, total_size);
  2262. }
  2263. else
  2264. {
  2265. //for scene without pic resources save a node for status check
  2266. res_manager_init_compact_buffer(scene_id, buf_block_struct_size);
  2267. }
  2268. if(async_preload)
  2269. {
  2270. _add_item_to_preload_list(sublist);
  2271. }
  2272. else
  2273. {
  2274. _add_item_to_loading_list(sublist);
  2275. }
  2276. // _dump_sram_usage();
  2277. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_3, (uint32_t)scene_id);
  2278. SYS_LOG_INF("\n ### scene 0x%x, total_size %d\n", scene_id, total_size);
  2279. return 0;
  2280. }
  2281. int lvgl_res_preload_scene_compact(uint32_t scene_id, const uint32_t* resource_id, uint32_t resource_num, void (*callback)(int32_t, void *), void* user_data,
  2282. const char* style_path, const char* picture_path, const char* text_path)
  2283. {
  2284. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  2285. if(callback == lvgl_res_scene_preload_default_cb_for_view)
  2286. {
  2287. ui_view_update((uint32_t)user_data);
  2288. }
  2289. else
  2290. {
  2291. callback(LVGL_RES_PRELOAD_STATUS_FINISHED, user_data);
  2292. }
  2293. return 0;
  2294. #else
  2295. int ret;
  2296. ret = _res_preload_scene_compact(scene_id, resource_id, resource_num, callback, user_data, style_path, picture_path, text_path, 1);
  2297. if(ret < 0)
  2298. {
  2299. SYS_LOG_ERR("preload failed 0x%x\n", scene_id);
  2300. return -1;
  2301. }
  2302. return 0;
  2303. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  2304. }
  2305. int lvgl_res_preload_scene_compact_serial(uint32_t scene_id, const uint32_t* resource_id, uint32_t resource_num, void (*callback)(int32_t, void *), void* user_data,
  2306. const char* style_path, const char* picture_path, const char* text_path)
  2307. {
  2308. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  2309. if(callback == lvgl_res_scene_preload_default_cb_for_view)
  2310. {
  2311. ui_view_update((uint32_t)user_data);
  2312. }
  2313. else
  2314. {
  2315. callback(LVGL_RES_PRELOAD_STATUS_FINISHED, user_data);
  2316. }
  2317. return 0;
  2318. #else
  2319. int ret;
  2320. preload_param_t* param_item;
  2321. ret = _res_preload_scene_compact(scene_id, resource_id, resource_num, callback, user_data, style_path, picture_path, text_path, 0);
  2322. if(ret < 0)
  2323. {
  2324. SYS_LOG_ERR("preload failed 0x%x\n", scene_id);
  2325. return -1;
  2326. }
  2327. if(sync_param_list == NULL)
  2328. {
  2329. SYS_LOG_ERR("invalid loading list\n");
  2330. return -1;
  2331. }
  2332. while(sync_param_list)
  2333. {
  2334. param_item = sync_param_list;
  2335. sync_param_list = param_item->next;
  2336. if(param_item->preload_type == PRELOAD_TYPE_NORMAL)
  2337. {
  2338. ret = res_manager_preload_bitmap(param_item->res_info, param_item->bitmap);
  2339. res_manager_free_resource_structure(param_item->bitmap);
  2340. }
  2341. else if(param_item->preload_type == PRELOAD_TYPE_NORMAL_COMPACT)
  2342. {
  2343. ret = res_manager_preload_bitmap_compact(param_item->scene_id, param_item->res_info, param_item->bitmap);
  2344. res_manager_free_resource_structure(param_item->bitmap);
  2345. }
  2346. else if(param_item->preload_type == PRELOAD_TYPE_BEGIN_CALLBACK)
  2347. {
  2348. //FIXME
  2349. }
  2350. else if(param_item->preload_type == PRELOAD_TYPE_END_CALLBACK)
  2351. {
  2352. //FIXME
  2353. }
  2354. else
  2355. {
  2356. //presumably already freed
  2357. SYS_LOG_ERR("unknown preload type: %d\n", param_item->preload_type);
  2358. continue;
  2359. }
  2360. memset(param_item, 0, sizeof(preload_param_t));
  2361. res_array_free(param_item);
  2362. }
  2363. return 0;
  2364. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  2365. }
  2366. void lvgl_res_scene_preload_default_cb_for_view(int32_t status, void *view_id)
  2367. {
  2368. SYS_LOG_INF("preload view %u, status %d\n", (uint32_t)view_id, status);
  2369. if (status == LVGL_RES_PRELOAD_STATUS_LOADING) {
  2370. os_strace_u32(SYS_TRACE_ID_VIEW_PRELOAD, (uint32_t)view_id);
  2371. } else {
  2372. os_strace_end_call_u32(SYS_TRACE_ID_VIEW_PRELOAD, (uint32_t)view_id);
  2373. }
  2374. #ifdef CONFIG_UI_MANAGER
  2375. if (status == LVGL_RES_PRELOAD_STATUS_FINISHED) {
  2376. ui_view_update((uint32_t)view_id);
  2377. }
  2378. #endif
  2379. }
  2380. int lvgl_res_preload_scene_compact_default_init(uint32_t scene_id, const uint32_t* resource_id, uint32_t resource_num, void (*callback)(int32_t, void *),
  2381. const char* style_path, const char* picture_path, const char* text_path)
  2382. {
  2383. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  2384. return 0;
  2385. #else
  2386. preload_default_t* record;
  2387. preload_default_t* item;
  2388. int32_t i;
  2389. uint32_t path_total_len;
  2390. uint32_t pic_path_start;
  2391. uint32_t str_path_start;
  2392. if(style_path == NULL)
  2393. {
  2394. SYS_LOG_ERR("invalid init param, need style path\n");
  2395. return -1;
  2396. }
  2397. path_total_len = strlen(style_path)+1;
  2398. path_total_len = ((path_total_len+3)/4)*4;
  2399. pic_path_start = path_total_len;
  2400. if(picture_path)
  2401. {
  2402. path_total_len += strlen(picture_path)+1;
  2403. path_total_len = ((path_total_len+3)/4)*4;
  2404. }
  2405. str_path_start = path_total_len;
  2406. if(text_path)
  2407. {
  2408. path_total_len += strlen(text_path)+1;
  2409. path_total_len = ((path_total_len+3)/4)*4;
  2410. }
  2411. record = preload_default_list;
  2412. while(record != NULL)
  2413. {
  2414. if(record->scene_id == scene_id)
  2415. {
  2416. return 0;
  2417. }
  2418. record = record->next;
  2419. }
  2420. item = res_mem_alloc(RES_MEM_POOL_BMP ,sizeof(preload_default_t)+path_total_len+resource_num*sizeof(uint32_t));
  2421. if(item == NULL)
  2422. {
  2423. SYS_LOG_ERR("alloc preload defautl data failed 0x%x\n", scene_id);
  2424. res_manager_dump_info();
  2425. return -1;
  2426. }
  2427. item->scene_id = scene_id;
  2428. if(resource_id)
  2429. {
  2430. item->resource_id = (uint32_t*)((uint32_t)item+sizeof(preload_default_t)+path_total_len);
  2431. }
  2432. else
  2433. {
  2434. item->resource_id = NULL;
  2435. }
  2436. item->resource_num = resource_num;
  2437. item->callback = callback;
  2438. item->style_path = (char*)((uint32_t)item+sizeof(preload_default_t));
  2439. memset(item->style_path, 0, strlen(style_path)+1);
  2440. strcpy(item->style_path, style_path);
  2441. if(picture_path)
  2442. {
  2443. item->picture_path = item->style_path + pic_path_start;
  2444. memset(item->picture_path, 0, strlen(picture_path)+1);
  2445. strcpy(item->picture_path, picture_path);
  2446. }
  2447. else
  2448. {
  2449. item->picture_path = NULL;
  2450. }
  2451. if(text_path)
  2452. {
  2453. item->text_path = item->style_path + str_path_start;
  2454. memset(item->text_path, 0, strlen(text_path)+1);
  2455. strcpy(item->text_path, text_path);
  2456. }
  2457. else
  2458. {
  2459. item->text_path = NULL;
  2460. }
  2461. item->next = NULL;
  2462. if(resource_id)
  2463. {
  2464. for(i=0;i<resource_num;i++)
  2465. {
  2466. item->resource_id[i] = resource_id[i];
  2467. }
  2468. }
  2469. if(preload_default_list == NULL)
  2470. {
  2471. preload_default_list = item;
  2472. return 0;
  2473. }
  2474. record = preload_default_list;
  2475. while(record != NULL)
  2476. {
  2477. if(record->next == NULL)
  2478. {
  2479. record->next = item;
  2480. return 0;
  2481. }
  2482. else
  2483. {
  2484. record = record->next;
  2485. }
  2486. }
  2487. return 0;
  2488. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  2489. }
  2490. int lvgl_res_scene_is_loaded(uint32_t scene_id)
  2491. {
  2492. return res_manager_scene_is_loaded(scene_id);
  2493. }
  2494. int lvgl_res_preload_scene_compact_default_with_path(uint32_t scene_id, uint32_t view_id, bool reload_update, bool load_serial,
  2495. const char* style_path, const char* picture_path, const char* text_path)
  2496. {
  2497. int ret = 0;
  2498. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  2499. ui_view_update((uint32_t)view_id);
  2500. #else
  2501. preload_default_t* record = preload_default_list;
  2502. while(record != NULL)
  2503. {
  2504. if(record->scene_id == scene_id)
  2505. {
  2506. if(reload_update)
  2507. {
  2508. #ifdef CONFIG_UI_MANAGER
  2509. view_set_refresh_en(view_id, 0);
  2510. #endif
  2511. if(record->callback == NULL)
  2512. {
  2513. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2514. lvgl_res_scene_preload_default_cb_for_view, (void*)view_id,
  2515. style_path, picture_path, text_path);
  2516. }
  2517. else
  2518. {
  2519. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2520. record->callback, (void*)view_id,
  2521. style_path, picture_path, text_path);
  2522. }
  2523. }
  2524. else if(!load_serial)
  2525. {
  2526. if(record->callback == NULL)
  2527. {
  2528. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2529. lvgl_res_scene_preload_default_cb_for_view, (void*)view_id,
  2530. style_path, picture_path, text_path);
  2531. }
  2532. else
  2533. {
  2534. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2535. record->callback, (void*)view_id,
  2536. style_path, picture_path, text_path);
  2537. }
  2538. }
  2539. else
  2540. {
  2541. if(record->callback == NULL)
  2542. {
  2543. ret = lvgl_res_preload_scene_compact_serial(scene_id, record->resource_id, record->resource_num,
  2544. lvgl_res_scene_preload_default_cb_for_view, (void*)view_id,
  2545. style_path, picture_path, text_path);
  2546. }
  2547. else
  2548. {
  2549. ret = lvgl_res_preload_scene_compact_serial(scene_id, record->resource_id, record->resource_num,
  2550. record->callback, (void*)view_id,
  2551. style_path, picture_path, text_path);
  2552. }
  2553. }
  2554. }
  2555. record = record->next;
  2556. }
  2557. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  2558. return ret;
  2559. }
  2560. int lvgl_res_preload_scene_compact_default(uint32_t scene_id, uint32_t view_id, bool reload_update, bool load_serial)
  2561. {
  2562. int ret = 0;
  2563. #ifdef CONFIG_RES_MANAGER_SKIP_PRELOAD
  2564. ui_view_update((uint32_t)view_id);
  2565. #else
  2566. preload_default_t* record = preload_default_list;
  2567. while(record != NULL)
  2568. {
  2569. if(record->scene_id == scene_id)
  2570. {
  2571. if(reload_update)
  2572. {
  2573. #ifdef CONFIG_UI_MANAGER
  2574. view_set_refresh_en(view_id, 0);
  2575. #endif
  2576. if(record->callback == NULL)
  2577. {
  2578. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2579. lvgl_res_scene_preload_default_cb_for_view, (void*)view_id,
  2580. record->style_path, record->picture_path, record->text_path);
  2581. }
  2582. else
  2583. {
  2584. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2585. record->callback, (void*)view_id,
  2586. record->style_path, record->picture_path, record->text_path);
  2587. }
  2588. }
  2589. else if(!load_serial)
  2590. {
  2591. if(record->callback == NULL)
  2592. {
  2593. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2594. lvgl_res_scene_preload_default_cb_for_view, (void*)view_id,
  2595. record->style_path, record->picture_path, record->text_path);
  2596. }
  2597. else
  2598. {
  2599. ret = lvgl_res_preload_scene_compact(scene_id, record->resource_id, record->resource_num,
  2600. record->callback, (void*)view_id,
  2601. record->style_path, record->picture_path, record->text_path);
  2602. }
  2603. }
  2604. else
  2605. {
  2606. if(record->callback == NULL)
  2607. {
  2608. ret = lvgl_res_preload_scene_compact_serial(scene_id, record->resource_id, record->resource_num,
  2609. lvgl_res_scene_preload_default_cb_for_view, (void*)view_id,
  2610. record->style_path, record->picture_path, record->text_path);
  2611. }
  2612. else
  2613. {
  2614. ret = lvgl_res_preload_scene_compact_serial(scene_id, record->resource_id, record->resource_num,
  2615. record->callback, (void*)view_id,
  2616. record->style_path, record->picture_path, record->text_path);
  2617. }
  2618. }
  2619. }
  2620. record = record->next;
  2621. }
  2622. #endif //CONFIG_RES_MANAGER_SKIP_PRELOAD
  2623. return ret;
  2624. }
  2625. int lvgl_res_set_current_string_res(const uint8_t** string_res, uint32_t string_cnt)
  2626. {
  2627. return res_manager_set_current_string_res(string_res, string_cnt);
  2628. }
  2629. uint8_t* lvgl_res_get_string(uint8_t* key)
  2630. {
  2631. return res_manager_get_string(key);
  2632. }
  2633. uint8_t* lvgl_res_get_string_from_id(int str_id)
  2634. {
  2635. return res_manager_get_string_from_id(str_id);
  2636. }
  2637. void lvgl_res_clear_decoder_cache(void)
  2638. {
  2639. res_manager_clear_decoder_cache();
  2640. }