vo_opengl: split backend code from common.c to context.c

Now common.c only contains the code for the function loader, while
context.c contains the backend loader/dispatcher.

Not calling it "backend.c", because the central struct is called
MPGLContext.
This commit is contained in:
wm4 2015-12-19 12:59:07 +01:00
parent 32cd85bc7e
commit 6154c1d06d
16 changed files with 345 additions and 275 deletions

View File

@ -54,6 +54,7 @@ SOURCES-$(ALSA) += audio/out/ao_alsa.c
SOURCES-$(CACA) += video/out/vo_caca.c
SOURCES-$(SDL2) += audio/out/ao_sdl.c video/out/vo_sdl.c
SOURCES-$(GL) += video/out/opengl/common.c \
video/out/opengl/context.c \
video/out/opengl/osd.c \
video/out/opengl/lcms.c \
video/out/opengl/video.c \

View File

@ -26,7 +26,7 @@
#include "common/common.h"
#include "video/out/w32_common.h"
#include "common.h"
#include "context.h"
struct priv {
EGLDisplay egl_display;

View File

@ -24,7 +24,7 @@
#include <dlfcn.h>
#include "video/out/cocoa_common.h"
#include "osdep/macosx_versions.h"
#include "common.h"
#include "context.h"
struct cgl_context {
CGLPixelFormatObj pix;

View File

@ -26,11 +26,6 @@
* version 2.1 of the License, or (at your option) any later version.
*/
/**
* \file gl_common.c
* \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
@ -38,11 +33,9 @@
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include "talloc.h"
#include "common.h"
#include "common/common.h"
#include "options/options.h"
#include "options/m_option.h"
// This guesses if the current GL context is a suspected software renderer.
static bool is_software_gl(GL *gl)
@ -534,190 +527,3 @@ void mpgl_load_functions(GL *gl, void *(*getProcAddress)(const GLubyte *),
{
mpgl_load_functions2(gl, get_procaddr_wrapper, getProcAddress, ext2, log);
}
extern const struct mpgl_driver mpgl_driver_x11;
extern const struct mpgl_driver mpgl_driver_x11egl;
extern const struct mpgl_driver mpgl_driver_x11_probe;
extern const struct mpgl_driver mpgl_driver_drm_egl;
extern const struct mpgl_driver mpgl_driver_cocoa;
extern const struct mpgl_driver mpgl_driver_wayland;
extern const struct mpgl_driver mpgl_driver_w32;
extern const struct mpgl_driver mpgl_driver_angle;
extern const struct mpgl_driver mpgl_driver_dxinterop;
extern const struct mpgl_driver mpgl_driver_rpi;
static const struct mpgl_driver *const backends[] = {
#if HAVE_RPI
&mpgl_driver_rpi,
#endif
#if HAVE_GL_COCOA
&mpgl_driver_cocoa,
#endif
#if HAVE_EGL_ANGLE
&mpgl_driver_angle,
#endif
#if HAVE_GL_WIN32
&mpgl_driver_w32,
#endif
#if HAVE_GL_DXINTEROP
&mpgl_driver_dxinterop,
#endif
#if HAVE_GL_WAYLAND
&mpgl_driver_wayland,
#endif
#if HAVE_GL_X11
&mpgl_driver_x11_probe,
#endif
#if HAVE_EGL_X11
&mpgl_driver_x11egl,
#endif
#if HAVE_GL_X11
&mpgl_driver_x11,
#endif
#if HAVE_EGL_DRM
&mpgl_driver_drm_egl,
#endif
};
int mpgl_find_backend(const char *name)
{
if (name == NULL || strcmp(name, "auto") == 0)
return -1;
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++) {
if (strcmp(backends[n]->name, name) == 0)
return n;
}
return -2;
}
int mpgl_validate_backend_opt(struct mp_log *log, const struct m_option *opt,
struct bstr name, struct bstr param)
{
if (bstr_equals0(param, "help")) {
mp_info(log, "OpenGL windowing backends:\n");
mp_info(log, " auto (autodetect)\n");
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++)
mp_info(log, " %s\n", backends[n]->name);
return M_OPT_EXIT - 1;
}
char s[20];
snprintf(s, sizeof(s), "%.*s", BSTR_P(param));
return mpgl_find_backend(s) >= -1 ? 1 : M_OPT_INVALID;
}
#if HAVE_C11_TLS
static _Thread_local MPGLContext *current_context;
static void * GLAPIENTRY get_native_display(const char *name)
{
if (current_context && current_context->native_display_type &&
name && strcmp(current_context->native_display_type, name) == 0)
return current_context->native_display;
return NULL;
}
static void set_current_context(MPGLContext *context)
{
current_context = context;
if (context && !context->gl->MPGetNativeDisplay)
context->gl->MPGetNativeDisplay = get_native_display;
}
#else
static void set_current_context(MPGLContext *context)
{
}
#endif
static MPGLContext *init_backend(struct vo *vo, const struct mpgl_driver *driver,
bool probing, int vo_flags)
{
MPGLContext *ctx = talloc_ptrtype(NULL, ctx);
*ctx = (MPGLContext) {
.gl = talloc_zero(ctx, GL),
.vo = vo,
.driver = driver,
};
bool old_probing = vo->probing;
vo->probing = probing; // hack; kill it once backends are separate
MP_VERBOSE(vo, "Initializing OpenGL backend '%s'\n", ctx->driver->name);
ctx->priv = talloc_zero_size(ctx, ctx->driver->priv_size);
if (ctx->driver->init(ctx, vo_flags) < 0) {
vo->probing = old_probing;
talloc_free(ctx);
return NULL;
}
vo->probing = old_probing;
if (!ctx->gl->version && !ctx->gl->es)
goto cleanup;
if (probing && ctx->gl->es && (vo_flags & VOFLAG_NO_GLES)) {
MP_VERBOSE(ctx->vo, "Skipping GLES backend.\n");
goto cleanup;
}
if (ctx->gl->mpgl_caps & MPGL_CAP_SW) {
MP_WARN(ctx->vo, "Suspected software renderer or indirect context.\n");
if (vo->probing && !(vo_flags & VOFLAG_SW))
goto cleanup;
}
ctx->gl->debug_context = !!(vo_flags & VOFLAG_GL_DEBUG);
set_current_context(ctx);
return ctx;
cleanup:
mpgl_uninit(ctx);
return NULL;
}
// Create a VO window and create a GL context on it.
// vo_flags: passed to the backend's create window function
MPGLContext *mpgl_init(struct vo *vo, const char *backend_name, int vo_flags)
{
MPGLContext *ctx = NULL;
int index = mpgl_find_backend(backend_name);
if (index == -1) {
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++) {
ctx = init_backend(vo, backends[n], true, vo_flags);
if (ctx)
break;
}
// VO forced, but no backend is ok => force the first that works at all
if (!ctx && !vo->probing) {
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++) {
ctx = init_backend(vo, backends[n], false, vo_flags);
if (ctx)
break;
}
}
} else if (index >= 0) {
ctx = init_backend(vo, backends[index], false, vo_flags);
}
return ctx;
}
int mpgl_reconfig_window(struct MPGLContext *ctx)
{
return ctx->driver->reconfig(ctx);
}
int mpgl_control(struct MPGLContext *ctx, int *events, int request, void *arg)
{
return ctx->driver->control(ctx, events, request, arg);
}
void mpgl_swap_buffers(struct MPGLContext *ctx)
{
ctx->driver->swap_buffers(ctx);
}
void mpgl_uninit(MPGLContext *ctx)
{
set_current_context(NULL);
if (ctx)
ctx->driver->uninit(ctx);
talloc_free(ctx);
}

View File

@ -75,76 +75,6 @@ enum {
#define MPGL_VER_P(ver) MPGL_VER_GET_MAJOR(ver), MPGL_VER_GET_MINOR(ver)
enum {
VOFLAG_GLES = 1 << 0, // Hint to create a GLES2 context
VOFLAG_NO_GLES = 1 << 1, // Hint to create a desktop GL context
VOFLAG_GL_DEBUG = 1 << 2, // Hint to request debug OpenGL context
VOFLAG_ALPHA = 1 << 3, // Hint to request alpha framebuffer
VOFLAG_SW = 1 << 4, // Hint to accept a software GL renderer
};
struct MPGLContext;
// A windowing backend (like X11, win32, ...), which provides OpenGL rendering.
struct mpgl_driver {
const char *name;
// Size of the struct allocated for MPGLContext.priv
int priv_size;
// Init the GL context and possibly the underlying VO backend.
// The created context should be compatible to GL 3.2 core profile, but
// some other GL versions are supported as well (e.g. GL 2.1 or GLES 2).
// Return 0 on success, negative value (-1) on error.
int (*init)(struct MPGLContext *ctx, int vo_flags);
// Resize the window, or create a new window if there isn't one yet.
// Currently, there is an unfortunate interaction with ctx->vo, and
// display size etc. are determined by it.
// Return 0 on success, negative value (-1) on error.
int (*reconfig)(struct MPGLContext *ctx);
// Present the frame.
void (*swap_buffers)(struct MPGLContext *ctx);
// This behaves exactly like vo_driver.control().
int (*control)(struct MPGLContext *ctx, int *events, int request, void *arg);
// Destroy the GL context and possibly the underlying VO backend.
void (*uninit)(struct MPGLContext *ctx);
};
typedef struct MPGLContext {
GL *gl;
struct vo *vo;
const struct mpgl_driver *driver;
// For hwdec_vaegl.c.
const char *native_display_type;
void *native_display;
// Windows-specific hack. See vo_opengl dwmflush suboption.
int dwm_flush_opt;
// Flip the rendered image vertically. This is useful for dxinterop.
bool flip_v;
// For free use by the mpgl_driver.
void *priv;
} MPGLContext;
MPGLContext *mpgl_init(struct vo *vo, const char *backend_name, int vo_flags);
void mpgl_uninit(MPGLContext *ctx);
int mpgl_reconfig_window(struct MPGLContext *ctx);
int mpgl_control(struct MPGLContext *ctx, int *events, int request, void *arg);
void mpgl_swap_buffers(struct MPGLContext *ctx);
int mpgl_find_backend(const char *name);
struct m_option;
int mpgl_validate_backend_opt(struct mp_log *log, const struct m_option *opt,
struct bstr name, struct bstr param);
void mpgl_load_functions(GL *gl, void *(*getProcAddress)(const GLubyte *),
const char *ext2, struct mp_log *log);
void mpgl_load_functions2(GL *gl, void *(*get_fn)(void *ctx, const char *n),

228
video/out/opengl/context.c Normal file
View File

@ -0,0 +1,228 @@
/*
* common OpenGL routines
*
* copyleft (C) 2005-2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
* Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
* gave me lots of good ideas.
*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*
* You can alternatively redistribute this file and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include "context.h"
#include "common/common.h"
#include "options/options.h"
#include "options/m_option.h"
extern const struct mpgl_driver mpgl_driver_x11;
extern const struct mpgl_driver mpgl_driver_x11egl;
extern const struct mpgl_driver mpgl_driver_x11_probe;
extern const struct mpgl_driver mpgl_driver_drm_egl;
extern const struct mpgl_driver mpgl_driver_cocoa;
extern const struct mpgl_driver mpgl_driver_wayland;
extern const struct mpgl_driver mpgl_driver_w32;
extern const struct mpgl_driver mpgl_driver_angle;
extern const struct mpgl_driver mpgl_driver_dxinterop;
extern const struct mpgl_driver mpgl_driver_rpi;
static const struct mpgl_driver *const backends[] = {
#if HAVE_RPI
&mpgl_driver_rpi,
#endif
#if HAVE_GL_COCOA
&mpgl_driver_cocoa,
#endif
#if HAVE_EGL_ANGLE
&mpgl_driver_angle,
#endif
#if HAVE_GL_WIN32
&mpgl_driver_w32,
#endif
#if HAVE_GL_DXINTEROP
&mpgl_driver_dxinterop,
#endif
#if HAVE_GL_WAYLAND
&mpgl_driver_wayland,
#endif
#if HAVE_GL_X11
&mpgl_driver_x11_probe,
#endif
#if HAVE_EGL_X11
&mpgl_driver_x11egl,
#endif
#if HAVE_GL_X11
&mpgl_driver_x11,
#endif
#if HAVE_EGL_DRM
&mpgl_driver_drm_egl,
#endif
};
int mpgl_find_backend(const char *name)
{
if (name == NULL || strcmp(name, "auto") == 0)
return -1;
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++) {
if (strcmp(backends[n]->name, name) == 0)
return n;
}
return -2;
}
int mpgl_validate_backend_opt(struct mp_log *log, const struct m_option *opt,
struct bstr name, struct bstr param)
{
if (bstr_equals0(param, "help")) {
mp_info(log, "OpenGL windowing backends:\n");
mp_info(log, " auto (autodetect)\n");
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++)
mp_info(log, " %s\n", backends[n]->name);
return M_OPT_EXIT - 1;
}
char s[20];
snprintf(s, sizeof(s), "%.*s", BSTR_P(param));
return mpgl_find_backend(s) >= -1 ? 1 : M_OPT_INVALID;
}
#if HAVE_C11_TLS
static _Thread_local MPGLContext *current_context;
static void * GLAPIENTRY get_native_display(const char *name)
{
if (current_context && current_context->native_display_type &&
name && strcmp(current_context->native_display_type, name) == 0)
return current_context->native_display;
return NULL;
}
static void set_current_context(MPGLContext *context)
{
current_context = context;
if (context && !context->gl->MPGetNativeDisplay)
context->gl->MPGetNativeDisplay = get_native_display;
}
#else
static void set_current_context(MPGLContext *context)
{
}
#endif
static MPGLContext *init_backend(struct vo *vo, const struct mpgl_driver *driver,
bool probing, int vo_flags)
{
MPGLContext *ctx = talloc_ptrtype(NULL, ctx);
*ctx = (MPGLContext) {
.gl = talloc_zero(ctx, GL),
.vo = vo,
.driver = driver,
};
bool old_probing = vo->probing;
vo->probing = probing; // hack; kill it once backends are separate
MP_VERBOSE(vo, "Initializing OpenGL backend '%s'\n", ctx->driver->name);
ctx->priv = talloc_zero_size(ctx, ctx->driver->priv_size);
if (ctx->driver->init(ctx, vo_flags) < 0) {
vo->probing = old_probing;
talloc_free(ctx);
return NULL;
}
vo->probing = old_probing;
if (!ctx->gl->version && !ctx->gl->es)
goto cleanup;
if (probing && ctx->gl->es && (vo_flags & VOFLAG_NO_GLES)) {
MP_VERBOSE(ctx->vo, "Skipping GLES backend.\n");
goto cleanup;
}
if (ctx->gl->mpgl_caps & MPGL_CAP_SW) {
MP_WARN(ctx->vo, "Suspected software renderer or indirect context.\n");
if (vo->probing && !(vo_flags & VOFLAG_SW))
goto cleanup;
}
ctx->gl->debug_context = !!(vo_flags & VOFLAG_GL_DEBUG);
set_current_context(ctx);
return ctx;
cleanup:
mpgl_uninit(ctx);
return NULL;
}
// Create a VO window and create a GL context on it.
// vo_flags: passed to the backend's create window function
MPGLContext *mpgl_init(struct vo *vo, const char *backend_name, int vo_flags)
{
MPGLContext *ctx = NULL;
int index = mpgl_find_backend(backend_name);
if (index == -1) {
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++) {
ctx = init_backend(vo, backends[n], true, vo_flags);
if (ctx)
break;
}
// VO forced, but no backend is ok => force the first that works at all
if (!ctx && !vo->probing) {
for (int n = 0; n < MP_ARRAY_SIZE(backends); n++) {
ctx = init_backend(vo, backends[n], false, vo_flags);
if (ctx)
break;
}
}
} else if (index >= 0) {
ctx = init_backend(vo, backends[index], false, vo_flags);
}
return ctx;
}
int mpgl_reconfig_window(struct MPGLContext *ctx)
{
return ctx->driver->reconfig(ctx);
}
int mpgl_control(struct MPGLContext *ctx, int *events, int request, void *arg)
{
return ctx->driver->control(ctx, events, request, arg);
}
void mpgl_swap_buffers(struct MPGLContext *ctx)
{
ctx->driver->swap_buffers(ctx);
}
void mpgl_uninit(MPGLContext *ctx)
{
set_current_context(NULL);
if (ctx)
ctx->driver->uninit(ctx);
talloc_free(ctx);
}

104
video/out/opengl/context.h Normal file
View File

@ -0,0 +1,104 @@
/*
* common OpenGL routines
*
* copyleft (C) 2005-2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
* Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
* gave me lots of good ideas.
*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*
* You can alternatively redistribute this file and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#ifndef MP_GL_CONTEXT_H_
#define MP_GL_CONTEXT_H_
#include "common.h"
enum {
VOFLAG_GLES = 1 << 0, // Hint to create a GLES2 context
VOFLAG_NO_GLES = 1 << 1, // Hint to create a desktop GL context
VOFLAG_GL_DEBUG = 1 << 2, // Hint to request debug OpenGL context
VOFLAG_ALPHA = 1 << 3, // Hint to request alpha framebuffer
VOFLAG_SW = 1 << 4, // Hint to accept a software GL renderer
};
struct MPGLContext;
// A windowing backend (like X11, win32, ...), which provides OpenGL rendering.
struct mpgl_driver {
const char *name;
// Size of the struct allocated for MPGLContext.priv
int priv_size;
// Init the GL context and possibly the underlying VO backend.
// The created context should be compatible to GL 3.2 core profile, but
// some other GL versions are supported as well (e.g. GL 2.1 or GLES 2).
// Return 0 on success, negative value (-1) on error.
int (*init)(struct MPGLContext *ctx, int vo_flags);
// Resize the window, or create a new window if there isn't one yet.
// Currently, there is an unfortunate interaction with ctx->vo, and
// display size etc. are determined by it.
// Return 0 on success, negative value (-1) on error.
int (*reconfig)(struct MPGLContext *ctx);
// Present the frame.
void (*swap_buffers)(struct MPGLContext *ctx);
// This behaves exactly like vo_driver.control().
int (*control)(struct MPGLContext *ctx, int *events, int request, void *arg);
// Destroy the GL context and possibly the underlying VO backend.
void (*uninit)(struct MPGLContext *ctx);
};
typedef struct MPGLContext {
GL *gl;
struct vo *vo;
const struct mpgl_driver *driver;
// For hwdec_vaegl.c.
const char *native_display_type;
void *native_display;
// Windows-specific hack. See vo_opengl dwmflush suboption.
int dwm_flush_opt;
// Flip the rendered image vertically. This is useful for dxinterop.
bool flip_v;
// For free use by the mpgl_driver.
void *priv;
} MPGLContext;
MPGLContext *mpgl_init(struct vo *vo, const char *backend_name, int vo_flags);
void mpgl_uninit(MPGLContext *ctx);
int mpgl_reconfig_window(struct MPGLContext *ctx);
int mpgl_control(struct MPGLContext *ctx, int *events, int request, void *arg);
void mpgl_swap_buffers(struct MPGLContext *ctx);
int mpgl_find_backend(const char *name);
struct m_option;
int mpgl_validate_backend_opt(struct mp_log *log, const struct m_option *opt,
struct bstr name, struct bstr param);
#endif

View File

@ -34,7 +34,7 @@
#include <EGL/eglext.h>
#include <GL/gl.h>
#include "common.h"
#include "context.h"
#include "common/common.h"
#include "video/out/drm_common.h"

View File

@ -25,7 +25,7 @@
#include <d3d9.h>
#include <dwmapi.h>
#include "video/out/w32_common.h"
#include "common.h"
#include "context.h"
// For WGL_ACCESS_WRITE_DISCARD_NV, etc.
#include <GL/wglext.h>

View File

@ -25,7 +25,7 @@
#include "common/common.h"
#include "video/out/x11_common.h"
#include "common.h"
#include "context.h"
#include "rpi.h"

View File

@ -25,7 +25,7 @@
#include <dwmapi.h>
#include "video/out/w32_common.h"
#include "video/out/win32/exclusive_hack.h"
#include "common.h"
#include "context.h"
struct w32_context {
int opt_swapinterval;

View File

@ -17,7 +17,7 @@
*/
#include "video/out/wayland_common.h"
#include "common.h"
#include "context.h"
static void egl_resize(struct vo_wayland_state *wl)
{

View File

@ -27,7 +27,7 @@
#include "header_fixes.h"
#include "video/out/x11_common.h"
#include "common.h"
#include "context.h"
struct glx_context {
XVisualInfo *vinfo;

View File

@ -28,7 +28,7 @@
#include "common/common.h"
#include "video/out/x11_common.h"
#include "common.h"
#include "context.h"
#include "egl_helpers.h"
struct priv {

View File

@ -43,7 +43,7 @@
#include "video/mp_image.h"
#include "sub/osd.h"
#include "opengl/common.h"
#include "opengl/context.h"
#include "opengl/utils.h"
#include "opengl/hwdec.h"
#include "opengl/osd.h"

View File

@ -313,6 +313,7 @@ def build(ctx):
( "video/out/filter_kernels.c" ),
( "video/out/opengl/cocoa.c", "gl-cocoa" ),
( "video/out/opengl/common.c", "gl" ),
( "video/out/opengl/context.c", "gl" ),
( "video/out/opengl/egl_helpers.c", "egl-helpers" ),
( "video/out/opengl/rpi.c", "rpi" ),
( "video/out/opengl/hwdec.c", "gl" ),