1use std::time::Duration;
2
3use ff_preview::FrameSink;
4
5use crate::graph::RenderGraph;
6
7#[cfg(feature = "wgpu")]
14pub struct TextureHandle {
15 pub texture: wgpu::Texture,
16 pub view: wgpu::TextureView,
17 pub width: u32,
18 pub height: u32,
19}
20
21pub struct GpuFrameSink {
43 graph: RenderGraph,
44 downstream: Box<dyn FrameSink>,
45}
46
47impl GpuFrameSink {
48 #[must_use]
51 pub fn new(graph: RenderGraph, downstream: Box<dyn FrameSink>) -> Self {
52 Self { graph, downstream }
53 }
54}
55
56impl FrameSink for GpuFrameSink {
57 fn push_frame(&mut self, rgba: &[u8], width: u32, height: u32, pts: Duration) {
58 #[cfg(feature = "wgpu")]
59 {
60 match self.graph.process_gpu(rgba, width, height) {
61 Ok(processed) => {
62 self.downstream.push_frame(&processed, width, height, pts);
63 return;
64 }
65 Err(e) => {
66 log::warn!("GpuFrameSink GPU processing failed, using CPU fallback error={e}");
67 }
68 }
69 }
70 let processed = self.graph.process_cpu(rgba, width, height);
72 self.downstream.push_frame(&processed, width, height, pts);
73 }
74
75 fn flush(&mut self) {
76 self.downstream.flush();
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83 use std::sync::{Arc, Mutex};
84
85 use crate::nodes::ColorGradeNode;
86
87 struct CollectSink(Arc<Mutex<Vec<Vec<u8>>>>);
88
89 impl FrameSink for CollectSink {
90 fn push_frame(&mut self, rgba: &[u8], _w: u32, _h: u32, _pts: Duration) {
91 self.0
92 .lock()
93 .unwrap_or_else(std::sync::PoisonError::into_inner)
94 .push(rgba.to_vec());
95 }
96 }
97
98 #[test]
99 fn gpu_frame_sink_cpu_path_should_forward_processed_frame() {
100 let graph = RenderGraph::new_cpu().push_cpu(ColorGradeNode::new(0.5, 1.0, 1.0, 0.0, 0.0));
102
103 let collected = Arc::new(Mutex::new(Vec::new()));
104 let downstream = Box::new(CollectSink(Arc::clone(&collected)));
105 let mut sink = GpuFrameSink::new(graph, downstream);
106
107 let pts = Duration::from_millis(0);
108 sink.push_frame(&[128u8, 128, 128, 255], 1, 1, pts);
111
112 let guard = collected
113 .lock()
114 .unwrap_or_else(std::sync::PoisonError::into_inner);
115 assert_eq!(guard.len(), 1, "exactly one frame must be forwarded");
116 assert!(
117 guard[0][0] > 128,
118 "brightness +0.5 must increase R channel; got {}",
119 guard[0][0]
120 );
121 }
122
123 #[test]
124 fn gpu_frame_sink_flush_should_propagate_to_downstream() {
125 struct FlushTracker(Arc<Mutex<bool>>);
126 impl FrameSink for FlushTracker {
127 fn push_frame(&mut self, _: &[u8], _: u32, _: u32, _: Duration) {}
128 fn flush(&mut self) {
129 *self
130 .0
131 .lock()
132 .unwrap_or_else(std::sync::PoisonError::into_inner) = true;
133 }
134 }
135
136 let flushed = Arc::new(Mutex::new(false));
137 let mut sink = GpuFrameSink::new(
138 RenderGraph::new_cpu(),
139 Box::new(FlushTracker(Arc::clone(&flushed))),
140 );
141 sink.flush();
142 assert!(
143 *flushed
144 .lock()
145 .unwrap_or_else(std::sync::PoisonError::into_inner),
146 "flush must propagate to downstream"
147 );
148 }
149
150 #[test]
151 fn gpu_frame_sink_should_be_send() {
152 fn assert_send<T: Send>() {}
153 assert_send::<GpuFrameSink>();
154 }
155}