res_manager_api.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <os_common_api.h>
  5. #include <ui_mem.h>
  6. #include <memory/mem_cache.h>
  7. #include "res_manager_api.h"
  8. #include "res_mempool.h"
  9. #ifdef CONFIG_JPEG_HAL
  10. #include <jpeg_hal.h>
  11. #endif
  12. #ifndef CONFIG_SIMULATOR
  13. #include "lz4.h"
  14. #include <sdfs.h>
  15. #else
  16. #include <fs/fs.h>
  17. #define FS_O_READ 0x01
  18. #define FS_SEEK_SET SEEK_SET
  19. #define FS_SEEK_END SEEK_END
  20. #endif
  21. #ifdef CONFIG_SIMULATOR
  22. #define MAX_PARTITION_ID 0
  23. #else
  24. #define MAX_PARTITION_ID 1
  25. #endif
  26. #define RES_MEM_DEBUG 0
  27. #define SCENE_ITEM_SIZE 3
  28. #define MAX_REGULAR_NUM 512
  29. #define STY_PATH_MAX_LEN 24
  30. #ifndef CONFIG_RES_COMPACT_BUFFER_BLOCK_SIZE
  31. #define COMPACT_BUFFER_BLOCK_SIZE 360*360*2+64
  32. #else
  33. #define COMPACT_BUFFER_BLOCK_SIZE CONFIG_RES_COMPACT_BUFFER_BLOCK_SIZE
  34. #endif
  35. #define COMPACT_BUFFER_MAX_PAD_SIZE 4*1024
  36. #define COMPACT_BUFFER_MARGIN_SIZE 64
  37. #define PACK __attribute__ ((packed))
  38. //以下宏定义资源图片的类型
  39. typedef enum{
  40. RES_TYPE_INVALID = 0,
  41. RES_TYPE_PIC = 1, // 16 bit picture (RGB_565, R[15:11], G[10:5], B[4:0])
  42. RES_TYPE_STRING = 2, // string
  43. RES_TYPE_MSTRING, // multi-Language string
  44. RES_TYPE_LOGO = 4, // BIN format picture
  45. RES_TYPE_PNG, // 24 bit picture with alpha (ARGB_8565, A[23:16], R[15:11], G[10:5], B[4:0])
  46. RES_TYPE_PIC_4,
  47. RES_TYPE_PIC_COMPRESSED = 7, // compressed 16 bit picture (compressed RGB_565)
  48. RES_TYPE_PNG_COMPRESSED, // compressed 24 bit compressed picture with alpha (compressed ARGB_8565)
  49. RES_TYPE_LOGO_COMPRESSED, // compressed BIN format picture
  50. RES_TYPE_PIC_RGB888 = 10, // 24 bit picture without alpha (RGB_888, R[23:16], G[16:8], B[7:0])
  51. RES_TYPE_PIC_ARGB8888, // 32 bit picture with alpha (ARGB_8888, , A[31:24], R[23:16], G[16:8], B[7:0])
  52. RES_TYPE_PIC_RGB888_COMPRESSED, // compressed 24 bit picture without alpha (RGB_888)
  53. RES_TYPE_PIC_ARGB8888_COMPRESSED = 13, // compressed 32 bit picture with alpha (ARGB_8888)
  54. RES_TYPE_PIC_LZ4 = 14, // 16 bit picture (RGB_565, R[15:11], G[10:5], B[4:0]) with LZ4 compressed
  55. RES_TYPE_PIC_ARGB8888_LZ4, // 32 bit picture with alpha (ARGB_8888, , A[31:24], R[23:16], G[16:8], B[7:0]) with LZ4 compressed
  56. RES_TYPE_A8 = 30, // Raw format of A8
  57. RES_TYPE_A8_LZ4, // LZ4 compressed format of A8
  58. RES_TYPE_ARGB6666, // Raw format of ARGB6666
  59. RES_TYPE_ARGB6666_LZ4, // LZ4 compressed of ARGB6666
  60. RES_TYPE_PIC_ARGB1555, // Raw format of ARGB1555
  61. RES_TYPE_JPEG, // jpeg format
  62. RES_TYPE_TILE_COMPRESSED, // tile compressed format
  63. RES_TYPE_ETC2_LZ4 = 42, // LZ4 compressed format of ETC2
  64. }res_type_e;
  65. //style header
  66. typedef struct
  67. {
  68. uint32_t scene_sum;
  69. uint32_t reserve;
  70. }style_head_t;
  71. //资源文件数据结构
  72. typedef struct
  73. {
  74. uint8_t magic[4]; //'R', 'E', 'S', 0x19
  75. uint16_t counts;
  76. uint16_t w_format;
  77. uint8_t brgb;
  78. uint8_t version;
  79. uint16_t id_start;
  80. uint8_t reserved[3];
  81. uint8_t ch_extend;
  82. }res_head_t;
  83. typedef struct
  84. {
  85. uint32_t offset;
  86. uint16_t length;
  87. uint8_t type;
  88. uint8_t name[8];
  89. uint8_t len_ext;
  90. }res_entry_t;
  91. typedef struct _buf_block_s
  92. {
  93. uint32_t source;
  94. uint32_t id;
  95. uint32_t size;
  96. uint32_t ref;
  97. uint32_t width;
  98. uint32_t height;
  99. uint32_t bytes_per_pixel;
  100. uint32_t format;
  101. uint8_t* addr;
  102. uint32_t regular_info;
  103. uint8_t align_reserved[20];
  104. struct _buf_block_s* next;
  105. }buf_block_t;
  106. typedef struct
  107. {
  108. compact_buffer_t* compact_buffer_list;
  109. buf_block_t* head;
  110. }resource_buffer_t;
  111. typedef struct _regular_info_s
  112. {
  113. uint32_t scene_id;
  114. uint32_t magic;
  115. uint32_t num;
  116. uint32_t* id;
  117. struct _regular_info_s* next;
  118. }regular_info_t;
  119. static resource_buffer_t bitmap_buffer;
  120. static resource_buffer_t text_buffer;
  121. static resource_buffer_t scene_buffer;
  122. static regular_info_t* regular_info_list;
  123. static int screen_bitmap_size;
  124. static int ui_mem_total = 0;
  125. static size_t res_mem_align = 4;
  126. os_mutex bitmap_cache_mutex;
  127. os_mutex bitmap_read_mutex;
  128. //#define MAX_RES_VERSIONS 1
  129. //static const char* part_path[6] = {"/NAND:A/","/NAND:B","/NAND:C/","/NAND:D", "/NAND:E/","/NAND:F"};
  130. //static char latest_partition;
  131. static const char storage_part_name[10] = {'A','B','C','D','E','F','G','H','I','J'};
  132. #ifdef CONFIG_RES_MANAGER_IMG_DECODER
  133. static const uint8_t** current_string_res;
  134. static uint32_t current_string_res_cnt;
  135. extern res_string_item_t RES_STRING_ID_DIC[];
  136. #endif
  137. extern int sd_fmap(const char *filename, void** addr, int *len);
  138. static int _search_res_id_in_files(resource_info_t* info, resource_bitmap_t* bitmap, uint32_t* bmp_pos, uint32_t* compress_size);
  139. void _resource_buffer_deinit(uint32_t force_clear)
  140. {
  141. buf_block_t* item;
  142. buf_block_t* listp;
  143. if(force_clear)
  144. {
  145. listp = text_buffer.head;
  146. while(listp != NULL)
  147. {
  148. item = listp;
  149. listp = listp->next;
  150. res_mem_free(RES_MEM_POOL_TXT, item);
  151. }
  152. memset(&text_buffer, 0, sizeof(text_buffer));
  153. listp = scene_buffer.head;
  154. while(listp != NULL)
  155. {
  156. item = listp;
  157. listp = listp->next;
  158. res_mem_free(RES_MEM_POOL_SCENE, item);
  159. }
  160. memset(&scene_buffer, 0, sizeof(scene_buffer));
  161. listp = bitmap_buffer.head;
  162. while(listp != NULL)
  163. {
  164. item = listp;
  165. listp = listp->next;
  166. if(item->size > sizeof(buf_block_t))
  167. {
  168. res_mem_free(RES_MEM_POOL_BMP, item);
  169. }
  170. else
  171. {
  172. SYS_LOG_DBG("%d ui mem free %d\n", __LINE__, item->id);
  173. res_mem_free_block(item->addr);
  174. ui_mem_total--;
  175. res_mem_free(RES_MEM_POOL_BMP, item);
  176. }
  177. }
  178. memset(&bitmap_buffer, 0, sizeof(bitmap_buffer));
  179. }
  180. }
  181. void _resource_buffer_init(void)
  182. {
  183. memset(&bitmap_buffer, 0, sizeof(bitmap_buffer));
  184. memset(&text_buffer, 0, sizeof(text_buffer));
  185. memset(&scene_buffer, 0, sizeof(scene_buffer));
  186. regular_info_list = NULL;
  187. }
  188. compact_buffer_t* _init_compact_buffer(uint32_t scene_id, uint32_t request_size)
  189. {
  190. compact_buffer_t* buffer;
  191. compact_buffer_t* tail;
  192. buf_block_t* item;
  193. buffer = (compact_buffer_t*)res_array_alloc(RES_MEM_SIMPLE_COMPACT, sizeof(compact_buffer_t));
  194. if(buffer == NULL)
  195. {
  196. SYS_LOG_INF("malloc compact buffer struct failed\n");
  197. return NULL;
  198. }
  199. #ifdef CONFIG_RES_MANAGER_ALIGN
  200. buffer->addr = (uint8_t*)res_mem_aligned_alloc(RES_MEM_POOL_BMP, res_mem_align, request_size);
  201. #else
  202. buffer->addr = (uint8_t*)res_mem_alloc(RES_MEM_POOL_BMP, request_size);
  203. #endif
  204. if(buffer->addr == NULL)
  205. {
  206. /*
  207. SYS_LOG_INF("no enough sapce for scene 0x%x compact buffer, size %d\n", scene_id, request_size);
  208. res_array_free(buffer);
  209. res_manager_dump_info();
  210. return NULL;
  211. */
  212. buffer->addr = res_mem_alloc_block(screen_bitmap_size, __func__);
  213. if(buffer->addr == NULL)
  214. {
  215. SYS_LOG_INF("no extra sapce for scene 0x%x\n", scene_id);
  216. res_array_free(buffer);
  217. res_manager_dump_info();
  218. return NULL;
  219. }
  220. ui_mem_total++;
  221. buffer->free_size = screen_bitmap_size;
  222. }
  223. else
  224. {
  225. buffer->free_size = request_size;
  226. }
  227. //init first item block to prevent false check
  228. item = (buf_block_t*)buffer->addr;
  229. item->regular_info = 0;
  230. buffer->scene_id = scene_id;
  231. buffer->offset = 0;
  232. #if RES_MEM_DEBUG
  233. {
  234. compact_buffer_t* check = bitmap_buffer.compact_buffer_list;
  235. uint32_t check_count = 0;
  236. while(check != NULL)
  237. {
  238. if(check == buffer)
  239. {
  240. SYS_LOG_INF("invalid compact buffer list for buffer 0x%x:\n", buffer);
  241. break;
  242. }
  243. check = check->next;
  244. check_count++;
  245. if(check_count > 500)
  246. {
  247. SYS_LOG_INF("loophole compact buffer list for buffer 0x%x:\n", buffer);
  248. break;
  249. }
  250. }
  251. if(check != NULL)
  252. {
  253. check = bitmap_buffer.compact_buffer_list;
  254. check_count = 0;
  255. while(check != NULL)
  256. {
  257. SYS_LOG_INF("compact_buffer: 0x%x, addr 0x%x\n", check, check->addr);
  258. check = check->next;
  259. check_count++;
  260. if(check_count > 500)
  261. {
  262. break;
  263. }
  264. }
  265. res_mem_free(RES_MEM_POOL_BMP, buffer->addr);
  266. res_array_free(buffer);
  267. return NULL;
  268. }
  269. }
  270. #endif
  271. // buffer->next = bitmap_buffer.compact_buffer_list;
  272. // bitmap_buffer.compact_buffer_list = buffer;
  273. buffer->next = NULL;
  274. tail = bitmap_buffer.compact_buffer_list;
  275. if(tail == NULL)
  276. {
  277. bitmap_buffer.compact_buffer_list = buffer;
  278. }
  279. else
  280. {
  281. while(tail->next != NULL)
  282. {
  283. tail = tail->next;
  284. }
  285. tail->next = buffer;
  286. }
  287. // SYS_LOG_INF("\n testalloc compact buffer inited for 0x%x, size %d, buffer %p, addr %p\n", scene_id, request_size, buffer, buffer->addr);
  288. #if RES_MEM_DEBUG
  289. if(res_mem_check()<0)
  290. {
  291. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  292. res_mem_dump();
  293. }
  294. #endif
  295. return buffer;
  296. }
  297. uint8_t* _get_resource_scene_buffer(uint32_t source, uint32_t id, uint32_t size)
  298. {
  299. buf_block_t* item = NULL;
  300. item = scene_buffer.head;
  301. while(item != NULL)
  302. {
  303. if((item->source == source)&&(item->id == id))
  304. {
  305. break;
  306. }
  307. item = item->next;
  308. }
  309. if(item != NULL)
  310. {
  311. //found loaded scene, which is not supposed to happen but fine
  312. item->ref ++;
  313. return item->addr;
  314. }
  315. else
  316. {
  317. item = (buf_block_t*)res_mem_alloc(RES_MEM_POOL_SCENE, sizeof(buf_block_t) + size);
  318. if(item == NULL)
  319. {
  320. SYS_LOG_INF("error: scene buffer not enough for size %d \n", size);
  321. res_manager_dump_info();
  322. return NULL;
  323. }
  324. SYS_LOG_INF("item %p", item);
  325. item->source = source;
  326. item->id = id;
  327. item->size = sizeof(buf_block_t) + size;
  328. item->ref = 1;
  329. item->addr = (uint8_t*)item+sizeof(buf_block_t);
  330. item->next = scene_buffer.head;
  331. scene_buffer.head = item;
  332. #if RES_MEM_DEBUG
  333. if(res_mem_check()<0)
  334. {
  335. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  336. res_mem_dump();
  337. }
  338. #endif
  339. return item->addr;
  340. }
  341. }
  342. uint8_t* _try_resource_scene_buffer(uint32_t source, uint32_t id)
  343. {
  344. buf_block_t* item;
  345. item = scene_buffer.head;
  346. while(item != NULL)
  347. {
  348. if((item->source == source)&&(item->id == id))
  349. {
  350. break;
  351. }
  352. item = item->next;
  353. }
  354. if(item != NULL)
  355. {
  356. //found loaded text
  357. item->ref ++;
  358. return item->addr;
  359. }
  360. else
  361. {
  362. //not found, need to load from file
  363. return NULL;
  364. }
  365. }
  366. void _free_resource_scene_buffer(void* p)
  367. {
  368. buf_block_t* item = scene_buffer.head;
  369. buf_block_t* prev = NULL;
  370. if(p == NULL)
  371. {
  372. return;
  373. }
  374. while(item != NULL)
  375. {
  376. if(item->addr == p)
  377. {
  378. break;
  379. }
  380. prev = item;
  381. item = item->next;
  382. }
  383. if(item == NULL)
  384. {
  385. SYS_LOG_INF("scene not found \n");
  386. return;
  387. }
  388. else if((item != NULL)&&(item->ref > 1))
  389. {
  390. item->ref --;
  391. //FIXME
  392. }
  393. else
  394. {
  395. if(prev == NULL)
  396. {
  397. scene_buffer.head = item->next;
  398. }
  399. else
  400. {
  401. prev->next = item->next;
  402. }
  403. res_mem_free(RES_MEM_POOL_SCENE, (void*)item);
  404. #if RES_MEM_DEBUG
  405. if(res_mem_check()<0)
  406. {
  407. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  408. res_mem_dump();
  409. }
  410. #endif
  411. }
  412. }
  413. uint8_t* _get_resource_text_buffer(uint32_t source, uint32_t id, uint32_t size)
  414. {
  415. buf_block_t* item;
  416. item = text_buffer.head;
  417. while(item != NULL)
  418. {
  419. if((item->source == source)&&(item->id == id))
  420. {
  421. break;
  422. }
  423. item = item->next;
  424. }
  425. if(item != NULL)
  426. {
  427. //found loaded text, which is not supposed to happen but fine
  428. item->ref ++;
  429. return item->addr;
  430. }
  431. else
  432. {
  433. item = (buf_block_t*)res_mem_alloc(RES_MEM_POOL_TXT, sizeof(buf_block_t) + size);
  434. if(item == NULL)
  435. {
  436. SYS_LOG_INF("error: resource buffer not enough for size 0x%x \n", size);
  437. res_manager_dump_info();
  438. return NULL;
  439. }
  440. item->source = source;
  441. item->id = id;
  442. item->size = sizeof(buf_block_t) + size;
  443. item->ref = 1;
  444. item->addr = (uint8_t*)item+sizeof(buf_block_t);
  445. item->next = text_buffer.head;
  446. text_buffer.head = item;
  447. return item->addr;
  448. }
  449. }
  450. uint8_t* _try_resource_text_buffer(uint32_t source, uint32_t id)
  451. {
  452. buf_block_t* item;
  453. item = text_buffer.head;
  454. while(item != NULL)
  455. {
  456. if((item->source == source)&&(item->id == id))
  457. {
  458. break;
  459. }
  460. item = item->next;
  461. }
  462. if(item != NULL)
  463. {
  464. //found loaded text
  465. item->ref ++;
  466. return item->addr;
  467. }
  468. else
  469. {
  470. //not found, need to load from file
  471. return NULL;
  472. }
  473. }
  474. void _free_resource_text_buffer(void* p)
  475. {
  476. buf_block_t* item = text_buffer.head;
  477. buf_block_t* prev = NULL;
  478. while(item != NULL)
  479. {
  480. if(item->addr == p)
  481. {
  482. break;
  483. }
  484. prev = item;
  485. item = item->next;
  486. }
  487. if(item == NULL)
  488. {
  489. SYS_LOG_INF("resource text %p not found \n", p);
  490. return;
  491. }
  492. else if((item != NULL)&&(item->ref > 1))
  493. {
  494. item->ref --;
  495. //FIXME
  496. return;
  497. }
  498. else
  499. {
  500. if(prev == NULL)
  501. {
  502. text_buffer.head = item->next;
  503. }
  504. else
  505. {
  506. prev->next = item->next;
  507. }
  508. res_mem_free(RES_MEM_POOL_TXT, (void*)item);
  509. }
  510. }
  511. uint8_t* _get_resource_bitmap_buffer_compact(uint32_t source, resource_bitmap_t* bitmap, uint32_t size, uint32_t scene_id)
  512. {
  513. compact_buffer_t* buffer;
  514. buf_block_t* item = NULL;
  515. uint8_t* ui_mem_buf = NULL;
  516. buffer = bitmap_buffer.compact_buffer_list;
  517. if(size == screen_bitmap_size)
  518. {
  519. ui_mem_buf = res_mem_alloc_block(size, __func__);
  520. if(ui_mem_buf == NULL)
  521. {
  522. SYS_LOG_INF("cant malloc screen size bitmap buffer from ui mem \n");
  523. res_manager_dump_info();
  524. return NULL;
  525. }
  526. SYS_LOG_DBG("1 ui mem alloc %d, addr %p, scene 0x%x\n", bitmap->sty_data->id, ui_mem_buf, scene_id);
  527. ui_mem_total++;
  528. }
  529. if(bitmap->regular_info != 0)
  530. {
  531. if(size == screen_bitmap_size)
  532. {
  533. buffer = _init_compact_buffer(scene_id, sizeof(buf_block_t)+COMPACT_BUFFER_MARGIN_SIZE);
  534. if(buffer == NULL)
  535. {
  536. SYS_LOG_DBG("%d ui mem free %d\n", __LINE__, bitmap->sty_data->id);
  537. res_mem_free_block(ui_mem_buf);
  538. ui_mem_total--;
  539. return NULL;
  540. }
  541. }
  542. else
  543. {
  544. buffer = _init_compact_buffer(scene_id, size+sizeof(buf_block_t)+COMPACT_BUFFER_MARGIN_SIZE);
  545. if(buffer == NULL)
  546. {
  547. return NULL;
  548. }
  549. }
  550. item = (buf_block_t*)(buffer->addr + buffer->offset);
  551. }
  552. else
  553. {
  554. while(buffer != NULL)
  555. {
  556. // SYS_LOG_INF("buffer->scene_id 0x%x\n", buffer->scene_id);
  557. if(buffer->scene_id == scene_id)
  558. {
  559. //check current buffer free size
  560. if(size != screen_bitmap_size && buffer->free_size >= size+sizeof(buf_block_t))
  561. {
  562. break;
  563. }
  564. if(size == screen_bitmap_size && buffer->free_size >= sizeof(buf_block_t))
  565. {
  566. break;
  567. }
  568. }
  569. buffer = buffer->next;
  570. }
  571. if(buffer == NULL)
  572. {
  573. //malloc a new compact buffer for scene id
  574. buffer = _init_compact_buffer(scene_id, size+sizeof(buf_block_t)+COMPACT_BUFFER_MARGIN_SIZE);
  575. if(buffer == NULL)
  576. {
  577. return NULL;
  578. }
  579. }
  580. item = (buf_block_t*)(buffer->addr+buffer->offset);
  581. }
  582. item->source = source;
  583. item->id = bitmap->sty_data->id;
  584. item->ref = 0;
  585. item->width = (uint32_t)bitmap->sty_data->width;
  586. item->height = (uint32_t)bitmap->sty_data->height;
  587. item->bytes_per_pixel = (uint32_t)bitmap->sty_data->bytes_per_pixel;
  588. item->format = (uint32_t)bitmap->sty_data->format;
  589. if(size == screen_bitmap_size)
  590. {
  591. item->size = sizeof(buf_block_t);
  592. item->addr = ui_mem_buf;
  593. }
  594. else
  595. {
  596. item->size = sizeof(buf_block_t) + size;
  597. item->addr = (uint8_t *)item + sizeof(buf_block_t);
  598. }
  599. item->next = bitmap_buffer.head;
  600. item->regular_info = bitmap->regular_info;
  601. bitmap_buffer.head = item;
  602. // SYS_LOG_INF("\n\n cache bitmap %d return 0x%x\n", id, item->addr);
  603. buffer->offset += item->size;
  604. buffer->free_size -= item->size;
  605. #if RES_MEM_DEBUG
  606. if(res_mem_check()<0)
  607. {
  608. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  609. res_mem_dump();
  610. }
  611. #endif
  612. return item->addr;
  613. }
  614. static buf_block_t* _malloc_buf_block(uint32_t size)
  615. {
  616. buf_block_t* item;
  617. uint8_t* ui_mem_buf;
  618. if(size == screen_bitmap_size)
  619. {
  620. ui_mem_buf = res_mem_alloc_block(size, __func__);
  621. if(ui_mem_buf == NULL)
  622. {
  623. SYS_LOG_INF("cant malloc screen size bitmap buffer from ui mem \n");
  624. return NULL;
  625. }
  626. ui_mem_total++;
  627. //SYS_LOG_DBG("2 ui mem alloc %d, addr %p, styid 0x%x\n", bitmap->sty_data->id, ui_mem_buf, bitmap->sty_data->sty_id);
  628. #ifdef CONFIG_RES_MANAGER_ALIGN
  629. item = (buf_block_t*)res_mem_aligned_alloc(RES_MEM_POOL_BMP, res_mem_align, sizeof(buf_block_t));
  630. #else
  631. item = (buf_block_t*)res_mem_alloc(RES_MEM_POOL_BMP, sizeof(buf_block_t));
  632. #endif
  633. if(item == NULL)
  634. {
  635. //SYS_LOG_DBG("%d ui mem free %d\n", __LINE__, bitmap->sty_data->id);
  636. res_mem_free_block(ui_mem_buf);
  637. ui_mem_total--;
  638. SYS_LOG_INF("%s %d: error: resource buffer not enough for size %d\n", __func__, __LINE__, size);
  639. return NULL;
  640. }
  641. item->size = sizeof(buf_block_t);
  642. item->addr = ui_mem_buf;
  643. }
  644. else
  645. {
  646. #ifdef CONFIG_RES_MANAGER_ALIGN
  647. item = (buf_block_t*)res_mem_aligned_alloc(RES_MEM_POOL_BMP, res_mem_align, sizeof(buf_block_t) + size);
  648. #else
  649. item = (buf_block_t*)res_mem_alloc(RES_MEM_POOL_BMP, sizeof(buf_block_t) + size);
  650. #endif
  651. if(item == NULL)
  652. {
  653. SYS_LOG_INF("error: resource buffer not enough for size %d\n", size);
  654. return NULL;
  655. }
  656. item->size = sizeof(buf_block_t) + size;
  657. item->addr = (uint8_t *)item + sizeof(buf_block_t);
  658. }
  659. return item;
  660. }
  661. static uint8_t* _get_resource_bitmap_buffer(uint32_t source, resource_bitmap_t* bitmap, uint32_t size, uint32_t force_ref)
  662. {
  663. buf_block_t* item;
  664. size = ((size+res_mem_align-1)/res_mem_align)*res_mem_align; //aligned next offset to 4 bytes
  665. if(force_ref > 1 )
  666. {
  667. return _get_resource_bitmap_buffer_compact(source, bitmap, size, force_ref);
  668. }
  669. item = bitmap_buffer.head;
  670. while(item != NULL)
  671. {
  672. if((item->source == source)&&(item->id == bitmap->sty_data->id))
  673. {
  674. break;
  675. }
  676. item = item->next;
  677. }
  678. if(item != NULL)
  679. {
  680. //found loaded bitmap, which is not supposed to happen but fine
  681. SYS_LOG_INF("should not happen\n");
  682. if(force_ref >= 1)
  683. {
  684. item->ref ++;
  685. }
  686. return item->addr;
  687. }
  688. else
  689. {
  690. item = _malloc_buf_block(size);
  691. if (item == NULL)
  692. {
  693. res_manager_dump_info();
  694. return NULL;
  695. }
  696. item->source = source;
  697. item->id = bitmap->sty_data->id;
  698. if(force_ref >= 1)
  699. {
  700. item->ref = 1;
  701. }
  702. else
  703. {
  704. item->ref = 0;
  705. }
  706. item->width = (uint32_t)bitmap->sty_data->width;
  707. item->height = (uint32_t)bitmap->sty_data->height;
  708. item->bytes_per_pixel = (uint32_t)bitmap->sty_data->bytes_per_pixel;
  709. item->format = (uint32_t)bitmap->sty_data->format;
  710. item->next = bitmap_buffer.head;
  711. bitmap_buffer.head = item;
  712. item->regular_info = bitmap->regular_info;
  713. #if RES_MEM_DEBUG
  714. if(res_mem_check()<0)
  715. {
  716. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  717. res_mem_dump();
  718. }
  719. #endif
  720. return item->addr;
  721. }
  722. }
  723. //uint8_t* _try_resource_bitmap_buffer(uint32_t source, uint32_t id, uint32_t* width, uint32_t* height,
  724. // uint32_t* bytes_per_pixel, uint32_t* format, uint32_t force_ref)
  725. static uint8_t* _try_resource_bitmap_buffer(uint32_t source, resource_bitmap_t* bitmap, uint32_t force_ref)
  726. {
  727. buf_block_t* item;
  728. item = bitmap_buffer.head;
  729. while(item != NULL)
  730. {
  731. if((item->source == source)&&(item->id == bitmap->sty_data->id))
  732. {
  733. break;
  734. }
  735. item = item->next;
  736. }
  737. if(item != NULL)
  738. {
  739. //found loaded bitmap
  740. bitmap->regular_info = item->regular_info;
  741. if(force_ref == 1)
  742. {
  743. item->ref ++;
  744. }
  745. // SYS_LOG_INF("\n ### got cached bitmap id %d\n", id);
  746. return item->addr;
  747. }
  748. else
  749. {
  750. //not found, need to load from file
  751. return NULL;
  752. }
  753. }
  754. static int32_t _get_resource_bitmap_map(resource_info_t* info, resource_bitmap_t* bitmap)
  755. {
  756. #ifdef CONFIG_RES_MANAGER_USE_STYLE_MMAP
  757. int32_t ret;
  758. int32_t compress_size = 0;
  759. uint32_t bmp_pos = 0;
  760. if(bitmap->sty_data->format != RESOURCE_BITMAP_FORMAT_RAW
  761. && bitmap->sty_data->format != RESOURCE_BITMAP_FORMAT_INDEX8
  762. && bitmap->sty_data->format != RESOURCE_BITMAP_FORMAT_INDEX4
  763. #ifdef CONFIG_LVGL_USE_IMG_DECODER_ACTS
  764. && bitmap->sty_data->format != RESOURCE_BITMAP_FORMAT_JPEG
  765. #endif
  766. )
  767. {
  768. return -1;
  769. }
  770. #ifndef CONFIG_SIMULATOR
  771. if(info->pic_res_mmap_addr == NULL)
  772. {
  773. SYS_LOG_INF("info->pic_res_mmap_addr %p\n", info->pic_res_mmap_addr);
  774. return -1;
  775. }
  776. ret = _search_res_id_in_files(info, bitmap, &bmp_pos, &compress_size);
  777. if(ret < 0)
  778. {
  779. //search faild, how to fix?
  780. SYS_LOG_INF("search res id in files faild in map\n");
  781. return -1;
  782. }
  783. bitmap->buffer = info->pic_res_mmap_addr + bmp_pos;
  784. return 0;
  785. #else
  786. ret = _search_res_id_in_files(info, bitmap, &bmp_pos, &compress_size);
  787. if(ret < 0)
  788. {
  789. //search faild, how to fix?
  790. SYS_LOG_INF("search res id in files faild in map\n");
  791. return -1;
  792. }
  793. bitmap->buffer = malloc(compress_size);
  794. fs_seek(&info->pic_fp, bmp_pos, FS_SEEK_SET);
  795. ret = fs_read(&info->pic_fp, bitmap->buffer, compress_size);
  796. if(ret < compress_size)
  797. {
  798. SYS_LOG_INF("bitmap read error %d\n", ret);
  799. }
  800. return 0;
  801. #endif
  802. #else
  803. //config not defined
  804. return -1;
  805. #endif //CONFIG_RES_MANAGER_USE_STYLE_MMAP
  806. }
  807. uint32_t _check_bitmap_in_compact_buffer(void* p)
  808. {
  809. compact_buffer_t* buffer;
  810. uint32_t ret = 0;
  811. buf_block_t* item = (buf_block_t*)p;
  812. buffer = bitmap_buffer.compact_buffer_list;
  813. while(buffer != NULL)
  814. {
  815. if(p >= (void*)buffer->addr && p < (void*)(buffer->addr + buffer->offset))
  816. {
  817. ret = 1;
  818. if(p==buffer->addr && item->size == buffer->offset && buffer->free_size <= COMPACT_BUFFER_MARGIN_SIZE)
  819. {
  820. SYS_LOG_DBG("compact to be released %p\n", p);
  821. ret = (uint32_t)buffer;
  822. }
  823. break;
  824. }
  825. buffer = buffer->next;
  826. }
  827. return ret;
  828. }
  829. void _free_resource_bitmap_buffer(void *p)
  830. {
  831. buf_block_t* item = bitmap_buffer.head;
  832. buf_block_t* prev = NULL;
  833. uint32_t is_compact = 0;
  834. #if RES_MEM_DEBUG
  835. if(res_mem_check()<0)
  836. {
  837. SYS_LOG_INF("%s %d item 0x%x \n", __FILE__, __LINE__, item);
  838. res_mem_dump();
  839. }
  840. #endif
  841. #ifdef CONFIG_SIMULATOR
  842. #if CONFIG_UI_RES_MEM_POOL_SIZE > 0
  843. if (!ui_mem_is_res(p) && !ui_mem_is_fb(p))
  844. {
  845. free(p);
  846. return;
  847. }
  848. #endif
  849. #endif
  850. while(item != NULL)
  851. {
  852. if(item->addr == p)
  853. {
  854. break;
  855. }
  856. prev = item;
  857. item = item->next;
  858. }
  859. if(item == NULL)
  860. {
  861. // SYS_LOG_INF("resource bitmap not found %p", p);
  862. /*
  863. item = bitmap_buffer.head;
  864. while(item!=NULL)
  865. {
  866. SYS_LOG_INF("item in list 0x%x \n", item->addr);
  867. item = item->next;
  868. }
  869. */
  870. return;
  871. }
  872. //check if p belongs to scene buffer, use item in check in case p is in ui mem
  873. if(bitmap_buffer.compact_buffer_list != NULL)
  874. {
  875. is_compact = _check_bitmap_in_compact_buffer((void*)item);
  876. }
  877. if(item->ref > 1)
  878. {
  879. item->ref --;
  880. //FIXME
  881. return;
  882. }
  883. else if(item->regular_info != 0)
  884. {
  885. return;
  886. }
  887. else
  888. {
  889. if(prev == NULL)
  890. {
  891. bitmap_buffer.head = item->next;
  892. }
  893. else
  894. {
  895. prev->next = item->next;
  896. }
  897. if(is_compact == 0)
  898. {
  899. //if item size equals buf_block_t meaning it's a screen size
  900. if(item->size > sizeof(buf_block_t))
  901. {
  902. res_mem_free(RES_MEM_POOL_BMP, (void*)item);
  903. }
  904. else
  905. {
  906. SYS_LOG_DBG("%d ui mem free %d\n", __LINE__, item->id);
  907. res_mem_free_block(item->addr);
  908. ui_mem_total--;
  909. res_mem_free(RES_MEM_POOL_BMP, (void*)item);
  910. }
  911. }
  912. else
  913. {
  914. //in case item was deleted in bitmap list first,
  915. //then when scene unloads, no check upon it, so ui mem leaked.
  916. if(item->size == sizeof(buf_block_t))
  917. {
  918. SYS_LOG_DBG("%d ui mem free %d\n", __LINE__, item->id);
  919. res_mem_free_block(item->addr);
  920. ui_mem_total--;
  921. item->addr = NULL;
  922. }
  923. else if(is_compact > 1)
  924. {
  925. //only 1 pic in compact buffer, compact buffer can be released here
  926. //theoretically, when a pic is unloaded in a scene, it wont be used before next preload
  927. compact_buffer_t* waste = (compact_buffer_t*)is_compact;
  928. compact_buffer_t* prbuf = NULL;
  929. compact_buffer_t* buffer = bitmap_buffer.compact_buffer_list;
  930. while(waste != buffer)
  931. {
  932. prbuf = buffer;
  933. buffer = buffer->next;
  934. }
  935. if(prbuf == NULL)
  936. {
  937. bitmap_buffer.compact_buffer_list = buffer->next;
  938. }
  939. else
  940. {
  941. prbuf->next = buffer->next;
  942. }
  943. SYS_LOG_DBG("compact early release %p\n", buffer->addr);
  944. if(buffer->free_size + buffer->offset == screen_bitmap_size)
  945. {
  946. res_mem_free_block(buffer->addr);
  947. ui_mem_total--;
  948. }
  949. else
  950. {
  951. res_mem_free(RES_MEM_POOL_BMP, buffer->addr);
  952. }
  953. res_array_free(buffer);
  954. }
  955. }
  956. #if RES_MEM_DEBUG
  957. if(res_mem_check()<0)
  958. {
  959. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  960. res_mem_dump();
  961. }
  962. #endif
  963. }
  964. }
  965. static void _init_pic_mmap_addr(resource_info_t* info, const char* picres_path)
  966. {
  967. #ifdef CONFIG_RES_MANAGER_USE_STYLE_MMAP
  968. #ifndef CONFIG_SIMULATOR
  969. uint32_t res_size;
  970. sd_fmap(picres_path, (void**)&info->pic_res_mmap_addr, &res_size);
  971. #endif //CONFIG_SIMULATOR
  972. #endif //CONFIG_RES_MANAGER_USE_STYLE_MMAP
  973. }
  974. static int _init_pic_search_param(resource_info_t* info, const char* picres_path)
  975. {
  976. int i,k;
  977. int ret;
  978. res_head_t res_head;
  979. res_entry_t* entry;
  980. char* partition;
  981. char* res_path = NULL;
  982. int part_count = 0;
  983. info->pic_search_param = (pic_search_param_t*)res_mem_alloc(RES_MEM_POOL_BMP, (MAX_PARTITION_ID+1)*sizeof(pic_search_param_t));
  984. if(info->pic_search_param == NULL)
  985. {
  986. SYS_LOG_INF("no enough memory to store pic res param\n");
  987. res_manager_dump_info();
  988. return -1;
  989. }
  990. memset(info->pic_search_param, 0, (MAX_PARTITION_ID+1)*sizeof(pic_search_param_t));
  991. res_path = (char*)mem_malloc(strlen(picres_path)+1);
  992. if(res_path == NULL)
  993. {
  994. SYS_LOG_INF("no enough memory to store pic res path\n");
  995. return -1;
  996. }
  997. memset(res_path, 0, strlen(picres_path)+1);
  998. strcpy(res_path, picres_path);
  999. #ifdef CONFIG_SIMULATOR
  1000. partition = NULL;
  1001. part_count = 0;
  1002. #else
  1003. if(res_is_auto_search_files())
  1004. {
  1005. partition = strrchr(res_path, ':') + 1;
  1006. part_count = MAX_PARTITION_ID;
  1007. //path out of normal path boundry, use it directly
  1008. if(*partition > storage_part_name[part_count])
  1009. {
  1010. partition = NULL;
  1011. part_count = 0;
  1012. }
  1013. }
  1014. else
  1015. {
  1016. partition = NULL;
  1017. part_count = 0;
  1018. }
  1019. #endif
  1020. for(i=0;i<=part_count;i++)
  1021. {
  1022. if (partition)
  1023. {
  1024. *partition = storage_part_name[i];
  1025. SYS_LOG_DBG("partition %c\n", *partition);
  1026. }
  1027. SYS_LOG_DBG("res_path %s\n", res_path);
  1028. if(i == 0)
  1029. {
  1030. ret = fs_open(&info->pic_fp, res_path, FS_O_READ);
  1031. if ( ret < 0 )
  1032. {
  1033. SYS_LOG_INF(" open picture resource file %s failed!\n", res_path);
  1034. mem_free(res_path);
  1035. return -1;
  1036. }
  1037. fs_seek(&info->pic_fp, 0, FS_SEEK_SET);
  1038. ret = fs_read(&info->pic_fp, &res_head, sizeof(res_head_t));
  1039. if(ret < sizeof(res_head_t))
  1040. {
  1041. SYS_LOG_INF("read picture resource header failed\n");
  1042. fs_close(&info->pic_fp);
  1043. mem_free(res_path);
  1044. return -1;
  1045. }
  1046. if(res_head.magic[0]!='R' || res_head.magic[1]!='E' || res_head.magic[2]!='S')
  1047. {
  1048. SYS_LOG_INF("read picture resource header invalid\n");
  1049. fs_close(&info->pic_fp);
  1050. mem_free(res_path);
  1051. return -1;
  1052. }
  1053. entry = res_mem_alloc(RES_MEM_POOL_BMP, res_head.counts*sizeof(res_entry_t));
  1054. if(entry == NULL)
  1055. {
  1056. SYS_LOG_INF("malloc res entries failed\n");
  1057. fs_close(&info->pic_fp);
  1058. mem_free(res_path);
  1059. res_manager_dump_info();
  1060. return -1;
  1061. }
  1062. ret = fs_read(&info->pic_fp, entry, res_head.counts*sizeof(res_entry_t));
  1063. if(ret < res_head.counts*sizeof(res_entry_t))
  1064. {
  1065. SYS_LOG_INF("read picture resource entries failed\n");
  1066. res_mem_free(RES_MEM_POOL_BMP, entry);
  1067. fs_close(&info->pic_fp);
  1068. mem_free(res_path);
  1069. return -1;
  1070. }
  1071. info->pic_search_param[i].id_start = 1;
  1072. info->pic_search_param[i].id_end = info->pic_search_param[i].id_start + res_head.counts - 1;
  1073. info->pic_search_param[i].pic_offsets = res_mem_alloc(RES_MEM_POOL_BMP, res_head.counts*sizeof(uint32_t));
  1074. if(info->pic_search_param[i].pic_offsets == NULL)
  1075. {
  1076. SYS_LOG_INF("malloc pic search offsets failed");
  1077. res_mem_free(RES_MEM_POOL_BMP, entry);
  1078. fs_close(&info->pic_fp);
  1079. mem_free(res_path);
  1080. res_manager_dump_info();
  1081. return -1;
  1082. }
  1083. info->pic_search_param[i].compress_size = res_mem_alloc(RES_MEM_POOL_BMP, res_head.counts*sizeof(uint32_t));
  1084. if(info->pic_search_param[i].compress_size == NULL)
  1085. {
  1086. SYS_LOG_INF("malloc pic search offsets failed");
  1087. res_mem_free(RES_MEM_POOL_BMP, entry);
  1088. res_mem_free(RES_MEM_POOL_BMP, info->pic_search_param[i].pic_offsets);
  1089. info->pic_search_param[i].pic_offsets = NULL;
  1090. fs_close(&info->pic_search_param[i].res_fp);
  1091. mem_free(res_path);
  1092. res_manager_dump_info();
  1093. return -1;
  1094. }
  1095. SYS_LOG_INF("id start %d, id_end %d\n", info->pic_search_param[i].id_start, info->pic_search_param[i].id_end);
  1096. for(k=0;k<res_head.counts;k++)
  1097. {
  1098. //skip 4 bytes before bitmap data, which are metric params
  1099. info->pic_search_param[i].pic_offsets[k]= entry[k].offset+4;
  1100. info->pic_search_param[i].compress_size[k] = ((entry[k].len_ext << 16) | entry[k].length) - 4;
  1101. }
  1102. res_mem_free(RES_MEM_POOL_BMP, entry);
  1103. }
  1104. else
  1105. {
  1106. ret = fs_open(&info->pic_search_param[i].res_fp, res_path, FS_O_READ);
  1107. if ( ret < 0 )
  1108. {
  1109. continue;
  1110. }
  1111. fs_seek(&info->pic_search_param[i].res_fp, 0, FS_SEEK_SET);
  1112. ret = fs_read(&info->pic_search_param[i].res_fp, &res_head, sizeof(res_head_t));
  1113. if(ret < sizeof(res_head_t))
  1114. {
  1115. SYS_LOG_INF("read picture resource header failed\n");
  1116. mem_free(res_path);
  1117. return -1;
  1118. }
  1119. entry = res_mem_alloc(RES_MEM_POOL_BMP, res_head.counts*sizeof(res_entry_t));
  1120. if(entry == NULL)
  1121. {
  1122. SYS_LOG_INF("malloc res entries failed\n");
  1123. fs_close(&info->pic_search_param[i].res_fp);
  1124. mem_free(res_path);
  1125. res_manager_dump_info();
  1126. return -1;
  1127. }
  1128. ret = fs_read(&info->pic_search_param[i].res_fp, entry, res_head.counts*sizeof(res_entry_t));
  1129. if(ret < res_head.counts*sizeof(res_entry_t))
  1130. {
  1131. SYS_LOG_INF("read picture resource entries failed\n");
  1132. res_mem_free(RES_MEM_POOL_BMP, entry);
  1133. fs_close(&info->pic_search_param[i].res_fp);
  1134. mem_free(res_path);
  1135. return -1;
  1136. }
  1137. info->pic_search_param[i].id_start = res_head.id_start;
  1138. info->pic_search_param[i].id_end = res_head.id_start + res_head.counts - 1;
  1139. info->pic_search_param[i].pic_offsets = res_mem_alloc(RES_MEM_POOL_BMP, res_head.counts*sizeof(uint32_t));
  1140. if(info->pic_search_param[i].pic_offsets == NULL)
  1141. {
  1142. SYS_LOG_INF("malloc pic search offsets failed");
  1143. res_mem_free(RES_MEM_POOL_BMP, entry);
  1144. fs_close(&info->pic_search_param[i].res_fp);
  1145. mem_free(res_path);
  1146. res_manager_dump_info();
  1147. return -1;
  1148. }
  1149. info->pic_search_param[i].compress_size = res_mem_alloc(RES_MEM_POOL_BMP, res_head.counts*sizeof(uint32_t));
  1150. if(info->pic_search_param[i].compress_size == NULL)
  1151. {
  1152. SYS_LOG_INF("malloc pic search compress size failed");
  1153. res_mem_free(RES_MEM_POOL_BMP, entry);
  1154. res_mem_free(RES_MEM_POOL_BMP, info->pic_search_param[i].pic_offsets);
  1155. info->pic_search_param[i].pic_offsets = NULL;
  1156. fs_close(&info->pic_search_param[i].res_fp);
  1157. mem_free(res_path);
  1158. res_manager_dump_info();
  1159. return -1;
  1160. }
  1161. SYS_LOG_INF("id start %d, id_end %d\n", info->pic_search_param[i].id_start, info->pic_search_param[i].id_end);
  1162. for(k=0;k<res_head.counts;k++)
  1163. {
  1164. //skip 4 bytes before bitmap data, which are metric params
  1165. info->pic_search_param[i].pic_offsets[k]= entry[k].offset+4;
  1166. info->pic_search_param[i].compress_size[k] = ((entry[k].len_ext << 16) | entry[k].length) - 4;
  1167. }
  1168. res_mem_free(RES_MEM_POOL_BMP, entry);
  1169. }
  1170. info->pic_search_max_volume++;
  1171. }
  1172. mem_free(res_path);
  1173. _init_pic_mmap_addr(info, picres_path);
  1174. return 0;
  1175. }
  1176. //读取style 文件的头部信息, 返回值是该配置文件中scene 的总数
  1177. //缓存图片资源文件索引信息
  1178. static int _read_head( resource_info_t* info )
  1179. {
  1180. style_head_t* head;
  1181. int ret;
  1182. uint32_t sty_size;
  1183. os_strace_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_0, (uint32_t)0);
  1184. fs_seek(&info->style_fp, 0, FS_SEEK_END);
  1185. sty_size = fs_tell(&info->style_fp);
  1186. fs_seek(&info->style_fp, 0, FS_SEEK_SET);
  1187. #if defined(CONFIG_RES_MANAGER_USE_STYLE_MMAP) && !defined(CONFIG_SIMULATOR)
  1188. ret = sd_fmap(info->sty_path, (void**)&info->sty_mem, &sty_size);
  1189. if(ret < 0)
  1190. {
  1191. SYS_LOG_INF("mmap style file failed\n");
  1192. return -1;
  1193. }
  1194. head = (style_head_t*)info->sty_mem;
  1195. if(head->scene_sum == 0xffffffff)
  1196. {
  1197. SYS_LOG_INF("read style file invalid 0x%x\n", head->scene_sum);
  1198. info->sty_mem = NULL;
  1199. return -1;
  1200. }
  1201. #else
  1202. info->sty_mem = (uint8_t*)res_mem_alloc(RES_MEM_POOL_SCENE, sty_size);
  1203. if(info->sty_mem == NULL)
  1204. {
  1205. SYS_LOG_INF("no enough mem for style data\n");
  1206. res_manager_dump_info();
  1207. return -1;
  1208. }
  1209. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_0, (uint32_t)0);
  1210. ret = fs_read(&info->style_fp, info->sty_mem, sty_size);
  1211. if(ret < sty_size)
  1212. {
  1213. SYS_LOG_INF("read style file error:%d\n", ret);
  1214. res_mem_free(RES_MEM_POOL_SCENE, info->sty_mem);
  1215. info->sty_mem = NULL;
  1216. return -1;
  1217. }
  1218. head = (style_head_t*)info->sty_mem;
  1219. if(head->scene_sum == 0xffffffff)
  1220. {
  1221. SYS_LOG_INF("read style file invalid 0x%x\n", head->scene_sum);
  1222. free(info->sty_mem);
  1223. info->sty_mem = NULL;
  1224. return -1;
  1225. }
  1226. #endif
  1227. info->sum = head->scene_sum;
  1228. info->scenes = (resource_scene_t*)(info->sty_mem + sizeof(style_head_t));
  1229. info->sty_data = info->sty_mem + sizeof(style_head_t) + sizeof(resource_scene_t)*info->sum;
  1230. #if RES_MEM_DEBUG
  1231. if(res_mem_check()<0)
  1232. {
  1233. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  1234. res_mem_dump();
  1235. }
  1236. #endif
  1237. return 0;
  1238. }
  1239. static int res_manager_inited = 0;
  1240. void res_manager_init(void)
  1241. {
  1242. if(res_manager_inited == 0)
  1243. {
  1244. res_mem_init();
  1245. _resource_buffer_init();
  1246. res_mem_align = res_mem_get_align();
  1247. os_mutex_init(&bitmap_cache_mutex);
  1248. os_mutex_init(&bitmap_read_mutex);
  1249. res_manager_inited = 1;
  1250. }
  1251. }
  1252. void res_manager_set_screen_size(uint32_t screen_w, uint32_t screen_h)
  1253. {
  1254. uint32_t align = res_mem_get_align();
  1255. uint32_t size;
  1256. size = screen_w*screen_h*2;
  1257. size = ((size+align-1)/align)*align;
  1258. screen_bitmap_size = size;
  1259. }
  1260. void res_manager_clear_cache(uint32_t force_clear)
  1261. {
  1262. _resource_buffer_deinit(force_clear);
  1263. }
  1264. int32_t res_manager_set_str_file(resource_info_t* info, const char* text_path)
  1265. {
  1266. char* file_path = NULL;
  1267. char* partition;
  1268. int i = 0;
  1269. int ret;
  1270. buf_block_t* listp;
  1271. buf_block_t* item;
  1272. buf_block_t* prev;
  1273. if( text_path != NULL)
  1274. {
  1275. //close old file
  1276. fs_close(&info->text_fp);
  1277. if(info->str_path)
  1278. {
  1279. mem_free(info->str_path);
  1280. info->str_path = NULL;
  1281. }
  1282. #ifdef CONFIG_SIMULATOR
  1283. ret = fs_open(&info->text_fp, text_path, FS_O_READ);
  1284. if ( ret < 0 )
  1285. {
  1286. SYS_LOG_INF(" open text resource file %s failed! \n", text_path);
  1287. goto ERR_EXIT;
  1288. }
  1289. #else
  1290. if(res_is_auto_search_files())
  1291. {
  1292. file_path = (char*)mem_malloc(strlen(text_path)+1);
  1293. if(file_path == NULL)
  1294. {
  1295. SYS_LOG_INF("malloc file path failed\n");
  1296. goto ERR_EXIT;
  1297. }
  1298. memset(file_path, 0, strlen(text_path)+1);
  1299. strcpy(file_path, text_path);
  1300. partition = strrchr(file_path, ':') + 1;
  1301. if(partition == NULL)
  1302. {
  1303. SYS_LOG_INF("invalid str file path\n");
  1304. goto ERR_EXIT;
  1305. }
  1306. for(i=MAX_PARTITION_ID;i>=0;i--)
  1307. {
  1308. *partition = storage_part_name[i];
  1309. ret = fs_open(&info->text_fp, file_path, FS_O_READ);
  1310. if ( ret < 0 )
  1311. {
  1312. SYS_LOG_INF("open str file path %s failed\n", file_path);
  1313. continue;
  1314. }
  1315. else
  1316. {
  1317. SYS_LOG_INF("open str file path %s ok\n", file_path);
  1318. break;
  1319. }
  1320. }
  1321. if(i<0)
  1322. {
  1323. SYS_LOG_INF(" open str file %s failed! \n", text_path);
  1324. goto ERR_EXIT;
  1325. }
  1326. }
  1327. else
  1328. {
  1329. ret = fs_open(&info->text_fp, text_path, FS_O_READ);
  1330. if ( ret < 0 )
  1331. {
  1332. SYS_LOG_INF(" open text resource file %s failed! \n", text_path);
  1333. goto ERR_EXIT;
  1334. }
  1335. }
  1336. #endif
  1337. info->str_path = (uint8_t*)mem_malloc(strlen(text_path)+1);
  1338. if(info->str_path == NULL)
  1339. {
  1340. SYS_LOG_INF("malloc info str path failed\n");
  1341. goto ERR_EXIT;
  1342. }
  1343. memset(info->str_path, 0, strlen(text_path)+1);
  1344. strcpy(info->str_path, text_path);
  1345. //clear cache
  1346. listp = text_buffer.head;
  1347. prev = NULL;
  1348. while(listp != NULL)
  1349. {
  1350. if(listp->source == (uint32_t)info)
  1351. {
  1352. item = listp;
  1353. listp = listp->next;
  1354. if(prev)
  1355. {
  1356. prev->next = listp;
  1357. }
  1358. if(text_buffer.head == item)
  1359. {
  1360. text_buffer.head = listp;
  1361. }
  1362. res_mem_free(RES_MEM_POOL_TXT, item);
  1363. }
  1364. else
  1365. {
  1366. prev = listp;
  1367. listp = listp->next;
  1368. }
  1369. }
  1370. // memset(&text_buffer, 0, sizeof(text_buffer));
  1371. }
  1372. else
  1373. {
  1374. SYS_LOG_INF("text resource file path null");
  1375. }
  1376. if(file_path)
  1377. {
  1378. mem_free(file_path);
  1379. }
  1380. return 0;
  1381. ERR_EXIT:
  1382. if(file_path)
  1383. mem_free(file_path);
  1384. fs_close(&info->text_fp);
  1385. if(info->str_path)
  1386. {
  1387. mem_free(info->str_path);
  1388. info->str_path = NULL;
  1389. }
  1390. return -1;
  1391. }
  1392. resource_info_t* res_manager_open_resources( const char* style_path, const char* picture_path, const char* text_path )
  1393. {
  1394. int ret;
  1395. resource_info_t* info;
  1396. char* partition;
  1397. char* file_path = NULL;
  1398. os_strace_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_2, (uint32_t)0);
  1399. info = (resource_info_t*)mem_malloc( sizeof( resource_info_t ) );
  1400. if ( info == NULL )
  1401. {
  1402. SYS_LOG_INF( "error: malloc resource info failed ! \n" );
  1403. return NULL;
  1404. }
  1405. memset(info, 0, sizeof(resource_info_t));
  1406. if ( style_path != NULL )
  1407. {
  1408. int i;
  1409. SYS_LOG_INF("open sty file %s\n", style_path);
  1410. #ifdef CONFIG_SIMULATOR
  1411. ret = fs_open(&info->style_fp, style_path, FS_O_READ);
  1412. if ( ret < 0 )
  1413. {
  1414. SYS_LOG_INF(" open style file %s failed! \n", style_path);
  1415. goto ERR_EXIT;
  1416. }
  1417. #else
  1418. if(res_is_auto_search_files())
  1419. {
  1420. file_path = (char*)mem_malloc(strlen(style_path)+1);
  1421. if(file_path == NULL)
  1422. {
  1423. SYS_LOG_INF("malloc style file path failed! \n");
  1424. goto ERR_EXIT;
  1425. }
  1426. memset(file_path, 0, strlen(style_path)+1);
  1427. strcpy(file_path, style_path);
  1428. partition = strrchr(file_path, ':') + 1;
  1429. if(partition == NULL)
  1430. goto ERR_EXIT;
  1431. SYS_LOG_INF("MAX_PARTITION_ID %d\n", MAX_PARTITION_ID);
  1432. for(i=MAX_PARTITION_ID;i>=0;i--)
  1433. {
  1434. SYS_LOG_INF("partition %d: %c\n", i, *partition);
  1435. *partition = storage_part_name[i];
  1436. ret = fs_open(&info->style_fp, file_path, FS_O_READ);
  1437. if ( ret < 0 )
  1438. {
  1439. continue;
  1440. }
  1441. else
  1442. {
  1443. SYS_LOG_INF("style file_path %d: %s\n", i, file_path);
  1444. break;
  1445. }
  1446. }
  1447. if(i<0)
  1448. {
  1449. ret = fs_open(&info->style_fp, style_path, FS_O_READ);
  1450. if ( ret < 0 )
  1451. {
  1452. SYS_LOG_INF(" open style file %s failed! \n", style_path);
  1453. goto ERR_EXIT;
  1454. }
  1455. }
  1456. }
  1457. else
  1458. {
  1459. ret = fs_open(&info->style_fp, style_path, FS_O_READ);
  1460. if ( ret < 0 )
  1461. {
  1462. SYS_LOG_INF(" open style file %s failed! \n", style_path);
  1463. goto ERR_EXIT;
  1464. }
  1465. }
  1466. #endif
  1467. info->sty_path = (uint8_t*)mem_malloc(strlen(style_path)+1);
  1468. memset(info->sty_path, 0, strlen(style_path)+1);
  1469. strcpy(info->sty_path, style_path);
  1470. }
  1471. else
  1472. {
  1473. SYS_LOG_INF("style file path null");
  1474. }
  1475. if( picture_path != NULL)
  1476. {
  1477. SYS_LOG_INF("open pic file %s\n", picture_path);
  1478. _init_pic_search_param(info, picture_path);
  1479. }
  1480. else
  1481. {
  1482. SYS_LOG_INF("picture resource file path null");
  1483. }
  1484. if( text_path != NULL)
  1485. {
  1486. SYS_LOG_INF("open text file %s\n", text_path);
  1487. #ifdef CONFIG_SIMULATOR
  1488. ret = fs_open(&info->text_fp, text_path, FS_O_READ);
  1489. if ( ret < 0 )
  1490. {
  1491. SYS_LOG_INF(" open text resource file %s failed! \n", text_path);
  1492. goto ERR_EXIT;
  1493. }
  1494. #else
  1495. if(res_is_auto_search_files())
  1496. {
  1497. int i = 0;
  1498. if(file_path)
  1499. mem_free(file_path);
  1500. file_path = (char*)mem_malloc(strlen(text_path)+1);
  1501. memset(file_path, 0, strlen(text_path)+1);
  1502. strcpy(file_path, text_path);
  1503. partition = strrchr(file_path, ':') + 1;
  1504. if(partition == NULL)
  1505. goto ERR_EXIT;
  1506. for(i=MAX_PARTITION_ID;i>=0;i--)
  1507. {
  1508. *partition = storage_part_name[i];
  1509. ret = fs_open(&info->text_fp, file_path, FS_O_READ);
  1510. if ( ret < 0 )
  1511. {
  1512. continue;
  1513. }
  1514. else
  1515. {
  1516. break;
  1517. }
  1518. }
  1519. if(i<0)
  1520. {
  1521. ret = fs_open(&info->text_fp, text_path, FS_O_READ);
  1522. if ( ret < 0 )
  1523. {
  1524. SYS_LOG_INF(" open text resource file %s failed!\n", text_path);
  1525. goto ERR_EXIT;
  1526. }
  1527. }
  1528. }
  1529. else
  1530. {
  1531. ret = fs_open(&info->text_fp, text_path, FS_O_READ);
  1532. if ( ret < 0 )
  1533. {
  1534. SYS_LOG_INF(" open text resource file %s failed!\n", text_path);
  1535. goto ERR_EXIT;
  1536. }
  1537. }
  1538. #endif
  1539. info->str_path = (uint8_t*)mem_malloc(strlen(text_path)+1);
  1540. memset(info->str_path, 0, strlen(text_path)+1);
  1541. strcpy(info->str_path, text_path);
  1542. }
  1543. else
  1544. {
  1545. SYS_LOG_INF("text resource file path null \n");
  1546. }
  1547. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_2, (uint32_t)0);
  1548. _read_head( info ); //读取style头部信息,不做返回值判断,可能不存在样式文件。
  1549. if(file_path)
  1550. mem_free(file_path);
  1551. return info;
  1552. ERR_EXIT:
  1553. if(file_path)
  1554. mem_free(file_path);
  1555. fs_close(&info->style_fp);
  1556. fs_close(&info->pic_fp);
  1557. fs_close(&info->text_fp);
  1558. if(info->sty_path)
  1559. {
  1560. mem_free(info->sty_path);
  1561. info->sty_path = NULL;
  1562. }
  1563. if(info->str_path)
  1564. {
  1565. mem_free(info->str_path);
  1566. info->str_path = NULL;
  1567. }
  1568. mem_free(info);
  1569. return NULL;
  1570. }
  1571. void res_manager_close_resources( resource_info_t* info )
  1572. {
  1573. if(info == NULL)
  1574. {
  1575. SYS_LOG_INF("null resource info pointer \n");
  1576. return;
  1577. }
  1578. if(info->sty_path != NULL)
  1579. {
  1580. SYS_LOG_INF("close sty file %s\n", info->sty_path);
  1581. fs_close(&info->style_fp);
  1582. #ifndef CONFIG_SIMULATOR
  1583. memset(&info->style_fp, 0, sizeof(struct fs_file_t));
  1584. #endif
  1585. }
  1586. if(info->pic_search_param != NULL)
  1587. {
  1588. fs_close(&info->pic_fp);
  1589. #ifndef CONFIG_SIMULATOR
  1590. memset(&info->pic_fp, 0, sizeof(struct fs_file_t));
  1591. #endif
  1592. }
  1593. if(info->str_path != NULL)
  1594. {
  1595. fs_close(&info->text_fp);
  1596. #ifndef CONFIG_SIMULATOR
  1597. memset(&info->text_fp, 0, sizeof(struct fs_file_t));
  1598. #endif
  1599. }
  1600. if(info->sty_mem != NULL)
  1601. {
  1602. res_mem_free(RES_MEM_POOL_SCENE, info->sty_mem);
  1603. info->sty_mem = NULL;
  1604. info->scenes = NULL;
  1605. }
  1606. if(info->sty_path != NULL)
  1607. {
  1608. mem_free(info->sty_path);
  1609. info->sty_path = NULL;
  1610. }
  1611. if(info->str_path != NULL)
  1612. {
  1613. mem_free(info->str_path);
  1614. info->str_path = NULL;
  1615. }
  1616. if(info->pic_search_param != NULL)
  1617. {
  1618. int i;
  1619. for(i=0;i<(MAX_PARTITION_ID+1);i++)
  1620. {
  1621. if(info->pic_search_param[i].pic_offsets != NULL)
  1622. {
  1623. res_mem_free(RES_MEM_POOL_BMP, info->pic_search_param[i].pic_offsets);
  1624. }
  1625. if(info->pic_search_param[i].compress_size != NULL)
  1626. {
  1627. res_mem_free(RES_MEM_POOL_BMP, info->pic_search_param[i].compress_size);
  1628. }
  1629. }
  1630. res_mem_free(RES_MEM_POOL_BMP, info->pic_search_param);
  1631. info->pic_search_param = NULL;
  1632. }
  1633. mem_free(info);
  1634. }
  1635. resource_scene_t* _get_scene( resource_info_t* info, unsigned int* scene_item, uint32_t id )
  1636. {
  1637. resource_scene_t* scene;
  1638. unsigned int offset;
  1639. unsigned int size;
  1640. //FIXME: scene buffer should differ from bitmap buffer as in space range,
  1641. //so the mutex wont affect each other.
  1642. os_strace_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_1, (uint32_t)id);
  1643. #if RES_MEM_DEBUG
  1644. if(res_mem_check()<0)
  1645. {
  1646. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  1647. res_mem_dump();
  1648. }
  1649. #endif
  1650. offset = *( scene_item + 1 );
  1651. size = *( scene_item + 2 );
  1652. SYS_LOG_INF("sty data %p, scene size %d, offset 0x%x \n", info->sty_data, size, offset);
  1653. scene = ( resource_scene_t* )_try_resource_scene_buffer((uint32_t)info, id);
  1654. if(scene != NULL)
  1655. {
  1656. // SYS_LOG_INF("scene cached buffer 0x%x, size 0x%x", scene, size);
  1657. // SYS_LOG_INF("scene %d, %d, %d %d", scene->x, scene->y, scene->width, scene->height);
  1658. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_1, (uint32_t)id);
  1659. return scene;
  1660. }
  1661. else
  1662. {
  1663. scene = ( resource_scene_t* )_get_resource_scene_buffer((uint32_t)info, id, size);
  1664. if(scene == NULL)
  1665. {
  1666. SYS_LOG_INF("error: cant get scene buffer \n");
  1667. return NULL;
  1668. }
  1669. }
  1670. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_1, (uint32_t)id);
  1671. os_strace_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_2, (uint32_t)id);
  1672. memcpy(scene, info->sty_data+offset, size);
  1673. scene->scene_id = id;
  1674. os_strace_end_call_u32(SYS_TRACE_ID_RES_SCENE_PRELOAD_2, (uint32_t)id);
  1675. // SYS_LOG_INF("scene id 0x%x, scene_id 0x%x\n", id, scene->scene_id);
  1676. // SYS_LOG_INF("scene %d, %d, %d %d", scene->x, scene->y, scene->width, scene->height);
  1677. #if RES_MEM_DEBUG
  1678. if(res_mem_check()<0)
  1679. {
  1680. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  1681. res_mem_dump();
  1682. }
  1683. #endif
  1684. return scene;
  1685. }
  1686. resource_scene_t* res_manager_load_scene(resource_info_t* info, uint32_t scene_id)
  1687. {
  1688. unsigned int i;
  1689. resource_scene_t* scenes;
  1690. if ( info == NULL )
  1691. {
  1692. SYS_LOG_INF("resource info null \n");
  1693. return NULL;
  1694. }
  1695. scenes = info->scenes;
  1696. SYS_LOG_INF("scene id 0x%x, info sum %d \n", scene_id, info->sum);
  1697. for ( i = 0; i < info->sum; i++ )
  1698. {
  1699. //SYS_LOG_INF("scene_item 0x%x\n", scenes[i].scene_id);
  1700. if ( scenes[i].scene_id == scene_id )
  1701. {
  1702. return &scenes[i];
  1703. }
  1704. }
  1705. SYS_LOG_INF("cannot find scene id: 0x%x \n", scene_id);
  1706. return NULL;
  1707. }
  1708. int32_t res_manager_set_pictures_regular(resource_info_t* info, uint32_t scene_id, uint32_t group_id, uint32_t subgroup_id, uint32_t* id, uint32_t num)
  1709. {
  1710. regular_info_t* item;
  1711. uint8_t* offset;
  1712. uint32_t magic;
  1713. magic = scene_id + group_id + subgroup_id;
  1714. item = regular_info_list;
  1715. while(item != NULL)
  1716. {
  1717. if(item->magic == magic && item->num == num)
  1718. {
  1719. //FIXME:we ignore sty_path(aka.source) here for simplicity
  1720. if(memcmp(item->id, id, num*sizeof(uint32_t))==0)
  1721. {
  1722. //got a match
  1723. return 0;
  1724. }
  1725. }
  1726. item = item->next;
  1727. }
  1728. item = (regular_info_t*)res_mem_alloc(RES_MEM_POOL_SCENE, sizeof(regular_info_t)+num*sizeof(uint32_t));
  1729. if(item == NULL)
  1730. {
  1731. SYS_LOG_INF("error: resource buffer not enough for regular info\n");
  1732. res_manager_dump_info();
  1733. return -1;
  1734. }
  1735. memset(item, 0, sizeof(regular_info_t)+num*sizeof(uint32_t));
  1736. item->magic = magic;
  1737. item->scene_id = scene_id;
  1738. offset = (uint8_t*)item;
  1739. offset += sizeof(regular_info_t);
  1740. item->id = (uint32_t*)offset;
  1741. memcpy(item->id, id, num*sizeof(uint32_t));
  1742. item->num = num;
  1743. item->next = regular_info_list;
  1744. regular_info_list = item;
  1745. return 0;
  1746. }
  1747. int32_t res_manager_clear_regular_pictures(const uint8_t* sty_path, uint32_t scene_id)
  1748. {
  1749. regular_info_t* regular = regular_info_list;
  1750. regular_info_t* prev = NULL;
  1751. regular_info_t* found = NULL;
  1752. buf_block_t* item;
  1753. while(regular != NULL)
  1754. {
  1755. if(regular->scene_id == scene_id)
  1756. {
  1757. item = bitmap_buffer.head;
  1758. while(item != NULL)
  1759. {
  1760. //clear regular flag
  1761. if(item->regular_info == (uint32_t)regular)
  1762. {
  1763. item->regular_info = 0;
  1764. }
  1765. item = item->next;
  1766. }
  1767. found = regular;
  1768. regular = found->next;
  1769. if(found == regular_info_list)
  1770. {
  1771. regular_info_list = regular;
  1772. }
  1773. res_mem_free(RES_MEM_POOL_SCENE, found);
  1774. }
  1775. else
  1776. {
  1777. prev = regular;
  1778. regular = regular->next;
  1779. }
  1780. }
  1781. return 0;
  1782. }
  1783. void _unload_scene_bitmaps(uint32_t scene_id)
  1784. {
  1785. compact_buffer_t* buffer = NULL;
  1786. compact_buffer_t* prev_buf= NULL;
  1787. compact_buffer_t* found;
  1788. buf_block_t* item;
  1789. buf_block_t* prev;
  1790. uint8_t* buffer_end;
  1791. SYS_LOG_INF("\n\n ###### _unload_scene_bitmaps 0x%x \n", scene_id);
  1792. if(scene_id == 0)
  1793. {
  1794. return;
  1795. }
  1796. os_mutex_lock(&bitmap_cache_mutex, OS_FOREVER);
  1797. buffer = bitmap_buffer.compact_buffer_list;
  1798. while(buffer != NULL)
  1799. {
  1800. if(buffer->scene_id == scene_id)
  1801. {
  1802. //check regular
  1803. item = (buf_block_t*)buffer->addr;
  1804. if(buffer->offset != 0 && item->regular_info != 0)
  1805. {
  1806. prev_buf = buffer;
  1807. buffer = buffer->next;
  1808. continue;
  1809. }
  1810. if(buffer->offset == 0 && buffer->free_size == sizeof(buf_block_t))
  1811. {
  1812. //pseudo nodes
  1813. SYS_LOG_INF("empty preload scene 0x%x\n", buffer->scene_id);
  1814. }
  1815. else
  1816. {
  1817. // SYS_LOG_INF("testalloc compact unload 0x%x, buffer %p, addr %p\n", scene_id, buffer, buffer->addr);
  1818. //clear buffer nodes from bitmap list
  1819. buffer_end = buffer->addr + buffer->offset -1;
  1820. item = bitmap_buffer.head;
  1821. prev = bitmap_buffer.head;
  1822. while(item != NULL)
  1823. {
  1824. if(((void*)item >= (void*)buffer->addr) && ((void*)item <= (void*)buffer_end))
  1825. {
  1826. if(item->size == sizeof(buf_block_t) && item->addr != NULL)
  1827. {
  1828. SYS_LOG_DBG("%d ui mem free %d\n", __LINE__, item->id);
  1829. res_mem_free_block(item->addr);
  1830. ui_mem_total--;
  1831. }
  1832. if(item == bitmap_buffer.head)
  1833. {
  1834. bitmap_buffer.head = item->next;
  1835. }
  1836. else
  1837. {
  1838. prev->next = item->next;
  1839. }
  1840. }
  1841. else
  1842. {
  1843. prev = item;
  1844. }
  1845. item = item->next;
  1846. }
  1847. }
  1848. //release scene data
  1849. if(buffer == bitmap_buffer.compact_buffer_list)
  1850. {
  1851. bitmap_buffer.compact_buffer_list = buffer->next;
  1852. }
  1853. else
  1854. {
  1855. prev_buf->next = buffer->next;
  1856. }
  1857. found = buffer;
  1858. buffer = found->next;
  1859. if(found->free_size + found->offset == screen_bitmap_size)
  1860. {
  1861. res_mem_free_block(found->addr);
  1862. ui_mem_total--;
  1863. }
  1864. else
  1865. {
  1866. res_mem_free(RES_MEM_POOL_BMP, found->addr);
  1867. }
  1868. #if RES_MEM_DEBUG
  1869. if(res_mem_check()<0)
  1870. {
  1871. SYS_LOG_INF("%s %d found addr 0x%x\n", __FILE__, __LINE__, found->addr);
  1872. res_mem_dump();
  1873. }
  1874. #endif
  1875. res_array_free(found);
  1876. }
  1877. else
  1878. {
  1879. prev_buf = buffer;
  1880. buffer = buffer->next;
  1881. }
  1882. }
  1883. SYS_LOG_INF("exit\n");
  1884. os_mutex_unlock(&bitmap_cache_mutex);
  1885. }
  1886. void _unload_scene(uint32_t id, resource_scene_t* scene)
  1887. {
  1888. _unload_scene_bitmaps(id);
  1889. _free_resource_scene_buffer(scene);
  1890. }
  1891. void res_manager_unload_scene(uint32_t id, resource_scene_t* scene)
  1892. {
  1893. if(scene != NULL)
  1894. {
  1895. _unload_scene(id, scene);
  1896. }
  1897. else if(id > 0)
  1898. {
  1899. _unload_scene_bitmaps(id);
  1900. }
  1901. }
  1902. void _unload_bitmap(resource_bitmap_t* bitmap)
  1903. {
  1904. if(bitmap == NULL)
  1905. {
  1906. SYS_LOG_INF("%s %d: error: unload null resource \n", __func__, __LINE__);
  1907. return;
  1908. }
  1909. os_mutex_lock(&bitmap_cache_mutex, OS_FOREVER);
  1910. if(bitmap->regular_info == 0 && bitmap->buffer != NULL)
  1911. {
  1912. _free_resource_bitmap_buffer(bitmap->buffer);
  1913. }
  1914. os_mutex_unlock(&bitmap_cache_mutex);
  1915. }
  1916. void _unload_string(resource_text_t* text)
  1917. {
  1918. if(text == NULL)
  1919. {
  1920. SYS_LOG_INF("%s %d: error: unload null resource \n", __func__, __LINE__);
  1921. return;
  1922. }
  1923. _free_resource_text_buffer(text->buffer);
  1924. }
  1925. static int _search_res_id_in_files(resource_info_t* info, resource_bitmap_t* bitmap, uint32_t* bmp_pos, uint32_t* compress_size)
  1926. {
  1927. int low, high, mid;
  1928. uint32_t id = bitmap->sty_data->id;
  1929. if(info->pic_search_param == NULL)
  1930. {
  1931. return -1;
  1932. }
  1933. low = 0;
  1934. high = info->pic_search_max_volume-1;
  1935. mid = (info->pic_search_max_volume-1)/2;
  1936. //SYS_LOG_INF("id %d, start %d, end %d\n", id, info->pic_search_param[mid].id_start, info->pic_search_param[mid].id_end);
  1937. while(id < info->pic_search_param[mid].id_start || id > info->pic_search_param[mid].id_end)
  1938. {
  1939. if(id < info->pic_search_param[mid].id_start)
  1940. {
  1941. high = mid - 1;
  1942. }
  1943. else
  1944. {
  1945. low = mid + 1;
  1946. }
  1947. if(low > high)
  1948. {
  1949. //not found
  1950. return -1;
  1951. }
  1952. mid = (low+high)/2;
  1953. }
  1954. id -= info->pic_search_param[mid].id_start;
  1955. *bmp_pos = info->pic_search_param[mid].pic_offsets[id];
  1956. *compress_size = info->pic_search_param[mid].compress_size[id];
  1957. return mid;
  1958. }
  1959. int32_t _load_bitmap(resource_info_t* info, resource_bitmap_t* bitmap, uint32_t force_ref)
  1960. {
  1961. int32_t ret;
  1962. int32_t bmp_size;
  1963. int32_t compress_size = 0;
  1964. uint8_t *compress_buf = NULL;
  1965. struct fs_file_t* pic_fp;
  1966. uint32_t bmp_pos = 0;
  1967. uint8_t jpeg_out_format = HAL_JPEG_OUT_RGB565;
  1968. if((info == NULL) || (bitmap == NULL))
  1969. {
  1970. SYS_LOG_INF("error: invalid param to load bitmap \n");
  1971. return -1;
  1972. }
  1973. os_strace_u32(SYS_TRACE_ID_RES_BMP_LOAD_0, (uint32_t)bitmap->sty_data->id);
  1974. os_mutex_lock(&bitmap_cache_mutex, OS_FOREVER);
  1975. if(_get_resource_bitmap_map(info, bitmap) == 0)
  1976. {
  1977. //bitmap directly mapped from file
  1978. os_mutex_unlock(&bitmap_cache_mutex);
  1979. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_0, (uint32_t)bitmap->sty_data->id);
  1980. return 0;
  1981. }
  1982. #if RES_MEM_DEBUG
  1983. if(res_mem_check()<0)
  1984. {
  1985. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  1986. res_mem_dump();
  1987. }
  1988. #endif
  1989. //bitmap->buffer = _try_resource_bitmap_buffer((uint32_t)info, bitmap->id, &bitmap->width, &bitmap->height, &bitmap->bytes_per_pixel, &bitmap->format, force_ref);
  1990. bitmap->buffer = _try_resource_bitmap_buffer((uint32_t)info, bitmap, force_ref);
  1991. if(bitmap->buffer != NULL)
  1992. {
  1993. //got bitmap from bitmap cache
  1994. os_mutex_unlock(&bitmap_cache_mutex);
  1995. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_0, (uint32_t)bitmap->sty_data->id);
  1996. return 0;
  1997. }
  1998. if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_JPEG) {
  1999. #ifdef CONFIG_LV_COLOR_DEPTH_32
  2000. bitmap->sty_data->bytes_per_pixel = 3;
  2001. jpeg_out_format = HAL_JPEG_OUT_RGB888;
  2002. #else
  2003. bitmap->sty_data->bytes_per_pixel = 2;
  2004. jpeg_out_format = HAL_JPEG_OUT_RGB565;
  2005. #endif
  2006. }
  2007. if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_ETC2_EAC) {
  2008. bmp_size = ((bitmap->sty_data->width + 0x3) & ~0x3) *
  2009. ((bitmap->sty_data->height + 0x3) & ~0x3);
  2010. } else if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX8) {
  2011. bmp_size = bitmap->sty_data->width * bitmap->sty_data->height + 1024 + 4;
  2012. } else if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_LVGL_INDEX8) {
  2013. bmp_size = bitmap->sty_data->width * bitmap->sty_data->height + 1024;
  2014. } else if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX4) {
  2015. bmp_size = ((bitmap->sty_data->width + 0x1) >> 1) * bitmap->sty_data->height + 64;
  2016. } else {
  2017. bmp_size = bitmap->sty_data->width * bitmap->sty_data->height *
  2018. bitmap->sty_data->bytes_per_pixel;
  2019. }
  2020. bitmap->buffer = _get_resource_bitmap_buffer((uint32_t)info, bitmap, bmp_size, force_ref);
  2021. if(bitmap->buffer == NULL)
  2022. {
  2023. SYS_LOG_INF("error: no buffer to load bitmap \n");
  2024. os_mutex_unlock(&bitmap_cache_mutex);
  2025. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_0, (uint32_t)bitmap->sty_data->id);
  2026. return -1;
  2027. }
  2028. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_0, (uint32_t)bitmap->sty_data->id);
  2029. if(res_debug_load_bitmap_is_on())
  2030. {
  2031. SYS_LOG_INF("styid 0x%x, id %d, wh %d, %d, addr %p\n", bitmap->sty_data->sty_id, bitmap->sty_data->id,
  2032. bitmap->sty_data->width, bitmap->sty_data->height, bitmap->buffer);
  2033. }
  2034. os_strace_u32(SYS_TRACE_ID_RES_BMP_LOAD_1, (uint32_t)bitmap->sty_data->id);
  2035. ret = _search_res_id_in_files(info, bitmap, &bmp_pos, &compress_size);
  2036. if(ret <= 0)
  2037. {
  2038. pic_fp = &info->pic_fp;
  2039. }
  2040. else
  2041. {
  2042. pic_fp = &info->pic_search_param[ret].res_fp;
  2043. }
  2044. // SYS_LOG_INF("\n pres_entry length %d, type %d, offset 0x%x \n", pres_entry->length, pres_entry->type, pres_entry->offset);
  2045. #if !defined(CONFIG_RES_MANAGER_USE_STYLE_MMAP)||defined(CONFIG_SIMULATOR)
  2046. fs_seek(pic_fp, bmp_pos, FS_SEEK_SET);
  2047. #endif
  2048. //compress_size = bitmap->sty_data->compress_size;
  2049. if (compress_size > 0)
  2050. {
  2051. #if !defined(CONFIG_RES_MANAGER_USE_STYLE_MMAP)||defined(CONFIG_SIMULATOR)
  2052. compress_buf = (uint8_t*)res_mem_alloc(RES_MEM_POOL_BMP, compress_size);
  2053. if(compress_buf == NULL)
  2054. {
  2055. SYS_LOG_INF("error: no buffer to load compressed bitmap \n");
  2056. os_mutex_unlock(&bitmap_cache_mutex);
  2057. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_1, (uint32_t)bitmap->sty_data->id);
  2058. res_manager_dump_info();
  2059. return -1;
  2060. }
  2061. ret = fs_read(pic_fp, compress_buf, compress_size);
  2062. if(ret < compress_size)
  2063. {
  2064. SYS_LOG_INF("bitmap read error %d\n", ret);
  2065. }
  2066. #else
  2067. compress_buf = info->pic_res_mmap_addr + bmp_pos;
  2068. #endif
  2069. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_1, (uint32_t)bitmap->sty_data->id);
  2070. os_strace_u32(SYS_TRACE_ID_RES_BMP_LOAD_2, (uint32_t)bitmap->sty_data->id);
  2071. if(ret == compress_size)
  2072. {
  2073. #ifndef CONFIG_LVGL_USE_IMG_DECODER_ACTS
  2074. #ifdef CONFIG_JPEG_HAL
  2075. if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_JPEG) {
  2076. ret = jpg_decode(compress_buf, compress_size, bitmap->buffer, jpeg_out_format, bitmap->sty_data->width, 0, 0, bitmap->sty_data->width, bitmap->sty_data->height);
  2077. }
  2078. else
  2079. #endif
  2080. #endif
  2081. {
  2082. #ifndef CONFIG_SIMULATOR
  2083. ret = p_brom_misc_api->p_decompress(compress_buf, bitmap->buffer, compress_size, bmp_size);
  2084. #else
  2085. ret = LZ4_decompress_safe(compress_buf, bitmap->buffer, compress_size, bmp_size);
  2086. #endif
  2087. }
  2088. }
  2089. if ((bitmap->sty_data->format != RESOURCE_BITMAP_FORMAT_INDEX8 && ret < bmp_size) ||
  2090. (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX8 && ret <= bitmap->sty_data->width * bitmap->sty_data->height + 4)){
  2091. SYS_LOG_INF("bmp dump pos for %s: 0x%x\n", info->sty_path, bmp_pos);
  2092. for (int i = 0; i < 4; i++) {
  2093. SYS_LOG_INF("%d: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", i, compress_buf[8*i], compress_buf[8*i+1], compress_buf[8*i+2],
  2094. compress_buf[8*i+3], compress_buf[8*i+4], compress_buf[8*i+5], compress_buf[8*i+6], compress_buf[8*i+7]);
  2095. }
  2096. } else {
  2097. ret = bmp_size; /* for RESOURCE_BITMAP_FORMAT_INDEX8 */
  2098. }
  2099. #if !defined(CONFIG_RES_MANAGER_USE_STYLE_MMAP)||defined(CONFIG_SIMULATOR)
  2100. res_mem_free(RES_MEM_POOL_BMP, compress_buf);
  2101. #endif
  2102. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_2, (uint32_t)bitmap->sty_data->id);
  2103. }
  2104. else
  2105. {
  2106. ret = fs_read(pic_fp, bitmap->buffer, bmp_size);
  2107. os_strace_end_call_u32(SYS_TRACE_ID_RES_BMP_LOAD_1, (uint32_t)bitmap->sty_data->id);
  2108. }
  2109. if(ret < bmp_size)
  2110. {
  2111. SYS_LOG_INF("bitmap load error %d, compress_buf %p, compress_size %d, styid 0x%x, id %d, w %d, h %d, bmp_size %d, buffer %p, bmp_pos 0x%x\n",
  2112. ret, compress_buf, compress_size, bitmap->sty_data->sty_id, bitmap->sty_data->id, bitmap->sty_data->width, bitmap->sty_data->height,
  2113. bmp_size, bitmap->buffer, bmp_pos);
  2114. }
  2115. mem_dcache_clean(bitmap->buffer, bmp_size);
  2116. #if RES_MEM_DEBUG
  2117. if(res_mem_check()<0)
  2118. {
  2119. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  2120. res_mem_dump();
  2121. }
  2122. #endif
  2123. os_mutex_unlock(&bitmap_cache_mutex);
  2124. return 0;
  2125. }
  2126. int _load_string(resource_info_t* info, resource_text_t* text)
  2127. {
  2128. int ret;
  2129. res_head_t res_head;
  2130. res_entry_t res_entry;
  2131. if((info == NULL) || (text == NULL))
  2132. {
  2133. SYS_LOG_INF("error: invalid param to load text \n");
  2134. return -1;
  2135. }
  2136. text->buffer = _try_resource_text_buffer((uint32_t)info, text->sty_data->id);
  2137. if(text->buffer != NULL)
  2138. {
  2139. //got string buffer from cache
  2140. return 0;
  2141. }
  2142. fs_seek(&info->text_fp, 0, FS_SEEK_SET);
  2143. memset(&res_head, 0, sizeof(res_head));
  2144. ret = fs_read(&info->text_fp, &res_head, sizeof(res_head));
  2145. if (ret < sizeof(res_head) || res_head.version != 2)
  2146. {
  2147. //support utf8 only for now
  2148. SYS_LOG_INF("error: unsupported encoding %d\n", res_head.version);
  2149. return -1;
  2150. }
  2151. memset(&res_entry, 0, sizeof(res_entry_t));
  2152. fs_seek(&info->text_fp,(int)(text->sty_data->id * (int)sizeof(res_entry_t)) , FS_SEEK_SET);
  2153. ret = fs_read(&info->text_fp, &res_entry, sizeof(res_entry_t));
  2154. if(ret < sizeof(res_entry_t))
  2155. {
  2156. SYS_LOG_INF("error: cannot find string resource in %s, id %d\n", info->str_path, text->sty_data->id);
  2157. return -1;
  2158. }
  2159. // SYS_LOG_INF("offset %d, length %d", res_entry.offset, res_entry.length);
  2160. fs_seek(&info->text_fp, res_entry.offset, FS_SEEK_SET);
  2161. text->buffer = _get_resource_text_buffer((uint32_t)info, text->sty_data->id, res_entry.length+1);
  2162. if(text->buffer == NULL)
  2163. {
  2164. SYS_LOG_INF("error: cannot get text buffer for %s : id %d, offset %d, length %d\n", info->str_path, text->sty_data->id, res_entry.offset, res_entry.length);
  2165. return -1;
  2166. }
  2167. // SYS_LOG_INF("text buffer 0x%x\n", text->buffer);
  2168. memset(text->buffer, 0, res_entry.length+1);
  2169. ret = fs_read(&info->text_fp, text->buffer, res_entry.length);
  2170. if(ret < res_entry.length)
  2171. {
  2172. SYS_LOG_INF("error: cannot read string in %s for resource id %d, offset %d, length %d\n", info->str_path, text->sty_data->id, res_entry.offset, res_entry.length);
  2173. return -1;
  2174. }
  2175. return 0;
  2176. }
  2177. uint32_t _check_resource_is_regular(uint32_t res_magic, uint32_t res_sty_id)
  2178. {
  2179. regular_info_t* regular = regular_info_list;
  2180. uint32_t i;
  2181. while(regular != NULL)
  2182. {
  2183. if(regular->magic+res_sty_id == res_magic)
  2184. {
  2185. uint32_t* id = regular->id;
  2186. for(i=0;i<regular->num;i++)
  2187. {
  2188. if(res_sty_id == id[i])
  2189. {
  2190. return (uint32_t)regular;
  2191. }
  2192. }
  2193. }
  2194. regular = regular->next;
  2195. }
  2196. return 0;
  2197. }
  2198. void* res_manager_get_scene_child(resource_info_t* res_info, resource_scene_t* scene, uint32_t id)
  2199. {
  2200. uint32_t i;
  2201. resource_t* resource;
  2202. char* buf = NULL; //地址不对齐,不能用数据类型指针。
  2203. int ret;
  2204. if ( scene == NULL )
  2205. {
  2206. SYS_LOG_INF("null scene \n");
  2207. return NULL;
  2208. }
  2209. buf = (char*)res_info->sty_data + scene->child_offset;
  2210. resource = (resource_t*)buf;
  2211. // SYS_LOG_INF("resource sum %d, child offset %d, LF 0x%x\n", scene->resource_sum, scene->child_offset, id);
  2212. for ( i = 0; i < scene->resource_sum; i++ )
  2213. {
  2214. //SYS_LOG_INF("resource id 0x%x, offset 0x%x",resource->id, resource->offset);
  2215. if ( resource->id == id )
  2216. {
  2217. break;
  2218. }
  2219. buf = res_info->sty_data + resource->offset;
  2220. resource = (resource_t*)buf;
  2221. }
  2222. if(i >= scene->resource_sum)
  2223. {
  2224. SYS_LOG_INF("%s %d: error: cannot find resource 0x%x \n", __func__, __LINE__, id);
  2225. return NULL;
  2226. }
  2227. // SYS_LOG_INF("resource->type 0x%x", resource.type);
  2228. if(resource->type == RESOURCE_TYPE_GROUP)
  2229. {
  2230. //buf is now on group resource start
  2231. resource_group_t* res = (resource_group_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_group_t));
  2232. if(res == NULL)
  2233. {
  2234. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2235. return NULL;
  2236. }
  2237. sty_group_t* group = (sty_group_t*)resource;
  2238. res->sty_data = group;
  2239. return res;
  2240. }
  2241. if(resource->type == RESOURCE_TYPE_PICTURE)
  2242. {
  2243. //buf is now on resource start
  2244. resource_bitmap_t* res = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2245. if(res == NULL)
  2246. {
  2247. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2248. return NULL;
  2249. }
  2250. memset(res, 0, sizeof(resource_bitmap_t));
  2251. sty_picture_t* pic = (sty_picture_t*)resource;
  2252. res->sty_data = pic;
  2253. res->regular_info = 0;
  2254. // SYS_LOG_INF("pic w %d, h %d, id 0x%x", res->width, res->height, res->id);
  2255. ret = _load_bitmap(res_info, res, 1);
  2256. if(ret < 0)
  2257. {
  2258. SYS_LOG_INF("%s %d: get scene child bitmap faild\n", __func__, __LINE__);
  2259. res_manager_release_resource(res);
  2260. res = NULL;
  2261. }
  2262. return res;
  2263. }
  2264. if(resource->type == RESOURCE_TYPE_TEXT)
  2265. {
  2266. //buf is now on resource start
  2267. resource_text_t* res = (resource_text_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_text_t));
  2268. if(res == NULL)
  2269. {
  2270. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2271. return NULL;
  2272. }
  2273. memset(res, 0, sizeof(resource_text_t));
  2274. sty_string_t* str = (sty_string_t*)resource;
  2275. res->sty_data = str;
  2276. // SYS_LOG_INF("str size %d, id 0x%x", res->font_size, res->id);
  2277. ret = _load_string(res_info, res);
  2278. if(ret < 0)
  2279. {
  2280. res_manager_release_resource(res);
  2281. res = NULL;
  2282. }
  2283. return res;
  2284. }
  2285. if(resource->type == RESOURCE_TYPE_PICREGION)
  2286. {
  2287. resource_picregion_t* res = (resource_picregion_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_picregion_t));
  2288. if(res == NULL)
  2289. {
  2290. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2291. return NULL;
  2292. }
  2293. sty_picregion_t* picreg = (sty_picregion_t*)resource;
  2294. res->sty_data = picreg;
  2295. res->regular_info = 0;
  2296. return res;
  2297. }
  2298. return NULL;
  2299. }
  2300. void* res_manager_get_group_child(resource_info_t* res_info, resource_group_t* resgroup, uint32_t id )
  2301. {
  2302. unsigned int i;
  2303. resource_t* resource;
  2304. char*buf;
  2305. int ret;
  2306. if ( resgroup == NULL )
  2307. {
  2308. SYS_LOG_INF("invalid group \n");
  2309. return NULL;
  2310. }
  2311. buf = (char*)res_info->sty_data + resgroup->sty_data->child_offset;
  2312. resource = (resource_t*)buf;
  2313. // SYS_LOG_INF("resource sum %d, search id 0x%x\n", resgroup->sty_data->resource_sum, id);
  2314. for ( i = 0; i < resgroup->sty_data->resource_sum; i++ )
  2315. {
  2316. // SYS_LOG_INF("\n ### resource id 0x%x\n", resource->id);
  2317. if ( resource->id == id )
  2318. {
  2319. break;
  2320. }
  2321. buf = res_info->sty_data + resource->offset;
  2322. resource = (resource_t*)buf;
  2323. }
  2324. if(i >= resgroup->sty_data->resource_sum)
  2325. {
  2326. SYS_LOG_INF("%s %d: cant find resource 0x%x \n", __func__, __LINE__, id);
  2327. return NULL;
  2328. }
  2329. // SYS_LOG_INF("resource.type 0x%x", resource.type);
  2330. if(resource->type == RESOURCE_TYPE_GROUP)
  2331. {
  2332. //buf is now on group resource start
  2333. resource_group_t* res = (resource_group_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_group_t));
  2334. if(res == NULL)
  2335. {
  2336. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2337. return NULL;
  2338. }
  2339. memset(res, 0, sizeof(resource_group_t));
  2340. sty_group_t* group = (sty_group_t*)resource;
  2341. res->sty_data = group;
  2342. return res;
  2343. }
  2344. if(resource->type == RESOURCE_TYPE_PICTURE)
  2345. {
  2346. //buf is now on resource start
  2347. resource_bitmap_t* res = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2348. if(res == NULL)
  2349. {
  2350. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2351. return NULL;
  2352. }
  2353. memset(res, 0, sizeof(resource_bitmap_t));
  2354. sty_picture_t* pic = (sty_picture_t*)resource;
  2355. res->sty_data = pic;
  2356. // SYS_LOG_INF("pic w %d, h %d, id 0x%x", res->width, res->height, res->id);
  2357. res->regular_info = 0;
  2358. ret = _load_bitmap(res_info, res, 1);
  2359. if(ret < 0)
  2360. {
  2361. SYS_LOG_INF("%s %d: get group child bitmap faild\n", __func__, __LINE__);
  2362. res_manager_release_resource(res);
  2363. res = NULL;
  2364. }
  2365. return res;
  2366. }
  2367. if(resource->type == RESOURCE_TYPE_TEXT)
  2368. {
  2369. //buf is now on resource start
  2370. resource_text_t* res = (resource_text_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_text_t));
  2371. if(res == NULL)
  2372. {
  2373. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2374. return NULL;
  2375. }
  2376. memset(res, 0, sizeof(resource_text_t));
  2377. sty_string_t* str = (sty_string_t*)resource;
  2378. res->sty_data = str;
  2379. // SYS_LOG_INF("str size %d, id 0x%x", res->font_size, res->id);
  2380. ret = _load_string(res_info, res);
  2381. if(ret < 0)
  2382. {
  2383. res_manager_release_resource(res);
  2384. res = NULL;
  2385. }
  2386. return res;
  2387. }
  2388. if(resource->type == RESOURCE_TYPE_PICREGION)
  2389. {
  2390. resource_picregion_t* res = (resource_picregion_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_picregion_t));
  2391. if(res == NULL)
  2392. {
  2393. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2394. return NULL;
  2395. }
  2396. sty_picregion_t* picreg = (sty_picregion_t*)resource;
  2397. res->sty_data = picreg;
  2398. res->regular_info = 0;
  2399. return res;
  2400. }
  2401. return NULL;
  2402. }
  2403. resource_bitmap_t* res_manager_load_frame_from_picregion(resource_info_t* info, resource_picregion_t* picreg, uint32_t frame)
  2404. {
  2405. resource_bitmap_t* bitmap;
  2406. uint8_t* buf;
  2407. int32_t ret;
  2408. if(info == NULL || picreg == NULL)
  2409. {
  2410. SYS_LOG_INF("invalid picreg \n");
  2411. return NULL;
  2412. }
  2413. bitmap = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2414. if(bitmap == NULL)
  2415. {
  2416. SYS_LOG_INF("no space for frame loading \n");
  2417. return NULL;
  2418. }
  2419. buf = info->sty_data + picreg->sty_data->id_offset + sizeof(sty_picture_t)*frame;
  2420. bitmap->sty_data = (sty_picture_t*)buf;
  2421. bitmap->regular_info = picreg->regular_info;
  2422. ret = _load_bitmap(info, bitmap, 1);
  2423. if(ret < 0)
  2424. {
  2425. SYS_LOG_INF("picregion load frame faild \n");
  2426. res_manager_release_resource(bitmap);
  2427. bitmap = NULL;
  2428. }
  2429. return bitmap;
  2430. }
  2431. void res_manager_release_resource(void* resource)
  2432. {
  2433. uint32_t type = *((uint32_t*)(*((uint32_t*)(resource))));
  2434. if(type == RESOURCE_TYPE_GROUP)
  2435. {
  2436. res_array_free(resource);
  2437. }
  2438. else if(type == RESOURCE_TYPE_PICTURE)
  2439. {
  2440. resource_bitmap_t* bitmap = (resource_bitmap_t*)resource;
  2441. _unload_bitmap(bitmap);
  2442. res_array_free(resource);
  2443. }
  2444. else if(type == RESOURCE_TYPE_TEXT)
  2445. {
  2446. resource_text_t* text = (resource_text_t*)resource;
  2447. _unload_string(text);
  2448. res_array_free(resource);
  2449. }
  2450. else if(type == RESOURCE_TYPE_PICREGION)
  2451. {
  2452. resource_picregion_t* picreg = (resource_picregion_t*)resource;
  2453. res_array_free(picreg);
  2454. }
  2455. }
  2456. void res_manager_free_resource_structure(void* resource)
  2457. {
  2458. if(resource != NULL)
  2459. {
  2460. res_array_free(resource);
  2461. }
  2462. }
  2463. void res_manager_free_bitmap_data(void* data)
  2464. {
  2465. if(data != NULL)
  2466. {
  2467. os_mutex_lock(&bitmap_cache_mutex, OS_FOREVER);
  2468. _free_resource_bitmap_buffer(data);
  2469. os_mutex_unlock(&bitmap_cache_mutex);
  2470. }
  2471. }
  2472. void res_manager_free_text_data(void* data)
  2473. {
  2474. if(data != NULL)
  2475. {
  2476. _free_resource_text_buffer(data);
  2477. }
  2478. }
  2479. int32_t res_manager_preload_bitmap(resource_info_t* res_info, resource_bitmap_t* bitmap)
  2480. {
  2481. int32_t ret;
  2482. ret = _load_bitmap(res_info, bitmap, 0);
  2483. if(ret < 0)
  2484. {
  2485. SYS_LOG_INF("preload bitmap error 0x%x, %p\n", bitmap->sty_data->id, bitmap->sty_data);
  2486. return -1;
  2487. }
  2488. else
  2489. {
  2490. //SYS_LOG_INF("preload success 0x%x\n", bitmap->sty_data->id);
  2491. return 0;
  2492. }
  2493. }
  2494. int32_t res_manager_preload_bitmap_compact(uint32_t scene_id, resource_info_t* res_info, resource_bitmap_t* bitmap)
  2495. {
  2496. int32_t ret;
  2497. // SYS_LOG_INF("\n res_manager_preload_bitmap_compact scene_id 0x%x\n", scene_id);
  2498. ret = _load_bitmap(res_info, bitmap, scene_id);
  2499. if(ret < 0)
  2500. {
  2501. SYS_LOG_INF("preload bitmap compact error scene 0x%x, 0x%x\n", scene_id, bitmap->sty_data->id);
  2502. return -1;
  2503. }
  2504. else
  2505. {
  2506. //SYS_LOG_INF("preload success 0x%x\n", bitmap->sty_data->id);
  2507. return 0;
  2508. }
  2509. }
  2510. int32_t res_manager_load_bitmap(resource_info_t* res_info, resource_bitmap_t* bitmap)
  2511. {
  2512. int32_t ret;
  2513. ret = _load_bitmap(res_info, bitmap, 1);
  2514. if(ret < 0)
  2515. {
  2516. SYS_LOG_INF("load bitmap error 0x%x\n", bitmap->sty_data->sty_id);
  2517. return -1;
  2518. }
  2519. else
  2520. {
  2521. return 0;
  2522. }
  2523. }
  2524. uint32_t res_manager_get_bitmap_buf_block_unit_size(void)
  2525. {
  2526. return sizeof(buf_block_t);
  2527. }
  2528. int32_t res_manager_init_compact_buffer(uint32_t scene_id, size_t size)
  2529. {
  2530. compact_buffer_t* ret;
  2531. os_mutex_lock(&bitmap_cache_mutex, OS_FOREVER);
  2532. ret = _init_compact_buffer(scene_id, size);
  2533. if(ret == NULL)
  2534. {
  2535. SYS_LOG_INF("alloc compact buffer failed: 0x%x, %d\n", scene_id, size);
  2536. os_mutex_unlock(&bitmap_cache_mutex);
  2537. return -1;
  2538. }
  2539. os_mutex_unlock(&bitmap_cache_mutex);
  2540. return 0;
  2541. }
  2542. void* res_manager_preload_from_scene(resource_info_t* res_info, resource_scene_t* scene, uint32_t id)
  2543. {
  2544. uint32_t i;
  2545. resource_t* resource;
  2546. char* buf = NULL; //地址不对齐,不能用数据类型指针。
  2547. if ( scene == NULL )
  2548. {
  2549. SYS_LOG_INF("%s %d: null scene to preload \n", __func__, __LINE__);
  2550. return NULL;
  2551. }
  2552. buf = (char*)res_info->sty_data + scene->child_offset;
  2553. resource = (resource_t*)buf;
  2554. // SYS_LOG_INF("resource sum %d, child offset %d", scene->resource_sum, scene->child_offset);
  2555. for ( i = 0; i < scene->resource_sum; i++ )
  2556. {
  2557. // SYS_LOG_INF("resource id 0x%x, offset 0x%x",resource.id, resource.offset);
  2558. if ( resource->id == id )
  2559. {
  2560. break;
  2561. }
  2562. buf = res_info->sty_data + resource->offset;
  2563. resource = (resource_t*)buf;
  2564. }
  2565. if(i >= scene->resource_sum)
  2566. {
  2567. SYS_LOG_INF("%s %d: error: cannot find resource 0x%x in scene 0x%x\n", __func__, __LINE__, id, scene->scene_id);
  2568. return NULL;
  2569. }
  2570. if(resource->type == RESOURCE_TYPE_GROUP)
  2571. {
  2572. //buf is now on group resource start
  2573. resource_group_t* res = (resource_group_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_group_t));
  2574. if(res == NULL)
  2575. {
  2576. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2577. return NULL;
  2578. }
  2579. sty_group_t* group = (sty_group_t*)resource;
  2580. res->sty_data = group;
  2581. return res;
  2582. }
  2583. if(resource->type == RESOURCE_TYPE_PICREGION)
  2584. {
  2585. resource_picregion_t* res = (resource_picregion_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_picregion_t));
  2586. if(res == NULL)
  2587. {
  2588. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2589. return NULL;
  2590. }
  2591. sty_picregion_t* picreg = (sty_picregion_t*)resource;
  2592. res->sty_data = picreg;
  2593. res->regular_info = _check_resource_is_regular(picreg->magic, picreg->sty_id);
  2594. return res;
  2595. }
  2596. // SYS_LOG_INF("resource->type 0x%x", resource.type);
  2597. if(resource->type == RESOURCE_TYPE_PICTURE)
  2598. {
  2599. //buf is now on resource start
  2600. resource_bitmap_t* res = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2601. if(res == NULL)
  2602. {
  2603. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2604. return NULL;
  2605. }
  2606. memset(res, 0, sizeof(resource_bitmap_t));
  2607. sty_picture_t* pic = (sty_picture_t*)resource;
  2608. res->sty_data = pic;
  2609. res->regular_info = _check_resource_is_regular(pic->magic, pic->sty_id);
  2610. // SYS_LOG_INF("scene 0x%x, id 0x%x, regular 0x%x", scene->scene_id, id, res->regular_info);
  2611. #if RES_MEM_DEBUG
  2612. if(res_mem_check()<0)
  2613. {
  2614. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  2615. res_mem_dump();
  2616. }
  2617. #endif
  2618. return res;
  2619. }
  2620. // SYS_LOG_INF("wrong type to preload");
  2621. return NULL;
  2622. }
  2623. resource_bitmap_t* res_manager_preload_from_group(resource_info_t* res_info, resource_group_t* group, uint32_t scene_id, uint32_t id)
  2624. {
  2625. uint32_t i;
  2626. resource_t* resource;
  2627. char* buf = NULL; //地址不对齐,不能用数据类型指针。
  2628. if ( group == NULL )
  2629. {
  2630. SYS_LOG_INF("%s %d: null scene to preload \n", __func__, __LINE__);
  2631. return NULL;
  2632. }
  2633. buf = (char*)res_info->sty_data + group->sty_data->child_offset;
  2634. resource = (resource_t*)buf;
  2635. // SYS_LOG_INF("resource sum %d, search id 0x%x\n", resgroup->resource_sum, id);
  2636. for ( i = 0; i < group->sty_data->resource_sum; i++ )
  2637. {
  2638. // SYS_LOG_INF("\n ### resource id 0x%x\n", resource->id);
  2639. if ( resource->id == id )
  2640. {
  2641. break;
  2642. }
  2643. buf = res_info->sty_data + resource->offset;
  2644. resource = (resource_t*)buf;
  2645. }
  2646. if(i >= group->sty_data->resource_sum)
  2647. {
  2648. SYS_LOG_INF("%s %d: cant find resource 0x%x \n", __func__, __LINE__, id);
  2649. return NULL;
  2650. }
  2651. // SYS_LOG_INF("resource->type 0x%x", resource.type);
  2652. if(resource->type == RESOURCE_TYPE_PICTURE)
  2653. {
  2654. //buf is now on resource start
  2655. resource_bitmap_t* res = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2656. if(res == NULL)
  2657. {
  2658. SYS_LOG_INF("%s %d: error: no space for resource to preload\n", __func__, __LINE__);
  2659. return NULL;
  2660. }
  2661. memset(res, 0, sizeof(resource_bitmap_t));
  2662. sty_picture_t* pic = (sty_picture_t*)resource;
  2663. res->sty_data = pic;
  2664. res->regular_info = _check_resource_is_regular(pic->magic, pic->sty_id);
  2665. // SYS_LOG_INF("scene 0x%x, id 0x%x, regular 0x%x", scene_id, id, res->regular_info);
  2666. #if RES_MEM_DEBUG
  2667. if(res_mem_check()<0)
  2668. {
  2669. SYS_LOG_INF("%s %d\n", __FILE__, __LINE__);
  2670. res_mem_dump();
  2671. }
  2672. #endif
  2673. return res;
  2674. }
  2675. // SYS_LOG_INF("wrong type to preload");
  2676. return NULL;
  2677. }
  2678. resource_bitmap_t* res_manager_preload_from_picregion(resource_info_t* res_info, resource_picregion_t* picreg, uint32_t frame)
  2679. {
  2680. resource_bitmap_t* bitmap;
  2681. uint8_t* buf;
  2682. if(res_info == NULL || picreg == NULL)
  2683. {
  2684. SYS_LOG_INF("invalid picreg for preload \n");
  2685. return NULL;
  2686. }
  2687. bitmap = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2688. if(bitmap == NULL)
  2689. {
  2690. SYS_LOG_INF("no space for frame preloading \n");
  2691. return NULL;
  2692. }
  2693. buf = res_info->sty_data + picreg->sty_data->id_offset + sizeof(sty_picture_t)*frame;
  2694. bitmap->sty_data = (sty_picture_t*)buf;
  2695. bitmap->regular_info = picreg->regular_info;
  2696. return bitmap;
  2697. }
  2698. void* res_manager_preload_next_group_child(resource_info_t * info, resource_group_t* group, int* count, uint32_t* offset, uint32_t scene_id, uint32_t pargroup_id)
  2699. {
  2700. resource_t* resource;
  2701. char* buf = NULL; //地址不对齐,不能用数据类型指针。
  2702. uint32_t cur_count = *count;
  2703. uint32_t next_offset = *offset;
  2704. if(group == NULL)
  2705. {
  2706. SYS_LOG_INF("invalid param for res_manager_preload_next_group_child\n");
  2707. return NULL;
  2708. }
  2709. if(group->sty_data->resource_sum == 0)
  2710. {
  2711. return NULL;
  2712. }
  2713. if(cur_count == 0)
  2714. {
  2715. buf = (char*)info->sty_data + group->sty_data->child_offset;
  2716. resource = (resource_t*)buf;
  2717. *count = 1;
  2718. *offset = resource->offset;
  2719. }
  2720. else if(cur_count >= group->sty_data->resource_sum)
  2721. {
  2722. //reach the last one
  2723. return NULL;
  2724. }
  2725. else
  2726. {
  2727. buf = info->sty_data + next_offset;
  2728. resource = (resource_t*)buf;
  2729. cur_count++;
  2730. next_offset = resource->offset;
  2731. *count = cur_count;
  2732. *offset = next_offset;
  2733. }
  2734. if(resource->type == RESOURCE_TYPE_GROUP)
  2735. {
  2736. //buf is now on group resource start
  2737. resource_group_t* res = (resource_group_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_group_t));
  2738. if(res == NULL)
  2739. {
  2740. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2741. return NULL;
  2742. }
  2743. sty_group_t* group = (sty_group_t*)resource;
  2744. res->sty_data = group;
  2745. return res;
  2746. }
  2747. if(resource->type == RESOURCE_TYPE_PICREGION)
  2748. {
  2749. resource_picregion_t* res = (resource_picregion_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_picregion_t));
  2750. if(res == NULL)
  2751. {
  2752. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2753. return NULL;
  2754. }
  2755. sty_picregion_t* picreg = (sty_picregion_t*)resource;
  2756. res->sty_data = picreg;
  2757. res->regular_info = _check_resource_is_regular(picreg->magic, picreg->sty_id);
  2758. return res;
  2759. }
  2760. if(resource->type == RESOURCE_TYPE_PICTURE)
  2761. {
  2762. //buf is now on resource start
  2763. resource_bitmap_t* res = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2764. if(res == NULL)
  2765. {
  2766. SYS_LOG_INF("%s %d: error: no space for resource to preload \n", __func__, __LINE__);
  2767. return NULL;
  2768. }
  2769. memset(res, 0, sizeof(resource_bitmap_t));
  2770. sty_picture_t* pic = (sty_picture_t*)resource;
  2771. res->sty_data = pic;
  2772. res->regular_info = _check_resource_is_regular(pic->magic, pic->sty_id);
  2773. // SYS_LOG_INF("pic w %d, h %d, id 0x%x", res->width, res->height, res->id);
  2774. // SYS_LOG_INF("scene 0x%x, id 0x%x, regular 0x%x", scene_id, resource->id, res->regular_info);
  2775. return res;
  2776. }
  2777. //ignore text
  2778. return NULL;
  2779. }
  2780. void* res_manager_preload_next_scene_child(resource_info_t* info, resource_scene_t* scene, uint32_t* count, uint32_t* offset)
  2781. {
  2782. resource_t* resource;
  2783. uint8_t* buf = NULL; //地址不对齐,不能用数据类型指针。
  2784. uint32_t cur_count = *count;
  2785. uint32_t next_offset = (uint32_t)*offset;
  2786. if(scene == NULL)
  2787. {
  2788. SYS_LOG_INF("invalid param for res_manager_preload_next_scene_child\n");
  2789. return NULL;
  2790. }
  2791. if(scene->resource_sum == 0)
  2792. {
  2793. return NULL;
  2794. }
  2795. if(cur_count == 0)
  2796. {
  2797. buf = (uint8_t*)info->sty_data + scene->child_offset;
  2798. resource = (resource_t*)buf;
  2799. *count = 1;
  2800. *offset = resource->offset;
  2801. }
  2802. else if(cur_count >= scene->resource_sum)
  2803. {
  2804. //reach the last one
  2805. return NULL;
  2806. }
  2807. else
  2808. {
  2809. buf = (uint8_t*)info->sty_data + next_offset;
  2810. resource = (resource_t*)buf;
  2811. cur_count++;
  2812. next_offset = resource->offset;
  2813. *count = cur_count;
  2814. *offset = next_offset;
  2815. }
  2816. if(resource->type == RESOURCE_TYPE_GROUP)
  2817. {
  2818. //buf is now on group resource start
  2819. resource_group_t* res = (resource_group_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_group_t));
  2820. if(res == NULL)
  2821. {
  2822. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2823. return NULL;
  2824. }
  2825. sty_group_t* group = (sty_group_t*)resource;
  2826. res->sty_data = group;
  2827. return res;
  2828. }
  2829. if(resource->type == RESOURCE_TYPE_PICREGION)
  2830. {
  2831. resource_picregion_t* res = (resource_picregion_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_picregion_t));
  2832. if(res == NULL)
  2833. {
  2834. SYS_LOG_INF("%s %d: error: no space for resource \n", __func__, __LINE__);
  2835. return NULL;
  2836. }
  2837. sty_picregion_t* picreg = (sty_picregion_t*)resource;
  2838. res->sty_data = picreg;
  2839. res->regular_info = _check_resource_is_regular(picreg->magic, picreg->sty_id);
  2840. return res;
  2841. }
  2842. if(resource->type == RESOURCE_TYPE_PICTURE)
  2843. {
  2844. //buf is now on resource start
  2845. resource_bitmap_t* res = (resource_bitmap_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(resource_bitmap_t));
  2846. if(res == NULL)
  2847. {
  2848. SYS_LOG_INF("%s %d: error: no space for resource to preload \n", __func__, __LINE__);
  2849. return NULL;
  2850. }
  2851. memset(res, 0, sizeof(resource_bitmap_t));
  2852. sty_picture_t* pic = (sty_picture_t*)resource;
  2853. res->sty_data = pic;
  2854. res->regular_info = _check_resource_is_regular(pic->magic, pic->sty_id);
  2855. // SYS_LOG_INF("scene 0x%x, id 0x%x, regular 0x%x", cur_scene_id, resource->id, res->regular_info);
  2856. return res;
  2857. }
  2858. //ignore text;
  2859. return NULL;
  2860. }
  2861. void res_manager_preload_finish_check(uint32_t scene_id)
  2862. {
  2863. compact_buffer_t* buffer;
  2864. os_strace_u32(SYS_TRACE_ID_RES_CHECK_PRELOAD, (uint32_t)scene_id);
  2865. os_mutex_lock(&bitmap_cache_mutex, OS_FOREVER);
  2866. buffer = bitmap_buffer.compact_buffer_list;
  2867. while(buffer != NULL)
  2868. {
  2869. if(buffer->scene_id == scene_id)
  2870. {
  2871. mem_dcache_clean(buffer->addr, buffer->offset);
  2872. }
  2873. buffer = buffer->next;
  2874. }
  2875. os_mutex_unlock(&bitmap_cache_mutex);
  2876. os_strace_end_call_u32(SYS_TRACE_ID_RES_CHECK_PRELOAD, (uint32_t)scene_id);
  2877. }
  2878. uint32_t res_manager_scene_is_loaded(uint32_t scene_id)
  2879. {
  2880. compact_buffer_t* buffer = bitmap_buffer.compact_buffer_list;
  2881. buf_block_t* item;
  2882. while(buffer)
  2883. {
  2884. // SYS_LOG_DBG("buffer->scene_id 0x%x, scene_id 0x%x\n", buffer->scene_id, scene_id);
  2885. if(buffer->scene_id == scene_id)
  2886. {
  2887. item = (buf_block_t*)buffer->addr;
  2888. if(item->regular_info == 0)
  2889. {
  2890. return 1;
  2891. }
  2892. }
  2893. buffer = buffer->next;
  2894. }
  2895. return 0;
  2896. }
  2897. #ifdef CONFIG_RES_MANAGER_IMG_DECODER
  2898. #include <lvgl/lvgl.h>
  2899. #define RAW_RES_DATA_SIZE (sizeof(resource_bitmap_t))
  2900. static lv_img_cf_t _get_res_img_cf(const resource_bitmap_t *sty)
  2901. {
  2902. lv_img_cf_t cf;
  2903. switch (sty->sty_data->format) {
  2904. case RESOURCE_BITMAP_FORMAT_ARGB8888:
  2905. cf = LV_IMG_CF_ARGB_8888;
  2906. break;
  2907. case RESOURCE_BITMAP_FORMAT_RGB565:
  2908. case RESOURCE_BITMAP_FORMAT_JPEG:
  2909. cf = LV_IMG_CF_RGB_565;
  2910. break;
  2911. case RESOURCE_BITMAP_FORMAT_ARGB8565:
  2912. cf = LV_IMG_CF_ARGB_8565;
  2913. break;
  2914. case RESOURCE_BITMAP_FORMAT_A8:
  2915. cf = LV_IMG_CF_ALPHA_8BIT;
  2916. break;
  2917. case RESOURCE_BITMAP_FORMAT_ARGB6666:
  2918. cf = LV_IMG_CF_ARGB_6666;
  2919. break;
  2920. case RESOURCE_BITMAP_FORMAT_ARGB1555:
  2921. cf = LV_IMG_CF_ARGB_1555;
  2922. break;
  2923. case RESOURCE_BITMAP_FORMAT_INDEX8:
  2924. cf = LV_IMG_CF_INDEXED_8BIT_ACTS;
  2925. break;
  2926. case RESOURCE_BITMAP_FORMAT_LVGL_INDEX8:
  2927. cf = LV_IMG_CF_INDEXED_8BIT;
  2928. break;
  2929. case RESOURCE_BITMAP_FORMAT_INDEX4:
  2930. cf = LV_IMG_CF_INDEXED_4BIT;
  2931. break;
  2932. case RESOURCE_BITMAP_FORMAT_ETC2_EAC:
  2933. cf = LV_IMG_CF_ETC2_EAC;
  2934. break;
  2935. case RESOURCE_BITMAP_FORMAT_RGB888:
  2936. case RESOURCE_BITMAP_FORMAT_RAW:
  2937. default:
  2938. cf = LV_IMG_CF_UNKNOWN;
  2939. }
  2940. return cf;
  2941. }
  2942. static int32_t _load_bitmap_for_decoder(resource_info_t* info, resource_bitmap_t* bitmap)
  2943. {
  2944. int32_t ret;
  2945. int32_t bmp_size;
  2946. int32_t compress_size = 0;
  2947. uint8_t *compress_buf = NULL;
  2948. struct fs_file_t* pic_fp;
  2949. uint32_t bmp_pos = 0;
  2950. if(bitmap == NULL)
  2951. {
  2952. SYS_LOG_INF("error: invalid param to load bitmap \n");
  2953. return -1;
  2954. }
  2955. if(_get_resource_bitmap_map(info, bitmap) == 0)
  2956. {
  2957. //bitmap directly mapped from file
  2958. return 0;
  2959. }
  2960. #if RES_MEM_DEBUG
  2961. SYS_LOG_INF("decode bitmap id %d, w %d, height %d, bpp %d\n", bitmap->sty_data->id, bitmap->sty_data->width, bitmap->sty_data->height, bitmap->sty_data->bytes_per_pixel);
  2962. #endif
  2963. //bmp_size = bitmap->sty_data->width * bitmap->sty_data->height * bitmap->sty_data->bytes_per_pixel;
  2964. if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_ETC2_EAC) {
  2965. bmp_size = ((bitmap->sty_data->width + 0x3) & ~0x3) *
  2966. ((bitmap->sty_data->height + 0x3) & ~0x3);
  2967. } else if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX8) {
  2968. bmp_size = bitmap->sty_data->width * bitmap->sty_data->height + 1024 + 4;
  2969. } else if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_LVGL_INDEX8) {
  2970. bmp_size = bitmap->sty_data->width * bitmap->sty_data->height + 1024;
  2971. } else if (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX4) {
  2972. bmp_size = ((bitmap->sty_data->width + 0x1) >> 1) * bitmap->sty_data->height + 64;
  2973. } else {
  2974. bmp_size = bitmap->sty_data->width * bitmap->sty_data->height *
  2975. bitmap->sty_data->bytes_per_pixel;
  2976. }
  2977. bitmap->buffer = _get_resource_bitmap_buffer(0, bitmap, bmp_size, 1);
  2978. if(bitmap->buffer == NULL)
  2979. {
  2980. SYS_LOG_INF("error: no buffer to load bitmap \n");
  2981. return -2;
  2982. }
  2983. ret = _search_res_id_in_files(info, bitmap, &bmp_pos, &compress_size);
  2984. if(ret <= 0)
  2985. {
  2986. pic_fp = &info->pic_fp;
  2987. }
  2988. else
  2989. {
  2990. pic_fp = &info->pic_search_param[ret].res_fp;
  2991. }
  2992. #if !defined(CONFIG_RES_MANAGER_USE_STYLE_MMAP)||defined(CONFIG_SIMULATOR)
  2993. fs_seek(pic_fp, bmp_pos, FS_SEEK_SET);
  2994. #endif
  2995. if (compress_size > 0)
  2996. {
  2997. #if !defined(CONFIG_RES_MANAGER_USE_STYLE_MMAP)||defined(CONFIG_SIMULATOR)
  2998. compress_buf = (uint8_t*)res_mem_alloc(RES_MEM_POOL_BMP, compress_size);
  2999. if(compress_buf == NULL)
  3000. {
  3001. SYS_LOG_INF("error: no buffer to load compressed bitmap \n");
  3002. _free_resource_bitmap_buffer(bitmap->buffer);
  3003. bitmap->buffer = NULL;
  3004. res_manager_dump_info();
  3005. return -2;
  3006. }
  3007. ret = fs_read(pic_fp, compress_buf, compress_size);
  3008. if(ret < compress_size)
  3009. {
  3010. SYS_LOG_INF("bitmap read error %d\n", ret);
  3011. }
  3012. #else
  3013. compress_buf = info->pic_res_mmap_addr + bmp_pos;
  3014. #endif
  3015. if(ret == compress_size)
  3016. {
  3017. #ifndef CONFIG_LVGL_USE_IMG_DECODER_ACTS
  3018. #ifdef CONFIG_JPEG_HAL
  3019. if (*(uint32_t*)compress_buf == JPEG_FLAG) {
  3020. #if RES_MEM_DEBUG
  3021. SYS_LOG_INF("jpg decode\n");
  3022. #endif
  3023. ret = jpg_decode(compress_buf, compress_size, bitmap->buffer, 1, bitmap->sty_data->width, 0, 0, bitmap->sty_data->width, bitmap->sty_data->height);
  3024. }
  3025. else
  3026. #endif
  3027. #endif
  3028. {
  3029. #ifndef CONFIG_SIMULATOR
  3030. #if RES_MEM_DEBUG
  3031. SYS_LOG_INF("brom decompress\n");
  3032. #endif
  3033. ret = p_brom_misc_api->p_decompress(compress_buf, bitmap->buffer, compress_size, bmp_size);
  3034. #else
  3035. ret = LZ4_decompress_safe(compress_buf, bitmap->buffer, compress_size, bmp_size);
  3036. #endif
  3037. }
  3038. }
  3039. if ((bitmap->sty_data->format != RESOURCE_BITMAP_FORMAT_INDEX8 && ret < bmp_size) ||
  3040. (bitmap->sty_data->format == RESOURCE_BITMAP_FORMAT_INDEX8 && ret <= bitmap->sty_data->width * bitmap->sty_data->height + 4)){
  3041. SYS_LOG_INF("bmp dump pos for %s: 0x%x\n", info->sty_path, bmp_pos);
  3042. for (int i = 0; i < 4; i++) {
  3043. SYS_LOG_INF("%d: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", i, compress_buf[8*i], compress_buf[8*i+1], compress_buf[8*i+2],
  3044. compress_buf[8*i+3], compress_buf[8*i+4], compress_buf[8*i+5], compress_buf[8*i+6], compress_buf[8*i+7]);
  3045. }
  3046. } else {
  3047. ret = bmp_size; /* for RESOURCE_BITMAP_FORMAT_INDEX8 */
  3048. }
  3049. #if !defined(CONFIG_RES_MANAGER_USE_STYLE_MMAP)||defined(CONFIG_SIMULATOR)
  3050. res_mem_free(RES_MEM_POOL_BMP, compress_buf);
  3051. #endif
  3052. }
  3053. else
  3054. {
  3055. ret = fs_read(pic_fp, bitmap->buffer, bmp_size);
  3056. }
  3057. if(ret < bmp_size)
  3058. {
  3059. SYS_LOG_INF("decoder bitmap load error %d, compress_buf %p, id %d, w %d, h %d, bmp_size %d\n",
  3060. ret, compress_buf, bitmap->sty_data->id, bitmap->sty_data->width, bitmap->sty_data->height, bmp_size);
  3061. SYS_LOG_INF("decoder bitmap compress size %d, sty compress size %d, bmp pos %d, sty bmp pos\n",
  3062. compress_size, bitmap->sty_data->compress_size, bmp_pos, bitmap->sty_data->bmp_pos);
  3063. }
  3064. mem_dcache_clean(bitmap->buffer, bmp_size);
  3065. return 0;
  3066. }
  3067. typedef struct
  3068. {
  3069. int pic_id;
  3070. uint8_t* data;
  3071. uint32_t time_to_open;
  3072. int life;
  3073. int force;
  3074. }decoder_res_cache_entry_t;
  3075. #define DECODER_RES_CACHE_AGING 1
  3076. #define DECODER_RES_CACHE_LIFE_LIMIT 1000
  3077. static decoder_res_cache_entry_t* decoder_res_cache = NULL;
  3078. static int cache_entry_cnt = CONFIG_RES_MANAGER_LVGL_RES_CACHE_MAX;
  3079. static uint32_t sys_cycle;
  3080. static uint32_t sys_time_ms;
  3081. #ifdef CONFIG_SIMULATOR
  3082. #include <Windows.h>
  3083. #endif
  3084. static uint32_t _tick_get()
  3085. {
  3086. #ifndef CONFIG_SIMULATOR
  3087. uint32_t cycle = k_cycle_get_32();
  3088. uint32_t delta_ms = k_cyc_to_ms_floor32(cycle - sys_cycle);
  3089. sys_cycle += delta_ms * (sys_clock_hw_cycles_per_sec() / 1000);
  3090. sys_time_ms += delta_ms;
  3091. return sys_time_ms;
  3092. #else
  3093. return GetTickCount();
  3094. #endif
  3095. }
  3096. static uint32_t _tick_elaps(uint32_t prev_tick)
  3097. {
  3098. uint32_t act_time = _tick_get();
  3099. /*If there is no overflow in sys_time simple subtract*/
  3100. if(act_time >= prev_tick) {
  3101. prev_tick = act_time - prev_tick;
  3102. }
  3103. else {
  3104. prev_tick = UINT32_MAX - prev_tick + 1;
  3105. prev_tick += act_time;
  3106. }
  3107. return prev_tick;
  3108. }
  3109. static int _add_to_decoder_res_cache(resource_bitmap_t* bitmap, decoder_res_cache_entry_t** cache_entry, int force)
  3110. {
  3111. int i;
  3112. decoder_res_cache_entry_t* cache = NULL;
  3113. decoder_res_cache_entry_t* cached_src = NULL;
  3114. if(cache_entry_cnt == 0)
  3115. {
  3116. //decoder res cache not inited
  3117. SYS_LOG_INF("decoder res cache not inited \n");
  3118. return -1;
  3119. }
  3120. if(decoder_res_cache == NULL)
  3121. {
  3122. decoder_res_cache = (decoder_res_cache_entry_t*)res_mem_alloc(RES_MEM_POOL_BMP, sizeof(decoder_res_cache_entry_t)*cache_entry_cnt);
  3123. if(!decoder_res_cache)
  3124. {
  3125. SYS_LOG_INF("init decoder res cache failed \n");
  3126. return -1;
  3127. }
  3128. memset(decoder_res_cache, 0, sizeof(decoder_res_cache_entry_t)*cache_entry_cnt);
  3129. }
  3130. cache = decoder_res_cache;
  3131. for(i=0;i<cache_entry_cnt;i++)
  3132. {
  3133. if(cache[i].life > INT32_MIN + DECODER_RES_CACHE_AGING)
  3134. {
  3135. cache[i].life -= DECODER_RES_CACHE_AGING;
  3136. }
  3137. }
  3138. for(i=0;i<cache_entry_cnt;i++)
  3139. {
  3140. if(cache[i].pic_id == bitmap->sty_data->id)
  3141. {
  3142. //found cached pic
  3143. cached_src = &cache[i];
  3144. cached_src->life += cached_src->time_to_open;
  3145. if(cached_src->life > DECODER_RES_CACHE_LIFE_LIMIT)
  3146. {
  3147. cached_src->life = DECODER_RES_CACHE_LIFE_LIMIT;
  3148. }
  3149. break;
  3150. }
  3151. }
  3152. if(cached_src)
  3153. {
  3154. bitmap->buffer = cached_src->data;
  3155. if(force == 1)
  3156. {
  3157. cached_src->force = 1;
  3158. }
  3159. *cache_entry = cached_src;
  3160. return 1;
  3161. }
  3162. cached_src = &cache[0];
  3163. for(i = 1; i < cache_entry_cnt; i++) {
  3164. if(cache[i].life < cached_src->life && cache[i].force == 0) {
  3165. cached_src = &cache[i];
  3166. }
  3167. }
  3168. /*
  3169. SYS_LOG_INF("cache life 0-7 %d, %d, %d, %d, %d, %d, %d, %d\n",
  3170. cache[0].life, cache[1].life, cache[2].life, cache[3].life, cache[4].life, cache[5].life, cache[6].life, cache[7].life);
  3171. SYS_LOG_INF("cache life 8-15 %d, %d, %d, %d, %d, %d, %d, %d\n",
  3172. cache[8].life, cache[9].life, cache[10].life, cache[11].life, cache[12].life, cache[13].life, cache[14].life, cache[15].life);
  3173. */
  3174. if(cached_src->data != NULL)
  3175. {
  3176. //FIXME: how to close decoder dsc
  3177. _free_resource_bitmap_buffer(cached_src->data);
  3178. memset(cached_src, 0, sizeof(decoder_res_cache_entry_t));
  3179. }
  3180. if(force == 1)
  3181. {
  3182. cached_src->force = 1;
  3183. }
  3184. *cache_entry = cached_src;
  3185. return 0;
  3186. }
  3187. static void _clear_decoder_res_cache(void)
  3188. {
  3189. int i;
  3190. decoder_res_cache_entry_t* cache_entry = decoder_res_cache;
  3191. if(cache_entry == NULL)
  3192. {
  3193. return;
  3194. }
  3195. for(i = 0; i < cache_entry_cnt; i++)
  3196. {
  3197. //FIXME: do we need to consider reserve forced cache?
  3198. if(cache_entry[i].data != NULL)
  3199. {
  3200. _free_resource_bitmap_buffer(cache_entry[i].data);
  3201. }
  3202. memset(&cache_entry[i], 0, sizeof(decoder_res_cache_entry_t));
  3203. }
  3204. return;
  3205. }
  3206. static int _free_one_cache_entry(resource_bitmap_t* bitmap, decoder_res_cache_entry_t* cached_src)
  3207. {
  3208. decoder_res_cache_entry_t* cache = decoder_res_cache;
  3209. decoder_res_cache_entry_t * inv_src = NULL;
  3210. int k, j;
  3211. for (k = 0; k < cache_entry_cnt; k++)
  3212. {
  3213. if (cache[k].data != NULL && cached_src != &cache[k] && cache[k].force == 0)
  3214. {
  3215. inv_src = &cache[k];
  3216. break;
  3217. }
  3218. }
  3219. if (inv_src == NULL)
  3220. {
  3221. //whole cache cleared out but no enough space
  3222. return -1;
  3223. }
  3224. if (k < cache_entry_cnt - 1)
  3225. {
  3226. for (j = k + 1; j < cache_entry_cnt; j++)
  3227. {
  3228. if (cached_src != &cache[j] && cache[j].data != NULL && cache[j].life < inv_src->life && cache[j].force == 0)
  3229. {
  3230. inv_src = &cache[j];
  3231. }
  3232. }
  3233. }
  3234. _free_resource_bitmap_buffer(inv_src->data);
  3235. memset(inv_src, 0, sizeof(decoder_res_cache_entry_t));
  3236. return 0;
  3237. }
  3238. void res_manager_clear_decoder_cache(void)
  3239. {
  3240. _clear_decoder_res_cache();
  3241. }
  3242. int res_manager_decode_bitmap_to_img_dsc(void* raw, void* decoded)
  3243. {
  3244. resource_bitmap_t bitmap;
  3245. lv_img_dsc_t* raw_img = (lv_img_dsc_t*)raw;
  3246. lv_img_dsc_t* decoded_img = (lv_img_dsc_t*)decoded;
  3247. int ret;
  3248. if((raw_img->header.cf != LV_IMG_CF_RAW && raw_img->header.cf != LV_IMG_CF_RAW_ALPHA) ||
  3249. (raw_img->data_size != RAW_RES_DATA_SIZE))
  3250. {
  3251. SYS_LOG_ERR("not a raw res img ");
  3252. return -1;
  3253. }
  3254. memcpy(&bitmap, raw_img->data, raw_img->data_size);
  3255. bitmap.buffer = NULL;
  3256. ret = res_manager_load_bitmap_for_decoder(&bitmap, 1);
  3257. if(ret < 0)
  3258. {
  3259. SYS_LOG_ERR("load bitmap failed in res_manager_decode_bitmap_to_img_dsc\n");
  3260. return -1;
  3261. }
  3262. decoded_img->header.cf = _get_res_img_cf(&bitmap);
  3263. decoded_img->header.always_zero = 0;
  3264. decoded_img->header.w = bitmap.sty_data->width;
  3265. decoded_img->header.h = bitmap.sty_data->height;
  3266. decoded_img->data = bitmap.buffer;
  3267. decoded_img->data_size = bitmap.sty_data->width * bitmap.sty_data->height * bitmap.sty_data->bytes_per_pixel;
  3268. return 0;
  3269. }
  3270. int res_manager_load_bitmap_for_decoder(resource_bitmap_t* bitmap, int force)
  3271. {
  3272. resource_info_t* info = (resource_info_t*)bitmap->sty_data->magic;
  3273. int ret;
  3274. decoder_res_cache_entry_t* cache_item = NULL;
  3275. if(info->reference == 0)
  3276. {
  3277. ret = _init_pic_search_param(info, info->sty_path);
  3278. if(ret < 0)
  3279. {
  3280. SYS_LOG_INF("init img resource failed\n");
  3281. return -1;
  3282. }
  3283. info->reference = 1;
  3284. }
  3285. ret = _add_to_decoder_res_cache(bitmap, &cache_item, force);
  3286. if(ret < 0)
  3287. {
  3288. SYS_LOG_ERR("add to decoder res cache failed\n");
  3289. return -1;
  3290. }
  3291. if(ret > 0)
  3292. {
  3293. //found in cache, add life or sth.
  3294. bitmap->buffer = cache_item->data;
  3295. #if RES_MEM_DEBUG
  3296. SYS_LOG_INF("bitmap->buffer cached %p\n", bitmap->buffer);
  3297. #endif
  3298. return 0;
  3299. }
  3300. uint32_t t_start = _tick_get();
  3301. ret = _load_bitmap_for_decoder(info, bitmap);
  3302. while(ret == -2)
  3303. {
  3304. //res mem not enough, release some unforced cache
  3305. ret = _free_one_cache_entry(bitmap, cache_item);
  3306. if(ret < 0)
  3307. {
  3308. SYS_LOG_INF("no enough res cache mem\n");
  3309. break;
  3310. }
  3311. ret = _load_bitmap_for_decoder(info, bitmap);
  3312. }
  3313. if(ret < 0)
  3314. {
  3315. //other load bitmap error, return error
  3316. SYS_LOG_INF("load img resource failed\n");
  3317. if(cache_item)
  3318. {
  3319. memset(cache_item, 0, sizeof(decoder_res_cache_entry_t));
  3320. cache_item->life = INT32_MIN;
  3321. }
  3322. return ret;
  3323. }
  3324. if(cache_item)
  3325. {
  3326. cache_item->pic_id = bitmap->sty_data->id;
  3327. cache_item->data = bitmap->buffer;
  3328. cache_item->life = 0;
  3329. if(cache_item->time_to_open == 0)
  3330. {
  3331. cache_item->time_to_open = _tick_elaps(t_start);
  3332. }
  3333. if(cache_item->time_to_open == 0)
  3334. {
  3335. cache_item->time_to_open = 1;
  3336. }
  3337. bitmap->buffer = cache_item->data;
  3338. SYS_LOG_INF("bitmap->buffer %p\n", bitmap->buffer);
  3339. }
  3340. return 0;
  3341. }
  3342. void res_manager_free_bitmap_for_decoder(void* ptr)
  3343. {
  3344. int i;
  3345. decoder_res_cache_entry_t* cache_entry = decoder_res_cache;
  3346. if(cache_entry == NULL || ptr == NULL)
  3347. {
  3348. return;
  3349. }
  3350. for(i=0;i<cache_entry_cnt;i++)
  3351. {
  3352. if(cache_entry[i].data == ptr)
  3353. {
  3354. if(cache_entry[i].force == 1)
  3355. {
  3356. cache_entry[i].force = 0;
  3357. }
  3358. if(cache_entry[i].life > 0)
  3359. {
  3360. cache_entry[i].life = 0;
  3361. }
  3362. break;
  3363. }
  3364. }
  3365. //mem block buffer need to release immediately for following big pics, or they will alloc in normal res mem
  3366. if(res_mem_is_block(ptr))
  3367. {
  3368. SYS_LOG_INF("free decoded %p\n", ptr);
  3369. _free_resource_bitmap_buffer(ptr);
  3370. memset(&cache_entry[i], 0, sizeof(decoder_res_cache_entry_t));
  3371. }
  3372. }
  3373. static int _find_id_from_key(uint8_t* key)
  3374. {
  3375. int low, high, mid;
  3376. int ret;
  3377. low = 1;
  3378. high = current_string_res_cnt;
  3379. mid = (current_string_res_cnt)/2;
  3380. while(low <= high)
  3381. {
  3382. ret = strcmp(key, RES_STRING_ID_DIC[mid].key);
  3383. if(ret < 0)
  3384. {
  3385. high = mid - 1;
  3386. }
  3387. else if(ret > 0)
  3388. {
  3389. low = mid + 1;
  3390. }
  3391. else
  3392. {
  3393. break;
  3394. }
  3395. mid = (low+high)/2;
  3396. }
  3397. if(low <= high)
  3398. {
  3399. return RES_STRING_ID_DIC[mid].value;
  3400. }
  3401. return -1;
  3402. }
  3403. int res_manager_set_current_string_res(const uint8_t** string_res, uint32_t string_cnt)
  3404. {
  3405. if(string_res == NULL)
  3406. {
  3407. return -1;
  3408. }
  3409. current_string_res = string_res;
  3410. current_string_res_cnt = string_cnt;
  3411. return 0;
  3412. }
  3413. uint8_t* res_manager_get_string(uint8_t* key)
  3414. {
  3415. int id = 1;
  3416. if(!current_string_res)
  3417. {
  3418. SYS_LOG_ERR("current string resource not inited\n");
  3419. return NULL;
  3420. }
  3421. id = _find_id_from_key(key);
  3422. if(id > 0 && id <= current_string_res_cnt)
  3423. {
  3424. return (uint8_t*)current_string_res[id-1];
  3425. }
  3426. return NULL;
  3427. }
  3428. uint8_t* res_manager_get_string_from_id(int id)
  3429. {
  3430. if(!current_string_res)
  3431. {
  3432. SYS_LOG_ERR("current string resource not inited\n");
  3433. return NULL;
  3434. }
  3435. if(id >= 0 && id < current_string_res_cnt)
  3436. {
  3437. return (uint8_t*)current_string_res[RES_STRING_ID_DIC[id+1].value-1];
  3438. }
  3439. return NULL;
  3440. }
  3441. #else
  3442. int res_manager_decode_bitmap_to_img_dsc(void* raw, void* decoded)
  3443. {
  3444. return 0;
  3445. }
  3446. int res_manager_load_bitmap_for_decoder(resource_bitmap_t* bitmap)
  3447. {
  3448. return 0;
  3449. }
  3450. void res_manager_free_bitmap_for_decoder(void* ptr)
  3451. {
  3452. return;
  3453. }
  3454. int res_manager_set_current_string_res(const uint8_t** string_res, uint32_t string_cnt)
  3455. {
  3456. return 0;
  3457. }
  3458. uint8_t* res_manager_get_string(uint8_t* key)
  3459. {
  3460. return NULL;
  3461. }
  3462. uint8_t* res_manager_get_string_from_id(int id)
  3463. {
  3464. return NULL;
  3465. }
  3466. void res_manager_clear_decoder_cache(void)
  3467. {
  3468. return;
  3469. }
  3470. #endif
  3471. void res_manager_dump_info(void)
  3472. {
  3473. compact_buffer_t* citem;
  3474. buf_block_t* bitem;
  3475. uint32_t mem_peak;
  3476. //mem dump
  3477. res_mem_dump();
  3478. //mem peak dump
  3479. mem_peak = res_mem_get_mem_peak();
  3480. if(mem_peak > 0)
  3481. {
  3482. SYS_LOG_INF("res mem peak size is %d\n", mem_peak);
  3483. }
  3484. else
  3485. {
  3486. SYS_LOG_INF("res mem peak statistic not enabled\n");
  3487. }
  3488. //ui mem info
  3489. SYS_LOG_INF("full screen bitmap total %d\n", ui_mem_total);
  3490. //compact buffer dump();
  3491. citem = bitmap_buffer.compact_buffer_list;
  3492. while(citem != NULL)
  3493. {
  3494. SYS_LOG_INF("compact buffer sceneid 0x%x, addr %p, size %d, freesize %d \n", citem->scene_id, citem->addr, citem->offset, citem->free_size);
  3495. citem = citem->next;
  3496. }
  3497. //bitmap dump();
  3498. bitem = bitmap_buffer.head;
  3499. while(bitem != NULL)
  3500. {
  3501. SYS_LOG_INF("bitmap buffer id %d, width %d, height %d, addr %p, size %d, ref %d \n", bitem->id, bitem->width, bitem->height, bitem->addr, bitem->size, bitem->ref);
  3502. bitem = bitem->next;
  3503. }
  3504. //txt dump()
  3505. bitem = text_buffer.head;
  3506. while(bitem != NULL)
  3507. {
  3508. SYS_LOG_INF("txt buffer id %d, addr %p \n", bitem->id, bitem->addr);
  3509. bitem = bitem->next;
  3510. }
  3511. }
  3512. #ifdef CONFIG_RES_MANAGER_ENABLE_MEM_LEAK_DEBUG
  3513. void res_manager_compact_buffer_check(uint32_t scene_id)
  3514. {
  3515. compact_buffer_t* citem;
  3516. citem = bitmap_buffer.compact_buffer_list;
  3517. while(citem != NULL)
  3518. {
  3519. if(citem->scene_id != scene_id)
  3520. {
  3521. SYS_LOG_INF("RES LEAK WARNING: scene 0x%x compact buffer exsit when layout scene 0x%x\n", citem->scene_id, scene_id);
  3522. }
  3523. citem = citem->next;
  3524. }
  3525. }
  3526. #endif