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"
33 #include "font_load.h"
36 static const vo_info_t info
=
38 "Direct3D 9 Renderer",
40 "Georgi Petrov (gogothebee) <gogothebee@gmail.com>",
45 * Link essential libvo functions: preinit, config, control, draw_frame,
46 * draw_slice, draw_osd, flip_page, check_events, uninit and
49 const LIBVO_EXTERN(direct3d
)
52 /* Global variables "priv" structure. I try to keep their count low.
54 static struct global_priv
{
55 int is_paused
; /**< 1 = Movie is paused,
56 0 = Movie is not paused */
57 int is_clear_needed
; /**< 1 = Clear the backbuffer before StretchRect
58 0 = (default) Don't clear it */
59 D3DLOCKED_RECT locked_rect
; /**< The locked Offscreen surface */
60 RECT fs_movie_rect
; /**< Rect (upscaled) of the movie when displayed
62 RECT fs_panscan_rect
; /**< PanScan source surface cropping in
64 int src_width
; /**< Source (movie) width */
65 int src_height
; /**< Source (movie) heigth */
67 D3DFORMAT movie_src_fmt
; /**< Movie colorspace format (depends on
69 D3DFORMAT desktop_fmt
; /**< Desktop (screen) colorspace format.
71 LPDIRECT3D9 d3d_handle
; /**< Direct3D Handle */
72 LPDIRECT3DDEVICE9 d3d_device
; /**< The Direct3D Adapter */
73 IDirect3DSurface9
*d3d_surface
; /**< Offscreen Direct3D Surface. MPlayer
74 renders inside it. Uses colorspace
75 priv->movie_src_fmt */
76 IDirect3DTexture9
*d3d_texture_osd
; /**< Direct3D Texture. Uses RGBA */
77 IDirect3DTexture9
*d3d_texture_system
; /**< Direct3D Texture. System memory
78 cannot lock a normal texture. Uses RGBA */
79 IDirect3DSurface9
*d3d_backbuf
; /**< Video card's back buffer (used to
80 display next frame) */
81 int is_osd_populated
; /**< 1 = OSD texture has something to display,
82 0 = OSD texture is clear */
83 int device_caps_power2_only
; /**< 1 = texture sizes have to be power 2
84 0 = texture sizes can be anything */
85 int device_caps_square_only
; /**< 1 = textures have to be square
86 0 = textures do not have to be square */
87 int device_texture_sys
; /**< 1 = device can texture from system memory
88 0 = device requires shadow */
89 int max_texture_width
; /**< from the device capabilities */
90 int max_texture_height
; /**< from the device capabilities */
91 int osd_width
; /**< current width of the OSD */
92 int osd_height
; /**< current height of the OSD */
93 int osd_texture_width
; /**< current width of the OSD texture */
94 int osd_texture_height
; /**< current height of the OSD texture */
98 const unsigned int mplayer_fmt
; /**< Given by MPlayer */
99 const D3DFORMAT fourcc
; /**< Required by D3D's test function */
102 /* Map table from reported MPlayer format to the required
103 fourcc. This is needed to perform the format query. */
105 static const struct_fmt_table fmt_table
[] = {
106 {IMGFMT_YV12
, MAKEFOURCC('Y','V','1','2')},
107 {IMGFMT_I420
, MAKEFOURCC('I','4','2','0')},
108 {IMGFMT_IYUV
, MAKEFOURCC('I','Y','U','V')},
109 {IMGFMT_YVU9
, MAKEFOURCC('Y','V','U','9')},
110 {IMGFMT_YUY2
, D3DFMT_YUY2
},
111 {IMGFMT_UYVY
, D3DFMT_UYVY
},
112 {IMGFMT_BGR32
, D3DFMT_X8R8G8B8
},
113 {IMGFMT_RGB32
, D3DFMT_X8B8G8R8
},
114 {IMGFMT_BGR24
, D3DFMT_R8G8B8
}, //untested
115 {IMGFMT_BGR16
, D3DFMT_R5G6B5
},
116 {IMGFMT_BGR15
, D3DFMT_X1R5G5B5
},
117 {IMGFMT_BGR8
, D3DFMT_R3G3B2
}, //untested
120 #define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0]))
122 #define D3DFVF_MY_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
125 float x
, y
, z
; /* Position of vertex in 3D space */
126 float tu
, tv
; /* Texture coordinates */
129 /****************************************************************************
133 * Direct3D specific implementation functions *
137 ****************************************************************************/
139 /** @brief Calculate scaled fullscreen movie rectangle with
140 * preserved aspect ratio.
142 static void calc_fs_rect(void)
144 int scaled_height
= 0;
145 int scaled_width
= 0;
147 // set default values
148 priv
->fs_movie_rect
.left
= 0;
149 priv
->fs_movie_rect
.right
= vo_dwidth
;
150 priv
->fs_movie_rect
.top
= 0;
151 priv
->fs_movie_rect
.bottom
= vo_dheight
;
152 priv
->fs_panscan_rect
.left
= 0;
153 priv
->fs_panscan_rect
.right
= priv
->src_width
;
154 priv
->fs_panscan_rect
.top
= 0;
155 priv
->fs_panscan_rect
.bottom
= priv
->src_height
;
159 // adjust for fullscreen aspect and panscan
160 aspect(&scaled_width
, &scaled_height
, A_ZOOM
);
162 scaled_width
+= vo_panscan_x
;
163 scaled_height
+= vo_panscan_y
;
165 // note: border is rounded to a multiple of two since at least
166 // ATI drivers can not handle odd values with YV12 input
167 if (scaled_width
> vo_dwidth
) {
168 int border
= priv
->src_width
* (scaled_width
- vo_dwidth
) / scaled_width
;
169 border
= (border
/ 2 + 1) & ~1;
170 priv
->fs_panscan_rect
.left
= border
;
171 priv
->fs_panscan_rect
.right
= priv
->src_width
- border
;
173 priv
->fs_movie_rect
.left
= (vo_dwidth
- scaled_width
) / 2;
174 priv
->fs_movie_rect
.right
= priv
->fs_movie_rect
.left
+ scaled_width
;
176 if (scaled_height
> vo_dheight
) {
177 int border
= priv
->src_height
* (scaled_height
- vo_dheight
) / scaled_height
;
178 border
= (border
/ 2 + 1) & ~1;
179 priv
->fs_panscan_rect
.top
= border
;
180 priv
->fs_panscan_rect
.bottom
= priv
->src_height
- border
;
182 priv
->fs_movie_rect
.top
= (vo_dheight
- scaled_height
) / 2;
183 priv
->fs_movie_rect
.bottom
= priv
->fs_movie_rect
.top
+ scaled_height
;
186 mp_msg(MSGT_VO
, MSGL_V
,
187 "<vo_direct3d>Fullscreen Movie Rect: t: %ld, l: %ld, r: %ld, b:%ld\r\n",
188 priv
->fs_movie_rect
.top
, priv
->fs_movie_rect
.left
,
189 priv
->fs_movie_rect
.right
, priv
->fs_movie_rect
.bottom
);
191 /* The backbuffer should be cleared before next StretchRect. This is
192 * necessary because our new draw area could be smaller than the
193 * previous one used by StretchRect and without it, leftovers from the
194 * previous frame will be left. */
195 priv
->is_clear_needed
= 1;
198 /** @brief Destroy D3D Offscreen and Backbuffer surfaces.
200 static void destroy_d3d_surfaces(void)
202 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>destroy_d3d_surfaces called\r\n");
203 /* Let's destroy the old (if any) D3D Surfaces */
205 if (priv
->locked_rect
.pBits
)
206 IDirect3DSurface9_UnlockRect(priv
->d3d_surface
);
207 priv
->locked_rect
.pBits
= NULL
;
209 if (priv
->d3d_surface
)
210 IDirect3DSurface9_Release(priv
->d3d_surface
);
211 priv
->d3d_surface
= NULL
;
213 /* kill the OSD texture and its shadow copy */
214 if (priv
->d3d_texture_osd
)
215 IDirect3DTexture9_Release(priv
->d3d_texture_osd
);
216 priv
->d3d_texture_osd
= NULL
;
218 if (priv
->d3d_texture_system
)
219 IDirect3DTexture9_Release(priv
->d3d_texture_system
);
220 priv
->d3d_texture_system
= NULL
;
222 if (priv
->d3d_backbuf
)
223 IDirect3DSurface9_Release(priv
->d3d_backbuf
);
224 priv
->d3d_backbuf
= NULL
;
227 /** @brief Create D3D Offscreen and Backbuffer surfaces.
228 * @return 1 on success, 0 on failure
230 static int create_d3d_surfaces(void)
232 int osd_width
= vo_dwidth
, osd_height
= vo_dheight
;
233 int tex_width
= osd_width
, tex_height
= osd_height
;
234 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d><INFO>create_d3d_surfaces called.\n");
236 if (FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
237 priv
->d3d_device
, priv
->src_width
, priv
->src_height
,
238 priv
->movie_src_fmt
, D3DPOOL_DEFAULT
, &priv
->d3d_surface
, NULL
))) {
239 mp_msg(MSGT_VO
, MSGL_ERR
,
240 "<vo_direct3d><INFO>IDirect3D9_CreateOffscreenPlainSurface Failed.\n");
244 if (FAILED(IDirect3DDevice9_GetBackBuffer(priv
->d3d_device
, 0, 0,
245 D3DBACKBUFFER_TYPE_MONO
,
246 &priv
->d3d_backbuf
))) {
247 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Back Buffer address get failed\n");
251 /* calculate the best size for the OSD depending on the factors from the device */
252 if (priv
->device_caps_power2_only
) {
255 while (tex_width
< osd_width
) tex_width
<<= 1;
256 while (tex_height
< osd_height
) tex_height
<<= 1;
258 if (priv
->device_caps_square_only
)
259 /* device only supports square textures */
260 tex_width
= tex_height
= tex_width
> tex_height
? tex_width
: tex_height
;
261 // better round up to a multiple of 16
262 tex_width
= (tex_width
+ 15) & ~15;
263 tex_height
= (tex_height
+ 15) & ~15;
265 // make sure we respect the size limits without breaking aspect or pow2-requirements
266 while (tex_width
> priv
->max_texture_width
|| tex_height
> priv
->max_texture_height
) {
273 priv
->osd_width
= osd_width
;
274 priv
->osd_height
= osd_height
;
275 priv
->osd_texture_width
= tex_width
;
276 priv
->osd_texture_height
= tex_height
;
278 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d><INFO>surface (%d, %d) requested = (%d, %d)\n",
279 vo_dwidth
, vo_dheight
, priv
->osd_texture_width
, priv
->osd_texture_height
);
282 if (FAILED(IDirect3DDevice9_CreateTexture(priv
->d3d_device
,
283 priv
->osd_texture_width
,
284 priv
->osd_texture_height
,
289 &priv
->d3d_texture_system
,
291 mp_msg(MSGT_VO
,MSGL_ERR
,
292 "<vo_direct3d><INFO>IDirect3DDevice9_CreateTexture Failed (d3d_texture_system).\n");
296 if (!priv
->device_texture_sys
) {
297 /* only create if we need a shadow version on the external device */
298 if (FAILED(IDirect3DDevice9_CreateTexture(priv
->d3d_device
,
299 priv
->osd_texture_width
,
300 priv
->osd_texture_height
,
305 &priv
->d3d_texture_osd
,
307 mp_msg(MSGT_VO
,MSGL_ERR
,
308 "<vo_direct3d><INFO>IDirect3DDevice9_CreateTexture Failed (d3d_texture_osd).\n");
313 /* setup default renderstate */
314 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_SRCBLEND
, D3DBLEND_ONE
);
315 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_DESTBLEND
, D3DBLEND_INVSRCALPHA
);
316 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHAFUNC
, D3DCMP_GREATER
);
317 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHAREF
, (DWORD
)0x0);
318 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_LIGHTING
, FALSE
);
319 IDirect3DDevice9_SetSamplerState(priv
->d3d_device
, 0, D3DSAMP_MINFILTER
, D3DTEXF_LINEAR
);
320 IDirect3DDevice9_SetSamplerState(priv
->d3d_device
, 0, D3DSAMP_MAGFILTER
, D3DTEXF_LINEAR
);
325 /** @brief Fill D3D Presentation parameters
327 static void fill_d3d_presentparams(D3DPRESENT_PARAMETERS
*present_params
)
329 /* Prepare Direct3D initialization parameters. */
330 memset(present_params
, 0, sizeof(D3DPRESENT_PARAMETERS
));
331 present_params
->Windowed
= TRUE
;
332 present_params
->SwapEffect
= D3DSWAPEFFECT_COPY
;
333 present_params
->Flags
= D3DPRESENTFLAG_VIDEO
;
334 present_params
->hDeviceWindow
= vo_w32_window
; /* w32_common var */
335 present_params
->BackBufferWidth
= 0; /* Fill up window Width */
336 present_params
->BackBufferHeight
= 0; /* Fill up window Height */
337 present_params
->MultiSampleType
= D3DMULTISAMPLE_NONE
;
338 /* D3DPRESENT_INTERVAL_ONE = vsync */
339 present_params
->PresentationInterval
= D3DPRESENT_INTERVAL_ONE
;
340 present_params
->BackBufferFormat
= priv
->desktop_fmt
;
341 present_params
->BackBufferCount
= 1;
342 present_params
->EnableAutoDepthStencil
= FALSE
;
345 /** @brief Configure initial Direct3D context. The first
346 * function called to initialize the D3D context.
347 * @return 1 on success, 0 on failure
349 static int configure_d3d(void)
351 D3DPRESENT_PARAMETERS present_params
;
352 D3DDISPLAYMODE disp_mode
;
354 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d><INFO>configure_d3d called\n");
356 /* Get the current desktop display mode, so we can set up a back buffer
357 * of the same format. */
358 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv
->d3d_handle
,
361 mp_msg(MSGT_VO
, MSGL_ERR
,
362 "<vo_direct3d><INFO>Could not read adapter display mode.\n");
366 /* Write current Desktop's colorspace format in the global storage. */
367 priv
->desktop_fmt
= disp_mode
.Format
;
369 fill_d3d_presentparams(&present_params
);
371 /* vo_w32_window is w32_common variable. It's a handle to the window. */
372 if (FAILED(IDirect3D9_CreateDevice(priv
->d3d_handle
,
374 D3DDEVTYPE_HAL
, vo_w32_window
,
375 D3DCREATE_SOFTWARE_VERTEXPROCESSING
,
376 &present_params
, &priv
->d3d_device
))) {
377 mp_msg(MSGT_VO
, MSGL_ERR
,
378 "<vo_direct3d><INFO>Could not create the D3D device\n");
382 if (!create_d3d_surfaces())
385 mp_msg(MSGT_VO
, MSGL_V
,
386 "New BackBuffer: Width: %d, Height:%d. VO Dest Width:%d, Height: %d\n",
387 present_params
.BackBufferWidth
, present_params
.BackBufferHeight
,
388 vo_dwidth
, vo_dheight
);
395 /** @brief Reconfigure the whole Direct3D. Called only
396 * when the video adapter becomes uncooperative.
397 * @return 1 on success, 0 on failure
399 static int reconfigure_d3d(void)
401 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d><INFO>reconfigure_d3d called.\n");
403 /* Destroy the Offscreen and Backbuffer surfaces */
404 destroy_d3d_surfaces();
406 /* Destroy the D3D Device */
407 if (priv
->d3d_device
)
408 IDirect3DDevice9_Release(priv
->d3d_device
);
409 priv
->d3d_device
= NULL
;
411 /* Stop the whole Direct3D */
412 IDirect3D9_Release(priv
->d3d_handle
);
414 /* Initialize Direct3D from the beginning */
415 priv
->d3d_handle
= Direct3DCreate9(D3D_SDK_VERSION
);
416 if (!priv
->d3d_handle
) {
417 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Unable to initialize Direct3D\n");
421 /* Configure Direct3D */
422 if (!configure_d3d())
429 /** @brief Resize Direct3D context on window resize.
430 * @return 1 on success, 0 on failure
432 static int resize_d3d(void)
434 D3DPRESENT_PARAMETERS present_params
;
436 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d><INFO>resize_d3d called.\n");
438 destroy_d3d_surfaces();
440 /* Reset the D3D Device with all parameters the same except the new
443 fill_d3d_presentparams(&present_params
);
444 if (FAILED(IDirect3DDevice9_Reset(priv
->d3d_device
, &present_params
))) {
445 mp_msg(MSGT_VO
, MSGL_ERR
,
446 "<vo_direct3d><INFO>Could not reset the D3D device\n");
450 if (!create_d3d_surfaces())
453 mp_msg(MSGT_VO
, MSGL_V
,
454 "New BackBuffer: Width: %d, Height:%d. VO Dest Width:%d, Height: %d\n",
455 present_params
.BackBufferWidth
, present_params
.BackBufferHeight
,
456 vo_dwidth
, vo_dheight
);
460 #ifdef CONFIG_FREETYPE
461 // font needs to be adjusted
464 // OSD needs to be drawn fresh for new size
465 vo_osd_changed(OSDTYPE_OSD
);
470 /** @brief Uninitialize Direct3D and close the window.
472 static void uninit_d3d(void)
474 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>uninit_d3d called\r\n");
476 destroy_d3d_surfaces();
478 /* Destroy the D3D Device */
479 if (priv
->d3d_device
)
480 IDirect3DDevice9_Release(priv
->d3d_device
);
481 priv
->d3d_device
= NULL
;
483 /* Stop the whole D3D. */
484 if (priv
->d3d_handle
) {
485 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Calling IDirect3D9_Release\r\n");
486 IDirect3D9_Release(priv
->d3d_handle
);
488 priv
->d3d_handle
= NULL
;
491 /** @brief Render a frame on the screen.
492 * @param mpi mpi structure with the decoded frame inside
493 * @return VO_TRUE on success, VO_ERROR on failure
495 static uint32_t render_d3d_frame(mp_image_t
*mpi
)
497 /* Uncomment when direct rendering is implemented.
498 * if (mpi->flags & MP_IMGFLAG_DIRECT) ...
501 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
504 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) { /* Copy a planar frame. */
505 draw_slice(mpi
->planes
, mpi
->stride
, mpi
->w
, mpi
->h
, 0, 0);
509 /* If we're here, then we should lock the rect and copy a packed frame */
510 if (!priv
->locked_rect
.pBits
) {
511 if (FAILED(IDirect3DSurface9_LockRect(priv
->d3d_surface
,
512 &priv
->locked_rect
, NULL
, 0))) {
513 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Surface lock failure\n");
518 memcpy_pic(priv
->locked_rect
.pBits
, mpi
->planes
[0], mpi
->stride
[0],
519 mpi
->height
, priv
->locked_rect
.Pitch
, mpi
->stride
[0]);
522 /* This unlock is used for both slice_draw path and render_d3d_frame path. */
523 if (FAILED(IDirect3DSurface9_UnlockRect(priv
->d3d_surface
))) {
524 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Surface unlock failure\n");
527 priv
->locked_rect
.pBits
= NULL
;
529 if (FAILED(IDirect3DDevice9_BeginScene(priv
->d3d_device
))) {
530 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>BeginScene failed\n");
534 if (priv
->is_clear_needed
) {
535 IDirect3DDevice9_Clear(priv
->d3d_device
, 0, NULL
,
536 D3DCLEAR_TARGET
, 0, 0, 0);
537 priv
->is_clear_needed
= 0;
540 if (FAILED(IDirect3DDevice9_StretchRect(priv
->d3d_device
,
542 &priv
->fs_panscan_rect
,
544 &priv
->fs_movie_rect
,
546 mp_msg(MSGT_VO
, MSGL_ERR
,
547 "<vo_direct3d>Unable to copy the frame to the back buffer\n");
551 if (FAILED(IDirect3DDevice9_EndScene(priv
->d3d_device
))) {
552 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>EndScene failed\n");
560 /** @brief Query if movie colorspace is supported by the HW.
561 * @return 0 on failure, device capabilities (not probed
562 * currently) on success.
564 static int query_format(uint32_t movie_fmt
)
567 for (i
= 0; i
< DISPLAY_FORMAT_TABLE_ENTRIES
; i
++) {
568 if (fmt_table
[i
].mplayer_fmt
== movie_fmt
) {
569 /* Test conversion from Movie colorspace to
570 * display's target colorspace. */
571 if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv
->d3d_handle
,
575 priv
->desktop_fmt
))) {
576 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Rejected image format: %s\n",
577 vo_format_name(fmt_table
[i
].mplayer_fmt
));
581 priv
->movie_src_fmt
= fmt_table
[i
].fourcc
;
582 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Accepted image format: %s\n",
583 vo_format_name(fmt_table
[i
].mplayer_fmt
));
584 return (VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
585 | VFCAP_OSD
| VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
);
593 /****************************************************************************
597 * libvo Control / Callback functions *
601 ****************************************************************************/
606 /** @brief libvo Callback: Preinitialize the video card.
607 * Preinit the hardware just enough to be queried about
610 * @return 0 on success, -1 on failure
613 static int preinit(const char *arg
)
615 D3DDISPLAYMODE disp_mode
;
620 /* Set to zero all global variables. */
621 priv
= calloc(1, sizeof(struct global_priv
));
623 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Not enough memory\r\n");
628 > Please use subopt-helper.h for this, see vo_gl.c:preinit for
629 > an example of how to use it.
632 priv
->d3d_handle
= Direct3DCreate9(D3D_SDK_VERSION
);
633 if (!priv
->d3d_handle
) {
634 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Unable to initialize Direct3D\n");
638 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv
->d3d_handle
,
641 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Could not read display mode\n");
645 /* Store in priv->desktop_fmt the user desktop's colorspace. Usually XRGB. */
646 priv
->desktop_fmt
= disp_mode
.Format
;
648 mp_msg(MSGT_VO
, MSGL_V
, "disp_mode.Width %d, disp_mode.Height %d\n",
649 disp_mode
.Width
, disp_mode
.Height
);
651 if (FAILED(IDirect3D9_GetDeviceCaps(priv
->d3d_handle
,
655 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Could not read display capabilities\n");
659 /* Store relevant information reguarding caps of device */
660 texture_caps
= disp_caps
.TextureCaps
;
661 dev_caps
= disp_caps
.DevCaps
;
662 priv
->device_caps_power2_only
= (texture_caps
& D3DPTEXTURECAPS_POW2
) &&
663 !(texture_caps
& D3DPTEXTURECAPS_NONPOW2CONDITIONAL
);
664 priv
->device_caps_square_only
= texture_caps
& D3DPTEXTURECAPS_SQUAREONLY
;
665 priv
->device_texture_sys
= dev_caps
& D3DDEVCAPS_TEXTURESYSTEMMEMORY
;
666 priv
->max_texture_width
= disp_caps
.MaxTextureWidth
;
667 priv
->max_texture_height
= disp_caps
.MaxTextureHeight
;
669 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>device_caps_power2_only %d, device_caps_square_only %d\n"
670 "<vo_direct3d>device_texture_sys %d\n"
671 "<vo_direct3d>max_texture_width %d, max_texture_height %d\n",
672 priv
->device_caps_power2_only
, priv
->device_caps_square_only
,
673 priv
->device_texture_sys
, priv
->max_texture_width
,
674 priv
->max_texture_height
);
676 /* w32_common framework call. Configures window on the screen, gets
677 * fullscreen dimensions and does other useful stuff.
679 if (!vo_w32_init()) {
680 mp_msg(MSGT_VO
, MSGL_V
, "Unable to configure onscreen window\r\n");
689 /** @brief libvo Callback: Handle control requests.
690 * @return VO_TRUE on success, VO_NOTIMPL when not implemented
692 static int control(uint32_t request
, void *data
)
695 case VOCTRL_QUERY_FORMAT
:
696 return query_format(*(uint32_t*) data
);
697 case VOCTRL_GET_IMAGE
: /* Direct Rendering. Not implemented yet. */
698 mp_msg(MSGT_VO
, MSGL_V
,
699 "<vo_direct3d>Direct Rendering request. Not implemented yet\n");
701 case VOCTRL_DRAW_IMAGE
:
702 return render_d3d_frame(data
);
703 case VOCTRL_FULLSCREEN
:
715 case VOCTRL_GUISUPPORT
:
717 case VOCTRL_SET_EQUALIZER
:
719 case VOCTRL_GET_EQUALIZER
:
728 case VOCTRL_UPDATE_SCREENINFO
:
729 w32_update_xinerama_info();
731 case VOCTRL_SET_PANSCAN
:
734 case VOCTRL_GET_PANSCAN
:
740 /** @brief libvo Callback: Configre the Direct3D adapter.
741 * @param width Movie source width
742 * @param height Movie source height
743 * @param d_width Screen (destination) width
744 * @param d_height Screen (destination) height
745 * @param options Options bitmap
746 * @param title Window title
747 * @param format Movie colorspace format (using MPlayer's
748 * defines, e.g. IMGFMT_YUY2)
749 * @return 0 on success, VO_ERROR on failure
751 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
752 uint32_t d_height
, uint32_t options
, char *title
,
756 priv
->src_width
= width
;
757 priv
->src_height
= height
;
759 /* w32_common framework call. Creates window on the screen with
760 * the given coordinates.
762 if (!vo_w32_config(d_width
, d_height
, options
)) {
763 mp_msg(MSGT_VO
, MSGL_V
, "Unable to create onscreen window\r\n");
767 /* "config" may be called several times, so if this is not the first
768 * call, we should destroy Direct3D adapter and surfaces before
769 * calling configure_d3d, which will create them again.
772 destroy_d3d_surfaces();
774 /* Destroy the D3D Device */
775 if (priv
->d3d_device
)
776 IDirect3DDevice9_Release(priv
->d3d_device
);
777 priv
->d3d_device
= NULL
;
779 if (!configure_d3d())
782 return 0; /* Success */
785 /** @brief libvo Callback: Flip next already drawn frame on the
788 static void flip_page(void)
790 if (FAILED(IDirect3DDevice9_Present(priv
->d3d_device
, 0, 0, 0, 0))) {
791 mp_msg(MSGT_VO
, MSGL_V
,
792 "<vo_direct3d>Video adapter became uncooperative.\n");
793 mp_msg(MSGT_VO
, MSGL_ERR
, "<vo_direct3d>Trying to reinitialize it...\n");
794 if (!reconfigure_d3d()) {
795 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Reinitialization Failed.\n");
798 if (FAILED(IDirect3DDevice9_Present(priv
->d3d_device
, 0, 0, 0, 0))) {
799 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Reinitialization Failed.\n");
803 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Video adapter reinitialized.\n");
808 /** @brief libvo Callback: Uninitializes all pointers and closes
809 * all D3D related stuff,
811 static void uninit(void)
813 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Uninitialization\r\n");
816 vo_w32_uninit(); /* w32_common framework call */
821 /** @brief libvo Callback: Handles video window events.
823 static void check_events(void)
826 /* w32_common framework call. Handles video window events.
827 * Updates global libvo's vo_dwidth/vo_dheight upon resize
828 * with the new window width/height.
830 flags
= vo_w32_check_events();
831 if (flags
& VO_EVENT_RESIZE
)
834 if ((flags
& VO_EVENT_EXPOSE
) && priv
->is_paused
)
838 /** @brief libvo Callback: Draw slice
839 * @return 0 on success
841 static int draw_slice(uint8_t *src
[], int stride
[], int w
,int h
,int x
,int y
)
843 char *my_src
; /**< Pointer to the source image */
844 char *dst
; /**< Pointer to the destination image */
845 int uv_stride
; /**< Stride of the U/V planes */
847 /* Lock the offscreen surface if it's not already locked. */
848 if (!priv
->locked_rect
.pBits
) {
849 if (FAILED(IDirect3DSurface9_LockRect(priv
->d3d_surface
,
850 &priv
->locked_rect
, NULL
, 0))) {
851 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>Surface lock failure\n");
856 uv_stride
= priv
->locked_rect
.Pitch
/ 2;
859 dst
= priv
->locked_rect
.pBits
;
860 dst
= dst
+ priv
->locked_rect
.Pitch
* y
+ x
;
862 memcpy_pic(dst
, my_src
, w
, h
, priv
->locked_rect
.Pitch
, stride
[0]);
870 dst
= priv
->locked_rect
.pBits
;
871 dst
= dst
+ priv
->locked_rect
.Pitch
* priv
->src_height
873 if (priv
->movie_src_fmt
== MAKEFOURCC('Y','V','1','2'))
878 memcpy_pic(dst
, my_src
, w
, h
, uv_stride
, stride
[1]);
881 dst
= priv
->locked_rect
.pBits
;
882 dst
= dst
+ priv
->locked_rect
.Pitch
* priv
->src_height
883 + uv_stride
* (priv
->src_height
/ 2) + uv_stride
* y
+ x
;
884 if (priv
->movie_src_fmt
== MAKEFOURCC('Y','V','1','2'))
889 memcpy_pic(dst
, my_src
, w
, h
, uv_stride
, stride
[2]);
891 return 0; /* Success */
894 /** @brief libvo Callback: Unused function
896 static int draw_frame(uint8_t *src
[])
898 mp_msg(MSGT_VO
, MSGL_V
, "<vo_direct3d>draw_frame called\n");
902 /** @brief Maps MPlayer alpha to D3D
903 * 0x0 -> transparent and discarded by alpha test
904 * 0x1 -> 0xFF to become opaque
905 * other alpha values are inverted +1 (2 = -2)
906 * These values are then inverted again with
907 the texture filter D3DBLEND_INVSRCALPHA
909 void vo_draw_alpha_l8a8(int w
, int h
, unsigned char* src
, unsigned char *srca
,
910 int srcstride
, unsigned char* dstbase
, int dststride
)
913 for (y
= 0; y
< h
; y
++) {
914 unsigned short *dst
= (unsigned short*)dstbase
;
916 for (x
= 0; x
< w
; x
++) {
917 dst
[x
] = (-srca
[x
] << 8) | src
[x
];
921 dstbase
+= dststride
;
925 /** @brief Callback function to render the OSD to the texture
927 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
928 unsigned char *srca
, int stride
)
930 D3DLOCKED_RECT locked_rect
; /**< Offscreen surface we lock in order
931 to copy MPlayer's frame inside it.*/
933 if (FAILED(IDirect3DTexture9_LockRect(priv
->d3d_texture_system
, 0,
934 &locked_rect
, NULL
, 0))) {
935 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>IDirect3DTexture9_LockRect failure.\n");
939 vo_draw_alpha_l8a8(w
, h
, src
, srca
, stride
,
940 (unsigned char *)locked_rect
.pBits
+ locked_rect
.Pitch
*y0
+ 2*x0
, locked_rect
.Pitch
);
942 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
943 if (FAILED(IDirect3DTexture9_UnlockRect(priv
->d3d_texture_system
, 0))) {
944 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>IDirect3DTexture9_UnlockRect failure.\n");
948 priv
->is_osd_populated
= 1;
951 /** @brief libvo Callback: Draw OSD/Subtitles,
953 static void draw_osd(void)
955 if (vo_osd_changed(0)) {
956 D3DLOCKED_RECT locked_rect
; /**< Offscreen surface we lock in order
957 to copy MPlayer's frame inside it.*/
960 if (FAILED(IDirect3DTexture9_LockRect(priv
->d3d_texture_system
, 0,
961 &locked_rect
, NULL
, 0))) {
962 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>IDirect3DTexture9_LockRect failure.\n");
966 /* clear the whole texture to avoid issues due to interpolation */
967 memset(locked_rect
.pBits
, 0, locked_rect
.Pitch
* priv
->osd_texture_height
);
969 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
970 if (FAILED(IDirect3DTexture9_UnlockRect(priv
->d3d_texture_system
, 0))) {
971 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>IDirect3DTexture9_UnlockRect failure.\n");
975 priv
->is_osd_populated
= 0;
976 /* required for if subs are in the boarder region */
977 priv
->is_clear_needed
= 1;
979 vo_draw_text(priv
->osd_width
, priv
->osd_height
, draw_alpha
);
981 if (!priv
->device_texture_sys
)
983 /* only DMA to the shadow if its required */
984 if (FAILED(IDirect3DDevice9_UpdateTexture(priv
->d3d_device
,
985 (IDirect3DBaseTexture9
*)priv
->d3d_texture_system
,
986 (IDirect3DBaseTexture9
*)priv
->d3d_texture_osd
))) {
987 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>IDirect3DDevice9_UpdateTexture failure.\n");
995 if (priv
->is_osd_populated
) {
997 struct_vertex osd_quad_vb
[] = {
998 {-1.0f
, 1.0f
, 0.0f
, 0, 0 },
999 { 1.0f
, 1.0f
, 0.0f
, 1, 0 },
1000 {-1.0f
,-1.0f
, 0.0f
, 0, 1 },
1001 { 1.0f
,-1.0f
, 0.0f
, 1, 1 }
1004 /* calculate the texture coordinates */
1006 osd_quad_vb
[3].tu
= (float)priv
->osd_width
/ priv
->osd_texture_width
;
1008 osd_quad_vb
[3].tv
= (float)priv
->osd_height
/ priv
->osd_texture_height
;
1010 if (FAILED(IDirect3DDevice9_BeginScene(priv
->d3d_device
))) {
1011 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>BeginScene failed\n");
1015 /* turn on alpha test */
1016 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHABLENDENABLE
, TRUE
);
1017 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHATESTENABLE
, TRUE
);
1019 /* need to use a texture here (done here as we may be able to texture from system memory) */
1020 IDirect3DDevice9_SetTexture(priv
->d3d_device
, 0,
1021 (IDirect3DBaseTexture9
*)(priv
->device_texture_sys
1022 ? priv
->d3d_texture_system
: priv
->d3d_texture_osd
));
1024 IDirect3DDevice9_SetFVF(priv
->d3d_device
, D3DFVF_MY_VERTEX
);
1025 IDirect3DDevice9_DrawPrimitiveUP(priv
->d3d_device
, D3DPT_TRIANGLESTRIP
, 2, osd_quad_vb
, sizeof(struct_vertex
));
1027 /* turn off alpha test */
1028 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHATESTENABLE
, FALSE
);
1029 IDirect3DDevice9_SetRenderState(priv
->d3d_device
, D3DRS_ALPHABLENDENABLE
, FALSE
);
1031 if (FAILED(IDirect3DDevice9_EndScene(priv
->d3d_device
))) {
1032 mp_msg(MSGT_VO
,MSGL_ERR
,"<vo_direct3d>EndScene failed\n");