ResMemMgr.c
上传用户:lqx1163
上传日期:2014-08-13
资源大小:9183k
文件大小:54k
源码类别:

MTK

开发平台:

C/C++

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *  ResMemMgr.c
  40.  *
  41.  * Project:
  42.  * --------
  43.  *  MAUI
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  Resource Memory Sharing Manager
  48.  *
  49.  *  It is for resource memory management in Enhanced NFB (ENFB) platform.
  50.  *
  51.  * Author:
  52.  * -------
  53.  * -------
  54.  *                      
  55.  *============================================================================
  56.  *             HISTORY
  57.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  58.  *------------------------------------------------------------------------------
  59.  * removed!
  60.  *
  61.  * removed!
  62.  * removed!
  63.  * removed!
  64.  *
  65.  * removed!
  66.  * removed!
  67.  * removed!
  68.  *
  69.  * removed!
  70.  * removed!
  71.  * removed!
  72.  *
  73.  * removed!
  74.  * removed!
  75.  * removed!
  76.  * removed!
  77.  *
  78.  * removed!
  79.  * removed!
  80.  * removed!
  81.  * removed!
  82.  *
  83.  * removed!
  84.  * removed!
  85.  * removed!
  86.  *
  87.  * removed!
  88.  * removed!
  89.  * removed!
  90.  *
  91.  * removed!
  92.  * removed!
  93.  * removed!
  94.  *
  95.  * removed!
  96.  * removed!
  97.  * removed!
  98.  *
  99.  * removed!
  100.  * removed!
  101.  * removed!
  102.  * removed!
  103.  *
  104.  * removed!
  105.  * removed!
  106.  * removed!
  107.  *
  108.  * removed!
  109.  * removed!
  110.  * removed!
  111.  *
  112.  * removed!
  113.  * removed!
  114.  * removed!
  115.  *
  116.  * removed!
  117.  * removed!
  118.  * removed!
  119.  *
  120.  * removed!
  121.  * removed!
  122.  * removed!
  123.  *
  124.  * removed!
  125.  *
  126.  *------------------------------------------------------------------------------
  127.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  128.  *============================================================================
  129.  ****************************************************************************/
  130. /***************************************************************************** 
  131.  * Include
  132.  *****************************************************************************/
  133. #if !defined(__MTK_TARGET__)
  134. #define UNICODE
  135. #include <stdio.h>
  136. #include "Windows.h"
  137. #endif /* !defined(__MTK_TARGET__) */ 
  138. #if !defined(__MTK_TARGET__)
  139. #define _WINDOWS_       /* to solve redefine prbolem */
  140. #endif 
  141. #include "kal_non_specific_general_types.h"
  142. #include "kal_release.h"
  143. #include "MMI_features.h"
  144. #include "stdC.h"
  145. #include "GlobalDefs.h"
  146. #include "DebugInitDef.h"
  147. #include "ResMemMgrGprot.h"
  148. #include "CustDataRes.h"
  149. #include "gdi_include.h"
  150. #if defined(__MMI_RESOURCE_ENFB_SUPPORT__)
  151. #include "CustENFBStr.h"
  152. #include "nfb_loader.h"
  153. #endif /* defined(__MMI_RESOURCE_ENFB_SUPPORT__) */ 
  154. /***************************************************************************** 
  155.  * Define
  156.  *****************************************************************************/
  157. #define MMI_FRM_RESMEM_CS_ENTER  GDI_LOCK
  158. #define MMI_FRM_RESMEM_CS_LEAVE  GDI_UNLOCK
  159. /* Invalid resoruce ID */
  160. #define MMI_FRM_RESMEM_INVALID_RES_ID          0xFFFF
  161. /*----------------------------------------------------------------*
  162.   Image resource                                                 
  163.  *----------------------------------------------------------------*/
  164. /* Unit test */
  165. // #define MMI_FRM_RESMEM_UNIT_TEST
  166. /* Enable debugging invalid pointer usage by application */
  167. // #define MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER
  168. #if defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER)
  169. #define MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER    5
  170. #define MMI_FRM_RESMEM_SPOT_ICON_RESERVED_SIZE         300
  171. #else /* defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER) */ 
  172. #define MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER    1
  173. #endif /* defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER) */ 
  174. /* size of memory pool
  175.    the size is adjustable */
  176. #define MMI_FRM_RESMEM_POOL_LOD_ON_DEMAND_SIZE  (750 * 1024)    /* 750 K */
  177. #define MMI_FRM_RESMEM_POOL_ALWASY_EXIST_SIZE   (200 * 1024)    /* 200 K */
  178. /* maximum number of records for keeping memory allocation informaitonry
  179.    the number is adjustable */
  180. #define MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER    100
  181. #define MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER      20
  182. #if !defined(__MTK_TARGET__)
  183. #ifdef MMI_ON_WIN32
  184. #define MMI_FRM_RESMEM_IMAGE_FILE  L"..\..\Customer\CustResource\CustENFBImgData"
  185. #else 
  186. #define MMI_FRM_RESMEM_IMAGE_FILE   L"..\MoDIS\CustENFBImgData"       /* relative to fssim_root_dir (mcuMoDISWIN32FS) */
  187. #endif 
  188. #endif /* !defined(__MTK_TARGET__) */ 
  189. /*----------------------------------------------------------------*
  190.   String resource
  191.  *----------------------------------------------------------------*/
  192. /* The file name of the string raw data */
  193. #if !defined(__MTK_TARGET__)
  194. #ifdef MMI_ON_WIN32
  195. #define MMI_FRM_RESMEM_STRING_FILE     L"..\..\Customer\CustResource\CustENFBStrData"
  196. #else 
  197. #define MMI_FRM_RESMEM_STRING_FILE     L"..\MoDIS\CustENFBStrData"
  198. #endif 
  199. #endif /* !defined(__MTK_TARGET__) */ 
  200. #define MMI_FRM_RESMEM_POOL_STR_RAM_SIZE  (MaxENFBStringSize)   /* bytes */
  201. /***************************************************************************** 
  202.  * Typedef 
  203.  *****************************************************************************/
  204. /*----------------------------------------------------------------*
  205.   Image resource                                                 
  206.  *----------------------------------------------------------------*/
  207. #ifdef MMI_ON_WIN32
  208. typedef void *KAL_ADM_ID;
  209. typedef unsigned char kal_uint8;
  210. typedef unsigned long kal_uint32;
  211. typedef long kal_int32;
  212. typedef BOOL kal_bool;
  213. #endif /* MMI_ON_WIN32 */ 
  214. typedef struct
  215. {
  216.     U16 id;
  217.     U8 *mem_ptr;
  218.     U32 size;
  219. } mmi_frm_resmem_alloc_struct;
  220. /*----------------------------------------------------------------*
  221.   String resource
  222.  *----------------------------------------------------------------*/
  223. /*----------------------------------------------------------------*
  224.   Common
  225.  *----------------------------------------------------------------*/
  226. /* internal contenxt struct */
  227. typedef struct
  228. {
  229.     mmi_frm_resmem_alloc_struct
  230.         lod_ary[MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER][MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER];
  231.     mmi_frm_resmem_alloc_struct always_ary[MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER];
  232.     U16 always_keep_ID[MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER];
  233.     KAL_ADM_ID adm_lod_id[MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER];
  234.     KAL_ADM_ID adm_always_id;
  235.     U8 lod_active_screen;
  236.     /* for adjustment */
  237.     U32 lod_mem_using;
  238.     U32 lod_mem_max_used;
  239.     U32 always_mem_using;
  240.     U32 always_mem_max_used;
  241.     /* string part */
  242.     U8 *str_content;
  243.     U8 *str_map;
  244. } mmi_frm_resmem_context_struct;
  245. /*****************************************************************************              
  246.  * Local Variable                                                                    
  247.  *****************************************************************************/
  248. #if defined(__MMI_RESOURCE_ENFB_SUPPORT__)
  249. static mmi_frm_resmem_context_struct g_mmi_frm_resmem_cntx;
  250. /*----------------------------------------------------------------*
  251.   Image resource                                                 
  252.  *----------------------------------------------------------------*/
  253. /* memory pool */
  254. #ifdef __MTK_TARGET__
  255. #pragma arm section zidata = "LARGEPOOL_ZI"
  256. #endif 
  257. static U8
  258.     g_mmi_frm_resmem_lod_pool[MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER][MMI_FRM_RESMEM_POOL_LOD_ON_DEMAND_SIZE];
  259. static U8 g_mmi_frm_resmem_always_pool[MMI_FRM_RESMEM_POOL_ALWASY_EXIST_SIZE];
  260. #ifdef __MTK_TARGET__
  261. #pragma arm section zidata
  262. #endif 
  263. /* for KAL ADM */
  264. static const kal_uint32 g_mmi_frm_resmem_mem_pool_chunk_size[11] = 
  265. {
  266.     16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 0xffffffff, 0
  267. };
  268. /*----------------------------------------------------------------*
  269.   String resource
  270.  *----------------------------------------------------------------*/
  271. #ifdef __MTK_TARGET__
  272. #pragma arm section zidata = "LARGEPOOL_ZI"
  273. #endif 
  274. static U8 g_mmi_frm_resmem_str_pool[MMI_FRM_RESMEM_POOL_STR_RAM_SIZE];
  275. #ifdef __MTK_TARGET__
  276. #pragma arm section zidata
  277. #endif 
  278. /*****************************************************************************              
  279.  * Local Function                                                                    
  280.  *****************************************************************************/
  281. /*----------------------------------------------------------------*
  282.   Image resource                                                 
  283.  *----------------------------------------------------------------*/
  284. #if !defined(__MTK_TARGET__)
  285. // #ifdef MMI_ON_WIN32
  286. /*****************************************************************************
  287.  * FUNCTION
  288.  *  NFB_RES_Load
  289.  * DESCRIPTION
  290.  *  
  291.  * PARAMETERS
  292.  *  cid             [IN]        
  293.  *  dst_addr        [?]         [?]
  294.  *  offset          [IN]        
  295.  *  length          [IN]        
  296.  * RETURNS
  297.  *  
  298.  *****************************************************************************/
  299. static kal_bool NFB_RES_Load(kal_uint8 cid, void *dst_addr, kal_uint32 offset, kal_uint32 length)
  300. {
  301.     /*----------------------------------------------------------------*/
  302.     /* Local Variables                                                */
  303.     /*----------------------------------------------------------------*/
  304. #define TCHAR_BUFFER_SIZE 512
  305.     HANDLE hFile;
  306.     int ret = 0;
  307.     int size = length;
  308.     char *buf = (char*)dst_addr;
  309.     U32 bytesRead;
  310.     TCHAR tchBuffer[TCHAR_BUFFER_SIZE];
  311.     extern TCHAR fssim_root_dir[];
  312.     /*----------------------------------------------------------------*/
  313.     /* Code Body                                                      */
  314.     /*----------------------------------------------------------------*/
  315.     GetCurrentDirectory(TCHAR_BUFFER_SIZE, tchBuffer);
  316.     if (cid == ENFB_CONTENT_IMAGERES)
  317.     {
  318.     #ifdef MMI_ON_WIN32
  319.         wcscpy(tchBuffer, MMI_FRM_RESMEM_IMAGE_FILE);
  320.     #else /* MMI_ON_WIN32 */ 
  321.         wcscpy(tchBuffer, fssim_root_dir);
  322.         wcscat(tchBuffer, MMI_FRM_RESMEM_IMAGE_FILE);
  323.     #endif /* MMI_ON_WIN32 */ 
  324.     }
  325.     else if (cid == ENFB_CONTENT_STRINGRES)
  326.     {
  327.     #ifdef MMI_ON_WIN32
  328.         wcscpy(tchBuffer, MMI_FRM_RESMEM_STRING_FILE);
  329.     #else /* MMI_ON_WIN32 */ 
  330.         wcscpy(tchBuffer, fssim_root_dir);
  331.         wcscat(tchBuffer, MMI_FRM_RESMEM_STRING_FILE);
  332.     #endif /* MMI_ON_WIN32 */ 
  333.     }
  334.     else
  335.     {
  336.         MMI_ASSERT(0);
  337.     }
  338.     hFile = CreateFile(tchBuffer, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  339.     if (hFile == INVALID_HANDLE_VALUE)
  340.     {
  341.         MMI_ASSERT(0);
  342.         exit(1);
  343.     }
  344.     SetFilePointer(hFile, offset, NULL, FILE_BEGIN);
  345.     ret = ReadFile(hFile, dst_addr, (DWORD) length, &bytesRead, NULL);
  346.     CloseHandle(hFile);
  347.     return TRUE;
  348. }
  349. /*****************************************************************************
  350.  * FUNCTION
  351.  *  NFB_RES_Loadv
  352.  * DESCRIPTION
  353.  *  
  354.  * PARAMETERS
  355.  *  cid                 [IN]        
  356.  *  scatter             [?]         [?]
  357.  *  scatter_number      [IN]        
  358.  * RETURNS
  359.  *  
  360.  *****************************************************************************/
  361. static kal_bool NFB_RES_Loadv(kal_uint8 cid, enfb_scatter *scatter, kal_uint32 scatter_number)
  362. {
  363.     /*----------------------------------------------------------------*/
  364.     /* Local Variables                                                */
  365.     /*----------------------------------------------------------------*/
  366.     U8 i;
  367.     /*----------------------------------------------------------------*/
  368.     /* Code Body                                                      */
  369.     /*----------------------------------------------------------------*/
  370.     for (i = 0; i < scatter_number; i++)
  371.     {
  372.         NFB_RES_Load(cid, scatter[i].dst_addr, scatter[i].offset, scatter[i].length);
  373.     }
  374.     return TRUE;
  375. }
  376. #endif /* !defined(__MTK_TARGET__) */ 
  377. #ifdef MMI_ON_WIN32
  378. /*****************************************************************************
  379.  * FUNCTION
  380.  *  kal_adm_create
  381.  * DESCRIPTION
  382.  *  
  383.  * PARAMETERS
  384.  *  mem_addr            [?]         [?]
  385.  *  size                [IN]        
  386.  *  subpool_size        [?]         [?]
  387.  *  islogging           [IN]        
  388.  * RETURNS
  389.  *  
  390.  *****************************************************************************/
  391. static KAL_ADM_ID kal_adm_create(void *mem_addr, kal_uint32 size, kal_uint32 *subpool_size, kal_bool islogging)
  392. {
  393.     /*----------------------------------------------------------------*/
  394.     /* Local Variables                                                */
  395.     /*----------------------------------------------------------------*/
  396.     static int make_compiler_happy = 1;
  397.     /*----------------------------------------------------------------*/
  398.     /* Code Body                                                      */
  399.     /*----------------------------------------------------------------*/
  400.     return &make_compiler_happy;
  401. }
  402. /*****************************************************************************
  403.  * FUNCTION
  404.  *  kal_adm_free
  405.  * DESCRIPTION
  406.  *  
  407.  * PARAMETERS
  408.  *  adm_id          [IN]        
  409.  *  mem_addr        [?]         [?]
  410.  * RETURNS
  411.  *  void
  412.  *****************************************************************************/
  413. static void kal_adm_free(KAL_ADM_ID adm_id, void *mem_addr)
  414. {
  415.     /*----------------------------------------------------------------*/
  416.     /* Local Variables                                                */
  417.     /*----------------------------------------------------------------*/
  418.     /*----------------------------------------------------------------*/
  419.     /* Code Body                                                      */
  420.     /*----------------------------------------------------------------*/
  421.     free(mem_addr);
  422.     return;
  423. }
  424. /*****************************************************************************
  425.  * FUNCTION
  426.  *  kal_adm_internal_alloc
  427.  * DESCRIPTION
  428.  *  
  429.  * PARAMETERS
  430.  *  adm_id          [IN]        
  431.  *  size            [IN]        
  432.  *  filename        [?]         [?]
  433.  *  line            [IN]        
  434.  * RETURNS
  435.  *  void
  436.  *****************************************************************************/
  437. static void *kal_adm_internal_alloc(KAL_ADM_ID adm_id, kal_uint32 size, char *filename, kal_uint32 line)
  438. {
  439.     /*----------------------------------------------------------------*/
  440.     /* Local Variables                                                */
  441.     /*----------------------------------------------------------------*/
  442.     /*----------------------------------------------------------------*/
  443.     /* Code Body                                                      */
  444.     /*----------------------------------------------------------------*/
  445.     return malloc(size);
  446. }
  447. /*****************************************************************************
  448.  * FUNCTION
  449.  *  kal_adm_delete
  450.  * DESCRIPTION
  451.  *  
  452.  * PARAMETERS
  453.  *  adm_id      [IN]        
  454.  * RETURNS
  455.  *  
  456.  *****************************************************************************/
  457. static kal_status kal_adm_delete(KAL_ADM_ID adm_id)
  458. {
  459.     /*----------------------------------------------------------------*/
  460.     /* Local Variables                                                */
  461.     /*----------------------------------------------------------------*/
  462.     /*----------------------------------------------------------------*/
  463.     /* Code Body                                                      */
  464.     /*----------------------------------------------------------------*/
  465.     return KAL_SUCCESS;
  466. }
  467. #endif /* MMI_ON_WIN32 */ 
  468. /*****************************************************************************
  469.  * FUNCTION
  470.  *  mmi_frm_resmem_rec_add
  471.  * DESCRIPTION
  472.  *  add record of resource memory occupation
  473.  * PARAMETERS
  474.  *  mem_type        [IN]        Memory type
  475.  *  id              [IN]        Resource ID
  476.  *  ptr             [IN]        Pointer to resource in RAM
  477.  *  size            [IN]        Size of the resource
  478.  * RETURNS
  479.  *  When succeed MMI_TRUE; otherwise MMI_FALSE
  480.  *****************************************************************************/
  481. static MMI_BOOL mmi_frm_resmem_rec_add(mmi_frm_resmem_enum mem_type, U16 id, U8 *ptr, U32 size)
  482. {
  483.     /*----------------------------------------------------------------*/
  484.     /* Local Variables                                                */
  485.     /*----------------------------------------------------------------*/
  486.     U16 i, number = 0;
  487.     mmi_frm_resmem_alloc_struct *rec = NULL;
  488.     /*----------------------------------------------------------------*/
  489.     /* Code Body                                                      */
  490.     /*----------------------------------------------------------------*/
  491.     MMI_ASSERT(id != MMI_FRM_RESMEM_INVALID_RES_ID);
  492.     MMI_ASSERT(ptr != NULL);
  493.     if (mem_type == MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND)
  494.     {
  495.         number = MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER;
  496.         rec = g_mmi_frm_resmem_cntx.lod_ary[g_mmi_frm_resmem_cntx.lod_active_screen];
  497.     }
  498.     else if (mem_type == MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST)
  499.     {
  500.         number = MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER;
  501.         rec = g_mmi_frm_resmem_cntx.always_ary;
  502.     }
  503.     else
  504.     {
  505.         MMI_ASSERT(0);
  506.     }
  507.     for (i = 0; i < number; i++, rec++)
  508.     {
  509.         if (rec->mem_ptr == NULL)
  510.         {
  511.             MMI_ASSERT(rec->id == MMI_FRM_RESMEM_INVALID_RES_ID);
  512.             rec->id = id;
  513.             rec->mem_ptr = ptr;
  514.             rec->size = size;
  515.             return MMI_TRUE;
  516.         }
  517.     }
  518.     /* empty item is no found. Call the module's owner to handle this situation */
  519.     MMI_ASSERT(0);
  520.     return MMI_FALSE;
  521. }
  522. /*****************************************************************************
  523.  * FUNCTION
  524.  *  mmi_frm_resmem_rec_del
  525.  * DESCRIPTION
  526.  *  init the module
  527.  * PARAMETERS
  528.  *  mem_type        [IN]        Memory type
  529.  *  id              [IN]        Resource ID
  530.  * RETURNS
  531.  *  resource pointer; NULL if no record allocated
  532.  *****************************************************************************/
  533. static U8 *mmi_frm_resmem_rec_del(mmi_frm_resmem_enum mem_type, U16 id)
  534. {
  535.     /*----------------------------------------------------------------*/
  536.     /* Local Variables                                                */
  537.     /*----------------------------------------------------------------*/
  538.     U16 i, number = 0;
  539.     mmi_frm_resmem_alloc_struct *rec = NULL;
  540.     U8 *ret_ptr;
  541.     /*----------------------------------------------------------------*/
  542.     /* Code Body                                                      */
  543.     /*----------------------------------------------------------------*/
  544.     MMI_ASSERT(id != MMI_FRM_RESMEM_INVALID_RES_ID);
  545.     if (mem_type == MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND)
  546.     {
  547.         number = MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER;
  548.         rec = g_mmi_frm_resmem_cntx.lod_ary[g_mmi_frm_resmem_cntx.lod_active_screen];
  549.     }
  550.     else if (mem_type == MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST)
  551.     {
  552.         number = MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER;
  553.         rec = g_mmi_frm_resmem_cntx.always_ary;
  554.     }
  555.     else
  556.     {
  557.         MMI_ASSERT(0);
  558.     }
  559.     for (i = 0; i < number; i++, rec++)
  560.     {
  561.         if (rec->id == id)
  562.         {
  563.             rec->id = MMI_FRM_RESMEM_INVALID_RES_ID;
  564.             ret_ptr = rec->mem_ptr;
  565.             rec->mem_ptr = NULL;
  566.             return ret_ptr;
  567.         }
  568.     }
  569.     return NULL;
  570. }
  571. /*****************************************************************************
  572.  * FUNCTION
  573.  *  mmi_frm_resmem_rec_del_one
  574.  * DESCRIPTION
  575.  *  delete one record of resource memory occupation, if available
  576.  * PARAMETERS
  577.  *  mem_type            [IN]        Memory type
  578.  *  which_screen        [IN]        Which screen, indentifying what managment table to do the deleting. only valid for LOD
  579.  *  id                  [OUT]       Which ID. If not interested, pass in NULL
  580.  * RETURNS
  581.  *  resource pointer; NULL if no record allocated
  582.  *****************************************************************************/
  583. static U8 *mmi_frm_resmem_rec_del_one(mmi_frm_resmem_enum mem_type, U16 which_screen, U16 *id)
  584. {
  585.     /*----------------------------------------------------------------*/
  586.     /* Local Variables                                                */
  587.     /*----------------------------------------------------------------*/
  588.     U16 i, number = 0;
  589.     mmi_frm_resmem_alloc_struct *rec = NULL;
  590.     U8 *ret_ptr;
  591.     /*----------------------------------------------------------------*/
  592.     /* Code Body                                                      */
  593.     /*----------------------------------------------------------------*/
  594.     if (mem_type == MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND)
  595.     {
  596.         MMI_ASSERT(which_screen < MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER);
  597.         number = MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER;
  598.         rec = g_mmi_frm_resmem_cntx.lod_ary[which_screen];
  599.     }
  600.     else if (mem_type == MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST)
  601.     {
  602.         number = MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER;
  603.         rec = g_mmi_frm_resmem_cntx.always_ary;
  604.     }
  605.     else
  606.     {
  607.         MMI_ASSERT(0);
  608.     }
  609.     /* TBD. enhance performance */
  610.     for (i = 0; i < number; i++, rec++)
  611.     {
  612.         if (rec->id != MMI_FRM_RESMEM_INVALID_RES_ID)
  613.         {
  614.             MMI_ASSERT(rec->mem_ptr != NULL);
  615.             if (id != NULL)
  616.             {
  617.                 *id = rec->id;
  618.             }
  619.             ret_ptr = rec->mem_ptr;
  620.             rec->id = MMI_FRM_RESMEM_INVALID_RES_ID;
  621.             rec->mem_ptr = NULL;
  622.             rec->size = 0;
  623.             return ret_ptr;
  624.         }
  625.     }
  626.     return NULL;
  627. }
  628. /*****************************************************************************
  629.  * FUNCTION
  630.  *  mmi_frm_resmem_rec_if_exist
  631.  * DESCRIPTION
  632.  *  query whether the resource is existing in memory based on records
  633.  * PARAMETERS
  634.  *  flag        [IN]        Bit-wise flag: combination of MMI_FRM_RESMEM_MEMORY_LOAD_ON_DEMAND, MMI_FRM_RESMEM_MEMORY_ALWAYS_EXIST
  635.  *  id          [IN]        Resource ID
  636.  *  size        [OUT]       Place to receive resource size. It not of interest, pass in NULL
  637.  * RETURNS
  638.  *  if exist, return the memory pointer; otherwise, NULL
  639.  *****************************************************************************/
  640. static U8 *mmi_frm_resmem_rec_if_exist(U32 flag, U16 id, U32 *size)
  641. {
  642.     /*----------------------------------------------------------------*/
  643.     /* Local Variables                                                */
  644.     /*----------------------------------------------------------------*/
  645.     U16 j, i, number;
  646.     mmi_frm_resmem_alloc_struct *rec;
  647.     /*----------------------------------------------------------------*/
  648.     /* Code Body                                                      */
  649.     /*----------------------------------------------------------------*/
  650.     MMI_ASSERT((flag & (~(MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST | MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND))) == 0);
  651.     MMI_ASSERT(id != MMI_FRM_RESMEM_INVALID_RES_ID);
  652.     for (j = 0;; j++)
  653.     {
  654.         if (j == 0)
  655.         {
  656.             if (flag & MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST)
  657.             {
  658.                 number = MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER;
  659.                 rec = g_mmi_frm_resmem_cntx.always_ary;
  660.             }
  661.             else
  662.             {
  663.                 continue;
  664.             }
  665.         }
  666.         else if (j == 1)
  667.         {
  668.             if (flag & MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND)
  669.             {
  670.                 number = MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER;
  671.                 rec = g_mmi_frm_resmem_cntx.lod_ary[g_mmi_frm_resmem_cntx.lod_active_screen];
  672.             }
  673.             else
  674.             {
  675.                 continue;
  676.             }
  677.         }
  678.         else
  679.         {
  680.             /* not found */
  681.             break;
  682.         }
  683.         for (i = 0; i < number; i++, rec++)
  684.         {
  685.             if (rec->id == id)
  686.             {
  687.                 if (size != NULL)
  688.                 {
  689.                     *size = rec->size;
  690.                 }
  691.                 return rec->mem_ptr;
  692.             }
  693.         }
  694.     }
  695.     return NULL;
  696. }
  697. /*****************************************************************************
  698.  * FUNCTION
  699.  *  mmi_frm_resmem_if_res_to_always_keep
  700.  * DESCRIPTION
  701.  *  query whether to keep the specified resource
  702.  * PARAMETERS
  703.  *  id      [IN]        Resource ID
  704.  * RETURNS
  705.  *  MMI_TRUE if necessary
  706.  *****************************************************************************/
  707. static MMI_BOOL mmi_frm_resmem_if_res_to_always_keep(U16 id)
  708. {
  709.     /*----------------------------------------------------------------*/
  710.     /* Local Variables                                                */
  711.     /*----------------------------------------------------------------*/
  712.     U16 i;
  713.     /*----------------------------------------------------------------*/
  714.     /* Code Body                                                      */
  715.     /*----------------------------------------------------------------*/
  716.     for (i = 0; i < MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER; i++)
  717.     {
  718.         if (g_mmi_frm_resmem_cntx.always_keep_ID[i] == id)
  719.         {
  720.             return MMI_TRUE;
  721.         }
  722.     }
  723.     return MMI_FALSE;
  724. }
  725. #ifdef MMI_FRM_RESMEM_UNIT_TEST
  726. /*****************************************************************************
  727.  * FUNCTION
  728.  *  mmi_frm_resmem_unit_test
  729.  * DESCRIPTION
  730.  *  
  731.  * PARAMETERS
  732.  *  void
  733.  * RETURNS
  734.  *  void
  735.  *****************************************************************************/
  736. void mmi_frm_resmem_unit_test(void)
  737. {
  738.     /*----------------------------------------------------------------*/
  739.     /* Local Variables                                                */
  740.     /*----------------------------------------------------------------*/
  741.     U8 *ptr;
  742.     /*----------------------------------------------------------------*/
  743.     /* Code Body                                                      */
  744.     /*----------------------------------------------------------------*/
  745.     mmi_frm_resmem_init();
  746.     /* test case: load single image */
  747.     {
  748.         ptr = mmi_frm_resmem_if_ramed(2);
  749.         mmi_frm_resmem_load_single_image(2, 2000, 22);
  750.         mmi_frm_resmem_load_single_image(4, 4000, 444);
  751.         mmi_frm_resmem_load_single_image(5, 5000, 5555);
  752.         ptr = mmi_frm_resmem_load_single_image(4, 4000, 444);
  753.         ptr = mmi_frm_resmem_if_ramed(2);
  754.     }
  755.     /* test case: reset LOD memory */
  756.     {
  757.         mmi_frm_resmem_reset(MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND);
  758.         ptr = mmi_frm_resmem_if_ramed(2);
  759.     }
  760.     /* test case: load array image */
  761.     {
  762.         static mmi_frm_resmem_res_info_struct info_array[10];
  763.         ptr = mmi_frm_resmem_if_ramed(7);
  764.         info_array[0].id = 1;
  765.         info_array[0].offset = 1000;
  766.         info_array[0].size = 111;
  767.         info_array[1].id = 2;
  768.         info_array[1].offset = 2000;
  769.         info_array[1].size = 222;
  770.         info_array[2].id = 7;
  771.         info_array[2].offset = 7000;
  772.         info_array[2].size = 777;
  773.         info_array[3].id = 8;
  774.         info_array[3].offset = 8000;
  775.         info_array[3].size = 888;
  776.         ptr = mmi_frm_resmem_load_multi_image(7, info_array, 4);
  777.         ptr = mmi_frm_resmem_if_ramed(7);
  778.     }
  779.     /* test case: load single image */
  780.     {
  781.         U16 i;
  782.         ptr = mmi_frm_resmem_if_ramed(15);
  783.         for (i = 1; i < 20; i++)
  784.         {
  785.             mmi_frm_resmem_load_single_image(i, i * 20, i * 20);
  786.         }
  787.         ptr = mmi_frm_resmem_if_ramed(10);
  788.     }
  789.     /* test case: load image to Always Exist area */
  790.     {
  791.         U16 i;
  792.         ptr = mmi_frm_resmem_if_ramed(21);
  793.         for (i = 10; i < 30; i++)
  794.         {
  795.             mmi_frm_resmem_reg_retain(i);
  796.             mmi_frm_resmem_load_single_image(i, i * 20, i * 20);
  797.         }
  798.         ptr = mmi_frm_resmem_if_ramed(21);
  799.     }
  800.     /* test case: remove image from Always Exist area */
  801.     {
  802.         U16 i;
  803.         ptr = mmi_frm_resmem_if_ramed(21);
  804.         for (i = 10; i < 30; i++)
  805.         {
  806.             mmi_frm_resmem_dereg_retain(i);
  807.         }
  808.         ptr = mmi_frm_resmem_if_ramed(21);
  809.     }
  810.     return;
  811. }
  812. #endif /* MMI_FRM_RESMEM_UNIT_TEST */ 
  813. /*****************************************************************************              
  814.  * Extern Global Variable                                                                    
  815.  *****************************************************************************/
  816. /*****************************************************************************              
  817.  * Extern Global Function                                                                    
  818.  *****************************************************************************/
  819. /*****************************************************************************
  820.  * FUNCTION
  821.  *  mmi_frm_resmem_init
  822.  * DESCRIPTION
  823.  *  init the module
  824.  * PARAMETERS
  825.  *  void
  826.  * RETURNS
  827.  *  void
  828.  *****************************************************************************/
  829. void mmi_frm_resmem_init(void)
  830. {
  831.     /*----------------------------------------------------------------*/
  832.     /* Local Variables                                                */
  833.     /*----------------------------------------------------------------*/
  834.     U16 i, j;
  835.     /*----------------------------------------------------------------*/
  836.     /* Code Body                                                      */
  837.     /*----------------------------------------------------------------*/
  838.     memset(&g_mmi_frm_resmem_cntx, 0, sizeof(mmi_frm_resmem_context_struct));
  839.     /* init ID table */
  840.     for (j = 0; j < MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER; j++)
  841.     {
  842.         for (i = 0; i < MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER; i++)
  843.         {
  844.             g_mmi_frm_resmem_cntx.lod_ary[j][i].id = MMI_FRM_RESMEM_INVALID_RES_ID;
  845.         }
  846.     }
  847.     for (i = 0; i < MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER; i++)
  848.     {
  849.         g_mmi_frm_resmem_cntx.always_ary[i].id = MMI_FRM_RESMEM_INVALID_RES_ID;
  850.     }
  851.     /* init retain-ID registration table */
  852.     for (i = 0; i < MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER; i++)
  853.     {
  854.         g_mmi_frm_resmem_cntx.always_keep_ID[i] = MMI_FRM_RESMEM_INVALID_RES_ID;
  855.     }
  856.     /* init malloc record table */
  857.     for (i = 0; i < MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER; i++)
  858.     {
  859.         g_mmi_frm_resmem_cntx.adm_lod_id[i] = kal_adm_create(
  860.                                                 g_mmi_frm_resmem_lod_pool[i],
  861.                                                 MMI_FRM_RESMEM_POOL_LOD_ON_DEMAND_SIZE,
  862.                                                 (kal_uint32*) g_mmi_frm_resmem_mem_pool_chunk_size,
  863.                                                 KAL_TRUE);
  864.         MMI_ASSERT(g_mmi_frm_resmem_cntx.adm_lod_id[i] != (KAL_ADM_ID) NULL);
  865.     }
  866.     g_mmi_frm_resmem_cntx.adm_always_id = kal_adm_create(
  867.                                             g_mmi_frm_resmem_always_pool,
  868.                                             MMI_FRM_RESMEM_POOL_ALWASY_EXIST_SIZE,
  869.                                             (kal_uint32*) g_mmi_frm_resmem_mem_pool_chunk_size,
  870.                                             KAL_TRUE);
  871.     MMI_ASSERT(g_mmi_frm_resmem_cntx.adm_always_id != (KAL_ADM_ID) NULL);
  872.     g_mmi_frm_resmem_cntx.lod_active_screen = 0;
  873. #if !defined(__MTK_TARGET__)
  874. #if !defined(MMI_ON_WIN32)
  875.     FS_SanityCheck();
  876. #endif 
  877. #endif /* !defined(__MTK_TARGET__) */ 
  878.     return;
  879. }
  880. /*****************************************************************************
  881.  * FUNCTION
  882.  *  mmi_frm_resmem_if_ramed
  883.  * DESCRIPTION
  884.  *  check whether the memory of the specificed resurce is existing in memory.
  885.  * PARAMETERS
  886.  *  id      [IN]        Resource ID
  887.  * RETURNS
  888.  *  if exist, the pointer of RAM ; otherwise, NULL
  889.  *****************************************************************************/
  890. U8 *mmi_frm_resmem_if_ramed(U16 id)
  891. {
  892.     /*----------------------------------------------------------------*/
  893.     /* Local Variables                                                */
  894.     /*----------------------------------------------------------------*/
  895.     U8 *ret;
  896.     /*----------------------------------------------------------------*/
  897.     /* Code Body                                                      */
  898.     /*----------------------------------------------------------------*/
  899.     MMI_FRM_RESMEM_CS_ENTER;
  900.     MMI_ASSERT(id != MMI_FRM_RESMEM_INVALID_RES_ID);
  901.     ret = mmi_frm_resmem_rec_if_exist((MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST | MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND), id, NULL);
  902.     MMI_FRM_RESMEM_CS_LEAVE;
  903.     return ret;
  904. }
  905. /*****************************************************************************
  906.  * FUNCTION
  907.  *  mmi_frm_resmem_load_single_image
  908.  * DESCRIPTION
  909.  *  load the specificed resurce into memory.
  910.  * PARAMETERS
  911.  *  id          [IN]        Resource ID
  912.  *  offset      [IN]        Offset in bin
  913.  *  size        [IN]        Resource size
  914.  * RETURNS
  915.  *  memory pointer; NULL if not successful
  916.  *****************************************************************************/
  917. U8 *mmi_frm_resmem_load_single_image(U16 id, U32 offset, U32 size)
  918. {
  919.     /*----------------------------------------------------------------*/
  920.     /* Local Variables                                                */
  921.     /*----------------------------------------------------------------*/
  922.     U32 buf_size;
  923.     U8 *ret_ptr = NULL;
  924.     kal_bool kal_rslt;
  925.     KAL_ADM_ID adm_id;
  926.     MMI_BOOL ret_bool;
  927.     mmi_frm_resmem_enum mem_type;
  928.     /*----------------------------------------------------------------*/
  929.     /* Code Body                                                      */
  930.     /*----------------------------------------------------------------*/
  931.     MMI_FRM_RESMEM_CS_ENTER;
  932.     MMI_ASSERT(id != MMI_FRM_RESMEM_INVALID_RES_ID);
  933.     MMI_ASSERT(size != 0);
  934.     /* PC-Simulator did not call thie init as expected, so do here again */
  935. #if !defined(__MTK_TARGET__)
  936. #if defined(MMI_ON_WIN32)
  937.     if (g_mmi_frm_resmem_cntx.adm_always_id == (KAL_ADM_ID) NULL)
  938.     {
  939.         mmi_frm_resmem_init();
  940.     }
  941. #endif /* defined(MMI_ON_WIN32) */ 
  942. #endif /* !defined(__MTK_TARGET__) */ 
  943.     /* check whether already exist in RAM */
  944.     ret_ptr = mmi_frm_resmem_if_ramed(id);
  945.     if (ret_ptr != NULL)
  946.     {
  947.         MMI_FRM_RESMEM_CS_LEAVE;
  948.         return ret_ptr;
  949.     }
  950.     /* decide which kind of memory */
  951.     if (mmi_frm_resmem_if_res_to_always_keep(id) == MMI_TRUE)
  952.     {
  953.         mem_type = MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST;
  954.         adm_id = g_mmi_frm_resmem_cntx.adm_always_id;
  955.     }
  956.     else
  957.     {
  958.         mem_type = MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND;
  959.         adm_id = g_mmi_frm_resmem_cntx.adm_lod_id[g_mmi_frm_resmem_cntx.lod_active_screen];
  960.     }
  961.     MMI_ASSERT(adm_id != (KAL_ADM_ID) NULL);
  962.     /* allocate RAM block */
  963.     buf_size = size;
  964. #if defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER)
  965.     buf_size = (buf_size >= MMI_FRM_RESMEM_SPOT_ICON_RESERVED_SIZE ? buf_size : MMI_FRM_RESMEM_SPOT_ICON_RESERVED_SIZE);
  966. #endif 
  967.     ret_ptr = kal_adm_alloc(adm_id, buf_size);
  968.     /* Check memory alloc success or not. 
  969.        If assertion failure here, maybe result from
  970.        - image/animation/video size increased, or
  971.        - more images are added in one screen, or
  972.        - mmi_frm_resmem_reset() is not called when screen change, or
  973.        - macro MMI_FRM_RESMEM_POOL_LOD_ON_DEMAND_SIZE value is too small */
  974.     MMI_ASSERT(ret_ptr != NULL);
  975.     /* 4-byte alignment for GDI API requirement, at least necessary for target */
  976.     MMI_ASSERT(((U32) ret_ptr & 0x00000003) == 0);
  977.     if (mem_type == MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND)
  978.     {
  979.         g_mmi_frm_resmem_cntx.lod_mem_using += buf_size;
  980.         if (g_mmi_frm_resmem_cntx.lod_mem_using > g_mmi_frm_resmem_cntx.lod_mem_max_used)
  981.         {
  982.             g_mmi_frm_resmem_cntx.lod_mem_max_used = g_mmi_frm_resmem_cntx.lod_mem_using;
  983.             MMI_ASSERT(g_mmi_frm_resmem_cntx.lod_mem_max_used < MMI_FRM_RESMEM_POOL_LOD_ON_DEMAND_SIZE);
  984.         }
  985.     }
  986.     else
  987.     {
  988.         g_mmi_frm_resmem_cntx.always_mem_using += buf_size;
  989.         if (g_mmi_frm_resmem_cntx.always_mem_using > g_mmi_frm_resmem_cntx.always_mem_max_used)
  990.         {
  991.             g_mmi_frm_resmem_cntx.always_mem_max_used = g_mmi_frm_resmem_cntx.always_mem_using;
  992.             MMI_ASSERT(g_mmi_frm_resmem_cntx.always_mem_max_used < MMI_FRM_RESMEM_POOL_ALWASY_EXIST_SIZE);
  993.         }
  994.     }
  995.     /* load via driver */
  996.     kal_rslt = NFB_RES_Load(ENFB_CONTENT_IMAGERES, (void*)ret_ptr, offset, size);
  997.     MMI_ASSERT(kal_rslt == KAL_TRUE);
  998.     if (kal_rslt == KAL_FALSE)
  999.     {
  1000.         kal_adm_free(adm_id, ret_ptr);
  1001.         ret_ptr = NULL;
  1002.         MMI_FRM_RESMEM_CS_LEAVE;
  1003.         return NULL;
  1004.     }
  1005.     /* update managment records */
  1006.     ret_bool = mmi_frm_resmem_rec_add(mem_type, id, ret_ptr, size);
  1007.     MMI_ASSERT(ret_bool == MMI_TRUE);
  1008.     MMI_FRM_RESMEM_CS_LEAVE;
  1009.     return ret_ptr;
  1010. }
  1011. /*****************************************************************************
  1012.  * FUNCTION
  1013.  *  mmi_frm_resmem_load_multi_image
  1014.  * DESCRIPTION
  1015.  *  load the specificed resurce into memory. Since the resource in in a
  1016.  *  multi-res pack, it is better to one, rather than multiple, access to ROM.
  1017.  * PARAMETERS
  1018.  *  id              [IN]        Resource ID
  1019.  *  info_array      [IN]        Array the stores the resource-pack info, which allows single access
  1020.  *  res_cnt         [IN]        The number of resource in info_array
  1021.  * RETURNS
  1022.  *  memory pointer; NULL if not successful
  1023.  *****************************************************************************/
  1024. U8 *mmi_frm_resmem_load_multi_image(U16 id, const mmi_frm_resmem_res_info_struct *info_array, U32 res_cnt)
  1025. {
  1026.     /*----------------------------------------------------------------*/
  1027.     /* Local Variables                                                */
  1028.     /*----------------------------------------------------------------*/
  1029.     U32 buf_size;
  1030.     U8 i;
  1031.     U8 *ret_ptr = 0;
  1032.     static enfb_scatter scatter[MMI_FRM_RESMEM_SCATTER_MAX_NUMBER];
  1033.     kal_bool kal_rslt;
  1034.     KAL_ADM_ID adm_id;
  1035.     MMI_BOOL ret_bool;
  1036.     mmi_frm_resmem_enum mem_type;
  1037.     const mmi_frm_resmem_res_info_struct *info;
  1038.     /*----------------------------------------------------------------*/
  1039.     /* Code Body                                                      */
  1040.     /*----------------------------------------------------------------*/
  1041.     MMI_FRM_RESMEM_CS_ENTER;
  1042.     MMI_ASSERT(id != MMI_FRM_RESMEM_INVALID_RES_ID);
  1043.     MMI_ASSERT(info_array != 0);
  1044.     MMI_ASSERT(res_cnt != 0);
  1045.     MMI_ASSERT(res_cnt < MMI_FRM_RESMEM_SCATTER_MAX_NUMBER);
  1046.     /* check whether already exist in in RAM */
  1047.     ret_ptr = mmi_frm_resmem_if_ramed(id);
  1048.     if (ret_ptr != NULL)
  1049.     {
  1050.         MMI_FRM_RESMEM_CS_LEAVE;
  1051.         return ret_ptr;
  1052.     }
  1053.     /* decide which kind of memory */
  1054.     if (mmi_frm_resmem_if_res_to_always_keep(id) == MMI_TRUE)
  1055.     {
  1056.         mem_type = MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST;
  1057.         adm_id = g_mmi_frm_resmem_cntx.adm_always_id;
  1058.     }
  1059.     else
  1060.     {
  1061.         mem_type = MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND;
  1062.         adm_id = g_mmi_frm_resmem_cntx.adm_lod_id[g_mmi_frm_resmem_cntx.lod_active_screen];
  1063.     }
  1064.     MMI_ASSERT(adm_id != (KAL_ADM_ID) NULL);
  1065.     /* allocate RAM block */
  1066.     for (i = 0, info = info_array; i < res_cnt; i++, info++)
  1067.     {
  1068.         buf_size = info->size;
  1069.     #if defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER)
  1070.         buf_size =
  1071.             (buf_size >= MMI_FRM_RESMEM_SPOT_ICON_RESERVED_SIZE ? buf_size : MMI_FRM_RESMEM_SPOT_ICON_RESERVED_SIZE);
  1072.     #endif /* defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER) */ 
  1073.         ret_ptr = kal_adm_alloc(adm_id, buf_size);
  1074.         MMI_ASSERT(ret_ptr != NULL);
  1075.         /* 4-byte alignment for GDI API requirement, at least necessary for target */
  1076.         MMI_ASSERT(((U32) ret_ptr & 0x00000003) == 0);
  1077.         scatter[i].dst_addr = ret_ptr;
  1078.         scatter[i].offset = info->offset;
  1079.         scatter[i].length = info->size;
  1080.         if (mem_type == MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND)
  1081.         {
  1082.             g_mmi_frm_resmem_cntx.lod_mem_using += buf_size;
  1083.             if (g_mmi_frm_resmem_cntx.lod_mem_using > g_mmi_frm_resmem_cntx.lod_mem_max_used)
  1084.             {
  1085.                 g_mmi_frm_resmem_cntx.lod_mem_max_used = g_mmi_frm_resmem_cntx.lod_mem_using;
  1086.                 MMI_ASSERT(g_mmi_frm_resmem_cntx.lod_mem_max_used < MMI_FRM_RESMEM_POOL_LOD_ON_DEMAND_SIZE);
  1087.             }
  1088.         }
  1089.         else
  1090.         {
  1091.             g_mmi_frm_resmem_cntx.always_mem_using += buf_size;
  1092.             if (g_mmi_frm_resmem_cntx.always_mem_using > g_mmi_frm_resmem_cntx.always_mem_max_used)
  1093.             {
  1094.                 g_mmi_frm_resmem_cntx.always_mem_max_used = g_mmi_frm_resmem_cntx.always_mem_using;
  1095.             }
  1096.             MMI_ASSERT(g_mmi_frm_resmem_cntx.always_mem_max_used < MMI_FRM_RESMEM_POOL_ALWASY_EXIST_SIZE);
  1097.         }
  1098.     }
  1099.     /* load via driver */
  1100.     kal_rslt = NFB_RES_Loadv(ENFB_CONTENT_IMAGERES, scatter, res_cnt);
  1101.     MMI_ASSERT(kal_rslt == KAL_TRUE);
  1102.     if (kal_rslt == KAL_FALSE)
  1103.     {
  1104.         for (i = 0; i < res_cnt; i++)
  1105.         {
  1106.             kal_adm_free(adm_id, scatter[i].dst_addr);
  1107.         }
  1108.         ret_ptr = NULL;
  1109.         MMI_FRM_RESMEM_CS_LEAVE;
  1110.         return NULL;
  1111.     }
  1112.     /* update managment records */
  1113.     for (i = 0, info = info_array; i < res_cnt; i++, info++)
  1114.     {
  1115.         ret_bool = mmi_frm_resmem_rec_add(mem_type, info->id, scatter[i].dst_addr, scatter[i].length);
  1116.         MMI_ASSERT(ret_bool == MMI_TRUE);
  1117.         if (id == info->id)
  1118.         {
  1119.             ret_ptr = scatter[i].dst_addr;
  1120.         }
  1121.     }
  1122.     MMI_FRM_RESMEM_CS_LEAVE;
  1123.     return ret_ptr;
  1124. }
  1125. /*****************************************************************************
  1126.  * FUNCTION
  1127.  *  mmi_frm_resmem_reset
  1128.  * DESCRIPTION
  1129.  *  check whether the memory of the specificed resurce is existing in memory.
  1130.  * PARAMETERS
  1131.  *  flag        [IN]        Bit-wise flag: combination of MMI_FRM_RESMEM_MEMORY_LOAD_ON_DEMAND, MMI_FRM_RESMEM_MEMORY_ALWAYS_EXIST
  1132.  * RETURNS
  1133.  *  void
  1134.  *****************************************************************************/
  1135. void mmi_frm_resmem_reset(U32 flag)
  1136. {
  1137.     /*----------------------------------------------------------------*/
  1138.     /* Local Variables                                                */
  1139.     /*----------------------------------------------------------------*/
  1140.     U8 *mem_addr;
  1141. #if defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER)
  1142.     S32 size;
  1143. #endif 
  1144.     /*----------------------------------------------------------------*/
  1145.     /* Code Body                                                      */
  1146.     /*----------------------------------------------------------------*/
  1147.     MMI_FRM_RESMEM_CS_ENTER;
  1148.     if (flag & MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND)
  1149.     {
  1150.     #if defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER)
  1151.         /* display "X" icon for invlaid pointer usage */
  1152.         U16 i, number;
  1153.         mmi_frm_resmem_alloc_struct *rec;
  1154.     U8 *res_src = NULL;
  1155.         number = MMI_FRM_RESMEM_LOAD_ON_DEMAND_NUMBER;
  1156.         rec = g_mmi_frm_resmem_cntx.lod_ary[g_mmi_frm_resmem_cntx.lod_active_screen];
  1157.         for (i = 0; i < number; i++, rec++)
  1158.         {
  1159.             if (rec->mem_ptr != NULL)
  1160.             {
  1161.                 /* dangling pointer. fill in special icon. this icon is not in 3rd ROM */
  1162.                 res_src = NULL;
  1163.                 res_src = (U8*) GetImage(IMG_GLOBAL_DEFAULT);
  1164.                 MMI_ASSERT(res_src != NULL);
  1165.                 size = ((S32) (res_src[2]) |
  1166.                         (S32) ((S32) res_src[3] << 8) |
  1167.                         (S32) ((S32) res_src[4] << 16) | (S32) ((S32) res_src[5] << 24));
  1168.                 MMI_ASSERT(MMI_FRM_RESMEM_SPOT_ICON_RESERVED_SIZE > size);
  1169.                 memcpy((char*)rec->mem_ptr, res_src, size);
  1170.                 rec->size = size;
  1171.             }
  1172.         }
  1173.         /* prepare the memory for next screen usage */
  1174.         g_mmi_frm_resmem_cntx.lod_active_screen++;
  1175.         g_mmi_frm_resmem_cntx.lod_active_screen %= MMI_FRM_RESMEM_LOAD_ON_DEMAND_SCREEN_NUMBER;
  1176.     #else /* defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER) */ 
  1177.         MMI_ASSERT(g_mmi_frm_resmem_cntx.lod_active_screen == 0);
  1178.     #endif /* defined(MMI_FRM_RESMEM_DEBUG_DANGLING_POINTER) */ 
  1179.         /* delete and create again */
  1180.         do
  1181.         {
  1182.             mem_addr = mmi_frm_resmem_rec_del_one(
  1183.                         MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND,
  1184.                         g_mmi_frm_resmem_cntx.lod_active_screen,
  1185.                         NULL);
  1186.             if (mem_addr == NULL)
  1187.             {
  1188.                 break;
  1189.             }
  1190.             kal_adm_free(g_mmi_frm_resmem_cntx.adm_lod_id[g_mmi_frm_resmem_cntx.lod_active_screen], mem_addr);
  1191.         } while (1);
  1192.         kal_adm_delete(g_mmi_frm_resmem_cntx.adm_lod_id[g_mmi_frm_resmem_cntx.lod_active_screen]);
  1193.         g_mmi_frm_resmem_cntx.adm_lod_id[g_mmi_frm_resmem_cntx.lod_active_screen] = kal_adm_create(
  1194.                                                                                         g_mmi_frm_resmem_lod_pool[g_mmi_frm_resmem_cntx.lod_active_screen],
  1195.                                                                                         MMI_FRM_RESMEM_POOL_LOD_ON_DEMAND_SIZE,
  1196.                                                                                         (kal_uint32*) g_mmi_frm_resmem_mem_pool_chunk_size,
  1197.                                                                                         KAL_TRUE);
  1198.     }
  1199.     g_mmi_frm_resmem_cntx.lod_mem_using = 0;
  1200.     /* should call this to avoid incorrect displaying because of GDI cache */
  1201.     gdi_image_cache_reset();
  1202.     MMI_FRM_RESMEM_CS_LEAVE;
  1203.     return;
  1204. }
  1205. /*****************************************************************************
  1206.  * FUNCTION
  1207.  *  mmi_frm_resmem_reg_retain
  1208.  * DESCRIPTION
  1209.  *  Register what resource to be keept without auto-clear at screen change
  1210.  *  Please note that if the resource is already allocated in LOD, it will
  1211.  *  be moved Always_Exist area.
  1212.  * PARAMETERS
  1213.  *  id      [IN]        Resource ID
  1214.  * RETURNS
  1215.  *  void
  1216.  *****************************************************************************/
  1217. void mmi_frm_resmem_reg_retain(U16 id)
  1218. {
  1219.     /*----------------------------------------------------------------*/
  1220.     /* Local Variables                                                */
  1221.     /*----------------------------------------------------------------*/
  1222.     U16 i;
  1223.     U8 *mem_addr = NULL, *mem_addr_dst;
  1224.     U32 size;
  1225.     MMI_BOOL ret_bool;
  1226.     /*----------------------------------------------------------------*/
  1227.     /* Code Body                                                      */
  1228.     /*----------------------------------------------------------------*/
  1229.     MMI_FRM_RESMEM_CS_ENTER;
  1230.     /* registration */
  1231.     if (mmi_frm_resmem_if_res_to_always_keep(id) == MMI_FALSE)
  1232.     {
  1233.         for (i = 0; i < MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER; i++)
  1234.         {
  1235.             if (g_mmi_frm_resmem_cntx.always_keep_ID[i] == MMI_FRM_RESMEM_INVALID_RES_ID)
  1236.             {
  1237.                 g_mmi_frm_resmem_cntx.always_keep_ID[i] = id;
  1238.                 break;
  1239.             }
  1240.         }
  1241.     }
  1242.     /* consider already load in memory */
  1243.     /* alreay in Always Exist area? */
  1244.     if (NULL != mmi_frm_resmem_rec_if_exist(MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST, id, NULL))
  1245.     {
  1246.         /* do nothing */
  1247.         MMI_FRM_RESMEM_CS_LEAVE;
  1248.         return;
  1249.     }
  1250.     /* already in LOD area? */
  1251.     mem_addr = mmi_frm_resmem_rec_if_exist(MMI_FRM_RESMEM_TYPE_LOD_ON_DEMAND, id, &size);
  1252.     if (mem_addr != NULL)
  1253.     {
  1254.         /* move to Always Exist area */
  1255.         mem_addr_dst = kal_adm_alloc(g_mmi_frm_resmem_cntx.adm_always_id, size);
  1256.         memcpy(mem_addr_dst, mem_addr, size);
  1257.         g_mmi_frm_resmem_cntx.always_mem_using += size;
  1258.         if (g_mmi_frm_resmem_cntx.always_mem_using > g_mmi_frm_resmem_cntx.always_mem_max_used)
  1259.         {
  1260.             g_mmi_frm_resmem_cntx.always_mem_max_used = g_mmi_frm_resmem_cntx.always_mem_using;
  1261.         }
  1262.         MMI_ASSERT(g_mmi_frm_resmem_cntx.always_mem_max_used < MMI_FRM_RESMEM_POOL_ALWASY_EXIST_SIZE);
  1263.         ret_bool = mmi_frm_resmem_rec_add(MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST, id, mem_addr_dst, size);
  1264.         MMI_ASSERT(ret_bool == MMI_TRUE);
  1265.     #if 0
  1266. /* under construction !*/
  1267. /* under construction !*/
  1268. /* under construction !*/
  1269.     #endif /* 0 */ 
  1270.     }
  1271.     MMI_FRM_RESMEM_CS_LEAVE;
  1272.     return;
  1273. }
  1274. /*****************************************************************************
  1275.  * FUNCTION
  1276.  *  mmi_frm_resmem_dereg_retain
  1277.  * DESCRIPTION
  1278.  *  de-register what resource to be keept without auto-clear at screen change
  1279.  *  Please note that the calling will also clear the memory of specified resource.
  1280.  *  So the allocated pointer, if any, will not valid any more.
  1281.  * PARAMETERS
  1282.  *  id      [IN]        Resource ID
  1283.  * RETURNS
  1284.  *  void
  1285.  *****************************************************************************/
  1286. void mmi_frm_resmem_dereg_retain(U16 id)
  1287. {
  1288.     /*----------------------------------------------------------------*/
  1289.     /* Local Variables                                                */
  1290.     /*----------------------------------------------------------------*/
  1291.     U16 i;
  1292.     U8 *mem_addr;
  1293.     U32 size;
  1294.     /*----------------------------------------------------------------*/
  1295.     /* Code Body                                                      */
  1296.     /*----------------------------------------------------------------*/
  1297.     MMI_FRM_RESMEM_CS_ENTER;
  1298.     for (i = 0; i < MMI_FRM_RESMEM_ALWAYS_EXIST_NUMBER; i++)
  1299.     {
  1300.         if (g_mmi_frm_resmem_cntx.always_keep_ID[i] == id)
  1301.         {
  1302.             g_mmi_frm_resmem_cntx.always_keep_ID[i] = MMI_FRM_RESMEM_INVALID_RES_ID;
  1303.             break;
  1304.         }
  1305.     }
  1306.     /* clear those res de-reg always attribute */
  1307.     mem_addr = mmi_frm_resmem_rec_if_exist(MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST, id, &size);
  1308.     if (mem_addr != NULL)
  1309.     {
  1310.         mmi_frm_resmem_rec_del(MMI_FRM_RESMEM_TYPE_ALWAYS_EXIST, id);
  1311.         g_mmi_frm_resmem_cntx.always_mem_using -= size;
  1312.         kal_adm_free(g_mmi_frm_resmem_cntx.adm_always_id, mem_addr);
  1313.         /* should call this to avoid incorrect displaying because of GDI cache */
  1314.         gdi_image_cache_reset();
  1315.     }
  1316.     MMI_FRM_RESMEM_CS_LEAVE;
  1317.     return;
  1318. }
  1319. /*----------------------------------------------------------------*
  1320.   String resource
  1321.  *----------------------------------------------------------------*/
  1322. /*****************************************************************************
  1323.  * FUNCTION
  1324.  *  mmi_frm_resmem_load_str_suite
  1325.  * DESCRIPTION
  1326.  *  load the specificed string resurce suite into memory.
  1327.  * PARAMETERS
  1328.  *  ptr_ary         [OUT]       Array of memory pointers to string suite
  1329.  *  offset_ary      [IN]        Array of offset positions in flash file
  1330.  *  size_ary        [IN]        Array of size in flash file
  1331.  *  ary_count       [IN]        Number of entries in offset_ary and size_ary
  1332.  * RETURNS
  1333.  *  void
  1334.  *****************************************************************************/
  1335. void mmi_frm_resmem_load_str_suite(U8 **ptr_ary, U32 *offset_ary, U32 *size_ary, U32 ary_count)
  1336. {
  1337.     /*----------------------------------------------------------------*/
  1338.     /* Local Variables                                                */
  1339.     /*----------------------------------------------------------------*/
  1340.     /*----------------------------------------------------------------*/
  1341.     /* Code Body                                                      */
  1342.     /*----------------------------------------------------------------*/
  1343.     MMI_ASSERT(ptr_ary != NULL);
  1344.     MMI_ASSERT(offset_ary != NULL);
  1345.     MMI_ASSERT(size_ary != NULL);
  1346.     MMI_ASSERT(ary_count == 2);
  1347.     /* string content part */
  1348.     NFB_RES_Load(ENFB_CONTENT_STRINGRES, g_mmi_frm_resmem_str_pool, offset_ary[0], size_ary[0]);
  1349.     g_mmi_frm_resmem_cntx.str_content = g_mmi_frm_resmem_str_pool;
  1350.     /* string map part */
  1351.     NFB_RES_Load(ENFB_CONTENT_STRINGRES, g_mmi_frm_resmem_str_pool + size_ary[0], offset_ary[1], size_ary[1]);
  1352.     g_mmi_frm_resmem_cntx.str_map = g_mmi_frm_resmem_str_pool + size_ary[0];
  1353.     ptr_ary[0] = g_mmi_frm_resmem_cntx.str_content;
  1354.     ptr_ary[1] = g_mmi_frm_resmem_cntx.str_map;
  1355.     return;
  1356. }
  1357. #endif /* defined(__MMI_RESOURCE_ENFB_SUPPORT__) */