| 1 | use smithay::backend::renderer::element::{Element, Id, Kind, RenderElement, UnderlyingStorage}; |
| 2 | use smithay::backend::renderer::utils::{CommitCounter, OpaqueRegions}; |
| 3 | use smithay::backend::renderer::{Color32F, Frame as _, Renderer}; |
| 4 | use smithay::utils::user_data::UserDataMap; |
| 5 | use smithay::utils::{Buffer, Logical, Physical, Point, Rectangle, Scale, Size}; |
| 6 | |
| 7 | |
| 8 | #[derive(Debug, Clone)] |
| 9 | pub struct SolidColorBuffer { |
| 10 | id: Id, |
| 11 | size: Size<f64, Logical>, |
| 12 | commit: CommitCounter, |
| 13 | color: Color32F, |
| 14 | } |
| 15 | |
| 16 | |
| 17 | #[derive(Debug, Clone)] |
| 18 | pub struct SolidColorRenderElement { |
| 19 | id: Id, |
| 20 | geometry: Rectangle<f64, Logical>, |
| 21 | commit: CommitCounter, |
| 22 | color: Color32F, |
| 23 | kind: Kind, |
| 24 | } |
| 25 | |
| 26 | impl Default for SolidColorBuffer { |
| 27 | fn default() -> Self { |
| 28 | Self { |
| 29 | id: Id::new(), |
| 30 | size: Default::default(), |
| 31 | commit: Default::default(), |
| 32 | color: Default::default(), |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl SolidColorBuffer { |
| 38 | pub fn new(size: impl Into<Size<f64, Logical>>, color: impl Into<Color32F>) -> Self { |
| 39 | SolidColorBuffer { |
| 40 | id: Id::new(), |
| 41 | color: color.into(), |
| 42 | commit: CommitCounter::default(), |
| 43 | size: size.into(), |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub fn resize(&mut self, size: impl Into<Size<f64, Logical>>) { |
| 48 | let size = size.into(); |
| 49 | if size != self.size { |
| 50 | self.size = size; |
| 51 | self.commit.increment(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn set_color(&mut self, color: impl Into<Color32F>) { |
| 56 | let color = color.into(); |
| 57 | if color != self.color { |
| 58 | self.color = color; |
| 59 | self.commit.increment(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub fn update(&mut self, size: impl Into<Size<f64, Logical>>, color: impl Into<Color32F>) { |
| 64 | let size = size.into(); |
| 65 | let color = color.into(); |
| 66 | if size != self.size || color != self.color { |
| 67 | self.size = size; |
| 68 | self.color = color; |
| 69 | self.commit.increment(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn color(&self) -> Color32F { |
| 74 | self.color |
| 75 | } |
| 76 | |
| 77 | pub fn size(&self) -> Size<f64, Logical> { |
| 78 | self.size |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl SolidColorRenderElement { |
| 83 | pub fn from_buffer( |
| 84 | buffer: &SolidColorBuffer, |
| 85 | location: impl Into<Point<f64, Logical>>, |
| 86 | alpha: f32, |
| 87 | kind: Kind, |
| 88 | ) -> Self { |
| 89 | let geo = Rectangle::new(location.into(), buffer.size()); |
| 90 | let color = buffer.color * alpha; |
| 91 | Self::new(buffer.id.clone(), geo, buffer.commit, color, kind) |
| 92 | } |
| 93 | |
| 94 | pub fn new( |
| 95 | id: Id, |
| 96 | geometry: Rectangle<f64, Logical>, |
| 97 | commit: CommitCounter, |
| 98 | color: Color32F, |
| 99 | kind: Kind, |
| 100 | ) -> Self { |
| 101 | SolidColorRenderElement { |
| 102 | id, |
| 103 | geometry, |
| 104 | commit, |
| 105 | color, |
| 106 | kind, |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn color(&self) -> Color32F { |
| 111 | self.color |
| 112 | } |
| 113 | |
| 114 | pub fn geo(&self) -> Rectangle<f64, Logical> { |
| 115 | self.geometry |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl Element for SolidColorRenderElement { |
| 120 | fn id(&self) -> &Id { |
| 121 | &self.id |
| 122 | } |
| 123 | |
| 124 | fn current_commit(&self) -> CommitCounter { |
| 125 | self.commit |
| 126 | } |
| 127 | |
| 128 | fn src(&self) -> Rectangle<f64, Buffer> { |
| 129 | Rectangle::from_size(Size::from((1., 1.))) |
| 130 | } |
| 131 | |
| 132 | fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> { |
| 133 | self.geometry.to_physical_precise_round(scale) |
| 134 | } |
| 135 | |
| 136 | fn opaque_regions(&self, scale: Scale<f64>) -> OpaqueRegions<i32, Physical> { |
| 137 | if self.color.is_opaque() { |
| 138 | let rect = Rectangle::from_size(self.geometry.size).to_physical_precise_down(scale); |
| 139 | OpaqueRegions::from_slice(&[rect]) |
| 140 | } else { |
| 141 | OpaqueRegions::default() |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn alpha(&self) -> f32 { |
| 146 | self.color.a() |
| 147 | } |
| 148 | |
| 149 | fn kind(&self) -> Kind { |
| 150 | self.kind |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl<R: Renderer> RenderElement<R> for SolidColorRenderElement { |
| 155 | fn draw( |
| 156 | &self, |
| 157 | frame: &mut R::Frame<'_, '_>, |
| 158 | _src: Rectangle<f64, Buffer>, |
| 159 | dst: Rectangle<i32, Physical>, |
| 160 | damage: &[Rectangle<i32, Physical>], |
| 161 | _opaque_regions: &[Rectangle<i32, Physical>], |
| 162 | _cache: Option<&UserDataMap>, |
| 163 | ) -> Result<(), R::Error> { |
| 164 | frame.draw_solid(dst, damage, self.color) |
| 165 | } |
| 166 | |
| 167 | #[inline] |
| 168 | fn underlying_storage(&self, _renderer: &mut R) -> Option<UnderlyingStorage<'_>> { |
| 169 | None |
| 170 | } |
| 171 | } |