| 1 | use std::time::Duration; |
| 2 | |
| 3 | use smithay::backend::renderer::element::Kind; |
| 4 | use smithay::backend::renderer::gles::GlesTexture; |
| 5 | use smithay::utils::{Scale, Transform}; |
| 6 | |
| 7 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 8 | use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; |
| 9 | use crate::render_helpers::RenderTarget; |
| 10 | |
| 11 | pub const DELAY: Duration = Duration::from_millis(250); |
| 12 | pub const DURATION: Duration = Duration::from_millis(500); |
| 13 | |
| 14 | #[derive(Debug)] |
| 15 | pub struct ScreenTransition { |
| 16 | |
| 17 | from_texture: [TextureBuffer<GlesTexture>; 3], |
| 18 | |
| 19 | start_at: Duration, |
| 20 | |
| 21 | alpha: f32, |
| 22 | } |
| 23 | |
| 24 | impl ScreenTransition { |
| 25 | pub fn new(from_texture: [TextureBuffer<GlesTexture>; 3], start_at: Duration) -> Self { |
| 26 | Self { |
| 27 | from_texture, |
| 28 | start_at, |
| 29 | alpha: 1., |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub fn advance_animations(&mut self, current_time: Duration) { |
| 34 | if self.start_at + DURATION <= current_time { |
| 35 | self.alpha = 0.; |
| 36 | } else if self.start_at <= current_time { |
| 37 | self.alpha = 1. - (current_time - self.start_at).as_secs_f32() / DURATION.as_secs_f32(); |
| 38 | } else { |
| 39 | self.alpha = 1.; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn is_done(&self) -> bool { |
| 44 | self.alpha == 0. |
| 45 | } |
| 46 | |
| 47 | pub fn update_render_elements(&mut self, scale: Scale<f64>, transform: Transform) { |
| 48 | |
| 49 | for buffer in &mut self.from_texture { |
| 50 | buffer.set_texture_scale(scale); |
| 51 | buffer.set_texture_transform(transform); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn render(&self, target: RenderTarget) -> PrimaryGpuTextureRenderElement { |
| 56 | let idx = match target { |
| 57 | RenderTarget::Output => 0, |
| 58 | RenderTarget::Screencast => 1, |
| 59 | RenderTarget::ScreenCapture => 2, |
| 60 | }; |
| 61 | |
| 62 | PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 63 | self.from_texture[idx].clone(), |
| 64 | (0., 0.), |
| 65 | self.alpha, |
| 66 | None, |
| 67 | None, |
| 68 | Kind::Unspecified, |
| 69 | )) |
| 70 | } |
| 71 | } |