vo_xv: Fix context Shminfo table size
[mplayer.git] / libvo / vo_direct3d.c
blobc0f79d45d441c700e1cce84d212e379998fb6997
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 */
73 LPDIRECT3D9 d3d_handle; /**< Direct3D Handle */
74 LPDIRECT3DDEVICE9 d3d_device; /**< The Direct3D Adapter */
75 IDirect3DSurface9 *d3d_surface; /**< Offscreen Direct3D Surface. MPlayer
76 renders inside it. Uses colorspace
77 priv->movie_src_fmt */
78 IDirect3DTexture9 *d3d_texture_osd; /**< Direct3D Texture. Uses RGBA */
79 IDirect3DTexture9 *d3d_texture_system; /**< Direct3D Texture. System memory
80 cannot lock a normal texture. Uses RGBA */
81 IDirect3DSurface9 *d3d_backbuf; /**< Video card's back buffer (used to
82 display next frame) */
83 int cur_backbuf_width; /**< Current backbuffer width */
84 int cur_backbuf_height; /**< Current backbuffer height */
85 int is_osd_populated; /**< 1 = OSD texture has something to display,
86 0 = OSD texture is clear */
87 int device_caps_power2_only; /**< 1 = texture sizes have to be power 2
88 0 = texture sizes can be anything */
89 int device_caps_square_only; /**< 1 = textures have to be square
90 0 = textures do not have to be square */
91 int device_texture_sys; /**< 1 = device can texture from system memory
92 0 = device requires shadow */
93 int max_texture_width; /**< from the device capabilities */
94 int max_texture_height; /**< from the device capabilities */
95 int osd_width; /**< current width of the OSD */
96 int osd_height; /**< current height of the OSD */
97 int osd_texture_width; /**< current width of the OSD texture */
98 int osd_texture_height; /**< current height of the OSD texture */
99 } *priv;
101 typedef struct {
102 const unsigned int mplayer_fmt; /**< Given by MPlayer */
103 const D3DFORMAT fourcc; /**< Required by D3D's test function */
104 } struct_fmt_table;
106 /* Map table from reported MPlayer format to the required
107 fourcc. This is needed to perform the format query. */
109 static const struct_fmt_table fmt_table[] = {
110 {IMGFMT_YV12, MAKEFOURCC('Y','V','1','2')},
111 {IMGFMT_I420, MAKEFOURCC('I','4','2','0')},
112 {IMGFMT_IYUV, MAKEFOURCC('I','Y','U','V')},
113 {IMGFMT_YVU9, MAKEFOURCC('Y','V','U','9')},
114 {IMGFMT_YUY2, D3DFMT_YUY2},
115 {IMGFMT_UYVY, D3DFMT_UYVY},
116 {IMGFMT_BGR32, D3DFMT_X8R8G8B8},
117 {IMGFMT_RGB32, D3DFMT_X8B8G8R8},
118 {IMGFMT_BGR24, D3DFMT_R8G8B8}, //untested
119 {IMGFMT_BGR16, D3DFMT_R5G6B5},
120 {IMGFMT_BGR15, D3DFMT_X1R5G5B5},
121 {IMGFMT_BGR8 , D3DFMT_R3G3B2}, //untested
124 #define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0]))
126 #define D3DFVF_MY_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
128 typedef struct {
129 float x, y, z; /* Position of vertex in 3D space */
130 float tu, tv; /* Texture coordinates */
131 } struct_vertex;
133 typedef enum back_buffer_action {
134 BACKBUFFER_CREATE,
135 BACKBUFFER_RESET
136 } back_buffer_action_e;
137 /****************************************************************************
141 * Direct3D specific implementation functions *
145 ****************************************************************************/
147 /** @brief Calculate scaled fullscreen movie rectangle with
148 * preserved aspect ratio.
150 static void calc_fs_rect(void)
152 struct vo_rect src_rect;
153 struct vo_rect dst_rect;
154 struct vo_rect borders;
155 calc_src_dst_rects(priv->src_width, priv->src_height, &src_rect, &dst_rect, &borders, NULL);
157 priv->fs_movie_rect.left = dst_rect.left;
158 priv->fs_movie_rect.right = dst_rect.right;
159 priv->fs_movie_rect.top = dst_rect.top;
160 priv->fs_movie_rect.bottom = dst_rect.bottom;
161 priv->fs_panscan_rect.left = src_rect.left;
162 priv->fs_panscan_rect.right = src_rect.right;
163 priv->fs_panscan_rect.top = src_rect.top;
164 priv->fs_panscan_rect.bottom = src_rect.bottom;
165 priv->border_x = borders.left;
166 priv->border_y = borders.top;
168 mp_msg(MSGT_VO, MSGL_V,
169 "<vo_direct3d>Fullscreen movie rectangle: t: %ld, l: %ld, r: %ld, b:%ld\n",
170 priv->fs_movie_rect.top, priv->fs_movie_rect.left,
171 priv->fs_movie_rect.right, priv->fs_movie_rect.bottom);
173 /* The backbuffer should be cleared before next StretchRect. This is
174 * necessary because our new draw area could be smaller than the
175 * previous one used by StretchRect and without it, leftovers from the
176 * previous frame will be left. */
177 priv->is_clear_needed = 1;
180 /** @brief Destroy D3D Offscreen and Backbuffer surfaces.
182 static void destroy_d3d_surfaces(void)
184 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>destroy_d3d_surfaces called.\n");
185 /* Let's destroy the old (if any) D3D Surfaces */
187 if (priv->locked_rect.pBits)
188 IDirect3DSurface9_UnlockRect(priv->d3d_surface);
189 priv->locked_rect.pBits = NULL;
191 if (priv->d3d_surface)
192 IDirect3DSurface9_Release(priv->d3d_surface);
193 priv->d3d_surface = NULL;
195 /* kill the OSD texture and its shadow copy */
196 if (priv->d3d_texture_osd)
197 IDirect3DTexture9_Release(priv->d3d_texture_osd);
198 priv->d3d_texture_osd = NULL;
200 if (priv->d3d_texture_system)
201 IDirect3DTexture9_Release(priv->d3d_texture_system);
202 priv->d3d_texture_system = NULL;
204 if (priv->d3d_backbuf)
205 IDirect3DSurface9_Release(priv->d3d_backbuf);
206 priv->d3d_backbuf = NULL;
209 /** @brief Create D3D Offscreen and Backbuffer surfaces. Each
210 * surface is created only if it's not already present.
211 * @return 1 on success, 0 on failure
213 static int create_d3d_surfaces(void)
215 int osd_width = vo_dwidth, osd_height = vo_dheight;
216 int tex_width = osd_width, tex_height = osd_height;
217 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>create_d3d_surfaces called.\n");
219 if (!priv->d3d_surface &&
220 FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
221 priv->d3d_device, priv->src_width, priv->src_height,
222 priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) {
223 mp_msg(MSGT_VO, MSGL_ERR,
224 "<vo_direct3d>Allocating offscreen surface failed.\n");
225 return 0;
228 if (!priv->d3d_backbuf &&
229 FAILED(IDirect3DDevice9_GetBackBuffer(priv->d3d_device, 0, 0,
230 D3DBACKBUFFER_TYPE_MONO,
231 &priv->d3d_backbuf))) {
232 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating backbuffer failed.\n");
233 return 0;
236 /* calculate the best size for the OSD depending on the factors from the device */
237 if (priv->device_caps_power2_only) {
238 tex_width = 1;
239 tex_height = 1;
240 while (tex_width < osd_width ) tex_width <<= 1;
241 while (tex_height < osd_height) tex_height <<= 1;
243 if (priv->device_caps_square_only)
244 /* device only supports square textures */
245 tex_width = tex_height = tex_width > tex_height ? tex_width : tex_height;
246 // better round up to a multiple of 16
247 tex_width = (tex_width + 15) & ~15;
248 tex_height = (tex_height + 15) & ~15;
250 // make sure we respect the size limits without breaking aspect or pow2-requirements
251 while (tex_width > priv->max_texture_width || tex_height > priv->max_texture_height) {
252 osd_width >>= 1;
253 osd_height >>= 1;
254 tex_width >>= 1;
255 tex_height >>= 1;
258 priv->osd_width = osd_width;
259 priv->osd_height = osd_height;
260 priv->osd_texture_width = tex_width;
261 priv->osd_texture_height = tex_height;
263 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>OSD texture size (%dx%d), requested (%dx%d).\n",
264 vo_dwidth, vo_dheight, priv->osd_texture_width, priv->osd_texture_height);
266 /* create OSD */
267 if (!priv->d3d_texture_system &&
268 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
269 priv->osd_texture_width,
270 priv->osd_texture_height,
272 D3DUSAGE_DYNAMIC,
273 D3DFMT_A8L8,
274 D3DPOOL_SYSTEMMEM,
275 &priv->d3d_texture_system,
276 NULL))) {
277 mp_msg(MSGT_VO,MSGL_ERR,
278 "<vo_direct3d>Allocating OSD texture in system RAM failed.\n");
279 return 0;
282 if (!priv->device_texture_sys) {
283 /* only create if we need a shadow version on the external device */
284 if (!priv->d3d_texture_osd &&
285 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
286 priv->osd_texture_width,
287 priv->osd_texture_height,
289 D3DUSAGE_DYNAMIC,
290 D3DFMT_A8L8,
291 D3DPOOL_DEFAULT,
292 &priv->d3d_texture_osd,
293 NULL))) {
294 mp_msg(MSGT_VO,MSGL_ERR,
295 "<vo_direct3d>Allocating OSD texture in video RAM failed.\n");
296 return 0;
300 /* setup default renderstate */
301 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_ONE);
302 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
303 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
304 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAREF, (DWORD)0x0);
305 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_LIGHTING, FALSE);
306 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
307 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
309 return 1;
312 /** @brief Fill D3D Presentation parameters
314 static void fill_d3d_presentparams(D3DPRESENT_PARAMETERS *present_params)
316 /* Prepare Direct3D initialization parameters. */
317 memset(present_params, 0, sizeof(D3DPRESENT_PARAMETERS));
318 present_params->Windowed = TRUE;
319 present_params->SwapEffect = D3DSWAPEFFECT_COPY;
320 present_params->Flags = D3DPRESENTFLAG_VIDEO;
321 present_params->hDeviceWindow = vo_w32_window; /* w32_common var */
322 present_params->BackBufferWidth = priv->cur_backbuf_width;
323 present_params->BackBufferHeight = priv->cur_backbuf_height;
324 present_params->MultiSampleType = D3DMULTISAMPLE_NONE;
325 present_params->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
326 present_params->BackBufferFormat = priv->desktop_fmt;
327 present_params->BackBufferCount = 1;
328 present_params->EnableAutoDepthStencil = FALSE;
332 /** @brief Create a new backbuffer. Create or Reset the D3D
333 * device.
334 * @return 1 on success, 0 on failure
336 static int change_d3d_backbuffer(back_buffer_action_e action)
338 D3DPRESENT_PARAMETERS present_params;
340 destroy_d3d_surfaces();
342 /* Grow the backbuffer in the required dimension. */
343 if (vo_dwidth > priv->cur_backbuf_width)
344 priv->cur_backbuf_width = vo_dwidth;
346 if (vo_dheight > priv->cur_backbuf_height)
347 priv->cur_backbuf_height = vo_dheight;
349 /* The grown backbuffer dimensions are ready and fill_d3d_presentparams
350 * will use them, so we can reset the device.
352 fill_d3d_presentparams(&present_params);
354 /* vo_w32_window is w32_common variable. It's a handle to the window. */
355 if (action == BACKBUFFER_CREATE &&
356 FAILED(IDirect3D9_CreateDevice(priv->d3d_handle,
357 D3DADAPTER_DEFAULT,
358 D3DDEVTYPE_HAL, vo_w32_window,
359 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
360 &present_params, &priv->d3d_device))) {
361 mp_msg(MSGT_VO, MSGL_V,
362 "<vo_direct3d>Creating Direct3D device failed.\n");
363 return 0;
366 if (action == BACKBUFFER_RESET &&
367 FAILED(IDirect3DDevice9_Reset(priv->d3d_device, &present_params))) {
368 mp_msg(MSGT_VO, MSGL_ERR,
369 "<vo_direct3d>Reseting Direct3D device failed.\n");
370 return 0;
373 mp_msg(MSGT_VO, MSGL_V,
374 "<vo_direct3d>New backbuffer (%dx%d), VO (%dx%d)\n",
375 present_params.BackBufferWidth, present_params.BackBufferHeight,
376 vo_dwidth, vo_dheight);
378 return 1;
381 /** @brief Configure initial Direct3D context. The first
382 * function called to initialize the D3D context.
383 * @return 1 on success, 0 on failure
385 static int configure_d3d(void)
387 D3DDISPLAYMODE disp_mode;
388 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
390 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>configure_d3d called.\n");
392 destroy_d3d_surfaces();
394 /* Get the current desktop display mode, so we can set up a back buffer
395 * of the same format. */
396 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
397 D3DADAPTER_DEFAULT,
398 &disp_mode))) {
399 mp_msg(MSGT_VO, MSGL_ERR,
400 "<vo_direct3d>Reading adapter display mode failed.\n");
401 return 0;
404 /* Write current Desktop's colorspace format in the global storage. */
405 priv->desktop_fmt = disp_mode.Format;
407 if (!change_d3d_backbuffer(BACKBUFFER_CREATE))
408 return 0;
410 if (!create_d3d_surfaces())
411 return 0;
413 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
414 &vp))) {
415 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
416 return 0;
419 calc_fs_rect();
421 return 1;
424 /** @brief Reconfigure the whole Direct3D. Called only
425 * when the video adapter becomes uncooperative.
426 * @return 1 on success, 0 on failure
428 static int reconfigure_d3d(void)
430 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>reconfigure_d3d called.\n");
432 /* Destroy the offscreen, OSD and backbuffer surfaces */
433 destroy_d3d_surfaces();
435 /* Destroy the D3D Device */
436 if (priv->d3d_device)
437 IDirect3DDevice9_Release(priv->d3d_device);
438 priv->d3d_device = NULL;
440 /* Stop the whole Direct3D */
441 IDirect3D9_Release(priv->d3d_handle);
443 /* Initialize Direct3D from the beginning */
444 priv->d3d_handle = Direct3DCreate9(D3D_SDK_VERSION);
445 if (!priv->d3d_handle) {
446 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
447 return 0;
450 /* Configure Direct3D */
451 if (!configure_d3d())
452 return 0;
454 return 1;
457 /** @brief Resize Direct3D context on window resize.
458 * @return 1 on success, 0 on failure
460 static int resize_d3d(void)
462 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
464 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>resize_d3d called.\n");
466 /* Make sure that backbuffer is large enough to accomodate the new
467 viewport dimensions. Grow it if necessary. */
469 if (vo_dwidth > priv->cur_backbuf_width ||
470 vo_dheight > priv->cur_backbuf_height) {
471 if (!change_d3d_backbuffer(BACKBUFFER_RESET))
472 return 0;
475 /* Destroy the OSD textures. They should always match the new dimensions
476 * of the onscreen window, so on each resize we need new OSD dimensions.
479 if (priv->d3d_texture_osd)
480 IDirect3DTexture9_Release(priv->d3d_texture_osd);
481 priv->d3d_texture_osd = NULL;
483 if (priv->d3d_texture_system)
484 IDirect3DTexture9_Release(priv->d3d_texture_system);
485 priv->d3d_texture_system = NULL;
488 /* Recreate the OSD. The function will observe that the offscreen plain
489 * surface and the backbuffer are not destroyed and will skip their creation,
490 * effectively recreating only the OSD.
493 if (!create_d3d_surfaces())
494 return 0;
496 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
497 &vp))) {
498 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
499 return 0;
502 calc_fs_rect();
504 #ifdef CONFIG_FREETYPE
505 // font needs to be adjusted
506 force_load_font = 1;
507 #endif
508 // OSD needs to be drawn fresh for new size
509 vo_osd_changed(OSDTYPE_OSD);
511 return 1;
514 /** @brief Uninitialize Direct3D and close the window.
516 static void uninit_d3d(void)
518 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit_d3d called.\n");
520 destroy_d3d_surfaces();
522 /* Destroy the D3D Device */
523 if (priv->d3d_device)
524 IDirect3DDevice9_Release(priv->d3d_device);
525 priv->d3d_device = NULL;
527 /* Stop the whole D3D. */
528 if (priv->d3d_handle) {
529 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Stopping Direct3D.\n");
530 IDirect3D9_Release(priv->d3d_handle);
532 priv->d3d_handle = NULL;
535 /** @brief Render a frame on the screen.
536 * @param mpi mpi structure with the decoded frame inside
537 * @return VO_TRUE on success, VO_ERROR on failure
539 static uint32_t render_d3d_frame(mp_image_t *mpi)
541 /* Uncomment when direct rendering is implemented.
542 * if (mpi->flags & MP_IMGFLAG_DIRECT) ...
545 /* If the D3D device is uncooperative (not initialized), return success.
546 The device will be probed for reinitialization in the next flip_page() */
547 if (!priv->d3d_device)
548 return VO_TRUE;
550 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
551 goto skip_upload;
553 if (mpi->flags & MP_IMGFLAG_PLANAR) { /* Copy a planar frame. */
554 draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
555 goto skip_upload;
558 /* If we're here, then we should lock the rect and copy a packed frame */
559 if (!priv->locked_rect.pBits) {
560 if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface,
561 &priv->locked_rect, NULL, 0))) {
562 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Surface lock failed.\n");
563 return VO_ERROR;
567 memcpy_pic(priv->locked_rect.pBits, mpi->planes[0], mpi->stride[0],
568 mpi->height, priv->locked_rect.Pitch, mpi->stride[0]);
570 skip_upload:
571 /* This unlock is used for both slice_draw path and render_d3d_frame path. */
572 if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface))) {
573 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface unlock failed.\n");
574 return VO_ERROR;
576 priv->locked_rect.pBits = NULL;
578 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
579 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>BeginScene failed.\n");
580 return VO_ERROR;
583 if (priv->is_clear_needed) {
584 IDirect3DDevice9_Clear(priv->d3d_device, 0, NULL,
585 D3DCLEAR_TARGET, 0, 0, 0);
586 priv->is_clear_needed = 0;
589 if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device,
590 priv->d3d_surface,
591 &priv->fs_panscan_rect,
592 priv->d3d_backbuf,
593 &priv->fs_movie_rect,
594 D3DTEXF_LINEAR))) {
595 mp_msg(MSGT_VO, MSGL_ERR,
596 "<vo_direct3d>Copying frame to the backbuffer failed.\n");
597 return VO_ERROR;
600 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
601 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>EndScene failed.\n");
602 return VO_ERROR;
605 return VO_TRUE;
609 /** @brief Query if movie colorspace is supported by the HW.
610 * @return 0 on failure, device capabilities (not probed
611 * currently) on success.
613 static int query_format(uint32_t movie_fmt)
615 int i;
616 for (i = 0; i < DISPLAY_FORMAT_TABLE_ENTRIES; i++) {
617 if (fmt_table[i].mplayer_fmt == movie_fmt) {
618 /* Test conversion from Movie colorspace to
619 * display's target colorspace. */
620 if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle,
621 D3DADAPTER_DEFAULT,
622 D3DDEVTYPE_HAL,
623 fmt_table[i].fourcc,
624 priv->desktop_fmt))) {
625 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Rejected image format: %s\n",
626 vo_format_name(fmt_table[i].mplayer_fmt));
627 return 0;
630 priv->movie_src_fmt = fmt_table[i].fourcc;
631 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Accepted image format: %s\n",
632 vo_format_name(fmt_table[i].mplayer_fmt));
633 return (VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
634 | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN);
639 return 0;
642 /****************************************************************************
646 * libvo Control / Callback functions *
650 ****************************************************************************/
655 /** @brief libvo Callback: Preinitialize the video card.
656 * Preinit the hardware just enough to be queried about
657 * supported formats.
659 * @return 0 on success, -1 on failure
662 static int preinit(const char *arg)
664 D3DDISPLAYMODE disp_mode;
665 D3DCAPS9 disp_caps;
666 DWORD texture_caps;
667 DWORD dev_caps;
669 /* Set to zero all global variables. */
670 priv = calloc(1, sizeof(struct global_priv));
671 if (!priv) {
672 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating private memory failed.\n");
673 return -1;
676 /* FIXME
677 > Please use subopt-helper.h for this, see vo_gl.c:preinit for
678 > an example of how to use it.
681 priv->d3d_handle = Direct3DCreate9(D3D_SDK_VERSION);
682 if (!priv->d3d_handle) {
683 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
684 return -1;
687 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
688 D3DADAPTER_DEFAULT,
689 &disp_mode))) {
690 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display mode failed.\n");
691 return -1;
694 /* Store in priv->desktop_fmt the user desktop's colorspace. Usually XRGB. */
695 priv->desktop_fmt = disp_mode.Format;
696 priv->cur_backbuf_width = disp_mode.Width;
697 priv->cur_backbuf_height = disp_mode.Height;
699 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Setting backbuffer dimensions to (%dx%d).\n",
700 disp_mode.Width, disp_mode.Height);
702 if (FAILED(IDirect3D9_GetDeviceCaps(priv->d3d_handle,
703 D3DADAPTER_DEFAULT,
704 D3DDEVTYPE_HAL,
705 &disp_caps))) {
706 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display capabilities failed.\n");
707 return -1;
710 /* Store relevant information reguarding caps of device */
711 texture_caps = disp_caps.TextureCaps;
712 dev_caps = disp_caps.DevCaps;
713 priv->device_caps_power2_only = (texture_caps & D3DPTEXTURECAPS_POW2) &&
714 !(texture_caps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
715 priv->device_caps_square_only = texture_caps & D3DPTEXTURECAPS_SQUAREONLY;
716 priv->device_texture_sys = dev_caps & D3DDEVCAPS_TEXTURESYSTEMMEMORY;
717 priv->max_texture_width = disp_caps.MaxTextureWidth;
718 priv->max_texture_height = disp_caps.MaxTextureHeight;
720 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>device_caps_power2_only %d, device_caps_square_only %d\n"
721 "<vo_direct3d>device_texture_sys %d\n"
722 "<vo_direct3d>max_texture_width %d, max_texture_height %d\n",
723 priv->device_caps_power2_only, priv->device_caps_square_only,
724 priv->device_texture_sys, priv->max_texture_width,
725 priv->max_texture_height);
727 /* w32_common framework call. Configures window on the screen, gets
728 * fullscreen dimensions and does other useful stuff.
730 if (!vo_w32_init()) {
731 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Configuring onscreen window failed.\n");
732 return -1;
735 return 0;
740 /** @brief libvo Callback: Handle control requests.
741 * @return VO_TRUE on success, VO_NOTIMPL when not implemented
743 static int control(uint32_t request, void *data)
745 switch (request) {
746 case VOCTRL_QUERY_FORMAT:
747 return query_format(*(uint32_t*) data);
748 case VOCTRL_GET_IMAGE: /* Direct Rendering. Not implemented yet. */
749 mp_msg(MSGT_VO, MSGL_V,
750 "<vo_direct3d>Direct Rendering request. Not implemented yet.\n");
751 return VO_NOTIMPL;
752 case VOCTRL_DRAW_IMAGE:
753 return render_d3d_frame(data);
754 case VOCTRL_FULLSCREEN:
755 vo_w32_fullscreen();
756 resize_d3d();
757 return VO_TRUE;
758 case VOCTRL_RESET:
759 return VO_NOTIMPL;
760 case VOCTRL_PAUSE:
761 priv->is_paused = 1;
762 return VO_TRUE;
763 case VOCTRL_RESUME:
764 priv->is_paused = 0;
765 return VO_TRUE;
766 case VOCTRL_GUISUPPORT:
767 return VO_NOTIMPL;
768 case VOCTRL_SET_EQUALIZER:
769 return VO_NOTIMPL;
770 case VOCTRL_GET_EQUALIZER:
771 return VO_NOTIMPL;
772 case VOCTRL_ONTOP:
773 vo_w32_ontop();
774 return VO_TRUE;
775 case VOCTRL_BORDER:
776 vo_w32_border();
777 resize_d3d();
778 return VO_TRUE;
779 case VOCTRL_UPDATE_SCREENINFO:
780 w32_update_xinerama_info();
781 return VO_TRUE;
782 case VOCTRL_SET_PANSCAN:
783 calc_fs_rect();
784 return VO_TRUE;
785 case VOCTRL_GET_PANSCAN:
786 return VO_TRUE;
788 return VO_FALSE;
791 /** @brief libvo Callback: Configre the Direct3D adapter.
792 * @param width Movie source width
793 * @param height Movie source height
794 * @param d_width Screen (destination) width
795 * @param d_height Screen (destination) height
796 * @param options Options bitmap
797 * @param title Window title
798 * @param format Movie colorspace format (using MPlayer's
799 * defines, e.g. IMGFMT_YUY2)
800 * @return 0 on success, VO_ERROR on failure
802 static int config(uint32_t width, uint32_t height, uint32_t d_width,
803 uint32_t d_height, uint32_t options, char *title,
804 uint32_t format)
807 priv->src_width = width;
808 priv->src_height = height;
810 /* w32_common framework call. Creates window on the screen with
811 * the given coordinates.
813 if (!vo_w32_config(d_width, d_height, options)) {
814 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Creating onscreen window failed.\n");
815 return VO_ERROR;
818 /* "config" may be called several times, so if this is not the first
819 * call, we should destroy Direct3D adapter and surfaces before
820 * calling configure_d3d, which will create them again.
823 destroy_d3d_surfaces();
825 /* Destroy the D3D Device */
826 if (priv->d3d_device)
827 IDirect3DDevice9_Release(priv->d3d_device);
828 priv->d3d_device = NULL;
830 if (!configure_d3d())
831 return VO_ERROR;
833 return 0; /* Success */
836 /** @brief libvo Callback: Flip next already drawn frame on the
837 * screen.
839 static void flip_page(void)
841 RECT rect = {0, 0, vo_dwidth, vo_dheight};
842 if (!priv->d3d_device ||
843 FAILED(IDirect3DDevice9_Present(priv->d3d_device, &rect, 0, 0, 0))) {
844 mp_msg(MSGT_VO, MSGL_V,
845 "<vo_direct3d>Trying to reinitialize uncooperative video adapter.\n");
846 if (!reconfigure_d3d()) {
847 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Reinitialization failed.\n");
848 return;
850 else
851 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Video adapter reinitialized.\n");
855 /** @brief libvo Callback: Uninitializes all pointers and closes
856 * all D3D related stuff,
858 static void uninit(void)
860 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit called.\n");
862 uninit_d3d();
863 vo_w32_uninit(); /* w32_common framework call */
864 free(priv);
865 priv = NULL;
868 /** @brief libvo Callback: Handles video window events.
870 static void check_events(void)
872 int flags;
873 /* w32_common framework call. Handles video window events.
874 * Updates global libvo's vo_dwidth/vo_dheight upon resize
875 * with the new window width/height.
877 flags = vo_w32_check_events();
878 if (flags & VO_EVENT_RESIZE)
879 resize_d3d();
881 if ((flags & VO_EVENT_EXPOSE) && priv->is_paused)
882 flip_page();
885 /** @brief libvo Callback: Draw slice
886 * @return 0 on success
888 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y )
890 char *my_src; /**< Pointer to the source image */
891 char *dst; /**< Pointer to the destination image */
892 int uv_stride; /**< Stride of the U/V planes */
894 /* If the D3D device is uncooperative (not initialized), return success.
895 The device will be probed for reinitialization in the next flip_page() */
896 if (!priv->d3d_device)
897 return 0;
899 /* Lock the offscreen surface if it's not already locked. */
900 if (!priv->locked_rect.pBits) {
901 if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface,
902 &priv->locked_rect, NULL, 0))) {
903 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface lock failure.\n");
904 return VO_FALSE;
908 uv_stride = priv->locked_rect.Pitch / 2;
910 /* Copy Y */
911 dst = priv->locked_rect.pBits;
912 dst = dst + priv->locked_rect.Pitch * y + x;
913 my_src = src[0];
914 memcpy_pic(dst, my_src, w, h, priv->locked_rect.Pitch, stride[0]);
916 w /= 2;
917 h /= 2;
918 x /= 2;
919 y /= 2;
921 /* Copy U */
922 dst = priv->locked_rect.pBits;
923 dst = dst + priv->locked_rect.Pitch * priv->src_height
924 + uv_stride * y + x;
925 if (priv->movie_src_fmt == MAKEFOURCC('Y','V','1','2'))
926 my_src = src[2];
927 else
928 my_src = src[1];
930 memcpy_pic(dst, my_src, w, h, uv_stride, stride[1]);
932 /* Copy V */
933 dst = priv->locked_rect.pBits;
934 dst = dst + priv->locked_rect.Pitch * priv->src_height
935 + uv_stride * (priv->src_height / 2) + uv_stride * y + x;
936 if (priv->movie_src_fmt == MAKEFOURCC('Y','V','1','2'))
937 my_src=src[1];
938 else
939 my_src=src[2];
941 memcpy_pic(dst, my_src, w, h, uv_stride, stride[2]);
943 return 0; /* Success */
946 /** @brief libvo Callback: Unused function
948 static int draw_frame(uint8_t *src[])
950 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>draw_frame called.\n");
951 return VO_FALSE;
954 /** @brief Maps MPlayer alpha to D3D
955 * 0x0 -> transparent and discarded by alpha test
956 * 0x1 -> 0xFF to become opaque
957 * other alpha values are inverted +1 (2 = -2)
958 * These values are then inverted again with
959 the texture filter D3DBLEND_INVSRCALPHA
961 void vo_draw_alpha_l8a8(int w, int h, unsigned char* src, unsigned char *srca,
962 int srcstride, unsigned char* dstbase, int dststride)
964 int y;
965 for (y = 0; y < h; y++) {
966 unsigned short *dst = (unsigned short*)dstbase;
967 int x;
968 for (x = 0; x < w; x++) {
969 dst[x] = (-srca[x] << 8) | src[x];
971 src += srcstride;
972 srca += srcstride;
973 dstbase += dststride;
977 /** @brief Callback function to render the OSD to the texture
979 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
980 unsigned char *srca, int stride)
982 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
983 to copy MPlayer's frame inside it.*/
985 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
986 &locked_rect, NULL, 0))) {
987 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture lock failed.\n");
988 return;
991 vo_draw_alpha_l8a8(w, h, src, srca, stride,
992 (unsigned char *)locked_rect.pBits + locked_rect.Pitch*y0 + 2*x0, locked_rect.Pitch);
994 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
995 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
996 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture unlock failed.\n");
997 return;
1000 priv->is_osd_populated = 1;
1003 /** @brief libvo Callback: Draw OSD/Subtitles,
1005 static void draw_osd(void)
1007 // we can not render OSD if we lost the device e.g. because it was uncooperative
1008 if (!priv->d3d_device)
1009 return;
1011 if (vo_osd_changed(0)) {
1012 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
1013 to copy MPlayer's frame inside it.*/
1015 /* clear the OSD */
1016 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
1017 &locked_rect, NULL, 0))) {
1018 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture lock failed.\n");
1019 return;
1022 /* clear the whole texture to avoid issues due to interpolation */
1023 memset(locked_rect.pBits, 0, locked_rect.Pitch * priv->osd_texture_height);
1025 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1026 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
1027 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture unlock failed.\n");
1028 return;
1031 priv->is_osd_populated = 0;
1032 /* required for if subs are in the boarder region */
1033 priv->is_clear_needed = 1;
1035 vo_draw_text_ext(priv->osd_width, priv->osd_height, priv->border_x, priv->border_y,
1036 priv->border_x, priv->border_y, priv->src_width, priv->src_height, draw_alpha);
1038 if (!priv->device_texture_sys)
1040 /* only DMA to the shadow if its required */
1041 if (FAILED(IDirect3DDevice9_UpdateTexture(priv->d3d_device,
1042 (IDirect3DBaseTexture9 *)priv->d3d_texture_system,
1043 (IDirect3DBaseTexture9 *)priv->d3d_texture_osd))) {
1044 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture transfer failed.\n");
1045 return;
1050 /* update OSD */
1052 if (priv->is_osd_populated) {
1054 struct_vertex osd_quad_vb[] = {
1055 {-1.0f, 1.0f, 0.0f, 0, 0 },
1056 { 1.0f, 1.0f, 0.0f, 1, 0 },
1057 {-1.0f,-1.0f, 0.0f, 0, 1 },
1058 { 1.0f,-1.0f, 0.0f, 1, 1 }
1061 /* calculate the texture coordinates */
1062 osd_quad_vb[1].tu =
1063 osd_quad_vb[3].tu = (float)priv->osd_width / priv->osd_texture_width;
1064 osd_quad_vb[2].tv =
1065 osd_quad_vb[3].tv = (float)priv->osd_height / priv->osd_texture_height;
1067 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
1068 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>BeginScene failed.\n");
1069 return;
1072 /* turn on alpha test */
1073 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, TRUE);
1074 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, TRUE);
1076 /* need to use a texture here (done here as we may be able to texture from system memory) */
1077 IDirect3DDevice9_SetTexture(priv->d3d_device, 0,
1078 (IDirect3DBaseTexture9 *)(priv->device_texture_sys
1079 ? priv->d3d_texture_system : priv->d3d_texture_osd));
1081 IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_MY_VERTEX);
1082 IDirect3DDevice9_DrawPrimitiveUP(priv->d3d_device, D3DPT_TRIANGLESTRIP, 2, osd_quad_vb, sizeof(struct_vertex));
1084 /* turn off alpha test */
1085 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, FALSE);
1086 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, FALSE);
1088 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
1089 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>EndScene failed.\n");
1090 return;