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