mod.rs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. use alloc::{string::String, sync::Arc, vec::Vec};
  2. use render_helper::{FrameP, FramePointerStatus};
  3. use system_error::SystemError;
  4. use crate::{
  5. driver::{base::device::Device, tty::virtual_terminal::Color},
  6. init::boot_params,
  7. libs::rwlock::RwLock,
  8. mm::{ucontext::LockedVMA, PhysAddr, VirtAddr},
  9. };
  10. use self::{
  11. fbmem::{FbDevice, FrameBufferManager},
  12. render_helper::{BitIter, EndianPattern},
  13. };
  14. const COLOR_TABLE_8: &[u32] = &[
  15. 0x00000000, 0xff000000, 0x00ff0000, 0xffff0000, 0x0000ff00, 0xff00ff00, 0x00ffff00, 0xffffff00,
  16. 0x000000ff, 0xff0000ff, 0x00ff00ff, 0xffff00ff, 0x0000ffff, 0xff00ffff, 0x00ffffff, 0xffffffff,
  17. ];
  18. const COLOR_TABLE_16: &[u32] = &[0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff];
  19. const COLOR_TABLE_32: &[u32] = &[0x00000000, 0xffffffff];
  20. pub mod fbcon;
  21. pub mod fbmem;
  22. pub mod fbsysfs;
  23. pub mod modedb;
  24. pub mod render_helper;
  25. // 帧缓冲区id
  26. int_like!(FbId, u32);
  27. lazy_static! {
  28. pub static ref FRAME_BUFFER_SET: RwLock<Vec<Option<Arc<dyn FrameBuffer>>>> = {
  29. let mut ret = Vec::new();
  30. ret.resize(FrameBufferManager::FB_MAX, None);
  31. RwLock::new(ret)
  32. };
  33. }
  34. impl FbId {
  35. /// 帧缓冲区id的初始值(无效值)
  36. pub const INIT: Self = Self::new(u32::MAX);
  37. /// 判断是否为无效的帧缓冲区id
  38. #[allow(dead_code)]
  39. pub const fn is_valid(&self) -> bool {
  40. if self.0 == Self::INIT.0 || self.0 >= FrameBufferManager::FB_MAX as u32 {
  41. return false;
  42. }
  43. return true;
  44. }
  45. }
  46. /// 帧缓冲区应该实现的接口
  47. pub trait FrameBuffer: FrameBufferInfo + FrameBufferOps + Device {
  48. /// 获取帧缓冲区的id
  49. fn fb_id(&self) -> FbId;
  50. /// 设置帧缓冲区的id
  51. fn set_fb_id(&self, id: FbId);
  52. /// 通用的软件图像绘画
  53. fn generic_imageblit(&self, image: &FbImage) {
  54. let boot_param = boot_params().read();
  55. let x = image.x;
  56. let y = image.y;
  57. let byte_per_pixel = core::mem::size_of::<u32>() as u32;
  58. let bit_per_pixel = self.current_fb_var().bits_per_pixel;
  59. // 计算图像在帧缓冲中的起始位
  60. let bitstart = (y * self.current_fb_fix().line_length * 8) + (x * bit_per_pixel);
  61. let start_index = bitstart & (32 - 1);
  62. let pitch_index = (self.current_fb_fix().line_length & (byte_per_pixel - 1)) * 8;
  63. let dst2 = boot_param.screen_info.lfb_virt_base;
  64. if dst2.is_none() {
  65. return;
  66. }
  67. let mut safe_pointer = FrameP::new(
  68. self.current_fb_var().yres as usize,
  69. self.current_fb_var().xres as usize,
  70. self.current_fb_var().bits_per_pixel as usize,
  71. dst2.unwrap(),
  72. image,
  73. );
  74. let _ = self.fb_sync();
  75. if image.depth == 1 {
  76. let fg;
  77. let bg;
  78. if self.current_fb_fix().visual == FbVisual::TrueColor
  79. || self.current_fb_fix().visual == FbVisual::DirectColor
  80. {
  81. let fb_info_data = self.framebuffer_info_data().read();
  82. fg = fb_info_data.pesudo_palette[image.fg as usize];
  83. bg = fb_info_data.pesudo_palette[image.bg as usize];
  84. } else {
  85. fg = image.fg;
  86. bg = image.bg;
  87. }
  88. if 32 % bit_per_pixel == 0
  89. && start_index == 0
  90. && pitch_index == 0
  91. && image.width & (32 / bit_per_pixel - 1) == 0
  92. && (8..=32).contains(&bit_per_pixel)
  93. {
  94. unsafe { self.fast_imageblit(image, &mut safe_pointer, fg, bg) }
  95. } else {
  96. self.slow_imageblit(image, &mut safe_pointer, fg, bg)
  97. }
  98. } else {
  99. todo!("color image blit todo");
  100. }
  101. }
  102. /// 优化的单色图像绘制函数
  103. ///
  104. /// 仅当 bits_per_pixel 为 8、16 或 32 时才能使用。
  105. /// 要求 image->width 可以被像素或 dword (ppw) 整除。
  106. /// 要求 fix->line_length 可以被 4 整除。
  107. /// 扫描线的开始和结束都是 dword 对齐的。
  108. unsafe fn fast_imageblit(&self, image: &FbImage, dst1: &mut FrameP, fg: u32, bg: u32) {
  109. let bpp = self.current_fb_var().bits_per_pixel;
  110. let mut fgx = fg;
  111. let mut bgx = bg;
  112. let ppw = 32 / bpp;
  113. let spitch = image.width.div_ceil(8);
  114. let mut color_tab: [u32; 16] = [0; 16];
  115. let tab: &[u32] = match bpp {
  116. 8 => COLOR_TABLE_8,
  117. 16 => COLOR_TABLE_16,
  118. 32 => COLOR_TABLE_32,
  119. _ => {
  120. return;
  121. }
  122. };
  123. for _ in (0..(ppw - 1)).rev() {
  124. fgx <<= bpp;
  125. bgx <<= bpp;
  126. fgx |= fg;
  127. bgx |= bg;
  128. }
  129. let bitmask = (1 << ppw) - 1;
  130. let eorx = fgx ^ bgx;
  131. let k = image.width / ppw;
  132. for (idx, val) in tab.iter().enumerate() {
  133. color_tab[idx] = (*val & eorx) ^ bgx;
  134. }
  135. let mut shift;
  136. let mut src;
  137. let mut offset = 0;
  138. let mut j = 0;
  139. let mut count = 0;
  140. for _ in (0..image.height).rev() {
  141. shift = 8;
  142. src = offset;
  143. match ppw {
  144. 4 => {
  145. // 8bpp
  146. j = k;
  147. while j >= 2 {
  148. dst1.write(color_tab[(image.data[src] as usize >> 4) & bitmask]);
  149. dst1.write(color_tab[(image.data[src] as usize) & bitmask]);
  150. j -= 2;
  151. src += 1;
  152. }
  153. }
  154. 2 => {
  155. // 16bpp
  156. j = k;
  157. while j >= 4 {
  158. dst1.write(color_tab[(image.data[src] as usize >> 6) & bitmask]);
  159. dst1.write(color_tab[(image.data[src] as usize >> 4) & bitmask]);
  160. dst1.write(color_tab[(image.data[src] as usize >> 2) & bitmask]);
  161. dst1.write(color_tab[(image.data[src] as usize) & bitmask]);
  162. src += 1;
  163. j -= 4;
  164. }
  165. }
  166. 1 => {
  167. // 32 bpp
  168. j = k;
  169. while j >= 8 {
  170. dst1.write(color_tab[(image.data[src] as usize >> 7) & bitmask]);
  171. dst1.write(color_tab[(image.data[src] as usize >> 6) & bitmask]);
  172. dst1.write(color_tab[(image.data[src] as usize >> 5) & bitmask]);
  173. dst1.write(color_tab[(image.data[src] as usize >> 4) & bitmask]);
  174. dst1.write(color_tab[(image.data[src] as usize >> 3) & bitmask]);
  175. dst1.write(color_tab[(image.data[src] as usize >> 2) & bitmask]);
  176. dst1.write(color_tab[(image.data[src] as usize >> 1) & bitmask]);
  177. dst1.write(color_tab[(image.data[src] as usize) & bitmask]);
  178. src += 1;
  179. j -= 8;
  180. }
  181. }
  182. _ => {}
  183. }
  184. /*
  185. * For image widths that are not a multiple of 8, there
  186. * are trailing pixels left on the current line. Print
  187. * them as well.
  188. */
  189. while j != 0 {
  190. shift -= ppw;
  191. dst1.write(color_tab[(image.data[src] as usize >> shift) & bitmask]);
  192. if shift == 0 {
  193. shift = 8;
  194. src += 1;
  195. }
  196. j -= 1;
  197. }
  198. count += 1;
  199. dst1.move_with_offset(self.current_fb_fix().line_length * count);
  200. offset += spitch as usize;
  201. }
  202. }
  203. fn slow_imageblit(&self, _image: &FbImage, safe_dst: &mut FrameP, _fg: u32, _bg: u32) {
  204. let mut count = 0;
  205. let mut pt_status = FramePointerStatus::Normal;
  206. let iter = BitIter::new(
  207. _fg,
  208. _bg,
  209. EndianPattern::Big,
  210. EndianPattern::Little,
  211. self.current_fb_var().bits_per_pixel / 8,
  212. _image.data.iter(),
  213. _image.width,
  214. );
  215. for (content, full) in iter {
  216. match pt_status {
  217. FramePointerStatus::OutOfBuffer => {
  218. return;
  219. }
  220. FramePointerStatus::OutOfScreen => {}
  221. FramePointerStatus::Normal => {
  222. pt_status = safe_dst.write(content);
  223. }
  224. }
  225. if full {
  226. count += 1;
  227. safe_dst.move_with_offset(self.current_fb_fix().line_length * count);
  228. pt_status = FramePointerStatus::Normal;
  229. }
  230. }
  231. }
  232. }
  233. #[derive(Debug, Default)]
  234. pub struct FrameBufferInfoData {
  235. /// 颜色映射
  236. pub color_map: Vec<Color>,
  237. /// 颜色映射表
  238. pub pesudo_palette: Vec<u32>,
  239. }
  240. impl FrameBufferInfoData {
  241. pub fn new() -> Self {
  242. Self {
  243. ..Default::default()
  244. }
  245. }
  246. }
  247. /// 帧缓冲区信息
  248. #[allow(dead_code)]
  249. pub trait FrameBufferInfo: FrameBufferOps {
  250. fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData>;
  251. /// Amount of ioremapped VRAM or 0
  252. fn screen_size(&self) -> usize;
  253. /// 获取当前的可变帧缓冲信息
  254. fn current_fb_var(&self) -> FbVarScreenInfo;
  255. /// 获取当前的固定帧缓冲信息
  256. fn current_fb_fix(&self) -> FixedScreenInfo;
  257. /// 获取当前的视频模式
  258. fn video_mode(&self) -> Option<&FbVideoMode>;
  259. /// 获取当前帧缓冲区对应的`/sys/class/graphics/fb0`或者`/sys/class/graphics/fb1`等的设备结构体
  260. fn fb_device(&self) -> Option<Arc<FbDevice>>;
  261. /// 设置当前帧缓冲区对应的`/sys/class/graphics/fb0`或者`/sys/class/graphics/fb1`等的设备结构体
  262. fn set_fb_device(&self, device: Option<Arc<FbDevice>>);
  263. /// 获取帧缓冲区的状态
  264. fn state(&self) -> FbState;
  265. /// 颜色位深
  266. fn color_depth(&self) -> u32 {
  267. return 8;
  268. // 以下逻辑没问题,但是当前没有初始化好var,所以先直接返回当前vasafb的8
  269. // let var = self.current_fb_var();
  270. // let fix = self.current_fb_fix();
  271. // if fix.visual == FbVisual::Mono01 || fix.visual == FbVisual::Mono10 {
  272. // return 1;
  273. // } else {
  274. // if var.green.length == var.blue.length
  275. // && var.green.length == var.red.length
  276. // && var.green.offset == var.blue.offset
  277. // && var.green.offset == var.red.offset
  278. // {
  279. // error!("return {}", var.green.length);
  280. // return var.green.length;
  281. // } else {
  282. // return var.green.length + var.blue.length + var.red.length;
  283. // }
  284. // }
  285. }
  286. /// ## 设置调色板
  287. fn set_color_map(&self, cmap: Vec<Color>) -> Result<(), SystemError> {
  288. let ret = self.fb_set_color_map(cmap.clone());
  289. if ret.is_err() && ret.clone().unwrap_err() == SystemError::ENOSYS {
  290. for (idx, color) in cmap.iter().enumerate() {
  291. if self
  292. .fb_set_color_register(idx as u16, color.red, color.green, color.blue)
  293. .is_err()
  294. {
  295. break;
  296. }
  297. }
  298. self.framebuffer_info_data().write().color_map = cmap;
  299. } else {
  300. if ret.is_ok() {
  301. self.framebuffer_info_data().write().color_map = cmap;
  302. }
  303. return ret;
  304. }
  305. Ok(())
  306. }
  307. }
  308. /// 帧缓冲区操作
  309. ///
  310. /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/fb.h#237
  311. #[allow(dead_code)]
  312. pub trait FrameBufferOps {
  313. fn fb_open(&self, user: bool);
  314. fn fb_release(&self, user: bool);
  315. /// 读取帧缓冲区的内容。
  316. ///
  317. /// 对于具有奇特非线性布局的帧缓冲区或正常内存映射访问无法工作的帧缓冲区,可以使用此方法。
  318. fn fb_read(&self, _buf: &mut [u8], _pos: usize) -> Result<usize, SystemError> {
  319. Err(SystemError::ENOSYS)
  320. }
  321. /// 将帧缓冲区的内容写入。
  322. ///
  323. /// 对于具有奇特非线性布局的帧缓冲区或正常内存映射访问无法工作的帧缓冲区,可以使用此方法。
  324. fn fb_write(&self, _buf: &[u8], _pos: usize) -> Result<usize, SystemError> {
  325. Err(SystemError::ENOSYS)
  326. }
  327. /// 设置帧缓冲区的颜色寄存器。
  328. ///
  329. /// 颜色寄存器的数量和含义取决于帧缓冲区的硬件。
  330. ///
  331. /// ## 参数
  332. ///
  333. /// - `regno`:寄存器编号。
  334. /// - `red`:红色分量。
  335. /// - `green`:绿色分量。
  336. /// - `blue`:蓝色分量。
  337. fn fb_set_color_register(
  338. &self,
  339. regno: u16,
  340. red: u16,
  341. green: u16,
  342. blue: u16,
  343. ) -> Result<(), SystemError>;
  344. /// 设置帧缓冲区的黑屏模式
  345. fn fb_blank(&self, blank_mode: BlankMode) -> Result<(), SystemError>;
  346. /// 在帧缓冲区中绘制一个矩形。
  347. fn fb_fillrect(&self, _data: FillRectData) -> Result<(), SystemError> {
  348. Err(SystemError::ENOSYS)
  349. }
  350. /// 将数据从一处复制到另一处。
  351. fn fb_copyarea(&self, _data: CopyAreaData);
  352. /// 将帧缓冲区的内容映射到用户空间。
  353. fn fb_mmap(&self, _vma: &Arc<LockedVMA>) -> Result<(), SystemError> {
  354. Err(SystemError::ENOSYS)
  355. }
  356. /// 卸载与该帧缓冲区相关的所有资源
  357. fn fb_destroy(&self);
  358. /// 画光标
  359. fn fb_cursor(&self, _cursor: &FbCursor) -> Result<(), SystemError> {
  360. return Err(SystemError::ENOSYS);
  361. }
  362. /// 画软光标(暂时简要实现)
  363. fn soft_cursor(&self, cursor: FbCursor) -> Result<(), SystemError> {
  364. let mut image = cursor.image.clone();
  365. if cursor.enable {
  366. match cursor.rop {
  367. true => {
  368. for i in 0..image.data.len() {
  369. image.data[i] ^= cursor.mask[i];
  370. }
  371. }
  372. false => {
  373. for i in 0..image.data.len() {
  374. image.data[i] &= cursor.mask[i];
  375. }
  376. }
  377. }
  378. }
  379. self.fb_image_blit(&image);
  380. Ok(())
  381. }
  382. fn fb_sync(&self) -> Result<(), SystemError> {
  383. return Err(SystemError::ENOSYS);
  384. }
  385. /// 绘画位图
  386. fn fb_image_blit(&self, image: &FbImage);
  387. fn fb_set_color_map(&self, _cmap: Vec<Color>) -> Result<(), SystemError> {
  388. return Err(SystemError::ENOSYS);
  389. }
  390. }
  391. /// 帧缓冲区的状态
  392. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  393. pub enum FbState {
  394. Running = 0,
  395. Suspended = 1,
  396. }
  397. /// 屏幕黑屏模式。
  398. #[allow(dead_code)]
  399. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  400. pub enum BlankMode {
  401. /// 取消屏幕黑屏, 垂直同步和水平同步均打开
  402. Unblank,
  403. /// 屏幕黑屏, 垂直同步和水平同步均打开
  404. Normal,
  405. /// 屏幕黑屏, 水平同步打开, 垂直同步关闭
  406. HSync,
  407. /// 屏幕黑屏, 水平同步关闭, 垂直同步打开
  408. VSync,
  409. /// 屏幕黑屏, 水平同步和垂直同步均关闭
  410. Powerdown,
  411. }
  412. /// `FillRectData` 结构体用于表示一个矩形区域并填充特定颜色。
  413. ///
  414. /// # 结构体字段
  415. /// * `dx`:
  416. /// * `dy`:
  417. /// * `width`:
  418. /// * `height`: 矩形的高度
  419. /// * `color`: 用于填充矩形的颜色,是一个32位无符号整数
  420. /// * `rop`: 光栅操作(Raster Operation),用于定义如何将颜色应用到矩形区域
  421. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  422. pub struct FillRectData {
  423. /// 矩形左上角的x坐标(相对于屏幕)
  424. pub dx: u32,
  425. /// 矩形左上角的y坐标(相对于屏幕)
  426. pub dy: u32,
  427. /// 矩形的宽度
  428. pub width: u32,
  429. /// 矩形的高度
  430. pub height: u32,
  431. /// 用于填充矩形的颜色,是一个32位无符号整数
  432. pub color: u32,
  433. /// 光栅操作(Raster Operation),用于定义如何将颜色应用到矩形区域
  434. pub rop: FillRectROP,
  435. }
  436. impl FillRectData {
  437. #[allow(dead_code)]
  438. pub fn new(dx: u32, dy: u32, width: u32, height: u32, color: u32, rop: FillRectROP) -> Self {
  439. Self {
  440. dx,
  441. dy,
  442. width,
  443. height,
  444. color,
  445. rop,
  446. }
  447. }
  448. }
  449. /// 光栅操作(Raster Operation),用于定义如何将颜色应用到矩形区域
  450. #[allow(dead_code)]
  451. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  452. pub enum FillRectROP {
  453. /// 复制操作,即直接将指定颜色应用到矩形区域,覆盖原有颜色。
  454. Copy,
  455. /// 异或操作,即将指定颜色与矩形区域原有颜色进行异或操作,结果颜色应用到矩形区域。
  456. Xor,
  457. }
  458. /// `CopyAreaData` 结构体用于表示一个矩形区域,并指定从哪个源位置复制数据。
  459. ///
  460. /// 注意,源位置必须是有意义的(即包围的矩形都必须得在屏幕内)
  461. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  462. pub struct CopyAreaData {
  463. /// 目标矩形左上角的x坐标
  464. pub dx: i32,
  465. /// 目标矩形左上角的y坐标
  466. pub dy: i32,
  467. /// 矩形的宽度
  468. pub width: u32,
  469. /// 矩形的高度
  470. pub height: u32,
  471. /// 源矩形左上角的x坐标
  472. pub sx: i32,
  473. /// 源矩形左上角的y坐标
  474. pub sy: i32,
  475. }
  476. impl CopyAreaData {
  477. #[allow(dead_code)]
  478. pub fn new(dx: i32, dy: i32, width: u32, height: u32, sx: i32, sy: i32) -> Self {
  479. Self {
  480. dx,
  481. dy,
  482. width,
  483. height,
  484. sx,
  485. sy,
  486. }
  487. }
  488. }
  489. /// `FbVarScreenInfo` 结构体用于描述屏幕的各种属性。
  490. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  491. pub struct FbVarScreenInfo {
  492. /// 可见分辨率的宽度
  493. pub xres: u32,
  494. /// 可见分辨率的高度
  495. pub yres: u32,
  496. /// 虚拟分辨率的宽度
  497. pub xres_virtual: u32,
  498. /// 虚拟分辨率的高度
  499. pub yres_virtual: u32,
  500. /// 从虚拟到可见分辨率的偏移量(宽度方向)
  501. pub xoffset: u32,
  502. /// 从虚拟到可见分辨率的偏移量(高度方向)
  503. pub yoffset: u32,
  504. /// 每像素的位数
  505. pub bits_per_pixel: u32,
  506. /// 颜色模式
  507. pub color_mode: FbColorMode,
  508. /// 红色位域
  509. pub red: FbBitfield,
  510. /// 绿色位域
  511. pub green: FbBitfield,
  512. /// 蓝色位域
  513. pub blue: FbBitfield,
  514. /// 透明度位域
  515. pub transp: FbBitfield,
  516. /// 像素格式
  517. pub pixel_format: FbPixelFormat,
  518. /// 激活标志(参见FB_ACTIVATE_*)
  519. pub activate: FbActivateFlags,
  520. /// 帧缓冲区的高度(像素) None表示未知
  521. pub height: Option<u32>,
  522. /// 帧缓冲区的宽度(像素) None表示未知
  523. pub width: Option<u32>,
  524. /// 像素时钟(皮秒)
  525. pub pixclock: u32,
  526. /// 左边距
  527. pub left_margin: u32,
  528. /// 右边距
  529. pub right_margin: u32,
  530. /// 上边距
  531. pub upper_margin: u32,
  532. /// 下边距
  533. pub lower_margin: u32,
  534. /// 水平同步的长度
  535. pub hsync_len: u32,
  536. /// 垂直同步的长度
  537. pub vsync_len: u32,
  538. /// 同步标志(参见FB_SYNC_*)
  539. pub sync: FbSyncFlags,
  540. /// 视频模式(参见FB_VMODE_*)
  541. pub vmode: FbVModeFlags,
  542. /// 逆时针旋转的角度
  543. pub rotate_angle: u32,
  544. /// 颜色空间
  545. pub colorspace: V4l2Colorspace,
  546. }
  547. impl Default for FbVarScreenInfo {
  548. fn default() -> Self {
  549. Self {
  550. xres: Default::default(),
  551. yres: Default::default(),
  552. xres_virtual: Default::default(),
  553. yres_virtual: Default::default(),
  554. xoffset: Default::default(),
  555. yoffset: Default::default(),
  556. bits_per_pixel: Default::default(),
  557. color_mode: Default::default(),
  558. red: Default::default(),
  559. green: Default::default(),
  560. blue: Default::default(),
  561. transp: Default::default(),
  562. pixel_format: Default::default(),
  563. activate: Default::default(),
  564. height: None,
  565. width: None,
  566. pixclock: Default::default(),
  567. left_margin: Default::default(),
  568. right_margin: Default::default(),
  569. upper_margin: Default::default(),
  570. lower_margin: Default::default(),
  571. hsync_len: Default::default(),
  572. vsync_len: Default::default(),
  573. sync: FbSyncFlags::empty(),
  574. vmode: FbVModeFlags::empty(),
  575. rotate_angle: Default::default(),
  576. colorspace: Default::default(),
  577. }
  578. }
  579. }
  580. /// 帧缓冲区的颜色模式
  581. ///
  582. /// 默认为彩色
  583. #[allow(dead_code)]
  584. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
  585. pub enum FbColorMode {
  586. /// 灰度
  587. GrayScale,
  588. /// 彩色
  589. #[default]
  590. Color,
  591. /// FOURCC
  592. FourCC,
  593. }
  594. /// `FbBitfield` 结构体用于描述颜色字段的位域。
  595. ///
  596. /// 所有的偏移量都是从右边开始,位于一个精确为'bits_per_pixel'宽度的"像素"值内。
  597. /// 一个像素之后是一个位流,并且未经修改地写入视频内存。
  598. ///
  599. /// 对于伪颜色:所有颜色组件的偏移和长度应该相同。
  600. /// 偏移指定了调色板索引在像素值中的最低有效位的位置。
  601. /// 长度表示可用的调色板条目的数量(即条目数 = 1 << 长度)。
  602. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
  603. pub struct FbBitfield {
  604. /// 位域的起始位置
  605. pub offset: u32,
  606. /// 位域的长度
  607. pub length: u32,
  608. /// 最高有效位是否在右边
  609. pub msb_right: bool,
  610. }
  611. impl FbBitfield {
  612. #[allow(dead_code)]
  613. pub fn new(offset: u32, length: u32, msb_right: bool) -> Self {
  614. Self {
  615. offset,
  616. length,
  617. msb_right,
  618. }
  619. }
  620. }
  621. bitflags! {
  622. /// `FbActivateFlags` 用于描述帧缓冲区的激活标志。
  623. ///
  624. /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/uapi/linux/fb.h#198
  625. pub struct FbActivateFlags: u32 {
  626. /// 立即设置值(或vbl)
  627. const FB_ACTIVATE_NOW = 0;
  628. /// 在下一次打开时激活
  629. const FB_ACTIVATE_NXTOPEN = 1;
  630. /// don't set, round up impossible values
  631. const FB_ACTIVATE_TEST = 2;
  632. const FB_ACTIVATE_MASK = 15;
  633. /// 在下一个vbl上激活值
  634. const FB_ACTIVATE_VBL = 16;
  635. /// 在vbl上更改色彩映射
  636. const FB_ACTIVATE_CHANGE_CMAP_VBL = 32;
  637. /// 更改此fb上的所有VC
  638. const FB_ACTIVATE_ALL = 64;
  639. /// 即使没有变化也强制应用
  640. const FB_ACTIVATE_FORCE = 128;
  641. /// 使视频模式无效
  642. const FB_ACTIVATE_INV_MODE = 256;
  643. /// 用于KDSET vt ioctl
  644. const FB_ACTIVATE_KD_TEXT = 512;
  645. }
  646. }
  647. impl Default for FbActivateFlags {
  648. fn default() -> Self {
  649. FbActivateFlags::FB_ACTIVATE_NOW
  650. }
  651. }
  652. #[allow(dead_code)]
  653. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
  654. pub enum FbPixelFormat {
  655. #[default]
  656. Standard,
  657. /// Hold And Modify
  658. HAM,
  659. /// order of pixels in each byte is reversed
  660. Reserved,
  661. }
  662. bitflags! {
  663. pub struct FbSyncFlags: u32 {
  664. /// 水平同步高电平有效
  665. const FB_SYNC_HOR_HIGH_ACT = 1;
  666. /// 垂直同步高电平有效
  667. const FB_SYNC_VERT_HIGH_ACT = 2;
  668. /// 外部同步
  669. const FB_SYNC_EXT = 4;
  670. /// 复合同步高电平有效
  671. const FB_SYNC_COMP_HIGH_ACT = 8;
  672. /// 广播视频时序
  673. const FB_SYNC_BROADCAST = 16;
  674. /// sync on green
  675. const FB_SYNC_ON_GREEN = 32;
  676. }
  677. }
  678. bitflags! {
  679. /// `FbVModeFlags` 用于描述帧缓冲区的视频模式。
  680. pub struct FbVModeFlags: u32 {
  681. /// 非交错
  682. const FB_VMODE_NONINTERLACED = 0;
  683. /// 交错
  684. const FB_VMODE_INTERLACED = 1;
  685. /// 双扫描
  686. const FB_VMODE_DOUBLE = 2;
  687. /// 交错:首先是顶行
  688. const FB_VMODE_ODD_FLD_FIRST = 4;
  689. /// 掩码
  690. const FB_VMODE_MASK = 255;
  691. /// ywrap代替平移
  692. const FB_VMODE_YWRAP = 256;
  693. /// 平滑xpan可能(内部使用)
  694. const FB_VMODE_SMOOTH_XPAN = 512;
  695. /// 不更新x/yoffset
  696. const FB_VMODE_CONUPDATE = 512;
  697. }
  698. }
  699. /// 视频颜色空间
  700. #[allow(dead_code)]
  701. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
  702. pub enum V4l2Colorspace {
  703. /// 默认颜色空间,即让驱动程序自行判断。只能用于视频捕获。
  704. #[default]
  705. Default = 0,
  706. /// SMPTE 170M:用于广播NTSC/PAL SDTV
  707. Smpte170m = 1,
  708. /// 过时的1998年前的SMPTE 240M HDTV标准,已被Rec 709取代
  709. Smpte240m = 2,
  710. /// Rec.709:用于HDTV
  711. Rec709 = 3,
  712. /// 已弃用,不要使用。没有驱动程序会返回这个。这是基于对bt878数据表的误解。
  713. Bt878 = 4,
  714. /// NTSC 1953颜色空间。只有在处理非常非常旧的NTSC录音时才有意义。已被SMPTE 170M取代。
  715. System470M = 5,
  716. /// EBU Tech 3213 PAL/SECAM颜色空间。
  717. System470Bg = 6,
  718. /// 实际上是V4L2_COLORSPACE_SRGB,V4L2_YCBCR_ENC_601和V4L2_QUANTIZATION_FULL_RANGE的简写。用于(Motion-)JPEG。
  719. Jpeg = 7,
  720. /// 用于RGB颜色空间,如大多数网络摄像头所产生的。
  721. Srgb = 8,
  722. /// opRGB颜色空间
  723. Oprgb = 9,
  724. /// BT.2020颜色空间,用于UHDTV。
  725. Bt2020 = 10,
  726. /// Raw颜色空间:用于RAW未处理的图像
  727. Raw = 11,
  728. /// DCI-P3颜色空间,用于电影投影机
  729. DciP3 = 12,
  730. /// Largest supported colorspace value, assigned by the compiler, used
  731. /// by the framework to check for invalid values.
  732. Last,
  733. }
  734. /// `FixedScreenInfo` 结构体用于描述屏幕的固定属性。
  735. #[allow(dead_code)]
  736. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  737. pub struct FixedScreenInfo {
  738. // 字符串,用于标识屏幕,例如 "TT Builtin"
  739. pub id: [char; 16],
  740. // 帧缓冲区的起始物理地址
  741. pub smem_start: Option<PhysAddr>,
  742. // 帧缓冲区的长度
  743. pub smem_len: usize,
  744. // 屏幕类型,参考 FB_TYPE_
  745. pub fb_type: FbType,
  746. // 用于表示交错平面的小端辅助类型
  747. pub type_aux: u32,
  748. // 视觉类型,参考 FB_VISUAL_
  749. pub visual: FbVisual,
  750. // 水平缩放步长,如果无硬件缩放,则为0
  751. pub xpanstep: u16,
  752. // 垂直缩放步长,如果无硬件缩放,则为0
  753. pub ypanstep: u16,
  754. // 垂直环绕步长,如果无硬件环绕,则为0
  755. pub ywrapstep: u16,
  756. // 一行的大小(以字节为单位)
  757. pub line_length: u32,
  758. // 内存映射I/O端口的起始物理地址
  759. pub mmio_start: Option<PhysAddr>,
  760. // 内存映射I/O的长度
  761. pub mmio_len: usize,
  762. // 表示驱动器拥有的特定芯片/卡片类型
  763. pub accel: FbAccel,
  764. // 表示支持的特性,参考 FB_CAP_
  765. pub capabilities: FbCapability,
  766. }
  767. impl FixedScreenInfo {
  768. /// 将字符串转换为长度为16的字符数组(包含结尾的`\0`)
  769. ///
  770. /// ## 参数
  771. ///
  772. /// - `name`: 字符串,长度不超过15,超过的部分将被截断
  773. ///
  774. /// ## 返回
  775. ///
  776. /// 长度为16的字符数组
  777. pub const fn name2id(name: &str) -> [char; 16] {
  778. let mut id = [0u8 as char; 16];
  779. let mut i = 0;
  780. while i < 15 && i < name.len() {
  781. id[i] = name.as_bytes()[i] as char;
  782. i += 1;
  783. }
  784. id[i] = '\0';
  785. return id;
  786. }
  787. }
  788. impl Default for FixedScreenInfo {
  789. fn default() -> Self {
  790. Self {
  791. id: Default::default(),
  792. smem_start: None,
  793. smem_len: Default::default(),
  794. fb_type: FbType::PackedPixels,
  795. type_aux: Default::default(),
  796. visual: FbVisual::Mono10,
  797. xpanstep: Default::default(),
  798. ypanstep: Default::default(),
  799. ywrapstep: Default::default(),
  800. line_length: Default::default(),
  801. mmio_start: None,
  802. mmio_len: Default::default(),
  803. accel: Default::default(),
  804. capabilities: Default::default(),
  805. }
  806. }
  807. }
  808. /// 帧缓冲类型
  809. #[allow(dead_code)]
  810. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  811. pub enum FbType {
  812. /// 压缩像素
  813. PackedPixels = 0,
  814. /// 非交错平面
  815. Planes = 1,
  816. /// 交错平面
  817. InterleavedPlanes = 2,
  818. /// 文本/属性
  819. Text = 3,
  820. /// EGA/VGA平面
  821. VgaPlanes = 4,
  822. /// 由V4L2 FOURCC标识的类型
  823. FourCC = 5,
  824. }
  825. /// 帧缓冲视觉类型
  826. #[allow(dead_code)]
  827. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  828. pub enum FbVisual {
  829. /// 单色。1=黑色 0=白色
  830. Mono01 = 0,
  831. /// 单色。1=白色 0=黑色
  832. Mono10 = 1,
  833. /// 真彩色
  834. TrueColor = 2,
  835. /// 伪彩色(如Atari)
  836. PseudoColor = 3,
  837. /// 直接颜色
  838. DirectColor = 4,
  839. /// 只读的伪彩色
  840. StaticPseudoColor = 5,
  841. /// 由FOURCC标识的类型
  842. FourCC,
  843. }
  844. #[allow(dead_code)]
  845. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
  846. pub enum FbCapability {
  847. #[default]
  848. Default = 0,
  849. /// 设备支持基于FOURCC的格式。
  850. FourCC,
  851. }
  852. /// 视频模式
  853. #[allow(dead_code)]
  854. #[derive(Debug, Clone, Eq, PartialEq)]
  855. pub struct FbVideoMode {
  856. /// 可选的名称
  857. pub name: Option<String>,
  858. /// 可选的刷新率
  859. pub refresh: Option<u32>,
  860. /// 水平分辨率
  861. pub xres: u32,
  862. /// 垂直分辨率
  863. pub yres: u32,
  864. /// 像素时钟
  865. pub pixclock: u32,
  866. /// 左边距
  867. pub left_margin: u32,
  868. /// 右边距
  869. pub right_margin: u32,
  870. /// 上边距
  871. pub upper_margin: u32,
  872. /// 下边距
  873. pub lower_margin: u32,
  874. /// 水平同步长度
  875. pub hsync_len: u32,
  876. /// 垂直同步长度
  877. pub vsync_len: u32,
  878. /// 同步
  879. pub sync: FbSyncFlags,
  880. /// 视频模式
  881. pub vmode: FbVModeFlags,
  882. /// 标志
  883. pub flag: u32,
  884. }
  885. #[allow(dead_code)]
  886. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
  887. pub enum FbAccel {
  888. /// 没有硬件加速器
  889. #[default]
  890. None,
  891. AtariBlitter = 1,
  892. AmigaBlitter = 2,
  893. S3Trio64 = 3,
  894. NCR77C32BLT = 4,
  895. S3Virge = 5,
  896. AtiMach64GX = 6,
  897. DECTGA = 7,
  898. AtiMach64CT = 8,
  899. AtiMach64VT = 9,
  900. AtiMach64GT = 10,
  901. SunCreator = 11,
  902. SunCGSix = 12,
  903. SunLeo = 13,
  904. IMSTwinTurbo = 14,
  905. Acc3DLabsPermedia2 = 15,
  906. MatroxMGA2064W = 16,
  907. MatroxMGA1064SG = 17,
  908. MatroxMGA2164W = 18,
  909. MatroxMGA2164WAGP = 19,
  910. MatroxMGAG400 = 20,
  911. NV3 = 21,
  912. NV4 = 22,
  913. NV5 = 23,
  914. NV6 = 24,
  915. XGIVolariV = 25,
  916. XGIVolariZ = 26,
  917. Omap1610 = 27,
  918. TridentTGUI = 28,
  919. Trident3DImage = 29,
  920. TridentBlade3D = 30,
  921. TridentBladeXP = 31,
  922. CirrusAlpine = 32,
  923. NeoMagicNM2070 = 90,
  924. NeoMagicNM2090 = 91,
  925. NeoMagicNM2093 = 92,
  926. NeoMagicNM2097 = 93,
  927. NeoMagicNM2160 = 94,
  928. NeoMagicNM2200 = 95,
  929. NeoMagicNM2230 = 96,
  930. NeoMagicNM2360 = 97,
  931. NeoMagicNM2380 = 98,
  932. PXA3XX = 99,
  933. Savage4 = 0x80,
  934. Savage3D = 0x81,
  935. Savage3DMV = 0x82,
  936. Savage2000 = 0x83,
  937. SavageMXMV = 0x84,
  938. SavageMX = 0x85,
  939. SavageIXMV = 0x86,
  940. SavageIX = 0x87,
  941. ProSavagePM = 0x88,
  942. ProSavageKM = 0x89,
  943. S3Twister = 0x8a,
  944. S3TwisterK = 0x8b,
  945. SuperSavage = 0x8c,
  946. ProSavageDDR = 0x8d,
  947. ProSavageDDRK = 0x8e,
  948. // Add other accelerators here
  949. }
  950. #[allow(dead_code)]
  951. #[derive(Debug, Copy, Clone)]
  952. pub struct BootTimeScreenInfo {
  953. pub origin_x: u8,
  954. pub origin_y: u8,
  955. /// text mode时,每行的字符数
  956. pub origin_video_cols: u8,
  957. /// text mode时,行数
  958. pub origin_video_lines: u8,
  959. /// 标记屏幕是否为VGA类型
  960. pub is_vga: bool,
  961. /// video mode type
  962. pub video_type: BootTimeVideoType,
  963. // 以下字段用于线性帧缓冲区
  964. /// 线性帧缓冲区的起始物理地址
  965. pub lfb_base: PhysAddr,
  966. /// 线性帧缓冲区在初始化阶段被映射到的起始虚拟地址
  967. ///
  968. /// 这个值可能会被设置2次:
  969. ///
  970. /// - 内存管理初始化之前,early init阶段,临时映射
  971. /// - 内存管理初始化完毕,重新映射时被设置
  972. pub lfb_virt_base: Option<VirtAddr>,
  973. /// 线性帧缓冲区的长度
  974. pub lfb_size: usize,
  975. /// 线性帧缓冲区的宽度(像素)
  976. pub lfb_width: u32,
  977. /// 线性帧缓冲区的高度(像素)
  978. pub lfb_height: u32,
  979. /// 线性帧缓冲区的深度(位数)
  980. pub lfb_depth: u8,
  981. /// 红色位域的大小
  982. pub red_size: u8,
  983. /// 红色位域的偏移量(左移位数)
  984. pub red_pos: u8,
  985. /// 绿色位域的大小
  986. pub green_size: u8,
  987. /// 绿色位域的偏移量(左移位数)
  988. pub green_pos: u8,
  989. /// 蓝色位域的大小
  990. pub blue_size: u8,
  991. /// 蓝色位域的偏移量(左移位数)
  992. pub blue_pos: u8,
  993. }
  994. impl BootTimeScreenInfo {
  995. pub const DEFAULT: Self = Self {
  996. origin_x: 0,
  997. origin_y: 0,
  998. is_vga: false,
  999. lfb_base: PhysAddr::new(0),
  1000. lfb_size: 0,
  1001. lfb_width: 0,
  1002. lfb_height: 0,
  1003. red_size: 0,
  1004. red_pos: 0,
  1005. green_size: 0,
  1006. green_pos: 0,
  1007. blue_size: 0,
  1008. blue_pos: 0,
  1009. video_type: BootTimeVideoType::UnDefined,
  1010. origin_video_cols: 0,
  1011. origin_video_lines: 0,
  1012. lfb_virt_base: None,
  1013. lfb_depth: 0,
  1014. };
  1015. }
  1016. /// Video types for different display hardware
  1017. #[allow(dead_code)]
  1018. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  1019. pub enum BootTimeVideoType {
  1020. UnDefined,
  1021. /// Monochrome Text Display
  1022. Mda,
  1023. /// CGA Display
  1024. Cga,
  1025. /// EGA/VGA in Monochrome Mode
  1026. EgaM,
  1027. /// EGA in Color Mode
  1028. EgaC,
  1029. /// VGA+ in Color Mode
  1030. VgaC,
  1031. /// VESA VGA in graphic mode
  1032. Vlfb,
  1033. /// ACER PICA-61 local S3 video
  1034. PicaS3,
  1035. /// MIPS Magnum 4000 G364 video
  1036. MipsG364,
  1037. /// Various SGI graphics hardware
  1038. Sgi,
  1039. /// DEC TGA
  1040. TgaC,
  1041. /// Sun frame buffer
  1042. Sun,
  1043. /// Sun PCI based frame buffer
  1044. SunPci,
  1045. /// PowerMacintosh frame buffer
  1046. Pmac,
  1047. /// EFI graphic mode
  1048. Efi,
  1049. }
  1050. impl From<u8> for BootTimeVideoType {
  1051. fn from(value: u8) -> Self {
  1052. match value {
  1053. 0 => BootTimeVideoType::UnDefined,
  1054. 0x10 => BootTimeVideoType::Mda,
  1055. 0x11 => BootTimeVideoType::Cga,
  1056. 0x20 => BootTimeVideoType::EgaM,
  1057. 0x21 => BootTimeVideoType::EgaC,
  1058. 0x22 => BootTimeVideoType::VgaC,
  1059. 0x23 => BootTimeVideoType::Vlfb,
  1060. 0x30 => BootTimeVideoType::PicaS3,
  1061. 0x31 => BootTimeVideoType::MipsG364,
  1062. 0x33 => BootTimeVideoType::Sgi,
  1063. 0x40 => BootTimeVideoType::TgaC,
  1064. 0x50 => BootTimeVideoType::Sun,
  1065. 0x51 => BootTimeVideoType::SunPci,
  1066. 0x60 => BootTimeVideoType::Pmac,
  1067. 0x70 => BootTimeVideoType::Efi,
  1068. _ => BootTimeVideoType::UnDefined,
  1069. }
  1070. }
  1071. }
  1072. #[derive(Debug, Default)]
  1073. pub struct FbCursor {
  1074. /// 设置选项
  1075. pub set_mode: FbCursorSetMode,
  1076. /// 开关选项
  1077. pub enable: bool,
  1078. /// 表示光标图像的位操作,true表示XOR,false表示COPY
  1079. pub rop: bool,
  1080. /// 表示光标掩码(mask)的数据。掩码用于定义光标的形状,指定了哪些像素是光标的一部分。
  1081. pub mask: Vec<u8>,
  1082. /// 表示光标的热点位置,即在光标图像中被认为是"焦点"的位置
  1083. pub hot_x: u32,
  1084. pub hot_y: u32,
  1085. /// 光标图像
  1086. pub image: FbImage,
  1087. }
  1088. bitflags! {
  1089. /// 硬件光标控制
  1090. #[derive(Default)]
  1091. pub struct FbCursorSetMode:u8 {
  1092. /// 设置位图
  1093. const FB_CUR_SETIMAGE = 0x01;
  1094. /// 设置位置
  1095. const FB_CUR_SETPOS = 0x02;
  1096. /// 设置热点
  1097. const FB_CUR_SETHOT = 0x04;
  1098. /// ColorMap
  1099. const FB_CUR_SETCMAP = 0x08;
  1100. /// 形状
  1101. const FB_CUR_SETSHAPE = 0x10;
  1102. /// Size
  1103. const FB_CUR_SETSIZE = 0x20;
  1104. /// 全设置
  1105. const FB_CUR_SETALL = 0xFF;
  1106. }
  1107. }
  1108. #[allow(dead_code)]
  1109. #[derive(Debug, Clone, Copy)]
  1110. pub enum ScrollMode {
  1111. Move,
  1112. PanMove,
  1113. WrapMove,
  1114. Redraw,
  1115. PanRedraw,
  1116. }
  1117. impl Default for ScrollMode {
  1118. /// ## 默认Move
  1119. fn default() -> Self {
  1120. Self::Move
  1121. }
  1122. }
  1123. #[derive(Debug, Default, Clone)]
  1124. pub struct FbImage {
  1125. pub x: u32,
  1126. pub y: u32,
  1127. pub width: u32,
  1128. pub height: u32,
  1129. pub fg: u32,
  1130. pub bg: u32,
  1131. pub depth: u8,
  1132. pub data: Vec<u8>,
  1133. }