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., 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 # include <multimon.h>
52 #undef GetSystemMetrics
54 #ifndef MONITOR_DEFAULTTONEAREST
55 # define MONITOR_DEFAULTTONEAREST 2
60 /*****************************************************************************
62 * Defining them here allows us to get rid of the dxguid library during
64 *****************************************************************************/
66 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
67 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
69 /*****************************************************************************
71 *****************************************************************************/
72 static int OpenVideo ( vlc_object_t * );
73 static void CloseVideo ( vlc_object_t * );
75 static int Init ( vout_thread_t * );
76 static void End ( vout_thread_t * );
77 static int Manage ( vout_thread_t * );
78 static void Display ( vout_thread_t *, picture_t * );
79 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
81 static int NewPictureVec ( vout_thread_t *, picture_t *, int );
82 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
83 static int UpdatePictureStruct( vout_thread_t *, picture_t *, int );
85 static int DirectXInitDDraw ( vout_thread_t *p_vout );
86 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
87 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
88 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
89 static int DirectXCreateSurface ( vout_thread_t *p_vout,
90 LPDIRECTDRAWSURFACE2 *, int, int, int );
91 static void DirectXCloseSurface ( vout_thread_t *p_vout,
92 LPDIRECTDRAWSURFACE2 );
93 static int DirectXCreateClipper ( vout_thread_t *p_vout );
94 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
95 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
96 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
98 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
100 void SwitchWallpaperMode( vout_thread_t *, vlc_bool_t );
102 /* Object variables callbacks */
103 static int FindDevicesCallback( vlc_object_t *, char const *,
104 vlc_value_t, vlc_value_t, void * );
105 static int WallpaperCallback( vlc_object_t *, char const *,
106 vlc_value_t, vlc_value_t, void * );
108 /*****************************************************************************
110 *****************************************************************************/
111 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
112 #define HW_YUV_LONGTEXT N_( \
113 "Try to use hardware acceleration for YUV->RGB conversions. " \
114 "This option doesn't have any effect when using overlays." )
116 #define SYSMEM_TEXT N_("Use video buffers in system memory")
117 #define SYSMEM_LONGTEXT N_( \
118 "Create video buffers in system memory instead of video memory. This " \
119 "isn't recommended as usually using video memory allows to benefit from " \
120 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
121 "This option doesn't have any effect when using overlays." )
123 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
124 #define TRIPLEBUF_LONGTEXT N_( \
125 "Try to use triple buffering when using YUV overlays. That results in " \
126 "much better video quality (no flickering)." )
128 #define DEVICE_TEXT N_("Name of desired display device")
129 #define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " \
130 "specify the Windows device name of the display that you want the video " \
131 "window to open on. For example, \"\\\\.\\DISPLAY1\" or " \
132 "\"\\\\.\\DISPLAY2\"." )
134 #define WALLPAPER_TEXT N_("Enable wallpaper mode ")
135 #define WALLPAPER_LONGTEXT N_( \
136 "The wallpaper mode allows you to display the video as the desktop " \
137 "background. Note that this feature only works in overlay mode and " \
138 "the desktop must not already have a wallpaper." )
140 static char *ppsz_dev[] = { "" };
141 static char *ppsz_dev_text[] = { N_("Default") };
144 set_shortname( "DirectX" );
145 set_category( CAT_VIDEO );
146 set_subcategory( SUBCAT_VIDEO_VOUT );
147 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
149 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
151 add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT,
152 TRIPLEBUF_LONGTEXT, VLC_TRUE );
154 add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
156 change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback );
157 change_action_add( FindDevicesCallback, N_("Refresh list") );
159 add_bool( "directx-wallpaper", 0, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
162 set_description( _("DirectX video output") );
163 set_capability( "video output", 100 );
164 add_shortcut( "directx" );
165 set_callbacks( OpenVideo, CloseVideo );
167 /* FIXME: Hack to avoid unregistering our window class */
168 linked_with_a_crap_library_which_uses_atexit( );
172 /* check if we registered a window class because we need to
175 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
176 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
179 /*****************************************************************************
180 * OpenVideo: allocate DirectX video thread output method
181 *****************************************************************************
182 * This function allocates and initialize the DirectX vout method.
183 *****************************************************************************/
184 static int OpenVideo( vlc_object_t *p_this )
186 vout_thread_t * p_vout = (vout_thread_t *)p_this;
190 /* Allocate structure */
191 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
192 if( p_vout->p_sys == NULL )
194 msg_Err( p_vout, "out of memory" );
197 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
199 /* Initialisations */
200 p_vout->pf_init = Init;
201 p_vout->pf_end = End;
202 p_vout->pf_manage = Manage;
203 p_vout->pf_render = NULL;
204 p_vout->pf_display = Display;
206 p_vout->p_sys->p_ddobject = NULL;
207 p_vout->p_sys->p_display = NULL;
208 p_vout->p_sys->p_current_surface = NULL;
209 p_vout->p_sys->p_clipper = NULL;
210 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
211 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
212 p_vout->p_sys->i_changes = 0;
213 p_vout->p_sys->b_wallpaper = 0;
214 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
215 SetRectEmpty( &p_vout->p_sys->rect_display );
216 SetRectEmpty( &p_vout->p_sys->rect_parent );
218 /* Multimonitor stuff */
219 p_vout->p_sys->hmonitor = NULL;
220 p_vout->p_sys->p_display_driver = NULL;
221 p_vout->p_sys->MonitorFromWindow = NULL;
222 p_vout->p_sys->GetMonitorInfo = NULL;
223 if( (huser32 = GetModuleHandle( _T("USER32") ) ) )
225 p_vout->p_sys->MonitorFromWindow = (HMONITOR (WINAPI *)( HWND, DWORD ))
226 GetProcAddress( huser32, _T("MonitorFromWindow") );
227 p_vout->p_sys->GetMonitorInfo =
229 GetProcAddress( huser32, "GetMonitorInfoA" );
231 GetProcAddress( huser32, _T("GetMonitorInfoW") );
235 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
236 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
237 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
238 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
239 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
240 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
242 p_vout->p_sys->b_cursor_hidden = 0;
243 p_vout->p_sys->i_lastmoved = mdate();
245 /* Set main window's size */
246 p_vout->p_sys->i_window_width = p_vout->i_window_width;
247 p_vout->p_sys->i_window_height = p_vout->i_window_height;
249 /* Create the DirectXEventThread, this thread is created by us to isolate
250 * the Win32 PeekMessage function calls. We want to do this because
251 * Windows can stay blocked inside this call for a long time, and when
252 * this happens it thus blocks vlc's video_output thread.
253 * DirectXEventThread will take care of the creation of the video
254 * window (because PeekMessage has to be called from the same thread which
255 * created the window). */
256 msg_Dbg( p_vout, "creating DirectXEventThread" );
257 p_vout->p_sys->p_event =
258 vlc_object_create( p_vout, sizeof(event_thread_t) );
259 p_vout->p_sys->p_event->p_vout = p_vout;
260 if( vlc_thread_create( p_vout->p_sys->p_event, "DirectX Events Thread",
261 E_(DirectXEventThread), 0, 1 ) )
263 msg_Err( p_vout, "cannot create DirectXEventThread" );
264 vlc_object_destroy( p_vout->p_sys->p_event );
265 p_vout->p_sys->p_event = NULL;
269 if( p_vout->p_sys->p_event->b_error )
271 msg_Err( p_vout, "DirectXEventThread failed" );
275 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
277 msg_Dbg( p_vout, "DirectXEventThread running" );
279 /* Initialise DirectDraw */
280 if( DirectXInitDDraw( p_vout ) )
282 msg_Err( p_vout, "cannot initialize DirectDraw" );
286 /* Create the directx display */
287 if( DirectXCreateDisplay( p_vout ) )
289 msg_Err( p_vout, "cannot initialize DirectDraw" );
293 /* Variable to indicate if the window should be on top of others */
294 /* Trigger a callback right now */
295 var_Get( p_vout, "video-on-top", &val );
296 var_Set( p_vout, "video-on-top", val );
298 /* Variable to indicate if the window should be on top of others */
299 /* Trigger a callback right now */
300 var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
301 val.psz_string = _("Wallpaper");
302 var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
303 var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
304 var_Get( p_vout, "directx-wallpaper", &val );
305 var_Set( p_vout, "directx-wallpaper", val );
310 CloseVideo( VLC_OBJECT(p_vout) );
314 /*****************************************************************************
315 * Init: initialize DirectX video thread output method
316 *****************************************************************************
317 * This function create the directx surfaces needed by the output thread.
318 * It is called at the beginning of the thread.
319 *****************************************************************************/
320 static int Init( vout_thread_t *p_vout )
325 /* Get a few default parameters */
326 var_Get( p_vout, "overlay", &val );
327 p_vout->p_sys->b_using_overlay = val.b_bool;
328 var_Get( p_vout, "directx-use-sysmem", &val );
329 p_vout->p_sys->b_use_sysmem = val.b_bool;
330 var_Get( p_vout, "directx-hw-yuv", &val );
331 p_vout->p_sys->b_hw_yuv = val.b_bool;
332 var_Get( p_vout, "directx-3buffering", &val );
333 p_vout->p_sys->b_3buf_overlay = val.b_bool;
335 /* Initialise DirectDraw if not already done.
336 * We do this here because on multi-monitor systems we may have to
337 * re-create the directdraw surfaces. */
338 if( !p_vout->p_sys->p_ddobject &&
339 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
341 msg_Err( p_vout, "cannot initialize DirectDraw" );
345 /* Create the directx display */
346 if( !p_vout->p_sys->p_display &&
347 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
349 msg_Err( p_vout, "cannot initialize DirectDraw" );
353 /* Initialize the output structure.
354 * Since DirectDraw can do rescaling for us, stick to the default
355 * coordinates and aspect. */
356 p_vout->output.i_width = p_vout->render.i_width;
357 p_vout->output.i_height = p_vout->render.i_height;
358 p_vout->output.i_aspect = p_vout->render.i_aspect;
359 p_vout->fmt_out = p_vout->fmt_in;
360 E_(DirectXUpdateRects)( p_vout, VLC_TRUE );
362 #define MAX_DIRECTBUFFERS 1
363 /* Right now we use only 1 directbuffer because we don't want the
364 * video decoder to decode directly into direct buffers as they are
365 * created into video memory and video memory is _really_ slow */
367 /* Choose the chroma we will try first. */
368 switch( p_vout->render.i_chroma )
370 case VLC_FOURCC('Y','U','Y','2'):
371 case VLC_FOURCC('Y','U','N','V'):
372 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
374 case VLC_FOURCC('U','Y','V','Y'):
375 case VLC_FOURCC('U','Y','N','V'):
376 case VLC_FOURCC('Y','4','2','2'):
377 p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
379 case VLC_FOURCC('Y','V','Y','U'):
380 p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
383 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
387 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
389 i_chroma_backup = p_vout->output.i_chroma;
391 if( !I_OUTPUTPICTURES )
393 /* hmmm, it didn't work! Let's try commonly supported chromas */
394 if( p_vout->output.i_chroma != VLC_FOURCC('I','4','2','0') )
396 p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
397 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
399 if( !I_OUTPUTPICTURES )
401 /* hmmm, it still didn't work! Let's try another one */
402 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
403 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
407 if( !I_OUTPUTPICTURES )
409 /* If it still didn't work then don't try to use an overlay */
410 p_vout->output.i_chroma = i_chroma_backup;
411 p_vout->p_sys->b_using_overlay = 0;
412 NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
415 /* Change the window title bar text */
416 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
418 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
422 /*****************************************************************************
423 * End: terminate Sys video thread output method
424 *****************************************************************************
425 * Terminate an output method created by Create.
426 * It is called at the end of the thread.
427 *****************************************************************************/
428 static void End( vout_thread_t *p_vout )
430 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
432 DirectXCloseDisplay( p_vout );
433 DirectXCloseDDraw( p_vout );
438 /*****************************************************************************
439 * CloseVideo: destroy Sys video thread output method
440 *****************************************************************************
441 * Terminate an output method created by Create
442 *****************************************************************************/
443 static void CloseVideo( vlc_object_t *p_this )
445 vout_thread_t * p_vout = (vout_thread_t *)p_this;
447 msg_Dbg( p_vout, "CloseVideo" );
449 if( p_vout->p_sys->p_event )
451 vlc_object_detach( p_vout->p_sys->p_event );
453 /* Kill DirectXEventThread */
454 p_vout->p_sys->p_event->b_die = VLC_TRUE;
456 /* we need to be sure DirectXEventThread won't stay stuck in
457 * GetMessage, so we send a fake message */
458 if( p_vout->p_sys->hwnd )
460 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
463 vlc_thread_join( p_vout->p_sys->p_event );
464 vlc_object_destroy( p_vout->p_sys->p_event );
467 vlc_mutex_destroy( &p_vout->p_sys->lock );
469 /* Make sure the wallpaper is restored */
470 SwitchWallpaperMode( p_vout, VLC_FALSE );
474 free( p_vout->p_sys );
475 p_vout->p_sys = NULL;
479 /*****************************************************************************
480 * Manage: handle Sys events
481 *****************************************************************************
482 * This function should be called regularly by the video output thread.
483 * It returns a non null value if an error occurred.
484 *****************************************************************************/
485 static int Manage( vout_thread_t *p_vout )
487 WINDOWPLACEMENT window_placement;
489 /* If we do not control our window, we check for geometry changes
490 * ourselves because the parent might not send us its events. */
491 vlc_mutex_lock( &p_vout->p_sys->lock );
492 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
497 vlc_mutex_unlock( &p_vout->p_sys->lock );
499 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
500 point.x = point.y = 0;
501 ClientToScreen( p_vout->p_sys->hparent, &point );
502 OffsetRect( &rect_parent, point.x, point.y );
504 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
506 p_vout->p_sys->rect_parent = rect_parent;
508 /* This one is to force the update even if only
509 * the position has changed */
510 SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
511 rect_parent.right - rect_parent.left,
512 rect_parent.bottom - rect_parent.top, 0 );
514 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
515 rect_parent.right - rect_parent.left,
516 rect_parent.bottom - rect_parent.top, 0 );
521 vlc_mutex_unlock( &p_vout->p_sys->lock );
527 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
529 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
531 /* Check if we are still on the same monitor */
532 if( p_vout->p_sys->MonitorFromWindow &&
533 p_vout->p_sys->hmonitor !=
534 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
535 MONITOR_DEFAULTTONEAREST ) )
537 /* This will force the vout core to recreate the picture buffers */
538 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
542 /* Check for cropping / aspect changes */
543 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
544 p_vout->i_changes & VOUT_ASPECT_CHANGE )
546 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
547 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
549 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
550 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
551 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
552 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
553 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
554 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
555 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
556 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
557 E_(DirectXUpdateRects)( p_vout, VLC_TRUE );
558 vout_Control( p_vout, VOUT_SET_ZOOM );
561 /* We used to call the Win32 PeekMessage function here to read the window
562 * messages. But since window can stay blocked into this function for a
563 * long time (for example when you move your window on the screen), I
564 * decided to isolate PeekMessage in another thread. */
566 if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
568 SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
569 p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
570 E_(DirectXUpdateOverlay)( p_vout );
576 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
577 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
580 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
581 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
583 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
585 /* We need to switch between Maximized and Normal sized window */
586 window_placement.length = sizeof(WINDOWPLACEMENT);
587 GetWindowPlacement( hwnd, &window_placement );
588 if( p_vout->b_fullscreen )
590 /* Change window style, no borders and no title bar */
591 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
592 SetWindowLong( hwnd, GWL_STYLE, i_style );
594 if( p_vout->p_sys->hparent )
596 /* Retrieve current window position so fullscreen will happen
597 * on the right screen */
600 ClientToScreen( p_vout->p_sys->hwnd, &point );
601 GetClientRect( p_vout->p_sys->hwnd, &rect );
602 SetWindowPos( hwnd, 0, point.x, point.y,
603 rect.right, rect.bottom,
604 SWP_NOZORDER|SWP_FRAMECHANGED );
605 GetWindowPlacement( hwnd, &window_placement );
608 /* Maximize window */
609 window_placement.showCmd = SW_SHOWMAXIMIZED;
610 SetWindowPlacement( hwnd, &window_placement );
611 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
612 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
614 if( p_vout->p_sys->hparent )
617 GetClientRect( hwnd, &rect );
618 SetParent( p_vout->p_sys->hwnd, hwnd );
619 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
620 rect.right, rect.bottom,
621 SWP_NOZORDER|SWP_FRAMECHANGED );
624 SetForegroundWindow( hwnd );
628 /* Change window style, no borders and no title bar */
629 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
632 window_placement.showCmd = SW_SHOWNORMAL;
633 SetWindowPlacement( hwnd, &window_placement );
634 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
635 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
637 if( p_vout->p_sys->hparent )
640 GetClientRect( p_vout->p_sys->hparent, &rect );
641 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
642 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
643 rect.right, rect.bottom,
644 SWP_NOZORDER|SWP_FRAMECHANGED );
646 ShowWindow( hwnd, SW_HIDE );
647 SetForegroundWindow( p_vout->p_sys->hparent );
650 /* Make sure the mouse cursor is displayed */
651 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
654 /* Update the object variable and trigger callback */
655 val.b_bool = p_vout->b_fullscreen;
656 var_Set( p_vout, "fullscreen", val );
658 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
659 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
665 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
666 (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 )
671 /* Hide the cursor only if it is inside our window */
672 GetCursorPos( &point );
673 hwnd = WindowFromPoint(point);
674 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
676 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
680 p_vout->p_sys->i_lastmoved = mdate();
685 * "Always on top" status change
687 if( p_vout->p_sys->b_on_top_change )
690 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
692 var_Get( p_vout, "video-on-top", &val );
694 /* Set the window on top if necessary */
695 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
698 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
699 MF_BYCOMMAND | MFS_CHECKED );
700 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
701 SWP_NOSIZE | SWP_NOMOVE );
704 /* The window shouldn't be on top */
705 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
708 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
709 MF_BYCOMMAND | MFS_UNCHECKED );
710 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
711 SWP_NOSIZE | SWP_NOMOVE );
714 p_vout->p_sys->b_on_top_change = VLC_FALSE;
717 /* Check if the event thread is still running */
718 if( p_vout->p_sys->p_event->b_die )
720 return VLC_EGENERIC; /* exit */
726 /*****************************************************************************
727 * Display: displays previously rendered output
728 *****************************************************************************
729 * This function sends the currently rendered image to the display, wait until
730 * it is displayed and switch the two rendering buffers, preparing next frame.
731 *****************************************************************************/
732 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
736 if( (p_vout->p_sys->p_display == NULL) )
738 msg_Warn( p_vout, "no display!" );
742 /* Our surface can be lost so be sure to check this
743 * and restore it if need be */
744 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
745 == DDERR_SURFACELOST )
747 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
748 p_vout->p_sys->b_using_overlay )
749 E_(DirectXUpdateOverlay)( p_vout );
752 if( !p_vout->p_sys->b_using_overlay )
756 /* We ask for the "NOTEARING" option */
757 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
758 ddbltfx.dwSize = sizeof(DDBLTFX);
759 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
761 /* Blit video surface to display */
762 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
763 &p_vout->p_sys->rect_dest_clipped,
764 p_pic->p_sys->p_surface,
765 &p_vout->p_sys->rect_src_clipped,
766 DDBLT_ASYNC, &ddbltfx );
767 if( dxresult != DD_OK )
769 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
774 else /* using overlay */
776 /* Flip the overlay buffers if we are using back buffers */
777 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
782 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
784 if( dxresult != DD_OK )
786 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
789 /* set currently displayed pic */
790 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
792 /* Lock surface to get all the required info */
793 if( DirectXLockSurface( p_vout, p_pic ) )
796 msg_Warn( p_vout, "cannot lock surface" );
799 DirectXUnlockSurface( p_vout, p_pic );
803 /* following functions are local */
805 /*****************************************************************************
806 * DirectXEnumCallback: Device enumeration
807 *****************************************************************************
808 * This callback function is called by DirectDraw once for each
809 * available DirectDraw device.
810 *****************************************************************************/
811 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
812 LPTSTR psz_drivername, VOID* p_context,
815 vout_thread_t *p_vout = (vout_thread_t *)p_context;
818 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
822 var_Get( p_vout, "directx-device", &device );
824 if( ( !device.psz_string || !*device.psz_string ) &&
825 hmon == p_vout->p_sys->hmonitor )
827 if( device.psz_string ) free( device.psz_string );
829 else if( strcmp( psz_drivername, device.psz_string ) == 0 )
831 MONITORINFO monitor_info;
832 monitor_info.cbSize = sizeof( MONITORINFO );
834 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
838 /* Move window to the right screen */
839 GetWindowRect( p_vout->p_sys->hwnd, &rect );
840 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
842 rect.left = monitor_info.rcWork.left;
843 rect.top = monitor_info.rcWork.top;
844 msg_Dbg( p_vout, "DirectXEnumCallback: Setting window "
845 "position to %d,%d", rect.left, rect.top );
846 SetWindowPos( p_vout->p_sys->hwnd, NULL,
847 rect.left, rect.top, 0, 0,
848 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
852 p_vout->p_sys->hmonitor = hmon;
853 if( device.psz_string ) free( device.psz_string );
857 if( device.psz_string ) free( device.psz_string );
858 return TRUE; /* Keep enumerating */
861 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
862 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
863 if( p_vout->p_sys->p_display_driver )
864 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
867 return TRUE; /* Keep enumerating */
870 /*****************************************************************************
871 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
872 *****************************************************************************
873 * This function initialise and allocate resources for DirectDraw.
874 *****************************************************************************/
875 static int DirectXInitDDraw( vout_thread_t *p_vout )
878 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
879 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
881 LPDIRECTDRAW p_ddobject;
883 msg_Dbg( p_vout, "DirectXInitDDraw" );
885 /* Load direct draw DLL */
886 p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
887 if( p_vout->p_sys->hddraw_dll == NULL )
889 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
893 OurDirectDrawCreate =
894 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
895 _T("DirectDrawCreate") );
896 if( OurDirectDrawCreate == NULL )
898 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
902 OurDirectDrawEnumerateEx =
903 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
905 "DirectDrawEnumerateExA" );
907 _T("DirectDrawEnumerateExW") );
910 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
914 var_Get( p_vout, "directx-device", &device );
915 if( device.psz_string )
917 msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
918 free( device.psz_string );
921 p_vout->p_sys->hmonitor =
922 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
923 MONITOR_DEFAULTTONEAREST );
925 /* Enumerate displays */
926 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
927 DDENUM_ATTACHEDSECONDARYDEVICES );
930 /* Initialize DirectDraw now */
931 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
933 if( dxresult != DD_OK )
935 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
939 /* Get the IDirectDraw2 interface */
940 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
941 (LPVOID *)&p_vout->p_sys->p_ddobject );
942 /* Release the unused interface */
943 IDirectDraw_Release( p_ddobject );
944 if( dxresult != DD_OK )
946 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
950 /* Set DirectDraw Cooperative level, ie what control we want over Windows
952 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
953 NULL, DDSCL_NORMAL );
954 if( dxresult != DD_OK )
956 msg_Err( p_vout, "cannot set direct draw cooperative level" );
960 /* Get the size of the current display device */
961 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
963 MONITORINFO monitor_info;
964 monitor_info.cbSize = sizeof( MONITORINFO );
965 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
967 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
971 p_vout->p_sys->rect_display.left = 0;
972 p_vout->p_sys->rect_display.top = 0;
973 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
974 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
977 msg_Dbg( p_vout, "screen dimensions (%ix%i,%ix%i)",
978 p_vout->p_sys->rect_display.left,
979 p_vout->p_sys->rect_display.top,
980 p_vout->p_sys->rect_display.right,
981 p_vout->p_sys->rect_display.bottom );
983 /* Probe the capabilities of the hardware */
984 DirectXGetDDrawCaps( p_vout );
986 msg_Dbg( p_vout, "End DirectXInitDDraw" );
990 if( p_vout->p_sys->p_ddobject )
991 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
992 if( p_vout->p_sys->hddraw_dll )
993 FreeLibrary( p_vout->p_sys->hddraw_dll );
994 p_vout->p_sys->hddraw_dll = NULL;
995 p_vout->p_sys->p_ddobject = NULL;
999 /*****************************************************************************
1000 * DirectXCreateDisplay: create the DirectDraw display.
1001 *****************************************************************************
1002 * Create and initialize display according to preferences specified in the vout
1004 *****************************************************************************/
1005 static int DirectXCreateDisplay( vout_thread_t *p_vout )
1009 LPDIRECTDRAWSURFACE p_display;
1011 msg_Dbg( p_vout, "DirectXCreateDisplay" );
1013 /* Now get the primary surface. This surface is what you actually see
1015 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
1016 ddsd.dwSize = sizeof(DDSURFACEDESC);
1017 ddsd.dwFlags = DDSD_CAPS;
1018 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1020 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1021 &ddsd, &p_display, NULL );
1022 if( dxresult != DD_OK )
1024 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
1025 return VLC_EGENERIC;
1028 dxresult = IDirectDrawSurface_QueryInterface( p_display,
1029 &IID_IDirectDrawSurface2,
1030 (LPVOID *)&p_vout->p_sys->p_display );
1031 /* Release the old interface */
1032 IDirectDrawSurface_Release( p_display );
1033 if ( dxresult != DD_OK )
1035 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
1036 "(error %li)", dxresult );
1037 return VLC_EGENERIC;
1040 /* The clipper will be used only in non-overlay mode */
1041 DirectXCreateClipper( p_vout );
1043 /* Make sure the colorkey will be painted */
1044 p_vout->p_sys->i_colorkey = 1;
1045 p_vout->p_sys->i_rgb_colorkey =
1046 DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
1048 /* Create the actual brush */
1049 SetClassLong( p_vout->p_sys->hvideownd, GCL_HBRBACKGROUND,
1050 (LONG)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
1051 InvalidateRect( p_vout->p_sys->hvideownd, NULL, TRUE );
1052 E_(DirectXUpdateRects)( p_vout, VLC_TRUE );
1057 /*****************************************************************************
1058 * DirectXCreateClipper: Create a clipper that will be used when blitting the
1059 * RGB surface to the main display.
1060 *****************************************************************************
1061 * This clipper prevents us to modify by mistake anything on the screen
1062 * which doesn't belong to our window. For example when a part of our video
1063 * window is hidden by another window.
1064 *****************************************************************************/
1065 static int DirectXCreateClipper( vout_thread_t *p_vout )
1069 msg_Dbg( p_vout, "DirectXCreateClipper" );
1071 /* Create the clipper */
1072 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
1073 &p_vout->p_sys->p_clipper, NULL );
1074 if( dxresult != DD_OK )
1076 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
1080 /* Associate the clipper to the window */
1081 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
1082 p_vout->p_sys->hvideownd );
1083 if( dxresult != DD_OK )
1085 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
1090 /* associate the clipper with the surface */
1091 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
1092 p_vout->p_sys->p_clipper);
1093 if( dxresult != DD_OK )
1095 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
1103 if( p_vout->p_sys->p_clipper )
1105 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1107 p_vout->p_sys->p_clipper = NULL;
1108 return VLC_EGENERIC;
1111 /*****************************************************************************
1112 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
1113 *****************************************************************************
1114 * The best method of display is with an YUV overlay because the YUV->RGB
1115 * conversion is done in hardware.
1116 * You can also create a plain RGB surface.
1117 * ( Maybe we could also try an RGB overlay surface, which could have hardware
1118 * scaling and which would also be faster in window mode because you don't
1119 * need to do any blitting to the main display...)
1120 *****************************************************************************/
1121 static int DirectXCreateSurface( vout_thread_t *p_vout,
1122 LPDIRECTDRAWSURFACE2 *pp_surface_final,
1123 int i_chroma, int b_overlay,
1127 LPDIRECTDRAWSURFACE p_surface;
1130 /* Create the video surface */
1133 /* Now try to create the YUV overlay surface.
1134 * This overlay will be displayed on top of the primary surface.
1135 * A color key is used to determine whether or not the overlay will be
1136 * displayed, ie the overlay will be displayed in place of the primary
1137 * surface wherever the primary surface will have this color.
1138 * The video window has been created with a background of this color so
1139 * the overlay will be only displayed on top of this window */
1141 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
1142 ddsd.dwSize = sizeof(DDSURFACEDESC);
1143 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1144 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1145 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1146 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1147 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
1148 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
1149 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
1151 ddsd.dwHeight = p_vout->render.i_height;
1152 ddsd.dwWidth = p_vout->render.i_width;
1153 ddsd.dwBackBufferCount = i_backbuffers;
1155 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1156 &ddsd, &p_surface, NULL );
1157 if( dxresult != DD_OK )
1159 *pp_surface_final = NULL;
1160 return VLC_EGENERIC;
1166 vlc_bool_t b_rgb_surface =
1167 ( i_chroma == VLC_FOURCC('R','G','B','2') )
1168 || ( i_chroma == VLC_FOURCC('R','V','1','5') )
1169 || ( i_chroma == VLC_FOURCC('R','V','1','6') )
1170 || ( i_chroma == VLC_FOURCC('R','V','2','4') )
1171 || ( i_chroma == VLC_FOURCC('R','V','3','2') );
1173 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
1174 ddsd.dwSize = sizeof(DDSURFACEDESC);
1175 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1176 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
1177 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1178 ddsd.dwHeight = p_vout->render.i_height;
1179 ddsd.dwWidth = p_vout->render.i_width;
1181 if( p_vout->p_sys->b_use_sysmem )
1182 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1184 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
1186 if( !b_rgb_surface )
1188 ddsd.dwFlags |= DDSD_PIXELFORMAT;
1189 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1190 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1193 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1194 &ddsd, &p_surface, NULL );
1195 if( dxresult != DD_OK )
1197 *pp_surface_final = NULL;
1198 return VLC_EGENERIC;
1202 /* Now that the surface is created, try to get a newer DirectX interface */
1203 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
1204 &IID_IDirectDrawSurface2,
1205 (LPVOID *)pp_surface_final );
1206 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
1207 if ( dxresult != DD_OK )
1209 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
1210 "(error %li)", dxresult );
1211 *pp_surface_final = NULL;
1212 return VLC_EGENERIC;
1217 /* Check the overlay is useable as some graphics cards allow creating
1218 * several overlays but only one can be used at one time. */
1219 p_vout->p_sys->p_current_surface = *pp_surface_final;
1220 if( E_(DirectXUpdateOverlay)( p_vout ) != VLC_SUCCESS )
1222 IDirectDrawSurface2_Release( *pp_surface_final );
1223 *pp_surface_final = NULL;
1224 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1225 return VLC_EGENERIC;
1232 /*****************************************************************************
1233 * DirectXUpdateOverlay: Move or resize overlay surface on video display.
1234 *****************************************************************************
1235 * This function is used to move or resize an overlay surface on the screen.
1236 * Ususally the overlay is moved by the user and thus, by a move or resize
1237 * event (in Manage).
1238 *****************************************************************************/
1239 int E_(DirectXUpdateOverlay)( vout_thread_t *p_vout )
1244 RECT rect_src = p_vout->p_sys->rect_src_clipped;
1245 RECT rect_dest = p_vout->p_sys->rect_dest_clipped;
1247 if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1249 if( p_vout->p_sys->b_wallpaper )
1251 int i_x, i_y, i_width, i_height;
1253 rect_src.left = p_vout->fmt_out.i_x_offset;
1254 rect_src.top = p_vout->fmt_out.i_y_offset;
1255 rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
1256 rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
1258 rect_dest = p_vout->p_sys->rect_display;
1259 vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1260 &i_x, &i_y, &i_width, &i_height );
1262 rect_dest.left += i_x;
1263 rect_dest.right = rect_dest.left + i_width;
1264 rect_dest.top += i_y;
1265 rect_dest.bottom = rect_dest.top + i_height;
1268 vlc_mutex_lock( &p_vout->p_sys->lock );
1269 if( p_vout->p_sys->p_current_surface == NULL )
1271 vlc_mutex_unlock( &p_vout->p_sys->lock );
1272 return VLC_EGENERIC;
1275 /* The new window dimensions should already have been computed by the
1276 * caller of this function */
1278 /* Position and show the overlay */
1279 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1280 ddofx.dwSize = sizeof(DDOVERLAYFX);
1281 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1282 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1284 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1286 dxresult = IDirectDrawSurface2_UpdateOverlay(
1287 p_vout->p_sys->p_current_surface,
1288 &rect_src, p_vout->p_sys->p_display, &rect_dest,
1291 vlc_mutex_unlock( &p_vout->p_sys->lock );
1293 if(dxresult != DD_OK)
1295 msg_Warn( p_vout, "DirectXUpdateOverlay cannot move/resize overlay" );
1296 return VLC_EGENERIC;
1302 /*****************************************************************************
1303 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1304 *****************************************************************************
1305 * This function returns all resources allocated by DirectXInitDDraw.
1306 *****************************************************************************/
1307 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1309 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1310 if( p_vout->p_sys->p_ddobject != NULL )
1312 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1313 p_vout->p_sys->p_ddobject = NULL;
1316 if( p_vout->p_sys->hddraw_dll != NULL )
1318 FreeLibrary( p_vout->p_sys->hddraw_dll );
1319 p_vout->p_sys->hddraw_dll = NULL;
1322 if( p_vout->p_sys->p_display_driver != NULL )
1324 free( p_vout->p_sys->p_display_driver );
1325 p_vout->p_sys->p_display_driver = NULL;
1328 p_vout->p_sys->hmonitor = NULL;
1331 /*****************************************************************************
1332 * DirectXCloseDisplay: close and reset the DirectX display device
1333 *****************************************************************************
1334 * This function returns all resources allocated by DirectXCreateDisplay.
1335 *****************************************************************************/
1336 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1338 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1340 if( p_vout->p_sys->p_clipper != NULL )
1342 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1343 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1344 p_vout->p_sys->p_clipper = NULL;
1347 if( p_vout->p_sys->p_display != NULL )
1349 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1350 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1351 p_vout->p_sys->p_display = NULL;
1355 /*****************************************************************************
1356 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1357 *****************************************************************************
1358 * This function returns all resources allocated for the surface.
1359 *****************************************************************************/
1360 static void DirectXCloseSurface( vout_thread_t *p_vout,
1361 LPDIRECTDRAWSURFACE2 p_surface )
1363 msg_Dbg( p_vout, "DirectXCloseSurface" );
1364 if( p_surface != NULL )
1366 IDirectDrawSurface2_Release( p_surface );
1370 /*****************************************************************************
1371 * NewPictureVec: allocate a vector of identical pictures
1372 *****************************************************************************
1373 * Returns 0 on success, -1 otherwise
1374 *****************************************************************************/
1375 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1379 int i_ret = VLC_SUCCESS;
1380 LPDIRECTDRAWSURFACE2 p_surface;
1382 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1383 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1384 (char *)&p_vout->output.i_chroma );
1386 I_OUTPUTPICTURES = 0;
1388 /* First we try to use an YUV overlay surface.
1389 * The overlay surface that we create won't be used to decode directly
1390 * into it because accessing video memory directly is way to slow (remember
1391 * that pictures are decoded macroblock per macroblock). Instead the video
1392 * will be decoded in picture buffers in system memory which will then be
1393 * memcpy() to the overlay surface. */
1394 if( p_vout->p_sys->b_using_overlay )
1396 /* Triple buffering rocks! it doesn't have any processing overhead
1397 * (you don't have to wait for the vsync) and provides for a very nice
1398 * video quality (no tearing). */
1399 if( p_vout->p_sys->b_3buf_overlay )
1400 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1401 p_vout->output.i_chroma,
1402 p_vout->p_sys->b_using_overlay,
1403 2 /* number of backbuffers */ );
1405 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1407 /* Try to reduce the number of backbuffers */
1408 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1409 p_vout->output.i_chroma,
1410 p_vout->p_sys->b_using_overlay,
1411 0 /* number of backbuffers */ );
1414 if( i_ret == VLC_SUCCESS )
1417 picture_t front_pic;
1418 picture_sys_t front_pic_sys;
1419 front_pic.p_sys = &front_pic_sys;
1421 /* Allocate internal structure */
1422 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1423 if( p_pic[0].p_sys == NULL )
1425 DirectXCloseSurface( p_vout, p_surface );
1429 /* set front buffer */
1430 p_pic[0].p_sys->p_front_surface = p_surface;
1432 /* Get the back buffer */
1433 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1434 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1435 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1436 p_surface, &dds_caps,
1437 &p_pic[0].p_sys->p_surface ) )
1439 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1440 /* front buffer is the same as back buffer */
1441 p_pic[0].p_sys->p_surface = p_surface;
1445 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1446 p_pic[0].p_sys->p_front_surface;
1448 /* Reset the front buffer memory */
1449 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1452 for( i = 0; i < front_pic.i_planes; i++ )
1453 for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1454 memset( front_pic.p[i].p_pixels + j *
1455 front_pic.p[i].i_pitch, 127,
1456 front_pic.p[i].i_visible_pitch );
1458 DirectXUnlockSurface( p_vout, &front_pic );
1461 E_(DirectXUpdateOverlay)( p_vout );
1462 I_OUTPUTPICTURES = 1;
1463 msg_Dbg( p_vout, "YUV overlay created successfully" );
1467 /* As we can't have an overlay, we'll try to create a plain offscreen
1468 * surface. This surface will reside in video memory because there's a
1469 * better chance then that we'll be able to use some kind of hardware
1470 * acceleration like rescaling, blitting or YUV->RGB conversions.
1471 * We then only need to blit this surface onto the main display when we
1472 * want to display it */
1473 if( !p_vout->p_sys->b_using_overlay )
1475 if( p_vout->p_sys->b_hw_yuv )
1479 vlc_bool_t b_result = VLC_FALSE;
1481 /* Check if the chroma is supported first. This is required
1482 * because a few buggy drivers don't mind creating the surface
1483 * even if they don't know about the chroma. */
1484 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1485 &i_codes, NULL ) == DD_OK )
1487 pi_codes = malloc( i_codes * sizeof(DWORD) );
1488 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1489 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1491 for( i = 0; i < (int)i_codes; i++ )
1493 if( p_vout->output.i_chroma == pi_codes[i] )
1495 b_result = VLC_TRUE;
1503 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1504 p_vout->output.i_chroma,
1506 0 /* no back buffers */ );
1508 p_vout->p_sys->b_hw_yuv = VLC_FALSE;
1511 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1513 /* Our last choice is to use a plain RGB surface */
1514 DDPIXELFORMAT ddpfPixelFormat;
1516 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1517 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1520 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1522 switch( ddpfPixelFormat.dwRGBBitCount )
1525 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
1526 p_vout->output.pf_setpalette = SetPalette;
1529 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
1532 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
1535 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
1538 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
1541 msg_Err( p_vout, "unknown screen depth" );
1542 return VLC_EGENERIC;
1544 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1545 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1546 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1549 p_vout->p_sys->b_hw_yuv = 0;
1551 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1552 p_vout->output.i_chroma,
1554 0 /* no back buffers */ );
1556 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1558 /* Give it a last try with b_use_sysmem enabled */
1559 p_vout->p_sys->b_use_sysmem = 1;
1561 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1562 p_vout->output.i_chroma,
1564 0 /* no back buffers */ );
1568 if( i_ret == VLC_SUCCESS )
1570 /* Allocate internal structure */
1571 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1572 if( p_pic[0].p_sys == NULL )
1574 DirectXCloseSurface( p_vout, p_surface );
1578 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1581 I_OUTPUTPICTURES = 1;
1583 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1584 (char *)&p_vout->output.i_chroma );
1589 /* Now that we've got all our direct-buffers, we can finish filling in the
1590 * picture_t structures */
1591 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1593 p_pic[i].i_status = DESTROYED_PICTURE;
1594 p_pic[i].i_type = DIRECT_PICTURE;
1595 p_pic[i].b_slow = VLC_TRUE;
1596 p_pic[i].pf_lock = DirectXLockSurface;
1597 p_pic[i].pf_unlock = DirectXUnlockSurface;
1598 PP_OUTPUTPICTURE[i] = &p_pic[i];
1600 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1603 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1604 I_OUTPUTPICTURES = 0;
1605 msg_Err( p_vout, "cannot lock surface" );
1606 return VLC_EGENERIC;
1608 DirectXUnlockSurface( p_vout, &p_pic[i] );
1611 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1612 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1617 /*****************************************************************************
1618 * FreePicture: destroy a picture vector allocated with NewPictureVec
1619 *****************************************************************************
1621 *****************************************************************************/
1622 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1627 vlc_mutex_lock( &p_vout->p_sys->lock );
1628 p_vout->p_sys->p_current_surface = 0;
1629 vlc_mutex_unlock( &p_vout->p_sys->lock );
1631 for( i = 0; i < i_num_pics; i++ )
1633 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1635 for( i = 0; i < i_num_pics; i++ )
1637 free( p_pic[i].p_sys );
1642 /*****************************************************************************
1643 * UpdatePictureStruct: updates the internal data in the picture_t structure
1644 *****************************************************************************
1645 * This will setup stuff for use by the video_output thread
1646 *****************************************************************************/
1647 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
1650 switch( p_vout->output.i_chroma )
1652 case VLC_FOURCC('R','G','B','2'):
1653 case VLC_FOURCC('R','V','1','5'):
1654 case VLC_FOURCC('R','V','1','6'):
1655 case VLC_FOURCC('R','V','2','4'):
1656 case VLC_FOURCC('R','V','3','2'):
1657 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1658 p_pic->p->i_lines = p_vout->output.i_height;
1659 p_pic->p->i_visible_lines = p_vout->output.i_height;
1660 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1661 switch( p_vout->output.i_chroma )
1663 case VLC_FOURCC('R','G','B','2'):
1664 p_pic->p->i_pixel_pitch = 1;
1666 case VLC_FOURCC('R','V','1','5'):
1667 case VLC_FOURCC('R','V','1','6'):
1668 p_pic->p->i_pixel_pitch = 2;
1670 case VLC_FOURCC('R','V','2','4'):
1671 p_pic->p->i_pixel_pitch = 3;
1673 case VLC_FOURCC('R','V','3','2'):
1674 p_pic->p->i_pixel_pitch = 4;
1677 return VLC_EGENERIC;
1679 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1680 p_pic->p->i_pixel_pitch;
1681 p_pic->i_planes = 1;
1684 case VLC_FOURCC('Y','V','1','2'):
1685 case VLC_FOURCC('I','4','2','0'):
1687 /* U and V inverted compared to I420
1688 * Fixme: this should be handled by the vout core */
1689 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
1691 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1692 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1693 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1694 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1695 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1696 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1697 p_pic->p[Y_PLANE].i_pixel_pitch;
1699 p_pic->V_PIXELS = p_pic->Y_PIXELS
1700 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1701 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1702 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1703 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1704 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1705 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1706 p_pic->p[V_PLANE].i_pixel_pitch;
1708 p_pic->U_PIXELS = p_pic->V_PIXELS
1709 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1710 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1711 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1712 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1713 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1714 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1715 p_pic->p[U_PLANE].i_pixel_pitch;
1717 p_pic->i_planes = 3;
1720 case VLC_FOURCC('I','Y','U','V'):
1722 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1723 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1724 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1725 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1726 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1727 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1728 p_pic->p[Y_PLANE].i_pixel_pitch;
1730 p_pic->U_PIXELS = p_pic->Y_PIXELS
1731 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1732 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1733 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1734 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1735 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1736 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1737 p_pic->p[U_PLANE].i_pixel_pitch;
1739 p_pic->V_PIXELS = p_pic->U_PIXELS
1740 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1741 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1742 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1743 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1744 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1745 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1746 p_pic->p[V_PLANE].i_pixel_pitch;
1748 p_pic->i_planes = 3;
1751 case VLC_FOURCC('U','Y','V','Y'):
1752 case VLC_FOURCC('Y','U','Y','2'):
1754 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1755 p_pic->p->i_lines = p_vout->output.i_height;
1756 p_pic->p->i_visible_lines = p_vout->output.i_height;
1757 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1758 p_pic->p->i_pixel_pitch = 2;
1759 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1760 p_pic->p->i_pixel_pitch;
1762 p_pic->i_planes = 1;
1766 /* Unknown chroma, tell the guy to get lost */
1767 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1768 p_vout->output.i_chroma,
1769 (char*)&p_vout->output.i_chroma );
1770 return VLC_EGENERIC;
1776 /*****************************************************************************
1777 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1778 *****************************************************************************
1779 * It is nice to know which features are supported by the hardware so we can
1780 * find ways to optimize our rendering.
1781 *****************************************************************************/
1782 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1787 /* This is just an indication of whether or not we'll support overlay,
1788 * but with this test we don't know if we support YUV overlay */
1789 memset( &ddcaps, 0, sizeof( DDCAPS ));
1790 ddcaps.dwSize = sizeof(DDCAPS);
1791 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1793 if(dxresult != DD_OK )
1795 msg_Warn( p_vout, "cannot get caps" );
1799 vlc_bool_t bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1800 bHasColorKey, bCanStretch, bCanBltFourcc,
1801 bAlignBoundarySrc, bAlignBoundaryDest,
1802 bAlignSizeSrc, bAlignSizeDest;
1804 /* Determine if the hardware supports overlay surfaces */
1805 bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1806 /* Determine if the hardware supports overlay surfaces */
1807 bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1808 /* Determine if the hardware supports overlay deinterlacing */
1809 bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1810 /* Determine if the hardware supports colorkeying */
1811 bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1812 /* Determine if the hardware supports scaling of the overlay surface */
1813 bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1814 /* Determine if the hardware supports color conversion during a blit */
1815 bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1816 /* Determine overlay source boundary alignment */
1817 bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1818 /* Determine overlay destination boundary alignment */
1819 bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1820 /* Determine overlay destination size alignment */
1821 bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1822 /* Determine overlay destination size alignment */
1823 bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1825 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1826 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1828 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1829 bHasColorKey, bCanStretch, bCanBltFourcc );
1831 if( bAlignBoundarySrc || bAlignBoundaryDest ||
1832 bAlignSizeSrc || bAlignSizeDest )
1834 if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1835 ddcaps.dwAlignBoundarySrc;
1836 if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1837 ddcaps.dwAlignBoundaryDest;
1838 if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1839 ddcaps.dwAlignSizeSrc;
1840 if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1841 ddcaps.dwAlignSizeDest;
1843 msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1844 "align_boundary_dest=%i,%i "
1845 "align_size_src=%i,%i align_size_dest=%i,%i",
1846 bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1847 bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1848 bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1849 bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1852 /* Don't ask for troubles */
1853 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1857 /*****************************************************************************
1858 * DirectXLockSurface: Lock surface and get picture data pointer
1859 *****************************************************************************
1860 * This function locks a surface and get the surface descriptor which amongst
1861 * other things has the pointer to the picture data.
1862 *****************************************************************************/
1863 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1867 /* Lock the surface to get a valid pointer to the picture buffer */
1868 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1869 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1870 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1871 NULL, &p_pic->p_sys->ddsd,
1872 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1874 if( dxresult != DD_OK )
1876 if( dxresult == DDERR_INVALIDPARAMS )
1878 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1879 * in an invalid params error */
1880 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1881 &p_pic->p_sys->ddsd,
1884 if( dxresult == DDERR_SURFACELOST )
1886 /* Your surface can be lost so be sure
1887 * to check this and restore it if needed */
1889 /* When using overlays with back-buffers, we need to restore
1890 * the front buffer so the back-buffers get restored as well. */
1891 if( p_vout->p_sys->b_using_overlay )
1892 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1894 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1896 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1897 &p_pic->p_sys->ddsd,
1900 if( dxresult == DDERR_SURFACELOST )
1901 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1904 if( dxresult != DD_OK )
1906 return VLC_EGENERIC;
1910 /* Now we have a pointer to the surface memory, we can update our picture
1912 if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
1915 DirectXUnlockSurface( p_vout, p_pic );
1916 return VLC_EGENERIC;
1922 /*****************************************************************************
1923 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1924 *****************************************************************************/
1925 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1927 /* Unlock the Surface */
1928 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1931 return VLC_EGENERIC;
1934 /*****************************************************************************
1935 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1936 *****************************************************************************/
1937 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
1942 uint32_t i_pixel_backup;
1945 ddsd.dwSize = sizeof(ddsd);
1946 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1947 &ddsd, DDLOCK_WAIT, NULL );
1948 if( dxresult != DD_OK ) return 0;
1950 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1952 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1955 *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1958 *(uint8_t *)ddsd.lpSurface = *pi_color;
1962 *(uint16_t *)ddsd.lpSurface = *pi_color;
1965 /* Seems to be problematic so we'll just put black as the colorkey */
1968 *(uint32_t *)ddsd.lpSurface = *pi_color;
1972 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1974 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1976 i_rgb = GetPixel( hdc, 0, 0 );
1977 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1980 ddsd.dwSize = sizeof(ddsd);
1981 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1982 &ddsd, DDLOCK_WAIT, NULL );
1983 if( dxresult != DD_OK ) return i_rgb;
1985 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1987 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1992 /*****************************************************************************
1993 * A few toolbox functions
1994 *****************************************************************************/
1995 void SwitchWallpaperMode( vout_thread_t *p_vout, vlc_bool_t b_on )
1999 if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
2001 hwnd = FindWindow( _T("Progman"), NULL );
2002 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
2003 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
2006 msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
2007 "wallpaper mode not supported" );
2011 p_vout->p_sys->b_wallpaper = b_on;
2013 msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
2015 if( p_vout->p_sys->b_wallpaper )
2017 p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
2018 p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
2020 ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
2021 ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
2025 ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
2026 ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
2029 /* Update desktop */
2030 InvalidateRect( hwnd, NULL, TRUE );
2031 UpdateWindow( hwnd );
2034 /*****************************************************************************
2035 * config variable callback
2036 *****************************************************************************/
2037 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
2038 LPTSTR psz_drivername, VOID* p_context,
2041 module_config_t *p_item = (module_config_t *)p_context;
2044 (char **)realloc( p_item->ppsz_list,
2045 (p_item->i_list+2) * sizeof(char *) );
2046 p_item->ppsz_list_text =
2047 (char **)realloc( p_item->ppsz_list_text,
2048 (p_item->i_list+2) * sizeof(char *) );
2050 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
2051 p_item->ppsz_list_text[p_item->i_list] = NULL;
2053 p_item->ppsz_list[p_item->i_list] = NULL;
2054 p_item->ppsz_list_text[p_item->i_list] = NULL;
2056 return TRUE; /* Keep enumerating */
2059 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2060 vlc_value_t newval, vlc_value_t oldval, void *d)
2062 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
2064 HINSTANCE hddraw_dll;
2066 module_config_t *p_item;
2069 p_item = config_FindConfig( p_this, psz_name );
2070 if( !p_item ) return VLC_SUCCESS;
2072 /* Clear-up the current list */
2073 if( p_item->i_list )
2075 /* Keep the first entry */
2076 for( i = 1; i < p_item->i_list; i++ )
2078 free( p_item->ppsz_list[i] );
2079 free( p_item->ppsz_list_text[i] );
2081 /* TODO: Remove when no more needed */
2082 p_item->ppsz_list[i] = NULL;
2083 p_item->ppsz_list_text[i] = NULL;
2087 /* Load direct draw DLL */
2088 hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
2089 if( hddraw_dll == NULL ) return VLC_SUCCESS;
2091 OurDirectDrawEnumerateEx =
2093 (void *)GetProcAddress( hddraw_dll, "DirectDrawEnumerateExA" );
2095 (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
2098 if( OurDirectDrawEnumerateEx )
2100 /* Enumerate displays */
2101 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
2102 DDENUM_ATTACHEDSECONDARYDEVICES );
2105 FreeLibrary( hddraw_dll );
2107 /* Signal change to the interface */
2108 p_item->b_dirty = VLC_TRUE;
2113 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
2114 vlc_value_t oldval, vlc_value_t newval,
2117 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2119 if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
2120 (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
2122 playlist_t *p_playlist;
2125 (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
2129 /* Modify playlist as well because the vout might have to be
2131 var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
2132 var_Set( p_playlist, "directx-wallpaper", newval );
2134 vlc_object_release( p_playlist );
2137 p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
2143 /*****************************************************************************
2144 * SetPalette: sets an 8 bpp palette
2145 *****************************************************************************/
2146 static void SetPalette( vout_thread_t *p_vout,
2147 uint16_t *red, uint16_t *green, uint16_t *blue )
2149 msg_Err( p_vout, "FIXME: SetPalette unimplemented" );