1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-27 01:22:30 +00:00
mpv/video/out/gl_w32.c
wm4 9a3f1f24ca win32: don't use CS_OWNDC
CS_OWNDC will make GetDC() always return the same HDC. This might
become a problem when OpenGL rendering and window management are
on different threads. Although I'm not too sure about this; our
code never requests a HDC outside of the OpenGL backend, and it
depends on whether win32 will internally request DCs. But in any
case, this seems to be cleaner.

Move the GL pixelformat setup code to gl_w32.c, where it's actually
needed. This also fixes that SetPixelFormat() should be called only
once, and not every time video params change.
2014-07-26 20:28:01 +02:00

251 lines
6.9 KiB
C

/*
* 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.
*/
#include <assert.h>
#include <windows.h>
#include "w32_common.h"
#include "gl_common.h"
struct w32_context {
HGLRC context;
HDC hdc;
};
static bool create_dc(struct MPGLContext *ctx, int flags)
{
struct w32_context *w32_ctx = ctx->priv;
HWND win = vo_w32_hwnd(ctx->vo);
assert(!w32_ctx->hdc);
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;
}
static void *w32gpa(const GLubyte *procName)
{
HMODULE oglmod;
void *res = wglGetProcAddress(procName);
if (res)
return res;
oglmod = GetModuleHandle("opengl32.dll");
return GetProcAddress(oglmod, procName);
}
static bool create_context_w32_old(struct MPGLContext *ctx, int flags)
{
struct w32_context *w32_ctx = ctx->priv;
HGLRC *context = &w32_ctx->context;
if (*context)
return true;
if (!create_dc(ctx, flags))
return false;
HDC windc = w32_ctx->hdc;
bool res = false;
HGLRC new_context = wglCreateContext(windc);
if (!new_context) {
MP_FATAL(ctx->vo, "Could not create GL context!\n");
goto out;
}
if (!wglMakeCurrent(windc, new_context)) {
MP_FATAL(ctx->vo, "Could not set GL context!\n");
wglDeleteContext(new_context);
goto out;
}
*context = new_context;
mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log);
res = true;
out:
return res;
}
static bool create_context_w32_gl3(struct MPGLContext *ctx, int flags)
{
struct w32_context *w32_ctx = ctx->priv;
HGLRC *context = &w32_ctx->context;
if (*context) // reuse existing context
return true; // not reusing it breaks gl3!
if (!create_dc(ctx, flags))
return false;
HDC windc = w32_ctx->hdc;
HGLRC new_context = 0;
new_context = wglCreateContext(windc);
if (!new_context) {
MP_FATAL(ctx->vo, "Could not create GL context!\n");
return false;
}
// set context
if (!wglMakeCurrent(windc, new_context)) {
MP_FATAL(ctx->vo, "Could not set GL context!\n");
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
};
*context = wglCreateContextAttribsARB(windc, 0, attribs);
if (! *context) {
// 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;
*context = wglCreateContextAttribsARB(windc, 0, attribs);
}
if (! *context) {
int err = GetLastError();
MP_FATAL(ctx->vo, "Could not create an OpenGL 3.x context: error 0x%x\n", err);
goto out;
}
wglMakeCurrent(NULL, NULL);
wglDeleteContext(new_context);
if (!wglMakeCurrent(windc, *context)) {
MP_FATAL(ctx->vo, "Could not set GL3 context!\n");
wglDeleteContext(*context);
return false;
}
/* update function pointers */
mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log);
return true;
unsupported:
MP_ERR(ctx->vo, "The current OpenGL implementation does not support OpenGL 3.x \n");
out:
wglDeleteContext(new_context);
return false;
}
static bool config_window_w32(struct MPGLContext *ctx, int flags)
{
if (!vo_w32_config(ctx->vo, flags))
return false;
bool success = false;
if (ctx->requested_gl_version >= MPGL_VER(3, 0))
success = create_context_w32_gl3(ctx, flags);
if (!success)
success = create_context_w32_old(ctx, flags);
return success;
}
static void releaseGlContext_w32(MPGLContext *ctx)
{
struct w32_context *w32_ctx = ctx->priv;
HGLRC *context = &w32_ctx->context;
if (*context) {
wglMakeCurrent(0, 0);
wglDeleteContext(*context);
}
*context = 0;
if (w32_ctx->hdc)
ReleaseDC(vo_w32_hwnd(ctx->vo), w32_ctx->hdc);
w32_ctx->hdc = NULL;
}
static void swapGlBuffers_w32(MPGLContext *ctx)
{
struct w32_context *w32_ctx = ctx->priv;
SwapBuffers(w32_ctx->hdc);
}
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;
ctx->vo_control = vo_w32_control;
}