niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·53500dc·Tahinli·1 month ago·2998 commits
| 1 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; |
| 2 | use smithay::backend::renderer::element::Kind; |
| 3 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 4 | use smithay::backend::renderer::utils::{import_surface, RendererSurfaceStateUserData}; |
| 5 | use smithay::backend::renderer::{ImportAll, Renderer}; |
| 6 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 7 | use smithay::utils::{Logical, Physical, Point, Scale}; |
| 8 | use smithay::wayland::compositor::{with_surface_tree_downward, TraversalAction}; |
| 9 | |
| 10 | use super::texture::TextureBuffer; |
| 11 | use super::BakedBuffer; |
| 12 | |
| 13 | /// Renders elements from a surface tree as textures into `storage`. |
| 14 | pub fn render_snapshot_from_surface_tree( |
| 15 | renderer: &mut GlesRenderer, |
| 16 | surface: &WlSurface, |
| 17 | location: Point<f64, Logical>, |
| 18 | storage: &mut Vec<BakedBuffer<TextureBuffer<GlesTexture>>>, |
| 19 | ) { |
| 20 | let _span = tracy_client::span!("render_snapshot_from_surface_tree"); |
| 21 | |
| 22 | with_surface_tree_downward( |
| 23 | surface, |
| 24 | location, |
| 25 | |_, states, location| { |
| 26 | let mut location = *location; |
| 27 | let data = states.data_map.get::<RendererSurfaceStateUserData>(); |
| 28 | |
| 29 | if let Some(data) = data { |
| 30 | let data = &*data.lock().unwrap(); |
| 31 | |
| 32 | if let Some(view) = data.view() { |
| 33 | location += view.offset.to_f64(); |
| 34 | TraversalAction::DoChildren(location) |
| 35 | } else { |
| 36 | TraversalAction::SkipChildren |
| 37 | } |
| 38 | } else { |
| 39 | TraversalAction::SkipChildren |
| 40 | } |
| 41 | }, |
| 42 | |_, states, location| { |
| 43 | let mut location = *location; |
| 44 | let data = states.data_map.get::<RendererSurfaceStateUserData>(); |
| 45 | |
| 46 | if let Some(data) = data { |
| 47 | let Some(view) = data.lock().unwrap().view() else { |
| 48 | return; |
| 49 | }; |
| 50 | location += view.offset.to_f64(); |
| 51 | |
| 52 | if let Err(err) = import_surface(renderer, states) { |
| 53 | warn!("failed to import surface: {err:?}"); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | let data = data.lock().unwrap(); |
| 58 | let Some(texture) = data.texture(renderer.context_id()) else { |
| 59 | return; |
| 60 | }; |
| 61 | |
| 62 | let buffer = TextureBuffer::from_texture( |
| 63 | renderer, |
| 64 | texture.clone(), |
| 65 | f64::from(data.buffer_scale()), |
| 66 | data.buffer_transform(), |
| 67 | Vec::new(), |
| 68 | ); |
| 69 | |
| 70 | let baked = BakedBuffer { |
| 71 | buffer, |
| 72 | location, |
| 73 | src: Some(view.src), |
| 74 | dst: Some(view.dst), |
| 75 | }; |
| 76 | |
| 77 | storage.push(baked); |
| 78 | } |
| 79 | }, |
| 80 | |_, _, _| true, |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | pub fn push_elements_from_surface_tree<R>( |
| 85 | renderer: &mut R, |
| 86 | surface: &WlSurface, |
| 87 | // Fractional scale expects surface buffers to be aligned to physical pixels. |
| 88 | location: Point<i32, Physical>, |
| 89 | scale: Scale<f64>, |
| 90 | alpha: f32, |
| 91 | kind: Kind, |
| 92 | push: &mut dyn FnMut(WaylandSurfaceRenderElement<R>), |
| 93 | ) where |
| 94 | R: Renderer + ImportAll, |
| 95 | R::TextureId: Clone + 'static, |
| 96 | { |
| 97 | let _span = tracy_client::span!("push_elements_from_surface_tree"); |
| 98 | |
| 99 | let location = location.to_f64(); |
| 100 | |
| 101 | with_surface_tree_downward( |
| 102 | surface, |
| 103 | location, |
| 104 | |_, states, location| { |
| 105 | let mut location = *location; |
| 106 | let data = states.data_map.get::<RendererSurfaceStateUserData>(); |
| 107 | |
| 108 | if let Some(data) = data { |
| 109 | if let Some(view) = data.lock().unwrap().view() { |
| 110 | location += view.offset.to_f64().to_physical(scale); |
| 111 | TraversalAction::DoChildren(location) |
| 112 | } else { |
| 113 | TraversalAction::SkipChildren |
| 114 | } |
| 115 | } else { |
| 116 | TraversalAction::SkipChildren |
| 117 | } |
| 118 | }, |
| 119 | |surface, states, location| { |
| 120 | let mut location = *location; |
| 121 | let data = states.data_map.get::<RendererSurfaceStateUserData>(); |
| 122 | |
| 123 | if let Some(data) = data { |
| 124 | let has_view = if let Some(view) = data.lock().unwrap().view() { |
| 125 | location += view.offset.to_f64().to_physical(scale); |
| 126 | true |
| 127 | } else { |
| 128 | false |
| 129 | }; |
| 130 | |
| 131 | if has_view { |
| 132 | match WaylandSurfaceRenderElement::from_surface( |
| 133 | renderer, surface, states, location, alpha, kind, |
| 134 | ) { |
| 135 | Ok(Some(surface)) => push(surface), |
| 136 | Ok(None) => {} // surface is not mapped |
| 137 | Err(err) => { |
| 138 | warn!("failed to import surface: {}", err); |
| 139 | } |
| 140 | }; |
| 141 | } |
| 142 | } |
| 143 | }, |
| 144 | |_, _, _| true, |
| 145 | ); |
| 146 | } |