2013-03-01 14:55:08 +00:00
|
|
|
/*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* 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 18:28:01 +00:00
|
|
|
#include <assert.h>
|
2013-03-01 14:55:08 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include "w32_common.h"
|
|
|
|
#include "gl_common.h"
|
|
|
|
|
|
|
|
struct w32_context {
|
|
|
|
HGLRC context;
|
2014-07-26 18:28:01 +00:00
|
|
|
HDC hdc;
|
2014-08-05 17:40:57 +00:00
|
|
|
int flags;
|
2013-03-01 14:55:08 +00:00
|
|
|
};
|
|
|
|
|
2014-07-26 18:28:01 +00: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 20:58:38 +00:00
|
|
|
if (w32_ctx->hdc)
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (flags & VOFLAG_STEREO)
|
|
|
|
pfd.dwFlags |= PFD_STEREO;
|
|
|
|
|
|
|
|
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 14:55:08 +00:00
|
|
|
static void *w32gpa(const GLubyte *procName)
|
|
|
|
{
|
|
|
|
HMODULE oglmod;
|
|
|
|
void *res = wglGetProcAddress(procName);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
oglmod = GetModuleHandle("opengl32.dll");
|
|
|
|
return GetProcAddress(oglmod, procName);
|
|
|
|
}
|
|
|
|
|
2014-08-01 20:58:38 +00:00
|
|
|
static bool create_context_w32_old(struct MPGLContext *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
|
2014-07-26 18:28:01 +00:00
|
|
|
HDC windc = w32_ctx->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
|
|
|
}
|
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
w32_ctx->context = context;
|
2013-03-01 14:55:08 +00:00
|
|
|
|
2013-09-11 22:57:32 +00:00
|
|
|
mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log);
|
2014-08-05 17:26:04 +00:00
|
|
|
return true;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 20:58:38 +00:00
|
|
|
static bool create_context_w32_gl3(struct MPGLContext *ctx)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
|
2014-07-26 18:28:01 +00:00
|
|
|
HDC windc = w32_ctx->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)
|
|
|
|
= 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 gl_version = ctx->requested_gl_version;
|
|
|
|
int attribs[] = {
|
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, MPGL_VER_GET_MAJOR(gl_version),
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, MPGL_VER_GET_MINOR(gl_version),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
w32_ctx->context = context;
|
|
|
|
|
2013-03-01 14:55:08 +00:00
|
|
|
/* update function pointers */
|
2013-09-11 22:57:32 +00:00
|
|
|
mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log);
|
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)
|
|
|
|
{
|
|
|
|
struct MPGLContext *ctx = ptr;
|
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
|
|
|
|
if (!create_dc(ctx, w32_ctx->flags))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ctx->requested_gl_version >= MPGL_VER(3, 0))
|
|
|
|
create_context_w32_gl3(ctx);
|
|
|
|
if (!w32_ctx->context)
|
|
|
|
create_context_w32_old(ctx);
|
|
|
|
wglMakeCurrent(w32_ctx->hdc, NULL);
|
|
|
|
}
|
|
|
|
|
2014-05-07 19:34:05 +00:00
|
|
|
static bool config_window_w32(struct MPGLContext *ctx, int flags)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2014-08-05 17:26:04 +00:00
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
2014-05-06 21:01:19 +00:00
|
|
|
if (!vo_w32_config(ctx->vo, flags))
|
2013-03-01 14:55:08 +00:00
|
|
|
return false;
|
|
|
|
|
2014-08-05 17:26:04 +00:00
|
|
|
if (w32_ctx->context) // reuse existing context
|
|
|
|
return true;
|
|
|
|
|
2014-08-05 17:40:57 +00:00
|
|
|
w32_ctx->flags = flags;
|
|
|
|
vo_w32_run_on_thread(ctx->vo, create_ctx, ctx);
|
2014-08-01 20:58:38 +00:00
|
|
|
|
2014-08-05 17:40:57 +00:00
|
|
|
if (w32_ctx->context)
|
|
|
|
wglMakeCurrent(w32_ctx->hdc, w32_ctx->context);
|
|
|
|
return !!w32_ctx->context;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 17:40:57 +00:00
|
|
|
static void destroy_gl(void *ptr)
|
2013-03-01 14:55:08 +00:00
|
|
|
{
|
2014-08-05 17:40:57 +00:00
|
|
|
struct MPGLContext *ctx = ptr;
|
2013-03-01 14:55:08 +00:00
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
2014-08-05 17:40:57 +00:00
|
|
|
if (w32_ctx->context)
|
2014-08-05 17:26:04 +00:00
|
|
|
wglDeleteContext(w32_ctx->context);
|
|
|
|
w32_ctx->context = 0;
|
2014-07-26 18:28:01 +00:00
|
|
|
if (w32_ctx->hdc)
|
|
|
|
ReleaseDC(vo_w32_hwnd(ctx->vo), w32_ctx->hdc);
|
|
|
|
w32_ctx->hdc = NULL;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 17:40:57 +00:00
|
|
|
static void releaseGlContext_w32(MPGLContext *ctx)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-01 14:55:08 +00:00
|
|
|
static void swapGlBuffers_w32(MPGLContext *ctx)
|
|
|
|
{
|
2014-07-26 18:28:01 +00:00
|
|
|
struct w32_context *w32_ctx = ctx->priv;
|
|
|
|
SwapBuffers(w32_ctx->hdc);
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mpgl_set_backend_w32(MPGLContext *ctx)
|
|
|
|
{
|
|
|
|
ctx->priv = talloc_zero(ctx, struct w32_context);
|
|
|
|
ctx->config_window = config_window_w32;
|
|
|
|
ctx->releaseGlContext = releaseGlContext_w32;
|
|
|
|
ctx->swapGlBuffers = swapGlBuffers_w32;
|
|
|
|
ctx->vo_init = vo_w32_init;
|
|
|
|
ctx->vo_uninit = vo_w32_uninit;
|
2013-05-15 16:17:18 +00:00
|
|
|
ctx->vo_control = vo_w32_control;
|
2013-03-01 14:55:08 +00:00
|
|
|
}
|