niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 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, ToRenderElement}; |
| 10 | use crate::render_helpers::{RenderCtx, RenderTarget}; |
| 11 | |
| 12 | /// Snapshot of a render. |
| 13 | #[derive(Debug)] |
| 14 | pub struct RenderSnapshot<C, B> { |
| 15 | /// Contents for a normal render. |
| 16 | /// |
| 17 | /// Relative to the geometry. |
| 18 | pub contents: Vec<C>, |
| 19 | |
| 20 | /// Contents that are not blocked out, but the background is blocked out. |
| 21 | /// |
| 22 | /// If `None` then the background doesn't have any blocked-out surfaces, and normal `contents` |
| 23 | /// can be used instead. |
| 24 | pub contents_with_blocked_out_bg: Option<Vec<C>>, |
| 25 | |
| 26 | /// Blocked-out contents. |
| 27 | /// |
| 28 | /// Relative to the geometry. |
| 29 | pub blocked_out_contents: Vec<B>, |
| 30 | |
| 31 | /// Where the contents were blocked out from at the time of the snapshot. |
| 32 | pub block_out_from: Option<BlockOutFrom>, |
| 33 | |
| 34 | /// Visual size of the element at the point of the snapshot. |
| 35 | pub size: Size<f64, Logical>, |
| 36 | |
| 37 | /// Contents rendered into a texture (lazily). |
| 38 | pub texture: OnceCell<Option<(GlesTexture, Rectangle<i32, Physical>)>>, |
| 39 | |
| 40 | /// Contents with blocked-out bg rendered into a texture (lazily). |
| 41 | pub texture_with_blocked_out_bg: OnceCell<Option<(GlesTexture, Rectangle<i32, Physical>)>>, |
| 42 | |
| 43 | /// Blocked-out contents rendered into a texture (lazily). |
| 44 | pub blocked_out_texture: OnceCell<Option<(GlesTexture, Rectangle<i32, Physical>)>>, |
| 45 | } |
| 46 | |
| 47 | impl<C, B, EC, EB> RenderSnapshot<C, B> |
| 48 | where |
| 49 | C: ToRenderElement<RenderElement = EC>, |
| 50 | B: ToRenderElement<RenderElement = EB>, |
| 51 | EC: RenderElement<GlesRenderer>, |
| 52 | EB: RenderElement<GlesRenderer>, |
| 53 | { |
| 54 | pub fn texture( |
| 55 | &self, |
| 56 | ctx: RenderCtx<GlesRenderer>, |
| 57 | scale: Scale<f64>, |
| 58 | ) -> Option<&(GlesTexture, Rectangle<i32, Physical>)> { |
| 59 | if ctx.target.should_block_out(self.block_out_from) { |
| 60 | self.blocked_out_texture.get_or_init(|| { |
| 61 | let _span = tracy_client::span!("RenderSnapshot::texture"); |
| 62 | |
| 63 | let elements: Vec<_> = self |
| 64 | .blocked_out_contents |
| 65 | .iter() |
| 66 | .map(|baked| { |
| 67 | baked.to_render_element(Point::from((0., 0.)), scale, 1., Kind::Unspecified) |
| 68 | }) |
| 69 | .collect(); |
| 70 | |
| 71 | match render_to_encompassing_texture( |
| 72 | ctx.renderer, |
| 73 | scale, |
| 74 | Transform::Normal, |
| 75 | Fourcc::Abgr8888, |
| 76 | &elements, |
| 77 | ) { |
| 78 | Ok((texture, _sync_point, geo)) => Some((texture, geo)), |
| 79 | Err(err) => { |
| 80 | warn!("error rendering blocked-out contents to texture: {err:?}"); |
| 81 | None |
| 82 | } |
| 83 | } |
| 84 | }) |
| 85 | } else if ctx.target != RenderTarget::Output && self.contents_with_blocked_out_bg.is_some() |
| 86 | { |
| 87 | let contents = self.contents_with_blocked_out_bg.as_ref().unwrap(); |
| 88 | self.texture_with_blocked_out_bg.get_or_init(|| { |
| 89 | let _span = tracy_client::span!("RenderSnapshot::texture"); |
| 90 | |
| 91 | let elements: Vec<_> = contents |
| 92 | .iter() |
| 93 | .map(|baked| { |
| 94 | baked.to_render_element(Point::from((0., 0.)), scale, 1., Kind::Unspecified) |
| 95 | }) |
| 96 | .collect(); |
| 97 | |
| 98 | match render_to_encompassing_texture( |
| 99 | ctx.renderer, |
| 100 | scale, |
| 101 | Transform::Normal, |
| 102 | Fourcc::Abgr8888, |
| 103 | &elements, |
| 104 | ) { |
| 105 | Ok((texture, _sync_point, geo)) => Some((texture, geo)), |
| 106 | Err(err) => { |
| 107 | warn!("error rendering contents with blocked-out bg to texture: {err:?}"); |
| 108 | None |
| 109 | } |
| 110 | } |
| 111 | }) |
| 112 | } else { |
| 113 | self.texture.get_or_init(|| { |
| 114 | let _span = tracy_client::span!("RenderSnapshot::texture"); |
| 115 | |
| 116 | let elements: Vec<_> = self |
| 117 | .contents |
| 118 | .iter() |
| 119 | .map(|baked| { |
| 120 | baked.to_render_element(Point::from((0., 0.)), scale, 1., Kind::Unspecified) |
| 121 | }) |
| 122 | .collect(); |
| 123 | |
| 124 | match render_to_encompassing_texture( |
| 125 | ctx.renderer, |
| 126 | scale, |
| 127 | Transform::Normal, |
| 128 | Fourcc::Abgr8888, |
| 129 | &elements, |
| 130 | ) { |
| 131 | Ok((texture, _sync_point, geo)) => Some((texture, geo)), |
| 132 | Err(err) => { |
| 133 | warn!("error rendering contents to texture: {err:?}"); |
| 134 | None |
| 135 | } |
| 136 | } |
| 137 | }) |
| 138 | } |
| 139 | .as_ref() |
| 140 | } |
| 141 | } |