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