2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "surfacewin.h"
27 #include "dsallocator.h"
31 #define BACKBUFFER_WIDTH 1920
32 #define BACKBUFFER_HEIGHT 1080
36 typedef HRESULT (__stdcall *FCT_DXVA2CreateDirect3DDeviceManager9)(UINT* pResetToken, IDirect3DDeviceManager9** ppDeviceManager);
37 typedef HRESULT (__stdcall *FCT_MFCreateVideoSampleFromSurface)(IUnknown* pUnkSurface, IMFSample** ppSample);
39 FCT_DXVA2CreateDirect3DDeviceManager9 ptrDXVA2CreateDirect3DDeviceManager9=NULL;
40 FCT_MFCreateVideoSampleFromSurface ptrMFCreateVideoSampleFromSurface=NULL;
42 //This is stuff for rendering the OSD
52 evrstate=EVR_pres_off;
55 external_driving=false;
57 filter_type=D3DTEXF_FORCE_DWORD;
58 lastrendertime=timeGetTime();
59 event = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
60 d3dmutex = CreateMutex(NULL,FALSE,NULL);
62 dxvadevicehandle=NULL;
65 hlib=LoadLibrary("dxva2.dll");
70 ptrDXVA2CreateDirect3DDeviceManager9=(FCT_DXVA2CreateDirect3DDeviceManager9)GetProcAddress(hlib, "DXVA2CreateDirect3DDeviceManager9");
71 if (!ptrDXVA2CreateDirect3DDeviceManager9){
76 hlib=LoadLibrary("evr.dll");
82 ptrMFCreateVideoSampleFromSurface = (FCT_MFCreateVideoSampleFromSurface)GetProcAddress(hlib,"MFCreateVideoSampleFromSurface");
83 if (!ptrMFCreateVideoSampleFromSurface){
99 CloseHandle(d3dmutex);
104 if (!initted) return 0;
108 Surface * OsdWin::createNewSurface(){
109 return (Surface*)new SurfaceWin();
112 int OsdWin::init(void* device)
114 if (initted) return 0;
115 Video* video = Video::getInstance();
116 window=*((HWND*)device);
117 //First Create Direct 3D Object
118 d3d=Direct3DCreate9(D3D_SDK_VERSION);
121 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 object!");
124 // then create the Device
125 D3DPRESENT_PARAMETERS d3dparas;
126 ZeroMemory(&d3dparas,sizeof(d3dparas));
127 d3dparas.BackBufferWidth=BACKBUFFER_WIDTH;
128 d3dparas.BackBufferHeight=BACKBUFFER_HEIGHT;
129 d3dparas.Windowed=TRUE;
130 d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
131 if (d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,*((HWND*) device),
132 D3DCREATE_SOFTWARE_VERTEXPROCESSING |D3DCREATE_MULTITHREADED,&d3dparas,&d3ddevice)!=D3D_OK) {
133 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 device!");
136 d3ddevice->GetRenderTarget(0,&d3drtsurf);
139 if (!InitVertexBuffer()) {
140 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 vertex buf!");
143 /* We have to determine which kind of filtering is supported*/
145 d3ddevice->GetDeviceCaps(&caps);
146 if ( ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)!=0)
147 && ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)!=0)) {
148 if (filter_type==D3DTEXF_FORCE_DWORD) {
149 filter_type=D3DTEXF_LINEAR;
152 if (filter_type==D3DTEXF_LINEAR)
154 filter_type=D3DTEXF_POINT;
158 if ( ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MINFPOINT)!=0)
159 && ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)!=0)) {
160 if (filter_type==D3DTEXF_FORCE_DWORD)
162 filter_type=D3DTEXF_POINT;
165 if (filter_type==D3DTEXF_POINT) {
166 filter_type=D3DTEXF_NONE;
169 if (filter_type==D3DTEXF_FORCE_DWORD) {
170 filter_type=D3DTEXF_NONE;
175 if (ptrDXVA2CreateDirect3DDeviceManager9(&dxvatoken,&d3ddevman)!=S_OK) evrsupported=false;
177 d3ddevman->ResetDevice(d3ddevice,dxvatoken);
184 //Now we will create the Screen
185 screen = new SurfaceWin(Surface::SCREEN);
186 SetEvent(event);//Now all devices are ready
187 screen->create(video->getScreenWidth(), video->getScreenHeight());
189 initted = 1; // must set this here or create surface won't work
195 void OsdWin::LockDevice()
197 if (!evrsupported) return;
198 if (!dxvadevicehandle)
200 d3ddevman->OpenDeviceHandle(&dxvadevicehandle);
202 IDirect3DDevice9 *temp;
203 d3ddevman->LockDevice(dxvadevicehandle,&temp,TRUE);
207 void OsdWin::UnlockDevice()
209 if (!evrsupported) return;
210 if (!initted) return;
211 d3ddevman->UnlockDevice(dxvadevicehandle,TRUE);
212 if (dxvadevicehandle)
214 d3ddevman->CloseDeviceHandle(dxvadevicehandle);
215 dxvadevicehandle=NULL;
220 DWORD OsdWin::getFilterCaps()
222 if (!initted) return NULL;
224 d3ddevice->GetDeviceCaps(&caps);
225 return caps.StretchRectFilterCaps;
228 LPDIRECT3DVERTEXBUFFER9 OsdWin::InitVertexBuffer(DWORD width, DWORD height)
230 LPDIRECT3DVERTEXBUFFER9 ret =NULL;
231 Video* video=Video::getInstance();
232 FLOAT texx=((float)video->getScreenWidth())/1024.f;
233 FLOAT texy=((float)video->getScreenHeight())/1024.f;
234 D3DCOLOR osdcolor=D3DCOLOR_RGBA(255,255,255,255);
235 osdvertices[0].c=osdcolor;
236 osdvertices[0].x=0.f-0.5f;
237 osdvertices[0].y=0.f-0.5f;
238 osdvertices[0].z=0.5f;
239 osdvertices[0].rhw=1.f;
240 osdvertices[0].u=0.f;
241 osdvertices[0].v=0.f;
242 osdvertices[1].c=osdcolor;
243 osdvertices[1].x=((float)width)-0.5f;
244 osdvertices[1].y=0.f-0.5f;
245 osdvertices[1].z=0.5f;
246 osdvertices[1].u=texx;
247 osdvertices[1].v=0.f;
248 osdvertices[1].rhw=1.f;
249 osdvertices[2].c=osdcolor;
250 osdvertices[2].x=((float)width)-0.5f;
251 osdvertices[2].y=((float)height)-0.5f;
252 osdvertices[2].z=0.5f;
253 osdvertices[2].rhw=1.f;
254 osdvertices[2].u=texx;
255 osdvertices[2].v=texy;
256 osdvertices[3].c=osdcolor;
257 osdvertices[3].x=0.f-0.5f;
258 osdvertices[3].y=((float)height)-0.5f;
259 osdvertices[3].z=0.5f;
260 osdvertices[3].rhw=1.f;
261 osdvertices[3].u=0.f;
262 osdvertices[3].v=texy;
264 if (d3ddevice->CreateVertexBuffer(4*sizeof(OSDVERTEX),0,D3DFVF_OSDVERTEX,D3DPOOL_MANAGED,
265 &ret,NULL)!=D3D_OK) {
269 if (ret->Lock(0,sizeof(osdvertices),&pvertex,0)!=D3D_OK) {
272 memcpy(pvertex,osdvertices,sizeof(osdvertices));
277 int OsdWin::shutdown()
279 if (!initted) return 0;
282 if (d3ddevman) d3ddevman->Release();
283 d3drtsurf->Release();
284 d3ddevice->Release();
286 if (swapsurf) swapsurf->Release();
287 if (swappy) swappy->Release();
292 void OsdWin::screenShot(const char* fileName)
294 screen->screenShot(fileName);
297 void OsdWin::threadMethod()
303 if (evrstate==EVR_pres_off || evrstate==EVR_pres_pause)
306 } else if (evrstate==EVR_pres_started)
308 LPDIRECT3DSURFACE9 surf;
309 if (dsallocator) dsallocator->GetNextSurface(&surf,&waittime);
318 if (dsallocator) dsallocator->DiscardSurfaceandgetWait(&waittime);
323 if (waittime!=0) Sleep(min(10,waittime));
329 void OsdWin::threadPostStopCleanup()
336 // This function is called from the WinMain function in order to get Screen updates
337 void OsdWin::Render()
339 if (!initted) return ;
340 if (external_driving) {
341 DWORD time1=timeGetTime(); //Updates the Menue
342 if ((time1-lastrendertime)>200) {//5 fps for OSD updates are enough, avoids tearing
343 InternalRendering(NULL);
344 lastrendertime=timeGetTime();
346 //Sleep(5); //Sleep for 5 ms, in order to avoid blocking the other threads
349 DWORD time1=timeGetTime();
350 if ((time1-lastrendertime)>50) {//10 fps for OSD updates are enough, avoids tearing
351 InternalRendering(NULL);
352 lastrendertime=timeGetTime();
361 void OsdWin::RenderDS(LPDIRECT3DSURFACE9 present){
362 if (!initted) return;
363 if (external_driving) {
364 InternalRendering(present);
365 lastrendertime=timeGetTime();
370 void OsdWin::InternalRendering(LPDIRECT3DSURFACE9 present){
372 HRESULT losty=d3ddevice->TestCooperativeLevel();
373 if (losty==D3DERR_DEVICELOST) {
376 return; //Device Lost
378 if (losty==D3DERR_DEVICENOTRESET){
383 WaitForSingleObject(event,INFINITE);
388 LPDIRECT3DSURFACE9 targetsurf;
392 d3ddevice->SetRenderTarget(0,swapsurf);//Stupid VMR manipulates the render target
396 targetsurf=d3drtsurf;
397 d3ddevice->SetRenderTarget(0,d3drtsurf);//Stupid VMR manipulates the render target
399 D3DSURFACE_DESC targetdesc;
400 targetsurf->GetDesc(&targetdesc);
402 if (external_driving) {
403 //Copy video to Backbuffer
404 if (present!=NULL ) {
405 VideoWin* video =(VideoWin*) Video::getInstance();
406 /*calculating destination rect */
407 RECT destrect={0,0,/*video->getScreenWidth()*/ targetdesc.Width,
408 /*video->getScreenHeight()*/targetdesc.Height};
409 UCHAR mode=video->getMode();
412 destrect.right=destrect.right/2;
413 destrect.bottom=destrect.bottom/2;
415 destrect.right=destrect.right/2+video->getPosx()*2;
416 destrect.bottom=destrect.bottom/2+video->getPosy()*2;
417 destrect.left=video->getPosx()*2;
418 destrect.top=video->getPosy()*2;
419 d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
422 D3DSURFACE_DESC surf_desc;
423 present->GetDesc(&surf_desc);//for chop sides
424 RECT sourcerect= {0,0,surf_desc.Width,surf_desc.Height};
425 if (video->getPseudoTVsize()==Video::ASPECT4X3
426 && video->getMode()==Video::NORMAL
427 && video->getAspectRatio()==Video::ASPECT16X9) {
428 unsigned int correction=((double) (surf_desc.Width))*4.*9./3./16.;
429 sourcerect.left=(surf_desc.Width-correction)/2;
430 sourcerect.right=sourcerect.left+correction;
432 d3ddevice->StretchRect(present,&sourcerect,targetsurf ,&destrect,filter_type);
436 VideoWin* video =(VideoWin*) Video::getInstance();
438 if (!video->isVideoOn()) d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
440 LPDIRECT3DVERTEXBUFFER9 vb=NULL;
441 vb=InitVertexBuffer(targetdesc.Width,targetdesc.Height);
444 if (d3ddevice->BeginScene()==D3D_OK) {
445 d3ddevice->SetStreamSource(0,vb,0,sizeof(OSDVERTEX));
446 d3ddevice->SetFVF(D3DFVF_OSDVERTEX);
447 d3ddevice->SetTexture(0,((SurfaceWin*)screen)->getD3dtexture());
448 //d3ddevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
449 d3ddevice->SetTextureStageState(0, D3DTSS_ALPHAOP,D3DTOP_MODULATE);
451 d3ddevice->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
452 d3ddevice->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
455 d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
456 d3ddevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
457 d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
458 d3ddevice->SetRenderState(D3DRS_LIGHTING,FALSE);
461 d3ddevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
462 d3ddevice->EndScene();
463 //Show it to the user!
467 if (hres=swappy->Present(NULL,NULL,NULL,NULL,0)==D3DERR_DEVICELOST){
469 if (!external_driving) DoLost();
474 if (hres=d3ddevice->Present(NULL,NULL,NULL,NULL)==D3DERR_DEVICELOST){
476 if (!external_driving) DoLost();
486 // if (!external_driving) {
487 // Sleep(4);//The User can wait for 4 milliseconds to see his changes
491 bool OsdWin::DoLost(){
492 Log::getInstance()->log("OSD", Log::WARN, "Direct3D Device Lost! Reobtaining...");
494 if (external_driving && dsallocator!=NULL) {
495 dsallocator->LostDevice(d3ddevice,d3d); //Propagate the information through DS
497 //First we free up all resources
498 Video* video = Video::getInstance();
499 ((SurfaceWin*)screen)->ReleaseSurface();
500 if (d3drtsurf) d3drtsurf->Release();
502 D3DPRESENT_PARAMETERS d3dparas;
503 ZeroMemory(&d3dparas,sizeof(d3dparas));
504 d3dparas.BackBufferWidth=BACKBUFFER_WIDTH;
505 d3dparas.BackBufferHeight=BACKBUFFER_HEIGHT;
506 d3dparas.Windowed=TRUE;
507 d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
509 if (swapsurf) {swapsurf->Release();swapsurf=NULL;};
510 if (swappy) {swappy->Release();swappy=NULL;};
512 if (d3ddevice->Reset(&d3dparas)!=D3D_OK){
515 d3ddevice->GetRenderTarget(0,&d3drtsurf);
516 if (d3ddevman) d3ddevman->ResetDevice(d3ddevice,dxvatoken);
517 //InitVertexBuffer();
518 //Redraw Views, Chris could you add a member function to BoxStack, so that
519 // I can cause it to completely redraw the Views?
520 // Otherwise the OSD would be distorted after Device Lost
526 screen->create(video->getScreenWidth(), video->getScreenHeight());
532 LPDIRECT3DDEVICE9 OsdWin::getD3dDev() {
533 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
537 LPDIRECT3D9 OsdWin::getD3d() {
538 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
542 void OsdWin::BeginPainting() {//We synchronize calls to d3d between different threads
543 WaitForSingleObject(d3dmutex,INFINITE);
547 void OsdWin::EndPainting() {
549 ReleaseMutex(d3dmutex);
552 void OsdWin::setExternalDriving(DsAllocator* dsall,DWORD width, DWORD height) {
557 d3ddevice->StretchRect(swapsurf,NULL,d3drtsurf,NULL,filter_type);
558 LPDIRECT3DSWAPCHAIN9 temp=swappy;
559 LPDIRECT3DSURFACE9 tempsurf=swapsurf;
568 external_driving=false;
572 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
575 if (width>BACKBUFFER_WIDTH || height>BACKBUFFER_HEIGHT)
577 D3DPRESENT_PARAMETERS d3dparas;
578 ZeroMemory(&d3dparas,sizeof(d3dparas));
579 d3dparas.BackBufferWidth=width;
580 d3dparas.BackBufferHeight=height;
581 d3dparas.Windowed=TRUE;
582 d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
583 if (d3ddevice->CreateAdditionalSwapChain(&d3dparas,&swappy)!=D3D_OK){
584 Log::getInstance()->log("OSD", Log::WARN, "Could not create Swap Chain!");
587 swappy->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&swapsurf);
589 Log::getInstance()->log("OSD", Log::INFO, "Create Additional Swap Chain %d %d!",width,height);
593 external_driving=true;
598 void OsdWin::Blank() {
599 WaitForSingleObject(event,INFINITE);
601 d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);