Use SetErrorMode so Windows will not show all kinds of error dialogs
[mplayer/glamo.git] / libvo / vo_direct3d.c
blob48287c238fbebd3f0b4528043a659aee895bfb9c
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 */
67 D3DFORMAT movie_src_fmt; /**< Movie colorspace format (depends on
68 the movie's codec) */
69 D3DFORMAT desktop_fmt; /**< Desktop (screen) colorspace format.
70 Usually XRGB */
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 cur_backbuf_width; /**< Current backbuffer width */
82 int cur_backbuf_height; /**< Current backbuffer height */
83 int is_osd_populated; /**< 1 = OSD texture has something to display,
84 0 = OSD texture is clear */
85 int device_caps_power2_only; /**< 1 = texture sizes have to be power 2
86 0 = texture sizes can be anything */
87 int device_caps_square_only; /**< 1 = textures have to be square
88 0 = textures do not have to be square */
89 int device_texture_sys; /**< 1 = device can texture from system memory
90 0 = device requires shadow */
91 int max_texture_width; /**< from the device capabilities */
92 int max_texture_height; /**< from the device capabilities */
93 int osd_width; /**< current width of the OSD */
94 int osd_height; /**< current height of the OSD */
95 int osd_texture_width; /**< current width of the OSD texture */
96 int osd_texture_height; /**< current height of the OSD texture */
97 } *priv;
99 typedef struct {
100 const unsigned int mplayer_fmt; /**< Given by MPlayer */
101 const D3DFORMAT fourcc; /**< Required by D3D's test function */
102 } struct_fmt_table;
104 /* Map table from reported MPlayer format to the required
105 fourcc. This is needed to perform the format query. */
107 static const struct_fmt_table fmt_table[] = {
108 {IMGFMT_YV12, MAKEFOURCC('Y','V','1','2')},
109 {IMGFMT_I420, MAKEFOURCC('I','4','2','0')},
110 {IMGFMT_IYUV, MAKEFOURCC('I','Y','U','V')},
111 {IMGFMT_YVU9, MAKEFOURCC('Y','V','U','9')},
112 {IMGFMT_YUY2, D3DFMT_YUY2},
113 {IMGFMT_UYVY, D3DFMT_UYVY},
114 {IMGFMT_BGR32, D3DFMT_X8R8G8B8},
115 {IMGFMT_RGB32, D3DFMT_X8B8G8R8},
116 {IMGFMT_BGR24, D3DFMT_R8G8B8}, //untested
117 {IMGFMT_BGR16, D3DFMT_R5G6B5},
118 {IMGFMT_BGR15, D3DFMT_X1R5G5B5},
119 {IMGFMT_BGR8 , D3DFMT_R3G3B2}, //untested
122 #define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0]))
124 #define D3DFVF_MY_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
126 typedef struct {
127 float x, y, z; /* Position of vertex in 3D space */
128 float tu, tv; /* Texture coordinates */
129 } struct_vertex;
131 typedef enum back_buffer_action {
132 BACKBUFFER_CREATE,
133 BACKBUFFER_RESET
134 } back_buffer_action_e;
135 /****************************************************************************
139 * Direct3D specific implementation functions *
143 ****************************************************************************/
145 /** @brief Calculate scaled fullscreen movie rectangle with
146 * preserved aspect ratio.
148 static void calc_fs_rect(void)
150 int scaled_height = 0;
151 int scaled_width = 0;
153 // set default values
154 priv->fs_movie_rect.left = 0;
155 priv->fs_movie_rect.right = vo_dwidth;
156 priv->fs_movie_rect.top = 0;
157 priv->fs_movie_rect.bottom = vo_dheight;
158 priv->fs_panscan_rect.left = 0;
159 priv->fs_panscan_rect.right = priv->src_width;
160 priv->fs_panscan_rect.top = 0;
161 priv->fs_panscan_rect.bottom = priv->src_height;
162 if (!vo_fs)
163 return;
165 // adjust for fullscreen aspect and panscan
166 aspect(&scaled_width, &scaled_height, A_ZOOM);
167 panscan_calc();
168 scaled_width += vo_panscan_x;
169 scaled_height += vo_panscan_y;
171 // note: border is rounded to a multiple of two since at least
172 // ATI drivers can not handle odd values with YV12 input
173 if (scaled_width > vo_dwidth) {
174 int border = priv->src_width * (scaled_width - vo_dwidth) / scaled_width;
175 border = (border / 2 + 1) & ~1;
176 priv->fs_panscan_rect.left = border;
177 priv->fs_panscan_rect.right = priv->src_width - border;
178 } else {
179 priv->fs_movie_rect.left = (vo_dwidth - scaled_width) / 2;
180 priv->fs_movie_rect.right = priv->fs_movie_rect.left + scaled_width;
182 if (scaled_height > vo_dheight) {
183 int border = priv->src_height * (scaled_height - vo_dheight) / scaled_height;
184 border = (border / 2 + 1) & ~1;
185 priv->fs_panscan_rect.top = border;
186 priv->fs_panscan_rect.bottom = priv->src_height - border;
187 } else {
188 priv->fs_movie_rect.top = (vo_dheight - scaled_height) / 2;
189 priv->fs_movie_rect.bottom = priv->fs_movie_rect.top + scaled_height;
192 mp_msg(MSGT_VO, MSGL_V,
193 "<vo_direct3d>Fullscreen movie rectangle: t: %ld, l: %ld, r: %ld, b:%ld\n",
194 priv->fs_movie_rect.top, priv->fs_movie_rect.left,
195 priv->fs_movie_rect.right, priv->fs_movie_rect.bottom);
197 /* The backbuffer should be cleared before next StretchRect. This is
198 * necessary because our new draw area could be smaller than the
199 * previous one used by StretchRect and without it, leftovers from the
200 * previous frame will be left. */
201 priv->is_clear_needed = 1;
204 /** @brief Destroy D3D Offscreen and Backbuffer surfaces.
206 static void destroy_d3d_surfaces(void)
208 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>destroy_d3d_surfaces called.\n");
209 /* Let's destroy the old (if any) D3D Surfaces */
211 if (priv->locked_rect.pBits)
212 IDirect3DSurface9_UnlockRect(priv->d3d_surface);
213 priv->locked_rect.pBits = NULL;
215 if (priv->d3d_surface)
216 IDirect3DSurface9_Release(priv->d3d_surface);
217 priv->d3d_surface = NULL;
219 /* kill the OSD texture and its shadow copy */
220 if (priv->d3d_texture_osd)
221 IDirect3DTexture9_Release(priv->d3d_texture_osd);
222 priv->d3d_texture_osd = NULL;
224 if (priv->d3d_texture_system)
225 IDirect3DTexture9_Release(priv->d3d_texture_system);
226 priv->d3d_texture_system = NULL;
228 if (priv->d3d_backbuf)
229 IDirect3DSurface9_Release(priv->d3d_backbuf);
230 priv->d3d_backbuf = NULL;
233 /** @brief Create D3D Offscreen and Backbuffer surfaces. Each
234 * surface is created only if it's not already present.
235 * @return 1 on success, 0 on failure
237 static int create_d3d_surfaces(void)
239 int osd_width = vo_dwidth, osd_height = vo_dheight;
240 int tex_width = osd_width, tex_height = osd_height;
241 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>create_d3d_surfaces called.\n");
243 if (!priv->d3d_surface &&
244 FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
245 priv->d3d_device, priv->src_width, priv->src_height,
246 priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) {
247 mp_msg(MSGT_VO, MSGL_ERR,
248 "<vo_direct3d>Allocating offscreen surface failed.\n");
249 return 0;
252 if (!priv->d3d_backbuf &&
253 FAILED(IDirect3DDevice9_GetBackBuffer(priv->d3d_device, 0, 0,
254 D3DBACKBUFFER_TYPE_MONO,
255 &priv->d3d_backbuf))) {
256 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating backbuffer failed.\n");
257 return 0;
260 /* calculate the best size for the OSD depending on the factors from the device */
261 if (priv->device_caps_power2_only) {
262 tex_width = 1;
263 tex_height = 1;
264 while (tex_width < osd_width ) tex_width <<= 1;
265 while (tex_height < osd_height) tex_height <<= 1;
267 if (priv->device_caps_square_only)
268 /* device only supports square textures */
269 tex_width = tex_height = tex_width > tex_height ? tex_width : tex_height;
270 // better round up to a multiple of 16
271 tex_width = (tex_width + 15) & ~15;
272 tex_height = (tex_height + 15) & ~15;
274 // make sure we respect the size limits without breaking aspect or pow2-requirements
275 while (tex_width > priv->max_texture_width || tex_height > priv->max_texture_height) {
276 osd_width >>= 1;
277 osd_height >>= 1;
278 tex_width >>= 1;
279 tex_height >>= 1;
282 priv->osd_width = osd_width;
283 priv->osd_height = osd_height;
284 priv->osd_texture_width = tex_width;
285 priv->osd_texture_height = tex_height;
287 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>OSD texture size (%dx%d), requested (%dx%d).\n",
288 vo_dwidth, vo_dheight, priv->osd_texture_width, priv->osd_texture_height);
290 /* create OSD */
291 if (!priv->d3d_texture_system &&
292 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
293 priv->osd_texture_width,
294 priv->osd_texture_height,
296 D3DUSAGE_DYNAMIC,
297 D3DFMT_A8L8,
298 D3DPOOL_SYSTEMMEM,
299 &priv->d3d_texture_system,
300 NULL))) {
301 mp_msg(MSGT_VO,MSGL_ERR,
302 "<vo_direct3d>Allocating OSD texture in system RAM failed.\n");
303 return 0;
306 if (!priv->device_texture_sys) {
307 /* only create if we need a shadow version on the external device */
308 if (!priv->d3d_texture_osd &&
309 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
310 priv->osd_texture_width,
311 priv->osd_texture_height,
313 D3DUSAGE_DYNAMIC,
314 D3DFMT_A8L8,
315 D3DPOOL_DEFAULT,
316 &priv->d3d_texture_osd,
317 NULL))) {
318 mp_msg(MSGT_VO,MSGL_ERR,
319 "<vo_direct3d>Allocating OSD texture in video RAM failed.\n");
320 return 0;
324 /* setup default renderstate */
325 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_ONE);
326 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
327 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
328 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAREF, (DWORD)0x0);
329 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_LIGHTING, FALSE);
330 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
331 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
333 return 1;
336 /** @brief Fill D3D Presentation parameters
338 static void fill_d3d_presentparams(D3DPRESENT_PARAMETERS *present_params)
340 /* Prepare Direct3D initialization parameters. */
341 memset(present_params, 0, sizeof(D3DPRESENT_PARAMETERS));
342 present_params->Windowed = TRUE;
343 present_params->SwapEffect = D3DSWAPEFFECT_COPY;
344 present_params->Flags = D3DPRESENTFLAG_VIDEO;
345 present_params->hDeviceWindow = vo_w32_window; /* w32_common var */
346 present_params->BackBufferWidth = priv->cur_backbuf_width;
347 present_params->BackBufferHeight = priv->cur_backbuf_height;
348 present_params->MultiSampleType = D3DMULTISAMPLE_NONE;
349 present_params->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
350 present_params->BackBufferFormat = priv->desktop_fmt;
351 present_params->BackBufferCount = 1;
352 present_params->EnableAutoDepthStencil = FALSE;
356 /** @brief Create a new backbuffer. Create or Reset the D3D
357 * device.
358 * @return 1 on success, 0 on failure
360 static int change_d3d_backbuffer(back_buffer_action_e action)
362 D3DPRESENT_PARAMETERS present_params;
364 destroy_d3d_surfaces();
366 /* Grow the backbuffer in the required dimension. */
367 if (vo_dwidth > priv->cur_backbuf_width)
368 priv->cur_backbuf_width = vo_dwidth;
370 if (vo_dheight > priv->cur_backbuf_height)
371 priv->cur_backbuf_height = vo_dheight;
373 /* The grown backbuffer dimensions are ready and fill_d3d_presentparams
374 * will use them, so we can reset the device.
376 fill_d3d_presentparams(&present_params);
378 /* vo_w32_window is w32_common variable. It's a handle to the window. */
379 if (action == BACKBUFFER_CREATE &&
380 FAILED(IDirect3D9_CreateDevice(priv->d3d_handle,
381 D3DADAPTER_DEFAULT,
382 D3DDEVTYPE_HAL, vo_w32_window,
383 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
384 &present_params, &priv->d3d_device))) {
385 mp_msg(MSGT_VO, MSGL_V,
386 "<vo_direct3d>Creating Direct3D device failed.\n");
387 return 0;
390 if (action == BACKBUFFER_RESET &&
391 FAILED(IDirect3DDevice9_Reset(priv->d3d_device, &present_params))) {
392 mp_msg(MSGT_VO, MSGL_ERR,
393 "<vo_direct3d>Reseting Direct3D device failed.\n");
394 return 0;
397 mp_msg(MSGT_VO, MSGL_V,
398 "<vo_direct3d>New backbuffer (%dx%d), VO (%dx%d)\n",
399 present_params.BackBufferWidth, present_params.BackBufferHeight,
400 vo_dwidth, vo_dheight);
402 return 1;
405 /** @brief Configure initial Direct3D context. The first
406 * function called to initialize the D3D context.
407 * @return 1 on success, 0 on failure
409 static int configure_d3d(void)
411 D3DDISPLAYMODE disp_mode;
412 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
414 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>configure_d3d called.\n");
416 destroy_d3d_surfaces();
418 /* Get the current desktop display mode, so we can set up a back buffer
419 * of the same format. */
420 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
421 D3DADAPTER_DEFAULT,
422 &disp_mode))) {
423 mp_msg(MSGT_VO, MSGL_ERR,
424 "<vo_direct3d>Reading adapter display mode failed.\n");
425 return 0;
428 /* Write current Desktop's colorspace format in the global storage. */
429 priv->desktop_fmt = disp_mode.Format;
431 if (!change_d3d_backbuffer(BACKBUFFER_CREATE))
432 return 0;
434 if (!create_d3d_surfaces())
435 return 0;
437 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
438 &vp))) {
439 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
440 return 0;
443 calc_fs_rect();
445 return 1;
448 /** @brief Reconfigure the whole Direct3D. Called only
449 * when the video adapter becomes uncooperative.
450 * @return 1 on success, 0 on failure
452 static int reconfigure_d3d(void)
454 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>reconfigure_d3d called.\n");
456 /* Destroy the offscreen, OSD and backbuffer surfaces */
457 destroy_d3d_surfaces();
459 /* Destroy the D3D Device */
460 if (priv->d3d_device)
461 IDirect3DDevice9_Release(priv->d3d_device);
462 priv->d3d_device = NULL;
464 /* Stop the whole Direct3D */
465 IDirect3D9_Release(priv->d3d_handle);
467 /* Initialize Direct3D from the beginning */
468 priv->d3d_handle = Direct3DCreate9(D3D_SDK_VERSION);
469 if (!priv->d3d_handle) {
470 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
471 return 0;
474 /* Configure Direct3D */
475 if (!configure_d3d())
476 return 0;
478 return 1;
481 /** @brief Resize Direct3D context on window resize.
482 * @return 1 on success, 0 on failure
484 static int resize_d3d(void)
486 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
488 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>resize_d3d called.\n");
490 /* Make sure that backbuffer is large enough to accomodate the new
491 viewport dimensions. Grow it if necessary. */
493 if (vo_dwidth > priv->cur_backbuf_width ||
494 vo_dheight > priv->cur_backbuf_height) {
495 if (!change_d3d_backbuffer(BACKBUFFER_RESET))
496 return 0;
499 /* Destroy the OSD textures. They should always match the new dimensions
500 * of the onscreen window, so on each resize we need new OSD dimensions.
503 if (priv->d3d_texture_osd)
504 IDirect3DTexture9_Release(priv->d3d_texture_osd);
505 priv->d3d_texture_osd = NULL;
507 if (priv->d3d_texture_system)
508 IDirect3DTexture9_Release(priv->d3d_texture_system);
509 priv->d3d_texture_system = NULL;
512 /* Recreate the OSD. The function will observe that the offscreen plain
513 * surface and the backbuffer are not destroyed and will skip their creation,
514 * effectively recreating only the OSD.
517 if (!create_d3d_surfaces())
518 return 0;
520 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
521 &vp))) {
522 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
523 return 0;
526 calc_fs_rect();
528 #ifdef CONFIG_FREETYPE
529 // font needs to be adjusted
530 force_load_font = 1;
531 #endif
532 // OSD needs to be drawn fresh for new size
533 vo_osd_changed(OSDTYPE_OSD);
535 return 1;
538 /** @brief Uninitialize Direct3D and close the window.
540 static void uninit_d3d(void)
542 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit_d3d called.\n");
544 destroy_d3d_surfaces();
546 /* Destroy the D3D Device */
547 if (priv->d3d_device)
548 IDirect3DDevice9_Release(priv->d3d_device);
549 priv->d3d_device = NULL;
551 /* Stop the whole D3D. */
552 if (priv->d3d_handle) {
553 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Stopping Direct3D.\n");
554 IDirect3D9_Release(priv->d3d_handle);
556 priv->d3d_handle = NULL;
559 /** @brief Render a frame on the screen.
560 * @param mpi mpi structure with the decoded frame inside
561 * @return VO_TRUE on success, VO_ERROR on failure
563 static uint32_t render_d3d_frame(mp_image_t *mpi)
565 /* Uncomment when direct rendering is implemented.
566 * if (mpi->flags & MP_IMGFLAG_DIRECT) ...
569 /* If the D3D device is uncooperative (not initialized), return success.
570 The device will be probed for reinitialization in the next flip_page() */
571 if (!priv->d3d_device)
572 return VO_TRUE;
574 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
575 goto skip_upload;
577 if (mpi->flags & MP_IMGFLAG_PLANAR) { /* Copy a planar frame. */
578 draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
579 goto skip_upload;
582 /* If we're here, then we should lock the rect and copy a packed frame */
583 if (!priv->locked_rect.pBits) {
584 if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface,
585 &priv->locked_rect, NULL, 0))) {
586 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Surface lock failed.\n");
587 return VO_ERROR;
591 memcpy_pic(priv->locked_rect.pBits, mpi->planes[0], mpi->stride[0],
592 mpi->height, priv->locked_rect.Pitch, mpi->stride[0]);
594 skip_upload:
595 /* This unlock is used for both slice_draw path and render_d3d_frame path. */
596 if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface))) {
597 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface unlock failed.\n");
598 return VO_ERROR;
600 priv->locked_rect.pBits = NULL;
602 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
603 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>BeginScene failed.\n");
604 return VO_ERROR;
607 if (priv->is_clear_needed) {
608 IDirect3DDevice9_Clear(priv->d3d_device, 0, NULL,
609 D3DCLEAR_TARGET, 0, 0, 0);
610 priv->is_clear_needed = 0;
613 if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device,
614 priv->d3d_surface,
615 &priv->fs_panscan_rect,
616 priv->d3d_backbuf,
617 &priv->fs_movie_rect,
618 D3DTEXF_LINEAR))) {
619 mp_msg(MSGT_VO, MSGL_ERR,
620 "<vo_direct3d>Copying frame to the backbuffer failed.\n");
621 return VO_ERROR;
624 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
625 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>EndScene failed.\n");
626 return VO_ERROR;
629 return VO_TRUE;
633 /** @brief Query if movie colorspace is supported by the HW.
634 * @return 0 on failure, device capabilities (not probed
635 * currently) on success.
637 static int query_format(uint32_t movie_fmt)
639 int i;
640 for (i = 0; i < DISPLAY_FORMAT_TABLE_ENTRIES; i++) {
641 if (fmt_table[i].mplayer_fmt == movie_fmt) {
642 /* Test conversion from Movie colorspace to
643 * display's target colorspace. */
644 if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle,
645 D3DADAPTER_DEFAULT,
646 D3DDEVTYPE_HAL,
647 fmt_table[i].fourcc,
648 priv->desktop_fmt))) {
649 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Rejected image format: %s\n",
650 vo_format_name(fmt_table[i].mplayer_fmt));
651 return 0;
654 priv->movie_src_fmt = fmt_table[i].fourcc;
655 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Accepted image format: %s\n",
656 vo_format_name(fmt_table[i].mplayer_fmt));
657 return (VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
658 | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN);
663 return 0;
666 /****************************************************************************
670 * libvo Control / Callback functions *
674 ****************************************************************************/
679 /** @brief libvo Callback: Preinitialize the video card.
680 * Preinit the hardware just enough to be queried about
681 * supported formats.
683 * @return 0 on success, -1 on failure
686 static int preinit(const char *arg)
688 D3DDISPLAYMODE disp_mode;
689 D3DCAPS9 disp_caps;
690 DWORD texture_caps;
691 DWORD dev_caps;
693 /* Set to zero all global variables. */
694 priv = calloc(1, sizeof(struct global_priv));
695 if (!priv) {
696 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating private memory failed.\n");
697 return -1;
700 /* FIXME
701 > Please use subopt-helper.h for this, see vo_gl.c:preinit for
702 > an example of how to use it.
705 priv->d3d_handle = Direct3DCreate9(D3D_SDK_VERSION);
706 if (!priv->d3d_handle) {
707 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
708 return -1;
711 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
712 D3DADAPTER_DEFAULT,
713 &disp_mode))) {
714 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display mode failed.\n");
715 return -1;
718 /* Store in priv->desktop_fmt the user desktop's colorspace. Usually XRGB. */
719 priv->desktop_fmt = disp_mode.Format;
720 priv->cur_backbuf_width = disp_mode.Width;
721 priv->cur_backbuf_height = disp_mode.Height;
723 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Setting backbuffer dimensions to (%dx%d).\n",
724 disp_mode.Width, disp_mode.Height);
726 if (FAILED(IDirect3D9_GetDeviceCaps(priv->d3d_handle,
727 D3DADAPTER_DEFAULT,
728 D3DDEVTYPE_HAL,
729 &disp_caps))) {
730 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display capabilities failed.\n");
731 return -1;
734 /* Store relevant information reguarding caps of device */
735 texture_caps = disp_caps.TextureCaps;
736 dev_caps = disp_caps.DevCaps;
737 priv->device_caps_power2_only = (texture_caps & D3DPTEXTURECAPS_POW2) &&
738 !(texture_caps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
739 priv->device_caps_square_only = texture_caps & D3DPTEXTURECAPS_SQUAREONLY;
740 priv->device_texture_sys = dev_caps & D3DDEVCAPS_TEXTURESYSTEMMEMORY;
741 priv->max_texture_width = disp_caps.MaxTextureWidth;
742 priv->max_texture_height = disp_caps.MaxTextureHeight;
744 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>device_caps_power2_only %d, device_caps_square_only %d\n"
745 "<vo_direct3d>device_texture_sys %d\n"
746 "<vo_direct3d>max_texture_width %d, max_texture_height %d\n",
747 priv->device_caps_power2_only, priv->device_caps_square_only,
748 priv->device_texture_sys, priv->max_texture_width,
749 priv->max_texture_height);
751 /* w32_common framework call. Configures window on the screen, gets
752 * fullscreen dimensions and does other useful stuff.
754 if (!vo_w32_init()) {
755 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Configuring onscreen window failed.\n");
756 return -1;
759 return 0;
764 /** @brief libvo Callback: Handle control requests.
765 * @return VO_TRUE on success, VO_NOTIMPL when not implemented
767 static int control(uint32_t request, void *data, ...)
769 switch (request) {
770 case VOCTRL_QUERY_FORMAT:
771 return query_format(*(uint32_t*) data);
772 case VOCTRL_GET_IMAGE: /* Direct Rendering. Not implemented yet. */
773 mp_msg(MSGT_VO, MSGL_V,
774 "<vo_direct3d>Direct Rendering request. Not implemented yet.\n");
775 return VO_NOTIMPL;
776 case VOCTRL_DRAW_IMAGE:
777 return render_d3d_frame(data);
778 case VOCTRL_FULLSCREEN:
779 vo_w32_fullscreen();
780 resize_d3d();
781 return VO_TRUE;
782 case VOCTRL_RESET:
783 return VO_NOTIMPL;
784 case VOCTRL_PAUSE:
785 priv->is_paused = 1;
786 return VO_TRUE;
787 case VOCTRL_RESUME:
788 priv->is_paused = 0;
789 return VO_TRUE;
790 case VOCTRL_GUISUPPORT:
791 return VO_NOTIMPL;
792 case VOCTRL_SET_EQUALIZER:
793 return VO_NOTIMPL;
794 case VOCTRL_GET_EQUALIZER:
795 return VO_NOTIMPL;
796 case VOCTRL_ONTOP:
797 vo_w32_ontop();
798 return VO_TRUE;
799 case VOCTRL_BORDER:
800 vo_w32_border();
801 resize_d3d();
802 return VO_TRUE;
803 case VOCTRL_UPDATE_SCREENINFO:
804 w32_update_xinerama_info();
805 return VO_TRUE;
806 case VOCTRL_SET_PANSCAN:
807 calc_fs_rect();
808 return VO_TRUE;
809 case VOCTRL_GET_PANSCAN:
810 return VO_TRUE;
812 return VO_FALSE;
815 /** @brief libvo Callback: Configre the Direct3D adapter.
816 * @param width Movie source width
817 * @param height Movie source height
818 * @param d_width Screen (destination) width
819 * @param d_height Screen (destination) height
820 * @param options Options bitmap
821 * @param title Window title
822 * @param format Movie colorspace format (using MPlayer's
823 * defines, e.g. IMGFMT_YUY2)
824 * @return 0 on success, VO_ERROR on failure
826 static int config(uint32_t width, uint32_t height, uint32_t d_width,
827 uint32_t d_height, uint32_t options, char *title,
828 uint32_t format)
831 priv->src_width = width;
832 priv->src_height = height;
834 /* w32_common framework call. Creates window on the screen with
835 * the given coordinates.
837 if (!vo_w32_config(d_width, d_height, options)) {
838 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Creating onscreen window failed.\n");
839 return VO_ERROR;
842 /* "config" may be called several times, so if this is not the first
843 * call, we should destroy Direct3D adapter and surfaces before
844 * calling configure_d3d, which will create them again.
847 destroy_d3d_surfaces();
849 /* Destroy the D3D Device */
850 if (priv->d3d_device)
851 IDirect3DDevice9_Release(priv->d3d_device);
852 priv->d3d_device = NULL;
854 if (!configure_d3d())
855 return VO_ERROR;
857 return 0; /* Success */
860 /** @brief libvo Callback: Flip next already drawn frame on the
861 * screen.
863 static void flip_page(void)
865 RECT rect = {0, 0, vo_dwidth, vo_dheight};
866 if (!priv->d3d_device ||
867 FAILED(IDirect3DDevice9_Present(priv->d3d_device, &rect, 0, 0, 0))) {
868 mp_msg(MSGT_VO, MSGL_V,
869 "<vo_direct3d>Trying to reinitialize uncooperative video adapter.\n");
870 if (!reconfigure_d3d()) {
871 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Reinitialization failed.\n");
872 return;
874 else
875 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Video adapter reinitialized.\n");
879 /** @brief libvo Callback: Uninitializes all pointers and closes
880 * all D3D related stuff,
882 static void uninit(void)
884 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit called.\n");
886 uninit_d3d();
887 vo_w32_uninit(); /* w32_common framework call */
888 free(priv);
889 priv = NULL;
892 /** @brief libvo Callback: Handles video window events.
894 static void check_events(void)
896 int flags;
897 /* w32_common framework call. Handles video window events.
898 * Updates global libvo's vo_dwidth/vo_dheight upon resize
899 * with the new window width/height.
901 flags = vo_w32_check_events();
902 if (flags & VO_EVENT_RESIZE)
903 resize_d3d();
905 if ((flags & VO_EVENT_EXPOSE) && priv->is_paused)
906 flip_page();
909 /** @brief libvo Callback: Draw slice
910 * @return 0 on success
912 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y )
914 char *my_src; /**< Pointer to the source image */
915 char *dst; /**< Pointer to the destination image */
916 int uv_stride; /**< Stride of the U/V planes */
918 /* If the D3D device is uncooperative (not initialized), return success.
919 The device will be probed for reinitialization in the next flip_page() */
920 if (!priv->d3d_device)
921 return 0;
923 /* Lock the offscreen surface if it's not already locked. */
924 if (!priv->locked_rect.pBits) {
925 if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface,
926 &priv->locked_rect, NULL, 0))) {
927 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface lock failure.\n");
928 return VO_FALSE;
932 uv_stride = priv->locked_rect.Pitch / 2;
934 /* Copy Y */
935 dst = priv->locked_rect.pBits;
936 dst = dst + priv->locked_rect.Pitch * y + x;
937 my_src = src[0];
938 memcpy_pic(dst, my_src, w, h, priv->locked_rect.Pitch, stride[0]);
940 w /= 2;
941 h /= 2;
942 x /= 2;
943 y /= 2;
945 /* Copy U */
946 dst = priv->locked_rect.pBits;
947 dst = dst + priv->locked_rect.Pitch * priv->src_height
948 + uv_stride * y + x;
949 if (priv->movie_src_fmt == MAKEFOURCC('Y','V','1','2'))
950 my_src = src[2];
951 else
952 my_src = src[1];
954 memcpy_pic(dst, my_src, w, h, uv_stride, stride[1]);
956 /* Copy V */
957 dst = priv->locked_rect.pBits;
958 dst = dst + priv->locked_rect.Pitch * priv->src_height
959 + uv_stride * (priv->src_height / 2) + uv_stride * y + x;
960 if (priv->movie_src_fmt == MAKEFOURCC('Y','V','1','2'))
961 my_src=src[1];
962 else
963 my_src=src[2];
965 memcpy_pic(dst, my_src, w, h, uv_stride, stride[2]);
967 return 0; /* Success */
970 /** @brief libvo Callback: Unused function
972 static int draw_frame(uint8_t *src[])
974 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>draw_frame called.\n");
975 return VO_FALSE;
978 /** @brief Maps MPlayer alpha to D3D
979 * 0x0 -> transparent and discarded by alpha test
980 * 0x1 -> 0xFF to become opaque
981 * other alpha values are inverted +1 (2 = -2)
982 * These values are then inverted again with
983 the texture filter D3DBLEND_INVSRCALPHA
985 void vo_draw_alpha_l8a8(int w, int h, unsigned char* src, unsigned char *srca,
986 int srcstride, unsigned char* dstbase, int dststride)
988 int y;
989 for (y = 0; y < h; y++) {
990 unsigned short *dst = (unsigned short*)dstbase;
991 int x;
992 for (x = 0; x < w; x++) {
993 dst[x] = (-srca[x] << 8) | src[x];
995 src += srcstride;
996 srca += srcstride;
997 dstbase += dststride;
1001 /** @brief Callback function to render the OSD to the texture
1003 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
1004 unsigned char *srca, int stride)
1006 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
1007 to copy MPlayer's frame inside it.*/
1009 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
1010 &locked_rect, NULL, 0))) {
1011 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture lock failed.\n");
1012 return;
1015 vo_draw_alpha_l8a8(w, h, src, srca, stride,
1016 (unsigned char *)locked_rect.pBits + locked_rect.Pitch*y0 + 2*x0, locked_rect.Pitch);
1018 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1019 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
1020 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture unlock failed.\n");
1021 return;
1024 priv->is_osd_populated = 1;
1027 /** @brief libvo Callback: Draw OSD/Subtitles,
1029 static void draw_osd(void)
1031 // we can not render OSD if we lost the device e.g. because it was uncooperative
1032 if (!priv->d3d_device)
1033 return;
1035 if (vo_osd_changed(0)) {
1036 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
1037 to copy MPlayer's frame inside it.*/
1039 /* clear the OSD */
1040 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
1041 &locked_rect, NULL, 0))) {
1042 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture lock failed.\n");
1043 return;
1046 /* clear the whole texture to avoid issues due to interpolation */
1047 memset(locked_rect.pBits, 0, locked_rect.Pitch * priv->osd_texture_height);
1049 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1050 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
1051 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture unlock failed.\n");
1052 return;
1055 priv->is_osd_populated = 0;
1056 /* required for if subs are in the boarder region */
1057 priv->is_clear_needed = 1;
1059 vo_draw_text(priv->osd_width, priv->osd_height, draw_alpha);
1061 if (!priv->device_texture_sys)
1063 /* only DMA to the shadow if its required */
1064 if (FAILED(IDirect3DDevice9_UpdateTexture(priv->d3d_device,
1065 (IDirect3DBaseTexture9 *)priv->d3d_texture_system,
1066 (IDirect3DBaseTexture9 *)priv->d3d_texture_osd))) {
1067 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture transfer failed.\n");
1068 return;
1073 /* update OSD */
1075 if (priv->is_osd_populated) {
1077 struct_vertex osd_quad_vb[] = {
1078 {-1.0f, 1.0f, 0.0f, 0, 0 },
1079 { 1.0f, 1.0f, 0.0f, 1, 0 },
1080 {-1.0f,-1.0f, 0.0f, 0, 1 },
1081 { 1.0f,-1.0f, 0.0f, 1, 1 }
1084 /* calculate the texture coordinates */
1085 osd_quad_vb[1].tu =
1086 osd_quad_vb[3].tu = (float)priv->osd_width / priv->osd_texture_width;
1087 osd_quad_vb[2].tv =
1088 osd_quad_vb[3].tv = (float)priv->osd_height / priv->osd_texture_height;
1090 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
1091 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>BeginScene failed.\n");
1092 return;
1095 /* turn on alpha test */
1096 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, TRUE);
1097 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, TRUE);
1099 /* need to use a texture here (done here as we may be able to texture from system memory) */
1100 IDirect3DDevice9_SetTexture(priv->d3d_device, 0,
1101 (IDirect3DBaseTexture9 *)(priv->device_texture_sys
1102 ? priv->d3d_texture_system : priv->d3d_texture_osd));
1104 IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_MY_VERTEX);
1105 IDirect3DDevice9_DrawPrimitiveUP(priv->d3d_device, D3DPT_TRIANGLESTRIP, 2, osd_quad_vb, sizeof(struct_vertex));
1107 /* turn off alpha test */
1108 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, FALSE);
1109 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, FALSE);
1111 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
1112 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>EndScene failed.\n");
1113 return;