vendetta / libvendetta / mpv / mpv.v
761 lines · 684 sloc · 25.94 KB
Raw
1// libmpv wrapper for V. The declarations follow mpv's own headers: client.h,
2// render.h and render_gl.h.
3@[has_globals]
4module mpv
5
6#flag -lmpv
7
8$if linux {
9 #flag -lGL
10 #flag -DGL_GLEXT_PROTOTYPES
11}
12$if windows {
13 // The win64 kit ships the libmpv headers, an import library generated from
14 // the DLL's export table, and nothing else; see mpv/win64/README.md.
15 #flag windows -I @VMODROOT/mpv/win64/include
16 #flag windows -L @VMODROOT/mpv/win64/lib
17 #flag windows -lmpv
18 // wgl_proc asks opengl32.dll for GL entry points.
19 #flag windows -lopengl32
20}
21$if android {
22 // The android kit ships the libmpv headers and takes the libmpv shared
23 // library for the architecture being linked; see mpv/android/README.md.
24 // One directory for the library, because vab compiles the V sources once
25 // and lets the NDK compile the C per architecture — the -L line has to be
26 // the same for every arch, so a build links one architecture at a time
27 // and vab's --libs dir carries the runtime copies into the APK.
28 #flag android -I @VMODROOT/mpv/android/include
29 #flag android -L @VMODROOT/mpv/android/lib
30 // android_gl_proc resolves GL entry points from libGLESv3 by dlsym and
31 // falls back to libEGL's trampolines for extensions; the surface's
32 // texture work links GLESv3 directly. liblog and libandroid come with
33 // the NativeActivity build the sokol backend expects.
34 #flag android -lEGL
35 #flag android -lGLESv3
36 #flag android -llog
37 #flag android -landroid
38}
39$if linux {
40 #flag -std=gnu99
41}
42$if macos {
43 // libmpv comes from Homebrew or MacPorts: clang on macOS searches none
44 // of those prefixes by default, so all are given and clang ignores the
45 // ones that do not exist (Homebrew keeps /opt/homebrew on Apple Silicon
46 // and /usr/local on Intel; MacPorts /opt/local). A libmpv installed
47 // anywhere else reaches the build through -cflags '-I<dir> -L<dir>'.
48 #flag darwin -I/opt/homebrew/include
49 #flag darwin -I/usr/local/include
50 #flag darwin -I/opt/local/include
51 #flag darwin -L/opt/homebrew/lib
52 #flag darwin -L/usr/local/lib
53 #flag darwin -L/opt/local/lib
54 // The surface's GL calls link straight against the framework, the way
55 // the Linux path links -lGL.
56 #flag darwin -framework OpenGL
57}
58$if openbsd {
59 // libmpv comes from the packages tree (/usr/local), which clang on
60 // OpenBSD searches none of; the GL side needs no path here, because
61 // xenocara's /usr/X11R6 tree is put on the search paths by sokol's own
62 // OpenBSD flags, which every module's build shares.
63 #flag openbsd -I/usr/local/include
64 #flag openbsd -L/usr/local/lib
65}
66$if netbsd {
67 // libmpv comes from pkgsrc (/usr/pkg), which clang on NetBSD does not
68 // search; the GL side needs no path here, because the base system's
69 // X11R7 tree is put on the search paths by sokol's own NetBSD flags.
70 #flag netbsd -I/usr/pkg/include
71 #flag netbsd -L/usr/pkg/lib
72}
73$if dragonfly {
74 // Everything comes from dports (/usr/local), the FreeBSD layout, and
75 // clang on DragonFly searches it no better than OpenBSD's does.
76 #flag dragonfly -I/usr/local/include
77 #flag dragonfly -L/usr/local/lib
78}
79$if solaris {
80 // Solaris needs no path: its IPS packages install libmpv, the X11 and GL
81 // trees and their headers into /usr, which the compiler and the linker
82 // search by default (both bitnesses, the 64-bit one through the driver's
83 // own /usr/lib/amd64). A libmpv from OpenCSW (/opt/csw) reaches the
84 // build through -cflags '-I<dir> -L<dir>'.
85}
86
87#include "mpv/client.h"
88#include "mpv/render.h"
89#include "mpv/render_gl.h"
90
91$if linux || freebsd || openbsd || netbsd || dragonfly || solaris || macos || android {
92 #include <dlfcn.h>
93}
94$if windows {
95 #include <windows.h>
96}
97$if android {
98 #include <EGL/egl.h>
99}
100
101pub const format_string = 1
102pub const format_flag = 3
103pub const format_double = 5
104
105pub const event_none = 0
106pub const event_get_property_reply = 3
107pub const event_set_property_reply = 4
108pub const event_log_message = 2
109pub const event_shutdown = 1
110pub const event_start_file = 6
111pub const event_end_file = 7
112pub const event_file_loaded = 8
113pub const event_command_reply = 5
114pub const event_seek = 20
115pub const event_playback_restart = 21
116pub const event_property_change = 22
117
118const render_param_invalid = 0
119const render_param_api_type = 1
120const render_param_opengl_init_params = 2
121const render_param_opengl_fbo = 3
122const render_param_flip_y = 4
123
124// render_param_advanced_control is MPV_RENDER_PARAM_ADVANCED_CONTROL. The int
125// it points at turns on the render thread's advanced mode: the core prepares
126// frames ahead and decoding may render straight to a texture. The API user
127// must guarantee the render thread never waits on the core while it is on.
128const render_param_advanced_control = 10
129
130// render_param_block_for_target_time is MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME.
131// The int it points at decides whether render_context_draw waits for the
132// target display time (the default, up to video-timing-offset, 50 ms) before
133// returning.
134const render_param_block_for_target_time = 12
135const render_param_next_frame_info = 11
136const render_param_skip_rendering = 13
137
138// render_frame_info_present is MPV_RENDER_FRAME_INFO_PRESENT: the info
139// describes a queued frame rather than "no next frame".
140pub const render_frame_info_present = 1 << 0
141pub const render_frame_info_redraw = 1 << 1
142
143// render_frame_info_repeat is MPV_RENDER_FRAME_INFO_REPEAT: the queued frame
144// re-presents the picture already on screen, held over its vsyncs.
145pub const render_frame_info_repeat = 1 << 2
146const render_param_x11_display = 8
147
148// RenderParam matches mpv_render_param: a type tag, then a pointer.
149struct RenderParam {
150 typ i32
151 data voidptr
152}
153
154// OpenglFbo matches mpv_opengl_fbo.
155struct OpenglFbo {
156 fbo i32
157 w i32
158 h i32
159 internal_format i32
160}
161
162// OpenglInitParams matches mpv_opengl_init_params.
163struct OpenglInitParams {
164 get_proc_address fn (voidptr, &char) voidptr @[required]
165 get_proc_address_ctx voidptr
166}
167
168fn C.mpv_create() voidptr
169fn C.mpv_initialize(voidptr) i32
170fn C.mpv_destroy(voidptr)
171fn C.mpv_terminate_destroy(voidptr)
172fn C.mpv_error_string(i32) &char
173fn C.mpv_free(voidptr)
174fn C.mpv_client_name(voidptr) &char
175fn C.mpv_set_option_string(voidptr, &char, &char) i32
176fn C.mpv_command(voidptr, voidptr) i32
177fn C.mpv_command_async(voidptr, u64, voidptr) i32
178fn C.mpv_set_property_async(voidptr, u64, &char, i32, voidptr) i32
179fn C.mpv_set_property(voidptr, &char, i32, voidptr) i32
180fn C.mpv_set_property_string(voidptr, &char, &char) i32
181fn C.mpv_get_property(voidptr, &char, i32, voidptr) i32
182fn C.mpv_get_property_string(voidptr, &char) &char
183fn C.mpv_observe_property(voidptr, u64, &char, i32) i32
184fn C.mpv_wait_event(voidptr, f64) voidptr
185fn C.mpv_render_context_create(&voidptr, voidptr, voidptr) i32
186fn C.mpv_render_context_render(voidptr, voidptr) i32
187fn C.mpv_render_context_free(voidptr)
188fn C.mpv_render_context_set_update_callback(voidptr, UpdateCallback, voidptr)
189fn C.mpv_get_property_async(voidptr, u64, &char, i32) i32
190fn C.mpv_render_context_report_swap(voidptr)
191
192@[typedef]
193pub struct C.mpv_render_param {
194 type int
195 data voidptr
196}
197
198fn C.mpv_render_context_get_info(voidptr, C.mpv_render_param) i32
199fn C.mpv_get_time_us(voidptr) i64
200fn C.mpv_render_context_update(voidptr) u64
201fn C.mpv_request_log_messages(voidptr, &char) i32
202
203$if linux || freebsd || openbsd || netbsd || dragonfly || solaris || macos || android {
204 // Linux and the BSDs dlsym libGL's global symbol table (dlfcn ships in
205 // the C library on all of them); macOS dlopens the OpenGL framework
206 // first, so dlopen is declared for all of them.
207 fn C.dlopen(&char, i32) voidptr
208 fn C.dlsym(voidptr, &char) voidptr
209}
210
211$if android {
212 type AvJniSetJavaVm = fn (voidptr, voidptr) i32
213
214 // register_java_vm gives libavcodec the JavaVM before mpv starts: mpv's
215 // Android audio output and hardware decoding reach Java through it. The
216 // library is already loaded as libmpv's dependency, so dlopen answers with
217 // the handle that is in use, and RTLD_NOW is 2 on Android.
218 pub fn register_java_vm(vm voidptr) {
219 avcodec := C.dlopen(c'libavcodec.so', 2)
220 if avcodec == unsafe { nil } {
221 return
222 }
223 setter := C.dlsym(avcodec, c'av_jni_set_java_vm')
224 if setter == unsafe { nil } {
225 return
226 }
227 unsafe { AvJniSetJavaVm(setter)(vm, nil) }
228 }
229}
230
231$if windows {
232 fn C.wglGetProcAddress(&char) voidptr
233 fn C.LoadLibraryA(&char) voidptr
234
235 // wgl_proc resolves an OpenGL entry point the way mpv's get_proc_address
236 // needs on Windows: wglGetProcAddress answers for everything newer than
237 // GL 1.1 once a context is current, and the GL 1.1 names export from
238 // opengl32.dll itself, which the WGL backend of sokol has already loaded.
239 // The surface code asks the same question for its texture work.
240 pub fn wgl_proc(name &char) voidptr {
241 proc := C.wglGetProcAddress(name)
242 if proc != unsafe { nil } {
243 return proc
244 }
245 gl := C.LoadLibraryA(c'opengl32.dll')
246 if gl != unsafe { nil } {
247 return C.GetProcAddress(gl, &u8(name))
248 }
249 return unsafe { nil }
250 }
251}
252
253$if macos {
254 // macOS resolves GL entry points out of the OpenGL framework binary
255 // directly, the way Linux dlsyms libGL: everything up to GL 4.1 exports
256 // from it, so dlopen + dlsym answers every name mpv asks for. The handle
257 // is cached because mpv asks for hundreds of entry points at
258 // render-context creation, and without the cache every one of those asks
259 // would open the framework again before it could resolve its target.
260 __global macos_gl_handle = unsafe { nil }
261
262 pub fn macos_gl_proc(name &char) voidptr {
263 if macos_gl_handle == unsafe { nil } {
264 // RTLD_LAZY is 1; dlfcn.h is included above.
265 macos_gl_handle = C.dlopen(c'/System/Library/Frameworks/OpenGL.framework/OpenGL', 1)
266 }
267 if macos_gl_handle == unsafe { nil } {
268 return unsafe { nil }
269 }
270 proc := C.dlsym(macos_gl_handle, name)
271 if proc != unsafe { nil } {
272 return proc
273 }
274 return C.dlsym(unsafe { nil }, name)
275 }
276}
277
278$if android {
279 // Android resolves GL entry points through libEGL: the driver's EGL
280 // answers for every name mpv asks for while the context sokol's Android
281 // backend made current is on, which is how mpv-android resolves its own
282 // render API. eglGetProcAddress returns nil for a name the driver does
283 // not carry, and mpv treats that as the missing function it is.
284 fn C.eglGetProcAddress(&char) voidptr
285
286 // android_gl_handle caches the libGLESv3 handle: its own symbols are the
287 // driver's real entry points, which dlsym answers for every core GL name.
288 __global android_gl_handle = unsafe { nil }
289
290 pub fn android_gl_proc(name &char) voidptr {
291 // dlsym first: the linked libGLESv3 exports every core GLES entry
292 // point directly, and eglGetProcAddress's trampolines have been seen
293 // misbehaving for some of them on Adreno (mpv's mapped-buffer upload
294 // path lands black tiles in the video texture). Extensions fall back
295 // to EGL, which remains the only source for those names.
296 if android_gl_handle == unsafe { nil } {
297 android_gl_handle = C.dlopen(c'libGLESv3.so', 2)
298 }
299 if android_gl_handle != unsafe { nil } {
300 proc := C.dlsym(android_gl_handle, name)
301 if proc != unsafe { nil } {
302 return proc
303 }
304 }
305 return C.eglGetProcAddress(name)
306 }
307}
308
309// gl_get_proc_address resolves OpenGL entry points from the GL library the
310// program links against, which is what mpv asks for through get_proc_address.
311// GlxGetProcAddress is glXGetProcAddressARB, found through dlsym so no GLX header
312// is needed; a plain C declaration without its prototype would truncate pointers.
313type GlxGetProcAddress = fn (&char) voidptr
314
315// glx_get_proc_ptr caches glXGetProcAddressARB, and gl_scope_handle caches the
316// main-program handle the scope lookups run through: mpv asks for hundreds of
317// entry points at render-context creation, and without the caches every one of
318// those asks runs dlsym for the resolver itself before it can resolve its
319// target.
320__global glx_get_proc_ptr = unsafe { nil }
321__global gl_scope_handle = unsafe { nil }
322
323$if windows {
324 fn win32_gl_get_proc(name &char) voidptr {
325 return wgl_proc(name)
326 }
327}
328
329fn gl_get_proc_address(_ voidptr, name &char) voidptr {
330 $if windows {
331 return win32_gl_get_proc(name)
332 }
333 $if macos {
334 return macos_gl_proc(name)
335 }
336 $if android {
337 return android_gl_proc(name)
338 }
339 $if linux || freebsd || openbsd || netbsd || dragonfly || solaris {
340 // Same Mesa libGL on all, but dlsym's nil handle does not ask the
341 // same question on all: glibc answers it with the global scope;
342 // the BSDs' dlsym(nil, ...) searches the caller's own object and —
343 // on FreeBSD and DragonFly — nothing else, and the main program
344 // merely references libGL, so there every name came back nil and
345 // the render context died on mpv's "Can't load OpenGL functions"
346 // while sokol's link-time GL calls kept working. dlopen(nil, ...)
347 // hands back a handle on the global symbol object, which every
348 // rtld documents as the main program and its startup dependencies,
349 // and dlsym through that handle searches the global scope on all,
350 // which is what libGL needs. RTLD_LAZY is 1 on all.
351 if gl_scope_handle == unsafe { nil } {
352 gl_scope_handle = C.dlopen(unsafe { nil }, 1)
353 }
354 if glx_get_proc_ptr == unsafe { nil } && gl_scope_handle != unsafe { nil } {
355 glx_get_proc_ptr = C.dlsym(gl_scope_handle, c'glXGetProcAddressARB')
356 }
357 if glx_get_proc_ptr != unsafe { nil } {
358 proc := unsafe { GlxGetProcAddress(glx_get_proc_ptr)(name) }
359 if proc != unsafe { nil } {
360 return proc
361 }
362 }
363 if gl_scope_handle != unsafe { nil } {
364 return C.dlsym(gl_scope_handle, name)
365 }
366 return unsafe { nil }
367 }
368}
369
370// create makes a handle. Set options with set_option, then call initialize.
371pub fn create() voidptr {
372 return C.mpv_create()
373}
374
375pub fn initialize(ctx voidptr) i32 {
376 return C.mpv_initialize(ctx)
377}
378
379// destroy tears the core down, not just the client handle: mpv_destroy would
380// only detach, leaving the core and its audio output running — a destroyed
381// player's pulse/pipewire connection would stay open until process exit
382// closed everything at once. Call sites all mean a full teardown, so the
383// terminating variant is what destroy is here.
384pub fn destroy(ctx voidptr) {
385 C.mpv_terminate_destroy(ctx)
386}
387
388// error_string describes a negative return code.
389pub fn error_string(code i32) string {
390 return unsafe { cstring_to_vstring(C.mpv_error_string(code)) }
391}
392
393pub fn client_name(ctx voidptr) string {
394 return unsafe { cstring_to_vstring(C.mpv_client_name(ctx)) }
395}
396
397// set_option sets an option. Before initialize every option is accepted; after
398// it, only the runtime options are.
399pub fn set_option(ctx voidptr, name string, value string) i32 {
400 return C.mpv_set_option_string(ctx, name.str, value.str)
401}
402
403// command runs a command with separate arguments, so a path may contain spaces.
404pub fn command(ctx voidptr, args []string) i32 {
405 mut argv := []voidptr{cap: args.len + 1}
406 for arg in args {
407 argv << voidptr(arg.str)
408 }
409 argv << unsafe { voidptr(nil) }
410 return C.mpv_command(ctx, argv.data)
411}
412
413// command_async queues the command and returns at once. Its result arrives as a
414// COMMAND_REPLY event carrying reply_userdata. Use it from the UI thread: a
415// blocking call waits for mpv's core, which may itself be waiting for a frame.
416pub fn command_async(ctx voidptr, reply_userdata u64, args []string) i32 {
417 mut argv := []voidptr{cap: args.len + 1}
418 for arg in args {
419 argv << voidptr(arg.str)
420 }
421 argv << unsafe { voidptr(nil) }
422 return C.mpv_command_async(ctx, reply_userdata, argv.data)
423}
424
425// set_double_async sets a double property without waiting for mpv's core.
426pub fn set_double_async(ctx voidptr, reply_userdata u64, name string, value f64) i32 {
427 mut v := value
428 return C.mpv_set_property_async(ctx, reply_userdata, name.str, format_double, voidptr(&v))
429}
430
431// set_string_async sets a string property without waiting for mpv's core.
432pub fn set_string_async(ctx voidptr, reply_userdata u64, name string, value string) i32 {
433 mut v := value.str
434 return C.mpv_set_property_async(ctx, reply_userdata, name.str, format_string, voidptr(&v))
435}
436
437// set_flag_async sets a flag property without waiting for mpv's core.
438pub fn set_flag_async(ctx voidptr, reply_userdata u64, name string, value bool) i32 {
439 mut flag := i32(0)
440 flag = i32(value)
441 return C.mpv_set_property_async(ctx, reply_userdata, name.str, format_flag, voidptr(&flag))
442}
443
444pub fn loadfile(ctx voidptr, path string) i32 {
445 return command(ctx, ['loadfile', path])
446}
447
448pub fn stop(ctx voidptr) i32 {
449 return command(ctx, ['stop'])
450}
451
452pub fn seek(ctx voidptr, seconds f64) i32 {
453 return command(ctx, ['seek', '${seconds:.3f}', 'absolute'])
454}
455
456pub fn set_property_string(ctx voidptr, name string, value string) i32 {
457 return C.mpv_set_property_string(ctx, name.str, value.str)
458}
459
460pub fn set_flag(ctx voidptr, name string, value bool) i32 {
461 mut flag := i32(0)
462 flag = i32(value)
463 return C.mpv_set_property(ctx, name.str, format_flag, voidptr(&flag))
464}
465
466pub fn get_string(ctx voidptr, name string) ?string {
467 res := C.mpv_get_property_string(ctx, name.str)
468 if res == unsafe { nil } {
469 return none
470 }
471 defer {
472 C.mpv_free(voidptr(res))
473 }
474 return unsafe { tos_clone(&u8(res)) }
475}
476
477pub fn get_flag(ctx voidptr, name string) ?bool {
478 mut value := i32(0)
479 if C.mpv_get_property(ctx, name.str, format_flag, voidptr(&value)) < 0 {
480 return none
481 }
482 return value != 0
483}
484
485pub fn get_double(ctx voidptr, name string) ?f64 {
486 mut value := f64(0)
487 if C.mpv_get_property(ctx, name.str, format_double, voidptr(&value)) < 0 {
488 return none
489 }
490 return value
491}
492
493// wait_event returns the next queued event. With timeout 0 it does not block;
494// check event_id against event_none to find the end of the queue.
495pub fn wait_event(ctx voidptr, timeout f64) voidptr {
496 return C.mpv_wait_event(ctx, timeout)
497}
498
499// request_log_messages makes mpv send its log as LOG_MESSAGE events at this
500// level: "fatal", "error", "warn", "info", "v", or "debug".
501pub fn request_log_messages(ctx voidptr, level string) i32 {
502 return C.mpv_request_log_messages(ctx, level.str)
503}
504
505// log_message_text reads a LOG_MESSAGE event. mpv_event_log_message is
506// {prefix, level, text} and its pointer is at offset 16 of mpv_event.
507pub fn log_message_text(ev voidptr) string {
508 data := unsafe { *(&voidptr(u64(ev) + 16)) }
509 return unsafe { cstring_to_vstring(*(&&char(u64(data) + 16))) }
510}
511
512// event_data reads mpv_event.data, which sits at offset 16.
513fn event_data(ev voidptr) voidptr {
514 return unsafe { *(&voidptr(u64(ev) + 16)) }
515}
516
517// property_name reads the name of a PROPERTY_CHANGE event.
518pub fn property_name(ev voidptr) string {
519 prop := u64(event_data(ev))
520 name := unsafe { *(&&char(prop)) }
521 return unsafe { cstring_to_vstring(name) }
522}
523
524// property_value returns the data pointer of a PROPERTY_CHANGE event, or nil when
525// mpv has no value for the property (for example when no file is loaded).
526fn property_value(ev voidptr) voidptr {
527 prop := event_data(ev)
528 return unsafe { *(&voidptr(u64(prop) + 16)) }
529}
530
531// property_string reads the value of a string-format PROPERTY_CHANGE event.
532pub fn property_string(ev voidptr) ?string {
533 value := property_value(ev)
534 if value == unsafe { nil } {
535 return none
536 }
537 text := unsafe { *(&&char(value)) }
538 if text == unsafe { nil } {
539 return none
540 }
541 return unsafe { tos_clone(&u8(text)) }
542}
543
544pub fn property_double(ev voidptr) ?f64 {
545 value := property_value(ev)
546 if value == unsafe { nil } {
547 return none
548 }
549 return unsafe { *(&f64(value)) }
550}
551
552pub fn property_flag(ev voidptr) ?bool {
553 value := property_value(ev)
554 if value == unsafe { nil } {
555 return none
556 }
557 return unsafe { *(&i32(value)) } != 0
558}
559
560pub fn event_reply_userdata(ev voidptr) u64 {
561 return unsafe { *(&u64(u64(ev) + 8)) }
562}
563
564// event_error reads mpv_event.error, the status of a reply: 0 or more on
565// success, a negative mpv error code otherwise. It sits at offset 4.
566pub fn event_error(ev voidptr) i32 {
567 return unsafe { *(&i32(u64(ev) + 4)) }
568}
569
570// event_id reads mpv_event.event_id, the first field of every event.
571pub fn event_id(ev voidptr) i32 {
572 return unsafe { *(&i32(ev)) }
573}
574
575pub fn observe_property(ctx voidptr, reply_userdata u64, name string, format i32) i32 {
576 return C.mpv_observe_property(ctx, reply_userdata, name.str, format)
577}
578
579// get_property_async queues a property read and returns at once. The reply
580// arrives on the event queue as a GET_PROPERTY_REPLY event carrying the
581// reply_userdata; its value reads out of the same accessors as property
582// changes (property_name, property_string, ...).
583pub fn get_property_async(ctx voidptr, reply_userdata u64, name string, format i32) i32 {
584 return C.mpv_get_property_async(ctx, reply_userdata, name.str, format)
585}
586
587// UpdateCallback is the mpv_render_update_callback: called on a libmpv thread
588// when a new frame may be available. It must never call mpv_render_*
589// functions; wake the render loop and return.
590pub type UpdateCallback = fn (voidptr)
591
592// render_context_set_update_callback registers the update callback. Advanced
593// control requires it to be set soon after the render context is created.
594pub fn render_context_set_update_callback(render voidptr, cb UpdateCallback, user_data voidptr) {
595 C.mpv_render_context_set_update_callback(render, cb, user_data)
596}
597
598// render_context_create_gl creates an OpenGL render context on the GL context
599// that is current. Create it after initialize and before loading a file, or mpv
600// has nowhere to draw video. Returns nil on failure.
601pub fn render_context_create_gl(ctx voidptr, x11_display voidptr) voidptr {
602 return render_context_create_gl_advanced(ctx, x11_display, false)
603}
604
605// render_context_create_gl_advanced is render_context_create_gl with
606// MPV_RENDER_PARAM_ADVANCED_CONTROL. While it is on, the core may prepare
607// frames ahead and decode straight to a texture, but the thread calling the
608// mpv_render_* functions must never wait on the core: synchronous property
609// reads from that thread can deadlock it for good.
610// On Linux, the BSDs and Solaris the X11 display is passed along (sometimes
611// used by hwdec); on Windows and macOS there is no such render parameter, so
612// the argument is ignored — macOS's GL backend reads the context that is
613// already current.
614pub fn render_context_create_gl_advanced(ctx voidptr, x11_display voidptr, advanced bool) voidptr {
615 init_params := OpenglInitParams{
616 get_proc_address: gl_get_proc_address
617 get_proc_address_ctx: unsafe { nil }
618 }
619 api_type := c'opengl'
620 advanced_flag := if advanced { i32(1) } else { i32(0) }
621 mut params := [
622 RenderParam{
623 typ: render_param_api_type
624 data: voidptr(api_type)
625 },
626 RenderParam{
627 typ: render_param_opengl_init_params
628 data: voidptr(&init_params)
629 },
630 ]
631 $if linux || freebsd || openbsd || netbsd || dragonfly || solaris {
632 params << RenderParam{
633 typ: render_param_x11_display
634 data: x11_display
635 }
636 }
637 params << [
638 RenderParam{
639 typ: render_param_advanced_control
640 data: voidptr(&advanced_flag)
641 },
642 RenderParam{
643 typ: render_param_invalid
644 data: unsafe { nil }
645 },
646 ]
647 mut render := voidptr(unsafe { nil })
648 if C.mpv_render_context_create(&render, ctx, params.data) < 0 {
649 return unsafe { nil }
650 }
651 return render
652}
653
654// RenderFrameInfo mirrors mpv_render_frame_info: the flags of the next
655// queued frame and the absolute time (mpv_get_time_us clock, microseconds) at
656// which it should be displayed.
657pub struct RenderFrameInfo {
658pub mut:
659 flags u64
660 target_time i64
661}
662
663// render_context_frame_info asks about the frame that render_context_draw
664// would show next. Info without the render_frame_info_present bit means no
665// frame is queued.
666pub fn render_context_frame_info(render voidptr) RenderFrameInfo {
667 mut info := RenderFrameInfo{}
668 param := C.mpv_render_param{
669 type: render_param_next_frame_info
670 data: voidptr(&info)
671 }
672 C.mpv_render_context_get_info(render, param)
673 return info
674}
675
676// get_time_us is mpv_get_time_us on the handle's clock, the clock target_time
677// values are on.
678pub fn get_time_us(ctx voidptr) i64 {
679 return C.mpv_get_time_us(ctx)
680}
681
682// render_context_draw renders the current video frame into the framebuffer
683// fbo, which must be w by h pixels. The rows are bottom-up, as GL textures are.
684// When skip is true, mpv discards the frame instead of drawing it — an
685// output-side drop: the surface keeps its previous picture and the queue
686// moves on, which is how a slow machine sheds late frames without stalling.
687// render_context_draw renders the current video frame into the framebuffer
688// fbo, which must be w by h pixels. The rows are bottom-up, as GL textures
689// are. When skip is true, mpv discards the frame instead of drawing it — an
690// already late frame is dropped on the output side rather than shown late.
691// When block_for_target_time is true (mpv's default), the call waits out the
692// frame's target display time before returning, which is what makes the
693// caller's swap land on the vsync the frame was scheduled for; repeats and
694// redraws return at once regardless.
695// flip_on_android selects the flip_y render param per platform.
696fn flip_on_android() bool {
697 $if mpv_noflip ? {
698 return false
699 }
700 return true
701}
702
703pub fn render_context_draw(
704 render voidptr,
705 fbo u32,
706 w i32,
707 h i32,
708 skip bool,
709 block_for_target_time bool
710) i32 {
711 target := OpenglFbo{
712 fbo: i32(fbo)
713 w: w
714 h: h
715 }
716 flip := i32(if flip_on_android() { 1 } else { 0 })
717 block := i32(if block_for_target_time { 1 } else { 0 })
718 skip_flag := i32(if skip { 1 } else { 0 })
719 params := [
720 RenderParam{
721 typ: render_param_opengl_fbo
722 data: voidptr(&target)
723 },
724 RenderParam{
725 typ: render_param_flip_y
726 data: voidptr(&flip)
727 },
728 RenderParam{
729 typ: render_param_block_for_target_time
730 data: voidptr(&block)
731 },
732 RenderParam{
733 typ: render_param_skip_rendering
734 data: voidptr(&skip_flag)
735 },
736 RenderParam{
737 typ: render_param_invalid
738 data: unsafe { nil }
739 },
740 ]
741 return C.mpv_render_context_render(render, params.data)
742}
743
744// render_context_update reports the pending work: render_update_frame is set
745// when a new video frame should be drawn with render_context_draw.
746pub fn render_context_update(render voidptr) u64 {
747 return C.mpv_render_context_update(render)
748}
749
750// render_context_report_swap tells mpv a frame was presented. Optional, but
751// with advanced control on mpv's video timing depends on it: without the
752// reports its timing logic degrades and A/V sync drifts under load.
753pub fn render_context_report_swap(render voidptr) {
754 C.mpv_render_context_report_swap(render)
755}
756
757pub const render_update_frame = u64(1)
758
759pub fn render_context_free(render voidptr) {
760 C.mpv_render_context_free(render)
761}
762