]> git.vomp.tv Git - vompclient-marten.git/blob - osdopengl.cc
Preliminray OMX h264 playback stuff not finished yet
[vompclient-marten.git] / osdopengl.cc
1 /*\r
2     Copyright 2004-2005 Chris Tallon, 2006,2011-2012 Marten Richter\r
3 \r
4     This file is part of VOMP.\r
5 \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
10 \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
15 \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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
19 */\r
20 \r
21 \r
22 #include "osdopengl.h"\r
23 #include "mtd.h"\r
24 #include "videovpeogl.h"\r
25 #include "surfaceopengl.h"\r
26 \r
27 \r
28 #include "message.h"\r
29 #include "command.h"\r
30 \r
31 #include "shaders/generic__vertex_shader.h"\r
32 #include "shaders/osd__frag_shader.h"\r
33 \r
34 #define  BACKBUFFER_WIDTH 1920\r
35 #define  BACKBUFFER_HEIGHT 1080\r
36 \r
37 \r
38 \r
39 long long getTimeMS() {\r
40         struct timespec ts;\r
41         clock_gettime(CLOCK_MONOTONIC, &ts);\r
42         return ts.tv_sec*1000+ts.tv_nsec/1000000LL;\r
43 }\r
44 \r
45 \r
46 \r
47 OsdOpenGL::OsdOpenGL()\r
48 {\r
49   glmutex.Lock();\r
50 \r
51   external_driving=false;\r
52 \r
53   lastrendertime=getTimeMS();\r
54   display_height=0;\r
55   display_width=0;\r
56   osd_shader=0;\r
57   gen_shader=0;\r
58   osd_program=0;\r
59 \r
60 #ifdef BENCHMARK_FPS\r
61         last_benchmark_time=getTimeMS();\r
62         num_benchmark_frames=0;\r
63 #endif\r
64 \r
65   \r
66 }\r
67 \r
68 OsdOpenGL::~OsdOpenGL()\r
69 {\r
70 \r
71   if (initted) \r
72   {\r
73           threadStop();\r
74                 shutdown();\r
75   }\r
76 \r
77 \r
78   glmutex.Unlock();\r
79 }\r
80 \r
81 int OsdOpenGL::getFD()\r
82 {\r
83   if (!initted) return 0;\r
84   return fdOsd;\r
85 }\r
86 \r
87 Surface * OsdOpenGL::createNewSurface() {\r
88         return (Surface*)new SurfaceOpenGL();\r
89 }\r
90 \r
91 int OsdOpenGL::init(void* device)\r
92 {\r
93   if (initted) return 0;\r
94   Video* video = Video::getInstance();\r
95    //window=*((HWND*)device);\r
96   \r
97    // May be this device specific part should go to a device specific child class\r
98 \r
99    //init broadcom chipset (Move to video?)\r
100    bcm_host_init();\r
101 \r
102    //First get connection to egl\r
103    egl_display=eglGetDisplay(EGL_DEFAULT_DISPLAY);\r
104 \r
105    if (egl_display==EGL_NO_DISPLAY) {\r
106            Log::getInstance()->log("OSD", Log::WARN, "Could not get egl display! %x",eglGetError());\r
107            return 0;\r
108    }\r
109 \r
110 \r
111 \r
112    if (eglInitialize(egl_display, NULL, NULL)==EGL_FALSE) {\r
113            Log::getInstance()->log("OSD", Log::WARN, "Initialising display failed! %x",eglGetError());\r
114            return 0;\r
115    }\r
116 \r
117    const char *query_str=eglQueryString(egl_display,EGL_CLIENT_APIS);\r
118    if (query_str) Log::getInstance()->log("OSD", Log::NOTICE, "%s",query_str);\r
119    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",eglGetError());\r
120    query_str=eglQueryString(egl_display,EGL_EXTENSIONS);\r
121    if (query_str)    Log::getInstance()->log("OSD", Log::NOTICE, "%s",query_str);\r
122    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",eglGetError());\r
123 \r
124    const EGLint attributs[]={\r
125                  EGL_RED_SIZE,8,EGL_GREEN_SIZE, 8,EGL_BLUE_SIZE, 8,EGL_ALPHA_SIZE, 8,\r
126          EGL_SURFACE_TYPE, EGL_WINDOW_BIT,\r
127          EGL_CONFORMANT, EGL_OPENGL_ES2_BIT,\r
128          EGL_NONE\r
129    }; // Here, we might have to select the resolution!\r
130 \r
131 \r
132    EGLint number;\r
133 \r
134    if (eglChooseConfig(egl_display, attributs, &egl_ourconfig, 1, &number)==EGL_FALSE) {\r
135            Log::getInstance()->log("OSD", Log::WARN, "Choosing egl config failed! %d",eglGetError());\r
136            return 0;\r
137    }\r
138 \r
139    const EGLint attr_context[]={\r
140                    EGL_CONTEXT_CLIENT_VERSION,2,\r
141           EGL_NONE\r
142       };\r
143 \r
144    egl_context=eglCreateContext(egl_display,egl_ourconfig,EGL_NO_CONTEXT,attr_context);\r
145    if (egl_context==EGL_NO_CONTEXT) {\r
146            Log::getInstance()->log("OSD", Log::WARN, "Creating egl context failed! %d",eglGetError());\r
147            return 0;\r
148    }\r
149 \r
150    // warning broadcom specific, get display size!\r
151    display_width=display_height=0;\r
152    if (graphics_get_display_size(0, &display_width, &display_height)<0) {\r
153            Log::getInstance()->log("OSD", Log::WARN, "Getting display size failed! (BCM API) ");\r
154            return 0;\r
155    }\r
156    Log::getInstance()->log("OSD", Log::NOTICE, "Displaysize is %d x %d ",display_width, display_height);\r
157    VC_RECT_T dst_rect ={0,0,display_width,display_height};\r
158    VC_RECT_T src_rect={0,0,display_width<<16,display_height<<16};\r
159    DISPMANX_DISPLAY_HANDLE_T bcm_display;\r
160    DISPMANX_ELEMENT_HANDLE_T bcm_element;\r
161    DISPMANX_UPDATE_HANDLE_T  bcm_update;\r
162 \r
163 \r
164    bcm_display=vc_dispmanx_display_open(0);\r
165    bcm_update=vc_dispmanx_update_start(0);\r
166    bcm_element=vc_dispmanx_element_add(bcm_update,bcm_display,\r
167          0,&dst_rect, 0,\r
168          &src_rect,DISPMANX_PROTECTION_NONE,0, 0, (DISPMANX_TRANSFORM_T) 0);\r
169 \r
170    vc_dispmanx_update_submit_sync(bcm_update);\r
171    static EGL_DISPMANX_WINDOW_T nativewindow;\r
172    nativewindow.element=bcm_element;\r
173    nativewindow.height=display_height;\r
174    nativewindow.width=display_width;\r
175 \r
176    egl_surface = eglCreateWindowSurface(egl_display,egl_ourconfig, &nativewindow,NULL );\r
177    if (egl_surface==EGL_NO_SURFACE) {\r
178            Log::getInstance()->log("OSD", Log::WARN, "Creating egl window surface failed!");\r
179            return 0;\r
180    }\r
181 \r
182    if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {\r
183            Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed");\r
184                    return 0;\r
185    }\r
186    // Test stuff\r
187 \r
188 \r
189 \r
190   //Now we will create the Screen\r
191   screen = (Surface*) new SurfaceOpenGL(Surface::SCREEN);\r
192 \r
193   screen->create(video->getScreenWidth(), video->getScreenHeight());\r
194   screen->display();\r
195   initted = 1; // must set this here or create surface won't work\r
196 \r
197   //glGenBuffers(1, &vB);\r
198   //glGenBuffers(1, &iB);\r
199 \r
200   //Preparing the Shaders\r
201 \r
202   gen_shader=CreateShader(generic_vertex_shader, GL_VERTEX_SHADER);\r
203   osd_shader=CreateShader(osd_frag_shader, GL_FRAGMENT_SHADER);\r
204 \r
205 \r
206   osd_program=glCreateProgram();\r
207   if (osd_program==0) {\r
208           Log::getInstance()->log("OSD", Log::WARN, "Creating glsl program failed!%d",glGetError());\r
209       return 0;\r
210   }\r
211   glAttachShader(osd_program,gen_shader);\r
212   glAttachShader(osd_program,osd_shader);\r
213   glBindAttribLocation(osd_program,0,"vec_pos");\r
214   glBindAttribLocation(osd_program,1,"tex_coord");\r
215 \r
216   osd_sampler_loc=glGetUniformLocation(osd_program,"texture");\r
217 \r
218   glLinkProgram(osd_program);\r
219   GLint link_status;\r
220   glGetShaderiv(osd_program,GL_LINK_STATUS, &link_status);\r
221 \r
222   if (!link_status) {\r
223           char buffer[1024];\r
224           glGetProgramInfoLog(osd_program,1024,NULL,buffer);\r
225           Log::getInstance()->log("OSD", Log::WARN, "Compiling Programm failed!");\r
226           Log::getInstance()->log("OSD", Log::WARN, "%s",buffer);\r
227           glDeleteProgram(osd_program);\r
228           return 0;\r
229   }\r
230 \r
231   glClearColor(0.0f,0.0f,0.0f,1.f);\r
232 \r
233   eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );\r
234 \r
235   if (((VideoVPEOGL*)Video::getInstance())->initUsingOSDObjects()!=1) { //call Video for init opengl stuff\r
236           return 0;\r
237   }\r
238 \r
239 \r
240   glmutex.Unlock();\r
241   threadStart();\r
242 \r
243   return 1;\r
244 }\r
245 \r
246 \r
247 GLuint OsdOpenGL::CreateShader(const GLchar * source, GLenum type)\r
248 {\r
249         GLuint ret_shad=0;\r
250 \r
251         ret_shad=glCreateShader(type);\r
252         if (ret_shad==0 ) {\r
253                 Log::getInstance()->log("OSD", Log::WARN, "Creating Shader failed! %d",glGetError());\r
254                 return 0;\r
255         }\r
256         glShaderSource(ret_shad,1,&source,NULL);\r
257         glCompileShader(ret_shad);\r
258         GLint comp_status;\r
259         glGetShaderiv(ret_shad,GL_COMPILE_STATUS, &comp_status);\r
260 \r
261         if (!comp_status) {\r
262                 char buffer[1024];\r
263                 Log::getInstance()->log("OSD", Log::WARN, "Compiling Shader failed!");\r
264                 glGetShaderInfoLog(ret_shad,1024,NULL,buffer);\r
265                 Log::getInstance()->log("OSD", Log::WARN, "%s",buffer);\r
266                 glDeleteShader(ret_shad);\r
267                 return 0;\r
268         }\r
269         return ret_shad;\r
270 }\r
271 \r
272         \r
273 void OsdOpenGL::InitVertexBuffer(float  scalex,float scaley)\r
274 {\r
275   Video* video=Video::getInstance();\r
276   float texx=1.f;\r
277   float texy=1.f;\r
278   OSDCOLOR osdcolor={1.f,1.f,1.f,1.f};\r
279 \r
280  // osdvertices[0].c=osdcolor;\r
281   osdvertices[0].x= (scalex);\r
282   osdvertices[0].y=-scaley;\r
283   osdvertices[0].z=0.5;\r
284   osdvertices[0].u=texx;\r
285   osdvertices[0].v=texy;\r
286  // osdvertices[1].c=osdcolor;\r
287   osdvertices[1].x=(scalex);\r
288   osdvertices[1].y=(scaley);\r
289   osdvertices[1].z=0.5f;\r
290   osdvertices[1].u=texx;\r
291   osdvertices[1].v=0.f;\r
292   //  osdvertices[0].c=osdcolor;\r
293   osdvertices[2].x=(-scalex);\r
294   osdvertices[2].y=-scaley;\r
295   osdvertices[2].z=0.5f;\r
296   osdvertices[2].u=0.f;\r
297   osdvertices[2].v=texy;\r
298  // osdvertices[3].c=osdcolor;\r
299   osdvertices[3].x=-scalex;\r
300   osdvertices[3].y=(scaley);\r
301   osdvertices[3].z=0.5f;\r
302   osdvertices[3].u=0.f;\r
303   osdvertices[3].v=0.f;\r
304   \r
305   osdindices[0]=0;\r
306   osdindices[1]=1;\r
307   osdindices[2]=2;\r
308   osdindices[3]=0;\r
309   osdindices[4]=2;\r
310   osdindices[5]=3;\r
311 \r
312 \r
313 \r
314 \r
315  // glBindBuffer(GL_ARRAY_BUFFER, vB);\r
316  // glBufferData(GL_ARRAY_BUFFER, sizeof(osdvertices), osdvertices, GL_STATIC_DRAW);\r
317 \r
318 \r
319  // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iB);\r
320   //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(osdindices), osdindices, GL_STATIC_DRAW);\r
321 \r
322 \r
323 \r
324   return;\r
325 }\r
326 \r
327 int OsdOpenGL::shutdown()\r
328 {\r
329   if (!initted) return 0;\r
330   initted = 0;\r
331 \r
332   (((VideoVPEOGL*)Video::getInstance())->shutdownUsingOSDObjects());\r
333 \r
334 \r
335   if (osd_shader!=0) glDeleteShader(osd_shader);\r
336   if (gen_shader!=0) glDeleteShader(gen_shader);\r
337   if (osd_program!=0) glDeleteProgram(osd_program);\r
338 \r
339   glClear(GL_COLOR_BUFFER_BIT);\r
340   eglSwapBuffers(egl_display, egl_surface);\r
341   eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );\r
342   eglDestroySurface(egl_display,egl_surface);\r
343   eglDestroyContext(egl_display,egl_context);\r
344   eglTerminate(egl_display );\r
345 \r
346   return 1;\r
347 }\r
348 \r
349 void OsdOpenGL::screenShot(const char* fileName)\r
350 {\r
351   screen->screenShot(fileName);\r
352 }\r
353 \r
354 void OsdOpenGL::threadMethod()\r
355 {\r
356         // We have to claim the gl context for this thread\r
357         //glmutex.Lock();\r
358 \r
359         //glmutex.Unlock();\r
360         while (true)\r
361         {\r
362                 unsigned int waittime=10;\r
363                 if (initted){\r
364                 //      if (evrstate==EVR_pres_off || evrstate==EVR_pres_pause)\r
365                 //      {\r
366                                 Render();\r
367                                 //TODO get surfaces from Video object\r
368         /*              } else if (evrstate==EVR_pres_started)\r
369                         {\r
370                                 LPDIRECT3DSURFACE9 surf;\r
371                                 if (dsallocator) dsallocator->GetNextSurface(&surf,&waittime);\r
372                                 if (surf==NULL)\r
373                                 {\r
374                                         Render();\r
375                                 }\r
376                                 else\r
377                                 {\r
378                                         RenderDS(surf);\r
379                                         surf->Release();\r
380                                         if (dsallocator) dsallocator->DiscardSurfaceandgetWait(&waittime);\r
381                                 }\r
382                         }*/\r
383                 }\r
384                 threadCheckExit();\r
385                 if (waittime!=0) MILLISLEEP(min(10,waittime));\r
386                 //Sleep(1);\r
387         }\r
388         //eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );\r
389 }\r
390 \r
391 \r
392 void OsdOpenGL::threadPostStopCleanup()\r
393 {\r
394         //Doing nothing\r
395         //goo;\r
396 }\r
397 \r
398 \r
399 // This function is called from the WinMain function in order to get Screen updates\r
400 void OsdOpenGL::Render()\r
401 {\r
402         if (!initted) return ;\r
403         if (external_driving) {\r
404         long long time1=getTimeMS();\r
405 \r
406                 if ((time1-lastrendertime)>200) {//5 fps for OSD updates are enough, avoids tearing\r
407                         InternalRendering(NULL);\r
408                         lastrendertime=getTimeMS();\r
409         } else {\r
410                    //Sleep(5); //Sleep for 5 ms, in order to avoid blocking the other threads\r
411         }\r
412         } else {\r
413                 struct timespec ts;\r
414                 clock_gettime(CLOCK_MONOTONIC, &ts);\r
415                 long long time1=ts.tv_sec*1000+ts.tv_nsec/1000000LL;\r
416                 if ((time1-lastrendertime)>50) {//10 fps for OSD updates are enough, avoids tearing\r
417                         InternalRendering(NULL);\r
418                         lastrendertime=getTimeMS();\r
419                 } else {\r
420                         //Sleep(5);\r
421                 \r
422                 }\r
423                 \r
424         }\r
425 }\r
426 \r
427 void OsdOpenGL::RenderDS(GLuint  present){\r
428         if (!initted) return; \r
429         if (external_driving) {\r
430                 InternalRendering(present);\r
431                 lastrendertime=getTimeMS();\r
432         }\r
433 }\r
434 \r
435 \r
436 void OsdOpenGL::InternalRendering(GLuint  present){\r
437     BeginPainting();\r
438   /*  HRESULT losty=d3ddevice->TestCooperativeLevel();\r
439     if (losty==D3DERR_DEVICELOST) {\r
440         //Sleep(10);\r
441                 EndPainting();\r
442         return; //Device Lost\r
443     }\r
444     if (losty==D3DERR_DEVICENOTRESET){\r
445            EndPainting();\r
446        DoLost();\r
447        return;\r
448     }\r
449         WaitForSingleObject(event,INFINITE);\r
450         */\r
451    \r
452         \r
453 /*\r
454     LPDIRECT3DSURFACE9 targetsurf;\r
455         if (swappy)\r
456         {\r
457                 targetsurf=swapsurf;\r
458                 d3ddevice->SetRenderTarget(0,swapsurf);//Stupid VMR manipulates the render target\r
459         } \r
460         else\r
461         {\r
462                 targetsurf=d3drtsurf;\r
463                 d3ddevice->SetRenderTarget(0,d3drtsurf);//Stupid VMR manipulates the render target\r
464         }\r
465         D3DSURFACE_DESC targetdesc;\r
466         targetsurf->GetDesc(&targetdesc);\r
467 \r
468         if (external_driving) {\r
469                 //Copy video to Backbuffer\r
470                 if (present!=NULL ) {\r
471                         VideoWin* video =(VideoWin*) Video::getInstance();\r
472                         /*calculating destination rect *\r
473                         RECT destrect={0,0,/*video->getScreenWidth()* targetdesc.Width,\r
474                                 /*video->getScreenHeight()*targetdesc.Height};\r
475                         UCHAR mode=video->getMode();\r
476                         switch (mode) {\r
477                         case Video::EIGHTH:\r
478                         destrect.right=destrect.right/2;\r
479                         destrect.bottom=destrect.bottom/2;\r
480                         case Video::QUARTER:\r
481                         destrect.right=destrect.right/2+video->getPosx()*2;\r
482                         destrect.bottom=destrect.bottom/2+video->getPosy()*2;\r
483                         destrect.left=video->getPosx()*2;\r
484                         destrect.top=video->getPosy()*2;\r
485                         d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);\r
486                         break;\r
487                         };\r
488                         D3DSURFACE_DESC surf_desc;\r
489                         present->GetDesc(&surf_desc);//for chop sides\r
490                         RECT sourcerect= {0,0,surf_desc.Width,surf_desc.Height};\r
491                         if (video->getPseudoTVsize()==Video::ASPECT4X3 \r
492                                 && video->getMode()==Video::NORMAL \r
493                                 && video->getAspectRatio()==Video::ASPECT16X9) {\r
494                                         unsigned int correction=((double) (surf_desc.Width))*4.*9./3./16.;\r
495                                         sourcerect.left=(surf_desc.Width-correction)/2;\r
496                                         sourcerect.right=sourcerect.left+correction;\r
497                         }\r
498                         d3ddevice->StretchRect(present,&sourcerect,targetsurf ,&destrect,filter_type);\r
499 \r
500                 }\r
501         } else {\r
502                 VideoWin* video =(VideoWin*) Video::getInstance();\r
503                 //Clear Background\r
504                 if (!video->isVideoOn()) d3ddevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);\r
505         }*/\r
506 \r
507 \r
508         //InitVertexBuffer(display_width,display_height);\r
509     InitVertexBuffer(1.f,1.f);\r
510 \r
511 \r
512         glViewport(0, 0, display_width,display_height);\r
513 \r
514         glClear(GL_COLOR_BUFFER_BIT);\r
515         glUseProgram(osd_program);\r
516 \r
517         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(OSDVERTEX), osdvertices);\r
518         glEnableVertexAttribArray(0);\r
519         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,sizeof(OSDVERTEX), &(osdvertices[0].u));\r
520         glEnableVertexAttribArray(1);\r
521 \r
522 \r
523 \r
524 \r
525         glActiveTexture(GL_TEXTURE0);\r
526         glBindTexture(GL_TEXTURE_2D,((SurfaceOpenGL*)screen)->getTexture());\r
527 \r
528         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);\r
529         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);\r
530 \r
531         glUniform1i(osd_sampler_loc,0);\r
532 \r
533         if (0) { //This is ok for ffmpeg rendering not OMX\r
534                 glEnable(GL_BLEND);\r
535                 glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,GL_ZERO,GL_ONE);\r
536 \r
537         }\r
538 /*      glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE,sizeof(OSDVERTEX),\r
539                         (GLvoid*)(((char*)osdvertices)+3*sizeof(GLfloat)));\r
540         glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE,sizeof(OSDVERTEX),\r
541                         (GLvoid*)(((char*)osdvertices)+3*sizeof(GLfloat)+sizeof(OSDCOLOR)));*/\r
542         //glDisable(GL_LIGHTING);\r
543         //glEnable(GL_TEXTURE_2D);\r
544         //glEnable(GL_BLEND);\r
545     //glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
546     //glDepthFunc(GL_ALWAYS);\r
547     //glDisable(GL_DEPTH_TEST);\r
548     //glDisable(GL_STENCIL_TEST);\r
549     //glDisable(GL_CULL_FACE);\r
550 \r
551 \r
552 \r
553 /*\r
554         glActiveTexture(GL_TEXTURE0);\r
555         glBindTexture(GL_TEXTURE_2D,((SurfaceOpenGL*)screen)->getTexture());\r
556         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);\r
557         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);*/\r
558 //      glUniform1i(mTextureUniformHandle, present);\r
559 \r
560 \r
561 \r
562         glDrawArrays(GL_TRIANGLE_STRIP, 0,  4);\r
563 \r
564         //glDrawElements(GL_TRIANGLES, sizeof(osdindices)/sizeof(osdindices[0]), GL_UNSIGNED_BYTE, 0);\r
565 \r
566 \r
567 \r
568 \r
569 /*      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(OSDVERTEX), 0);\r
570         glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE,sizeof(OSDVERTEX),\r
571                                 (GLvoid*)(3*sizeof(GLfloat)));\r
572         glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE,sizeof(OSDVERTEX),\r
573                         (GLvoid*)(3*sizeof(GLfloat)+sizeof(OSDCOLOR)));*/\r
574 \r
575         //glDisable(GL_BLEND);\r
576         //glDisable(GL_TEXTURE_2D);\r
577         \r
578         //Show it to the user!\r
579         eglSwapBuffers(egl_display, egl_surface);\r
580 \r
581         EndPainting();\r
582 #ifdef BENCHMARK_FPS\r
583         num_benchmark_frames++;\r
584         if (getTimeMS()-last_benchmark_time>4000) {\r
585                 float fps=1000./(float)(getTimeMS()-last_benchmark_time);\r
586                 fps*=((float)num_benchmark_frames);\r
587                 num_benchmark_frames=0;\r
588                 Log::getInstance()->log("OSD", Log::NOTICE, "Current FPS %g", fps);\r
589                 last_benchmark_time=getTimeMS();\r
590 \r
591         }\r
592 \r
593 #endif\r
594 \r
595         \r
596 //      if (!external_driving) {\r
597 //              Sleep(4);//The User can wait for 4 milliseconds to see his changes\r
598 //      }\r
599 }\r
600 \r
601 bool OsdOpenGL::DoLost(){\r
602         /*\r
603         Log::getInstance()->log("OSD", Log::WARN, "Direct3D Device Lost! Reobtaining...");\r
604         ResetEvent(event);\r
605         if (external_driving && dsallocator!=NULL) {\r
606                 dsallocator->LostDevice(d3ddevice,d3d); //Propagate the information through DS\r
607         }\r
608         //First we free up all resources\r
609         Video* video = Video::getInstance();\r
610         ((SurfaceWin*)screen)->ReleaseSurface();\r
611         if (d3drtsurf) d3drtsurf->Release();\r
612     d3drtsurf=NULL;\r
613         D3DPRESENT_PARAMETERS d3dparas;\r
614         ZeroMemory(&d3dparas,sizeof(d3dparas));\r
615         d3dparas.BackBufferWidth=BACKBUFFER_WIDTH;\r
616         d3dparas.BackBufferHeight=BACKBUFFER_HEIGHT;\r
617         d3dparas.Windowed=TRUE;\r
618         d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;\r
619 \r
620         if (swapsurf) {swapsurf->Release();swapsurf=NULL;};\r
621         if (swappy) {swappy->Release();swappy=NULL;};\r
622 \r
623         if (d3ddevice->Reset(&d3dparas)!=D3D_OK){\r
624                 return false;\r
625         }\r
626     d3ddevice->GetRenderTarget(0,&d3drtsurf);\r
627         if (d3ddevman) d3ddevman->ResetDevice(d3ddevice,dxvatoken);\r
628         //InitVertexBuffer();\r
629     //Redraw Views, Chris could you add a member function to BoxStack, so that\r
630         // I can cause it to completely redraw the Views?\r
631         // Otherwise the OSD would be distorted after Device Lost\r
632         // FIXME\r
633         \r
634         SetEvent(event);\r
635 \r
636 \r
637         screen->create(video->getScreenWidth(), video->getScreenHeight());\r
638         screen->display();*/\r
639         \r
640         return true;\r
641 \r
642 }\r
643 \r
644 \r
645 void OsdOpenGL::BeginPainting() {//We synchronize calls to d3d between different threads\r
646         glmutex.Lock();\r
647         if (initted) {\r
648                 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {\r
649                         Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed in thread %d",eglGetError());\r
650                         return;\r
651                 }\r
652         }\r
653 }\r
654 \r
655 void OsdOpenGL::EndPainting() {\r
656         eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );\r
657         glmutex.Unlock();\r
658 }\r
659 \r
660 void OsdOpenGL::setExternalDriving(/*DsAllocator* dsall,*/unsigned int  width, unsigned height) {\r
661         /*\r
662         if (swappy)\r
663         {\r
664                 BeginPainting();\r
665                 d3ddevice->StretchRect(swapsurf,NULL,d3drtsurf,NULL,filter_type);\r
666                 LPDIRECT3DSWAPCHAIN9 temp=swappy;\r
667                 LPDIRECT3DSURFACE9 tempsurf=swapsurf;\r
668                 swappy=NULL;\r
669                 swapsurf=NULL;\r
670                 EndPainting();\r
671                 tempsurf->Release();\r
672                 temp->Release();\r
673         }\r
674 \r
675         if (dsall==NULL) {\r
676                 external_driving=false;\r
677                 dsallocator=NULL;       \r
678                 return;\r
679         }\r
680         WaitForSingleObject(event,INFINITE);//We will only return if we are initted\r
681         BeginPainting();\r
682 \r
683         if (width>BACKBUFFER_WIDTH || height>BACKBUFFER_HEIGHT) \r
684         {\r
685                 D3DPRESENT_PARAMETERS d3dparas;\r
686                 ZeroMemory(&d3dparas,sizeof(d3dparas));\r
687                 d3dparas.BackBufferWidth=width;\r
688                 d3dparas.BackBufferHeight=height;\r
689                 d3dparas.Windowed=TRUE;\r
690                 d3dparas.SwapEffect=D3DSWAPEFFECT_COPY;\r
691                 if (d3ddevice->CreateAdditionalSwapChain(&d3dparas,&swappy)!=D3D_OK){\r
692                         Log::getInstance()->log("OSD", Log::WARN, "Could not create Swap Chain!");\r
693                         //return 0;\r
694                 } else {\r
695                         swappy->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&swapsurf);\r
696                 }\r
697         Log::getInstance()->log("OSD", Log::INFO, "Create Additional Swap Chain %d %d!",width,height);\r
698         }\r
699 \r
700         dsallocator=dsall;\r
701         external_driving=true;\r
702         \r
703         EndPainting();*/\r
704 }\r
705 \r
706 void OsdOpenGL::Blank() {\r
707         BeginPainting();\r
708         glClearColor(0.15f, 0.25f, 0.35f, 1.0f); // change this to black after testing\r
709         glClear( GL_COLOR_BUFFER_BIT );\r
710         glClear( GL_DEPTH_BUFFER_BIT );\r
711         EndPainting();\r
712 }\r