]> git.sesse.net Git - vlc/blob - modules/video_output/msw/glwin32.c
Made vout_display_opengl_t private.
[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 #undef GetSystemMetrics
36
37 #ifndef MONITOR_DEFAULTTONEAREST
38 #   define MONITOR_DEFAULTTONEAREST 2
39 #endif
40
41 #include "../opengl.h"
42 #include "common.h"
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open (vlc_object_t *);
48 static void Close(vlc_object_t *);
49
50 vlc_module_begin()
51     set_category(CAT_VIDEO)
52     set_subcategory(SUBCAT_VIDEO_VOUT)
53     set_shortname("OpenGL")
54     set_description(N_("OpenGL video output"))
55     set_capability("vout display", 160)
56     add_shortcut("glwin32", "opengl")
57     set_callbacks(Open, Close)
58 vlc_module_end()
59
60 /*****************************************************************************
61  * Local prototypes.
62  *****************************************************************************/
63 static picture_pool_t *Pool  (vout_display_t *, unsigned);
64 static void           Prepare(vout_display_t *, picture_t *, subpicture_t *);
65 static void           Display(vout_display_t *, picture_t *, subpicture_t *);
66 static int            Control(vout_display_t *, int, va_list);
67 static void           Manage (vout_display_t *);
68
69 static void           Swap   (vlc_gl_t *);
70 static void          *GetProcAddress(vlc_gl_t *, const char *);
71
72 /**
73  * It creates an OpenGL vout display.
74  */
75 static int Open(vlc_object_t *object)
76 {
77     vout_display_t *vd = (vout_display_t *)object;
78     vout_display_sys_t *sys;
79
80     /* Allocate structure */
81     vd->sys = sys = calloc(1, sizeof(*sys));
82     if (!sys)
83         return VLC_ENOMEM;
84
85     /* */
86     if (CommonInit(vd))
87         goto error;
88
89     EventThreadUpdateTitle(sys->event, VOUT_TITLE " (OpenGL output)");
90
91     /* */
92     sys->hGLDC = GetDC(sys->hvideownd);
93
94     /* Set the pixel format for the DC */
95     PIXELFORMATDESCRIPTOR pfd;
96     memset(&pfd, 0, sizeof(pfd));
97     pfd.nSize = sizeof(pfd);
98     pfd.nVersion = 1;
99     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
100     pfd.iPixelType = PFD_TYPE_RGBA;
101     pfd.cColorBits = 24;
102     pfd.cDepthBits = 16;
103     pfd.iLayerType = PFD_MAIN_PLANE;
104     SetPixelFormat(sys->hGLDC,
105                    ChoosePixelFormat(sys->hGLDC, &pfd), &pfd);
106
107     /* Create and enable the render context */
108     sys->hGLRC = wglCreateContext(sys->hGLDC);
109     wglMakeCurrent(sys->hGLDC, sys->hGLRC);
110
111     /* */
112     sys->gl.lock = NULL;
113     sys->gl.unlock = NULL;
114     sys->gl.swap = Swap;
115     sys->gl.getProcAddress = GetProcAddress;
116     sys->gl.sys = vd;
117
118     video_format_t fmt = vd->fmt;
119     sys->vgl = vout_display_opengl_New(&fmt, &sys->gl);
120     if (!sys->vgl)
121         goto error;
122
123     vout_display_info_t info = vd->info;
124     info.has_double_click = true;
125     info.has_hide_mouse = false;
126     info.has_pictures_invalid = true;
127     info.has_event_thread = true;
128
129    /* Setup vout_display now that everything is fine */
130     vd->fmt  = fmt;
131     vd->info = info;
132
133     vd->pool    = Pool;
134     vd->prepare = Prepare;
135     vd->display = Display;
136     vd->control = Control;
137     vd->manage  = Manage;
138
139     return VLC_SUCCESS;
140
141 error:
142     Close(object);
143     return VLC_EGENERIC;
144 }
145
146 /**
147  * It destroys an OpenGL vout display.
148  */
149 static void Close(vlc_object_t *object)
150 {
151     vout_display_t *vd = (vout_display_t *)object;
152     vout_display_sys_t *sys = vd->sys;
153
154     if (sys->vgl)
155         vout_display_opengl_Delete(sys->vgl);
156
157     if (sys->hGLDC && sys->hGLRC)
158         wglMakeCurrent(NULL, NULL);
159     if (sys->hGLRC)
160         wglDeleteContext(sys->hGLRC);
161     if (sys->hGLDC)
162         ReleaseDC(sys->hvideownd, sys->hGLDC);
163
164     CommonClean(vd);
165
166     free(sys);
167 }
168
169 /* */
170 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
171 {
172     vout_display_sys_t *sys = vd->sys;
173     VLC_UNUSED(count);
174
175     if (!sys->pool)
176         sys->pool = vout_display_opengl_GetPool(sys->vgl);
177     return sys->pool;
178 }
179
180 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
181 {
182     vout_display_sys_t *sys = vd->sys;
183
184     vout_display_opengl_Prepare(sys->vgl, picture);
185     VLC_UNUSED(subpicture);
186 }
187
188 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
189 {
190     vout_display_sys_t *sys = vd->sys;
191
192     vout_display_opengl_Display(sys->vgl, &vd->source);
193
194     picture_Release(picture);
195     VLC_UNUSED(subpicture);
196
197     CommonDisplay(vd);
198 }
199
200 static int Control(vout_display_t *vd, int query, va_list args)
201 {
202     switch (query) {
203     case VOUT_DISPLAY_GET_OPENGL: {
204         vlc_gl_t **gl = va_arg(args, vlc_gl_t **);
205         *gl = &vd->sys->gl;
206
207         CommonDisplay(vd);
208         return VLC_SUCCESS;
209     }
210     default:
211         return CommonControl(vd, query, args);
212     }
213 }
214
215 static void Manage (vout_display_t *vd)
216 {
217     vout_display_sys_t *sys = vd->sys;
218
219     CommonManage(vd);
220
221     const int width  = sys->rect_dest.right  - sys->rect_dest.left;
222     const int height = sys->rect_dest.bottom - sys->rect_dest.top;
223     glViewport(0, 0, width, height);
224 }
225
226 static void Swap(vlc_gl_t *gl)
227 {
228     vout_display_t *vd = gl->sys;
229
230     SwapBuffers(vd->sys->hGLDC);
231 }
232
233 static void *GetProcAddress(vlc_gl_t *gl, const char *name)
234 {
235     return wglGetProcAddress(name);
236 }
237