]> git.vomp.tv Git - vompclient-marten.git/blob - osdopenvg.cc
Detect mpeg audio frame header changes, and wait at least 4 packets after a change...
[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         VGfloat  save_matrix2[9];
813         switch (command.instr) {
814         case DrawPath: {
815         vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
816         //      VGuint rgba;
817         //      rgba = vgGetColor((VGPaint) command.reference);
818                 //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);
819                 //vgSeti(VG_FILL_RULE,);
820
821                 vgGetMatrix(save_matrix);
822                 vgSetPaint((VGPaint) command.reference,VG_FILL_PATH);
823                 vgSetPaint((VGPaint) command.reference,VG_STROKE_PATH);
824                 vgTranslate(command.x,command.y);
825                 vgScale(command.w,command.h);
826                 vgDrawPath(std_paths[command.target.path_index],VG_FILL_PATH);
827                 vgLoadMatrix(save_matrix);
828         } break;
829         case DrawImage: {
830             vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
831                 vgGetMatrix(save_matrix);
832                 vgTranslate(command.x,command.y);
833
834
835
836
837                 //vgScale(command.w,command.h);
838                 VGfloat imagewidth=vgGetParameteri((VGImage) command.target.image, VG_IMAGE_WIDTH);
839                 VGfloat imageheight=vgGetParameteri((VGImage) command.target.image, VG_IMAGE_HEIGHT);
840                 if (command.reference) { //special behaviout for bw images they act as a mask on the current paint
841                         vgSetPaint((VGPaint) command.reference,VG_FILL_PATH);
842                         vgSetPaint((VGPaint) command.reference,VG_STROKE_PATH);
843                         vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_STENCIL);
844                         vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
845                         vgScale(aspect_correction,1.f);
846                         vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
847                     vgGetMatrix(save_matrix2);
848                     vgScale(imagewidth,imageheight);
849                 } else {
850
851                         //vgScale(720.f/((float)BACKBUFFER_WIDTH), 576.f/((float)BACKBUFFER_HEIGHT));
852                         float scalex=command.w/imagewidth;
853                         float scaley=command.h/imageheight;
854                         //vgScale(command.w/imagewidth,command.h/imageheight);
855                         vgScale(scalex,scaley);
856                         vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_NORMAL);
857                         //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Image Scale  %g %g %g %g %g %g",command.w,imagewidth,command.h,imageheight,scalex,scaley);
858                 }
859
860
861                 //vgLoadIdentity();
862                 //vgTranslate(200.f,500.f);
863                 //vgScale(100.f,100.f);
864
865                 vgDrawImage((VGImage) command.target.image);
866                 //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,
867                 //              vgGetError());
868                 if (command.reference) {
869                         vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_NORMAL);
870                         vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
871                         vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
872                         vgLoadMatrix(save_matrix2);
873                 }
874
875                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
876                 vgLoadMatrix(save_matrix);
877         } break;
878         case DrawGlyph: {
879                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
880                 vgGetMatrix(save_matrix);
881                 vgSetPaint((VGPaint) command.reference,VG_FILL_PATH);
882                 vgSetPaint((VGPaint) command.reference,VG_STROKE_PATH);
883                 vgTranslate(command.x,command.y);
884                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
885                 vgGetMatrix(save_matrix2);
886                 unsigned int glyph_index=FT_Get_Char_Index(ft_face,command.target.textchar);
887             vgScale(font_exp_x[glyph_index],font_height);
888
889
890                 VGfloat gori[]={0.,0.};
891                 vgSetfv(VG_GLYPH_ORIGIN,2,gori);
892
893
894                 vgDrawGlyph(vgfont,glyph_index,VG_FILL_PATH,VG_FALSE);
895                 //vgDrawPath(std_paths[Rectangle],VG_FILL_PATH);
896         /*      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,
897                                                 vgGetError());*/
898                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
899                 vgLoadMatrix(save_matrix);
900                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
901                 vgLoadMatrix(save_matrix2);
902
903                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
904         } break;
905         case DrawTTchar:{
906                 cTeletextChar tchar;
907                 tchar.setInternal(command.target.ttchar);
908                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
909                 vgGetMatrix(save_matrix);
910                 enumTeletextColor ttforegcolour=tchar.GetFGColor();
911                 enumTeletextColor ttbackgcolour=tchar.GetBGColor();
912             if (tchar.GetBoxedOut()) {
913                 ttforegcolour=ttcTransparent;
914                 ttbackgcolour=ttcTransparent;
915             }
916             vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_STENCIL);
917             vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
918
919
920                 vgTranslate(command.x+command.w*11.85f*1.4f,command.y+command.h*19.75f);
921                 VGfloat gori[]={0.,0.};
922                 vgSetfv(VG_GLYPH_ORIGIN,2,gori);
923
924                 vgScale(-1.4f,2.f);
925                 unsigned int color=Surface::enumTeletextColorToCoulour(ttbackgcolour).rgba();
926                 color=color<<8 | (color &0xff000000)>>24;
927                 vgSetColor(vgttpaint,color);
928                 vgSetPaint((VGPaint) vgttpaint,VG_FILL_PATH);
929                 vgSetPaint((VGPaint) vgttpaint,VG_STROKE_PATH);
930                 cTeletextChar filled;
931                 filled.setInternal(0x187f);
932                 unsigned int glyph_index=loadTTchar(filled);
933                 vgDrawGlyph(vgttfont,glyph_index,VG_FILL_PATH,VG_FALSE);
934
935                 color=Surface::enumTeletextColorToCoulour(ttforegcolour).rgba();
936                 color=color<<8 | (color &0xff000000)>>24;
937                 vgSetColor(vgttpaint,color);
938                 vgSetPaint((VGPaint) vgttpaint,VG_FILL_PATH);
939                 vgSetPaint((VGPaint) vgttpaint,VG_STROKE_PATH);
940                 glyph_index=loadTTchar(tchar);
941                 vgDrawGlyph(vgttfont,glyph_index,VG_FILL_PATH,VG_FALSE);
942
943                 /*      Log::getInstance()->log("OSD", Log::DEBUG, "Draw TTchar %x %x %x %x",glyph_index,ttforegcolour,Surface::enumTeletextColorToCoulour(ttforegcolour).rgba(),
944                                         vgGetColor(vgttpaint));*/
945
946
947                 vgLoadMatrix(save_matrix);
948                 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
949                 vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_NORMAL);
950                 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
951
952         }break;
953         }
954 }
955
956 unsigned int OsdOpenVG::handleTask(OpenVGCommand& command)
957 {
958         switch (command.task){
959         case OVGreplacefont: {
960                  vgDestroyFont(vgfont);
961                  loadFont(true);
962                  return 0;
963         } break;
964
965         case OVGdestroyImageRef: {
966                 vgDestroyImage((VGImage)command.param1);
967                 return 0;
968         } break;
969         case OVGdestroyPaint: {
970                 Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint Destroy %d ",command.param1);
971                 vgDestroyPaint((VGPaint)command.param1);
972                 return 0;
973         } break;
974         case OVGcreateImagePalette: {
975                 VGImage input=vgCreateImage(VG_A_8,command.param1, command.param2,
976                                 VG_IMAGE_QUALITY_NONANTIALIASED|
977                                 VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
978                 vgImageSubData(input,command.data,command.param1,
979                                                         VG_A_8,0,0,command.param1, command.param2); // upload palettized image data
980                 VGImage handle=vgCreateImage(VG_sRGBA_8888,command.param1, command.param2,
981                                                 VG_IMAGE_QUALITY_NONANTIALIASED|
982                                                 VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
983                 VGuint *palette=(VGuint*)malloc(256*sizeof(VGuint));
984                 VGuint *in_palette=(VGuint*)command.data2;
985                 for (int i=0;i<256;i++) {
986                         VGuint color=in_palette[i];
987                         palette[i]=color<<8 | (color &0xff000000)>>24;
988                 }
989
990                 vgLookupSingle(handle,input,palette,VG_ALPHA,VG_FALSE,VG_FALSE);
991                 free(palette);
992                 vgDestroyImage(input);
993
994                 return handle;
995         } break;
996         case OVGcreateMonoBitmap: {
997                 VGImage handle=vgCreateImage(VG_A_1,command.param1, command.param2,
998                                         VG_IMAGE_QUALITY_NONANTIALIASED|
999                                         VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
1000                 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create mono  %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1001                 unsigned int buffer_len=(command.param1*command.param2)>>3;
1002                 unsigned char * buffer=(unsigned char*)malloc(buffer_len);
1003                 unsigned char * r_buffer1=buffer;
1004                 const unsigned char * r_buffer2=(const unsigned char *)command.data;
1005                 unsigned char *buffer_end=buffer+buffer_len;
1006                 while (r_buffer1!=buffer_end) {
1007                         unsigned char byte=*r_buffer2;
1008                         *r_buffer1=((byte * 0x0802LU & 0x22110LU) | (byte * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
1009                         r_buffer1++;r_buffer2++;
1010                 }
1011
1012
1013                 vgImageSubData(handle,buffer,command.param1>>3,
1014                                         VG_A_1,0,0,command.param1, command.param2);
1015                 free(buffer);
1016         //      Log::getInstance()->log("OSD", Log::DEBUG, "Draw create mono2  %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1017                 return handle;
1018         } break;
1019         case OVGcreateImageFile: {
1020                 VGImage handle;
1021                 try{
1022                         Image *imagefile=(Image*)command.data;
1023                         Blob imageblob;
1024                         imagefile->write(&imageblob,"RGBA");
1025
1026
1027                         handle=vgCreateImage(VG_sXBGR_8888,imagefile->columns(),imagefile->rows(),
1028                                         VG_IMAGE_QUALITY_NONANTIALIASED|
1029                                         VG_IMAGE_QUALITY_FASTER|VG_IMAGE_QUALITY_BETTER);
1030                 //      Log::getInstance()->log("OSD", Log::DEBUG, "Draw create image details  %d %d %x mark1",imagefile->columns(),imagefile->rows(),*(unsigned int*)imageblob.data());
1031                         vgImageSubData(handle,imageblob.data(),imagefile->columns()*4,
1032                                         VG_sXBGR_8888,0,0,imagefile->columns(),imagefile->rows());
1033                 //      Log::getInstance()->log("OSD", Log::DEBUG, "Draw create image details  %d %d %x mark2",imagefile->columns(),imagefile->rows(),*(unsigned int*)imageblob.data());
1034                         delete imagefile;
1035                 }catch( Exception &error_ )
1036                 {
1037                         Log::getInstance()->log("OSD", Log::DEBUG, "Libmagick hT: %s",error_.what());
1038
1039                         return 0;
1040                 }
1041
1042                 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create file  %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1043                 return handle;
1044         } break;
1045         case OVGcreateColorRef :{
1046                 VGPaint handle;
1047                 handle=vgCreatePaint();
1048                 DrawStyle *style=(DrawStyle*)command.data;
1049                 switch (style->ft) {
1050                 case DrawStyle::Color: {
1051                         vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
1052                         vgSetColor(handle,command.param1);
1053                         VGuint rgba;
1054                         rgba = vgGetColor((VGPaint)handle);
1055                         Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint %d %x %x",handle,command.param1,rgba);
1056                 } break;
1057                 case DrawStyle::GradientLinear: {
1058                         vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_LINEAR_GRADIENT);
1059                         VGfloat params[]={style->x1,style->y1,style->x2,style->y2,style->r};
1060                         Log::getInstance()->log("OSD", Log::DEBUG, "Draw Gradient %d %g %g %g %g",handle,params[0],params[1],params[2],params[3]);
1061                         vgSetParameterfv(handle,VG_PAINT_LINEAR_GRADIENT,4,params);
1062
1063
1064
1065                 } break;
1066                 case DrawStyle::GradientRadial: {
1067                         vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_RADIAL_GRADIENT);
1068                         VGfloat params[]={style->x1,style->y1,style->x2,style->y2,style->r};
1069                         vgSetParameterfv(handle,VG_PAINT_RADIAL_GRADIENT,5,params);
1070
1071
1072
1073                 } break;
1074                 };
1075                 if (style->ft==DrawStyle::GradientLinear ||style->ft==DrawStyle::GradientRadial) {
1076                         VGfloat colorramp[5*5];
1077                         colorramp[0+0*5]=0.f;
1078                         colorramp[1+0*5]=((float)style->red)/255.f;
1079                         colorramp[2+0*5]=((float)style->green)/255.f;
1080                         colorramp[3+0*5]=((float)style->blue)/255.f;
1081                         colorramp[4+0*5]=((float)style->alpha)/255.f;
1082                         for (int i=0;i<(style->num_colors-1);i++) {
1083                                 colorramp[0+(i+1)*5]=style->grad_pos[i];
1084                                 colorramp[1+(i+1)*5]=((float)style->grad_col[i].red)/255.f;
1085                                 colorramp[2+(i+1)*5]=((float)style->grad_col[i].green)/255.f;
1086                                 colorramp[3+(i+1)*5]=((float)style->grad_col[i].blue)/255.f;
1087                                 colorramp[4+(i+1)*5]=((float)style->grad_col[i].alpha)/255.f;
1088                         }
1089                         colorramp[0+(style->num_colors)*5]=1.f;
1090                         colorramp[1+(style->num_colors)*5]=((float)style->grad_col[style->num_colors-1].red)/255.f;
1091                         colorramp[2+(style->num_colors)*5]=((float)style->grad_col[style->num_colors-1].green)/255.f;
1092                         colorramp[3+(style->num_colors)*5]=((float)style->grad_col[style->num_colors-1].blue)/255.f;
1093                         colorramp[4+(style->num_colors)*5]=((float)style->grad_col[style->num_colors-1].alpha)/255.f;
1094                         vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_SPREAD_MODE,VG_COLOR_RAMP_SPREAD_REFLECT);
1095                         vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_PREMULTIPLIED,VG_FALSE);
1096                         vgSetParameterfv(handle,VG_PAINT_COLOR_RAMP_STOPS,5+(style->num_colors)*5,colorramp);
1097                 }
1098
1099                 return handle;
1100         } break;
1101         case OVGimageUploadLine: {
1102                 vgImageSubData((VGImage)command.param1,command.data,0,VG_sARGB_8888,0,command.param2,command.param3,1);
1103                 return 0;
1104         } break;
1105
1106         }
1107 }
1108
1109 bool OsdOpenVG::processTasks()
1110 {
1111         bool worked=false;
1112         taskmutex.Lock();
1113         vgmutex.Lock();
1114         while (vgcommands.size()>0)
1115         {
1116                 OpenVGCommand &comm=vgcommands.front();
1117                 OpenVGResponse resp;
1118                 resp.result=handleTask(comm);
1119                 resp.id=comm.id;
1120                 if (comm.id) {
1121                         vgresponses.push_back(resp);
1122                 }
1123                 vgcommands.pop_front();
1124                 taskmutex.Unlock();
1125                 vgmutex.Unlock();
1126                 //threadCheckExit();
1127                 pthread_mutex_lock(&vgtaskCondMutex);
1128                 pthread_cond_signal(&vgtaskCond);
1129                 pthread_mutex_unlock(&vgtaskCondMutex);
1130                 taskmutex.Lock();
1131                 vgmutex.Lock();
1132                 worked=true;
1133         }
1134         taskmutex.Unlock();
1135         vgmutex.Unlock();
1136
1137         return worked;
1138 }
1139
1140 bool OsdOpenVG::haveOpenVGResponse(unsigned int id,unsigned int * resp)
1141 {
1142         taskmutex.Lock();
1143         if (vgresponses.size()>0)
1144         {
1145                 deque<OpenVGResponse>::iterator itty=vgresponses.begin();
1146                 while (itty!=vgresponses.end())
1147                 {
1148                         if ((*itty).id==id) {
1149                                 *resp=(*itty).result;
1150                                 taskmutex.Unlock();
1151                                 return true;
1152                         }
1153                         itty++;
1154                 }
1155         }
1156         taskmutex.Unlock();
1157         return false;
1158 }
1159
1160
1161 unsigned int  OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm,bool wait)
1162 {
1163         taskmutex.Lock();
1164         if (wait){
1165                 comm.id=wait_id;
1166                 wait_id++;
1167         } else {
1168                 comm.id=0; // we are not waiting
1169         }
1170         vgcommands.push_back(comm);
1171         taskmutex.Unlock();
1172         threadSignal();
1173         while (wait) {
1174                 unsigned int resp;
1175                 if (!haveOpenVGResponse(comm.id,&resp)) {
1176                         struct timespec target_time;
1177                         clock_gettime(CLOCK_REALTIME,&target_time);
1178                         target_time.tv_nsec+=1000000LL*100;
1179                         if (target_time.tv_nsec>999999999) {
1180                                         target_time.tv_nsec-=1000000000L;
1181                                         target_time.tv_sec+=1;
1182                         }
1183                         pthread_mutex_lock(&vgtaskCondMutex);
1184                         pthread_cond_timedwait(&vgtaskCond, &vgtaskCondMutex,&target_time);
1185                         pthread_mutex_unlock(&vgtaskCondMutex);
1186                 } else {
1187                         return resp;
1188                 }
1189         }
1190         return 0;
1191 }
1192
1193 void OsdOpenVG::imageUploadLine(ImageIndex index,unsigned int j,unsigned int width,void *data)
1194 {
1195         vgImageSubData((VGImage)index,data,0,VG_sARGB_8888,0,j,width,1);
1196
1197         struct OpenVGCommand comm;
1198         comm.task=OVGimageUploadLine;
1199         comm.param1=index;
1200         comm.param2=j;
1201         comm.param3=width;
1202         comm.data=data;
1203         putOpenVGCommand(comm,true);
1204 }
1205
1206 void OsdOpenVG::destroyImageRef(ImageIndex index)
1207 {
1208         struct OpenVGCommand comm;
1209         comm.task=OVGdestroyImageRef;
1210         comm.param1=index;
1211         putOpenVGCommand(comm,false);
1212 }
1213
1214 ImageIndex OsdOpenVG::createJpeg(const char* fileName, int *width,int *height)
1215 {
1216         Image* magicimage=NULL;
1217         bool mem=false;
1218         struct OpenVGCommand comm;
1219         comm.task=OVGcreateImageFile;
1220
1221         try{
1222                 // Now figure out, if it is a special case
1223                 if (strcmp(fileName,"/vdr.jpg")==0) {
1224                         magicimage=new Image(Blob(vdr_data,vdr_data_end-vdr_data));
1225                         *height=100; // this is faked so that the system does use the old coordinate system
1226                         *width=ceil(190.f*aspect_correction);
1227                         Log::getInstance()->log("OSD", Log::DEBUG, "LoadIm vdr.jpg");
1228                 } else if (strcmp(fileName,"/wallpaperPAL.jpg")==0) {
1229                         magicimage=new Image(Blob(wallpaper_data,wallpaper_data_end-wallpaper_data));
1230                         *width=720; // this is faked so that the system does use the old coordinate system
1231                         *height=576;
1232                         Log::getInstance()->log("OSD", Log::DEBUG, "LoadIm wallpaperPAL.jpg");
1233                 } else {
1234                         magicimage=new Image();
1235                         magicimage->read(fileName);
1236                         Log::getInstance()->log("OSD", Log::DEBUG, "LoadIm file: %s",fileName);
1237                         *width=ceil(magicimage->baseColumns()*aspect_correction); // this is faked so that the system does use the old coordinate system
1238                         *height=magicimage->baseRows();
1239                 }
1240
1241         }catch( Exception &error_ )
1242         {
1243                 Log::getInstance()->log("OSD", Log::DEBUG, "Libmagick: %s",error_.what());
1244
1245                 return 0;
1246         }
1247         comm.data=magicimage;
1248         return putOpenVGCommand(comm,true);
1249 }
1250
1251 ImageIndex OsdOpenVG::createMonoBitmap(void *base,int width,int height)
1252 {
1253         struct OpenVGCommand comm;
1254         comm.task=OVGcreateMonoBitmap;
1255         comm.param1=width;
1256         comm.param2=height;
1257         comm.data=base;
1258         return putOpenVGCommand(comm,true);
1259 }
1260
1261 ImageIndex OsdOpenVG::createImagePalette(int width,int height,const unsigned char *image_data,const unsigned int*palette_data)
1262 {
1263     struct OpenVGCommand comm;
1264     comm.task=OVGcreateImagePalette;
1265     comm.param1=width;
1266     comm.param2=height;
1267     comm.data=image_data;
1268     comm.data2=palette_data;
1269     return putOpenVGCommand(comm,true);
1270 }
1271
1272 void OsdOpenVG::destroyStyleRef(unsigned int index)
1273 {
1274         struct OpenVGCommand comm;
1275         comm.task=OVGdestroyPaint;
1276         comm.param1=index;
1277         putOpenVGCommand(comm,false);
1278 }
1279
1280 unsigned int OsdOpenVG::createStyleRef(const DrawStyle &c)
1281 {
1282         unsigned int col=c.rgba();
1283         struct OpenVGCommand comm;
1284         comm.task=OVGcreateColorRef;
1285         comm.param1=col<<8 | (col &0xff000000)>>24;
1286         comm.data=&c;
1287         return putOpenVGCommand(comm,true);
1288 }
1289
1290 unsigned int OsdOpenVG::createColorRef(const Colour &c)
1291 {
1292         unsigned int col=c.rgba();
1293         struct OpenVGCommand comm;
1294         comm.task=OVGcreateColorRef;
1295         comm.param1=col<<8 | (col &0xff000000)>>24;
1296         comm.data=&c;
1297         return putOpenVGCommand(comm,true);
1298 }
1299
1300