screen_manager.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "screen_manager.h"
  2. #include <common/string.h>
  3. #include <driver/multiboot2/multiboot2.h>
  4. #include <common/kprint.h>
  5. #include <common/spinlock.h>
  6. #include <mm/mm.h>
  7. #include <mm/slab.h>
  8. #include <driver/uart/uart.h>
  9. #include <driver/video/video.h>
  10. extern struct scm_buffer_info_t video_frame_buffer_info;
  11. static struct List scm_framework_list;
  12. static spinlock_t scm_register_lock; // 框架注册锁
  13. static spinlock_t scm_screen_own_lock = {1}; // 改变屏幕归属者时,需要对该锁加锁
  14. static struct scm_ui_framework_t *__current_framework; // 当前拥有屏幕控制权的框架
  15. static uint32_t scm_ui_max_id = 0;
  16. static bool __scm_alloc_enabled = false; // 允许动态申请内存的标志位
  17. static bool __scm_double_buffer_enabled = false; // 允许双缓冲的标志位
  18. /**
  19. * @brief 创建新的帧缓冲区
  20. *
  21. * @param type 帧缓冲区类型
  22. * @return struct scm_buffer_info_t* 新的帧缓冲区结构体
  23. */
  24. static struct scm_buffer_info_t *__create_buffer(uint64_t type)
  25. {
  26. // 若未启用双缓冲,则直接返回帧缓冲区
  27. if (unlikely(__scm_double_buffer_enabled == false))
  28. return &video_frame_buffer_info;
  29. struct scm_buffer_info_t *buf = (struct scm_buffer_info_t *)kmalloc(sizeof(struct scm_buffer_info_t), 0);
  30. if (buf == NULL)
  31. return (void*)-ENOMEM;
  32. memset(buf, 0, sizeof(struct scm_buffer_info_t));
  33. buf->bit_depth = video_frame_buffer_info.bit_depth;
  34. buf->flags = SCM_BF_DB;
  35. if (type & SCM_BF_PIXEL)
  36. buf->flags |= SCM_BF_PIXEL;
  37. else
  38. buf->flags |= SCM_BF_TEXT;
  39. buf->height = video_frame_buffer_info.height;
  40. buf->width = video_frame_buffer_info.width;
  41. buf->size = video_frame_buffer_info.size;
  42. struct Page *p = alloc_pages(ZONE_NORMAL, PAGE_2M_ALIGN(video_frame_buffer_info.size) / PAGE_2M_SIZE, 0);
  43. if (p == NULL)
  44. goto failed;
  45. buf->vaddr = (uint64_t)phys_2_virt(p->addr_phys);
  46. return buf;
  47. failed:;
  48. kfree(buf);
  49. return (void*)-ENOMEM;
  50. }
  51. /**
  52. * @brief 销毁双缓冲区
  53. *
  54. * @param buf
  55. * @return int
  56. */
  57. static int __destroy_buffer(struct scm_buffer_info_t *buf)
  58. {
  59. // 不能销毁帧缓冲区对象
  60. if (unlikely(buf == &video_frame_buffer_info || buf == NULL))
  61. return -EINVAL;
  62. if (unlikely(buf->vaddr == NULL))
  63. return -EINVAL;
  64. if (unlikely(verify_area(buf->vaddr, buf->size) == true))
  65. return -EINVAL;
  66. // 是否双缓冲区
  67. if (buf->flags & SCM_BF_FB)
  68. return -EINVAL;
  69. // 释放内存页
  70. free_pages(Phy_to_2M_Page(virt_2_phys(buf->vaddr)), PAGE_2M_ALIGN(video_frame_buffer_info.size) / PAGE_2M_SIZE);
  71. return 0;
  72. }
  73. /**
  74. * @brief 初始化屏幕管理模块
  75. *
  76. */
  77. void scm_init()
  78. {
  79. list_init(&scm_framework_list);
  80. spin_init(&scm_register_lock);
  81. spin_init(&scm_screen_own_lock);
  82. io_mfence();
  83. scm_ui_max_id = 0;
  84. __scm_alloc_enabled = false; // 禁用动态申请内存
  85. __scm_double_buffer_enabled = false; // 禁用双缓冲
  86. __current_framework = NULL;
  87. }
  88. /**
  89. * @brief 检查ui框架结构体中的参数设置是否合法
  90. *
  91. * @param name 框架名称
  92. * @param type 框架类型
  93. * @param ops 框架的操作
  94. * @return int
  95. */
  96. static int __check_ui_param(const char *name, const uint8_t type, const struct scm_ui_framework_operations_t *ops)
  97. {
  98. if (name == NULL)
  99. return -EINVAL;
  100. if (!(type == SCM_FRAMWORK_TYPE_GUI || type == SCM_FRAMWORK_TYPE_TEXT))
  101. return -EINVAL;
  102. if (ops == NULL)
  103. return -EINVAL;
  104. if (ops->install == NULL || ops->uninstall == NULL || ops->enable == NULL || ops->disable == NULL || ops->change == NULL)
  105. return -EINVAL;
  106. return 0;
  107. }
  108. /**
  109. * @brief 向屏幕管理器注册UI框架(动态获取框架对象结构体)
  110. *
  111. * @param name 框架名
  112. * @param type 类型
  113. * @param ops 框架操作方法
  114. * @return int
  115. */
  116. int scm_register_alloc(const char *name, const uint8_t type, struct scm_ui_framework_operations_t *ops)
  117. {
  118. // 若未启用动态申请,则返回。
  119. if (unlikely(__scm_alloc_enabled == false))
  120. return -EAGAIN;
  121. // 检查参数合法性
  122. if (__check_ui_param(name, type, ops) != 0)
  123. return -EINVAL;
  124. struct scm_ui_framework_t *ui = (struct scm_ui_framework_t *)kmalloc(sizeof(struct scm_ui_framework_t *), 0);
  125. memset(ui, 0, sizeof(struct scm_ui_framework_t));
  126. strncpy(ui->name, name, 15);
  127. ui->type = type;
  128. ui->ui_ops = ops;
  129. list_init(&ui->list);
  130. spin_lock(&scm_register_lock);
  131. ui->id = scm_ui_max_id++;
  132. spin_unlock(&scm_register_lock);
  133. // 创建帧缓冲区
  134. ui->buf = __create_buffer(ui->type);
  135. if ((uint64_t)(ui->buf) == (uint64_t)-ENOMEM)
  136. {
  137. kfree(ui);
  138. return -ENOMEM;
  139. }
  140. // 把ui框架加入链表
  141. list_add(&scm_framework_list, &ui->list);
  142. // 调用ui框架的回调函数以安装ui框架,并将其激活
  143. ui->ui_ops->install(ui->buf);
  144. ui->ui_ops->enable(NULL);
  145. if (__current_framework == NULL)
  146. return scm_framework_enable(ui);
  147. return 0;
  148. }
  149. /**
  150. * @brief 向屏幕管理器注册UI框架(静态设置的框架对象)
  151. *
  152. * @param ui 框架结构体指针
  153. * @return int 错误码
  154. */
  155. int scm_register(struct scm_ui_framework_t *ui)
  156. {
  157. if (ui == NULL)
  158. return -EINVAL;
  159. if (__check_ui_param(ui->name, ui->type, ui->ui_ops) != 0)
  160. return -EINVAL;
  161. list_init(&ui->list);
  162. spin_lock(&scm_register_lock);
  163. ui->id = scm_ui_max_id++;
  164. spin_unlock(&scm_register_lock);
  165. ui->buf = __create_buffer(ui->type);
  166. if ((uint64_t)(ui->buf) == (uint64_t)-ENOMEM)
  167. return -ENOMEM;
  168. // 把ui框架加入链表
  169. list_add(&scm_framework_list, &ui->list);
  170. // 调用ui框架的回调函数以安装ui框架,并将其激活
  171. ui->ui_ops->install(ui->buf);
  172. ui->ui_ops->enable(NULL);
  173. if (__current_framework == NULL)
  174. return scm_framework_enable(ui);
  175. return 0;
  176. }
  177. /**
  178. * @brief 向屏幕管理器卸载UI框架
  179. *
  180. * @param ui ui框架结构体
  181. * @return int
  182. */
  183. int scm_unregister(struct scm_ui_framework_t *ui)
  184. {
  185. }
  186. /**
  187. * @brief 向屏幕管理器卸载动态创建的UI框架
  188. *
  189. * @param ui ui框架结构体
  190. * @return int
  191. */
  192. int scm_unregister_alloc(struct scm_ui_framework_t *ui)
  193. {
  194. }
  195. /**
  196. * @brief 允许动态申请内存
  197. *
  198. * @return int
  199. */
  200. int scm_enable_alloc()
  201. {
  202. __scm_alloc_enabled = true;
  203. return 0;
  204. }
  205. /**
  206. * @brief 允许双缓冲区
  207. *
  208. * @return int
  209. */
  210. int scm_enable_double_buffer()
  211. {
  212. if (__scm_double_buffer_enabled == true)
  213. return 0;
  214. __scm_double_buffer_enabled = true;
  215. if (list_empty(&scm_framework_list))
  216. return 0;
  217. // 逐个检查已经注册了的ui框架,将其缓冲区更改为双缓冲
  218. struct scm_ui_framework_t *ptr = container_of(list_next(&scm_framework_list), struct scm_ui_framework_t, list);
  219. do
  220. {
  221. if (ptr->buf == &video_frame_buffer_info)
  222. {
  223. uart_send_str(COM1, "##init double buffer##");
  224. struct scm_buffer_info_t *buf = __create_buffer(SCM_BF_DB | SCM_BF_PIXEL);
  225. if ((uint64_t)(buf) == (uint64_t)-ENOMEM)
  226. return -ENOMEM;
  227. uart_send_str(COM1, "##to change double buffer##");
  228. if (ptr->ui_ops->change(buf) != 0)
  229. {
  230. __destroy_buffer(buf);
  231. kfree(buf);
  232. }
  233. }
  234. } while (list_next(&ptr->list) != &scm_framework_list);
  235. // 设置定时刷新的对象
  236. video_set_refresh_target(__current_framework->buf);
  237. // 通知显示驱动,启动双缓冲
  238. video_reinitialize(true);
  239. uart_send_str(COM1, "##initialized double buffer##");
  240. return 0;
  241. }
  242. /**
  243. * @brief 启用某个ui框架,将它的帧缓冲区渲染到屏幕上
  244. *
  245. * @param ui 要启动的ui框架
  246. * @return int 返回码
  247. */
  248. int scm_framework_enable(struct scm_ui_framework_t *ui)
  249. {
  250. if (ui->buf->vaddr == NULL)
  251. return -EINVAL;
  252. spin_lock(&scm_screen_own_lock);
  253. int retval = 0;
  254. if (__scm_double_buffer_enabled == true)
  255. {
  256. retval = video_set_refresh_target(ui->buf);
  257. if (retval == 0)
  258. __current_framework = ui;
  259. }
  260. else
  261. __current_framework = ui;
  262. spin_unlock(&scm_screen_own_lock);
  263. return retval;
  264. }
  265. /**
  266. * @brief 当内存管理单元被初始化之后,重新处理帧缓冲区问题
  267. *
  268. */
  269. void scm_reinit()
  270. {
  271. scm_enable_alloc();
  272. video_reinitialize(false);
  273. // 遍历当前所有使用帧缓冲区的框架,更新地址
  274. // 逐个检查已经注册了的ui框架,将其缓冲区更改为双缓冲
  275. struct scm_ui_framework_t *ptr = container_of(list_next(&scm_framework_list), struct scm_ui_framework_t, list);
  276. do
  277. {
  278. if (ptr->buf == &video_frame_buffer_info)
  279. {
  280. ptr->ui_ops->change(&video_frame_buffer_info);
  281. }
  282. } while (list_next(&ptr->list) != &scm_framework_list);
  283. return;
  284. }