1 /*****************************************************************************
2 * vout.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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 /*****************************************************************************
27 * This plugin will use YUV overlay if supported, using overlay will result in
28 * the best video quality (hardware interpolation when rescaling the picture)
29 * and the fastest display as it requires less processing.
31 * If YUV overlay is not supported this plugin will use RGB offscreen video
32 * surfaces that will be blitted onto the primary surface (display) to
33 * effectively display the pictures. This fallback method also enables us to
34 * display video in window mode.
36 *****************************************************************************/
37 #include <errno.h> /* ENOMEM */
43 #include <vlc_common.h>
44 #include <vlc_plugin.h>
45 #include <vlc_interface.h>
46 #include <vlc_playlist.h>
54 # include <multimon.h>
56 #undef GetSystemMetrics
58 #ifndef MONITOR_DEFAULTTONEAREST
59 # define MONITOR_DEFAULTTONEAREST 2
64 /*****************************************************************************
65 * picture_sys_t: direct buffer method descriptor
66 *****************************************************************************
67 * This structure is part of the picture descriptor, it describes the
68 * DirectX specific properties of a direct buffer.
69 *****************************************************************************/
72 LPDIRECTDRAWSURFACE2 p_surface;
73 LPDIRECTDRAWSURFACE2 p_front_surface;
77 /*****************************************************************************
79 * Defining them here allows us to get rid of the dxguid library during
81 *****************************************************************************/
85 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
86 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
88 /*****************************************************************************
90 *****************************************************************************/
91 static int OpenVideo ( vlc_object_t * );
92 static void CloseVideo ( vlc_object_t * );
94 static int Init ( vout_thread_t * );
95 static void End ( vout_thread_t * );
96 static int Manage ( vout_thread_t * );
97 static void Display ( vout_thread_t *, picture_t * );
98 static void FirstDisplay( vout_thread_t *, picture_t * );
99 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
101 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
102 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
103 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
105 static int DirectXInitDDraw ( vout_thread_t *p_vout );
106 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
107 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
108 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
109 static int DirectXCreateSurface ( vout_thread_t *p_vout,
110 LPDIRECTDRAWSURFACE2 *, int, int, int );
111 static void DirectXCloseSurface ( vout_thread_t *p_vout,
112 LPDIRECTDRAWSURFACE2 );
113 static int DirectXCreateClipper ( vout_thread_t *p_vout );
114 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
115 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
116 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
118 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
120 void SwitchWallpaperMode( vout_thread_t *, bool );
122 /* Object variables callbacks */
123 static int FindDevicesCallback( vlc_object_t *, char const *,
124 vlc_value_t, vlc_value_t, void * );
125 static int WallpaperCallback( vlc_object_t *, char const *,
126 vlc_value_t, vlc_value_t, void * );
128 /*****************************************************************************
130 *****************************************************************************/
131 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
132 #define HW_YUV_LONGTEXT N_( \
133 "Try to use hardware acceleration for YUV->RGB conversions. " \
134 "This option doesn't have any effect when using overlays." )
136 #define SYSMEM_TEXT N_("Use video buffers in system memory")
137 #define SYSMEM_LONGTEXT N_( \
138 "Create video buffers in system memory instead of video memory. This " \
139 "isn't recommended as usually using video memory allows to benefit from " \
140 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
141 "This option doesn't have any effect when using overlays." )
143 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
144 #define TRIPLEBUF_LONGTEXT N_( \
145 "Try to use triple buffering when using YUV overlays. That results in " \
146 "much better video quality (no flickering)." )
148 #define DEVICE_TEXT N_("Name of desired display device")
149 #define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " \
150 "specify the Windows device name of the display that you want the video " \
151 "window to open on. For example, \"\\\\.\\DISPLAY1\" or " \
152 "\"\\\\.\\DISPLAY2\"." )
154 #define WALLPAPER_TEXT N_("Enable wallpaper mode ")
155 #define WALLPAPER_LONGTEXT N_( \
156 "The wallpaper mode allows you to display the video as the desktop " \
157 "background. Note that this feature only works in overlay mode and " \
158 "the desktop must not already have a wallpaper." )
160 static const char *const ppsz_dev[] = { "" };
161 static const char *const ppsz_dev_text[] = { N_("Default") };
164 set_shortname( "DirectX" )
165 set_category( CAT_VIDEO )
166 set_subcategory( SUBCAT_VIDEO_VOUT )
167 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
169 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
171 add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT,
172 TRIPLEBUF_LONGTEXT, true );
174 add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
176 change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback );
177 change_action_add( FindDevicesCallback, N_("Refresh list") )
179 add_bool( "directx-wallpaper", 0, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
182 set_description( N_("DirectX video output") )
183 set_capability( "video output", 100 )
184 add_shortcut( "directx" )
185 set_callbacks( OpenVideo, CloseVideo )
187 /* FIXME: Hack to avoid unregistering our window class */
188 linked_with_a_crap_library_which_uses_atexit ()
192 /* check if we registered a window class because we need to
195 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
196 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
199 /*****************************************************************************
200 * OpenVideo: allocate DirectX video thread output method
201 *****************************************************************************
202 * This function allocates and initialize the DirectX vout method.
203 *****************************************************************************/
204 static int OpenVideo( vlc_object_t *p_this )
206 vout_thread_t * p_vout = (vout_thread_t *)p_this;
210 /* Allocate structure */
211 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
212 if( p_vout->p_sys == NULL )
214 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
216 /* Initialisations */
217 p_vout->pf_init = Init;
218 p_vout->pf_end = End;
219 p_vout->pf_manage = Manage;
220 p_vout->pf_render = NULL;
221 p_vout->pf_display = FirstDisplay;
223 p_vout->p_sys->p_ddobject = NULL;
224 p_vout->p_sys->p_display = NULL;
225 p_vout->p_sys->p_current_surface = NULL;
226 p_vout->p_sys->p_clipper = NULL;
227 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
228 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
229 p_vout->p_sys->i_changes = 0;
230 p_vout->p_sys->b_wallpaper = 0;
231 vlc_mutex_init( &p_vout->p_sys->lock );
232 SetRectEmpty( &p_vout->p_sys->rect_display );
233 SetRectEmpty( &p_vout->p_sys->rect_parent );
235 /* Multimonitor stuff */
236 p_vout->p_sys->hmonitor = NULL;
237 p_vout->p_sys->p_display_driver = NULL;
238 p_vout->p_sys->MonitorFromWindow = NULL;
239 p_vout->p_sys->GetMonitorInfo = NULL;
240 if( (huser32 = GetModuleHandle( _T("USER32") ) ) )
242 p_vout->p_sys->MonitorFromWindow = (HMONITOR (WINAPI *)( HWND, DWORD ))
243 GetProcAddress( huser32, _T("MonitorFromWindow") );
244 p_vout->p_sys->GetMonitorInfo =
246 GetProcAddress( huser32, "GetMonitorInfoA" );
248 GetProcAddress( huser32, _T("GetMonitorInfoW") );
252 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
253 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
254 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
255 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
256 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
257 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
258 var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
260 p_vout->p_sys->b_cursor_hidden = 0;
261 p_vout->p_sys->i_lastmoved = mdate();
262 p_vout->p_sys->i_mouse_hide_timeout =
263 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
265 /* Set main window's size */
266 p_vout->p_sys->i_window_width = p_vout->i_window_width;
267 p_vout->p_sys->i_window_height = p_vout->i_window_height;
269 /* Create the Vout EventThread, this thread is created by us to isolate
270 * the Win32 PeekMessage function calls. We want to do this because
271 * Windows can stay blocked inside this call for a long time, and when
272 * this happens it thus blocks vlc's video_output thread.
273 * DirectXEventThread will take care of the creation of the video
274 * window (because PeekMessage has to be called from the same thread which
275 * created the window). */
276 msg_Dbg( p_vout, "creating DirectXEventThread" );
277 p_vout->p_sys->p_event =
278 vlc_object_create( p_vout, sizeof(event_thread_t) );
279 p_vout->p_sys->p_event->p_vout = p_vout;
280 if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
281 EventThread, 0, 1 ) )
283 msg_Err( p_vout, "cannot create Vout EventThread" );
284 vlc_object_release( p_vout->p_sys->p_event );
285 p_vout->p_sys->p_event = NULL;
289 if( p_vout->p_sys->p_event->b_error )
291 msg_Err( p_vout, "Vout EventThread failed" );
295 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
297 msg_Dbg( p_vout, "Vout EventThread running" );
299 /* Initialise DirectDraw */
300 if( DirectXInitDDraw( p_vout ) )
302 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
306 /* Create the directx display */
307 if( DirectXCreateDisplay( p_vout ) )
309 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
313 /* Variable to indicate if the window should be on top of others */
314 /* Trigger a callback right now */
315 var_Get( p_vout, "video-on-top", &val );
316 var_Set( p_vout, "video-on-top", val );
318 /* Variable to indicate if the window should be on top of others */
319 /* Trigger a callback right now */
320 var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
321 val.psz_string = _("Wallpaper");
322 var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
323 var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
324 var_Get( p_vout, "directx-wallpaper", &val );
325 var_Set( p_vout, "directx-wallpaper", val );
327 /* disable screensaver by temporarily changing system settings */
328 p_vout->p_sys->i_spi_lowpowertimeout = 0;
329 p_vout->p_sys->i_spi_powerofftimeout = 0;
330 p_vout->p_sys->i_spi_screensavetimeout = 0;
331 var_Get( p_vout, "disable-screensaver", &val);
333 msg_Dbg(p_vout, "disabling screen saver");
334 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
335 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
336 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
337 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
339 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
340 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
341 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
342 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
344 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
345 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
346 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
347 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
354 CloseVideo( VLC_OBJECT(p_vout) );
358 /*****************************************************************************
359 * Init: initialize DirectX video thread output method
360 *****************************************************************************
361 * This function create the directx surfaces needed by the output thread.
362 * It is called at the beginning of the thread.
363 *****************************************************************************/
364 static int Init( vout_thread_t *p_vout )
369 /* Get a few default parameters */
370 var_Get( p_vout, "overlay", &val );
371 p_vout->p_sys->b_using_overlay = val.b_bool;
372 var_Get( p_vout, "directx-use-sysmem", &val );
373 p_vout->p_sys->b_use_sysmem = val.b_bool;
374 var_Get( p_vout, "directx-hw-yuv", &val );
375 p_vout->p_sys->b_hw_yuv = val.b_bool;
376 var_Get( p_vout, "directx-3buffering", &val );
377 p_vout->p_sys->b_3buf_overlay = val.b_bool;
379 /* Initialise DirectDraw if not already done.
380 * We do this here because on multi-monitor systems we may have to
381 * re-create the directdraw surfaces. */
382 if( !p_vout->p_sys->p_ddobject &&
383 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
385 msg_Err( p_vout, "cannot initialize DirectDraw" );
389 /* Create the directx display */
390 if( !p_vout->p_sys->p_display &&
391 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
393 msg_Err( p_vout, "cannot initialize DirectDraw" );
397 /* Initialize the output structure.
398 * Since DirectDraw can do rescaling for us, stick to the default
399 * coordinates and aspect. */
400 p_vout->output.i_width = p_vout->render.i_width;
401 p_vout->output.i_height = p_vout->render.i_height;
402 p_vout->output.i_aspect = p_vout->render.i_aspect;
403 p_vout->fmt_out = p_vout->fmt_in;
404 UpdateRects( p_vout, true );
406 #define MAX_DIRECTBUFFERS 1
407 /* Right now we use only 1 directbuffer because we don't want the
408 * video decoder to decode directly into direct buffers as they are
409 * created into video memory and video memory is _really_ slow */
411 /* Choose the chroma we will try first. */
412 switch( p_vout->render.i_chroma )
414 case VLC_FOURCC('Y','U','Y','2'):
415 case VLC_FOURCC('Y','U','N','V'):
416 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
418 case VLC_FOURCC('U','Y','V','Y'):
419 case VLC_FOURCC('U','Y','N','V'):
420 case VLC_FOURCC('Y','4','2','2'):
421 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
423 case VLC_FOURCC('Y','V','Y','U'):
424 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
427 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
431 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
433 i_chroma_backup = p_vout->output.i_chroma;
435 if( !I_OUTPUTPICTURES )
437 /* hmmm, it didn't work! Let's try commonly supported chromas */
438 if( p_vout->output.i_chroma != VLC_FOURCC('I','4','2','0') )
440 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
441 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
443 if( !I_OUTPUTPICTURES )
445 /* hmmm, it still didn't work! Let's try another one */
446 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
447 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
451 if( !I_OUTPUTPICTURES )
453 /* If it still didn't work then don't try to use an overlay */
454 p_vout->output.i_chroma = i_chroma_backup;
455 p_vout->p_sys->b_using_overlay = 0;
456 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
459 /* Change the window title bar text */
460 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
462 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
466 /*****************************************************************************
467 * End: terminate Sys video thread output method
468 *****************************************************************************
469 * Terminate an output method created by Create.
470 * It is called at the end of the thread.
471 *****************************************************************************/
472 static void End( vout_thread_t *p_vout )
474 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
476 DirectXCloseDisplay( p_vout );
477 DirectXCloseDDraw( p_vout );
482 /*****************************************************************************
483 * CloseVideo: destroy Sys video thread output method
484 *****************************************************************************
485 * Terminate an output method created by Create
486 *****************************************************************************/
487 static void CloseVideo( vlc_object_t *p_this )
489 vout_thread_t * p_vout = (vout_thread_t *)p_this;
491 if( p_vout->b_fullscreen )
493 msg_Dbg( p_vout, "Quitting fullscreen" );
494 Win32ToggleFullscreen( p_vout );
495 /* Force fullscreen in the core for the next video */
496 var_SetBool( p_vout, "fullscreen", true );
499 if( p_vout->p_sys->p_event )
501 vlc_object_detach( p_vout->p_sys->p_event );
503 /* Kill Vout EventThread */
504 vlc_object_kill( p_vout->p_sys->p_event );
506 /* we need to be sure Vout EventThread won't stay stuck in
507 * GetMessage, so we send a fake message */
508 if( p_vout->p_sys->hwnd )
510 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
513 vlc_thread_join( p_vout->p_sys->p_event );
514 vlc_object_release( p_vout->p_sys->p_event );
517 vlc_mutex_destroy( &p_vout->p_sys->lock );
519 /* Make sure the wallpaper is restored */
520 SwitchWallpaperMode( p_vout, false );
522 /* restore screensaver system settings */
523 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
524 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
525 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
527 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
528 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
529 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
531 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
532 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
533 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
536 free( p_vout->p_sys );
537 p_vout->p_sys = NULL;
540 /*****************************************************************************
541 * Manage: handle Sys events
542 *****************************************************************************
543 * This function should be called regularly by the video output thread.
544 * It returns a non null value if an error occurred.
545 *****************************************************************************/
546 static int Manage( vout_thread_t *p_vout )
548 /* If we do not control our window, we check for geometry changes
549 * ourselves because the parent might not send us its events. */
550 vlc_mutex_lock( &p_vout->p_sys->lock );
551 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
556 vlc_mutex_unlock( &p_vout->p_sys->lock );
558 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
559 point.x = point.y = 0;
560 ClientToScreen( p_vout->p_sys->hparent, &point );
561 OffsetRect( &rect_parent, point.x, point.y );
563 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
565 p_vout->p_sys->rect_parent = rect_parent;
567 /* This one is to force the update even if only
568 * the position has changed */
569 SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
570 rect_parent.right - rect_parent.left,
571 rect_parent.bottom - rect_parent.top, 0 );
573 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
574 rect_parent.right - rect_parent.left,
575 rect_parent.bottom - rect_parent.top, 0 );
580 vlc_mutex_unlock( &p_vout->p_sys->lock );
586 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
588 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
590 /* Check if we are still on the same monitor */
591 if( p_vout->p_sys->MonitorFromWindow &&
592 p_vout->p_sys->hmonitor !=
593 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
594 MONITOR_DEFAULTTONEAREST ) )
596 /* This will force the vout core to recreate the picture buffers */
597 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
601 /* Check for cropping / aspect changes */
602 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
603 p_vout->i_changes & VOUT_ASPECT_CHANGE )
605 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
606 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
608 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
609 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
610 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
611 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
612 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
613 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
614 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
615 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
616 UpdateRects( p_vout, true );
619 /* We used to call the Win32 PeekMessage function here to read the window
620 * messages. But since window can stay blocked into this function for a
621 * long time (for example when you move your window on the screen), I
622 * decided to isolate PeekMessage in another thread. */
624 if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
626 SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
627 p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
628 DirectDrawUpdateOverlay( p_vout );
634 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
635 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
637 Win32ToggleFullscreen( p_vout );
639 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
640 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
646 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
647 (mdate() - p_vout->p_sys->i_lastmoved) >
648 p_vout->p_sys->i_mouse_hide_timeout )
653 /* Hide the cursor only if it is inside our window */
654 GetCursorPos( &point );
655 hwnd = WindowFromPoint(point);
656 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
658 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
662 p_vout->p_sys->i_lastmoved = mdate();
667 * "Always on top" status change
669 if( p_vout->p_sys->b_on_top_change )
672 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
674 var_Get( p_vout, "video-on-top", &val );
676 /* Set the window on top if necessary */
677 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
680 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
681 MF_BYCOMMAND | MFS_CHECKED );
682 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
683 SWP_NOSIZE | SWP_NOMOVE );
686 /* The window shouldn't be on top */
687 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
690 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
691 MF_BYCOMMAND | MFS_UNCHECKED );
692 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
693 SWP_NOSIZE | SWP_NOMOVE );
696 p_vout->p_sys->b_on_top_change = false;
699 /* Check if the event thread is still running */
700 if( !vlc_object_alive (p_vout->p_sys->p_event) )
702 return VLC_EGENERIC; /* exit */
708 /*****************************************************************************
709 * Display: displays previously rendered output
710 *****************************************************************************
711 * This function sends the currently rendered image to the display, wait until
712 * it is displayed and switch the two rendering buffers, preparing next frame.
713 *****************************************************************************/
714 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
718 if( (p_vout->p_sys->p_display == NULL) )
720 msg_Warn( p_vout, "no display!" );
724 /* Our surface can be lost so be sure to check this
725 * and restore it if need be */
726 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
727 == DDERR_SURFACELOST )
729 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
730 p_vout->p_sys->b_using_overlay )
731 DirectDrawUpdateOverlay( p_vout );
734 if( !p_vout->p_sys->b_using_overlay )
738 /* We ask for the "NOTEARING" option */
739 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
740 ddbltfx.dwSize = sizeof(DDBLTFX);
741 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
743 /* Blit video surface to display */
744 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
745 &p_vout->p_sys->rect_dest_clipped,
746 p_pic->p_sys->p_surface,
747 &p_vout->p_sys->rect_src_clipped,
748 DDBLT_ASYNC, &ddbltfx );
749 if( dxresult != DD_OK )
751 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
755 else /* using overlay */
757 /* Flip the overlay buffers if we are using back buffers */
758 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
763 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
765 if( dxresult != DD_OK )
767 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
770 /* set currently displayed pic */
771 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
773 /* Lock surface to get all the required info */
774 if( DirectXLockSurface( p_vout, p_pic ) )
777 msg_Warn( p_vout, "cannot lock surface" );
780 DirectXUnlockSurface( p_vout, p_pic );
785 ** this function is only used once when the first picture is received
786 ** this function will show the video window once video is ready for
790 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
792 /* get initial picture rendered on overlay surface */
793 Display(p_vout, p_pic);
795 IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
796 DDWAITVB_BLOCKBEGIN, NULL);
798 if( p_vout->p_sys->b_using_overlay )
800 /* set the colorkey as the backgound brush for the video window */
801 SetClassLong( p_vout->p_sys->hvideownd, GCL_HBRBACKGROUND,
802 (LONG)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
805 ** Video window is initially hidden, show it now since we got a
808 SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
816 /* use and restores proper display function for further pictures */
817 p_vout->pf_display = Display;
820 /* following functions are local */
822 /*****************************************************************************
823 * DirectXEnumCallback: Device enumeration
824 *****************************************************************************
825 * This callback function is called by DirectDraw once for each
826 * available DirectDraw device.
827 *****************************************************************************/
828 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
829 LPTSTR psz_drivername, VOID* p_context,
832 vout_thread_t *p_vout = (vout_thread_t *)p_context;
835 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
839 var_Get( p_vout, "directx-device", &device );
841 if( ( !device.psz_string || !*device.psz_string ) &&
842 hmon == p_vout->p_sys->hmonitor )
844 free( device.psz_string );
846 else if( strcmp( psz_drivername, device.psz_string ) == 0 )
848 MONITORINFO monitor_info;
849 monitor_info.cbSize = sizeof( MONITORINFO );
851 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
855 /* Move window to the right screen */
856 GetWindowRect( p_vout->p_sys->hwnd, &rect );
857 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
859 rect.left = monitor_info.rcWork.left;
860 rect.top = monitor_info.rcWork.top;
861 msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
862 "position to %ld,%ld", rect.left, rect.top );
863 SetWindowPos( p_vout->p_sys->hwnd, NULL,
864 rect.left, rect.top, 0, 0,
865 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
869 p_vout->p_sys->hmonitor = hmon;
870 free( device.psz_string );
874 free( device.psz_string );
875 return TRUE; /* Keep enumerating */
878 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
879 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
880 if( p_vout->p_sys->p_display_driver )
881 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
884 return TRUE; /* Keep enumerating */
887 /*****************************************************************************
888 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
889 *****************************************************************************
890 * This function initialise and allocate resources for DirectDraw.
891 *****************************************************************************/
892 static int DirectXInitDDraw( vout_thread_t *p_vout )
895 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
896 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
898 LPDIRECTDRAW p_ddobject;
900 msg_Dbg( p_vout, "DirectXInitDDraw" );
902 /* Load direct draw DLL */
903 p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
904 if( p_vout->p_sys->hddraw_dll == NULL )
906 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
910 OurDirectDrawCreate =
911 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
912 _T("DirectDrawCreate") );
913 if( OurDirectDrawCreate == NULL )
915 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
919 OurDirectDrawEnumerateEx =
920 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
922 "DirectDrawEnumerateExA" );
924 _T("DirectDrawEnumerateExW") );
927 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
931 var_Get( p_vout, "directx-device", &device );
932 if( device.psz_string )
934 msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
935 free( device.psz_string );
938 p_vout->p_sys->hmonitor =
939 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
940 MONITOR_DEFAULTTONEAREST );
942 /* Enumerate displays */
943 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
944 DDENUM_ATTACHEDSECONDARYDEVICES );
947 /* Initialize DirectDraw now */
948 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
950 if( dxresult != DD_OK )
952 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
956 /* Get the IDirectDraw2 interface */
957 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
958 (LPVOID *)&p_vout->p_sys->p_ddobject );
959 /* Release the unused interface */
960 IDirectDraw_Release( p_ddobject );
961 if( dxresult != DD_OK )
963 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
967 /* Set DirectDraw Cooperative level, ie what control we want over Windows
969 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
970 NULL, DDSCL_NORMAL );
971 if( dxresult != DD_OK )
973 msg_Err( p_vout, "cannot set direct draw cooperative level" );
977 /* Get the size of the current display device */
978 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
980 MONITORINFO monitor_info;
981 monitor_info.cbSize = sizeof( MONITORINFO );
982 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
984 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
988 p_vout->p_sys->rect_display.left = 0;
989 p_vout->p_sys->rect_display.top = 0;
990 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
991 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
994 msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
995 p_vout->p_sys->rect_display.left,
996 p_vout->p_sys->rect_display.top,
997 p_vout->p_sys->rect_display.right,
998 p_vout->p_sys->rect_display.bottom );
1000 /* Probe the capabilities of the hardware */
1001 DirectXGetDDrawCaps( p_vout );
1003 msg_Dbg( p_vout, "End DirectXInitDDraw" );
1007 if( p_vout->p_sys->p_ddobject )
1008 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
1009 if( p_vout->p_sys->hddraw_dll )
1010 FreeLibrary( p_vout->p_sys->hddraw_dll );
1011 p_vout->p_sys->hddraw_dll = NULL;
1012 p_vout->p_sys->p_ddobject = NULL;
1013 return VLC_EGENERIC;
1016 /*****************************************************************************
1017 * DirectXCreateDisplay: create the DirectDraw display.
1018 *****************************************************************************
1019 * Create and initialize display according to preferences specified in the vout
1021 *****************************************************************************/
1022 static int DirectXCreateDisplay( vout_thread_t *p_vout )
1026 LPDIRECTDRAWSURFACE p_display;
1028 msg_Dbg( p_vout, "DirectXCreateDisplay" );
1030 /* Now get the primary surface. This surface is what you actually see
1032 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
1033 ddsd.dwSize = sizeof(DDSURFACEDESC);
1034 ddsd.dwFlags = DDSD_CAPS;
1035 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1037 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1038 &ddsd, &p_display, NULL );
1039 if( dxresult != DD_OK )
1041 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
1042 return VLC_EGENERIC;
1045 dxresult = IDirectDrawSurface_QueryInterface( p_display,
1046 &IID_IDirectDrawSurface2,
1047 (LPVOID *)&p_vout->p_sys->p_display );
1048 /* Release the old interface */
1049 IDirectDrawSurface_Release( p_display );
1050 if ( dxresult != DD_OK )
1052 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
1053 "(error %li)", dxresult );
1054 return VLC_EGENERIC;
1057 /* The clipper will be used only in non-overlay mode */
1058 DirectXCreateClipper( p_vout );
1060 /* Make sure the colorkey will be painted */
1061 p_vout->p_sys->i_colorkey = 1;
1062 p_vout->p_sys->i_rgb_colorkey =
1063 DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
1065 UpdateRects( p_vout, true );
1070 /*****************************************************************************
1071 * DirectXCreateClipper: Create a clipper that will be used when blitting the
1072 * RGB surface to the main display.
1073 *****************************************************************************
1074 * This clipper prevents us to modify by mistake anything on the screen
1075 * which doesn't belong to our window. For example when a part of our video
1076 * window is hidden by another window.
1077 *****************************************************************************/
1078 static int DirectXCreateClipper( vout_thread_t *p_vout )
1082 msg_Dbg( p_vout, "DirectXCreateClipper" );
1084 /* Create the clipper */
1085 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
1086 &p_vout->p_sys->p_clipper, NULL );
1087 if( dxresult != DD_OK )
1089 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
1093 /* Associate the clipper to the window */
1094 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
1095 p_vout->p_sys->hvideownd );
1096 if( dxresult != DD_OK )
1098 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
1103 /* associate the clipper with the surface */
1104 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
1105 p_vout->p_sys->p_clipper);
1106 if( dxresult != DD_OK )
1108 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
1116 if( p_vout->p_sys->p_clipper )
1118 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1120 p_vout->p_sys->p_clipper = NULL;
1121 return VLC_EGENERIC;
1124 /*****************************************************************************
1125 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
1126 *****************************************************************************
1127 * The best method of display is with an YUV overlay because the YUV->RGB
1128 * conversion is done in hardware.
1129 * You can also create a plain RGB surface.
1130 * ( Maybe we could also try an RGB overlay surface, which could have hardware
1131 * scaling and which would also be faster in window mode because you don't
1132 * need to do any blitting to the main display...)
1133 *****************************************************************************/
1134 static int DirectXCreateSurface( vout_thread_t *p_vout,
1135 LPDIRECTDRAWSURFACE2 *pp_surface_final,
1136 int i_chroma, int b_overlay,
1140 LPDIRECTDRAWSURFACE p_surface;
1143 /* Create the video surface */
1146 /* Now try to create the YUV overlay surface.
1147 * This overlay will be displayed on top of the primary surface.
1148 * A color key is used to determine whether or not the overlay will be
1149 * displayed, ie the overlay will be displayed in place of the primary
1150 * surface wherever the primary surface will have this color.
1151 * The video window has been created with a background of this color so
1152 * the overlay will be only displayed on top of this window */
1154 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
1155 ddsd.dwSize = sizeof(DDSURFACEDESC);
1156 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1157 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1158 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1159 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1160 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
1161 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
1162 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
1164 ddsd.dwHeight = p_vout->render.i_height;
1165 ddsd.dwWidth = p_vout->render.i_width;
1166 ddsd.dwBackBufferCount = i_backbuffers;
1168 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1169 &ddsd, &p_surface, NULL );
1170 if( dxresult != DD_OK )
1172 *pp_surface_final = NULL;
1173 return VLC_EGENERIC;
1179 bool b_rgb_surface =
1180 ( i_chroma == VLC_FOURCC('R','G','B','2') )
1181 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
1182 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
1183 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
1184 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
1186 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
1187 ddsd.dwSize = sizeof(DDSURFACEDESC);
1188 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1189 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
1190 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1191 ddsd.dwHeight = p_vout->render.i_height;
1192 ddsd.dwWidth = p_vout->render.i_width;
1194 if( p_vout->p_sys->b_use_sysmem )
1195 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1197 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
1199 if( !b_rgb_surface )
1201 ddsd.dwFlags |= DDSD_PIXELFORMAT;
1202 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1203 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1206 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1207 &ddsd, &p_surface, NULL );
1208 if( dxresult != DD_OK )
1210 *pp_surface_final = NULL;
1211 return VLC_EGENERIC;
1215 /* Now that the surface is created, try to get a newer DirectX interface */
1216 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
1217 &IID_IDirectDrawSurface2,
1218 (LPVOID *)pp_surface_final );
1219 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
1220 if ( dxresult != DD_OK )
1222 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
1223 "(error %li)", dxresult );
1224 *pp_surface_final = NULL;
1225 return VLC_EGENERIC;
1230 /* Check the overlay is useable as some graphics cards allow creating
1231 * several overlays but only one can be used at one time. */
1232 p_vout->p_sys->p_current_surface = *pp_surface_final;
1233 if( DirectDrawUpdateOverlay( p_vout ) != VLC_SUCCESS )
1235 IDirectDrawSurface2_Release( *pp_surface_final );
1236 *pp_surface_final = NULL;
1237 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1238 return VLC_EGENERIC;
1245 /*****************************************************************************
1246 * DirectDrawUpdateOverlay: Move or resize overlay surface on video display.
1247 *****************************************************************************
1248 * This function is used to move or resize an overlay surface on the screen.
1249 * Ususally the overlay is moved by the user and thus, by a move or resize
1250 * event (in Manage).
1251 *****************************************************************************/
1252 int DirectDrawUpdateOverlay( vout_thread_t *p_vout )
1257 RECT rect_src = p_vout->p_sys->rect_src_clipped;
1258 RECT rect_dest = p_vout->p_sys->rect_dest_clipped;
1260 if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1262 if( p_vout->p_sys->b_wallpaper )
1264 int i_x, i_y, i_width, i_height;
1266 rect_src.left = p_vout->fmt_out.i_x_offset;
1267 rect_src.top = p_vout->fmt_out.i_y_offset;
1268 rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
1269 rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
1271 rect_dest = p_vout->p_sys->rect_display;
1272 vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1273 &i_x, &i_y, &i_width, &i_height );
1275 rect_dest.left += i_x;
1276 rect_dest.right = rect_dest.left + i_width;
1277 rect_dest.top += i_y;
1278 rect_dest.bottom = rect_dest.top + i_height;
1281 vlc_mutex_lock( &p_vout->p_sys->lock );
1282 if( p_vout->p_sys->p_current_surface == NULL )
1284 vlc_mutex_unlock( &p_vout->p_sys->lock );
1285 return VLC_EGENERIC;
1288 /* The new window dimensions should already have been computed by the
1289 * caller of this function */
1291 /* Position and show the overlay */
1292 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1293 ddofx.dwSize = sizeof(DDOVERLAYFX);
1294 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1295 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1297 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1299 dxresult = IDirectDrawSurface2_UpdateOverlay(
1300 p_vout->p_sys->p_current_surface,
1301 &rect_src, p_vout->p_sys->p_display, &rect_dest,
1304 vlc_mutex_unlock( &p_vout->p_sys->lock );
1306 if(dxresult != DD_OK)
1308 msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
1309 return VLC_EGENERIC;
1315 /*****************************************************************************
1316 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1317 *****************************************************************************
1318 * This function returns all resources allocated by DirectXInitDDraw.
1319 *****************************************************************************/
1320 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1322 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1323 if( p_vout->p_sys->p_ddobject != NULL )
1325 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1326 p_vout->p_sys->p_ddobject = NULL;
1329 if( p_vout->p_sys->hddraw_dll != NULL )
1331 FreeLibrary( p_vout->p_sys->hddraw_dll );
1332 p_vout->p_sys->hddraw_dll = NULL;
1335 free( p_vout->p_sys->p_display_driver );
1336 p_vout->p_sys->p_display_driver = NULL;
1338 p_vout->p_sys->hmonitor = NULL;
1341 /*****************************************************************************
1342 * DirectXCloseDisplay: close and reset the DirectX display device
1343 *****************************************************************************
1344 * This function returns all resources allocated by DirectXCreateDisplay.
1345 *****************************************************************************/
1346 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1348 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1350 if( p_vout->p_sys->p_clipper != NULL )
1352 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1353 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1354 p_vout->p_sys->p_clipper = NULL;
1357 if( p_vout->p_sys->p_display != NULL )
1359 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1360 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1361 p_vout->p_sys->p_display = NULL;
1365 /*****************************************************************************
1366 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1367 *****************************************************************************
1368 * This function returns all resources allocated for the surface.
1369 *****************************************************************************/
1370 static void DirectXCloseSurface( vout_thread_t *p_vout,
1371 LPDIRECTDRAWSURFACE2 p_surface )
1373 msg_Dbg( p_vout, "DirectXCloseSurface" );
1374 if( p_surface != NULL )
1376 IDirectDrawSurface2_Release( p_surface );
1380 /*****************************************************************************
1381 * NewPictureVec: allocate a vector of identical pictures
1382 *****************************************************************************
1383 * Returns 0 on success, -1 otherwise
1384 *****************************************************************************/
1385 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1389 int i_ret = VLC_SUCCESS;
1390 LPDIRECTDRAWSURFACE2 p_surface;
1392 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1393 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1394 (char *)&p_vout->output.i_chroma );
1396 I_OUTPUTPICTURES = 0;
1398 /* First we try to use an YUV overlay surface.
1399 * The overlay surface that we create won't be used to decode directly
1400 * into it because accessing video memory directly is way to slow (remember
1401 * that pictures are decoded macroblock per macroblock). Instead the video
1402 * will be decoded in picture buffers in system memory which will then be
1403 * memcpy() to the overlay surface. */
1404 if( p_vout->p_sys->b_using_overlay )
1406 /* Triple buffering rocks! it doesn't have any processing overhead
1407 * (you don't have to wait for the vsync) and provides for a very nice
1408 * video quality (no tearing). */
1409 if( p_vout->p_sys->b_3buf_overlay )
1410 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1411 p_vout->output.i_chroma,
1412 p_vout->p_sys->b_using_overlay,
1413 2 /* number of backbuffers */ );
1415 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1417 /* Try to reduce the number of backbuffers */
1418 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1419 p_vout->output.i_chroma,
1420 p_vout->p_sys->b_using_overlay,
1421 0 /* number of backbuffers */ );
1424 if( i_ret == VLC_SUCCESS )
1427 picture_t front_pic;
1428 picture_sys_t front_pic_sys;
1429 front_pic.p_sys = &front_pic_sys;
1431 /* Allocate internal structure */
1432 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1433 if( p_pic[0].p_sys == NULL )
1435 DirectXCloseSurface( p_vout, p_surface );
1439 /* set front buffer */
1440 p_pic[0].p_sys->p_front_surface = p_surface;
1442 /* Get the back buffer */
1443 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1444 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1445 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1446 p_surface, &dds_caps,
1447 &p_pic[0].p_sys->p_surface ) )
1449 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1450 /* front buffer is the same as back buffer */
1451 p_pic[0].p_sys->p_surface = p_surface;
1455 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1456 p_pic[0].p_sys->p_front_surface;
1458 /* Reset the front buffer memory */
1459 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1462 for( i = 0; i < front_pic.i_planes; i++ )
1463 for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1464 memset( front_pic.p[i].p_pixels + j *
1465 front_pic.p[i].i_pitch, 127,
1466 front_pic.p[i].i_visible_pitch );
1468 DirectXUnlockSurface( p_vout, &front_pic );
1471 DirectDrawUpdateOverlay( p_vout );
1472 I_OUTPUTPICTURES = 1;
1473 msg_Dbg( p_vout, "YUV overlay created successfully" );
1477 /* As we can't have an overlay, we'll try to create a plain offscreen
1478 * surface. This surface will reside in video memory because there's a
1479 * better chance then that we'll be able to use some kind of hardware
1480 * acceleration like rescaling, blitting or YUV->RGB conversions.
1481 * We then only need to blit this surface onto the main display when we
1482 * want to display it */
1483 if( !p_vout->p_sys->b_using_overlay )
1485 if( p_vout->p_sys->b_hw_yuv )
1489 bool b_result = false;
1491 /* Check if the chroma is supported first. This is required
1492 * because a few buggy drivers don't mind creating the surface
1493 * even if they don't know about the chroma. */
1494 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1495 &i_codes, NULL ) == DD_OK )
1497 pi_codes = malloc( i_codes * sizeof(DWORD) );
1498 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1499 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1501 for( i = 0; i < (int)i_codes; i++ )
1503 if( p_vout->output.i_chroma == pi_codes[i] )
1513 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1514 p_vout->output.i_chroma,
1516 0 /* no back buffers */ );
1518 p_vout->p_sys->b_hw_yuv = false;
1521 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1523 /* Our last choice is to use a plain RGB surface */
1524 DDPIXELFORMAT ddpfPixelFormat;
1526 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1527 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1530 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1532 switch( ddpfPixelFormat.dwRGBBitCount )
1535 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1536 p_vout->output.pf_setpalette = SetPalette;
1539 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1542 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1545 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1548 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1551 msg_Err( p_vout, "unknown screen depth" );
1552 return VLC_EGENERIC;
1554 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1555 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1556 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1559 p_vout->p_sys->b_hw_yuv = 0;
1561 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1562 p_vout->output.i_chroma,
1564 0 /* no back buffers */ );
1566 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1568 /* Give it a last try with b_use_sysmem enabled */
1569 p_vout->p_sys->b_use_sysmem = 1;
1571 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1572 p_vout->output.i_chroma,
1574 0 /* no back buffers */ );
1578 if( i_ret == VLC_SUCCESS )
1580 /* Allocate internal structure */
1581 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1582 if( p_pic[0].p_sys == NULL )
1584 DirectXCloseSurface( p_vout, p_surface );
1588 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1591 I_OUTPUTPICTURES = 1;
1593 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1594 (char *)&p_vout->output.i_chroma );
1599 /* Now that we've got all our direct-buffers, we can finish filling in the
1600 * picture_t structures */
1601 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1603 p_pic[i].i_status = DESTROYED_PICTURE;
1604 p_pic[i].i_type = DIRECT_PICTURE;
1605 p_pic[i].b_slow = true;
1606 p_pic[i].pf_lock = DirectXLockSurface;
1607 p_pic[i].pf_unlock = DirectXUnlockSurface;
1608 PP_OUTPUTPICTURE[i] = &p_pic[i];
1610 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1613 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1614 I_OUTPUTPICTURES = 0;
1615 msg_Err( p_vout, "cannot lock surface" );
1616 return VLC_EGENERIC;
1618 DirectXUnlockSurface( p_vout, &p_pic[i] );
1621 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1622 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1627 /*****************************************************************************
1628 * FreePicture: destroy a picture vector allocated with NewPictureVec
1629 *****************************************************************************
1631 *****************************************************************************/
1632 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1637 vlc_mutex_lock( &p_vout->p_sys->lock );
1638 p_vout->p_sys->p_current_surface = 0;
1639 vlc_mutex_unlock( &p_vout->p_sys->lock );
1641 for( i = 0; i < i_num_pics; i++ )
1643 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1645 for( i = 0; i < i_num_pics; i++ )
1647 free( p_pic[i].p_sys );
1652 /*****************************************************************************
1653 * UpdatePictureStruct: updates the internal data in the picture_t structure
1654 *****************************************************************************
1655 * This will setup stuff for use by the video_output thread
1656 *****************************************************************************/
1657 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1660 switch( p_vout->output.i_chroma )
1662 case VLC_FOURCC('R','G','B','2'):
1663 case VLC_FOURCC('R','V','1','5'):
1664 case VLC_FOURCC('R','V','1','6'):
1665 case VLC_FOURCC('R','V','2','4'):
1666 case VLC_FOURCC('R','V','3','2'):
1667 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1668 p_pic->p->i_lines = p_vout->output.i_height;
1669 p_pic->p->i_visible_lines = p_vout->output.i_height;
1670 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1671 switch( p_vout->output.i_chroma )
1673 case VLC_FOURCC('R','G','B','2'):
1674 p_pic->p->i_pixel_pitch = 1;
1676 case VLC_FOURCC('R','V','1','5'):
1677 case VLC_FOURCC('R','V','1','6'):
1678 p_pic->p->i_pixel_pitch = 2;
1680 case VLC_FOURCC('R','V','2','4'):
1681 p_pic->p->i_pixel_pitch = 3;
1683 case VLC_FOURCC('R','V','3','2'):
1684 p_pic->p->i_pixel_pitch = 4;
1687 return VLC_EGENERIC;
1689 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1690 p_pic->p->i_pixel_pitch;
1691 p_pic->i_planes = 1;
1694 case VLC_FOURCC('Y','V','1','2'):
1695 case VLC_FOURCC('I','4','2','0'):
1697 /* U and V inverted compared to I420
1698 * Fixme: this should be handled by the vout core */
1699 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
1701 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1702 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1703 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1704 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1705 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1706 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1707 p_pic->p[Y_PLANE].i_pixel_pitch;
1709 p_pic->V_PIXELS = p_pic->Y_PIXELS
1710 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1711 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1712 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1713 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1714 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1715 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1716 p_pic->p[V_PLANE].i_pixel_pitch;
1718 p_pic->U_PIXELS = p_pic->V_PIXELS
1719 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1720 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1721 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1722 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1723 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1724 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1725 p_pic->p[U_PLANE].i_pixel_pitch;
1727 p_pic->i_planes = 3;
1730 case VLC_FOURCC('I','Y','U','V'):
1732 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1733 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1734 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1735 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1736 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1737 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1738 p_pic->p[Y_PLANE].i_pixel_pitch;
1740 p_pic->U_PIXELS = p_pic->Y_PIXELS
1741 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1742 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1743 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1744 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1745 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1746 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1747 p_pic->p[U_PLANE].i_pixel_pitch;
1749 p_pic->V_PIXELS = p_pic->U_PIXELS
1750 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1751 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1752 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1753 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1754 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1755 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1756 p_pic->p[V_PLANE].i_pixel_pitch;
1758 p_pic->i_planes = 3;
1761 case VLC_FOURCC('U','Y','V','Y'):
1762 case VLC_FOURCC('Y','U','Y','2'):
1764 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1765 p_pic->p->i_lines = p_vout->output.i_height;
1766 p_pic->p->i_visible_lines = p_vout->output.i_height;
1767 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1768 p_pic->p->i_pixel_pitch = 2;
1769 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1770 p_pic->p->i_pixel_pitch;
1772 p_pic->i_planes = 1;
1776 /* Unknown chroma, tell the guy to get lost */
1777 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1778 p_vout->output.i_chroma,
1779 (char*)&p_vout->output.i_chroma );
1780 return VLC_EGENERIC;
1786 /*****************************************************************************
1787 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1788 *****************************************************************************
1789 * It is nice to know which features are supported by the hardware so we can
1790 * find ways to optimize our rendering.
1791 *****************************************************************************/
1792 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1797 /* This is just an indication of whether or not we'll support overlay,
1798 * but with this test we don't know if we support YUV overlay */
1799 memset( &ddcaps, 0, sizeof( DDCAPS ));
1800 ddcaps.dwSize = sizeof(DDCAPS);
1801 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1803 if(dxresult != DD_OK )
1805 msg_Warn( p_vout, "cannot get caps" );
1809 bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1810 bHasColorKey, bCanStretch, bCanBltFourcc,
1811 bAlignBoundarySrc, bAlignBoundaryDest,
1812 bAlignSizeSrc, bAlignSizeDest;
1814 /* Determine if the hardware supports overlay surfaces */
1815 bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1816 /* Determine if the hardware supports overlay surfaces */
1817 bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1818 /* Determine if the hardware supports overlay deinterlacing */
1819 bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1820 /* Determine if the hardware supports colorkeying */
1821 bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1822 /* Determine if the hardware supports scaling of the overlay surface */
1823 bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1824 /* Determine if the hardware supports color conversion during a blit */
1825 bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1826 /* Determine overlay source boundary alignment */
1827 bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1828 /* Determine overlay destination boundary alignment */
1829 bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1830 /* Determine overlay destination size alignment */
1831 bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1832 /* Determine overlay destination size alignment */
1833 bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1835 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1836 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1838 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1839 bHasColorKey, bCanStretch, bCanBltFourcc );
1841 if( bAlignBoundarySrc || bAlignBoundaryDest ||
1842 bAlignSizeSrc || bAlignSizeDest )
1844 if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1845 ddcaps.dwAlignBoundarySrc;
1846 if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1847 ddcaps.dwAlignBoundaryDest;
1848 if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1849 ddcaps.dwAlignSizeSrc;
1850 if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1851 ddcaps.dwAlignSizeDest;
1853 msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1854 "align_boundary_dest=%i,%i "
1855 "align_size_src=%i,%i align_size_dest=%i,%i",
1856 bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1857 bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1858 bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1859 bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1862 /* Don't ask for troubles */
1863 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1867 /*****************************************************************************
1868 * DirectXLockSurface: Lock surface and get picture data pointer
1869 *****************************************************************************
1870 * This function locks a surface and get the surface descriptor which amongst
1871 * other things has the pointer to the picture data.
1872 *****************************************************************************/
1873 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1877 /* Lock the surface to get a valid pointer to the picture buffer */
1878 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1879 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1880 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1881 NULL, &p_pic->p_sys->ddsd,
1882 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1884 if( dxresult != DD_OK )
1886 if( dxresult == DDERR_INVALIDPARAMS )
1888 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1889 * in an invalid params error */
1890 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1891 &p_pic->p_sys->ddsd,
1894 if( dxresult == DDERR_SURFACELOST )
1896 /* Your surface can be lost so be sure
1897 * to check this and restore it if needed */
1899 /* When using overlays with back-buffers, we need to restore
1900 * the front buffer so the back-buffers get restored as well. */
1901 if( p_vout->p_sys->b_using_overlay )
1902 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1904 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1906 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1907 &p_pic->p_sys->ddsd,
1910 if( dxresult == DDERR_SURFACELOST )
1911 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1914 if( dxresult != DD_OK )
1916 return VLC_EGENERIC;
1920 /* Now we have a pointer to the surface memory, we can update our picture
1922 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1925 DirectXUnlockSurface( p_vout, p_pic );
1926 return VLC_EGENERIC;
1932 /*****************************************************************************
1933 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1934 *****************************************************************************/
1935 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1937 /* Unlock the Surface */
1938 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1941 return VLC_EGENERIC;
1944 /*****************************************************************************
1945 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1946 *****************************************************************************/
1947 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
1952 uint32_t i_pixel_backup;
1955 ddsd.dwSize = sizeof(ddsd);
1956 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1957 &ddsd, DDLOCK_WAIT, NULL );
1958 if( dxresult != DD_OK ) return 0;
1960 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1962 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1965 *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1968 *(uint8_t *)ddsd.lpSurface = *pi_color;
1972 *(uint16_t *)ddsd.lpSurface = *pi_color;
1975 /* Seems to be problematic so we'll just put black as the colorkey */
1978 *(uint32_t *)ddsd.lpSurface = *pi_color;
1982 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1984 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1986 i_rgb = GetPixel( hdc, 0, 0 );
1987 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1990 ddsd.dwSize = sizeof(ddsd);
1991 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1992 &ddsd, DDLOCK_WAIT, NULL );
1993 if( dxresult != DD_OK ) return i_rgb;
1995 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1997 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
2002 /*****************************************************************************
2003 * A few toolbox functions
2004 *****************************************************************************/
2005 void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
2009 if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
2011 hwnd = FindWindow( _T("Progman"), NULL );
2012 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
2013 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
2016 msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
2017 "wallpaper mode not supported" );
2021 p_vout->p_sys->b_wallpaper = b_on;
2023 msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
2025 if( p_vout->p_sys->b_wallpaper )
2027 p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
2028 p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
2030 ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
2031 ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
2035 ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
2036 ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
2039 /* Update desktop */
2040 InvalidateRect( hwnd, NULL, TRUE );
2041 UpdateWindow( hwnd );
2044 /*****************************************************************************
2045 * config variable callback
2046 *****************************************************************************/
2047 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
2048 LPTSTR psz_drivername, VOID* p_context,
2051 module_config_t *p_item = (module_config_t *)p_context;
2054 (char **)realloc( p_item->ppsz_list,
2055 (p_item->i_list+2) * sizeof(char *) );
2056 p_item->ppsz_list_text =
2057 (char **)realloc( p_item->ppsz_list_text,
2058 (p_item->i_list+2) * sizeof(char *) );
2060 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
2061 p_item->ppsz_list_text[p_item->i_list] = NULL;
2063 p_item->ppsz_list[p_item->i_list] = NULL;
2064 p_item->ppsz_list_text[p_item->i_list] = NULL;
2066 return TRUE; /* Keep enumerating */
2069 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2070 vlc_value_t newval, vlc_value_t oldval, void *d)
2072 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
2074 HINSTANCE hddraw_dll;
2076 module_config_t *p_item;
2079 p_item = config_FindConfig( p_this, psz_name );
2080 if( !p_item ) return VLC_SUCCESS;
2082 /* Clear-up the current list */
2083 if( p_item->i_list )
2085 /* Keep the first entry */
2086 for( i = 1; i < p_item->i_list; i++ )
2088 free( p_item->ppsz_list[i] );
2089 free( p_item->ppsz_list_text[i] );
2091 /* TODO: Remove when no more needed */
2092 p_item->ppsz_list[i] = NULL;
2093 p_item->ppsz_list_text[i] = NULL;
2097 /* Load direct draw DLL */
2098 hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
2099 if( hddraw_dll == NULL ) return VLC_SUCCESS;
2101 OurDirectDrawEnumerateEx =
2103 (void *)GetProcAddress( hddraw_dll, "DirectDrawEnumerateExA" );
2105 (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
2108 if( OurDirectDrawEnumerateEx )
2110 /* Enumerate displays */
2111 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
2112 DDENUM_ATTACHEDSECONDARYDEVICES );
2115 FreeLibrary( hddraw_dll );
2117 /* Signal change to the interface */
2118 p_item->b_dirty = true;
2123 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
2124 vlc_value_t oldval, vlc_value_t newval,
2127 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2129 if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
2130 (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
2132 playlist_t *p_playlist = pl_Hold( p_vout );
2136 /* Modify playlist as well because the vout might have to be
2138 var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
2139 var_Set( p_playlist, "directx-wallpaper", newval );
2140 pl_Release( p_vout );
2143 p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
2149 /*****************************************************************************
2150 * SetPalette: sets an 8 bpp palette
2151 *****************************************************************************/
2152 static void SetPalette( vout_thread_t *p_vout,
2153 uint16_t *red, uint16_t *green, uint16_t *blue )
2155 msg_Err( p_vout, "FIXME: SetPalette unimplemented" );