1 /*****************************************************************************
2 * glwin32.c: Windows OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
5 * $Id: b6e8fbd6d3f22d0e821db6530740efc454ebbd02 $
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 <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_vout_display.h>
32
33 #include <windows.h>
34 #include <versionhelpers.h>
35
36 #define GLEW_STATIC
37 #include "../opengl/vout_helper.h"
38 #include <GL/wglew.h>
39
40 #include "common.h"
41
42 /*****************************************************************************
43 * Module descriptor
44 *****************************************************************************/
45 static int Open (vlc_object_t *);
46 static void Close(vlc_object_t *);
47
48 vlc_module_begin()
49 set_category(CAT_VIDEO)
50 set_subcategory(SUBCAT_VIDEO_VOUT)
51 set_shortname("OpenGL")
52 set_description(N_("OpenGL video output for Windows"))
53 set_capability("vout display", 275)
54 add_shortcut("glwin32", "opengl")
55 set_callbacks(Open, Close)
56 add_glopts()
57 vlc_module_end()
58
59 /*****************************************************************************
60 * Local prototypes.
61 *****************************************************************************/
62 struct vout_display_sys_t
63 {
64 vout_display_sys_win32_t sys;
65
66 vlc_gl_t *gl;
67 vout_display_opengl_t *vgl;
68 };
69
70 static picture_pool_t *Pool (vout_display_t *, unsigned);
71 static void Prepare(vout_display_t *, picture_t *, subpicture_t *);
72 static void Display(vout_display_t *, picture_t *, subpicture_t *);
73 static void Manage (vout_display_t *);
74
Control(vout_display_t * vd,int query,va_list args)75 static int Control(vout_display_t *vd, int query, va_list args)
76 {
77 vout_display_sys_t *sys = vd->sys;
78
79 if (query == VOUT_DISPLAY_CHANGE_VIEWPOINT)
80 return vout_display_opengl_SetViewpoint(sys->vgl,
81 &va_arg (args, const vout_display_cfg_t* )->viewpoint);
82
83 return CommonControl(vd, query, args);
84 }
85
EmbedVideoWindow_Control(vout_window_t * wnd,int query,va_list ap)86 static int EmbedVideoWindow_Control(vout_window_t *wnd, int query, va_list ap)
87 {
88 VLC_UNUSED( ap ); VLC_UNUSED( query );
89 return VLC_EGENERIC;
90 }
91
EmbedVideoWindow_Create(vout_display_t * vd)92 static vout_window_t *EmbedVideoWindow_Create(vout_display_t *vd)
93 {
94 vout_display_sys_t *sys = vd->sys;
95
96 if (!sys->sys.hvideownd)
97 return NULL;
98
99 vout_window_t *wnd = vlc_object_create(vd, sizeof(vout_window_t));
100 if (!wnd)
101 return NULL;
102
103 wnd->type = VOUT_WINDOW_TYPE_HWND;
104 wnd->handle.hwnd = sys->sys.hvideownd;
105 wnd->control = EmbedVideoWindow_Control;
106 return wnd;
107 }
108
109 /**
110 * It creates an OpenGL vout display.
111 */
Open(vlc_object_t * object)112 static int Open(vlc_object_t *object)
113 {
114 vout_display_t *vd = (vout_display_t *)object;
115 vout_display_sys_t *sys;
116
117 /* do not use OpenGL on XP unless forced */
118 if(!object->obj.force && !IsWindowsVistaOrGreater())
119 return VLC_EGENERIC;
120
121 /* Allocate structure */
122 vd->sys = sys = calloc(1, sizeof(*sys));
123 if (!sys)
124 return VLC_ENOMEM;
125
126 /* */
127 if (CommonInit(vd))
128 goto error;
129
130 EventThreadUpdateTitle(sys->sys.event, VOUT_TITLE " (OpenGL output)");
131
132 vout_window_t *surface = EmbedVideoWindow_Create(vd);
133 if (!surface)
134 goto error;
135
136 sys->gl = vlc_gl_Create (surface, VLC_OPENGL, "$gl");
137 if (!sys->gl)
138 {
139 vlc_object_release(surface);
140 goto error;
141 }
142
143 vlc_gl_Resize (sys->gl, vd->cfg->display.width, vd->cfg->display.height);
144
145 video_format_t fmt = vd->fmt;
146 const vlc_fourcc_t *subpicture_chromas;
147 if (vlc_gl_MakeCurrent (sys->gl))
148 goto error;
149 sys->vgl = vout_display_opengl_New(&fmt, &subpicture_chromas, sys->gl,
150 &vd->cfg->viewpoint);
151 vlc_gl_ReleaseCurrent (sys->gl);
152 if (!sys->vgl)
153 goto error;
154
155 vout_display_info_t info = vd->info;
156 info.has_double_click = true;
157 info.subpicture_chromas = subpicture_chromas;
158
159 /* Setup vout_display now that everything is fine */
160 vd->fmt = fmt;
161 vd->info = info;
162
163 vd->pool = Pool;
164 vd->prepare = Prepare;
165 vd->display = Display;
166 vd->control = Control;
167 vd->manage = Manage;
168
169 return VLC_SUCCESS;
170
171 error:
172 Close(object);
173 return VLC_EGENERIC;
174 }
175
176 /**
177 * It destroys an OpenGL vout display.
178 */
Close(vlc_object_t * object)179 static void Close(vlc_object_t *object)
180 {
181 vout_display_t *vd = (vout_display_t *)object;
182 vout_display_sys_t *sys = vd->sys;
183 vlc_gl_t *gl = sys->gl;
184
185 if (gl)
186 {
187 vout_window_t *surface = gl->surface;
188 if (sys->vgl)
189 {
190 vlc_gl_MakeCurrent (gl);
191 vout_display_opengl_Delete(sys->vgl);
192 vlc_gl_ReleaseCurrent (gl);
193 }
194 vlc_gl_Release (gl);
195 vlc_object_release(surface);
196 }
197
198 CommonClean(vd);
199
200 free(sys);
201 }
202
203 /* */
Pool(vout_display_t * vd,unsigned count)204 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
205 {
206 vout_display_sys_t *sys = vd->sys;
207
208 if (!sys->sys.pool && vlc_gl_MakeCurrent (sys->gl) == VLC_SUCCESS)
209 {
210 sys->sys.pool = vout_display_opengl_GetPool(sys->vgl, count);
211 vlc_gl_ReleaseCurrent (sys->gl);
212 }
213 return sys->sys.pool;
214 }
215
Prepare(vout_display_t * vd,picture_t * picture,subpicture_t * subpicture)216 static void Prepare(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
217 {
218 vout_display_sys_t *sys = vd->sys;
219
220 if (vlc_gl_MakeCurrent (sys->gl) == VLC_SUCCESS)
221 {
222 vout_display_opengl_Prepare (sys->vgl, picture, subpicture);
223 vlc_gl_ReleaseCurrent (sys->gl);
224 }
225 }
226
Display(vout_display_t * vd,picture_t * picture,subpicture_t * subpicture)227 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
228 {
229 vout_display_sys_t *sys = vd->sys;
230
231 if (vlc_gl_MakeCurrent (sys->gl) == VLC_SUCCESS)
232 {
233 vout_display_opengl_Display (sys->vgl, &vd->source);
234 vlc_gl_ReleaseCurrent (sys->gl);
235 }
236
237 picture_Release(picture);
238 if (subpicture)
239 subpicture_Delete(subpicture);
240
241 CommonDisplay(vd);
242 }
243
Manage(vout_display_t * vd)244 static void Manage (vout_display_t *vd)
245 {
246 vout_display_sys_t *sys = vd->sys;
247
248 CommonManage(vd);
249
250 const int width = sys->sys.rect_dest.right - sys->sys.rect_dest.left;
251 const int height = sys->sys.rect_dest.bottom - sys->sys.rect_dest.top;
252 vlc_gl_Resize (sys->gl, width, height);
253 if (vlc_gl_MakeCurrent (sys->gl) != VLC_SUCCESS)
254 return;
255 vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)width / height);
256 vout_display_opengl_Viewport(sys->vgl, 0, 0, width, height);
257 vlc_gl_ReleaseCurrent (sys->gl);
258 }
259