xref: /DragonOS/kernel/src/libs/lib_ui/screen_manager.rs (revision 52da9a59374752b4d01907b052135a0d317781dd)
1abe3a6eaShanjiezhou use core::{
2abe3a6eaShanjiezhou     fmt::Debug,
3abe3a6eaShanjiezhou     intrinsics::unlikely,
4abe3a6eaShanjiezhou     sync::atomic::{AtomicBool, AtomicU32, Ordering},
5abe3a6eaShanjiezhou };
6abe3a6eaShanjiezhou 
7abe3a6eaShanjiezhou use alloc::{boxed::Box, collections::LinkedList, string::String, sync::Arc};
891e9d4abSLoGin use system_error::SystemError;
9abe3a6eaShanjiezhou 
10abe3a6eaShanjiezhou use crate::{
11*52da9a59SGnoCiYeH     driver::{serial::serial8250::send_to_default_serial8250_port, video::video_refresh_manager},
121a72a751SLoGin     libs::{lib_ui::textui::textui_is_enable_put_to_window, rwlock::RwLock, spinlock::SpinLock},
13abe3a6eaShanjiezhou     mm::VirtAddr,
14abe3a6eaShanjiezhou };
15abe3a6eaShanjiezhou 
161a72a751SLoGin use super::{
171a72a751SLoGin     textui::{textui_disable_put_to_window, textui_enable_put_to_window},
181a72a751SLoGin     textui_no_alloc::textui_init_no_alloc,
191a72a751SLoGin };
20abe3a6eaShanjiezhou 
21abe3a6eaShanjiezhou /// 全局的UI框架列表
221496ba7bSLoGin pub static SCM_FRAMEWORK_LIST: SpinLock<LinkedList<Arc<dyn ScmUiFramework>>> =
23abe3a6eaShanjiezhou     SpinLock::new(LinkedList::new());
24abe3a6eaShanjiezhou 
251496ba7bSLoGin /// 当前在使用的UI框架
261496ba7bSLoGin pub static CURRENT_FRAMEWORK: RwLock<Option<Arc<dyn ScmUiFramework>>> = RwLock::new(None);
27abe3a6eaShanjiezhou 
28abe3a6eaShanjiezhou /// 是否启用双缓冲
29abe3a6eaShanjiezhou pub static SCM_DOUBLE_BUFFER_ENABLED: AtomicBool = AtomicBool::new(false);
30abe3a6eaShanjiezhou 
31abe3a6eaShanjiezhou bitflags! {
32abe3a6eaShanjiezhou   pub struct ScmBufferFlag:u8 {
33abe3a6eaShanjiezhou     // 帧缓冲区标志位
34abe3a6eaShanjiezhou        const SCM_BF_FB = 1 << 0; // 当前buffer是设备显存中的帧缓冲区
35abe3a6eaShanjiezhou        const SCM_BF_DB = 1 << 1; // 当前buffer是双缓冲
36abe3a6eaShanjiezhou        const SCM_BF_TEXT = 1 << 2; // 使用文本模式
37abe3a6eaShanjiezhou        const SCM_BF_PIXEL = 1 << 3; // 使用图像模式
38abe3a6eaShanjiezhou    }
39abe3a6eaShanjiezhou }
40abe3a6eaShanjiezhou #[derive(Clone, Debug)]
41abe3a6eaShanjiezhou #[allow(dead_code)]
42abe3a6eaShanjiezhou pub enum ScmFramworkType {
43abe3a6eaShanjiezhou     Text,
44abe3a6eaShanjiezhou     Gui,
45abe3a6eaShanjiezhou     Unused,
46abe3a6eaShanjiezhou }
471496ba7bSLoGin #[derive(Debug, Clone)]
48abe3a6eaShanjiezhou pub enum ScmBuffer {
491496ba7bSLoGin     DeviceBuffer(VirtAddr),
501496ba7bSLoGin     DoubleBuffer(Arc<SpinLock<Box<[u32]>>>),
51abe3a6eaShanjiezhou }
521496ba7bSLoGin 
531496ba7bSLoGin #[derive(Debug, Clone)]
54abe3a6eaShanjiezhou pub struct ScmBufferInfo {
55abe3a6eaShanjiezhou     width: u32,     // 帧缓冲区宽度(pixel或columns)
56abe3a6eaShanjiezhou     height: u32,    // 帧缓冲区高度(pixel或lines)
57abe3a6eaShanjiezhou     size: u32,      // 帧缓冲区大小(bytes)
58abe3a6eaShanjiezhou     bit_depth: u32, // 像素点位深度
59abe3a6eaShanjiezhou     pub buf: ScmBuffer,
60abe3a6eaShanjiezhou     flags: ScmBufferFlag, // 帧缓冲区标志位
61abe3a6eaShanjiezhou }
62abe3a6eaShanjiezhou 
631496ba7bSLoGin #[allow(dead_code)]
64abe3a6eaShanjiezhou impl ScmBufferInfo {
65abe3a6eaShanjiezhou     /// 创建新的帧缓冲区信息
66abe3a6eaShanjiezhou     ///
67abe3a6eaShanjiezhou     /// ## 参数
68abe3a6eaShanjiezhou     ///
69abe3a6eaShanjiezhou     /// - `buf_type` 帧缓冲区类型
70abe3a6eaShanjiezhou     ///
71abe3a6eaShanjiezhou     /// ## 返回值
72abe3a6eaShanjiezhou     ///
73abe3a6eaShanjiezhou     /// - `Result<Self, SystemError>` 创建成功返回新的帧缓冲区结构体,创建失败返回错误码
741496ba7bSLoGin     pub fn new(mut buf_type: ScmBufferFlag) -> Result<Self, SystemError> {
75abe3a6eaShanjiezhou         if unlikely(SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) == false) {
761496ba7bSLoGin             let mut device_buffer = video_refresh_manager().device_buffer().clone();
771496ba7bSLoGin             buf_type.remove(ScmBufferFlag::SCM_BF_DB);
781496ba7bSLoGin             buf_type.insert(ScmBufferFlag::SCM_BF_FB);
791496ba7bSLoGin             device_buffer.flags = buf_type;
801496ba7bSLoGin             return Ok(device_buffer);
81abe3a6eaShanjiezhou         } else {
821496ba7bSLoGin             let device_buffer_guard = video_refresh_manager().device_buffer();
83abe3a6eaShanjiezhou 
841496ba7bSLoGin             let buf_space: Arc<SpinLock<Box<[u32]>>> = Arc::new(SpinLock::new(
851496ba7bSLoGin                 vec![0u32; (device_buffer_guard.size / 4) as usize].into_boxed_slice(),
86abe3a6eaShanjiezhou             ));
87abe3a6eaShanjiezhou 
881496ba7bSLoGin             assert!(buf_type.contains(ScmBufferFlag::SCM_BF_DB));
89abe3a6eaShanjiezhou 
901496ba7bSLoGin             assert_eq!(
911496ba7bSLoGin                 device_buffer_guard.size as usize,
921496ba7bSLoGin                 buf_space.lock().len() * core::mem::size_of::<u32>()
931496ba7bSLoGin             );
94abe3a6eaShanjiezhou 
951496ba7bSLoGin             // 创建双缓冲区
961496ba7bSLoGin             let buffer = Self {
971496ba7bSLoGin                 width: device_buffer_guard.width,
981496ba7bSLoGin                 height: device_buffer_guard.height,
991496ba7bSLoGin                 size: device_buffer_guard.size,
1001496ba7bSLoGin                 bit_depth: device_buffer_guard.bit_depth,
1011496ba7bSLoGin                 flags: buf_type,
1021496ba7bSLoGin                 buf: ScmBuffer::DoubleBuffer(buf_space),
103abe3a6eaShanjiezhou             };
1041496ba7bSLoGin             drop(device_buffer_guard);
1051496ba7bSLoGin 
1061496ba7bSLoGin             return Ok(buffer);
107abe3a6eaShanjiezhou         }
108abe3a6eaShanjiezhou     }
1091496ba7bSLoGin 
1101496ba7bSLoGin     pub unsafe fn new_device_buffer(
1111496ba7bSLoGin         width: u32,
1121496ba7bSLoGin         height: u32,
1131496ba7bSLoGin         size: u32,
1141496ba7bSLoGin         bit_depth: u32,
1151496ba7bSLoGin         buf_type: ScmBufferFlag,
1161496ba7bSLoGin         vaddr: VirtAddr,
1171496ba7bSLoGin     ) -> Result<Self, SystemError> {
1181496ba7bSLoGin         let buffer = Self {
1191496ba7bSLoGin             width,
1201496ba7bSLoGin             height,
1211496ba7bSLoGin             size,
1221496ba7bSLoGin             bit_depth,
1231496ba7bSLoGin             flags: buf_type,
1241496ba7bSLoGin             buf: ScmBuffer::DeviceBuffer(vaddr),
1251496ba7bSLoGin         };
1261496ba7bSLoGin         return Ok(buffer);
127abe3a6eaShanjiezhou     }
1281496ba7bSLoGin 
1291496ba7bSLoGin     pub fn buf_size(&self) -> usize {
1301496ba7bSLoGin         self.size as usize
1311496ba7bSLoGin     }
1321496ba7bSLoGin 
1331496ba7bSLoGin     pub fn bit_depth(&self) -> u32 {
1341496ba7bSLoGin         self.bit_depth
1351496ba7bSLoGin     }
1361496ba7bSLoGin 
1371496ba7bSLoGin     pub fn height(&self) -> u32 {
138abe3a6eaShanjiezhou         self.height
139abe3a6eaShanjiezhou     }
1401496ba7bSLoGin 
1411496ba7bSLoGin     pub fn width(&self) -> u32 {
142abe3a6eaShanjiezhou         self.width
143abe3a6eaShanjiezhou     }
1441496ba7bSLoGin 
145abe3a6eaShanjiezhou     pub fn is_double_buffer(&self) -> bool {
146abe3a6eaShanjiezhou         match &self.buf {
147abe3a6eaShanjiezhou             ScmBuffer::DoubleBuffer(_) => true,
148abe3a6eaShanjiezhou             _ => false,
149abe3a6eaShanjiezhou         }
150abe3a6eaShanjiezhou     }
151abe3a6eaShanjiezhou     pub fn is_device_buffer(&self) -> bool {
152abe3a6eaShanjiezhou         match &self.buf {
153abe3a6eaShanjiezhou             ScmBuffer::DeviceBuffer(_) => true,
154abe3a6eaShanjiezhou             _ => false,
155abe3a6eaShanjiezhou         }
156abe3a6eaShanjiezhou     }
1571496ba7bSLoGin 
1581496ba7bSLoGin     pub fn copy_from_nonoverlapping(&mut self, src: &ScmBufferInfo) {
1591496ba7bSLoGin         assert!(self.buf_size() == src.buf_size());
1601496ba7bSLoGin         match &self.buf {
1611496ba7bSLoGin             ScmBuffer::DeviceBuffer(vaddr) => {
1621496ba7bSLoGin                 let len = self.buf_size() / core::mem::size_of::<u32>();
1631496ba7bSLoGin                 let self_buf_guard =
1641496ba7bSLoGin                     unsafe { core::slice::from_raw_parts_mut(vaddr.data() as *mut u32, len) };
1651496ba7bSLoGin                 match &src.buf {
1661496ba7bSLoGin                     ScmBuffer::DeviceBuffer(vaddr) => {
1671496ba7bSLoGin                         let src_buf_guard =
1681496ba7bSLoGin                             unsafe { core::slice::from_raw_parts(vaddr.data() as *const u32, len) };
1691496ba7bSLoGin                         self_buf_guard.copy_from_slice(src_buf_guard);
1701496ba7bSLoGin                     }
1711496ba7bSLoGin                     ScmBuffer::DoubleBuffer(double_buffer) => {
1721496ba7bSLoGin                         let src_buf_guard = double_buffer.lock();
1731496ba7bSLoGin                         self_buf_guard.copy_from_slice(src_buf_guard.as_ref());
1741496ba7bSLoGin                     }
1751496ba7bSLoGin                 };
176abe3a6eaShanjiezhou             }
177abe3a6eaShanjiezhou 
1781496ba7bSLoGin             ScmBuffer::DoubleBuffer(double_buffer) => {
1791496ba7bSLoGin                 let mut double_buffer_guard = double_buffer.lock();
1801496ba7bSLoGin                 match &src.buf {
1811496ba7bSLoGin                     ScmBuffer::DeviceBuffer(vaddr) => {
1821496ba7bSLoGin                         let len = src.buf_size() / core::mem::size_of::<u32>();
1831496ba7bSLoGin                         double_buffer_guard.as_mut().copy_from_slice(unsafe {
1841496ba7bSLoGin                             core::slice::from_raw_parts(vaddr.data() as *const u32, len)
1851496ba7bSLoGin                         });
1861496ba7bSLoGin                     }
1871496ba7bSLoGin                     ScmBuffer::DoubleBuffer(double_buffer) => {
1881496ba7bSLoGin                         let x = double_buffer.lock();
1891496ba7bSLoGin                         double_buffer_guard.as_mut().copy_from_slice(x.as_ref());
1901496ba7bSLoGin                     }
1911496ba7bSLoGin                 };
192abe3a6eaShanjiezhou             }
193abe3a6eaShanjiezhou         }
194abe3a6eaShanjiezhou     }
195abe3a6eaShanjiezhou }
1961496ba7bSLoGin 
197abe3a6eaShanjiezhou #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
198abe3a6eaShanjiezhou pub struct ScmUiFrameworkId(u32);
199abe3a6eaShanjiezhou 
200abe3a6eaShanjiezhou impl ScmUiFrameworkId {
201abe3a6eaShanjiezhou     /// 分配一个新的框架id
202abe3a6eaShanjiezhou     pub fn new() -> Self {
203abe3a6eaShanjiezhou         static MAX_ID: AtomicU32 = AtomicU32::new(0);
204abe3a6eaShanjiezhou         return ScmUiFrameworkId(MAX_ID.fetch_add(1, Ordering::SeqCst));
205abe3a6eaShanjiezhou     }
206abe3a6eaShanjiezhou }
207abe3a6eaShanjiezhou #[allow(dead_code)]
208abe3a6eaShanjiezhou #[derive(Debug, Clone)]
209abe3a6eaShanjiezhou pub struct ScmUiFrameworkMetadata {
210abe3a6eaShanjiezhou     id: ScmUiFrameworkId,
211abe3a6eaShanjiezhou     name: String,
212abe3a6eaShanjiezhou     framework_type: ScmFramworkType,
213abe3a6eaShanjiezhou     pub buf_info: ScmBufferInfo,
214abe3a6eaShanjiezhou }
215abe3a6eaShanjiezhou 
216abe3a6eaShanjiezhou impl ScmUiFrameworkMetadata {
217abe3a6eaShanjiezhou     pub fn new(name: String, framework_type: ScmFramworkType) -> Self {
218abe3a6eaShanjiezhou         match framework_type {
219abe3a6eaShanjiezhou             ScmFramworkType::Text => {
220abe3a6eaShanjiezhou                 let result = ScmUiFrameworkMetadata {
221abe3a6eaShanjiezhou                     id: ScmUiFrameworkId::new(),
222abe3a6eaShanjiezhou                     name,
223abe3a6eaShanjiezhou                     framework_type: ScmFramworkType::Text,
2241496ba7bSLoGin                     buf_info: ScmBufferInfo::new(
2251496ba7bSLoGin                         ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_DB,
2261496ba7bSLoGin                     )
2271496ba7bSLoGin                     .unwrap(),
228abe3a6eaShanjiezhou                 };
229abe3a6eaShanjiezhou 
230abe3a6eaShanjiezhou                 return result;
231abe3a6eaShanjiezhou             }
232abe3a6eaShanjiezhou             ScmFramworkType::Gui => todo!(),
233abe3a6eaShanjiezhou             ScmFramworkType::Unused => todo!(),
234abe3a6eaShanjiezhou         }
235abe3a6eaShanjiezhou     }
236abe3a6eaShanjiezhou     pub fn buf_info(&self) -> ScmBufferInfo {
237abe3a6eaShanjiezhou         return self.buf_info.clone();
238abe3a6eaShanjiezhou     }
239abe3a6eaShanjiezhou     pub fn set_buf_info(&mut self, buf_info: ScmBufferInfo) {
240abe3a6eaShanjiezhou         self.buf_info = buf_info;
241abe3a6eaShanjiezhou     }
242abe3a6eaShanjiezhou }
243abe3a6eaShanjiezhou pub trait ScmUiFramework: Sync + Send + Debug {
244abe3a6eaShanjiezhou     // 安装ui框架的回调函数
245abe3a6eaShanjiezhou     fn install(&self) -> Result<i32, SystemError> {
246abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
247abe3a6eaShanjiezhou     }
248abe3a6eaShanjiezhou     // 卸载ui框架的回调函数
249abe3a6eaShanjiezhou     fn uninstall(&self) -> Result<i32, SystemError> {
250abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
251abe3a6eaShanjiezhou     }
252abe3a6eaShanjiezhou     // 启用ui框架的回调函数
253abe3a6eaShanjiezhou     fn enable(&self) -> Result<i32, SystemError> {
254abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
255abe3a6eaShanjiezhou     }
256abe3a6eaShanjiezhou     // 禁用ui框架的回调函数
257abe3a6eaShanjiezhou     fn disable(&self) -> Result<i32, SystemError> {
258abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
259abe3a6eaShanjiezhou     }
260abe3a6eaShanjiezhou     // 改变ui框架的帧缓冲区的回调函数
261abe3a6eaShanjiezhou     fn change(&self, _buf: ScmBufferInfo) -> Result<i32, SystemError> {
262abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
263abe3a6eaShanjiezhou     }
264abe3a6eaShanjiezhou     /// @brief 获取ScmUiFramework的元数据
265abe3a6eaShanjiezhou     /// @return 成功:Ok(ScmUiFramework的元数据)
266abe3a6eaShanjiezhou     ///         失败:Err(错误码)
267abe3a6eaShanjiezhou     fn metadata(&self) -> Result<ScmUiFrameworkMetadata, SystemError> {
268abe3a6eaShanjiezhou         // 若文件系统没有实现此方法,则返回“不支持”
269abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
270abe3a6eaShanjiezhou     }
271abe3a6eaShanjiezhou }
272abe3a6eaShanjiezhou 
273abe3a6eaShanjiezhou /// 初始化屏幕控制模块
274abe3a6eaShanjiezhou ///
275abe3a6eaShanjiezhou /// ## 调用时机
276abe3a6eaShanjiezhou ///
277abe3a6eaShanjiezhou /// 该函数在内核启动的早期进行调用。调用时,内存管理模块尚未初始化。
2781a72a751SLoGin pub fn scm_init(enable_put_to_window: bool) {
279abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(false, Ordering::SeqCst); // 禁用双缓冲
2801a72a751SLoGin     if enable_put_to_window {
2811a72a751SLoGin         textui_enable_put_to_window();
2821a72a751SLoGin     } else {
2831a72a751SLoGin         textui_disable_put_to_window();
2841a72a751SLoGin     }
2851a72a751SLoGin     textui_init_no_alloc(enable_put_to_window);
286abe3a6eaShanjiezhou 
287a03c4f9dSLoGin     send_to_default_serial8250_port("\nfinish_scm_init\n\0".as_bytes());
288abe3a6eaShanjiezhou }
289abe3a6eaShanjiezhou 
290abe3a6eaShanjiezhou /// 启用某个ui框架,将它的帧缓冲区渲染到屏幕上
291abe3a6eaShanjiezhou /// ## 参数
292abe3a6eaShanjiezhou ///
293abe3a6eaShanjiezhou /// - framework 要启动的ui框架
294abe3a6eaShanjiezhou 
295abe3a6eaShanjiezhou pub fn scm_framework_enable(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
296abe3a6eaShanjiezhou     // 获取信息
297abe3a6eaShanjiezhou     let metadata = framework.metadata()?;
298abe3a6eaShanjiezhou 
299abe3a6eaShanjiezhou     // if metadata.buf_info.buf.is_null() {
300abe3a6eaShanjiezhou     //     return Err(SystemError::EINVAL);
301abe3a6eaShanjiezhou     // }
302abe3a6eaShanjiezhou     let mut current_framework = CURRENT_FRAMEWORK.write();
303abe3a6eaShanjiezhou 
304abe3a6eaShanjiezhou     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) == true {
3051496ba7bSLoGin         video_refresh_manager().set_refresh_target(&metadata.buf_info)?;
306abe3a6eaShanjiezhou     }
307abe3a6eaShanjiezhou 
3081496ba7bSLoGin     framework.enable()?;
309abe3a6eaShanjiezhou     current_framework.replace(framework);
310abe3a6eaShanjiezhou 
311abe3a6eaShanjiezhou     return Ok(0);
312abe3a6eaShanjiezhou }
313abe3a6eaShanjiezhou /// 向屏幕管理器注册UI框架
314abe3a6eaShanjiezhou ///
315abe3a6eaShanjiezhou /// ## 参数
316abe3a6eaShanjiezhou /// - framework 框架结构体
317abe3a6eaShanjiezhou 
318abe3a6eaShanjiezhou pub fn scm_register(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
319abe3a6eaShanjiezhou     // 把ui框架加入链表
320abe3a6eaShanjiezhou 
321abe3a6eaShanjiezhou     SCM_FRAMEWORK_LIST.lock().push_back(framework.clone());
322abe3a6eaShanjiezhou     // 调用ui框架的回调函数以安装ui框架,并将其激活
323abe3a6eaShanjiezhou     framework.install()?;
324abe3a6eaShanjiezhou 
325abe3a6eaShanjiezhou     // 如果当前还没有框架获得了屏幕的控制权,就让其拿去
326abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
327abe3a6eaShanjiezhou         return scm_framework_enable(framework);
328abe3a6eaShanjiezhou     }
329abe3a6eaShanjiezhou     return Ok(0);
330abe3a6eaShanjiezhou }
331abe3a6eaShanjiezhou 
3321496ba7bSLoGin /// 屏幕管理器启用双缓冲区
3331496ba7bSLoGin #[allow(dead_code)]
3341496ba7bSLoGin pub fn scm_enable_double_buffer() -> Result<i32, SystemError> {
335abe3a6eaShanjiezhou     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) {
336abe3a6eaShanjiezhou         // 已经开启了双缓冲区了, 直接退出
337abe3a6eaShanjiezhou         return Ok(0);
338abe3a6eaShanjiezhou     }
339abe3a6eaShanjiezhou     let scm_list = SCM_FRAMEWORK_LIST.lock();
340abe3a6eaShanjiezhou     if scm_list.is_empty() {
341abe3a6eaShanjiezhou         // scm 框架链表为空
342abe3a6eaShanjiezhou         return Ok(0);
343abe3a6eaShanjiezhou     }
344abe3a6eaShanjiezhou     drop(scm_list);
345abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(true, Ordering::SeqCst);
346abe3a6eaShanjiezhou     // 创建双缓冲区
3471496ba7bSLoGin     let buf_info = ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
3481496ba7bSLoGin 
3491496ba7bSLoGin     // 设置定时刷新的对象
3501496ba7bSLoGin     video_refresh_manager()
3511496ba7bSLoGin         .set_refresh_target(&buf_info)
3521496ba7bSLoGin         .expect("set refresh target failed");
3531496ba7bSLoGin 
3541496ba7bSLoGin     // 设置当前框架的帧缓冲区
355abe3a6eaShanjiezhou     CURRENT_FRAMEWORK
356abe3a6eaShanjiezhou         .write()
357abe3a6eaShanjiezhou         .as_ref()
358abe3a6eaShanjiezhou         .unwrap()
359abe3a6eaShanjiezhou         .change(buf_info)?;
360abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新为双缓冲区
361abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
362abe3a6eaShanjiezhou         if !(*framework).metadata()?.buf_info.is_double_buffer() {
363abe3a6eaShanjiezhou             let new_buf_info =
364abe3a6eaShanjiezhou                 ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
365abe3a6eaShanjiezhou             (*framework).change(new_buf_info)?;
366abe3a6eaShanjiezhou         }
367abe3a6eaShanjiezhou     }
368abe3a6eaShanjiezhou     // 通知显示驱动,启动双缓冲
3691496ba7bSLoGin     video_refresh_manager().video_reinitialize(true)?;
370abe3a6eaShanjiezhou 
371abe3a6eaShanjiezhou     return Ok(0);
372abe3a6eaShanjiezhou }
3731496ba7bSLoGin 
374abe3a6eaShanjiezhou /// 允许往窗口打印信息
375abe3a6eaShanjiezhou pub fn scm_enable_put_to_window() {
3761496ba7bSLoGin     // mm之前要继续往窗口打印信息时,因为没有动态内存分配(textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
377abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
3781a72a751SLoGin         textui_enable_put_to_window();
379abe3a6eaShanjiezhou     } else {
380abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
381abe3a6eaShanjiezhou             .write()
382abe3a6eaShanjiezhou             .as_ref()
383abe3a6eaShanjiezhou             .unwrap()
384abe3a6eaShanjiezhou             .enable()
385abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
386abe3a6eaShanjiezhou         if r.is_negative() {
387a03c4f9dSLoGin             send_to_default_serial8250_port("scm_enable_put_to_window() failed.\n\0".as_bytes());
388abe3a6eaShanjiezhou         }
389abe3a6eaShanjiezhou     }
390abe3a6eaShanjiezhou }
391abe3a6eaShanjiezhou /// 禁止往窗口打印信息
392abe3a6eaShanjiezhou pub fn scm_disable_put_to_window() {
393abe3a6eaShanjiezhou     // mm之前要停止往窗口打印信息时,因为没有动态内存分配(rwlock与otion依然能用,但是textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
394abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
3951a72a751SLoGin         textui_disable_put_to_window();
3961a72a751SLoGin         assert!(textui_is_enable_put_to_window() == false);
397abe3a6eaShanjiezhou     } else {
398abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
399abe3a6eaShanjiezhou             .write()
400abe3a6eaShanjiezhou             .as_ref()
401abe3a6eaShanjiezhou             .unwrap()
402abe3a6eaShanjiezhou             .disable()
403abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
404abe3a6eaShanjiezhou         if r.is_negative() {
405a03c4f9dSLoGin             send_to_default_serial8250_port("scm_disable_put_to_window() failed.\n\0".as_bytes());
406abe3a6eaShanjiezhou         }
407abe3a6eaShanjiezhou     }
408abe3a6eaShanjiezhou }
409abe3a6eaShanjiezhou /// 当内存管理单元被初始化之后,重新处理帧缓冲区问题
4105b59005fSLoGin #[inline(never)]
4115b59005fSLoGin pub fn scm_reinit() -> Result<(), SystemError> {
4125b59005fSLoGin     let r = true_scm_reinit();
4135b59005fSLoGin     if r.is_err() {
414a03c4f9dSLoGin         send_to_default_serial8250_port("scm reinit failed.\n\0".as_bytes());
415abe3a6eaShanjiezhou     }
416abe3a6eaShanjiezhou     return r;
417abe3a6eaShanjiezhou }
4185b59005fSLoGin fn true_scm_reinit() -> Result<(), SystemError> {
4191496ba7bSLoGin     video_refresh_manager()
4201496ba7bSLoGin         .video_reinitialize(false)
4211496ba7bSLoGin         .expect("video reinitialize failed");
422abe3a6eaShanjiezhou 
423abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新地址
4241496ba7bSLoGin     let device_buffer = video_refresh_manager().device_buffer().clone();
425abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
426abe3a6eaShanjiezhou         if framework.metadata()?.buf_info().is_device_buffer() {
4271496ba7bSLoGin             framework.change(device_buffer.clone())?;
428abe3a6eaShanjiezhou         }
429abe3a6eaShanjiezhou     }
430abe3a6eaShanjiezhou 
431abe3a6eaShanjiezhou     scm_enable_put_to_window();
432abe3a6eaShanjiezhou 
4335b59005fSLoGin     return Ok(());
434abe3a6eaShanjiezhou }
435