screen_manager.c 8.8 KB

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