]> git.vomp.tv Git - vompclient-marten.git/blob - osdopenvg.cc
Fix option numbers
[vompclient-marten.git] / osdopenvg.cc
1 /*
2     Copyright 2004-2005 Chris Tallon, 2006,2011-2012 Marten Richter
3
4     This file is part of VOMP.
5
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.
10
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.
15
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.
19 */
20
21
22 #include "osdopenvg.h"
23 #include "mtd.h"
24 #include "videoomx.h"
25 #include "surface.h"
26 #include <Magick++.h>
27
28 #include "message.h"
29 #include "command.h"
30 #include "teletxt/txtfont.h"
31
32 #include <sys/syscall.h>
33 #include <fontconfig/fontconfig.h>
34 #include <vector>
35 #include <math.h>
36 #include <bcm_host.h>
37
38 using namespace Magick;
39
40 extern uint8_t vdr_data[]     asm("_binary_other_vdrhires_jpg_start");
41 extern uint8_t vdr_data_end[] asm("_binary_other_vdrhires_jpg_end");
42 extern uint8_t wallpaper_data[]     asm("_binary_other_wallpaper720p_jpg_start");
43 extern uint8_t wallpaper_data_end[] asm("_binary_other_wallpaper720p_jpg_end");
44
45
46 #define  BACKBUFFER_WIDTH 1280
47 #define  BACKBUFFER_HEIGHT 720
48
49
50 OsdOpenVG::OsdOpenVG()
51 {
52   vgmutex.Lock();
53   taskmutex.Lock();
54   lastrendertime=getTimeMS();
55   display_height=0;
56   display_width=0;
57   mode=0;
58   aspect_correction=1.;
59
60   freetype_inited=false;
61   wait_id=1;
62   const char *fontname="Droid Sans:style=Regular";
63   cur_fontname=(char*)malloc(strlen(fontname)+1);
64   strcpy(cur_fontname,fontname);
65
66 }
67
68 OsdOpenVG::~OsdOpenVG()
69 {
70
71   if (initted)
72   {
73                 shutdown();
74   }
75
76   if (cur_fontname) free(cur_fontname);
77   if (freetype_inited) FT_Done_Face(ft_face);
78   if (!fontnames.size()) {
79           vector<char*>::iterator itty=fontnames.begin();
80           while (itty!=fontnames.end()) {
81                   free((void*)*itty);
82
83                   itty++;
84           }
85   }
86   if (!fontnames_keys.size()) {
87           vector<char*>::iterator itty=fontnames_keys.begin();
88           while (itty!=fontnames_keys.end()) {
89                   free((void*)*itty);
90
91                   itty++;
92           }
93     }
94
95   vgmutex.Unlock();
96   taskmutex.Unlock();
97 }
98
99
100
101 int OsdOpenVG::init(void* device)
102 {
103   if (initted) return 0;
104   Video* video = Video::getInstance();
105    //window=*((HWND*)device);
106
107    // May be this device specific part should go to a device specific child class
108
109    //init broadcom chipset (Move to video?)
110
111
112    //First get connection to egl
113    egl_display=eglGetDisplay(EGL_DEFAULT_DISPLAY);
114
115    if (egl_display==EGL_NO_DISPLAY) {
116            Log::getInstance()->log("OSD", Log::WARN, "Could not get egl display! %x",eglGetError());
117            vgmutex.Unlock();
118            return 0;
119    }
120
121
122
123    if (eglInitialize(egl_display, NULL, NULL)==EGL_FALSE) {
124            Log::getInstance()->log("OSD", Log::WARN, "Initialising display failed! %x",eglGetError());
125            vgmutex.Unlock();
126            return 0;
127    }
128
129
130    const char *query_str=eglQueryString(egl_display,EGL_CLIENT_APIS);
131    if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
132    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",eglGetError());
133    query_str=eglQueryString(egl_display,EGL_EXTENSIONS);
134    if (query_str)    Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
135    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",eglGetError());
136
137    if (eglBindAPI(EGL_OPENVG_API)==EGL_FALSE) {
138            Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api failed! %x",eglGetError());
139            vgmutex.Unlock();
140            return 0;
141    }
142
143    const EGLint attributs[]={
144                  EGL_RED_SIZE,8,EGL_GREEN_SIZE, 8,EGL_BLUE_SIZE, 8,EGL_ALPHA_SIZE, 8,
145          EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_PBUFFER_BIT,
146          EGL_CONFORMANT, EGL_OPENVG_BIT,
147          EGL_NONE
148    }; // Here, we might have to select the resolution!
149
150
151    EGLint number;
152
153    if (eglChooseConfig(egl_display, attributs, &egl_ourconfig, 1, &number)==EGL_FALSE) {
154            Log::getInstance()->log("OSD", Log::WARN, "Choosing egl config failed! %x",eglGetError());
155            vgmutex.Unlock();
156            return 0;
157    }
158
159
160
161    egl_context=eglCreateContext(egl_display,egl_ourconfig,NULL,NULL);
162    if (egl_context==EGL_NO_CONTEXT) {
163            Log::getInstance()->log("OSD", Log::WARN, "Creating egl context failed! %x",eglGetError());
164            vgmutex.Unlock();
165            return 0;
166    }
167
168    // warning broadcom specific, get display size!
169    display_width=display_height=0;
170    if (graphics_get_display_size(0, &display_width, &display_height)<0) {
171            Log::getInstance()->log("OSD", Log::WARN, "Getting display size failed! (BCM API) ");
172            vgmutex.Unlock();
173            return 0;
174    }
175    Log::getInstance()->log("OSD", Log::NOTICE, "Displaysize is %d x %d ",display_width, display_height);
176    VC_RECT_T dst_rect ={0,0,display_width,display_height};
177    VC_RECT_T src_rect={0,0,BACKBUFFER_WIDTH <<16,BACKBUFFER_HEIGHT<<16};
178   // VC_RECT_T src_rect_bg={0,0,1<<16,1<<16};
179  //  VC_RECT_T src_rect_im={0,0,1,1};
180
181   /* uint32_t back_image_ptr;
182    bcm_backres=vc_dispmanx_resource_create(VC_IMAGE_RGB888,1,1,&back_image_ptr);
183    unsigned int color=0x00FF0000;
184    vc_dispmanx_resource_write_data(bcm_backres,VC_IMAGE_RGB888,4,&color,&src_rect_im);*/
185
186    DISPMANX_UPDATE_HANDLE_T  bcm_update;
187    bcm_display=vc_dispmanx_display_open(0);
188    bcm_update=vc_dispmanx_update_start(0);
189    bcm_element=vc_dispmanx_element_add(bcm_update,bcm_display,
190          2,&dst_rect, 0,
191          &src_rect,DISPMANX_PROTECTION_NONE,0, 0, (DISPMANX_TRANSFORM_T) 0);
192
193
194  /*  bcm_background=vc_dispmanx_element_add(bcm_update,bcm_display,
195             0,&dst_rect,bcm_backres ,
196             &src_rect_bg,DISPMANX_PROTECTION_NONE,0, 0, (DISPMANX_TRANSFORM_T) 0);*/
197
198    vc_dispmanx_update_submit_sync(bcm_update);
199
200
201
202    static EGL_DISPMANX_WINDOW_T nativewindow;
203    nativewindow.element=bcm_element;
204    nativewindow.height=BACKBUFFER_HEIGHT;
205    nativewindow.width=BACKBUFFER_WIDTH;
206
207    egl_surface = eglCreateWindowSurface(egl_display,egl_ourconfig, &nativewindow,NULL );
208    if (egl_surface==EGL_NO_SURFACE) {
209            Log::getInstance()->log("OSD", Log::WARN, "Creating egl window surface failed!");
210            vgmutex.Unlock();
211            return 0;
212    }
213    Log::getInstance()->log("OSD", Log::DEBUG, "Making egl current in1%d",syscall(SYS_gettid));
214    if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {
215            Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed");
216            vgmutex.Unlock();
217                    return 0;
218    }
219    // Test stuff
220
221    query_str=(const char*)vgGetString(VG_VERSION) ;
222    if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
223    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",vgGetError());
224
225    query_str=(const char*)vgGetString(VG_VENDOR) ;
226    if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
227    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",vgGetError());
228
229    query_str=(const char*)vgGetString(VG_RENDERER) ;
230    if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
231    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",vgGetError());
232
233    query_str=(const char*)vgGetString(VG_EXTENSIONS) ;
234    if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
235    else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x",vgGetError());
236
237   aspect_correction= ((float)BACKBUFFER_HEIGHT)/576.f/(((float)BACKBUFFER_WIDTH)/720.f);
238   initPaths();
239   if (!fontnames.size()) {
240           //inspired by and copied from vdr's code
241           FcInit();
242           FcObjectSet *objset= FcObjectSetBuild(FC_FAMILY, FC_STYLE, NULL);
243           FcPattern * pattern=FcPatternCreate();
244           FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
245           FcFontSet* fonts = FcFontList(NULL, pattern, objset);
246           for (int i=0; i<fonts->nfont;i++) {
247                   char *s = (char *)FcNameUnparse(fonts->fonts[i]);
248
249                   if (s) {
250                           char *c= strchr(s,':');
251                           if (c) {
252                                   char *s2= strchr(c+1,',');
253                                   if (s2) *s2=0;
254                           }
255                           char *p=strchr(s,',');
256                           if (p) {
257                                   if (!c) *p=0;
258                                   else memmove(p,c,strlen(c)+1);
259                           }
260                           char *key=(char*)malloc(strlen(s)+1);
261                           strcpy(key,s);
262                           fontnames_keys.push_back(key);
263                           char *s2=strstr(s,"style=");
264                           if (s2) {
265                                   memmove(s2,s2+6,strlen(s2+6)+1);
266                           }
267                           fontnames.push_back(s);
268                   }
269           }
270           FcFontSetDestroy(fonts);
271           FcPatternDestroy(pattern);
272           FcObjectSetDestroy(objset);
273   }
274
275   if (!loadFont(false)) {
276           return 0;
277   }
278   vgttfont=vgCreateFont(0);
279   vgttpaint=vgCreatePaint();
280   vgSetParameteri( vgttpaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
281   vgSetColor(vgttpaint,0xffffffff);
282
283   eglSwapInterval(egl_display, 1 );
284
285   Log::getInstance()->log("OSD", Log::DEBUG, "Making egl current out 1%d",syscall(SYS_gettid));
286   eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
287   //Now we will create the Screen
288   initted = 1; // must set this here or create surface won't work
289
290
291   /*if (((VideoOMX*)Video::getInstance())->initUsingOSDObjects()!=1) { //call Video for init  stuff
292           return 0;
293   }*/
294   InitializeMagick("");
295
296   pthread_cond_init(&vgtaskCond, NULL);
297   pthread_mutex_init(&vgtaskCondMutex, NULL);
298
299   threadStart();
300   taskmutex.Unlock();
301   vgmutex.Unlock();
302
303
304   return 1;
305 }
306
307
308 void OsdOpenVG::initPaths()
309 {
310
311
312         VGPath current;
313         current=vgCreatePath(VG_PATH_FORMAT_STANDARD,
314                         VG_PATH_DATATYPE_F,1.f,0.f,
315                         0,0,VG_PATH_CAPABILITY_ALL);
316
317         vguLine(current,0.f,0.f,1.f,0.f);
318         // HorzLine
319         std_paths[HorzLine]=current;
320
321         current=vgCreatePath(VG_PATH_FORMAT_STANDARD,
322                                 VG_PATH_DATATYPE_F,1.f,0.f,
323                                 0,0,VG_PATH_CAPABILITY_ALL);
324         vguLine(current,0.f,0.f,0.f,1.f);
325         // VertLine
326         std_paths[VertLine]=current;
327
328         current=vgCreatePath(VG_PATH_FORMAT_STANDARD,
329                                 VG_PATH_DATATYPE_F,1.f,0.f,
330                                 0,0,VG_PATH_CAPABILITY_ALL);
331         //vguRect(current,0.f,0.f,1.f,1.f);
332         vguRect(current,0.f,0.f,1.f,1.f);
333         // Rectabgle
334         std_paths[Rectangle]=current;
335
336         current=vgCreatePath(VG_PATH_FORMAT_STANDARD,
337                                 VG_PATH_DATATYPE_F,1.f,0.f,
338                                 0,0,0);
339         vguEllipse(current,0.f,0.f,1.f,1.f);
340         // Point
341         std_paths[Point]=current;
342
343 }
344
345 void OsdOpenVG::destroyPaths()
346 {
347         vgDestroyPath(std_paths[HorzLine]);
348         vgDestroyPath(std_paths[VertLine]);
349         vgDestroyPath(std_paths[Rectangle]);
350         vgDestroyPath(std_paths[Point]);
351
352 }
353
354 int OsdOpenVG::stopUpdate()
355 {
356         threadStop();
357         processTasks();
358         return 1;
359 }
360
361 void OsdOpenVG::purgeAllReferences()
362 {
363         images_ref.clear();
364         styles_ref.clear(); // remove all references
365
366
367         map<void *,ImageIndex>::iterator mitty=monobitmaps.begin();
368         while (mitty!=monobitmaps.end()) {
369                 vgDestroyImage((VGImage)(*mitty).second);
370                 mitty++;
371         }
372         monobitmaps.clear();
373
374         map<string,ImageIndex>::iterator jitty=jpegs.begin();
375         while (jitty!=jpegs.end()) {
376                 vgDestroyImage((VGImage)(*jitty).second);
377                 jitty++;
378         }
379         jpegs.clear();
380
381         map<pair<Colour*,unsigned int>,unsigned int>::iterator sitty=styles.begin();
382         while (sitty!=styles.end()) {
383                 vgDestroyPaint((VGPaint)(*sitty).second);
384                 sitty++;
385         }
386         styles.clear();
387
388 }
389
390 int OsdOpenVG::shutdown()
391 {
392   if (!initted) return 0;
393
394   initted = 0;
395   Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark1");
396   threadStop();
397   Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark1a");
398   //(((VideoOMX*)Video::getInstance())->shutdownUsingOSDObjects());
399   if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {
400           Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed in shutdown %x",eglGetError());
401   }
402   if (eglBindAPI(EGL_OPENVG_API)==EGL_FALSE) {
403           Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api thread failed! %x",eglGetError());
404   }
405
406   Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark2");
407   processTasks();
408   Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark3");
409
410   taskmutex.Lock();
411   vgmutex.Lock();
412
413
414
415   //purgeAllReferences();
416
417   vgDestroyFont(vgfont);
418   vgDestroyFont(vgttfont);
419   vgDestroyPaint(vgttpaint);
420   destroyPaths();
421   float colclear[]={0.8f,0.8f,0.8f,1.f};
422   vgSetfv(VG_CLEAR_COLOR,4,colclear);
423   vgClear(0,0,BACKBUFFER_WIDTH,BACKBUFFER_HEIGHT);
424   eglSwapBuffers(egl_display, egl_surface);
425   Log::getInstance()->log("OSD", Log::DEBUG, "Making egl current out final");
426   eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
427   if (eglDestroySurface(egl_display,egl_surface)==EGL_FALSE) {
428           Log::getInstance()->log("OSD", Log::ERR, "eglDestroySurface failed %x",eglGetError());
429   }
430   if (eglDestroyContext(egl_display,egl_context)==EGL_FALSE) {
431           Log::getInstance()->log("OSD", Log::ERR, "eglDestroyContext failed %x",eglGetError());
432   }
433   if (eglTerminate(egl_display )==EGL_FALSE) {
434           Log::getInstance()->log("OSD", Log::ERR, "eglTerminate failed %x",eglGetError());
435   }
436
437   DISPMANX_UPDATE_HANDLE_T  bcm_update;
438   bcm_update=vc_dispmanx_update_start(0);
439
440   vc_dispmanx_element_remove(bcm_update,bcm_element);
441 //  vc_dispmanx_element_remove(bcm_update,bcm_background);
442   vc_dispmanx_update_submit_sync(bcm_update);
443 //  vc_dispmanx_resource_delete(bcm_backres);
444   vc_dispmanx_display_close(bcm_display);
445
446
447
448   return 1;
449 }
450
451
452
453
454 void OsdOpenVG::threadMethod()
455 {
456         // We have to claim the egl context for this thread
457         Log::getInstance()->log("OSD", Log::NOTICE, "Entering drawing thread");
458         if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {
459                 Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed in thread %x",eglGetError());
460                 return;
461         }
462         if (eglBindAPI(EGL_OPENVG_API)==EGL_FALSE) {
463                 Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api thread failed! %x",eglGetError());
464                 return;
465         }
466         int ts=0;
467         while (true)
468         {
469                 ts=1;
470                 unsigned int waittime=1;
471
472                 if (initted) {
473
474                         long long time1 = getTimeMS();
475                         if ((time1 - lastrendertime) > 200) {//5 fps for OSD updates are enough, avoids tearing
476                                 InternalRendering();
477                                 lastrendertime = getTimeMS();
478
479                         }
480                         if (processTasks()) ts=0;
481                 }
482                 if (!threadIsActive()) {
483                         if (eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT )== EGL_FALSE) {
484                                    Log::getInstance()->log("OSD", Log::WARN, "Making egl Current out thread failed");
485                         }
486                         threadCheckExit();
487                 }
488                 if (ts!=0) {
489                         struct timespec target_time;
490                         clock_gettime(CLOCK_REALTIME,&target_time);
491                         target_time.tv_nsec+=1000000LL*ts;
492                         if (target_time.tv_nsec>999999999) {
493                                 target_time.tv_nsec-=1000000000L;
494                                 target_time.tv_sec+=1;
495                         }
496                         threadLock();
497                         threadWaitForSignalTimed(&target_time);
498                         threadUnlock();
499                 }
500                 //Sleep(1);
501         }
502         eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
503 }
504
505
506 void OsdOpenVG::threadPostStopCleanup()
507 {
508         //Doing nothing
509         //goo;
510         Log::getInstance()->log("OSD", Log::NOTICE, "Exiting drawing thread");
511 }
512
513 void OsdOpenVG::InternalRendering(){
514         vgmutex.Lock();
515     float colclear[]={1.f,1.0f,1.f,1.f};
516     vgSetfv(VG_CLEAR_COLOR,4,colclear);
517         vgClear(0,0,BACKBUFFER_WIDTH,BACKBUFFER_HEIGHT);
518         vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
519
520
521         drawSurfaces(); //iterate through and draws all commands
522
523         //Show it to the user!
524         eglSwapBuffers(egl_display, egl_surface);
525         vgmutex.Unlock();
526
527 }
528
529 /*font stuff*/
530
531 float OsdOpenVG::getFontHeight()
532 {
533         return font_height; //dummy
534 }
535 float OsdOpenVG::getCharWidth(wchar_t c)
536 {
537         unsigned int glyph_index=FT_Get_Char_Index(ft_face,c);
538         return font_exp_x[glyph_index];
539 }
540
541 unsigned int OsdOpenVG::loadTTchar(cTeletextChar c)
542 {
543         unsigned int glyph_index=c.getGlyphIndex();
544         if (tt_font_chars.find(glyph_index)!=tt_font_chars.end())
545         {
546                 return glyph_index;
547         }
548
549         unsigned int buffer[10];
550         const VGfloat glyphOrigin[] = { 0.f, 0.f };
551         const VGfloat escapement[] = { 12.f, 0.f };
552         unsigned int * charmap = GetFontChar(c, buffer);
553         if (!charmap) { //invalid char
554                 return 0;
555         }
556         for (int i=0;i<10;i++) {
557                 buffer[i]=charmap[i]>>4;
558         }
559
560         VGImage handle = vgCreateImage(
561                         VG_A_8,
562                         12,
563                         10,
564                         VG_IMAGE_QUALITY_NONANTIALIASED | VG_IMAGE_QUALITY_FASTER
565                                         | VG_IMAGE_QUALITY_BETTER);
566         vgImageSubData(handle, buffer, 4, VG_A_1, 0, 0, 12, 10);
567         vgSetGlyphToImage(
568                         vgttfont,
569                         glyph_index,
570                         handle, (VGfloat*)glyphOrigin, (VGfloat*)escapement);
571         vgDestroyImage(handle);
572         tt_font_chars[glyph_index]=1;
573
574         return glyph_index;
575 }
576
577 int OsdOpenVG::getFontNames(const char *** names,const char *** names_keys)
578 {
579         *names=(const char**)&(fontnames[0]);
580         *names_keys=(const char**)&(fontnames_keys[0]);
581         return fontnames.size();
582 }
583
584 void OsdOpenVG::setFont(const char * fontname) {
585
586         if (strcmp(fontname,cur_fontname)) {
587                 // new font!
588                 if (cur_fontname) free(cur_fontname);
589                 cur_fontname=(char*)malloc(strlen(fontname)+1);
590                 strcpy(cur_fontname,fontname);
591
592                 struct OpenVGCommand comm;
593                 comm.task=OVGreplacefont;
594                 putOpenVGCommand(comm,false);
595
596
597         }
598 }
599
600
601 int  OsdOpenVG::loadFont(bool newfont)
602 {
603         int error;
604         float font_size=16.f;
605         if (!freetype_inited) {
606                 error=FT_Init_FreeType(&ft_library);
607                 if (error)
608                 {
609                         Log::getInstance()->log("OSD", Log::WARN, "Could not load freetype %x",error);
610                         return 0;
611                 }
612         }
613
614         if (!freetype_inited || newfont) {
615                 //first get the filename algorith extracted from vdr by Klaus Schmidinger
616                 FcInit();
617                 FcPattern *pattern=FcNameParse((FcChar8*)cur_fontname);
618                 FcPatternAddBool(pattern,FC_SCALABLE,FcTrue);
619                 FcConfigSubstitute(NULL,pattern,FcMatchPattern);
620                 FcDefaultSubstitute(pattern);
621                 FcResult fres;
622                 FcFontSet *fonts=FcFontSort(NULL,pattern,FcFalse,NULL,&fres);
623                 FcChar8 *filename=NULL;
624                 if (fonts) {
625                         for (int i=0;i<fonts->nfont;i++) {
626                                 FcBool canscale;
627                                 FcPatternGetBool(fonts->fonts[i],FC_SCALABLE,0,&canscale);
628                                 if (canscale){
629                                         FcPatternGetString(fonts->fonts[i],FC_FILE,0,&filename);
630                                         break;
631                                 }
632                         }
633                         FcFontSetDestroy(fonts);
634                 } else {
635                         Log::getInstance()->log("OSD", Log::CRIT, "Could not locate a font! Abort!");
636                         return 0;
637                 }
638                 FcPatternDestroy(pattern);
639
640                 Log::getInstance()->log("OSD", Log::NOTICE, "Load Font %s: %s", cur_fontname,filename);
641                 // second load the font
642                 FT_Face     new_ft_face;
643                 error=FT_New_Face(ft_library,(const char*)filename,0,&new_ft_face );
644                 if (error) {
645                         Log::getInstance()->log("OSD", Log::WARN, "Could not load font face %x %s",error,filename);
646                         return 0;
647                 }
648                 error=FT_Set_Char_Size(new_ft_face,0,font_size*256,0,0 /*dpi*/);
649                 if (error) {
650                         FT_Done_Face(new_ft_face);
651                         Log::getInstance()->log("OSD", Log::WARN, "Could not set  face size %x",error);
652                         return 0;
653                 }
654                 FT_Face old_ft_face=ft_face; // do it thread safe
655                 ft_face=new_ft_face;
656                 if (freetype_inited) FT_Done_Face(old_ft_face);//
657                 freetype_inited=true;
658         }
659         vgfont=vgCreateFont(0);
660         FT_ULong cur_char;
661         FT_UInt glyph;
662         font_height=ft_face->size->metrics.height/256.f;
663         cur_char = FT_Get_First_Char(ft_face,&glyph);
664         vector<VGubyte> segments;
665         vector<VGfloat> coord;
666         segments.reserve(256);
667         coord.reserve(1024);
668         //Log::getInstance()->log("OSD", Log::DEBUG, "Create Glyph test %d %x %x %d",cur_char,font_data_end,font_data,glyph);
669         while (glyph !=0)
670         {
671                 error=FT_Load_Glyph(ft_face,glyph,FT_LOAD_DEFAULT);
672                 if (error){
673                         FT_Done_Face(ft_face);
674                         Log::getInstance()->log("OSD", Log::WARN, "Could not load glyph %x",error);
675                         return 0;
676                 }
677                 VGPath path;
678                 FT_Outline ot=ft_face->glyph->outline;
679                 segments.clear();
680                 coord.clear();
681
682                 if (ot.n_contours ==0) {
683                         path=VG_INVALID_HANDLE;
684                 } else {
685                         path=vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F,
686                                         1.0f,0.f,0,0,VG_PATH_CAPABILITY_ALL);
687
688                         /* convert glyph */
689                         FT_Vector *pt=ot.points;
690                         const char *tags=ot.tags;
691                         const short* cont=ot.contours;
692                         short n_cont=ot.n_contours;
693                         short n_point=ot.n_points;
694                         short last_cont=0;
695                         for (short point=0;n_cont!=0;cont++,n_cont--) {
696                                 short next_cont=*cont+1;
697                                 bool first=true;
698                                 char last_tag=0;
699                                 short first_point=point;
700                                 //Log::getInstance()->log("OSD", Log::DEBUG, "runs %d",*cont);
701                                 for (;point<next_cont;point++) {
702                                         char tag=tags[point];
703                                         FT_Vector fpoint=pt[point];
704                                 //      Log::getInstance()->log("OSD", Log::DEBUG, "tag %d point %d %d: %d %d",tag,fpoint.x,fpoint.y,point,n_point);
705                                         if (first) {
706                                                 segments.push_back(VG_MOVE_TO);
707                                                 first=false;
708                                         } else if (tag &0x1) { //on curve
709                                                 if (last_tag &0x1) {
710                                                         segments.push_back(VG_LINE_TO);
711                                                 } else if (last_tag &0x2){
712                                                         segments.push_back(VG_CUBIC_TO);
713                                                 } else {
714                                                         segments.push_back(VG_QUAD_TO);
715                                                 }
716
717                                         } else {
718                                                 if (!(tag &0x2)){
719                                                         if (!(last_tag &0x1)) {
720                                                                 segments.push_back(VG_QUAD_TO);
721                                                                 int coord_size=coord.size();
722                                                                 VGfloat x=(coord[coord_size-2]+ ((float)fpoint.x)/256.f)*0.5f*aspect_correction;
723                                                                 VGfloat y=(coord[coord_size-1]+(font_size- ((float)fpoint.y)/256.f))*0.5f;
724                                                                 coord.push_back(x);
725                                                                 coord.push_back(y);
726                                                         }
727                                                 }
728
729
730                                         }
731                                         last_tag=tag;
732                                         coord.push_back(((float)fpoint.x)*aspect_correction/256.);
733                                         coord.push_back(font_size-((float)fpoint.y)/256.);
734                                         //Log::getInstance()->log("OSD", Log::DEBUG, "Create APD Glyph coord %d %d %g %g",fpoint.x,fpoint.y,coord[coord.size()-2],coord[coord.size()-1]);
735                                 }
736                                 if (!(last_tag &0x1)) {
737                                         if (last_tag &0x2) {
738                                                 segments.push_back(VG_CUBIC_TO);
739                                         } else {
740                                                 segments.push_back(VG_QUAD_TO);
741                                         }
742                                         coord.push_back(((float)pt[first_point].x)*aspect_correction/256.);
743                                         coord.push_back(font_size-((float)pt[first_point].y)/256.);
744                                 }
745                                 //segments.push_back(VG_CLOSE_PATH);
746
747
748                         }
749                         vgAppendPathData(path,segments.size(),&segments[0],&coord[0]);
750                         int n=0;
751                 /*      for (int m=0;m<segments.size();m++) {
752                                 switch (segments[m])
753                                 {
754                                 case VG_MOVE_TO:
755                                         Log::getInstance()->log("OSD", Log::DEBUG, "Move To %g %g",coord[n],coord[n+1]);n+=2; break;
756                                 case VG_LINE_TO:
757                                         Log::getInstance()->log("OSD", Log::DEBUG, "Line To %g %g",coord[n],coord[n+1]);n+=2; break;
758                                 case VG_CUBIC_TO:
759                                         Log::getInstance()->log("OSD", Log::DEBUG, "Cubic To %g %g %g %g %g %g",coord[n],coord[n+1],coord[n+2],coord[n+3],coord[n+4],coord[n+5]);n+=6; break;
760                                 case VG_QUAD_TO:
761                                         Log::getInstance()->log("OSD", Log::DEBUG, "Quad To %g %g %g %g",coord[n],coord[n+1],coord[n+2],coord[n+3]);n+=4; break;
762                                 case VG_CLOSE_PATH:
763                                         Log::getInstance()->log("OSD", Log::DEBUG, "Close Path"); break;
764                                 }
765
766                         }*/
767                         //vguRect(path,0.f,0.f,1.f,1.f);
768                         //Log::getInstance()->log("OSD", Log::DEBUG, "Create APD Glyph %d %x",segments.size(),vgGetError());
769                 }
770                 VGfloat ori[]={0.f,0.f};
771                 VGfloat esp[]={ft_face->glyph->advance.x/256.f*aspect_correction,ft_face->glyph->advance.y/256.f};
772                 font_exp_x[glyph]=ft_face->glyph->advance.x/256.f*aspect_correction; //recalculate
773
774                 vgSetGlyphToPath(vgfont,glyph,path,VG_FALSE,ori,esp);
775                 //Log::getInstance()->log("OSD", Log::DEBUG, "Create Glyph %d %d %x",path,glyph,vgGetError());
776                 if (path!=VG_INVALID_HANDLE) {
777                         vgDestroyPath(path);
778                 }
779                 cur_char = FT_Get_Next_Char(ft_face,cur_char,&glyph);
780         }
781         return 1;
782 }
783
784
785 void OsdOpenVG::drawSetTrans(SurfaceCommands & sc)
786 {
787         vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
788         vgLoadIdentity();
789         vgScale(((float)BACKBUFFER_WIDTH)/720.f, -((float)BACKBUFFER_HEIGHT)/576.f);
790         vgTranslate(0.f+sc.x,-576.f+sc.y);
791
792         vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
793         vgLoadIdentity();
794     vgScale(((float)BACKBUFFER_WIDTH)/720.f, -((float)BACKBUFFER_HEIGHT)/576.f);
795         vgTranslate(0.f+sc.x,-576.f+sc.y);
796
797         vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
798         vgLoadIdentity();
799         vgScale(((float)BACKBUFFER_WIDTH)/720.f, -((float)BACKBUFFER_HEIGHT)/576.f);
800         vgTranslate(0.f+sc.x,-576.f+sc.y);
801
802
803
804         //vgTranslate(0.f+sc.x,576.f-sc.y);
805         //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Translate %g %g",sc.x,sc.y);
806
807 }
808 void OsdOpenVG::executeDrawCommand(SVGCommand & command)
809 {
810
811         VGfloat  save_matrix[9];
812         switch (command.instr) {
813         case DrawPath: {
814         vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
815         //      VGuint rgba;
816         //      rgba = vgGetColor((VGPaint) command.reference);
817                 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Path %d %x %g %g %g %g",command.reference,command.target.path_index,command.x,command.y,command.w,command.h);
818                 //vgSeti(VG_FILL_RULE,);
819
820                 vgGetMatrix(save_matrix);
821                 vgSetPaint((VGPaint) command.reference,VG_FILL_PATH);
822                 vgSetPaint((VGPaint) command.reference,VG_STROKE_PATH);
823                 vgTranslate(command.x,command.y);
824                 vgScale(command.w,command.h);
825                 vgDrawPath(std_paths[command.target.path_index],VG_FILL_PATH);
826                 vgLoadMatrix(save_matrix);
827         } break;
828         case DrawImage: {
829             vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
830                 vgGetMatrix(save_matrix);
831                 vgTranslate(command.x,command.y);
832                 //vgScale(command.w,command.h);
833                 if (command.reference) { //special behaviout for bw images they act as a mask on the current paint
834                         vgSetPaint((VGPaint) command.reference,VG_FILL_PATH);
835                         vgSetPaint((VGPaint) command.reference,VG_STROKE_PATH);
836                         vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_STENCIL);
837                         vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
838                         vgScale(aspect_correction,1.f);
839                 } else {
840                         VGfloat imagewidth=vgGetParameteri((VGImage) command.target.image, VG_IMAGE_WIDTH);
841                         VGfloat imageheight=vgGetParameteri((VGImage) command.target.image, VG_IMAGE_HEIGHT);
842                         //vgScale(720.f/((float)BACKBUFFER_WIDTH), 576.f/((float)BACKBUFFER_HEIGHT));
843                         float scalex=command.w/imagewidth;
844                         float scaley=command.h/imageheight;
845                         //vgScale(command.w/imagewidth,command.h/imageheight);
846                         vgScale(scalex,scaley);
847                         vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_NORMAL);
848                         //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Image Scale  %g %g %g %g %g %g",command.w,imagewidth,command.h,imageheight,scalex,scaley);
849                 }
850
851
852                 //vgLoadIdentity();
853                 //vgTranslate(200.f,500.f);
854                 //vgScale(100.f,100.f);
855
856                 vgDrawImage((VGImage) command.target.image);
857                 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Image %d %x %g %g %g %g %x",command.reference,command.target.image,command.x,command.y,command.w,command.h,
858                 //              vgGetError());
859                 if (command.reference) {
860                         vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_NORMAL);
861                         vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
862                 }
863                 vgLoadMatrix(save_matrix);
864         } break;
865         case DrawGlyph: {
866                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
867                 vgGetMatrix(save_matrix);
868                 vgSetPaint((VGPaint) command.reference,VG_FILL_PATH);
869                 vgSetPaint((VGPaint) command.reference,VG_STROKE_PATH);
870                 vgTranslate(command.x,command.y);
871                 VGfloat gori[]={0.,0.};
872                 vgSetfv(VG_GLYPH_ORIGIN,2,gori);
873
874                 unsigned int glyph_index=FT_Get_Char_Index(ft_face,command.target.textchar);
875                 vgDrawGlyph(vgfont,glyph_index,VG_FILL_PATH,VG_FALSE);
876                 //vgDrawPath(std_paths[Rectangle],VG_FILL_PATH);
877         /*      Log::getInstance()->log("OSD", Log::DEBUG, "Draw Glyph %d %c %d %g %g %x",command.reference,command.target.textchar,glyph_index,command.x,command.y,
878                                                 vgGetError());*/
879                 vgLoadMatrix(save_matrix);
880                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
881         } break;
882         case DrawTTchar:{
883                 cTeletextChar tchar;
884                 tchar.setInternal(command.target.ttchar);
885                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
886                 vgGetMatrix(save_matrix);
887                 enumTeletextColor ttforegcolour=tchar.GetFGColor();
888                 enumTeletextColor ttbackgcolour=tchar.GetBGColor();
889             if (tchar.GetBoxedOut()) {
890                 ttforegcolour=ttcTransparent;
891                 ttbackgcolour=ttcTransparent;
892             }
893             vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_STENCIL);
894             vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
895
896
897                 vgTranslate(command.x+command.w*11.85f*1.4f,command.y+command.h*19.75f);
898                 VGfloat gori[]={0.,0.};
899                 vgSetfv(VG_GLYPH_ORIGIN,2,gori);
900
901                 vgScale(-1.4f,2.f);
902                 unsigned int color=Surface::enumTeletextColorToCoulour(ttbackgcolour).rgba();
903                 color=color<<8 | (color &0xff000000)>>24;
904                 vgSetColor(vgttpaint,color);
905                 vgSetPaint((VGPaint) vgttpaint,VG_FILL_PATH);
906                 vgSetPaint((VGPaint) vgttpaint,VG_STROKE_PATH);
907                 cTeletextChar filled;
908                 filled.setInternal(0x187f);
909                 unsigned int glyph_index=loadTTchar(filled);
910                 vgDrawGlyph(vgttfont,glyph_index,VG_FILL_PATH,VG_FALSE);
911
912                 color=Surface::enumTeletextColorToCoulour(ttforegcolour).rgba();
913                 color=color<<8 | (color &0xff000000)>>24;
914                 vgSetColor(vgttpaint,color);
915                 vgSetPaint((VGPaint) vgttpaint,VG_FILL_PATH);
916                 vgSetPaint((VGPaint) vgttpaint,VG_STROKE_PATH);
917                 glyph_index=loadTTchar(tchar);
918                 vgDrawGlyph(vgttfont,glyph_index,VG_FILL_PATH,VG_FALSE);
919
920                 /*      Log::getInstance()->log("OSD", Log::DEBUG, "Draw TTchar %x %x %x %x",glyph_index,ttforegcolour,Surface::enumTeletextColorToCoulour(ttforegcolour).rgba(),
921                                         vgGetColor(vgttpaint));*/
922
923
924                 vgLoadMatrix(save_matrix);
925                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
926                 vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_NORMAL);
927                 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
928
929         }break;
930         }
931 }
932
933 unsigned int OsdOpenVG::handleTask(OpenVGCommand& command)
934 {
935         switch (command.task){
936         case OVGreplacefont: {
937                  vgDestroyFont(vgfont);
938                  loadFont(true);
939                  return 0;
940         } break;
941
942         case OVGdestroyImageRef: {
943                 vgDestroyImage((VGImage)command.param1);
944                 return 0;
945         } break;
946         case OVGdestroyPaint: {
947                 Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint Destroy %d ",command.param1);
948                 vgDestroyPaint((VGPaint)command.param1);
949                 return 0;
950         } break;
951         case OVGcreateImagePalette: {
952                 VGImage input=vgCreateImage(VG_A_8,command.param1, command.param2,
953                                 VG_IMAGE_QUALITY_NONANTIALIASED|
954                                 VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
955                 vgImageSubData(input,command.data,command.param1,
956                                                         VG_A_8,0,0,command.param1, command.param2); // upload palettized image data
957                 VGImage handle=vgCreateImage(VG_sRGBA_8888,command.param1, command.param2,
958                                                 VG_IMAGE_QUALITY_NONANTIALIASED|
959                                                 VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
960                 VGuint *palette=(VGuint*)malloc(256*sizeof(VGuint));
961                 VGuint *in_palette=(VGuint*)command.data2;
962                 for (int i=0;i<256;i++) {
963                         VGuint color=in_palette[i];
964                         palette[i]=color<<8 | (color &0xff000000)>>24;
965                 }
966
967                 vgLookupSingle(handle,input,palette,VG_ALPHA,VG_FALSE,VG_FALSE);
968                 free(palette);
969                 vgDestroyImage(input);
970
971                 return handle;
972         } break;
973         case OVGcreateMonoBitmap: {
974                 VGImage handle=vgCreateImage(VG_A_1,command.param1, command.param2,
975                                         VG_IMAGE_QUALITY_NONANTIALIASED|
976                                         VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
977                 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create mono  %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
978                 unsigned int buffer_len=(command.param1*command.param2)>>3;
979                 unsigned char * buffer=(unsigned char*)malloc(buffer_len);
980                 unsigned char * r_buffer1=buffer;
981                 const unsigned char * r_buffer2=(const unsigned char *)command.data;
982                 unsigned char *buffer_end=buffer+buffer_len;
983                 while (r_buffer1!=buffer_end) {
984                         unsigned char byte=*r_buffer2;
985                         *r_buffer1=((byte * 0x0802LU & 0x22110LU) | (byte * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
986                         r_buffer1++;r_buffer2++;
987                 }
988
989
990                 vgImageSubData(handle,buffer,command.param1>>3,
991                                         VG_A_1,0,0,command.param1, command.param2);
992                 free(buffer);
993         //      Log::getInstance()->log("OSD", Log::DEBUG, "Draw create mono2  %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
994                 return handle;
995         } break;
996         case OVGcreateImageFile: {
997                 VGImage handle;
998                 try{
999                         Image *imagefile=(Image*)command.data;
1000                         Blob imageblob;
1001                         imagefile->write(&imageblob,"RGBA");
1002
1003
1004                         handle=vgCreateImage(VG_sXBGR_8888,imagefile->columns(),imagefile->rows(),
1005                                         VG_IMAGE_QUALITY_NONANTIALIASED|
1006                                         VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
1007                 //      Log::getInstance()->log("OSD", Log::DEBUG, "Draw create image details  %d %d %x mark1",imagefile->columns(),imagefile->rows(),*(unsigned int*)imageblob.data());
1008                         vgImageSubData(handle,imageblob.data(),imagefile->columns()*4,
1009                                         VG_sXBGR_8888,0,0,imagefile->columns(),imagefile->rows());
1010                 //      Log::getInstance()->log("OSD", Log::DEBUG, "Draw create image details  %d %d %x mark2",imagefile->columns(),imagefile->rows(),*(unsigned int*)imageblob.data());
1011                         delete imagefile;
1012                 }catch( Exception &error_ )
1013                 {
1014                         Log::getInstance()->log("OSD", Log::DEBUG, "Libmagick hT: %s",error_.what());
1015
1016                         return 0;
1017                 }
1018
1019                 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create file  %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1020                 return handle;
1021         } break;
1022         case OVGcreateColorRef :{
1023                 VGPaint handle;
1024                 handle=vgCreatePaint();
1025                 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
1026                 vgSetColor(handle,command.param1);
1027                 VGuint rgba;
1028                 rgba = vgGetColor((VGPaint)handle);
1029                 Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint %d %x %x",handle,command.param1,rgba);
1030                 return handle;
1031         } break;
1032         case OVGimageUploadLine: {
1033                 vgImageSubData((VGImage)command.param1,command.data,0,VG_sARGB_8888,0,command.param2,command.param3,1);
1034                 return 0;
1035         } break;
1036
1037         }
1038 }
1039
1040 bool OsdOpenVG::processTasks()
1041 {
1042         bool worked=false;
1043         taskmutex.Lock();
1044         vgmutex.Lock();
1045         while (vgcommands.size()>0)
1046         {
1047                 OpenVGCommand &comm=vgcommands.front();
1048                 OpenVGResponse resp;
1049                 resp.result=handleTask(comm);
1050                 resp.id=comm.id;
1051                 if (comm.id) {
1052                         vgresponses.push_back(resp);
1053                 }
1054                 vgcommands.pop_front();
1055                 taskmutex.Unlock();
1056                 vgmutex.Unlock();
1057                 //threadCheckExit();
1058                 pthread_mutex_lock(&vgtaskCondMutex);
1059                 pthread_cond_signal(&vgtaskCond);
1060                 pthread_mutex_unlock(&vgtaskCondMutex);
1061                 taskmutex.Lock();
1062                 vgmutex.Lock();
1063                 worked=true;
1064         }
1065         taskmutex.Unlock();
1066         vgmutex.Unlock();
1067
1068         return worked;
1069 }
1070
1071 bool OsdOpenVG::haveOpenVGResponse(unsigned int id,unsigned int * resp)
1072 {
1073         taskmutex.Lock();
1074         if (vgresponses.size()>0)
1075         {
1076                 deque<OpenVGResponse>::iterator itty=vgresponses.begin();
1077                 while (itty!=vgresponses.end())
1078                 {
1079                         if ((*itty).id==id) {
1080                                 *resp=(*itty).result;
1081                                 taskmutex.Unlock();
1082                                 return true;
1083                         }
1084                         itty++;
1085                 }
1086         }
1087         taskmutex.Unlock();
1088         return false;
1089 }
1090
1091
1092 unsigned int  OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm,bool wait)
1093 {
1094         taskmutex.Lock();
1095         if (wait){
1096                 comm.id=wait_id;
1097                 wait_id++;
1098         } else {
1099                 comm.id=0; // we are not waiting
1100         }
1101         vgcommands.push_back(comm);
1102         taskmutex.Unlock();
1103         threadSignal();
1104         while (wait) {
1105                 unsigned int resp;
1106                 if (!haveOpenVGResponse(comm.id,&resp)) {
1107                         struct timespec target_time;
1108                         clock_gettime(CLOCK_REALTIME,&target_time);
1109                         target_time.tv_nsec+=1000000LL*100;
1110                         if (target_time.tv_nsec>999999999) {
1111                                         target_time.tv_nsec-=1000000000L;
1112                                         target_time.tv_sec+=1;
1113                         }
1114                         pthread_mutex_lock(&vgtaskCondMutex);
1115                         pthread_cond_timedwait(&vgtaskCond, &vgtaskCondMutex,&target_time);
1116                         pthread_mutex_unlock(&vgtaskCondMutex);
1117                 } else {
1118                         return resp;
1119                 }
1120         }
1121         return 0;
1122 }
1123
1124 void OsdOpenVG::imageUploadLine(ImageIndex index,unsigned int j,unsigned int width,void *data)
1125 {
1126         vgImageSubData((VGImage)index,data,0,VG_sARGB_8888,0,j,width,1);
1127
1128         struct OpenVGCommand comm;
1129         comm.task=OVGimageUploadLine;
1130         comm.param1=index;
1131         comm.param2=j;
1132         comm.param3=width;
1133         comm.data=data;
1134         putOpenVGCommand(comm,true);
1135 }
1136
1137 void OsdOpenVG::destroyImageRef(ImageIndex index)
1138 {
1139         struct OpenVGCommand comm;
1140         comm.task=OVGdestroyImageRef;
1141         comm.param1=index;
1142         putOpenVGCommand(comm,false);
1143 }
1144
1145 ImageIndex OsdOpenVG::createJpeg(const char* fileName, int *width,int *height)
1146 {
1147         Image* magicimage=NULL;
1148         bool mem=false;
1149         struct OpenVGCommand comm;
1150         comm.task=OVGcreateImageFile;
1151
1152         try{
1153                 // Now figure out, if it is a special case
1154                 if (strcmp(fileName,"/vdr.jpg")==0) {
1155                         magicimage=new Image(Blob(vdr_data,vdr_data_end-vdr_data));
1156                         *height=100; // this is faked so that the system does use the old coordinate system
1157                         *width=ceil(190.f*aspect_correction);
1158                         Log::getInstance()->log("OSD", Log::DEBUG, "LoadIm vdr.jpg");
1159                 } else if (strcmp(fileName,"/wallpaperPAL.jpg")==0) {
1160                         magicimage=new Image(Blob(wallpaper_data,wallpaper_data_end-wallpaper_data));
1161                         *width=720; // this is faked so that the system does use the old coordinate system
1162                         *height=576;
1163                         Log::getInstance()->log("OSD", Log::DEBUG, "LoadIm wallpaperPAL.jpg");
1164                 } else {
1165                         magicimage=new Image();
1166                         magicimage->read(fileName);
1167                         Log::getInstance()->log("OSD", Log::DEBUG, "LoadIm file: %s",fileName);
1168                         *width=ceil(magicimage->baseColumns()*aspect_correction); // this is faked so that the system does use the old coordinate system
1169                         *height=magicimage->baseRows();
1170                 }
1171
1172         }catch( Exception &error_ )
1173         {
1174                 Log::getInstance()->log("OSD", Log::DEBUG, "Libmagick: %s",error_.what());
1175
1176                 return 0;
1177         }
1178         comm.data=magicimage;
1179         return putOpenVGCommand(comm,true);
1180 }
1181
1182 ImageIndex OsdOpenVG::createMonoBitmap(void *base,int width,int height)
1183 {
1184         struct OpenVGCommand comm;
1185         comm.task=OVGcreateMonoBitmap;
1186         comm.param1=width;
1187         comm.param2=height;
1188         comm.data=base;
1189         return putOpenVGCommand(comm,true);
1190 }
1191
1192 ImageIndex OsdOpenVG::createImagePalette(int width,int height,const unsigned char *image_data,const unsigned int*palette_data)
1193 {
1194     struct OpenVGCommand comm;
1195     comm.task=OVGcreateImagePalette;
1196     comm.param1=width;
1197     comm.param2=height;
1198     comm.data=image_data;
1199     comm.data2=palette_data;
1200     return putOpenVGCommand(comm,true);
1201 }
1202
1203 void OsdOpenVG::destroyStyleRef(unsigned int index)
1204 {
1205         struct OpenVGCommand comm;
1206         comm.task=OVGdestroyPaint;
1207         comm.param1=index;
1208         putOpenVGCommand(comm,false);
1209 }
1210
1211 unsigned int OsdOpenVG::createStyleRef(const DrawStyle &c)
1212 {
1213         unsigned int col=c.rgba();
1214         struct OpenVGCommand comm;
1215         comm.task=OVGcreateColorRef;
1216         comm.param1=col<<8 | (col &0xff000000)>>24;
1217         comm.data=&c;
1218         return putOpenVGCommand(comm,true);
1219 }
1220
1221 unsigned int OsdOpenVG::createColorRef(const Colour &c)
1222 {
1223         unsigned int col=c.rgba();
1224         struct OpenVGCommand comm;
1225         comm.task=OVGcreateColorRef;
1226         comm.param1=col<<8 | (col &0xff000000)>>24;
1227         comm.data=&c;
1228         return putOpenVGCommand(comm,true);
1229 }
1230
1231