1 //! # Framebuffer 2 //! 3 //! Process specific GPU buffers that can be attached to a plane. 4 5 use crate::buffer; 6 use crate::control; 7 use drm_ffi as ffi; 8 use drm_fourcc::{DrmFourcc, DrmModifier}; 9 10 /// A handle to a framebuffer 11 #[repr(transparent)] 12 #[derive(Copy, Clone, Hash, PartialEq, Eq)] 13 pub struct Handle(control::RawResourceHandle); 14 15 // Safety: Handle is repr(transparent) over NonZeroU32 16 unsafe impl bytemuck::ZeroableInOption for Handle {} 17 unsafe impl bytemuck::PodInOption for Handle {} 18 19 impl From<Handle> for control::RawResourceHandle { from(handle: Handle) -> Self20 fn from(handle: Handle) -> Self { 21 handle.0 22 } 23 } 24 25 impl From<Handle> for u32 { from(handle: Handle) -> Self26 fn from(handle: Handle) -> Self { 27 handle.0.into() 28 } 29 } 30 31 impl From<control::RawResourceHandle> for Handle { from(handle: control::RawResourceHandle) -> Self32 fn from(handle: control::RawResourceHandle) -> Self { 33 Handle(handle) 34 } 35 } 36 37 impl control::ResourceHandle for Handle { 38 const FFI_TYPE: u32 = ffi::DRM_MODE_OBJECT_FB; 39 } 40 41 impl std::fmt::Debug for Handle { fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result42 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 43 f.debug_tuple("framebuffer::Handle").field(&self.0).finish() 44 } 45 } 46 47 /// Information about a framebuffer 48 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] 49 pub struct Info { 50 pub(crate) handle: Handle, 51 pub(crate) size: (u32, u32), 52 pub(crate) pitch: u32, 53 pub(crate) bpp: u32, 54 pub(crate) depth: u32, 55 pub(crate) buffer: Option<buffer::Handle>, 56 } 57 58 impl std::fmt::Display for Info { fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 60 write!(f, "Framebuffer {}", self.handle.0) 61 } 62 } 63 64 impl Info { 65 /// Returns the handle to this framebuffer. handle(&self) -> Handle66 pub fn handle(&self) -> Handle { 67 self.handle 68 } 69 70 /// Returns the size of this framebuffer. size(&self) -> (u32, u32)71 pub fn size(&self) -> (u32, u32) { 72 self.size 73 } 74 75 /// Returns the pitch of this framebuffer. pitch(&self) -> u3276 pub fn pitch(&self) -> u32 { 77 self.pitch 78 } 79 80 /// Returns the bits-per-pixel of this framebuffer. bpp(&self) -> u3281 pub fn bpp(&self) -> u32 { 82 self.bpp 83 } 84 85 /// Returns the depth of this framebuffer. depth(&self) -> u3286 pub fn depth(&self) -> u32 { 87 self.depth 88 } 89 90 /// Returns the buffer handle of this framebuffer. buffer(&self) -> Option<buffer::Handle>91 pub fn buffer(&self) -> Option<buffer::Handle> { 92 self.buffer 93 } 94 } 95 96 /// Information about a framebuffer (with modifiers) 97 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] 98 pub struct PlanarInfo { 99 pub(crate) handle: Handle, 100 pub(crate) size: (u32, u32), 101 pub(crate) pixel_format: DrmFourcc, 102 pub(crate) flags: control::FbCmd2Flags, 103 pub(crate) buffers: [Option<buffer::Handle>; 4], 104 pub(crate) pitches: [u32; 4], 105 pub(crate) offsets: [u32; 4], 106 pub(crate) modifier: Option<DrmModifier>, 107 } 108 109 impl PlanarInfo { 110 /// Returns the handle to this framebuffer. handle(&self) -> Handle111 pub fn handle(&self) -> Handle { 112 self.handle 113 } 114 115 /// Returns the size of this framebuffer. size(&self) -> (u32, u32)116 pub fn size(&self) -> (u32, u32) { 117 self.size 118 } 119 120 /// Returns the pixel format of this framebuffer. pixel_format(&self) -> DrmFourcc121 pub fn pixel_format(&self) -> DrmFourcc { 122 self.pixel_format 123 } 124 125 /// Returns the flags of this framebuffer. flags(&self) -> control::FbCmd2Flags126 pub fn flags(&self) -> control::FbCmd2Flags { 127 self.flags 128 } 129 130 /// Returns the buffer handles of this framebuffer. buffers(&self) -> [Option<buffer::Handle>; 4]131 pub fn buffers(&self) -> [Option<buffer::Handle>; 4] { 132 self.buffers 133 } 134 135 /// Returns the pitches of this framebuffer. pitches(&self) -> [u32; 4]136 pub fn pitches(&self) -> [u32; 4] { 137 self.pitches 138 } 139 140 /// Returns the offsets of this framebuffer. offsets(&self) -> [u32; 4]141 pub fn offsets(&self) -> [u32; 4] { 142 self.offsets 143 } 144 145 /// Returns the modifier of this framebuffer. modifier(&self) -> Option<DrmModifier>146 pub fn modifier(&self) -> Option<DrmModifier> { 147 self.modifier 148 } 149 } 150