2 Copyright 2004-2005 Chris Tallon
\r
4 This file is part of VOMP.
\r
6 VOMP is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 VOMP is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with VOMP; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
24 #include "videowin.h"
\r
25 #include "surfacewin.h"
\r
27 #include "dsallocator.h"
\r
30 //This is stuff for rendering the OSD
\r
39 external_driving=false;
\r
41 event = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
\r
42 d3dmutex = CreateMutex(NULL,FALSE,NULL);
\r
48 if (initted) shutdown();
\r
50 CloseHandle(d3dmutex);
\r
55 if (!initted) return 0;
\r
59 int OsdWin::init(void* device)
\r
61 if (initted) return 0;
\r
62 Video* video = Video::getInstance();
\r
63 //First Create Direct 3D Object
\r
64 d3d=Direct3DCreate9(D3D_SDK_VERSION);
\r
67 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 object!");
\r
70 // then create the Device
\r
71 D3DPRESENT_PARAMETERS d3dparas;
\r
72 ZeroMemory(&d3dparas,sizeof(d3dparas));
\r
73 d3dparas.BackBufferWidth=video->getScreenWidth();
\r
74 d3dparas.BackBufferHeight=video->getScreenHeight();
\r
75 d3dparas.Windowed=TRUE;
\r
76 d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
\r
77 if (d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,*((HWND*) device),
\r
78 D3DCREATE_SOFTWARE_VERTEXPROCESSING |D3DCREATE_MULTITHREADED,&d3dparas,&d3ddevice)!=D3D_OK) {
\r
79 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 device!");
\r
82 d3ddevice->GetRenderTarget(0,&d3drtsurf);
\r
84 if (!InitVertexBuffer()) {
\r
85 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D9 vertex buf!");
\r
89 //Now we will create the Screen
\r
90 screen = new SurfaceWin(Surface::SCREEN);
\r
91 SetEvent(event);//Now all devices are ready
\r
92 screen->create(video->getScreenWidth(), video->getScreenHeight());
\r
94 initted = 1; // must set this here or create surface won't work
\r
100 int OsdWin::InitVertexBuffer() {
\r
101 Video* video = Video::getInstance();
\r
102 FLOAT texx=((float)video->getScreenWidth())/1024.f;
\r
103 FLOAT texy=((float)video->getScreenHeight())/1024.f;
\r
104 D3DCOLOR osdcolor=D3DCOLOR_RGBA(255,255,255,255);
\r
105 osdvertices[0].c=osdcolor;
\r
106 osdvertices[0].x=0.f-0.5f;
\r
107 osdvertices[0].y=0.f-0.5f;
\r
108 osdvertices[0].z=0.5f;
\r
109 osdvertices[0].rhw=1.f;
\r
110 osdvertices[0].u=0.f;
\r
111 osdvertices[0].v=0.f;
\r
112 osdvertices[1].c=osdcolor;
\r
113 osdvertices[1].x=((float)video->getScreenWidth())-0.5f;-0.5f;
\r
114 osdvertices[1].y=0.f-0.5f;
\r
115 osdvertices[1].z=0.5f;
\r
116 osdvertices[1].u=texx;
\r
117 osdvertices[1].v=0.f;
\r
118 osdvertices[1].rhw=1.f;
\r
119 osdvertices[2].c=osdcolor;
\r
120 osdvertices[2].x=((float)video->getScreenWidth())-0.5f;
\r
121 osdvertices[2].y=((float)video->getScreenHeight())-0.5f;
\r
122 osdvertices[2].z=0.5f;
\r
123 osdvertices[2].rhw=1.f;
\r
124 osdvertices[2].u=texx;
\r
125 osdvertices[2].v=texy;
\r
126 osdvertices[3].c=osdcolor;
\r
127 osdvertices[3].x=0.f-0.5f;
\r
128 osdvertices[3].y=((float)video->getScreenHeight())-0.5f;
\r
129 osdvertices[3].z=0;
\r
130 osdvertices[3].rhw=1.f;
\r
131 osdvertices[3].u=0.f;
\r
132 osdvertices[3].v=texy;
\r
137 if (d3ddevice->CreateVertexBuffer(4*sizeof(OSDVERTEX),0,D3DFVF_OSDVERTEX,D3DPOOL_MANAGED,
\r
138 &d3dvb,NULL)!=D3D_OK) {
\r
141 void *pvertex=NULL;
\r
142 if (d3dvb->Lock(0,sizeof(osdvertices),&pvertex,0)!=D3D_OK) {
\r
145 memcpy(pvertex,osdvertices,sizeof(osdvertices));
\r
150 int OsdWin::shutdown()
\r
152 if (!initted) return 0;
\r
154 d3drtsurf->Release();
\r
155 d3ddevice->Release();
\r
161 void OsdWin::screenShot(char* fileName)
\r
163 screen->screenShot(fileName);
\r
166 // This function is called from the WinMain function in order to get Screen updates
\r
167 void OsdWin::Render()
\r
169 if (!initted) return ;
\r
170 if (external_driving) {
\r
171 Sleep(5); //Sleep for 5 ms, in order to avoid blocking the other threads
\r
173 InternalRendering(NULL);
\r
177 void OsdWin::RenderDS(LPDIRECT3DSURFACE9 present){
\r
178 if (!initted) return;
\r
179 if (external_driving) {
\r
180 InternalRendering(present);
\r
185 void OsdWin::InternalRendering(LPDIRECT3DSURFACE9 present){
\r
186 WaitForSingleObject(event,INFINITE);
\r
188 if (external_driving) {
\r
189 //Copy video to Backbuffer
\r
190 if (present!=NULL) {
\r
191 VideoWin* video =(VideoWin*) Video::getInstance();
\r
192 /*calculating destination rect */
\r
193 RECT destrect={0,0,video->getScreenWidth(),video->getScreenHeight()};
\r
194 UCHAR mode=video->getMode();
\r
196 case Video::EIGHTH:
\r
197 destrect.right=destrect.right/2;
\r
198 destrect.bottom=destrect.bottom/2;
\r
199 case Video::QUARTER:
\r
200 destrect.right=destrect.right/2+video->getPosx()*2;
\r
201 destrect.bottom=destrect.bottom/2+video->getPosy()*2;
\r
202 destrect.left=video->getPosx()*2;
\r
203 destrect.top=video->getPosy()*2;
\r
204 d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
\r
207 D3DSURFACE_DESC surf_desc;
\r
208 present->GetDesc(&surf_desc);//for chop sides
\r
209 RECT sourcerect= {0,0,surf_desc.Width,surf_desc.Height};
\r
210 if (video->getPseudoTVsize()==Video::ASPECT4X3
\r
211 && video->getMode()==Video::NORMAL
\r
212 && video->getAspectRatio()==Video::ASPECT16X9) {
\r
213 unsigned int correction=((double) (surf_desc.Width))*4.*9./3./16.;
\r
214 sourcerect.left=(surf_desc.Width-correction)/2;
\r
215 sourcerect.right=sourcerect.left+correction;
\r
218 d3ddevice->StretchRect(present,&sourcerect,d3drtsurf ,&destrect,D3DTEXF_LINEAR);
\r
221 VideoWin* video =(VideoWin*) Video::getInstance();
\r
223 if (!video->isVideoOn()) d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
\r
226 if (d3ddevice->BeginScene()==D3D_OK) {
\r
227 d3ddevice->SetStreamSource(0,d3dvb,0,sizeof(OSDVERTEX));
\r
228 d3ddevice->SetFVF(D3DFVF_OSDVERTEX);
\r
229 d3ddevice->SetTexture(0,((SurfaceWin*)screen)->getD3dtexture());
\r
230 //d3ddevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
\r
231 d3ddevice->SetTextureStageState(0, D3DTSS_ALPHAOP,D3DTOP_MODULATE);
\r
232 d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
\r
233 d3ddevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
\r
234 d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
\r
235 d3ddevice->SetRenderState(D3DRS_LIGHTING,FALSE);
\r
238 d3ddevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
\r
239 d3ddevice->EndScene();
\r
240 //Show it to the user!
\r
241 if (d3ddevice->Present(NULL,NULL,NULL,NULL)==D3DERR_DEVICELOST){
\r
247 if (!external_driving) {
\r
248 Sleep(4);//The User can wait for 4 milliseconds to see his changes
\r
252 void OsdWin::DoLost(){
\r
253 Log::getInstance()->log("OSD", Log::WARN, "Direct3D Device Lost! Reobtaining...");
\r
255 if (external_driving && dsallocator!=NULL) {
\r
256 dsallocator->LostDevice(); //Propagate the information through DS
\r
258 //First we free up all resources
\r
259 Video* video = Video::getInstance();
\r
260 ((SurfaceWin*)screen)->ReleaseSurface();
\r
261 d3drtsurf->Release();
\r
264 D3DPRESENT_PARAMETERS d3dparas;
\r
265 ZeroMemory(&d3dparas,sizeof(d3dparas));
\r
266 d3dparas.BackBufferWidth=video->getScreenWidth();
\r
267 d3dparas.BackBufferHeight=video->getScreenHeight();
\r
268 d3dparas.Windowed=TRUE;
\r
269 d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;
\r
270 if (d3ddevice->Reset(&d3dparas)!=D3D_OK){
\r
273 screen->create(video->getScreenWidth(), video->getScreenHeight());
\r
275 d3ddevice->GetRenderTarget(0,&d3drtsurf);
\r
276 InitVertexBuffer();
\r
277 //Redraw Views, Chris could you add a member function to viewman, so that
\r
278 // I can cause it to completely redraw the Views?
\r
279 // Otherwise the OSD would be distorted after Device Lost
\r
284 LPDIRECT3DDEVICE9 OsdWin::getD3dDev() {
\r
285 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
\r
289 LPDIRECT3D9 OsdWin::getD3d() {
\r
290 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
\r
294 void OsdWin::BeginPainting() {//We synchronize calls to d3d between different threads
\r
295 WaitForSingleObject(d3dmutex,INFINITE);
\r
298 void OsdWin::EndPainting() {
\r
299 ReleaseMutex(d3dmutex);
\r
302 void OsdWin::setExternalDriving(DsAllocator* dsall) {
\r
304 external_driving=false;
\r
308 WaitForSingleObject(event,INFINITE);//We will only return if we are initted
\r
310 external_driving=true;
\r
313 void OsdWin::Blank() {
\r
314 WaitForSingleObject(event,INFINITE);
\r
316 d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
\r