forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB64a9351Ivan Molodetskikh2 years ago1319 commits

screen_transition.rs71 lines, 2.2 KiBHistoryRawDownload
1use std::time::Duration;
2
3use smithay::backend::renderer::element::Kind;
4use smithay::backend::renderer::gles::GlesTexture;
5use smithay::utils::{Scale, Transform};
6
7use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
8use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement};
9use crate::render_helpers::RenderTarget;
10
11pub const DELAY: Duration = Duration::from_millis(250);
12pub const DURATION: Duration = Duration::from_millis(500);
13
14#[derive(Debug)]
15pub struct ScreenTransition {
16 /// Texture to crossfade from for each render target.
17 from_texture: [TextureBuffer<GlesTexture>; 3],
18 /// Monotonic time when to start the crossfade.
19 start_at: Duration,
20 /// Current crossfade alpha.
21 alpha: f32,
22}
23
24impl 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 // These textures should remain full-screen, even if scale or transform changes.
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}