Skip to main content

alpha2_engine/
snapshot.rs

1use alpha2_eval::{
2    ActiveIndex, AudioSpan, AudioSpanDesc, AudioSpanScratch, EvalError, FrameDesc, FrameNeed,
3    evaluate, evaluate_audio, evaluate_audio_in, frame_needs,
4};
5use alpha2_model::Project;
6use alpha2_time::{MediaTime, TimeRange};
7
8/// The next [`EngineProject::generation`].
9static NEXT_GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
10
11/// An immutable project snapshot and its reusable evaluation index.
12#[derive(Clone, Debug)]
13pub struct EngineProject {
14    project: Project,
15    index: ActiveIndex,
16    generation: u64,
17    /// [`Project::draws_text`], read once.
18    draws_text: bool,
19}
20
21impl EngineProject {
22    /// Validates a project and builds its active interval index.
23    ///
24    /// # Errors
25    /// Returns an evaluation error when the project is structurally invalid.
26    pub fn new(project: Project) -> Result<Self, EvalError> {
27        let index = ActiveIndex::build(&project)?;
28        Ok(Self {
29            draws_text: project.draws_text(),
30            project,
31            index,
32            generation: NEXT_GENERATION.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
33        })
34    }
35
36    /// Process-unique snapshot identity, shared by clones. An [`crate::Exporter`] renders only
37    /// its own snapshot.
38    #[must_use]
39    pub const fn generation(&self) -> u64 {
40        self.generation
41    }
42
43    /// Whether any frame may draw text, which the host must prepare
44    /// ([`crate::TextTextures::prepare_at`]).
45    #[must_use]
46    pub const fn draws_text(&self) -> bool {
47        self.draws_text
48    }
49
50    /// Evaluates a deterministic frame description.
51    ///
52    /// # Errors
53    /// Returns an error if the immutable index and project disagree.
54    pub fn evaluate(&self, time: MediaTime) -> Result<FrameDesc<'_>, EvalError> {
55        evaluate(&self.project, &self.index, time)
56    }
57
58    /// The errors [`Self::evaluate`] would return in `range` because a clip's composition cannot
59    /// draw ([`alpha2_eval::ActiveIndex::plan_errors`]). An export checks its range before frame 0.
60    #[must_use]
61    pub fn plan_errors(&self, range: alpha2_time::TimeRange) -> Vec<EvalError> {
62        self.index.plan_errors(range)
63    }
64
65    /// Returns the host frames needed at a timeline timestamp.
66    ///
67    /// # Errors
68    /// Returns an error if the immutable index and project disagree.
69    pub fn frame_needs(&self, time: MediaTime) -> Result<Vec<FrameNeed>, EvalError> {
70        frame_needs(&self.project, &self.index, time)
71    }
72
73    /// Evaluates every audio clip overlapping a half-open timeline span.
74    ///
75    /// # Errors
76    /// Returns an error if the immutable index and project disagree.
77    pub fn evaluate_audio(&self, range: TimeRange) -> Result<AudioSpanDesc<'_>, EvalError> {
78        evaluate_audio(&self.project, &self.index, range)
79    }
80
81    /// [`Self::evaluate_audio`] into reusable `scratch`.
82    ///
83    /// # Errors
84    /// Returns an error if the immutable index and project disagree.
85    pub fn evaluate_audio_in<'s>(
86        &self,
87        range: TimeRange,
88        scratch: &'s mut AudioSpanScratch,
89    ) -> Result<AudioSpan<'_, 's>, EvalError> {
90        evaluate_audio_in(&self.project, &self.index, range, scratch)
91    }
92
93    /// The smallest interval holding everything the timeline shows, captions included
94    /// ([`alpha2_model::Timeline::extent`]).
95    #[must_use]
96    pub fn timeline_range(&self) -> Option<TimeRange> {
97        self.project.extent()
98    }
99
100    /// Every effect the project can draw, by id and version, each once. Compositions that cannot
101    /// draw are reported when a frame shows them.
102    #[must_use]
103    pub fn effects(&self) -> Vec<(&str, u32)> {
104        let mut effects = self
105            .index
106            .effects()
107            .map(|effect| (effect.id.as_str(), effect.version))
108            .collect::<Vec<_>>();
109        effects.sort_unstable();
110        effects.dedup();
111        effects
112    }
113
114    /// Checks that `renderer` can draw every effect the project uses
115    /// ([`Self::effects`]).
116    ///
117    /// # Errors
118    /// Returns [`crate::EngineError::UnregisteredEffect`] naming the first missing one.
119    pub fn check_effects(
120        &self,
121        renderer: &alpha2_render::Renderer,
122    ) -> Result<(), crate::EngineError> {
123        crate::frame::require_effects(renderer, self.effects())
124    }
125
126    /// Returns the immutable project document.
127    #[must_use]
128    pub fn project(&self) -> &Project {
129        &self.project
130    }
131
132    /// What the project's timeline-time values read their data bindings through.
133    #[must_use]
134    pub fn sample_context(&self) -> alpha2_eval::SampleContext<'_> {
135        self.index.sample_context(&self.project)
136    }
137}