xref: /DragonOS/kernel/src/libs/lib_ui/screen_manager.rs (revision 2755467c790d6510fa97cbf052ce8e91ad1372c6)
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),
50*2755467cS曾俊     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 
84*2755467cS曾俊             let buf_space: Arc<SpinLock<Box<[u8]>>> = Arc::new(SpinLock::new(
85*2755467cS曾俊                 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,
92*2755467cS曾俊                 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 =
158*2755467cS曾俊                     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 =
162*2755467cS曾俊                             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) => {
176*2755467cS曾俊                         let len = src.buf_size() / core::mem::size_of::<u8>();
1771496ba7bSLoGin                         double_buffer_guard.as_mut().copy_from_slice(unsafe {
178*2755467cS曾俊                             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> {
240abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
241abe3a6eaShanjiezhou     }
242abe3a6eaShanjiezhou     // 卸载ui框架的回调函数
243abe3a6eaShanjiezhou     fn uninstall(&self) -> Result<i32, SystemError> {
244abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
245abe3a6eaShanjiezhou     }
246abe3a6eaShanjiezhou     // 启用ui框架的回调函数
247abe3a6eaShanjiezhou     fn enable(&self) -> Result<i32, SystemError> {
248abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
249abe3a6eaShanjiezhou     }
250abe3a6eaShanjiezhou     // 禁用ui框架的回调函数
251abe3a6eaShanjiezhou     fn disable(&self) -> Result<i32, SystemError> {
252abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
253abe3a6eaShanjiezhou     }
254abe3a6eaShanjiezhou     // 改变ui框架的帧缓冲区的回调函数
255abe3a6eaShanjiezhou     fn change(&self, _buf: ScmBufferInfo) -> Result<i32, SystemError> {
256abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
257abe3a6eaShanjiezhou     }
258abe3a6eaShanjiezhou     /// @brief 获取ScmUiFramework的元数据
259abe3a6eaShanjiezhou     /// @return 成功:Ok(ScmUiFramework的元数据)
260abe3a6eaShanjiezhou     ///         失败:Err(错误码)
261abe3a6eaShanjiezhou     fn metadata(&self) -> Result<ScmUiFrameworkMetadata, SystemError> {
262abe3a6eaShanjiezhou         // 若文件系统没有实现此方法,则返回“不支持”
263abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
264abe3a6eaShanjiezhou     }
265abe3a6eaShanjiezhou }
266abe3a6eaShanjiezhou 
267abe3a6eaShanjiezhou /// 初始化屏幕控制模块
268abe3a6eaShanjiezhou ///
269abe3a6eaShanjiezhou /// ## 调用时机
270abe3a6eaShanjiezhou ///
271abe3a6eaShanjiezhou /// 该函数在内核启动的早期进行调用。调用时,内存管理模块尚未初始化。
2721a72a751SLoGin pub fn scm_init(enable_put_to_window: bool) {
273abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(false, Ordering::SeqCst); // 禁用双缓冲
2741a72a751SLoGin     if enable_put_to_window {
2751a72a751SLoGin         textui_enable_put_to_window();
2761a72a751SLoGin     } else {
2771a72a751SLoGin         textui_disable_put_to_window();
2781a72a751SLoGin     }
2791a72a751SLoGin     textui_init_no_alloc(enable_put_to_window);
280abe3a6eaShanjiezhou 
281a03c4f9dSLoGin     send_to_default_serial8250_port("\nfinish_scm_init\n\0".as_bytes());
282abe3a6eaShanjiezhou }
283abe3a6eaShanjiezhou 
284abe3a6eaShanjiezhou /// 启用某个ui框架,将它的帧缓冲区渲染到屏幕上
285abe3a6eaShanjiezhou /// ## 参数
286abe3a6eaShanjiezhou ///
287abe3a6eaShanjiezhou /// - framework 要启动的ui框架
288abe3a6eaShanjiezhou 
289abe3a6eaShanjiezhou pub fn scm_framework_enable(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
290abe3a6eaShanjiezhou     // 获取信息
291abe3a6eaShanjiezhou     let metadata = framework.metadata()?;
292abe3a6eaShanjiezhou 
293abe3a6eaShanjiezhou     // if metadata.buf_info.buf.is_null() {
294abe3a6eaShanjiezhou     //     return Err(SystemError::EINVAL);
295abe3a6eaShanjiezhou     // }
296abe3a6eaShanjiezhou     let mut current_framework = CURRENT_FRAMEWORK.write();
297abe3a6eaShanjiezhou 
298b5b571e0SLoGin     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) {
2991496ba7bSLoGin         video_refresh_manager().set_refresh_target(&metadata.buf_info)?;
300abe3a6eaShanjiezhou     }
301abe3a6eaShanjiezhou 
3021496ba7bSLoGin     framework.enable()?;
303abe3a6eaShanjiezhou     current_framework.replace(framework);
304abe3a6eaShanjiezhou 
305abe3a6eaShanjiezhou     return Ok(0);
306abe3a6eaShanjiezhou }
307abe3a6eaShanjiezhou /// 向屏幕管理器注册UI框架
308abe3a6eaShanjiezhou ///
309abe3a6eaShanjiezhou /// ## 参数
310abe3a6eaShanjiezhou /// - framework 框架结构体
311abe3a6eaShanjiezhou 
312abe3a6eaShanjiezhou pub fn scm_register(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
313abe3a6eaShanjiezhou     // 把ui框架加入链表
314abe3a6eaShanjiezhou 
315abe3a6eaShanjiezhou     SCM_FRAMEWORK_LIST.lock().push_back(framework.clone());
316abe3a6eaShanjiezhou     // 调用ui框架的回调函数以安装ui框架,并将其激活
317abe3a6eaShanjiezhou     framework.install()?;
318abe3a6eaShanjiezhou 
319abe3a6eaShanjiezhou     // 如果当前还没有框架获得了屏幕的控制权,就让其拿去
320abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
321abe3a6eaShanjiezhou         return scm_framework_enable(framework);
322abe3a6eaShanjiezhou     }
323abe3a6eaShanjiezhou     return Ok(0);
324abe3a6eaShanjiezhou }
325abe3a6eaShanjiezhou 
3261496ba7bSLoGin /// 屏幕管理器启用双缓冲区
3271496ba7bSLoGin #[allow(dead_code)]
3281496ba7bSLoGin pub fn scm_enable_double_buffer() -> Result<i32, SystemError> {
329abe3a6eaShanjiezhou     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) {
330abe3a6eaShanjiezhou         // 已经开启了双缓冲区了, 直接退出
331abe3a6eaShanjiezhou         return Ok(0);
332abe3a6eaShanjiezhou     }
333abe3a6eaShanjiezhou     let scm_list = SCM_FRAMEWORK_LIST.lock();
334abe3a6eaShanjiezhou     if scm_list.is_empty() {
335abe3a6eaShanjiezhou         // scm 框架链表为空
336abe3a6eaShanjiezhou         return Ok(0);
337abe3a6eaShanjiezhou     }
338abe3a6eaShanjiezhou     drop(scm_list);
339abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(true, Ordering::SeqCst);
340abe3a6eaShanjiezhou     // 创建双缓冲区
3411496ba7bSLoGin     let buf_info = ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
3421496ba7bSLoGin 
3431496ba7bSLoGin     // 设置定时刷新的对象
3441496ba7bSLoGin     video_refresh_manager()
3451496ba7bSLoGin         .set_refresh_target(&buf_info)
3461496ba7bSLoGin         .expect("set refresh target failed");
3471496ba7bSLoGin 
3481496ba7bSLoGin     // 设置当前框架的帧缓冲区
349abe3a6eaShanjiezhou     CURRENT_FRAMEWORK
350abe3a6eaShanjiezhou         .write()
351abe3a6eaShanjiezhou         .as_ref()
352abe3a6eaShanjiezhou         .unwrap()
353abe3a6eaShanjiezhou         .change(buf_info)?;
354abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新为双缓冲区
355abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
356abe3a6eaShanjiezhou         if !(*framework).metadata()?.buf_info.is_double_buffer() {
357abe3a6eaShanjiezhou             let new_buf_info =
358abe3a6eaShanjiezhou                 ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
359abe3a6eaShanjiezhou             (*framework).change(new_buf_info)?;
360abe3a6eaShanjiezhou         }
361abe3a6eaShanjiezhou     }
362abe3a6eaShanjiezhou     // 通知显示驱动,启动双缓冲
3631496ba7bSLoGin     video_refresh_manager().video_reinitialize(true)?;
364abe3a6eaShanjiezhou 
365abe3a6eaShanjiezhou     return Ok(0);
366abe3a6eaShanjiezhou }
3671496ba7bSLoGin 
368abe3a6eaShanjiezhou /// 允许往窗口打印信息
369abe3a6eaShanjiezhou pub fn scm_enable_put_to_window() {
3701496ba7bSLoGin     // mm之前要继续往窗口打印信息时,因为没有动态内存分配(textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
371abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
3721a72a751SLoGin         textui_enable_put_to_window();
373abe3a6eaShanjiezhou     } else {
374abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
375abe3a6eaShanjiezhou             .write()
376abe3a6eaShanjiezhou             .as_ref()
377abe3a6eaShanjiezhou             .unwrap()
378abe3a6eaShanjiezhou             .enable()
379abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
380abe3a6eaShanjiezhou         if r.is_negative() {
381a03c4f9dSLoGin             send_to_default_serial8250_port("scm_enable_put_to_window() failed.\n\0".as_bytes());
382abe3a6eaShanjiezhou         }
383abe3a6eaShanjiezhou     }
384abe3a6eaShanjiezhou }
385abe3a6eaShanjiezhou /// 禁止往窗口打印信息
386abe3a6eaShanjiezhou pub fn scm_disable_put_to_window() {
387abe3a6eaShanjiezhou     // mm之前要停止往窗口打印信息时,因为没有动态内存分配(rwlock与otion依然能用,但是textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
388abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
3891a72a751SLoGin         textui_disable_put_to_window();
390b5b571e0SLoGin         assert!(!textui_is_enable_put_to_window());
391abe3a6eaShanjiezhou     } else {
392abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
393abe3a6eaShanjiezhou             .write()
394abe3a6eaShanjiezhou             .as_ref()
395abe3a6eaShanjiezhou             .unwrap()
396abe3a6eaShanjiezhou             .disable()
397abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
398abe3a6eaShanjiezhou         if r.is_negative() {
399a03c4f9dSLoGin             send_to_default_serial8250_port("scm_disable_put_to_window() failed.\n\0".as_bytes());
400abe3a6eaShanjiezhou         }
401abe3a6eaShanjiezhou     }
402abe3a6eaShanjiezhou }
403abe3a6eaShanjiezhou /// 当内存管理单元被初始化之后,重新处理帧缓冲区问题
4045b59005fSLoGin #[inline(never)]
4055b59005fSLoGin pub fn scm_reinit() -> Result<(), SystemError> {
406338f6903SLoGin     #[cfg(target_arch = "x86_64")]
407338f6903SLoGin     {
4085b59005fSLoGin         let r = true_scm_reinit();
4095b59005fSLoGin         if r.is_err() {
410a03c4f9dSLoGin             send_to_default_serial8250_port("scm reinit failed.\n\0".as_bytes());
411abe3a6eaShanjiezhou         }
412abe3a6eaShanjiezhou         return r;
413abe3a6eaShanjiezhou     }
414338f6903SLoGin 
415338f6903SLoGin     #[cfg(not(target_arch = "x86_64"))]
416338f6903SLoGin     {
417338f6903SLoGin         return Ok(());
418338f6903SLoGin     }
419338f6903SLoGin }
420338f6903SLoGin 
421338f6903SLoGin #[allow(dead_code)]
4225b59005fSLoGin fn true_scm_reinit() -> Result<(), SystemError> {
4231496ba7bSLoGin     video_refresh_manager()
4241496ba7bSLoGin         .video_reinitialize(false)
4251496ba7bSLoGin         .expect("video reinitialize failed");
426abe3a6eaShanjiezhou 
427abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新地址
4281496ba7bSLoGin     let device_buffer = video_refresh_manager().device_buffer().clone();
429abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
430abe3a6eaShanjiezhou         if framework.metadata()?.buf_info().is_device_buffer() {
4311496ba7bSLoGin             framework.change(device_buffer.clone())?;
432abe3a6eaShanjiezhou         }
433abe3a6eaShanjiezhou     }
434abe3a6eaShanjiezhou 
435abe3a6eaShanjiezhou     scm_enable_put_to_window();
436abe3a6eaShanjiezhou 
4375b59005fSLoGin     return Ok(());
438abe3a6eaShanjiezhou }
439