forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

screen_transition.rs77 lines, 2.3 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::animation::Clock;
8use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
9use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement};
10use crate::render_helpers::RenderTarget;
11
12pub const DELAY: Duration = Duration::from_millis(250);
13pub const DURATION: Duration = Duration::from_millis(500);
14
15#[derive(Debug)]
16pub struct ScreenTransition {
17 /// Texture to crossfade from for each render target.
18 from_texture: [TextureBuffer<GlesTexture>; 3],
19 /// Monotonic time when to start the crossfade.
20 start_at: Duration,
21 /// Clock to drive animations.
22 clock: Clock,
23}
24
25impl 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 // These textures should remain full-screen, even if scale or transform changes.
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 // Screen transition ignores animation slowdown.
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}