2013-03-01 14:55:08 +00:00
|
|
|
/*
|
2013-09-09 16:37:33 +00:00
|
|
|
* This file is part of mpv video player.
|
|
|
|
* Copyright © 2013 Alexander Preisinger <alexander.preisinger@gmail.com>
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2013-03-01 14:55:08 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2013-09-09 16:37:33 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-03-01 14:55:08 +00:00
|
|
|
* 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
|
2013-09-09 16:37:33 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-03-01 14:55:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wayland_common.h"
|
|
|
|
#include "gl_common.h"
|
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
static void egl_resize(struct vo_wayland_state *wl)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2013-08-23 09:29:09 +00:00
|
|
|
int32_t x = wl->window.sh_x;
|
|
|
|
int32_t y = wl->window.sh_y;
|
|
|
|
int32_t width = wl->window.sh_width;
|
|
|
|
int32_t height = wl->window.sh_height;
|
2013-05-07 18:51:09 +00:00
|
|
|
|
2014-07-23 22:33:29 +00:00
|
|
|
if (!wl->egl_context.egl_window)
|
|
|
|
return;
|
|
|
|
|
2013-08-23 09:29:09 +00:00
|
|
|
// get the real size of the window
|
|
|
|
// this improves moving the window while resizing it
|
2014-01-04 15:43:58 +00:00
|
|
|
wl_egl_window_get_attached_size(wl->egl_context.egl_window,
|
2013-07-18 15:35:28 +00:00
|
|
|
&wl->window.width,
|
|
|
|
&wl->window.height);
|
2013-05-07 18:51:09 +00:00
|
|
|
|
2013-09-12 14:36:52 +00:00
|
|
|
MP_VERBOSE(wl, "resizing %dx%d -> %dx%d\n", wl->window.width,
|
|
|
|
wl->window.height,
|
|
|
|
width,
|
|
|
|
height);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2013-08-23 09:29:09 +00:00
|
|
|
if (x != 0)
|
2013-07-18 15:35:28 +00:00
|
|
|
x = wl->window.width - width;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2013-08-23 09:29:09 +00:00
|
|
|
if (y != 0)
|
2013-07-18 15:35:28 +00:00
|
|
|
y = wl->window.height - height;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
wl_egl_window_resize(wl->egl_context.egl_window, width, height, x, y);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2013-07-18 15:35:28 +00:00
|
|
|
wl->window.width = width;
|
|
|
|
wl->window.height = height;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
|
|
|
/* set size for mplayer */
|
2013-08-23 09:29:09 +00:00
|
|
|
wl->vo->dwidth = wl->window.width;
|
|
|
|
wl->vo->dheight = wl->window.height;
|
|
|
|
|
|
|
|
wl->vo->want_redraw = true;
|
|
|
|
wl->window.events = 0;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool egl_create_context(struct vo_wayland_state *wl,
|
2013-04-02 19:01:07 +00:00
|
|
|
MPGLContext *ctx,
|
|
|
|
bool enable_alpha)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
|
|
|
EGLint major, minor, n;
|
|
|
|
|
|
|
|
GL *gl = ctx->gl;
|
|
|
|
const char *eglstr = "";
|
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
if (!(wl->egl_context.egl.dpy = eglGetDisplay(wl->display.display)))
|
2013-03-01 14:55:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
EGLint config_attribs[] = {
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
|
|
|
EGL_RED_SIZE, 1,
|
|
|
|
EGL_GREEN_SIZE, 1,
|
|
|
|
EGL_BLUE_SIZE, 1,
|
2013-04-02 19:01:07 +00:00
|
|
|
EGL_ALPHA_SIZE, enable_alpha,
|
2013-03-01 14:55:08 +00:00
|
|
|
EGL_DEPTH_SIZE, 1,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
/* major and minor here returns the supported EGL version (e.g.: 1.4) */
|
2014-01-04 15:43:58 +00:00
|
|
|
if (eglInitialize(wl->egl_context.egl.dpy, &major, &minor) != EGL_TRUE)
|
2013-03-01 14:55:08 +00:00
|
|
|
return false;
|
|
|
|
|
2013-09-12 14:36:52 +00:00
|
|
|
MP_VERBOSE(wl, "EGL version %d.%d\n", major, minor);
|
2013-07-31 17:19:44 +00:00
|
|
|
|
2013-03-01 14:55:08 +00:00
|
|
|
EGLint context_attribs[] = {
|
|
|
|
EGL_CONTEXT_MAJOR_VERSION_KHR,
|
|
|
|
MPGL_VER_GET_MAJOR(ctx->requested_gl_version),
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
if (eglBindAPI(EGL_OPENGL_API) != EGL_TRUE)
|
|
|
|
return false;
|
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
eglChooseConfig(wl->egl_context.egl.dpy, config_attribs,
|
|
|
|
&wl->egl_context.egl.conf, 1, &n);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
wl->egl_context.egl.ctx = eglCreateContext(wl->egl_context.egl.dpy,
|
|
|
|
wl->egl_context.egl.conf,
|
|
|
|
EGL_NO_CONTEXT,
|
|
|
|
context_attribs);
|
|
|
|
if (!wl->egl_context.egl.ctx) {
|
2013-07-19 11:30:23 +00:00
|
|
|
/* fallback to any GL version */
|
2014-01-04 14:33:52 +00:00
|
|
|
MP_WARN(wl, "can't create context for requested OpenGL version: "
|
2014-01-04 16:26:55 +00:00
|
|
|
"fall back to any version available\n");
|
2013-07-19 11:30:23 +00:00
|
|
|
context_attribs[0] = EGL_NONE;
|
2014-01-04 15:43:58 +00:00
|
|
|
wl->egl_context.egl.ctx = eglCreateContext(wl->egl_context.egl.dpy,
|
|
|
|
wl->egl_context.egl.conf,
|
|
|
|
EGL_NO_CONTEXT,
|
|
|
|
context_attribs);
|
2013-07-19 11:30:23 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
if (!wl->egl_context.egl.ctx)
|
2013-07-19 11:30:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
eglMakeCurrent(wl->egl_context.egl.dpy, NULL, NULL, wl->egl_context.egl.ctx);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
eglstr = eglQueryString(wl->egl_context.egl.dpy, EGL_EXTENSIONS);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2013-09-11 22:57:32 +00:00
|
|
|
mpgl_load_functions(gl, (void*(*)(const GLubyte*))eglGetProcAddress, eglstr,
|
2013-09-12 14:36:52 +00:00
|
|
|
wl->log);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:16:18 +00:00
|
|
|
static void egl_create_window(struct vo_wayland_state *wl)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2014-01-28 12:07:00 +00:00
|
|
|
wl->egl_context.egl_window = wl_egl_window_create(wl->window.video_surface,
|
2014-01-04 15:43:58 +00:00
|
|
|
wl->window.width,
|
|
|
|
wl->window.height);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
wl->egl_context.egl_surface = eglCreateWindowSurface(wl->egl_context.egl.dpy,
|
|
|
|
wl->egl_context.egl.conf,
|
|
|
|
wl->egl_context.egl_window,
|
|
|
|
NULL);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
eglMakeCurrent(wl->egl_context.egl.dpy,
|
|
|
|
wl->egl_context.egl_surface,
|
|
|
|
wl->egl_context.egl_surface,
|
|
|
|
wl->egl_context.egl.ctx);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2013-07-18 15:35:28 +00:00
|
|
|
wl_display_dispatch_pending(wl->display.display);
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 19:34:05 +00:00
|
|
|
static bool config_window_wayland(struct MPGLContext *ctx, int flags)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
|
|
|
struct vo_wayland_state * wl = ctx->vo->wayland;
|
2013-04-02 19:01:07 +00:00
|
|
|
bool enable_alpha = !!(flags & VOFLAG_ALPHA);
|
2013-03-01 14:55:08 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2014-05-06 21:16:18 +00:00
|
|
|
if (!vo_wayland_config(ctx->vo, flags))
|
2013-05-16 14:43:34 +00:00
|
|
|
return false;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
if (!wl->egl_context.egl.ctx) {
|
2013-03-01 14:55:08 +00:00
|
|
|
/* Create OpenGL context */
|
2014-01-04 15:43:58 +00:00
|
|
|
ret = egl_create_context(wl, ctx, enable_alpha);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
|
|
|
/* If successfully created the context and we don't want to hide the
|
|
|
|
* window than also create the window immediately */
|
|
|
|
if (ret && !(VOFLAG_HIDDEN & flags))
|
2014-05-06 21:16:18 +00:00
|
|
|
egl_create_window(wl);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else {
|
2014-01-04 15:43:58 +00:00
|
|
|
if (!wl->egl_context.egl_window) {
|
2013-03-01 14:55:08 +00:00
|
|
|
/* If the context exists and the hidden flag is unset then
|
|
|
|
* create the window */
|
|
|
|
if (!(VOFLAG_HIDDEN & flags))
|
2014-05-06 21:16:18 +00:00
|
|
|
egl_create_window(wl);
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void releaseGlContext_wayland(MPGLContext *ctx)
|
|
|
|
{
|
|
|
|
GL *gl = ctx->gl;
|
2014-01-04 15:43:58 +00:00
|
|
|
struct vo_wayland_state *wl = ctx->vo->wayland;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-08-30 08:17:51 +00:00
|
|
|
if (wl->egl_context.egl.ctx) {
|
|
|
|
gl->Finish();
|
|
|
|
eglReleaseThread();
|
|
|
|
wl_egl_window_destroy(wl->egl_context.egl_window);
|
|
|
|
eglDestroySurface(wl->egl_context.egl.dpy, wl->egl_context.egl_surface);
|
|
|
|
eglMakeCurrent(wl->egl_context.egl.dpy, NULL, NULL, EGL_NO_CONTEXT);
|
|
|
|
eglDestroyContext(wl->egl_context.egl.dpy, wl->egl_context.egl.ctx);
|
|
|
|
}
|
2014-01-04 15:43:58 +00:00
|
|
|
eglTerminate(wl->egl_context.egl.dpy);
|
|
|
|
wl->egl_context.egl.ctx = NULL;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 16:19:24 +00:00
|
|
|
static void swapGlBuffers_wayland(MPGLContext *ctx)
|
|
|
|
{
|
|
|
|
struct vo_wayland_state *wl = ctx->vo->wayland;
|
2014-01-04 20:04:11 +00:00
|
|
|
eglSwapBuffers(wl->egl_context.egl.dpy, wl->egl_context.egl_surface);
|
2014-01-04 16:19:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
static int control(struct vo *vo, int *events, int request, void *data)
|
|
|
|
{
|
|
|
|
struct vo_wayland_state *wl = vo->wayland;
|
|
|
|
int r = vo_wayland_control(vo, events, request, data);
|
2013-08-23 09:29:09 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
if (*events & VO_EVENT_RESIZE)
|
|
|
|
egl_resize(wl);
|
2013-08-23 09:29:09 +00:00
|
|
|
|
2014-01-04 15:43:58 +00:00
|
|
|
return r;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mpgl_set_backend_wayland(MPGLContext *ctx)
|
|
|
|
{
|
|
|
|
ctx->config_window = config_window_wayland;
|
|
|
|
ctx->releaseGlContext = releaseGlContext_wayland;
|
|
|
|
ctx->swapGlBuffers = swapGlBuffers_wayland;
|
2014-01-04 15:43:58 +00:00
|
|
|
ctx->vo_control = control;
|
2013-03-01 14:55:08 +00:00
|
|
|
ctx->vo_init = vo_wayland_init;
|
|
|
|
ctx->vo_uninit = vo_wayland_uninit;
|
|
|
|
}
|