options: fix autoloaded profile handling of flag options
[mplayer/greg.git] / libvo / vo_direct3d.c
blob384dcb477bd39d9b7c334f2c23425bbc785dbecd
1 /*
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.
21 #include <windows.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <d3d9.h>
25 #include "config.h"
26 #include "video_out.h"
27 #include "video_out_internal.h"
28 #include "fastmemcpy.h"
29 #include "mp_msg.h"
30 #include "aspect.h"
31 #include "w32_common.h"
32 #include "libavutil/common.h"
33 #include "font_load.h"
34 #include "sub.h"
36 static const vo_info_t info =
38 "Direct3D 9 Renderer",
39 "direct3d",
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
47 * the structure info.
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
61 in fullscreen */
62 RECT fs_panscan_rect; /**< PanScan source surface cropping in
63 fullscreen */
64 int src_width; /**< Source (movie) width */
65 int src_height; /**< Source (movie) heigth */
66 int border_x; /**< horizontal border value for OSD */
67 int border_y; /**< vertical border value for OSD */
69 D3DFORMAT movie_src_fmt; /**< Movie colorspace format (depends on
70 the movie's codec) */
71 D3DFORMAT desktop_fmt; /**< Desktop (screen) colorspace format.
72 Usually XRGB */
74 HANDLE d3d9_dll; /**< d3d9 Library HANDLE */
75 IDirect3D9 * (WINAPI *pDirect3DCreate9)(UINT); /**< pointer to Direct3DCreate9 function */
77 LPDIRECT3D9 d3d_handle; /**< Direct3D Handle */
78 LPDIRECT3DDEVICE9 d3d_device; /**< The Direct3D Adapter */
79 IDirect3DSurface9 *d3d_surface; /**< Offscreen Direct3D Surface. MPlayer
80 renders inside it. Uses colorspace
81 priv->movie_src_fmt */
82 IDirect3DTexture9 *d3d_texture_osd; /**< Direct3D Texture. Uses RGBA */
83 IDirect3DTexture9 *d3d_texture_system; /**< Direct3D Texture. System memory
84 cannot lock a normal texture. Uses RGBA */
85 IDirect3DSurface9 *d3d_backbuf; /**< Video card's back buffer (used to
86 display next frame) */
87 int cur_backbuf_width; /**< Current backbuffer width */
88 int cur_backbuf_height; /**< Current backbuffer height */
89 int is_osd_populated; /**< 1 = OSD texture has something to display,
90 0 = OSD texture is clear */
91 int device_caps_power2_only; /**< 1 = texture sizes have to be power 2
92 0 = texture sizes can be anything */
93 int device_caps_square_only; /**< 1 = textures have to be square
94 0 = textures do not have to be square */
95 int device_texture_sys; /**< 1 = device can texture from system memory
96 0 = device requires shadow */
97 int max_texture_width; /**< from the device capabilities */
98 int max_texture_height; /**< from the device capabilities */
99 int osd_width; /**< current width of the OSD */
100 int osd_height; /**< current height of the OSD */
101 int osd_texture_width; /**< current width of the OSD texture */
102 int osd_texture_height; /**< current height of the OSD texture */
103 } *priv;
105 typedef struct {
106 const unsigned int mplayer_fmt; /**< Given by MPlayer */
107 const D3DFORMAT fourcc; /**< Required by D3D's test function */
108 } struct_fmt_table;
110 /* Map table from reported MPlayer format to the required
111 fourcc. This is needed to perform the format query. */
113 static const struct_fmt_table fmt_table[] = {
114 {IMGFMT_YV12, MAKEFOURCC('Y','V','1','2')},
115 {IMGFMT_I420, MAKEFOURCC('I','4','2','0')},
116 {IMGFMT_IYUV, MAKEFOURCC('I','Y','U','V')},
117 {IMGFMT_YVU9, MAKEFOURCC('Y','V','U','9')},
118 {IMGFMT_YUY2, D3DFMT_YUY2},
119 {IMGFMT_UYVY, D3DFMT_UYVY},
120 {IMGFMT_BGR32, D3DFMT_X8R8G8B8},
121 {IMGFMT_RGB32, D3DFMT_X8B8G8R8},
122 {IMGFMT_BGR24, D3DFMT_R8G8B8}, //untested
123 {IMGFMT_BGR16, D3DFMT_R5G6B5},
124 {IMGFMT_BGR15, D3DFMT_X1R5G5B5},
125 {IMGFMT_BGR8 , D3DFMT_R3G3B2}, //untested
128 #define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0]))
130 #define D3DFVF_MY_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
132 typedef struct {
133 float x, y, z; /* Position of vertex in 3D space */
134 float tu, tv; /* Texture coordinates */
135 } struct_vertex;
137 typedef enum back_buffer_action {
138 BACKBUFFER_CREATE,
139 BACKBUFFER_RESET
140 } back_buffer_action_e;
141 /****************************************************************************
145 * Direct3D specific implementation functions *
149 ****************************************************************************/
151 /** @brief Calculate scaled fullscreen movie rectangle with
152 * preserved aspect ratio.
154 static void calc_fs_rect(void)
156 struct vo_rect src_rect;
157 struct vo_rect dst_rect;
158 struct vo_rect borders;
159 calc_src_dst_rects(priv->src_width, priv->src_height, &src_rect, &dst_rect, &borders, NULL);
161 priv->fs_movie_rect.left = dst_rect.left;
162 priv->fs_movie_rect.right = dst_rect.right;
163 priv->fs_movie_rect.top = dst_rect.top;
164 priv->fs_movie_rect.bottom = dst_rect.bottom;
165 priv->fs_panscan_rect.left = src_rect.left;
166 priv->fs_panscan_rect.right = src_rect.right;
167 priv->fs_panscan_rect.top = src_rect.top;
168 priv->fs_panscan_rect.bottom = src_rect.bottom;
169 priv->border_x = borders.left;
170 priv->border_y = borders.top;
172 mp_msg(MSGT_VO, MSGL_V,
173 "<vo_direct3d>Fullscreen movie rectangle: t: %ld, l: %ld, r: %ld, b:%ld\n",
174 priv->fs_movie_rect.top, priv->fs_movie_rect.left,
175 priv->fs_movie_rect.right, priv->fs_movie_rect.bottom);
177 /* The backbuffer should be cleared before next StretchRect. This is
178 * necessary because our new draw area could be smaller than the
179 * previous one used by StretchRect and without it, leftovers from the
180 * previous frame will be left. */
181 priv->is_clear_needed = 1;
184 /** @brief Destroy D3D Offscreen and Backbuffer surfaces.
186 static void destroy_d3d_surfaces(void)
188 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>destroy_d3d_surfaces called.\n");
189 /* Let's destroy the old (if any) D3D Surfaces */
191 if (priv->locked_rect.pBits)
192 IDirect3DSurface9_UnlockRect(priv->d3d_surface);
193 priv->locked_rect.pBits = NULL;
195 if (priv->d3d_surface)
196 IDirect3DSurface9_Release(priv->d3d_surface);
197 priv->d3d_surface = NULL;
199 /* kill the OSD texture and its shadow copy */
200 if (priv->d3d_texture_osd)
201 IDirect3DTexture9_Release(priv->d3d_texture_osd);
202 priv->d3d_texture_osd = NULL;
204 if (priv->d3d_texture_system)
205 IDirect3DTexture9_Release(priv->d3d_texture_system);
206 priv->d3d_texture_system = NULL;
208 if (priv->d3d_backbuf)
209 IDirect3DSurface9_Release(priv->d3d_backbuf);
210 priv->d3d_backbuf = NULL;
213 /** @brief Create D3D Offscreen and Backbuffer surfaces. Each
214 * surface is created only if it's not already present.
215 * @return 1 on success, 0 on failure
217 static int create_d3d_surfaces(void)
219 int osd_width = vo_dwidth, osd_height = vo_dheight;
220 int tex_width = osd_width, tex_height = osd_height;
221 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>create_d3d_surfaces called.\n");
223 if (!priv->d3d_surface &&
224 FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
225 priv->d3d_device, priv->src_width, priv->src_height,
226 priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) {
227 mp_msg(MSGT_VO, MSGL_ERR,
228 "<vo_direct3d>Allocating offscreen surface failed.\n");
229 return 0;
232 if (!priv->d3d_backbuf &&
233 FAILED(IDirect3DDevice9_GetBackBuffer(priv->d3d_device, 0, 0,
234 D3DBACKBUFFER_TYPE_MONO,
235 &priv->d3d_backbuf))) {
236 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating backbuffer failed.\n");
237 return 0;
240 /* calculate the best size for the OSD depending on the factors from the device */
241 if (priv->device_caps_power2_only) {
242 tex_width = 1;
243 tex_height = 1;
244 while (tex_width < osd_width ) tex_width <<= 1;
245 while (tex_height < osd_height) tex_height <<= 1;
247 if (priv->device_caps_square_only)
248 /* device only supports square textures */
249 tex_width = tex_height = tex_width > tex_height ? tex_width : tex_height;
250 // better round up to a multiple of 16
251 tex_width = (tex_width + 15) & ~15;
252 tex_height = (tex_height + 15) & ~15;
254 // make sure we respect the size limits without breaking aspect or pow2-requirements
255 while (tex_width > priv->max_texture_width || tex_height > priv->max_texture_height) {
256 osd_width >>= 1;
257 osd_height >>= 1;
258 tex_width >>= 1;
259 tex_height >>= 1;
262 priv->osd_width = osd_width;
263 priv->osd_height = osd_height;
264 priv->osd_texture_width = tex_width;
265 priv->osd_texture_height = tex_height;
267 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>OSD texture size (%dx%d), requested (%dx%d).\n",
268 vo_dwidth, vo_dheight, priv->osd_texture_width, priv->osd_texture_height);
270 /* create OSD */
271 if (!priv->d3d_texture_system &&
272 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
273 priv->osd_texture_width,
274 priv->osd_texture_height,
276 D3DUSAGE_DYNAMIC,
277 D3DFMT_A8L8,
278 D3DPOOL_SYSTEMMEM,
279 &priv->d3d_texture_system,
280 NULL))) {
281 mp_msg(MSGT_VO,MSGL_ERR,
282 "<vo_direct3d>Allocating OSD texture in system RAM failed.\n");
283 return 0;
286 if (!priv->device_texture_sys) {
287 /* only create if we need a shadow version on the external device */
288 if (!priv->d3d_texture_osd &&
289 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
290 priv->osd_texture_width,
291 priv->osd_texture_height,
293 D3DUSAGE_DYNAMIC,
294 D3DFMT_A8L8,
295 D3DPOOL_DEFAULT,
296 &priv->d3d_texture_osd,
297 NULL))) {
298 mp_msg(MSGT_VO,MSGL_ERR,
299 "<vo_direct3d>Allocating OSD texture in video RAM failed.\n");
300 return 0;
304 /* setup default renderstate */
305 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_ONE);
306 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
307 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
308 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAREF, (DWORD)0x0);
309 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_LIGHTING, FALSE);
310 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
311 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
313 return 1;
316 /** @brief Fill D3D Presentation parameters
318 static void fill_d3d_presentparams(D3DPRESENT_PARAMETERS *present_params)
320 /* Prepare Direct3D initialization parameters. */
321 memset(present_params, 0, sizeof(D3DPRESENT_PARAMETERS));
322 present_params->Windowed = TRUE;
323 present_params->SwapEffect = D3DSWAPEFFECT_COPY;
324 present_params->Flags = D3DPRESENTFLAG_VIDEO;
325 present_params->hDeviceWindow = vo_w32_window; /* w32_common var */
326 present_params->BackBufferWidth = priv->cur_backbuf_width;
327 present_params->BackBufferHeight = priv->cur_backbuf_height;
328 present_params->MultiSampleType = D3DMULTISAMPLE_NONE;
329 present_params->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
330 present_params->BackBufferFormat = priv->desktop_fmt;
331 present_params->BackBufferCount = 1;
332 present_params->EnableAutoDepthStencil = FALSE;
336 /** @brief Create a new backbuffer. Create or Reset the D3D
337 * device.
338 * @return 1 on success, 0 on failure
340 static int change_d3d_backbuffer(back_buffer_action_e action)
342 D3DPRESENT_PARAMETERS present_params;
344 destroy_d3d_surfaces();
346 /* Grow the backbuffer in the required dimension. */
347 if (vo_dwidth > priv->cur_backbuf_width)
348 priv->cur_backbuf_width = vo_dwidth;
350 if (vo_dheight > priv->cur_backbuf_height)
351 priv->cur_backbuf_height = vo_dheight;
353 /* The grown backbuffer dimensions are ready and fill_d3d_presentparams
354 * will use them, so we can reset the device.
356 fill_d3d_presentparams(&present_params);
358 /* vo_w32_window is w32_common variable. It's a handle to the window. */
359 if (action == BACKBUFFER_CREATE &&
360 FAILED(IDirect3D9_CreateDevice(priv->d3d_handle,
361 D3DADAPTER_DEFAULT,
362 D3DDEVTYPE_HAL, vo_w32_window,
363 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
364 &present_params, &priv->d3d_device))) {
365 mp_msg(MSGT_VO, MSGL_V,
366 "<vo_direct3d>Creating Direct3D device failed.\n");
367 return 0;
370 if (action == BACKBUFFER_RESET &&
371 FAILED(IDirect3DDevice9_Reset(priv->d3d_device, &present_params))) {
372 mp_msg(MSGT_VO, MSGL_ERR,
373 "<vo_direct3d>Reseting Direct3D device failed.\n");
374 return 0;
377 mp_msg(MSGT_VO, MSGL_V,
378 "<vo_direct3d>New backbuffer (%dx%d), VO (%dx%d)\n",
379 present_params.BackBufferWidth, present_params.BackBufferHeight,
380 vo_dwidth, vo_dheight);
382 return 1;
385 /** @brief Configure initial Direct3D context. The first
386 * function called to initialize the D3D context.
387 * @return 1 on success, 0 on failure
389 static int configure_d3d(void)
391 D3DDISPLAYMODE disp_mode;
392 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
394 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>configure_d3d called.\n");
396 destroy_d3d_surfaces();
398 /* Get the current desktop display mode, so we can set up a back buffer
399 * of the same format. */
400 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
401 D3DADAPTER_DEFAULT,
402 &disp_mode))) {
403 mp_msg(MSGT_VO, MSGL_ERR,
404 "<vo_direct3d>Reading adapter display mode failed.\n");
405 return 0;
408 /* Write current Desktop's colorspace format in the global storage. */
409 priv->desktop_fmt = disp_mode.Format;
411 if (!change_d3d_backbuffer(BACKBUFFER_CREATE))
412 return 0;
414 if (!create_d3d_surfaces())
415 return 0;
417 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
418 &vp))) {
419 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
420 return 0;
423 calc_fs_rect();
425 return 1;
428 /** @brief Reconfigure the whole Direct3D. Called only
429 * when the video adapter becomes uncooperative.
430 * @return 1 on success, 0 on failure
432 static int reconfigure_d3d(void)
434 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>reconfigure_d3d called.\n");
436 /* Destroy the offscreen, OSD and backbuffer surfaces */
437 destroy_d3d_surfaces();
439 /* Destroy the D3D Device */
440 if (priv->d3d_device)
441 IDirect3DDevice9_Release(priv->d3d_device);
442 priv->d3d_device = NULL;
444 /* Stop the whole Direct3D */
445 IDirect3D9_Release(priv->d3d_handle);
447 /* Initialize Direct3D from the beginning */
448 priv->d3d_handle = priv->pDirect3DCreate9(D3D_SDK_VERSION);
449 if (!priv->d3d_handle) {
450 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
451 return 0;
454 /* Configure Direct3D */
455 if (!configure_d3d())
456 return 0;
458 return 1;
461 /** @brief Resize Direct3D context on window resize.
462 * @return 1 on success, 0 on failure
464 static int resize_d3d(void)
466 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
468 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>resize_d3d called.\n");
470 /* Make sure that backbuffer is large enough to accomodate the new
471 viewport dimensions. Grow it if necessary. */
473 if (vo_dwidth > priv->cur_backbuf_width ||
474 vo_dheight > priv->cur_backbuf_height) {
475 if (!change_d3d_backbuffer(BACKBUFFER_RESET))
476 return 0;
479 /* Destroy the OSD textures. They should always match the new dimensions
480 * of the onscreen window, so on each resize we need new OSD dimensions.
483 if (priv->d3d_texture_osd)
484 IDirect3DTexture9_Release(priv->d3d_texture_osd);
485 priv->d3d_texture_osd = NULL;
487 if (priv->d3d_texture_system)
488 IDirect3DTexture9_Release(priv->d3d_texture_system);
489 priv->d3d_texture_system = NULL;
492 /* Recreate the OSD. The function will observe that the offscreen plain
493 * surface and the backbuffer are not destroyed and will skip their creation,
494 * effectively recreating only the OSD.
497 if (!create_d3d_surfaces())
498 return 0;
500 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
501 &vp))) {
502 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
503 return 0;
506 calc_fs_rect();
508 #ifdef CONFIG_FREETYPE
509 // font needs to be adjusted
510 force_load_font = 1;
511 #endif
512 // OSD needs to be drawn fresh for new size
513 vo_osd_changed(OSDTYPE_OSD);
515 return 1;
518 /** @brief Uninitialize Direct3D and close the window.
520 static void uninit_d3d(void)
522 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit_d3d called.\n");
524 destroy_d3d_surfaces();
526 /* Destroy the D3D Device */
527 if (priv->d3d_device)
528 IDirect3DDevice9_Release(priv->d3d_device);
529 priv->d3d_device = NULL;
531 /* Stop the whole D3D. */
532 if (priv->d3d_handle) {
533 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Stopping Direct3D.\n");
534 IDirect3D9_Release(priv->d3d_handle);
536 priv->d3d_handle = NULL;
539 /** @brief Render a frame on the screen.
540 * @param mpi mpi structure with the decoded frame inside
541 * @return VO_TRUE on success, VO_ERROR on failure
543 static uint32_t render_d3d_frame(mp_image_t *mpi)
545 /* Uncomment when direct rendering is implemented.
546 * if (mpi->flags & MP_IMGFLAG_DIRECT) ...
549 /* If the D3D device is uncooperative (not initialized), return success.
550 The device will be probed for reinitialization in the next flip_page() */
551 if (!priv->d3d_device)
552 return VO_TRUE;
554 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
555 goto skip_upload;
557 if (mpi->flags & MP_IMGFLAG_PLANAR) { /* Copy a planar frame. */
558 draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
559 goto skip_upload;
562 /* If we're here, then we should lock the rect and copy a packed frame */
563 if (!priv->locked_rect.pBits) {
564 if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface,
565 &priv->locked_rect, NULL, 0))) {
566 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Surface lock failed.\n");
567 return VO_ERROR;
571 memcpy_pic(priv->locked_rect.pBits, mpi->planes[0], mpi->stride[0],
572 mpi->height, priv->locked_rect.Pitch, mpi->stride[0]);
574 skip_upload:
575 /* This unlock is used for both slice_draw path and render_d3d_frame path. */
576 if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface))) {
577 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface unlock failed.\n");
578 return VO_ERROR;
580 priv->locked_rect.pBits = NULL;
582 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
583 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>BeginScene failed.\n");
584 return VO_ERROR;
587 if (priv->is_clear_needed) {
588 IDirect3DDevice9_Clear(priv->d3d_device, 0, NULL,
589 D3DCLEAR_TARGET, 0, 0, 0);
590 priv->is_clear_needed = 0;
593 if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device,
594 priv->d3d_surface,
595 &priv->fs_panscan_rect,
596 priv->d3d_backbuf,
597 &priv->fs_movie_rect,
598 D3DTEXF_LINEAR))) {
599 mp_msg(MSGT_VO, MSGL_ERR,
600 "<vo_direct3d>Copying frame to the backbuffer failed.\n");
601 return VO_ERROR;
604 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
605 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>EndScene failed.\n");
606 return VO_ERROR;
609 return VO_TRUE;
613 /** @brief Query if movie colorspace is supported by the HW.
614 * @return 0 on failure, device capabilities (not probed
615 * currently) on success.
617 static int query_format(uint32_t movie_fmt)
619 int i;
620 for (i = 0; i < DISPLAY_FORMAT_TABLE_ENTRIES; i++) {
621 if (fmt_table[i].mplayer_fmt == movie_fmt) {
622 /* Test conversion from Movie colorspace to
623 * display's target colorspace. */
624 if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle,
625 D3DADAPTER_DEFAULT,
626 D3DDEVTYPE_HAL,
627 fmt_table[i].fourcc,
628 priv->desktop_fmt))) {
629 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Rejected image format: %s\n",
630 vo_format_name(fmt_table[i].mplayer_fmt));
631 return 0;
634 priv->movie_src_fmt = fmt_table[i].fourcc;
635 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Accepted image format: %s\n",
636 vo_format_name(fmt_table[i].mplayer_fmt));
637 return (VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
638 | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN);
643 return 0;
646 /****************************************************************************
650 * libvo Control / Callback functions *
654 ****************************************************************************/
659 /** @brief libvo Callback: Preinitialize the video card.
660 * Preinit the hardware just enough to be queried about
661 * supported formats.
663 * @return 0 on success, -1 on failure
666 static int preinit(const char *arg)
668 D3DDISPLAYMODE disp_mode;
669 D3DCAPS9 disp_caps;
670 DWORD texture_caps;
671 DWORD dev_caps;
673 /* Set to zero all global variables. */
674 priv = calloc(1, sizeof(struct global_priv));
675 if (!priv) {
676 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating private memory failed.\n");
677 goto err_out;
680 /* FIXME
681 > Please use subopt-helper.h for this, see vo_gl.c:preinit for
682 > an example of how to use it.
685 priv->d3d9_dll = LoadLibraryA("d3d9.dll");
686 if (!priv->d3d9_dll) {
687 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Unable to dynamically load d3d9.dll\n");
688 goto err_out;
691 priv->pDirect3DCreate9 = (void *)GetProcAddress(priv->d3d9_dll, "Direct3DCreate9");
692 if (!priv->pDirect3DCreate9) {
693 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Unable to find entry point of Direct3DCreate9\n");
694 goto err_out;
697 priv->d3d_handle = priv->pDirect3DCreate9(D3D_SDK_VERSION);
698 if (!priv->d3d_handle) {
699 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
700 goto err_out;
703 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
704 D3DADAPTER_DEFAULT,
705 &disp_mode))) {
706 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display mode failed.\n");
707 goto err_out;
710 /* Store in priv->desktop_fmt the user desktop's colorspace. Usually XRGB. */
711 priv->desktop_fmt = disp_mode.Format;
712 priv->cur_backbuf_width = disp_mode.Width;
713 priv->cur_backbuf_height = disp_mode.Height;
715 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Setting backbuffer dimensions to (%dx%d).\n",
716 disp_mode.Width, disp_mode.Height);
718 if (FAILED(IDirect3D9_GetDeviceCaps(priv->d3d_handle,
719 D3DADAPTER_DEFAULT,
720 D3DDEVTYPE_HAL,
721 &disp_caps))) {
722 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display capabilities failed.\n");
723 goto err_out;
726 /* Store relevant information reguarding caps of device */
727 texture_caps = disp_caps.TextureCaps;
728 dev_caps = disp_caps.DevCaps;
729 priv->device_caps_power2_only = (texture_caps & D3DPTEXTURECAPS_POW2) &&
730 !(texture_caps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
731 priv->device_caps_square_only = texture_caps & D3DPTEXTURECAPS_SQUAREONLY;
732 priv->device_texture_sys = dev_caps & D3DDEVCAPS_TEXTURESYSTEMMEMORY;
733 priv->max_texture_width = disp_caps.MaxTextureWidth;
734 priv->max_texture_height = disp_caps.MaxTextureHeight;
736 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>device_caps_power2_only %d, device_caps_square_only %d\n"
737 "<vo_direct3d>device_texture_sys %d\n"
738 "<vo_direct3d>max_texture_width %d, max_texture_height %d\n",
739 priv->device_caps_power2_only, priv->device_caps_square_only,
740 priv->device_texture_sys, priv->max_texture_width,
741 priv->max_texture_height);
743 /* w32_common framework call. Configures window on the screen, gets
744 * fullscreen dimensions and does other useful stuff.
746 if (!vo_w32_init()) {
747 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Configuring onscreen window failed.\n");
748 goto err_out;
751 return 0;
753 err_out:
754 uninit();
755 return -1;
760 /** @brief libvo Callback: Handle control requests.
761 * @return VO_TRUE on success, VO_NOTIMPL when not implemented
763 static int control(uint32_t request, void *data)
765 switch (request) {
766 case VOCTRL_QUERY_FORMAT:
767 return query_format(*(uint32_t*) data);
768 case VOCTRL_GET_IMAGE: /* Direct Rendering. Not implemented yet. */
769 mp_msg(MSGT_VO, MSGL_V,
770 "<vo_direct3d>Direct Rendering request. Not implemented yet.\n");
771 return VO_NOTIMPL;
772 case VOCTRL_DRAW_IMAGE:
773 return render_d3d_frame(data);
774 case VOCTRL_FULLSCREEN:
775 vo_w32_fullscreen();
776 resize_d3d();
777 return VO_TRUE;
778 case VOCTRL_RESET:
779 return VO_NOTIMPL;
780 case VOCTRL_PAUSE:
781 priv->is_paused = 1;
782 return VO_TRUE;
783 case VOCTRL_RESUME:
784 priv->is_paused = 0;
785 return VO_TRUE;
786 case VOCTRL_SET_EQUALIZER:
787 return VO_NOTIMPL;
788 case VOCTRL_GET_EQUALIZER:
789 return VO_NOTIMPL;
790 case VOCTRL_ONTOP:
791 vo_w32_ontop();
792 return VO_TRUE;
793 case VOCTRL_BORDER:
794 vo_w32_border();
795 resize_d3d();
796 return VO_TRUE;
797 case VOCTRL_UPDATE_SCREENINFO:
798 w32_update_xinerama_info();
799 return VO_TRUE;
800 case VOCTRL_SET_PANSCAN:
801 calc_fs_rect();
802 return VO_TRUE;
803 case VOCTRL_GET_PANSCAN:
804 return VO_TRUE;
806 return VO_FALSE;
809 /** @brief libvo Callback: Configre the Direct3D adapter.
810 * @param width Movie source width
811 * @param height Movie source height
812 * @param d_width Screen (destination) width
813 * @param d_height Screen (destination) height
814 * @param options Options bitmap
815 * @param title Window title
816 * @param format Movie colorspace format (using MPlayer's
817 * defines, e.g. IMGFMT_YUY2)
818 * @return 0 on success, VO_ERROR on failure
820 static int config(uint32_t width, uint32_t height, uint32_t d_width,
821 uint32_t d_height, uint32_t options, char *title,
822 uint32_t format)
825 priv->src_width = width;
826 priv->src_height = height;
828 /* w32_common framework call. Creates window on the screen with
829 * the given coordinates.
831 if (!vo_w32_config(d_width, d_height, options)) {
832 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Creating onscreen window failed.\n");
833 return VO_ERROR;
836 /* "config" may be called several times, so if this is not the first
837 * call, we should destroy Direct3D adapter and surfaces before
838 * calling configure_d3d, which will create them again.
841 destroy_d3d_surfaces();
843 /* Destroy the D3D Device */
844 if (priv->d3d_device)
845 IDirect3DDevice9_Release(priv->d3d_device);
846 priv->d3d_device = NULL;
848 if (!configure_d3d())
849 return VO_ERROR;
851 return 0; /* Success */
854 /** @brief libvo Callback: Flip next already drawn frame on the
855 * screen.
857 static void flip_page(void)
859 RECT rect = {0, 0, vo_dwidth, vo_dheight};
860 if (!priv->d3d_device ||
861 FAILED(IDirect3DDevice9_Present(priv->d3d_device, &rect, 0, 0, 0))) {
862 mp_msg(MSGT_VO, MSGL_V,
863 "<vo_direct3d>Trying to reinitialize uncooperative video adapter.\n");
864 if (!reconfigure_d3d()) {
865 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Reinitialization failed.\n");
866 return;
868 else
869 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Video adapter reinitialized.\n");
873 /** @brief libvo Callback: Uninitializes all pointers and closes
874 * all D3D related stuff,
876 static void uninit(void)
878 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit called.\n");
880 uninit_d3d();
881 vo_w32_uninit(); /* w32_common framework call */
882 if (priv->d3d9_dll)
883 FreeLibrary(priv->d3d9_dll);
884 priv->d3d9_dll = NULL;
885 free(priv);
886 priv = NULL;
889 /** @brief libvo Callback: Handles video window events.
891 static void check_events(void)
893 int flags;
894 /* w32_common framework call. Handles video window events.
895 * Updates global libvo's vo_dwidth/vo_dheight upon resize
896 * with the new window width/height.
898 flags = vo_w32_check_events();
899 if (flags & VO_EVENT_RESIZE)
900 resize_d3d();
902 if ((flags & VO_EVENT_EXPOSE) && priv->is_paused)
903 flip_page();
906 /** @brief libvo Callback: Draw slice
907 * @return 0 on success
909 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y )
911 char *my_src; /**< Pointer to the source image */
912 char *dst; /**< Pointer to the destination image */
913 int uv_stride; /**< Stride of the U/V planes */
915 /* If the D3D device is uncooperative (not initialized), return success.
916 The device will be probed for reinitialization in the next flip_page() */
917 if (!priv->d3d_device)
918 return 0;
920 /* Lock the offscreen surface if it's not already locked. */
921 if (!priv->locked_rect.pBits) {
922 if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface,
923 &priv->locked_rect, NULL, 0))) {
924 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface lock failure.\n");
925 return VO_FALSE;
929 uv_stride = priv->locked_rect.Pitch / 2;
931 /* Copy Y */
932 dst = priv->locked_rect.pBits;
933 dst = dst + priv->locked_rect.Pitch * y + x;
934 my_src = src[0];
935 memcpy_pic(dst, my_src, w, h, priv->locked_rect.Pitch, stride[0]);
937 w /= 2;
938 h /= 2;
939 x /= 2;
940 y /= 2;
942 /* Copy U */
943 dst = priv->locked_rect.pBits;
944 dst = dst + priv->locked_rect.Pitch * priv->src_height
945 + uv_stride * y + x;
946 if (priv->movie_src_fmt == MAKEFOURCC('Y','V','1','2'))
947 my_src = src[2];
948 else
949 my_src = src[1];
951 memcpy_pic(dst, my_src, w, h, uv_stride, stride[1]);
953 /* Copy V */
954 dst = priv->locked_rect.pBits;
955 dst = dst + priv->locked_rect.Pitch * priv->src_height
956 + uv_stride * (priv->src_height / 2) + uv_stride * y + x;
957 if (priv->movie_src_fmt == MAKEFOURCC('Y','V','1','2'))
958 my_src=src[1];
959 else
960 my_src=src[2];
962 memcpy_pic(dst, my_src, w, h, uv_stride, stride[2]);
964 return 0; /* Success */
967 /** @brief libvo Callback: Unused function
969 static int draw_frame(uint8_t *src[])
971 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>draw_frame called.\n");
972 return VO_FALSE;
975 /** @brief Maps MPlayer alpha to D3D
976 * 0x0 -> transparent and discarded by alpha test
977 * 0x1 -> 0xFF to become opaque
978 * other alpha values are inverted +1 (2 = -2)
979 * These values are then inverted again with
980 the texture filter D3DBLEND_INVSRCALPHA
982 void vo_draw_alpha_l8a8(int w, int h, unsigned char* src, unsigned char *srca,
983 int srcstride, unsigned char* dstbase, int dststride)
985 int y;
986 for (y = 0; y < h; y++) {
987 unsigned short *dst = (unsigned short*)dstbase;
988 int x;
989 for (x = 0; x < w; x++) {
990 dst[x] = (-srca[x] << 8) | src[x];
992 src += srcstride;
993 srca += srcstride;
994 dstbase += dststride;
998 /** @brief Callback function to render the OSD to the texture
1000 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
1001 unsigned char *srca, int stride)
1003 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
1004 to copy MPlayer's frame inside it.*/
1006 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
1007 &locked_rect, NULL, 0))) {
1008 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture lock failed.\n");
1009 return;
1012 vo_draw_alpha_l8a8(w, h, src, srca, stride,
1013 (unsigned char *)locked_rect.pBits + locked_rect.Pitch*y0 + 2*x0, locked_rect.Pitch);
1015 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1016 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
1017 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture unlock failed.\n");
1018 return;
1021 priv->is_osd_populated = 1;
1024 /** @brief libvo Callback: Draw OSD/Subtitles,
1026 static void draw_osd(void)
1028 // we can not render OSD if we lost the device e.g. because it was uncooperative
1029 if (!priv->d3d_device)
1030 return;
1032 if (vo_osd_changed(0)) {
1033 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
1034 to copy MPlayer's frame inside it.*/
1036 /* clear the OSD */
1037 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
1038 &locked_rect, NULL, 0))) {
1039 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture lock failed.\n");
1040 return;
1043 /* clear the whole texture to avoid issues due to interpolation */
1044 memset(locked_rect.pBits, 0, locked_rect.Pitch * priv->osd_texture_height);
1046 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1047 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
1048 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture unlock failed.\n");
1049 return;
1052 priv->is_osd_populated = 0;
1053 /* required for if subs are in the boarder region */
1054 priv->is_clear_needed = 1;
1056 vo_draw_text_ext(priv->osd_width, priv->osd_height, priv->border_x, priv->border_y,
1057 priv->border_x, priv->border_y, priv->src_width, priv->src_height, draw_alpha);
1059 if (!priv->device_texture_sys)
1061 /* only DMA to the shadow if its required */
1062 if (FAILED(IDirect3DDevice9_UpdateTexture(priv->d3d_device,
1063 (IDirect3DBaseTexture9 *)priv->d3d_texture_system,
1064 (IDirect3DBaseTexture9 *)priv->d3d_texture_osd))) {
1065 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture transfer failed.\n");
1066 return;
1071 /* update OSD */
1073 if (priv->is_osd_populated) {
1075 struct_vertex osd_quad_vb[] = {
1076 {-1.0f, 1.0f, 0.0f, 0, 0 },
1077 { 1.0f, 1.0f, 0.0f, 1, 0 },
1078 {-1.0f,-1.0f, 0.0f, 0, 1 },
1079 { 1.0f,-1.0f, 0.0f, 1, 1 }
1082 /* calculate the texture coordinates */
1083 osd_quad_vb[1].tu =
1084 osd_quad_vb[3].tu = (float)priv->osd_width / priv->osd_texture_width;
1085 osd_quad_vb[2].tv =
1086 osd_quad_vb[3].tv = (float)priv->osd_height / priv->osd_texture_height;
1088 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
1089 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>BeginScene failed.\n");
1090 return;
1093 /* turn on alpha test */
1094 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, TRUE);
1095 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, TRUE);
1097 /* need to use a texture here (done here as we may be able to texture from system memory) */
1098 IDirect3DDevice9_SetTexture(priv->d3d_device, 0,
1099 (IDirect3DBaseTexture9 *)(priv->device_texture_sys
1100 ? priv->d3d_texture_system : priv->d3d_texture_osd));
1102 IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_MY_VERTEX);
1103 IDirect3DDevice9_DrawPrimitiveUP(priv->d3d_device, D3DPT_TRIANGLESTRIP, 2, osd_quad_vb, sizeof(struct_vertex));
1105 /* turn off alpha test */
1106 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, FALSE);
1107 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, FALSE);
1109 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
1110 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>EndScene failed.\n");
1111 return;