]> git.vomp.tv Git - vompclient-marten.git/blob - osdwin.cc
Fix symbol and font gradients
[vompclient-marten.git] / osdwin.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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.
19 */
20
21
22 #include "osdwin.h"
23 #include "mtd.h"
24 #include "videowin.h"
25 #include "surfacewin.h"
26
27 #include "dsallocator.h"
28 #include "message.h"
29 #include "command.h"
30
31 #define  BACKBUFFER_WIDTH 1920
32 #define  BACKBUFFER_HEIGHT 1080
33
34
35
36 typedef HRESULT (__stdcall *FCT_DXVA2CreateDirect3DDeviceManager9)(UINT* pResetToken, IDirect3DDeviceManager9** ppDeviceManager);
37 typedef HRESULT (__stdcall *FCT_MFCreateVideoSampleFromSurface)(IUnknown* pUnkSurface, IMFSample** ppSample);
38
39 FCT_DXVA2CreateDirect3DDeviceManager9 ptrDXVA2CreateDirect3DDeviceManager9=NULL;
40 FCT_MFCreateVideoSampleFromSurface ptrMFCreateVideoSampleFromSurface=NULL;
41
42 //This is stuff for rendering the OSD
43
44
45 OsdWin::OsdWin()
46 {
47   d3d=NULL;
48   d3ddevice=NULL;
49   d3drtsurf=NULL;
50   swappy=NULL;
51   swapsurf=NULL;
52   evrstate=EVR_pres_off;
53   window=NULL;
54
55   external_driving=false;
56   dsallocator=NULL;
57   filter_type=D3DTEXF_FORCE_DWORD;
58   lastrendertime=timeGetTime();
59   event = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
60   d3dmutex = CreateMutex(NULL,FALSE,NULL);
61   /*EVR stuff*/
62   dxvadevicehandle=NULL;
63   evrsupported=true;
64   HMODULE hlib=NULL;
65   hlib=LoadLibrary("dxva2.dll");
66   if (!hlib) {
67           evrsupported=false;
68           return;
69   }
70   ptrDXVA2CreateDirect3DDeviceManager9=(FCT_DXVA2CreateDirect3DDeviceManager9)GetProcAddress(hlib, "DXVA2CreateDirect3DDeviceManager9");
71   if (!ptrDXVA2CreateDirect3DDeviceManager9){
72           evrsupported=false;
73           return;
74   }
75
76   hlib=LoadLibrary("evr.dll");
77   if (!hlib) {
78           evrsupported=false;
79           return;
80   }
81  
82   ptrMFCreateVideoSampleFromSurface = (FCT_MFCreateVideoSampleFromSurface)GetProcAddress(hlib,"MFCreateVideoSampleFromSurface");
83   if (!ptrMFCreateVideoSampleFromSurface){
84           evrsupported=false;
85           return;
86   }
87   
88 }
89
90 OsdWin::~OsdWin()
91 {
92
93   if (initted) 
94   {
95           threadStop();
96                 shutdown();
97   }
98   CloseHandle(event);
99   CloseHandle(d3dmutex);
100 }
101
102 int OsdWin::getFD()
103 {
104   if (!initted) return 0;
105   return fdOsd;
106 }
107
108 Surface * OsdWin::createNewSurface(){
109         return (Surface*)new SurfaceWin();
110 }
111
112 int OsdWin::init(void* device)
113 {
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);
119   if (!d3d) 
120   {
121     Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 object!");
122     return 0;
123   }
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!");
134        return 0;
135   }
136   d3ddevice->GetRenderTarget(0,&d3drtsurf);
137
138   /*
139   if (!InitVertexBuffer()) {
140            Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 vertex buf!");
141                   return 0;
142   }*/
143   /* We have to determine which kind of filtering is supported*/
144   D3DCAPS9 caps;
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;
150           }
151   } else {
152       if (filter_type==D3DTEXF_LINEAR)
153       {
154           filter_type=D3DTEXF_POINT;
155       }
156   }
157   
158   if ( ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MINFPOINT)!=0)
159           && ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)!=0)) {
160        if (filter_type==D3DTEXF_FORCE_DWORD) 
161        {
162                   filter_type=D3DTEXF_POINT;
163    }
164    } else {
165        if (filter_type==D3DTEXF_POINT) {
166            filter_type=D3DTEXF_NONE;
167    }
168    }
169    if (filter_type==D3DTEXF_FORCE_DWORD) {
170        filter_type=D3DTEXF_NONE;
171    }
172
173    if (evrsupported)
174    {
175            if (ptrDXVA2CreateDirect3DDeviceManager9(&dxvatoken,&d3ddevman)!=S_OK) evrsupported=false;
176            else {
177                    d3ddevman->ResetDevice(d3ddevice,dxvatoken);
178            }
179    }
180
181
182
183
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());
188   screen->display();
189   initted = 1; // must set this here or create surface won't work
190   threadStart(); 
191
192   return 1;
193 }
194
195 void OsdWin::LockDevice()
196 {
197         if (!evrsupported) return;
198         if (!dxvadevicehandle) 
199         {
200                 d3ddevman->OpenDeviceHandle(&dxvadevicehandle);
201         }
202         IDirect3DDevice9 *temp;
203         d3ddevman->LockDevice(dxvadevicehandle,&temp,TRUE);
204
205 }
206
207 void OsdWin::UnlockDevice()
208 {
209         if (!evrsupported) return;
210         if (!initted) return;
211         d3ddevman->UnlockDevice(dxvadevicehandle,TRUE);
212         if (dxvadevicehandle) 
213         {
214                 d3ddevman->CloseDeviceHandle(dxvadevicehandle);
215                 dxvadevicehandle=NULL;
216         }
217
218 }
219
220 DWORD OsdWin::getFilterCaps()
221 {
222     if (!initted) return NULL;
223     D3DCAPS9 caps;
224     d3ddevice->GetDeviceCaps(&caps);
225     return caps.StretchRectFilterCaps;
226 }
227         
228 LPDIRECT3DVERTEXBUFFER9  OsdWin::InitVertexBuffer(DWORD width, DWORD height)
229 {
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;
263   
264   if (d3ddevice->CreateVertexBuffer(4*sizeof(OSDVERTEX),0,D3DFVF_OSDVERTEX,D3DPOOL_MANAGED,
265           &ret,NULL)!=D3D_OK) {
266           return NULL;
267   }
268   void *pvertex=NULL;
269   if (ret->Lock(0,sizeof(osdvertices),&pvertex,0)!=D3D_OK) {
270           return NULL;
271   }
272   memcpy(pvertex,osdvertices,sizeof(osdvertices));
273   ret->Unlock();
274   return ret;
275 }
276
277 int OsdWin::shutdown()
278 {
279   if (!initted) return 0;
280   initted = 0;
281   evrsupported=0;
282   if (d3ddevman) d3ddevman->Release();
283   d3drtsurf->Release();
284   d3ddevice->Release();
285   d3d->Release();
286   if (swapsurf) swapsurf->Release();
287   if (swappy) swappy->Release();
288
289   return 1;
290 }
291
292 void OsdWin::screenShot(const char* fileName)
293 {
294   screen->screenShot(fileName);
295 }
296
297 void OsdWin::threadMethod()
298 {
299         while (true)
300         {
301                 DWORD waittime=10;
302                 if (initted){
303                         if (evrstate==EVR_pres_off || evrstate==EVR_pres_pause) 
304                         {
305                                 Render();
306                         } else if (evrstate==EVR_pres_started)
307                         {
308                                 LPDIRECT3DSURFACE9 surf;
309                                 if (dsallocator) dsallocator->GetNextSurface(&surf,&waittime);
310                                 if (surf==NULL)
311                                 {
312                                         Render();
313                                 }
314                                 else
315                                 {
316                                         RenderDS(surf);
317                                         surf->Release();
318                                         if (dsallocator) dsallocator->DiscardSurfaceandgetWait(&waittime);
319                                 }
320                         }
321                 }
322                 threadCheckExit();
323                 if (waittime!=0) Sleep(min(10,waittime));
324                 //Sleep(1);
325         }
326 }
327
328
329 void OsdWin::threadPostStopCleanup()
330 {
331         //Doing nothing
332         //goo;
333 }
334
335
336 // This function is called from the WinMain function in order to get Screen updates
337 void OsdWin::Render()
338 {
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();
345         } else {
346                    //Sleep(5); //Sleep for 5 ms, in order to avoid blocking the other threads
347         }
348         } else {
349                 DWORD time1=timeGetTime();
350                 if ((time1-lastrendertime)>50) {//10 fps for OSD updates are enough, avoids tearing
351                         InternalRendering(NULL);
352                         lastrendertime=timeGetTime();
353                 } else {
354                         //Sleep(5);
355                 
356                 }
357                 
358         }
359 }
360
361 void OsdWin::RenderDS(LPDIRECT3DSURFACE9 present){
362         if (!initted) return; 
363         if (external_driving) {
364                 InternalRendering(present);
365         lastrendertime=timeGetTime();
366         }
367 }
368
369
370 void OsdWin::InternalRendering(LPDIRECT3DSURFACE9 present){
371     BeginPainting();
372     HRESULT losty=d3ddevice->TestCooperativeLevel();
373     if (losty==D3DERR_DEVICELOST) {
374         //Sleep(10);
375                 EndPainting();
376         return; //Device Lost
377     }
378     if (losty==D3DERR_DEVICENOTRESET){
379            EndPainting();
380        DoLost();
381        return;
382     }
383         WaitForSingleObject(event,INFINITE);
384         
385    
386         
387     
388     LPDIRECT3DSURFACE9 targetsurf;
389         if (swappy)
390         {
391                 targetsurf=swapsurf;
392                 d3ddevice->SetRenderTarget(0,swapsurf);//Stupid VMR manipulates the render target
393         } 
394         else
395         {
396                 targetsurf=d3drtsurf;
397                 d3ddevice->SetRenderTarget(0,d3drtsurf);//Stupid VMR manipulates the render target
398         }
399         D3DSURFACE_DESC targetdesc;
400         targetsurf->GetDesc(&targetdesc);
401
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();
410                         switch (mode) {
411                         case Video::EIGHTH:
412                         destrect.right=destrect.right/2;
413                         destrect.bottom=destrect.bottom/2;
414                         case Video::QUARTER:
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);
420                         break;
421                         };
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;
431                         }
432                         d3ddevice->StretchRect(present,&sourcerect,targetsurf ,&destrect,filter_type);
433
434                 }
435         } else {
436                 VideoWin* video =(VideoWin*) Video::getInstance();
437                 //Clear Background
438                 if (!video->isVideoOn()) d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
439         }
440         LPDIRECT3DVERTEXBUFFER9 vb=NULL;
441         vb=InitVertexBuffer(targetdesc.Width,targetdesc.Height);
442
443         //Drawing the OSD
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);
450
451         d3ddevice->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
452         d3ddevice->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
453
454
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);
459
460
461                 d3ddevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
462                 d3ddevice->EndScene();
463                 //Show it to the user!
464         HRESULT hres;
465                 if (swappy)
466                 {
467                         if (hres=swappy->Present(NULL,NULL,NULL,NULL,0)==D3DERR_DEVICELOST){
468                                 //EndPainting();
469                                 if (!external_driving) DoLost();
470                         }
471                 } 
472                 else
473                 {
474                         if (hres=d3ddevice->Present(NULL,NULL,NULL,NULL)==D3DERR_DEVICELOST){
475                                 //EndPainting();
476                                 if (!external_driving) DoLost();
477                         }
478                 }
479                 
480         }
481         
482         vb->Release();
483         EndPainting();
484
485         
486 //      if (!external_driving) {
487 //              Sleep(4);//The User can wait for 4 milliseconds to see his changes
488 //      }
489 }
490
491 bool OsdWin::DoLost(){
492         Log::getInstance()->log("OSD", Log::WARN, "Direct3D Device Lost! Reobtaining...");
493         ResetEvent(event);
494         if (external_driving && dsallocator!=NULL) {
495                 dsallocator->LostDevice(d3ddevice,d3d); //Propagate the information through DS
496         }
497         //First we free up all resources
498         Video* video = Video::getInstance();
499         ((SurfaceWin*)screen)->ReleaseSurface();
500         if (d3drtsurf) d3drtsurf->Release();
501     d3drtsurf=NULL;
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;
508
509         if (swapsurf) {swapsurf->Release();swapsurf=NULL;};
510         if (swappy) {swappy->Release();swappy=NULL;};
511
512         if (d3ddevice->Reset(&d3dparas)!=D3D_OK){
513                 return false;
514         }
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
521         // FIXME
522         
523         SetEvent(event);
524
525
526         screen->create(video->getScreenWidth(), video->getScreenHeight());
527         screen->display();
528         
529         return true;
530
531 }
532 LPDIRECT3DDEVICE9 OsdWin::getD3dDev() {
533         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
534         return d3ddevice;
535 }
536
537 LPDIRECT3D9 OsdWin::getD3d() {
538         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
539         return d3d;
540 }
541
542 void OsdWin::BeginPainting() {//We synchronize calls to d3d between different threads
543         WaitForSingleObject(d3dmutex,INFINITE);
544         LockDevice();
545 }
546
547 void OsdWin::EndPainting() {
548         UnlockDevice();
549         ReleaseMutex(d3dmutex);
550 }
551
552 void OsdWin::setExternalDriving(DsAllocator* dsall,DWORD width, DWORD height) {
553         
554         if (swappy)
555         {
556                 BeginPainting();
557                 d3ddevice->StretchRect(swapsurf,NULL,d3drtsurf,NULL,filter_type);
558                 LPDIRECT3DSWAPCHAIN9 temp=swappy;
559                 LPDIRECT3DSURFACE9 tempsurf=swapsurf;
560                 swappy=NULL;
561                 swapsurf=NULL;
562                 EndPainting();
563                 tempsurf->Release();
564                 temp->Release();
565         }
566
567         if (dsall==NULL) {
568                 external_driving=false;
569                 dsallocator=NULL;       
570                 return;
571         }
572         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
573         BeginPainting();
574
575         if (width>BACKBUFFER_WIDTH || height>BACKBUFFER_HEIGHT) 
576         {
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!");
585                         //return 0;
586                 } else {
587                         swappy->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&swapsurf);
588                 }
589         Log::getInstance()->log("OSD", Log::INFO, "Create Additional Swap Chain %d %d!",width,height);
590         }
591
592         dsallocator=dsall;
593         external_driving=true;
594         
595         EndPainting();
596 }
597
598 void OsdWin::Blank() {
599         WaitForSingleObject(event,INFINITE);
600         BeginPainting();
601         d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
602         EndPainting();
603 }