]> git.sesse.net Git - vlc/blob - modules/video_output/msw/direct3d.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / video_output / msw / direct3d.c
1 /*****************************************************************************
2  * direct3d.c: Windows Direct3D video output module
3  *****************************************************************************
4  * Copyright (C) 2006-2009 the VideoLAN team
5  *$Id$
6  *
7  * Authors: Damien Fouilleul <damienf@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 surface if supported, using YUV will result in
28  * the best video quality (hardware filering 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.
34  *
35  *****************************************************************************/
36 #include <errno.h>                                                 /* ENOMEM */
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_interface.h>
45 #include <vlc_playlist.h>
46 #include <vlc_vout.h>
47
48 #include <windows.h>
49 #include <d3d9.h>
50
51 #include "vout.h"
52
53 /*****************************************************************************
54  * Local prototypes.
55  *****************************************************************************/
56 static int  OpenVideo  ( vlc_object_t * );
57 static void CloseVideo ( vlc_object_t * );
58
59 static int  Init      ( vout_thread_t * );
60 static void End       ( vout_thread_t * );
61 static int  Manage    ( vout_thread_t * );
62 static void Display   ( vout_thread_t *, picture_t * );
63 static void FirstDisplay( vout_thread_t *, picture_t * );
64
65 static int Direct3DVoutCreate     ( vout_thread_t * );
66 static void Direct3DVoutRelease   ( vout_thread_t * );
67
68 static int  Direct3DVoutOpen      ( vout_thread_t * );
69 static void Direct3DVoutClose     ( vout_thread_t * );
70
71 static int Direct3DVoutResetDevice( vout_thread_t * );
72
73 static int Direct3DVoutCreatePictures   ( vout_thread_t *, size_t );
74 static void Direct3DVoutReleasePictures ( vout_thread_t * );
75
76 static int Direct3DVoutLockSurface  ( vout_thread_t *, picture_t * );
77 static int Direct3DVoutUnlockSurface( vout_thread_t *, picture_t * );
78
79 static int Direct3DVoutCreateScene      ( vout_thread_t * );
80 static void Direct3DVoutReleaseScene    ( vout_thread_t * );
81 static void Direct3DVoutRenderScene     ( vout_thread_t *, picture_t * );
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86
87 static bool IsVistaOrAbove(void)
88 {
89     OSVERSIONINFO winVer;
90     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
91
92     if( GetVersionEx(&winVer) )
93     {
94         if( winVer.dwMajorVersion > 5 )
95         {
96             /* Windows Vista or above, make this module the default */
97             return true;
98         }
99     }
100     /* Windows XP or lower, make sure this module isn't the default */
101     return false;
102 }
103
104 static int OpenVideoXP( vlc_object_t *obj )
105 {
106     return IsVistaOrAbove() ? VLC_EGENERIC : OpenVideo( obj );
107 }
108
109 static int OpenVideoVista( vlc_object_t *obj )
110 {
111     return IsVistaOrAbove() ? OpenVideo( obj ) : VLC_EGENERIC;
112 }
113
114 vlc_module_begin ()
115     set_shortname( "Direct3D" )
116     set_category( CAT_VIDEO )
117     set_subcategory( SUBCAT_VIDEO_VOUT )
118     set_description( N_("DirectX 3D video output") )
119     set_capability( "video output", 50 )
120     add_shortcut( "direct3d" )
121     set_callbacks( OpenVideoXP, CloseVideo )
122
123     /* FIXME: Hack to avoid unregistering our window class */
124     linked_with_a_crap_library_which_uses_atexit ()
125
126     add_submodule()
127         set_capability( "video output", 150 )
128         add_shortcut( "direct3d" )
129         set_callbacks( OpenVideoVista, CloseVideo )
130 vlc_module_end ()
131
132 #if 0 /* FIXME */
133     /* check if we registered a window class because we need to
134      * unregister it */
135     WNDCLASS wndclass;
136     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
137         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
138 #endif
139
140 /*****************************************************************************
141  * CUSTOMVERTEX:
142  *****************************************************************************
143  *****************************************************************************/
144 typedef struct
145 {
146     FLOAT       x,y,z;      // vertex untransformed position
147     FLOAT       rhw;        // eye distance
148     D3DCOLOR    diffuse;    // diffuse color
149     FLOAT       tu, tv;     // texture relative coordinates
150 } CUSTOMVERTEX;
151
152 #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
153
154 /*****************************************************************************
155  * OpenVideo: allocate Vout video thread output method
156  *****************************************************************************
157  * This function allocates and initialize the Direct3D vout method.
158  *****************************************************************************/
159 static int OpenVideo( vlc_object_t *p_this )
160 {
161     vout_thread_t * p_vout = (vout_thread_t *)p_this;
162
163     /* Allocate structure */
164     p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
165     if( p_vout->p_sys == NULL )
166         return VLC_ENOMEM;
167
168     if( VLC_SUCCESS != Direct3DVoutCreate( p_vout ) )
169     {
170         msg_Err( p_vout, "Direct3D could not be initialized !");
171         Direct3DVoutRelease( p_vout );
172         free( p_vout->p_sys );
173         return VLC_EGENERIC;
174     }
175
176     /* Initialisations */
177     p_vout->pf_init = Init;
178     p_vout->pf_end = End;
179     p_vout->pf_manage = Manage;
180     p_vout->pf_render = Direct3DVoutRenderScene;
181     p_vout->pf_display = FirstDisplay;
182
183     p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
184     p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
185     p_vout->p_sys->i_changes = 0;
186     vlc_mutex_init( &p_vout->p_sys->lock );
187     SetRectEmpty( &p_vout->p_sys->rect_display );
188     SetRectEmpty( &p_vout->p_sys->rect_parent );
189
190     var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
191     var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
192
193     p_vout->p_sys->b_cursor_hidden = 0;
194     p_vout->p_sys->i_lastmoved = mdate();
195     p_vout->p_sys->i_mouse_hide_timeout =
196         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
197
198     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
199     var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
200
201     /* Set main window's size */
202     p_vout->p_sys->i_window_width = p_vout->i_window_width;
203     p_vout->p_sys->i_window_height = p_vout->i_window_height;
204
205     /* Create the Vout EventThread, this thread is created by us to isolate
206      * the Win32 PeekMessage function calls. We want to do this because
207      * Windows can stay blocked inside this call for a long time, and when
208      * this happens it thus blocks vlc's video_output thread.
209      * Vout EventThread will take care of the creation of the video
210      * window (because PeekMessage has to be called from the same thread which
211      * created the window). */
212     msg_Dbg( p_vout, "creating Vout EventThread" );
213     p_vout->p_sys->p_event =
214         vlc_object_create( p_vout, sizeof(event_thread_t) );
215     p_vout->p_sys->p_event->p_vout = p_vout;
216     p_vout->p_sys->p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
217     if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
218                            EventThread, 0 ) )
219     {
220         msg_Err( p_vout, "cannot create Vout EventThread" );
221         CloseHandle( p_vout->p_sys->p_event->window_ready );
222         vlc_object_release( p_vout->p_sys->p_event );
223         p_vout->p_sys->p_event = NULL;
224         goto error;
225     }
226     WaitForSingleObject( p_vout->p_sys->p_event->window_ready, INFINITE );
227     CloseHandle( p_vout->p_sys->p_event->window_ready );
228
229     if( p_vout->p_sys->p_event->b_error )
230     {
231         msg_Err( p_vout, "Vout EventThread failed" );
232         goto error;
233     }
234
235     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
236
237     msg_Dbg( p_vout, "Vout EventThread running" );
238
239     /* Variable to indicate if the window should be on top of others */
240     /* Trigger a callback right now */
241     var_TriggerCallback( p_vout, "video-on-top" );
242
243     /* disable screensaver by temporarily changing system settings */
244     p_vout->p_sys->i_spi_lowpowertimeout = 0;
245     p_vout->p_sys->i_spi_powerofftimeout = 0;
246     p_vout->p_sys->i_spi_screensavetimeout = 0;
247     if( var_GetBool( p_vout, "disable-screensaver" ) ) {
248         msg_Dbg(p_vout, "disabling screen saver");
249         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
250             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
251         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
252             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
253         }
254         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
255             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
256         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
257             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
258         }
259         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
260             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
261         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
262             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
263         }
264     }
265     return VLC_SUCCESS;
266
267 error:
268     CloseVideo( VLC_OBJECT(p_vout) );
269     return VLC_EGENERIC;
270 }
271
272 /*****************************************************************************
273  * CloseVideo: destroy Sys video thread output method
274  *****************************************************************************
275  * Terminate an output method created by Create
276  *****************************************************************************/
277 static void CloseVideo( vlc_object_t *p_this )
278 {
279     vout_thread_t * p_vout = (vout_thread_t *)p_this;
280
281     Direct3DVoutRelease( p_vout );
282
283     if( p_vout->b_fullscreen )
284     {
285         msg_Dbg( p_vout, "Quitting fullscreen" );
286         Win32ToggleFullscreen( p_vout );
287         /* Force fullscreen in the core for the next video */
288         var_SetBool( p_vout, "fullscreen", true );
289     }
290
291     if( p_vout->p_sys->p_event )
292     {
293         vlc_object_detach( p_vout->p_sys->p_event );
294
295         /* Kill Vout EventThread */
296         vlc_object_kill( p_vout->p_sys->p_event );
297
298         /* we need to be sure Vout EventThread won't stay stuck in
299          * GetMessage, so we send a fake message */
300         if( p_vout->p_sys->hwnd )
301         {
302             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
303         }
304
305         vlc_thread_join( p_vout->p_sys->p_event );
306         vlc_object_release( p_vout->p_sys->p_event );
307     }
308
309     vlc_mutex_destroy( &p_vout->p_sys->lock );
310
311     /* restore screensaver system settings */
312     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
313         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
314             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
315     }
316     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
317         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
318             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
319     }
320     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
321         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
322             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
323     }
324
325     free( p_vout->p_sys );
326     p_vout->p_sys = NULL;
327 }
328
329 /*****************************************************************************
330  * Init: initialize Direct3D video thread output method
331  *****************************************************************************/
332 static int Init( vout_thread_t *p_vout )
333 {
334     int i_ret;
335
336     p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
337
338     /* Initialise Direct3D */
339     if( VLC_SUCCESS != Direct3DVoutOpen( p_vout ) )
340     {
341         msg_Err( p_vout, "cannot initialize Direct3D" );
342         return VLC_EGENERIC;
343     }
344
345     /* Initialize the output structure.
346      * Since Direct3D can do rescaling for us, stick to the default
347      * coordinates and aspect. */
348     p_vout->output.i_width  = p_vout->render.i_width;
349     p_vout->output.i_height = p_vout->render.i_height;
350     p_vout->output.i_aspect = p_vout->render.i_aspect;
351     p_vout->fmt_out = p_vout->fmt_in;
352     UpdateRects( p_vout, true );
353
354     /*  create picture pool */
355     p_vout->output.i_chroma = 0;
356     i_ret = Direct3DVoutCreatePictures(p_vout, 1);
357     if( VLC_SUCCESS != i_ret )
358     {
359         msg_Err(p_vout, "Direct3D picture pool initialization failed !");
360         return i_ret;
361     }
362
363     /* create scene */
364     i_ret = Direct3DVoutCreateScene(p_vout);
365     if( VLC_SUCCESS != i_ret )
366     {
367         msg_Err(p_vout, "Direct3D scene initialization failed !");
368         Direct3DVoutReleasePictures(p_vout);
369         return i_ret;
370     }
371
372     /* Change the window title bar text */
373     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
374
375     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
376     return VLC_SUCCESS;
377 }
378
379 /*****************************************************************************
380  * End: terminate Sys video thread output method
381  *****************************************************************************
382  * Terminate an output method created by Create.
383  * It is called at the end of the thread.
384  *****************************************************************************/
385 static void End( vout_thread_t *p_vout )
386 {
387     Direct3DVoutReleaseScene(p_vout);
388     Direct3DVoutReleasePictures(p_vout);
389     Direct3DVoutClose( p_vout );
390 }
391
392 /*****************************************************************************
393  * Manage: handle Sys events
394  *****************************************************************************
395  * This function should be called regularly by the video output thread.
396  * It returns a non null value if an error occurred.
397  *****************************************************************************/
398 static int Manage( vout_thread_t *p_vout )
399 {
400     /* If we do not control our window, we check for geometry changes
401      * ourselves because the parent might not send us its events. */
402     vlc_mutex_lock( &p_vout->p_sys->lock );
403     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
404     {
405         RECT rect_parent;
406         POINT point;
407
408         vlc_mutex_unlock( &p_vout->p_sys->lock );
409
410         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
411         point.x = point.y = 0;
412         ClientToScreen( p_vout->p_sys->hparent, &point );
413         OffsetRect( &rect_parent, point.x, point.y );
414
415         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
416         {
417             p_vout->p_sys->rect_parent = rect_parent;
418
419             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
420                           rect_parent.right - rect_parent.left,
421                           rect_parent.bottom - rect_parent.top,
422                           SWP_NOZORDER );
423         }
424     }
425     else
426     {
427         vlc_mutex_unlock( &p_vout->p_sys->lock );
428     }
429
430     /*
431      * Position Change
432      */
433     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
434     {
435 #if 0 /* need that when bicubic filter is available */
436         RECT rect;
437         UINT width, height;
438
439         GetClientRect(p_vout->p_sys->hvideownd, &rect);
440         width  = rect.right-rect.left;
441         height = rect.bottom-rect.top;
442
443         if( (width != p_vout->p_sys->d3dpp.BackBufferWidth)
444          || (height != p_vout->p_sys->d3dpp.BackBufferHeight) )
445         {
446             msg_Dbg(p_vout, "resizing device back buffers to (%lux%lu)", width, height);
447             // need to reset D3D device to resize back buffer
448             if( VLC_SUCCESS != Direct3DVoutResetDevice(p_vout, width, height) )
449                 return VLC_EGENERIC;
450         }
451 #endif
452         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
453     }
454
455     /* autoscale toggle */
456     if( p_vout->i_changes & VOUT_SCALE_CHANGE )
457     {
458         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
459
460         p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
461         p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
462
463         UpdateRects( p_vout, true );
464     }
465
466     /* scaling factor */
467     if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
468     {
469         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
470
471         p_vout->b_autoscale = false;
472         p_vout->i_zoom =
473             (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
474         UpdateRects( p_vout, true );
475     }
476
477     /* Check for cropping / aspect changes */
478     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
479         p_vout->i_changes & VOUT_ASPECT_CHANGE )
480     {
481         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
482         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
483
484         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
485         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
486         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
487         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
488         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
489         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
490         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
491         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
492         UpdateRects( p_vout, true );
493     }
494
495     /* We used to call the Win32 PeekMessage function here to read the window
496      * messages. But since window can stay blocked into this function for a
497      * long time (for example when you move your window on the screen), I
498      * decided to isolate PeekMessage in another thread. */
499
500     /*
501      * Fullscreen change
502      */
503     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
504         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
505     {
506         Win32ToggleFullscreen( p_vout );
507
508         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
509         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
510     }
511
512     /*
513      * Pointer change
514      */
515     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
516         (mdate() - p_vout->p_sys->i_lastmoved) >
517             p_vout->p_sys->i_mouse_hide_timeout )
518     {
519         POINT point;
520         HWND hwnd;
521
522         /* Hide the cursor only if it is inside our window */
523         GetCursorPos( &point );
524         hwnd = WindowFromPoint(point);
525         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
526         {
527             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
528         }
529         else
530         {
531             p_vout->p_sys->i_lastmoved = mdate();
532         }
533     }
534
535     /*
536      * "Always on top" status change
537      */
538     if( p_vout->p_sys->b_on_top_change )
539     {
540         vlc_value_t val;
541         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
542
543         var_Get( p_vout, "video-on-top", &val );
544
545         /* Set the window on top if necessary */
546         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
547                            & WS_EX_TOPMOST ) )
548         {
549             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
550                            MF_BYCOMMAND | MFS_CHECKED );
551             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
552                           SWP_NOSIZE | SWP_NOMOVE );
553         }
554         else
555         /* The window shouldn't be on top */
556         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
557                            & WS_EX_TOPMOST ) )
558         {
559             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
560                            MF_BYCOMMAND | MFS_UNCHECKED );
561             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
562                           SWP_NOSIZE | SWP_NOMOVE );
563         }
564
565         p_vout->p_sys->b_on_top_change = false;
566     }
567
568     /* Check if the event thread is still running */
569     if( !vlc_object_alive (p_vout->p_sys->p_event) )
570     {
571         return VLC_EGENERIC; /* exit */
572     }
573
574     return VLC_SUCCESS;
575 }
576
577 /*****************************************************************************
578  * Display: displays previously rendered output
579  *****************************************************************************
580  * This function sends the currently rendered image to the display, wait until
581  * it is displayed and switch the two rendering buffers, preparing next frame.
582  *****************************************************************************/
583 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
584 {
585     LPDIRECT3DDEVICE9       p_d3ddev = p_vout->p_sys->p_d3ddev;
586     // Present the back buffer contents to the display
587     // stretching and filtering happens here
588     HRESULT hr = IDirect3DDevice9_Present(p_d3ddev,
589                     &(p_vout->p_sys->rect_src_clipped),
590                     NULL, NULL, NULL);
591     if( FAILED(hr) )
592         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
593 }
594
595 /*
596 ** this function is only used once when the first picture is received
597 ** this function will show the video window once a picture is ready
598 */
599
600 static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
601 {
602     /* get initial picture presented through D3D */
603     Display(p_vout, p_pic);
604
605     /*
606     ** Video window is initially hidden, show it now since we got a
607     ** picture to show.
608     */
609     SetWindowPos( p_vout->p_sys->hvideownd, 0, 0, 0, 0, 0,
610         SWP_ASYNCWINDOWPOS|
611         SWP_FRAMECHANGED|
612         SWP_SHOWWINDOW|
613         SWP_NOMOVE|
614         SWP_NOSIZE|
615         SWP_NOZORDER );
616
617     /* use and restores proper display function for further pictures */
618     p_vout->pf_display = Display;
619 }
620
621 /*****************************************************************************
622  * DirectD3DVoutCreate: Initialize and instance of Direct3D9
623  *****************************************************************************
624  * This function initialize Direct3D and analyze available resources from
625  * default adapter.
626  *****************************************************************************/
627 static int Direct3DVoutCreate( vout_thread_t *p_vout )
628 {
629     HRESULT hr;
630     LPDIRECT3D9 p_d3dobj;
631     D3DCAPS9 d3dCaps;
632
633     LPDIRECT3D9 (WINAPI *OurDirect3DCreate9)(UINT SDKVersion);
634
635     p_vout->p_sys->hd3d9_dll = LoadLibrary(TEXT("D3D9.DLL"));
636     if( NULL == p_vout->p_sys->hd3d9_dll )
637     {
638         msg_Warn( p_vout, "cannot load d3d9.dll, aborting" );
639         return VLC_EGENERIC;
640     }
641
642     OurDirect3DCreate9 =
643       (void *)GetProcAddress( p_vout->p_sys->hd3d9_dll,
644                               TEXT("Direct3DCreate9") );
645     if( OurDirect3DCreate9 == NULL )
646     {
647         msg_Err( p_vout, "Cannot locate reference to Direct3DCreate9 ABI in DLL" );
648         return VLC_EGENERIC;
649     }
650
651     /* Create the D3D object. */
652     p_d3dobj = OurDirect3DCreate9( D3D_SDK_VERSION );
653     if( NULL == p_d3dobj )
654     {
655        msg_Err( p_vout, "Could not create Direct3D9 instance.");
656        return VLC_EGENERIC;
657     }
658     p_vout->p_sys->p_d3dobj = p_d3dobj;
659
660     /*
661     ** Get device capabilities
662     */
663     ZeroMemory(&d3dCaps, sizeof(d3dCaps));
664     hr = IDirect3D9_GetDeviceCaps(p_d3dobj, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);
665     if( FAILED(hr) )
666     {
667        msg_Err( p_vout, "Could not read adapter capabilities. (hr=0x%lX)", hr);
668        return VLC_EGENERIC;
669     }
670     /* TODO: need to test device capabilities and select the right render function */
671
672     return VLC_SUCCESS;
673 }
674
675 /*****************************************************************************
676  * DirectD3DVoutRelease: release an instance of Direct3D9
677  *****************************************************************************/
678
679 static void Direct3DVoutRelease( vout_thread_t *p_vout )
680 {
681     if( p_vout->p_sys->p_d3dobj )
682     {
683        IDirect3D9_Release(p_vout->p_sys->p_d3dobj);
684        p_vout->p_sys->p_d3dobj = NULL;
685     }
686     if( NULL != p_vout->p_sys->hd3d9_dll )
687     {
688         FreeLibrary(p_vout->p_sys->hd3d9_dll);
689         p_vout->p_sys->hd3d9_dll = NULL;
690     }
691 }
692
693 static int Direct3DFillPresentationParameters(vout_thread_t *p_vout, D3DPRESENT_PARAMETERS *d3dpp)
694 {
695     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
696     D3DDISPLAYMODE d3ddm;
697     HRESULT hr;
698
699     /*
700     ** Get the current desktop display mode, so we can set up a back
701     ** buffer of the same format
702     */
703     hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
704     if( FAILED(hr))
705     {
706        msg_Err( p_vout, "Could not read adapter display mode. (hr=0x%lX)", hr);
707        return VLC_EGENERIC;
708     }
709
710     /* keep a copy of current desktop format */
711     p_vout->p_sys->bbFormat = d3ddm.Format;
712
713     /* Set up the structure used to create the D3DDevice. */
714     ZeroMemory( d3dpp, sizeof(D3DPRESENT_PARAMETERS) );
715     d3dpp->Flags                  = D3DPRESENTFLAG_VIDEO;
716     d3dpp->Windowed               = TRUE;
717     d3dpp->hDeviceWindow          = p_vout->p_sys->hvideownd;
718     d3dpp->BackBufferWidth        = p_vout->output.i_width;
719     d3dpp->BackBufferHeight       = p_vout->output.i_height;
720     d3dpp->SwapEffect             = D3DSWAPEFFECT_COPY;
721     d3dpp->MultiSampleType        = D3DMULTISAMPLE_NONE;
722     d3dpp->PresentationInterval   = D3DPRESENT_INTERVAL_DEFAULT;
723     d3dpp->BackBufferFormat       = d3ddm.Format;
724     d3dpp->BackBufferCount        = 1;
725     d3dpp->EnableAutoDepthStencil = FALSE;
726
727     return VLC_SUCCESS;
728 }
729
730 /*****************************************************************************
731  * DirectD3DVoutOpen: Takes care of all the Direct3D9 initialisations
732  *****************************************************************************
733  * This function creates Direct3D device
734  * this must be called from the vout thread for performance reason, as
735  * all Direct3D Device APIs are used in a non multithread safe environment
736  *****************************************************************************/
737 static int Direct3DVoutOpen( vout_thread_t *p_vout )
738 {
739     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
740     LPDIRECT3DDEVICE9 p_d3ddev;
741     D3DPRESENT_PARAMETERS d3dpp;
742     HRESULT hr;
743
744     if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
745         return VLC_EGENERIC;
746
747     // Create the D3DDevice
748     hr = IDirect3D9_CreateDevice(p_d3dobj, D3DADAPTER_DEFAULT,
749                                  D3DDEVTYPE_HAL, p_vout->p_sys->hvideownd,
750                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING|
751                                  D3DCREATE_MULTITHREADED,
752                                  &d3dpp, &p_d3ddev );
753     if( FAILED(hr) )
754     {
755        msg_Err(p_vout, "Could not create the D3D device! (hr=0x%lX)", hr);
756        return VLC_EGENERIC;
757     }
758     p_vout->p_sys->p_d3ddev = p_d3ddev;
759
760     msg_Dbg( p_vout, "Direct3D device adapter successfully initialized" );
761     return VLC_SUCCESS;
762 }
763
764 /*****************************************************************************
765  * DirectD3DClose: release the Direct3D9 device
766  *****************************************************************************/
767 static void Direct3DVoutClose( vout_thread_t *p_vout )
768 {
769     if( p_vout->p_sys->p_d3ddev )
770     {
771        IDirect3DDevice9_Release(p_vout->p_sys->p_d3ddev);
772        p_vout->p_sys->p_d3ddev = NULL;
773     }
774
775     p_vout->p_sys->hmonitor = NULL;
776 }
777
778 /*****************************************************************************
779  * DirectD3DClose: reset the Direct3D9 device
780  *****************************************************************************
781  * All resources must be deallocated before the reset occur, they will be
782  * realllocated once the reset has been performed successfully
783  *****************************************************************************/
784 static int Direct3DVoutResetDevice( vout_thread_t *p_vout )
785 {
786     LPDIRECT3DDEVICE9       p_d3ddev = p_vout->p_sys->p_d3ddev;
787     D3DPRESENT_PARAMETERS   d3dpp;
788     HRESULT hr;
789
790     if( VLC_SUCCESS != Direct3DFillPresentationParameters(p_vout, &d3dpp) )
791         return VLC_EGENERIC;
792
793     // release all D3D objects
794     Direct3DVoutReleaseScene( p_vout );
795     Direct3DVoutReleasePictures( p_vout );
796
797     hr = IDirect3DDevice9_Reset(p_d3ddev, &d3dpp);
798     if( SUCCEEDED(hr) )
799     {
800         // re-create them
801         if( (VLC_SUCCESS != Direct3DVoutCreatePictures(p_vout, 1))
802          || (VLC_SUCCESS != Direct3DVoutCreateScene(p_vout)) )
803         {
804             msg_Dbg(p_vout, "%s failed !", __FUNCTION__);
805             return VLC_EGENERIC;
806         }
807     }
808     else {
809         msg_Err(p_vout, "%s failed ! (hr=%08lX)", __FUNCTION__, hr);
810         return VLC_EGENERIC;
811     }
812     return VLC_SUCCESS;
813 }
814
815 static D3DFORMAT Direct3DVoutSelectFormat( vout_thread_t *p_vout, D3DFORMAT target,
816     const D3DFORMAT *formats, size_t count)
817 {
818     LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
819     size_t c;
820
821     for( c=0; c<count; ++c )
822     {
823         HRESULT hr;
824         D3DFORMAT format = formats[c];
825         /* test whether device can create a surface of that format */
826         hr = IDirect3D9_CheckDeviceFormat(p_d3dobj, D3DADAPTER_DEFAULT,
827                 D3DDEVTYPE_HAL, target, 0, D3DRTYPE_SURFACE, format);
828         if( SUCCEEDED(hr) )
829         {
830             /* test whether device can perform color-conversion
831             ** from that format to target format
832             */
833             hr = IDirect3D9_CheckDeviceFormatConversion(p_d3dobj,
834                     D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
835                     format, target);
836         }
837         if( SUCCEEDED(hr) )
838         {
839             // found a compatible format
840             switch( format )
841             {
842                 case D3DFMT_UYVY:
843                     msg_Dbg( p_vout, "selected surface pixel format is UYVY");
844                     break;
845                 case D3DFMT_YUY2:
846                     msg_Dbg( p_vout, "selected surface pixel format is YUY2");
847                     break;
848                 case D3DFMT_X8R8G8B8:
849                     msg_Dbg( p_vout, "selected surface pixel format is X8R8G8B8");
850                     break;
851                 case D3DFMT_A8R8G8B8:
852                     msg_Dbg( p_vout, "selected surface pixel format is A8R8G8B8");
853                     break;
854                 case D3DFMT_R8G8B8:
855                     msg_Dbg( p_vout, "selected surface pixel format is R8G8B8");
856                     break;
857                 case D3DFMT_R5G6B5:
858                     msg_Dbg( p_vout, "selected surface pixel format is R5G6B5");
859                     break;
860                 case D3DFMT_X1R5G5B5:
861                     msg_Dbg( p_vout, "selected surface pixel format is X1R5G5B5");
862                     break;
863                 default:
864                     msg_Dbg( p_vout, "selected surface pixel format is 0x%0X", format);
865                     break;
866             }
867             return format;
868         }
869         else if( D3DERR_NOTAVAILABLE != hr )
870         {
871             msg_Err( p_vout, "Could not query adapter supported formats. (hr=0x%lX)", hr);
872             break;
873         }
874     }
875     return D3DFMT_UNKNOWN;
876 }
877
878 static D3DFORMAT Direct3DVoutFindFormat(vout_thread_t *p_vout, int i_chroma, D3DFORMAT target)
879 {
880     //if( p_vout->p_sys->b_hw_yuv && ! _got_vista_or_above )
881     if( p_vout->p_sys->b_hw_yuv )
882     {
883     /* it sounds like vista does not support YUV surfaces at all */
884         switch( i_chroma )
885         {
886             case VLC_FOURCC('U','Y','V','Y'):
887             case VLC_FOURCC('U','Y','N','V'):
888             case VLC_FOURCC('Y','4','2','2'):
889             {
890                 static const D3DFORMAT formats[] =
891                     { D3DFMT_UYVY, D3DFMT_YUY2, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
892                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
893             }
894             case VLC_FOURCC('I','4','2','0'):
895             case VLC_FOURCC('I','4','2','2'):
896             case VLC_FOURCC('Y','V','1','2'):
897             {
898                 /* typically 3D textures don't support planar format
899                 ** fallback to packed version and use CPU for the conversion
900                 */
901                 static const D3DFORMAT formats[] =
902                     { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
903                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
904             }
905             case VLC_FOURCC('Y','U','Y','2'):
906             case VLC_FOURCC('Y','U','N','V'):
907             {
908                 static const D3DFORMAT formats[] =
909                     { D3DFMT_YUY2, D3DFMT_UYVY, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
910                 return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
911             }
912         }
913     }
914
915     switch( i_chroma )
916     {
917         case VLC_FOURCC('R', 'V', '1', '5'):
918         {
919             static const D3DFORMAT formats[] =
920                 { D3DFMT_X1R5G5B5 };
921             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
922         }
923         case VLC_FOURCC('R', 'V', '1', '6'):
924         {
925             static const D3DFORMAT formats[] =
926                 { D3DFMT_R5G6B5 };
927             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
928         }
929         case VLC_FOURCC('R', 'V', '2', '4'):
930         {
931             static const D3DFORMAT formats[] =
932                 { D3DFMT_R8G8B8, D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8 };
933             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
934         }
935         case VLC_FOURCC('R', 'V', '3', '2'):
936         {
937             static const D3DFORMAT formats[] =
938                 { D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8 };
939             return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
940         }
941         default:
942         {
943             /* use display default format */
944             LPDIRECT3D9 p_d3dobj = p_vout->p_sys->p_d3dobj;
945             D3DDISPLAYMODE d3ddm;
946
947             HRESULT hr = IDirect3D9_GetAdapterDisplayMode(p_d3dobj, D3DADAPTER_DEFAULT, &d3ddm );
948             if( SUCCEEDED(hr))
949             {
950                 /*
951                 ** some professional cards could use some advanced pixel format as default,
952                 ** make sure we stick with chromas that we can handle internally
953                 */
954                 switch( d3ddm.Format )
955                 {
956                     case D3DFMT_R8G8B8:
957                     case D3DFMT_X8R8G8B8:
958                     case D3DFMT_A8R8G8B8:
959                     case D3DFMT_R5G6B5:
960                     case D3DFMT_X1R5G5B5:
961                         msg_Dbg( p_vout, "defaulting to adapter pixel format");
962                         return Direct3DVoutSelectFormat(p_vout, target, &d3ddm.Format, 1);
963                     default:
964                     {
965                         /* if we fall here, that probably means that we need to render some YUV format */
966                         static const D3DFORMAT formats[] =
967                             { D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, D3DFMT_X1R5G5B5 };
968                         msg_Dbg( p_vout, "defaulting to built-in pixel format");
969                         return Direct3DVoutSelectFormat(p_vout, target, formats, sizeof(formats)/sizeof(D3DFORMAT));
970                     }
971                 }
972             }
973         }
974     }
975     return D3DFMT_UNKNOWN;
976 }
977
978 static int Direct3DVoutSetOutputFormat(vout_thread_t *p_vout, D3DFORMAT format)
979 {
980     switch( format )
981     {
982         case D3DFMT_YUY2:
983             p_vout->output.i_chroma = VLC_FOURCC('Y', 'U', 'Y', '2');
984             break;
985         case D3DFMT_UYVY:
986             p_vout->output.i_chroma = VLC_FOURCC('U', 'Y', 'V', 'Y');
987             break;
988         case D3DFMT_R8G8B8:
989             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '2', '4');
990             p_vout->output.i_rmask = 0xff0000;
991             p_vout->output.i_gmask = 0x00ff00;
992             p_vout->output.i_bmask = 0x0000ff;
993             break;
994         case D3DFMT_X8R8G8B8:
995         case D3DFMT_A8R8G8B8:
996             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '3', '2');
997             p_vout->output.i_rmask = 0x00ff0000;
998             p_vout->output.i_gmask = 0x0000ff00;
999             p_vout->output.i_bmask = 0x000000ff;
1000             break;
1001         case D3DFMT_R5G6B5:
1002             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '1', '6');
1003             p_vout->output.i_rmask = (0x1fL)<<11;
1004             p_vout->output.i_gmask = (0x3fL)<<5;
1005             p_vout->output.i_bmask = (0x1fL)<<0;
1006             break;
1007         case D3DFMT_X1R5G5B5:
1008             p_vout->output.i_chroma = VLC_FOURCC('R', 'V', '1', '5');
1009             p_vout->output.i_rmask = (0x1fL)<<10;
1010             p_vout->output.i_gmask = (0x1fL)<<5;
1011             p_vout->output.i_bmask = (0x1fL)<<0;
1012             break;
1013         default:
1014             return VLC_EGENERIC;
1015     }
1016     return VLC_SUCCESS;
1017 }
1018
1019 /*****************************************************************************
1020  * Direct3DVoutCreatePictures: allocate a vector of identical pictures
1021  *****************************************************************************
1022  * Each picture has an associated offscreen surface in video memory
1023  * depending on hardware capabilities the picture chroma will be as close
1024  * as possible to the orginal render chroma to reduce CPU conversion overhead
1025  * and delegate this work to video card GPU
1026  *****************************************************************************/
1027 static int Direct3DVoutCreatePictures( vout_thread_t *p_vout, size_t i_num_pics )
1028 {
1029     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
1030     D3DFORMAT               format;
1031     HRESULT hr;
1032     size_t c;
1033     // if vout is already running, use current chroma, otherwise choose from upstream
1034     int i_chroma = p_vout->output.i_chroma ? p_vout->output.i_chroma
1035                                            : p_vout->render.i_chroma;
1036
1037     I_OUTPUTPICTURES = 0;
1038
1039     /*
1040     ** find the appropriate D3DFORMAT for the render chroma, the format will be the closest to
1041     ** the requested chroma which is usable by the hardware in an offscreen surface, as they
1042     ** typically support more formats than textures
1043     */
1044     format = Direct3DVoutFindFormat(p_vout, i_chroma, p_vout->p_sys->bbFormat);
1045     if( VLC_SUCCESS != Direct3DVoutSetOutputFormat(p_vout, format) )
1046     {
1047         msg_Err(p_vout, "surface pixel format is not supported.");
1048         return VLC_EGENERIC;
1049     }
1050
1051     for( c=0; c<i_num_pics; )
1052     {
1053
1054         LPDIRECT3DSURFACE9 p_d3dsurf;
1055         picture_t *p_pic = p_vout->p_picture+c;
1056
1057         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(p_d3ddev,
1058                 p_vout->render.i_width,
1059                 p_vout->render.i_height,
1060                 format,
1061                 D3DPOOL_DEFAULT,
1062                 &p_d3dsurf,
1063                 NULL);
1064         if( FAILED(hr) )
1065         {
1066             msg_Err(p_vout, "Failed to create picture surface. (hr=0x%lx)", hr);
1067             Direct3DVoutReleasePictures(p_vout);
1068             return VLC_EGENERIC;
1069         }
1070
1071         /* fill surface with black color */
1072         IDirect3DDevice9_ColorFill(p_d3ddev, p_d3dsurf, NULL, D3DCOLOR_ARGB(0xFF, 0, 0, 0) );
1073
1074         /* assign surface to internal structure */
1075         p_pic->p_sys = (void *)p_d3dsurf;
1076
1077         /* Now that we've got our direct-buffer, we can finish filling in the
1078          * picture_t structures */
1079         switch( p_vout->output.i_chroma )
1080         {
1081             case VLC_FOURCC('R','G','B','2'):
1082                 p_pic->p->i_lines = p_vout->output.i_height;
1083                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1084                 p_pic->p->i_pixel_pitch = 1;
1085                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1086                     p_pic->p->i_pixel_pitch;
1087                 p_pic->i_planes = 1;
1088             break;
1089             case VLC_FOURCC('R','V','1','5'):
1090             case VLC_FOURCC('R','V','1','6'):
1091                 p_pic->p->i_lines = p_vout->output.i_height;
1092                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1093                 p_pic->p->i_pixel_pitch = 2;
1094                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1095                     p_pic->p->i_pixel_pitch;
1096                 p_pic->i_planes = 1;
1097             break;
1098             case VLC_FOURCC('R','V','2','4'):
1099                 p_pic->p->i_lines = p_vout->output.i_height;
1100                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1101                 p_pic->p->i_pixel_pitch = 3;
1102                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1103                     p_pic->p->i_pixel_pitch;
1104                 p_pic->i_planes = 1;
1105             break;
1106             case VLC_FOURCC('R','V','3','2'):
1107                 p_pic->p->i_lines = p_vout->output.i_height;
1108                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1109                 p_pic->p->i_pixel_pitch = 4;
1110                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1111                     p_pic->p->i_pixel_pitch;
1112                 p_pic->i_planes = 1;
1113                 break;
1114             case VLC_FOURCC('U','Y','V','Y'):
1115             case VLC_FOURCC('Y','U','Y','2'):
1116                 p_pic->p->i_lines = p_vout->output.i_height;
1117                 p_pic->p->i_visible_lines = p_vout->output.i_height;
1118                 p_pic->p->i_pixel_pitch = 2;
1119                 p_pic->p->i_visible_pitch = p_vout->output.i_width *
1120                     p_pic->p->i_pixel_pitch;
1121                 p_pic->i_planes = 1;
1122                 break;
1123             default:
1124                 Direct3DVoutReleasePictures(p_vout);
1125                 return VLC_EGENERIC;
1126         }
1127         p_pic->i_status = DESTROYED_PICTURE;
1128         p_pic->i_type   = DIRECT_PICTURE;
1129         p_pic->b_slow   = true;
1130         p_pic->pf_lock  = Direct3DVoutLockSurface;
1131         p_pic->pf_unlock = Direct3DVoutUnlockSurface;
1132         PP_OUTPUTPICTURE[c] = p_pic;
1133
1134         I_OUTPUTPICTURES = ++c;
1135     }
1136
1137     msg_Dbg( p_vout, "%u Direct3D pictures created successfully", c );
1138
1139     return VLC_SUCCESS;
1140 }
1141
1142 /*****************************************************************************
1143  * Direct3DVoutReleasePictures: destroy a picture vector
1144  *****************************************************************************
1145  * release all video resources used for pictures
1146  *****************************************************************************/
1147 static void Direct3DVoutReleasePictures( vout_thread_t *p_vout)
1148 {
1149     size_t i_num_pics = I_OUTPUTPICTURES;
1150     size_t c;
1151     for( c=0; c<i_num_pics; ++c )
1152     {
1153         picture_t *p_pic = p_vout->p_picture+c;
1154         if( p_pic->p_sys )
1155         {
1156             LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1157
1158             p_pic->p_sys = NULL;
1159
1160             if( p_d3dsurf )
1161             {
1162                 IDirect3DSurface9_Release(p_d3dsurf);
1163             }
1164         }
1165     }
1166     msg_Dbg( p_vout, "%u Direct3D pictures released.", c);
1167
1168     I_OUTPUTPICTURES = 0;
1169 }
1170
1171 /*****************************************************************************
1172  * Direct3DVoutLockSurface: Lock surface and get picture data pointer
1173  *****************************************************************************
1174  * This function locks a surface and get the surface descriptor which amongst
1175  * other things has the pointer to the picture data.
1176  *****************************************************************************/
1177 static int Direct3DVoutLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1178 {
1179     HRESULT hr;
1180     D3DLOCKED_RECT d3drect;
1181     LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1182
1183     if( NULL == p_d3dsurf )
1184         return VLC_EGENERIC;
1185
1186     /* Lock the surface to get a valid pointer to the picture buffer */
1187     hr = IDirect3DSurface9_LockRect(p_d3dsurf, &d3drect, NULL, 0);
1188     if( FAILED(hr) )
1189     {
1190         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1191         return VLC_EGENERIC;
1192     }
1193
1194     /* fill in buffer info in first plane */
1195     p_pic->p->p_pixels = d3drect.pBits;
1196     p_pic->p->i_pitch  = d3drect.Pitch;
1197
1198     return VLC_SUCCESS;
1199 }
1200
1201 /*****************************************************************************
1202  * Direct3DVoutUnlockSurface: Unlock a surface locked by Direct3DLockSurface().
1203  *****************************************************************************/
1204 static int Direct3DVoutUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
1205 {
1206     HRESULT hr;
1207     LPDIRECT3DSURFACE9 p_d3dsurf = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1208
1209     if( NULL == p_d3dsurf )
1210         return VLC_EGENERIC;
1211
1212     /* Unlock the Surface */
1213     hr = IDirect3DSurface9_UnlockRect(p_d3dsurf);
1214     if( FAILED(hr) )
1215     {
1216         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1217         return VLC_EGENERIC;
1218     }
1219     return VLC_SUCCESS;
1220 }
1221
1222 /*****************************************************************************
1223  * Direct3DVoutCreateScene: allocate and initialize a 3D scene
1224  *****************************************************************************
1225  * for advanced blending/filtering a texture needs be used in a 3D scene.
1226  *****************************************************************************/
1227
1228 static int Direct3DVoutCreateScene( vout_thread_t *p_vout )
1229 {
1230     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
1231     LPDIRECT3DTEXTURE9      p_d3dtex;
1232     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1233
1234     HRESULT hr;
1235
1236     /*
1237     ** Create a texture for use when rendering a scene
1238     ** for performance reason, texture format is identical to backbuffer
1239     ** which would usually be a RGB format
1240     */
1241     hr = IDirect3DDevice9_CreateTexture(p_d3ddev,
1242             p_vout->render.i_width,
1243             p_vout->render.i_height,
1244             1,
1245             D3DUSAGE_RENDERTARGET,
1246             p_vout->p_sys->bbFormat,
1247             D3DPOOL_DEFAULT,
1248             &p_d3dtex,
1249             NULL);
1250     if( FAILED(hr))
1251     {
1252         msg_Err(p_vout, "Failed to create texture. (hr=0x%lx)", hr);
1253         return VLC_EGENERIC;
1254     }
1255
1256     /*
1257     ** Create a vertex buffer for use when rendering scene
1258     */
1259     hr = IDirect3DDevice9_CreateVertexBuffer(p_d3ddev,
1260             sizeof(CUSTOMVERTEX)*4,
1261             D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
1262             D3DFVF_CUSTOMVERTEX,
1263             D3DPOOL_DEFAULT,
1264             &p_d3dvtc,
1265             NULL);
1266     if( FAILED(hr) )
1267     {
1268         msg_Err(p_vout, "Failed to create vertex buffer. (hr=0x%lx)", hr);
1269             IDirect3DTexture9_Release(p_d3dtex);
1270         return VLC_EGENERIC;
1271     }
1272
1273     p_vout->p_sys->p_d3dtex = p_d3dtex;
1274     p_vout->p_sys->p_d3dvtc = p_d3dvtc;
1275
1276     // Texture coordinates outside the range [0.0, 1.0] are set
1277     // to the texture color at 0.0 or 1.0, respectively.
1278     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
1279     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
1280
1281     // Set linear filtering quality
1282     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
1283     IDirect3DDevice9_SetSamplerState(p_d3ddev, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
1284
1285     // set maximum ambient light
1286     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));
1287
1288     // Turn off culling
1289     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_CULLMODE, D3DCULL_NONE);
1290
1291     // Turn off the zbuffer
1292     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ZENABLE, D3DZB_FALSE);
1293
1294     // Turn off lights
1295     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_LIGHTING, FALSE);
1296
1297     // Enable dithering
1298     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DITHERENABLE, TRUE);
1299
1300     // disable stencil
1301     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_STENCILENABLE, FALSE);
1302
1303     // manage blending
1304     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHABLENDENABLE, TRUE);
1305     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
1306     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
1307     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHATESTENABLE,TRUE);
1308     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAREF, 0x10);
1309     IDirect3DDevice9_SetRenderState(p_d3ddev, D3DRS_ALPHAFUNC,D3DCMP_GREATER);
1310
1311     // Set texture states
1312     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLOROP,D3DTOP_MODULATE);
1313     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
1314     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_COLORARG2,D3DTA_DIFFUSE);
1315
1316     // turn off alpha operation
1317     IDirect3DDevice9_SetTextureStageState(p_d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
1318
1319     msg_Dbg( p_vout, "Direct3D scene created successfully");
1320
1321     return VLC_SUCCESS;
1322 }
1323
1324 /*****************************************************************************
1325  * Direct3DVoutReleaseScene
1326  *****************************************************************************/
1327 static void Direct3DVoutReleaseScene( vout_thread_t *p_vout )
1328 {
1329     LPDIRECT3DTEXTURE9      p_d3dtex = p_vout->p_sys->p_d3dtex;
1330     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc = p_vout->p_sys->p_d3dvtc;
1331
1332     if( p_d3dvtc )
1333     {
1334         IDirect3DVertexBuffer9_Release(p_d3dvtc);
1335         p_vout->p_sys->p_d3dvtc = NULL;
1336     }
1337
1338     if( p_d3dtex )
1339     {
1340         IDirect3DTexture9_Release(p_d3dtex);
1341         p_vout->p_sys->p_d3dtex = NULL;
1342     }
1343     msg_Dbg( p_vout, "Direct3D scene released successfully");
1344 }
1345
1346 /*****************************************************************************
1347  * Render: copy picture surface into a texture and render into a scene
1348  *****************************************************************************
1349  * This function is intented for higher end 3D cards, with pixel shader support
1350  * and at least 64 MB of video RAM.
1351  *****************************************************************************/
1352 static void Direct3DVoutRenderScene( vout_thread_t *p_vout, picture_t *p_pic )
1353 {
1354     LPDIRECT3DDEVICE9       p_d3ddev  = p_vout->p_sys->p_d3ddev;
1355     LPDIRECT3DTEXTURE9      p_d3dtex;
1356     LPDIRECT3DVERTEXBUFFER9 p_d3dvtc;
1357     LPDIRECT3DSURFACE9      p_d3dsrc, p_d3ddest;
1358     CUSTOMVERTEX            *p_vertices;
1359     HRESULT hr;
1360     float f_width, f_height;
1361
1362     // check if device is still available
1363     hr = IDirect3DDevice9_TestCooperativeLevel(p_d3ddev);
1364     if( FAILED(hr) )
1365     {
1366         if( (D3DERR_DEVICENOTRESET != hr)
1367          || (VLC_SUCCESS != Direct3DVoutResetDevice(p_vout)) )
1368         {
1369             // device is not usable at present (lost device, out of video mem ?)
1370             return;
1371         }
1372     }
1373     p_d3dtex  = p_vout->p_sys->p_d3dtex;
1374     p_d3dvtc  = p_vout->p_sys->p_d3dvtc;
1375
1376     /* Clear the backbuffer and the zbuffer */
1377     hr = IDirect3DDevice9_Clear( p_d3ddev, 0, NULL, D3DCLEAR_TARGET,
1378                               D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );
1379     if( FAILED(hr) )
1380     {
1381         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1382         return;
1383     }
1384
1385     /*  retrieve picture surface */
1386     p_d3dsrc = (LPDIRECT3DSURFACE9)p_pic->p_sys;
1387     if( NULL == p_d3dsrc )
1388     {
1389         msg_Dbg( p_vout, "no surface to render ?");
1390         return;
1391     }
1392
1393     /* retrieve texture top-level surface */
1394     hr = IDirect3DTexture9_GetSurfaceLevel(p_d3dtex, 0, &p_d3ddest);
1395     if( FAILED(hr) )
1396     {
1397         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1398         return;
1399     }
1400
1401     /* Copy picture surface into texture surface, color space conversion happens here */
1402     hr = IDirect3DDevice9_StretchRect(p_d3ddev, p_d3dsrc, NULL, p_d3ddest, NULL, D3DTEXF_NONE);
1403     IDirect3DSurface9_Release(p_d3ddest);
1404     if( FAILED(hr) )
1405     {
1406         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1407         return;
1408     }
1409
1410     /* Update the vertex buffer */
1411     hr = IDirect3DVertexBuffer9_Lock(p_d3dvtc, 0, 0, (VOID **)(&p_vertices), D3DLOCK_DISCARD);
1412     if( FAILED(hr) )
1413     {
1414         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1415         return;
1416     }
1417
1418     /* Setup vertices */
1419     f_width  = (float)(p_vout->output.i_width);
1420     f_height = (float)(p_vout->output.i_height);
1421
1422     /* -0.5f is a "feature" of DirectX and it seems to apply to Direct3d also */
1423     /* http://www.sjbrown.co.uk/2003/05/01/fix-directx-rasterisation/ */
1424     p_vertices[0].x       = -0.5f;       // left
1425     p_vertices[0].y       = -0.5f;       // top
1426     p_vertices[0].z       = 0.0f;
1427     p_vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1428     p_vertices[0].rhw     = 1.0f;
1429     p_vertices[0].tu      = 0.0f;
1430     p_vertices[0].tv      = 0.0f;
1431
1432     p_vertices[1].x       = f_width - 0.5f;    // right
1433     p_vertices[1].y       = -0.5f;       // top
1434     p_vertices[1].z       = 0.0f;
1435     p_vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1436     p_vertices[1].rhw     = 1.0f;
1437     p_vertices[1].tu      = 1.0f;
1438     p_vertices[1].tv      = 0.0f;
1439
1440     p_vertices[2].x       = f_width - 0.5f;    // right
1441     p_vertices[2].y       = f_height - 0.5f;   // bottom
1442     p_vertices[2].z       = 0.0f;
1443     p_vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1444     p_vertices[2].rhw     = 1.0f;
1445     p_vertices[2].tu      = 1.0f;
1446     p_vertices[2].tv      = 1.0f;
1447
1448     p_vertices[3].x       = -0.5f;       // left
1449     p_vertices[3].y       = f_height - 0.5f;   // bottom
1450     p_vertices[3].z       = 0.0f;
1451     p_vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
1452     p_vertices[3].rhw     = 1.0f;
1453     p_vertices[3].tu      = 0.0f;
1454     p_vertices[3].tv      = 1.0f;
1455
1456     hr= IDirect3DVertexBuffer9_Unlock(p_d3dvtc);
1457     if( FAILED(hr) )
1458     {
1459         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1460         return;
1461     }
1462
1463     // Begin the scene
1464     hr = IDirect3DDevice9_BeginScene(p_d3ddev);
1465     if( FAILED(hr) )
1466     {
1467         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1468         return;
1469     }
1470
1471     // Setup our texture. Using textures introduces the texture stage states,
1472     // which govern how textures get blended together (in the case of multiple
1473     // textures) and lighting information. In this case, we are modulating
1474     // (blending) our texture with the diffuse color of the vertices.
1475     hr = IDirect3DDevice9_SetTexture(p_d3ddev, 0, (LPDIRECT3DBASETEXTURE9)p_d3dtex);
1476     if( FAILED(hr) )
1477     {
1478         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1479         IDirect3DDevice9_EndScene(p_d3ddev);
1480         return;
1481     }
1482
1483     // Render the vertex buffer contents
1484     hr = IDirect3DDevice9_SetStreamSource(p_d3ddev, 0, p_d3dvtc, 0, sizeof(CUSTOMVERTEX));
1485     if( FAILED(hr) )
1486     {
1487         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1488         IDirect3DDevice9_EndScene(p_d3ddev);
1489         return;
1490     }
1491
1492     // we use FVF instead of vertex shader
1493     hr = IDirect3DDevice9_SetFVF(p_d3ddev, D3DFVF_CUSTOMVERTEX);
1494     if( FAILED(hr) )
1495     {
1496         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1497         IDirect3DDevice9_EndScene(p_d3ddev);
1498         return;
1499     }
1500
1501     // draw rectangle
1502     hr = IDirect3DDevice9_DrawPrimitive(p_d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
1503     if( FAILED(hr) )
1504     {
1505         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1506         IDirect3DDevice9_EndScene(p_d3ddev);
1507         return;
1508     }
1509
1510     // End the scene
1511     hr = IDirect3DDevice9_EndScene(p_d3ddev);
1512     if( FAILED(hr) )
1513     {
1514         msg_Dbg( p_vout, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
1515         return;
1516     }
1517 }
1518