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.last = last;
109 if self.stop.is_none() {
110 self.end = None;
111 }
112 }
113
114 pub(super) fn last(&self) -> i64 {
115 self.last
116 }
117
118 fn next_to(&self, n: usize) -> i64 {
119 self.last.min(self.at.saturating_add(n as i64)).max(self.at)
120 }
121
122 pub(super) fn read(&mut self, n: usize) -> Result<Option<Block>, EngineError> {
124 let from = self.at;
125 if !self.pulled(n)? {
126 return Ok(None);
127 }
128 let end = self.end.map_or(self.at, |end| end.clamp(from, self.at));
129 let held = self.table.samples(self.table.root, Extent::new(from, end));
130 Ok(Some(Block {
131 planes: held.planes,
132 start: from,
133 }))
134 }
135
136 pub(super) fn skip(&mut self, to: i64) -> Result<Vec<usize>, EngineError> {
140 if self.end.is_some_and(|end| self.at >= end) {
141 return Ok(Vec::new());
142 }
143 let to = to.min(self.last);
144 let silenced = self.table.skipped(Extent::new(to, self.last))?;
145 self.recording.reach(to);
146 (self.at, self.heard) = (to, to);
147 let future = (to < self.last).then(|| Extent::new(to, self.last));
148 self.table.release(future, Extent::NOWHERE, self.start);
149 if self.last <= to {
150 self.end = Some(to);
151 }
152 Ok(silenced)
153 }
154
155 pub(super) fn pull(&mut self) -> Result<bool, EngineError> {
156 self.pulled(self.block)
157 }
158
159 pub(super) fn pulled(&mut self, n: usize) -> Result<bool, EngineError> {
161 let from = self.at;
162 if self.end.is_some_and(|end| from >= end) {
163 return Ok(false);
164 }
165 let to = self.next_to(n);
166 self.recording.reach(from);
167 let window = Extent::new(from, to);
168 if from == self.start {
169 let history = self
170 .table
171 .history(window, self.block as i64, &mut self.recording)?;
172 self.priced(&history);
173 }
174 let asked = self.spill.as_ref().map(|_| self.table.demand(window));
175 let pulled = self.table.pull(window, &mut self.recording)?;
176 self.priced(&pulled);
177 if let (Some(spill), Some(asked)) = (&mut self.spill, asked) {
178 spill.take(&self.table, &asked);
179 }
180 self.work.samples += (to - from) as u64;
181 self.at = to;
182 self.settle(from, to);
183 let future = (to < self.last).then(|| Extent::new(to, self.last));
184 let keep = Extent::new(from.min(to.saturating_sub(self.keep)), to);
185 self.table.release(future, keep, self.start);
186 Ok(true)
187 }
188
189 fn priced(&mut self, pulled: &Pulled) {
190 self.work.priced_flops += pulled.priced;
191 self.work.waves = self.work.waves.map(|held| held + pulled.waves);
192 self.most_bytes = self.most_bytes.max(pulled.most_bytes);
193 }
194
195 pub(super) fn most_bytes(&self) -> usize {
196 self.most_bytes
197 }
198
199 fn settle(&mut self, from: i64, to: i64) {
202 if self.end.is_some() {
203 return;
204 }
205 if self.last <= to {
206 self.end = Some(to);
207 }
208 let Some(until) = &self.until else {
209 return;
210 };
211 let frame = self.frame as i64;
212 let open = self.start + (from - 1 - self.start).div_euclid(frame) * frame;
213 let base = open.max(self.heard);
214 let heard = self.table.samples(self.table.root, Extent::new(base, to));
215 let planes: Vec<&[f64]> = heard.planes.iter().map(Vec::as_slice).collect();
216 let rate = self.table.values[self.table.root].grid.rate;
217 let known = Known::new(planes, base, self.start, self.frame, rate, to == self.last);
218 if let Some(at) = until.first(&known, base, to) {
219 self.stop = Some(at);
220 self.end = Some(at.max(from));
221 }
222 }
223
224 pub(super) fn end(&self) -> Option<i64> {
225 self.end
226 }
227
228 pub(super) fn stop(&self) -> Option<i64> {
229 self.stop
230 }
231}