2013-03-01 14:55:08 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2016-01-19 17:36:34 +00:00
|
|
|
* mpv is free software; you can redistribute it 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.
|
2013-03-01 14:55:08 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +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
|
2016-01-19 17:36:34 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-03-01 14:55:08 +00:00
|
|
|
*
|
2016-01-19 17:36:34 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-03-01 14:55:08 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-26 18:28:01 +00:00
|
|
|
#include <assert.h>
|
2013-03-01 14:55:08 +00:00
|
|
|
#include <windows.h>
|
2015-11-01 20:49:56 +00:00
|
|
|
#include <dwmapi.h>
|
2017-01-20 13:02:17 +00:00
|
|
|
|
|
|
|
#include "options/m_config.h"
|
2015-08-28 23:10:30 +00:00
|
|
|
#include "video/out/w32_common.h"
|
2015-12-19 11:59:07 +00:00
|
|
|
#include "context.h"
|
2017-09-15 15:37:28 +00:00
|
|
|
#include "utils.h"
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2017-04-07 12:30:51 +00:00
|
|
|
#if !defined(WGL_CONTEXT_MAJOR_VERSION_ARB)
|
|
|
|
/* these are supposed to be defined in wingdi.h but mingw's is too old */
|
|
|
|
/* only the bits actually used by mplayer are defined */
|
|
|
|
/* reference: http://www.opengl.org/registry/specs/ARB/wgl_create_context.txt */
|
|
|
|
|
|
|
|
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
|
|
|
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
|
|
|
|
#define WGL_CONTEXT_FLAGS_ARB 0x2094
|
|
|
|
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
|
|
|
|
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
|
|
|
|
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
|
|
|
|
#endif
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
struct priv {
|
|
|
|
GL gl;
|
|
|
|
|
2015-10-02 15:49:22 +00:00
|
|
|
int opt_swapinterval;
|
|
|
|
int current_swapinterval;
|
|
|
|
|
|
|
|
int (GLAPIENTRY *real_wglSwapInterval)(int);
|
|
|
|
|
2013-03-01 14:55:08 +00:00
|
|
|
HGLRC context;
|
2014-07-26 18:28:01 +00:00
|
|
|
HDC hdc;
|
2013-03-01 14:55:08 +00:00
|
|
|
};
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static void wgl_uninit(struct ra_ctx *ctx);
|
2015-10-02 16:09:13 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static __thread struct priv *current_wgl_context;
|
2015-10-02 15:49:22 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static int GLAPIENTRY wgl_swap_interval(int interval)
|
2015-10-02 15:49:22 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
if (current_wgl_context)
|
|
|
|
current_wgl_context->opt_swapinterval = interval;
|
2015-10-02 15:49:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static bool create_dc(struct ra_ctx *ctx)
|
2014-07-26 18:28:01 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2014-07-26 18:28:01 +00:00
|
|
|
HWND win = vo_w32_hwnd(ctx->vo);
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
if (p->hdc)
|
2014-08-01 20:58:38 +00:00
|
|
|
return true;
|
2014-07-26 18:28:01 +00:00
|
|
|
|
|
|
|
HDC hdc = GetDC(win);
|
|
|
|
if (!hdc)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PIXELFORMATDESCRIPTOR pfd;
|
|
|
|
memset(&pfd, 0, sizeof pfd);
|
|
|
|
pfd.nSize = sizeof pfd;
|
|
|
|
pfd.nVersion = 1;
|
|
|
|
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
|
|
|
|
|
|
|
pfd.iPixelType = PFD_TYPE_RGBA;
|
|
|
|
pfd.cColorBits = 24;
|
|
|
|
pfd.iLayerType = PFD_MAIN_PLANE;
|
|
|
|
int pf = ChoosePixelFormat(hdc, &pfd);
|
|
|
|
|
|
|
|
if (!pf) {
|
|
|
|
MP_ERR(ctx->vo, "unable to select a valid pixel format!\n");
|
|
|
|
ReleaseDC(win, hdc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetPixelFormat(hdc, pf, &pfd);
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
p->hdc = hdc;
|
2014-07-26 18:28:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static void *wglgpa(const GLubyte *procName)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
|
|
|
HMODULE oglmod;
|
|
|
|
void *res = wglGetProcAddress(procName);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2015-08-01 18:51:52 +00:00
|
|
|
oglmod = GetModuleHandle(L"opengl32.dll");
|
2013-03-01 14:55:08 +00:00
|
|
|
return GetProcAddress(oglmod, procName);
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static bool create_context_wgl_old(struct ra_ctx *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
HDC windc = p->hdc;
|
2013-03-01 14:55:08 +00:00
|
|
|
bool res = false;
|
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
HGLRC context = wglCreateContext(windc);
|
|
|
|
if (!context) {
|
2013-09-11 19:39:50 +00:00
|
|
|
MP_FATAL(ctx->vo, "Could not create GL context!\n");
|
2014-08-05 17:26:04 +00:00
|
|
|
return res;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
if (!wglMakeCurrent(windc, context)) {
|
2013-09-11 19:39:50 +00:00
|
|
|
MP_FATAL(ctx->vo, "Could not set GL context!\n");
|
2014-08-05 17:26:04 +00:00
|
|
|
wglDeleteContext(context);
|
|
|
|
return res;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
p->context = context;
|
2014-08-05 17:26:04 +00:00
|
|
|
return true;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static bool create_context_wgl_gl3(struct ra_ctx *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
struct priv *p = ctx->priv;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
HDC windc = p->hdc;
|
2014-08-05 17:26:04 +00:00
|
|
|
HGLRC context = 0;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
// A legacy context is needed to get access to the new functions.
|
|
|
|
HGLRC legacy_context = wglCreateContext(windc);
|
|
|
|
if (!legacy_context) {
|
2013-09-11 19:39:50 +00:00
|
|
|
MP_FATAL(ctx->vo, "Could not create GL context!\n");
|
2013-03-01 14:55:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set context
|
2014-08-05 17:26:04 +00:00
|
|
|
if (!wglMakeCurrent(windc, legacy_context)) {
|
2013-09-11 19:39:50 +00:00
|
|
|
MP_FATAL(ctx->vo, "Could not set GL context!\n");
|
2013-03-01 14:55:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *(GLAPIENTRY *wglGetExtensionsStringARB)(HDC hdc)
|
2017-09-15 15:37:28 +00:00
|
|
|
= wglgpa((const GLubyte*)"wglGetExtensionsStringARB");
|
2013-03-01 14:55:08 +00:00
|
|
|
|
|
|
|
if (!wglGetExtensionsStringARB)
|
|
|
|
goto unsupported;
|
|
|
|
|
|
|
|
const char *wgl_exts = wglGetExtensionsStringARB(windc);
|
|
|
|
if (!strstr(wgl_exts, "WGL_ARB_create_context"))
|
|
|
|
goto unsupported;
|
|
|
|
|
|
|
|
HGLRC (GLAPIENTRY *wglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext,
|
|
|
|
const int *attribList)
|
2017-09-15 15:37:28 +00:00
|
|
|
= wglgpa((const GLubyte*)"wglCreateContextAttribsARB");
|
2013-03-01 14:55:08 +00:00
|
|
|
|
|
|
|
if (!wglCreateContextAttribsARB)
|
|
|
|
goto unsupported;
|
|
|
|
|
|
|
|
int attribs[] = {
|
2015-05-14 11:07:00 +00:00
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
|
2013-03-01 14:55:08 +00:00
|
|
|
WGL_CONTEXT_FLAGS_ARB, 0,
|
|
|
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
context = wglCreateContextAttribsARB(windc, 0, attribs);
|
|
|
|
if (!context) {
|
2013-03-01 14:55:08 +00:00
|
|
|
// NVidia, instead of ignoring WGL_CONTEXT_FLAGS_ARB, will error out if
|
|
|
|
// it's present on pre-3.2 contexts.
|
|
|
|
// Remove it from attribs and retry the context creation.
|
|
|
|
attribs[6] = attribs[7] = 0;
|
2014-08-05 17:26:04 +00:00
|
|
|
context = wglCreateContextAttribsARB(windc, 0, attribs);
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
2014-08-05 17:26:04 +00:00
|
|
|
if (!context) {
|
2013-03-01 14:55:08 +00:00
|
|
|
int err = GetLastError();
|
2013-09-11 19:39:50 +00:00
|
|
|
MP_FATAL(ctx->vo, "Could not create an OpenGL 3.x context: error 0x%x\n", err);
|
2013-03-01 14:55:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
wglMakeCurrent(windc, NULL);
|
|
|
|
wglDeleteContext(legacy_context);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
if (!wglMakeCurrent(windc, context)) {
|
2013-09-11 19:39:50 +00:00
|
|
|
MP_FATAL(ctx->vo, "Could not set GL3 context!\n");
|
2014-08-05 17:26:04 +00:00
|
|
|
wglDeleteContext(context);
|
2013-03-01 14:55:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
p->context = context;
|
2013-03-01 14:55:08 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
unsupported:
|
2014-08-01 20:58:38 +00:00
|
|
|
MP_ERR(ctx->vo, "The OpenGL driver does not support OpenGL 3.x \n");
|
2013-03-01 14:55:08 +00:00
|
|
|
out:
|
2014-08-05 17:26:04 +00:00
|
|
|
wglMakeCurrent(windc, NULL);
|
|
|
|
wglDeleteContext(legacy_context);
|
2013-03-01 14:55:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-05 17:40:57 +00:00
|
|
|
static void create_ctx(void *ptr)
|
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
struct ra_ctx *ctx = ptr;
|
|
|
|
struct priv *p = ctx->priv;
|
2014-08-05 17:40:57 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
if (!create_dc(ctx))
|
2014-08-05 17:40:57 +00:00
|
|
|
return;
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
create_context_wgl_gl3(ctx);
|
|
|
|
if (!p->context)
|
|
|
|
create_context_wgl_old(ctx);
|
2015-03-16 23:15:28 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
wglMakeCurrent(p->hdc, NULL);
|
2014-08-05 17:40:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static bool compositor_active(struct ra_ctx *ctx)
|
2015-11-01 19:43:46 +00:00
|
|
|
{
|
|
|
|
// For Windows 7.
|
|
|
|
BOOL enabled = 0;
|
2015-12-04 15:26:00 +00:00
|
|
|
if (FAILED(DwmIsCompositionEnabled(&enabled)) || !enabled)
|
2015-11-01 19:43:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// This works at least on Windows 8.1: it returns an error in fullscreen,
|
|
|
|
// which is also when we get consistent timings without DwmFlush. Might
|
|
|
|
// be cargo-cult.
|
|
|
|
DWM_TIMING_INFO info = { .cbSize = sizeof(DWM_TIMING_INFO) };
|
2015-12-04 15:26:00 +00:00
|
|
|
if (FAILED(DwmGetCompositionTimingInfo(0, &info)))
|
2015-11-01 19:43:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static void wgl_swap_buffers(struct ra_ctx *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
SwapBuffers(p->hdc);
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2015-10-02 15:49:22 +00:00
|
|
|
// default if we don't DwmFLush
|
2017-09-15 15:37:28 +00:00
|
|
|
int new_swapinterval = p->opt_swapinterval;
|
2015-03-16 23:15:28 +00:00
|
|
|
|
2017-01-20 13:02:17 +00:00
|
|
|
int dwm_flush_opt;
|
|
|
|
mp_read_option_raw(ctx->global, "opengl-dwmflush", &m_option_type_choice,
|
|
|
|
&dwm_flush_opt);
|
|
|
|
|
|
|
|
if (dwm_flush_opt >= 0) {
|
|
|
|
if ((dwm_flush_opt == 1 && !ctx->vo->opts->fullscreen) ||
|
|
|
|
(dwm_flush_opt == 2) ||
|
|
|
|
(dwm_flush_opt == 0 && compositor_active(ctx)))
|
2015-11-01 19:43:46 +00:00
|
|
|
{
|
2015-12-04 15:26:00 +00:00
|
|
|
if (DwmFlush() == S_OK)
|
2015-11-01 19:43:46 +00:00
|
|
|
new_swapinterval = 0;
|
|
|
|
}
|
2015-03-16 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
if (new_swapinterval != p->current_swapinterval &&
|
|
|
|
p->real_wglSwapInterval)
|
2015-10-02 15:49:22 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
p->real_wglSwapInterval(new_swapinterval);
|
2015-10-02 15:49:22 +00:00
|
|
|
MP_VERBOSE(ctx->vo, "set SwapInterval(%d)\n", new_swapinterval);
|
2015-03-16 23:15:28 +00:00
|
|
|
}
|
2017-09-15 15:37:28 +00:00
|
|
|
p->current_swapinterval = new_swapinterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool wgl_init(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct priv *p = ctx->priv = talloc_zero(ctx, struct priv);
|
|
|
|
GL *gl = &p->gl;
|
|
|
|
|
|
|
|
if (!vo_w32_init(ctx->vo))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
vo_w32_run_on_thread(ctx->vo, create_ctx, ctx);
|
|
|
|
if (!p->context)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
current_wgl_context = p;
|
|
|
|
wglMakeCurrent(p->hdc, p->context);
|
|
|
|
|
|
|
|
mpgl_load_functions(gl, wglgpa, NULL, ctx->vo->log);
|
|
|
|
|
|
|
|
if (!gl->SwapInterval)
|
|
|
|
MP_VERBOSE(ctx->vo, "WGL_EXT_swap_control missing.\n");
|
|
|
|
p->real_wglSwapInterval = gl->SwapInterval;
|
|
|
|
gl->SwapInterval = wgl_swap_interval;
|
|
|
|
p->current_swapinterval = -1;
|
|
|
|
|
|
|
|
struct ra_gl_ctx_params params = {
|
|
|
|
.swap_buffers = wgl_swap_buffers,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ra_gl_ctx_init(ctx, gl, params))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
DwmEnableMMCSS(TRUE);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
wgl_uninit(ctx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resize(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
ra_gl_ctx_resize(ctx->swapchain, ctx->vo->dwidth, ctx->vo->dheight, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool wgl_reconfig(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
vo_w32_config(ctx->vo);
|
|
|
|
resize(ctx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_gl(void *ptr)
|
|
|
|
{
|
|
|
|
struct ra_ctx *ctx = ptr;
|
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
if (p->context)
|
|
|
|
wglDeleteContext(p->context);
|
|
|
|
p->context = 0;
|
|
|
|
if (p->hdc)
|
|
|
|
ReleaseDC(vo_w32_hwnd(ctx->vo), p->hdc);
|
|
|
|
p->hdc = NULL;
|
|
|
|
current_wgl_context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wgl_uninit(struct ra_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct priv *p = ctx->priv;
|
|
|
|
ra_gl_ctx_uninit(ctx);
|
|
|
|
if (p->context)
|
|
|
|
wglMakeCurrent(p->hdc, 0);
|
|
|
|
vo_w32_run_on_thread(ctx->vo, destroy_gl, ctx);
|
|
|
|
|
|
|
|
DwmEnableMMCSS(FALSE);
|
|
|
|
vo_w32_uninit(ctx->vo);
|
2015-03-16 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
static int wgl_control(struct ra_ctx *ctx, int *events, int request, void *arg)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2017-09-15 15:37:28 +00:00
|
|
|
int ret = vo_w32_control(ctx->vo, events, request, arg);
|
|
|
|
if (*events & VO_EVENT_RESIZE)
|
|
|
|
resize(ctx);
|
|
|
|
return ret;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
2015-10-02 16:09:13 +00:00
|
|
|
|
2017-09-15 15:37:28 +00:00
|
|
|
const struct ra_ctx_fns ra_ctx_wgl = {
|
|
|
|
.type = "opengl",
|
2015-12-19 07:39:14 +00:00
|
|
|
.name = "win",
|
2017-09-15 15:37:28 +00:00
|
|
|
.init = wgl_init,
|
|
|
|
.reconfig = wgl_reconfig,
|
|
|
|
.control = wgl_control,
|
|
|
|
.uninit = wgl_uninit,
|
2015-10-02 16:09:13 +00:00
|
|
|
};
|