1use sva_samples::Extent;
4
5use super::RenderConfig;
6use super::table::spill::Spill;
7use super::table::{Pulled, Table};
8use super::until::{Known, Until};
9use crate::cache::Recording;
10use crate::error::EngineError;
11use crate::flops::Work;
12use crate::query::{DEFAULT_FRAME_SECS, Representation};
13
14pub(super) struct Driver {
15 pub(super) table: Table,
16 pub(super) start: i64,
17 pub(super) at: i64,
18 last: i64,
19 block: usize,
20 until: Option<Until>,
21 frame: usize,
22 stop: Option<i64>,
23 end: Option<i64>,
24 keep: i64,
26 heard: i64,
28 most_bytes: usize,
29 pub(super) work: Work,
30 pub(super) recording: Recording,
31 pub(super) spill: Option<Spill>,
32}
33
34pub struct Block {
35 planes: Vec<Vec<f64>>,
36 start: i64,
37}
38
39impl Block {
40 pub fn start(&self) -> i64 {
41 self.start
42 }
43
44 pub fn len(&self) -> usize {
45 self.planes[0].len()
46 }
47
48 pub fn is_empty(&self) -> bool {
49 self.len() == 0
50 }
51
52 pub fn width(&self) -> usize {
53 self.planes.len()
54 }
55
56 pub fn plane(&self, c: usize) -> &[f64] {
57 &self.planes[c]
58 }
59}
60
61pub(super) fn frame(config: &RenderConfig, rate: u32) -> usize {
63 let secs = config
64 .asks
65 .iter()
66 .find_map(|ask| match ask.representation {
67 Representation::Envelope { frame_secs } => Some(frame_secs),
68 _ => None,
69 })
70 .flatten()
71 .unwrap_or(DEFAULT_FRAME_SECS);
72 ((secs * f64::from(rate)).round() as usize).max(1)
73}
74
75impl Driver {
76 pub(super) fn new(
77 table: Table,
78 range: Extent,
79 block: usize,
80 config: &RenderConfig,
81 recording: Recording,
82 ) -> Driver {
83 let frame = frame(config, config.rate);
84 Driver {
85 table,
86 start: range.start,
87 at: range.start,
88 heard: range.start,
89 last: range.end,
90 block,
91 keep: config.until.as_ref().map_or(0, |_| frame as i64),
92 until: config.until.clone(),
93 frame,
94 stop: None,
95 end: None,
96 most_bytes: 0,
97 work: Work {
98 waves: Some(0),
99 ..Work::default()
100 },
101 recording,
102 spill: None,
103 }
104 }
105
106 pub(super) fn replace(&mut self, table: Table, last: i64) {
107 self.table = table;
108 self.bound(last);
109 }
110
111 pub(super) fn bound(&mut self, last: i64) {
113 self.last = last;
114 if self.stop.is_none() {
115 self.end = None;
116 }
117 }
118
119 pub(super) fn last(&self) -> i64 {
120 self.last
121 }
122
123 fn next_to(&self, n: usize) -> i64 {
124 self.last.min(self.at.saturating_add(n as i64)).max(self.at)
125 }
126
127 pub(super) fn read(&mut self, n: usize) -> Result<Option<Block>, EngineError> {
129 let from = self.at;
130 if !self.pulled(n)? {
131 return Ok(None);
132 }
133 let end = self.end.map_or(self.at, |end| end.clamp(from, self.at));
134 let held = self.table.samples(self.table.root, Extent::new(from, end));
135 Ok(Some(Block {
136 planes: held.planes,
137 start: from,
138 }))
139 }
140
141 pub(super) fn skip(&mut self, to: i64) -> Result<Vec<usize>, EngineError> {
145 if self.end.is_some_and(|end| self.at >= end) {
146 return Ok(Vec::new());
147 }
148 let to = to.min(self.last);
149 let silenced = self.table.skipped(Extent::new(to, self.last))?;
150 self.recording.reach(to);
151 (self.at, self.heard) = (to, to);
152 let future = (to < self.last).then(|| Extent::new(to, self.last));
153 self.table.release(future, Extent::NOWHERE, self.start);
154 if self.last <= to {
155 self.end = Some(to);
156 }
157 Ok(silenced)
158 }
159
160 pub(super) fn pull(&mut self) -> Result<bool, EngineError> {
161 self.pulled(self.block)
162 }
163
164 pub(super) fn pulled(&mut self, n: usize) -> Result<bool, EngineError> {
166 let from = self.at;
167 if self.end.is_some_and(|end| from >= end) {
168 return Ok(false);
169 }
170 let to = self.next_to(n);
171 self.recording.reach(from);
172 let window = Extent::new(from, to);
173 if from == self.start {
174 let history = self
175 .table
176 .history(window, self.block as i64, &mut self.recording)?;
177 self.priced(&history);
178 }
179 let asked = self.spill.as_ref().map(|_| self.table.demand(window));
180 let pulled = self.table.pull(window, &mut self.recording)?;
181 self.priced(&pulled);
182 if let (Some(spill), Some(asked)) = (&mut self.spill, asked) {
183 spill.take(&self.table, &asked);
184 }
185 self.work.samples += (to - from) as u64;
186 self.at = to;
187 self.settle(from, to);
188 let future = (to < self.last).then(|| Extent::new(to, self.last));
189 let keep = Extent::new(from.min(to.saturating_sub(self.keep)), to);
190 self.table.release(future, keep, self.start);
191 Ok(true)
192 }
193
194 fn priced(&mut self, pulled: &Pulled) {
195 self.work.priced_flops += pulled.priced;
196 self.work.waves = self.work.waves.map(|held| held + pulled.waves);
197 self.most_bytes = self.most_bytes.max(pulled.most_bytes);
198 }
199
200 pub(super) fn most_bytes(&self) -> usize {
201 self.most_bytes
202 }
203
204 fn settle(&mut self, from: i64, to: i64) {
207 if self.end.is_some() {
208 return;
209 }
210 if self.last <= to {
211 self.end = Some(to);
212 }
213 let Some(until) = &self.until else {
214 return;
215 };
216 let frame = self.frame as i64;
217 let open = self.start + (from - 1 - self.start).div_euclid(frame) * frame;
218 let base = open.max(self.heard);
219 let heard = self.table.samples(self.table.root, Extent::new(base, to));
220 let planes: Vec<&[f64]> = heard.planes.iter().map(Vec::as_slice).collect();
221 let rate = self.table.values[self.table.root].grid.rate;
222 let known = Known::new(planes, base, self.start, self.frame, rate, to == self.last);
223 if let Some(at) = until.first(&known, base, to) {
224 self.stop = Some(at);
225 self.end = Some(at.max(from));
226 }
227 }
228
229 pub(super) fn end(&self) -> Option<i64> {
230 self.end
231 }
232
233 pub(super) fn stop(&self) -> Option<i64> {
234 self.stop
235 }
236}