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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "surfacewin.h"
27 #include "dsallocator.h"
32 //This is stuff for rendering the OSD
41 external_driving=false;
43 filter_type=D3DTEXF_NONE;
44 lastrendertime=timeGetTime();
45 event = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
46 d3dmutex = CreateMutex(NULL,FALSE,NULL);
52 if (initted) shutdown();
54 CloseHandle(d3dmutex);
59 if (!initted) return 0;
63 int OsdWin::init(void* device)
65 if (initted) return 0;
66 Video* video = Video::getInstance();
67 //First Create Direct 3D Object
68 d3d=Direct3DCreate9(D3D_SDK_VERSION);
71 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 object!");
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!");
86 d3ddevice->GetRenderTarget(0,&d3drtsurf);
88 if (!InitVertexBuffer()) {
89 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 vertex buf!");
92 /* We have to determine which kind of filtering is supported*/
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;
100 if ( ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)!=0)
101 && ((caps.StretchRectFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)!=0)) {
102 filter_type=D3DTEXF_LINEAR;
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());
110 initted = 1; // must set this here or create surface won't work
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;
146 osdvertices[3].rhw=1.f;
147 osdvertices[3].u=0.f;
148 osdvertices[3].v=texy;
153 if (d3ddevice->CreateVertexBuffer(4*sizeof(OSDVERTEX),0,D3DFVF_OSDVERTEX,D3DPOOL_MANAGED,
154 &d3dvb,NULL)!=D3D_OK) {
158 if (d3dvb->Lock(0,sizeof(osdvertices),&pvertex,0)!=D3D_OK) {
161 memcpy(pvertex,osdvertices,sizeof(osdvertices));
166 int OsdWin::shutdown()
168 if (!initted) return 0;
170 d3drtsurf->Release();
171 d3ddevice->Release();
177 void OsdWin::screenShot(char* fileName)
179 screen->screenShot(fileName);
182 // This function is called from the WinMain function in order to get Screen updates
183 void OsdWin::Render()
185 if (!initted) return ;
186 if (external_driving) {
187 Sleep(5); //Sleep for 5 ms, in order to avoid blocking the other threads
189 DWORD time1=timeGetTime();
190 if ((time1-lastrendertime)>50) {//10 fps for OSD updates are enough, avoids tearing
191 InternalRendering(NULL);
192 lastrendertime=timeGetTime();
201 void OsdWin::RenderDS(LPDIRECT3DSURFACE9 present){
202 if (!initted) return;
203 if (external_driving) {
204 InternalRendering(present);
209 void OsdWin::InternalRendering(LPDIRECT3DSURFACE9 present){
210 WaitForSingleObject(d3dmutex,INFINITE);
211 HRESULT losty=d3ddevice->TestCooperativeLevel();
212 if (losty==D3DERR_DEVICELOST) {
214 return; //Device Lost
216 if (losty==D3DERR_DEVICENOTRESET){
220 WaitForSingleObject(event,INFINITE);
224 if (external_driving) {
225 //Copy video to Backbuffer
227 VideoWin* video =(VideoWin*) Video::getInstance();
228 /*calculating destination rect */
229 RECT destrect={0,0,video->getScreenWidth(),video->getScreenHeight()};
230 UCHAR mode=video->getMode();
233 destrect.right=destrect.right/2;
234 destrect.bottom=destrect.bottom/2;
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);
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;
253 d3ddevice->StretchRect(present,&sourcerect,d3drtsurf ,&destrect,filter_type);
256 VideoWin* video =(VideoWin*) Video::getInstance();
258 if (!video->isVideoOn()) d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
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);
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){
278 if (!external_driving) DoLost();
282 ReleaseMutex(d3dmutex);
283 if (!external_driving) {
284 Sleep(4);//The User can wait for 4 milliseconds to see his changes
288 bool OsdWin::DoLost(){
289 Log::getInstance()->log("OSD", Log::WARN, "Direct3D Device Lost! Reobtaining...");
291 if (external_driving && dsallocator!=NULL) {
292 dsallocator->LostDevice(d3ddevice,d3d); //Propagate the information through DS
294 //First we free up all resources
295 Video* video = Video::getInstance();
296 ((SurfaceWin*)screen)->ReleaseSurface();
297 if (d3drtsurf) d3drtsurf->Release();
299 if (d3dvb) d3dvb->Release();
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){
310 d3ddevice->GetRenderTarget(0,&d3drtsurf);
312 //Redraw Views, Chris could you add a member function to BoxStack, so that
313 // I can cause it to completely redraw the Views?
314 // Otherwise the OSD would be distorted after Device Lost
320 screen->create(video->getScreenWidth(), video->getScreenHeight());
326 LPDIRECT3DDEVICE9 OsdWin::getD3dDev() {
327 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
331 LPDIRECT3D9 OsdWin::getD3d() {
332 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
336 void OsdWin::BeginPainting() {//We synchronize calls to d3d between different threads
337 WaitForSingleObject(d3dmutex,INFINITE);
340 void OsdWin::EndPainting() {
341 ReleaseMutex(d3dmutex);
344 void OsdWin::setExternalDriving(DsAllocator* dsall) {
346 external_driving=false;
350 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
352 external_driving=true;
355 void OsdWin::Blank() {
356 WaitForSingleObject(event,INFINITE);
358 d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);