screen_manager.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "screen_manager.h"
  2. #include <common/kprint.h>
  3. #include <common/spinlock.h>
  4. #include <common/string.h>
  5. #include <driver/multiboot2/multiboot2.h>
  6. #include <driver/uart/uart.h>
  7. #include <driver/video/video.h>
  8. #include <mm/mm.h>
  9. #include <mm/slab.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) == 0)
  101. return -EINVAL;
  102. if (ops == NULL)
  103. return -EINVAL;
  104. if (ops->install == NULL || ops->uninstall == NULL || ops->enable == NULL || ops->disable == NULL ||
  105. ops->change == NULL)
  106. return -EINVAL;
  107. return 0;
  108. }
  109. /**
  110. * @brief 向屏幕管理器注册UI框架(动态获取框架对象结构体)
  111. *
  112. * @param name 框架名
  113. * @param type 类型
  114. * @param ops 框架操作方法
  115. * @return int
  116. */
  117. int scm_register_alloc(const char *name, const uint8_t type, struct scm_ui_framework_operations_t *ops)
  118. {
  119. // 若未启用动态申请,则返回。
  120. if (unlikely(__scm_alloc_enabled == false))
  121. return -EAGAIN;
  122. // 检查参数合法性
  123. if (__check_ui_param(name, type, ops) != 0)
  124. return -EINVAL;
  125. struct scm_ui_framework_t *ui = (struct scm_ui_framework_t *)kmalloc(sizeof(struct scm_ui_framework_t *), 0);
  126. memset(ui, 0, sizeof(struct scm_ui_framework_t));
  127. strncpy(ui->name, name, 15);
  128. ui->type = type;
  129. ui->ui_ops = ops;
  130. list_init(&ui->list);
  131. spin_lock(&scm_register_lock);
  132. ui->id = scm_ui_max_id++;
  133. spin_unlock(&scm_register_lock);
  134. // 创建帧缓冲区
  135. ui->buf = __create_buffer(ui->type);
  136. if ((uint64_t)(ui->buf) == (uint64_t)-ENOMEM)
  137. {
  138. kfree(ui);
  139. return -ENOMEM;
  140. }
  141. // 把ui框架加入链表
  142. list_add(&scm_framework_list, &ui->list);
  143. // 调用ui框架的回调函数以安装ui框架,并将其激活
  144. ui->ui_ops->install(ui->buf);
  145. ui->ui_ops->enable(NULL);
  146. if (__current_framework == NULL)
  147. return scm_framework_enable(ui);
  148. return 0;
  149. }
  150. /**
  151. * @brief 向屏幕管理器注册UI框架(静态设置的框架对象)
  152. *
  153. * @param ui 框架结构体指针
  154. * @return int 错误码
  155. */
  156. int scm_register(struct scm_ui_framework_t *ui)
  157. {
  158. if (ui == NULL)
  159. return -EINVAL;
  160. if (__check_ui_param(ui->name, ui->type, ui->ui_ops) != 0)
  161. return -EINVAL;
  162. list_init(&ui->list);
  163. spin_lock(&scm_register_lock);
  164. ui->id = scm_ui_max_id++;
  165. spin_unlock(&scm_register_lock);
  166. ui->buf = __create_buffer(ui->type);
  167. if ((uint64_t)(ui->buf) == (uint64_t)-ENOMEM)
  168. return -ENOMEM;
  169. // 把ui框架加入链表
  170. list_add(&scm_framework_list, &ui->list);
  171. // 调用ui框架的回调函数以安装ui框架,并将其激活
  172. ui->ui_ops->install(ui->buf);
  173. ui->ui_ops->enable(NULL);
  174. if (__current_framework == NULL)
  175. return scm_framework_enable(ui);
  176. return 0;
  177. }
  178. /**
  179. * @brief 向屏幕管理器卸载UI框架
  180. *
  181. * @param ui ui框架结构体
  182. * @return int
  183. */
  184. int scm_unregister(struct scm_ui_framework_t *ui)
  185. {
  186. return 0;
  187. }
  188. /**
  189. * @brief 向屏幕管理器卸载动态创建的UI框架
  190. *
  191. * @param ui ui框架结构体
  192. * @return int
  193. */
  194. int scm_unregister_alloc(struct scm_ui_framework_t *ui)
  195. {
  196. return 0;
  197. }
  198. /**
  199. * @brief 允许动态申请内存
  200. *
  201. * @return int
  202. */
  203. int scm_enable_alloc()
  204. {
  205. __scm_alloc_enabled = true;
  206. return 0;
  207. }
  208. /**
  209. * @brief 允许双缓冲区
  210. *
  211. * @return int
  212. */
  213. int scm_enable_double_buffer()
  214. {
  215. if (__scm_double_buffer_enabled == true) // 已经开启了双缓冲区了, 直接退出
  216. return 0;
  217. __scm_double_buffer_enabled = true;
  218. if (list_empty(&scm_framework_list)) // scm 框架链表为空
  219. return 0;
  220. // 逐个检查已经注册了的ui框架,将其缓冲区更改为双缓冲
  221. struct scm_ui_framework_t *ptr = container_of(list_next(&scm_framework_list), struct scm_ui_framework_t, list);
  222. // 这里的ptr不需要特判空指针吗 问题1
  223. do
  224. {
  225. if (ptr->buf == &video_frame_buffer_info)
  226. {
  227. uart_send_str(COM1, "##init double buffer##\n");
  228. struct scm_buffer_info_t *buf = __create_buffer(SCM_BF_DB | SCM_BF_PIXEL);
  229. if ((uint64_t)(buf) == (uint64_t)-ENOMEM)
  230. return -ENOMEM;
  231. uart_send_str(COM1, "##to change double buffer##\n");
  232. if (ptr->ui_ops->change(buf) != 0) // 这里的change回调函数不会是空指针吗 问题2
  233. {
  234. __destroy_buffer(buf);
  235. kfree(buf);
  236. }
  237. }
  238. } while (list_next(&ptr->list) != &scm_framework_list); // 枚举链表的每一个ui框架
  239. // 设置定时刷新的对象
  240. video_set_refresh_target(__current_framework->buf);
  241. // 通知显示驱动,启动双缓冲
  242. video_reinitialize(true);
  243. uart_send_str(COM1, "##initialized double buffer##\n");
  244. return 0;
  245. }
  246. /**
  247. * @brief 启用某个ui框架,将它的帧缓冲区渲染到屏幕上
  248. *
  249. * @param ui 要启动的ui框架
  250. * @return int 返回码
  251. */
  252. int scm_framework_enable(struct scm_ui_framework_t *ui)
  253. {
  254. if (ui->buf->vaddr == NULL)
  255. return -EINVAL;
  256. spin_lock(&scm_screen_own_lock);
  257. int retval = 0;
  258. if (__scm_double_buffer_enabled == true)
  259. {
  260. retval = video_set_refresh_target(ui->buf);
  261. if (retval == 0)
  262. __current_framework = ui;
  263. }
  264. else
  265. __current_framework = ui;
  266. spin_unlock(&scm_screen_own_lock);
  267. return retval;
  268. }
  269. /**
  270. * @brief 当内存管理单元被初始化之后,重新处理帧缓冲区问题
  271. *
  272. */
  273. void scm_reinit()
  274. {
  275. scm_enable_alloc();
  276. video_reinitialize(false);
  277. // 遍历当前所有使用帧缓冲区的框架,更新地址
  278. // 逐个检查已经注册了的ui框架,将其缓冲区更改为双缓冲
  279. struct scm_ui_framework_t *ptr = container_of(list_next(&scm_framework_list), struct scm_ui_framework_t, list);
  280. do
  281. {
  282. if (ptr->buf == &video_frame_buffer_info)
  283. {
  284. ptr->ui_ops->change(&video_frame_buffer_info);
  285. }
  286. } while (list_next(&ptr->list) != &scm_framework_list);
  287. return;
  288. }