lvgl_img_loader.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief LVGL image loader interface
  9. */
  10. #ifndef FRAMEWORK_DISPLAY_INCLUDE_LVGL_ANIMIMG_LOADER_H_
  11. #define FRAMEWORK_DISPLAY_INCLUDE_LVGL_ANIMIMG_LOADER_H_
  12. /**
  13. * @defgroup lvgl_image_apis LVGL Image Loader APIs
  14. * @ingroup lvgl_apis
  15. * @{
  16. */
  17. /*********************
  18. * INCLUDES
  19. *********************/
  20. #include "lvgl_res_loader.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**********************
  25. * TYPEDEFS
  26. **********************/
  27. struct lvgl_img_loader;
  28. typedef struct lvgl_img_loader_fn {
  29. /* load a image */
  30. int (*load)(struct lvgl_img_loader * loader, uint16_t index, lv_img_dsc_t * dsc, lv_point_t * offset);
  31. /* (optional) unload a image */
  32. void (*unload)(struct lvgl_img_loader * loader, lv_img_dsc_t * dsc);
  33. /* (optional) preload a image */
  34. void (*preload)(struct lvgl_img_loader * loader, uint16_t index);
  35. /* get image count */
  36. uint16_t (*get_count)(struct lvgl_img_loader * loader);
  37. } lvgl_img_loader_fn_t;
  38. typedef struct lvgl_img_loader {
  39. const lvgl_img_loader_fn_t * fn;
  40. int16_t last_preload_idx;
  41. union {
  42. struct { /* picarray loader */
  43. const lv_img_dsc_t * dsc;
  44. const lv_point_t * offsets;
  45. uint16_t count;
  46. } picarray;
  47. struct { /* picregion loader */
  48. uint32_t scene_id;
  49. lvgl_res_picregion_t * node;
  50. } picregion;
  51. struct { /* picgroup loader */
  52. uint32_t scene_id;
  53. lvgl_res_group_t * node;
  54. const uint32_t * ids;
  55. uint16_t count;
  56. } picgroup;
  57. /* custom loader */
  58. void * user_data;
  59. };
  60. } lvgl_img_loader_t;
  61. /**********************
  62. * GLOBAL PROTOTYPES
  63. **********************/
  64. /**
  65. * Set the image array resource
  66. *
  67. * @param loader pointer to structure lvgl_img_loader
  68. * @param dsc pointer to a series images
  69. * @param offsets position translation array of the images
  70. * @param num number of images
  71. *
  72. * @retval 0 on success else negative code.
  73. */
  74. int lvgl_img_loader_set_picarray(lvgl_img_loader_t * loader,
  75. const lv_img_dsc_t * dsc, const lv_point_t * offsets, uint16_t num);
  76. /**
  77. * Set the picture region resource
  78. *
  79. * @param loader pointer to structure lvgl_img_loader
  80. * @param scene_id scene id
  81. * @param picregion pointer to structure lvgl_res_picregion
  82. *
  83. * @retval 0 on success else negative code.
  84. */
  85. int lvgl_img_loader_set_picregion(lvgl_img_loader_t * loader, uint32_t scene_id,
  86. lvgl_res_picregion_t * picregion);
  87. /**
  88. * Set the picture group resource
  89. *
  90. * @param loader pointer to structure lvgl_img_loader
  91. * @param scene_id scene id
  92. * @param picgroup pointer to structure lvgl_res_group
  93. * @param ids picture ids in the picgroup
  94. * @param num number of pictures
  95. *
  96. * @retval 0 on success else negative code.
  97. */
  98. int lvgl_img_loader_set_picgroup(lvgl_img_loader_t * loader, uint32_t scene_id,
  99. lvgl_res_group_t * picgroup, const uint32_t * ids, uint16_t num);
  100. /**
  101. * Set the custom resource
  102. *
  103. * @param loader pointer to structure lvgl_img_loader
  104. * @param user_data custom data
  105. *
  106. * @retval 0 on success else negative code.
  107. */
  108. int lvgl_img_loader_set_custom(lvgl_img_loader_t *loader,
  109. const lvgl_img_loader_fn_t * fn, void * user_data);
  110. /**
  111. * Query the loader is initialized or not
  112. *
  113. * @param loader pointer to structure lvgl_img_loader
  114. *
  115. * @retval image count
  116. */
  117. static inline bool lvgl_img_loader_is_inited(lvgl_img_loader_t * loader)
  118. {
  119. return loader->fn != NULL;
  120. }
  121. /**
  122. * Get image count
  123. *
  124. * @param loader pointer to structure lvgl_img_loader
  125. *
  126. * @retval image count
  127. */
  128. static inline uint16_t lvgl_img_loader_get_count(lvgl_img_loader_t * loader)
  129. {
  130. return loader->fn->get_count(loader);
  131. }
  132. /**
  133. * load an image count
  134. *
  135. * @param loader pointer to structure lvgl_img_loader
  136. * @param index image index to load
  137. * @param dsc pointer to structure lv_img_dsc_t to store the image
  138. * @param offset pointer to structure lv_point_t to store the image position translation
  139. *
  140. * @retval 0 on success else negative code
  141. */
  142. static inline int lvgl_img_loader_load(lvgl_img_loader_t * loader, uint16_t index,
  143. lv_img_dsc_t * dsc, lv_point_t * offset)
  144. {
  145. if (loader->last_preload_idx != -1 && index != loader->last_preload_idx) {
  146. /* unload skipped preloaded img */
  147. loader->fn->load(loader, loader->last_preload_idx, dsc, offset);
  148. if (dsc->data != NULL)
  149. loader->fn->unload(loader, dsc);
  150. loader->last_preload_idx = -1;
  151. }
  152. return loader->fn->load(loader, index, dsc, offset);
  153. }
  154. /**
  155. * Unload an image count
  156. *
  157. * @param loader pointer to structure lvgl_img_loader
  158. * @param index image index to unload
  159. * @param dsc pointer to an image dsc returned by load()
  160. *
  161. * @retval N/A
  162. */
  163. static inline void lvgl_img_loader_unload(lvgl_img_loader_t * loader, lv_img_dsc_t * dsc)
  164. {
  165. if (loader->fn->unload)
  166. loader->fn->unload(loader, dsc);
  167. }
  168. /**
  169. * Preload an image count
  170. *
  171. * @param loader pointer to structure lvgl_img_loader
  172. * @param index image index to preload
  173. *
  174. * @retval N/A
  175. */
  176. static inline void lvgl_img_loader_preload(lvgl_img_loader_t * loader, uint16_t index)
  177. {
  178. if (loader->fn->preload) {
  179. loader->fn->preload(loader, index);
  180. loader->last_preload_idx = index;
  181. }
  182. }
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. /**
  187. * @}
  188. */
  189. #endif /* FRAMEWORK_DISPLAY_INCLUDE_LVGL_ANIMIMG_LOADER_H_ */