2 * Copyright (c) 2008 Georgi Petrov (gogothebee) <gogothebee@gmail.com>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "video_out.h"
27 #include "video_out_internal.h"
28 #include "fastmemcpy.h"
31 #include "w32_common.h"
32 #include "libavutil/common.h"
35 static const vo_info_t info
=
37 "Direct3D 9 Renderer",
39 "Georgi Petrov (gogothebee) <gogothebee@gmail.com>",
44 * Link essential libvo functions: preinit, config, control, draw_frame,
45 * draw_slice, draw_osd, flip_page, check_events, uninit and
48 const LIBVO_EXTERN(direct3d
)
51 /* Global variables "priv" structure. I try to keep their count low.
53 static struct global_priv
{
54 int is_paused
; /**< 1 = Movie is paused,
55 0 = Movie is not paused */
56 int is_clear_needed
; /**< 1 = Clear the backbuffer before StretchRect
57 0 = (default) Don't clear it */
58 D3DLOCKED_RECT locked_rect
; /**< The locked offscreen surface */
59 RECT fs_movie_rect
; /**< Rect (upscaled) of the movie when displayed
61 RECT fs_panscan_rect
; /**< PanScan source surface cropping in
63 int src_width
; /**< Source (movie) width */
64 int src_height
; /**< Source (movie) heigth */
65 int border_x
; /**< horizontal border value for OSD */
66 int border_y
; /**< vertical border value for OSD */
68 D3DFORMAT movie_src_fmt
; /**< Movie colorspace format (depends on
70 D3DFORMAT desktop_fmt
; /**< Desktop (screen) colorspace format.
73 HANDLE d3d9_dll
; /**< d3d9 Library HANDLE */
74 IDirect3D9
* (WINAPI
*pDirect3DCreate9
)(UINT
); /**< pointer to Direct3DCreate9 function */
76 LPDIRECT3D9 d3d_handle
; /**< Direct3D Handle */
77 LPDIRECT3DDEVICE9 d3d_device
; /**< The Direct3D Adapter */
78 IDirect3DSurface9
*d3d_surface
; /**< Offscreen Direct3D Surface. MPlayer
79 renders inside it. Uses colorspace
80 priv->movie_src_fmt */
81 IDirect3DTexture9
*d3d_texture_osd
; /**< Direct3D Texture. Uses RGBA */
82 IDirect3DTexture9
*d3d_texture_system
; /**< Direct3D Texture. System memory
83 cannot lock a normal texture. Uses RGBA */
84 IDirect3DSurface9
*d3d_backbuf
; /**< Video card's back buffer (used to
85 display next frame) */
86 int cur_backbuf_width
; /**< Current backbuffer width */
87 int cur_backbuf_height
; /**< Current backbuffer height */
88 int is_osd_populated
; /**< 1 = OSD texture has something to display,
89 0 = OSD texture is clear */
90 int device_caps_power2_only
; /**< 1 = texture sizes have to be power 2
91 0 = texture sizes can be anything */
92 int device_caps_square_only
; /**< 1 = textures have to be square
93 0 = textures do not have to be square */
94 int device_texture_sys
; /**< 1 = device can texture from system memory
95 0 = device requires shadow */
96 int max_texture_width
; /**< from the device capabilities */
97 int max_texture_height
; /**< from the device capabilities */
98 int osd_width
; /**< current width of the OSD */
99 int osd_height
; /**< current height of the OSD */
100 int osd_texture_width
; /**< current width of the OSD texture */
101 int osd_texture_height
; /**< current height of the OSD texture */
105 const unsigned int mplayer_fmt
; /**< Given by MPlayer */
106 const D3DFORMAT fourcc
; /**< Required by D3D's test function */
109 /* Map table from reported MPlayer format to the required
110 fourcc. This is needed to perform the format query. */
112 static const struct_fmt_table fmt_table
[] = {
113 {IMGFMT_YV12
, MAKEFOURCC('Y','V','1','2')},
114 {IMGFMT_I420
, MAKEFOURCC('I','4','2','0')},
115 {IMGFMT_IYUV
, MAKEFOURCC('I','Y','U','V')},
116 {IMGFMT_YVU9
, MAKEFOURCC('Y','V','U','9')},
117 {IMGFMT_YUY2
, D3DFMT_YUY2
},
118 {IMGFMT_UYVY
, D3DFMT_UYVY
},
119 {IMGFMT_BGR32
, D3DFMT_X8R8G8B8
},
120 {IMGFMT_RGB32
, D3DFMT_X8B8G8R8
},
121 {IMGFMT_BGR24
, D3DFMT_R8G8B8
}, //untested
122 {IMGFMT_BGR16
, D3DFMT_R5G6B5
},
123 {IMGFMT_BGR15
, D3DFMT_X1R5G5B5
},
124 {IMGFMT_BGR8
, D3DFMT_R3G3B2
}, //untested
127 #define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0]))
129 #define D3DFVF_MY_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
132 float x
, y
, z
; /* Position of vertex in 3D space */
133 float tu
, tv
; /* Texture coordinates */
136 typedef enum back_buffer_action
{
139 } back_buffer_action_e
;
140 /****************************************************************************
144 * Direct3D specific implementation functions *
148 ****************************************************************************/
150 /** @brief Calculate scaled fullscreen movie rectangle with
151 * preserved aspect ratio.
153 static void calc_fs_rect(void)
155 struct vo_rect src_rect
;
156 struct vo_rect dst_rect
;
157 struct vo_rect borders
;
158 calc_src_dst_rects(priv
->src_width
, priv
->src_height
, &src_rect
, &dst_rect
, &borders
, NULL
);
160 priv
->fs_movie_rect
.left
= dst_rect
.left
;
161 priv
->fs_movie_rect
.right
= dst_rect
.right
;
162 priv
->fs_movie_rect
.top
= dst_rect
.top
;
163 priv
->fs_movie_rect
.bottom
= dst_rect
.bottom
;
164 priv
->fs_panscan_rect
.left
= src_rect
.left
;
165 priv
->fs_panscan_rect
.right
= src_rect
.right
;
166 priv
->fs_panscan_rect
.top
= src_rect
.top
;
167 priv
->fs_panscan_rect
.bottom
= src_rect
.bottom
;
168 priv
->border_x
= borders
.left
;
169 priv
->border_y
= borders
.top
;
171 mp_msg(MSGT_VO
, MSGL_V
,
172 "<vo_direct3d>Fullscreen movie rectangle: t: %ld, l: %ld, r: %ld, b:%ld\n",
173 priv
->fs_movie_rect
.top
, priv
->fs_movie_rect
.left
,
174 priv
->fs_movie_rect
.right
, priv
->fs_movie_rect
.bottom
);
176 /* The backbuffer should be cleared before next StretchRect. This is
177 * necessary because our new draw area could be smaller than the
178 * previous one used by StretchRect and without it, leftovers from the
179 * previous frame will be left. */
180 priv
->is_clear_needed
= 1;
183 /** @brief Destroy D3D Offscreen and Backbuffer surfaces.
185 static void destroy_d3d_surfaces(void)
187 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>destroy_d3d_surfaces called.\n");
188 /* Let's destroy the old (if any) D3D Surfaces */
190 if (priv
->locked_rect
.pBits
)
191 IDirect3DSurface9_UnlockRect(priv
->d3d_surface
);
192 priv
->locked_rect
.pBits
= NULL
;
194 if (priv
->d3d_surface
)
195 IDirect3DSurface9_Release(priv
->d3d_surface
);
196 priv
->d3d_surface
= NULL
;
198 /* kill the OSD texture and its shadow copy */
199 if (priv
->d3d_texture_osd
)
200 IDirect3DTexture9_Release(priv
->d3d_texture_osd
);
201 priv
->d3d_texture_osd
= NULL
;
203 if (priv
->d3d_texture_system
)
204 IDirect3DTexture9_Release(priv
->d3d_texture_system
);
205 priv
->d3d_texture_system
= NULL
;
207 if (priv
->d3d_backbuf
)
208 IDirect3DSurface9_Release(priv
->d3d_backbuf
);
209 priv
->d3d_backbuf
= NULL
;
212 /** @brief Create D3D Offscreen and Backbuffer surfaces. Each
213 * surface is created only if it's not already present.
214 * @return 1 on success, 0 on failure
216 static int create_d3d_surfaces(void)
218 int osd_width
= vo_dwidth
, osd_height
= vo_dheight
;
219 int tex_width
= osd_width
, tex_height
= osd_height
;
220 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>create_d3d_surfaces called.\n");
222 if (!priv
->d3d_surface
&&
223 FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
224 priv
->d3d_device
, priv
->src_width
, priv
->src_height
,
225 priv
->movie_src_fmt
, D3DPOOL_DEFAULT
, &priv
->d3d_surface
, NULL
))) {
226 mp_msg(MSGT_VO
, MSGL_ERR
,
227 "<vo_direct3d>Allocating offscreen surface failed.\n");
231 if (!priv
->d3d_backbuf
&&
232 FAILED(IDirect3DDevice9_GetBackBuffer(priv
->d3d_device
, 0, 0,
233 D3DBACKBUFFER_TYPE_MONO
,
234 &priv
->d3d_backbuf
))) {
235 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Allocating backbuffer failed.\n");
239 /* calculate the best size for the OSD depending on the factors from the device */
240 if (priv
->device_caps_power2_only
) {
243 while (tex_width
< osd_width
) tex_width
<<= 1;
244 while (tex_height
< osd_height
) tex_height
<<= 1;
246 if (priv
->device_caps_square_only
)
247 /* device only supports square textures */
248 tex_width
= tex_height
= tex_width
> tex_height
? tex_width
: tex_height
;
249 // better round up to a multiple of 16
250 tex_width
= (tex_width
+ 15) & ~15;
251 tex_height
= (tex_height
+ 15) & ~15;
253 // make sure we respect the size limits without breaking aspect or pow2-requirements
254 while (tex_width
> priv
->max_texture_width
|| tex_height
> priv
->max_texture_height
) {
261 priv
->osd_width
= osd_width
;
262 priv
->osd_height
= osd_height
;
263 priv
->osd_texture_width
= tex_width
;
264 priv
->osd_texture_height
= tex_height
;
266 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>OSD texture size (%dx%d), requested (%dx%d).\n",
267 vo_dwidth
, vo_dheight
, priv
->osd_texture_width
, priv
->osd_texture_height
);
270 if (!priv
->d3d_texture_system
&&
271 FAILED(IDirect3DDevice9_CreateTexture(priv
->d3d_device
,
272 priv
->osd_texture_width
,
273 priv
->osd_texture_height
,
278 &priv
->d3d_texture_system
,
280 mp_msg(MSGT_VO
,MSGL_ERR
,
281 "<vo_direct3d>Allocating OSD texture in system RAM failed.\n");
285 if (!priv
->device_texture_sys
) {
286 /* only create if we need a shadow version on the external device */
287 if (!priv
->d3d_texture_osd
&&
288 FAILED(IDirect3DDevice9_CreateTexture(priv
->d3d_device
,
289 priv
->osd_texture_width
,
290 priv
->osd_texture_height
,
295 &priv
->d3d_texture_osd
,
297 mp_msg(MSGT_VO
,MSGL_ERR
,
298 "<vo_direct3d>Allocating OSD texture in video RAM failed.\n");
303 /* setup default renderstate */
304 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_SRCBLEND
, D3DBLEND_ONE
);
305 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_DESTBLEND
, D3DBLEND_INVSRCALPHA
);
306 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHAFUNC
, D3DCMP_GREATER
);
307 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHAREF
, (DWORD
)0x0);
308 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_LIGHTING
, FALSE
);
309 IDirect3DDevice9_SetSamplerState(priv
->d3d_device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_LINEAR
);
310 IDirect3DDevice9_SetSamplerState(priv
->d3d_device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_LINEAR
);
315 /** @brief Fill D3D Presentation parameters
317 static void fill_d3d_presentparams(D3DPRESENT_PARAMETERS
*present_params
)
319 /* Prepare Direct3D initialization parameters. */
320 memset(present_params
, 0, sizeof(D3DPRESENT_PARAMETERS
));
321 present_params
->Windowed
= TRUE
;
322 present_params
->SwapEffect
= D3DSWAPEFFECT_COPY
;
323 present_params
->Flags
= D3DPRESENTFLAG_VIDEO
;
324 present_params
->hDeviceWindow
= vo_w32_window
; /* w32_common var */
325 present_params
->BackBufferWidth
= priv
->cur_backbuf_width
;
326 present_params
->BackBufferHeight
= priv
->cur_backbuf_height
;
327 present_params
->MultiSampleType
= D3DMULTISAMPLE_NONE
;
328 present_params
->PresentationInterval
= D3DPRESENT_INTERVAL_ONE
;
329 present_params
->BackBufferFormat
= priv
->desktop_fmt
;
330 present_params
->BackBufferCount
= 1;
331 present_params
->EnableAutoDepthStencil
= FALSE
;
335 /** @brief Create a new backbuffer. Create or Reset the D3D
337 * @return 1 on success, 0 on failure
339 static int change_d3d_backbuffer(back_buffer_action_e action
)
341 D3DPRESENT_PARAMETERS present_params
;
343 destroy_d3d_surfaces();
345 /* Grow the backbuffer in the required dimension. */
346 if (vo_dwidth
> priv
->cur_backbuf_width
)
347 priv
->cur_backbuf_width
= vo_dwidth
;
349 if (vo_dheight
> priv
->cur_backbuf_height
)
350 priv
->cur_backbuf_height
= vo_dheight
;
352 /* The grown backbuffer dimensions are ready and fill_d3d_presentparams
353 * will use them, so we can reset the device.
355 fill_d3d_presentparams(&present_params
);
357 /* vo_w32_window is w32_common variable. It's a handle to the window. */
358 if (action
== BACKBUFFER_CREATE
&&
359 FAILED(IDirect3D9_CreateDevice(priv
->d3d_handle
,
361 D3DDEVTYPE_HAL
, vo_w32_window
,
362 D3DCREATE_SOFTWARE_VERTEXPROCESSING
,
363 &present_params
, &priv
->d3d_device
))) {
364 mp_msg(MSGT_VO
, MSGL_V
,
365 "<vo_direct3d>Creating Direct3D device failed.\n");
369 if (action
== BACKBUFFER_RESET
&&
370 FAILED(IDirect3DDevice9_Reset(priv
->d3d_device
, &present_params
))) {
371 mp_msg(MSGT_VO
, MSGL_ERR
,
372 "<vo_direct3d>Reseting Direct3D device failed.\n");
376 mp_msg(MSGT_VO
, MSGL_V
,
377 "<vo_direct3d>New backbuffer (%dx%d), VO (%dx%d)\n",
378 present_params
.BackBufferWidth
, present_params
.BackBufferHeight
,
379 vo_dwidth
, vo_dheight
);
384 /** @brief Configure initial Direct3D context. The first
385 * function called to initialize the D3D context.
386 * @return 1 on success, 0 on failure
388 static int configure_d3d(void)
390 D3DDISPLAYMODE disp_mode
;
391 D3DVIEWPORT9 vp
= {0, 0, vo_dwidth
, vo_dheight
, 0, 1};
393 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>configure_d3d called.\n");
395 destroy_d3d_surfaces();
397 /* Get the current desktop display mode, so we can set up a back buffer
398 * of the same format. */
399 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv
->d3d_handle
,
402 mp_msg(MSGT_VO
, MSGL_ERR
,
403 "<vo_direct3d>Reading adapter display mode failed.\n");
407 /* Write current Desktop's colorspace format in the global storage. */
408 priv
->desktop_fmt
= disp_mode
.Format
;
410 if (!change_d3d_backbuffer(BACKBUFFER_CREATE
))
413 if (!create_d3d_surfaces())
416 if (FAILED(IDirect3DDevice9_SetViewport(priv
->d3d_device
,
418 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Setting viewport failed.\n");
427 /** @brief Reconfigure the whole Direct3D. Called only
428 * when the video adapter becomes uncooperative.
429 * @return 1 on success, 0 on failure
431 static int reconfigure_d3d(void)
433 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>reconfigure_d3d called.\n");
435 /* Destroy the offscreen, OSD and backbuffer surfaces */
436 destroy_d3d_surfaces();
438 /* Destroy the D3D Device */
439 if (priv
->d3d_device
)
440 IDirect3DDevice9_Release(priv
->d3d_device
);
441 priv
->d3d_device
= NULL
;
443 /* Stop the whole Direct3D */
444 IDirect3D9_Release(priv
->d3d_handle
);
446 /* Initialize Direct3D from the beginning */
447 priv
->d3d_handle
= priv
->pDirect3DCreate9(D3D_SDK_VERSION
);
448 if (!priv
->d3d_handle
) {
449 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Initializing Direct3D failed.\n");
453 /* Configure Direct3D */
454 if (!configure_d3d())
460 /** @brief Resize Direct3D context on window resize.
461 * @return 1 on success, 0 on failure
463 static int resize_d3d(void)
465 D3DVIEWPORT9 vp
= {0, 0, vo_dwidth
, vo_dheight
, 0, 1};
467 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>resize_d3d called.\n");
469 /* Make sure that backbuffer is large enough to accomodate the new
470 viewport dimensions. Grow it if necessary. */
472 if (vo_dwidth
> priv
->cur_backbuf_width
||
473 vo_dheight
> priv
->cur_backbuf_height
) {
474 if (!change_d3d_backbuffer(BACKBUFFER_RESET
))
478 /* Destroy the OSD textures. They should always match the new dimensions
479 * of the onscreen window, so on each resize we need new OSD dimensions.
482 if (priv
->d3d_texture_osd
)
483 IDirect3DTexture9_Release(priv
->d3d_texture_osd
);
484 priv
->d3d_texture_osd
= NULL
;
486 if (priv
->d3d_texture_system
)
487 IDirect3DTexture9_Release(priv
->d3d_texture_system
);
488 priv
->d3d_texture_system
= NULL
;
491 /* Recreate the OSD. The function will observe that the offscreen plain
492 * surface and the backbuffer are not destroyed and will skip their creation,
493 * effectively recreating only the OSD.
496 if (!create_d3d_surfaces())
499 if (FAILED(IDirect3DDevice9_SetViewport(priv
->d3d_device
,
501 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Setting viewport failed.\n");
507 // OSD needs to be drawn fresh for new size
508 vo_osd_changed(OSDTYPE_OSD
);
513 /** @brief Uninitialize Direct3D and close the window.
515 static void uninit_d3d(void)
517 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>uninit_d3d called.\n");
519 destroy_d3d_surfaces();
521 /* Destroy the D3D Device */
522 if (priv
->d3d_device
)
523 IDirect3DDevice9_Release(priv
->d3d_device
);
524 priv
->d3d_device
= NULL
;
526 /* Stop the whole D3D. */
527 if (priv
->d3d_handle
) {
528 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Stopping Direct3D.\n");
529 IDirect3D9_Release(priv
->d3d_handle
);
531 priv
->d3d_handle
= NULL
;
534 /** @brief Render a frame on the screen.
535 * @param mpi mpi structure with the decoded frame inside
536 * @return VO_TRUE on success, VO_ERROR on failure
538 static uint32_t render_d3d_frame(mp_image_t
*mpi
)
540 /* Uncomment when direct rendering is implemented.
541 * if (mpi->flags & MP_IMGFLAG_DIRECT) ...
544 /* If the D3D device is uncooperative (not initialized), return success.
545 The device will be probed for reinitialization in the next flip_page() */
546 if (!priv
->d3d_device
)
549 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
552 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) { /* Copy a planar frame. */
553 draw_slice(mpi
->planes
, mpi
->stride
, mpi
->w
, mpi
->h
, 0, 0);
557 /* If we're here, then we should lock the rect and copy a packed frame */
558 if (!priv
->locked_rect
.pBits
) {
559 if (FAILED(IDirect3DSurface9_LockRect(priv
->d3d_surface
,
560 &priv
->locked_rect
, NULL
, 0))) {
561 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Surface lock failed.\n");
566 memcpy_pic(priv
->locked_rect
.pBits
, mpi
->planes
[0], mpi
->stride
[0],
567 mpi
->height
, priv
->locked_rect
.Pitch
, mpi
->stride
[0]);
570 /* This unlock is used for both slice_draw path and render_d3d_frame path. */
571 if (FAILED(IDirect3DSurface9_UnlockRect(priv
->d3d_surface
))) {
572 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Surface unlock failed.\n");
575 priv
->locked_rect
.pBits
= NULL
;
577 if (FAILED(IDirect3DDevice9_BeginScene(priv
->d3d_device
))) {
578 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>BeginScene failed.\n");
582 if (priv
->is_clear_needed
) {
583 IDirect3DDevice9_Clear(priv
->d3d_device
, 0, NULL
,
584 D3DCLEAR_TARGET
, 0, 0, 0);
585 priv
->is_clear_needed
= 0;
588 if (FAILED(IDirect3DDevice9_StretchRect(priv
->d3d_device
,
590 &priv
->fs_panscan_rect
,
592 &priv
->fs_movie_rect
,
594 mp_msg(MSGT_VO
, MSGL_ERR
,
595 "<vo_direct3d>Copying frame to the backbuffer failed.\n");
599 if (FAILED(IDirect3DDevice9_EndScene(priv
->d3d_device
))) {
600 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>EndScene failed.\n");
608 /** @brief Query if movie colorspace is supported by the HW.
609 * @return 0 on failure, device capabilities (not probed
610 * currently) on success.
612 static int query_format(uint32_t movie_fmt
)
615 for (i
= 0; i
< DISPLAY_FORMAT_TABLE_ENTRIES
; i
++) {
616 if (fmt_table
[i
].mplayer_fmt
== movie_fmt
) {
617 /* Test conversion from Movie colorspace to
618 * display's target colorspace. */
619 if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv
->d3d_handle
,
623 priv
->desktop_fmt
))) {
624 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Rejected image format: %s\n",
625 vo_format_name(fmt_table
[i
].mplayer_fmt
));
629 priv
->movie_src_fmt
= fmt_table
[i
].fourcc
;
630 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Accepted image format: %s\n",
631 vo_format_name(fmt_table
[i
].mplayer_fmt
));
632 return (VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
633 | VFCAP_OSD
| VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
);
641 /****************************************************************************
645 * libvo Control / Callback functions *
649 ****************************************************************************/
654 /** @brief libvo Callback: Preinitialize the video card.
655 * Preinit the hardware just enough to be queried about
658 * @return 0 on success, -1 on failure
661 static int preinit(const char *arg
)
663 D3DDISPLAYMODE disp_mode
;
668 /* Set to zero all global variables. */
669 priv
= calloc(1, sizeof(struct global_priv
));
671 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Allocating private memory failed.\n");
676 > Please use subopt-helper.h for this, see vo_gl.c:preinit for
677 > an example of how to use it.
680 priv
->d3d9_dll
= LoadLibraryA("d3d9.dll");
681 if (!priv
->d3d9_dll
) {
682 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Unable to dynamically load d3d9.dll\n");
686 priv
->pDirect3DCreate9
= (void *)GetProcAddress(priv
->d3d9_dll
, "Direct3DCreate9");
687 if (!priv
->pDirect3DCreate9
) {
688 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Unable to find entry point of Direct3DCreate9\n");
692 priv
->d3d_handle
= priv
->pDirect3DCreate9(D3D_SDK_VERSION
);
693 if (!priv
->d3d_handle
) {
694 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Initializing Direct3D failed.\n");
698 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv
->d3d_handle
,
701 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Reading display mode failed.\n");
705 /* Store in priv->desktop_fmt the user desktop's colorspace. Usually XRGB. */
706 priv
->desktop_fmt
= disp_mode
.Format
;
707 priv
->cur_backbuf_width
= disp_mode
.Width
;
708 priv
->cur_backbuf_height
= disp_mode
.Height
;
710 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Setting backbuffer dimensions to (%dx%d).\n",
711 disp_mode
.Width
, disp_mode
.Height
);
713 if (FAILED(IDirect3D9_GetDeviceCaps(priv
->d3d_handle
,
717 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Reading display capabilities failed.\n");
721 /* Store relevant information reguarding caps of device */
722 texture_caps
= disp_caps
.TextureCaps
;
723 dev_caps
= disp_caps
.DevCaps
;
724 priv
->device_caps_power2_only
= (texture_caps
& D3DPTEXTURECAPS_POW2
) &&
725 !(texture_caps
& D3DPTEXTURECAPS_NONPOW2CONDITIONAL
);
726 priv
->device_caps_square_only
= texture_caps
& D3DPTEXTURECAPS_SQUAREONLY
;
727 priv
->device_texture_sys
= dev_caps
& D3DDEVCAPS_TEXTURESYSTEMMEMORY
;
728 priv
->max_texture_width
= disp_caps
.MaxTextureWidth
;
729 priv
->max_texture_height
= disp_caps
.MaxTextureHeight
;
731 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>device_caps_power2_only %d, device_caps_square_only %d\n"
732 "<vo_direct3d>device_texture_sys %d\n"
733 "<vo_direct3d>max_texture_width %d, max_texture_height %d\n",
734 priv
->device_caps_power2_only
, priv
->device_caps_square_only
,
735 priv
->device_texture_sys
, priv
->max_texture_width
,
736 priv
->max_texture_height
);
738 /* w32_common framework call. Configures window on the screen, gets
739 * fullscreen dimensions and does other useful stuff.
741 if (!vo_w32_init()) {
742 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Configuring onscreen window failed.\n");
755 /** @brief libvo Callback: Handle control requests.
756 * @return VO_TRUE on success, VO_NOTIMPL when not implemented
758 static int control(uint32_t request
, void *data
)
761 case VOCTRL_QUERY_FORMAT
:
762 return query_format(*(uint32_t*) data
);
763 case VOCTRL_GET_IMAGE
: /* Direct Rendering. Not implemented yet. */
764 mp_msg(MSGT_VO
, MSGL_V
,
765 "<vo_direct3d>Direct Rendering request. Not implemented yet.\n");
767 case VOCTRL_DRAW_IMAGE
:
768 return render_d3d_frame(data
);
769 case VOCTRL_FULLSCREEN
:
781 case VOCTRL_SET_EQUALIZER
:
783 case VOCTRL_GET_EQUALIZER
:
792 case VOCTRL_UPDATE_SCREENINFO
:
793 w32_update_xinerama_info();
795 case VOCTRL_SET_PANSCAN
:
798 case VOCTRL_GET_PANSCAN
:
804 /** @brief libvo Callback: Configre the Direct3D adapter.
805 * @param width Movie source width
806 * @param height Movie source height
807 * @param d_width Screen (destination) width
808 * @param d_height Screen (destination) height
809 * @param options Options bitmap
810 * @param title Window title
811 * @param format Movie colorspace format (using MPlayer's
812 * defines, e.g. IMGFMT_YUY2)
813 * @return 0 on success, VO_ERROR on failure
815 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
816 uint32_t d_height
, uint32_t options
, char *title
,
820 priv
->src_width
= width
;
821 priv
->src_height
= height
;
823 /* w32_common framework call. Creates window on the screen with
824 * the given coordinates.
826 if (!vo_w32_config(d_width
, d_height
, options
)) {
827 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Creating onscreen window failed.\n");
831 /* "config" may be called several times, so if this is not the first
832 * call, we should destroy Direct3D adapter and surfaces before
833 * calling configure_d3d, which will create them again.
836 destroy_d3d_surfaces();
838 /* Destroy the D3D Device */
839 if (priv
->d3d_device
)
840 IDirect3DDevice9_Release(priv
->d3d_device
);
841 priv
->d3d_device
= NULL
;
843 if (!configure_d3d())
846 return 0; /* Success */
849 /** @brief libvo Callback: Flip next already drawn frame on the
852 static void flip_page(void)
854 RECT rect
= {0, 0, vo_dwidth
, vo_dheight
};
855 if (!priv
->d3d_device
||
856 FAILED(IDirect3DDevice9_Present(priv
->d3d_device
, &rect
, 0, 0, 0))) {
857 mp_msg(MSGT_VO
, MSGL_V
,
858 "<vo_direct3d>Trying to reinitialize uncooperative video adapter.\n");
859 if (!reconfigure_d3d()) {
860 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Reinitialization failed.\n");
864 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Video adapter reinitialized.\n");
868 /** @brief libvo Callback: Uninitializes all pointers and closes
869 * all D3D related stuff,
871 static void uninit(void)
873 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>uninit called.\n");
876 vo_w32_uninit(); /* w32_common framework call */
878 FreeLibrary(priv
->d3d9_dll
);
879 priv
->d3d9_dll
= NULL
;
884 /** @brief libvo Callback: Handles video window events.
886 static void check_events(void)
889 /* w32_common framework call. Handles video window events.
890 * Updates global libvo's vo_dwidth/vo_dheight upon resize
891 * with the new window width/height.
893 flags
= vo_w32_check_events();
894 if (flags
& VO_EVENT_RESIZE
)
897 if ((flags
& VO_EVENT_EXPOSE
) && priv
->is_paused
)
901 /** @brief libvo Callback: Draw slice
902 * @return 0 on success
904 static int draw_slice(uint8_t *src
[], int stride
[], int w
,int h
,int x
,int y
)
906 char *my_src
; /**< Pointer to the source image */
907 char *dst
; /**< Pointer to the destination image */
908 int uv_stride
; /**< Stride of the U/V planes */
910 /* If the D3D device is uncooperative (not initialized), return success.
911 The device will be probed for reinitialization in the next flip_page() */
912 if (!priv
->d3d_device
)
915 /* Lock the offscreen surface if it's not already locked. */
916 if (!priv
->locked_rect
.pBits
) {
917 if (FAILED(IDirect3DSurface9_LockRect(priv
->d3d_surface
,
918 &priv
->locked_rect
, NULL
, 0))) {
919 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Surface lock failure.\n");
924 uv_stride
= priv
->locked_rect
.Pitch
/ 2;
927 dst
= priv
->locked_rect
.pBits
;
928 dst
= dst
+ priv
->locked_rect
.Pitch
* y
+ x
;
930 memcpy_pic(dst
, my_src
, w
, h
, priv
->locked_rect
.Pitch
, stride
[0]);
938 dst
= priv
->locked_rect
.pBits
;
939 dst
= dst
+ priv
->locked_rect
.Pitch
* priv
->src_height
941 if (priv
->movie_src_fmt
== MAKEFOURCC('Y','V','1','2'))
946 memcpy_pic(dst
, my_src
, w
, h
, uv_stride
, stride
[1]);
949 dst
= priv
->locked_rect
.pBits
;
950 dst
= dst
+ priv
->locked_rect
.Pitch
* priv
->src_height
951 + uv_stride
* (priv
->src_height
/ 2) + uv_stride
* y
+ x
;
952 if (priv
->movie_src_fmt
== MAKEFOURCC('Y','V','1','2'))
957 memcpy_pic(dst
, my_src
, w
, h
, uv_stride
, stride
[2]);
959 return 0; /* Success */
962 /** @brief libvo Callback: Unused function
964 static int draw_frame(uint8_t *src
[])
966 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>draw_frame called.\n");
970 /** @brief Maps MPlayer alpha to D3D
971 * 0x0 -> transparent and discarded by alpha test
972 * 0x1 -> 0xFF to become opaque
973 * other alpha values are inverted +1 (2 = -2)
974 * These values are then inverted again with
975 the texture filter D3DBLEND_INVSRCALPHA
977 static void vo_draw_alpha_l8a8(int w
, int h
, unsigned char* src
,
978 unsigned char *srca
, int srcstride
,
979 unsigned char* dstbase
, int dststride
)
982 for (y
= 0; y
< h
; y
++) {
983 unsigned short *dst
= (unsigned short*)dstbase
;
985 for (x
= 0; x
< w
; x
++) {
986 dst
[x
] = (-srca
[x
] << 8) | src
[x
];
990 dstbase
+= dststride
;
994 /** @brief Callback function to render the OSD to the texture
996 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
997 unsigned char *srca
, int stride
)
999 D3DLOCKED_RECT locked_rect
; /**< Offscreen surface we lock in order
1000 to copy MPlayer's frame inside it.*/
1002 if (FAILED(IDirect3DTexture9_LockRect(priv
->d3d_texture_system
, 0,
1003 &locked_rect
, NULL
, 0))) {
1004 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>OSD texture lock failed.\n");
1008 vo_draw_alpha_l8a8(w
, h
, src
, srca
, stride
,
1009 (unsigned char *)locked_rect
.pBits
+ locked_rect
.Pitch
*y0
+ 2*x0
, locked_rect
.Pitch
);
1011 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1012 if (FAILED(IDirect3DTexture9_UnlockRect(priv
->d3d_texture_system
, 0))) {
1013 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>OSD texture unlock failed.\n");
1017 priv
->is_osd_populated
= 1;
1020 /** @brief libvo Callback: Draw OSD/Subtitles,
1022 static void draw_osd(void)
1024 // we can not render OSD if we lost the device e.g. because it was uncooperative
1025 if (!priv
->d3d_device
)
1028 if (vo_osd_changed(0)) {
1029 D3DLOCKED_RECT locked_rect
; /**< Offscreen surface we lock in order
1030 to copy MPlayer's frame inside it.*/
1033 if (FAILED(IDirect3DTexture9_LockRect(priv
->d3d_texture_system
, 0,
1034 &locked_rect
, NULL
, 0))) {
1035 mp_msg(MSGT_VO
,MSGL_ERR
, "<vo_direct3d>OSD texture lock failed.\n");
1039 /* clear the whole texture to avoid issues due to interpolation */
1040 memset(locked_rect
.pBits
, 0, locked_rect
.Pitch
* priv
->osd_texture_height
);
1042 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1043 if (FAILED(IDirect3DTexture9_UnlockRect(priv
->d3d_texture_system
, 0))) {
1044 mp_msg(MSGT_VO
,MSGL_ERR
, "<vo_direct3d>OSD texture unlock failed.\n");
1048 priv
->is_osd_populated
= 0;
1049 /* required for if subs are in the boarder region */
1050 priv
->is_clear_needed
= 1;
1052 vo_draw_text_ext(priv
->osd_width
, priv
->osd_height
, priv
->border_x
, priv
->border_y
,
1053 priv
->border_x
, priv
->border_y
, priv
->src_width
, priv
->src_height
, draw_alpha
);
1055 if (!priv
->device_texture_sys
)
1057 /* only DMA to the shadow if its required */
1058 if (FAILED(IDirect3DDevice9_UpdateTexture(priv
->d3d_device
,
1059 (IDirect3DBaseTexture9
*)priv
->d3d_texture_system
,
1060 (IDirect3DBaseTexture9
*)priv
->d3d_texture_osd
))) {
1061 mp_msg(MSGT_VO
,MSGL_ERR
, "<vo_direct3d>OSD texture transfer failed.\n");
1069 if (priv
->is_osd_populated
) {
1071 struct_vertex osd_quad_vb
[] = {
1072 {-1.0f
, 1.0f
, 0.0f
, 0, 0 },
1073 { 1.0f
, 1.0f
, 0.0f
, 1, 0 },
1074 {-1.0f
,-1.0f
, 0.0f
, 0, 1 },
1075 { 1.0f
,-1.0f
, 0.0f
, 1, 1 }
1078 /* calculate the texture coordinates */
1080 osd_quad_vb
[3].tu
= (float)priv
->osd_width
/ priv
->osd_texture_width
;
1082 osd_quad_vb
[3].tv
= (float)priv
->osd_height
/ priv
->osd_texture_height
;
1084 if (FAILED(IDirect3DDevice9_BeginScene(priv
->d3d_device
))) {
1085 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>BeginScene failed.\n");
1089 /* turn on alpha test */
1090 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHABLENDENABLE
, TRUE
);
1091 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHATESTENABLE
, TRUE
);
1093 /* need to use a texture here (done here as we may be able to texture from system memory) */
1094 IDirect3DDevice9_SetTexture(priv
->d3d_device
, 0,
1095 (IDirect3DBaseTexture9
*)(priv
->device_texture_sys
1096 ? priv
->d3d_texture_system
: priv
->d3d_texture_osd
));
1098 IDirect3DDevice9_SetFVF(priv
->d3d_device
, D3DFVF_MY_VERTEX
);
1099 IDirect3DDevice9_DrawPrimitiveUP(priv
->d3d_device
, D3DPT_TRIANGLESTRIP
, 2, osd_quad_vb
, sizeof(struct_vertex
));
1101 /* turn off alpha test */
1102 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHATESTENABLE
, FALSE
);
1103 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHABLENDENABLE
, FALSE
);
1105 if (FAILED(IDirect3DDevice9_EndScene(priv
->d3d_device
))) {
1106 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>EndScene failed.\n");