screen_manager.c 7.1 KB

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