graphic.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-11 GuEe-GUI the first version
  9. */
  10. #include <rtthread.h>
  11. #include <virtio_gpu.h>
  12. #include <virtio_input.h>
  13. #define GRAPHIC_THREAD_PRIORITY 25
  14. #define GRAPHIC_THREAD_STACK_SIZE 4096
  15. #define GRAPHIC_THREAD_TIMESLICE 5
  16. static rt_uint32_t cur_min[2];
  17. static rt_uint32_t cur_max[2];
  18. static rt_uint32_t cur_range[2];
  19. static rt_uint32_t cur_points[2];
  20. static rt_uint32_t cur_last_points[2];
  21. static rt_bool_t cur_event_sync;
  22. static rt_uint32_t color[2] = { 0xff0000, 0x0000ff };
  23. static rt_uint8_t cursor[VIRTIO_GPU_CURSOR_IMG_SIZE] rt_align(VIRTIO_PAGE_SIZE);
  24. void tablet_event_handler(struct virtio_input_event event)
  25. {
  26. static rt_bool_t cur_btn_down = RT_FALSE;
  27. if (event.type == EV_ABS)
  28. {
  29. if (event.code == 0)
  30. {
  31. cur_points[0] = (cur_max[0] * (event.value - cur_min[0]) + cur_range[0] / 2) / cur_range[0];
  32. }
  33. else if (event.code == 1)
  34. {
  35. cur_points[1] = (cur_max[1] * (event.value - cur_min[1]) + cur_range[1] / 2) / cur_range[1];
  36. }
  37. }
  38. else if (event.type == EV_KEY)
  39. {
  40. if (event.code == BTN_LEFT)
  41. {
  42. if (cur_btn_down && event.value == 0)
  43. {
  44. color[0] ^= color[1];
  45. color[1] ^= color[0];
  46. color[0] ^= color[1];
  47. cur_btn_down = RT_FALSE;
  48. cur_event_sync = RT_TRUE;
  49. }
  50. else
  51. {
  52. cur_btn_down = RT_TRUE;
  53. }
  54. }
  55. }
  56. else if (event.type == EV_SYN)
  57. {
  58. cur_event_sync = RT_TRUE;
  59. }
  60. }
  61. void graphic_thread(void *param)
  62. {
  63. int i;
  64. char dev_name[RT_NAME_MAX];
  65. rt_device_t device = RT_NULL;
  66. rt_device_t tablet_dev = RT_NULL;
  67. struct virtio_input_config tablet_config;
  68. rt_uint32_t white = 0xffffff;
  69. rt_device_t gpu_dev = RT_NULL;
  70. struct rt_device_rect_info rect_info;
  71. struct rt_device_graphic_info graphic_info;
  72. struct rt_device_graphic_ops *virtio_gpu_graphic_ops;
  73. /* GPU */
  74. device = rt_device_find("virtio-gpu0");
  75. if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
  76. {
  77. virtio_gpu_graphic_ops = rt_graphix_ops(device);
  78. rt_memset(&rect_info, 0, sizeof(rect_info));
  79. rt_memset(&graphic_info, 0, sizeof(graphic_info));
  80. rt_device_control(device, VIRTIO_DEVICE_CTRL_GPU_SET_PRIMARY, RT_NULL);
  81. rt_device_control(device, VIRTIO_DEVICE_CTRL_GPU_CREATE_2D, (void *)RTGRAPHIC_PIXEL_FORMAT_RGB888);
  82. rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &graphic_info);
  83. rect_info.x = 0;
  84. rect_info.y = 0;
  85. rect_info.width = graphic_info.width;
  86. rect_info.height = graphic_info.height;
  87. if (graphic_info.framebuffer != RT_NULL)
  88. {
  89. int i = 0;
  90. rt_memset(graphic_info.framebuffer, 0xff,
  91. graphic_info.width * graphic_info.height * graphic_info.bits_per_pixel);
  92. cur_last_points[0] = graphic_info.width / 2;
  93. cur_last_points[1] = graphic_info.height / 2;
  94. virtio_gpu_graphic_ops->draw_hline((char *)&color[0], 0, graphic_info.width, cur_last_points[1]);
  95. virtio_gpu_graphic_ops->draw_vline((char *)&color[1], cur_last_points[0], 0, graphic_info.height);
  96. rt_device_control(device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  97. while (i < sizeof(cursor) / sizeof(rt_uint32_t))
  98. {
  99. /* R: 0x4c G: 0xaf B: 0x50 A: 0.8 */
  100. ((rt_uint32_t *)cursor)[i] = 0xcc4caf50;
  101. ++i;
  102. }
  103. rt_device_control(device, VIRTIO_DEVICE_CTRL_CURSOR_SETUP, cursor);
  104. rt_device_control(device, VIRTIO_DEVICE_CTRL_CURSOR_MOVE, (rt_uint32_t[]){0, 0});
  105. gpu_dev = device;
  106. }
  107. }
  108. /* Keyboard, Mouse, Tablet */
  109. for (i = 0; i < 3; ++i)
  110. {
  111. rt_snprintf(dev_name, RT_NAME_MAX, "virtio-input%d", i);
  112. device = rt_device_find(dev_name);
  113. if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
  114. {
  115. enum virtio_input_type type;
  116. rt_device_control(device, VIRTIO_DEVICE_CTRL_INPUT_GET_TYPE, &type);
  117. if (type == VIRTIO_INPUT_TYPE_TABLET)
  118. {
  119. tablet_dev = device;
  120. }
  121. else
  122. {
  123. rt_device_close(device);
  124. }
  125. }
  126. }
  127. if (tablet_dev == RT_NULL || gpu_dev == RT_NULL)
  128. {
  129. goto _graphic_fail;
  130. }
  131. cur_max[0] = graphic_info.width;
  132. cur_max[1] = graphic_info.height;
  133. rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_X_INFO, &tablet_config);
  134. cur_min[0] = tablet_config.abs.min;
  135. cur_range[0] = tablet_config.abs.max - cur_min[0];
  136. rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_Y_INFO, &tablet_config);
  137. cur_min[1] = tablet_config.abs.min;
  138. cur_range[1] = tablet_config.abs.max - cur_min[1];
  139. cur_event_sync = RT_FALSE;
  140. rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_BIND_BSCT_HANDLER, tablet_event_handler);
  141. for (;;)
  142. {
  143. while (cur_event_sync)
  144. {
  145. virtio_gpu_graphic_ops->draw_hline((char *)&white, 0, graphic_info.width, cur_last_points[1]);
  146. virtio_gpu_graphic_ops->draw_vline((char *)&white, cur_last_points[0], 0, graphic_info.height);
  147. cur_last_points[0] = cur_points[0];
  148. cur_last_points[1] = cur_points[1];
  149. virtio_gpu_graphic_ops->draw_hline((char *)&color[0], 0, graphic_info.width, cur_last_points[1]);
  150. virtio_gpu_graphic_ops->draw_vline((char *)&color[1], cur_last_points[0], 0, graphic_info.height);
  151. rt_device_control(gpu_dev, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  152. cur_event_sync = RT_FALSE;
  153. rt_thread_mdelay(1);
  154. }
  155. }
  156. _graphic_fail:
  157. if (gpu_dev != RT_NULL)
  158. {
  159. rt_device_close(gpu_dev);
  160. }
  161. if (tablet_dev != RT_NULL)
  162. {
  163. rt_device_close(tablet_dev);
  164. }
  165. }
  166. int graphic_init(void)
  167. {
  168. rt_thread_t graphic_tid = rt_thread_create("graphic work", graphic_thread, RT_NULL,
  169. GRAPHIC_THREAD_STACK_SIZE, GRAPHIC_THREAD_PRIORITY, GRAPHIC_THREAD_TIMESLICE);
  170. if (graphic_tid != RT_NULL)
  171. {
  172. rt_thread_startup(graphic_tid);
  173. return RT_EOK;
  174. }
  175. return -RT_ERROR;
  176. }
  177. #ifdef RT_USING_SMP
  178. INIT_ENV_EXPORT(graphic_init);
  179. #else
  180. MSH_CMD_EXPORT(graphic_init, Graphic initialize);
  181. #endif