lvgl_img_res_decoder.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @file lvgl_img_res_decoder.c
  3. *
  4. */
  5. /*
  6. * Copyright (c) 2020 Actions Technology Co., Ltd
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. */
  10. /*********************
  11. * INCLUDES
  12. *********************/
  13. #include <os_common_api.h>
  14. #include <lvgl/lvgl_img_decoder.h>
  15. #include <lvgl/src/draw/lv_img_decoder.h>
  16. #include <lvgl/src/draw/lv_img_cache.h>
  17. #include <lvgl/src/draw/lv_draw_img.h>
  18. #include <lvgl/porting/decoder/lv_img_decoder_acts.h>
  19. #include <res_manager_api.h>
  20. /*********************
  21. * DEFINES
  22. *********************/
  23. /**********************
  24. * TYPEDEFS
  25. **********************/
  26. /**********************
  27. * STATIC PROTOTYPES
  28. **********************/
  29. static lv_res_t _img_decoder_acts_res_info(lv_img_decoder_t * decoder, const void * src,
  30. lv_img_header_t * header);
  31. static lv_res_t _img_decoder_acts_res_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc);
  32. static lv_res_t _img_decoder_acts_res_read_line(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc,
  33. lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf);
  34. static void _img_decoder_acts_res_close(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc);
  35. /**********************
  36. * STATIC VARIABLES
  37. **********************/
  38. /**********************
  39. * GLOBAL FUNCTIONS
  40. **********************/
  41. void lvgl_img_decoder_acts_res_init(void)
  42. {
  43. lv_img_decoder_t *res_decoder = lv_img_decoder_create();
  44. LV_ASSERT_MALLOC(res_decoder);
  45. if(res_decoder == NULL) {
  46. LV_LOG_WARN("lvgl_img_decoder_acts_init: out of memory");
  47. return;
  48. }
  49. lv_img_decoder_set_info_cb(res_decoder, _img_decoder_acts_res_info);
  50. lv_img_decoder_set_open_cb(res_decoder, _img_decoder_acts_res_open);
  51. lv_img_decoder_set_read_line_cb(res_decoder, _img_decoder_acts_res_read_line);
  52. lv_img_decoder_set_close_cb(res_decoder, _img_decoder_acts_res_close);
  53. }
  54. void lvgl_img_decoder_acts_res_release_cache(void)
  55. {
  56. }
  57. static lv_img_cf_t _get_res_img_cf(const resource_bitmap_t *sty)
  58. {
  59. lv_img_cf_t cf;
  60. switch (sty->sty_data->format) {
  61. case RESOURCE_BITMAP_FORMAT_ARGB8888:
  62. cf = LV_IMG_CF_ARGB_8888;
  63. break;
  64. case RESOURCE_BITMAP_FORMAT_RGB565:
  65. case RESOURCE_BITMAP_FORMAT_JPEG:
  66. cf = LV_IMG_CF_RGB_565;
  67. break;
  68. case RESOURCE_BITMAP_FORMAT_ARGB8565:
  69. cf = LV_IMG_CF_ARGB_8565;
  70. break;
  71. case RESOURCE_BITMAP_FORMAT_A8:
  72. cf = LV_IMG_CF_ALPHA_8BIT;
  73. break;
  74. case RESOURCE_BITMAP_FORMAT_ARGB6666:
  75. cf = LV_IMG_CF_ARGB_6666;
  76. break;
  77. case RESOURCE_BITMAP_FORMAT_ARGB1555:
  78. cf = LV_IMG_CF_ARGB_1555;
  79. break;
  80. case RESOURCE_BITMAP_FORMAT_RGB888:
  81. case RESOURCE_BITMAP_FORMAT_RAW:
  82. default:
  83. cf = LV_IMG_CF_UNKNOWN;
  84. }
  85. return cf;
  86. }
  87. static lv_res_t _img_decoder_acts_res_info(lv_img_decoder_t * decoder, const void * src, lv_img_header_t * header)
  88. {
  89. LV_UNUSED(decoder);
  90. lv_img_src_t src_type = lv_img_src_get_type(src);
  91. // SYS_LOG_INF("in _img_decoder_acts_res_info %d\n", src_type);
  92. if (src_type == LV_IMG_SRC_VARIABLE)
  93. {
  94. const lv_img_dsc_t * img_dsc = src;
  95. if (img_dsc->header.cf != LV_IMG_CF_RAW && img_dsc->header.cf != LV_IMG_CF_RAW_ALPHA)
  96. {
  97. // SYS_LOG_INF("not raw\n");
  98. return LV_RES_INV;
  99. }
  100. if (img_dsc->data_size != sizeof(resource_bitmap_t))
  101. {
  102. SYS_LOG_INF("%d not style bitmap %d\n", img_dsc->data_size, sizeof(resource_bitmap_t));
  103. return LV_RES_INV;
  104. }
  105. const resource_bitmap_t * bitmap = (resource_bitmap_t *)img_dsc->data;
  106. header->always_zero = 0;
  107. header->w = bitmap->sty_data->width;
  108. header->h = bitmap->sty_data->height;
  109. header->cf = _get_res_img_cf(bitmap);
  110. return LV_RES_OK;
  111. }
  112. return LV_RES_INV;
  113. }
  114. static lv_res_t _img_decoder_acts_res_open(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
  115. {
  116. LV_UNUSED(decoder);
  117. int ret;
  118. lv_img_dsc_t* src = (lv_img_dsc_t*)dsc->src;
  119. if(src->data_size != sizeof(resource_bitmap_t))
  120. {
  121. return LV_RES_INV;
  122. }
  123. resource_bitmap_t* sty = (resource_bitmap_t*)src->data;
  124. ret = res_manager_load_bitmap_for_decoder(sty, 0);
  125. if(ret < 0)
  126. {
  127. if(ret == -2)
  128. {
  129. dsc->error_msg = (void*)2;
  130. }
  131. return LV_RES_INV;
  132. }
  133. dsc->img_data = sty->buffer;
  134. return LV_RES_OK;
  135. }
  136. static lv_res_t _img_decoder_acts_res_read_line(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc,
  137. lv_coord_t x, lv_coord_t y, lv_coord_t len, uint8_t * buf)
  138. {
  139. return LV_RES_OK;
  140. }
  141. static void _img_decoder_acts_res_close(lv_img_decoder_t * decoder, lv_img_decoder_dsc_t * dsc)
  142. {
  143. LV_UNUSED(decoder);
  144. lv_img_dsc_t* src = (lv_img_dsc_t*)dsc->src;
  145. if(src->data_size != sizeof(resource_bitmap_t))
  146. {
  147. return;
  148. }
  149. if(dsc->img_data)
  150. {
  151. res_manager_free_bitmap_for_decoder((void*)dsc->img_data);
  152. }
  153. dsc->img_data = NULL;
  154. dsc->error_msg = NULL;
  155. dsc->img_data = NULL;
  156. dsc->user_data = NULL;
  157. dsc->time_to_open = 0;
  158. return;
  159. }