1 /*
2 * Copyright (c) 2008 Georgi Petrov (gogothebee) <gogothebee@gmail.com>
3 *
4 * This file is part of MPlayer.
5 *
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.
10 *
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.
15 *
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.
19 */
20
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 #define NO_DRAW_FRAME
28 #include "video_out_internal.h"
29 #include "fastmemcpy.h"
30 #include "mp_msg.h"
31 #include "aspect.h"
32 #include "w32_common.h"
33 #include "libavutil/common.h"
34 #include "sub/font_load.h"
35 #include "sub/sub.h"
36
37 static const vo_info_t info =
38 {
39 "Direct3D 9 Renderer",
40 "direct3d",
41 "Georgi Petrov (gogothebee) <gogothebee@gmail.com>",
42 ""
43 };
44
45 /*
46 * Link essential libvo functions: preinit, config, control, draw_frame,
47 * draw_slice, draw_osd, flip_page, check_events, uninit and
48 * the structure info.
49 */
50 const LIBVO_EXTERN(direct3d)
51
52
53 /* Global variables "priv" structure. I try to keep their count low.
54 */
55 static struct global_priv {
56 int is_paused; /**< 1 = Movie is paused,
57 0 = Movie is not paused */
58 int is_clear_needed; /**< 1 = Clear the backbuffer before StretchRect
59 0 = (default) Don't clear it */
60 D3DLOCKED_RECT locked_rect; /**< The locked offscreen surface */
61 RECT fs_movie_rect; /**< Rect (upscaled) of the movie when displayed
62 in fullscreen */
63 RECT fs_panscan_rect; /**< PanScan source surface cropping in
64 fullscreen */
65 int src_width; /**< Source (movie) width */
66 int src_height; /**< Source (movie) heigth */
67 int border_x; /**< horizontal border value for OSD */
68 int border_y; /**< vertical border value for OSD */
69
70 D3DFORMAT movie_src_fmt; /**< Movie colorspace format (depends on
71 the movie's codec) */
72 D3DFORMAT desktop_fmt; /**< Desktop (screen) colorspace format.
73 Usually XRGB */
74
75 HANDLE d3d9_dll; /**< d3d9 Library HANDLE */
76 IDirect3D9 * (WINAPI *pDirect3DCreate9)(UINT); /**< pointer to Direct3DCreate9 function */
77
78 LPDIRECT3D9 d3d_handle; /**< Direct3D Handle */
79 LPDIRECT3DDEVICE9 d3d_device; /**< The Direct3D Adapter */
80 IDirect3DSurface9 *d3d_surface; /**< Offscreen Direct3D Surface. MPlayer
81 renders inside it. Uses colorspace
82 priv->movie_src_fmt */
83 IDirect3DTexture9 *d3d_texture_osd; /**< Direct3D Texture. Uses RGBA */
84 IDirect3DTexture9 *d3d_texture_system; /**< Direct3D Texture. System memory
85 cannot lock a normal texture. Uses RGBA */
86 IDirect3DSurface9 *d3d_backbuf; /**< Video card's back buffer (used to
87 display next frame) */
88 int cur_backbuf_width; /**< Current backbuffer width */
89 int cur_backbuf_height; /**< Current backbuffer height */
90 int is_osd_populated; /**< 1 = OSD texture has something to display,
91 0 = OSD texture is clear */
92 int device_caps_power2_only; /**< 1 = texture sizes have to be power 2
93 0 = texture sizes can be anything */
94 int device_caps_square_only; /**< 1 = textures have to be square
95 0 = textures do not have to be square */
96 int device_texture_sys; /**< 1 = device can texture from system memory
97 0 = device requires shadow */
98 int max_texture_width; /**< from the device capabilities */
99 int max_texture_height; /**< from the device capabilities */
100 int osd_width; /**< current width of the OSD */
101 int osd_height; /**< current height of the OSD */
102 int osd_texture_width; /**< current width of the OSD texture */
103 int osd_texture_height; /**< current height of the OSD texture */
104 } *priv;
105
106 typedef struct {
107 const unsigned int mplayer_fmt; /**< Given by MPlayer */
108 const D3DFORMAT fourcc; /**< Required by D3D's test function */
109 } struct_fmt_table;
110
111 /* Map table from reported MPlayer format to the required
112 fourcc. This is needed to perform the format query. */
113
114 static const struct_fmt_table fmt_table[] = {
115 {IMGFMT_YV12, MAKEFOURCC('Y','V','1','2')},
116 {IMGFMT_I420, MAKEFOURCC('I','4','2','0')},
117 {IMGFMT_IYUV, MAKEFOURCC('I','Y','U','V')},
118 {IMGFMT_YVU9, MAKEFOURCC('Y','V','U','9')},
119 {IMGFMT_YUY2, D3DFMT_YUY2},
120 {IMGFMT_UYVY, D3DFMT_UYVY},
121 {IMGFMT_BGR32, D3DFMT_X8R8G8B8},
122 {IMGFMT_RGB32, D3DFMT_X8B8G8R8},
123 {IMGFMT_BGR24, D3DFMT_R8G8B8}, //untested
124 {IMGFMT_BGR16, D3DFMT_R5G6B5},
125 {IMGFMT_BGR15, D3DFMT_X1R5G5B5},
126 {IMGFMT_BGR8 , D3DFMT_R3G3B2}, //untested
127 };
128
129 #define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0]))
130
131 #define D3DFVF_MY_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
132
133 typedef struct {
134 float x, y, z; /* Position of vertex in 3D space */
135 float tu, tv; /* Texture coordinates */
136 } struct_vertex;
137
138 typedef enum back_buffer_action {
139 BACKBUFFER_CREATE,
140 BACKBUFFER_RESET
141 } back_buffer_action_e;
142 /****************************************************************************
143 * *
144 * *
145 * *
146 * Direct3D specific implementation functions *
147 * *
148 * *
149 * *
150 ****************************************************************************/
151
152 /** @brief Calculate scaled fullscreen movie rectangle with
153 * preserved aspect ratio.
154 */
calc_fs_rect(void)155 static void calc_fs_rect(void)
156 {
157 struct vo_rect src_rect;
158 struct vo_rect dst_rect;
159 struct vo_rect borders;
160 calc_src_dst_rects(priv->src_width, priv->src_height, &src_rect, &dst_rect, &borders, NULL);
161
162 priv->fs_movie_rect.left = dst_rect.left;
163 priv->fs_movie_rect.right = dst_rect.right;
164 priv->fs_movie_rect.top = dst_rect.top;
165 priv->fs_movie_rect.bottom = dst_rect.bottom;
166 priv->fs_panscan_rect.left = src_rect.left;
167 priv->fs_panscan_rect.right = src_rect.right;
168 priv->fs_panscan_rect.top = src_rect.top;
169 priv->fs_panscan_rect.bottom = src_rect.bottom;
170 priv->border_x = borders.left;
171 priv->border_y = borders.top;
172
173 mp_msg(MSGT_VO, MSGL_V,
174 "<vo_direct3d>Fullscreen movie rectangle: t: %ld, l: %ld, r: %ld, b:%ld\n",
175 priv->fs_movie_rect.top, priv->fs_movie_rect.left,
176 priv->fs_movie_rect.right, priv->fs_movie_rect.bottom);
177
178 /* The backbuffer should be cleared before next StretchRect. This is
179 * necessary because our new draw area could be smaller than the
180 * previous one used by StretchRect and without it, leftovers from the
181 * previous frame will be left. */
182 priv->is_clear_needed = 1;
183 }
184
185 /** @brief Destroy D3D Offscreen and Backbuffer surfaces.
186 */
destroy_d3d_surfaces(void)187 static void destroy_d3d_surfaces(void)
188 {
189 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>destroy_d3d_surfaces called.\n");
190 /* Let's destroy the old (if any) D3D Surfaces */
191
192 if (priv->locked_rect.pBits)
193 IDirect3DSurface9_UnlockRect(priv->d3d_surface);
194 priv->locked_rect.pBits = NULL;
195
196 if (priv->d3d_surface)
197 IDirect3DSurface9_Release(priv->d3d_surface);
198 priv->d3d_surface = NULL;
199
200 /* kill the OSD texture and its shadow copy */
201 if (priv->d3d_texture_osd)
202 IDirect3DTexture9_Release(priv->d3d_texture_osd);
203 priv->d3d_texture_osd = NULL;
204
205 if (priv->d3d_texture_system)
206 IDirect3DTexture9_Release(priv->d3d_texture_system);
207 priv->d3d_texture_system = NULL;
208
209 if (priv->d3d_backbuf)
210 IDirect3DSurface9_Release(priv->d3d_backbuf);
211 priv->d3d_backbuf = NULL;
212 }
213
214 /** @brief Create D3D Offscreen and Backbuffer surfaces. Each
215 * surface is created only if it's not already present.
216 * @return 1 on success, 0 on failure
217 */
create_d3d_surfaces(void)218 static int create_d3d_surfaces(void)
219 {
220 int osd_width = vo_dwidth, osd_height = vo_dheight;
221 int tex_width = osd_width, tex_height = osd_height;
222 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>create_d3d_surfaces called.\n");
223
224 if (!priv->d3d_surface &&
225 FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(
226 priv->d3d_device, priv->src_width, priv->src_height,
227 priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) {
228 mp_msg(MSGT_VO, MSGL_ERR,
229 "<vo_direct3d>Allocating offscreen surface failed.\n");
230 return 0;
231 }
232
233 if (!priv->d3d_backbuf &&
234 FAILED(IDirect3DDevice9_GetBackBuffer(priv->d3d_device, 0, 0,
235 D3DBACKBUFFER_TYPE_MONO,
236 &priv->d3d_backbuf))) {
237 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating backbuffer failed.\n");
238 return 0;
239 }
240
241 if (!tex_width || !tex_height) {
242 mp_msg(MSGT_VO, MSGL_V,
243 "<vo_direct3d>Deferring surface creation because width or height is 0.\n");
244 return 0;
245 }
246
247 /* calculate the best size for the OSD depending on the factors from the device */
248 if (priv->device_caps_power2_only) {
249 tex_width = 1;
250 tex_height = 1;
251 while (tex_width < osd_width ) tex_width <<= 1;
252 while (tex_height < osd_height) tex_height <<= 1;
253 }
254 if (priv->device_caps_square_only)
255 /* device only supports square textures */
256 tex_width = tex_height = tex_width > tex_height ? tex_width : tex_height;
257 // better round up to a multiple of 16
258 tex_width = (tex_width + 15) & ~15;
259 tex_height = (tex_height + 15) & ~15;
260
261 // make sure we respect the size limits without breaking aspect or pow2-requirements
262 while (tex_width > priv->max_texture_width || tex_height > priv->max_texture_height) {
263 osd_width >>= 1;
264 osd_height >>= 1;
265 tex_width >>= 1;
266 tex_height >>= 1;
267 }
268
269 priv->osd_width = osd_width;
270 priv->osd_height = osd_height;
271 priv->osd_texture_width = tex_width;
272 priv->osd_texture_height = tex_height;
273
274 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>OSD texture size (%dx%d), requested (%dx%d).\n",
275 vo_dwidth, vo_dheight, priv->osd_texture_width, priv->osd_texture_height);
276
277 /* create OSD */
278 if (!priv->d3d_texture_system &&
279 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
280 priv->osd_texture_width,
281 priv->osd_texture_height,
282 1,
283 D3DUSAGE_DYNAMIC,
284 D3DFMT_A8L8,
285 D3DPOOL_SYSTEMMEM,
286 &priv->d3d_texture_system,
287 NULL))) {
288 mp_msg(MSGT_VO,MSGL_ERR,
289 "<vo_direct3d>Allocating OSD texture in system RAM failed.\n");
290 return 0;
291 }
292
293 if (!priv->device_texture_sys) {
294 /* only create if we need a shadow version on the external device */
295 if (!priv->d3d_texture_osd &&
296 FAILED(IDirect3DDevice9_CreateTexture(priv->d3d_device,
297 priv->osd_texture_width,
298 priv->osd_texture_height,
299 1,
300 D3DUSAGE_DYNAMIC,
301 D3DFMT_A8L8,
302 D3DPOOL_DEFAULT,
303 &priv->d3d_texture_osd,
304 NULL))) {
305 mp_msg(MSGT_VO,MSGL_ERR,
306 "<vo_direct3d>Allocating OSD texture in video RAM failed.\n");
307 return 0;
308 }
309 }
310
311 /* setup default renderstate */
312 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_ONE);
313 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
314 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
315 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAREF, (DWORD)0x0);
316 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_LIGHTING, FALSE);
317 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
318 IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
319
320 return 1;
321 }
322
323 /** @brief Fill D3D Presentation parameters
324 */
fill_d3d_presentparams(D3DPRESENT_PARAMETERS * present_params)325 static void fill_d3d_presentparams(D3DPRESENT_PARAMETERS *present_params)
326 {
327 /* Prepare Direct3D initialization parameters. */
328 memset(present_params, 0, sizeof(D3DPRESENT_PARAMETERS));
329 present_params->Windowed = TRUE;
330 present_params->SwapEffect = D3DSWAPEFFECT_COPY;
331 present_params->Flags = D3DPRESENTFLAG_VIDEO;
332 present_params->hDeviceWindow = vo_w32_window; /* w32_common var */
333 present_params->BackBufferWidth = priv->cur_backbuf_width;
334 present_params->BackBufferHeight = priv->cur_backbuf_height;
335 present_params->MultiSampleType = D3DMULTISAMPLE_NONE;
336 present_params->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
337 present_params->BackBufferFormat = priv->desktop_fmt;
338 present_params->BackBufferCount = 1;
339 present_params->EnableAutoDepthStencil = FALSE;
340 }
341
342
343 /** @brief Create a new backbuffer. Create or Reset the D3D
344 * device.
345 * @return 1 on success, 0 on failure
346 */
change_d3d_backbuffer(back_buffer_action_e action)347 static int change_d3d_backbuffer(back_buffer_action_e action)
348 {
349 D3DPRESENT_PARAMETERS present_params;
350
351 destroy_d3d_surfaces();
352
353 /* Grow the backbuffer in the required dimension. */
354 if (vo_dwidth > priv->cur_backbuf_width)
355 priv->cur_backbuf_width = vo_dwidth;
356
357 if (vo_dheight > priv->cur_backbuf_height)
358 priv->cur_backbuf_height = vo_dheight;
359
360 /* The grown backbuffer dimensions are ready and fill_d3d_presentparams
361 * will use them, so we can reset the device.
362 */
363 fill_d3d_presentparams(&present_params);
364
365 /* vo_w32_window is w32_common variable. It's a handle to the window. */
366 if (action == BACKBUFFER_CREATE &&
367 FAILED(IDirect3D9_CreateDevice(priv->d3d_handle,
368 D3DADAPTER_DEFAULT,
369 D3DDEVTYPE_HAL, vo_w32_window,
370 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
371 &present_params, &priv->d3d_device))) {
372 mp_msg(MSGT_VO, MSGL_V,
373 "<vo_direct3d>Creating Direct3D device failed.\n");
374 return 0;
375 }
376
377 if (action == BACKBUFFER_RESET &&
378 FAILED(IDirect3DDevice9_Reset(priv->d3d_device, &present_params))) {
379 mp_msg(MSGT_VO, MSGL_ERR,
380 "<vo_direct3d>Reseting Direct3D device failed.\n");
381 return 0;
382 }
383
384 mp_msg(MSGT_VO, MSGL_V,
385 "<vo_direct3d>New backbuffer (%dx%d), VO (%dx%d)\n",
386 present_params.BackBufferWidth, present_params.BackBufferHeight,
387 vo_dwidth, vo_dheight);
388
389 return 1;
390 }
391
392 /** @brief Configure initial Direct3D context. The first
393 * function called to initialize the D3D context.
394 * @return 1 on success, 0 on failure
395 */
configure_d3d(void)396 static int configure_d3d(void)
397 {
398 D3DDISPLAYMODE disp_mode;
399 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
400
401 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>configure_d3d called.\n");
402
403 destroy_d3d_surfaces();
404
405 /* Get the current desktop display mode, so we can set up a back buffer
406 * of the same format. */
407 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
408 D3DADAPTER_DEFAULT,
409 &disp_mode))) {
410 mp_msg(MSGT_VO, MSGL_ERR,
411 "<vo_direct3d>Reading adapter display mode failed.\n");
412 return 0;
413 }
414
415 /* Write current Desktop's colorspace format in the global storage. */
416 priv->desktop_fmt = disp_mode.Format;
417
418 if (!change_d3d_backbuffer(BACKBUFFER_CREATE))
419 return 0;
420
421 if (!create_d3d_surfaces())
422 return 0;
423
424 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
425 &vp))) {
426 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
427 return 0;
428 }
429
430 calc_fs_rect();
431
432 return 1;
433 }
434
435 /** @brief Reconfigure the whole Direct3D. Called only
436 * when the video adapter becomes uncooperative.
437 * @return 1 on success, 0 on failure
438 */
reconfigure_d3d(void)439 static int reconfigure_d3d(void)
440 {
441 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>reconfigure_d3d called.\n");
442
443 /* Destroy the offscreen, OSD and backbuffer surfaces */
444 destroy_d3d_surfaces();
445
446 /* Destroy the D3D Device */
447 if (priv->d3d_device)
448 IDirect3DDevice9_Release(priv->d3d_device);
449 priv->d3d_device = NULL;
450
451 /* Stop the whole Direct3D */
452 IDirect3D9_Release(priv->d3d_handle);
453
454 /* Initialize Direct3D from the beginning */
455 priv->d3d_handle = priv->pDirect3DCreate9(D3D_SDK_VERSION);
456 if (!priv->d3d_handle) {
457 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
458 return 0;
459 }
460
461 /* Configure Direct3D */
462 if (!configure_d3d())
463 return 0;
464
465 return 1;
466 }
467
468 /** @brief Resize Direct3D context on window resize.
469 * @return 1 on success, 0 on failure
470 */
resize_d3d(void)471 static int resize_d3d(void)
472 {
473 D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1};
474
475 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>resize_d3d called.\n");
476
477 /* Make sure that backbuffer is large enough to accomodate the new
478 viewport dimensions. Grow it if necessary. */
479
480 if (vo_dwidth > priv->cur_backbuf_width ||
481 vo_dheight > priv->cur_backbuf_height) {
482 if (!change_d3d_backbuffer(BACKBUFFER_RESET))
483 return 0;
484 }
485
486 /* Destroy the OSD textures. They should always match the new dimensions
487 * of the onscreen window, so on each resize we need new OSD dimensions.
488 */
489
490 if (priv->d3d_texture_osd)
491 IDirect3DTexture9_Release(priv->d3d_texture_osd);
492 priv->d3d_texture_osd = NULL;
493
494 if (priv->d3d_texture_system)
495 IDirect3DTexture9_Release(priv->d3d_texture_system);
496 priv->d3d_texture_system = NULL;
497
498
499 /* Recreate the OSD. The function will observe that the offscreen plain
500 * surface and the backbuffer are not destroyed and will skip their creation,
501 * effectively recreating only the OSD.
502 */
503
504 if (!create_d3d_surfaces())
505 return 0;
506
507 if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device,
508 &vp))) {
509 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Setting viewport failed.\n");
510 return 0;
511 }
512
513 calc_fs_rect();
514
515 #ifdef CONFIG_FREETYPE
516 // font needs to be adjusted
517 force_load_font = 1;
518 #endif
519 // OSD needs to be drawn fresh for new size
520 vo_osd_changed(OSDTYPE_OSD);
521
522 return 1;
523 }
524
525 /** @brief Uninitialize Direct3D and close the window.
526 */
uninit_d3d(void)527 static void uninit_d3d(void)
528 {
529 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit_d3d called.\n");
530
531 destroy_d3d_surfaces();
532
533 /* Destroy the D3D Device */
534 if (priv->d3d_device)
535 IDirect3DDevice9_Release(priv->d3d_device);
536 priv->d3d_device = NULL;
537
538 /* Stop the whole D3D. */
539 if (priv->d3d_handle) {
540 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Stopping Direct3D.\n");
541 IDirect3D9_Release(priv->d3d_handle);
542 }
543 priv->d3d_handle = NULL;
544 }
545
546 /** @brief Render a frame on the screen.
547 * @param mpi mpi structure with the decoded frame inside
548 * @return VO_TRUE on success, VO_ERROR on failure
549 */
render_d3d_frame(mp_image_t * mpi)550 static uint32_t render_d3d_frame(mp_image_t *mpi)
551 {
552 /* Uncomment when direct rendering is implemented.
553 * if (mpi->flags & MP_IMGFLAG_DIRECT) ...
554 */
555
556 /* If the D3D device is uncooperative (not initialized), return success.
557 The device will be probed for reinitialization in the next flip_page() */
558 if (!priv->d3d_device)
559 return VO_TRUE;
560
561 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
562 goto skip_upload;
563
564 if (mpi->flags & MP_IMGFLAG_PLANAR) { /* Copy a planar frame. */
565 draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0);
566 goto skip_upload;
567 }
568
569 /* If we're here, then we should lock the rect and copy a packed frame */
570 if (!priv->locked_rect.pBits) {
571 if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface,
572 &priv->locked_rect, NULL, 0))) {
573 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Surface lock failed.\n");
574 return VO_ERROR;
575 }
576 }
577
578 memcpy_pic(priv->locked_rect.pBits, mpi->planes[0], mpi->stride[0],
579 mpi->height, priv->locked_rect.Pitch, mpi->stride[0]);
580
581 skip_upload:
582 /* This unlock is used for both slice_draw path and render_d3d_frame path. */
583 if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface))) {
584 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Surface unlock failed.\n");
585 return VO_ERROR;
586 }
587 priv->locked_rect.pBits = NULL;
588
589 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
590 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>BeginScene failed.\n");
591 return VO_ERROR;
592 }
593
594 if (priv->is_clear_needed) {
595 IDirect3DDevice9_Clear(priv->d3d_device, 0, NULL,
596 D3DCLEAR_TARGET, 0, 0, 0);
597 priv->is_clear_needed = 0;
598 }
599
600 if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device,
601 priv->d3d_surface,
602 &priv->fs_panscan_rect,
603 priv->d3d_backbuf,
604 &priv->fs_movie_rect,
605 D3DTEXF_LINEAR))) {
606 mp_msg(MSGT_VO, MSGL_ERR,
607 "<vo_direct3d>Copying frame to the backbuffer failed.\n");
608 return VO_ERROR;
609 }
610
611 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
612 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>EndScene failed.\n");
613 return VO_ERROR;
614 }
615
616 return VO_TRUE;
617 }
618
619
620 /** @brief Query if movie colorspace is supported by the HW.
621 * @return 0 on failure, device capabilities (not probed
622 * currently) on success.
623 */
query_format(uint32_t movie_fmt)624 static int query_format(uint32_t movie_fmt)
625 {
626 int i;
627 for (i = 0; i < DISPLAY_FORMAT_TABLE_ENTRIES; i++) {
628 if (fmt_table[i].mplayer_fmt == movie_fmt) {
629 /* Test conversion from Movie colorspace to
630 * display's target colorspace. */
631 if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle,
632 D3DADAPTER_DEFAULT,
633 D3DDEVTYPE_HAL,
634 fmt_table[i].fourcc,
635 priv->desktop_fmt))) {
636 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Rejected image format: %s\n",
637 vo_format_name(fmt_table[i].mplayer_fmt));
638 return 0;
639 }
640
641 priv->movie_src_fmt = fmt_table[i].fourcc;
642 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Accepted image format: %s\n",
643 vo_format_name(fmt_table[i].mplayer_fmt));
644 return (VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
645 | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN);
646
647 }
648 }
649
650 return 0;
651 }
652
653 /****************************************************************************
654 * *
655 * *
656 * *
657 * libvo Control / Callback functions *
658 * *
659 * *
660 * *
661 ****************************************************************************/
662
663
664
665
666 /** @brief libvo Callback: Preinitialize the video card.
667 * Preinit the hardware just enough to be queried about
668 * supported formats.
669 *
670 * @return 0 on success, -1 on failure
671 */
672
preinit(const char * arg)673 static int preinit(const char *arg)
674 {
675 D3DDISPLAYMODE disp_mode;
676 D3DCAPS9 disp_caps;
677 DWORD texture_caps;
678 DWORD dev_caps;
679
680 /* Set to zero all global variables. */
681 priv = calloc(1, sizeof(struct global_priv));
682 if (!priv) {
683 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Allocating private memory failed.\n");
684 goto err_out;
685 }
686
687 /* FIXME
688 > Please use subopt-helper.h for this, see vo_gl.c:preinit for
689 > an example of how to use it.
690 */
691
692 priv->d3d9_dll = LoadLibraryA("d3d9.dll");
693 if (!priv->d3d9_dll) {
694 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Unable to dynamically load d3d9.dll\n");
695 goto err_out;
696 }
697
698 priv->pDirect3DCreate9 = (void *)GetProcAddress(priv->d3d9_dll, "Direct3DCreate9");
699 if (!priv->pDirect3DCreate9) {
700 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Unable to find entry point of Direct3DCreate9\n");
701 goto err_out;
702 }
703
704 priv->d3d_handle = priv->pDirect3DCreate9(D3D_SDK_VERSION);
705 if (!priv->d3d_handle) {
706 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Initializing Direct3D failed.\n");
707 goto err_out;
708 }
709
710 if (FAILED(IDirect3D9_GetAdapterDisplayMode(priv->d3d_handle,
711 D3DADAPTER_DEFAULT,
712 &disp_mode))) {
713 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display mode failed.\n");
714 goto err_out;
715 }
716
717 /* Store in priv->desktop_fmt the user desktop's colorspace. Usually XRGB. */
718 priv->desktop_fmt = disp_mode.Format;
719 priv->cur_backbuf_width = disp_mode.Width;
720 priv->cur_backbuf_height = disp_mode.Height;
721
722 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Setting backbuffer dimensions to (%dx%d).\n",
723 disp_mode.Width, disp_mode.Height);
724
725 if (FAILED(IDirect3D9_GetDeviceCaps(priv->d3d_handle,
726 D3DADAPTER_DEFAULT,
727 D3DDEVTYPE_HAL,
728 &disp_caps))) {
729 mp_msg(MSGT_VO, MSGL_ERR, "<vo_direct3d>Reading display capabilities failed.\n");
730 goto err_out;
731 }
732
733 /* Store relevant information reguarding caps of device */
734 texture_caps = disp_caps.TextureCaps;
735 dev_caps = disp_caps.DevCaps;
736 priv->device_caps_power2_only = (texture_caps & D3DPTEXTURECAPS_POW2) &&
737 !(texture_caps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
738 priv->device_caps_square_only = texture_caps & D3DPTEXTURECAPS_SQUAREONLY;
739 priv->device_texture_sys = dev_caps & D3DDEVCAPS_TEXTURESYSTEMMEMORY;
740 priv->max_texture_width = disp_caps.MaxTextureWidth;
741 priv->max_texture_height = disp_caps.MaxTextureHeight;
742
743 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>device_caps_power2_only %d, device_caps_square_only %d\n"
744 "<vo_direct3d>device_texture_sys %d\n"
745 "<vo_direct3d>max_texture_width %d, max_texture_height %d\n",
746 priv->device_caps_power2_only, priv->device_caps_square_only,
747 priv->device_texture_sys, priv->max_texture_width,
748 priv->max_texture_height);
749
750 /* w32_common framework call. Configures window on the screen, gets
751 * fullscreen dimensions and does other useful stuff.
752 */
753 if (!vo_w32_init()) {
754 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Configuring onscreen window failed.\n");
755 goto err_out;
756 }
757
758 return 0;
759
760 err_out:
761 uninit();
762 return -1;
763 }
764
765
766
767 /** @brief libvo Callback: Handle control requests.
768 * @return VO_TRUE on success, VO_NOTIMPL when not implemented
769 */
control(uint32_t request,void * data)770 static int control(uint32_t request, void *data)
771 {
772 switch (request) {
773 case VOCTRL_QUERY_FORMAT:
774 return query_format(*(uint32_t*) data);
775 case VOCTRL_GET_IMAGE: /* Direct Rendering. Not implemented yet. */
776 mp_msg(MSGT_VO, MSGL_V,
777 "<vo_direct3d>Direct Rendering request. Not implemented yet.\n");
778 return VO_NOTIMPL;
779 case VOCTRL_DRAW_IMAGE:
780 return render_d3d_frame(data);
781 case VOCTRL_FULLSCREEN:
782 vo_w32_fullscreen();
783 resize_d3d();
784 return VO_TRUE;
785 case VOCTRL_PAUSE:
786 priv->is_paused = 1;
787 return VO_TRUE;
788 case VOCTRL_RESUME:
789 priv->is_paused = 0;
790 return VO_TRUE;
791 case VOCTRL_GUISUPPORT:
792 return VO_TRUE;
793 case VOCTRL_ONTOP:
794 vo_w32_ontop();
795 return VO_TRUE;
796 case VOCTRL_BORDER:
797 vo_w32_border();
798 resize_d3d();
799 return VO_TRUE;
800 case VOCTRL_UPDATE_SCREENINFO:
801 w32_update_xinerama_info();
802 return VO_TRUE;
803 case VOCTRL_SET_PANSCAN:
804 calc_fs_rect();
805 return VO_TRUE;
806 case VOCTRL_GET_PANSCAN:
807 return VO_TRUE;
808 }
809 return VO_NOTIMPL;
810 }
811
812 /** @brief libvo Callback: Configre the Direct3D adapter.
813 * @param width Movie source width
814 * @param height Movie source height
815 * @param d_width Screen (destination) width
816 * @param d_height Screen (destination) height
817 * @param options Options bitmap
818 * @param title Window title
819 * @param format Movie colorspace format (using MPlayer's
820 * defines, e.g. IMGFMT_YUY2)
821 * @return 0 on success, VO_ERROR on failure
822 */
config(uint32_t width,uint32_t height,uint32_t d_width,uint32_t d_height,uint32_t options,char * title,uint32_t format)823 static int config(uint32_t width, uint32_t height, uint32_t d_width,
824 uint32_t d_height, uint32_t options, char *title,
825 uint32_t format)
826 {
827
828 priv->src_width = width;
829 priv->src_height = height;
830
831 /* w32_common framework call. Creates window on the screen with
832 * the given coordinates.
833 */
834 if (!vo_w32_config(d_width, d_height, options)) {
835 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Creating onscreen window failed.\n");
836 return VO_ERROR;
837 }
838
839 /* "config" may be called several times, so if this is not the first
840 * call, we should destroy Direct3D adapter and surfaces before
841 * calling configure_d3d, which will create them again.
842 */
843
844 destroy_d3d_surfaces();
845
846 /* Destroy the D3D Device */
847 if (priv->d3d_device)
848 IDirect3DDevice9_Release(priv->d3d_device);
849 priv->d3d_device = NULL;
850
851 if (!configure_d3d())
852 return VO_ERROR;
853
854 return 0; /* Success */
855 }
856
857 /** @brief libvo Callback: Flip next already drawn frame on the
858 * screen.
859 */
flip_page(void)860 static void flip_page(void)
861 {
862 RECT rect = {0, 0, vo_dwidth, vo_dheight};
863 if (!priv->d3d_device ||
864 FAILED(IDirect3DDevice9_Present(priv->d3d_device, &rect, 0, 0, 0))) {
865 mp_msg(MSGT_VO, MSGL_V,
866 "<vo_direct3d>Trying to reinitialize uncooperative video adapter.\n");
867 if (!reconfigure_d3d()) {
868 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Reinitialization failed.\n");
869 return;
870 }
871 else
872 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>Video adapter reinitialized.\n");
873 }
874 }
875
876 /** @brief libvo Callback: Uninitializes all pointers and closes
877 * all D3D related stuff,
878 */
uninit(void)879 static void uninit(void)
880 {
881 mp_msg(MSGT_VO, MSGL_V, "<vo_direct3d>uninit called.\n");
882
883 uninit_d3d();
884 vo_w32_uninit(); /* w32_common framework call */
885 if (priv->d3d9_dll)
886 FreeLibrary(priv->d3d9_dll);
887 priv->d3d9_dll = NULL;
888 free(priv);
889 priv = NULL;
890 }
891
892 /** @brief libvo Callback: Handles video window events.
893 */
check_events(void)894 static void check_events(void)
895 {
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.
900 */
901 flags = vo_w32_check_events();
902 if (flags & VO_EVENT_RESIZE)
903 resize_d3d();
904
905 if ((flags & VO_EVENT_EXPOSE) && priv->is_paused)
906 flip_page();
907 }
908
909 /** @brief libvo Callback: Draw slice
910 * @return 0 on success
911 */
draw_slice(uint8_t * src[],int stride[],int w,int h,int x,int y)912 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y )
913 {
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 */
917
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;
922
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;
929 }
930 }
931
932 uv_stride = priv->locked_rect.Pitch / 2;
933
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]);
939
940 w /= 2;
941 h /= 2;
942 x /= 2;
943 y /= 2;
944
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];
953
954 memcpy_pic(dst, my_src, w, h, uv_stride, stride[1]);
955
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];
964
965 memcpy_pic(dst, my_src, w, h, uv_stride, stride[2]);
966
967 return 0; /* Success */
968 }
969
970 /** @brief Maps MPlayer alpha to D3D
971 * 0x0 -> transparent and discarded by alpha test
972 * 0x1 -> 0xFF to become opaque
973 * other alpha values are inverted +1 (2 = -2)
974 * These values are then inverted again with
975 the texture filter D3DBLEND_INVSRCALPHA
976 */
vo_draw_alpha_l8a8(int w,int h,unsigned char * src,unsigned char * srca,int srcstride,unsigned char * dstbase,int dststride)977 static void vo_draw_alpha_l8a8(int w, int h, unsigned char* src,
978 unsigned char *srca, int srcstride,
979 unsigned char* dstbase, int dststride)
980 {
981 int y;
982 for (y = 0; y < h; y++) {
983 unsigned short *dst = (unsigned short*)dstbase;
984 int x;
985 for (x = 0; x < w; x++) {
986 dst[x] = (-srca[x] << 8) | src[x];
987 }
988 src += srcstride;
989 srca += srcstride;
990 dstbase += dststride;
991 }
992 }
993
994 /** @brief Callback function to render the OSD to the texture
995 */
draw_alpha(int x0,int y0,int w,int h,unsigned char * src,unsigned char * srca,int stride)996 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
997 unsigned char *srca, int stride)
998 {
999 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
1000 to copy MPlayer's frame inside it.*/
1001
1002 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
1003 &locked_rect, NULL, 0))) {
1004 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture lock failed.\n");
1005 return;
1006 }
1007
1008 vo_draw_alpha_l8a8(w, h, src, srca, stride,
1009 (unsigned char *)locked_rect.pBits + locked_rect.Pitch*y0 + 2*x0, locked_rect.Pitch);
1010
1011 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1012 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
1013 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>OSD texture unlock failed.\n");
1014 return;
1015 }
1016
1017 priv->is_osd_populated = 1;
1018 }
1019
1020 /** @brief libvo Callback: Draw OSD/Subtitles,
1021 */
draw_osd(void)1022 static void draw_osd(void)
1023 {
1024 // we can not render OSD if we lost the device e.g. because it was uncooperative
1025 // or if the OSD textures are not allocated (e.g. the window is minimized)
1026 if (!priv->d3d_device || !priv->d3d_texture_osd)
1027 return;
1028
1029 if (vo_osd_changed(0)) {
1030 D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order
1031 to copy MPlayer's frame inside it.*/
1032
1033 /* clear the OSD */
1034 if (FAILED(IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0,
1035 &locked_rect, NULL, 0))) {
1036 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture lock failed.\n");
1037 return;
1038 }
1039
1040 /* clear the whole texture to avoid issues due to interpolation */
1041 memset(locked_rect.pBits, 0, locked_rect.Pitch * priv->osd_texture_height);
1042
1043 /* this unlock is used for both slice_draw path and D3DRenderFrame path */
1044 if (FAILED(IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) {
1045 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture unlock failed.\n");
1046 return;
1047 }
1048
1049 priv->is_osd_populated = 0;
1050 /* required for if subs are in the boarder region */
1051 priv->is_clear_needed = 1;
1052
1053 vo_draw_text_ext(priv->osd_width, priv->osd_height, priv->border_x, priv->border_y,
1054 priv->border_x, priv->border_y, priv->src_width, priv->src_height, draw_alpha);
1055
1056 if (!priv->device_texture_sys)
1057 {
1058 /* only DMA to the shadow if its required */
1059 if (FAILED(IDirect3DDevice9_UpdateTexture(priv->d3d_device,
1060 (IDirect3DBaseTexture9 *)priv->d3d_texture_system,
1061 (IDirect3DBaseTexture9 *)priv->d3d_texture_osd))) {
1062 mp_msg(MSGT_VO,MSGL_ERR, "<vo_direct3d>OSD texture transfer failed.\n");
1063 return;
1064 }
1065 }
1066 }
1067
1068 /* update OSD */
1069
1070 if (priv->is_osd_populated) {
1071
1072 struct_vertex osd_quad_vb[] = {
1073 {-1.0f, 1.0f, 0.0f, 0, 0 },
1074 { 1.0f, 1.0f, 0.0f, 1, 0 },
1075 {-1.0f,-1.0f, 0.0f, 0, 1 },
1076 { 1.0f,-1.0f, 0.0f, 1, 1 }
1077 };
1078
1079 /* calculate the texture coordinates */
1080 osd_quad_vb[1].tu =
1081 osd_quad_vb[3].tu = (float)priv->osd_width / priv->osd_texture_width;
1082 osd_quad_vb[2].tv =
1083 osd_quad_vb[3].tv = (float)priv->osd_height / priv->osd_texture_height;
1084
1085 if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) {
1086 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>BeginScene failed.\n");
1087 return;
1088 }
1089
1090 /* turn on alpha test */
1091 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, TRUE);
1092 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, TRUE);
1093
1094 /* need to use a texture here (done here as we may be able to texture from system memory) */
1095 IDirect3DDevice9_SetTexture(priv->d3d_device, 0,
1096 (IDirect3DBaseTexture9 *)(priv->device_texture_sys
1097 ? priv->d3d_texture_system : priv->d3d_texture_osd));
1098
1099 IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_MY_VERTEX);
1100 IDirect3DDevice9_DrawPrimitiveUP(priv->d3d_device, D3DPT_TRIANGLESTRIP, 2, osd_quad_vb, sizeof(struct_vertex));
1101
1102 /* turn off alpha test */
1103 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, FALSE);
1104 IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, FALSE);
1105
1106 if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) {
1107 mp_msg(MSGT_VO,MSGL_ERR,"<vo_direct3d>EndScene failed.\n");
1108 return;
1109 }
1110 }
1111 }
1112