Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(host): draw the monitor frame on the thread that owns the device

Jareerafbd85e585ea
diff --git a/engine/crates/concat-host/src/preview.rs b/engine/crates/concat-host/src/preview.rs
index d586b5d6..f3db3cc7 100644
--- a/engine/crates/concat-host/src/preview.rs
+++ b/engine/crates/concat-host/src/preview.rs
@@ -4,15 +4,18 @@
//! The paused monitor's true frame.
//!
//! One reader pool for the app's lifetime: its whole value is what stays
-//! warm between scrubs. The pool locks per reader, so a frame is composited
-//! on whatever thread the caller chose while another thread decodes ahead
-//! on a different file - the window debounces and drops stale results, so a
-//! slow decode never wedges anything but itself.
+//! warm between scrubs. The pool locks per reader, so a frame is decoded on
+//! whatever thread the caller chose while another thread decodes ahead on a
+//! different file - the window debounces and drops stale results, so a slow
+//! decode never wedges anything but itself.
//!
//! With the `gpu` feature and a device from [`Monitor::with_gpu`], a frame
-//! is composited on that device and handed back as a texture: decoded
-//! pictures go up once, the composite happens where it is shown, and no
-//! pixel comes back down.
+//! is drawn on that device and handed back as a texture: decoded pictures
+//! go up once, the composite happens where it is shown, and no pixel comes
+//! back down. A frame is therefore two calls and not one -
+//! [`Monitor::frame_sources`] anywhere, [`Monitor::texture_of`] on the
+//! thread that owns the device - because the drawing is not a thing a
+//! worker may do; see `texture_of`.
use std::sync::{Arc, Mutex};
@@ -75,7 +78,7 @@ impl Monitor {
}
/// A monitor that composites on `device` - the window's - so
- /// [`Monitor::frame_texture`] yields textures the window shows as they
+ /// [`Monitor::texture_of`] yields textures the window shows as they
/// are.
#[cfg(feature = "gpu")]
pub fn with_gpu(device: wgpu::Device, queue: wgpu::Queue) -> Self {
@@ -100,23 +103,52 @@ impl Monitor {
}
}
- /// The engine-composited frame at one instant, as a texture on the
- /// device this monitor was given: `Rgba8Unorm`, `spec.width` by
- /// `spec.height`, bindable and renderable. Errs without a device, or
- /// once the device is lost.
+ /// The pictures a monitor frame is made of, decoded and placed but not
+ /// yet drawn.
+ ///
+ /// The half of a frame that is safe on any thread: it reads files and
+ /// the reader pool and never touches the device. The other half is
+ /// [`Monitor::texture_of`], which is safe on exactly one - see there
+ /// for why the two are split at all.
#[cfg(feature = "gpu")]
- pub fn frame_texture(
+ pub fn frame_sources(
&self,
clips: Arc<Vec<ExportClip>>,
settings: &DocumentSettings,
spec: FrameSpec,
+ ) -> Result<concat_export::PreviewSources, String> {
+ let plan = self.plan_for(clips, settings, spec, true);
+ concat_export::preview_sources_of(&self.pool, &plan, spec.time)
+ }
+
+ /// Draws [`Monitor::frame_sources`] into a texture on the device this
+ /// monitor was given: `Rgba8Unorm`, `spec.width` by `spec.height`,
+ /// bindable and renderable. Errs without a device, or once the device
+ /// is lost.
+ ///
+ /// # Call this from the thread that owns the device, and nowhere else
+ ///
+ /// That device is the window's, and the window's renderer took the
+ /// native queue out of it and submits to that queue itself, from the
+ /// event loop, outside anything wgpu locks. A queue is externally
+ /// synchronised in every one of the three APIs underneath: two threads
+ /// submitting to one is undefined, and what it does is not a wrong
+ /// pixel. On Mesa's Intel driver it corrupts the submission the driver
+ /// is building, the GPU hangs on the bad batch, and the reset takes the
+ /// device down for every process on the machine - the editor, the
+ /// player, the browser - until the machine is restarted (#70).
+ ///
+ /// So the decode goes to a worker and the drawing comes back here.
+ #[cfg(feature = "gpu")]
+ pub fn texture_of(
+ &self,
+ sources: &concat_export::PreviewSources,
+ spec: FrameSpec,
) -> Result<wgpu::Texture, String> {
let gpu = self
.gpu
.as_ref()
.ok_or_else(|| "the monitor has no GPU device".to_owned())?;
- let plan = self.plan_for(clips, settings, spec, true);
- let sources = concat_export::preview_sources_of(&self.pool, &plan, spec.time)?;
let mut gpu = gpu.lock().map_err(|_| "compositor poisoned".to_owned())?;
if sources.has_treatments() {
// A layer whose look is a shader is applied where the stack is
diff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rs
index e8326bad..56fdce39 100644
--- a/engine/crates/concat/src/studio.rs
+++ b/engine/crates/concat/src/studio.rs
@@ -1741,8 +1741,10 @@ impl Studio {
pub fn request_preview(&mut self) {
/// A monitor frame on its way to the window.
enum Picture {
- /// Already on the GPU, on the window's own device.
- Texture(concat_host::preview::wgpu::Texture),
+ /// Decoded and placed, waiting to be drawn on the window's own
+ /// device - which happens back on this thread, never on the
+ /// worker that decoded it. See `Monitor::texture_of`.
+ Sources(concat_export::PreviewSources),
/// Raw RGBA, to be uploaded.
Pixels(Vec<u8>, u32, u32),
}
@@ -1890,8 +1892,8 @@ impl Studio {
// one it comes back as pixels and is uploaded here.
let frame = if monitor.has_gpu() {
monitor
- .frame_texture(std::sync::Arc::clone(&clips), &settings, spec)
- .map(Picture::Texture)
+ .frame_sources(std::sync::Arc::clone(&clips), &settings, spec)
+ .map(Picture::Sources)
} else {
monitor
.frame(std::sync::Arc::clone(&clips), &settings, spec)
@@ -1909,20 +1911,32 @@ impl Studio {
}
frame
},
- |studio, _, _, result| {
+ move |studio, _, _, result| {
studio.preview_busy = false;
- match result {
- Ok(Picture::Texture(texture)) => match slint::Image::try_from(texture) {
- Ok(image) => studio.preview = image,
- Err(error) => eprintln!("concat: preview texture: {error}"),
- },
+ let picture = match result {
+ // Drawn here and not on the worker: this is the event
+ // loop, the one thread the window's renderer submits
+ // from, and a second thread submitting beside it hangs
+ // the GPU - see `Monitor::texture_of`.
+ Ok(Picture::Sources(sources)) => studio
+ .host
+ .monitor
+ .texture_of(&sources, spec)
+ .and_then(|texture| {
+ slint::Image::try_from(texture)
+ .map_err(|error| format!("preview texture: {error}"))
+ }),
Ok(Picture::Pixels(bytes, width, height)) => {
let buffer =
slint::SharedPixelBuffer::<slint::Rgba8Pixel>::clone_from_slice(
&bytes, width, height,
);
- studio.preview = slint::Image::from_rgba8(buffer);
+ Ok(slint::Image::from_rgba8(buffer))
}
+ Err(error) => Err(error),
+ };
+ match picture {
+ Ok(image) => studio.preview = image,
Err(error) => {
eprintln!("concat: preview: {error}");
if !studio.preview_failed {