2013-03-01 15:55:08 +01:00
|
|
|
/*
|
2015-04-13 09:36:54 +02:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2013-03-01 15:55:08 +01: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.
|
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-03-01 15:55:08 +01: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
|
2015-04-13 09:36:54 +02:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-03-01 15:55:08 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-07-26 20:28:01 +02:00
|
|
|
#include <assert.h>
|
2013-03-01 15:55:08 +01:00
|
|
|
#include <windows.h>
|
2015-08-29 01:10:30 +02:00
|
|
|
#include "video/out/w32_common.h"
|
2015-08-29 04:12:56 +02:00
|
|
|
#include "common.h"
|
2013-03-01 15:55:08 +01:00
|
|
|
|
|
|
|
struct w32_context {
|
2015-10-02 17:49:22 +02:00
|
|
|
int opt_swapinterval;
|
|
|
|
int current_swapinterval;
|
|
|
|
|
|
|
|
int (GLAPIENTRY *real_wglSwapInterval)(int);
|
|
|
|
|
2013-03-01 15:55:08 +01:00
|
|
|
HGLRC context;
|
2014-07-26 20:28:01 +02:00
|
|
|
HDC hdc;
|
2014-08-05 19:40:57 +02:00
|
|
|
int flags;
|
2015-03-17 00:15:28 +01:00
|
|
|
|
|
|
|
HINSTANCE dwmapi_dll;
|
2015-10-02 17:49:22 +02:00
|
|
|
HRESULT (WINAPI *dwmflush)(void);
|
2013-03-01 15:55:08 +01:00
|
|
|
};
|
|
|
|
|
2015-10-02 18:09:13 +02:00
|
|
|
static void w32_uninit(MPGLContext *ctx);
|
|
|
|
|
2015-10-02 17:49:22 +02:00
|
|
|
static __thread struct w32_context *current_w32_context;
|
|
|
|
|
|
|
|
static int GLAPIENTRY w32_swap_interval(int interval)
|
|
|
|
{
|
|
|
|
if (current_w32_context)
|
|
|
|
current_w32_context->opt_swapinterval = interval;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-26 20:28:01 +02:00
|
|
|
static bool create_dc(struct MPGLContext *ctx, int flags)
|
|
|
|
{
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
HWND win = vo_w32_hwnd(ctx->vo);
|
|
|
|
|
2014-08-01 22:58:38 +02:00
|
|
|
if (w32_ctx->hdc)
|
|
|
|
return true;
|
2014-07-26 20:28:01 +02: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);
|
|
|
|
|
|
|
|
int pfmt = GetPixelFormat(hdc);
|
|
|
|
if (DescribePixelFormat(hdc, pfmt, sizeof(PIXELFORMATDESCRIPTOR), &pfd)) {
|
|
|
|
ctx->depth_r = pfd.cRedBits;
|
|
|
|
ctx->depth_g = pfd.cGreenBits;
|
|
|
|
ctx->depth_b = pfd.cBlueBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
w32_ctx->hdc = hdc;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-01 15:55:08 +01:00
|
|
|
static void *w32gpa(const GLubyte *procName)
|
|
|
|
{
|
|
|
|
HMODULE oglmod;
|
|
|
|
void *res = wglGetProcAddress(procName);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2015-08-01 20:51:52 +02:00
|
|
|
oglmod = GetModuleHandle(L"opengl32.dll");
|
2013-03-01 15:55:08 +01:00
|
|
|
return GetProcAddress(oglmod, procName);
|
|
|
|
}
|
|
|
|
|
2014-08-01 22:58:38 +02:00
|
|
|
static bool create_context_w32_old(struct MPGLContext *ctx)
|
2013-03-01 15:55:08 +01:00
|
|
|
{
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
|
2014-07-26 20:28:01 +02:00
|
|
|
HDC windc = w32_ctx->hdc;
|
2013-03-01 15:55:08 +01:00
|
|
|
bool res = false;
|
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
HGLRC context = wglCreateContext(windc);
|
|
|
|
if (!context) {
|
2013-09-11 21:39:50 +02:00
|
|
|
MP_FATAL(ctx->vo, "Could not create GL context!\n");
|
2014-08-05 19:26:04 +02:00
|
|
|
return res;
|
2013-03-01 15:55:08 +01:00
|
|
|
}
|
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
if (!wglMakeCurrent(windc, context)) {
|
2013-09-11 21:39:50 +02:00
|
|
|
MP_FATAL(ctx->vo, "Could not set GL context!\n");
|
2014-08-05 19:26:04 +02:00
|
|
|
wglDeleteContext(context);
|
|
|
|
return res;
|
2013-03-01 15:55:08 +01:00
|
|
|
}
|
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
w32_ctx->context = context;
|
2013-03-01 15:55:08 +01:00
|
|
|
|
2013-09-12 00:57:32 +02:00
|
|
|
mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log);
|
2014-08-05 19:26:04 +02:00
|
|
|
return true;
|
2013-03-01 15:55:08 +01:00
|
|
|
}
|
|
|
|
|
2014-08-01 22:58:38 +02:00
|
|
|
static bool create_context_w32_gl3(struct MPGLContext *ctx)
|
2013-03-01 15:55:08 +01:00
|
|
|
{
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
|
2014-07-26 20:28:01 +02:00
|
|
|
HDC windc = w32_ctx->hdc;
|
2014-08-05 19:26:04 +02:00
|
|
|
HGLRC context = 0;
|
2013-03-01 15:55:08 +01:00
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
// A legacy context is needed to get access to the new functions.
|
|
|
|
HGLRC legacy_context = wglCreateContext(windc);
|
|
|
|
if (!legacy_context) {
|
2013-09-11 21:39:50 +02:00
|
|
|
MP_FATAL(ctx->vo, "Could not create GL context!\n");
|
2013-03-01 15:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set context
|
2014-08-05 19:26:04 +02:00
|
|
|
if (!wglMakeCurrent(windc, legacy_context)) {
|
2013-09-11 21:39:50 +02:00
|
|
|
MP_FATAL(ctx->vo, "Could not set GL context!\n");
|
2013-03-01 15:55:08 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *(GLAPIENTRY *wglGetExtensionsStringARB)(HDC hdc)
|
|
|
|
= w32gpa((const GLubyte*)"wglGetExtensionsStringARB");
|
|
|
|
|
|
|
|
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)
|
|
|
|
= w32gpa((const GLubyte*)"wglCreateContextAttribsARB");
|
|
|
|
|
|
|
|
if (!wglCreateContextAttribsARB)
|
|
|
|
goto unsupported;
|
|
|
|
|
|
|
|
int attribs[] = {
|
2015-05-14 13:07:00 +02:00
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
|
2013-03-01 15:55:08 +01:00
|
|
|
WGL_CONTEXT_FLAGS_ARB, 0,
|
|
|
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
context = wglCreateContextAttribsARB(windc, 0, attribs);
|
|
|
|
if (!context) {
|
2013-03-01 15:55:08 +01: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 19:26:04 +02:00
|
|
|
context = wglCreateContextAttribsARB(windc, 0, attribs);
|
2013-03-01 15:55:08 +01:00
|
|
|
}
|
2014-08-05 19:26:04 +02:00
|
|
|
if (!context) {
|
2013-03-01 15:55:08 +01:00
|
|
|
int err = GetLastError();
|
2013-09-11 21:39:50 +02:00
|
|
|
MP_FATAL(ctx->vo, "Could not create an OpenGL 3.x context: error 0x%x\n", err);
|
2013-03-01 15:55:08 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
wglMakeCurrent(windc, NULL);
|
|
|
|
wglDeleteContext(legacy_context);
|
2013-03-01 15:55:08 +01:00
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
if (!wglMakeCurrent(windc, context)) {
|
2013-09-11 21:39:50 +02:00
|
|
|
MP_FATAL(ctx->vo, "Could not set GL3 context!\n");
|
2014-08-05 19:26:04 +02:00
|
|
|
wglDeleteContext(context);
|
2013-03-01 15:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-05 19:26:04 +02:00
|
|
|
w32_ctx->context = context;
|
|
|
|
|
2013-03-01 15:55:08 +01:00
|
|
|
/* update function pointers */
|
2013-09-12 00:57:32 +02:00
|
|
|
mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log);
|
2013-03-01 15:55:08 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
unsupported:
|
2014-08-01 22:58:38 +02:00
|
|
|
MP_ERR(ctx->vo, "The OpenGL driver does not support OpenGL 3.x \n");
|
2013-03-01 15:55:08 +01:00
|
|
|
out:
|
2014-08-05 19:26:04 +02:00
|
|
|
wglMakeCurrent(windc, NULL);
|
|
|
|
wglDeleteContext(legacy_context);
|
2013-03-01 15:55:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-05 19:40:57 +02:00
|
|
|
static void create_ctx(void *ptr)
|
|
|
|
{
|
|
|
|
struct MPGLContext *ctx = ptr;
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
|
|
|
|
if (!create_dc(ctx, w32_ctx->flags))
|
|
|
|
return;
|
|
|
|
|
2015-01-21 20:32:42 +01:00
|
|
|
create_context_w32_gl3(ctx);
|
2014-08-05 19:40:57 +02:00
|
|
|
if (!w32_ctx->context)
|
|
|
|
create_context_w32_old(ctx);
|
2015-03-17 00:15:28 +01:00
|
|
|
|
2015-08-01 20:51:52 +02:00
|
|
|
w32_ctx->dwmapi_dll = LoadLibrary(L"Dwmapi.dll");
|
2015-03-17 00:15:28 +01:00
|
|
|
if (w32_ctx->dwmapi_dll)
|
2015-10-02 17:49:22 +02:00
|
|
|
w32_ctx->dwmflush = (void *)GetProcAddress(w32_ctx->dwmapi_dll, "DwmFlush");
|
2015-03-17 00:15:28 +01:00
|
|
|
|
2014-08-05 19:40:57 +02:00
|
|
|
wglMakeCurrent(w32_ctx->hdc, NULL);
|
|
|
|
}
|
|
|
|
|
2015-10-02 18:09:13 +02:00
|
|
|
static int w32_init(struct MPGLContext *ctx, int flags)
|
2013-03-01 15:55:08 +01:00
|
|
|
{
|
2015-10-02 18:09:13 +02:00
|
|
|
if (!vo_w32_init(ctx->vo))
|
|
|
|
goto fail;
|
2013-03-01 15:55:08 +01:00
|
|
|
|
2015-10-02 18:09:13 +02:00
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
2014-08-05 19:26:04 +02:00
|
|
|
|
2014-08-05 19:40:57 +02:00
|
|
|
w32_ctx->flags = flags;
|
|
|
|
vo_w32_run_on_thread(ctx->vo, create_ctx, ctx);
|
2014-08-01 22:58:38 +02:00
|
|
|
|
2015-10-02 17:49:22 +02:00
|
|
|
if (!w32_ctx->context)
|
2015-10-02 18:09:13 +02:00
|
|
|
goto fail;
|
2015-10-02 17:49:22 +02:00
|
|
|
|
|
|
|
if (!ctx->gl->SwapInterval)
|
|
|
|
MP_VERBOSE(ctx->vo, "WGL_EXT_swap_control missing.");
|
|
|
|
w32_ctx->real_wglSwapInterval = ctx->gl->SwapInterval;
|
|
|
|
ctx->gl->SwapInterval = w32_swap_interval;
|
|
|
|
w32_ctx->current_swapinterval = -1;
|
|
|
|
|
|
|
|
current_w32_context = w32_ctx;
|
|
|
|
wglMakeCurrent(w32_ctx->hdc, w32_ctx->context);
|
2015-10-02 18:09:13 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
w32_uninit(ctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int w32_reconfig(struct MPGLContext *ctx, int flags)
|
|
|
|
{
|
|
|
|
vo_w32_config(ctx->vo, flags);
|
|
|
|
return 0;
|
2013-03-01 15:55:08 +01:00
|
|
|
}
|
|
|
|
|
2014-08-05 19:40:57 +02:00
|
|
|
static void destroy_gl(void *ptr)
|
2013-03-01 15:55:08 +01:00
|
|
|
{
|
2014-08-05 19:40:57 +02:00
|
|
|
struct MPGLContext *ctx = ptr;
|
2013-03-01 15:55:08 +01:00
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
2014-08-05 19:40:57 +02:00
|
|
|
if (w32_ctx->context)
|
2014-08-05 19:26:04 +02:00
|
|
|
wglDeleteContext(w32_ctx->context);
|
|
|
|
w32_ctx->context = 0;
|
2014-07-26 20:28:01 +02:00
|
|
|
if (w32_ctx->hdc)
|
|
|
|
ReleaseDC(vo_w32_hwnd(ctx->vo), w32_ctx->hdc);
|
|
|
|
w32_ctx->hdc = NULL;
|
2015-10-02 17:49:22 +02:00
|
|
|
current_w32_context = NULL;
|
2013-03-01 15:55:08 +01:00
|
|
|
}
|
|
|
|
|
2015-10-02 18:09:13 +02:00
|
|
|
static void w32_uninit(MPGLContext *ctx)
|
2014-08-05 19:40:57 +02:00
|
|
|
{
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
if (w32_ctx->context)
|
|
|
|
wglMakeCurrent(w32_ctx->hdc, 0);
|
|
|
|
vo_w32_run_on_thread(ctx->vo, destroy_gl, ctx);
|
2015-03-17 00:15:28 +01:00
|
|
|
|
|
|
|
if (w32_ctx->dwmapi_dll)
|
|
|
|
FreeLibrary(w32_ctx->dwmapi_dll);
|
|
|
|
w32_ctx->dwmapi_dll = NULL;
|
2015-10-02 18:09:13 +02:00
|
|
|
vo_w32_uninit(ctx->vo);
|
2014-08-05 19:40:57 +02:00
|
|
|
}
|
|
|
|
|
2015-10-02 18:09:13 +02:00
|
|
|
static void w32_swap_buffers(MPGLContext *ctx)
|
2013-03-01 15:55:08 +01:00
|
|
|
{
|
2014-07-26 20:28:01 +02:00
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
SwapBuffers(w32_ctx->hdc);
|
2013-03-01 15:55:08 +01:00
|
|
|
|
2015-10-02 17:49:22 +02:00
|
|
|
// default if we don't DwmFLush
|
|
|
|
int new_swapinterval = w32_ctx->opt_swapinterval;
|
2015-03-17 00:15:28 +01:00
|
|
|
|
2015-10-02 17:49:22 +02:00
|
|
|
if (w32_ctx->dwmflush && w32_ctx->dwmflush() == S_OK &&
|
|
|
|
((ctx->dwm_flush_opt == 1 && !ctx->vo->opts->fullscreen) ||
|
|
|
|
ctx->dwm_flush_opt == 2))
|
2015-03-17 00:15:28 +01:00
|
|
|
{
|
|
|
|
new_swapinterval = 0;
|
|
|
|
}
|
|
|
|
|
2015-10-02 17:49:22 +02:00
|
|
|
if (new_swapinterval != w32_ctx->current_swapinterval &&
|
|
|
|
w32_ctx->real_wglSwapInterval)
|
|
|
|
{
|
|
|
|
w32_ctx->real_wglSwapInterval(new_swapinterval);
|
|
|
|
MP_VERBOSE(ctx->vo, "set SwapInterval(%d)\n", new_swapinterval);
|
2015-03-17 00:15:28 +01:00
|
|
|
}
|
2015-10-02 17:49:22 +02:00
|
|
|
w32_ctx->current_swapinterval = new_swapinterval;
|
2015-03-17 00:15:28 +01:00
|
|
|
}
|
|
|
|
|
2015-10-02 18:09:13 +02:00
|
|
|
static int w32_control(MPGLContext *ctx, int *events, int request, void *arg)
|
2013-03-01 15:55:08 +01:00
|
|
|
{
|
2015-10-02 18:09:13 +02:00
|
|
|
return vo_w32_control(ctx->vo, events, request, arg);
|
2013-03-01 15:55:08 +01:00
|
|
|
}
|
2015-10-02 18:09:13 +02:00
|
|
|
|
|
|
|
const struct mpgl_driver mpgl_driver_w32 = {
|
|
|
|
.name = "w32",
|
|
|
|
.priv_size = sizeof(struct w32_context),
|
|
|
|
.init = w32_init,
|
|
|
|
.reconfig = w32_reconfig,
|
|
|
|
.swap_buffers = w32_swap_buffers,
|
|
|
|
.control = w32_control,
|
|
|
|
.uninit = w32_uninit,
|
|
|
|
};
|