graphic.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. void tablet_event_handler(struct virtio_input_event event)
  24. {
  25. static rt_bool_t cur_btn_down = RT_FALSE;
  26. if (event.type == EV_ABS)
  27. {
  28. if (event.code == 0)
  29. {
  30. cur_points[0] = (cur_max[0] * (event.value - cur_min[0]) + cur_range[0] / 2) / cur_range[0];
  31. }
  32. else if (event.code == 1)
  33. {
  34. cur_points[1] = (cur_max[1] * (event.value - cur_min[1]) + cur_range[1] / 2) / cur_range[1];
  35. }
  36. }
  37. else if (event.type == EV_KEY)
  38. {
  39. if (event.code == BTN_LEFT)
  40. {
  41. if (cur_btn_down && event.value == 0)
  42. {
  43. color[0] ^= color[1];
  44. color[1] ^= color[0];
  45. color[0] ^= color[1];
  46. cur_btn_down = RT_FALSE;
  47. cur_event_sync = RT_TRUE;
  48. }
  49. else
  50. {
  51. cur_btn_down = RT_TRUE;
  52. }
  53. }
  54. }
  55. else if (event.type == EV_SYN)
  56. {
  57. cur_event_sync = RT_TRUE;
  58. }
  59. }
  60. void graphic_thread(void *param)
  61. {
  62. int i;
  63. char dev_name[RT_NAME_MAX];
  64. rt_device_t device = RT_NULL;
  65. rt_device_t tablet_dev = RT_NULL;
  66. struct virtio_input_config tablet_config;
  67. rt_uint32_t white = 0xffffff;
  68. rt_device_t gpu_dev = RT_NULL;
  69. struct rt_device_rect_info rect_info;
  70. struct rt_device_graphic_info graphic_info;
  71. struct rt_device_graphic_ops *virtio_gpu_graphic_ops;
  72. /* GPU */
  73. device = rt_device_find("virtio-gpu0");
  74. if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
  75. {
  76. virtio_gpu_graphic_ops = rt_graphix_ops(device);
  77. rt_memset(&rect_info, 0, sizeof(rect_info));
  78. rt_memset(&graphic_info, 0, sizeof(graphic_info));
  79. rt_device_control(device, VIRTIO_DEVICE_CTRL_GPU_SET_PRIMARY, RT_NULL);
  80. rt_device_control(device, VIRTIO_DEVICE_CTRL_GPU_CREATE_2D, (void *)RTGRAPHIC_PIXEL_FORMAT_RGB888);
  81. rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &graphic_info);
  82. rect_info.x = 0;
  83. rect_info.y = 0;
  84. rect_info.width = graphic_info.width;
  85. rect_info.height = graphic_info.height;
  86. if (graphic_info.framebuffer != RT_NULL)
  87. {
  88. rt_memset(graphic_info.framebuffer, 0xff,
  89. graphic_info.width * graphic_info.height * graphic_info.bits_per_pixel);
  90. cur_last_points[0] = graphic_info.width / 2;
  91. cur_last_points[1] = graphic_info.height / 2;
  92. virtio_gpu_graphic_ops->draw_hline((char *)&color[0], 0, graphic_info.width, cur_last_points[1]);
  93. virtio_gpu_graphic_ops->draw_vline((char *)&color[1], cur_last_points[0], 0, graphic_info.height);
  94. rt_device_control(device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  95. gpu_dev = device;
  96. }
  97. }
  98. /* Keyboard, Mouse, Tablet */
  99. for (i = 0; i < 3; ++i)
  100. {
  101. rt_snprintf(dev_name, RT_NAME_MAX, "virtio-input%d", i);
  102. device = rt_device_find(dev_name);
  103. if (device != RT_NULL && rt_device_open(device, 0) == RT_EOK)
  104. {
  105. enum virtio_input_type type;
  106. rt_device_control(device, VIRTIO_DEVICE_CTRL_INPUT_GET_TYPE, &type);
  107. if (type == VIRTIO_INPUT_TYPE_TABLET)
  108. {
  109. tablet_dev = device;
  110. }
  111. else
  112. {
  113. rt_device_close(device);
  114. }
  115. }
  116. }
  117. if (tablet_dev == RT_NULL || gpu_dev == RT_NULL)
  118. {
  119. goto _graphic_fail;
  120. }
  121. cur_max[0] = graphic_info.width;
  122. cur_max[1] = graphic_info.height;
  123. rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_X_INFO, &tablet_config);
  124. cur_min[0] = tablet_config.abs.min;
  125. cur_range[0] = tablet_config.abs.max - cur_min[0];
  126. rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_GET_ABS_Y_INFO, &tablet_config);
  127. cur_min[1] = tablet_config.abs.min;
  128. cur_range[1] = tablet_config.abs.max - cur_min[1];
  129. cur_event_sync = RT_FALSE;
  130. rt_device_control(tablet_dev, VIRTIO_DEVICE_CTRL_INPUT_BIND_BSCT_HANDLER, tablet_event_handler);
  131. for (;;)
  132. {
  133. while (cur_event_sync)
  134. {
  135. virtio_gpu_graphic_ops->draw_hline((char *)&white, 0, graphic_info.width, cur_last_points[1]);
  136. virtio_gpu_graphic_ops->draw_vline((char *)&white, cur_last_points[0], 0, graphic_info.height);
  137. cur_last_points[0] = cur_points[0];
  138. cur_last_points[1] = cur_points[1];
  139. virtio_gpu_graphic_ops->draw_hline((char *)&color[0], 0, graphic_info.width, cur_last_points[1]);
  140. virtio_gpu_graphic_ops->draw_vline((char *)&color[1], cur_last_points[0], 0, graphic_info.height);
  141. rt_device_control(gpu_dev, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  142. cur_event_sync = RT_FALSE;
  143. rt_thread_mdelay(1);
  144. }
  145. }
  146. _graphic_fail:
  147. if (gpu_dev != RT_NULL)
  148. {
  149. rt_device_close(gpu_dev);
  150. }
  151. if (tablet_dev != RT_NULL)
  152. {
  153. rt_device_close(tablet_dev);
  154. }
  155. }
  156. int graphic_init(void)
  157. {
  158. rt_thread_t graphic_tid = rt_thread_create("graphic work", graphic_thread, RT_NULL,
  159. GRAPHIC_THREAD_STACK_SIZE, GRAPHIC_THREAD_PRIORITY, GRAPHIC_THREAD_TIMESLICE);
  160. if (graphic_tid != RT_NULL)
  161. {
  162. rt_thread_startup(graphic_tid);
  163. return RT_EOK;
  164. }
  165. return -RT_ERROR;
  166. }
  167. #ifdef RT_USING_SMP
  168. INIT_ENV_EXPORT(graphic_init);
  169. #else
  170. MSH_CMD_EXPORT(graphic_init, Graphic initialize);
  171. #endif