]> git.sesse.net Git - vlc/blob - modules/video_output/msw/glwin32.c
b5647cfe78283c5e8ac09909a03f7acac50c7634
[vlc] / modules / video_output / msw / glwin32.c
1 /*****************************************************************************
2  * glwin32.c: Windows OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, 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 #include <multimon.h>
36 #undef GetSystemMetrics
37
38 #ifndef MONITOR_DEFAULTTONEAREST
39 #   define MONITOR_DEFAULTTONEAREST 2
40 #endif
41
42 #include "../opengl.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", 20)
57     add_shortcut("glwin32")
58     add_shortcut("opengl")
59     set_callbacks(Open, Close)
60 vlc_module_end()
61
62 /*****************************************************************************
63  * Local prototypes.
64  *****************************************************************************/
65 static picture_pool_t *Pool  (vout_display_t *, unsigned);
66 static void           Prepare(vout_display_t *, picture_t *);
67 static void           Display(vout_display_t *, picture_t *);
68 static int            Control(vout_display_t *, int, va_list);
69 static void           Manage (vout_display_t *);
70
71 static void           Swap   (vout_opengl_t *);
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     /* */
113     sys->gl.lock = NULL;
114     sys->gl.unlock = NULL;
115     sys->gl.swap = Swap;
116     sys->gl.sys = vd;
117
118     video_format_t fmt = vd->fmt;
119     if (vout_display_opengl_Init(&sys->vgl, &fmt, &sys->gl))
120         goto error;
121
122     vout_display_info_t info = vd->info;
123     info.has_double_click = true;
124     info.has_hide_mouse = false;
125     info.has_pictures_invalid = true;
126     info.has_event_thread = true;
127
128    /* Setup vout_display now that everything is fine */
129     vd->fmt  = fmt;
130     vd->info = info;
131
132     vd->pool    = Pool;
133     vd->prepare = Prepare;
134     vd->display = Display;
135     vd->control = Control;
136     vd->manage  = Manage;
137
138     return VLC_SUCCESS;
139
140 error:
141     Close(object);
142     return VLC_EGENERIC;
143 }
144
145 /**
146  * It destroys an OpenGL vout display.
147  */
148 static void Close(vlc_object_t *object)
149 {
150     vout_display_t *vd = (vout_display_t *)object;
151     vout_display_sys_t *sys = vd->sys;
152
153     if (sys->vgl.gl)
154         vout_display_opengl_Clean(&sys->vgl);
155
156     if (sys->hGLDC && sys->hGLRC)
157         wglMakeCurrent(NULL, NULL);
158     if (sys->hGLRC)
159         wglDeleteContext(sys->hGLRC);
160     if (sys->hGLDC)
161         ReleaseDC(sys->hvideownd, sys->hGLDC);
162
163     CommonClean(vd);
164
165     free(sys);
166 }
167
168 /* */
169 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
170 {
171     vout_display_sys_t *sys = vd->sys;
172     VLC_UNUSED(count);
173
174     if (!sys->pool)
175         sys->pool = vout_display_opengl_GetPool(&sys->vgl);
176     return sys->pool;
177 }
178
179 static void Prepare(vout_display_t *vd, picture_t *picture)
180 {
181     vout_display_sys_t *sys = vd->sys;
182
183     vout_display_opengl_Prepare(&sys->vgl, picture);
184 }
185
186 static void Display(vout_display_t *vd, picture_t *picture)
187 {
188     vout_display_sys_t *sys = vd->sys;
189
190     vout_display_opengl_Display(&sys->vgl, &vd->source);
191
192     picture_Release(picture);
193
194     CommonDisplay(vd);
195 }
196
197 static int Control(vout_display_t *vd, int query, va_list args)
198 {
199     switch (query) {
200     case VOUT_DISPLAY_GET_OPENGL: {
201         vout_opengl_t **gl = va_arg(args, vout_opengl_t **);
202         *gl = &vd->sys->gl;
203         return VLC_SUCCESS;
204     }
205     default:
206         return CommonControl(vd, query, args);
207     }
208 }
209
210 static void Manage (vout_display_t *vd)
211 {
212     vout_display_sys_t *sys = vd->sys;
213
214     CommonManage(vd);
215
216     const int width  = sys->rect_dest.right  - sys->rect_dest.left;
217     const int height = sys->rect_dest.bottom - sys->rect_dest.top;
218     glViewport(0, 0, width, height);
219 }
220
221 static void Swap(vout_opengl_t *gl)
222 {
223     vout_display_t *vd = gl->sys;
224
225     SwapBuffers(vd->sys->hGLDC);
226 }
227