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"
25 SurfaceWin::SurfaceWin(int id)
32 event = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
35 SurfaceWin::~SurfaceWin()
37 if (d3dsurface) d3dsurface->Release();
38 if (d3dtexture) d3dtexture->Release();
42 int SurfaceWin::create(UINT width, UINT height)
44 LPDIRECT3DDEVICE9 d3ddev=((OsdWin*)(Osd::getInstance()))->getD3dDev();
47 if (d3ddev->CreateTexture(1024,1024,0,0,D3DFMT_A8R8G8B8,
48 // Does every adapter with alpha blending support this?
49 D3DPOOL_DEFAULT,&d3dtexture ,NULL)!=D3D_OK) {
50 MILLISLEEP(50);//wait maybe next time it will work
53 if (d3dtexture->GetSurfaceLevel(0,&d3dsurface)!=D3D_OK) {
54 d3dtexture->Release();
61 if (hres=d3ddev->CreateOffscreenPlainSurface(width,height,D3DFMT_A8R8G8B8,
62 D3DPOOL_SYSTEMMEM,&d3dsurface,NULL)!=D3D_OK) {
63 MILLISLEEP(50);//wait maybe next time it will work
70 /* If someone does high performance Animations on the OSD, we have to change the types
71 of surface in order to address these performance issues, if we have only very few updates
72 per second this would be fast enough !*/
79 void SurfaceWin::display()
83 int SurfaceWin::fillblt(int x, int y, int width, int height, unsigned int c)
85 WaitForSingleObject(event,INFINITE); //since this might be called before surface
86 //allocation we will wait in this case, hopefully without deadlocks
87 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
90 return 0; //why does this happen
93 LPDIRECT3DDEVICE9 d3ddev=osd->getD3dDev();
96 //This should not happen!
100 osd->BeginPainting();
101 D3DLOCKED_RECT lockrect;
102 int cx,cy,cwidth,cheight;
103 cx=min(max(x,0),swidth-1);
104 cy=min(max(y,0),sheight-1);
105 cwidth=min(width,swidth-x);
106 cheight=min(height,sheight-y);
107 RECT rect={cx,cy,cwidth,cheight};
109 if (d3dsurface->LockRect(&lockrect,&rect,D3DLOCK_DISCARD)!=D3D_OK) {
114 for (line=0;line<cheight;line++) {
115 unsigned int*row=((unsigned int*)(((char*)lockrect.pBits)+lockrect.Pitch*line));
116 for (column=0;column<cwidth;column++) {
121 if (d3dsurface->UnlockRect()!=D3D_OK) {
132 void SurfaceWin::startFastDraw(){
133 WaitForSingleObject(event,INFINITE); //since this might be called before surface
134 //allocation we will wait in this case, hopefully without deadlocks
136 return; //why does this happen
138 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
139 LPDIRECT3DDEVICE9 d3ddev=osd->getD3dDev();
141 //This should not happen!
145 osd->BeginPainting();
146 // D3DLOCKED_RECT lockrect;
147 RECT rect={0,0,swidth,sheight};
148 if (d3dsurface->LockRect(&lockrect,&rect,D3DLOCK_DISCARD)!=D3D_OK) {
155 void SurfaceWin::endFastDraw(){
156 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
157 if (d3dsurface->UnlockRect()!=D3D_OK) {
166 void SurfaceWin::drawPixel(int x, int y, unsigned int c)
168 //FixMe: locking for every single Pixel will be painfully slow
171 WaitForSingleObject(event,INFINITE); //since this might be called before surface
172 //allocation we will wait in this case, hopefully without deadlocks
174 return; //why does this happen
176 osd=((OsdWin*)(Osd::getInstance()));
178 if (x>=swidth || y>=sheight) return; //do not draw outside the surface
180 //This should not happen!
185 osd->BeginPainting();
186 // D3DLOCKED_RECT lockrect;
187 RECT rect={x,y,x+1,y+1};
188 if (d3dsurface->LockRect(&lockrect,&rect,D3DLOCK_DISCARD)!=D3D_OK) {
192 unsigned int*row=(unsigned int*)(((char*)lockrect.pBits));
194 if (d3dsurface->UnlockRect()!=D3D_OK) {
200 unsigned int*row=(unsigned int*)(((char*)lockrect.pBits+lockrect.Pitch*y+4*x));
208 void SurfaceWin::drawHorzLine(int x1, int x2, int y, unsigned int c)
210 fillblt(x1, y, x2-x1, 1, c);
213 void SurfaceWin::drawVertLine(int x, int y1, int y2, unsigned int c)
215 fillblt(x, y1, 1, y2-y1, c);
218 int SurfaceWin::updateToScreen(int sx, int sy, int w, int h, int dx, int dy) // FIXME new, replace others with this FIXME
220 WaitForSingleObject(event,INFINITE); //since this might be called before surface
221 //allocation we will wait in this case, hopefully without deadlocks
223 return 0; //why does this happen
225 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
226 LPDIRECT3DDEVICE9 d3ddev=osd->getD3dDev();
227 LPDIRECT3DSURFACE9 screensurface=((SurfaceWin*)screen)->getD3dsurface();
228 if (!screensurface) return 0;
229 RECT sourcerect={sx,sy,sx+w,sy+h};
230 POINT destpoint={dx,dy};
231 osd->BeginPainting();
232 if (d3ddev->UpdateSurface(d3dsurface,&sourcerect,screensurface,&destpoint)!=D3D_OK) {
233 Log::getInstance()->log("Surface", Log::DEBUG, "Could not update to Screen!");
241 int SurfaceWin::blt(int fd, unsigned long shandle, int sx, int sy, int width, int height, unsigned long dhandle, int dx, int dy)
243 //I don't see code using this function, so I skip it, since it is a MVP specific interface
247 void SurfaceWin::screenShot(char* fileName)
249 //Isn't this for debugging only, so I won't implement it yet
252 void SurfaceWin::readPixel(int x, int y, unsigned char* r, unsigned char* g, unsigned char* b)
254 //Isn't this for debugging only, so I won't implement it yet
256 void SurfaceWin::ReleaseSurface()
259 LPDIRECT3DSURFACE9 temp_surf=d3dsurface;
260 LPDIRECT3DTEXTURE9 temp_text=d3dtexture;
264 if (temp_surf) temp_surf->Release();
265 if (temp_text) temp_text->Release();
268 void SurfaceWin::drawJpeg(char *fileName,DWORD x, DWORD y,DWORD *width, DWORD *height){
269 WaitForSingleObject(event,INFINITE); //since this might be called before surface
270 //allocation we will wait in this case, hopefully without deadlocks
272 return ; //why does this happen
274 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
277 D3DXIMAGE_INFO image_inf;
278 osd->BeginPainting();
279 // D3DXGetImageInfoFromFile(fileName,&image_inf);
280 D3DXGetImageInfoFromResource(NULL,fileName,&image_inf);
281 RECT dest_rec={x,y,x+image_inf.Width,
283 /* if (D3DXLoadSurfaceFromFile(
291 &image_inf)!=D3D_OK) {
292 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
295 if (D3DXLoadSurfaceFromResource(
304 &image_inf)!=D3D_OK) {
305 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
309 *width=image_inf.Width;
310 *height=image_inf.Height;
314 void SurfaceWin::drawJpeg(char *buffer,ULONG buflength,DWORD x, DWORD y,DWORD *width, DWORD *height){
315 WaitForSingleObject(event,INFINITE); //since this might be called before surface
316 //allocation we will wait in this case, hopefully without deadlocks
318 return ; //why does this happen
320 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
323 D3DXIMAGE_INFO image_inf;
324 osd->BeginPainting();
325 // D3DXGetImageInfoFromFile(fileName,&image_inf);
326 D3DXGetImageInfoFromFileInMemory((void*)buffer,buflength,&image_inf);
327 RECT dest_rec={x,y,x+image_inf.Width,
329 /* if (D3DXLoadSurfaceFromFile(
337 &image_inf)!=D3D_OK) {
338 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
341 /* if (D3DXLoadSurfaceFromResource(
350 &image_inf)!=D3D_OK) {
351 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
354 if (D3DXLoadSurfaceFromFileInMemory(
363 &image_inf)!=D3D_OK) {
364 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
368 *width=image_inf.Width;
369 *height=image_inf.Height;