2 Copyright 2004-2005 Chris Tallon, 2006,2011-2012 Marten Richter
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "osdopenvg.h"
30 #include "teletxt/txtfont.h"
32 #include <sys/syscall.h>
33 #include <fontconfig/fontconfig.h>
38 using namespace Magick;
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");
46 #define BACKBUFFER_WIDTH 1280
47 #define BACKBUFFER_HEIGHT 720
50 OsdOpenVG::OsdOpenVG()
54 lastrendertime=getTimeMS();
60 freetype_inited=false;
62 const char *fontname="Droid Sans:style=Regular";
63 cur_fontname=(char*)malloc(strlen(fontname)+1);
64 strcpy(cur_fontname,fontname);
68 OsdOpenVG::~OsdOpenVG()
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()) {
86 if (!fontnames_keys.size()) {
87 vector<char*>::iterator itty=fontnames_keys.begin();
88 while (itty!=fontnames_keys.end()) {
101 int OsdOpenVG::init(void* device)
103 if (initted) return 0;
104 Video* video = Video::getInstance();
105 //window=*((HWND*)device);
107 // May be this device specific part should go to a device specific child class
109 //init broadcom chipset (Move to video?)
112 //First get connection to egl
113 egl_display=eglGetDisplay(EGL_DEFAULT_DISPLAY);
115 if (egl_display==EGL_NO_DISPLAY) {
116 Log::getInstance()->log("OSD", Log::WARN, "Could not get egl display! %x",eglGetError());
123 if (eglInitialize(egl_display, NULL, NULL)==EGL_FALSE) {
124 Log::getInstance()->log("OSD", Log::WARN, "Initialising display failed! %x",eglGetError());
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());
137 if (eglBindAPI(EGL_OPENVG_API)==EGL_FALSE) {
138 Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api failed! %x",eglGetError());
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,
148 }; // Here, we might have to select the resolution!
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());
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());
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) ");
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};
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);*/
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,
191 &src_rect,DISPMANX_PROTECTION_NONE,0, 0, (DISPMANX_TRANSFORM_T) 0);
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);*/
198 vc_dispmanx_update_submit_sync(bcm_update);
202 static EGL_DISPMANX_WINDOW_T nativewindow;
203 nativewindow.element=bcm_element;
204 nativewindow.height=BACKBUFFER_HEIGHT;
205 nativewindow.width=BACKBUFFER_WIDTH;
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!");
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");
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());
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());
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());
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());
237 aspect_correction= ((float)BACKBUFFER_HEIGHT)/576.f/(((float)BACKBUFFER_WIDTH)/720.f);
239 if (!fontnames.size()) {
240 //inspired by and copied from vdr's code
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]);
250 char *c= strchr(s,':');
252 char *s2= strchr(c+1,',');
255 char *p=strchr(s,',');
258 else memmove(p,c,strlen(c)+1);
260 char *key=(char*)malloc(strlen(s)+1);
262 fontnames_keys.push_back(key);
263 char *s2=strstr(s,"style=");
265 memmove(s2,s2+6,strlen(s2+6)+1);
267 fontnames.push_back(s);
270 FcFontSetDestroy(fonts);
271 FcPatternDestroy(pattern);
272 FcObjectSetDestroy(objset);
275 if (!loadFont(false)) {
278 vgttfont=vgCreateFont(0);
279 vgttpaint=vgCreatePaint();
280 vgSetParameteri( vgttpaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
281 vgSetColor(vgttpaint,0xffffffff);
283 eglSwapInterval(egl_display, 1 );
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
291 /*if (((VideoOMX*)Video::getInstance())->initUsingOSDObjects()!=1) { //call Video for init stuff
294 InitializeMagick("");
296 pthread_cond_init(&vgtaskCond, NULL);
297 pthread_mutex_init(&vgtaskCondMutex, NULL);
308 void OsdOpenVG::initPaths()
313 current=vgCreatePath(VG_PATH_FORMAT_STANDARD,
314 VG_PATH_DATATYPE_F,1.f,0.f,
315 0,0,VG_PATH_CAPABILITY_ALL);
317 vguLine(current,0.f,0.f,1.f,0.f);
319 std_paths[HorzLine]=current;
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);
326 std_paths[VertLine]=current;
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);
334 std_paths[Rectangle]=current;
336 current=vgCreatePath(VG_PATH_FORMAT_STANDARD,
337 VG_PATH_DATATYPE_F,1.f,0.f,
339 vguEllipse(current,0.f,0.f,1.f,1.f);
341 std_paths[Point]=current;
345 void OsdOpenVG::destroyPaths()
347 vgDestroyPath(std_paths[HorzLine]);
348 vgDestroyPath(std_paths[VertLine]);
349 vgDestroyPath(std_paths[Rectangle]);
350 vgDestroyPath(std_paths[Point]);
354 int OsdOpenVG::stopUpdate()
361 void OsdOpenVG::purgeAllReferences()
364 styles_ref.clear(); // remove all references
367 map<void *,ImageIndex>::iterator mitty=monobitmaps.begin();
368 while (mitty!=monobitmaps.end()) {
369 vgDestroyImage((VGImage)(*mitty).second);
374 map<string,ImageIndex>::iterator jitty=jpegs.begin();
375 while (jitty!=jpegs.end()) {
376 vgDestroyImage((VGImage)(*jitty).second);
381 map<pair<Colour*,unsigned int>,unsigned int>::iterator sitty=styles.begin();
382 while (sitty!=styles.end()) {
383 vgDestroyPaint((VGPaint)(*sitty).second);
390 int OsdOpenVG::shutdown()
392 if (!initted) return 0;
395 Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark1");
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());
402 if (eglBindAPI(EGL_OPENVG_API)==EGL_FALSE) {
403 Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api thread failed! %x",eglGetError());
406 Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark2");
408 Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark3");
415 //purgeAllReferences();
417 vgDestroyFont(vgfont);
418 vgDestroyFont(vgttfont);
419 vgDestroyPaint(vgttpaint);
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());
430 if (eglDestroyContext(egl_display,egl_context)==EGL_FALSE) {
431 Log::getInstance()->log("OSD", Log::ERR, "eglDestroyContext failed %x",eglGetError());
433 if (eglTerminate(egl_display )==EGL_FALSE) {
434 Log::getInstance()->log("OSD", Log::ERR, "eglTerminate failed %x",eglGetError());
437 DISPMANX_UPDATE_HANDLE_T bcm_update;
438 bcm_update=vc_dispmanx_update_start(0);
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);
454 void OsdOpenVG::threadMethod()
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());
462 if (eglBindAPI(EGL_OPENVG_API)==EGL_FALSE) {
463 Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api thread failed! %x",eglGetError());
470 unsigned int waittime=1;
474 long long time1 = getTimeMS();
475 if ((time1 - lastrendertime) > 200) {//5 fps for OSD updates are enough, avoids tearing
477 lastrendertime = getTimeMS();
480 if (processTasks()) ts=0;
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");
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;
497 threadWaitForSignalTimed(&target_time);
502 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
506 void OsdOpenVG::threadPostStopCleanup()
510 Log::getInstance()->log("OSD", Log::NOTICE, "Exiting drawing thread");
513 void OsdOpenVG::InternalRendering(){
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);
521 drawSurfaces(); //iterate through and draws all commands
523 //Show it to the user!
524 eglSwapBuffers(egl_display, egl_surface);
531 float OsdOpenVG::getFontHeight()
533 return font_height; //dummy
535 float OsdOpenVG::getCharWidth(wchar_t c)
537 unsigned int glyph_index=FT_Get_Char_Index(ft_face,c);
538 return font_exp_x[glyph_index];
541 unsigned int OsdOpenVG::loadTTchar(cTeletextChar c)
543 unsigned int glyph_index=c.getGlyphIndex();
544 if (tt_font_chars.find(glyph_index)!=tt_font_chars.end())
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
556 for (int i=0;i<10;i++) {
557 buffer[i]=charmap[i]>>4;
560 VGImage handle = vgCreateImage(
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);
570 handle, (VGfloat*)glyphOrigin, (VGfloat*)escapement);
571 vgDestroyImage(handle);
572 tt_font_chars[glyph_index]=1;
577 int OsdOpenVG::getFontNames(const char *** names,const char *** names_keys)
579 *names=(const char**)&(fontnames[0]);
580 *names_keys=(const char**)&(fontnames_keys[0]);
581 return fontnames.size();
584 void OsdOpenVG::setFont(const char * fontname) {
586 if (strcmp(fontname,cur_fontname)) {
588 if (cur_fontname) free(cur_fontname);
589 cur_fontname=(char*)malloc(strlen(fontname)+1);
590 strcpy(cur_fontname,fontname);
592 struct OpenVGCommand comm;
593 comm.task=OVGreplacefont;
594 putOpenVGCommand(comm,false);
601 int OsdOpenVG::loadFont(bool newfont)
604 float font_size=16.f;
605 if (!freetype_inited) {
606 error=FT_Init_FreeType(&ft_library);
609 Log::getInstance()->log("OSD", Log::WARN, "Could not load freetype %x",error);
614 if (!freetype_inited || newfont) {
615 //first get the filename algorith extracted from vdr by Klaus Schmidinger
617 FcPattern *pattern=FcNameParse((FcChar8*)cur_fontname);
618 FcPatternAddBool(pattern,FC_SCALABLE,FcTrue);
619 FcConfigSubstitute(NULL,pattern,FcMatchPattern);
620 FcDefaultSubstitute(pattern);
622 FcFontSet *fonts=FcFontSort(NULL,pattern,FcFalse,NULL,&fres);
623 FcChar8 *filename=NULL;
625 for (int i=0;i<fonts->nfont;i++) {
627 FcPatternGetBool(fonts->fonts[i],FC_SCALABLE,0,&canscale);
629 FcPatternGetString(fonts->fonts[i],FC_FILE,0,&filename);
633 FcFontSetDestroy(fonts);
635 Log::getInstance()->log("OSD", Log::CRIT, "Could not locate a font! Abort!");
638 FcPatternDestroy(pattern);
640 Log::getInstance()->log("OSD", Log::NOTICE, "Load Font %s: %s", cur_fontname,filename);
641 // second load the font
643 error=FT_New_Face(ft_library,(const char*)filename,0,&new_ft_face );
645 Log::getInstance()->log("OSD", Log::WARN, "Could not load font face %x %s",error,filename);
648 error=FT_Set_Char_Size(new_ft_face,0,font_size*256,0,0 /*dpi*/);
650 FT_Done_Face(new_ft_face);
651 Log::getInstance()->log("OSD", Log::WARN, "Could not set face size %x",error);
654 FT_Face old_ft_face=ft_face; // do it thread safe
656 if (freetype_inited) FT_Done_Face(old_ft_face);//
657 freetype_inited=true;
659 vgfont=vgCreateFont(0);
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);
668 //Log::getInstance()->log("OSD", Log::DEBUG, "Create Glyph test %d %x %x %d",cur_char,font_data_end,font_data,glyph);
671 error=FT_Load_Glyph(ft_face,glyph,FT_LOAD_DEFAULT);
673 FT_Done_Face(ft_face);
674 Log::getInstance()->log("OSD", Log::WARN, "Could not load glyph %x",error);
678 FT_Outline ot=ft_face->glyph->outline;
682 if (ot.n_contours ==0) {
683 path=VG_INVALID_HANDLE;
685 path=vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F,
686 1.0f,0.f,0,0,VG_PATH_CAPABILITY_ALL);
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;
695 for (short point=0;n_cont!=0;cont++,n_cont--) {
696 short next_cont=*cont+1;
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);
706 segments.push_back(VG_MOVE_TO);
708 } else if (tag &0x1) { //on curve
710 segments.push_back(VG_LINE_TO);
711 } else if (last_tag &0x2){
712 segments.push_back(VG_CUBIC_TO);
714 segments.push_back(VG_QUAD_TO);
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;
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]);
736 if (!(last_tag &0x1)) {
738 segments.push_back(VG_CUBIC_TO);
740 segments.push_back(VG_QUAD_TO);
742 coord.push_back(((float)pt[first_point].x)*aspect_correction/256.);
743 coord.push_back(font_size-((float)pt[first_point].y)/256.);
745 //segments.push_back(VG_CLOSE_PATH);
749 vgAppendPathData(path,segments.size(),&segments[0],&coord[0]);
751 /* for (int m=0;m<segments.size();m++) {
755 Log::getInstance()->log("OSD", Log::DEBUG, "Move To %g %g",coord[n],coord[n+1]);n+=2; break;
757 Log::getInstance()->log("OSD", Log::DEBUG, "Line To %g %g",coord[n],coord[n+1]);n+=2; break;
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;
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;
763 Log::getInstance()->log("OSD", Log::DEBUG, "Close Path"); break;
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());
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
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) {
779 cur_char = FT_Get_Next_Char(ft_face,cur_char,&glyph);
785 void OsdOpenVG::drawSetTrans(SurfaceCommands & sc)
787 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
789 vgScale(((float)BACKBUFFER_WIDTH)/720.f, -((float)BACKBUFFER_HEIGHT)/576.f);
790 vgTranslate(0.f+sc.x,-576.f+sc.y);
792 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
794 vgScale(((float)BACKBUFFER_WIDTH)/720.f, -((float)BACKBUFFER_HEIGHT)/576.f);
795 vgTranslate(0.f+sc.x,-576.f+sc.y);
797 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
799 vgScale(((float)BACKBUFFER_WIDTH)/720.f, -((float)BACKBUFFER_HEIGHT)/576.f);
800 vgTranslate(0.f+sc.x,-576.f+sc.y);
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);
808 void OsdOpenVG::executeDrawCommand(SVGCommand & command)
811 VGfloat save_matrix[9];
812 VGfloat save_matrix2[9];
813 switch (command.instr) {
815 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
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,);
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);
830 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
831 vgGetMatrix(save_matrix);
832 vgTranslate(command.x,command.y);
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);
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);
862 //vgTranslate(200.f,500.f);
863 //vgScale(100.f,100.f);
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,
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);
875 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
876 vgLoadMatrix(save_matrix);
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);
890 VGfloat gori[]={0.,0.};
891 vgSetfv(VG_GLYPH_ORIGIN,2,gori);
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,
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);
903 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
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;
916 vgSeti(VG_IMAGE_MODE,VG_DRAW_IMAGE_STENCIL);
917 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
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);
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);
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);
943 /* Log::getInstance()->log("OSD", Log::DEBUG, "Draw TTchar %x %x %x %x",glyph_index,ttforegcolour,Surface::enumTeletextColorToCoulour(ttforegcolour).rgba(),
944 vgGetColor(vgttpaint));*/
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);
956 unsigned int OsdOpenVG::handleTask(OpenVGCommand& command)
958 switch (command.task){
959 case OVGreplacefont: {
960 vgDestroyFont(vgfont);
965 case OVGdestroyImageRef: {
966 vgDestroyImage((VGImage)command.param1);
969 case OVGdestroyPaint: {
970 Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint Destroy %d ",command.param1);
971 vgDestroyPaint((VGPaint)command.param1);
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;
990 vgLookupSingle(handle,input,palette,VG_ALPHA,VG_FALSE,VG_FALSE);
992 vgDestroyImage(input);
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++;
1013 vgImageSubData(handle,buffer,command.param1>>3,
1014 VG_A_1,0,0,command.param1, command.param2);
1016 // Log::getInstance()->log("OSD", Log::DEBUG, "Draw create mono2 %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1019 case OVGcreateImageFile: {
1022 Image *imagefile=(Image*)command.data;
1024 imagefile->write(&imageblob,"RGBA");
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());
1035 }catch( Exception &error_ )
1037 Log::getInstance()->log("OSD", Log::DEBUG, "Libmagick hT: %s",error_.what());
1042 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create file %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1045 case OVGcreateColorRef :{
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);
1054 rgba = vgGetColor((VGPaint)handle);
1055 Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint %d %x %x",handle,command.param1,rgba);
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);
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);
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;
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);
1101 case OVGimageUploadLine: {
1102 vgImageSubData((VGImage)command.param1,command.data,0,VG_sARGB_8888,0,command.param2,command.param3,1);
1109 bool OsdOpenVG::processTasks()
1114 while (vgcommands.size()>0)
1116 OpenVGCommand &comm=vgcommands.front();
1117 OpenVGResponse resp;
1118 resp.result=handleTask(comm);
1121 vgresponses.push_back(resp);
1123 vgcommands.pop_front();
1126 //threadCheckExit();
1127 pthread_mutex_lock(&vgtaskCondMutex);
1128 pthread_cond_signal(&vgtaskCond);
1129 pthread_mutex_unlock(&vgtaskCondMutex);
1140 bool OsdOpenVG::haveOpenVGResponse(unsigned int id,unsigned int * resp)
1143 if (vgresponses.size()>0)
1145 deque<OpenVGResponse>::iterator itty=vgresponses.begin();
1146 while (itty!=vgresponses.end())
1148 if ((*itty).id==id) {
1149 *resp=(*itty).result;
1161 unsigned int OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm,bool wait)
1168 comm.id=0; // we are not waiting
1170 vgcommands.push_back(comm);
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;
1183 pthread_mutex_lock(&vgtaskCondMutex);
1184 pthread_cond_timedwait(&vgtaskCond, &vgtaskCondMutex,&target_time);
1185 pthread_mutex_unlock(&vgtaskCondMutex);
1193 void OsdOpenVG::imageUploadLine(ImageIndex index,unsigned int j,unsigned int width,void *data)
1195 vgImageSubData((VGImage)index,data,0,VG_sARGB_8888,0,j,width,1);
1197 struct OpenVGCommand comm;
1198 comm.task=OVGimageUploadLine;
1203 putOpenVGCommand(comm,true);
1206 void OsdOpenVG::destroyImageRef(ImageIndex index)
1208 struct OpenVGCommand comm;
1209 comm.task=OVGdestroyImageRef;
1211 putOpenVGCommand(comm,false);
1214 ImageIndex OsdOpenVG::createJpeg(const char* fileName, int *width,int *height)
1216 Image* magicimage=NULL;
1218 struct OpenVGCommand comm;
1219 comm.task=OVGcreateImageFile;
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
1232 Log::getInstance()->log("OSD", Log::DEBUG, "LoadIm wallpaperPAL.jpg");
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();
1241 }catch( Exception &error_ )
1243 Log::getInstance()->log("OSD", Log::DEBUG, "Libmagick: %s",error_.what());
1247 comm.data=magicimage;
1248 return putOpenVGCommand(comm,true);
1251 ImageIndex OsdOpenVG::createMonoBitmap(void *base,int width,int height)
1253 struct OpenVGCommand comm;
1254 comm.task=OVGcreateMonoBitmap;
1258 return putOpenVGCommand(comm,true);
1261 ImageIndex OsdOpenVG::createImagePalette(int width,int height,const unsigned char *image_data,const unsigned int*palette_data)
1263 struct OpenVGCommand comm;
1264 comm.task=OVGcreateImagePalette;
1267 comm.data=image_data;
1268 comm.data2=palette_data;
1269 return putOpenVGCommand(comm,true);
1272 void OsdOpenVG::destroyStyleRef(unsigned int index)
1274 struct OpenVGCommand comm;
1275 comm.task=OVGdestroyPaint;
1277 putOpenVGCommand(comm,false);
1280 unsigned int OsdOpenVG::createStyleRef(const DrawStyle &c)
1282 unsigned int col=c.rgba();
1283 struct OpenVGCommand comm;
1284 comm.task=OVGcreateColorRef;
1285 comm.param1=col<<8 | (col &0xff000000)>>24;
1287 return putOpenVGCommand(comm,true);
1290 unsigned int OsdOpenVG::createColorRef(const Colour &c)
1292 unsigned int col=c.rgba();
1293 struct OpenVGCommand comm;
1294 comm.task=OVGcreateColorRef;
1295 comm.param1=col<<8 | (col &0xff000000)>>24;
1297 return putOpenVGCommand(comm,true);