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.
23 #include "surfaceopengl.h"
24 #include "osdopengl.h"
29 inline unsigned int InternalColour(unsigned int c){
30 return (c &0x000000FF)<<16 |
36 SurfaceOpenGL::SurfaceOpenGL(int id)
46 SurfaceOpenGL::~SurfaceOpenGL()
53 glDeleteTextures(1,&gltexture);
58 int SurfaceOpenGL::create(UINT width, UINT height)
60 OsdOpenGL* osd=((OsdOpenGL*)(Osd::getInstance()));
61 //osd->BeginPainting();
64 if (this == screen) { // We do not need locking here, since the osd calls this
68 glGenTextures(1, &gltexture);
70 glBindTexture(GL_TEXTURE_2D, gltexture);
72 void *image = malloc(sheight * swidth * 4);
73 memset(image, 0, sheight * swidth * 4);
74 /* for (int i=0;i< sheight * swidth * 4; i++) { //fill it with garbage, useful for debugging
75 ((char*)image)[i]=i%255;
77 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, swidth, sheight, 0, GL_RGBA,
78 GL_UNSIGNED_BYTE, image);
83 data=(char*)malloc(sizeof(uint32_t)*width*height);
92 void SurfaceOpenGL::display()
96 int SurfaceOpenGL::fillblt(int x, int y, int width, int height, const DrawStyle& c) {
98 //since this might be called before surface
99 //allocation we will wait in this case, hopefully without deadlocks
100 if (screen == this || !data ) {
101 //This should not happen!
106 /* OsdWin* osd=((OsdWin*)(Osd::getInstance()));
108 osd->BeginPainting();
110 glViewport(0,0,swidth,sheight);*/
112 //osd->EndPainting();
115 unsigned int my_c=InternalColour(c.rgba());
118 if (height+y>sheight) iheight=sheight-y;
120 if (width+x>swidth) iwidth=swidth-y;
124 for (line = y; line < (iheight + y); line++) {
125 uint32_t *row = ((unsigned int*) (((char*) data) + (swidth * line + x)
126 * sizeof(uint32_t)));
127 for (column = 0; column < iwidth; column++) {
132 /* if (d3dsurface->UnlockRect() != D3D_OK) {
136 osd->EndPainting();*/
142 void SurfaceOpenGL::startFastDraw(){
146 void SurfaceOpenGL::endFastDraw(){
150 void SurfaceOpenGL::drawPixel(int x, int y, Colour & colour, bool fastdraw) {
151 int c = ( (0xFF000000 )
153 | (colour.green << 8)
156 drawPixel(x, y, c, fastdraw);
159 void SurfaceOpenGL::drawPixel(int x, int y, unsigned int c, bool fastdraw) {
160 //FixMe: locking for every single Pixel will be painfully slow
161 if (screen == this) {
162 //This should not happen!
166 return; //why does this happen
170 srf_mutex.Lock(); //since this might be called before surface
172 //allocation we will wait in this case, hopefully without deadlocks
174 //osd = ((OsdWin*) (Osd::getInstance()));
176 if (x >= swidth || y >= sheight)
177 return; //do not draw outside the surface
179 unsigned int my_c=InternalColour(c);
181 unsigned int*row = (unsigned int*) (((char*) data + (swidth * y + x)
182 * sizeof(uint32_t)));
186 srf_mutex.Unlock(); //since this might be called before surface
191 void SurfaceOpenGL::drawHorzLine(int x1, int x2, int y, const DrawStyle& c)
193 fillblt(x1, y, x2-x1, 1, c);
196 void SurfaceOpenGL::drawVertLine(int x, int y1, int y2, const DrawStyle& c)
198 fillblt(x, y1, 1, y2-y1, c);
201 void SurfaceOpenGL::drawBitmap(int x, int y, const Bitmap& bm,const DisplayRegion & region) //region should not matter for SD
203 // Temporary code? Draw one pixel at a time using drawPixel()
205 for (UINT j = 0; j < bm.getHeight(); ++j)
206 for (UINT i = 0; i < bm.getWidth(); ++i)
207 drawPixel(x+i, y+j, bm.getColour(i,j),true);
211 int SurfaceOpenGL::updateToScreen(int sx, int sy, int w, int h, int dx, int dy) // FIXME new, replace others with this FIXME
213 // Log::getInstance()->log("Surface", Log::WARN, "UTS Mark1");
214 srf_mutex.Lock();//since this might be called before surface
215 //allocation we will wait in this case, hopefully without deadlocks
217 OsdOpenGL* osd=((OsdOpenGL*)(Osd::getInstance()));
218 // Log::getInstance()->log("Surface", Log::WARN, "UTS Mark2");
219 GLuint screengltexture=((SurfaceOpenGL*)screen)->getTexture();
221 osd->BeginPainting();
222 // Log::getInstance()->log("Surface", Log::WARN, "UTS Mark3");
223 glBindTexture(GL_TEXTURE_2D, screengltexture);
224 // Log::getInstance()->log("Surface", Log::WARN, "UTS Mark4 %d",glGetError());
226 for (int y=0;y<h;y++) { //hopefully this is not too slow
227 // Log::getInstance()->log("Surface", Log::WARN, "UTS Mark4a %d %d %d %d %d %d",sx,sy,w,h,dx,dy);
228 glTexSubImage2D(GL_TEXTURE_2D,0,dx,(dy+y),w,1,GL_RGBA,GL_UNSIGNED_BYTE,
229 data+((y+sy)*swidth+sx)*sizeof(uint32_t));
241 int SurfaceOpenGL::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 SurfaceOpenGL::screenShot(const char* fileName)
249 //Isn't this for debugging only, so I won't implement it yet
252 void SurfaceOpenGL::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
258 void SurfaceOpenGL::drawJpeg(const char *fileName,int x, int y,int *width, int *height){
260 WaitForSingleObject(event,INFINITE); //since this might be called before surface
261 //allocation we will wait in this case, hopefully without deadlocks
263 return ; //why does this happen
265 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
268 D3DXIMAGE_INFO image_inf;
269 osd->BeginPainting();
270 // D3DXGetImageInfoFromFile(fileName,&image_inf);
271 D3DXGetImageInfoFromResource(NULL,fileName,&image_inf);
272 RECT dest_rec={x,y,x+image_inf.Width,
274 /* if (D3DXLoadSurfaceFromFile(
282 &image_inf)!=D3D_OK) {
283 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
286 if (D3DXLoadSurfaceFromResource(
295 &image_inf)!=D3D_OK) {
296 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
300 *width=image_inf.Width;
301 *height=image_inf.Height;
306 void SurfaceOpenGL::drawJpeg(char *buffer,ULONG buflength,DWORD x, DWORD y,DWORD *width, DWORD *height){
307 WaitForSingleObject(event,INFINITE); //since this might be called before surface
308 //allocation we will wait in this case, hopefully without deadlocks
310 return ; //why does this happen
312 OsdWin* osd=((OsdWin*)(Osd::getInstance()));
315 D3DXIMAGE_INFO image_inf;
316 osd->BeginPainting();
317 // D3DXGetImageInfoFromFile(fileName,&image_inf);
318 D3DXGetImageInfoFromFileInMemory((void*)buffer,buflength,&image_inf);
319 RECT dest_rec={x,y,x+image_inf.Width,
321 /* if (D3DXLoadSurfaceFromFile(
329 &image_inf)!=D3D_OK) {
330 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
333 /* if (D3DXLoadSurfaceFromResource(
342 &image_inf)!=D3D_OK) {
343 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
346 if (D3DXLoadSurfaceFromFileInMemory(
355 &image_inf)!=D3D_OK) {
356 Log::getInstance()->log("Surface", Log::DEBUG, "Could not open jpeg!");
360 *width=image_inf.Width;
361 *height=image_inf.Height;