drv_g2d.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-12-10 Rbb666 the first version
  9. */
  10. #include <rtthread.h>
  11. #include <lcd_port.h>
  12. #include <stdlib.h>
  13. #include "drv_g2d.h"
  14. #ifdef BSP_USING_G2D
  15. static d2_device *_d2_handle;
  16. static d2_renderbuffer *renderbuffer;
  17. static uint32_t _GetD2Mode(void)
  18. {
  19. uint32_t r;
  20. r = d2_mode_rgb565;
  21. return r;
  22. }
  23. int G2d_Drv_HWInit(void)
  24. {
  25. d2_s32 d2_err;
  26. // Initialize D/AVE 2D driver
  27. _d2_handle = (d2_device *)rt_malloc(sizeof(_d2_handle));
  28. _d2_handle = d2_opendevice(0);
  29. d2_err = d2_inithw(_d2_handle, 0);
  30. if (d2_err != D2_OK)
  31. {
  32. rt_kprintf("Could NOT d2_inithw\n");
  33. d2_closedevice(_d2_handle);
  34. return RT_ERROR;
  35. }
  36. // Clear both buffers
  37. d2_framebuffer(_d2_handle,
  38. &fb_background[0],
  39. 480,
  40. 480,
  41. 272,
  42. (d2_s32) _GetD2Mode());
  43. d2_clear(_d2_handle, 0x000000);
  44. // Set various D2 parameters
  45. d2_setalphablendmode(_d2_handle, d2_bm_one, d2_bm_one_minus_alpha);
  46. d2_setblendmode(_d2_handle, d2_bm_alpha, d2_bm_one_minus_alpha);
  47. d2_setalphamode(_d2_handle, d2_am_constant);
  48. d2_setalpha(_d2_handle, UINT8_MAX);
  49. d2_setantialiasing(_d2_handle, 1);
  50. d2_setlinecap(_d2_handle, d2_lc_butt);
  51. d2_setlinejoin(_d2_handle, d2_lj_miter);
  52. renderbuffer = d2_newrenderbuffer(_d2_handle, 20, 20);
  53. if (!renderbuffer)
  54. {
  55. rt_kprintf("NO renderbuffer\n");
  56. d2_closedevice(_d2_handle);
  57. }
  58. return RT_EOK;
  59. }
  60. void _GraphicsHWDeInit(void)
  61. {
  62. // Stop graphics LCD controller
  63. while (FSP_SUCCESS != R_GLCDC_Stop(g_display0.p_ctrl))
  64. {
  65. /* Wait for GLCDC register update to complete before closing driver. */
  66. }
  67. R_GLCDC_Close(g_display0.p_ctrl);
  68. // Deinitialize D/AVE 2D driver
  69. d2_freerenderbuffer(_d2_handle, renderbuffer);
  70. d2_deinithw(_d2_handle);
  71. d2_closedevice(_d2_handle);
  72. }
  73. static int32_t _CircleAA(int32_t x0, int32_t y0, int32_t r, int32_t w)
  74. {
  75. uint32_t Mode;
  76. int32_t ret;
  77. Mode = _GetD2Mode();
  78. // Generate render operations
  79. d2_framebuffer(_d2_handle,
  80. (uint16_t *)&fb_background[0],
  81. LCD_WIDTH,
  82. LCD_WIDTH,
  83. LCD_HEIGHT,
  84. (d2_s32) Mode);
  85. d2_selectrenderbuffer(_d2_handle, renderbuffer);
  86. d2_setcolor(_d2_handle, 0, 0xF800); // green
  87. d2_cliprect(_d2_handle, 0, 0, LCD_WIDTH, LCD_HEIGHT);
  88. ret = d2_rendercircle(_d2_handle,
  89. (d2_point)(x0 << 4),
  90. (d2_point)(y0 << 4),
  91. (d2_width)(r << 4),
  92. (d2_width)(w << 4));
  93. // Execute render operations
  94. d2_executerenderbuffer(_d2_handle, renderbuffer, 0);
  95. d2_flushframe(_d2_handle);
  96. while (d2_commandspending(_d2_handle))
  97. {
  98. /* Do nothing */
  99. }
  100. return ret;
  101. }
  102. int G2D_Test_Draw_Circle()
  103. {
  104. _CircleAA(240, 100, 50, 10);
  105. return 0;
  106. }
  107. MSH_CMD_EXPORT(G2D_Test_Draw_Circle, G2D_Draw_Circle);
  108. #ifdef PKG_USING_LVGL
  109. void lv_port_gpu_blit(int32_t x,
  110. int32_t y,
  111. const void *p,
  112. const lv_area_t *area)
  113. {
  114. uint32_t Mode;
  115. uint32_t ModeSrc;
  116. Mode = _GetD2Mode();
  117. ModeSrc = d2_mode_rgb565;
  118. lv_coord_t img_w, img_h;
  119. img_w = lv_area_get_width(area);
  120. img_h = lv_area_get_height(area);
  121. // Generate render operations
  122. d2_framebuffer(_d2_handle,
  123. (uint16_t *)&fb_background[0],
  124. LCD_WIDTH,
  125. LCD_WIDTH,
  126. LCD_HEIGHT,
  127. (d2_s32) Mode);
  128. d2_selectrenderbuffer(_d2_handle, renderbuffer);
  129. d2_cliprect(_d2_handle, 0, 0, LCD_WIDTH, LCD_HEIGHT);
  130. d2_setblitsrc(_d2_handle, (void *) p, img_w, img_w, img_h, ModeSrc);
  131. d2_blitcopy(_d2_handle, img_w, img_h, 0, 0, (d2_width)(img_w << 4), (d2_width)(img_h << 4),
  132. (d2_point)(x << 4), (d2_point)(y << 4), 0);
  133. // Execute render operations
  134. d2_executerenderbuffer(_d2_handle, renderbuffer, 0);
  135. // In single-buffered mode always wait for DRW to finish before returning
  136. d2_flushframe(_d2_handle);
  137. }
  138. #endif
  139. void _LCD_FillRect(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color)
  140. {
  141. uint32_t Mode;
  142. int32_t xSize;
  143. int32_t ySize;
  144. Mode = _GetD2Mode();
  145. // Generate render operations
  146. d2_framebuffer(_d2_handle,
  147. (uint16_t *)&fb_background[0],
  148. LCD_WIDTH,
  149. LCD_WIDTH,
  150. LCD_HEIGHT,
  151. (d2_s32) Mode);
  152. d2_selectrenderbuffer(_d2_handle, renderbuffer);
  153. d2_setcolor(_d2_handle, 0, (uint16_t)color);
  154. d2_setalpha(_d2_handle, 0xff);
  155. d2_cliprect(_d2_handle, x0, y0, x1, y1);
  156. xSize = (x1 - x0) + 1;
  157. ySize = (y1 - y0) + 1;
  158. d2_renderbox(_d2_handle, (d2_point)(x0 << 4), (d2_point)(y0 << 4), (d2_width)(xSize << 4),
  159. (d2_width)(ySize << 4));
  160. rt_kprintf("color:%#x\n", color);
  161. // Execute render operations
  162. d2_executerenderbuffer(_d2_handle, renderbuffer, 0);
  163. d2_flushframe(_d2_handle);
  164. d2_setalpha(_d2_handle, UINT8_MAX);
  165. d2_u8 fillmode_bkup = d2_getfillmode(_d2_handle);
  166. d2_setfillmode(_d2_handle, fillmode_bkup);
  167. }
  168. int G2D_Test_Draw_FillRect(int argc, const char *argv[])
  169. {
  170. if (argc > 0)
  171. {
  172. _LCD_FillRect(100, 100, 150, 150, 0x00F800);
  173. _LCD_FillRect(100, 100, 120, 120, 0x0000FF);
  174. rt_kprintf("x0:%d y0:%d x1:%d y1:%d\n", atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  175. }
  176. return 0;
  177. }
  178. MSH_CMD_EXPORT(G2D_Test_Draw_FillRect, G2D_Test_Draw_FillRect);
  179. #endif