xref: /DragonOS/kernel/src/libs/lib_ui/screen_manager.rs (revision 1a72a751b18cf5bbe7b5b9e91aff530de0c18501)
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};
8abe3a6eaShanjiezhou 
9abe3a6eaShanjiezhou use crate::{
101496ba7bSLoGin     driver::{
11a03c4f9dSLoGin         tty::serial::serial8250::send_to_default_serial8250_port, video::video_refresh_manager,
12abe3a6eaShanjiezhou     },
13*1a72a751SLoGin     libs::{lib_ui::textui::textui_is_enable_put_to_window, rwlock::RwLock, spinlock::SpinLock},
14abe3a6eaShanjiezhou     mm::VirtAddr,
15abe3a6eaShanjiezhou     syscall::SystemError,
16abe3a6eaShanjiezhou };
17abe3a6eaShanjiezhou 
18*1a72a751SLoGin use super::{
19*1a72a751SLoGin     textui::{textui_disable_put_to_window, textui_enable_put_to_window},
20*1a72a751SLoGin     textui_no_alloc::textui_init_no_alloc,
21*1a72a751SLoGin };
22abe3a6eaShanjiezhou 
23abe3a6eaShanjiezhou /// 全局的UI框架列表
241496ba7bSLoGin pub static SCM_FRAMEWORK_LIST: SpinLock<LinkedList<Arc<dyn ScmUiFramework>>> =
25abe3a6eaShanjiezhou     SpinLock::new(LinkedList::new());
26abe3a6eaShanjiezhou 
271496ba7bSLoGin /// 当前在使用的UI框架
281496ba7bSLoGin pub static CURRENT_FRAMEWORK: RwLock<Option<Arc<dyn ScmUiFramework>>> = RwLock::new(None);
29abe3a6eaShanjiezhou 
30abe3a6eaShanjiezhou /// 是否启用双缓冲
31abe3a6eaShanjiezhou pub static SCM_DOUBLE_BUFFER_ENABLED: AtomicBool = AtomicBool::new(false);
32abe3a6eaShanjiezhou 
33abe3a6eaShanjiezhou bitflags! {
34abe3a6eaShanjiezhou   pub struct ScmBufferFlag:u8 {
35abe3a6eaShanjiezhou     // 帧缓冲区标志位
36abe3a6eaShanjiezhou        const SCM_BF_FB = 1 << 0; // 当前buffer是设备显存中的帧缓冲区
37abe3a6eaShanjiezhou        const SCM_BF_DB = 1 << 1; // 当前buffer是双缓冲
38abe3a6eaShanjiezhou        const SCM_BF_TEXT = 1 << 2; // 使用文本模式
39abe3a6eaShanjiezhou        const SCM_BF_PIXEL = 1 << 3; // 使用图像模式
40abe3a6eaShanjiezhou    }
41abe3a6eaShanjiezhou }
42abe3a6eaShanjiezhou #[derive(Clone, Debug)]
43abe3a6eaShanjiezhou #[allow(dead_code)]
44abe3a6eaShanjiezhou pub enum ScmFramworkType {
45abe3a6eaShanjiezhou     Text,
46abe3a6eaShanjiezhou     Gui,
47abe3a6eaShanjiezhou     Unused,
48abe3a6eaShanjiezhou }
491496ba7bSLoGin #[derive(Debug, Clone)]
50abe3a6eaShanjiezhou pub enum ScmBuffer {
511496ba7bSLoGin     DeviceBuffer(VirtAddr),
521496ba7bSLoGin     DoubleBuffer(Arc<SpinLock<Box<[u32]>>>),
53abe3a6eaShanjiezhou }
541496ba7bSLoGin 
551496ba7bSLoGin #[derive(Debug, Clone)]
56abe3a6eaShanjiezhou pub struct ScmBufferInfo {
57abe3a6eaShanjiezhou     width: u32,     // 帧缓冲区宽度(pixel或columns)
58abe3a6eaShanjiezhou     height: u32,    // 帧缓冲区高度(pixel或lines)
59abe3a6eaShanjiezhou     size: u32,      // 帧缓冲区大小(bytes)
60abe3a6eaShanjiezhou     bit_depth: u32, // 像素点位深度
61abe3a6eaShanjiezhou     pub buf: ScmBuffer,
62abe3a6eaShanjiezhou     flags: ScmBufferFlag, // 帧缓冲区标志位
63abe3a6eaShanjiezhou }
64abe3a6eaShanjiezhou 
651496ba7bSLoGin #[allow(dead_code)]
66abe3a6eaShanjiezhou impl ScmBufferInfo {
67abe3a6eaShanjiezhou     /// 创建新的帧缓冲区信息
68abe3a6eaShanjiezhou     ///
69abe3a6eaShanjiezhou     /// ## 参数
70abe3a6eaShanjiezhou     ///
71abe3a6eaShanjiezhou     /// - `buf_type` 帧缓冲区类型
72abe3a6eaShanjiezhou     ///
73abe3a6eaShanjiezhou     /// ## 返回值
74abe3a6eaShanjiezhou     ///
75abe3a6eaShanjiezhou     /// - `Result<Self, SystemError>` 创建成功返回新的帧缓冲区结构体,创建失败返回错误码
761496ba7bSLoGin     pub fn new(mut buf_type: ScmBufferFlag) -> Result<Self, SystemError> {
77abe3a6eaShanjiezhou         if unlikely(SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) == false) {
781496ba7bSLoGin             let mut device_buffer = video_refresh_manager().device_buffer().clone();
791496ba7bSLoGin             buf_type.remove(ScmBufferFlag::SCM_BF_DB);
801496ba7bSLoGin             buf_type.insert(ScmBufferFlag::SCM_BF_FB);
811496ba7bSLoGin             device_buffer.flags = buf_type;
821496ba7bSLoGin             return Ok(device_buffer);
83abe3a6eaShanjiezhou         } else {
841496ba7bSLoGin             let device_buffer_guard = video_refresh_manager().device_buffer();
85abe3a6eaShanjiezhou 
861496ba7bSLoGin             let buf_space: Arc<SpinLock<Box<[u32]>>> = Arc::new(SpinLock::new(
871496ba7bSLoGin                 vec![0u32; (device_buffer_guard.size / 4) as usize].into_boxed_slice(),
88abe3a6eaShanjiezhou             ));
89abe3a6eaShanjiezhou 
901496ba7bSLoGin             assert!(buf_type.contains(ScmBufferFlag::SCM_BF_DB));
91abe3a6eaShanjiezhou 
921496ba7bSLoGin             assert_eq!(
931496ba7bSLoGin                 device_buffer_guard.size as usize,
941496ba7bSLoGin                 buf_space.lock().len() * core::mem::size_of::<u32>()
951496ba7bSLoGin             );
96abe3a6eaShanjiezhou 
971496ba7bSLoGin             // 创建双缓冲区
981496ba7bSLoGin             let buffer = Self {
991496ba7bSLoGin                 width: device_buffer_guard.width,
1001496ba7bSLoGin                 height: device_buffer_guard.height,
1011496ba7bSLoGin                 size: device_buffer_guard.size,
1021496ba7bSLoGin                 bit_depth: device_buffer_guard.bit_depth,
1031496ba7bSLoGin                 flags: buf_type,
1041496ba7bSLoGin                 buf: ScmBuffer::DoubleBuffer(buf_space),
105abe3a6eaShanjiezhou             };
1061496ba7bSLoGin             drop(device_buffer_guard);
1071496ba7bSLoGin 
1081496ba7bSLoGin             return Ok(buffer);
109abe3a6eaShanjiezhou         }
110abe3a6eaShanjiezhou     }
1111496ba7bSLoGin 
1121496ba7bSLoGin     pub unsafe fn new_device_buffer(
1131496ba7bSLoGin         width: u32,
1141496ba7bSLoGin         height: u32,
1151496ba7bSLoGin         size: u32,
1161496ba7bSLoGin         bit_depth: u32,
1171496ba7bSLoGin         buf_type: ScmBufferFlag,
1181496ba7bSLoGin         vaddr: VirtAddr,
1191496ba7bSLoGin     ) -> Result<Self, SystemError> {
1201496ba7bSLoGin         let buffer = Self {
1211496ba7bSLoGin             width,
1221496ba7bSLoGin             height,
1231496ba7bSLoGin             size,
1241496ba7bSLoGin             bit_depth,
1251496ba7bSLoGin             flags: buf_type,
1261496ba7bSLoGin             buf: ScmBuffer::DeviceBuffer(vaddr),
1271496ba7bSLoGin         };
1281496ba7bSLoGin         return Ok(buffer);
129abe3a6eaShanjiezhou     }
1301496ba7bSLoGin 
1311496ba7bSLoGin     pub fn buf_size(&self) -> usize {
1321496ba7bSLoGin         self.size as usize
1331496ba7bSLoGin     }
1341496ba7bSLoGin 
1351496ba7bSLoGin     pub fn bit_depth(&self) -> u32 {
1361496ba7bSLoGin         self.bit_depth
1371496ba7bSLoGin     }
1381496ba7bSLoGin 
1391496ba7bSLoGin     pub fn height(&self) -> u32 {
140abe3a6eaShanjiezhou         self.height
141abe3a6eaShanjiezhou     }
1421496ba7bSLoGin 
1431496ba7bSLoGin     pub fn width(&self) -> u32 {
144abe3a6eaShanjiezhou         self.width
145abe3a6eaShanjiezhou     }
1461496ba7bSLoGin 
147abe3a6eaShanjiezhou     pub fn is_double_buffer(&self) -> bool {
148abe3a6eaShanjiezhou         match &self.buf {
149abe3a6eaShanjiezhou             ScmBuffer::DoubleBuffer(_) => true,
150abe3a6eaShanjiezhou             _ => false,
151abe3a6eaShanjiezhou         }
152abe3a6eaShanjiezhou     }
153abe3a6eaShanjiezhou     pub fn is_device_buffer(&self) -> bool {
154abe3a6eaShanjiezhou         match &self.buf {
155abe3a6eaShanjiezhou             ScmBuffer::DeviceBuffer(_) => true,
156abe3a6eaShanjiezhou             _ => false,
157abe3a6eaShanjiezhou         }
158abe3a6eaShanjiezhou     }
1591496ba7bSLoGin 
1601496ba7bSLoGin     pub fn copy_from_nonoverlapping(&mut self, src: &ScmBufferInfo) {
1611496ba7bSLoGin         assert!(self.buf_size() == src.buf_size());
1621496ba7bSLoGin         match &self.buf {
1631496ba7bSLoGin             ScmBuffer::DeviceBuffer(vaddr) => {
1641496ba7bSLoGin                 let len = self.buf_size() / core::mem::size_of::<u32>();
1651496ba7bSLoGin                 let self_buf_guard =
1661496ba7bSLoGin                     unsafe { core::slice::from_raw_parts_mut(vaddr.data() as *mut u32, len) };
1671496ba7bSLoGin                 match &src.buf {
1681496ba7bSLoGin                     ScmBuffer::DeviceBuffer(vaddr) => {
1691496ba7bSLoGin                         let src_buf_guard =
1701496ba7bSLoGin                             unsafe { core::slice::from_raw_parts(vaddr.data() as *const u32, len) };
1711496ba7bSLoGin                         self_buf_guard.copy_from_slice(src_buf_guard);
1721496ba7bSLoGin                     }
1731496ba7bSLoGin                     ScmBuffer::DoubleBuffer(double_buffer) => {
1741496ba7bSLoGin                         let src_buf_guard = double_buffer.lock();
1751496ba7bSLoGin                         self_buf_guard.copy_from_slice(src_buf_guard.as_ref());
1761496ba7bSLoGin                     }
1771496ba7bSLoGin                 };
178abe3a6eaShanjiezhou             }
179abe3a6eaShanjiezhou 
1801496ba7bSLoGin             ScmBuffer::DoubleBuffer(double_buffer) => {
1811496ba7bSLoGin                 let mut double_buffer_guard = double_buffer.lock();
1821496ba7bSLoGin                 match &src.buf {
1831496ba7bSLoGin                     ScmBuffer::DeviceBuffer(vaddr) => {
1841496ba7bSLoGin                         let len = src.buf_size() / core::mem::size_of::<u32>();
1851496ba7bSLoGin                         double_buffer_guard.as_mut().copy_from_slice(unsafe {
1861496ba7bSLoGin                             core::slice::from_raw_parts(vaddr.data() as *const u32, len)
1871496ba7bSLoGin                         });
1881496ba7bSLoGin                     }
1891496ba7bSLoGin                     ScmBuffer::DoubleBuffer(double_buffer) => {
1901496ba7bSLoGin                         let x = double_buffer.lock();
1911496ba7bSLoGin                         double_buffer_guard.as_mut().copy_from_slice(x.as_ref());
1921496ba7bSLoGin                     }
1931496ba7bSLoGin                 };
194abe3a6eaShanjiezhou             }
195abe3a6eaShanjiezhou         }
196abe3a6eaShanjiezhou     }
197abe3a6eaShanjiezhou }
1981496ba7bSLoGin 
199abe3a6eaShanjiezhou #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
200abe3a6eaShanjiezhou pub struct ScmUiFrameworkId(u32);
201abe3a6eaShanjiezhou 
202abe3a6eaShanjiezhou impl ScmUiFrameworkId {
203abe3a6eaShanjiezhou     /// 分配一个新的框架id
204abe3a6eaShanjiezhou     pub fn new() -> Self {
205abe3a6eaShanjiezhou         static MAX_ID: AtomicU32 = AtomicU32::new(0);
206abe3a6eaShanjiezhou         return ScmUiFrameworkId(MAX_ID.fetch_add(1, Ordering::SeqCst));
207abe3a6eaShanjiezhou     }
208abe3a6eaShanjiezhou }
209abe3a6eaShanjiezhou #[allow(dead_code)]
210abe3a6eaShanjiezhou #[derive(Debug, Clone)]
211abe3a6eaShanjiezhou pub struct ScmUiFrameworkMetadata {
212abe3a6eaShanjiezhou     id: ScmUiFrameworkId,
213abe3a6eaShanjiezhou     name: String,
214abe3a6eaShanjiezhou     framework_type: ScmFramworkType,
215abe3a6eaShanjiezhou     pub buf_info: ScmBufferInfo,
216abe3a6eaShanjiezhou }
217abe3a6eaShanjiezhou 
218abe3a6eaShanjiezhou impl ScmUiFrameworkMetadata {
219abe3a6eaShanjiezhou     pub fn new(name: String, framework_type: ScmFramworkType) -> Self {
220abe3a6eaShanjiezhou         match framework_type {
221abe3a6eaShanjiezhou             ScmFramworkType::Text => {
222abe3a6eaShanjiezhou                 let result = ScmUiFrameworkMetadata {
223abe3a6eaShanjiezhou                     id: ScmUiFrameworkId::new(),
224abe3a6eaShanjiezhou                     name,
225abe3a6eaShanjiezhou                     framework_type: ScmFramworkType::Text,
2261496ba7bSLoGin                     buf_info: ScmBufferInfo::new(
2271496ba7bSLoGin                         ScmBufferFlag::SCM_BF_TEXT | ScmBufferFlag::SCM_BF_DB,
2281496ba7bSLoGin                     )
2291496ba7bSLoGin                     .unwrap(),
230abe3a6eaShanjiezhou                 };
231abe3a6eaShanjiezhou 
232abe3a6eaShanjiezhou                 return result;
233abe3a6eaShanjiezhou             }
234abe3a6eaShanjiezhou             ScmFramworkType::Gui => todo!(),
235abe3a6eaShanjiezhou             ScmFramworkType::Unused => todo!(),
236abe3a6eaShanjiezhou         }
237abe3a6eaShanjiezhou     }
238abe3a6eaShanjiezhou     pub fn buf_info(&self) -> ScmBufferInfo {
239abe3a6eaShanjiezhou         return self.buf_info.clone();
240abe3a6eaShanjiezhou     }
241abe3a6eaShanjiezhou     pub fn set_buf_info(&mut self, buf_info: ScmBufferInfo) {
242abe3a6eaShanjiezhou         self.buf_info = buf_info;
243abe3a6eaShanjiezhou     }
244abe3a6eaShanjiezhou }
245abe3a6eaShanjiezhou pub trait ScmUiFramework: Sync + Send + Debug {
246abe3a6eaShanjiezhou     // 安装ui框架的回调函数
247abe3a6eaShanjiezhou     fn install(&self) -> Result<i32, SystemError> {
248abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
249abe3a6eaShanjiezhou     }
250abe3a6eaShanjiezhou     // 卸载ui框架的回调函数
251abe3a6eaShanjiezhou     fn uninstall(&self) -> Result<i32, SystemError> {
252abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
253abe3a6eaShanjiezhou     }
254abe3a6eaShanjiezhou     // 启用ui框架的回调函数
255abe3a6eaShanjiezhou     fn enable(&self) -> Result<i32, SystemError> {
256abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
257abe3a6eaShanjiezhou     }
258abe3a6eaShanjiezhou     // 禁用ui框架的回调函数
259abe3a6eaShanjiezhou     fn disable(&self) -> Result<i32, SystemError> {
260abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
261abe3a6eaShanjiezhou     }
262abe3a6eaShanjiezhou     // 改变ui框架的帧缓冲区的回调函数
263abe3a6eaShanjiezhou     fn change(&self, _buf: ScmBufferInfo) -> Result<i32, SystemError> {
264abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
265abe3a6eaShanjiezhou     }
266abe3a6eaShanjiezhou     /// @brief 获取ScmUiFramework的元数据
267abe3a6eaShanjiezhou     /// @return 成功:Ok(ScmUiFramework的元数据)
268abe3a6eaShanjiezhou     ///         失败:Err(错误码)
269abe3a6eaShanjiezhou     fn metadata(&self) -> Result<ScmUiFrameworkMetadata, SystemError> {
270abe3a6eaShanjiezhou         // 若文件系统没有实现此方法,则返回“不支持”
271abe3a6eaShanjiezhou         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
272abe3a6eaShanjiezhou     }
273abe3a6eaShanjiezhou }
274abe3a6eaShanjiezhou 
275abe3a6eaShanjiezhou /// 初始化屏幕控制模块
276abe3a6eaShanjiezhou ///
277abe3a6eaShanjiezhou /// ## 调用时机
278abe3a6eaShanjiezhou ///
279abe3a6eaShanjiezhou /// 该函数在内核启动的早期进行调用。调用时,内存管理模块尚未初始化。
280*1a72a751SLoGin pub fn scm_init(enable_put_to_window: bool) {
281abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(false, Ordering::SeqCst); // 禁用双缓冲
282*1a72a751SLoGin     if enable_put_to_window {
283*1a72a751SLoGin         textui_enable_put_to_window();
284*1a72a751SLoGin     } else {
285*1a72a751SLoGin         textui_disable_put_to_window();
286*1a72a751SLoGin     }
287*1a72a751SLoGin     textui_init_no_alloc(enable_put_to_window);
288abe3a6eaShanjiezhou 
289a03c4f9dSLoGin     send_to_default_serial8250_port("\nfinish_scm_init\n\0".as_bytes());
290abe3a6eaShanjiezhou }
291abe3a6eaShanjiezhou 
292abe3a6eaShanjiezhou /// 启用某个ui框架,将它的帧缓冲区渲染到屏幕上
293abe3a6eaShanjiezhou /// ## 参数
294abe3a6eaShanjiezhou ///
295abe3a6eaShanjiezhou /// - framework 要启动的ui框架
296abe3a6eaShanjiezhou 
297abe3a6eaShanjiezhou pub fn scm_framework_enable(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
298abe3a6eaShanjiezhou     // 获取信息
299abe3a6eaShanjiezhou     let metadata = framework.metadata()?;
300abe3a6eaShanjiezhou 
301abe3a6eaShanjiezhou     // if metadata.buf_info.buf.is_null() {
302abe3a6eaShanjiezhou     //     return Err(SystemError::EINVAL);
303abe3a6eaShanjiezhou     // }
304abe3a6eaShanjiezhou     let mut current_framework = CURRENT_FRAMEWORK.write();
305abe3a6eaShanjiezhou 
306abe3a6eaShanjiezhou     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) == true {
3071496ba7bSLoGin         video_refresh_manager().set_refresh_target(&metadata.buf_info)?;
308abe3a6eaShanjiezhou     }
309abe3a6eaShanjiezhou 
3101496ba7bSLoGin     framework.enable()?;
311abe3a6eaShanjiezhou     current_framework.replace(framework);
312abe3a6eaShanjiezhou 
313abe3a6eaShanjiezhou     return Ok(0);
314abe3a6eaShanjiezhou }
315abe3a6eaShanjiezhou /// 向屏幕管理器注册UI框架
316abe3a6eaShanjiezhou ///
317abe3a6eaShanjiezhou /// ## 参数
318abe3a6eaShanjiezhou /// - framework 框架结构体
319abe3a6eaShanjiezhou 
320abe3a6eaShanjiezhou pub fn scm_register(framework: Arc<dyn ScmUiFramework>) -> Result<i32, SystemError> {
321abe3a6eaShanjiezhou     // 把ui框架加入链表
322abe3a6eaShanjiezhou 
323abe3a6eaShanjiezhou     SCM_FRAMEWORK_LIST.lock().push_back(framework.clone());
324abe3a6eaShanjiezhou     // 调用ui框架的回调函数以安装ui框架,并将其激活
325abe3a6eaShanjiezhou     framework.install()?;
326abe3a6eaShanjiezhou 
327abe3a6eaShanjiezhou     // 如果当前还没有框架获得了屏幕的控制权,就让其拿去
328abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
329abe3a6eaShanjiezhou         return scm_framework_enable(framework);
330abe3a6eaShanjiezhou     }
331abe3a6eaShanjiezhou     return Ok(0);
332abe3a6eaShanjiezhou }
333abe3a6eaShanjiezhou 
3341496ba7bSLoGin /// 屏幕管理器启用双缓冲区
3351496ba7bSLoGin #[allow(dead_code)]
3361496ba7bSLoGin pub fn scm_enable_double_buffer() -> Result<i32, SystemError> {
337abe3a6eaShanjiezhou     if SCM_DOUBLE_BUFFER_ENABLED.load(Ordering::SeqCst) {
338abe3a6eaShanjiezhou         // 已经开启了双缓冲区了, 直接退出
339abe3a6eaShanjiezhou         return Ok(0);
340abe3a6eaShanjiezhou     }
341abe3a6eaShanjiezhou     let scm_list = SCM_FRAMEWORK_LIST.lock();
342abe3a6eaShanjiezhou     if scm_list.is_empty() {
343abe3a6eaShanjiezhou         // scm 框架链表为空
344abe3a6eaShanjiezhou         return Ok(0);
345abe3a6eaShanjiezhou     }
346abe3a6eaShanjiezhou     drop(scm_list);
347abe3a6eaShanjiezhou     SCM_DOUBLE_BUFFER_ENABLED.store(true, Ordering::SeqCst);
348abe3a6eaShanjiezhou     // 创建双缓冲区
3491496ba7bSLoGin     let buf_info = ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
3501496ba7bSLoGin 
3511496ba7bSLoGin     // 设置定时刷新的对象
3521496ba7bSLoGin     video_refresh_manager()
3531496ba7bSLoGin         .set_refresh_target(&buf_info)
3541496ba7bSLoGin         .expect("set refresh target failed");
3551496ba7bSLoGin 
3561496ba7bSLoGin     // 设置当前框架的帧缓冲区
357abe3a6eaShanjiezhou     CURRENT_FRAMEWORK
358abe3a6eaShanjiezhou         .write()
359abe3a6eaShanjiezhou         .as_ref()
360abe3a6eaShanjiezhou         .unwrap()
361abe3a6eaShanjiezhou         .change(buf_info)?;
362abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新为双缓冲区
363abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
364abe3a6eaShanjiezhou         if !(*framework).metadata()?.buf_info.is_double_buffer() {
365abe3a6eaShanjiezhou             let new_buf_info =
366abe3a6eaShanjiezhou                 ScmBufferInfo::new(ScmBufferFlag::SCM_BF_DB | ScmBufferFlag::SCM_BF_PIXEL)?;
367abe3a6eaShanjiezhou             (*framework).change(new_buf_info)?;
368abe3a6eaShanjiezhou         }
369abe3a6eaShanjiezhou     }
370abe3a6eaShanjiezhou     // 通知显示驱动,启动双缓冲
3711496ba7bSLoGin     video_refresh_manager().video_reinitialize(true)?;
372abe3a6eaShanjiezhou 
373abe3a6eaShanjiezhou     return Ok(0);
374abe3a6eaShanjiezhou }
3751496ba7bSLoGin 
376abe3a6eaShanjiezhou /// 允许往窗口打印信息
377abe3a6eaShanjiezhou pub fn scm_enable_put_to_window() {
3781496ba7bSLoGin     // mm之前要继续往窗口打印信息时,因为没有动态内存分配(textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
379abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
380*1a72a751SLoGin         textui_enable_put_to_window();
381abe3a6eaShanjiezhou     } else {
382abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
383abe3a6eaShanjiezhou             .write()
384abe3a6eaShanjiezhou             .as_ref()
385abe3a6eaShanjiezhou             .unwrap()
386abe3a6eaShanjiezhou             .enable()
387abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
388abe3a6eaShanjiezhou         if r.is_negative() {
389a03c4f9dSLoGin             send_to_default_serial8250_port("scm_enable_put_to_window() failed.\n\0".as_bytes());
390abe3a6eaShanjiezhou         }
391abe3a6eaShanjiezhou     }
392abe3a6eaShanjiezhou }
393abe3a6eaShanjiezhou /// 禁止往窗口打印信息
394abe3a6eaShanjiezhou pub fn scm_disable_put_to_window() {
395abe3a6eaShanjiezhou     // mm之前要停止往窗口打印信息时,因为没有动态内存分配(rwlock与otion依然能用,但是textui并没有往scm注册),且使用的是textui,要直接修改textui里面的值
396abe3a6eaShanjiezhou     if CURRENT_FRAMEWORK.read().is_none() {
397*1a72a751SLoGin         textui_disable_put_to_window();
398*1a72a751SLoGin         assert!(textui_is_enable_put_to_window() == false);
399abe3a6eaShanjiezhou     } else {
400abe3a6eaShanjiezhou         let r = CURRENT_FRAMEWORK
401abe3a6eaShanjiezhou             .write()
402abe3a6eaShanjiezhou             .as_ref()
403abe3a6eaShanjiezhou             .unwrap()
404abe3a6eaShanjiezhou             .disable()
405abe3a6eaShanjiezhou             .unwrap_or_else(|e| e.to_posix_errno());
406abe3a6eaShanjiezhou         if r.is_negative() {
407a03c4f9dSLoGin             send_to_default_serial8250_port("scm_disable_put_to_window() failed.\n\0".as_bytes());
408abe3a6eaShanjiezhou         }
409abe3a6eaShanjiezhou     }
410abe3a6eaShanjiezhou }
411abe3a6eaShanjiezhou /// 当内存管理单元被初始化之后,重新处理帧缓冲区问题
412abe3a6eaShanjiezhou #[no_mangle]
413abe3a6eaShanjiezhou pub extern "C" fn scm_reinit() -> i32 {
414abe3a6eaShanjiezhou     let r = true_scm_reinit().unwrap_or_else(|e| e.to_posix_errno());
415abe3a6eaShanjiezhou     if r.is_negative() {
416a03c4f9dSLoGin         send_to_default_serial8250_port("scm reinit failed.\n\0".as_bytes());
417abe3a6eaShanjiezhou     }
418abe3a6eaShanjiezhou     return r;
419abe3a6eaShanjiezhou }
420abe3a6eaShanjiezhou fn true_scm_reinit() -> Result<i32, SystemError> {
4211496ba7bSLoGin     video_refresh_manager()
4221496ba7bSLoGin         .video_reinitialize(false)
4231496ba7bSLoGin         .expect("video reinitialize failed");
424abe3a6eaShanjiezhou 
425abe3a6eaShanjiezhou     // 遍历当前所有使用帧缓冲区的框架,更新地址
4261496ba7bSLoGin     let device_buffer = video_refresh_manager().device_buffer().clone();
427abe3a6eaShanjiezhou     for framework in SCM_FRAMEWORK_LIST.lock().iter_mut() {
428abe3a6eaShanjiezhou         if framework.metadata()?.buf_info().is_device_buffer() {
4291496ba7bSLoGin             framework.change(device_buffer.clone())?;
430abe3a6eaShanjiezhou         }
431abe3a6eaShanjiezhou     }
432abe3a6eaShanjiezhou 
433abe3a6eaShanjiezhou     scm_enable_put_to_window();
434abe3a6eaShanjiezhou 
435abe3a6eaShanjiezhou     return Ok(0);
436abe3a6eaShanjiezhou }
437