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    pub(crate) fn curve_stretches(&self, clip: &alpha2_model::ClipId) -> &[alpha2_eval::Stretch] {
51        self.index.curve_stretches(&self.project, clip)
52    }
53
54    /// Evaluates a deterministic frame description.
55    ///
56    /// # Errors
57    /// Returns an error if the immutable index and project disagree.
58    pub fn evaluate(&self, time: MediaTime) -> Result<FrameDesc<'_>, EvalError> {
59        evaluate(&self.project, &self.index, time)
60    }
61
62    /// The errors [`Self::evaluate`] would return in `range` because a clip's composition cannot
63    /// draw ([`alpha2_eval::ActiveIndex::plan_errors`]). An export checks its range before frame 0.
64    #[must_use]
65    pub fn plan_errors(&self, range: alpha2_time::TimeRange) -> Vec<EvalError> {
66        self.index.plan_errors(range)
67    }
68
69    /// Returns the host frames needed at a timeline timestamp.
70    ///
71    /// # Errors
72    /// Returns an error if the immutable index and project disagree.
73    pub fn frame_needs(&self, time: MediaTime) -> Result<Vec<FrameNeed>, EvalError> {
74        frame_needs(&self.project, &self.index, time)
75    }
76
77    /// Evaluates every audio clip overlapping a half-open timeline span.
78    ///
79    /// # Errors
80    /// Returns an error if the immutable index and project disagree.
81    pub fn evaluate_audio(&self, range: TimeRange) -> Result<AudioSpanDesc<'_>, EvalError> {
82        evaluate_audio(&self.project, &self.index, range)
83    }
84
85    /// [`Self::evaluate_audio`] into reusable `scratch`.
86    ///
87    /// # Errors
88    /// Returns an error if the immutable index and project disagree.
89    pub fn evaluate_audio_in<'s>(
90        &self,
91        range: TimeRange,
92        scratch: &'s mut AudioSpanScratch,
93    ) -> Result<AudioSpan<'_, 's>, EvalError> {
94        evaluate_audio_in(&self.project, &self.index, range, scratch)
95    }
96
97    /// The smallest interval holding everything the timeline shows, captions included
98    /// ([`alpha2_model::Timeline::extent`]).
99    #[must_use]
100    pub fn timeline_range(&self) -> Option<TimeRange> {
101        self.project.extent()
102    }
103
104    /// Every effect the project can draw, by id and version, each once. Compositions that cannot
105    /// draw are reported when a frame shows them.
106    #[must_use]
107    pub fn effects(&self) -> Vec<(&str, u32)> {
108        let mut effects = self
109            .index
110            .effects()
111            .map(|effect| (effect.id.as_str(), effect.version))
112            .collect::<Vec<_>>();
113        effects.sort_unstable();
114        effects.dedup();
115        effects
116    }
117
118    /// Checks that `renderer` can draw every effect the project uses
119    /// ([`Self::effects`]).
120    ///
121    /// # Errors
122    /// Returns [`crate::EngineError::UnregisteredEffect`] naming the first missing one.
123    pub fn check_effects(
124        &self,
125        renderer: &alpha2_render::Renderer,
126    ) -> Result<(), crate::EngineError> {
127        crate::frame::require_effects(renderer, self.effects())
128    }
129
130    /// Returns the immutable project document.
131    #[must_use]
132    pub fn project(&self) -> &Project {
133        &self.project
134    }
135
136    /// What the project's timeline-time values read their data bindings through.
137    #[must_use]
138    pub fn sample_context(&self) -> alpha2_eval::SampleContext<'_> {
139        self.index.sample_context(&self.project)
140    }
141}