glwin32.c 6.76 KB
Newer Older
1
2
3
/*****************************************************************************
 * glwin32.c: Windows OpenGL provider
 *****************************************************************************
4
 * Copyright (C) 2001-2009 the VideoLAN team
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
 *****************************************************************************/
23
24
25
26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

27
#include <vlc_common.h>
28
#include <vlc_plugin.h>
29
#include <vlc_vout_display.h>
30
31
32
33
34
35
36
37
38
39
40

#include <windows.h>
#include <ddraw.h>
#include <commctrl.h>

#undef GetSystemMetrics

#ifndef MONITOR_DEFAULTTONEAREST
#   define MONITOR_DEFAULTTONEAREST 2
#endif

41
#include "../opengl.h"
42
#include <GL/wglew.h>
43
#include "common.h"
44
45
46
47

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
48
49
50
51
52
53
54
55
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

vlc_module_begin()
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VOUT)
    set_shortname("OpenGL")
    set_description(N_("OpenGL video output"))
56
    set_capability("vout display", 160)
57
    add_shortcut("glwin32", "opengl")
58
59
    set_callbacks(Open, Close)
vlc_module_end()
60
61

/*****************************************************************************
62
 * Local prototypes.
63
 *****************************************************************************/
64
static picture_pool_t *Pool  (vout_display_t *, unsigned);
65
66
static void           Prepare(vout_display_t *, picture_t *, subpicture_t *);
static void           Display(vout_display_t *, picture_t *, subpicture_t *);
67
68
static int            Control(vout_display_t *, int, va_list);
static void           Manage (vout_display_t *);
69

70
static void           Swap   (vlc_gl_t *);
Laurent Aimar's avatar
Laurent Aimar committed
71
static void          *OurGetProcAddress(vlc_gl_t *, const char *);
72
73
74
75
76

/**
 * It creates an OpenGL vout display.
 */
static int Open(vlc_object_t *object)
77
{
78
79
    vout_display_t *vd = (vout_display_t *)object;
    vout_display_sys_t *sys;
80
81

    /* Allocate structure */
82
83
    vd->sys = sys = calloc(1, sizeof(*sys));
    if (!sys)
84
85
        return VLC_ENOMEM;

86
87
    /* */
    if (CommonInit(vd))
88
89
        goto error;

90
    EventThreadUpdateTitle(sys->event, VOUT_TITLE " (OpenGL output)");
91

92
93
    /* */
    sys->hGLDC = GetDC(sys->hvideownd);
94
95

    /* Set the pixel format for the DC */
96
97
98
    PIXELFORMATDESCRIPTOR pfd;
    memset(&pfd, 0, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
99
100
101
102
103
104
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
105
106
    SetPixelFormat(sys->hGLDC,
                   ChoosePixelFormat(sys->hGLDC, &pfd), &pfd);
107
108

    /* Create and enable the render context */
109
110
111
    sys->hGLRC = wglCreateContext(sys->hGLDC);
    wglMakeCurrent(sys->hGLDC, sys->hGLRC);

112
113
114
115
116
117
118
119
120
    const char *extensions = (const char*)glGetString(GL_EXTENSIONS);
#ifdef WGL_EXT_swap_control
    if (HasExtension(extensions, "WGL_EXT_swap_control")) {
        PFNWGLSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
        if (SwapIntervalEXT)
            SwapIntervalEXT(1);
    }
#endif

121
122
123
124
    /* */
    sys->gl.lock = NULL;
    sys->gl.unlock = NULL;
    sys->gl.swap = Swap;
Laurent Aimar's avatar
Laurent Aimar committed
125
    sys->gl.getProcAddress = OurGetProcAddress;
126
127
128
    sys->gl.sys = vd;

    video_format_t fmt = vd->fmt;
129
130
    const vlc_fourcc_t *subpicture_chromas;
    sys->vgl = vout_display_opengl_New(&fmt, &subpicture_chromas, &sys->gl);
131
    if (!sys->vgl)
132
133
134
135
        goto error;

    vout_display_info_t info = vd->info;
    info.has_double_click = true;
136
    info.has_hide_mouse = false;
137
    info.has_event_thread = true;
138
    info.subpicture_chromas = subpicture_chromas;
139
140
141
142
143

   /* Setup vout_display now that everything is fine */
    vd->fmt  = fmt;
    vd->info = info;

144
    vd->pool    = Pool;
145
146
147
148
    vd->prepare = Prepare;
    vd->display = Display;
    vd->control = Control;
    vd->manage  = Manage;
149
150

    return VLC_SUCCESS;
151
152
153
154

error:
    Close(object);
    return VLC_EGENERIC;
155
156
}

157
158
159
160
/**
 * It destroys an OpenGL vout display.
 */
static void Close(vlc_object_t *object)
161
{
162
163
164
    vout_display_t *vd = (vout_display_t *)object;
    vout_display_sys_t *sys = vd->sys;

165
166
    if (sys->vgl)
        vout_display_opengl_Delete(sys->vgl);
167
168
169
170
171
172
173
174
175
176
177

    if (sys->hGLDC && sys->hGLRC)
        wglMakeCurrent(NULL, NULL);
    if (sys->hGLRC)
        wglDeleteContext(sys->hGLRC);
    if (sys->hGLDC)
        ReleaseDC(sys->hvideownd, sys->hGLDC);

    CommonClean(vd);

    free(sys);
178
179
}

180
/* */
181
static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
182
{
183
184
    vout_display_sys_t *sys = vd->sys;

185
    if (!sys->pool)
186
        sys->pool = vout_display_opengl_GetPool(sys->vgl, count);
187
    return sys->pool;
188
}
189

190
static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
191
192
{
    vout_display_sys_t *sys = vd->sys;
193

194
    vout_display_opengl_Prepare(sys->vgl, picture, subpicture);
195
196
}

197
static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
198
{
199
    vout_display_sys_t *sys = vd->sys;
200

201
    vout_display_opengl_Display(sys->vgl, &vd->source);
202

203
    picture_Release(picture);
Laurent Aimar's avatar
Laurent Aimar committed
204
205
    if (subpicture)
        subpicture_Delete(subpicture);
206
207

    CommonDisplay(vd);
208
209
}

210
static int Control(vout_display_t *vd, int query, va_list args)
211
{
212
213
    switch (query) {
    case VOUT_DISPLAY_GET_OPENGL: {
214
        vlc_gl_t **gl = va_arg(args, vlc_gl_t **);
215
        *gl = &vd->sys->gl;
Laurent Aimar's avatar
Laurent Aimar committed
216
217

        CommonDisplay(vd);
218
219
220
221
222
        return VLC_SUCCESS;
    }
    default:
        return CommonControl(vd, query, args);
    }
223
}
224

225
226
227
228
229
static void Manage (vout_display_t *vd)
{
    vout_display_sys_t *sys = vd->sys;

    CommonManage(vd);
230

231
232
233
234
235
    const int width  = sys->rect_dest.right  - sys->rect_dest.left;
    const int height = sys->rect_dest.bottom - sys->rect_dest.top;
    glViewport(0, 0, width, height);
}

236
static void Swap(vlc_gl_t *gl)
237
{
238
239
240
    vout_display_t *vd = gl->sys;

    SwapBuffers(vd->sys->hGLDC);
241
}
242

Laurent Aimar's avatar
Laurent Aimar committed
243
static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
244
{
245
    VLC_UNUSED(gl);
246
247
248
    return wglGetProcAddress(name);
}