niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·055a94d·Ivan Molodetskikh·1 year ago·2247 commits
| 1 | use std::cell::OnceCell; |
| 2 | |
| 3 | use niri_config::BlockOutFrom; |
| 4 | use smithay::backend::allocator::Fourcc; |
| 5 | use smithay::backend::renderer::element::{Kind, RenderElement}; |
| 6 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 7 | use smithay::utils::{Logical, Physical, Point, Rectangle, Scale, Size, Transform}; |
| 8 | |
| 9 | use super::{render_to_encompassing_texture, RenderTarget, ToRenderElement}; |
| 10 | |
| 11 | /// Snapshot of a render. |
| 12 | #[derive(Debug)] |
| 13 | pub struct RenderSnapshot<C, B> { |
| 14 | /// Contents for a normal render. |
| 15 | /// |
| 16 | /// Relative to the geometry. |
| 17 | pub contents: Vec<C>, |
| 18 | |
| 19 | /// Blocked-out contents. |
| 20 | /// |
| 21 | /// Relative to the geometry. |
| 22 | pub blocked_out_contents: Vec<B>, |
| 23 | |
| 24 | /// Where the contents were blocked out from at the time of the snapshot. |
| 25 | pub block_out_from: Option<BlockOutFrom>, |
| 26 | |
| 27 | /// Visual size of the element at the point of the snapshot. |
| 28 | pub size: Size<f64, Logical>, |
| 29 | |
| 30 | /// Contents rendered into a texture (lazily). |
| 31 | pub texture: OnceCell<Option<(GlesTexture, Rectangle<i32, Physical>)>>, |
| 32 | |
| 33 | /// Blocked-out contents rendered into a texture (lazily). |
| 34 | pub blocked_out_texture: OnceCell<Option<(GlesTexture, Rectangle<i32, Physical>)>>, |
| 35 | } |
| 36 | |
| 37 | impl<C, B, EC, EB> RenderSnapshot<C, B> |
| 38 | where |
| 39 | C: ToRenderElement<RenderElement = EC>, |
| 40 | B: ToRenderElement<RenderElement = EB>, |
| 41 | EC: RenderElement<GlesRenderer>, |
| 42 | EB: RenderElement<GlesRenderer>, |
| 43 | { |
| 44 | pub fn texture( |
| 45 | &self, |
| 46 | renderer: &mut GlesRenderer, |
| 47 | scale: Scale<f64>, |
| 48 | target: RenderTarget, |
| 49 | ) -> Option<&(GlesTexture, Rectangle<i32, Physical>)> { |
| 50 | if target.should_block_out(self.block_out_from) { |
| 51 | self.blocked_out_texture.get_or_init(|| { |
| 52 | let _span = tracy_client::span!("RenderSnapshot::Texture"); |
| 53 | |
| 54 | let elements: Vec<_> = self |
| 55 | .blocked_out_contents |
| 56 | .iter() |
| 57 | .map(|baked| { |
| 58 | baked.to_render_element(Point::from((0., 0.)), scale, 1., Kind::Unspecified) |
| 59 | }) |
| 60 | .collect(); |
| 61 | |
| 62 | match render_to_encompassing_texture( |
| 63 | renderer, |
| 64 | scale, |
| 65 | Transform::Normal, |
| 66 | Fourcc::Abgr8888, |
| 67 | &elements, |
| 68 | ) { |
| 69 | Ok((texture, _sync_point, geo)) => Some((texture, geo)), |
| 70 | Err(err) => { |
| 71 | warn!("error rendering blocked-out contents to texture: {err:?}"); |
| 72 | None |
| 73 | } |
| 74 | } |
| 75 | }) |
| 76 | } else { |
| 77 | self.texture.get_or_init(|| { |
| 78 | let _span = tracy_client::span!("RenderSnapshot::Texture"); |
| 79 | |
| 80 | let elements: Vec<_> = self |
| 81 | .contents |
| 82 | .iter() |
| 83 | .map(|baked| { |
| 84 | baked.to_render_element(Point::from((0., 0.)), scale, 1., Kind::Unspecified) |
| 85 | }) |
| 86 | .collect(); |
| 87 | |
| 88 | match render_to_encompassing_texture( |
| 89 | renderer, |
| 90 | scale, |
| 91 | Transform::Normal, |
| 92 | Fourcc::Abgr8888, |
| 93 | &elements, |
| 94 | ) { |
| 95 | Ok((texture, _sync_point, geo)) => Some((texture, geo)), |
| 96 | Err(err) => { |
| 97 | warn!("error rendering contents to texture: {err:?}"); |
| 98 | None |
| 99 | } |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | .as_ref() |
| 104 | } |
| 105 | } |