]> git.vomp.tv Git - vompclient.git/blob - osdwin.cc
Vista compatibility
[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., 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
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         DWORD time1=timeGetTime(); //Updates the Menue
188                 if ((time1-lastrendertime)>200) {//5 fps for OSD updates are enough, avoids tearing
189                         InternalRendering(NULL);
190                         lastrendertime=timeGetTime();
191         } else {
192                     Sleep(5); //Sleep for 5 ms, in order to avoid blocking the other threads
193         }
194         } else {
195                 DWORD time1=timeGetTime();
196                 if ((time1-lastrendertime)>50) {//10 fps for OSD updates are enough, avoids tearing
197                         InternalRendering(NULL);
198                         lastrendertime=timeGetTime();
199                 } else {
200                         Sleep(5);
201                 
202                 }
203                 
204         }
205 }
206
207 void OsdWin::RenderDS(LPDIRECT3DSURFACE9 present){
208         if (!initted) return;
209         if (external_driving) {
210                 InternalRendering(present);
211         lastrendertime=timeGetTime();
212         }
213 }
214
215
216 void OsdWin::InternalRendering(LPDIRECT3DSURFACE9 present){
217     WaitForSingleObject(d3dmutex,INFINITE);
218     HRESULT losty=d3ddevice->TestCooperativeLevel();
219     if (losty==D3DERR_DEVICELOST) {
220         Sleep(10);
221         return; //Device Lost
222     }
223     if (losty==D3DERR_DEVICENOTRESET){
224        DoLost();
225        return;
226     }
227         WaitForSingleObject(event,INFINITE);
228         
229    
230         BeginPainting();
231         if (external_driving) {
232                 //Copy video to Backbuffer
233                 if (present!=NULL) {
234                         VideoWin* video =(VideoWin*) Video::getInstance();
235                         /*calculating destination rect */
236                         RECT destrect={0,0,video->getScreenWidth(),video->getScreenHeight()};
237                         UCHAR mode=video->getMode();
238                         switch (mode) {
239                         case Video::EIGHTH:
240                         destrect.right=destrect.right/2;
241                         destrect.bottom=destrect.bottom/2;
242                         case Video::QUARTER:
243                         destrect.right=destrect.right/2+video->getPosx()*2;
244                         destrect.bottom=destrect.bottom/2+video->getPosy()*2;
245                         destrect.left=video->getPosx()*2;
246                         destrect.top=video->getPosy()*2;
247                         d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
248                         break;
249                         };
250                         D3DSURFACE_DESC surf_desc;
251                         present->GetDesc(&surf_desc);//for chop sides
252                         RECT sourcerect= {0,0,surf_desc.Width,surf_desc.Height};
253                         if (video->getPseudoTVsize()==Video::ASPECT4X3 
254                                 && video->getMode()==Video::NORMAL 
255                                 && video->getAspectRatio()==Video::ASPECT16X9) {
256                                         unsigned int correction=((double) (surf_desc.Width))*4.*9./3./16.;
257                                         sourcerect.left=(surf_desc.Width-correction)/2;
258                                         sourcerect.right=sourcerect.left+correction;
259                         }
260                         d3ddevice->StretchRect(present,&sourcerect,d3drtsurf  ,&destrect,filter_type);
261                 }
262         } else {
263                 VideoWin* video =(VideoWin*) Video::getInstance();
264                 //Clear Background
265                 if (!video->isVideoOn()) d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
266         }
267         //Drawing the OSD
268         if (d3ddevice->BeginScene()==D3D_OK) {
269                 d3ddevice->SetStreamSource(0,d3dvb,0,sizeof(OSDVERTEX));
270                 d3ddevice->SetFVF(D3DFVF_OSDVERTEX);
271                 d3ddevice->SetTexture(0,((SurfaceWin*)screen)->getD3dtexture());
272                 //d3ddevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
273                 d3ddevice->SetTextureStageState(0, D3DTSS_ALPHAOP,D3DTOP_MODULATE);
274                 d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
275                 d3ddevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
276                 d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
277                 d3ddevice->SetRenderState(D3DRS_LIGHTING,FALSE);
278
279
280                 d3ddevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
281                 d3ddevice->EndScene();
282                 //Show it to the user!
283                 if (d3ddevice->Present(NULL,NULL,NULL,NULL)==D3DERR_DEVICELOST){
284                         EndPainting();
285                         if (!external_driving) DoLost();
286                 }
287                 EndPainting();
288         }
289         ReleaseMutex(d3dmutex);
290         if (!external_driving) {
291                 Sleep(4);//The User can wait for 4 milliseconds to see his changes
292         }
293 }
294
295 bool OsdWin::DoLost(){
296         Log::getInstance()->log("OSD", Log::WARN, "Direct3D Device Lost! Reobtaining...");
297         ResetEvent(event);
298         if (external_driving && dsallocator!=NULL) {
299                 dsallocator->LostDevice(d3ddevice,d3d); //Propagate the information through DS
300         }
301         //First we free up all resources
302         Video* video = Video::getInstance();
303         ((SurfaceWin*)screen)->ReleaseSurface();
304         if (d3drtsurf) d3drtsurf->Release();
305     d3drtsurf=NULL;
306         if (d3dvb) d3dvb->Release();
307         d3dvb=NULL;
308         D3DPRESENT_PARAMETERS d3dparas;
309         ZeroMemory(&d3dparas,sizeof(d3dparas));
310         d3dparas.BackBufferWidth=video->getScreenWidth();
311         d3dparas.BackBufferHeight=video->getScreenHeight();
312         d3dparas.Windowed=TRUE;
313         d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
314         if (d3ddevice->Reset(&d3dparas)!=D3D_OK){
315                 return false;
316         }
317     d3ddevice->GetRenderTarget(0,&d3drtsurf);
318         InitVertexBuffer();
319     //Redraw Views, Chris could you add a member function to BoxStack, so that
320         // I can cause it to completely redraw the Views?
321         // Otherwise the OSD would be distorted after Device Lost
322         // FIXME
323         
324         SetEvent(event);
325
326
327         screen->create(video->getScreenWidth(), video->getScreenHeight());
328         screen->display();
329         
330         return true;
331
332 }
333 LPDIRECT3DDEVICE9 OsdWin::getD3dDev() {
334         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
335         return d3ddevice;
336 }
337
338 LPDIRECT3D9 OsdWin::getD3d() {
339         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
340         return d3d;
341 }
342
343 void OsdWin::BeginPainting() {//We synchronize calls to d3d between different threads
344         WaitForSingleObject(d3dmutex,INFINITE);
345 }
346
347 void OsdWin::EndPainting() {
348         ReleaseMutex(d3dmutex);
349 }
350
351 void OsdWin::setExternalDriving(DsAllocator* dsall) {
352         if (dsall==NULL) {
353                 external_driving=false;
354                 dsallocator=NULL;
355                 return;
356         }
357         WaitForSingleObject(event,INFINITE);//We will only return if we are initted
358         dsallocator=dsall;
359         external_driving=true;
360 }
361
362 void OsdWin::Blank() {
363         WaitForSingleObject(event,INFINITE);
364         BeginPainting();
365         d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
366         EndPainting();
367 }