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