directx.c 77.3 KB
Newer Older
1
/*****************************************************************************
2
 * directx.c: Windows DirectDraw video output
3
 *****************************************************************************
4
 * Copyright (C) 2001-2009 the VideoLAN team
5
 * $Id$
6
 *
gbazin's avatar
gbazin committed
7
 * Authors: Gildas Bazin <gbazin@videolan.org>
gbazin's avatar
   
gbazin committed
8
 *
9
10
11
12
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
gbazin's avatar
   
gbazin committed
13
 *
14
15
16
17
18
19
20
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
 *****************************************************************************/

/*****************************************************************************
gbazin's avatar
   
gbazin committed
25
26
27
28
29
30
31
32
33
34
 * Preamble:
 *
 * This plugin will use YUV overlay if supported, using overlay will result in
 * the best video quality (hardware interpolation when rescaling the picture)
 * and the fastest display as it requires less processing.
 *
 * If YUV overlay is not supported this plugin will use RGB offscreen video
 * surfaces that will be blitted onto the primary surface (display) to
 * effectively display the pictures. This fallback method also enables us to
 * display video in window mode.
35
 *
36
 *****************************************************************************/
37

gbazin's avatar
   
gbazin committed
38
#include <errno.h>                                                 /* ENOMEM */
39

40
41
42
43
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

44
#include <vlc_common.h>
45
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
46
#include <vlc_vout.h>
47
#include <vlc_playlist.h>   /* needed for wallpaper */
gbazin's avatar
   
gbazin committed
48
49

#include <ddraw.h>
50
#include <commctrl.h>       /* ListView_(Get|Set)* */
gbazin's avatar
   
gbazin committed
51

52
53
54
#ifndef UNDER_CE
#   include <multimon.h>
#endif
55
56
#undef GetSystemMetrics

gbazin's avatar
   
gbazin committed
57
58
59
60
#ifndef MONITOR_DEFAULTTONEAREST
#   define MONITOR_DEFAULTTONEAREST 2
#endif

gbazin's avatar
   
gbazin committed
61
62
#include "vout.h"

63
64
65
66
67
68
69
70
71
72
73
74
75
/*****************************************************************************
 * picture_sys_t: direct buffer method descriptor
 *****************************************************************************
 * This structure is part of the picture descriptor, it describes the
 * DirectX specific properties of a direct buffer.
 *****************************************************************************/
struct picture_sys_t
{
    LPDIRECTDRAWSURFACE2 p_surface;
    LPDIRECTDRAWSURFACE2 p_front_surface;
    DDSURFACEDESC        ddsd;
};

gbazin's avatar
   
gbazin committed
76
77
78
79
80
81
/*****************************************************************************
 * DirectDraw GUIDs.
 * Defining them here allows us to get rid of the dxguid library during
 * the linking stage.
 *****************************************************************************/
#include <initguid.h>
damienf's avatar
damienf committed
82
83
#undef GUID_EXT
#define GUID_EXT
gbazin's avatar
   
gbazin committed
84
85
DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
86
87

/*****************************************************************************
gbazin's avatar
   
gbazin committed
88
 * Local prototypes.
89
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
90
91
static int  OpenVideo  ( vlc_object_t * );
static void CloseVideo ( vlc_object_t * );
92

gbazin's avatar
   
gbazin committed
93
94
95
96
static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t * );
static int  Manage    ( vout_thread_t * );
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 * );
gbazin's avatar
   
gbazin committed
99

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
100
static int  NewPictureVec  ( vout_thread_t *, picture_t * );
gbazin's avatar
   
gbazin committed
101
static void FreePictureVec ( vout_thread_t *, picture_t *, int );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
102
static int  UpdatePictureStruct( vout_thread_t *, picture_t * );
gbazin's avatar
   
gbazin committed
103
104
105
106
107
108
109
110
111
112
113

static int  DirectXInitDDraw      ( vout_thread_t *p_vout );
static void DirectXCloseDDraw     ( vout_thread_t *p_vout );
static int  DirectXCreateDisplay  ( vout_thread_t *p_vout );
static void DirectXCloseDisplay   ( vout_thread_t *p_vout );
static int  DirectXCreateSurface  ( vout_thread_t *p_vout,
                                    LPDIRECTDRAWSURFACE2 *, int, int, int );
static void DirectXCloseSurface   ( vout_thread_t *p_vout,
                                    LPDIRECTDRAWSURFACE2 );
static int  DirectXCreateClipper  ( vout_thread_t *p_vout );
static void DirectXGetDDrawCaps   ( vout_thread_t *p_vout );
gbazin's avatar
   
gbazin committed
114
115
static int  DirectXLockSurface    ( vout_thread_t *p_vout, picture_t *p_pic );
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

gbazin's avatar
   
gbazin committed
121
/* Object variables callbacks */
gbazin's avatar
   
gbazin committed
122
123
static int FindDevicesCallback( vlc_object_t *, char const *,
                                vlc_value_t, vlc_value_t, void * );
124
125
static int WallpaperCallback( vlc_object_t *, char const *,
                              vlc_value_t, vlc_value_t, void * );
gbazin's avatar
   
gbazin committed
126

127
128
129
130
131
132
133
134
BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
                                 LPTSTR psz_drivername, VOID* p_context,
                                 HMONITOR hmon );
                                 
BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
                                  LPTSTR psz_drivername, VOID* p_context,
                                  HMONITOR hmon );
                                  
135
136
137
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
138
#define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
139
140
141
#define HW_YUV_LONGTEXT N_( \
    "Try to use hardware acceleration for YUV->RGB conversions. " \
    "This option doesn't have any effect when using overlays." )
zorglub's avatar
zorglub committed
142

Christophe Massiot's avatar
Christophe Massiot committed
143
#define SYSMEM_TEXT N_("Use video buffers in system memory")
144
145
146
147
148
#define SYSMEM_LONGTEXT N_( \
    "Create video buffers in system memory instead of video memory. This " \
    "isn't recommended as usually using video memory allows to benefit from " \
    "more hardware acceleration (like rescaling or YUV->RGB conversions). " \
    "This option doesn't have any effect when using overlays." )
zorglub's avatar
zorglub committed
149

Christophe Massiot's avatar
Christophe Massiot committed
150
#define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
gbazin's avatar
   
gbazin committed
151
#define TRIPLEBUF_LONGTEXT N_( \
152
    "Try to use triple buffering when using YUV overlays. That results in " \
gbazin's avatar
   
gbazin committed
153
    "much better video quality (no flickering)." )
zorglub's avatar
zorglub committed
154

gbazin's avatar
   
gbazin committed
155
#define DEVICE_TEXT N_("Name of desired display device")
156
157
158
159
#define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " \
    "specify the Windows device name of the display that you want the video " \
    "window to open on. For example, \"\\\\.\\DISPLAY1\" or " \
    "\"\\\\.\\DISPLAY2\"." )
gbazin's avatar
   
gbazin committed
160

161
162
163
164
165
166
#define WALLPAPER_TEXT N_("Enable wallpaper mode ")
#define WALLPAPER_LONGTEXT N_( \
    "The wallpaper mode allows you to display the video as the desktop " \
    "background. Note that this feature only works in overlay mode and " \
    "the desktop must not already have a wallpaper." )

167
168
static const char *const ppsz_dev[] = { "" };
static const char *const ppsz_dev_text[] = { N_("Default") };
169

170
171
172
173
vlc_module_begin ()
    set_shortname( "DirectX" )
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_VOUT )
174
    add_bool( "directx-hw-yuv", true, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
175
              true )
176
    add_bool( "directx-use-sysmem", false, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
177
              true )
178
    add_bool( "directx-3buffering", true, NULL, TRIPLEBUF_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
179
              TRIPLEBUF_LONGTEXT, true )
gbazin's avatar
   
gbazin committed
180
181

    add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182
                true )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
183
        change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback )
184
        change_action_add( FindDevicesCallback, N_("Refresh list") )
gbazin's avatar
   
gbazin committed
185

186
    add_bool( "directx-wallpaper", false, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
187
              true )
188

189
    set_description( N_("DirectX (DirectDraw) video output") )
190
191
192
    set_capability( "video output", 100 )
    add_shortcut( "directx" )
    set_callbacks( OpenVideo, CloseVideo )
193
194

    /* FIXME: Hack to avoid unregistering our window class */
195
196
    linked_with_a_crap_library_which_uses_atexit ()
vlc_module_end ()
197
198
199
200
201
202
203
204
205

#if 0 /* FIXME */
    /* check if we registered a window class because we need to
     * unregister it */
    WNDCLASS wndclass;
    if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
        UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
#endif

gbazin's avatar
   
gbazin committed
206
207
208
209
210
211
212
213
/*****************************************************************************
 * OpenVideo: allocate DirectX video thread output method
 *****************************************************************************
 * This function allocates and initialize the DirectX vout method.
 *****************************************************************************/
static int OpenVideo( vlc_object_t *p_this )
{
    vout_thread_t * p_vout = (vout_thread_t *)p_this;
gbazin's avatar
gbazin committed
214
    vlc_value_t val;
215
    HMODULE huser32;
gbazin's avatar
   
gbazin committed
216
217

    /* Allocate structure */
218
    p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
gbazin's avatar
   
gbazin committed
219
    if( p_vout->p_sys == NULL )
220
        return VLC_ENOMEM;
gbazin's avatar
   
gbazin committed
221
222
223
224
225
226

    /* Initialisations */
    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = Manage;
    p_vout->pf_render = NULL;
227
    p_vout->pf_display = FirstDisplay;
228
    p_vout->pf_control = Control;
gbazin's avatar
   
gbazin committed
229

230
231
232
233
    if( CommonInit( p_vout ) )
        goto error;

    /* */
gbazin's avatar
   
gbazin committed
234
235
236
237
    p_vout->p_sys->p_ddobject = NULL;
    p_vout->p_sys->p_display = NULL;
    p_vout->p_sys->p_current_surface = NULL;
    p_vout->p_sys->p_clipper = NULL;
238
    p_vout->p_sys->b_wallpaper = 0;
239
240
241
242
243
244

    /* Multimonitor stuff */
    p_vout->p_sys->hmonitor = NULL;
    p_vout->p_sys->p_display_driver = NULL;
    p_vout->p_sys->MonitorFromWindow = NULL;
    p_vout->p_sys->GetMonitorInfo = NULL;
245
    if( (huser32 = GetModuleHandle( _T("USER32") ) ) )
246
    {
247
        p_vout->p_sys->MonitorFromWindow = (HMONITOR (WINAPI *)( HWND, DWORD ))
248
            GetProcAddress( huser32, _T("MonitorFromWindow") );
249
        p_vout->p_sys->GetMonitorInfo =
250
            GetProcAddress( huser32, _T("GetMonitorInfoW") );
251
252
253
254
255
256
    }

    var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
    var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
gbazin's avatar
   
gbazin committed
257
    var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
gbazin's avatar
   
gbazin committed
258
259
260
261

    /* Initialise DirectDraw */
    if( DirectXInitDDraw( p_vout ) )
    {
262
        msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
gbazin's avatar
   
gbazin committed
263
264
265
266
267
268
        goto error;
    }

    /* Create the directx display */
    if( DirectXCreateDisplay( p_vout ) )
    {
269
        msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
gbazin's avatar
   
gbazin committed
270
271
272
        goto error;
    }

273
274
275
276
277
278
    /* Variable to indicate if the window should be on top of others */
    /* Trigger a callback right now */
    var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
    val.psz_string = _("Wallpaper");
    var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
    var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
279
    var_TriggerCallback( p_vout, "directx-wallpaper" );
280

281
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
282
283
284

 error:
    CloseVideo( VLC_OBJECT(p_vout) );
285
    return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
286
287
288
289
290
291
292
293
294
295
296
}

/*****************************************************************************
 * Init: initialize DirectX video thread output method
 *****************************************************************************
 * This function create the directx surfaces needed by the output thread.
 * It is called at the beginning of the thread.
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int i_chroma_backup;
297
298

    /* Get a few default parameters */
299
300
301
302
    p_vout->p_sys->b_using_overlay = var_GetBool( p_vout, "overlay" );
    p_vout->p_sys->b_use_sysmem = var_GetBool( p_vout, "directx-use-sysmem" );
    p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
    p_vout->p_sys->b_3buf_overlay = var_GetBool( p_vout, "directx-3buffering" );
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

    /* Initialise DirectDraw if not already done.
     * We do this here because on multi-monitor systems we may have to
     * re-create the directdraw surfaces. */
    if( !p_vout->p_sys->p_ddobject &&
        DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
    {
        msg_Err( p_vout, "cannot initialize DirectDraw" );
        return VLC_EGENERIC;
    }

    /* Create the directx display */
    if( !p_vout->p_sys->p_display &&
        DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
    {
        msg_Err( p_vout, "cannot initialize DirectDraw" );
        return VLC_EGENERIC;
    }
gbazin's avatar
   
gbazin committed
321
322
323
324
325
326
327

    /* Initialize the output structure.
     * Since DirectDraw can do rescaling for us, stick to the default
     * coordinates and aspect. */
    p_vout->output.i_width  = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;
    p_vout->output.i_aspect = p_vout->render.i_aspect;
328
    p_vout->fmt_out = p_vout->fmt_in;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
329
    UpdateRects( p_vout, true );
gbazin's avatar
   
gbazin committed
330
331
332
333
334
335
336
337
338

#define MAX_DIRECTBUFFERS 1
    /* Right now we use only 1 directbuffer because we don't want the
     * video decoder to decode directly into direct buffers as they are
     * created into video memory and video memory is _really_ slow */

    /* Choose the chroma we will try first. */
    switch( p_vout->render.i_chroma )
    {
339
340
        case VLC_CODEC_YUYV:
            p_vout->output.i_chroma = VLC_CODEC_YUYV;
gbazin's avatar
   
gbazin committed
341
            break;
342
343
        case VLC_CODEC_UYVY:
            p_vout->output.i_chroma = VLC_CODEC_UYVY;
gbazin's avatar
   
gbazin committed
344
            break;
345
346
        case VLC_CODEC_YVYU:
            p_vout->output.i_chroma = VLC_CODEC_YVYU;
gbazin's avatar
   
gbazin committed
347
            break;
348
349
350
        case VLC_CODEC_I420:
            p_vout->output.i_chroma = VLC_CODEC_I420;
            break;
gbazin's avatar
   
gbazin committed
351
        default:
352
353
354
            msg_Dbg( p_vout, "use default chroma YV12 for render " \
                             "chroma (%4.4s)",
                             (char *)&p_vout->render.i_chroma);
355
            p_vout->output.i_chroma = VLC_CODEC_YV12;
gbazin's avatar
   
gbazin committed
356
357
358
            break;
    }

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
359
    NewPictureVec( p_vout, p_vout->p_picture );
gbazin's avatar
   
gbazin committed
360
361
362
363
364
365

    i_chroma_backup = p_vout->output.i_chroma;

    if( !I_OUTPUTPICTURES )
    {
        /* hmmm, it didn't work! Let's try commonly supported chromas */
366
        if( p_vout->output.i_chroma != VLC_CODEC_I420 )
gbazin's avatar
   
gbazin committed
367
        {
368
            p_vout->output.i_chroma = VLC_CODEC_YV12;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
369
            NewPictureVec( p_vout, p_vout->p_picture );
gbazin's avatar
   
gbazin committed
370
        }
gbazin's avatar
   
gbazin committed
371
372
        if( !I_OUTPUTPICTURES )
        {
gbazin's avatar
   
gbazin committed
373
            /* hmmm, it still didn't work! Let's try another one */
374
            p_vout->output.i_chroma = VLC_CODEC_YUYV;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
375
            NewPictureVec( p_vout, p_vout->p_picture );
gbazin's avatar
   
gbazin committed
376
377
378
379
380
381
382
383
        }
    }

    if( !I_OUTPUTPICTURES )
    {
        /* If it still didn't work then don't try to use an overlay */
        p_vout->output.i_chroma = i_chroma_backup;
        p_vout->p_sys->b_using_overlay = 0;
basos G's avatar
basos G committed
384
        msg_Warn( p_vout, "Could not initialize directx overlay" ) ;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
385
        NewPictureVec( p_vout, p_vout->p_picture );
gbazin's avatar
   
gbazin committed
386
387
388
    }

    /* Change the window title bar text */
gbazin's avatar
gbazin committed
389
    PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
gbazin's avatar
   
gbazin committed
390

391
    p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
392
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
393
394
395
396
397
398
399
400
401
402
403
}

/*****************************************************************************
 * End: terminate Sys video thread output method
 *****************************************************************************
 * Terminate an output method created by Create.
 * It is called at the end of the thread.
 *****************************************************************************/
static void End( vout_thread_t *p_vout )
{
    FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
404
405
406
407

    DirectXCloseDisplay( p_vout );
    DirectXCloseDDraw( p_vout );

gbazin's avatar
   
gbazin committed
408
409
410
411
412
413
414
415
416
    return;
}

/*****************************************************************************
 * CloseVideo: destroy Sys video thread output method
 *****************************************************************************
 * Terminate an output method created by Create
 *****************************************************************************/
static void CloseVideo( vlc_object_t *p_this )
417
{
gbazin's avatar
   
gbazin committed
418
    vout_thread_t * p_vout = (vout_thread_t *)p_this;
419

420
    /* Make sure the wallpaper is restored */
ivoire's avatar
ivoire committed
421
    var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
422
    SwitchWallpaperMode( p_vout, false );
423

424
    CommonClean( p_vout );
425

ivoire's avatar
ivoire committed
426
    free( p_vout->p_sys );
gbazin's avatar
   
gbazin committed
427
428
429
430
431
432
}

/*****************************************************************************
 * Manage: handle Sys events
 *****************************************************************************
 * This function should be called regularly by the video output thread.
Sam Hocevar's avatar
Sam Hocevar committed
433
 * It returns a non null value if an error occurred.
gbazin's avatar
   
gbazin committed
434
435
436
 *****************************************************************************/
static int Manage( vout_thread_t *p_vout )
{
437
438
    /* If we do not control our window, we check for geometry changes
     * ourselves because the parent might not send us its events. */
gbazin's avatar
gbazin committed
439
    vlc_mutex_lock( &p_vout->p_sys->lock );
440
    if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
441
    {
gbazin's avatar
gbazin committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
        RECT rect_parent;
        POINT point;

        vlc_mutex_unlock( &p_vout->p_sys->lock );

        GetClientRect( p_vout->p_sys->hparent, &rect_parent );
        point.x = point.y = 0;
        ClientToScreen( p_vout->p_sys->hparent, &point );
        OffsetRect( &rect_parent, point.x, point.y );

        if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
        {
            p_vout->p_sys->rect_parent = rect_parent;

            /* This one is to force the update even if only
457
             * the position has changed */
gbazin's avatar
gbazin committed
458
459
460
461
462
463
464
465
466
467
468
469
            SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
                          rect_parent.right - rect_parent.left,
                          rect_parent.bottom - rect_parent.top, 0 );

            SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
                          rect_parent.right - rect_parent.left,
                          rect_parent.bottom - rect_parent.top, 0 );
        }
    }
    else
    {
        vlc_mutex_unlock( &p_vout->p_sys->lock );
470
471
    }

gbazin's avatar
   
gbazin committed
472
    /*
473
     * Position Change
gbazin's avatar
   
gbazin committed
474
     */
475
    if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
gbazin's avatar
   
gbazin committed
476
    {
477
478
        p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;

479
480
481
482
483
484
485
486
487
        /* Check if we are still on the same monitor */
        if( p_vout->p_sys->MonitorFromWindow &&
            p_vout->p_sys->hmonitor !=
                p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
                                                  MONITOR_DEFAULTTONEAREST ) )
        {
            /* This will force the vout core to recreate the picture buffers */
            p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
        }
gbazin's avatar
   
gbazin committed
488
489
    }

490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
    /* autoscale toggle */
    if( p_vout->i_changes & VOUT_SCALE_CHANGE )
    {
        p_vout->i_changes &= ~VOUT_SCALE_CHANGE;

        p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
        p_vout->i_zoom = (int) ZOOM_FP_FACTOR;

        UpdateRects( p_vout, true );
    }

    /* scaling factor */
    if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
    {
        p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;

        p_vout->b_autoscale = false;
        p_vout->i_zoom =
            (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
        UpdateRects( p_vout, true );
    }

512
513
514
    /* Check for cropping / aspect changes */
    if( p_vout->i_changes & VOUT_CROP_CHANGE ||
        p_vout->i_changes & VOUT_ASPECT_CHANGE )
515
    {
516
517
518
        p_vout->i_changes &= ~VOUT_CROP_CHANGE;
        p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;

519
520
521
522
        p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
        p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
        p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
        p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
523
524
525
526
        p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
        p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
        p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
        p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
527
        UpdateRects( p_vout, true );
528
529
    }

530
531
532
533
534
    /* We used to call the Win32 PeekMessage function here to read the window
     * messages. But since window can stay blocked into this function for a
     * long time (for example when you move your window on the screen), I
     * decided to isolate PeekMessage in another thread. */

535
536
537
538
    if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
    {
        SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
        p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
539
        DirectDrawUpdateOverlay( p_vout );
540
541
    }

gbazin's avatar
   
gbazin committed
542
543
544
545
546
547
    /*
     * Fullscreen change
     */
    if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
        || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
    {
548
        Win32ToggleFullscreen( p_vout );
gbazin's avatar
   
gbazin committed
549

gbazin's avatar
   
gbazin committed
550
551
552
553
554
555
556
        p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
        p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
    }

    /*
     * Pointer change
     */
557
    if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
558
559
        (mdate() - p_vout->p_sys->i_lastmoved) >
            p_vout->p_sys->i_mouse_hide_timeout )
gbazin's avatar
   
gbazin committed
560
    {
gbazin's avatar
gbazin committed
561
        POINT point;
562
        HWND hwnd;
gbazin's avatar
gbazin committed
563
564
565

        /* Hide the cursor only if it is inside our window */
        GetCursorPos( &point );
566
567
        hwnd = WindowFromPoint(point);
        if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
568
569
570
        {
            PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
        }
gbazin's avatar
gbazin committed
571
572
573
574
        else
        {
            p_vout->p_sys->i_lastmoved = mdate();
        }
gbazin's avatar
   
gbazin committed
575
576
    }

577
578
579
    /*
     * "Always on top" status change
     */
gbazin's avatar
   
gbazin committed
580
    if( p_vout->p_sys->b_on_top_change )
581
    {
gbazin's avatar
   
gbazin committed
582
583
584
        vlc_value_t val;
        HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );

gbazin's avatar
   
gbazin committed
585
        var_Get( p_vout, "video-on-top", &val );
zorglub's avatar
zorglub committed
586

587
        /* Set the window on top if necessary */
gbazin's avatar
   
gbazin committed
588
589
        if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
                           & WS_EX_TOPMOST ) )
590
591
592
        {
            CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
                           MF_BYCOMMAND | MFS_CHECKED );
gbazin's avatar
   
gbazin committed
593
594
            SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
                          SWP_NOSIZE | SWP_NOMOVE );
595
        }
gbazin's avatar
   
gbazin committed
596
        else
597
        /* The window shouldn't be on top */
gbazin's avatar
   
gbazin committed
598
599
        if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
                           & WS_EX_TOPMOST ) )
600
601
602
        {
            CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
                           MF_BYCOMMAND | MFS_UNCHECKED );
gbazin's avatar
   
gbazin committed
603
            SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
604
605
                          SWP_NOSIZE | SWP_NOMOVE );
        }
gbazin's avatar
   
gbazin committed
606

607
        p_vout->p_sys->b_on_top_change = false;
608
609
    }

610
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
}

/*****************************************************************************
 * Display: displays previously rendered output
 *****************************************************************************
 * This function sends the currently rendered image to the display, wait until
 * it is displayed and switch the two rendering buffers, preparing next frame.
 *****************************************************************************/
static void Display( vout_thread_t *p_vout, picture_t *p_pic )
{
    HRESULT dxresult;

    if( (p_vout->p_sys->p_display == NULL) )
    {
zorglub's avatar
zorglub committed
625
        msg_Warn( p_vout, "no display!" );
gbazin's avatar
   
gbazin committed
626
627
628
        return;
    }

gbazin's avatar
   
gbazin committed
629
630
631
632
633
634
635
    /* Our surface can be lost so be sure to check this
     * and restore it if need be */
    if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
        == DDERR_SURFACELOST )
    {
        if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
            p_vout->p_sys->b_using_overlay )
636
            DirectDrawUpdateOverlay( p_vout );
gbazin's avatar
   
gbazin committed
637
638
    }

gbazin's avatar
   
gbazin committed
639
640
641
642
643
644
645
646
647
648
    if( !p_vout->p_sys->b_using_overlay )
    {
        DDBLTFX  ddbltfx;

        /* We ask for the "NOTEARING" option */
        memset( &ddbltfx, 0, sizeof(DDBLTFX) );
        ddbltfx.dwSize = sizeof(DDBLTFX);
        ddbltfx.dwDDFX = DDBLTFX_NOTEARING;

        /* Blit video surface to display */
649
650
651
652
653
        dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
                                            &p_vout->p_sys->rect_dest_clipped,
                                            p_pic->p_sys->p_surface,
                                            &p_vout->p_sys->rect_src_clipped,
                                            DDBLT_ASYNC, &ddbltfx );
gbazin's avatar
   
gbazin committed
654
655
        if( dxresult != DD_OK )
        {
gbazin's avatar
   
gbazin committed
656
            msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
gbazin's avatar
   
gbazin committed
657
658
659
660
661
662
663
            return;
        }
    }
    else /* using overlay */
    {
        /* Flip the overlay buffers if we are using back buffers */
        if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
664
        {
gbazin's avatar
   
gbazin committed
665
            return;
666
        }
gbazin's avatar
   
gbazin committed
667
668
669
670

        dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
                                             NULL, DDFLIP_WAIT );
        if( dxresult != DD_OK )
671
        {
gbazin's avatar
   
gbazin committed
672
            msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
673
        }
gbazin's avatar
   
gbazin committed
674

gbazin's avatar
   
gbazin committed
675
676
        /* set currently displayed pic */
        p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
gbazin's avatar
   
gbazin committed
677

gbazin's avatar
   
gbazin committed
678
679
        /* Lock surface to get all the required info */
        if( DirectXLockSurface( p_vout, p_pic ) )
gbazin's avatar
   
gbazin committed
680
681
        {
            /* AAARRGG */
gbazin's avatar
   
gbazin committed
682
            msg_Warn( p_vout, "cannot lock surface" );
gbazin's avatar
   
gbazin committed
683
684
            return;
        }
gbazin's avatar
   
gbazin committed
685
        DirectXUnlockSurface( p_vout, p_pic );
gbazin's avatar
   
gbazin committed
686
687
688
    }
}

689
690
/*
** this function is only used once when the first picture is received
691
692
** this function will show the video window once video is ready for
** display.
693
*/
694
695

static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
696
{
697
698
699
700
701
702
    /* get initial picture rendered on overlay surface */
    Display(p_vout, p_pic);

    IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
            DDWAITVB_BLOCKBEGIN, NULL);

703
704
    if( p_vout->p_sys->b_using_overlay )
    {
705
        HBRUSH brush = CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey );
706
        /* set the colorkey as the backgound brush for the video window */
707
        SetClassLongPtr( p_vout->p_sys->hvideownd, GCLP_HBRBACKGROUND, (LONG_PTR)brush );
708
709
    }
    /*
710
    ** Video window is initially hidden, show it now since we got a
711
712
    ** picture to show.
    */
713
    SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
714
715
716
717
718
719
        SWP_ASYNCWINDOWPOS|
        SWP_FRAMECHANGED|
        SWP_SHOWWINDOW|
        SWP_NOMOVE|
        SWP_NOSIZE|
        SWP_NOZORDER );
720
721
722
723
724

    /* use and restores proper display function for further pictures */
    p_vout->pf_display = Display;
}

gbazin's avatar
   
gbazin committed
725
726
/* following functions are local */

727
728
729
/*****************************************************************************
 * DirectXEnumCallback: Device enumeration
 *****************************************************************************
zorglub's avatar
zorglub committed
730
 * This callback function is called by DirectDraw once for each
731
732
733
734
735
736
737
 * available DirectDraw device.
 *****************************************************************************/
BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
                                 LPTSTR psz_drivername, VOID* p_context,
                                 HMONITOR hmon )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_context;
gbazin's avatar
   
gbazin committed
738
739
    vlc_value_t device;

740
741
    msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );

gbazin's avatar
   
gbazin committed
742
    if( hmon )
743
    {
744
745
746
        var_Get( p_vout, "directx-device", &device );

        if( ( !device.psz_string || !*device.psz_string ) &&
gbazin's avatar
   
gbazin committed
747
748
            hmon == p_vout->p_sys->hmonitor )
        {
ivoire's avatar
ivoire committed
749
            free( device.psz_string );
gbazin's avatar
   
gbazin committed
750
751
752
        }
        else if( strcmp( psz_drivername, device.psz_string ) == 0 )
        {
zorglub's avatar
zorglub committed
753
754
            MONITORINFO monitor_info;
            monitor_info.cbSize = sizeof( MONITORINFO );
gbazin's avatar
   
gbazin committed
755
756
757
758
759
760
761
762
763
764
765

            if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
            {
                RECT rect;

                /* Move window to the right screen */
                GetWindowRect( p_vout->p_sys->hwnd, &rect );
                if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
                {
                    rect.left = monitor_info.rcWork.left;
                    rect.top = monitor_info.rcWork.top;
766
                    msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
767
                             "position to %ld,%ld", rect.left, rect.top );
gbazin's avatar
   
gbazin committed
768
769
770
771
772
773
                    SetWindowPos( p_vout->p_sys->hwnd, NULL,
                                  rect.left, rect.top, 0, 0,
                                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
                }
            }

gbazin's avatar
   
gbazin committed
774
            p_vout->p_sys->hmonitor = hmon;
ivoire's avatar
ivoire committed
775
            free( device.psz_string );
776
777
778
        }
        else
        {
ivoire's avatar
ivoire committed
779
            free( device.psz_string );
780
            return TRUE; /* Keep enumerating */
gbazin's avatar
   
gbazin committed
781
782
        }

783
784
785
786
787
788
        msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
        p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
        if( p_vout->p_sys->p_display_driver )
            memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
    }

789
790
791
    return TRUE; /* Keep enumerating */
}

gbazin's avatar
   
gbazin committed
792
793
794
795
796
797
798
/*****************************************************************************
 * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
 *****************************************************************************
 * This function initialise and allocate resources for DirectDraw.
 *****************************************************************************/
static int DirectXInitDDraw( vout_thread_t *p_vout )
{
799
800
801
802
803
    HRESULT dxresult;
    HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
    HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
                                                DWORD );
    LPDIRECTDRAW p_ddobject;
gbazin's avatar
   
gbazin committed
804
805
806

    msg_Dbg( p_vout, "DirectXInitDDraw" );

807
    /* Load direct draw DLL */
808
    p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
gbazin's avatar
   
gbazin committed
809
810
811
812
813
    if( p_vout->p_sys->hddraw_dll == NULL )
    {
        msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
        goto error;
    }
814
815

    OurDirectDrawCreate =
816
817
      (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
                              _T("DirectDrawCreate") );
818
    if( OurDirectDrawCreate == NULL )
gbazin's avatar
   
gbazin committed
819
820
821
822
823
    {
        msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
        goto error;
    }

824
825
    OurDirectDrawEnumerateEx =
      (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
826
                              _T("DirectDrawEnumerateExW") );
827

828
    if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
829
    {
gbazin's avatar
   
gbazin committed
830
831
832
        vlc_value_t device;

        var_Get( p_vout, "directx-device", &device );
833
834
835
836
837
        if( device.psz_string )
        {
            msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
            free( device.psz_string );
        }
gbazin's avatar
   
gbazin committed
838

839
840
841
842
        p_vout->p_sys->hmonitor =
            p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
                                              MONITOR_DEFAULTTONEAREST );

843
        /* Enumerate displays */
zorglub's avatar
zorglub committed
844
        OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
845
846
847
                                  DDENUM_ATTACHEDSECONDARYDEVICES );
    }

gbazin's avatar
   
gbazin committed
848
    /* Initialize DirectDraw now */
849
850
    dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
                                    &p_ddobject, NULL );
gbazin's avatar
   
gbazin committed
851
852
853
854
855
856
857
858
    if( dxresult != DD_OK )
    {
        msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
        goto error;
    }

    /* Get the IDirectDraw2 interface */
    dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
859
                                        &p_vout->p_sys->p_ddobject );
gbazin's avatar
   
gbazin committed
860
861
862
863
864
865
866
867
868
869
870
    /* Release the unused interface */
    IDirectDraw_Release( p_ddobject );
    if( dxresult != DD_OK )
    {
        msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
        goto error;
    }

    /* Set DirectDraw Cooperative level, ie what control we want over Windows
     * display */
    dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
871
                                                 NULL, DDSCL_NORMAL );
gbazin's avatar
   
gbazin committed
872
873
874
875
876
877
    if( dxresult != DD_OK )
    {
        msg_Err( p_vout, "cannot set direct draw cooperative level" );
        goto error;
    }

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
    /* Get the size of the current display device */
    if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
    {
        MONITORINFO monitor_info;
        monitor_info.cbSize = sizeof( MONITORINFO );
        p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
                                       &monitor_info );
        p_vout->p_sys->rect_display = monitor_info.rcMonitor;
    }
    else
    {
        p_vout->p_sys->rect_display.left = 0;
        p_vout->p_sys->rect_display.top = 0;
        p_vout->p_sys->rect_display.right  = GetSystemMetrics(SM_CXSCREEN);
        p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
    }

895
    msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
gbazin's avatar
   
gbazin committed
896
897
898
899
             p_vout->p_sys->rect_display.left,
             p_vout->p_sys->rect_display.top,
             p_vout->p_sys->rect_display.right,
             p_vout->p_sys->rect_display.bottom );
900

gbazin's avatar
   
gbazin committed
901
902
903
904
    /* Probe the capabilities of the hardware */
    DirectXGetDDrawCaps( p_vout );

    msg_Dbg( p_vout, "End DirectXInitDDraw" );
905
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
906
907
908
909
910
911
912
913

 error:
    if( p_vout->p_sys->p_ddobject )
        IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
    if( p_vout->p_sys->hddraw_dll )
        FreeLibrary( p_vout->p_sys->hddraw_dll );
    p_vout->p_sys->hddraw_dll = NULL;
    p_vout->p_sys->p_ddobject = NULL;
914
    return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
}

/*****************************************************************************
 * DirectXCreateDisplay: create the DirectDraw display.
 *****************************************************************************
 * Create and initialize display according to preferences specified in the vout
 * thread fields.
 *****************************************************************************/
static int DirectXCreateDisplay( vout_thread_t *p_vout )
{
    HRESULT              dxresult;
    DDSURFACEDESC        ddsd;
    LPDIRECTDRAWSURFACE  p_display;

    msg_Dbg( p_vout, "DirectXCreateDisplay" );

    /* Now get the primary surface. This surface is what you actually see
     * on your screen */
    memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
    ddsd.dwSize = sizeof(DDSURFACEDESC);
    ddsd.dwFlags = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

    dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
939
                                           &ddsd, &p_display, NULL );
gbazin's avatar
   
gbazin committed
940
941
    if( dxresult != DD_OK )
    {
gbazin's avatar
   
gbazin committed
942
        msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
943
        return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
944
945
946
947
    }

    dxresult = IDirectDrawSurface_QueryInterface( p_display,
                                         &IID_IDirectDrawSurface2,
948
                                         &p_vout->p_sys->p_display );
gbazin's avatar
   
gbazin committed
949
950
951
952
    /* Release the old interface */
    IDirectDrawSurface_Release( p_display );
    if ( dxresult != DD_OK )
    {
953
        msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
gbazin's avatar
   
gbazin committed
954
                         "(error %li)", dxresult );
955
        return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
956
957
958
959
960
    }

    /* The clipper will be used only in non-overlay mode */
    DirectXCreateClipper( p_vout );

961
962
963
    /* Make sure the colorkey will be painted */
    p_vout->p_sys->i_colorkey = 1;
    p_vout->p_sys->i_rgb_colorkey =
964
        DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
gbazin's avatar
   
gbazin committed
965

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
966
    UpdateRects( p_vout, true );
gbazin's avatar
   
gbazin committed
967

968
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
}

/*****************************************************************************
 * DirectXCreateClipper: Create a clipper that will be used when blitting the
 *                       RGB surface to the main display.
 *****************************************************************************
 * This clipper prevents us to modify by mistake anything on the screen
 * which doesn't belong to our window. For example when a part of our video
 * window is hidden by another window.
 *****************************************************************************/
static int DirectXCreateClipper( vout_thread_t *p_vout )
{
    HRESULT dxresult;

    msg_Dbg( p_vout, "DirectXCreateClipper" );

    /* Create the clipper */
    dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
                                           &p_vout->p_sys->p_clipper, NULL );
    if( dxresult != DD_OK )
    {
gbazin's avatar
   
gbazin committed
990
        msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
gbazin's avatar
   
gbazin committed
991
992
993
        goto error;
    }

994
    /* Associate the clipper to the window */
995
996
    dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
                                           p_vout->p_sys->hvideownd );
gbazin's avatar
   
gbazin committed
997
998
    if( dxresult != DD_OK )
    {
gbazin's avatar
   
gbazin committed
999
        msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
1000
                          dxresult );
gbazin's avatar
   
gbazin committed
1001
1002
1003
1004
1005
1006
1007
1008
        goto error;
    }

    /* associate the clipper with the surface */
    dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
                                             p_vout->p_sys->p_clipper);
    if( dxresult != DD_OK )
    {
gbazin's avatar
   
gbazin committed
1009
        msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
1010
                          dxresult );
gbazin's avatar
   
gbazin committed
1011
        goto error;
1012
    }
gbazin's avatar
   
gbazin committed
1013

1014
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
1015
1016
1017

 error:
    if( p_vout->p_sys->p_clipper )
1018
    {
gbazin's avatar
   
gbazin committed
1019
        IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
1020
    }
gbazin's avatar
   
gbazin committed
1021
    p_vout->p_sys->p_clipper = NULL;
1022
    return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
}

/*****************************************************************************
 * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
 *****************************************************************************
 * The best method of display is with an YUV overlay because the YUV->RGB
 * conversion is done in hardware.
 * You can also create a plain RGB surface.
 * ( Maybe we could also try an RGB overlay surface, which could have hardware
 * scaling and which would also be faster in window mode because you don't
 * need to do any blitting to the main display...)
 *****************************************************************************/
static int DirectXCreateSurface( vout_thread_t *p_vout,
                                 LPDIRECTDRAWSURFACE2 *pp_surface_final,
                                 int i_chroma, int b_overlay,
                                 int i_backbuffers )
{
    HRESULT dxresult;
    LPDIRECTDRAWSURFACE p_surface;
    DDSURFACEDESC ddsd;

    /* Create the video surface */
    if( b_overlay )
    {
        /* Now try to create the YUV overlay surface.
         * This overlay will be displayed on top of the primary surface.
         * A color key is used to determine whether or not the overlay will be
         * displayed, ie the overlay will be displayed in place of the primary
         * surface wherever the primary surface will have this color.
         * The video window has been created with a background of this color so
         * the overlay will be only displayed on top of this window */

        memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
        ddsd.dwSize = sizeof(DDSURFACEDESC);
        ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
        ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
        ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
1060
        ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
gbazin's avatar
   
gbazin committed
1061
        ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
1062
        ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
gbazin's avatar
   
gbazin committed
1063
1064
1065
1066
1067
1068
1069
        ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
                                : 0 );
        ddsd.dwHeight = p_vout->render.i_height;
        ddsd.dwWidth = p_vout->render.i_width;
        ddsd.dwBackBufferCount = i_backbuffers;

        dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1070
                                               &ddsd, &p_surface, NULL );
gbazin's avatar
   
gbazin committed
1071
1072
1073
        if( dxresult != DD_OK )
        {
            *pp_surface_final = NULL;
1074
            return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
1075
1076
1077
1078
1079
        }
    }

    if( !b_overlay )
    {
1080
        bool b_rgb_surface =
1081
1082
1083
1084
1085
            ( i_chroma == VLC_CODEC_RGB8 )
          || ( i_chroma == VLC_CODEC_RGB15 )
           || ( i_chroma == VLC_CODEC_RGB16 )
            || ( i_chroma == VLC_CODEC_RGB24 )
             || ( i_chroma == VLC_CODEC_RGB32 );
gbazin's avatar
   
gbazin committed
1086
1087
1088
1089

        memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
        ddsd.dwSize = sizeof(DDSURFACEDESC);
        ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1090
        ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
gbazin's avatar
   
gbazin committed
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
        ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
        ddsd.dwHeight = p_vout->render.i_height;
        ddsd.dwWidth = p_vout->render.i_width;

        if( p_vout->p_sys->b_use_sysmem )
            ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
        else
            ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;

        if( !b_rgb_surface )
        {
            ddsd.dwFlags |= DDSD_PIXELFORMAT;
            ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
            ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
        }

        dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
1108
                                               &ddsd, &p_surface, NULL );
gbazin's avatar
   
gbazin committed
1109
1110
1111
        if( dxresult != DD_OK )
        {
            *pp_surface_final = NULL;
1112
            return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
        }
    }

    /* Now that the surface is created, try to get a newer DirectX interface */
    dxresult = IDirectDrawSurface_QueryInterface( p_surface,
                                     &IID_IDirectDrawSurface2,
                                     (LPVOID *)pp_surface_final );
    IDirectDrawSurface_Release( p_surface );    /* Release the old interface */
    if ( dxresult != DD_OK )
    {
1123
        msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
gbazin's avatar
   
gbazin committed
1124