1 /*****************************************************************************
2 * directx.c: Windows DirectDraw video output
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
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 *****************************************************************************/
38 #include <errno.h> /* ENOMEM */
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
47 #include <vlc_playlist.h> /* needed for wallpaper */
50 #include <commctrl.h> /* ListView_(Get|Set)* */
53 # include <multimon.h>
55 #undef GetSystemMetrics
57 #ifndef MONITOR_DEFAULTTONEAREST
58 # define MONITOR_DEFAULTTONEAREST 2
63 /*****************************************************************************
64 * picture_sys_t: direct buffer method descriptor
65 *****************************************************************************
66 * This structure is part of the picture descriptor, it describes the
67 * DirectX specific properties of a direct buffer.
68 *****************************************************************************/
71 LPDIRECTDRAWSURFACE2 p_surface;
72 LPDIRECTDRAWSURFACE2 p_front_surface;
76 /*****************************************************************************
78 * Defining them here allows us to get rid of the dxguid library during
80 *****************************************************************************/
84 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
85 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
87 /*****************************************************************************
89 *****************************************************************************/
90 static int OpenVideo ( vlc_object_t * );
91 static void CloseVideo ( vlc_object_t * );
93 static int Init ( vout_thread_t * );
94 static void End ( vout_thread_t * );
95 static int Manage ( vout_thread_t * );
96 static void Display ( vout_thread_t *, picture_t * );
97 static void FirstDisplay( vout_thread_t *, picture_t * );
98 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
100 static int NewPictureVec ( vout_thread_t *, picture_t * );
101 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
102 static int UpdatePictureStruct( vout_thread_t *, picture_t * );
104 static int DirectXInitDDraw ( vout_thread_t *p_vout );
105 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
106 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
107 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
108 static int DirectXCreateSurface ( vout_thread_t *p_vout,
109 LPDIRECTDRAWSURFACE2 *, int, int, int );
110 static void DirectXCloseSurface ( vout_thread_t *p_vout,
111 LPDIRECTDRAWSURFACE2 );
112 static int DirectXCreateClipper ( vout_thread_t *p_vout );
113 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
114 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
115 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
117 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
119 void SwitchWallpaperMode( vout_thread_t *, bool );
121 /* Object variables callbacks */
122 static int FindDevicesCallback( vlc_object_t *, char const *,
123 vlc_value_t, vlc_value_t, void * );
124 static int WallpaperCallback( vlc_object_t *, char const *,
125 vlc_value_t, vlc_value_t, void * );
127 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
128 LPTSTR psz_drivername, VOID* p_context,
131 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
132 LPTSTR psz_drivername, VOID* p_context,
135 /*****************************************************************************
137 *****************************************************************************/
138 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
139 #define HW_YUV_LONGTEXT N_( \
140 "Try to use hardware acceleration for YUV->RGB conversions. " \
141 "This option doesn't have any effect when using overlays." )
143 #define SYSMEM_TEXT N_("Use video buffers in system memory")
144 #define SYSMEM_LONGTEXT N_( \
145 "Create video buffers in system memory instead of video memory. This " \
146 "isn't recommended as usually using video memory allows to benefit from " \
147 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
148 "This option doesn't have any effect when using overlays." )
150 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
151 #define TRIPLEBUF_LONGTEXT N_( \
152 "Try to use triple buffering when using YUV overlays. That results in " \
153 "much better video quality (no flickering)." )
155 #define DEVICE_TEXT N_("Name of desired display device")
156 #define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " \
157 "specify the Windows device name of the display that you want the video " \
158 "window to open on. For example, \"\\\\.\\DISPLAY1\" or " \
159 "\"\\\\.\\DISPLAY2\"." )
161 #define WALLPAPER_TEXT N_("Enable wallpaper mode ")
162 #define WALLPAPER_LONGTEXT N_( \
163 "The wallpaper mode allows you to display the video as the desktop " \
164 "background. Note that this feature only works in overlay mode and " \
165 "the desktop must not already have a wallpaper." )
167 static const char *const ppsz_dev[] = { "" };
168 static const char *const ppsz_dev_text[] = { N_("Default") };
171 set_shortname( "DirectX" )
172 set_category( CAT_VIDEO )
173 set_subcategory( SUBCAT_VIDEO_VOUT )
174 add_bool( "directx-hw-yuv", true, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
176 add_bool( "directx-use-sysmem", false, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
178 add_bool( "directx-3buffering", true, NULL, TRIPLEBUF_TEXT,
179 TRIPLEBUF_LONGTEXT, true )
181 add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
183 change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback )
184 change_action_add( FindDevicesCallback, N_("Refresh list") )
186 add_bool( "directx-wallpaper", false, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
189 set_description( N_("DirectX (DirectDraw) video output") )
190 set_capability( "video output", 100 )
191 add_shortcut( "directx" )
192 set_callbacks( OpenVideo, CloseVideo )
194 /* FIXME: Hack to avoid unregistering our window class */
195 linked_with_a_crap_library_which_uses_atexit ()
199 /* check if we registered a window class because we need to
202 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
203 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
206 /*****************************************************************************
207 * OpenVideo: allocate DirectX video thread output method
208 *****************************************************************************
209 * This function allocates and initialize the DirectX vout method.
210 *****************************************************************************/
211 static int OpenVideo( vlc_object_t *p_this )
213 vout_thread_t * p_vout = (vout_thread_t *)p_this;
217 /* Allocate structure */
218 p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
219 if( p_vout->p_sys == NULL )
222 /* Initialisations */
223 p_vout->pf_init = Init;
224 p_vout->pf_end = End;
225 p_vout->pf_manage = Manage;
226 p_vout->pf_render = NULL;
227 p_vout->pf_display = FirstDisplay;
229 p_vout->p_sys->p_ddobject = NULL;
230 p_vout->p_sys->p_display = NULL;
231 p_vout->p_sys->p_current_surface = NULL;
232 p_vout->p_sys->p_clipper = NULL;
233 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
234 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
235 p_vout->p_sys->i_changes = 0;
236 p_vout->p_sys->b_wallpaper = 0;
237 vlc_mutex_init( &p_vout->p_sys->lock );
238 SetRectEmpty( &p_vout->p_sys->rect_display );
239 SetRectEmpty( &p_vout->p_sys->rect_parent );
241 /* Multimonitor stuff */
242 p_vout->p_sys->hmonitor = NULL;
243 p_vout->p_sys->p_display_driver = NULL;
244 p_vout->p_sys->MonitorFromWindow = NULL;
245 p_vout->p_sys->GetMonitorInfo = NULL;
246 if( (huser32 = GetModuleHandle( _T("USER32") ) ) )
248 p_vout->p_sys->MonitorFromWindow = (HMONITOR (WINAPI *)( HWND, DWORD ))
249 GetProcAddress( huser32, _T("MonitorFromWindow") );
250 p_vout->p_sys->GetMonitorInfo =
251 GetProcAddress( huser32, _T("GetMonitorInfoW") );
254 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
255 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
256 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
257 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
258 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
259 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
260 var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
262 p_vout->p_sys->b_cursor_hidden = 0;
263 p_vout->p_sys->i_lastmoved = mdate();
264 p_vout->p_sys->i_mouse_hide_timeout =
265 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
267 /* Set main window's size */
268 p_vout->p_sys->i_window_width = p_vout->i_window_width;
269 p_vout->p_sys->i_window_height = p_vout->i_window_height;
271 if ( !CreateEventThread( p_vout ) )
274 /* Initialise DirectDraw */
275 if( DirectXInitDDraw( p_vout ) )
277 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
281 /* Create the directx display */
282 if( DirectXCreateDisplay( p_vout ) )
284 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
288 /* Variable to indicate if the window should be on top of others */
289 /* Trigger a callback right now */
290 var_TriggerCallback( p_vout, "video-on-top" );
292 /* Variable to indicate if the window should be on top of others */
293 /* Trigger a callback right now */
294 var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
295 val.psz_string = _("Wallpaper");
296 var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
297 var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
298 var_TriggerCallback( p_vout, "directx-wallpaper" );
300 DisableScreensaver ( p_vout );
305 CloseVideo( VLC_OBJECT(p_vout) );
309 /*****************************************************************************
310 * Init: initialize DirectX video thread output method
311 *****************************************************************************
312 * This function create the directx surfaces needed by the output thread.
313 * It is called at the beginning of the thread.
314 *****************************************************************************/
315 static int Init( vout_thread_t *p_vout )
319 /* Get a few default parameters */
320 p_vout->p_sys->b_using_overlay = var_GetBool( p_vout, "overlay" );
321 p_vout->p_sys->b_use_sysmem = var_GetBool( p_vout, "directx-use-sysmem" );
322 p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
323 p_vout->p_sys->b_3buf_overlay = var_GetBool( p_vout, "directx-3buffering" );
325 /* Initialise DirectDraw if not already done.
326 * We do this here because on multi-monitor systems we may have to
327 * re-create the directdraw surfaces. */
328 if( !p_vout->p_sys->p_ddobject &&
329 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
331 msg_Err( p_vout, "cannot initialize DirectDraw" );
335 /* Create the directx display */
336 if( !p_vout->p_sys->p_display &&
337 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
339 msg_Err( p_vout, "cannot initialize DirectDraw" );
343 /* Initialize the output structure.
344 * Since DirectDraw can do rescaling for us, stick to the default
345 * coordinates and aspect. */
346 p_vout->output.i_width = p_vout->render.i_width;
347 p_vout->output.i_height = p_vout->render.i_height;
348 p_vout->output.i_aspect = p_vout->render.i_aspect;
349 p_vout->fmt_out = p_vout->fmt_in;
350 UpdateRects( p_vout, true );
352 #define MAX_DIRECTBUFFERS 1
353 /* Right now we use only 1 directbuffer because we don't want the
354 * video decoder to decode directly into direct buffers as they are
355 * created into video memory and video memory is _really_ slow */
357 /* Choose the chroma we will try first. */
358 switch( p_vout->render.i_chroma )
361 p_vout->output.i_chroma = VLC_CODEC_YUYV;
364 p_vout->output.i_chroma = VLC_CODEC_UYVY;
367 p_vout->output.i_chroma = VLC_CODEC_YVYU;
370 p_vout->output.i_chroma = VLC_CODEC_I420;
373 msg_Dbg( p_vout, "use default chroma YV12 for render " \
375 (char *)&p_vout->render.i_chroma);
376 p_vout->output.i_chroma = VLC_CODEC_YV12;
380 NewPictureVec( p_vout, p_vout->p_picture );
382 i_chroma_backup = p_vout->output.i_chroma;
384 if( !I_OUTPUTPICTURES )
386 /* hmmm, it didn't work! Let's try commonly supported chromas */
387 if( p_vout->output.i_chroma != VLC_CODEC_I420 )
389 p_vout->output.i_chroma = VLC_CODEC_YV12;
390 NewPictureVec( p_vout, p_vout->p_picture );
392 if( !I_OUTPUTPICTURES )
394 /* hmmm, it still didn't work! Let's try another one */
395 p_vout->output.i_chroma = VLC_CODEC_YUYV;
396 NewPictureVec( p_vout, p_vout->p_picture );
400 if( !I_OUTPUTPICTURES )
402 /* If it still didn't work then don't try to use an overlay */
403 p_vout->output.i_chroma = i_chroma_backup;
404 p_vout->p_sys->b_using_overlay = 0;
405 msg_Warn( p_vout, "Could not initialize directx overlay" ) ;
406 NewPictureVec( p_vout, p_vout->p_picture );
409 /* Change the window title bar text */
410 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
412 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
416 /*****************************************************************************
417 * End: terminate Sys video thread output method
418 *****************************************************************************
419 * Terminate an output method created by Create.
420 * It is called at the end of the thread.
421 *****************************************************************************/
422 static void End( vout_thread_t *p_vout )
424 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
426 DirectXCloseDisplay( p_vout );
427 DirectXCloseDDraw( p_vout );
432 /*****************************************************************************
433 * CloseVideo: destroy Sys video thread output method
434 *****************************************************************************
435 * Terminate an output method created by Create
436 *****************************************************************************/
437 static void CloseVideo( vlc_object_t *p_this )
439 vout_thread_t * p_vout = (vout_thread_t *)p_this;
441 StopEventThread( p_vout );
443 /* Make sure the wallpaper is restored */
444 var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
445 SwitchWallpaperMode( p_vout, false );
447 RestoreScreensaver( p_vout );
449 free( p_vout->p_sys );
452 /*****************************************************************************
453 * Manage: handle Sys events
454 *****************************************************************************
455 * This function should be called regularly by the video output thread.
456 * It returns a non null value if an error occurred.
457 *****************************************************************************/
458 static int Manage( vout_thread_t *p_vout )
460 /* If we do not control our window, we check for geometry changes
461 * ourselves because the parent might not send us its events. */
462 vlc_mutex_lock( &p_vout->p_sys->lock );
463 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
468 vlc_mutex_unlock( &p_vout->p_sys->lock );
470 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
471 point.x = point.y = 0;
472 ClientToScreen( p_vout->p_sys->hparent, &point );
473 OffsetRect( &rect_parent, point.x, point.y );
475 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
477 p_vout->p_sys->rect_parent = rect_parent;
479 /* This one is to force the update even if only
480 * the position has changed */
481 SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
482 rect_parent.right - rect_parent.left,
483 rect_parent.bottom - rect_parent.top, 0 );
485 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
486 rect_parent.right - rect_parent.left,
487 rect_parent.bottom - rect_parent.top, 0 );
492 vlc_mutex_unlock( &p_vout->p_sys->lock );
498 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
500 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
502 /* Check if we are still on the same monitor */
503 if( p_vout->p_sys->MonitorFromWindow &&
504 p_vout->p_sys->hmonitor !=
505 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
506 MONITOR_DEFAULTTONEAREST ) )
508 /* This will force the vout core to recreate the picture buffers */
509 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
513 /* autoscale toggle */
514 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
516 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
518 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
519 p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
521 UpdateRects( p_vout, true );
525 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
527 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
529 p_vout->b_autoscale = false;
531 (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
532 UpdateRects( p_vout, true );
535 /* Check for cropping / aspect changes */
536 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
537 p_vout->i_changes & VOUT_ASPECT_CHANGE )
539 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
540 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
542 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
543 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
544 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
545 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
546 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
547 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
548 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
549 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
550 UpdateRects( p_vout, true );
553 /* We used to call the Win32 PeekMessage function here to read the window
554 * messages. But since window can stay blocked into this function for a
555 * long time (for example when you move your window on the screen), I
556 * decided to isolate PeekMessage in another thread. */
558 if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
560 SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
561 p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
562 DirectDrawUpdateOverlay( p_vout );
568 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
569 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
571 Win32ToggleFullscreen( p_vout );
573 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
574 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
580 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
581 (mdate() - p_vout->p_sys->i_lastmoved) >
582 p_vout->p_sys->i_mouse_hide_timeout )
587 /* Hide the cursor only if it is inside our window */
588 GetCursorPos( &point );
589 hwnd = WindowFromPoint(point);
590 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
592 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
596 p_vout->p_sys->i_lastmoved = mdate();
601 * "Always on top" status change
603 if( p_vout->p_sys->b_on_top_change )
606 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
608 var_Get( p_vout, "video-on-top", &val );
610 /* Set the window on top if necessary */
611 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
614 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
615 MF_BYCOMMAND | MFS_CHECKED );
616 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
617 SWP_NOSIZE | SWP_NOMOVE );
620 /* The window shouldn't be on top */
621 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
624 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
625 MF_BYCOMMAND | MFS_UNCHECKED );
626 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
627 SWP_NOSIZE | SWP_NOMOVE );
630 p_vout->p_sys->b_on_top_change = false;
633 /* Check if the event thread is still running */
634 if( !vlc_object_alive (p_vout->p_sys->p_event) )
636 return VLC_EGENERIC; /* exit */
642 /*****************************************************************************
643 * Display: displays previously rendered output
644 *****************************************************************************
645 * This function sends the currently rendered image to the display, wait until
646 * it is displayed and switch the two rendering buffers, preparing next frame.
647 *****************************************************************************/
648 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
652 if( (p_vout->p_sys->p_display == NULL) )
654 msg_Warn( p_vout, "no display!" );
658 /* Our surface can be lost so be sure to check this
659 * and restore it if need be */
660 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
661 == DDERR_SURFACELOST )
663 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
664 p_vout->p_sys->b_using_overlay )
665 DirectDrawUpdateOverlay( p_vout );
668 if( !p_vout->p_sys->b_using_overlay )
672 /* We ask for the "NOTEARING" option */
673 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
674 ddbltfx.dwSize = sizeof(DDBLTFX);
675 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
677 /* Blit video surface to display */
678 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
679 &p_vout->p_sys->rect_dest_clipped,
680 p_pic->p_sys->p_surface,
681 &p_vout->p_sys->rect_src_clipped,
682 DDBLT_ASYNC, &ddbltfx );
683 if( dxresult != DD_OK )
685 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
689 else /* using overlay */
691 /* Flip the overlay buffers if we are using back buffers */
692 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
697 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
699 if( dxresult != DD_OK )
701 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
704 /* set currently displayed pic */
705 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
707 /* Lock surface to get all the required info */
708 if( DirectXLockSurface( p_vout, p_pic ) )
711 msg_Warn( p_vout, "cannot lock surface" );
714 DirectXUnlockSurface( p_vout, p_pic );
719 ** this function is only used once when the first picture is received
720 ** this function will show the video window once video is ready for
724 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
726 /* get initial picture rendered on overlay surface */
727 Display(p_vout, p_pic);
729 IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
730 DDWAITVB_BLOCKBEGIN, NULL);
732 if( p_vout->p_sys->b_using_overlay )
734 HBRUSH brush = CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey );
735 /* set the colorkey as the backgound brush for the video window */
736 SetClassLongPtr( p_vout->p_sys->hvideownd, GCLP_HBRBACKGROUND, (LONG_PTR)brush );
739 ** Video window is initially hidden, show it now since we got a
742 SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
750 /* use and restores proper display function for further pictures */
751 p_vout->pf_display = Display;
754 /* following functions are local */
756 /*****************************************************************************
757 * DirectXEnumCallback: Device enumeration
758 *****************************************************************************
759 * This callback function is called by DirectDraw once for each
760 * available DirectDraw device.
761 *****************************************************************************/
762 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
763 LPTSTR psz_drivername, VOID* p_context,
766 vout_thread_t *p_vout = (vout_thread_t *)p_context;
769 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
773 var_Get( p_vout, "directx-device", &device );
775 if( ( !device.psz_string || !*device.psz_string ) &&
776 hmon == p_vout->p_sys->hmonitor )
778 free( device.psz_string );
780 else if( strcmp( psz_drivername, device.psz_string ) == 0 )
782 MONITORINFO monitor_info;
783 monitor_info.cbSize = sizeof( MONITORINFO );
785 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
789 /* Move window to the right screen */
790 GetWindowRect( p_vout->p_sys->hwnd, &rect );
791 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
793 rect.left = monitor_info.rcWork.left;
794 rect.top = monitor_info.rcWork.top;
795 msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
796 "position to %ld,%ld", rect.left, rect.top );
797 SetWindowPos( p_vout->p_sys->hwnd, NULL,
798 rect.left, rect.top, 0, 0,
799 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
803 p_vout->p_sys->hmonitor = hmon;
804 free( device.psz_string );
808 free( device.psz_string );
809 return TRUE; /* Keep enumerating */
812 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
813 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
814 if( p_vout->p_sys->p_display_driver )
815 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
818 return TRUE; /* Keep enumerating */
821 /*****************************************************************************
822 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
823 *****************************************************************************
824 * This function initialise and allocate resources for DirectDraw.
825 *****************************************************************************/
826 static int DirectXInitDDraw( vout_thread_t *p_vout )
829 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
830 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
832 LPDIRECTDRAW p_ddobject;
834 msg_Dbg( p_vout, "DirectXInitDDraw" );
836 /* Load direct draw DLL */
837 p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
838 if( p_vout->p_sys->hddraw_dll == NULL )
840 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
844 OurDirectDrawCreate =
845 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
846 _T("DirectDrawCreate") );
847 if( OurDirectDrawCreate == NULL )
849 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
853 OurDirectDrawEnumerateEx =
854 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
855 _T("DirectDrawEnumerateExW") );
857 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
861 var_Get( p_vout, "directx-device", &device );
862 if( device.psz_string )
864 msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
865 free( device.psz_string );
868 p_vout->p_sys->hmonitor =
869 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
870 MONITOR_DEFAULTTONEAREST );
872 /* Enumerate displays */
873 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
874 DDENUM_ATTACHEDSECONDARYDEVICES );
877 /* Initialize DirectDraw now */
878 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
880 if( dxresult != DD_OK )
882 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
886 /* Get the IDirectDraw2 interface */
887 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
888 &p_vout->p_sys->p_ddobject );
889 /* Release the unused interface */
890 IDirectDraw_Release( p_ddobject );
891 if( dxresult != DD_OK )
893 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
897 /* Set DirectDraw Cooperative level, ie what control we want over Windows
899 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
900 NULL, DDSCL_NORMAL );
901 if( dxresult != DD_OK )
903 msg_Err( p_vout, "cannot set direct draw cooperative level" );
907 /* Get the size of the current display device */
908 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
910 MONITORINFO monitor_info;
911 monitor_info.cbSize = sizeof( MONITORINFO );
912 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
914 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
918 p_vout->p_sys->rect_display.left = 0;
919 p_vout->p_sys->rect_display.top = 0;
920 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
921 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
924 msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
925 p_vout->p_sys->rect_display.left,
926 p_vout->p_sys->rect_display.top,
927 p_vout->p_sys->rect_display.right,
928 p_vout->p_sys->rect_display.bottom );
930 /* Probe the capabilities of the hardware */
931 DirectXGetDDrawCaps( p_vout );
933 msg_Dbg( p_vout, "End DirectXInitDDraw" );
937 if( p_vout->p_sys->p_ddobject )
938 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
939 if( p_vout->p_sys->hddraw_dll )
940 FreeLibrary( p_vout->p_sys->hddraw_dll );
941 p_vout->p_sys->hddraw_dll = NULL;
942 p_vout->p_sys->p_ddobject = NULL;
946 /*****************************************************************************
947 * DirectXCreateDisplay: create the DirectDraw display.
948 *****************************************************************************
949 * Create and initialize display according to preferences specified in the vout
951 *****************************************************************************/
952 static int DirectXCreateDisplay( vout_thread_t *p_vout )
956 LPDIRECTDRAWSURFACE p_display;
958 msg_Dbg( p_vout, "DirectXCreateDisplay" );
960 /* Now get the primary surface. This surface is what you actually see
962 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
963 ddsd.dwSize = sizeof(DDSURFACEDESC);
964 ddsd.dwFlags = DDSD_CAPS;
965 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
967 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
968 &ddsd, &p_display, NULL );
969 if( dxresult != DD_OK )
971 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
975 dxresult = IDirectDrawSurface_QueryInterface( p_display,
976 &IID_IDirectDrawSurface2,
977 &p_vout->p_sys->p_display );
978 /* Release the old interface */
979 IDirectDrawSurface_Release( p_display );
980 if ( dxresult != DD_OK )
982 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
983 "(error %li)", dxresult );
987 /* The clipper will be used only in non-overlay mode */
988 DirectXCreateClipper( p_vout );
990 /* Make sure the colorkey will be painted */
991 p_vout->p_sys->i_colorkey = 1;
992 p_vout->p_sys->i_rgb_colorkey =
993 DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
995 UpdateRects( p_vout, 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 bool b_rgb_surface =
1110 ( i_chroma == VLC_CODEC_RGB8 )
1111 || ( i_chroma == VLC_CODEC_RGB15 )
1112 || ( i_chroma == VLC_CODEC_RGB16 )
1113 || ( i_chroma == VLC_CODEC_RGB24 )
1114 || ( i_chroma == VLC_CODEC_RGB32 );
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( DirectDrawUpdateOverlay( 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 * DirectDrawUpdateOverlay: 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 DirectDrawUpdateOverlay( 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 unsigned i_x, i_y, i_width, i_height;
1196 rect_src.left = p_vout->fmt_out.i_x_offset;
1197 rect_src.top = p_vout->fmt_out.i_y_offset;
1198 rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
1199 rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
1201 rect_dest = p_vout->p_sys->rect_display;
1202 vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1203 &i_x, &i_y, &i_width, &i_height );
1205 rect_dest.left += i_x;
1206 rect_dest.right = rect_dest.left + i_width;
1207 rect_dest.top += i_y;
1208 rect_dest.bottom = rect_dest.top + i_height;
1211 vlc_mutex_lock( &p_vout->p_sys->lock );
1212 if( p_vout->p_sys->p_current_surface == NULL )
1214 vlc_mutex_unlock( &p_vout->p_sys->lock );
1215 return VLC_EGENERIC;
1218 /* The new window dimensions should already have been computed by the
1219 * caller of this function */
1221 /* Position and show the overlay */
1222 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1223 ddofx.dwSize = sizeof(DDOVERLAYFX);
1224 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1225 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1227 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1229 dxresult = IDirectDrawSurface2_UpdateOverlay(
1230 p_vout->p_sys->p_current_surface,
1231 &rect_src, p_vout->p_sys->p_display, &rect_dest,
1234 vlc_mutex_unlock( &p_vout->p_sys->lock );
1236 if(dxresult != DD_OK)
1238 msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
1239 return VLC_EGENERIC;
1245 /*****************************************************************************
1246 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1247 *****************************************************************************
1248 * This function returns all resources allocated by DirectXInitDDraw.
1249 *****************************************************************************/
1250 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1252 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1253 if( p_vout->p_sys->p_ddobject != NULL )
1255 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1256 p_vout->p_sys->p_ddobject = NULL;
1259 if( p_vout->p_sys->hddraw_dll != NULL )
1261 FreeLibrary( p_vout->p_sys->hddraw_dll );
1262 p_vout->p_sys->hddraw_dll = NULL;
1265 free( p_vout->p_sys->p_display_driver );
1266 p_vout->p_sys->p_display_driver = NULL;
1268 p_vout->p_sys->hmonitor = NULL;
1271 /*****************************************************************************
1272 * DirectXCloseDisplay: close and reset the DirectX display device
1273 *****************************************************************************
1274 * This function returns all resources allocated by DirectXCreateDisplay.
1275 *****************************************************************************/
1276 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1278 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1280 if( p_vout->p_sys->p_clipper != NULL )
1282 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1283 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1284 p_vout->p_sys->p_clipper = NULL;
1287 if( p_vout->p_sys->p_display != NULL )
1289 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1290 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1291 p_vout->p_sys->p_display = NULL;
1295 /*****************************************************************************
1296 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1297 *****************************************************************************
1298 * This function returns all resources allocated for the surface.
1299 *****************************************************************************/
1300 static void DirectXCloseSurface( vout_thread_t *p_vout,
1301 LPDIRECTDRAWSURFACE2 p_surface )
1303 msg_Dbg( p_vout, "DirectXCloseSurface" );
1304 if( p_surface != NULL )
1306 IDirectDrawSurface2_Release( p_surface );
1310 /*****************************************************************************
1311 * NewPictureVec: allocate a picture
1312 * FIXME? make it work for i_num_pic pictures...
1313 *****************************************************************************
1314 * Returns 0 on success, -1 otherwise
1315 *****************************************************************************/
1316 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic )
1319 int i_ret = VLC_SUCCESS;
1320 LPDIRECTDRAWSURFACE2 p_surface;
1322 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1323 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1324 (char *)&p_vout->output.i_chroma );
1326 I_OUTPUTPICTURES = 0;
1328 /* First we try to use an YUV overlay surface.
1329 * The overlay surface that we create won't be used to decode directly
1330 * into it because accessing video memory directly is way to slow (remember
1331 * that pictures are decoded macroblock per macroblock). Instead the video
1332 * will be decoded in picture buffers in system memory which will then be
1333 * memcpy() to the overlay surface. */
1334 if( p_vout->p_sys->b_using_overlay )
1336 /* Triple buffering rocks! it doesn't have any processing overhead
1337 * (you don't have to wait for the vsync) and provides for a very nice
1338 * video quality (no tearing). */
1339 if( p_vout->p_sys->b_3buf_overlay )
1340 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1341 p_vout->output.i_chroma,
1342 p_vout->p_sys->b_using_overlay,
1343 2 /* number of backbuffers */ );
1345 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1347 /* Try to reduce the number of backbuffers */
1348 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1349 p_vout->output.i_chroma,
1350 p_vout->p_sys->b_using_overlay,
1351 0 /* number of backbuffers */ );
1354 if( i_ret == VLC_SUCCESS )
1357 picture_t front_pic;
1358 picture_sys_t front_pic_sys;
1359 front_pic.p_sys = &front_pic_sys;
1361 /* Allocate internal structure */
1362 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1363 if( p_pic[0].p_sys == NULL )
1365 DirectXCloseSurface( p_vout, p_surface );
1369 /* set front buffer */
1370 p_pic[0].p_sys->p_front_surface = p_surface;
1372 /* Get the back buffer */
1373 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1374 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1375 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1376 p_surface, &dds_caps,
1377 &p_pic[0].p_sys->p_surface ) )
1379 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1380 /* front buffer is the same as back buffer */
1381 p_pic[0].p_sys->p_surface = p_surface;
1385 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1386 p_pic[0].p_sys->p_front_surface;
1388 /* Reset the front buffer memory */
1389 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1392 for( i = 0; i < front_pic.i_planes; i++ )
1393 for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1394 memset( front_pic.p[i].p_pixels + j *
1395 front_pic.p[i].i_pitch, 127,
1396 front_pic.p[i].i_visible_pitch );
1398 DirectXUnlockSurface( p_vout, &front_pic );
1401 DirectDrawUpdateOverlay( p_vout );
1402 I_OUTPUTPICTURES = 1;
1403 msg_Dbg( p_vout, "YUV overlay created successfully" );
1407 /* As we can't have an overlay, we'll try to create a plain offscreen
1408 * surface. This surface will reside in video memory because there's a
1409 * better chance then that we'll be able to use some kind of hardware
1410 * acceleration like rescaling, blitting or YUV->RGB conversions.
1411 * We then only need to blit this surface onto the main display when we
1412 * want to display it */
1413 if( !p_vout->p_sys->b_using_overlay )
1415 if( p_vout->p_sys->b_hw_yuv )
1419 bool b_result = false;
1421 /* Check if the chroma is supported first. This is required
1422 * because a few buggy drivers don't mind creating the surface
1423 * even if they don't know about the chroma. */
1424 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1425 &i_codes, NULL ) == DD_OK )
1427 pi_codes = malloc( i_codes * sizeof(DWORD) );
1428 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1429 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1431 for( i = 0; i < (int)i_codes; i++ )
1433 if( p_vout->output.i_chroma == pi_codes[i] )
1443 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1444 p_vout->output.i_chroma,
1446 0 /* no back buffers */ );
1448 p_vout->p_sys->b_hw_yuv = false;
1451 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1453 /* Our last choice is to use a plain RGB surface */
1454 DDPIXELFORMAT ddpfPixelFormat;
1456 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1457 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1460 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1462 switch( ddpfPixelFormat.dwRGBBitCount )
1465 p_vout->output.i_chroma = VLC_CODEC_RGB8;
1466 p_vout->output.pf_setpalette = SetPalette;
1469 p_vout->output.i_chroma = VLC_CODEC_RGB15;
1472 p_vout->output.i_chroma = VLC_CODEC_RGB16;
1475 p_vout->output.i_chroma = VLC_CODEC_RGB24;
1478 p_vout->output.i_chroma = VLC_CODEC_RGB32;
1481 msg_Err( p_vout, "unknown screen depth" );
1482 return VLC_EGENERIC;
1484 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1485 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1486 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1489 p_vout->p_sys->b_hw_yuv = 0;
1491 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1492 p_vout->output.i_chroma,
1494 0 /* no back buffers */ );
1496 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1498 /* Give it a last try with b_use_sysmem enabled */
1499 p_vout->p_sys->b_use_sysmem = 1;
1501 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1502 p_vout->output.i_chroma,
1504 0 /* no back buffers */ );
1508 if( i_ret == VLC_SUCCESS )
1510 /* Allocate internal structure */
1511 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1512 if( p_pic[0].p_sys == NULL )
1514 DirectXCloseSurface( p_vout, p_surface );
1518 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1521 I_OUTPUTPICTURES = 1;
1523 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1524 (char *)&p_vout->output.i_chroma );
1529 /* Now that we've got all our direct-buffers, we can finish filling in the
1530 * picture_t structures */
1531 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1533 p_pic[i].i_status = DESTROYED_PICTURE;
1534 p_pic[i].i_type = DIRECT_PICTURE;
1535 p_pic[i].b_slow = true;
1536 p_pic[i].pf_lock = DirectXLockSurface;
1537 p_pic[i].pf_unlock = DirectXUnlockSurface;
1538 PP_OUTPUTPICTURE[i] = &p_pic[i];
1540 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1543 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1544 I_OUTPUTPICTURES = 0;
1545 msg_Err( p_vout, "cannot lock surface" );
1546 return VLC_EGENERIC;
1548 DirectXUnlockSurface( p_vout, &p_pic[i] );
1551 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1552 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1557 /*****************************************************************************
1558 * FreePicture: destroy a picture vector allocated with NewPictureVec
1559 *****************************************************************************
1561 *****************************************************************************/
1562 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1567 vlc_mutex_lock( &p_vout->p_sys->lock );
1568 p_vout->p_sys->p_current_surface = 0;
1569 vlc_mutex_unlock( &p_vout->p_sys->lock );
1571 for( i = 0; i < i_num_pics; i++ )
1573 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1575 for( i = 0; i < i_num_pics; i++ )
1577 free( p_pic[i].p_sys );
1582 /*****************************************************************************
1583 * UpdatePictureStruct: updates the internal data in the picture_t structure
1584 *****************************************************************************
1585 * This will setup stuff for use by the video_output thread
1586 *****************************************************************************/
1587 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic )
1589 switch( p_vout->output.i_chroma )
1591 case VLC_CODEC_RGB8:
1592 case VLC_CODEC_RGB15:
1593 case VLC_CODEC_RGB16:
1594 case VLC_CODEC_RGB24:
1595 case VLC_CODEC_RGB32:
1596 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1597 p_pic->p->i_lines = p_vout->output.i_height;
1598 p_pic->p->i_visible_lines = p_vout->output.i_height;
1599 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1600 switch( p_vout->output.i_chroma )
1602 case VLC_CODEC_RGB8:
1603 p_pic->p->i_pixel_pitch = 1;
1605 case VLC_CODEC_RGB15:
1606 case VLC_CODEC_RGB16:
1607 p_pic->p->i_pixel_pitch = 2;
1609 case VLC_CODEC_RGB24:
1610 p_pic->p->i_pixel_pitch = 3;
1612 case VLC_CODEC_RGB32:
1613 p_pic->p->i_pixel_pitch = 4;
1616 return VLC_EGENERIC;
1618 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1619 p_pic->p->i_pixel_pitch;
1620 p_pic->i_planes = 1;
1623 case VLC_CODEC_YV12:
1625 /* U and V inverted compared to I420
1626 * Fixme: this should be handled by the vout core */
1627 /* could this be right? */
1628 p_vout->output.i_chroma = VLC_CODEC_I420;
1630 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1631 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1632 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1633 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1634 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1635 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1636 p_pic->p[Y_PLANE].i_pixel_pitch;
1638 p_pic->V_PIXELS = p_pic->Y_PIXELS
1639 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1640 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1641 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1642 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1643 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1644 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1645 p_pic->p[V_PLANE].i_pixel_pitch;
1647 p_pic->U_PIXELS = p_pic->V_PIXELS
1648 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1649 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1650 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1651 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1652 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1653 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1654 p_pic->p[U_PLANE].i_pixel_pitch;
1656 p_pic->i_planes = 3;
1659 case VLC_CODEC_I420:
1661 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1662 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1663 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1664 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1665 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1666 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1667 p_pic->p[Y_PLANE].i_pixel_pitch;
1669 p_pic->U_PIXELS = p_pic->Y_PIXELS
1670 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1671 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1672 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1673 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1674 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1675 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1676 p_pic->p[U_PLANE].i_pixel_pitch;
1678 p_pic->V_PIXELS = p_pic->U_PIXELS
1679 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1680 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1681 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1682 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1683 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1684 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1685 p_pic->p[V_PLANE].i_pixel_pitch;
1687 p_pic->i_planes = 3;
1690 case VLC_CODEC_UYVY:
1691 case VLC_CODEC_YUYV:
1693 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1694 p_pic->p->i_lines = p_vout->output.i_height;
1695 p_pic->p->i_visible_lines = p_vout->output.i_height;
1696 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1697 p_pic->p->i_pixel_pitch = 2;
1698 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1699 p_pic->p->i_pixel_pitch;
1701 p_pic->i_planes = 1;
1705 /* Unknown chroma, tell the guy to get lost */
1706 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1707 p_vout->output.i_chroma,
1708 (char*)&p_vout->output.i_chroma );
1709 return VLC_EGENERIC;
1715 /*****************************************************************************
1716 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1717 *****************************************************************************
1718 * It is nice to know which features are supported by the hardware so we can
1719 * find ways to optimize our rendering.
1720 *****************************************************************************/
1721 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1726 /* This is just an indication of whether or not we'll support overlay,
1727 * but with this test we don't know if we support YUV overlay */
1728 memset( &ddcaps, 0, sizeof( DDCAPS ));
1729 ddcaps.dwSize = sizeof(DDCAPS);
1730 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1732 if(dxresult != DD_OK )
1734 msg_Warn( p_vout, "cannot get caps" );
1738 bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1739 bHasColorKey, bCanStretch, bCanBltFourcc,
1740 bAlignBoundarySrc, bAlignBoundaryDest,
1741 bAlignSizeSrc, bAlignSizeDest;
1743 /* Determine if the hardware supports overlay surfaces */
1744 bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1745 /* Determine if the hardware supports overlay surfaces */
1746 bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1747 /* Determine if the hardware supports overlay deinterlacing */
1748 bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1749 /* Determine if the hardware supports colorkeying */
1750 bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1751 /* Determine if the hardware supports scaling of the overlay surface */
1752 bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1753 /* Determine if the hardware supports color conversion during a blit */
1754 bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1755 /* Determine overlay source boundary alignment */
1756 bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1757 /* Determine overlay destination boundary alignment */
1758 bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1759 /* Determine overlay destination size alignment */
1760 bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1761 /* Determine overlay destination size alignment */
1762 bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1764 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1765 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1767 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1768 bHasColorKey, bCanStretch, bCanBltFourcc );
1770 if( bAlignBoundarySrc || bAlignBoundaryDest ||
1771 bAlignSizeSrc || bAlignSizeDest )
1773 if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1774 ddcaps.dwAlignBoundarySrc;
1775 if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1776 ddcaps.dwAlignBoundaryDest;
1777 if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1778 ddcaps.dwAlignSizeSrc;
1779 if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1780 ddcaps.dwAlignSizeDest;
1782 msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1783 "align_boundary_dest=%i,%i "
1784 "align_size_src=%i,%i align_size_dest=%i,%i",
1785 bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1786 bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1787 bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1788 bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1791 /* Don't ask for troubles */
1792 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1796 /*****************************************************************************
1797 * DirectXLockSurface: Lock surface and get picture data pointer
1798 *****************************************************************************
1799 * This function locks a surface and get the surface descriptor which amongst
1800 * other things has the pointer to the picture data.
1801 *****************************************************************************/
1802 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1806 /* Lock the surface to get a valid pointer to the picture buffer */
1807 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1808 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1809 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1810 NULL, &p_pic->p_sys->ddsd,
1811 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1813 if( dxresult != DD_OK )
1815 if( dxresult == DDERR_INVALIDPARAMS )
1817 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1818 * in an invalid params error */
1819 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1820 &p_pic->p_sys->ddsd,
1823 if( dxresult == DDERR_SURFACELOST )
1825 /* Your surface can be lost so be sure
1826 * to check this and restore it if needed */
1828 /* When using overlays with back-buffers, we need to restore
1829 * the front buffer so the back-buffers get restored as well. */
1830 if( p_vout->p_sys->b_using_overlay )
1831 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1833 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1835 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1836 &p_pic->p_sys->ddsd,
1839 if( dxresult == DDERR_SURFACELOST )
1840 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1843 if( dxresult != DD_OK )
1845 return VLC_EGENERIC;
1849 /* Now we have a pointer to the surface memory, we can update our picture
1851 if( UpdatePictureStruct( p_vout, p_pic )
1854 DirectXUnlockSurface( p_vout, p_pic );
1855 return VLC_EGENERIC;
1861 /*****************************************************************************
1862 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1863 *****************************************************************************/
1864 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1866 VLC_UNUSED( p_vout );
1868 /* Unlock the Surface */
1869 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1872 return VLC_EGENERIC;
1875 /*****************************************************************************
1876 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1877 *****************************************************************************/
1878 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
1883 uint32_t i_pixel_backup;
1886 ddsd.dwSize = sizeof(ddsd);
1887 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1888 &ddsd, DDLOCK_WAIT, NULL );
1889 if( dxresult != DD_OK ) return 0;
1891 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1893 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1896 *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1899 *(uint8_t *)ddsd.lpSurface = *pi_color;
1903 *(uint16_t *)ddsd.lpSurface = *pi_color;
1906 /* Seems to be problematic so we'll just put black as the colorkey */
1909 *(uint32_t *)ddsd.lpSurface = *pi_color;
1913 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1915 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1917 i_rgb = GetPixel( hdc, 0, 0 );
1918 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1921 ddsd.dwSize = sizeof(ddsd);
1922 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1923 &ddsd, DDLOCK_WAIT, NULL );
1924 if( dxresult != DD_OK ) return i_rgb;
1926 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1928 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1933 /*****************************************************************************
1934 * A few toolbox functions
1935 *****************************************************************************/
1936 void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
1940 if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
1942 hwnd = FindWindow( _T("Progman"), NULL );
1943 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
1944 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
1947 msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
1948 "wallpaper mode not supported" );
1952 p_vout->p_sys->b_wallpaper = b_on;
1954 msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
1956 if( p_vout->p_sys->b_wallpaper )
1958 p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
1959 p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
1961 ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1962 ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1966 ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
1967 ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
1970 /* Update desktop */
1971 InvalidateRect( hwnd, NULL, TRUE );
1972 UpdateWindow( hwnd );
1975 /*****************************************************************************
1976 * config variable callback
1977 *****************************************************************************/
1978 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1979 LPTSTR psz_drivername, VOID* p_context,
1982 VLC_UNUSED( p_guid ); VLC_UNUSED( psz_desc ); VLC_UNUSED( hmon );
1984 module_config_t *p_item = (module_config_t *)p_context;
1987 (char **)realloc( p_item->ppsz_list,
1988 (p_item->i_list+2) * sizeof(char *) );
1989 p_item->ppsz_list_text =
1990 (char **)realloc( p_item->ppsz_list_text,
1991 (p_item->i_list+2) * sizeof(char *) );
1993 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1994 p_item->ppsz_list_text[p_item->i_list] = NULL;
1996 p_item->ppsz_list[p_item->i_list] = NULL;
1997 p_item->ppsz_list_text[p_item->i_list] = NULL;
1999 return TRUE; /* Keep enumerating */
2002 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2003 vlc_value_t newval, vlc_value_t oldval, void *d)
2005 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( d );
2007 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
2009 HINSTANCE hddraw_dll;
2011 module_config_t *p_item;
2014 p_item = config_FindConfig( p_this, psz_name );
2015 if( !p_item ) return VLC_SUCCESS;
2017 /* Clear-up the current list */
2018 if( p_item->i_list )
2020 /* Keep the first entry */
2021 for( i = 1; i < p_item->i_list; i++ )
2023 free( p_item->ppsz_list[i] );
2024 free( p_item->ppsz_list_text[i] );
2026 /* TODO: Remove when no more needed */
2027 p_item->ppsz_list[i] = NULL;
2028 p_item->ppsz_list_text[i] = NULL;
2032 /* Load direct draw DLL */
2033 hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
2034 if( hddraw_dll == NULL ) return VLC_SUCCESS;
2036 OurDirectDrawEnumerateEx =
2037 (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
2039 if( OurDirectDrawEnumerateEx )
2041 /* Enumerate displays */
2042 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
2043 DDENUM_ATTACHEDSECONDARYDEVICES );
2046 FreeLibrary( hddraw_dll );
2048 /* Signal change to the interface */
2049 p_item->b_dirty = true;
2054 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
2055 vlc_value_t oldval, vlc_value_t newval,
2058 VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval ); VLC_UNUSED( p_data );
2059 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2061 if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
2062 (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
2064 playlist_t *p_playlist = pl_Hold( p_vout );
2068 /* Modify playlist as well because the vout might have to be
2070 var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
2071 var_Set( p_playlist, "directx-wallpaper", newval );
2072 pl_Release( p_vout );
2075 p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
2081 /*****************************************************************************
2082 * SetPalette: sets an 8 bpp palette
2083 *****************************************************************************/
2084 static void SetPalette( vout_thread_t *p_vout,
2085 uint16_t *red, uint16_t *green, uint16_t *blue )
2087 VLC_UNUSED( red ); VLC_UNUSED( green );VLC_UNUSED( blue );
2088 msg_Err( p_vout, "FIXME: SetPalette unimplemented" );