]> git.vomp.tv Git - vompclient.git/blob - osdwin.cc
Windows updates
[vompclient.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
32 //This is stuff for rendering the OSD
33
34
35 OsdWin::OsdWin()
36 {
37   d3d=NULL;
38   d3ddevice=NULL;
39   d3dvb=NULL;
40   d3drtsurf=NULL;
41   external_driving=false;
42   dsallocator=NULL;
43   filter_type=D3DTEXF_NONE;
44   lastrendertime=timeGetTime();
45   event = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
46   d3dmutex = CreateMutex(NULL,FALSE,NULL);
47   
48 }
49
50 OsdWin::~OsdWin()
51 {
52   if (initted) shutdown();
53   CloseHandle(event);
54   CloseHandle(d3dmutex);
55 }
56
57 int OsdWin::getFD()
58 {
59   if (!initted) return 0;
60   return fdOsd;
61 }
62
63 int OsdWin::init(void* device)
64 {
65   if (initted) return 0;
66    Video* video = Video::getInstance();
67   //First Create Direct 3D Object
68   d3d=Direct3DCreate9(D3D_SDK_VERSION);
69   if (!d3d) 
70   {
71     Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 object!");
72     return 0;
73   }
74   // then create the Device
75   D3DPRESENT_PARAMETERS d3dparas;
76   ZeroMemory(&d3dparas,sizeof(d3dparas));
77   d3dparas.BackBufferWidth=video->getScreenWidth();
78   d3dparas.BackBufferHeight=video->getScreenHeight();
79   d3dparas.Windowed=TRUE;
80   d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
81   if (d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,*((HWND*) device),
82           D3DCREATE_SOFTWARE_VERTEXPROCESSING |D3DCREATE_MULTITHREADED,&d3dparas,&d3ddevice)!=D3D_OK) {
83            Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 device!");
84        return 0;
85   }
86   d3ddevice->GetRenderTarget(0,&d3drtsurf);
87
88   if (!InitVertexBuffer()) {
89            Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 vertex buf!");
90                   return 0;
91   }
92   /* We have to determine which kind of filtering is supported*/
93   D3DCAPS9 caps;
94   d3ddevice->GetDeviceCaps(&caps);
95   filter_type=D3DTEXF_NONE;
96   if ( ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MINFPOINT)!=0)
97           && ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)!=0)) {
98                   filter_type=D3DTEXF_POINT;
99    }
100    if ( ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)!=0)
101           && ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)!=0)) {
102                   filter_type=D3DTEXF_LINEAR;
103    }
104
105   //Now we will create the Screen
106   screen = new SurfaceWin(Surface::SCREEN);
107   SetEvent(event);//Now all devices are ready
108   screen->create(video->getScreenWidth(), video->getScreenHeight());
109   screen->display();
110   initted = 1; // must set this here or create surface won't work
111   
112   return 1;
113 }
114
115
116 int OsdWin::InitVertexBuffer() {
117   Video* video = Video::getInstance();
118   FLOAT texx=((float)video->getScreenWidth())/1024.f;
119   FLOAT texy=((float)video->getScreenHeight())/1024.f;
120   D3DCOLOR osdcolor=D3DCOLOR_RGBA(255,255,255,255);
121   osdvertices[0].c=osdcolor;
122   osdvertices[0].x=0.f-0.5f;
123   osdvertices[0].y=0.f-0.5f;
124   osdvertices[0].z=0.5f;
125   osdvertices[0].rhw=1.f;
126   osdvertices[0].u=0.f;
127   osdvertices[0].v=0.f;
128   osdvertices[1].c=osdcolor;
129   osdvertices[1].x=((float)video->getScreenWidth())-0.5f;-0.5f;
130   osdvertices[1].y=0.f-0.5f;
131   osdvertices[1].z=0.5f;
132   osdvertices[1].u=texx;
133   osdvertices[1].v=0.f;
134   osdvertices[1].rhw=1.f;
135   osdvertices[2].c=osdcolor;
136   osdvertices[2].x=((float)video->getScreenWidth())-0.5f;
137   osdvertices[2].y=((float)video->getScreenHeight())-0.5f;
138   osdvertices[2].z=0.5f;
139   osdvertices[2].rhw=1.f;
140   osdvertices[2].u=texx;
141   osdvertices[2].v=texy;
142   osdvertices[3].c=osdcolor;
143   osdvertices[3].x=0.f-0.5f;
144   osdvertices[3].y=((float)video->getScreenHeight())-0.5f;
145   osdvertices[3].z=0;
146   osdvertices[3].rhw=1.f;
147   osdvertices[3].u=0.f;
148   osdvertices[3].v=texy;
149   if (d3dvb) {
150           d3dvb->Release();
151           d3dvb=NULL;
152   }
153   if (d3ddevice->CreateVertexBuffer(4*sizeof(OSDVERTEX),0,D3DFVF_OSDVERTEX,D3DPOOL_MANAGED,
154           &d3dvb,NULL)!=D3D_OK) {
155           return 0;
156   }
157   void *pvertex=NULL;
158   if (d3dvb->Lock(0,sizeof(osdvertices),&pvertex,0)!=D3D_OK) {
159           return 0;
160   }
161   memcpy(pvertex,osdvertices,sizeof(osdvertices));
162   d3dvb->Unlock();
163   return 1;
164 }
165
166 int OsdWin::shutdown()
167 {
168   if (!initted) return 0;
169   initted = 0;
170   d3drtsurf->Release();
171   d3ddevice->Release();
172   d3d->Release();
173
174   return 1;
175 }
176
177 void OsdWin::screenShot(char* fileName)
178 {
179   screen->screenShot(fileName);
180 }
181
182 // This function is called from the WinMain function in order to get Screen updates
183 void OsdWin::Render()
184 {
185         if (!initted) return ;
186         if (external_driving) {
187                 Sleep(5); //Sleep for 5 ms, in order to avoid blocking the other threads
188         } else {
189                 DWORD time1=timeGetTime();
190                 if ((time1-lastrendertime)>50) {//10 fps for OSD updates are enough, avoids tearing
191                         InternalRendering(NULL);
192                         lastrendertime=timeGetTime();
193                 } else {
194                         Sleep(5);
195                 
196                 }
197                 
198         }
199 }
200
201 void OsdWin::RenderDS(LPDIRECT3DSURFACE9 present){
202         if (!initted) return;
203         if (external_driving) {
204                 InternalRendering(present);
205         }
206 }
207
208
209 void OsdWin::InternalRendering(LPDIRECT3DSURFACE9 present){
210     WaitForSingleObject(d3dmutex,INFINITE);
211     HRESULT losty=d3ddevice->TestCooperativeLevel();
212     if (losty==D3DERR_DEVICELOST) {
213         Sleep(10);
214         return; //Device Lost
215     }
216     if (losty==D3DERR_DEVICENOTRESET){
217        DoLost();
218        return;
219     }
220         WaitForSingleObject(event,INFINITE);
221         
222    
223         BeginPainting();
224         if (external_driving) {
225                 //Copy video to Backbuffer
226                 if (present!=NULL) {
227                         VideoWin* video =(VideoWin*) Video::getInstance();
228                         /*calculating destination rect */
229                         RECT destrect={0,0,video->getScreenWidth(),video->getScreenHeight()};
230                         UCHAR mode=video->getMode();
231                         switch (mode) {
232                         case Video::EIGHTH:
233                         destrect.right=destrect.right/2;
234                         destrect.bottom=destrect.bottom/2;
235                         case Video::QUARTER:
236                         destrect.right=destrect.right/2+video->getPosx()*2;
237                         destrect.bottom=destrect.bottom/2+video->getPosy()*2;
238                         destrect.left=video->getPosx()*2;
239                         destrect.top=video->getPosy()*2;
240                         d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
241                         break;
242                         };
243                         D3DSURFACE_DESC surf_desc;
244                         present->GetDesc(&surf_desc);//for chop sides
245                         RECT sourcerect= {0,0,surf_desc.Width,surf_desc.Height};
246                         if (video->getPseudoTVsize()==Video::ASPECT4X3 
247                                 && video->getMode()==Video::NORMAL 
248                                 && video->getAspectRatio()==Video::ASPECT16X9) {
249                                         unsigned int correction=((double) (surf_desc.Width))*4.*9./3./16.;
250                                         sourcerect.left=(surf_desc.Width-correction)/2;
251                                         sourcerect.right=sourcerect.left+correction;
252                         }
253                         d3ddevice->StretchRect(present,&sourcerect,d3drtsurf  ,&destrect,filter_type);
254                 }
255         } else {
256                 VideoWin* video =(VideoWin*) Video::getInstance();
257                 //Clear Background
258                 if (!video->isVideoOn()) d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
259         }
260         //Drawing the OSD
261         if (d3ddevice->BeginScene()==D3D_OK) {
262                 d3ddevice->SetStreamSource(0,d3dvb,0,sizeof(OSDVERTEX));
263                 d3ddevice->SetFVF(D3DFVF_OSDVERTEX);
264                 d3ddevice->SetTexture(0,((SurfaceWin*)screen)->getD3dtexture());
265                 //d3ddevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
266                 d3ddevice->SetTextureStageState(0, D3DTSS_ALPHAOP,D3DTOP_MODULATE);
267                 d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
268                 d3ddevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
269                 d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
270                 d3ddevice->SetRenderState(D3DRS_LIGHTING,FALSE);
271
272
273                 d3ddevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
274                 d3ddevice->EndScene();
275                 //Show it to the user!
276                 if (d3ddevice->Present(NULL,NULL,NULL,NULL)==D3DERR_DEVICELOST){
277                         EndPainting();
278                         if (!external_driving) DoLost();
279                 }
280                 EndPainting();
281         }
282         ReleaseMutex(d3dmutex);
283         if (!external_driving) {
284                 Sleep(4);//The User can wait for 4 milliseconds to see his changes
285         }
286 }
287
288 bool OsdWin::DoLost(){
289         Log::getInstance()->log("OSD", Log::WARN, "Direct3D Device Lost! Reobtaining...");
290         ResetEvent(event);
291         if (external_driving && dsallocator!=NULL) {
292                 dsallocator->LostDevice(d3ddevice,d3d); //Propagate the information through DS
293         }
294         //First we free up all resources
295         Video* video = Video::getInstance();
296         ((SurfaceWin*)screen)->ReleaseSurface();
297         if (d3drtsurf) d3drtsurf->Release();
298     d3drtsurf=NULL;
299         if (d3dvb) d3dvb->Release();
300         d3dvb=NULL;
301         D3DPRESENT_PARAMETERS d3dparas;
302         ZeroMemory(&d3dparas,sizeof(d3dparas));
303         d3dparas.BackBufferWidth=video->getScreenWidth();
304         d3dparas.BackBufferHeight=video->getScreenHeight();
305         d3dparas.Windowed=TRUE;
306         d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
307         if (d3ddevice->Reset(&d3dparas)!=D3D_OK){
308                 return false;
309         }
310     d3ddevice->GetRenderTarget(0,&d3drtsurf);
311         InitVertexBuffer();
312     //Redraw Views, Chris could you add a member function to viewman, so that
313         // I can cause it to completely redraw the Views?
314         // Otherwise the OSD would be distorted after Device Lost
315         
316         SetEvent(event);
317
318
319         screen->create(video->getScreenWidth(), video->getScreenHeight());
320         screen->display();
321         
322         return true;
323
324 }
325 LPDIRECT3DDEVICE9 OsdWin::getD3dDev() {
326         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
327         return d3ddevice;
328 }
329
330 LPDIRECT3D9 OsdWin::getD3d() {
331         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
332         return d3d;
333 }
334
335 void OsdWin::BeginPainting() {//We synchronize calls to d3d between different threads
336         WaitForSingleObject(d3dmutex,INFINITE);
337 }
338
339 void OsdWin::EndPainting() {
340         ReleaseMutex(d3dmutex);
341 }
342
343 void OsdWin::setExternalDriving(DsAllocator* dsall) {
344         if (dsall==NULL) {
345                 external_driving=false;
346                 dsallocator=NULL;
347                 return;
348         }
349         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
350         dsallocator=dsall;
351         external_driving=true;
352 }
353
354 void OsdWin::Blank() {
355         WaitForSingleObject(event,INFINITE);
356         BeginPainting();
357         d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
358         EndPainting();
359 }
360