1 /*****************************************************************************
2 * glwin32.c: Windows OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include <errno.h> /* ENOMEM */
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_interface.h>
40 #undef GetSystemMetrics
42 #ifndef MONITOR_DEFAULTTONEAREST
43 # define MONITOR_DEFAULTTONEAREST 2
50 /*****************************************************************************
52 *****************************************************************************/
53 static int OpenVideo ( vlc_object_t * );
54 static void CloseVideo ( vlc_object_t * );
56 static int Init ( vout_thread_t * );
57 static void End ( vout_thread_t * );
58 static int Manage ( vout_thread_t * );
59 static void GLSwapBuffers( vout_thread_t * );
60 static void FirstSwap( vout_thread_t * );
62 /*****************************************************************************
64 *****************************************************************************/
66 set_category( CAT_VIDEO )
67 set_subcategory( SUBCAT_VIDEO_VOUT )
68 set_shortname( "OpenGL" )
69 set_description( N_("OpenGL video output") )
70 set_capability( "opengl provider", 100 )
71 add_shortcut( "glwin32" )
72 set_callbacks( OpenVideo, CloseVideo )
74 /* FIXME: Hack to avoid unregistering our window class */
75 linked_with_a_crap_library_which_uses_atexit ()
79 /* check if we registered a window class because we need to
82 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
83 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
86 /*****************************************************************************
87 * OpenVideo: allocate OpenGL provider
88 *****************************************************************************
89 * This function creates and initializes a video window.
90 *****************************************************************************/
91 static int OpenVideo( vlc_object_t *p_this )
93 vout_thread_t * p_vout = (vout_thread_t *)p_this;
95 /* Allocate structure */
96 p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
97 if( p_vout->p_sys == NULL )
100 /* Initialisations */
101 p_vout->pf_init = Init;
102 p_vout->pf_end = End;
103 p_vout->pf_manage = Manage;
104 p_vout->pf_swap = FirstSwap;
105 p_vout->pf_control = Control;
107 if( CommonInit( p_vout ) )
113 CloseVideo( VLC_OBJECT(p_vout) );
117 /*****************************************************************************
118 * Init: initialize video thread output method
119 *****************************************************************************/
120 static int Init( vout_thread_t *p_vout )
122 PIXELFORMATDESCRIPTOR pfd;
125 /* Change the window title bar text */
126 EventThreadUpdateTitle( p_vout->p_sys->p_event, VOUT_TITLE " (OpenGL output)" );
128 p_vout->p_sys->hGLDC = GetDC( p_vout->p_sys->hvideownd );
130 /* Set the pixel format for the DC */
131 memset( &pfd, 0, sizeof( pfd ) );
132 pfd.nSize = sizeof( pfd );
134 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
135 pfd.iPixelType = PFD_TYPE_RGBA;
138 pfd.iLayerType = PFD_MAIN_PLANE;
139 iFormat = ChoosePixelFormat( p_vout->p_sys->hGLDC, &pfd );
140 SetPixelFormat( p_vout->p_sys->hGLDC, iFormat, &pfd );
142 /* Create and enable the render context */
143 p_vout->p_sys->hGLRC = wglCreateContext( p_vout->p_sys->hGLDC );
144 wglMakeCurrent( p_vout->p_sys->hGLDC, p_vout->p_sys->hGLRC );
149 /*****************************************************************************
150 * End: terminate Sys video thread output method
151 *****************************************************************************
152 * Terminate an output method created by Create.
153 * It is called at the end of the thread.
154 *****************************************************************************/
155 static void End( vout_thread_t *p_vout )
157 wglMakeCurrent( NULL, NULL );
158 wglDeleteContext( p_vout->p_sys->hGLRC );
159 ReleaseDC( p_vout->p_sys->hvideownd, p_vout->p_sys->hGLDC );
163 /*****************************************************************************
164 * CloseVideo: destroy Sys video thread output method
165 *****************************************************************************
166 * Terminate an output method created by Create
167 *****************************************************************************/
168 static void CloseVideo( vlc_object_t *p_this )
170 vout_thread_t * p_vout = (vout_thread_t *)p_this;
172 CommonClean( p_vout );
174 free( p_vout->p_sys );
177 /*****************************************************************************
178 * Manage: handle Sys events
179 *****************************************************************************
180 * This function should be called regularly by the video output thread.
181 * It returns a non null value if an error occurred.
182 *****************************************************************************/
183 static int Manage( vout_thread_t *p_vout )
185 vout_sys_t *p_sys = p_vout->p_sys;
187 const int i_width = p_sys->rect_dest.right - p_sys->rect_dest.left;
188 const int i_height = p_sys->rect_dest.bottom - p_sys->rect_dest.top;
189 glViewport( 0, 0, i_width, i_height );
191 CommonManage( p_vout );
195 /*****************************************************************************
196 * GLSwapBuffers: swap front/back buffers
197 *****************************************************************************/
198 static void GLSwapBuffers( vout_thread_t *p_vout )
200 SwapBuffers( p_vout->p_sys->hGLDC );
204 ** this function is only used once when the first picture is received
205 ** this function will show the video window once a picture is ready
208 static void FirstSwap( vout_thread_t *p_vout )
210 /* get initial picture buffer swapped to front buffer */
211 GLSwapBuffers( p_vout );
214 ** Video window is initially hidden, show it now since we got a
217 SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
225 /* use and restores proper swap function for further pictures */
226 p_vout->pf_swap = GLSwapBuffers;