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