lv_port_disp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-17 Wayne The first version
  9. */
  10. #include <lvgl.h>
  11. #include "nu_2d.h"
  12. #include "mmu.h"
  13. #if (LV_USE_GPU_N9H30_GE2D && LV_VERSION_CHECK(8, 2, 0))
  14. #include "lv_gpu_n9h30_ge2d.h"
  15. #endif
  16. #define LOG_TAG "lvgl.disp"
  17. #define DBG_ENABLE
  18. #define DBG_SECTION_NAME LOG_TAG
  19. #define DBG_LEVEL DBG_ERROR
  20. #define DBG_COLOR
  21. #include <rtdbg.h>
  22. /*A static or global variable to store the buffers*/
  23. static lv_disp_draw_buf_t disp_buf;
  24. static rt_device_t lcd_device = 0;
  25. static struct rt_device_graphic_info info;
  26. static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  27. static void *buf3_next = RT_NULL;
  28. static uint32_t u32FirstFlush = 0;
  29. static void nu_antitearing(lv_disp_draw_buf_t *draw_buf, lv_color_t *color_p)
  30. {
  31. if (buf3_next)
  32. {
  33. /* vsync-none: Use triple screen-sized buffers. */
  34. if (draw_buf->buf1 == color_p)
  35. draw_buf->buf1 = buf3_next;
  36. else
  37. draw_buf->buf2 = buf3_next;
  38. draw_buf->buf_act = buf3_next;
  39. buf3_next = color_p;
  40. }
  41. else
  42. {
  43. /* vsync-after: Use ping-pong screen-sized buffers only.*/
  44. rt_device_control(lcd_device, RTGRAPHIC_CTRL_WAIT_VSYNC, RT_NULL);
  45. }
  46. }
  47. static void nu_flush_full_refresh(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  48. {
  49. if (IS_CACHEABLE_VRAM(color_p))
  50. mmu_clean_dcache((uint32_t)color_p, disp_drv->draw_buf->size * sizeof(lv_color_t));
  51. /* Use PANDISPLAY without H/W copying */
  52. rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, color_p);
  53. nu_antitearing(disp_drv->draw_buf, color_p);
  54. if (!u32FirstFlush)
  55. {
  56. /* Enable backlight at first flushing. */
  57. rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWERON, RT_NULL);
  58. u32FirstFlush = 1;
  59. }
  60. lv_disp_flush_ready(disp_drv);
  61. }
  62. static void nu_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  63. {
  64. #if (LV_USE_GPU_N9H30_GE2D==1)
  65. lv_draw_sw_blend_dsc_t blend_flush = {0};
  66. lv_draw_ctx_t draw_flush = {0};
  67. lv_area_t flush_area = {0, 0, info.width - 1, info.height - 1 };
  68. blend_flush.blend_area = area;
  69. blend_flush.src_buf = color_p;
  70. blend_flush.mask_buf = NULL;
  71. blend_flush.opa = LV_OPA_MAX;
  72. blend_flush.blend_mode = LV_BLEND_MODE_NORMAL;
  73. draw_flush.buf = info.framebuffer;
  74. draw_flush.clip_area = &flush_area;
  75. draw_flush.buf_area = &flush_area;
  76. lv_draw_n9h30_ge2d_blend(&draw_flush, (const lv_draw_sw_blend_dsc_t *)&blend_flush);
  77. #else
  78. int32_t flush_area_w = lv_area_get_width(area);
  79. int32_t flush_area_h = lv_area_get_height(area);
  80. //if ( flush_area_w&0x3 != 0 )
  81. // rt_kprintf("[%s %08x] %dx%d %d %d\n", __func__, color_p, flush_area_w, flush_area_h, area->x1, area->y1 );
  82. /* Update dirty region. */
  83. // Enter GE2D ->
  84. ge2dInit(sizeof(lv_color_t) * 8, info.width, info.height, (void *)info.framebuffer);
  85. ge2dBitblt_SetAlphaMode(-1, 0, 0);
  86. ge2dBitblt_SetDrawMode(-1, 0, 0);
  87. ge2dSpriteBlt_Screen(area->x1, area->y1, flush_area_w, flush_area_h, (void *)color_p);
  88. // -> Leave GE2D
  89. #endif
  90. if (!u32FirstFlush)
  91. {
  92. /* Enable backlight at first flushing. */
  93. rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWERON, RT_NULL);
  94. u32FirstFlush = 1;
  95. }
  96. lv_disp_flush_ready(disp_drv);
  97. }
  98. #if LV_VERSION_EQUAL(8, 1, 0)
  99. static void nu_fill_cb(struct _lv_disp_drv_t *disp_drv, lv_color_t *dest_buf, lv_coord_t dest_width,
  100. const lv_area_t *fill_area, lv_color_t color)
  101. {
  102. int32_t fill_area_w = lv_area_get_width(fill_area);
  103. int32_t fill_area_h = lv_area_get_height(fill_area);
  104. if (lv_area_get_size(fill_area) < 3600)
  105. {
  106. /*Software filling*/
  107. int y;
  108. lv_color_t *disp_buf_first = dest_buf + dest_width * fill_area->y1 + fill_area->x1;
  109. for (y = 0; y < fill_area_h; y++)
  110. {
  111. lv_color_fill(disp_buf_first, color, fill_area_w);
  112. disp_buf_first += dest_width;
  113. }
  114. }
  115. else
  116. {
  117. //rt_kprintf("[blend_fill %d %08x] %dx%d %d %d\n", lv_area_get_size(fill_area), dest_buf, fill_area_w, fill_area_h, fill_area->x1, fill_area->y1 );
  118. if (IS_CACHEABLE_VRAM(dest_buf))
  119. mmu_clean_invalidated_dcache((uint32_t)dest_buf, sizeof(lv_color_t) * (dest_width * fill_area_h + fill_area_w));
  120. /*Hardware filling*/
  121. // Enter GE2D ->
  122. ge2dInit(sizeof(lv_color_t) * 8, fill_area_w, fill_area_h, (void *)dest_buf);
  123. ge2dClip_SetClip(fill_area->x1, fill_area->y1, fill_area->x2, fill_area->y2);
  124. if (sizeof(lv_color_t) == 4)
  125. ge2dFill_Solid(fill_area->x1, fill_area->y1, fill_area_w, fill_area_h, color.full);
  126. else if (sizeof(lv_color_t) == 2)
  127. ge2dFill_Solid_RGB565(fill_area->x1, fill_area->y1, fill_area_w, fill_area_h, color.full);
  128. ge2dClip_SetClip(-1, 0, 0, 0);
  129. // -> Leave GE2D
  130. }
  131. }
  132. #endif
  133. void nu_perf_monitor(struct _lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
  134. {
  135. rt_kprintf("Elapsed: %dms, Pixel: %d, Bytes:%d, %d%\n", time, px, px * sizeof(lv_color_t), px * 100 / disp_drv->draw_buf->size);
  136. }
  137. void lv_port_disp_init(void)
  138. {
  139. rt_err_t result;
  140. void *buf1 = RT_NULL;
  141. void *buf2 = RT_NULL;
  142. uint32_t u32FBSize;
  143. lcd_device = rt_device_find("lcd");
  144. if (lcd_device == 0)
  145. {
  146. LOG_E("error!");
  147. return;
  148. }
  149. /* get framebuffer address */
  150. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  151. if (result != RT_EOK)
  152. {
  153. LOG_E("error!");
  154. /* get device information failed */
  155. return;
  156. }
  157. /* Disable backlight at startup. */
  158. rt_device_control(lcd_device, RTGRAPHIC_CTRL_POWEROFF, RT_NULL);
  159. RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  160. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  161. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  162. /*Set the resolution of the display*/
  163. disp_drv.hor_res = info.width;
  164. disp_drv.ver_res = info.height;
  165. u32FBSize = info.height * info.width * (info.bits_per_pixel / 8);
  166. #if (LV_USE_ANTI_TEARING==1)
  167. disp_drv.full_refresh = 1;
  168. #endif
  169. if (disp_drv.full_refresh)
  170. {
  171. #if (LV_USE_GPU_N9H30_GE2D==1)
  172. buf1 = (void *)info.framebuffer; // Use Non-cacheable VRAM
  173. #else
  174. buf1 = (void *)((uint32_t)info.framebuffer & ~BIT31); // Use Cacheable VRAM
  175. #endif
  176. buf2 = (void *)((uint32_t)buf1 + u32FBSize);
  177. buf3_next = (void *)((uint32_t)buf2 + u32FBSize);
  178. rt_kprintf("LVGL: Use triple screen-sized buffers(full_refresh) - buf1@%08x, buf2@%08x, buf3_next@%08x\n", buf1, buf2, buf3_next);
  179. disp_drv.flush_cb = nu_flush_full_refresh;
  180. }
  181. else
  182. {
  183. buf1 = (void *)(((uint32_t)info.framebuffer) + u32FBSize);
  184. buf2 = (void *)((uint32_t)buf1 + u32FBSize);
  185. rt_kprintf("LVGL: Use two screen-sized buffers - buf1@%08x, buf2@%08x\n", buf1, buf2);
  186. rt_device_control(lcd_device, RTGRAPHIC_CTRL_PAN_DISPLAY, info.framebuffer);
  187. disp_drv.flush_cb = nu_flush;
  188. }
  189. /*Initialize `disp_buf` with the buffer(s).*/
  190. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, info.width * info.height);
  191. result = rt_device_open(lcd_device, 0);
  192. if (result != RT_EOK)
  193. {
  194. LOG_E("error!");
  195. return;
  196. }
  197. /*Set a display buffer*/
  198. disp_drv.draw_buf = &disp_buf;
  199. #if LV_VERSION_EQUAL(8, 1, 0)
  200. /*Fill a memory with a color (GPU only)*/
  201. disp_drv.gpu_fill_cb = nu_fill_cb;
  202. #endif
  203. #if (LV_USE_GPU_N9H30_GE2D && LV_VERSION_CHECK(8, 2, 0))
  204. disp_drv.draw_ctx_init = lv_draw_n9h30_ge2d_ctx_init;
  205. disp_drv.draw_ctx_deinit = lv_draw_n9h30_ge2d_ctx_init;
  206. disp_drv.draw_ctx_size = sizeof(lv_draw_n9h30_ge2d_ctx_t);
  207. #endif
  208. /*Called after every refresh cycle to tell the rendering and flushing time + the number of flushed pixels*/
  209. //disp_drv.monitor_cb = nu_perf_monitor;
  210. /*Finally register the driver*/
  211. lv_disp_drv_register(&disp_drv);
  212. }