Index: libvo/vo_direct3d.c =================================================================== --- libvo/vo_direct3d.c (revision 28031) +++ libvo/vo_direct3d.c (working copy) @@ -71,8 +71,14 @@ IDirect3DSurface9 *d3d_surface; /**< Offscreen Direct3D Surface. MPlayer renders inside it. Uses colorspace priv->movie_src_fmt */ + IDirect3DTexture9 *d3d_texture_osd; /**< Direct3D Texture. Uses RGBA */ + IDirect3DTexture9 *d3d_texture_system; /**< Direct3D Texture. System memory + cant lock a normal texture. Uses RGBA */ IDirect3DSurface9 *d3d_backbuf; /**< Video card's back buffer (used to display next frame) */ + IDirect3DVertexBuffer9 *d3d_quad_vb; /**< Vertex buffer that contains the texture + coordinates for the quad. */ + int is_osd_updated; /* has an update been made on the OSD surface */ } *priv; typedef struct { @@ -93,7 +99,27 @@ }; #define DISPLAY_FORMAT_TABLE_ENTRIES \ - (sizeof(fmt_table) / sizeof(fmt_table[0])) + (sizeof(fmt_table) / sizeof(fmt_table[0])) + +#define D3DFVF_MY_VERTEX ( D3DFVF_XYZ | D3DFVF_TEX1 ) + +#define OSD_TEXTURE_SIZE 512 // as this is a texture, it can be a different size to the movie + +typedef struct vertex_s +{ + float x, y, z; // Position of vertex in 3D space + float tu, tv; // Texture coordinates +} vertex_t; + +vertex_t osd_quad_vb[] = +{ + {-1.0f,-1.0f, 0.0f, 0, 1 }, + {-1.0f, 1.0f, 0.0f, 0, 0 }, + { 1.0f, 1.0f, 0.0f, 1, 0 }, + { 1.0f,-1.0f, 0.0f, 1, 1 } +}; + +extern void vo_draw_text(int dxs, int dys, void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride)); /**************************************************************************** * * @@ -180,6 +206,25 @@ priv->d3d_surface = NULL; } + /* kill the VB and the texture */ + if (priv->d3d_texture_osd != NULL) + { + IDirect3DTexture9_Release (priv->d3d_texture_osd); + priv->d3d_texture_osd = NULL; + } + + if (priv->d3d_texture_system != NULL) + { + IDirect3DTexture9_Release (priv->d3d_texture_system); + priv->d3d_texture_system = NULL; + } + + if (priv->d3d_quad_vb != NULL) + { + IDirect3DVertexBuffer9_Release (priv->d3d_quad_vb); + priv->d3d_quad_vb = NULL; + } + if (priv->d3d_device != NULL) { IDirect3DDevice9_Release (priv->d3d_device); priv->d3d_device = NULL; @@ -201,6 +246,8 @@ { D3DPRESENT_PARAMETERS present_params; D3DDISPLAYMODE disp_mode; + void *vertices = NULL; + float aspect; mp_msg(MSGT_VO,MSGL_V,"reconfigure_d3d called \n"); @@ -258,6 +305,55 @@ return 0; } + /* create OSD */ + if (FAILED (IDirect3DDevice9_CreateTexture( + priv->d3d_device, OSD_TEXTURE_SIZE, OSD_TEXTURE_SIZE, 1, D3DUSAGE_DYNAMIC, + D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &priv->d3d_texture_system, NULL))) + { + mp_msg(MSGT_VO,MSGL_ERR, + "IDirect3DDevice9_CreateTexture Failed (d3d_texture_system).\n"); + return 0; + } + + if (FAILED (IDirect3DDevice9_CreateTexture( + priv->d3d_device, OSD_TEXTURE_SIZE, OSD_TEXTURE_SIZE, 1, D3DUSAGE_DYNAMIC, + D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &priv->d3d_texture_osd, NULL))) + { + mp_msg(MSGT_VO,MSGL_ERR, + "IDirect3DDevice9_CreateTexture Failed (d3d_texture_osd).\n"); + return 0; + } + + if (FAILED (IDirect3DDevice9_CreateVertexBuffer( priv->d3d_device, sizeof(osd_quad_vb), + 0, D3DFVF_MY_VERTEX, + D3DPOOL_DEFAULT, &priv->d3d_quad_vb, NULL ))) + { + mp_msg(MSGT_VO,MSGL_ERR, + "IDirect3DDevice9_CreateVertexBuffer Failed (d3d_quad_vb).\n"); + return 0; + } + + if (FAILED (IDirect3DVertexBuffer9_Lock(priv->d3d_quad_vb, 0, sizeof(osd_quad_vb), + &vertices, 0 ))) + { + mp_msg(MSGT_VO,MSGL_ERR, + "IDirect3DVertexBuffer9_Lock Failed.\n"); + return 0; + } + + memcpy( vertices, osd_quad_vb, sizeof(osd_quad_vb) ); + aspect = (float)priv->src_width / priv->src_height; + /* populate the Y with the correct coordinate */ + ((vertex_t *)vertices)[0].tv = + ((vertex_t *)vertices)[3].tv = ((1.0f / OSD_TEXTURE_SIZE) * (OSD_TEXTURE_SIZE / aspect)); + + if (FAILED (IDirect3DVertexBuffer9_Unlock(priv->d3d_quad_vb))) + { + mp_msg(MSGT_VO,MSGL_ERR, + "IDirect3DVertexBuffer9_Unlock Failed.\n"); + return 0; + } + if (FAILED(IDirect3DDevice9_GetBackBuffer(priv->d3d_device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &(priv->d3d_backbuf)))) { @@ -346,6 +442,31 @@ return VO_ERROR; } + /* update OSD */ + if (priv->is_osd_updated) + { + /* turn on alpha test */ + IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); + IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAREF, (DWORD)1); + IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, TRUE); + + /* need to use a texture here */ + IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_LIGHTING, FALSE); + + IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); + IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); + + IDirect3DDevice9_SetTexture(priv->d3d_device, 0, (IDirect3DBaseTexture9 *)priv->d3d_texture_osd); + IDirect3DDevice9_SetStreamSource(priv->d3d_device, 0, priv->d3d_quad_vb, 0, sizeof(vertex_t) ); + IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_MY_VERTEX ); + IDirect3DDevice9_DrawPrimitive(priv->d3d_device, D3DPT_TRIANGLEFAN, 0, 2 ); + + /* turn off alpha test */ + IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, FALSE); + + priv->is_osd_updated = 0; + } + if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) { mp_msg(MSGT_VO,MSGL_ERR,"EndScene failed\n"); return VO_ERROR; @@ -382,7 +503,7 @@ mp_msg(MSGT_VO,MSGL_V,"Accepted image format: %s\n", vo_format_name(fmt_table[i].mplayer_fmt)); return (VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW - /*| VFCAP_OSD*/ | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN); + | VFCAP_OSD | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN); } } @@ -566,13 +687,6 @@ } } -/** @brief libvo Callback: Draw OSD/Subtitles, - * @return N/A - */ -static void draw_osd(void) -{ -} - /** @brief libvo Callback: Uninitializes all pointers and closes * all D3D related stuff, * @return N/A @@ -666,3 +780,51 @@ mp_msg(MSGT_VO,MSGL_V,"draw_frame called\n"); return VO_FALSE; } + +static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src, + unsigned char *srca, int stride) +{ + D3DLOCKED_RECT locked_rect; /**< Offscreen surface we lock in order + to copy MPlayer's frame inside it.*/ + + if (FAILED (IDirect3DTexture9_LockRect(priv->d3d_texture_system, 0, + &locked_rect, NULL, 0))) + { + mp_msg(MSGT_VO,MSGL_ERR,"IDirect3DTexture9_LockRect failure.\n"); + return; + } + + /* if mplayer has an asm clear then use this here instead, I couldn't see one */ + memset(locked_rect.pBits, 0, locked_rect.Pitch*OSD_TEXTURE_SIZE); + + vo_draw_alpha_rgb32(w,h,src,srca,stride, + (void *)((unsigned int)locked_rect.pBits+locked_rect.Pitch*y0+4*x0), locked_rect.Pitch); + + /* this unlock is used for both slice_draw path and D3DRenderFrame path */ + if (FAILED (IDirect3DTexture9_UnlockRect(priv->d3d_texture_system, 0))) + { + mp_msg(MSGT_VO,MSGL_ERR,"IDirect3DTexture9_UnlockRect failure.\n"); + return; + } + + if (FAILED (IDirect3DDevice9_UpdateTexture(priv->d3d_device, (IDirect3DBaseTexture9 *)priv->d3d_texture_system, + (IDirect3DBaseTexture9 *)priv->d3d_texture_osd))) + { + mp_msg(MSGT_VO,MSGL_ERR,"IDirect3DDevice9_UpdateTexture failure.\n"); + return; + } + + priv->is_osd_updated = 1; +} + +/** @brief libvo Callback: Draw OSD/Subtitles, + * @return N/A + */ +static void draw_osd(void) +{ + // calculate a width and height based on the OSD_TEXTURE_SIZE + float aspect = (float)priv->src_width / priv->src_height; + unsigned int calc_height = ((unsigned int)(OSD_TEXTURE_SIZE / aspect)); + + vo_draw_text(OSD_TEXTURE_SIZE, calc_height, draw_alpha); +}