2 Copyright 2006 Marten Richter
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "surfacewin.h"
26 SurfaceWin::SurfaceWin(int id)
33 event = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
36 SurfaceWin::~SurfaceWin()
38 if (d3dsurface) d3dsurface->Release();
39 if (d3dtexture) d3dtexture->Release();
43 int SurfaceWin::create(UINT width, UINT height)
45 LPDIRECT3DDEVICE9 d3ddev=((OsdWin*)(Osd::getInstance()))->getD3dDev();
48 if (d3ddev->CreateTexture(1024,1024,0,0,D3DFMT_A8R8G8B8,
49 // Does every adapter with alpha blending support this?
50 D3DPOOL_DEFAULT,&d3dtexture ,NULL)!=D3D_OK) {
51 MILLISLEEP(50);//wait maybe next time it will work
54 if (d3dtexture->GetSurfaceLevel(0,&d3dsurface)!=D3D_OK) {
55 d3dtexture->Release();
62 if (hres=d3ddev->CreateOffscreenPlainSurface(width,height,D3DFMT_A8R8G8B8,
63 D3DPOOL_SYSTEMMEM,&d3dsurface,NULL)!=D3D_OK) {
64 MILLISLEEP(50);//wait maybe next time it will work
71 /* If someone does high performance Animations on the OSD, we have to change the types
72 of surface in order to address these performance issues, if we have only very few updates
73 per second this would be fast enough !*/
80 void SurfaceWin::display()
84 int SurfaceWin::fillblt(int x, int y, int width, int height, unsigned int c)
86 WaitForSingleObject(event,INFINITE); //since this might be called before surface
87 //allocation we will wait in this case, hopefully without deadlocks
88 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
91 return 0; //why does this happen
94 LPDIRECT3DDEVICE9 d3ddev=osd->getD3dDev();
97 //This should not happen!
101 osd->BeginPainting();
102 D3DLOCKED_RECT lockrect;
103 int cx,cy,cwidth,cheight;
104 cx=min(max(x,0),swidth-1);
105 cy=min(max(y,0),sheight-1);
106 cwidth=min(width,swidth-x);
107 cheight=min(height,sheight-y);
108 RECT rect={cx,cy,cwidth,cheight};
110 if (d3dsurface->LockRect(&lockrect,&rect,D3DLOCK_DISCARD)!=D3D_OK) {
115 for (line=0;line<cheight;line++) {
116 unsigned int*row=((unsigned int*)(((char*)lockrect.pBits)+lockrect.Pitch*line));
117 for (column=0;column<cwidth;column++) {
122 if (d3dsurface->UnlockRect()!=D3D_OK) {
133 void SurfaceWin::startFastDraw(){
134 WaitForSingleObject(event,INFINITE); //since this might be called before surface
135 //allocation we will wait in this case, hopefully without deadlocks
137 return; //why does this happen
139 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
140 LPDIRECT3DDEVICE9 d3ddev=osd->getD3dDev();
142 //This should not happen!
146 osd->BeginPainting();
147 // D3DLOCKED_RECT lockrect;
148 RECT rect={0,0,swidth,sheight};
149 if (d3dsurface->LockRect(&lockrect,&rect,D3DLOCK_DISCARD)!=D3D_OK) {
156 void SurfaceWin::endFastDraw(){
157 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
158 if (d3dsurface->UnlockRect()!=D3D_OK) {
167 void SurfaceWin::drawPixel(int x, int y, unsigned int c)
169 //FixMe: locking for every single Pixel will be painfully slow
172 WaitForSingleObject(event,INFINITE); //since this might be called before surface
173 //allocation we will wait in this case, hopefully without deadlocks
175 return; //why does this happen
177 osd=((OsdWin*)(Osd::getInstance()));
179 if (x>=swidth || y>=sheight) return; //do not draw outside the surface
181 //This should not happen!
186 osd->BeginPainting();
187 // D3DLOCKED_RECT lockrect;
188 RECT rect={x,y,x+1,y+1};
189 if (d3dsurface->LockRect(&lockrect,&rect,D3DLOCK_DISCARD)!=D3D_OK) {
193 unsigned int*row=(unsigned int*)(((char*)lockrect.pBits));
195 if (d3dsurface->UnlockRect()!=D3D_OK) {
201 unsigned int*row=(unsigned int*)(((char*)lockrect.pBits+lockrect.Pitch*y+4*x));
209 void SurfaceWin::drawHorzLine(int x1, int x2, int y, unsigned int c)
211 fillblt(x1, y, x2-x1, 1, c);
214 void SurfaceWin::drawVertLine(int x, int y1, int y2, unsigned int c)
216 fillblt(x, y1, 1, y2-y1, c);
219 void SurfaceWin::drawBitmap(int x, int y, const Bitmap& bm)
221 // Temporary code? Draw one pixel at a time using drawPixel()
223 for (UINT j = 0; j < bm.getHeight(); ++j)
224 for (UINT i = 0; i < bm.getWidth(); ++i)
225 drawPixel(x+i, y+j, bm.getColour(i,j));
229 int SurfaceWin::updateToScreen(int sx, int sy, int w, int h, int dx, int dy) // FIXME new, replace others with this FIXME
231 WaitForSingleObject(event,INFINITE); //since this might be called before surface
232 //allocation we will wait in this case, hopefully without deadlocks
234 return 0; //why does this happen
236 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
237 LPDIRECT3DDEVICE9 d3ddev=osd->getD3dDev();
238 LPDIRECT3DSURFACE9 screensurface=((SurfaceWin*)screen)->getD3dsurface();
239 if (!screensurface) return 0;
240 RECT sourcerect={sx,sy,sx+w,sy+h};
241 POINT destpoint={dx,dy};
242 osd->BeginPainting();
243 if (d3ddev->UpdateSurface(d3dsurface,&sourcerect,screensurface,&destpoint)!=D3D_OK) {
244 Log::getInstance()->log("Surface", Log::DEBUG, "Could not update to Screen!");
252 int SurfaceWin::blt(int fd, unsigned long shandle, int sx, int sy, int width, int height, unsigned long dhandle, int dx, int dy)
254 //I don't see code using this function, so I skip it, since it is a MVP specific interface
258 void SurfaceWin::screenShot(char* fileName)
260 //Isn't this for debugging only, so I won't implement it yet
263 void SurfaceWin::readPixel(int x, int y, unsigned char* r, unsigned char* g, unsigned char* b)
265 //Isn't this for debugging only, so I won't implement it yet
267 void SurfaceWin::ReleaseSurface()
270 LPDIRECT3DSURFACE9 temp_surf=d3dsurface;
271 LPDIRECT3DTEXTURE9 temp_text=d3dtexture;
275 if (temp_surf) temp_surf->Release();
276 if (temp_text) temp_text->Release();
279 void SurfaceWin::drawJpeg(char *fileName,DWORD x, DWORD y,DWORD *width, DWORD *height){
280 WaitForSingleObject(event,INFINITE); //since this might be called before surface
281 //allocation we will wait in this case, hopefully without deadlocks
283 return ; //why does this happen
285 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
288 D3DXIMAGE_INFO image_inf;
289 osd->BeginPainting();
290 // D3DXGetImageInfoFromFile(fileName,&image_inf);
291 D3DXGetImageInfoFromResource(NULL,fileName,&image_inf);
292 RECT dest_rec={x,y,x+image_inf.Width,
294 /* if (D3DXLoadSurfaceFromFile(
302 &image_inf)!=D3D_OK) {
303 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
306 if (D3DXLoadSurfaceFromResource(
315 &image_inf)!=D3D_OK) {
316 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
320 *width=image_inf.Width;
321 *height=image_inf.Height;
325 void SurfaceWin::drawJpeg(char *buffer,ULONG buflength,DWORD x, DWORD y,DWORD *width, DWORD *height){
326 WaitForSingleObject(event,INFINITE); //since this might be called before surface
327 //allocation we will wait in this case, hopefully without deadlocks
329 return ; //why does this happen
331 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
334 D3DXIMAGE_INFO image_inf;
335 osd->BeginPainting();
336 // D3DXGetImageInfoFromFile(fileName,&image_inf);
337 D3DXGetImageInfoFromFileInMemory((void*)buffer,buflength,&image_inf);
338 RECT dest_rec={x,y,x+image_inf.Width,
340 /* if (D3DXLoadSurfaceFromFile(
348 &image_inf)!=D3D_OK) {
349 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
352 /* if (D3DXLoadSurfaceFromResource(
361 &image_inf)!=D3D_OK) {
362 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
365 if (D3DXLoadSurfaceFromFileInMemory(
374 &image_inf)!=D3D_OK) {
375 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
379 *width=image_inf.Width;
380 *height=image_inf.Height;