lvgl_memory.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <os_common_api.h>
  7. #include <memory/mem_cache.h>
  8. #include <lvgl/lvgl_memory.h>
  9. #include <display/display_hal.h>
  10. #include <ui_mem.h>
  11. #ifdef CONFIG_VG_LITE
  12. # include <vg_lite/vglite_util.h>
  13. #endif
  14. #if CONFIG_LV_VDB_NUM > 0
  15. /* NOTE:
  16. * 1) depending on chosen color depth buffer may be accessed using uint8_t *,
  17. * uint16_t * or uint32_t *, therefore buffer needs to be aligned accordingly to
  18. * prevent unaligned memory accesses.
  19. * 2) must align each buffer address and size to psram cache line size (32 bytes)
  20. * if allocated in psram.
  21. * 3) Verisilicon vg_lite buffer memory requires 64 bytes aligned
  22. */
  23. #ifdef CONFIG_VG_LITE
  24. # define BUFFER_ALIGN 64
  25. #else
  26. # define BUFFER_ALIGN 32
  27. #endif
  28. #ifndef CONFIG_UI_MEM_VDB_SHARE_SURFACE_BUFFER
  29. #if CONFIG_LV_VDB_SIZE <= 0
  30. # error CONFIG_LV_VDB_SIZE must greater than 0
  31. #endif
  32. #if LV_COLOR_SCREEN_TRANSP
  33. # define BUFFER_SIZE (((CONFIG_LV_VDB_SIZE * LV_IMG_PX_SIZE_ALPHA_BYTE) + (BUFFER_ALIGN - 1)) & ~(BUFFER_ALIGN - 1))
  34. # define NBR_PIXELS_IN_BUFFER (BUFFER_SIZE / LV_IMG_PX_SIZE_ALPHA_BYTE)
  35. #else
  36. # define BUFFER_SIZE (((CONFIG_LV_VDB_SIZE * LV_COLOR_SIZE / 8) + (BUFFER_ALIGN - 1)) & ~(BUFFER_ALIGN - 1))
  37. # define NBR_PIXELS_IN_BUFFER (BUFFER_SIZE * 8 / LV_COLOR_SIZE)
  38. #endif
  39. static uint8_t vdb_buf_0[BUFFER_SIZE] __aligned(BUFFER_ALIGN) __in_section_unique(lvgl.noinit.vdb.0);
  40. #if CONFIG_LV_VDB_NUM > 1
  41. static uint8_t vdb_buf_1[BUFFER_SIZE] __aligned(BUFFER_ALIGN) __in_section_unique(lvgl.noinit.vdb.1);
  42. #endif
  43. #if CONFIG_LV_VDB_NUM > 2
  44. static uint8_t vdb_buf_2[BUFFER_SIZE] __aligned(BUFFER_ALIGN) __in_section_unique(lvgl.noinit.vdb.2);
  45. #endif
  46. #if CONFIG_LV_VDB_NUM > 3
  47. static uint8_t vdb_buf_3[BUFFER_SIZE] __aligned(BUFFER_ALIGN) __in_section_unique(lvgl.noinit.vdb.3);
  48. #endif
  49. #endif /* CONFIG_UI_MEM_VDB_SHARE_SURFACE_BUFFER */
  50. #endif /* CONFIG_LV_VDB_NUM > 0*/
  51. int lvgl_draw_buffer_alloc_static(lv_disp_drv_t *disp_drv)
  52. {
  53. #if CONFIG_LV_VDB_NUM > 0
  54. #ifdef CONFIG_UI_MEM_VDB_SHARE_SURFACE_BUFFER
  55. uint8_t *bufs[CONFIG_LV_VDB_NUM];
  56. unsigned int total_size = ui_mem_get_share_surface_buffer_size();
  57. unsigned int max_buf_size = ((total_size / CONFIG_LV_VDB_NUM) & ~(BUFFER_ALIGN - 1));
  58. uint8_t px_size = LV_COLOR_SCREEN_TRANSP ? LV_IMG_PX_SIZE_ALPHA_BYTE : (LV_COLOR_SIZE / 8);
  59. unsigned int max_nbr_pixels = (max_buf_size / px_size);
  60. unsigned int buf_size;
  61. unsigned int nbr_pixels;
  62. int i;
  63. if (CONFIG_LV_VDB_SIZE > 0 && CONFIG_LV_VDB_SIZE < max_nbr_pixels) {
  64. nbr_pixels = CONFIG_LV_VDB_SIZE;
  65. buf_size = (nbr_pixels * px_size + BUFFER_ALIGN - 1) & ~(BUFFER_ALIGN - 1);
  66. } else {
  67. nbr_pixels = max_nbr_pixels; /* auto compute LV_VDB_SIZE if 0 */
  68. buf_size = max_buf_size;
  69. }
  70. bufs[0] = ui_mem_get_share_surface_buffer();
  71. for (i = 1; i < CONFIG_LV_VDB_NUM; i++) {
  72. bufs[i] = bufs[i - 1] + buf_size;
  73. }
  74. #else /* CONFIG_UI_MEM_VDB_SHARE_SURFACE_BUFFER */
  75. uint8_t *bufs[CONFIG_LV_VDB_NUM] = {
  76. vdb_buf_0,
  77. #if CONFIG_LV_VDB_NUM > 1
  78. vdb_buf_1,
  79. #endif
  80. #if CONFIG_LV_VDB_NUM > 2
  81. vdb_buf_2,
  82. #endif
  83. #if CONFIG_LV_VDB_NUM > 3
  84. vdb_buf_3,
  85. #endif
  86. };
  87. unsigned int nbr_pixels = NBR_PIXELS_IN_BUFFER;
  88. #endif /* CONFIG_UI_MEM_VDB_SHARE_SURFACE_BUFFER */
  89. LV_LOG_INFO("LVGL VDB: size %u, num %u\n", nbr_pixels, CONFIG_LV_VDB_NUM);
  90. lv_disp_draw_buf_init2(disp_drv->draw_buf, (void **)bufs, CONFIG_LV_VDB_NUM, nbr_pixels);
  91. return 0;
  92. #else
  93. return -ENOMEM;
  94. #endif /* CONFIG_LV_VDB_NUM > 0*/
  95. }
  96. void lvgl_img_buf_clean_cache(const lv_img_dsc_t *dsc)
  97. {
  98. uint32_t size = dsc->data_size;
  99. if (size == 0) {
  100. size = lv_img_buf_get_img_size(dsc->header.w, dsc->header.h, dsc->header.cf);
  101. }
  102. mem_dcache_clean(dsc->data, size);
  103. }
  104. void lvgl_img_buf_invalidate_cache(const lv_img_dsc_t *dsc)
  105. {
  106. uint32_t size = dsc->data_size;
  107. if (size == 0) {
  108. size = lv_img_buf_get_img_size(dsc->header.w, dsc->header.h, dsc->header.cf);
  109. }
  110. mem_dcache_invalidate(dsc->data, size);
  111. }
  112. bool lvgl_img_buf_is_clean(const void *buf, unsigned int size)
  113. {
  114. if (mem_is_cacheable(buf) == false) {
  115. return true;
  116. }
  117. #ifdef CONFIG_UI_MEMORY_MANAGER
  118. return ui_mem_is_type(MEM_FB, buf) || ui_mem_is_type(MEM_RES, buf);
  119. #else
  120. return false;
  121. #endif
  122. }
  123. uint32_t lvgl_img_cf_to_display(lv_img_cf_t cf, uint8_t * bits_per_pixel)
  124. {
  125. uint32_t format = 0;
  126. uint8_t bpp = 0;
  127. switch (cf) {
  128. case LV_IMG_CF_RGB_565:
  129. format = HAL_PIXEL_FORMAT_RGB_565;
  130. bpp = 16;
  131. break;
  132. case LV_IMG_CF_ARGB_8565:
  133. format = HAL_PIXEL_FORMAT_ARGB_8565;
  134. bpp = 24;
  135. break;
  136. case LV_IMG_CF_ARGB_8888:
  137. format = HAL_PIXEL_FORMAT_ARGB_8888;
  138. bpp = 32;
  139. break;
  140. case LV_IMG_CF_XRGB_8888:
  141. format = HAL_PIXEL_FORMAT_XRGB_8888;
  142. bpp = 32;
  143. break;
  144. case LV_IMG_CF_RGB_888:
  145. format = HAL_PIXEL_FORMAT_RGB_888;
  146. bpp = 24;
  147. break;
  148. case LV_IMG_CF_ARGB_6666:
  149. format = HAL_PIXEL_FORMAT_ARGB_6666;
  150. bpp = 24;
  151. break;
  152. case LV_IMG_CF_ARGB_1555:
  153. format = HAL_PIXEL_FORMAT_ARGB_1555;
  154. bpp = 16;
  155. break;
  156. case LV_IMG_CF_ALPHA_8BIT:
  157. format = HAL_PIXEL_FORMAT_A8;
  158. bpp = 8;
  159. break;
  160. case LV_IMG_CF_ALPHA_4BIT:
  161. format = HAL_PIXEL_FORMAT_A4;
  162. bpp = 4;
  163. break;
  164. case LV_IMG_CF_ALPHA_2BIT:
  165. format = HAL_PIXEL_FORMAT_A2;
  166. bpp = 2;
  167. break;
  168. case LV_IMG_CF_ALPHA_1BIT:
  169. format = HAL_PIXEL_FORMAT_A1;
  170. bpp = 1;
  171. break;
  172. case LV_IMG_CF_ALPHA_4BIT_LE:
  173. format = HAL_PIXEL_FORMAT_A4_LE;
  174. bpp = 4;
  175. break;
  176. case LV_IMG_CF_ALPHA_1BIT_LE:
  177. format = HAL_PIXEL_FORMAT_A1_LE;
  178. bpp = 1;
  179. break;
  180. case LV_IMG_CF_INDEXED_8BIT:
  181. case LV_IMG_CF_INDEXED_8BIT_ACTS:
  182. format = HAL_PIXEL_FORMAT_I8;
  183. bpp = 8;
  184. break;
  185. case LV_IMG_CF_INDEXED_4BIT:
  186. format = HAL_PIXEL_FORMAT_I4;
  187. bpp = 4;
  188. break;
  189. case LV_IMG_CF_INDEXED_2BIT:
  190. format = HAL_PIXEL_FORMAT_I2;
  191. bpp = 2;
  192. break;
  193. case LV_IMG_CF_INDEXED_1BIT:
  194. format = HAL_PIXEL_FORMAT_I1;
  195. bpp = 1;
  196. break;
  197. default:
  198. break;
  199. }
  200. if (bits_per_pixel) {
  201. *bits_per_pixel = bpp;
  202. }
  203. return format;
  204. }
  205. #ifdef CONFIG_VG_LITE
  206. int lvgl_img_cf_to_vglite(lv_img_cf_t cf, uint8_t * bits_per_pixel)
  207. {
  208. int format = -1;
  209. uint8_t bpp = 0;
  210. switch(cf) {
  211. #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP == 0
  212. case LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED: /* fall through */
  213. #endif
  214. case LV_IMG_CF_RGB_565:
  215. format = VG_LITE_BGR565;
  216. bpp = 16;
  217. break;
  218. case LV_IMG_CF_ARGB_8565:
  219. format = VG_LITE_BGRA5658;
  220. bpp = 24;
  221. break;
  222. case LV_IMG_CF_ARGB_8888:
  223. format = VG_LITE_BGRA8888;
  224. bpp = 32;
  225. break;
  226. #if LV_COLOR_DEPTH == 32
  227. case LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED: /* fall through */
  228. #endif
  229. case LV_IMG_CF_XRGB_8888:
  230. format = VG_LITE_BGRX8888;
  231. bpp = 32;
  232. break;
  233. case LV_IMG_CF_RGB_888:
  234. format = VG_LITE_BGR888;
  235. bpp = 24;
  236. break;
  237. case LV_IMG_CF_ARGB_1555:
  238. format = VG_LITE_BGRA5551;
  239. bpp = 16;
  240. break;
  241. case LV_IMG_CF_ALPHA_8BIT:
  242. format = VG_LITE_A8;
  243. bpp = 8;
  244. break;
  245. case LV_IMG_CF_INDEXED_8BIT:
  246. case LV_IMG_CF_INDEXED_8BIT_ACTS:
  247. format = VG_LITE_INDEX_8;
  248. bpp = 8;
  249. break;
  250. case LV_IMG_CF_ETC2_EAC:
  251. format = VG_LITE_RGBA8888_ETC2_EAC;
  252. bpp = 8;
  253. break;
  254. default:
  255. break;
  256. }
  257. if (bits_per_pixel) {
  258. *bits_per_pixel = bpp;
  259. }
  260. return format;
  261. }
  262. int lvgl_vglite_map(vg_lite_buffer_t *vgbuf, const void *color_p,
  263. uint16_t w, uint16_t h, uint16_t stride, lv_img_cf_t cf)
  264. {
  265. uint8_t bpp = 0;
  266. int format;
  267. if (vgbuf == NULL || color_p == NULL)
  268. return -EINVAL;
  269. format = lvgl_img_cf_to_vglite(cf, &bpp);
  270. if (format < 0)
  271. return -EINVAL;
  272. const uint8_t * color8 = color_p;
  273. if (cf == LV_IMG_CF_INDEXED_8BIT) {
  274. color8 += 1024;
  275. } else if (cf == LV_IMG_CF_INDEXED_8BIT_ACTS) {
  276. color8 += *((uint32_t *)color8) * 4 + 4;
  277. }
  278. stride = stride * bpp / 8;
  279. if (format == VG_LITE_RGBA8888_ETC2_EAC)
  280. stride = (stride + 0x3) & ~0x3;
  281. return vglite_buf_map(vgbuf, (void *)color8, w, h, stride, format);
  282. }
  283. int lvgl_vglite_map_img(vg_lite_buffer_t *vgbuf, const lv_img_dsc_t *img_dsc)
  284. {
  285. return lvgl_vglite_map(vgbuf, img_dsc->data, img_dsc->header.w,
  286. img_dsc->header.h, img_dsc->header.w, img_dsc->header.cf);
  287. }
  288. int lvgl_vglite_unmap(vg_lite_buffer_t *vgbuf)
  289. {
  290. return vglite_buf_unmap(vgbuf);
  291. }
  292. int lvgl_vglite_set_img_clut(const lv_img_dsc_t *img_dsc)
  293. {
  294. vg_lite_uint32_t *palette = (vg_lite_uint32_t *)img_dsc->data;
  295. switch (img_dsc->header.cf) {
  296. case LV_IMG_CF_INDEXED_8BIT_ACTS:
  297. palette += 4;
  298. break;
  299. case LV_IMG_CF_INDEXED_8BIT:
  300. break;
  301. default:
  302. return -EINVAL;
  303. }
  304. return vg_lite_set_CLUT(256, palette);
  305. }
  306. #endif /* CONFIG_VG_LITE */