libmpv  20200718-git-96cdf53
development library for the MPV media player
Looking for a C++ dev?
I'm looking for work. Hire me!
opengl_cb.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 the mpv developers
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14  */
15 
16 #ifndef MPV_CLIENT_API_OPENGL_CB_H_
17 #define MPV_CLIENT_API_OPENGL_CB_H_
18 
19 #include "client.h"
20 
21 #if !MPV_ENABLE_DEPRECATED
22 #error "This header and all API provided by it is deprecated. Use render.h instead."
23 #else
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30  *
31  * Overview
32  * --------
33  *
34  * Warning: this API is deprecated. A very similar API is provided by render.h
35  * and render_gl.h. The deprecated API is emulated with the new API.
36  *
37  * This API can be used to make mpv render into a foreign OpenGL context. It
38  * can be used to handle video display.
39  *
40  * The renderer needs to be explicitly initialized with mpv_opengl_cb_init_gl(),
41  * and then video can be drawn with mpv_opengl_cb_draw(). The user thread can
42  * be notified by new frames with mpv_opengl_cb_set_update_callback().
43  *
44  * You can output and embed video without this API by setting the mpv "wid"
45  * option to a native window handle (see "Embedding the video window" section
46  * in the client.h header). In general, using the opengl-cb API is recommended,
47  * because window embedding can cause various issues, especially with GUI
48  * toolkits and certain platforms.
49  *
50  * OpenGL interop
51  * --------------
52  *
53  * This assumes the OpenGL context lives on a certain thread controlled by the
54  * API user. The following functions require access to the OpenGL context:
55  * mpv_opengl_cb_init_gl
56  * mpv_opengl_cb_draw
57  * mpv_opengl_cb_uninit_gl
58  *
59  * The OpenGL context is indirectly accessed through the OpenGL function
60  * pointers returned by the get_proc_address callback in mpv_opengl_cb_init_gl.
61  * Generally, mpv will not load the system OpenGL library when using this API.
62  *
63  * Only "desktop" OpenGL version 2.1 and later and OpenGL ES version 2.0 and
64  * later are supported. With OpenGL 2.1, the GL_ARB_texture_rg is required. The
65  * renderer was written for the OpenGL 3.x core profile, with additional support
66  * for OpenGL 2.1 and OpenGL ES 2.0.
67  *
68  * Note that some hardware decoding interop API (as set with the "hwdec" option)
69  * may actually access some sort of host API, such as EGL.
70  *
71  * OpenGL state
72  * ------------
73  *
74  * OpenGL has a large amount of implicit state. All the mpv functions mentioned
75  * above expect that the OpenGL state is reasonably set to OpenGL standard
76  * defaults. Likewise, mpv will attempt to leave the OpenGL context with
77  * standard defaults. The following state is excluded from this:
78  *
79  * - the glViewport state
80  * - the glScissor state (but GL_SCISSOR_TEST is in its default value)
81  * - glBlendFuncSeparate() state (but GL_BLEND is in its default value)
82  * - glClearColor() state
83  * - mpv may overwrite the callback set with glDebugMessageCallback()
84  * - mpv always disables GL_DITHER at init
85  *
86  * Messing with the state could be avoided by creating shared OpenGL contexts,
87  * but this is avoided for the sake of compatibility and interoperability.
88  *
89  * On OpenGL 2.1, mpv will strictly call functions like glGenTextures() to
90  * create OpenGL objects. You will have to do the same. This ensures that
91  * objects created by mpv and the API users don't clash. Also, legacy state
92  * must be either in its defaults, or not interfere with core state.
93  *
94  * Threading
95  * ---------
96  *
97  * The mpv_opengl_cb_* functions can be called from any thread, under the
98  * following conditions:
99  * - only one of the mpv_opengl_cb_* functions can be called at the same time
100  * (unless they belong to different mpv cores created by mpv_create())
101  * - for functions which need an OpenGL context (see above) the OpenGL context
102  * must be "current" in the current thread, and it must be the same context
103  * as used with mpv_opengl_cb_init_gl()
104  * - never can be called from within the callbacks set with
105  * mpv_set_wakeup_callback() or mpv_opengl_cb_set_update_callback()
106  *
107  * Context and handle lifecycle
108  * ----------------------------
109  *
110  * Video initialization will fail if the OpenGL context was not initialized yet
111  * (with mpv_opengl_cb_init_gl()). Likewise, mpv_opengl_cb_uninit_gl() will
112  * disable video.
113  *
114  * When the mpv core is destroyed (e.g. via mpv_terminate_destroy()), the OpenGL
115  * context must have been uninitialized. If this doesn't happen, undefined
116  * behavior will result.
117  *
118  * Hardware decoding
119  * -----------------
120  *
121  * Hardware decoding via opengl_cb is fully supported, but requires some
122  * additional setup. (At least if direct hardware decoding modes are wanted,
123  * instead of copying back surface data from GPU to CPU RAM.)
124  *
125  * While "normal" mpv loads the OpenGL hardware decoding interop on demand,
126  * this can't be done with opengl_cb for internal technical reasons. Instead,
127  * it loads them by default, even if hardware decoding is not going to be used.
128  * In older mpv releases, this had to be done by setting the
129  * "opengl-hwdec-interop" or "hwdec-preload" options before calling
130  * mpv_opengl_cb_init_gl(). You can still use the newer "gpu-hwdec-interop"
131  * option to prevent loading of interop, or to load only a specific interop.
132  *
133  * There may be certain requirements on the OpenGL implementation:
134  * - Windows: ANGLE is required (although in theory GL/DX interop could be used)
135  * - Intel/Linux: EGL is required, and also a glMPGetNativeDisplay() callback
136  * must be provided (see sections below)
137  * - nVidia/Linux: Both GLX and EGL should work (GLX is required if vdpau is
138  * used, e.g. due to old drivers.)
139  * - OSX: CGL is required (CGLGetCurrentContext() returning non-NULL)
140  * - iOS: EAGL is required (EAGLContext.currentContext returning non-nil)
141  *
142  * Once these things are setup, hardware decoding can be enabled/disabled at
143  * any time by setting the "hwdec" property.
144  *
145  * Special windowing system interop considerations
146  * ------------------------------------------------
147  *
148  * In some cases, libmpv needs to have access to the windowing system's handles.
149  * This can be a pointer to a X11 "Display" for example. Usually this is needed
150  * only for hardware decoding.
151  *
152  * You can communicate these handles to libmpv by adding a pseudo-OpenGL
153  * extension "GL_MP_MPGetNativeDisplay" to the additional extension string when
154  * calling mpv_opengl_cb_init_gl(). The get_proc_address callback should resolve
155  * a function named "glMPGetNativeDisplay", which has the signature:
156  *
157  * void* GLAPIENTRY glMPGetNativeDisplay(const char* name)
158  *
159  * See below what names are defined. Usually, libmpv will use the native handle
160  * up until mpv_opengl_cb_uninit_gl() is called. If the name is not anything
161  * you know/expected, return NULL from the function.
162  */
163 
164 // Legacy - not supported anymore.
166  int x; // left coordinates of window (usually 0)
167  int y; // top coordinates of window (usually 0)
168  int width; // width of GL window
169  int height; // height of GL window
170 };
171 
172 // Legacy - not supported anymore.
174  // DRM fd (int). set this to -1 if invalid.
175  int fd;
176 
177  // currently used crtc id
178  int crtc_id;
179 
180  // currently used connector id
182 
183  // pointer to the drmModeAtomicReq that is being used for the renderloop.
184  // This atomic request pointer should be usually created at every renderloop.
185  struct _drmModeAtomicReq *atomic_request;
186 };
187 
188 /**
189  * nVidia/Linux via VDPAU requires GLX, which does not have this problem (the
190  * GLX API can return the current X11 Display).
191  *
192  * Windowing system interop on MS win32
193  * ------------------------------------
194  *
195  * You should use ANGLE, and make sure your application and libmpv are linked
196  * to the same ANGLE DLLs. libmpv will pick the device context (needed for
197  * hardware decoding) from the current ANGLE EGL context.
198  */
199 
200 /**
201  * Opaque context, returned by mpv_get_sub_api(MPV_SUB_API_OPENGL_CB).
202  *
203  * A context is bound to the mpv_handle it was retrieved from. The context
204  * will always be the same (for the same mpv_handle), and is valid until the
205  * mpv_handle it belongs to is released.
206  */
208 
209 typedef void (*mpv_opengl_cb_update_fn)(void *cb_ctx);
210 typedef void *(*mpv_opengl_cb_get_proc_address_fn)(void *fn_ctx, const char *name);
211 
212 /**
213  * Set the callback that notifies you when a new video frame is available, or
214  * if the video display configuration somehow changed and requires a redraw.
215  * Similar to mpv_set_wakeup_callback(), you must not call any mpv API from
216  * the callback, and all the other listed restrictions apply (such as not
217  * exiting the callback by throwing exceptions).
218  *
219  * @param callback callback(callback_ctx) is called if the frame should be
220  * redrawn
221  * @param callback_ctx opaque argument to the callback
222  */
224  mpv_opengl_cb_update_fn callback,
225  void *callback_ctx);
226 
227 /**
228  * Initialize the mpv OpenGL state. This retrieves OpenGL function pointers via
229  * get_proc_address, and creates OpenGL objects needed by mpv internally. It
230  * will also call APIs needed for rendering hardware decoded video in OpenGL,
231  * according to the mpv "hwdec" option.
232  *
233  * You must free the associated state at some point by calling the
234  * mpv_opengl_cb_uninit_gl() function. Not doing so may result in memory leaks
235  * or worse.
236  *
237  * @param exts optional _additional_ extension string, can be NULL
238  * @param get_proc_address callback used to retrieve function pointers to OpenGL
239  * functions. This is used for both standard functions
240  * and extension functions. (The extension string is
241  * checked whether extensions are really available.)
242  * The callback will be called from this function only
243  * (it is not stored and never used later).
244  * Usually, GL context APIs do this for you (e.g. with
245  * glXGetProcAddressARB or wglGetProcAddress), but
246  * some APIs do not always return pointers for all
247  * standard functions (even if present); in this case
248  * you have to compensate by looking up these functions
249  * yourself.
250  * @param get_proc_address_ctx arbitrary opaque user context passed to the
251  * get_proc_address callback
252  * @return error code (same as normal mpv_* API), including but not limited to:
253  * MPV_ERROR_UNSUPPORTED: the OpenGL version is not supported
254  * (or required extensions are missing)
255  * MPV_ERROR_INVALID_PARAMETER: the OpenGL state was already initialized
256  */
257 int mpv_opengl_cb_init_gl(mpv_opengl_cb_context *ctx, const char *exts,
258  mpv_opengl_cb_get_proc_address_fn get_proc_address,
259  void *get_proc_address_ctx);
260 
261 /**
262  * Render video. Requires that the OpenGL state is initialized.
263  *
264  * The video will use the full provided framebuffer. Options like "panscan" are
265  * applied to determine which part of the video should be visible and how the
266  * video should be scaled. You can change these options at runtime by using the
267  * mpv property API.
268  *
269  * The renderer will reconfigure itself every time the output rectangle/size
270  * is changed. (If you want to do animations, it might be better to do the
271  * animation on a FBO instead.)
272  *
273  * This function implicitly pulls a video frame from the internal queue and
274  * renders it. If no new frame is available, the previous frame is redrawn.
275  * The update callback set with mpv_opengl_cb_set_update_callback() notifies
276  * you when a new frame was added.
277  *
278  * @param fbo The framebuffer object to render on. Because the renderer might
279  * manage multiple FBOs internally for the purpose of video
280  * postprocessing, it will always bind and unbind FBOs itself. If
281  * you want mpv to render on the main framebuffer, pass 0.
282  * @param w Width of the framebuffer. This is either the video size if the fbo
283  * parameter is 0, or the allocated size of the texture backing the
284  * fbo. The renderer will always use the full size of the fbo.
285  * @param h Height of the framebuffer. Same as with the w parameter, except
286  * that this parameter can be negative. In this case, the video
287  * frame will be rendered flipped.
288  * @return 0
289  */
290 int mpv_opengl_cb_draw(mpv_opengl_cb_context *ctx, int fbo, int w, int h);
291 
292 /**
293  * Deprecated. Use mpv_opengl_cb_draw(). This function is equivalent to:
294  *
295  * int mpv_opengl_cb_render(mpv_opengl_cb_context *ctx, int fbo, int vp[4])
296  * { return mpv_opengl_cb_draw(ctx, fbo, vp[2], vp[3]); }
297  *
298  * vp[0] and vp[1] used to have a meaning, but are ignored in newer versions.
299  *
300  * This function will be removed in the future without version bump (this API
301  * was never marked as stable).
302  */
303 int mpv_opengl_cb_render(mpv_opengl_cb_context *ctx, int fbo, int vp[4]);
304 
305 /**
306  * Tell the renderer that a frame was flipped at the given time. This is
307  * optional, but can help the player to achieve better timing.
308  *
309  * Note that calling this at least once informs libmpv that you will use this
310  * function. If you use it inconsistently, expect bad video playback.
311  *
312  * If this is called while no video or no OpenGL is initialized, it is ignored.
313  *
314  * @param time The mpv time (using mpv_get_time_us()) at which the flip call
315  * returned. If 0 is passed, mpv_get_time_us() is used instead.
316  * Currently, this parameter is ignored.
317  * @return error code
318  */
319 int mpv_opengl_cb_report_flip(mpv_opengl_cb_context *ctx, int64_t time);
320 
321 /**
322  * Destroy the mpv OpenGL state.
323  *
324  * If video is still active (e.g. a file playing), video will be disabled
325  * forcefully.
326  *
327  * Calling this multiple times is ok.
328  *
329  * @return error code
330  */
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 
337 #endif /* else #if MPV_ENABLE_DEPRECATED */
338 
339 #endif
mpv_opengl_cb_window_pos::width
int width
Definition: opengl_cb.h:168
mpv_opengl_cb_set_update_callback
void mpv_opengl_cb_set_update_callback(mpv_opengl_cb_context *ctx, mpv_opengl_cb_update_fn callback, void *callback_ctx)
Set the callback that notifies you when a new video frame is available, or if the video display confi...
mpv_opengl_cb_window_pos::height
int height
Definition: opengl_cb.h:169
mpv_opengl_cb_window_pos
Definition: opengl_cb.h:165
mpv_opengl_cb_drm_params::crtc_id
int crtc_id
Definition: opengl_cb.h:178
mpv_opengl_cb_init_gl
int mpv_opengl_cb_init_gl(mpv_opengl_cb_context *ctx, const char *exts, mpv_opengl_cb_get_proc_address_fn get_proc_address, void *get_proc_address_ctx)
Initialize the mpv OpenGL state.
mpv_opengl_cb_context
struct mpv_opengl_cb_context mpv_opengl_cb_context
nVidia/Linux via VDPAU requires GLX, which does not have this problem (the GLX API can return the cur...
Definition: opengl_cb.h:207
mpv_opengl_cb_get_proc_address_fn
void *(* mpv_opengl_cb_get_proc_address_fn)(void *fn_ctx, const char *name)
Definition: opengl_cb.h:210
mpv_opengl_cb_drm_params::atomic_request
struct _drmModeAtomicReq * atomic_request
Definition: opengl_cb.h:185
mpv_opengl_cb_drm_params::fd
int fd
Definition: opengl_cb.h:175
mpv_opengl_cb_window_pos::y
int y
Definition: opengl_cb.h:167
mpv_opengl_cb_report_flip
int mpv_opengl_cb_report_flip(mpv_opengl_cb_context *ctx, int64_t time)
Tell the renderer that a frame was flipped at the given time.
mpv_opengl_cb_uninit_gl
int mpv_opengl_cb_uninit_gl(mpv_opengl_cb_context *ctx)
Destroy the mpv OpenGL state.
mpv_opengl_cb_update_fn
void(* mpv_opengl_cb_update_fn)(void *cb_ctx)
Definition: opengl_cb.h:209
mpv_opengl_cb_render
int mpv_opengl_cb_render(mpv_opengl_cb_context *ctx, int fbo, int vp[4])
Deprecated.
mpv_opengl_cb_draw
int mpv_opengl_cb_draw(mpv_opengl_cb_context *ctx, int fbo, int w, int h)
Render video.
client.h
mpv_opengl_cb_window_pos::x
int x
Definition: opengl_cb.h:166
mpv_opengl_cb_drm_params::connector_id
int connector_id
Definition: opengl_cb.h:181
mpv_opengl_cb_drm_params
Definition: opengl_cb.h:173