Skip to content

Commit

Permalink
Restore old EGL init
Browse files Browse the repository at this point in the history
But with the EGL surface and window created before mpv
  • Loading branch information
GhostNaN committed Nov 20, 2022
1 parent 7eb7b70 commit e21891a
Showing 1 changed file with 74 additions and 75 deletions.
149 changes: 74 additions & 75 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,15 @@ static void render(struct display_output *output) {
{MPV_RENDER_PARAM_INVALID, NULL},
};

if (!eglMakeCurrent(egl_display, output->egl_surface, output->egl_surface, egl_context)) {
if (!eglMakeCurrent(egl_display, output->egl_surface, output->egl_surface, egl_context))
cflp_error("Failed to make output surface current 0x%X", eglGetError());
}
glViewport(0, 0, output->width * output->scale, output->height * output->scale);

// Render frame
int err = mpv_render_context_render(mpv_glcontext, render_params);
if (err < 0) {
cflp_error("Failed to render frame with mpv, %s", mpv_error_string(err));
}
int mpv_err = mpv_render_context_render(mpv_glcontext, render_params);
if (mpv_err < 0)
cflp_error("Failed to render frame with mpv, %s", mpv_error_string(mpv_err));

// Callback new frame
output->frame_callback = wl_surface_frame(output->surface);
Expand Down Expand Up @@ -580,84 +579,63 @@ static void init_mpv(const struct wl_state *state) {
mpv_render_context_set_update_callback(mpv_glcontext, render_update_callback, NULL);
}

static void init_egl(struct display_output *output) {

if (!output->egl_window)
output->egl_window = wl_egl_window_create(output->surface, output->width * output->scale, output->height * output->scale);
static void init_egl(struct wl_state *state) {
egl_display = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, state->display, NULL);
if (!eglInitialize(egl_display, NULL, NULL)) {
cflp_error("Failed to initialize EGL 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
}

// Init once
bool load_opengl = 0;
if (!egl_display) {
load_opengl = 1;
eglBindAPI(EGL_OPENGL_API);
const EGLint win_attrib[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_NONE
};

egl_display = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, output->state->display, NULL);
if (!eglInitialize(egl_display, NULL, NULL)) {
cflp_error("Failed to initialize EGL 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
}
EGLint config_len;
if (!eglChooseConfig(egl_display, win_attrib, &egl_config, 1, &config_len)) {
cflp_error("Failed to set EGL frame buffer config 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
}

eglBindAPI(EGL_OPENGL_API);
const EGLint win_attrib[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
// Check for OpenGL compatibility for creating egl context
static const struct { int major, minor; } gl_versions[] = {
{4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0},
{3, 3}, {3, 2}, {3, 1}, {3, 0},
{0, 0}
};
egl_context = NULL;
for (uint i = 0; gl_versions[i].major > 0; i++) {
const EGLint ctx_attrib[] = {
EGL_CONTEXT_MAJOR_VERSION, gl_versions[i].major,
EGL_CONTEXT_MINOR_VERSION, gl_versions[i].major,
EGL_NONE
};

EGLint config_len;
if (!eglChooseConfig(egl_display, win_attrib, &egl_config, 1, &config_len)) {
cflp_error("Failed to set EGL frame buffer config 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
}

// Check for OpenGL compatibility for creating egl context
static const struct { int major, minor; } gl_versions[] = {
{4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0},
{3, 3}, {3, 2}, {3, 1}, {3, 0},
{0, 0}
};
egl_context = NULL;
for (uint i = 0; gl_versions[i].major > 0; i++) {
const EGLint ctx_attrib[] = {
EGL_CONTEXT_MAJOR_VERSION, gl_versions[i].major,
EGL_CONTEXT_MINOR_VERSION, gl_versions[i].major,
EGL_NONE
};
egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, ctx_attrib);
if (egl_context) {
if (VERBOSE)
cflp_info("OpenGL %i.%i EGL context created", gl_versions[i].major, gl_versions[i].minor);
break;
}
}
if (!egl_context) {
cflp_error("Failed to create EGL context 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, ctx_attrib);
if (egl_context) {
if (VERBOSE)
cflp_info("OpenGL %i.%i EGL context created", gl_versions[i].major, gl_versions[i].minor);
break;
}
}
if (!egl_context) {
cflp_error("Failed to create EGL context 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
}

if (!output->egl_surface)
output->egl_surface = eglCreatePlatformWindowSurface(egl_display, egl_config, output->egl_window, NULL);
if (!output->egl_surface)
cflp_error("Failed to create EGL surface for %s 0x%X", output->name, eglGetError());

if (!eglMakeCurrent(egl_display, output->egl_surface, output->egl_surface, egl_context))
cflp_error("Failed to make output surface current 0x%X", eglGetError());

eglSwapInterval(egl_display, 0);

if (load_opengl) {
if (!gladLoadGLLoader((GLADloadproc)eglGetProcAddress)) {
cflp_error("Failed to load OpenGL 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
} else if (VERBOSE) {
cflp_success("EGL initialized");
}
if (!eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context)) {
cflp_error("Failed to make context current 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
}

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
if (!gladLoadGLLoader((GLADloadproc)eglGetProcAddress)) {
cflp_error("Failed to load OpenGL 0x%X", eglGetError());
exit_mpvpaper(EXIT_FAILURE);
}
}

static void destroy_display_output(struct display_output *output) {
Expand Down Expand Up @@ -690,17 +668,38 @@ static void layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *su

// Setup render loop
struct wl_state *state = output->state;
if (!egl_display) {
init_egl(state);
if (VERBOSE)
cflp_success("EGL initialized");
}

if (!output->egl_window) {
init_egl(output);
output->egl_window = wl_egl_window_create(output->surface, output->width * output->scale, output->height * output->scale);
output->egl_surface = eglCreatePlatformWindowSurface(egl_display, egl_config, output->egl_window, NULL);
if (!output->egl_surface) {
cflp_error("Failed to create EGL surface for %s 0x%X", output->name, eglGetError());
destroy_display_output(output);
return;
}

if (!eglMakeCurrent(egl_display, output->egl_surface, output->egl_surface, egl_context))
cflp_error("Failed to make output surface current 0x%X", eglGetError());
eglSwapInterval(egl_display, 0);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
} else {
wl_egl_window_resize(output->egl_window, output->width * output->scale, output->height * output->scale, 0, 0);
}

if (!mpv) {
init_mpv(state);
init_threads();
if (VERBOSE)
cflp_success("MPV initialized");
}

// Start render loop
render(output);
}

Expand Down

0 comments on commit e21891a

Please sign in to comment.