]> git.sesse.net Git - vlc/blob - modules/video_output/msw/glwin32.c
DirectDraw: fix memleaks introduced by 46b26be57
[vlc] / modules / video_output / msw / glwin32.c
1 /*****************************************************************************
2  * glwin32.c: Windows OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_vout_display.h>
30
31 #include <windows.h>
32 #include <ddraw.h>
33 #include <commctrl.h>
34
35 #undef GetSystemMetrics
36
37 #ifndef MONITOR_DEFAULTTONEAREST
38 #   define MONITOR_DEFAULTTONEAREST 2
39 #endif
40
41 #include "../opengl.h"
42 #include <GL/wglew.h>
43 #include "common.h"
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  Open (vlc_object_t *);
49 static void Close(vlc_object_t *);
50
51 vlc_module_begin()
52     set_category(CAT_VIDEO)
53     set_subcategory(SUBCAT_VIDEO_VOUT)
54     set_shortname("OpenGL")
55     set_description(N_("OpenGL video output"))
56     set_capability("vout display", 160)
57     add_shortcut("glwin32", "opengl")
58     set_callbacks(Open, Close)
59 vlc_module_end()
60
61 /*****************************************************************************
62  * Local prototypes.
63  *****************************************************************************/
64 static picture_pool_t *Pool  (vout_display_t *, unsigned);
65 static void           Prepare(vout_display_t *, picture_t *, subpicture_t *);
66 static void           Display(vout_display_t *, picture_t *, subpicture_t *);
67 static int            Control(vout_display_t *, int, va_list);
68 static void           Manage (vout_display_t *);
69
70 static void           Swap   (vlc_gl_t *);
71 static void          *OurGetProcAddress(vlc_gl_t *, const char *);
72
73 /**
74  * It creates an OpenGL vout display.
75  */
76 static int Open(vlc_object_t *object)
77 {
78     vout_display_t *vd = (vout_display_t *)object;
79     vout_display_sys_t *sys;
80
81     /* Allocate structure */
82     vd->sys = sys = calloc(1, sizeof(*sys));
83     if (!sys)
84         return VLC_ENOMEM;
85
86     /* */
87     if (CommonInit(vd))
88         goto error;
89
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     PIXELFORMATDESCRIPTOR pfd;
97     memset(&pfd, 0, sizeof(pfd));
98     pfd.nSize = sizeof(pfd);
99     pfd.nVersion = 1;
100     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
101     pfd.iPixelType = PFD_TYPE_RGBA;
102     pfd.cColorBits = 24;
103     pfd.cDepthBits = 16;
104     pfd.iLayerType = PFD_MAIN_PLANE;
105     SetPixelFormat(sys->hGLDC,
106                    ChoosePixelFormat(sys->hGLDC, &pfd), &pfd);
107
108     /* Create and enable the render context */
109     sys->hGLRC = wglCreateContext(sys->hGLDC);
110     wglMakeCurrent(sys->hGLDC, sys->hGLRC);
111
112     const char *extensions = (const char*)glGetString(GL_EXTENSIONS);
113 #ifdef WGL_EXT_swap_control
114     if (HasExtension(extensions, "WGL_EXT_swap_control")) {
115         PFNWGLSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
116         if (SwapIntervalEXT)
117             SwapIntervalEXT(1);
118     }
119 #endif
120
121     /* */
122     sys->gl.lock = NULL;
123     sys->gl.unlock = NULL;
124     sys->gl.swap = Swap;
125     sys->gl.getProcAddress = OurGetProcAddress;
126     sys->gl.sys = vd;
127
128     video_format_t fmt = vd->fmt;
129     const vlc_fourcc_t *subpicture_chromas;
130     sys->vgl = vout_display_opengl_New(&fmt, &subpicture_chromas, &sys->gl);
131     if (!sys->vgl)
132         goto error;
133
134     vout_display_info_t info = vd->info;
135     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    /* Setup vout_display now that everything is fine */
141     vd->fmt  = fmt;
142     vd->info = info;
143
144     vd->pool    = Pool;
145     vd->prepare = Prepare;
146     vd->display = Display;
147     vd->control = Control;
148     vd->manage  = Manage;
149
150     return VLC_SUCCESS;
151
152 error:
153     Close(object);
154     return VLC_EGENERIC;
155 }
156
157 /**
158  * It destroys an OpenGL vout display.
159  */
160 static void Close(vlc_object_t *object)
161 {
162     vout_display_t *vd = (vout_display_t *)object;
163     vout_display_sys_t *sys = vd->sys;
164
165     if (sys->vgl)
166         vout_display_opengl_Delete(sys->vgl);
167
168     if (sys->hGLDC && sys->hGLRC)
169         wglMakeCurrent(NULL, NULL);
170     if (sys->hGLRC)
171         wglDeleteContext(sys->hGLRC);
172     if (sys->hGLDC)
173         ReleaseDC(sys->hvideownd, sys->hGLDC);
174
175     CommonClean(vd);
176
177     free(sys);
178 }
179
180 /* */
181 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
182 {
183     vout_display_sys_t *sys = vd->sys;
184
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);
204     if (subpicture)
205         subpicture_Delete(subpicture);
206
207     CommonDisplay(vd);
208 }
209
210 static int Control(vout_display_t *vd, int query, va_list args)
211 {
212     switch (query) {
213     case VOUT_DISPLAY_GET_OPENGL: {
214         vlc_gl_t **gl = va_arg(args, vlc_gl_t **);
215         *gl = &vd->sys->gl;
216
217         CommonDisplay(vd);
218         return VLC_SUCCESS;
219     }
220     default:
221         return CommonControl(vd, query, args);
222     }
223 }
224
225 static void Manage (vout_display_t *vd)
226 {
227     vout_display_sys_t *sys = vd->sys;
228
229     CommonManage(vd);
230
231     const int width  = sys->rect_dest.right  - sys->rect_dest.left;
232     const int height = sys->rect_dest.bottom - sys->rect_dest.top;
233     glViewport(0, 0, width, height);
234 }
235
236 static void Swap(vlc_gl_t *gl)
237 {
238     vout_display_t *vd = gl->sys;
239
240     SwapBuffers(vd->sys->hGLDC);
241 }
242
243 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
244 {
245     VLC_UNUSED(gl);
246     return wglGetProcAddress(name);
247 }
248