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/// Which render backend is attached — what
97/// [`Engine::attached_render`](crate::Engine::attached_render) reports.
98///
99/// Non-exhaustive: a future backend (e.g. Vulkan, if mpv ever exposes it
100/// through the render API) is an additive variant, not a breaking change.
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102#[non_exhaustive]
103pub enum RenderKind {
104    /// The OpenGL backend
105    /// ([`Engine::attach_gl_render`](crate::Engine::attach_gl_render)).
106    OpenGl,
107    /// The software backend
108    /// ([`Engine::attach_sw_render`](crate::Engine::attach_sw_render)).
109    Software,
110}
111
112/// The one attached render backend. Backends share the engine's single
113/// slot so `AlreadyAttached` and `detach_render` behave uniformly —
114/// mpv allows one render context per handle regardless of type.
115pub(crate) enum RenderBackend {
116    Gl(GlRender),
117    Sw(SwRender),
118}
119
120impl RenderBackend {
121    /// Process pending render work (`mpv_render_context_update`);
122    /// `true` when a new frame should be drawn.
123    pub(crate) fn update(&mut self) -> bool {
124        match self {
125            RenderBackend::Gl(r) => r.ctx.update(),
126            RenderBackend::Sw(r) => r.0.update(),
127        }
128    }
129
130    pub(crate) fn kind(&self) -> RenderKind {
131        match self {
132            RenderBackend::Gl(_) => RenderKind::OpenGl,
133            RenderBackend::Sw(_) => RenderKind::Software,
134        }
135    }
136}
137
138pub(crate) struct GlRender {
139    ctx: OwnedRenderContext,
140    block_for_target_time: bool,
141}
142
143impl GlRender {
144    /// Create an OpenGL render context co-owning `core` and register
145    /// `on_update`.
146    ///
147    /// `on_update` fires once synchronously here (mpv's documented
148    /// behavior) and afterwards from the render thread.
149    ///
150    /// # Safety
151    /// Forwards rsmpv's `new_opengl` contract: the target GL context must
152    /// be current on the calling thread now, on every later
153    /// [`render`](Self::render) or update, and when this value drops.
154    pub(crate) unsafe fn create(
155        core: Arc<Mpv>,
156        get_proc_address: ProcAddressFn,
157        options: GlRenderOptions,
158        on_update: impl Fn() + Send + Sync + 'static,
159    ) -> Result<Self> {
160        // SAFETY: GL-currency contract forwarded to the caller.
161        let mut ctx = unsafe {
162            OwnedRenderContext::new_opengl(core, options.advanced_control, get_proc_address)?
163        };
164        ctx.set_update_callback(on_update);
165        Ok(Self {
166            ctx,
167            block_for_target_time: options.block_for_target_time,
168        })
169    }
170
171    /// Draw the current frame into `fbo` (`0` = default framebuffer).
172    /// `flip_y` handles targets with a flipped origin (e.g. GTK's GLArea).
173    /// Whether this blocks until the frame's target time was fixed at
174    /// attach ([`GlRenderOptions::block_for_target_time`]).
175    pub(crate) fn render(&mut self, fbo: i32, w: i32, h: i32, flip_y: bool) -> Result<()> {
176        let fbo = OpenGlFbo {
177            fbo,
178            width: w,
179            height: h,
180            internal_format: 0,
181        };
182        self.ctx
183            .render_opengl(fbo, flip_y, self.block_for_target_time)?;
184        Ok(())
185    }
186}
187
188/// Software rendering: mpv draws the frame into a caller-provided RGBA
189/// buffer. No GL anywhere — no context-current requirements for rendering
190/// *or* teardown, so unlike [`GlRender`] this backend is fully safe and
191/// drops from any thread.
192pub(crate) struct SwRender(OwnedRenderContext);
193
194impl SwRender {
195    /// Create a software render context co-owning `core` and register
196    /// `on_update` (same contract as the GL backend: fires once
197    /// synchronously here, afterwards from mpv's render thread).
198    pub(crate) fn create(
199        core: Arc<Mpv>,
200        on_update: impl Fn() + Send + Sync + 'static,
201    ) -> Result<Self> {
202        let mut ctx = OwnedRenderContext::new_software(core)?;
203        ctx.set_update_callback(on_update);
204        Ok(Self(ctx))
205    }
206
207    /// Render the current frame as RGBA8 into `buf`, resizing it to
208    /// `w * h * 4`.
209    pub(crate) fn render(&mut self, w: i32, h: i32, buf: &mut Vec<u8>) -> Result<()> {
210        // Nothing to draw for empty or negative dimensions — rsmpv would
211        // reject them as InvalidParameter, but a no-op mirrors this
212        // crate's render-before-attach philosophy. The overflow check
213        // keeps the multiply sound on 32-bit targets.
214        let (Ok(uw), Ok(uh)) = (usize::try_from(w), usize::try_from(h)) else {
215            return Ok(());
216        };
217        let Some(len) = uw.checked_mul(uh).and_then(|p| p.checked_mul(4)) else {
218            return Ok(());
219        };
220        if len == 0 {
221            return Ok(());
222        }
223        buf.resize(len, 0);
224        self.0
225            .render_software(w, h, SwPixelFormat::Rgb0, uw * 4, buf)?;
226        // "rgb0" leaves the fourth byte of each pixel undefined; a
227        // consumer treating the buffer as RGBA reads it as alpha and
228        // gets garbage transparency. Force opaque here — the quirk
229        // belongs to the seam, not to every consumer.
230        for px in buf.chunks_exact_mut(4) {
231            px[3] = 0xFF;
232        }
233        Ok(())
234    }
235}