1 /*****************************************************************************
2 * vout.c: Windows DirectX video output display method
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 */
38 #include <stdlib.h> /* free() */
39 #include <string.h> /* strerror() */
50 #undef GetSystemMetrics
52 #ifndef MONITOR_DEFAULTTONEAREST
53 # define MONITOR_DEFAULTTONEAREST 2
58 /*****************************************************************************
60 * Defining them here allows us to get rid of the dxguid library during
62 *****************************************************************************/
64 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
65 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
67 /*****************************************************************************
69 *****************************************************************************/
70 static int OpenVideo ( vlc_object_t * );
71 static void CloseVideo ( vlc_object_t * );
73 static int Init ( vout_thread_t * );
74 static void End ( vout_thread_t * );
75 static int Manage ( vout_thread_t * );
76 static void Display ( vout_thread_t *, picture_t * );
78 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
79 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
80 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
82 static int DirectXInitDDraw ( vout_thread_t *p_vout );
83 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
84 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
85 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
86 static int DirectXCreateSurface ( vout_thread_t *p_vout,
87 LPDIRECTDRAWSURFACE2 *, int, int, int );
88 static void DirectXCloseSurface ( vout_thread_t *p_vout,
89 LPDIRECTDRAWSURFACE2 );
90 static int DirectXCreateClipper ( vout_thread_t *p_vout );
91 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
92 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
93 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
95 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color );
97 void SwitchWallpaperMode( vout_thread_t *, vlc_bool_t );
99 /* Object variables callbacks */
100 static int FindDevicesCallback( vlc_object_t *, char const *,
101 vlc_value_t, vlc_value_t, void * );
102 static int WallpaperCallback( vlc_object_t *, char const *,
103 vlc_value_t, vlc_value_t, void * );
105 /*****************************************************************************
107 *****************************************************************************/
108 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
109 #define HW_YUV_LONGTEXT N_( \
110 "Try to use hardware acceleration for YUV->RGB conversions. " \
111 "This option doesn't have any effect when using overlays." )
113 #define SYSMEM_TEXT N_("Use video buffers in system memory")
114 #define SYSMEM_LONGTEXT N_( \
115 "Create video buffers in system memory instead of video memory. This " \
116 "isn't recommended as usually using video memory allows to benefit from " \
117 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
118 "This option doesn't have any effect when using overlays." )
120 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
121 #define TRIPLEBUF_LONGTEXT N_( \
122 "Try to use triple buffering when using YUV overlays. That results in " \
123 "much better video quality (no flickering)." )
125 #define DEVICE_TEXT N_("Name of desired display device")
126 #define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " \
127 "specify the Windows device name of the display that you want the video " \
128 "window to open on. For example, \"\\\\.\\DISPLAY1\" or " \
129 "\"\\\\.\\DISPLAY2\"." )
131 #define WALLPAPER_TEXT N_("Enable wallpaper mode ")
132 #define WALLPAPER_LONGTEXT N_( \
133 "The wallpaper mode allows you to display the video as the desktop " \
134 "background. Note that this feature only works in overlay mode and " \
135 "the desktop must not already have a wallpaper." )
137 static char *ppsz_dev[] = { "" };
138 static char *ppsz_dev_text[] = { N_("Default") };
141 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
143 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
145 add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT,
146 TRIPLEBUF_LONGTEXT, VLC_TRUE );
148 add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
150 change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback );
151 change_action_add( FindDevicesCallback, N_("Refresh list") );
153 add_bool( "directx-wallpaper", 0, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
156 set_description( _("DirectX video output") );
157 set_capability( "video output", 100 );
158 add_shortcut( "directx" );
159 set_callbacks( OpenVideo, CloseVideo );
163 /* check if we registered a window class because we need to
166 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
167 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
170 /*****************************************************************************
171 * OpenVideo: allocate DirectX video thread output method
172 *****************************************************************************
173 * This function allocates and initialize the DirectX vout method.
174 *****************************************************************************/
175 static int OpenVideo( vlc_object_t *p_this )
177 vout_thread_t * p_vout = (vout_thread_t *)p_this;
181 /* Allocate structure */
182 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
183 if( p_vout->p_sys == NULL )
185 msg_Err( p_vout, "out of memory" );
188 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
190 /* Initialisations */
191 p_vout->pf_init = Init;
192 p_vout->pf_end = End;
193 p_vout->pf_manage = Manage;
194 p_vout->pf_render = NULL;
195 p_vout->pf_display = Display;
197 p_vout->p_sys->p_ddobject = NULL;
198 p_vout->p_sys->p_display = NULL;
199 p_vout->p_sys->p_current_surface = NULL;
200 p_vout->p_sys->p_clipper = NULL;
201 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
202 p_vout->p_sys->hparent = NULL;
203 p_vout->p_sys->i_changes = 0;
204 p_vout->p_sys->b_wallpaper = 0;
205 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
206 SetRectEmpty( &p_vout->p_sys->rect_display );
207 SetRectEmpty( &p_vout->p_sys->rect_parent );
209 /* Multimonitor stuff */
210 p_vout->p_sys->hmonitor = NULL;
211 p_vout->p_sys->p_display_driver = NULL;
212 p_vout->p_sys->MonitorFromWindow = NULL;
213 p_vout->p_sys->GetMonitorInfo = NULL;
214 if( (huser32 = GetModuleHandle( "USER32" ) ) )
216 p_vout->p_sys->MonitorFromWindow =
217 GetProcAddress( huser32, "MonitorFromWindow" );
218 p_vout->p_sys->GetMonitorInfo =
219 GetProcAddress( huser32, "GetMonitorInfoA" );
222 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
223 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
224 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
225 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
226 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
227 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
229 p_vout->p_sys->b_cursor_hidden = 0;
230 p_vout->p_sys->i_lastmoved = mdate();
232 /* Set main window's size */
233 p_vout->p_sys->i_window_width = p_vout->i_window_width;
234 p_vout->p_sys->i_window_height = p_vout->i_window_height;
236 /* Create the DirectXEventThread, this thread is created by us to isolate
237 * the Win32 PeekMessage function calls. We want to do this because
238 * Windows can stay blocked inside this call for a long time, and when
239 * this happens it thus blocks vlc's video_output thread.
240 * DirectXEventThread will take care of the creation of the video
241 * window (because PeekMessage has to be called from the same thread which
242 * created the window). */
243 msg_Dbg( p_vout, "creating DirectXEventThread" );
244 p_vout->p_sys->p_event =
245 vlc_object_create( p_vout, sizeof(event_thread_t) );
246 p_vout->p_sys->p_event->p_vout = p_vout;
247 if( vlc_thread_create( p_vout->p_sys->p_event, "DirectX Events Thread",
248 DirectXEventThread, 0, 1 ) )
250 msg_Err( p_vout, "cannot create DirectXEventThread" );
251 vlc_object_destroy( p_vout->p_sys->p_event );
252 p_vout->p_sys->p_event = NULL;
256 if( p_vout->p_sys->p_event->b_error )
258 msg_Err( p_vout, "DirectXEventThread failed" );
262 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
264 msg_Dbg( p_vout, "DirectXEventThread running" );
266 /* Initialise DirectDraw */
267 if( DirectXInitDDraw( p_vout ) )
269 msg_Err( p_vout, "cannot initialize DirectDraw" );
273 /* Create the directx display */
274 if( DirectXCreateDisplay( p_vout ) )
276 msg_Err( p_vout, "cannot initialize DirectDraw" );
280 /* Variable to indicate if the window should be on top of others */
281 /* Trigger a callback right now */
282 var_Get( p_vout, "video-on-top", &val );
283 var_Set( p_vout, "video-on-top", val );
285 /* Variable to indicate if the window should be on top of others */
286 /* Trigger a callback right now */
287 var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
288 val.psz_string = _("Wallpaper");
289 var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
290 var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
291 var_Get( p_vout, "directx-wallpaper", &val );
292 var_Set( p_vout, "directx-wallpaper", val );
297 CloseVideo( VLC_OBJECT(p_vout) );
301 /*****************************************************************************
302 * Init: initialize DirectX video thread output method
303 *****************************************************************************
304 * This function create the directx surfaces needed by the output thread.
305 * It is called at the beginning of the thread.
306 *****************************************************************************/
307 static int Init( vout_thread_t *p_vout )
312 /* Get a few default parameters */
313 var_Get( p_vout, "overlay", &val );
314 p_vout->p_sys->b_using_overlay = val.b_bool;
315 var_Get( p_vout, "directx-use-sysmem", &val );
316 p_vout->p_sys->b_use_sysmem = val.b_bool;
317 var_Get( p_vout, "directx-hw-yuv", &val );
318 p_vout->p_sys->b_hw_yuv = val.b_bool;
319 var_Get( p_vout, "directx-3buffering", &val );
320 p_vout->p_sys->b_3buf_overlay = val.b_bool;
322 /* Initialise DirectDraw if not already done.
323 * We do this here because on multi-monitor systems we may have to
324 * re-create the directdraw surfaces. */
325 if( !p_vout->p_sys->p_ddobject &&
326 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
328 msg_Err( p_vout, "cannot initialize DirectDraw" );
332 /* Create the directx display */
333 if( !p_vout->p_sys->p_display &&
334 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
336 msg_Err( p_vout, "cannot initialize DirectDraw" );
340 /* Initialize the output structure.
341 * Since DirectDraw can do rescaling for us, stick to the default
342 * coordinates and aspect. */
343 p_vout->output.i_width = p_vout->render.i_width;
344 p_vout->output.i_height = p_vout->render.i_height;
345 p_vout->output.i_aspect = p_vout->render.i_aspect;
347 #define MAX_DIRECTBUFFERS 1
348 /* Right now we use only 1 directbuffer because we don't want the
349 * video decoder to decode directly into direct buffers as they are
350 * created into video memory and video memory is _really_ slow */
352 /* Choose the chroma we will try first. */
353 switch( p_vout->render.i_chroma )
355 case VLC_FOURCC('Y','U','Y','2'):
356 case VLC_FOURCC('Y','U','N','V'):
357 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
359 case VLC_FOURCC('U','Y','V','Y'):
360 case VLC_FOURCC('U','Y','N','V'):
361 case VLC_FOURCC('Y','4','2','2'):
362 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
364 case VLC_FOURCC('Y','V','Y','U'):
365 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
368 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
372 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
374 i_chroma_backup = p_vout->output.i_chroma;
376 if( !I_OUTPUTPICTURES )
378 /* hmmm, it didn't work! Let's try commonly supported chromas */
379 if( p_vout->output.i_chroma != VLC_FOURCC('Y','V','1','2') )
381 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
382 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
384 if( !I_OUTPUTPICTURES )
386 /* hmmm, it still didn't work! Let's try another one */
387 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
388 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
392 if( !I_OUTPUTPICTURES )
394 /* If it still didn't work then don't try to use an overlay */
395 p_vout->output.i_chroma = i_chroma_backup;
396 p_vout->p_sys->b_using_overlay = 0;
397 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
400 /* Change the window title bar text */
401 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
406 /*****************************************************************************
407 * End: terminate Sys video thread output method
408 *****************************************************************************
409 * Terminate an output method created by Create.
410 * It is called at the end of the thread.
411 *****************************************************************************/
412 static void End( vout_thread_t *p_vout )
414 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
416 DirectXCloseDisplay( p_vout );
417 DirectXCloseDDraw( p_vout );
422 /*****************************************************************************
423 * CloseVideo: destroy Sys video thread output method
424 *****************************************************************************
425 * Terminate an output method created by Create
426 *****************************************************************************/
427 static void CloseVideo( vlc_object_t *p_this )
429 vout_thread_t * p_vout = (vout_thread_t *)p_this;
431 msg_Dbg( p_vout, "CloseVideo" );
433 if( p_vout->p_sys->p_event )
435 vlc_object_detach( p_vout->p_sys->p_event );
437 /* Kill DirectXEventThread */
438 p_vout->p_sys->p_event->b_die = VLC_TRUE;
440 /* we need to be sure DirectXEventThread won't stay stuck in
441 * GetMessage, so we send a fake message */
442 if( p_vout->p_sys->hwnd )
444 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
447 vlc_thread_join( p_vout->p_sys->p_event );
448 vlc_object_destroy( p_vout->p_sys->p_event );
451 vlc_mutex_destroy( &p_vout->p_sys->lock );
453 /* Make sure the wallpaper is restored */
454 SwitchWallpaperMode( p_vout, VLC_FALSE );
458 free( p_vout->p_sys );
459 p_vout->p_sys = NULL;
463 /*****************************************************************************
464 * Manage: handle Sys events
465 *****************************************************************************
466 * This function should be called regularly by the video output thread.
467 * It returns a non null value if an error occurred.
468 *****************************************************************************/
469 static int Manage( vout_thread_t *p_vout )
471 WINDOWPLACEMENT window_placement;
473 /* If we do not control our window, we check for geometry changes
474 * ourselves because the parent might not send us its events. */
475 vlc_mutex_lock( &p_vout->p_sys->lock );
476 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
481 vlc_mutex_unlock( &p_vout->p_sys->lock );
483 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
484 point.x = point.y = 0;
485 ClientToScreen( p_vout->p_sys->hparent, &point );
486 OffsetRect( &rect_parent, point.x, point.y );
488 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
490 p_vout->p_sys->rect_parent = rect_parent;
492 /* This one is to force the update even if only
493 * the position has changed */
494 SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
495 rect_parent.right - rect_parent.left,
496 rect_parent.bottom - rect_parent.top, 0 );
498 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
499 rect_parent.right - rect_parent.left,
500 rect_parent.bottom - rect_parent.top, 0 );
505 vlc_mutex_unlock( &p_vout->p_sys->lock );
511 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
513 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
515 /* Check if we are still on the same monitor */
516 if( p_vout->p_sys->MonitorFromWindow &&
517 p_vout->p_sys->hmonitor !=
518 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
519 MONITOR_DEFAULTTONEAREST ) )
521 /* This will force the vout core to recreate the picture buffers */
522 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
526 /* We used to call the Win32 PeekMessage function here to read the window
527 * messages. But since window can stay blocked into this function for a
528 * long time (for example when you move your window on the screen), I
529 * decided to isolate PeekMessage in another thread. */
531 if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
533 SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
534 p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
535 DirectXUpdateOverlay( p_vout );
541 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
542 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
547 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
549 /* We need to switch between Maximized and Normal sized window */
550 window_placement.length = sizeof(WINDOWPLACEMENT);
551 if( p_vout->b_fullscreen )
553 if( p_vout->p_sys->hparent )
557 /* Retrieve the window position */
558 point.x = point.y = 0;
559 ClientToScreen( p_vout->p_sys->hwnd, &point );
560 SetParent( p_vout->p_sys->hwnd, GetDesktopWindow() );
561 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
562 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
563 SetForegroundWindow( p_vout->p_sys->hwnd );
566 /* Maximized window */
567 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
568 window_placement.showCmd = SW_SHOWMAXIMIZED;
569 /* Change window style, no borders and no title bar */
570 i_style = WS_CLIPCHILDREN | WS_VISIBLE | WS_POPUP;
574 if( p_vout->p_sys->hparent )
576 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
577 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0, 0, 0,
578 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
579 i_style = WS_CLIPCHILDREN | WS_VISIBLE | WS_CHILD;
580 SetForegroundWindow( p_vout->p_sys->hparent );
584 i_style = WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW |
585 WS_SIZEBOX | WS_VISIBLE;
589 GetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
590 window_placement.showCmd = SW_SHOWNORMAL;
592 /* Make sure the mouse cursor is displayed */
593 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
596 /* Change window style, borders and title bar */
597 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE, i_style );
598 SetWindowPlacement( p_vout->p_sys->hwnd, &window_placement );
599 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0, 0, 0,
600 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
602 /* Update the object variable and trigger callback */
603 val.b_bool = p_vout->b_fullscreen;
604 var_Set( p_vout, "fullscreen", val );
606 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
607 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
613 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
614 (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 )
619 /* Hide the cursor only if it is inside our window */
620 GetCursorPos( &point );
621 hwnd = WindowFromPoint(point);
622 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
624 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
628 p_vout->p_sys->i_lastmoved = mdate();
633 * "Always on top" status change
635 if( p_vout->p_sys->b_on_top_change )
638 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
640 var_Get( p_vout, "video-on-top", &val );
642 /* Set the window on top if necessary */
643 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
646 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
647 MF_BYCOMMAND | MFS_CHECKED );
648 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
649 SWP_NOSIZE | SWP_NOMOVE );
652 /* The window shouldn't be on top */
653 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
656 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
657 MF_BYCOMMAND | MFS_UNCHECKED );
658 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
659 SWP_NOSIZE | SWP_NOMOVE );
662 p_vout->p_sys->b_on_top_change = VLC_FALSE;
665 /* Check if the event thread is still running */
666 if( p_vout->p_sys->p_event->b_die )
668 return VLC_EGENERIC; /* exit */
674 /*****************************************************************************
675 * Display: displays previously rendered output
676 *****************************************************************************
677 * This function sends the currently rendered image to the display, wait until
678 * it is displayed and switch the two rendering buffers, preparing next frame.
679 *****************************************************************************/
680 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
684 if( (p_vout->p_sys->p_display == NULL) )
686 msg_Warn( p_vout, "no display!" );
690 /* Our surface can be lost so be sure to check this
691 * and restore it if need be */
692 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
693 == DDERR_SURFACELOST )
695 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
696 p_vout->p_sys->b_using_overlay )
697 DirectXUpdateOverlay( p_vout );
700 if( !p_vout->p_sys->b_using_overlay )
704 /* We ask for the "NOTEARING" option */
705 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
706 ddbltfx.dwSize = sizeof(DDBLTFX);
707 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
709 /* Blit video surface to display */
710 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
711 &p_vout->p_sys->rect_dest_clipped,
712 p_pic->p_sys->p_surface,
713 &p_vout->p_sys->rect_src_clipped,
714 DDBLT_ASYNC, &ddbltfx );
715 if( dxresult != DD_OK )
717 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
722 else /* using overlay */
724 /* Flip the overlay buffers if we are using back buffers */
725 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
730 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
732 if( dxresult != DD_OK )
734 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
737 /* set currently displayed pic */
738 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
740 /* Lock surface to get all the required info */
741 if( DirectXLockSurface( p_vout, p_pic ) )
744 msg_Warn( p_vout, "cannot lock surface" );
747 DirectXUnlockSurface( p_vout, p_pic );
751 /* following functions are local */
753 /*****************************************************************************
754 * DirectXEnumCallback: Device enumeration
755 *****************************************************************************
756 * This callback function is called by DirectDraw once for each
757 * available DirectDraw device.
758 *****************************************************************************/
759 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
760 LPTSTR psz_drivername, VOID* p_context,
763 vout_thread_t *p_vout = (vout_thread_t *)p_context;
766 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
770 var_Get( p_vout, "directx-device", &device );
772 if( ( !device.psz_string || !*device.psz_string ) &&
773 hmon == p_vout->p_sys->hmonitor )
775 if( device.psz_string ) free( device.psz_string );
777 else if( strcmp( psz_drivername, device.psz_string ) == 0 )
779 MONITORINFO monitor_info;
780 monitor_info.cbSize = sizeof( MONITORINFO );
782 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
786 /* Move window to the right screen */
787 GetWindowRect( p_vout->p_sys->hwnd, &rect );
788 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
790 rect.left = monitor_info.rcWork.left;
791 rect.top = monitor_info.rcWork.top;
792 msg_Dbg( p_vout, "DirectXEnumCallback: Setting window "
793 "position to %d,%d", rect.left, rect.top );
794 SetWindowPos( p_vout->p_sys->hwnd, NULL,
795 rect.left, rect.top, 0, 0,
796 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
800 p_vout->p_sys->hmonitor = hmon;
801 if( device.psz_string ) free( device.psz_string );
805 if( device.psz_string ) free( device.psz_string );
806 return TRUE; /* Keep enumerating */
809 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
810 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
811 if( p_vout->p_sys->p_display_driver )
812 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
815 return TRUE; /* Keep enumerating */
818 /*****************************************************************************
819 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
820 *****************************************************************************
821 * This function initialise and allocate resources for DirectDraw.
822 *****************************************************************************/
823 static int DirectXInitDDraw( vout_thread_t *p_vout )
826 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
827 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
829 LPDIRECTDRAW p_ddobject;
831 msg_Dbg( p_vout, "DirectXInitDDraw" );
833 /* Load direct draw DLL */
834 p_vout->p_sys->hddraw_dll = LoadLibrary("DDRAW.DLL");
835 if( p_vout->p_sys->hddraw_dll == NULL )
837 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
841 OurDirectDrawCreate =
842 (void *)GetProcAddress(p_vout->p_sys->hddraw_dll, "DirectDrawCreate");
843 if( OurDirectDrawCreate == NULL )
845 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
849 OurDirectDrawEnumerateEx =
850 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
851 "DirectDrawEnumerateExA" );
853 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
857 var_Get( p_vout, "directx-device", &device );
858 if( device.psz_string )
860 msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
861 free( device.psz_string );
864 p_vout->p_sys->hmonitor =
865 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
866 MONITOR_DEFAULTTONEAREST );
868 /* Enumerate displays */
869 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
870 DDENUM_ATTACHEDSECONDARYDEVICES );
873 /* Initialize DirectDraw now */
874 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
876 if( dxresult != DD_OK )
878 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
882 /* Get the IDirectDraw2 interface */
883 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
884 (LPVOID *)&p_vout->p_sys->p_ddobject );
885 /* Release the unused interface */
886 IDirectDraw_Release( p_ddobject );
887 if( dxresult != DD_OK )
889 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
893 /* Set DirectDraw Cooperative level, ie what control we want over Windows
895 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
896 NULL, DDSCL_NORMAL );
897 if( dxresult != DD_OK )
899 msg_Err( p_vout, "cannot set direct draw cooperative level" );
903 /* Get the size of the current display device */
904 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
906 MONITORINFO monitor_info;
907 monitor_info.cbSize = sizeof( MONITORINFO );
908 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
910 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
914 p_vout->p_sys->rect_display.left = 0;
915 p_vout->p_sys->rect_display.top = 0;
916 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
917 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
920 msg_Dbg( p_vout, "screen dimensions (%ix%i,%ix%i)",
921 p_vout->p_sys->rect_display.left,
922 p_vout->p_sys->rect_display.top,
923 p_vout->p_sys->rect_display.right,
924 p_vout->p_sys->rect_display.bottom );
926 /* Probe the capabilities of the hardware */
927 DirectXGetDDrawCaps( p_vout );
929 msg_Dbg( p_vout, "End DirectXInitDDraw" );
933 if( p_vout->p_sys->p_ddobject )
934 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
935 if( p_vout->p_sys->hddraw_dll )
936 FreeLibrary( p_vout->p_sys->hddraw_dll );
937 p_vout->p_sys->hddraw_dll = NULL;
938 p_vout->p_sys->p_ddobject = NULL;
942 /*****************************************************************************
943 * DirectXCreateDisplay: create the DirectDraw display.
944 *****************************************************************************
945 * Create and initialize display according to preferences specified in the vout
947 *****************************************************************************/
948 static int DirectXCreateDisplay( vout_thread_t *p_vout )
952 LPDIRECTDRAWSURFACE p_display;
954 msg_Dbg( p_vout, "DirectXCreateDisplay" );
956 /* Now get the primary surface. This surface is what you actually see
958 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
959 ddsd.dwSize = sizeof(DDSURFACEDESC);
960 ddsd.dwFlags = DDSD_CAPS;
961 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
963 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
964 &ddsd, &p_display, NULL );
965 if( dxresult != DD_OK )
967 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
971 dxresult = IDirectDrawSurface_QueryInterface( p_display,
972 &IID_IDirectDrawSurface2,
973 (LPVOID *)&p_vout->p_sys->p_display );
974 /* Release the old interface */
975 IDirectDrawSurface_Release( p_display );
976 if ( dxresult != DD_OK )
978 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
979 "(error %li)", dxresult );
983 /* The clipper will be used only in non-overlay mode */
984 DirectXCreateClipper( p_vout );
986 /* Make sure the colorkey will be painted */
987 p_vout->p_sys->i_colorkey = 1;
988 p_vout->p_sys->i_rgb_colorkey =
989 DirectXFindColorkey( p_vout, p_vout->p_sys->i_colorkey );
991 /* Create the actual brush */
992 SetClassLong( p_vout->p_sys->hvideownd, GCL_HBRBACKGROUND,
993 (LONG)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
994 InvalidateRect( p_vout->p_sys->hvideownd, NULL, TRUE );
995 DirectXUpdateRects( p_vout, VLC_TRUE );
1000 /*****************************************************************************
1001 * DirectXCreateClipper: Create a clipper that will be used when blitting the
1002 * RGB surface to the main display.
1003 *****************************************************************************
1004 * This clipper prevents us to modify by mistake anything on the screen
1005 * which doesn't belong to our window. For example when a part of our video
1006 * window is hidden by another window.
1007 *****************************************************************************/
1008 static int DirectXCreateClipper( vout_thread_t *p_vout )
1012 msg_Dbg( p_vout, "DirectXCreateClipper" );
1014 /* Create the clipper */
1015 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
1016 &p_vout->p_sys->p_clipper, NULL );
1017 if( dxresult != DD_OK )
1019 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
1023 /* Associate the clipper to the window */
1024 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
1025 p_vout->p_sys->hvideownd );
1026 if( dxresult != DD_OK )
1028 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
1033 /* associate the clipper with the surface */
1034 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
1035 p_vout->p_sys->p_clipper);
1036 if( dxresult != DD_OK )
1038 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
1046 if( p_vout->p_sys->p_clipper )
1048 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1050 p_vout->p_sys->p_clipper = NULL;
1051 return VLC_EGENERIC;
1054 /*****************************************************************************
1055 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
1056 *****************************************************************************
1057 * The best method of display is with an YUV overlay because the YUV->RGB
1058 * conversion is done in hardware.
1059 * You can also create a plain RGB surface.
1060 * ( Maybe we could also try an RGB overlay surface, which could have hardware
1061 * scaling and which would also be faster in window mode because you don't
1062 * need to do any blitting to the main display...)
1063 *****************************************************************************/
1064 static int DirectXCreateSurface( vout_thread_t *p_vout,
1065 LPDIRECTDRAWSURFACE2 *pp_surface_final,
1066 int i_chroma, int b_overlay,
1070 LPDIRECTDRAWSURFACE p_surface;
1073 /* Create the video surface */
1076 /* Now try to create the YUV overlay surface.
1077 * This overlay will be displayed on top of the primary surface.
1078 * A color key is used to determine whether or not the overlay will be
1079 * displayed, ie the overlay will be displayed in place of the primary
1080 * surface wherever the primary surface will have this color.
1081 * The video window has been created with a background of this color so
1082 * the overlay will be only displayed on top of this window */
1084 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
1085 ddsd.dwSize = sizeof(DDSURFACEDESC);
1086 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1087 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1088 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1089 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1090 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
1091 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
1092 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
1094 ddsd.dwHeight = p_vout->render.i_height;
1095 ddsd.dwWidth = p_vout->render.i_width;
1096 ddsd.dwBackBufferCount = i_backbuffers;
1098 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1099 &ddsd, &p_surface, NULL );
1100 if( dxresult != DD_OK )
1102 *pp_surface_final = NULL;
1103 return VLC_EGENERIC;
1109 vlc_bool_t b_rgb_surface =
1110 ( i_chroma == VLC_FOURCC('R','G','B','2') )
1111 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
1112 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
1113 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
1114 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
1116 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
1117 ddsd.dwSize = sizeof(DDSURFACEDESC);
1118 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1119 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
1120 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1121 ddsd.dwHeight = p_vout->render.i_height;
1122 ddsd.dwWidth = p_vout->render.i_width;
1124 if( p_vout->p_sys->b_use_sysmem )
1125 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1127 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
1129 if( !b_rgb_surface )
1131 ddsd.dwFlags |= DDSD_PIXELFORMAT;
1132 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1133 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1136 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1137 &ddsd, &p_surface, NULL );
1138 if( dxresult != DD_OK )
1140 *pp_surface_final = NULL;
1141 return VLC_EGENERIC;
1145 /* Now that the surface is created, try to get a newer DirectX interface */
1146 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
1147 &IID_IDirectDrawSurface2,
1148 (LPVOID *)pp_surface_final );
1149 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
1150 if ( dxresult != DD_OK )
1152 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
1153 "(error %li)", dxresult );
1154 *pp_surface_final = NULL;
1155 return VLC_EGENERIC;
1160 /* Check the overlay is useable as some graphics cards allow creating
1161 * several overlays but only one can be used at one time. */
1162 p_vout->p_sys->p_current_surface = *pp_surface_final;
1163 if( DirectXUpdateOverlay( p_vout ) != VLC_SUCCESS )
1165 IDirectDrawSurface2_Release( *pp_surface_final );
1166 *pp_surface_final = NULL;
1167 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1168 return VLC_EGENERIC;
1175 /*****************************************************************************
1176 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
1177 *****************************************************************************
1178 * This function is used to move or resize an overlay surface on the screen.
1179 * Ususally the overlay is moved by the user and thus, by a move or resize
1180 * event (in Manage).
1181 *****************************************************************************/
1182 int DirectXUpdateOverlay( vout_thread_t *p_vout )
1187 RECT rect_src = p_vout->p_sys->rect_src_clipped;
1188 RECT rect_dest = p_vout->p_sys->rect_dest_clipped;
1190 if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1192 if( p_vout->p_sys->b_wallpaper )
1194 int i_x, i_y, i_width, i_height;
1196 rect_src.left = rect_src.top = 0;
1197 rect_src.right = p_vout->render.i_width;
1198 rect_src.bottom = p_vout->render.i_height;
1200 rect_dest = p_vout->p_sys->rect_display;
1201 vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1202 &i_x, &i_y, &i_width, &i_height );
1204 rect_dest.left += i_x;
1205 rect_dest.right = rect_dest.left + i_width;
1206 rect_dest.top += i_y;
1207 rect_dest.bottom = rect_dest.top + i_height;
1210 vlc_mutex_lock( &p_vout->p_sys->lock );
1211 if( p_vout->p_sys->p_current_surface == NULL )
1213 vlc_mutex_unlock( &p_vout->p_sys->lock );
1214 return VLC_EGENERIC;
1217 /* The new window dimensions should already have been computed by the
1218 * caller of this function */
1220 /* Position and show the overlay */
1221 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1222 ddofx.dwSize = sizeof(DDOVERLAYFX);
1223 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1224 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1226 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1228 dxresult = IDirectDrawSurface2_UpdateOverlay(
1229 p_vout->p_sys->p_current_surface,
1230 &rect_src, p_vout->p_sys->p_display, &rect_dest,
1233 vlc_mutex_unlock( &p_vout->p_sys->lock );
1235 if(dxresult != DD_OK)
1237 msg_Warn( p_vout, "DirectXUpdateOverlay cannot move/resize overlay" );
1238 return VLC_EGENERIC;
1244 /*****************************************************************************
1245 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1246 *****************************************************************************
1247 * This function returns all resources allocated by DirectXInitDDraw.
1248 *****************************************************************************/
1249 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1251 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1252 if( p_vout->p_sys->p_ddobject != NULL )
1254 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1255 p_vout->p_sys->p_ddobject = NULL;
1258 if( p_vout->p_sys->hddraw_dll != NULL )
1260 FreeLibrary( p_vout->p_sys->hddraw_dll );
1261 p_vout->p_sys->hddraw_dll = NULL;
1264 if( p_vout->p_sys->p_display_driver != NULL )
1266 free( p_vout->p_sys->p_display_driver );
1267 p_vout->p_sys->p_display_driver = NULL;
1270 p_vout->p_sys->hmonitor = NULL;
1273 /*****************************************************************************
1274 * DirectXCloseDisplay: close and reset the DirectX display device
1275 *****************************************************************************
1276 * This function returns all resources allocated by DirectXCreateDisplay.
1277 *****************************************************************************/
1278 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1280 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1282 if( p_vout->p_sys->p_clipper != NULL )
1284 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1285 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1286 p_vout->p_sys->p_clipper = NULL;
1289 if( p_vout->p_sys->p_display != NULL )
1291 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1292 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1293 p_vout->p_sys->p_display = NULL;
1297 /*****************************************************************************
1298 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1299 *****************************************************************************
1300 * This function returns all resources allocated for the surface.
1301 *****************************************************************************/
1302 static void DirectXCloseSurface( vout_thread_t *p_vout,
1303 LPDIRECTDRAWSURFACE2 p_surface )
1305 msg_Dbg( p_vout, "DirectXCloseSurface" );
1306 if( p_surface != NULL )
1308 IDirectDrawSurface2_Release( p_surface );
1312 /*****************************************************************************
1313 * NewPictureVec: allocate a vector of identical pictures
1314 *****************************************************************************
1315 * Returns 0 on success, -1 otherwise
1316 *****************************************************************************/
1317 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1321 int i_ret = VLC_SUCCESS;
1322 LPDIRECTDRAWSURFACE2 p_surface;
1324 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1325 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1326 (char *)&p_vout->output.i_chroma );
1328 I_OUTPUTPICTURES = 0;
1330 /* First we try to use an YUV overlay surface.
1331 * The overlay surface that we create won't be used to decode directly
1332 * into it because accessing video memory directly is way to slow (remember
1333 * that pictures are decoded macroblock per macroblock). Instead the video
1334 * will be decoded in picture buffers in system memory which will then be
1335 * memcpy() to the overlay surface. */
1336 if( p_vout->p_sys->b_using_overlay )
1338 /* Triple buffering rocks! it doesn't have any processing overhead
1339 * (you don't have to wait for the vsync) and provides for a very nice
1340 * video quality (no tearing). */
1341 if( p_vout->p_sys->b_3buf_overlay )
1342 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1343 p_vout->output.i_chroma,
1344 p_vout->p_sys->b_using_overlay,
1345 2 /* number of backbuffers */ );
1347 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1349 /* Try to reduce the number of backbuffers */
1350 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1351 p_vout->output.i_chroma,
1352 p_vout->p_sys->b_using_overlay,
1353 0 /* number of backbuffers */ );
1356 if( i_ret == VLC_SUCCESS )
1359 picture_t front_pic;
1360 picture_sys_t front_pic_sys;
1361 front_pic.p_sys = &front_pic_sys;
1363 /* Allocate internal structure */
1364 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1365 if( p_pic[0].p_sys == NULL )
1367 DirectXCloseSurface( p_vout, p_surface );
1371 /* set front buffer */
1372 p_pic[0].p_sys->p_front_surface = p_surface;
1374 /* Get the back buffer */
1375 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1376 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1377 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1378 p_surface, &dds_caps,
1379 &p_pic[0].p_sys->p_surface ) )
1381 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1382 /* front buffer is the same as back buffer */
1383 p_pic[0].p_sys->p_surface = p_surface;
1387 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1388 p_pic[0].p_sys->p_front_surface;
1390 /* Reset the front buffer memory */
1391 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1394 for( i = 0; i < front_pic.i_planes; i++ )
1395 for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1396 memset( front_pic.p[i].p_pixels + j *
1397 front_pic.p[i].i_pitch, 127,
1398 front_pic.p[i].i_visible_pitch );
1400 DirectXUnlockSurface( p_vout, &front_pic );
1403 DirectXUpdateOverlay( p_vout );
1404 I_OUTPUTPICTURES = 1;
1405 msg_Dbg( p_vout, "YUV overlay created successfully" );
1409 /* As we can't have an overlay, we'll try to create a plain offscreen
1410 * surface. This surface will reside in video memory because there's a
1411 * better chance then that we'll be able to use some kind of hardware
1412 * acceleration like rescaling, blitting or YUV->RGB conversions.
1413 * We then only need to blit this surface onto the main display when we
1414 * want to display it */
1415 if( !p_vout->p_sys->b_using_overlay )
1417 if( p_vout->p_sys->b_hw_yuv )
1421 vlc_bool_t b_result = VLC_FALSE;
1423 /* Check if the chroma is supported first. This is required
1424 * because a few buggy drivers don't mind creating the surface
1425 * even if they don't know about the chroma. */
1426 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1427 &i_codes, NULL ) == DD_OK )
1429 pi_codes = malloc( i_codes * sizeof(DWORD) );
1430 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1431 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1433 for( i = 0; i < (int)i_codes; i++ )
1435 if( p_vout->output.i_chroma == pi_codes[i] )
1437 b_result = VLC_TRUE;
1445 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1446 p_vout->output.i_chroma,
1448 0 /* no back buffers */ );
1450 p_vout->p_sys->b_hw_yuv = VLC_FALSE;
1453 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1455 /* Our last choice is to use a plain RGB surface */
1456 DDPIXELFORMAT ddpfPixelFormat;
1458 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1459 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1462 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1464 switch( ddpfPixelFormat.dwRGBBitCount )
1466 case 8: /* FIXME: set the palette */
1467 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1470 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1473 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1476 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1479 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1482 msg_Err( p_vout, "unknown screen depth" );
1483 return VLC_EGENERIC;
1485 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1486 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1487 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1490 p_vout->p_sys->b_hw_yuv = 0;
1492 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1493 p_vout->output.i_chroma,
1495 0 /* no back buffers */ );
1497 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1499 /* Give it a last try with b_use_sysmem enabled */
1500 p_vout->p_sys->b_use_sysmem = 1;
1502 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1503 p_vout->output.i_chroma,
1505 0 /* no back buffers */ );
1509 if( i_ret == VLC_SUCCESS )
1511 /* Allocate internal structure */
1512 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1513 if( p_pic[0].p_sys == NULL )
1515 DirectXCloseSurface( p_vout, p_surface );
1519 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1522 I_OUTPUTPICTURES = 1;
1524 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1525 (char *)&p_vout->output.i_chroma );
1530 /* Now that we've got all our direct-buffers, we can finish filling in the
1531 * picture_t structures */
1532 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1534 p_pic[i].i_status = DESTROYED_PICTURE;
1535 p_pic[i].i_type = DIRECT_PICTURE;
1536 p_pic[i].b_slow = VLC_TRUE;
1537 p_pic[i].pf_lock = DirectXLockSurface;
1538 p_pic[i].pf_unlock = DirectXUnlockSurface;
1539 PP_OUTPUTPICTURE[i] = &p_pic[i];
1541 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1544 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1545 I_OUTPUTPICTURES = 0;
1546 msg_Err( p_vout, "cannot lock surface" );
1547 return VLC_EGENERIC;
1549 DirectXUnlockSurface( p_vout, &p_pic[i] );
1552 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1553 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1558 /*****************************************************************************
1559 * FreePicture: destroy a picture vector allocated with NewPictureVec
1560 *****************************************************************************
1562 *****************************************************************************/
1563 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1568 vlc_mutex_lock( &p_vout->p_sys->lock );
1569 p_vout->p_sys->p_current_surface = 0;
1570 vlc_mutex_unlock( &p_vout->p_sys->lock );
1572 for( i = 0; i < i_num_pics; i++ )
1574 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1576 for( i = 0; i < i_num_pics; i++ )
1578 free( p_pic[i].p_sys );
1583 /*****************************************************************************
1584 * UpdatePictureStruct: updates the internal data in the picture_t structure
1585 *****************************************************************************
1586 * This will setup stuff for use by the video_output thread
1587 *****************************************************************************/
1588 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1591 switch( p_vout->output.i_chroma )
1593 case VLC_FOURCC('R','G','B','2'):
1594 case VLC_FOURCC('R','V','1','5'):
1595 case VLC_FOURCC('R','V','1','6'):
1596 case VLC_FOURCC('R','V','2','4'):
1597 case VLC_FOURCC('R','V','3','2'):
1598 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1599 p_pic->p->i_lines = p_vout->output.i_height;
1600 p_pic->p->i_visible_lines = p_vout->output.i_height;
1601 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1602 switch( p_vout->output.i_chroma )
1604 case VLC_FOURCC('R','G','B','2'):
1605 p_pic->p->i_pixel_pitch = 1;
1607 case VLC_FOURCC('R','V','1','5'):
1608 case VLC_FOURCC('R','V','1','6'):
1609 p_pic->p->i_pixel_pitch = 2;
1611 case VLC_FOURCC('R','V','2','4'):
1612 p_pic->p->i_pixel_pitch = 3;
1614 case VLC_FOURCC('R','V','3','2'):
1615 p_pic->p->i_pixel_pitch = 4;
1618 return VLC_EGENERIC;
1620 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1621 p_pic->p->i_pixel_pitch;
1622 p_pic->i_planes = 1;
1625 case VLC_FOURCC('Y','V','1','2'):
1627 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1628 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1629 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1630 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1631 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1632 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1633 p_pic->p[Y_PLANE].i_pixel_pitch;
1635 p_pic->V_PIXELS = p_pic->Y_PIXELS
1636 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1637 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1638 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1639 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1640 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1641 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1642 p_pic->p[V_PLANE].i_pixel_pitch;
1644 p_pic->U_PIXELS = p_pic->V_PIXELS
1645 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1646 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1647 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1648 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1649 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1650 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1651 p_pic->p[U_PLANE].i_pixel_pitch;
1653 p_pic->i_planes = 3;
1656 case VLC_FOURCC('I','Y','U','V'):
1658 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1659 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1660 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1661 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1662 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1663 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1664 p_pic->p[Y_PLANE].i_pixel_pitch;
1666 p_pic->U_PIXELS = p_pic->Y_PIXELS
1667 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1668 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1669 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1670 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1671 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1672 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1673 p_pic->p[U_PLANE].i_pixel_pitch;
1675 p_pic->V_PIXELS = p_pic->U_PIXELS
1676 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1677 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1678 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1679 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1680 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1681 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1682 p_pic->p[V_PLANE].i_pixel_pitch;
1684 p_pic->i_planes = 3;
1687 case VLC_FOURCC('U','Y','V','Y'):
1688 case VLC_FOURCC('Y','U','Y','2'):
1690 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1691 p_pic->p->i_lines = p_vout->output.i_height;
1692 p_pic->p->i_visible_lines = p_vout->output.i_height;
1693 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1694 p_pic->p->i_pixel_pitch = 2;
1695 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1696 p_pic->p->i_pixel_pitch;
1698 p_pic->i_planes = 1;
1702 /* Unknown chroma, tell the guy to get lost */
1703 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1704 p_vout->output.i_chroma,
1705 (char*)&p_vout->output.i_chroma );
1706 return VLC_EGENERIC;
1712 /*****************************************************************************
1713 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1714 *****************************************************************************
1715 * It is nice to know which features are supported by the hardware so we can
1716 * find ways to optimize our rendering.
1717 *****************************************************************************/
1718 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1723 /* This is just an indication of whether or not we'll support overlay,
1724 * but with this test we don't know if we support YUV overlay */
1725 memset( &ddcaps, 0, sizeof( DDCAPS ));
1726 ddcaps.dwSize = sizeof(DDCAPS);
1727 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1729 if(dxresult != DD_OK )
1731 msg_Warn( p_vout, "cannot get caps" );
1735 vlc_bool_t bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1736 bHasColorKey, bCanStretch, bCanBltFourcc,
1737 bAlignBoundarySrc, bAlignBoundaryDest,
1738 bAlignSizeSrc, bAlignSizeDest;
1740 /* Determine if the hardware supports overlay surfaces */
1741 bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1742 /* Determine if the hardware supports overlay surfaces */
1743 bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1744 /* Determine if the hardware supports overlay deinterlacing */
1745 bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1746 /* Determine if the hardware supports colorkeying */
1747 bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1748 /* Determine if the hardware supports scaling of the overlay surface */
1749 bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1750 /* Determine if the hardware supports color conversion during a blit */
1751 bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1752 /* Determine overlay source boundary alignment */
1753 bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1754 /* Determine overlay destination boundary alignment */
1755 bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1756 /* Determine overlay destination size alignment */
1757 bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1758 /* Determine overlay destination size alignment */
1759 bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1761 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1762 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1764 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1765 bHasColorKey, bCanStretch, bCanBltFourcc );
1767 if( bAlignBoundarySrc || bAlignBoundaryDest ||
1768 bAlignSizeSrc || bAlignSizeDest )
1770 if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1771 ddcaps.dwAlignBoundarySrc;
1772 if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1773 ddcaps.dwAlignBoundaryDest;
1774 if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1775 ddcaps.dwAlignSizeSrc;
1776 if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1777 ddcaps.dwAlignSizeDest;
1779 msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1780 "align_boundary_dest=%i,%i "
1781 "align_size_src=%i,%i align_size_dest=%i,%i",
1782 bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1783 bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1784 bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1785 bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1788 /* Don't ask for troubles */
1789 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1793 /*****************************************************************************
1794 * DirectXLockSurface: Lock surface and get picture data pointer
1795 *****************************************************************************
1796 * This function locks a surface and get the surface descriptor which amongst
1797 * other things has the pointer to the picture data.
1798 *****************************************************************************/
1799 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1803 /* Lock the surface to get a valid pointer to the picture buffer */
1804 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1805 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1806 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1807 NULL, &p_pic->p_sys->ddsd,
1808 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1810 if( dxresult != DD_OK )
1812 if( dxresult == DDERR_INVALIDPARAMS )
1814 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1815 * in an invalid params error */
1816 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1817 &p_pic->p_sys->ddsd,
1820 if( dxresult == DDERR_SURFACELOST )
1822 /* Your surface can be lost so be sure
1823 * to check this and restore it if needed */
1825 /* When using overlays with back-buffers, we need to restore
1826 * the front buffer so the back-buffers get restored as well. */
1827 if( p_vout->p_sys->b_using_overlay )
1828 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1830 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1832 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1833 &p_pic->p_sys->ddsd,
1835 if( dxresult == DDERR_SURFACELOST )
1836 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1838 if( dxresult != DD_OK )
1840 return VLC_EGENERIC;
1844 /* Now we have a pointer to the surface memory, we can update our picture
1846 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1849 DirectXUnlockSurface( p_vout, p_pic );
1850 return VLC_EGENERIC;
1856 /*****************************************************************************
1857 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1858 *****************************************************************************/
1859 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1861 /* Unlock the Surface */
1862 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1865 return VLC_EGENERIC;
1868 /*****************************************************************************
1869 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1870 *****************************************************************************/
1871 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t i_color )
1876 uint32_t i_pixel_backup;
1879 ddsd.dwSize = sizeof(ddsd);
1880 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1881 &ddsd, DDLOCK_WAIT, NULL );
1882 if( dxresult != DD_OK ) return 0;
1884 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1886 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1889 *(uint8_t *)ddsd.lpSurface = 0x11;
1892 *(uint8_t *)ddsd.lpSurface = 0x01;
1895 *(uint16_t *)ddsd.lpSurface = 0x01;
1898 *(uint32_t *)ddsd.lpSurface = 0x01;
1902 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1904 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1906 i_rgb = GetPixel( hdc, 0, 0 );
1907 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1910 ddsd.dwSize = sizeof(ddsd);
1911 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1912 &ddsd, DDLOCK_WAIT, NULL );
1913 if( dxresult != DD_OK ) return i_rgb;
1915 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1917 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1922 /*****************************************************************************
1923 * A few toolbox functions
1924 *****************************************************************************/
1925 void SwitchWallpaperMode( vout_thread_t *p_vout, vlc_bool_t b_on )
1929 if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
1931 hwnd = FindWindow( "Progman", NULL );
1932 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, "SHELLDLL_DefView", NULL );
1933 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, "SysListView32", NULL );
1936 msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
1937 "wallpaper mode not supported" );
1941 p_vout->p_sys->b_wallpaper = b_on;
1943 msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
1945 if( p_vout->p_sys->b_wallpaper )
1947 p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
1948 p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
1950 ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1951 ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1955 ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
1956 ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
1959 /* Update desktop */
1960 InvalidateRect( hwnd, NULL, TRUE );
1961 UpdateWindow( hwnd );
1964 /*****************************************************************************
1965 * config variable callback
1966 *****************************************************************************/
1967 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1968 LPTSTR psz_drivername, VOID* p_context,
1971 module_config_t *p_item = (module_config_t *)p_context;
1974 (char **)realloc( p_item->ppsz_list,
1975 (p_item->i_list+2) * sizeof(char *) );
1976 p_item->ppsz_list_text =
1977 (char **)realloc( p_item->ppsz_list_text,
1978 (p_item->i_list+2) * sizeof(char *) );
1980 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1981 p_item->ppsz_list_text[p_item->i_list] = NULL;
1983 p_item->ppsz_list[p_item->i_list] = NULL;
1984 p_item->ppsz_list_text[p_item->i_list] = NULL;
1986 return TRUE; /* Keep enumerating */
1989 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1990 vlc_value_t newval, vlc_value_t oldval, void *d)
1992 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
1994 HINSTANCE hddraw_dll;
1996 module_config_t *p_item;
1999 p_item = config_FindConfig( p_this, psz_name );
2000 if( !p_item ) return VLC_SUCCESS;
2002 /* Clear-up the current list */
2003 if( p_item->i_list )
2005 /* Keep the first entry */
2006 for( i = 1; i < p_item->i_list; i++ )
2008 free( p_item->ppsz_list[i] );
2009 free( p_item->ppsz_list_text[i] );
2011 /* TODO: Remove when no more needed */
2012 p_item->ppsz_list[i] = NULL;
2013 p_item->ppsz_list_text[i] = NULL;
2017 /* Load direct draw DLL */
2018 hddraw_dll = LoadLibrary("DDRAW.DLL");
2019 if( hddraw_dll == NULL ) return VLC_SUCCESS;
2021 OurDirectDrawEnumerateEx =
2022 (void *)GetProcAddress( hddraw_dll, "DirectDrawEnumerateExA" );
2024 if( OurDirectDrawEnumerateEx )
2026 /* Enumerate displays */
2027 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
2028 DDENUM_ATTACHEDSECONDARYDEVICES );
2031 FreeLibrary( hddraw_dll );
2033 /* Signal change to the interface */
2034 p_item->b_dirty = VLC_TRUE;
2039 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
2040 vlc_value_t oldval, vlc_value_t newval,
2043 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2045 if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
2046 (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
2048 playlist_t *p_playlist;
2051 (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
2055 /* Modify playlist as well because the vout might have to be
2057 var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
2058 var_Set( p_playlist, "directx-wallpaper", newval );
2060 vlc_object_release( p_playlist );
2063 p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;