xref: /DragonOS/kernel/src/libs/lib_ui/screen_manager.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
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::{
1152da9a59SGnoCiYeH     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),
502755467cS曾俊     DoubleBuffer(Arc<SpinLock<Box<[u8]>>>),
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> {
75b5b571e0SLoGin         if unlikely(!SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst)) {
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 
842755467cS曾俊             let buf_space: Arc<SpinLock<Box<[u8]>>> = Arc::new(SpinLock::new(
852755467cS曾俊                 vec![0u8; (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,
922755467cS曾俊                 buf_space.lock().len() * core::mem::size_of::<u8>()
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 {
146b5b571e0SLoGin         matches!(&self.buf, ScmBuffer::DoubleBuffer(_))
147abe3a6eaShanjiezhou     }
148abe3a6eaShanjiezhou     pub fn is_device_buffer(&self) -> bool {
149b5b571e0SLoGin         matches!(&self.buf, ScmBuffer::DeviceBuffer(_))
150abe3a6eaShanjiezhou     }
1511496ba7bSLoGin 
1521496ba7bSLoGin     pub fn copy_from_nonoverlapping(&mut self, src: &ScmBufferInfo) {
1531496ba7bSLoGin         assert!(self.buf_size() == src.buf_size());
1541496ba7bSLoGin         match &self.buf {
1551496ba7bSLoGin             ScmBuffer::DeviceBuffer(vaddr) => {
1561496ba7bSLoGin                 let len = self.buf_size() / core::mem::size_of::<u32>();
1571496ba7bSLoGin                 let self_buf_guard =
1582755467cS曾俊                     unsafe { core::slice::from_raw_parts_mut(vaddr.data() as *mut u8, len) };
1591496ba7bSLoGin                 match &src.buf {
1601496ba7bSLoGin                     ScmBuffer::DeviceBuffer(vaddr) => {
1611496ba7bSLoGin                         let src_buf_guard =
1622755467cS曾俊                             unsafe { core::slice::from_raw_parts(vaddr.data() as *const u8, len) };
1631496ba7bSLoGin                         self_buf_guard.copy_from_slice(src_buf_guard);
1641496ba7bSLoGin                     }
1651496ba7bSLoGin                     ScmBuffer::DoubleBuffer(double_buffer) => {
1661496ba7bSLoGin                         let src_buf_guard = double_buffer.lock();
1671496ba7bSLoGin                         self_buf_guard.copy_from_slice(src_buf_guard.as_ref());
1681496ba7bSLoGin                     }
1691496ba7bSLoGin                 };
170abe3a6eaShanjiezhou             }
171abe3a6eaShanjiezhou 
1721496ba7bSLoGin             ScmBuffer::DoubleBuffer(double_buffer) => {
1731496ba7bSLoGin                 let mut double_buffer_guard = double_buffer.lock();
1741496ba7bSLoGin                 match &src.buf {
1751496ba7bSLoGin                     ScmBuffer::DeviceBuffer(vaddr) => {
1762755467cS曾俊                         let len = src.buf_size() / core::mem::size_of::<u8>();
1771496ba7bSLoGin                         double_buffer_guard.as_mut().copy_from_slice(unsafe {
1782755467cS曾俊                             core::slice::from_raw_parts(vaddr.data() as *const u8, len)
1791496ba7bSLoGin                         });
1801496ba7bSLoGin                     }
1811496ba7bSLoGin                     ScmBuffer::DoubleBuffer(double_buffer) => {
1821496ba7bSLoGin                         let x = double_buffer.lock();
1831496ba7bSLoGin                         double_buffer_guard.as_mut().copy_from_slice(x.as_ref());
1841496ba7bSLoGin                     }
1851496ba7bSLoGin                 };
186abe3a6eaShanjiezhou             }
187abe3a6eaShanjiezhou         }
188abe3a6eaShanjiezhou     }
189abe3a6eaShanjiezhou }
1901496ba7bSLoGin 
191abe3a6eaShanjiezhou #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
192abe3a6eaShanjiezhou pub struct ScmUiFrameworkId(u32);
193abe3a6eaShanjiezhou 
194abe3a6eaShanjiezhou impl ScmUiFrameworkId {
195abe3a6eaShanjiezhou     /// 分配一个新的框架id
196abe3a6eaShanjiezhou     pub fn new() -> Self {
197abe3a6eaShanjiezhou         static MAX_ID: AtomicU32 = AtomicU32::new(0);
198abe3a6eaShanjiezhou         return ScmUiFrameworkId(MAX_ID.fetch_add(1, Ordering::SeqCst));
199abe3a6eaShanjiezhou     }
200abe3a6eaShanjiezhou }
201abe3a6eaShanjiezhou #[allow(dead_code)]
202abe3a6eaShanjiezhou #[derive(Debug, Clone)]
203abe3a6eaShanjiezhou pub struct ScmUiFrameworkMetadata {
204abe3a6eaShanjiezhou     id: ScmUiFrameworkId,
205abe3a6eaShanjiezhou     name: String,
206abe3a6eaShanjiezhou     framework_type: ScmFramworkType,
207abe3a6eaShanjiezhou     pub buf_info: ScmBufferInfo,
208abe3a6eaShanjiezhou }
209abe3a6eaShanjiezhou 
210abe3a6eaShanjiezhou impl ScmUiFrameworkMetadata {
211abe3a6eaShanjiezhou     pub fn new(name: String, framework_type: ScmFramworkType) -> Self {
212abe3a6eaShanjiezhou         match framework_type {
213abe3a6eaShanjiezhou             ScmFramworkType::Text => {
214abe3a6eaShanjiezhou                 let result = ScmUiFrameworkMetadata {
215abe3a6eaShanjiezhou                     id: ScmUiFrameworkId::new(),
216abe3a6eaShanjiezhou                     name,
217abe3a6eaShanjiezhou                     framework_type: ScmFramworkType::Text,
2181496ba7bSLoGin                     buf_info: ScmBufferInfo::new(
2191496ba7bSLoGin                         ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_DB,
2201496ba7bSLoGin                     )
2211496ba7bSLoGin                     .unwrap(),
222abe3a6eaShanjiezhou                 };
223abe3a6eaShanjiezhou 
224abe3a6eaShanjiezhou                 return result;
225abe3a6eaShanjiezhou             }
226abe3a6eaShanjiezhou             ScmFramworkType::Gui => todo!(),
227abe3a6eaShanjiezhou             ScmFramworkType::Unused => todo!(),
228abe3a6eaShanjiezhou         }
229abe3a6eaShanjiezhou     }
230abe3a6eaShanjiezhou     pub fn buf_info(&self) -> ScmBufferInfo {
231abe3a6eaShanjiezhou         return self.buf_info.clone();
232abe3a6eaShanjiezhou     }
233abe3a6eaShanjiezhou     pub fn set_buf_info(&mut self, buf_info: ScmBufferInfo) {
234abe3a6eaShanjiezhou         self.buf_info = buf_info;
235abe3a6eaShanjiezhou     }
236abe3a6eaShanjiezhou }
237abe3a6eaShanjiezhou pub trait ScmUiFramework: Sync + Send + Debug {
238abe3a6eaShanjiezhou     // 安装ui框架的回调函数
239abe3a6eaShanjiezhou     fn install(&self) -> Result<i32, SystemError> {
2401074eb34SSamuel Dai         return Err(SystemError::ENOSYS);
241abe3a6eaShanjiezhou     }
242abe3a6eaShanjiezhou     // 卸载ui框架的回调函数
243*bd70d2d1SLoGin     #[allow(dead_code)]
244abe3a6eaShanjiezhou     fn uninstall(&self) -> Result<i32, SystemError> {
2451074eb34SSamuel Dai         return Err(SystemError::ENOSYS);
246abe3a6eaShanjiezhou     }
247abe3a6eaShanjiezhou     // 启用ui框架的回调函数
248abe3a6eaShanjiezhou     fn enable(&self) -> Result<i32, SystemError> {
2491074eb34SSamuel Dai         return Err(SystemError::ENOSYS);
250abe3a6eaShanjiezhou     }
251abe3a6eaShanjiezhou     // 禁用ui框架的回调函数
252abe3a6eaShanjiezhou     fn disable(&self) -> Result<i32, SystemError> {
2531074eb34SSamuel Dai         return Err(SystemError::ENOSYS);
254abe3a6eaShanjiezhou     }
255abe3a6eaShanjiezhou     // 改变ui框架的帧缓冲区的回调函数
256abe3a6eaShanjiezhou     fn change(&self, _buf: ScmBufferInfo) -> Result<i32, SystemError> {
2571074eb34SSamuel Dai         return Err(SystemError::ENOSYS);
258abe3a6eaShanjiezhou     }
259abe3a6eaShanjiezhou     /// @brief 获取ScmUiFramework的元数据
260abe3a6eaShanjiezhou     /// @return 成功:Ok(ScmUiFramework的元数据)
261abe3a6eaShanjiezhou     ///         失败:Err(错误码)
262abe3a6eaShanjiezhou     fn metadata(&self) -> Result<ScmUiFrameworkMetadata, SystemError> {
263abe3a6eaShanjiezhou         // 若文件系统没有实现此方法,则返回“不支持”
2641074eb34SSamuel Dai         return Err(SystemError::ENOSYS);
265abe3a6eaShanjiezhou     }
266abe3a6eaShanjiezhou }
267abe3a6eaShanjiezhou 
268abe3a6eaShanjiezhou /// 初始化屏幕控制模块
269abe3a6eaShanjiezhou ///
270abe3a6eaShanjiezhou /// ## 调用时机
271abe3a6eaShanjiezhou ///
272abe3a6eaShanjiezhou /// 该函数在内核启动的早期进行调用。调用时,内存管理模块尚未初始化。
2731a72a751SLoGin pub fn scm_init(enable_put_to_window: bool) {
274abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(false, Ordering::SeqCst); // 禁用双缓冲
2751a72a751SLoGin     if enable_put_to_window {
2761a72a751SLoGin         textui_enable_put_to_window();
2771a72a751SLoGin     } else {
2781a72a751SLoGin         textui_disable_put_to_window();
2791a72a751SLoGin     }
2801a72a751SLoGin     textui_init_no_alloc(enable_put_to_window);
281abe3a6eaShanjiezhou 
282a03c4f9dSLoGin     send_to_default_serial8250_port("\nfinish_scm_init\n\0".as_bytes());
283abe3a6eaShanjiezhou }
284abe3a6eaShanjiezhou 
285abe3a6eaShanjiezhou /// 启用某个ui框架,将它的帧缓冲区渲染到屏幕上
286abe3a6eaShanjiezhou /// ## 参数
287abe3a6eaShanjiezhou ///
288abe3a6eaShanjiezhou /// - framework 要启动的ui框架
289abe3a6eaShanjiezhou 
290abe3a6eaShanjiezhou pub fn scm_framework_enable(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
291abe3a6eaShanjiezhou     // 获取信息
292abe3a6eaShanjiezhou     let metadata = framework.metadata()?;
293abe3a6eaShanjiezhou 
294abe3a6eaShanjiezhou     // if metadata.buf_info.buf.is_null() {
295abe3a6eaShanjiezhou     //     return Err(SystemError::EINVAL);
296abe3a6eaShanjiezhou     // }
297abe3a6eaShanjiezhou     let mut current_framework = CURRENT_FRAMEWORK.write();
298abe3a6eaShanjiezhou 
299b5b571e0SLoGin     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) {
3001496ba7bSLoGin         video_refresh_manager().set_refresh_target(&metadata.buf_info)?;
301abe3a6eaShanjiezhou     }
302abe3a6eaShanjiezhou 
3031496ba7bSLoGin     framework.enable()?;
304abe3a6eaShanjiezhou     current_framework.replace(framework);
305abe3a6eaShanjiezhou 
306abe3a6eaShanjiezhou     return Ok(0);
307abe3a6eaShanjiezhou }
308abe3a6eaShanjiezhou /// 向屏幕管理器注册UI框架
309abe3a6eaShanjiezhou ///
310abe3a6eaShanjiezhou /// ## 参数
311abe3a6eaShanjiezhou /// - framework 框架结构体
312abe3a6eaShanjiezhou 
313abe3a6eaShanjiezhou pub fn scm_register(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
314abe3a6eaShanjiezhou     // 把ui框架加入链表
315abe3a6eaShanjiezhou 
316abe3a6eaShanjiezhou     SCM_FRAMEWORK_LIST.lock().push_back(framework.clone());
317abe3a6eaShanjiezhou     // 调用ui框架的回调函数以安装ui框架,并将其激活
318abe3a6eaShanjiezhou     framework.install()?;
319abe3a6eaShanjiezhou 
320abe3a6eaShanjiezhou     // 如果当前还没有框架获得了屏幕的控制权,就让其拿去
321abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
322abe3a6eaShanjiezhou         return scm_framework_enable(framework);
323abe3a6eaShanjiezhou     }
324abe3a6eaShanjiezhou     return Ok(0);
325abe3a6eaShanjiezhou }
326abe3a6eaShanjiezhou 
3271496ba7bSLoGin /// 屏幕管理器启用双缓冲区
3281496ba7bSLoGin #[allow(dead_code)]
3291496ba7bSLoGin pub fn scm_enable_double_buffer() -> Result<i32, SystemError> {
330abe3a6eaShanjiezhou     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) {
331abe3a6eaShanjiezhou         // 已经开启了双缓冲区了, 直接退出
332abe3a6eaShanjiezhou         return Ok(0);
333abe3a6eaShanjiezhou     }
334abe3a6eaShanjiezhou     let scm_list = SCM_FRAMEWORK_LIST.lock();
335abe3a6eaShanjiezhou     if scm_list.is_empty() {
336abe3a6eaShanjiezhou         // scm 框架链表为空
337abe3a6eaShanjiezhou         return Ok(0);
338abe3a6eaShanjiezhou     }
339abe3a6eaShanjiezhou     drop(scm_list);
340abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(true, Ordering::SeqCst);
341abe3a6eaShanjiezhou     // 创建双缓冲区
3421496ba7bSLoGin     let buf_info = ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
3431496ba7bSLoGin 
3441496ba7bSLoGin     // 设置定时刷新的对象
3451496ba7bSLoGin     video_refresh_manager()
3461496ba7bSLoGin         .set_refresh_target(&buf_info)
3471496ba7bSLoGin         .expect("set refresh target failed");
3481496ba7bSLoGin 
3491496ba7bSLoGin     // 设置当前框架的帧缓冲区
350abe3a6eaShanjiezhou     CURRENT_FRAMEWORK
351abe3a6eaShanjiezhou         .write()
352abe3a6eaShanjiezhou         .as_ref()
353abe3a6eaShanjiezhou         .unwrap()
354abe3a6eaShanjiezhou         .change(buf_info)?;
355abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新为双缓冲区
356abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
357abe3a6eaShanjiezhou         if !(*framework).metadata()?.buf_info.is_double_buffer() {
358abe3a6eaShanjiezhou             let new_buf_info =
359abe3a6eaShanjiezhou                 ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
360abe3a6eaShanjiezhou             (*framework).change(new_buf_info)?;
361abe3a6eaShanjiezhou         }
362abe3a6eaShanjiezhou     }
363abe3a6eaShanjiezhou     // 通知显示驱动,启动双缓冲
3641496ba7bSLoGin     video_refresh_manager().video_reinitialize(true)?;
365abe3a6eaShanjiezhou 
366abe3a6eaShanjiezhou     return Ok(0);
367abe3a6eaShanjiezhou }
3681496ba7bSLoGin 
369abe3a6eaShanjiezhou /// 允许往窗口打印信息
370abe3a6eaShanjiezhou pub fn scm_enable_put_to_window() {
3711496ba7bSLoGin     // mm之前要继续往窗口打印信息时,因为没有动态内存分配(textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
372abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
3731a72a751SLoGin         textui_enable_put_to_window();
374abe3a6eaShanjiezhou     } else {
375abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
376abe3a6eaShanjiezhou             .write()
377abe3a6eaShanjiezhou             .as_ref()
378abe3a6eaShanjiezhou             .unwrap()
379abe3a6eaShanjiezhou             .enable()
380abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
381abe3a6eaShanjiezhou         if r.is_negative() {
382a03c4f9dSLoGin             send_to_default_serial8250_port("scm_enable_put_to_window() failed.\n\0".as_bytes());
383abe3a6eaShanjiezhou         }
384abe3a6eaShanjiezhou     }
385abe3a6eaShanjiezhou }
386abe3a6eaShanjiezhou /// 禁止往窗口打印信息
387abe3a6eaShanjiezhou pub fn scm_disable_put_to_window() {
388abe3a6eaShanjiezhou     // mm之前要停止往窗口打印信息时,因为没有动态内存分配(rwlock与otion依然能用,但是textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
389abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
3901a72a751SLoGin         textui_disable_put_to_window();
391b5b571e0SLoGin         assert!(!textui_is_enable_put_to_window());
392abe3a6eaShanjiezhou     } else {
393abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
394abe3a6eaShanjiezhou             .write()
395abe3a6eaShanjiezhou             .as_ref()
396abe3a6eaShanjiezhou             .unwrap()
397abe3a6eaShanjiezhou             .disable()
398abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
399abe3a6eaShanjiezhou         if r.is_negative() {
400a03c4f9dSLoGin             send_to_default_serial8250_port("scm_disable_put_to_window() failed.\n\0".as_bytes());
401abe3a6eaShanjiezhou         }
402abe3a6eaShanjiezhou     }
403abe3a6eaShanjiezhou }
404abe3a6eaShanjiezhou /// 当内存管理单元被初始化之后,重新处理帧缓冲区问题
4055b59005fSLoGin #[inline(never)]
4065b59005fSLoGin pub fn scm_reinit() -> Result<(), SystemError> {
407338f6903SLoGin     #[cfg(target_arch = "x86_64")]
408338f6903SLoGin     {
4095b59005fSLoGin         let r = true_scm_reinit();
4105b59005fSLoGin         if r.is_err() {
411a03c4f9dSLoGin             send_to_default_serial8250_port("scm reinit failed.\n\0".as_bytes());
412abe3a6eaShanjiezhou         }
413abe3a6eaShanjiezhou         return r;
414abe3a6eaShanjiezhou     }
415338f6903SLoGin 
416338f6903SLoGin     #[cfg(not(target_arch = "x86_64"))]
417338f6903SLoGin     {
418338f6903SLoGin         return Ok(());
419338f6903SLoGin     }
420338f6903SLoGin }
421338f6903SLoGin 
422338f6903SLoGin #[allow(dead_code)]
4235b59005fSLoGin fn true_scm_reinit() -> Result<(), SystemError> {
4241496ba7bSLoGin     video_refresh_manager()
4251496ba7bSLoGin         .video_reinitialize(false)
4261496ba7bSLoGin         .expect("video reinitialize failed");
427abe3a6eaShanjiezhou 
428abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新地址
4291496ba7bSLoGin     let device_buffer = video_refresh_manager().device_buffer().clone();
430abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
431abe3a6eaShanjiezhou         if framework.metadata()?.buf_info().is_device_buffer() {
4321496ba7bSLoGin             framework.change(device_buffer.clone())?;
433abe3a6eaShanjiezhou         }
434abe3a6eaShanjiezhou     }
435abe3a6eaShanjiezhou 
436abe3a6eaShanjiezhou     scm_enable_put_to_window();
437abe3a6eaShanjiezhou 
4385b59005fSLoGin     return Ok(());
439abe3a6eaShanjiezhou }
440