]> git.sesse.net Git - vlc/blob - modules/video_output/msw/directx.c
10b401dbdf017baf1103d03fe86f2057258baf86
[vlc] / modules / video_output / msw / directx.c
1 /*****************************************************************************
2  * directx.c: Windows DirectDraw video output
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble:
26  *
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.
30  *
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.
35  *
36  *****************************************************************************/
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #include <assert.h>
42
43 #include <vlc_common.h>
44 #include <vlc_plugin.h>
45 #include <vlc_vout.h>
46 #include <vlc_playlist.h>   /* needed for wallpaper */
47 #include <vlc_memory.h>
48
49 #include <ddraw.h>
50 #include <commctrl.h>       /* ListView_(Get|Set)* */
51
52 #ifndef UNDER_CE
53 #   include <multimon.h>
54 #endif
55 #undef GetSystemMetrics
56
57 #ifndef MONITOR_DEFAULTTONEAREST
58 #   define MONITOR_DEFAULTTONEAREST 2
59 #endif
60
61 #include "vout.h"
62
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  *****************************************************************************/
69 struct picture_sys_t
70 {
71     LPDIRECTDRAWSURFACE2 p_surface;
72     LPDIRECTDRAWSURFACE2 p_front_surface;
73     DDSURFACEDESC        ddsd;
74 };
75
76 /*****************************************************************************
77  * DirectDraw GUIDs.
78  * Defining them here allows us to get rid of the dxguid library during
79  * the linking stage.
80  *****************************************************************************/
81 #include <initguid.h>
82 #undef GUID_EXT
83 #define GUID_EXT
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 );
86
87 /*****************************************************************************
88  * Local prototypes.
89  *****************************************************************************/
90 static int  OpenVideo  ( vlc_object_t * );
91 static void CloseVideo ( vlc_object_t * );
92
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 * );
99
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 * );
103
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 );
116
117 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
118
119 void SwitchWallpaperMode( vout_thread_t *, bool );
120
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 * );
126
127 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
128                                  LPTSTR psz_drivername, VOID* p_context,
129                                  HMONITOR hmon );
130                                  
131 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
132                                   LPTSTR psz_drivername, VOID* p_context,
133                                   HMONITOR hmon );
134                                   
135 /*****************************************************************************
136  * Module descriptor
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." )
142
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." )
149
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)." )
154
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\"." )
160
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." )
166
167 static const char *const ppsz_dev[] = { "" };
168 static const char *const ppsz_dev_text[] = { N_("Default") };
169
170 vlc_module_begin ()
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,
175               true )
176     add_bool( "directx-use-sysmem", false, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
177               true )
178     add_bool( "directx-3buffering", true, NULL, TRIPLEBUF_TEXT,
179               TRIPLEBUF_LONGTEXT, true )
180
181     add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
182                 true )
183         change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback )
184         change_action_add( FindDevicesCallback, N_("Refresh list") )
185
186     add_bool( "directx-wallpaper", false, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
187               true )
188
189     set_description( N_("DirectX (DirectDraw) video output") )
190     set_capability( "video output", 100 )
191     add_shortcut( "directx" )
192     set_callbacks( OpenVideo, CloseVideo )
193
194     /* FIXME: Hack to avoid unregistering our window class */
195     linked_with_a_crap_library_which_uses_atexit ()
196 vlc_module_end ()
197
198 #if 0 /* FIXME */
199     /* check if we registered a window class because we need to
200      * unregister it */
201     WNDCLASS wndclass;
202     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
203         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
204 #endif
205
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 )
212 {
213     vout_thread_t * p_vout = (vout_thread_t *)p_this;
214     vlc_value_t val;
215     HMODULE huser32;
216
217     /* Allocate structure */
218     p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
219     if( p_vout->p_sys == NULL )
220         return VLC_ENOMEM;
221     vlc_mutex_init( &p_vout->p_sys->lock );
222
223     /* Initialisations */
224     p_vout->pf_init = Init;
225     p_vout->pf_end = End;
226     p_vout->pf_manage = Manage;
227     p_vout->pf_render = NULL;
228     p_vout->pf_display = FirstDisplay;
229     p_vout->pf_control = Control;
230
231     if( CommonInit( p_vout ) )
232         goto error;
233
234     /* */
235     p_vout->p_sys->p_ddobject = NULL;
236     p_vout->p_sys->p_display = NULL;
237     p_vout->p_sys->p_current_surface = NULL;
238     p_vout->p_sys->p_clipper = NULL;
239     p_vout->p_sys->b_wallpaper = 0;
240
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") ) ) )
247     {
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") );
252     }
253
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
260     /* Initialise DirectDraw */
261     if( DirectXInitDDraw( p_vout ) )
262     {
263         msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
264         goto error;
265     }
266
267     /* Create the directx display */
268     if( DirectXCreateDisplay( p_vout ) )
269     {
270         msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
271         goto error;
272     }
273
274     /* Variable to indicate if the window should be on top of others */
275     /* Trigger a callback right now */
276     var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
277     val.psz_string = _("Wallpaper");
278     var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
279     var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
280     var_TriggerCallback( p_vout, "directx-wallpaper" );
281
282     return VLC_SUCCESS;
283
284  error:
285     CloseVideo( VLC_OBJECT(p_vout) );
286     return VLC_EGENERIC;
287 }
288
289 /*****************************************************************************
290  * Init: initialize DirectX video thread output method
291  *****************************************************************************
292  * This function create the directx surfaces needed by the output thread.
293  * It is called at the beginning of the thread.
294  *****************************************************************************/
295 static int Init( vout_thread_t *p_vout )
296 {
297     int i_chroma_backup;
298
299     /* Get a few default parameters */
300     p_vout->p_sys->b_using_overlay = var_GetBool( p_vout, "overlay" );
301     p_vout->p_sys->b_use_sysmem = var_GetBool( p_vout, "directx-use-sysmem" );
302     p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
303     p_vout->p_sys->b_3buf_overlay = var_GetBool( p_vout, "directx-3buffering" );
304
305     /* Initialise DirectDraw if not already done.
306      * We do this here because on multi-monitor systems we may have to
307      * re-create the directdraw surfaces. */
308     if( !p_vout->p_sys->p_ddobject &&
309         DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
310     {
311         msg_Err( p_vout, "cannot initialize DirectDraw" );
312         return VLC_EGENERIC;
313     }
314
315     /* Create the directx display */
316     if( !p_vout->p_sys->p_display &&
317         DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
318     {
319         msg_Err( p_vout, "cannot initialize DirectDraw" );
320         return VLC_EGENERIC;
321     }
322
323     /* Initialize the output structure.
324      * Since DirectDraw can do rescaling for us, stick to the default
325      * coordinates and aspect. */
326     p_vout->output.i_width  = p_vout->render.i_width;
327     p_vout->output.i_height = p_vout->render.i_height;
328     p_vout->output.i_aspect = p_vout->render.i_aspect;
329     p_vout->fmt_out = p_vout->fmt_in;
330     UpdateRects( p_vout, true );
331
332 #define MAX_DIRECTBUFFERS 1
333     /* Right now we use only 1 directbuffer because we don't want the
334      * video decoder to decode directly into direct buffers as they are
335      * created into video memory and video memory is _really_ slow */
336
337     /* Choose the chroma we will try first. */
338     switch( p_vout->render.i_chroma )
339     {
340         case VLC_CODEC_YUYV:
341             p_vout->output.i_chroma = VLC_CODEC_YUYV;
342             break;
343         case VLC_CODEC_UYVY:
344             p_vout->output.i_chroma = VLC_CODEC_UYVY;
345             break;
346         case VLC_CODEC_YVYU:
347             p_vout->output.i_chroma = VLC_CODEC_YVYU;
348             break;
349         case VLC_CODEC_I420:
350             p_vout->output.i_chroma = VLC_CODEC_I420;
351             break;
352         default:
353             msg_Dbg( p_vout, "use default chroma YV12 for render " \
354                              "chroma (%4.4s)",
355                              (char *)&p_vout->render.i_chroma);
356             p_vout->output.i_chroma = VLC_CODEC_YV12;
357             break;
358     }
359
360     NewPictureVec( p_vout, p_vout->p_picture );
361
362     i_chroma_backup = p_vout->output.i_chroma;
363
364     if( !I_OUTPUTPICTURES )
365     {
366         /* hmmm, it didn't work! Let's try commonly supported chromas */
367         if( p_vout->output.i_chroma != VLC_CODEC_I420 )
368         {
369             p_vout->output.i_chroma = VLC_CODEC_YV12;
370             NewPictureVec( p_vout, p_vout->p_picture );
371         }
372         if( !I_OUTPUTPICTURES )
373         {
374             /* hmmm, it still didn't work! Let's try another one */
375             p_vout->output.i_chroma = VLC_CODEC_YUYV;
376             NewPictureVec( p_vout, p_vout->p_picture );
377         }
378     }
379
380     if( !I_OUTPUTPICTURES )
381     {
382         /* If it still didn't work then don't try to use an overlay */
383         p_vout->output.i_chroma = i_chroma_backup;
384         p_vout->p_sys->b_using_overlay = false;
385         msg_Warn( p_vout, "Could not initialize directx overlay" ) ;
386         NewPictureVec( p_vout, p_vout->p_picture );
387     }
388
389     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
390
391     /* Change the window title bar text */
392     const char *psz_fallback;
393     if( p_vout->p_sys->b_using_overlay )
394         psz_fallback = VOUT_TITLE " (hardware YUV overlay DirectX output)";
395     else if( p_vout->p_sys->b_hw_yuv )
396         psz_fallback = VOUT_TITLE " (hardware YUV DirectX output)";
397     else
398         psz_fallback = VOUT_TITLE " (software RGB DirectX output)";
399     EventThreadUpdateTitle( p_vout->p_sys->p_event, psz_fallback );
400     EventThreadUseOverlay( p_vout->p_sys->p_event, p_vout->p_sys->b_using_overlay );
401
402     return VLC_SUCCESS;
403 }
404
405 /*****************************************************************************
406  * End: terminate Sys video thread output method
407  *****************************************************************************
408  * Terminate an output method created by Create.
409  * It is called at the end of the thread.
410  *****************************************************************************/
411 static void End( vout_thread_t *p_vout )
412 {
413     FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
414
415     DirectXCloseDisplay( p_vout );
416     DirectXCloseDDraw( p_vout );
417
418     return;
419 }
420
421 /*****************************************************************************
422  * CloseVideo: destroy Sys video thread output method
423  *****************************************************************************
424  * Terminate an output method created by Create
425  *****************************************************************************/
426 static void CloseVideo( vlc_object_t *p_this )
427 {
428     vout_thread_t * p_vout = (vout_thread_t *)p_this;
429
430     /* Make sure the wallpaper is restored */
431     var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
432     SwitchWallpaperMode( p_vout, false );
433
434     CommonClean( p_vout );
435
436     vlc_mutex_destroy( &p_vout->p_sys->lock );
437     free( p_vout->p_sys );
438 }
439
440 /*****************************************************************************
441  * Manage: handle Sys events
442  *****************************************************************************
443  * This function should be called regularly by the video output thread.
444  * It returns a non null value if an error occurred.
445  *****************************************************************************/
446 static int Manage( vout_thread_t *p_vout )
447 {
448     CommonManage( p_vout );
449
450     /*
451      * Position Change
452      */
453     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
454     {
455         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
456
457         /* Check if we are still on the same monitor */
458         if( p_vout->p_sys->MonitorFromWindow &&
459             p_vout->p_sys->hmonitor !=
460                 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
461                                                   MONITOR_DEFAULTTONEAREST ) )
462         {
463             /* This will force the vout core to recreate the picture buffers */
464             p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
465         }
466     }
467
468     if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
469     {
470         SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
471         p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
472         DirectDrawUpdateOverlay( p_vout );
473     }
474
475     return VLC_SUCCESS;
476 }
477
478 /*****************************************************************************
479  * Display: displays previously rendered output
480  *****************************************************************************
481  * This function sends the currently rendered image to the display, wait until
482  * it is displayed and switch the two rendering buffers, preparing next frame.
483  *****************************************************************************/
484 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
485 {
486     HRESULT dxresult;
487
488     if( (p_vout->p_sys->p_display == NULL) )
489     {
490         msg_Warn( p_vout, "no display!" );
491         return;
492     }
493
494     /* Our surface can be lost so be sure to check this
495      * and restore it if need be */
496     if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
497         == DDERR_SURFACELOST )
498     {
499         if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
500             p_vout->p_sys->b_using_overlay )
501             DirectDrawUpdateOverlay( p_vout );
502     }
503
504     if( !p_vout->p_sys->b_using_overlay )
505     {
506         DDBLTFX  ddbltfx;
507
508         /* We ask for the "NOTEARING" option */
509         memset( &ddbltfx, 0, sizeof(DDBLTFX) );
510         ddbltfx.dwSize = sizeof(DDBLTFX);
511         ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
512
513         /* Blit video surface to display */
514         dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
515                                             &p_vout->p_sys->rect_dest_clipped,
516                                             p_pic->p_sys->p_surface,
517                                             &p_vout->p_sys->rect_src_clipped,
518                                             DDBLT_ASYNC, &ddbltfx );
519         if( dxresult != DD_OK )
520         {
521             msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
522             return;
523         }
524     }
525     else /* using overlay */
526     {
527         /* Flip the overlay buffers if we are using back buffers */
528         if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
529         {
530             return;
531         }
532
533         dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
534                                              NULL, DDFLIP_WAIT );
535         if( dxresult != DD_OK )
536         {
537             msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
538         }
539
540         /* set currently displayed pic */
541         p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
542
543         /* Lock surface to get all the required info */
544         if( DirectXLockSurface( p_vout, p_pic ) )
545         {
546             /* AAARRGG */
547             msg_Warn( p_vout, "cannot lock surface" );
548             return;
549         }
550         DirectXUnlockSurface( p_vout, p_pic );
551     }
552 }
553
554 /*
555 ** this function is only used once when the first picture is received
556 ** this function will show the video window once video is ready for
557 ** display.
558 */
559
560 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
561 {
562     /* get initial picture rendered on overlay surface */
563     Display(p_vout, p_pic);
564
565     IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
566             DDWAITVB_BLOCKBEGIN, NULL);
567
568     if( p_vout->p_sys->b_using_overlay )
569     {
570         HBRUSH brush = CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey );
571         /* set the colorkey as the backgound brush for the video window */
572         SetClassLongPtr( p_vout->p_sys->hvideownd, GCLP_HBRBACKGROUND, (LONG_PTR)brush );
573     }
574     /*
575     ** Video window is initially hidden, show it now since we got a
576     ** picture to show.
577     */
578     SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
579         SWP_ASYNCWINDOWPOS|
580         SWP_FRAMECHANGED|
581         SWP_SHOWWINDOW|
582         SWP_NOMOVE|
583         SWP_NOSIZE|
584         SWP_NOZORDER );
585
586     /* use and restores proper display function for further pictures */
587     p_vout->pf_display = Display;
588 }
589
590 /* following functions are local */
591
592 /*****************************************************************************
593  * DirectXEnumCallback: Device enumeration
594  *****************************************************************************
595  * This callback function is called by DirectDraw once for each
596  * available DirectDraw device.
597  *****************************************************************************/
598 BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
599                                  LPTSTR psz_drivername, VOID* p_context,
600                                  HMONITOR hmon )
601 {
602     vout_thread_t *p_vout = (vout_thread_t *)p_context;
603     char *psz_device;
604
605     msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
606
607     if( hmon )
608     {
609         psz_device = var_GetString( p_vout, "directx-device" );
610
611         if( ( !psz_device || !*psz_device ) &&
612             hmon == p_vout->p_sys->hmonitor )
613         {
614             free( psz_device );
615         }
616         else if( strcmp( psz_drivername, psz_device ) == 0 )
617         {
618             MONITORINFO monitor_info;
619             monitor_info.cbSize = sizeof( MONITORINFO );
620
621             if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
622             {
623                 RECT rect;
624
625                 /* Move window to the right screen */
626                 GetWindowRect( p_vout->p_sys->hwnd, &rect );
627                 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
628                 {
629                     rect.left = monitor_info.rcWork.left;
630                     rect.top = monitor_info.rcWork.top;
631                     msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
632                              "position to %ld,%ld", rect.left, rect.top );
633                     SetWindowPos( p_vout->p_sys->hwnd, NULL,
634                                   rect.left, rect.top, 0, 0,
635                                   SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
636                 }
637             }
638
639             p_vout->p_sys->hmonitor = hmon;
640             free( psz_device );
641         }
642         else
643         {
644             free( psz_device );
645             return TRUE; /* Keep enumerating */
646         }
647
648         msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
649         p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
650         if( p_vout->p_sys->p_display_driver )
651             memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
652     }
653
654     return TRUE; /* Keep enumerating */
655 }
656
657 /*****************************************************************************
658  * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
659  *****************************************************************************
660  * This function initialise and allocate resources for DirectDraw.
661  *****************************************************************************/
662 static int DirectXInitDDraw( vout_thread_t *p_vout )
663 {
664     HRESULT dxresult;
665     HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
666     HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
667                                                 DWORD );
668     LPDIRECTDRAW p_ddobject;
669
670     msg_Dbg( p_vout, "DirectXInitDDraw" );
671
672     /* Load direct draw DLL */
673     p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
674     if( p_vout->p_sys->hddraw_dll == NULL )
675     {
676         msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
677         goto error;
678     }
679
680     OurDirectDrawCreate =
681       (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
682                               _T("DirectDrawCreate") );
683     if( OurDirectDrawCreate == NULL )
684     {
685         msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
686         goto error;
687     }
688
689     OurDirectDrawEnumerateEx =
690       (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
691                               _T("DirectDrawEnumerateExW") );
692
693     if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
694     {
695         char *psz_device;
696
697         psz_device = var_GetString( p_vout, "directx-device" );
698         if( psz_device )
699         {
700             msg_Dbg( p_vout, "directx-device: %s", psz_device );
701             free( psz_device );
702         }
703
704         p_vout->p_sys->hmonitor =
705             p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
706                                               MONITOR_DEFAULTTONEAREST );
707
708         /* Enumerate displays */
709         OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
710                                   DDENUM_ATTACHEDSECONDARYDEVICES );
711     }
712
713     /* Initialize DirectDraw now */
714     dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
715                                     &p_ddobject, NULL );
716     if( dxresult != DD_OK )
717     {
718         msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
719         goto error;
720     }
721
722     /* Get the IDirectDraw2 interface */
723     dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
724                                         &p_vout->p_sys->p_ddobject );
725     /* Release the unused interface */
726     IDirectDraw_Release( p_ddobject );
727     if( dxresult != DD_OK )
728     {
729         msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
730         goto error;
731     }
732
733     /* Set DirectDraw Cooperative level, ie what control we want over Windows
734      * display */
735     dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
736                                                  NULL, DDSCL_NORMAL );
737     if( dxresult != DD_OK )
738     {
739         msg_Err( p_vout, "cannot set direct draw cooperative level" );
740         goto error;
741     }
742
743     /* Get the size of the current display device */
744     if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
745     {
746         MONITORINFO monitor_info;
747         monitor_info.cbSize = sizeof( MONITORINFO );
748         p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
749                                        &monitor_info );
750         p_vout->p_sys->rect_display = monitor_info.rcMonitor;
751     }
752     else
753     {
754         p_vout->p_sys->rect_display.left = 0;
755         p_vout->p_sys->rect_display.top = 0;
756         p_vout->p_sys->rect_display.right  = GetSystemMetrics(SM_CXSCREEN);
757         p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
758     }
759
760     msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
761              p_vout->p_sys->rect_display.left,
762              p_vout->p_sys->rect_display.top,
763              p_vout->p_sys->rect_display.right,
764              p_vout->p_sys->rect_display.bottom );
765
766     /* Probe the capabilities of the hardware */
767     DirectXGetDDrawCaps( p_vout );
768
769     msg_Dbg( p_vout, "End DirectXInitDDraw" );
770     return VLC_SUCCESS;
771
772  error:
773     if( p_vout->p_sys->p_ddobject )
774         IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
775     if( p_vout->p_sys->hddraw_dll )
776         FreeLibrary( p_vout->p_sys->hddraw_dll );
777     p_vout->p_sys->hddraw_dll = NULL;
778     p_vout->p_sys->p_ddobject = NULL;
779     return VLC_EGENERIC;
780 }
781
782 /*****************************************************************************
783  * DirectXCreateDisplay: create the DirectDraw display.
784  *****************************************************************************
785  * Create and initialize display according to preferences specified in the vout
786  * thread fields.
787  *****************************************************************************/
788 static int DirectXCreateDisplay( vout_thread_t *p_vout )
789 {
790     HRESULT              dxresult;
791     DDSURFACEDESC        ddsd;
792     LPDIRECTDRAWSURFACE  p_display;
793
794     msg_Dbg( p_vout, "DirectXCreateDisplay" );
795
796     /* Now get the primary surface. This surface is what you actually see
797      * on your screen */
798     memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
799     ddsd.dwSize = sizeof(DDSURFACEDESC);
800     ddsd.dwFlags = DDSD_CAPS;
801     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
802
803     dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
804                                            &ddsd, &p_display, NULL );
805     if( dxresult != DD_OK )
806     {
807         msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
808         return VLC_EGENERIC;
809     }
810
811     dxresult = IDirectDrawSurface_QueryInterface( p_display,
812                                          &IID_IDirectDrawSurface2,
813                                          &p_vout->p_sys->p_display );
814     /* Release the old interface */
815     IDirectDrawSurface_Release( p_display );
816     if ( dxresult != DD_OK )
817     {
818         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
819                          "(error %li)", dxresult );
820         return VLC_EGENERIC;
821     }
822
823     /* The clipper will be used only in non-overlay mode */
824     DirectXCreateClipper( p_vout );
825
826     /* Make sure the colorkey will be painted */
827     p_vout->p_sys->i_colorkey = 1;
828     p_vout->p_sys->i_rgb_colorkey =
829         DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
830
831     UpdateRects( p_vout, true );
832
833     return VLC_SUCCESS;
834 }
835
836 /*****************************************************************************
837  * DirectXCreateClipper: Create a clipper that will be used when blitting the
838  *                       RGB surface to the main display.
839  *****************************************************************************
840  * This clipper prevents us to modify by mistake anything on the screen
841  * which doesn't belong to our window. For example when a part of our video
842  * window is hidden by another window.
843  *****************************************************************************/
844 static int DirectXCreateClipper( vout_thread_t *p_vout )
845 {
846     HRESULT dxresult;
847
848     msg_Dbg( p_vout, "DirectXCreateClipper" );
849
850     /* Create the clipper */
851     dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
852                                            &p_vout->p_sys->p_clipper, NULL );
853     if( dxresult != DD_OK )
854     {
855         msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
856         goto error;
857     }
858
859     /* Associate the clipper to the window */
860     dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
861                                            p_vout->p_sys->hvideownd );
862     if( dxresult != DD_OK )
863     {
864         msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
865                           dxresult );
866         goto error;
867     }
868
869     /* associate the clipper with the surface */
870     dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
871                                              p_vout->p_sys->p_clipper);
872     if( dxresult != DD_OK )
873     {
874         msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
875                           dxresult );
876         goto error;
877     }
878
879     return VLC_SUCCESS;
880
881  error:
882     if( p_vout->p_sys->p_clipper )
883     {
884         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
885     }
886     p_vout->p_sys->p_clipper = NULL;
887     return VLC_EGENERIC;
888 }
889
890 /*****************************************************************************
891  * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
892  *****************************************************************************
893  * The best method of display is with an YUV overlay because the YUV->RGB
894  * conversion is done in hardware.
895  * You can also create a plain RGB surface.
896  * ( Maybe we could also try an RGB overlay surface, which could have hardware
897  * scaling and which would also be faster in window mode because you don't
898  * need to do any blitting to the main display...)
899  *****************************************************************************/
900 static int DirectXCreateSurface( vout_thread_t *p_vout,
901                                  LPDIRECTDRAWSURFACE2 *pp_surface_final,
902                                  int i_chroma, int b_overlay,
903                                  int i_backbuffers )
904 {
905     HRESULT dxresult;
906     LPDIRECTDRAWSURFACE p_surface;
907     DDSURFACEDESC ddsd;
908
909     /* Create the video surface */
910     if( b_overlay )
911     {
912         /* Now try to create the YUV overlay surface.
913          * This overlay will be displayed on top of the primary surface.
914          * A color key is used to determine whether or not the overlay will be
915          * displayed, ie the overlay will be displayed in place of the primary
916          * surface wherever the primary surface will have this color.
917          * The video window has been created with a background of this color so
918          * the overlay will be only displayed on top of this window */
919
920         memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
921         ddsd.dwSize = sizeof(DDSURFACEDESC);
922         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
923         ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
924         ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
925         ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
926         ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
927         ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
928         ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
929                                 : 0 );
930         ddsd.dwHeight = p_vout->render.i_height;
931         ddsd.dwWidth = p_vout->render.i_width;
932         ddsd.dwBackBufferCount = i_backbuffers;
933
934         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
935                                                &ddsd, &p_surface, NULL );
936         if( dxresult != DD_OK )
937         {
938             *pp_surface_final = NULL;
939             return VLC_EGENERIC;
940         }
941     }
942
943     if( !b_overlay )
944     {
945         bool b_rgb_surface =
946             ( i_chroma == VLC_CODEC_RGB8 )
947           || ( i_chroma == VLC_CODEC_RGB15 )
948            || ( i_chroma == VLC_CODEC_RGB16 )
949             || ( i_chroma == VLC_CODEC_RGB24 )
950              || ( i_chroma == VLC_CODEC_RGB32 );
951
952         memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
953         ddsd.dwSize = sizeof(DDSURFACEDESC);
954         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
955         ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
956         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
957         ddsd.dwHeight = p_vout->render.i_height;
958         ddsd.dwWidth = p_vout->render.i_width;
959
960         if( p_vout->p_sys->b_use_sysmem )
961             ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
962         else
963             ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
964
965         if( !b_rgb_surface )
966         {
967             ddsd.dwFlags |= DDSD_PIXELFORMAT;
968             ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
969             ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
970         }
971
972         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
973                                                &ddsd, &p_surface, NULL );
974         if( dxresult != DD_OK )
975         {
976             *pp_surface_final = NULL;
977             return VLC_EGENERIC;
978         }
979     }
980
981     /* Now that the surface is created, try to get a newer DirectX interface */
982     dxresult = IDirectDrawSurface_QueryInterface( p_surface,
983                                      &IID_IDirectDrawSurface2,
984                                      (LPVOID *)pp_surface_final );
985     IDirectDrawSurface_Release( p_surface );    /* Release the old interface */
986     if ( dxresult != DD_OK )
987     {
988         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
989                          "(error %li)", dxresult );
990         *pp_surface_final = NULL;
991         return VLC_EGENERIC;
992     }
993
994     if( b_overlay )
995     {
996         /* Check the overlay is useable as some graphics cards allow creating
997          * several overlays but only one can be used at one time. */
998         p_vout->p_sys->p_current_surface = *pp_surface_final;
999         if( DirectDrawUpdateOverlay( p_vout ) != VLC_SUCCESS )
1000         {
1001             IDirectDrawSurface2_Release( *pp_surface_final );
1002             *pp_surface_final = NULL;
1003             msg_Err( p_vout, "overlay unuseable (might already be in use)" );
1004             return VLC_EGENERIC;
1005         }
1006     }
1007
1008     return VLC_SUCCESS;
1009 }
1010
1011 /*****************************************************************************
1012  * DirectDrawUpdateOverlay: Move or resize overlay surface on video display.
1013  *****************************************************************************
1014  * This function is used to move or resize an overlay surface on the screen.
1015  * Ususally the overlay is moved by the user and thus, by a move or resize
1016  * event (in Manage).
1017  *****************************************************************************/
1018 int DirectDrawUpdateOverlay( vout_thread_t *p_vout )
1019 {
1020     DDOVERLAYFX     ddofx;
1021     DWORD           dwFlags;
1022     HRESULT         dxresult;
1023     RECT            rect_src = p_vout->p_sys->rect_src_clipped;
1024     RECT            rect_dest = p_vout->p_sys->rect_dest_clipped;
1025
1026     if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
1027
1028     if( p_vout->p_sys->b_wallpaper )
1029     {
1030         unsigned i_x, i_y, i_width, i_height;
1031
1032         rect_src.left = p_vout->fmt_out.i_x_offset;
1033         rect_src.top = p_vout->fmt_out.i_y_offset;
1034         rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
1035         rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
1036
1037         rect_dest = p_vout->p_sys->rect_display;
1038         vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
1039                            &i_x, &i_y, &i_width, &i_height );
1040
1041         rect_dest.left += i_x;
1042         rect_dest.right = rect_dest.left + i_width;
1043         rect_dest.top += i_y;
1044         rect_dest.bottom = rect_dest.top + i_height;
1045     }
1046
1047     vlc_mutex_lock( &p_vout->p_sys->lock );
1048     if( p_vout->p_sys->p_current_surface == NULL )
1049     {
1050         vlc_mutex_unlock( &p_vout->p_sys->lock );
1051         return VLC_EGENERIC;
1052     }
1053
1054     /* The new window dimensions should already have been computed by the
1055      * caller of this function */
1056
1057     /* Position and show the overlay */
1058     memset(&ddofx, 0, sizeof(DDOVERLAYFX));
1059     ddofx.dwSize = sizeof(DDOVERLAYFX);
1060     ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
1061     ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
1062
1063     dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
1064
1065     dxresult = IDirectDrawSurface2_UpdateOverlay(
1066                    p_vout->p_sys->p_current_surface,
1067                    &rect_src, p_vout->p_sys->p_display, &rect_dest,
1068                    dwFlags, &ddofx );
1069
1070     vlc_mutex_unlock( &p_vout->p_sys->lock );
1071
1072     if(dxresult != DD_OK)
1073     {
1074         msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
1075         return VLC_EGENERIC;
1076     }
1077
1078     return VLC_SUCCESS;
1079 }
1080
1081 /*****************************************************************************
1082  * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
1083  *****************************************************************************
1084  * This function returns all resources allocated by DirectXInitDDraw.
1085  *****************************************************************************/
1086 static void DirectXCloseDDraw( vout_thread_t *p_vout )
1087 {
1088     msg_Dbg( p_vout, "DirectXCloseDDraw" );
1089     if( p_vout->p_sys->p_ddobject != NULL )
1090     {
1091         IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
1092         p_vout->p_sys->p_ddobject = NULL;
1093     }
1094
1095     if( p_vout->p_sys->hddraw_dll != NULL )
1096     {
1097         FreeLibrary( p_vout->p_sys->hddraw_dll );
1098         p_vout->p_sys->hddraw_dll = NULL;
1099     }
1100
1101     free( p_vout->p_sys->p_display_driver );
1102     p_vout->p_sys->p_display_driver = NULL;
1103
1104     p_vout->p_sys->hmonitor = NULL;
1105 }
1106
1107 /*****************************************************************************
1108  * DirectXCloseDisplay: close and reset the DirectX display device
1109  *****************************************************************************
1110  * This function returns all resources allocated by DirectXCreateDisplay.
1111  *****************************************************************************/
1112 static void DirectXCloseDisplay( vout_thread_t *p_vout )
1113 {
1114     msg_Dbg( p_vout, "DirectXCloseDisplay" );
1115
1116     if( p_vout->p_sys->p_clipper != NULL )
1117     {
1118         msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
1119         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1120         p_vout->p_sys->p_clipper = NULL;
1121     }
1122
1123     if( p_vout->p_sys->p_display != NULL )
1124     {
1125         msg_Dbg( p_vout, "DirectXCloseDisplay display" );
1126         IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
1127         p_vout->p_sys->p_display = NULL;
1128     }
1129 }
1130
1131 /*****************************************************************************
1132  * DirectXCloseSurface: close the YUV overlay or RGB surface.
1133  *****************************************************************************
1134  * This function returns all resources allocated for the surface.
1135  *****************************************************************************/
1136 static void DirectXCloseSurface( vout_thread_t *p_vout,
1137                                  LPDIRECTDRAWSURFACE2 p_surface )
1138 {
1139     msg_Dbg( p_vout, "DirectXCloseSurface" );
1140     if( p_surface != NULL )
1141     {
1142         IDirectDrawSurface2_Release( p_surface );
1143     }
1144 }
1145
1146 /*****************************************************************************
1147  * NewPictureVec: allocate a picture
1148  * FIXME? make it work for i_num_pic pictures...
1149  *****************************************************************************
1150  * Returns 0 on success, -1 otherwise
1151  *****************************************************************************/
1152 static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic )
1153 {
1154     int i;
1155     int i_ret = VLC_SUCCESS;
1156     LPDIRECTDRAWSURFACE2 p_surface;
1157
1158     msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
1159              p_vout->p_sys->b_using_overlay ? "yes" : "no",
1160              (char *)&p_vout->output.i_chroma );
1161
1162     I_OUTPUTPICTURES = 0;
1163
1164     /* First we try to use an YUV overlay surface.
1165      * The overlay surface that we create won't be used to decode directly
1166      * into it because accessing video memory directly is way to slow (remember
1167      * that pictures are decoded macroblock per macroblock). Instead the video
1168      * will be decoded in picture buffers in system memory which will then be
1169      * memcpy() to the overlay surface. */
1170     if( p_vout->p_sys->b_using_overlay )
1171     {
1172         /* Triple buffering rocks! it doesn't have any processing overhead
1173          * (you don't have to wait for the vsync) and provides for a very nice
1174          * video quality (no tearing). */
1175         if( p_vout->p_sys->b_3buf_overlay )
1176             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1177                                           p_vout->output.i_chroma,
1178                                           p_vout->p_sys->b_using_overlay,
1179                                           2 /* number of backbuffers */ );
1180
1181         if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
1182         {
1183             /* Try to reduce the number of backbuffers */
1184             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1185                                           p_vout->output.i_chroma,
1186                                           p_vout->p_sys->b_using_overlay,
1187                                           0 /* number of backbuffers */ );
1188         }
1189
1190         if( i_ret == VLC_SUCCESS )
1191         {
1192             DDSCAPS dds_caps;
1193             picture_t front_pic;
1194             picture_sys_t front_pic_sys;
1195             front_pic.p_sys = &front_pic_sys;
1196
1197             /* Allocate internal structure */
1198             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1199             if( p_pic[0].p_sys == NULL )
1200             {
1201                 DirectXCloseSurface( p_vout, p_surface );
1202                 return VLC_ENOMEM;
1203             }
1204
1205             /* set front buffer */
1206             p_pic[0].p_sys->p_front_surface = p_surface;
1207
1208             /* Get the back buffer */
1209             memset( &dds_caps, 0, sizeof( DDSCAPS ) );
1210             dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
1211             if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
1212                                                 p_surface, &dds_caps,
1213                                                 &p_pic[0].p_sys->p_surface ) )
1214             {
1215                 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
1216                 /* front buffer is the same as back buffer */
1217                 p_pic[0].p_sys->p_surface = p_surface;
1218             }
1219
1220
1221             p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
1222                 p_pic[0].p_sys->p_front_surface;
1223
1224             /* Reset the front buffer memory */
1225             if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
1226             {
1227                 int i,j;
1228                 for( i = 0; i < front_pic.i_planes; i++ )
1229                     for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
1230                         memset( front_pic.p[i].p_pixels + j *
1231                                 front_pic.p[i].i_pitch, 127,
1232                                 front_pic.p[i].i_visible_pitch );
1233
1234                 DirectXUnlockSurface( p_vout, &front_pic );
1235             }
1236
1237             DirectDrawUpdateOverlay( p_vout );
1238             I_OUTPUTPICTURES = 1;
1239             msg_Dbg( p_vout, "YUV overlay created successfully" );
1240         }
1241     }
1242
1243     /* As we can't have an overlay, we'll try to create a plain offscreen
1244      * surface. This surface will reside in video memory because there's a
1245      * better chance then that we'll be able to use some kind of hardware
1246      * acceleration like rescaling, blitting or YUV->RGB conversions.
1247      * We then only need to blit this surface onto the main display when we
1248      * want to display it */
1249     if( !p_vout->p_sys->b_using_overlay )
1250     {
1251         if( p_vout->p_sys->b_hw_yuv )
1252         {
1253             DWORD i_codes;
1254             DWORD *pi_codes;
1255             bool b_result = false;
1256
1257             /* Check if the chroma is supported first. This is required
1258              * because a few buggy drivers don't mind creating the surface
1259              * even if they don't know about the chroma. */
1260             if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
1261                                              &i_codes, NULL ) == DD_OK )
1262             {
1263                 pi_codes = malloc( i_codes * sizeof(DWORD) );
1264                 if( pi_codes && IDirectDraw2_GetFourCCCodes(
1265                     p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
1266                 {
1267                     for( i = 0; i < (int)i_codes; i++ )
1268                     {
1269                         if( p_vout->output.i_chroma == pi_codes[i] )
1270                         {
1271                             b_result = true;
1272                             break;
1273                         }
1274                     }
1275                 }
1276                 free( pi_codes );
1277             }
1278
1279             if( b_result )
1280                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1281                                               p_vout->output.i_chroma,
1282                                               0 /* no overlay */,
1283                                               0 /* no back buffers */ );
1284             else
1285                 p_vout->p_sys->b_hw_yuv = false;
1286         }
1287
1288         if( i_ret || !p_vout->p_sys->b_hw_yuv )
1289         {
1290             /* Our last choice is to use a plain RGB surface */
1291             DDPIXELFORMAT ddpfPixelFormat;
1292
1293             ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1294             IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
1295                                                 &ddpfPixelFormat );
1296
1297             if( ddpfPixelFormat.dwFlags & DDPF_RGB )
1298             {
1299                 switch( ddpfPixelFormat.dwRGBBitCount )
1300                 {
1301                 case 8:
1302                     p_vout->output.i_chroma = VLC_CODEC_RGB8;
1303                     p_vout->output.pf_setpalette = SetPalette;
1304                     break;
1305                 case 15:
1306                     p_vout->output.i_chroma = VLC_CODEC_RGB15;
1307                     break;
1308                 case 16:
1309                     p_vout->output.i_chroma = VLC_CODEC_RGB16;
1310                     break;
1311                 case 24:
1312                     p_vout->output.i_chroma = VLC_CODEC_RGB24;
1313                     break;
1314                 case 32:
1315                     p_vout->output.i_chroma = VLC_CODEC_RGB32;
1316                     break;
1317                 default:
1318                     msg_Err( p_vout, "unknown screen depth" );
1319                     return VLC_EGENERIC;
1320                 }
1321                 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
1322                 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
1323                 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
1324             }
1325
1326             p_vout->p_sys->b_hw_yuv = 0;
1327
1328             i_ret = DirectXCreateSurface( p_vout, &p_surface,
1329                                           p_vout->output.i_chroma,
1330                                           0 /* no overlay */,
1331                                           0 /* no back buffers */ );
1332
1333             if( i_ret && !p_vout->p_sys->b_use_sysmem )
1334             {
1335                 /* Give it a last try with b_use_sysmem enabled */
1336                 p_vout->p_sys->b_use_sysmem = 1;
1337
1338                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
1339                                               p_vout->output.i_chroma,
1340                                               0 /* no overlay */,
1341                                               0 /* no back buffers */ );
1342             }
1343         }
1344
1345         if( i_ret == VLC_SUCCESS )
1346         {
1347             /* Allocate internal structure */
1348             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
1349             if( p_pic[0].p_sys == NULL )
1350             {
1351                 DirectXCloseSurface( p_vout, p_surface );
1352                 return VLC_ENOMEM;
1353             }
1354
1355             p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
1356                 = p_surface;
1357
1358             I_OUTPUTPICTURES = 1;
1359
1360             msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
1361                      (char *)&p_vout->output.i_chroma );
1362         }
1363     }
1364
1365
1366     /* Now that we've got all our direct-buffers, we can finish filling in the
1367      * picture_t structures */
1368     for( i = 0; i < I_OUTPUTPICTURES; i++ )
1369     {
1370         p_pic[i].i_status = DESTROYED_PICTURE;
1371         p_pic[i].i_type   = DIRECT_PICTURE;
1372         p_pic[i].b_slow   = true;
1373         p_pic[i].pf_lock  = DirectXLockSurface;
1374         p_pic[i].pf_unlock = DirectXUnlockSurface;
1375         PP_OUTPUTPICTURE[i] = &p_pic[i];
1376
1377         if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
1378         {
1379             /* AAARRGG */
1380             FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
1381             I_OUTPUTPICTURES = 0;
1382             msg_Err( p_vout, "cannot lock surface" );
1383             return VLC_EGENERIC;
1384         }
1385         DirectXUnlockSurface( p_vout, &p_pic[i] );
1386     }
1387
1388     msg_Dbg( p_vout, "End NewPictureVec (%s)",
1389              I_OUTPUTPICTURES ? "succeeded" : "failed" );
1390
1391     return VLC_SUCCESS;
1392 }
1393
1394 /*****************************************************************************
1395  * FreePicture: destroy a picture vector allocated with NewPictureVec
1396  *****************************************************************************
1397  *
1398  *****************************************************************************/
1399 static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
1400                             int i_num_pics )
1401 {
1402     int i;
1403
1404     vlc_mutex_lock( &p_vout->p_sys->lock );
1405     p_vout->p_sys->p_current_surface = 0;
1406     vlc_mutex_unlock( &p_vout->p_sys->lock );
1407
1408     for( i = 0; i < i_num_pics; i++ )
1409     {
1410         DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
1411
1412         for( i = 0; i < i_num_pics; i++ )
1413         {
1414             free( p_pic[i].p_sys );
1415         }
1416     }
1417 }
1418
1419 /*****************************************************************************
1420  * UpdatePictureStruct: updates the internal data in the picture_t structure
1421  *****************************************************************************
1422  * This will setup stuff for use by the video_output thread
1423  *****************************************************************************/
1424 static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic )
1425 {
1426     switch( p_vout->output.i_chroma )
1427     {
1428         case VLC_CODEC_RGB8:
1429         case VLC_CODEC_RGB15:
1430         case VLC_CODEC_RGB16:
1431         case VLC_CODEC_RGB24:
1432         case VLC_CODEC_RGB32:
1433             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1434             p_pic->p->i_lines = p_vout->output.i_height;
1435             p_pic->p->i_visible_lines = p_vout->output.i_height;
1436             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1437             switch( p_vout->output.i_chroma )
1438             {
1439                 case VLC_CODEC_RGB8:
1440                     p_pic->p->i_pixel_pitch = 1;
1441                     break;
1442                 case VLC_CODEC_RGB15:
1443                 case VLC_CODEC_RGB16:
1444                     p_pic->p->i_pixel_pitch = 2;
1445                     break;
1446                 case VLC_CODEC_RGB24:
1447                     p_pic->p->i_pixel_pitch = 3;
1448                     break;
1449                 case VLC_CODEC_RGB32:
1450                     p_pic->p->i_pixel_pitch = 4;
1451                     break;
1452                 default:
1453                     return VLC_EGENERIC;
1454             }
1455             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1456               p_pic->p->i_pixel_pitch;
1457             p_pic->i_planes = 1;
1458             break;
1459
1460         case VLC_CODEC_YV12:
1461
1462             /* U and V inverted compared to I420
1463              * Fixme: this should be handled by the vout core */
1464             /* could this be right? */
1465             p_vout->output.i_chroma = VLC_CODEC_I420;
1466
1467             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1468             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1469             p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1470             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1471             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1472             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1473               p_pic->p[Y_PLANE].i_pixel_pitch;
1474
1475             p_pic->V_PIXELS =  p_pic->Y_PIXELS
1476               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1477             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1478             p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1479             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1480             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1481             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1482               p_pic->p[V_PLANE].i_pixel_pitch;
1483
1484             p_pic->U_PIXELS = p_pic->V_PIXELS
1485               + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
1486             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1487             p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1488             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1489             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1490             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1491               p_pic->p[U_PLANE].i_pixel_pitch;
1492
1493             p_pic->i_planes = 3;
1494             break;
1495
1496         case VLC_CODEC_I420:
1497
1498             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
1499             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
1500             p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
1501             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
1502             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
1503             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
1504               p_pic->p[Y_PLANE].i_pixel_pitch;
1505
1506             p_pic->U_PIXELS = p_pic->Y_PIXELS
1507               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
1508             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
1509             p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1510             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1511             p_pic->p[U_PLANE].i_pixel_pitch = 1;
1512             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1513               p_pic->p[U_PLANE].i_pixel_pitch;
1514
1515             p_pic->V_PIXELS =  p_pic->U_PIXELS
1516               + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
1517             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
1518             p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
1519             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
1520             p_pic->p[V_PLANE].i_pixel_pitch = 1;
1521             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
1522               p_pic->p[V_PLANE].i_pixel_pitch;
1523
1524             p_pic->i_planes = 3;
1525             break;
1526
1527         case VLC_CODEC_UYVY:
1528         case VLC_CODEC_YUYV:
1529
1530             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
1531             p_pic->p->i_lines = p_vout->output.i_height;
1532             p_pic->p->i_visible_lines = p_vout->output.i_height;
1533             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
1534             p_pic->p->i_pixel_pitch = 2;
1535             p_pic->p->i_visible_pitch = p_vout->output.i_width *
1536               p_pic->p->i_pixel_pitch;
1537
1538             p_pic->i_planes = 1;
1539             break;
1540
1541         default:
1542             /* Unknown chroma, tell the guy to get lost */
1543             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
1544                      p_vout->output.i_chroma,
1545                      (char*)&p_vout->output.i_chroma );
1546             return VLC_EGENERIC;
1547     }
1548
1549     return VLC_SUCCESS;
1550 }
1551
1552 /*****************************************************************************
1553  * DirectXGetDDrawCaps: Probe the capabilities of the hardware
1554  *****************************************************************************
1555  * It is nice to know which features are supported by the hardware so we can
1556  * find ways to optimize our rendering.
1557  *****************************************************************************/
1558 static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
1559 {
1560     DDCAPS ddcaps;
1561     HRESULT dxresult;
1562
1563     /* This is just an indication of whether or not we'll support overlay,
1564      * but with this test we don't know if we support YUV overlay */
1565     memset( &ddcaps, 0, sizeof( DDCAPS ));
1566     ddcaps.dwSize = sizeof(DDCAPS);
1567     dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
1568                                      &ddcaps, NULL );
1569     if(dxresult != DD_OK )
1570     {
1571         msg_Warn( p_vout, "cannot get caps" );
1572     }
1573     else
1574     {
1575         bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1576              bHasColorKey, bCanStretch, bCanBltFourcc,
1577              bAlignBoundarySrc, bAlignBoundaryDest,
1578              bAlignSizeSrc, bAlignSizeDest;
1579
1580         /* Determine if the hardware supports overlay surfaces */
1581         bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
1582         /* Determine if the hardware supports overlay surfaces */
1583         bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
1584         /* Determine if the hardware supports overlay deinterlacing */
1585         bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
1586         /* Determine if the hardware supports colorkeying */
1587         bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
1588         /* Determine if the hardware supports scaling of the overlay surface */
1589         bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
1590         /* Determine if the hardware supports color conversion during a blit */
1591         bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
1592         /* Determine overlay source boundary alignment */
1593         bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
1594         /* Determine overlay destination boundary alignment */
1595         bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
1596         /* Determine overlay destination size alignment */
1597         bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
1598         /* Determine overlay destination size alignment */
1599         bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
1600
1601         msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
1602                          "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
1603                          "bltfourcc=%i",
1604                          bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
1605                          bHasColorKey, bCanStretch, bCanBltFourcc );
1606
1607         if( bAlignBoundarySrc || bAlignBoundaryDest ||
1608             bAlignSizeSrc || bAlignSizeDest )
1609         {
1610             if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
1611                 ddcaps.dwAlignBoundarySrc;
1612             if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
1613                 ddcaps.dwAlignBoundaryDest;
1614             if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
1615                 ddcaps.dwAlignSizeSrc;
1616             if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
1617                 ddcaps.dwAlignSizeDest;
1618
1619             msg_Dbg( p_vout, "align_boundary_src=%i,%i "
1620                      "align_boundary_dest=%i,%i "
1621                      "align_size_src=%i,%i align_size_dest=%i,%i",
1622                      bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
1623                      bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
1624                      bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
1625                      bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
1626         }
1627
1628         /* Don't ask for troubles */
1629         if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
1630     }
1631 }
1632
1633 /*****************************************************************************
1634  * DirectXLockSurface: Lock surface and get picture data pointer
1635  *****************************************************************************
1636  * This function locks a surface and get the surface descriptor which amongst
1637  * other things has the pointer to the picture data.
1638  *****************************************************************************/
1639 static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1640 {
1641     HRESULT dxresult;
1642
1643     /* Lock the surface to get a valid pointer to the picture buffer */
1644     memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
1645     p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
1646     dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
1647                                          NULL, &p_pic->p_sys->ddsd,
1648                                          DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
1649                                          NULL );
1650     if( dxresult != DD_OK )
1651     {
1652         if( dxresult == DDERR_INVALIDPARAMS )
1653         {
1654             /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
1655              * in an invalid params error */
1656             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1657                                              &p_pic->p_sys->ddsd,
1658                                              DDLOCK_WAIT, NULL);
1659         }
1660         if( dxresult == DDERR_SURFACELOST )
1661         {
1662             /* Your surface can be lost so be sure
1663              * to check this and restore it if needed */
1664
1665             /* When using overlays with back-buffers, we need to restore
1666              * the front buffer so the back-buffers get restored as well. */
1667             if( p_vout->p_sys->b_using_overlay  )
1668                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
1669             else
1670                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
1671
1672             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
1673                                                  &p_pic->p_sys->ddsd,
1674                                                  DDLOCK_WAIT, NULL);
1675 #ifndef NDEBUG
1676             if( dxresult == DDERR_SURFACELOST )
1677                 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
1678 #endif
1679         }
1680         if( dxresult != DD_OK )
1681         {
1682             return VLC_EGENERIC;
1683         }
1684     }
1685
1686     /* Now we have a pointer to the surface memory, we can update our picture
1687      * structure. */
1688     if( UpdatePictureStruct( p_vout, p_pic )
1689         != VLC_SUCCESS )
1690     {
1691         DirectXUnlockSurface( p_vout, p_pic );
1692         return VLC_EGENERIC;
1693     }
1694     else
1695         return VLC_SUCCESS;
1696 }
1697
1698 /*****************************************************************************
1699  * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
1700  *****************************************************************************/
1701 static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1702 {
1703     VLC_UNUSED( p_vout );
1704
1705     /* Unlock the Surface */
1706     if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
1707         return VLC_SUCCESS;
1708     else
1709         return VLC_EGENERIC;
1710 }
1711
1712 /*****************************************************************************
1713  * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
1714  *****************************************************************************/
1715 static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
1716 {
1717     DDSURFACEDESC ddsd;
1718     HRESULT dxresult;
1719     COLORREF i_rgb = 0;
1720     uint32_t i_pixel_backup;
1721     HDC hdc;
1722
1723     ddsd.dwSize = sizeof(ddsd);
1724     dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1725                                          &ddsd, DDLOCK_WAIT, NULL );
1726     if( dxresult != DD_OK ) return 0;
1727
1728     i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
1729
1730     switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
1731     {
1732     case 4:
1733         *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
1734         break;
1735     case 8:
1736         *(uint8_t *)ddsd.lpSurface = *pi_color;
1737         break;
1738     case 15:
1739     case 16:
1740         *(uint16_t *)ddsd.lpSurface = *pi_color;
1741         break;
1742     case 24:
1743         /* Seems to be problematic so we'll just put black as the colorkey */
1744         *pi_color = 0;
1745     default:
1746         *(uint32_t *)ddsd.lpSurface = *pi_color;
1747         break;
1748     }
1749
1750     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1751
1752     if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
1753     {
1754         i_rgb = GetPixel( hdc, 0, 0 );
1755         IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
1756     }
1757
1758     ddsd.dwSize = sizeof(ddsd);
1759     dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
1760                                          &ddsd, DDLOCK_WAIT, NULL );
1761     if( dxresult != DD_OK ) return i_rgb;
1762
1763     *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
1764
1765     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
1766
1767     return i_rgb;
1768 }
1769
1770 /*****************************************************************************
1771  * A few toolbox functions
1772  *****************************************************************************/
1773 void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
1774 {
1775     HWND hwnd;
1776
1777     if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
1778
1779     hwnd = FindWindow( _T("Progman"), NULL );
1780     if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
1781     if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
1782     if( !hwnd )
1783     {
1784         msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
1785                   "wallpaper mode not supported" );
1786         return;
1787     }
1788
1789     p_vout->p_sys->b_wallpaper = b_on;
1790
1791     msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
1792
1793     if( p_vout->p_sys->b_wallpaper )
1794     {
1795         p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
1796         p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
1797
1798         ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1799         ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
1800     }
1801     else if( hwnd )
1802     {
1803         ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
1804         ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
1805     }
1806
1807     /* Update desktop */
1808     InvalidateRect( hwnd, NULL, TRUE );
1809     UpdateWindow( hwnd );
1810 }
1811
1812 /*****************************************************************************
1813  * config variable callback
1814  *****************************************************************************/
1815 BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
1816                                   LPTSTR psz_drivername, VOID* p_context,
1817                                   HMONITOR hmon )
1818 {
1819     VLC_UNUSED( p_guid ); VLC_UNUSED( psz_desc ); VLC_UNUSED( hmon );
1820
1821     module_config_t *p_item = (module_config_t *)p_context;
1822
1823     p_item->ppsz_list = realloc_or_free( p_item->ppsz_list,
1824                           (p_item->i_list+2) * sizeof(char *) );
1825     assert( p_item->ppsz_list );
1826     p_item->ppsz_list_text = realloc_or_free( p_item->ppsz_list_text,
1827                           (p_item->i_list+2) * sizeof(char *) );
1828     assert( p_item->ppsz_list_text );
1829
1830     p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
1831     p_item->ppsz_list_text[p_item->i_list] = NULL;
1832     p_item->i_list++;
1833     p_item->ppsz_list[p_item->i_list] = NULL;
1834     p_item->ppsz_list_text[p_item->i_list] = NULL;
1835
1836     return TRUE; /* Keep enumerating */
1837 }
1838
1839 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1840                                vlc_value_t newval, vlc_value_t oldval, void *d)
1841 {
1842     VLC_UNUSED( newval ); VLC_UNUSED( oldval ); VLC_UNUSED( d );
1843
1844     HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
1845                                                 DWORD );
1846     HINSTANCE hddraw_dll;
1847
1848     module_config_t *p_item;
1849     int i;
1850
1851     p_item = config_FindConfig( p_this, psz_name );
1852     if( !p_item ) return VLC_SUCCESS;
1853
1854     /* Clear-up the current list */
1855     if( p_item->i_list )
1856     {
1857         /* Keep the first entry */
1858         for( i = 1; i < p_item->i_list; i++ )
1859         {
1860             free( p_item->ppsz_list[i] );
1861             free( p_item->ppsz_list_text[i] );
1862         }
1863         /* TODO: Remove when no more needed */
1864         p_item->ppsz_list[i] = NULL;
1865         p_item->ppsz_list_text[i] = NULL;
1866     }
1867     p_item->i_list = 1;
1868
1869     /* Load direct draw DLL */
1870     hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
1871     if( hddraw_dll == NULL ) return VLC_SUCCESS;
1872
1873     OurDirectDrawEnumerateEx =
1874       (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
1875
1876     if( OurDirectDrawEnumerateEx )
1877     {
1878         /* Enumerate displays */
1879         OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
1880                                   DDENUM_ATTACHEDSECONDARYDEVICES );
1881     }
1882
1883     FreeLibrary( hddraw_dll );
1884
1885     /* Signal change to the interface */
1886     p_item->b_dirty = true;
1887
1888     return VLC_SUCCESS;
1889 }
1890
1891 static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
1892                               vlc_value_t oldval, vlc_value_t newval,
1893                               void *p_data )
1894 {
1895     VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval ); VLC_UNUSED( p_data );
1896     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1897
1898     if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
1899         (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
1900     {
1901         playlist_t *p_playlist = pl_Hold( p_vout );
1902
1903         if( p_playlist )
1904         {
1905             /* Modify playlist as well because the vout might have to be
1906              * restarted */
1907             var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
1908             var_Set( p_playlist, "directx-wallpaper", newval );
1909             pl_Release( p_vout );
1910         }
1911
1912         p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
1913     }
1914
1915     return VLC_SUCCESS;
1916 }
1917
1918 /*****************************************************************************
1919  * SetPalette: sets an 8 bpp palette
1920  *****************************************************************************/
1921 static void SetPalette( vout_thread_t *p_vout,
1922                         uint16_t *red, uint16_t *green, uint16_t *blue )
1923 {
1924     VLC_UNUSED( red ); VLC_UNUSED( green );VLC_UNUSED( blue );
1925     msg_Err( p_vout, "FIXME: SetPalette unimplemented" );
1926 }