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 */
54 # include <multimon.h>
56 #undef GetSystemMetrics
58 #ifndef MONITOR_DEFAULTTONEAREST
59 # define MONITOR_DEFAULTTONEAREST 2
64 /*****************************************************************************
65 * picture_sys_t: direct buffer method descriptor
66 *****************************************************************************
67 * This structure is part of the picture descriptor, it describes the
68 * DirectX specific properties of a direct buffer.
69 *****************************************************************************/
72 LPDIRECTDRAWSURFACE2 p_surface;
73 LPDIRECTDRAWSURFACE2 p_front_surface;
77 /*****************************************************************************
79 * Defining them here allows us to get rid of the dxguid library during
81 *****************************************************************************/
85 DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
86 DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
88 /*****************************************************************************
90 *****************************************************************************/
91 static int OpenVideo ( vlc_object_t * );
92 static void CloseVideo ( vlc_object_t * );
94 static int Init ( vout_thread_t * );
95 static void End ( vout_thread_t * );
96 static int Manage ( vout_thread_t * );
97 static void Display ( vout_thread_t *, picture_t * );
98 static void FirstDisplay( vout_thread_t *, picture_t * );
99 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
101 static int NewPictureVec ( vout_thread_t *, picture_t * );
102 static void FreePictureVec ( vout_thread_t *, picture_t *, int );
103 static int UpdatePictureStruct( vout_thread_t *, picture_t * );
105 static int DirectXInitDDraw ( vout_thread_t *p_vout );
106 static void DirectXCloseDDraw ( vout_thread_t *p_vout );
107 static int DirectXCreateDisplay ( vout_thread_t *p_vout );
108 static void DirectXCloseDisplay ( vout_thread_t *p_vout );
109 static int DirectXCreateSurface ( vout_thread_t *p_vout,
110 LPDIRECTDRAWSURFACE2 *, int, int, int );
111 static void DirectXCloseSurface ( vout_thread_t *p_vout,
112 LPDIRECTDRAWSURFACE2 );
113 static int DirectXCreateClipper ( vout_thread_t *p_vout );
114 static void DirectXGetDDrawCaps ( vout_thread_t *p_vout );
115 static int DirectXLockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
116 static int DirectXUnlockSurface ( vout_thread_t *p_vout, picture_t *p_pic );
118 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
120 void SwitchWallpaperMode( vout_thread_t *, bool );
122 /* Object variables callbacks */
123 static int FindDevicesCallback( vlc_object_t *, char const *,
124 vlc_value_t, vlc_value_t, void * );
125 static int WallpaperCallback( vlc_object_t *, char const *,
126 vlc_value_t, vlc_value_t, void * );
128 /*****************************************************************************
130 *****************************************************************************/
131 #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
132 #define HW_YUV_LONGTEXT N_( \
133 "Try to use hardware acceleration for YUV->RGB conversions. " \
134 "This option doesn't have any effect when using overlays." )
136 #define SYSMEM_TEXT N_("Use video buffers in system memory")
137 #define SYSMEM_LONGTEXT N_( \
138 "Create video buffers in system memory instead of video memory. This " \
139 "isn't recommended as usually using video memory allows to benefit from " \
140 "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
141 "This option doesn't have any effect when using overlays." )
143 #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
144 #define TRIPLEBUF_LONGTEXT N_( \
145 "Try to use triple buffering when using YUV overlays. That results in " \
146 "much better video quality (no flickering)." )
148 #define DEVICE_TEXT N_("Name of desired display device")
149 #define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " \
150 "specify the Windows device name of the display that you want the video " \
151 "window to open on. For example, \"\\\\.\\DISPLAY1\" or " \
152 "\"\\\\.\\DISPLAY2\"." )
154 #define WALLPAPER_TEXT N_("Enable wallpaper mode ")
155 #define WALLPAPER_LONGTEXT N_( \
156 "The wallpaper mode allows you to display the video as the desktop " \
157 "background. Note that this feature only works in overlay mode and " \
158 "the desktop must not already have a wallpaper." )
160 static const char *const ppsz_dev[] = { "" };
161 static const char *const ppsz_dev_text[] = { N_("Default") };
164 set_shortname( "DirectX" )
165 set_category( CAT_VIDEO )
166 set_subcategory( SUBCAT_VIDEO_VOUT )
167 add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
169 add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
171 add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT,
172 TRIPLEBUF_LONGTEXT, true )
174 add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
176 change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback )
177 change_action_add( FindDevicesCallback, N_("Refresh list") )
179 add_bool( "directx-wallpaper", 0, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
182 set_description( N_("DirectX video output") )
183 set_capability( "video output", 100 )
184 add_shortcut( "directx" )
185 set_callbacks( OpenVideo, CloseVideo )
187 /* FIXME: Hack to avoid unregistering our window class */
188 linked_with_a_crap_library_which_uses_atexit ()
192 /* check if we registered a window class because we need to
195 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
196 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
199 /*****************************************************************************
200 * OpenVideo: allocate DirectX video thread output method
201 *****************************************************************************
202 * This function allocates and initialize the DirectX vout method.
203 *****************************************************************************/
204 static int OpenVideo( vlc_object_t *p_this )
206 vout_thread_t * p_vout = (vout_thread_t *)p_this;
210 /* Allocate structure */
211 p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
212 if( p_vout->p_sys == NULL )
215 /* Initialisations */
216 p_vout->pf_init = Init;
217 p_vout->pf_end = End;
218 p_vout->pf_manage = Manage;
219 p_vout->pf_render = NULL;
220 p_vout->pf_display = FirstDisplay;
222 p_vout->p_sys->p_ddobject = NULL;
223 p_vout->p_sys->p_display = NULL;
224 p_vout->p_sys->p_current_surface = NULL;
225 p_vout->p_sys->p_clipper = NULL;
226 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
227 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
228 p_vout->p_sys->i_changes = 0;
229 p_vout->p_sys->b_wallpaper = 0;
230 vlc_mutex_init( &p_vout->p_sys->lock );
231 SetRectEmpty( &p_vout->p_sys->rect_display );
232 SetRectEmpty( &p_vout->p_sys->rect_parent );
234 /* Multimonitor stuff */
235 p_vout->p_sys->hmonitor = NULL;
236 p_vout->p_sys->p_display_driver = NULL;
237 p_vout->p_sys->MonitorFromWindow = NULL;
238 p_vout->p_sys->GetMonitorInfo = NULL;
239 if( (huser32 = GetModuleHandle( _T("USER32") ) ) )
241 p_vout->p_sys->MonitorFromWindow = (HMONITOR (WINAPI *)( HWND, DWORD ))
242 GetProcAddress( huser32, _T("MonitorFromWindow") );
243 p_vout->p_sys->GetMonitorInfo =
245 GetProcAddress( huser32, "GetMonitorInfoA" );
247 GetProcAddress( huser32, _T("GetMonitorInfoW") );
251 var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
252 var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
253 var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
254 var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
255 var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
256 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
257 var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
259 p_vout->p_sys->b_cursor_hidden = 0;
260 p_vout->p_sys->i_lastmoved = mdate();
261 p_vout->p_sys->i_mouse_hide_timeout =
262 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
264 /* Set main window's size */
265 p_vout->p_sys->i_window_width = p_vout->i_window_width;
266 p_vout->p_sys->i_window_height = p_vout->i_window_height;
268 if ( !CreateEventThread( p_vout ) )
271 /* Initialise DirectDraw */
272 if( DirectXInitDDraw( p_vout ) )
274 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
278 /* Create the directx display */
279 if( DirectXCreateDisplay( p_vout ) )
281 msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
285 /* Variable to indicate if the window should be on top of others */
286 /* Trigger a callback right now */
287 var_TriggerCallback( p_vout, "video-on-top" );
289 /* Variable to indicate if the window should be on top of others */
290 /* Trigger a callback right now */
291 var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
292 val.psz_string = _("Wallpaper");
293 var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
294 var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
295 var_TriggerCallback( p_vout, "directx-wallpaper" );
297 DisableScreensaver ( p_vout );
302 CloseVideo( VLC_OBJECT(p_vout) );
306 /*****************************************************************************
307 * Init: initialize DirectX video thread output method
308 *****************************************************************************
309 * This function create the directx surfaces needed by the output thread.
310 * It is called at the beginning of the thread.
311 *****************************************************************************/
312 static int Init( vout_thread_t *p_vout )
316 /* Get a few default parameters */
317 p_vout->p_sys->b_using_overlay = var_GetBool( p_vout, "overlay" );
318 p_vout->p_sys->b_use_sysmem = var_GetBool( p_vout, "directx-use-sysmem" );
319 p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
320 p_vout->p_sys->b_3buf_overlay = var_GetBool( p_vout, "directx-3buffering" );
322 /* Initialise DirectDraw if not already done.
323 * We do this here because on multi-monitor systems we may have to
324 * re-create the directdraw surfaces. */
325 if( !p_vout->p_sys->p_ddobject &&
326 DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
328 msg_Err( p_vout, "cannot initialize DirectDraw" );
332 /* Create the directx display */
333 if( !p_vout->p_sys->p_display &&
334 DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
336 msg_Err( p_vout, "cannot initialize DirectDraw" );
340 /* Initialize the output structure.
341 * Since DirectDraw can do rescaling for us, stick to the default
342 * coordinates and aspect. */
343 p_vout->output.i_width = p_vout->render.i_width;
344 p_vout->output.i_height = p_vout->render.i_height;
345 p_vout->output.i_aspect = p_vout->render.i_aspect;
346 p_vout->fmt_out = p_vout->fmt_in;
347 UpdateRects( p_vout, true );
349 #define MAX_DIRECTBUFFERS 1
350 /* Right now we use only 1 directbuffer because we don't want the
351 * video decoder to decode directly into direct buffers as they are
352 * created into video memory and video memory is _really_ slow */
354 /* Choose the chroma we will try first. */
355 switch( p_vout->render.i_chroma )
358 p_vout->output.i_chroma = VLC_CODEC_YUYV;
361 p_vout->output.i_chroma = VLC_CODEC_UYVY;
364 p_vout->output.i_chroma = VLC_CODEC_YVYU;
367 p_vout->output.i_chroma = VLC_CODEC_YV12;
371 NewPictureVec( p_vout, p_vout->p_picture );
373 i_chroma_backup = p_vout->output.i_chroma;
375 if( !I_OUTPUTPICTURES )
377 /* hmmm, it didn't work! Let's try commonly supported chromas */
378 if( p_vout->output.i_chroma != VLC_CODEC_I420 )
380 p_vout->output.i_chroma = VLC_CODEC_YV12;
381 NewPictureVec( p_vout, p_vout->p_picture );
383 if( !I_OUTPUTPICTURES )
385 /* hmmm, it still didn't work! Let's try another one */
386 p_vout->output.i_chroma = VLC_CODEC_YUYV;
387 NewPictureVec( p_vout, p_vout->p_picture );
391 if( !I_OUTPUTPICTURES )
393 /* If it still didn't work then don't try to use an overlay */
394 p_vout->output.i_chroma = i_chroma_backup;
395 p_vout->p_sys->b_using_overlay = 0;
396 msg_Warn( p_vout, "Could not initialize directx overlay" ) ;
397 NewPictureVec( p_vout, p_vout->p_picture );
400 /* Change the window title bar text */
401 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
403 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
407 /*****************************************************************************
408 * End: terminate Sys video thread output method
409 *****************************************************************************
410 * Terminate an output method created by Create.
411 * It is called at the end of the thread.
412 *****************************************************************************/
413 static void End( vout_thread_t *p_vout )
415 FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
417 DirectXCloseDisplay( p_vout );
418 DirectXCloseDDraw( p_vout );
423 /*****************************************************************************
424 * CloseVideo: destroy Sys video thread output method
425 *****************************************************************************
426 * Terminate an output method created by Create
427 *****************************************************************************/
428 static void CloseVideo( vlc_object_t *p_this )
430 vout_thread_t * p_vout = (vout_thread_t *)p_this;
432 StopEventThread( p_vout );
434 /* Make sure the wallpaper is restored */
435 var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
436 SwitchWallpaperMode( p_vout, false );
438 RestoreScreensaver( p_vout );
440 free( p_vout->p_sys );
443 /*****************************************************************************
444 * Manage: handle Sys events
445 *****************************************************************************
446 * This function should be called regularly by the video output thread.
447 * It returns a non null value if an error occurred.
448 *****************************************************************************/
449 static int Manage( vout_thread_t *p_vout )
451 /* If we do not control our window, we check for geometry changes
452 * ourselves because the parent might not send us its events. */
453 vlc_mutex_lock( &p_vout->p_sys->lock );
454 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
459 vlc_mutex_unlock( &p_vout->p_sys->lock );
461 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
462 point.x = point.y = 0;
463 ClientToScreen( p_vout->p_sys->hparent, &point );
464 OffsetRect( &rect_parent, point.x, point.y );
466 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
468 p_vout->p_sys->rect_parent = rect_parent;
470 /* This one is to force the update even if only
471 * the position has changed */
472 SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
473 rect_parent.right - rect_parent.left,
474 rect_parent.bottom - rect_parent.top, 0 );
476 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
477 rect_parent.right - rect_parent.left,
478 rect_parent.bottom - rect_parent.top, 0 );
483 vlc_mutex_unlock( &p_vout->p_sys->lock );
489 if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
491 p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
493 /* Check if we are still on the same monitor */
494 if( p_vout->p_sys->MonitorFromWindow &&
495 p_vout->p_sys->hmonitor !=
496 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
497 MONITOR_DEFAULTTONEAREST ) )
499 /* This will force the vout core to recreate the picture buffers */
500 p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
504 /* autoscale toggle */
505 if( p_vout->i_changes & VOUT_SCALE_CHANGE )
507 p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
509 p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
510 p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
512 UpdateRects( p_vout, true );
516 if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
518 p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
520 p_vout->b_autoscale = false;
522 (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
523 UpdateRects( p_vout, true );
526 /* Check for cropping / aspect changes */
527 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
528 p_vout->i_changes & VOUT_ASPECT_CHANGE )
530 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
531 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
533 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
534 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
535 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
536 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
537 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
538 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
539 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
540 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
541 UpdateRects( p_vout, true );
544 /* We used to call the Win32 PeekMessage function here to read the window
545 * messages. But since window can stay blocked into this function for a
546 * long time (for example when you move your window on the screen), I
547 * decided to isolate PeekMessage in another thread. */
549 if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
551 SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
552 p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
553 DirectDrawUpdateOverlay( p_vout );
559 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
560 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
562 Win32ToggleFullscreen( p_vout );
564 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
565 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
571 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
572 (mdate() - p_vout->p_sys->i_lastmoved) >
573 p_vout->p_sys->i_mouse_hide_timeout )
578 /* Hide the cursor only if it is inside our window */
579 GetCursorPos( &point );
580 hwnd = WindowFromPoint(point);
581 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
583 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
587 p_vout->p_sys->i_lastmoved = mdate();
592 * "Always on top" status change
594 if( p_vout->p_sys->b_on_top_change )
597 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
599 var_Get( p_vout, "video-on-top", &val );
601 /* Set the window on top if necessary */
602 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
605 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
606 MF_BYCOMMAND | MFS_CHECKED );
607 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
608 SWP_NOSIZE | SWP_NOMOVE );
611 /* The window shouldn't be on top */
612 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
615 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
616 MF_BYCOMMAND | MFS_UNCHECKED );
617 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
618 SWP_NOSIZE | SWP_NOMOVE );
621 p_vout->p_sys->b_on_top_change = false;
624 /* Check if the event thread is still running */
625 if( !vlc_object_alive (p_vout->p_sys->p_event) )
627 return VLC_EGENERIC; /* exit */
633 /*****************************************************************************
634 * Display: displays previously rendered output
635 *****************************************************************************
636 * This function sends the currently rendered image to the display, wait until
637 * it is displayed and switch the two rendering buffers, preparing next frame.
638 *****************************************************************************/
639 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
643 if( (p_vout->p_sys->p_display == NULL) )
645 msg_Warn( p_vout, "no display!" );
649 /* Our surface can be lost so be sure to check this
650 * and restore it if need be */
651 if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
652 == DDERR_SURFACELOST )
654 if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
655 p_vout->p_sys->b_using_overlay )
656 DirectDrawUpdateOverlay( p_vout );
659 if( !p_vout->p_sys->b_using_overlay )
663 /* We ask for the "NOTEARING" option */
664 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
665 ddbltfx.dwSize = sizeof(DDBLTFX);
666 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
668 /* Blit video surface to display */
669 dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
670 &p_vout->p_sys->rect_dest_clipped,
671 p_pic->p_sys->p_surface,
672 &p_vout->p_sys->rect_src_clipped,
673 DDBLT_ASYNC, &ddbltfx );
674 if( dxresult != DD_OK )
676 msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
680 else /* using overlay */
682 /* Flip the overlay buffers if we are using back buffers */
683 if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
688 dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
690 if( dxresult != DD_OK )
692 msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
695 /* set currently displayed pic */
696 p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
698 /* Lock surface to get all the required info */
699 if( DirectXLockSurface( p_vout, p_pic ) )
702 msg_Warn( p_vout, "cannot lock surface" );
705 DirectXUnlockSurface( p_vout, p_pic );
710 ** this function is only used once when the first picture is received
711 ** this function will show the video window once video is ready for
715 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
717 /* get initial picture rendered on overlay surface */
718 Display(p_vout, p_pic);
720 IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
721 DDWAITVB_BLOCKBEGIN, NULL);
723 if( p_vout->p_sys->b_using_overlay )
725 /* set the colorkey as the backgound brush for the video window */
726 SetClassLongPtr( p_vout->p_sys->hvideownd, GCLP_HBRBACKGROUND,
727 (LONG_PTR)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
730 ** Video window is initially hidden, show it now since we got a
733 SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
741 /* use and restores proper display function for further pictures */
742 p_vout->pf_display = Display;
745 /* following functions are local */
747 /*****************************************************************************
748 * DirectXEnumCallback: Device enumeration
749 *****************************************************************************
750 * This callback function is called by DirectDraw once for each
751 * available DirectDraw device.
752 *****************************************************************************/
753 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
754 LPTSTR psz_drivername, VOID* p_context,
757 vout_thread_t *p_vout = (vout_thread_t *)p_context;
760 msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
764 var_Get( p_vout, "directx-device", &device );
766 if( ( !device.psz_string || !*device.psz_string ) &&
767 hmon == p_vout->p_sys->hmonitor )
769 free( device.psz_string );
771 else if( strcmp( psz_drivername, device.psz_string ) == 0 )
773 MONITORINFO monitor_info;
774 monitor_info.cbSize = sizeof( MONITORINFO );
776 if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
780 /* Move window to the right screen */
781 GetWindowRect( p_vout->p_sys->hwnd, &rect );
782 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
784 rect.left = monitor_info.rcWork.left;
785 rect.top = monitor_info.rcWork.top;
786 msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
787 "position to %ld,%ld", rect.left, rect.top );
788 SetWindowPos( p_vout->p_sys->hwnd, NULL,
789 rect.left, rect.top, 0, 0,
790 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
794 p_vout->p_sys->hmonitor = hmon;
795 free( device.psz_string );
799 free( device.psz_string );
800 return TRUE; /* Keep enumerating */
803 msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
804 p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
805 if( p_vout->p_sys->p_display_driver )
806 memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
809 return TRUE; /* Keep enumerating */
812 /*****************************************************************************
813 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
814 *****************************************************************************
815 * This function initialise and allocate resources for DirectDraw.
816 *****************************************************************************/
817 static int DirectXInitDDraw( vout_thread_t *p_vout )
820 HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
821 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
823 LPDIRECTDRAW p_ddobject;
825 msg_Dbg( p_vout, "DirectXInitDDraw" );
827 /* Load direct draw DLL */
828 p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
829 if( p_vout->p_sys->hddraw_dll == NULL )
831 msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
835 OurDirectDrawCreate =
836 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
837 _T("DirectDrawCreate") );
838 if( OurDirectDrawCreate == NULL )
840 msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
844 OurDirectDrawEnumerateEx =
845 (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
847 "DirectDrawEnumerateExA" );
849 _T("DirectDrawEnumerateExW") );
852 if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
856 var_Get( p_vout, "directx-device", &device );
857 if( device.psz_string )
859 msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
860 free( device.psz_string );
863 p_vout->p_sys->hmonitor =
864 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
865 MONITOR_DEFAULTTONEAREST );
867 /* Enumerate displays */
868 OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
869 DDENUM_ATTACHEDSECONDARYDEVICES );
872 /* Initialize DirectDraw now */
873 dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
875 if( dxresult != DD_OK )
877 msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
881 /* Get the IDirectDraw2 interface */
882 dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
883 (LPVOID *)&p_vout->p_sys->p_ddobject );
884 /* Release the unused interface */
885 IDirectDraw_Release( p_ddobject );
886 if( dxresult != DD_OK )
888 msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
892 /* Set DirectDraw Cooperative level, ie what control we want over Windows
894 dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
895 NULL, DDSCL_NORMAL );
896 if( dxresult != DD_OK )
898 msg_Err( p_vout, "cannot set direct draw cooperative level" );
902 /* Get the size of the current display device */
903 if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
905 MONITORINFO monitor_info;
906 monitor_info.cbSize = sizeof( MONITORINFO );
907 p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
909 p_vout->p_sys->rect_display = monitor_info.rcMonitor;
913 p_vout->p_sys->rect_display.left = 0;
914 p_vout->p_sys->rect_display.top = 0;
915 p_vout->p_sys->rect_display.right = GetSystemMetrics(SM_CXSCREEN);
916 p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
919 msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
920 p_vout->p_sys->rect_display.left,
921 p_vout->p_sys->rect_display.top,
922 p_vout->p_sys->rect_display.right,
923 p_vout->p_sys->rect_display.bottom );
925 /* Probe the capabilities of the hardware */
926 DirectXGetDDrawCaps( p_vout );
928 msg_Dbg( p_vout, "End DirectXInitDDraw" );
932 if( p_vout->p_sys->p_ddobject )
933 IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
934 if( p_vout->p_sys->hddraw_dll )
935 FreeLibrary( p_vout->p_sys->hddraw_dll );
936 p_vout->p_sys->hddraw_dll = NULL;
937 p_vout->p_sys->p_ddobject = NULL;
941 /*****************************************************************************
942 * DirectXCreateDisplay: create the DirectDraw display.
943 *****************************************************************************
944 * Create and initialize display according to preferences specified in the vout
946 *****************************************************************************/
947 static int DirectXCreateDisplay( vout_thread_t *p_vout )
951 LPDIRECTDRAWSURFACE p_display;
953 msg_Dbg( p_vout, "DirectXCreateDisplay" );
955 /* Now get the primary surface. This surface is what you actually see
957 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
958 ddsd.dwSize = sizeof(DDSURFACEDESC);
959 ddsd.dwFlags = DDSD_CAPS;
960 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
962 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
963 &ddsd, &p_display, NULL );
964 if( dxresult != DD_OK )
966 msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
970 dxresult = IDirectDrawSurface_QueryInterface( p_display,
971 &IID_IDirectDrawSurface2,
972 (LPVOID *)&p_vout->p_sys->p_display );
973 /* Release the old interface */
974 IDirectDrawSurface_Release( p_display );
975 if ( dxresult != DD_OK )
977 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
978 "(error %li)", dxresult );
982 /* The clipper will be used only in non-overlay mode */
983 DirectXCreateClipper( p_vout );
985 /* Make sure the colorkey will be painted */
986 p_vout->p_sys->i_colorkey = 1;
987 p_vout->p_sys->i_rgb_colorkey =
988 DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
990 UpdateRects( p_vout, true );
995 /*****************************************************************************
996 * DirectXCreateClipper: Create a clipper that will be used when blitting the
997 * RGB surface to the main display.
998 *****************************************************************************
999 * This clipper prevents us to modify by mistake anything on the screen
1000 * which doesn't belong to our window. For example when a part of our video
1001 * window is hidden by another window.
1002 *****************************************************************************/
1003 static int DirectXCreateClipper( vout_thread_t *p_vout )
1007 msg_Dbg( p_vout, "DirectXCreateClipper" );
1009 /* Create the clipper */
1010 dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
1011 &p_vout->p_sys->p_clipper, NULL );
1012 if( dxresult != DD_OK )
1014 msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
1018 /* Associate the clipper to the window */
1019 dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
1020 p_vout->p_sys->hvideownd );
1021 if( dxresult != DD_OK )
1023 msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
1028 /* associate the clipper with the surface */
1029 dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
1030 p_vout->p_sys->p_clipper);
1031 if( dxresult != DD_OK )
1033 msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
1041 if( p_vout->p_sys->p_clipper )
1043 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1045 p_vout->p_sys->p_clipper = NULL;
1046 return VLC_EGENERIC;
1049 /*****************************************************************************
1050 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
1051 *****************************************************************************
1052 * The best method of display is with an YUV overlay because the YUV->RGB
1053 * conversion is done in hardware.
1054 * You can also create a plain RGB surface.
1055 * ( Maybe we could also try an RGB overlay surface, which could have hardware
1056 * scaling and which would also be faster in window mode because you don't
1057 * need to do any blitting to the main display...)
1058 *****************************************************************************/
1059 static int DirectXCreateSurface( vout_thread_t *p_vout,
1060 LPDIRECTDRAWSURFACE2 *pp_surface_final,
1061 int i_chroma, int b_overlay,
1065 LPDIRECTDRAWSURFACE p_surface;
1068 /* Create the video surface */
1071 /* Now try to create the YUV overlay surface.
1072 * This overlay will be displayed on top of the primary surface.
1073 * A color key is used to determine whether or not the overlay will be
1074 * displayed, ie the overlay will be displayed in place of the primary
1075 * surface wherever the primary surface will have this color.
1076 * The video window has been created with a background of this color so
1077 * the overlay will be only displayed on top of this window */
1079 memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
1080 ddsd.dwSize = sizeof(DDSURFACEDESC);
1081 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1082 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1083 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1084 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
1085 ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
1086 ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
1087 ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
1089 ddsd.dwHeight = p_vout->render.i_height;
1090 ddsd.dwWidth = p_vout->render.i_width;
1091 ddsd.dwBackBufferCount = i_backbuffers;
1093 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1094 &ddsd, &p_surface, NULL );
1095 if( dxresult != DD_OK )
1097 *pp_surface_final = NULL;
1098 return VLC_EGENERIC;
1104 bool b_rgb_surface =
1105 ( i_chroma == VLC_CODEC_RGB8 )
1106 || ( i_chroma == VLC_CODEC_RGB15 )
1107 || ( i_chroma == VLC_CODEC_RGB16 )
1108 || ( i_chroma == VLC_CODEC_RGB24 )
1109 || ( i_chroma == VLC_CODEC_RGB32 );
1111 memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
1112 ddsd.dwSize = sizeof(DDSURFACEDESC);
1113 ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1114 ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
1115 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1116 ddsd.dwHeight = p_vout->render.i_height;
1117 ddsd.dwWidth = p_vout->render.i_width;
1119 if( p_vout->p_sys->b_use_sysmem )
1120 ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
1122 ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
1124 if( !b_rgb_surface )
1126 ddsd.dwFlags |= DDSD_PIXELFORMAT;
1127 ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1128 ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1131 dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1132 &ddsd, &p_surface, NULL );
1133 if( dxresult != DD_OK )
1135 *pp_surface_final = NULL;
1136 return VLC_EGENERIC;
1140 /* Now that the surface is created, try to get a newer DirectX interface */
1141 dxresult = IDirectDrawSurface_QueryInterface( p_surface,
1142 &IID_IDirectDrawSurface2,
1143 (LPVOID *)pp_surface_final );
1144 IDirectDrawSurface_Release( p_surface ); /* Release the old interface */
1145 if ( dxresult != DD_OK )
1147 msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
1148 "(error %li)", dxresult );
1149 *pp_surface_final = NULL;
1150 return VLC_EGENERIC;
1155 /* Check the overlay is useable as some graphics cards allow creating
1156 * several overlays but only one can be used at one time. */
1157 p_vout->p_sys->p_current_surface = *pp_surface_final;
1158 if( DirectDrawUpdateOverlay( p_vout ) != VLC_SUCCESS )
1160 IDirectDrawSurface2_Release( *pp_surface_final );
1161 *pp_surface_final = NULL;
1162 msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1163 return VLC_EGENERIC;
1170 /*****************************************************************************
1171 * DirectDrawUpdateOverlay: Move or resize overlay surface on video display.
1172 *****************************************************************************
1173 * This function is used to move or resize an overlay surface on the screen.
1174 * Ususally the overlay is moved by the user and thus, by a move or resize
1175 * event (in Manage).
1176 *****************************************************************************/
1177 int DirectDrawUpdateOverlay( vout_thread_t *p_vout )
1182 RECT rect_src = p_vout->p_sys->rect_src_clipped;
1183 RECT rect_dest = p_vout->p_sys->rect_dest_clipped;
1185 if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1187 if( p_vout->p_sys->b_wallpaper )
1189 int i_x, i_y, i_width, i_height;
1191 rect_src.left = p_vout->fmt_out.i_x_offset;
1192 rect_src.top = p_vout->fmt_out.i_y_offset;
1193 rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
1194 rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
1196 rect_dest = p_vout->p_sys->rect_display;
1197 vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1198 &i_x, &i_y, &i_width, &i_height );
1200 rect_dest.left += i_x;
1201 rect_dest.right = rect_dest.left + i_width;
1202 rect_dest.top += i_y;
1203 rect_dest.bottom = rect_dest.top + i_height;
1206 vlc_mutex_lock( &p_vout->p_sys->lock );
1207 if( p_vout->p_sys->p_current_surface == NULL )
1209 vlc_mutex_unlock( &p_vout->p_sys->lock );
1210 return VLC_EGENERIC;
1213 /* The new window dimensions should already have been computed by the
1214 * caller of this function */
1216 /* Position and show the overlay */
1217 memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1218 ddofx.dwSize = sizeof(DDOVERLAYFX);
1219 ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1220 ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1222 dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1224 dxresult = IDirectDrawSurface2_UpdateOverlay(
1225 p_vout->p_sys->p_current_surface,
1226 &rect_src, p_vout->p_sys->p_display, &rect_dest,
1229 vlc_mutex_unlock( &p_vout->p_sys->lock );
1231 if(dxresult != DD_OK)
1233 msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
1234 return VLC_EGENERIC;
1240 /*****************************************************************************
1241 * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1242 *****************************************************************************
1243 * This function returns all resources allocated by DirectXInitDDraw.
1244 *****************************************************************************/
1245 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1247 msg_Dbg( p_vout, "DirectXCloseDDraw" );
1248 if( p_vout->p_sys->p_ddobject != NULL )
1250 IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1251 p_vout->p_sys->p_ddobject = NULL;
1254 if( p_vout->p_sys->hddraw_dll != NULL )
1256 FreeLibrary( p_vout->p_sys->hddraw_dll );
1257 p_vout->p_sys->hddraw_dll = NULL;
1260 free( p_vout->p_sys->p_display_driver );
1261 p_vout->p_sys->p_display_driver = NULL;
1263 p_vout->p_sys->hmonitor = NULL;
1266 /*****************************************************************************
1267 * DirectXCloseDisplay: close and reset the DirectX display device
1268 *****************************************************************************
1269 * This function returns all resources allocated by DirectXCreateDisplay.
1270 *****************************************************************************/
1271 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1273 msg_Dbg( p_vout, "DirectXCloseDisplay" );
1275 if( p_vout->p_sys->p_clipper != NULL )
1277 msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1278 IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1279 p_vout->p_sys->p_clipper = NULL;
1282 if( p_vout->p_sys->p_display != NULL )
1284 msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1285 IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1286 p_vout->p_sys->p_display = NULL;
1290 /*****************************************************************************
1291 * DirectXCloseSurface: close the YUV overlay or RGB surface.
1292 *****************************************************************************
1293 * This function returns all resources allocated for the surface.
1294 *****************************************************************************/
1295 static void DirectXCloseSurface( vout_thread_t *p_vout,
1296 LPDIRECTDRAWSURFACE2 p_surface )
1298 msg_Dbg( p_vout, "DirectXCloseSurface" );
1299 if( p_surface != NULL )
1301 IDirectDrawSurface2_Release( p_surface );
1305 /*****************************************************************************
1306 * NewPictureVec: allocate a picture
1307 * FIXME? make it work for i_num_pic pictures...
1308 *****************************************************************************
1309 * Returns 0 on success, -1 otherwise
1310 *****************************************************************************/
1311 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic )
1314 int i_ret = VLC_SUCCESS;
1315 LPDIRECTDRAWSURFACE2 p_surface;
1317 msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1318 p_vout->p_sys->b_using_overlay ? "yes" : "no",
1319 (char *)&p_vout->output.i_chroma );
1321 I_OUTPUTPICTURES = 0;
1323 /* First we try to use an YUV overlay surface.
1324 * The overlay surface that we create won't be used to decode directly
1325 * into it because accessing video memory directly is way to slow (remember
1326 * that pictures are decoded macroblock per macroblock). Instead the video
1327 * will be decoded in picture buffers in system memory which will then be
1328 * memcpy() to the overlay surface. */
1329 if( p_vout->p_sys->b_using_overlay )
1331 /* Triple buffering rocks! it doesn't have any processing overhead
1332 * (you don't have to wait for the vsync) and provides for a very nice
1333 * video quality (no tearing). */
1334 if( p_vout->p_sys->b_3buf_overlay )
1335 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1336 p_vout->output.i_chroma,
1337 p_vout->p_sys->b_using_overlay,
1338 2 /* number of backbuffers */ );
1340 if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1342 /* Try to reduce the number of backbuffers */
1343 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1344 p_vout->output.i_chroma,
1345 p_vout->p_sys->b_using_overlay,
1346 0 /* number of backbuffers */ );
1349 if( i_ret == VLC_SUCCESS )
1352 picture_t front_pic;
1353 picture_sys_t front_pic_sys;
1354 front_pic.p_sys = &front_pic_sys;
1356 /* Allocate internal structure */
1357 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1358 if( p_pic[0].p_sys == NULL )
1360 DirectXCloseSurface( p_vout, p_surface );
1364 /* set front buffer */
1365 p_pic[0].p_sys->p_front_surface = p_surface;
1367 /* Get the back buffer */
1368 memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1369 dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1370 if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1371 p_surface, &dds_caps,
1372 &p_pic[0].p_sys->p_surface ) )
1374 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1375 /* front buffer is the same as back buffer */
1376 p_pic[0].p_sys->p_surface = p_surface;
1380 p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1381 p_pic[0].p_sys->p_front_surface;
1383 /* Reset the front buffer memory */
1384 if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1387 for( i = 0; i < front_pic.i_planes; i++ )
1388 for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1389 memset( front_pic.p[i].p_pixels + j *
1390 front_pic.p[i].i_pitch, 127,
1391 front_pic.p[i].i_visible_pitch );
1393 DirectXUnlockSurface( p_vout, &front_pic );
1396 DirectDrawUpdateOverlay( p_vout );
1397 I_OUTPUTPICTURES = 1;
1398 msg_Dbg( p_vout, "YUV overlay created successfully" );
1402 /* As we can't have an overlay, we'll try to create a plain offscreen
1403 * surface. This surface will reside in video memory because there's a
1404 * better chance then that we'll be able to use some kind of hardware
1405 * acceleration like rescaling, blitting or YUV->RGB conversions.
1406 * We then only need to blit this surface onto the main display when we
1407 * want to display it */
1408 if( !p_vout->p_sys->b_using_overlay )
1410 if( p_vout->p_sys->b_hw_yuv )
1414 bool b_result = false;
1416 /* Check if the chroma is supported first. This is required
1417 * because a few buggy drivers don't mind creating the surface
1418 * even if they don't know about the chroma. */
1419 if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1420 &i_codes, NULL ) == DD_OK )
1422 pi_codes = malloc( i_codes * sizeof(DWORD) );
1423 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1424 p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1426 for( i = 0; i < (int)i_codes; i++ )
1428 if( p_vout->output.i_chroma == pi_codes[i] )
1438 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1439 p_vout->output.i_chroma,
1441 0 /* no back buffers */ );
1443 p_vout->p_sys->b_hw_yuv = false;
1446 if( i_ret || !p_vout->p_sys->b_hw_yuv )
1448 /* Our last choice is to use a plain RGB surface */
1449 DDPIXELFORMAT ddpfPixelFormat;
1451 ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1452 IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1455 if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1457 switch( ddpfPixelFormat.dwRGBBitCount )
1460 p_vout->output.i_chroma = VLC_CODEC_RGB8;
1461 p_vout->output.pf_setpalette = SetPalette;
1464 p_vout->output.i_chroma = VLC_CODEC_RGB15;
1467 p_vout->output.i_chroma = VLC_CODEC_RGB16;
1470 p_vout->output.i_chroma = VLC_CODEC_RGB24;
1473 p_vout->output.i_chroma = VLC_CODEC_RGB32;
1476 msg_Err( p_vout, "unknown screen depth" );
1477 return VLC_EGENERIC;
1479 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1480 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1481 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1484 p_vout->p_sys->b_hw_yuv = 0;
1486 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1487 p_vout->output.i_chroma,
1489 0 /* no back buffers */ );
1491 if( i_ret && !p_vout->p_sys->b_use_sysmem )
1493 /* Give it a last try with b_use_sysmem enabled */
1494 p_vout->p_sys->b_use_sysmem = 1;
1496 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1497 p_vout->output.i_chroma,
1499 0 /* no back buffers */ );
1503 if( i_ret == VLC_SUCCESS )
1505 /* Allocate internal structure */
1506 p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1507 if( p_pic[0].p_sys == NULL )
1509 DirectXCloseSurface( p_vout, p_surface );
1513 p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1516 I_OUTPUTPICTURES = 1;
1518 msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1519 (char *)&p_vout->output.i_chroma );
1524 /* Now that we've got all our direct-buffers, we can finish filling in the
1525 * picture_t structures */
1526 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1528 p_pic[i].i_status = DESTROYED_PICTURE;
1529 p_pic[i].i_type = DIRECT_PICTURE;
1530 p_pic[i].b_slow = true;
1531 p_pic[i].pf_lock = DirectXLockSurface;
1532 p_pic[i].pf_unlock = DirectXUnlockSurface;
1533 PP_OUTPUTPICTURE[i] = &p_pic[i];
1535 if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1538 FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1539 I_OUTPUTPICTURES = 0;
1540 msg_Err( p_vout, "cannot lock surface" );
1541 return VLC_EGENERIC;
1543 DirectXUnlockSurface( p_vout, &p_pic[i] );
1546 msg_Dbg( p_vout, "End NewPictureVec (%s)",
1547 I_OUTPUTPICTURES ? "succeeded" : "failed" );
1552 /*****************************************************************************
1553 * FreePicture: destroy a picture vector allocated with NewPictureVec
1554 *****************************************************************************
1556 *****************************************************************************/
1557 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1562 vlc_mutex_lock( &p_vout->p_sys->lock );
1563 p_vout->p_sys->p_current_surface = 0;
1564 vlc_mutex_unlock( &p_vout->p_sys->lock );
1566 for( i = 0; i < i_num_pics; i++ )
1568 DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1570 for( i = 0; i < i_num_pics; i++ )
1572 free( p_pic[i].p_sys );
1577 /*****************************************************************************
1578 * UpdatePictureStruct: updates the internal data in the picture_t structure
1579 *****************************************************************************
1580 * This will setup stuff for use by the video_output thread
1581 *****************************************************************************/
1582 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic )
1584 switch( p_vout->output.i_chroma )
1586 case VLC_CODEC_RGB8:
1587 case VLC_CODEC_RGB15:
1588 case VLC_CODEC_RGB16:
1589 case VLC_CODEC_RGB24:
1590 case VLC_CODEC_RGB32:
1591 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1592 p_pic->p->i_lines = p_vout->output.i_height;
1593 p_pic->p->i_visible_lines = p_vout->output.i_height;
1594 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1595 switch( p_vout->output.i_chroma )
1597 case VLC_CODEC_RGB8:
1598 p_pic->p->i_pixel_pitch = 1;
1600 case VLC_CODEC_RGB15:
1601 case VLC_CODEC_RGB16:
1602 p_pic->p->i_pixel_pitch = 2;
1604 case VLC_CODEC_RGB24:
1605 p_pic->p->i_pixel_pitch = 3;
1607 case VLC_CODEC_RGB32:
1608 p_pic->p->i_pixel_pitch = 4;
1611 return VLC_EGENERIC;
1613 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1614 p_pic->p->i_pixel_pitch;
1615 p_pic->i_planes = 1;
1618 case VLC_CODEC_YV12:
1620 /* U and V inverted compared to I420
1621 * Fixme: this should be handled by the vout core */
1622 p_vout->output.i_chroma = VLC_CODEC_I420;
1624 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1625 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1626 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1627 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1628 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1629 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1630 p_pic->p[Y_PLANE].i_pixel_pitch;
1632 p_pic->V_PIXELS = p_pic->Y_PIXELS
1633 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1634 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1635 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1636 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1637 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1638 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1639 p_pic->p[V_PLANE].i_pixel_pitch;
1641 p_pic->U_PIXELS = p_pic->V_PIXELS
1642 + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1643 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1644 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1645 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1646 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1647 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1648 p_pic->p[U_PLANE].i_pixel_pitch;
1650 p_pic->i_planes = 3;
1653 case VLC_CODEC_I420:
1655 p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1656 p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1657 p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1658 p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1659 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1660 p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1661 p_pic->p[Y_PLANE].i_pixel_pitch;
1663 p_pic->U_PIXELS = p_pic->Y_PIXELS
1664 + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1665 p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1666 p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1667 p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1668 p_pic->p[U_PLANE].i_pixel_pitch = 1;
1669 p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1670 p_pic->p[U_PLANE].i_pixel_pitch;
1672 p_pic->V_PIXELS = p_pic->U_PIXELS
1673 + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1674 p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1675 p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1676 p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1677 p_pic->p[V_PLANE].i_pixel_pitch = 1;
1678 p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1679 p_pic->p[V_PLANE].i_pixel_pitch;
1681 p_pic->i_planes = 3;
1684 case VLC_CODEC_UYVY:
1685 case VLC_CODEC_YUYV:
1687 p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1688 p_pic->p->i_lines = p_vout->output.i_height;
1689 p_pic->p->i_visible_lines = p_vout->output.i_height;
1690 p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1691 p_pic->p->i_pixel_pitch = 2;
1692 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1693 p_pic->p->i_pixel_pitch;
1695 p_pic->i_planes = 1;
1699 /* Unknown chroma, tell the guy to get lost */
1700 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1701 p_vout->output.i_chroma,
1702 (char*)&p_vout->output.i_chroma );
1703 return VLC_EGENERIC;
1709 /*****************************************************************************
1710 * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1711 *****************************************************************************
1712 * It is nice to know which features are supported by the hardware so we can
1713 * find ways to optimize our rendering.
1714 *****************************************************************************/
1715 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1720 /* This is just an indication of whether or not we'll support overlay,
1721 * but with this test we don't know if we support YUV overlay */
1722 memset( &ddcaps, 0, sizeof( DDCAPS ));
1723 ddcaps.dwSize = sizeof(DDCAPS);
1724 dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1726 if(dxresult != DD_OK )
1728 msg_Warn( p_vout, "cannot get caps" );
1732 bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1733 bHasColorKey, bCanStretch, bCanBltFourcc,
1734 bAlignBoundarySrc, bAlignBoundaryDest,
1735 bAlignSizeSrc, bAlignSizeDest;
1737 /* Determine if the hardware supports overlay surfaces */
1738 bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1739 /* Determine if the hardware supports overlay surfaces */
1740 bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1741 /* Determine if the hardware supports overlay deinterlacing */
1742 bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1743 /* Determine if the hardware supports colorkeying */
1744 bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1745 /* Determine if the hardware supports scaling of the overlay surface */
1746 bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1747 /* Determine if the hardware supports color conversion during a blit */
1748 bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1749 /* Determine overlay source boundary alignment */
1750 bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1751 /* Determine overlay destination boundary alignment */
1752 bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1753 /* Determine overlay destination size alignment */
1754 bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1755 /* Determine overlay destination size alignment */
1756 bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1758 msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1759 "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1761 bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1762 bHasColorKey, bCanStretch, bCanBltFourcc );
1764 if( bAlignBoundarySrc || bAlignBoundaryDest ||
1765 bAlignSizeSrc || bAlignSizeDest )
1767 if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1768 ddcaps.dwAlignBoundarySrc;
1769 if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1770 ddcaps.dwAlignBoundaryDest;
1771 if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1772 ddcaps.dwAlignSizeSrc;
1773 if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1774 ddcaps.dwAlignSizeDest;
1776 msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1777 "align_boundary_dest=%i,%i "
1778 "align_size_src=%i,%i align_size_dest=%i,%i",
1779 bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1780 bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1781 bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1782 bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1785 /* Don't ask for troubles */
1786 if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1790 /*****************************************************************************
1791 * DirectXLockSurface: Lock surface and get picture data pointer
1792 *****************************************************************************
1793 * This function locks a surface and get the surface descriptor which amongst
1794 * other things has the pointer to the picture data.
1795 *****************************************************************************/
1796 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1800 /* Lock the surface to get a valid pointer to the picture buffer */
1801 memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1802 p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1803 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1804 NULL, &p_pic->p_sys->ddsd,
1805 DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1807 if( dxresult != DD_OK )
1809 if( dxresult == DDERR_INVALIDPARAMS )
1811 /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1812 * in an invalid params error */
1813 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1814 &p_pic->p_sys->ddsd,
1817 if( dxresult == DDERR_SURFACELOST )
1819 /* Your surface can be lost so be sure
1820 * to check this and restore it if needed */
1822 /* When using overlays with back-buffers, we need to restore
1823 * the front buffer so the back-buffers get restored as well. */
1824 if( p_vout->p_sys->b_using_overlay )
1825 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1827 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1829 dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1830 &p_pic->p_sys->ddsd,
1833 if( dxresult == DDERR_SURFACELOST )
1834 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1837 if( dxresult != DD_OK )
1839 return VLC_EGENERIC;
1843 /* Now we have a pointer to the surface memory, we can update our picture
1845 if( UpdatePictureStruct( p_vout, p_pic )
1848 DirectXUnlockSurface( p_vout, p_pic );
1849 return VLC_EGENERIC;
1855 /*****************************************************************************
1856 * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1857 *****************************************************************************/
1858 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1860 VLC_UNUSED( p_vout );
1862 /* Unlock the Surface */
1863 if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1866 return VLC_EGENERIC;
1869 /*****************************************************************************
1870 * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1871 *****************************************************************************/
1872 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
1877 uint32_t i_pixel_backup;
1880 ddsd.dwSize = sizeof(ddsd);
1881 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1882 &ddsd, DDLOCK_WAIT, NULL );
1883 if( dxresult != DD_OK ) return 0;
1885 i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1887 switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1890 *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1893 *(uint8_t *)ddsd.lpSurface = *pi_color;
1897 *(uint16_t *)ddsd.lpSurface = *pi_color;
1900 /* Seems to be problematic so we'll just put black as the colorkey */
1903 *(uint32_t *)ddsd.lpSurface = *pi_color;
1907 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1909 if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1911 i_rgb = GetPixel( hdc, 0, 0 );
1912 IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1915 ddsd.dwSize = sizeof(ddsd);
1916 dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1917 &ddsd, DDLOCK_WAIT, NULL );
1918 if( dxresult != DD_OK ) return i_rgb;
1920 *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1922 IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1927 /*****************************************************************************
1928 * A few toolbox functions
1929 *****************************************************************************/
1930 void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
1934 if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
1936 hwnd = FindWindow( _T("Progman"), NULL );
1937 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
1938 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
1941 msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
1942 "wallpaper mode not supported" );
1946 p_vout->p_sys->b_wallpaper = b_on;
1948 msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
1950 if( p_vout->p_sys->b_wallpaper )
1952 p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
1953 p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
1955 ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1956 ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1960 ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
1961 ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
1964 /* Update desktop */
1965 InvalidateRect( hwnd, NULL, TRUE );
1966 UpdateWindow( hwnd );
1969 /*****************************************************************************
1970 * config variable callback
1971 *****************************************************************************/
1972 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1973 LPTSTR psz_drivername, VOID* p_context,
1976 VLC_UNUSED( p_guid ); VLC_UNUSED( psz_desc ); VLC_UNUSED( hmon );
1978 module_config_t *p_item = (module_config_t *)p_context;
1981 (char **)realloc( p_item->ppsz_list,
1982 (p_item->i_list+2) * sizeof(char *) );
1983 p_item->ppsz_list_text =
1984 (char **)realloc( p_item->ppsz_list_text,
1985 (p_item->i_list+2) * sizeof(char *) );
1987 p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1988 p_item->ppsz_list_text[p_item->i_list] = NULL;
1990 p_item->ppsz_list[p_item->i_list] = NULL;
1991 p_item->ppsz_list_text[p_item->i_list] = NULL;
1993 return TRUE; /* Keep enumerating */
1996 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1997 vlc_value_t newval, vlc_value_t oldval, void *d)
1999 VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( d );
2001 HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
2003 HINSTANCE hddraw_dll;
2005 module_config_t *p_item;
2008 p_item = config_FindConfig( p_this, psz_name );
2009 if( !p_item ) return VLC_SUCCESS;
2011 /* Clear-up the current list */
2012 if( p_item->i_list )
2014 /* Keep the first entry */
2015 for( i = 1; i < p_item->i_list; i++ )
2017 free( p_item->ppsz_list[i] );
2018 free( p_item->ppsz_list_text[i] );
2020 /* TODO: Remove when no more needed */
2021 p_item->ppsz_list[i] = NULL;
2022 p_item->ppsz_list_text[i] = NULL;
2026 /* Load direct draw DLL */
2027 hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
2028 if( hddraw_dll == NULL ) return VLC_SUCCESS;
2030 OurDirectDrawEnumerateEx =
2032 (void *)GetProcAddress( hddraw_dll, "DirectDrawEnumerateExA" );
2034 (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
2037 if( OurDirectDrawEnumerateEx )
2039 /* Enumerate displays */
2040 OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
2041 DDENUM_ATTACHEDSECONDARYDEVICES );
2044 FreeLibrary( hddraw_dll );
2046 /* Signal change to the interface */
2047 p_item->b_dirty = true;
2052 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
2053 vlc_value_t oldval, vlc_value_t newval,
2056 VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval ); VLC_UNUSED( p_data );
2057 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2059 if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
2060 (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
2062 playlist_t *p_playlist = pl_Hold( p_vout );
2066 /* Modify playlist as well because the vout might have to be
2068 var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
2069 var_Set( p_playlist, "directx-wallpaper", newval );
2070 pl_Release( p_vout );
2073 p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
2079 /*****************************************************************************
2080 * SetPalette: sets an 8 bpp palette
2081 *****************************************************************************/
2082 static void SetPalette( vout_thread_t *p_vout,
2083 uint16_t *red, uint16_t *green, uint16_t *blue )
2085 VLC_UNUSED( red ); VLC_UNUSED( green );VLC_UNUSED( blue );
2086 msg_Err( p_vout, "FIXME: SetPalette unimplemented" );