Skip to main content

mpv_engine/
render.rs

1//! Render seam — OpenGL and software backends over rsmpv's
2//! [`OwnedRenderContext`].
3//!
4//! Since rsmpv 0.2 the safe render context comes in an owned flavor that
5//! co-owns the core through `Arc<Mpv>` and is `Send` — the two upstream
6//! changes this module's previous raw-`sys` incarnation was waiting for.
7//! Free-before-terminate ordering is now structural (the context's `Arc`
8//! keeps the core alive until the context drops), and what remains here is
9//! the backend-specific param plumbing: fbo/flip wiring for GL, dimension
10//! guards and the `rgb0` alpha quirk for software.
11//!
12//! Threading contract (unchanged): the update callback fires on **mpv's
13//! render thread**, and also *synchronously during registration* —
14//! consumers must be re-entrant-safe at attach time. For the GL backend,
15//! the target GL context must be current for creation, every render, and
16//! teardown — `mpv_render_context_free` tears down GL objects, and freeing
17//! without the right context current leaks them into whatever context *is*
18//! current (in GTK that manifested as whole-window rendering artifacts
19//! after the player page was popped). rsmpv encodes that per-call rule as
20//! an `unsafe` GL constructor; this crate forwards the obligation through
21//! [`Engine::attach_gl_render`](crate::Engine::attach_gl_render) rather
22//! than hiding it behind a safe fn that could still hit undefined
23//! behavior.
24
25use std::ffi::c_void;
26use std::sync::Arc;
27
28use rsmpv::Mpv;
29use rsmpv::render::{OpenGlFbo, OwnedRenderContext, SwPixelFormat};
30
31use crate::error::Result;
32
33/// Resolves GL symbols for mpv. Called during context creation (and
34/// possibly later render calls), so it must stay alive for the context's
35/// lifetime — rsmpv keeps it boxed inside the context.
36pub type ProcAddressFn = Box<dyn FnMut(&str) -> *mut c_void + Send + 'static>;
37
38/// Attach-time knobs for the OpenGL backend
39/// ([`Engine::attach_gl_render`](crate::Engine::attach_gl_render)). The
40/// default is mpv's stock behavior — right for a toolkit paint handler
41/// (GTK GLArea); shells whose render loop must not stall override per
42/// field. Attach-time on purpose: frame pacing is a property of the
43/// shell's render loop, not of any single frame.
44///
45/// Non-exhaustive so future knobs stay additive — which also forbids
46/// struct expressions outside this crate (E0639, functional record
47/// update included), so construct through the chainable setters:
48/// `GlRenderOptions::default().block_for_target_time(false)`.
49#[derive(Debug, Clone, Copy)]
50#[non_exhaustive]
51pub struct GlRenderOptions {
52    /// Block inside [`render_gl`](crate::Engine::render_gl) until the
53    /// frame's target display time — mpv's default, and the right pacing
54    /// when the toolkit's frame clock drives drawing. Set `false` for
55    /// render loops that must not stall (e.g. preparing inside a
56    /// compositor-thread pass, where blocking would hold up the whole
57    /// scene submit): mpv then returns immediately and frame pacing is
58    /// yours — do your own timing, or set the `video-timing-offset`
59    /// property to `0` (mpv's documented alternative).
60    pub block_for_target_time: bool,
61    /// mpv's `MPV_RENDER_PARAM_ADVANCED_CONTROL`: enables direct
62    /// rendering and GPU screenshots, but obligates the shell to follow
63    /// the render API threading rules strictly and to call
64    /// [`render_update`](crate::Engine::render_update) promptly after
65    /// **every** update callback (optional when this is off).
66    pub advanced_control: bool,
67}
68
69impl Default for GlRenderOptions {
70    fn default() -> Self {
71        Self {
72            block_for_target_time: true,
73            advanced_control: false,
74        }
75    }
76}
77
78impl GlRenderOptions {
79    /// Set [`block_for_target_time`](field@Self::block_for_target_time),
80    /// chainable from [`default()`](Default::default).
81    #[must_use]
82    pub fn block_for_target_time(mut self, block: bool) -> Self {
83        self.block_for_target_time = block;
84        self
85    }
86
87    /// Set [`advanced_control`](field@Self::advanced_control), chainable
88    /// from [`default()`](Default::default).
89    #[must_use]
90    pub fn advanced_control(mut self, advanced: bool) -> Self {
91        self.advanced_control = advanced;
92        self
93    }
94}
95
96/// The one attached render backend. Backends share the engine's single
97/// slot so `AlreadyAttached` and `detach_render` behave uniformly —
98/// mpv allows one render context per handle regardless of type.
99pub(crate) enum RenderBackend {
100    Gl(GlRender),
101    Sw(SwRender),
102}
103
104impl RenderBackend {
105    /// Process pending render work (`mpv_render_context_update`);
106    /// `true` when a new frame should be drawn.
107    pub(crate) fn update(&mut self) -> bool {
108        match self {
109            RenderBackend::Gl(r) => r.ctx.update(),
110            RenderBackend::Sw(r) => r.0.update(),
111        }
112    }
113}
114
115pub(crate) struct GlRender {
116    ctx: OwnedRenderContext,
117    block_for_target_time: bool,
118}
119
120impl GlRender {
121    /// Create an OpenGL render context co-owning `core` and register
122    /// `on_update`.
123    ///
124    /// `on_update` fires once synchronously here (mpv's documented
125    /// behavior) and afterwards from the render thread.
126    ///
127    /// # Safety
128    /// Forwards rsmpv's `new_opengl` contract: the target GL context must
129    /// be current on the calling thread now, on every later
130    /// [`render`](Self::render) or update, and when this value drops.
131    pub(crate) unsafe fn create(
132        core: Arc<Mpv>,
133        get_proc_address: ProcAddressFn,
134        options: GlRenderOptions,
135        on_update: impl Fn() + Send + Sync + 'static,
136    ) -> Result<Self> {
137        // SAFETY: GL-currency contract forwarded to the caller.
138        let mut ctx = unsafe {
139            OwnedRenderContext::new_opengl(core, options.advanced_control, get_proc_address)?
140        };
141        ctx.set_update_callback(on_update);
142        Ok(Self {
143            ctx,
144            block_for_target_time: options.block_for_target_time,
145        })
146    }
147
148    /// Draw the current frame into `fbo` (`0` = default framebuffer).
149    /// `flip_y` handles targets with a flipped origin (e.g. GTK's GLArea).
150    /// Whether this blocks until the frame's target time was fixed at
151    /// attach ([`GlRenderOptions::block_for_target_time`]).
152    pub(crate) fn render(&mut self, fbo: i32, w: i32, h: i32, flip_y: bool) -> Result<()> {
153        let fbo = OpenGlFbo {
154            fbo,
155            width: w,
156            height: h,
157            internal_format: 0,
158        };
159        self.ctx
160            .render_opengl(fbo, flip_y, self.block_for_target_time)?;
161        Ok(())
162    }
163}
164
165/// Software rendering: mpv draws the frame into a caller-provided RGBA
166/// buffer. No GL anywhere — no context-current requirements for rendering
167/// *or* teardown, so unlike [`GlRender`] this backend is fully safe and
168/// drops from any thread.
169pub(crate) struct SwRender(OwnedRenderContext);
170
171impl SwRender {
172    /// Create a software render context co-owning `core` and register
173    /// `on_update` (same contract as the GL backend: fires once
174    /// synchronously here, afterwards from mpv's render thread).
175    pub(crate) fn create(
176        core: Arc<Mpv>,
177        on_update: impl Fn() + Send + Sync + 'static,
178    ) -> Result<Self> {
179        let mut ctx = OwnedRenderContext::new_software(core)?;
180        ctx.set_update_callback(on_update);
181        Ok(Self(ctx))
182    }
183
184    /// Render the current frame as RGBA8 into `buf`, resizing it to
185    /// `w * h * 4`.
186    pub(crate) fn render(&mut self, w: i32, h: i32, buf: &mut Vec<u8>) -> Result<()> {
187        // Nothing to draw for empty or negative dimensions — rsmpv would
188        // reject them as InvalidParameter, but a no-op mirrors this
189        // crate's render-before-attach philosophy. The overflow check
190        // keeps the multiply sound on 32-bit targets.
191        let (Ok(uw), Ok(uh)) = (usize::try_from(w), usize::try_from(h)) else {
192            return Ok(());
193        };
194        let Some(len) = uw.checked_mul(uh).and_then(|p| p.checked_mul(4)) else {
195            return Ok(());
196        };
197        if len == 0 {
198            return Ok(());
199        }
200        buf.resize(len, 0);
201        self.0
202            .render_software(w, h, SwPixelFormat::Rgb0, uw * 4, buf)?;
203        // "rgb0" leaves the fourth byte of each pixel undefined; a
204        // consumer treating the buffer as RGBA reads it as alpha and
205        // gets garbage transparency. Force opaque here — the quirk
206        // belongs to the seam, not to every consumer.
207        for px in buf.chunks_exact_mut(4) {
208            px[3] = 0xFF;
209        }
210        Ok(())
211    }
212}