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, see <https://www.gnu.org/licenses/>.
21 #include <sys/syscall.h>
22 #include <fontconfig/fontconfig.h>
30 #include "messagequeue.h"
31 #include "teletxt/txtfont.h"
33 #include "osdopenvg.h"
36 #define EXTERNALPICTURE(name, fname, fileextension) extern uint8_t name ## _data[] asm("_binary_other_"#fname"_"#fileextension"_start"); \
37 extern uint8_t name ## _data_end[] asm("_binary_other_"#fname"_"#fileextension"_end");
41 #undef EXTERNALPICTURE
44 #define BACKBUFFER_WIDTH 1280
45 #define BACKBUFFER_HEIGHT 720
48 OsdOpenVG::OsdOpenVG()
52 lastrendertime = getTimeMS();
56 aspect_correction = 1.;
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);
66 #define EXTERNALPICTURE(name, fname, fileextension) static_artwork_begin[sa_ ## name]=name ## _data;
70 #undef EXTERNALPICTURE
71 #define EXTERNALPICTURE(name, fname, fileextension) static_artwork_end[sa_ ## name]=name ## _data_end;
75 #undef EXTERNALPICTURE
78 OsdOpenVG::~OsdOpenVG()
86 if (cur_fontname) free(cur_fontname);
88 if (freetype_inited) FT_Done_Face(ft_face);
90 // I think the following is broken as it is, but also possibly it shouldn't be free()ing the memory
91 // pointed at anyway, so it's correctly not working?!
93 if (!fontnames.size())
95 std::vector<char*>::iterator itty = fontnames.begin();
97 while (itty != fontnames.end())
109 if (fontnames_keys.size())
111 std::vector<char*>::iterator itty = fontnames_keys.begin();
113 while (itty != fontnames_keys.end())
126 int OsdOpenVG::init()
128 if (initted) return 0;
132 //init broadcom chipset (Move to video?)
135 //First get connection to egl
136 egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
138 if (egl_display == EGL_NO_DISPLAY)
140 Log::getInstance()->log("OSD", Log::WARN, "Could not get egl display! %x", eglGetError());
147 if (eglInitialize(egl_display, NULL, NULL) == EGL_FALSE)
149 Log::getInstance()->log("OSD", Log::WARN, "Initialising display failed! %x", eglGetError());
155 const char* query_str = eglQueryString(egl_display, EGL_CLIENT_APIS);
157 if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
158 else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x", eglGetError());
160 query_str = eglQueryString(egl_display, EGL_EXTENSIONS);
162 if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
163 else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x", eglGetError());
165 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
167 Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api failed! %x", eglGetError());
172 const EGLint attributs[] =
174 EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
175 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
176 EGL_CONFORMANT, EGL_OPENVG_BIT,
178 }; // Here, we might have to select the resolution!
183 if (eglChooseConfig(egl_display, attributs, &egl_ourconfig, 1, &number) == EGL_FALSE)
185 Log::getInstance()->log("OSD", Log::WARN, "Choosing egl config failed! %x", eglGetError());
192 egl_context = eglCreateContext(egl_display, egl_ourconfig, NULL, NULL);
194 if (egl_context == EGL_NO_CONTEXT)
196 Log::getInstance()->log("OSD", Log::WARN, "Creating egl context failed! %x", eglGetError());
201 // warning broadcom specific, get display size!
202 display_width = display_height = 0;
204 if (graphics_get_display_size(0, &display_width, &display_height) < 0)
206 Log::getInstance()->log("OSD", Log::WARN, "Getting display size failed! (BCM API) ");
211 Log::getInstance()->log("OSD", Log::NOTICE, "Displaysize is %d x %d ", display_width, display_height);
212 VC_RECT_T dst_rect = {0, 0, (int32_t)display_width, (int32_t)display_height};
213 VC_RECT_T src_rect = {0, 0, BACKBUFFER_WIDTH << 16, BACKBUFFER_HEIGHT << 16};
214 VC_RECT_T src_rect_bg = {0, 0, 16 << 16, 16 << 16};
215 // VC_RECT_T src_rect_im={0,0,16,16};
217 uint32_t back_image_ptr;
218 bcm_backres = vc_dispmanx_resource_create(VC_IMAGE_RGBX32, 16, 16, &back_image_ptr);
221 updateBackgroundColor(DrawStyle::WALLPAPER);
225 DISPMANX_UPDATE_HANDLE_T bcm_update;
226 bcm_display = vc_dispmanx_display_open(0);
227 bcm_update = vc_dispmanx_update_start(0);
228 bcm_element = vc_dispmanx_element_add(bcm_update, bcm_display,
230 &src_rect, DISPMANX_PROTECTION_NONE, 0, 0, (DISPMANX_TRANSFORM_T) 0);
233 bcm_background = vc_dispmanx_element_add(bcm_update, bcm_display,
234 0, &dst_rect, bcm_backres,
235 &src_rect_bg, DISPMANX_PROTECTION_NONE, 0, 0, (DISPMANX_TRANSFORM_T) 0);
237 vc_dispmanx_update_submit_sync(bcm_update);
241 static EGL_DISPMANX_WINDOW_T nativewindow;
242 nativewindow.element = bcm_element;
243 nativewindow.height = BACKBUFFER_HEIGHT;
244 nativewindow.width = BACKBUFFER_WIDTH;
246 egl_surface = eglCreateWindowSurface(egl_display, egl_ourconfig, &nativewindow, NULL );
248 if (egl_surface == EGL_NO_SURFACE)
250 Log::getInstance()->log("OSD", Log::WARN, "Creating egl window surface failed!");
255 Log::getInstance()->log("OSD", Log::DEBUG, "Making egl current in1%d", syscall(SYS_gettid));
257 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
259 Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed");
266 query_str = (const char*)vgGetString(VG_VERSION) ;
268 if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
269 else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x", vgGetError());
271 query_str = (const char*)vgGetString(VG_VENDOR) ;
273 if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
274 else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x", vgGetError());
276 query_str = (const char*)vgGetString(VG_RENDERER) ;
278 if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
279 else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x", vgGetError());
281 query_str = (const char*)vgGetString(VG_EXTENSIONS) ;
283 if (query_str) Log::getInstance()->logLongString("OSD", Log::NOTICE, query_str);
284 else Log::getInstance()->log("OSD", Log::WARN, "Could not query display %x", vgGetError());
286 aspect_correction = ((float)BACKBUFFER_HEIGHT) / 576.f / (((float)BACKBUFFER_WIDTH) / 720.f);
289 if (!fontnames.size())
291 //inspired by and copied from vdr's code
293 FcObjectSet* objset = FcObjectSetBuild(FC_FAMILY, FC_STYLE, NULL);
294 FcPattern* pattern = FcPatternCreate();
295 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
296 FcFontSet* fonts = FcFontList(NULL, pattern, objset);
298 for (int i = 0; i < fonts->nfont; i++)
300 char* s = (char*)FcNameUnparse(fonts->fonts[i]);
304 char* c = strchr(s, ':');
308 char* s2 = strchr(c + 1, ',');
313 char* p = strchr(s, ',');
318 else memmove(p, c, strlen(c) + 1);
321 char* key = (char*)malloc(strlen(s) + 1);
323 fontnames_keys.push_back(key);
324 char* s2 = strstr(s, "style=");
328 memmove(s2, s2 + 6, strlen(s2 + 6) + 1);
331 fontnames.push_back(s);
335 FcFontSetDestroy(fonts);
336 FcPatternDestroy(pattern);
337 FcObjectSetDestroy(objset);
340 if (!loadFont(false))
345 vgttfont = vgCreateFont(0);
346 vgttpaint = vgCreatePaint();
347 vgSetParameteri( vgttpaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
348 vgSetColor(vgttpaint, 0xffffffff);
350 eglSwapInterval(egl_display, 1 );
352 Log::getInstance()->log("OSD", Log::DEBUG, "Making egl current out 1%d", syscall(SYS_gettid));
353 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
354 //Now we will create the Screen
355 initted = true; // must set this here or create surface won't work
358 /*if (((VideoOMX*)Video::getInstance())->initUsingOSDObjects()!=1) { //call Video for init stuff
367 #ifdef PICTURE_DECODER_OMX
368 imageomx = new ImageOMX(&reader);
369 reader.addDecoder(imageomx);
376 void OsdOpenVG::updateBackgroundColor(DrawStyle bg)
378 unsigned int color[16 * 16];
380 if (!bcm_backres) return;
382 VC_RECT_T src_rect_im = {0, 0, 16, 16};
384 if (bg.ft != DrawStyle::GradientLinear)
389 //memset(color,0xFF,sizeof(unsigned int)*4*8);
390 for (int j = 0; j < 16; j++)
392 int helpr = (((15 - j) * bg.red) + (j * bg.grad_col[0].red)) / 15;
393 int helpb = (((15 - j) * bg.blue) + (j * bg.grad_col[0].blue)) / 15;
394 int helpg = (((15 - j) * bg.green) + (j * bg.grad_col[0].green)) / 15;
395 //unsigned int cur_col=help | (help<< 8) | (help<< 16) | (0xFF<< 24);
396 unsigned int cur_col = helpr | (helpg << (8)) | (helpb << (16)) | (0xFF << (24));
398 for (int i = 0; i < 16; i++)
400 color[i + 16 * j] = cur_col;
404 vc_dispmanx_resource_write_data(bcm_backres, VC_IMAGE_RGBX32, 16 * 4, color, &src_rect_im);
407 void OsdOpenVG::getScreenSize(int& width, int& height)
409 width = BACKBUFFER_WIDTH;
410 height = BACKBUFFER_HEIGHT;
413 void OsdOpenVG::getRealScreenSize(int& width, int& height)
415 width = display_width;
416 height = display_height;
419 bool OsdOpenVG::screenShot(void* buffer, int width, int height, bool osd /*include osd*/)
421 if (!initted) return false;
423 if (!buffer) return false;
425 DISPMANX_RESOURCE_HANDLE_T res;
426 DISPMANX_DISPLAY_HANDLE_T display;
430 res = vc_dispmanx_resource_create(VC_IMAGE_RGBA32, width, height, &image_ptr);
431 display = vc_dispmanx_display_open(0);
435 vc_dispmanx_snapshot(display, res,
436 (DISPMANX_TRANSFORM_T)(DISPMANX_SNAPSHOT_NO_RGB | DISPMANX_SNAPSHOT_FILL/*|DISPMANX_SNAPSHOT_PACK*/));
440 vc_dispmanx_snapshot(display, res,
441 (DISPMANX_TRANSFORM_T)(DISPMANX_SNAPSHOT_FILL));
444 vc_dispmanx_rect_set(&rect, 0, 0, width, height);
445 vc_dispmanx_resource_read_data(res, &rect, buffer, width * 4);
446 vc_dispmanx_resource_delete(res);
447 vc_dispmanx_display_close(display);
453 void OsdOpenVG::initPaths()
458 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
459 VG_PATH_DATATYPE_F, 1.f, 0.f,
460 0, 0, VG_PATH_CAPABILITY_ALL);
462 vguLine(current, 0.f, 0.f, 1.f, 0.f);
464 std_paths[PIHorzLine] = current;
466 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
467 VG_PATH_DATATYPE_F, 1.f, 0.f,
468 0, 0, VG_PATH_CAPABILITY_ALL);
469 vguLine(current, 0.f, 0.f, 0.f, 1.f);
471 std_paths[PIVertLine] = current;
473 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
474 VG_PATH_DATATYPE_F, 1.f, 0.f,
475 0, 0, VG_PATH_CAPABILITY_ALL);
476 //vguRect(current,0.f,0.f,1.f,1.f);
477 vguRect(current, 0.f, 0.f, 1.f, 1.f);
479 std_paths[PIRectangle] = current;
481 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
482 VG_PATH_DATATYPE_F, 1.f, 0.f,
484 vguEllipse(current, 0.f, 0.f, 1.f, 1.f);
486 std_paths[PIPoint] = current;
490 void OsdOpenVG::destroyPaths()
492 vgDestroyPath(std_paths[PIHorzLine]);
493 vgDestroyPath(std_paths[PIVertLine]);
494 vgDestroyPath(std_paths[PIRectangle]);
495 vgDestroyPath(std_paths[PIPoint]);
499 int OsdOpenVG::stopUpdate()
506 void OsdOpenVG::purgeAllReferences()
509 drawstyleHandlesRefCounts.clear(); // remove all references
512 map<void *,ImageIndex>::iterator mitty=monobitmaps.begin();
513 while (mitty!=monobitmaps.end()) {
514 vgDestroyImage((VGImage)(*mitty).second);
519 / *map<string,ImageIndex>::iterator jitty=jpegs.begin();
520 while (jitty!=jpegs.end()) {
521 vgDestroyImage((VGImage)(*jitty).second);
526 map<TVMediaInfo,ImageIndex>::iterator titty=tvmedias.begin();
527 while (titty!=tvmedias.end()) {
528 vgDestroyImage((VGImage)(*titty).second);
533 map<pair<Colour*,unsigned int>,unsigned int>::iterator sitty=drawstyleHandles.begin();
534 while (sitty!=drawstyleHandles.end()) {
535 vgDestroyPaint((VGPaint)(*sitty).second);
538 drawstyleHandles.clear();
542 int OsdOpenVG::shutdown()
545 #ifdef PICTURE_DECODER_OMX
547 if (imageomx) reader.removeDecoder(imageomx);
552 if (!initted) return 0;
555 Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark1");
557 Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark1a");
559 //(((VideoOMX*)Video::getInstance())->shutdownUsingOSDObjects());
560 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
562 Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed in shutdown %x", eglGetError());
565 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
567 Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api thread failed! %x", eglGetError());
570 Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark2");
572 Log::getInstance()->log("OSD", Log::DEBUG, "shutdown mark3");
579 //purgeAllReferences();
581 vgDestroyFont(vgfont);
582 vgDestroyFont(vgttfont);
583 vgDestroyPaint(vgttpaint);
585 float colclear[] = {0.8f, 0.8f, 0.8f, 1.f};
586 vgSetfv(VG_CLEAR_COLOR, 4, colclear);
587 vgClear(0, 0, BACKBUFFER_WIDTH, BACKBUFFER_HEIGHT);
588 eglSwapBuffers(egl_display, egl_surface);
589 Log::getInstance()->log("OSD", Log::DEBUG, "Making egl current out final");
590 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
592 if (eglDestroySurface(egl_display, egl_surface) == EGL_FALSE)
594 Log::getInstance()->log("OSD", Log::ERR, "eglDestroySurface failed %x", eglGetError());
597 if (eglDestroyContext(egl_display, egl_context) == EGL_FALSE)
599 Log::getInstance()->log("OSD", Log::ERR, "eglDestroyContext failed %x", eglGetError());
602 if (eglTerminate(egl_display ) == EGL_FALSE)
604 Log::getInstance()->log("OSD", Log::ERR, "eglTerminate failed %x", eglGetError());
607 DISPMANX_UPDATE_HANDLE_T bcm_update;
608 bcm_update = vc_dispmanx_update_start(0);
610 vc_dispmanx_element_remove(bcm_update, bcm_element);
611 vc_dispmanx_element_remove(bcm_update, bcm_background);
612 vc_dispmanx_update_submit_sync(bcm_update);
613 vc_dispmanx_resource_delete(bcm_backres);
615 vc_dispmanx_display_close(bcm_display);
625 void OsdOpenVG::threadMethod()
627 // We have to claim the egl context for this thread
628 Log::getInstance()->log("OSD", Log::NOTICE, "Entering drawing thread");
630 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
632 Log::getInstance()->log("OSD", Log::WARN, "Making egl Current failed in thread %x", eglGetError());
636 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
638 Log::getInstance()->log("OSD", Log::WARN, "Binding openvg api thread failed! %x", eglGetError());
650 long long time1 = getTimeMS();
652 if ((time1 - lastrendertime) > 200) //5 fps for OSD updates are enough, avoids tearing
655 lastrendertime = getTimeMS();
661 ts = time1 - lastrendertime;
664 if (processTasks()) ts = 0;
667 if (!threadIsActive())
669 if (eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT ) == EGL_FALSE)
671 Log::getInstance()->log("OSD", Log::WARN, "Making egl Current out thread failed");
679 struct timespec target_time;
680 clock_gettime(CLOCK_REALTIME, &target_time);
681 target_time.tv_nsec += 1000000LL * ts;
683 if (target_time.tv_nsec > 999999999)
685 target_time.tv_nsec -= 1000000000L;
686 target_time.tv_sec += 1;
690 threadWaitForSignalTimed(&target_time);
695 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
698 void OsdOpenVG::InternalRendering()
701 Colour bg = DrawStyle::WALLPAPER;
702 float colclear[] = {1.f, 1.0f, 1.f, 1.f};
704 if (bg.alpha == 0) colclear[3] = 0.f;
706 vgSetfv(VG_CLEAR_COLOR, 4, colclear);
707 vgClear(0, 0, BACKBUFFER_WIDTH, BACKBUFFER_HEIGHT);
708 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
711 drawSurfaces(); //iterate through and draws all commands
713 //Show it to the user!
714 eglSwapBuffers(egl_display, egl_surface);
721 float OsdOpenVG::getFontHeight()
723 return font_height; //dummy
725 float OsdOpenVG::getCharWidth(wchar_t c)
727 if (c < 256) return byte_char_width[c];
729 unsigned int glyph_index = FT_Get_Char_Index(ft_face, c);
730 return font_exp_x[glyph_index];
733 unsigned int OsdOpenVG::loadTTchar(cTeletextChar c)
735 unsigned int glyph_index = c.getGlyphIndex();
737 if (tt_font_chars.find(glyph_index) != tt_font_chars.end())
742 unsigned int buffer[10];
743 const VGfloat glyphOrigin[] = { 0.f, 0.f };
744 const VGfloat escapement[] = { 12.f, 0.f };
745 unsigned int* charmap = GetFontChar(c, buffer);
747 if (!charmap) //invalid char
752 for (int i = 0; i < 10; i++)
754 buffer[i] = charmap[i] >> 4;
757 VGImage handle = vgCreateImage(
761 VG_IMAGE_QUALITY_NONANTIALIASED | VG_IMAGE_QUALITY_FASTER
762 | VG_IMAGE_QUALITY_BETTER);
763 vgImageSubData(handle, buffer, 4, VG_A_1, 0, 0, 12, 10);
767 handle, (VGfloat*)glyphOrigin, (VGfloat*)escapement);
768 vgDestroyImage(handle);
769 tt_font_chars[glyph_index] = 1;
774 int OsdOpenVG::getFontNames(const char*** names, const char*** names_keys)
776 *names = (const char**) & (fontnames[0]);
777 *names_keys = (const char**) & (fontnames_keys[0]);
778 return fontnames.size();
781 void OsdOpenVG::setFont(const char* fontname)
784 if (strcmp(fontname, cur_fontname))
787 if (cur_fontname) free(cur_fontname);
789 cur_fontname = static_cast<char*>(malloc(strlen(fontname) + 1));
790 strcpy(cur_fontname, fontname);
792 struct OpenVGCommand comm;
793 comm.task = OVGreplacefont;
794 putOpenVGCommand(comm, false);
801 int OsdOpenVG::loadFont(bool newfont)
804 float font_size = 16.f;
806 if (!freetype_inited)
808 error = FT_Init_FreeType(&ft_library);
812 Log::getInstance()->log("OSD", Log::WARN, "Could not load freetype %x", error);
817 if (!freetype_inited || newfont)
819 //first get the filename algorith extracted from vdr by Klaus Schmidinger
821 FcPattern* pattern = FcNameParse((FcChar8*)cur_fontname);
822 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
823 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
824 FcDefaultSubstitute(pattern);
826 FcFontSet* fonts = FcFontSort(NULL, pattern, FcFalse, NULL, &fres);
827 FcChar8* filename = NULL;
831 for (int i = 0; i < fonts->nfont; i++)
834 FcPatternGetBool(fonts->fonts[i], FC_SCALABLE, 0, &canscale);
838 FcPatternGetString(fonts->fonts[i], FC_FILE, 0, &filename);
843 FcFontSetDestroy(fonts);
847 Log::getInstance()->log("OSD", Log::CRIT, "Could not locate a font! Abort!");
851 FcPatternDestroy(pattern);
853 Log::getInstance()->log("OSD", Log::NOTICE, "Load Font %s: %s", cur_fontname, filename);
854 // second load the font
856 error = FT_New_Face(ft_library, (const char*)filename, 0, &new_ft_face );
860 Log::getInstance()->log("OSD", Log::WARN, "Could not load font face %x %s", error, filename);
864 error = FT_Set_Char_Size(new_ft_face, 0, font_size * 256, 0, 0 /*dpi*/);
868 FT_Done_Face(new_ft_face);
869 Log::getInstance()->log("OSD", Log::WARN, "Could not set face size %x", error);
873 FT_Face old_ft_face = ft_face; // do it thread safe
874 ft_face = new_ft_face;
876 if (freetype_inited) FT_Done_Face(old_ft_face);//
878 freetype_inited = true;
881 vgfont = vgCreateFont(0);
884 font_height = ft_face->size->metrics.height / 256.f;
885 cur_char = FT_Get_First_Char(ft_face, &glyph);
886 std::vector<VGubyte> segments;
887 std::vector<VGfloat> coord;
888 segments.reserve(256);
891 //Log::getInstance()->log("OSD", Log::DEBUG, "Create Glyph test %d %x %x %d",cur_char,font_data_end,font_data,glyph);
894 error = FT_Load_Glyph(ft_face, glyph, FT_LOAD_DEFAULT);
898 cur_char = FT_Get_Next_Char(ft_face, cur_char, &glyph);
899 Log::getInstance()->log("OSD", Log::WARN, "Could not load glyph %x %c", error);
904 FT_Outline ot = ft_face->glyph->outline;
908 if (ot.n_contours == 0)
910 path = VG_INVALID_HANDLE;
914 path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F,
915 1.0f, 0.f, 0, 0, VG_PATH_CAPABILITY_ALL);
918 FT_Vector* pt = ot.points;
919 const char* tags = ot.tags;
920 const short* cont = ot.contours;
921 short n_cont = ot.n_contours;
923 //short n_point=ot.n_points;
925 for (short point = 0; n_cont != 0; cont++, n_cont--)
927 short next_cont = *cont + 1;
930 short first_point = point;
932 //Log::getInstance()->log("OSD", Log::DEBUG, "runs %d",*cont);
933 for (; point < next_cont; point++)
935 char tag = tags[point];
936 FT_Vector fpoint = pt[point];
938 // Log::getInstance()->log("OSD", Log::DEBUG, "tag %d point %d %d: %d %d",tag,fpoint.x,fpoint.y,point,n_point);
941 segments.push_back(VG_MOVE_TO);
944 else if (tag & 0x1) //on curve
948 segments.push_back(VG_LINE_TO);
950 else if (last_tag & 0x2)
952 segments.push_back(VG_CUBIC_TO);
956 segments.push_back(VG_QUAD_TO);
964 if (!(last_tag & 0x1))
966 segments.push_back(VG_QUAD_TO);
967 int coord_size = coord.size();
968 VGfloat x = (coord[coord_size - 2] + ((float)fpoint.x) / 256.f) * 0.5f * aspect_correction;
969 VGfloat y = (coord[coord_size - 1] + (font_size - ((float)fpoint.y) / 256.f)) * 0.5f;
979 coord.push_back(((float)fpoint.x)*aspect_correction / 256.);
980 coord.push_back(font_size - ((float)fpoint.y) / 256.);
981 //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]);
984 if (!(last_tag & 0x1))
988 segments.push_back(VG_CUBIC_TO);
992 segments.push_back(VG_QUAD_TO);
995 coord.push_back(((float)pt[first_point].x)*aspect_correction / 256.);
996 coord.push_back(font_size - ((float)pt[first_point].y) / 256.);
999 //segments.push_back(VG_CLOSE_PATH);
1004 vgAppendPathData(path, segments.size(), &segments[0], &coord[0]);
1005 /* for (int m=0;m<segments.size();m++) {
1007 switch (segments[m])
1010 Log::getInstance()->log("OSD", Log::DEBUG, "Move To %g %g",coord[n],coord[n+1]);n+=2; break;
1012 Log::getInstance()->log("OSD", Log::DEBUG, "Line To %g %g",coord[n],coord[n+1]);n+=2; break;
1014 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;
1016 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;
1018 Log::getInstance()->log("OSD", Log::DEBUG, "Close Path"); break;
1022 //vguRect(path,0.f,0.f,1.f,1.f);
1023 //Log::getInstance()->log("OSD", Log::DEBUG, "Create APD Glyph %d %x",segments.size(),vgGetError());
1026 VGfloat ori[] = {0.f, 0.f};
1027 VGfloat esp[] = {ft_face->glyph->advance.x / 256.f * aspect_correction, ft_face->glyph->advance.y / 256.f};
1028 font_exp_x[glyph] = ft_face->glyph->advance.x / 256.f * aspect_correction; //recalculate
1030 vgSetGlyphToPath(vgfont, glyph, path, VG_FALSE, ori, esp);
1032 //Log::getInstance()->log("OSD", Log::DEBUG, "Create Glyph %d %d %x",path,glyph,vgGetError());
1033 if (path != VG_INVALID_HANDLE)
1035 vgDestroyPath(path);
1038 cur_char = FT_Get_Next_Char(ft_face, cur_char, &glyph);
1041 for (int i = 0; i < 256; i++)
1043 unsigned int glyph_index = FT_Get_Char_Index(ft_face, i);
1044 byte_char_width[i] = font_exp_x[glyph_index];
1051 void OsdOpenVG::drawSetTrans(SurfaceInfo& sc)
1053 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1055 vgScale(((float)BACKBUFFER_WIDTH) / 720.f, -((float)BACKBUFFER_HEIGHT) / 576.f);
1056 vgTranslate(0.f + sc.x, -576.f + sc.y);
1058 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1060 vgScale(((float)BACKBUFFER_WIDTH) / 720.f, -((float)BACKBUFFER_HEIGHT) / 576.f);
1061 vgTranslate(0.f + sc.x, -576.f + sc.y);
1063 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1065 vgScale(((float)BACKBUFFER_WIDTH) / 720.f, -((float)BACKBUFFER_HEIGHT) / 576.f);
1066 vgTranslate(0.f + sc.x, -576.f + sc.y);
1067 clip_shift_x = sc.x;
1068 clip_shift_y = sc.y;
1072 //vgTranslate(0.f+sc.x,576.f-sc.y);
1073 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Translate %g %g",sc.x,sc.y);
1076 void OsdOpenVG::executeDrawCommand(SVGCommand& command)
1079 VGfloat save_matrix[9];
1080 VGfloat save_matrix2[9];
1082 switch (command.instr)
1086 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1088 // rgba = vgGetColor((VGPaint) command.reference);
1089 //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);
1090 //vgSeti(VG_FILL_RULE,);
1092 vgGetMatrix(save_matrix);
1093 vgSetPaint((VGPaint) command.reference, VG_FILL_PATH);
1094 vgSetPaint((VGPaint) command.reference, VG_STROKE_PATH);
1095 vgTranslate(command.x, command.y);
1096 vgScale(command.w, command.h);
1097 vgDrawPath(std_paths[command.target.path_index], VG_FILL_PATH);
1098 vgLoadMatrix(save_matrix);
1103 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1104 vgGetMatrix(save_matrix);
1105 VGfloat imagewidth = vgGetParameteri((VGImage) command.target.image, VG_IMAGE_WIDTH);
1106 VGfloat imageheight = vgGetParameteri((VGImage) command.target.image, VG_IMAGE_HEIGHT);
1109 //vgScale(command.w,command.h);
1111 if (command.reference) //special behaviout for bw images they act as a mask on the current paint
1113 vgTranslate(command.x, command.y);
1114 vgSetPaint((VGPaint) command.reference, VG_FILL_PATH);
1115 vgSetPaint((VGPaint) command.reference, VG_STROKE_PATH);
1116 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_STENCIL);
1117 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1118 vgScale(aspect_correction, 1.f);
1119 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1120 vgGetMatrix(save_matrix2);
1121 vgScale(imagewidth, imageheight);
1126 //vgScale(720.f/((float)BACKBUFFER_WIDTH), 576.f/((float)BACKBUFFER_HEIGHT));
1127 float scalex = command.w / imagewidth;
1128 float scaley = command.h / imageheight;
1129 float tx = command.x;
1130 float ty = command.y;
1132 if (command.corner == TopLeftLimited)
1134 if (scalex != 0.f && scaley != 0.f)
1136 float imageaspect = imagewidth / imageheight;
1137 float boxaspect = command.w / command.h / aspect_correction;
1139 if (imageaspect > boxaspect)
1142 ty += (command.h - imageheight * scalex / aspect_correction) * 0.5f;
1147 tx += (command.w - imagewidth * scaley * aspect_correction) * 0.5f;
1154 if (scalex == 0.f && scaley == 0.f)
1156 scalex = aspect_correction;
1159 else if (scalex == 0.f)
1161 scalex = scaley * aspect_correction;
1163 else if (scaley == 0.f)
1165 scaley = scalex / aspect_correction;
1169 if (command.corner == BottomRight || command.corner == BottomLeft || command.corner == BottomMiddle)
1171 ty -= imageheight * scaley;
1174 if (command.corner == BottomRight || command.corner == TopRight)
1176 tx -= imagewidth * scalex;
1179 if (command.corner == BottomMiddle || command.corner == TopMiddle)
1181 tx -= imagewidth * scalex * 0.5f;
1184 vgTranslate(tx, ty);
1185 //vgScale(command.w/imagewidth,command.h/imageheight);
1186 vgScale(scalex, scaley);
1187 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1188 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1189 //Log::getInstance()->log("OSD", Log::DEBUG, "TVMedia Draw Image Scale %g %g %g %g %g %g",command.w,imagewidth,command.h,imageheight,scalex,scaley);
1194 //vgTranslate(200.f,500.f);
1195 //vgScale(100.f,100.f);
1197 vgDrawImage((VGImage) command.target.image);
1199 //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,
1201 if (command.reference)
1203 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1204 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1205 vgLoadMatrix(save_matrix2);
1208 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
1210 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1211 vgLoadMatrix(save_matrix);
1216 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1217 vgGetMatrix(save_matrix);
1218 vgSetPaint((VGPaint) command.reference, VG_FILL_PATH);
1219 vgSetPaint((VGPaint) command.reference, VG_STROKE_PATH);
1220 vgTranslate(command.x, command.y);
1221 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1222 vgGetMatrix(save_matrix2);
1223 unsigned int glyph_index = FT_Get_Char_Index(ft_face, command.target.textchar);
1224 vgScale(font_exp_x[glyph_index], font_height);
1227 VGfloat gori[] = {0., 0.};
1228 vgSetfv(VG_GLYPH_ORIGIN, 2, gori);
1231 vgDrawGlyph(vgfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1232 //vgDrawPath(std_paths[Rectangle],VG_FILL_PATH);
1233 /* 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,
1235 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1236 vgLoadMatrix(save_matrix);
1237 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1238 vgLoadMatrix(save_matrix2);
1240 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1245 cTeletextChar tchar;
1246 tchar.setInternal(command.target.ttchar);
1247 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1248 vgGetMatrix(save_matrix);
1249 enumTeletextColor ttforegcolour = tchar.GetFGColor();
1250 enumTeletextColor ttbackgcolour = tchar.GetBGColor();
1252 if (tchar.GetBoxedOut())
1254 ttforegcolour = ttcTransparent;
1255 ttbackgcolour = ttcTransparent;
1258 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_STENCIL);
1259 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1262 vgTranslate(command.x + command.w * 11.85f * 1.4f, command.y + command.h * 19.75f);
1263 VGfloat gori[] = {0., 0.};
1264 vgSetfv(VG_GLYPH_ORIGIN, 2, gori);
1266 vgScale(-1.4f, 2.f);
1267 unsigned int color = Surface::enumTeletextColorToCoulour(ttbackgcolour).rgba();
1268 color = color << 8 | (color & 0xff000000) >> 24;
1269 vgSetColor(vgttpaint, color);
1270 vgSetPaint((VGPaint) vgttpaint, VG_FILL_PATH);
1271 vgSetPaint((VGPaint) vgttpaint, VG_STROKE_PATH);
1272 cTeletextChar filled;
1273 filled.setInternal(0x187f);
1274 unsigned int glyph_index = loadTTchar(filled);
1275 vgDrawGlyph(vgttfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1277 color = Surface::enumTeletextColorToCoulour(ttforegcolour).rgba();
1278 color = color << 8 | (color & 0xff000000) >> 24;
1279 vgSetColor(vgttpaint, color);
1280 vgSetPaint((VGPaint) vgttpaint, VG_FILL_PATH);
1281 vgSetPaint((VGPaint) vgttpaint, VG_STROKE_PATH);
1282 glyph_index = loadTTchar(tchar);
1283 vgDrawGlyph(vgttfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1285 /* Log::getInstance()->log("OSD", Log::DEBUG, "Draw TTchar %x %x %x %x",glyph_index,ttforegcolour,Surface::enumTeletextColorToCoulour(ttforegcolour).rgba(),
1286 vgGetColor(vgttpaint));*/
1289 vgLoadMatrix(save_matrix);
1290 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1291 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1292 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
1298 VGint coords[4] = {0, 0, 0, 0};
1299 coords[0] = ((command.x + clip_shift_x) * ((float)BACKBUFFER_WIDTH) / 720.f);
1300 coords[1] = ((576.f - command.y - clip_shift_y - command.h) * ((float)BACKBUFFER_HEIGHT) / 576.f);
1301 coords[2] = ((command.w - 1.f) * ((float)BACKBUFFER_WIDTH) / 720.f);
1302 coords[3] = ((command.h - 1.f) * ((float)BACKBUFFER_HEIGHT) / 576.f);
1303 vgSetiv(VG_SCISSOR_RECTS, 4, coords);
1305 if (command.w == 0.f && command.h == 0.f)
1306 vgSeti(VG_SCISSORING, VG_FALSE);
1307 else vgSeti(VG_SCISSORING, VG_TRUE);
1310 case DrawImageLoading:
1315 //int imcount=0;// this is debug code and should not go into release
1316 unsigned int OsdOpenVG::handleTask(OpenVGCommand& command)
1318 switch (command.task)
1320 case OVGreplacefont:
1322 vgDestroyFont(vgfont);
1327 case OVGdestroyImageRef: //imcount--;
1329 //Log::getInstance()->log("OSD", Log::DEBUG, "TVMedia Draw Image Destroy %x %d",command.param1,imcount);
1330 vgDestroyImage((VGImage)command.param1);
1334 case OVGdestroyPaint:
1336 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint Destroy %d ",command.param1);
1337 vgDestroyPaint((VGPaint)command.param1);
1341 case OVGcreateImagePalette: //imcount++;
1343 VGImage input = vgCreateImage(VG_A_8, command.param1, command.param2,
1344 VG_IMAGE_QUALITY_NONANTIALIASED |
1345 VG_IMAGE_QUALITY_FASTER | VG_IMAGE_QUALITY_BETTER);
1346 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create palette %d %d %x %d",command.param1,command.param2,vgGetError(),input);
1347 vgImageSubData(input, command.data, command.param1,
1348 VG_A_8, 0, 0, command.param1, command.param2); // upload palettized image data
1349 VGImage handle = vgCreateImage(VG_sRGBA_8888, command.param1, command.param2,
1350 VG_IMAGE_QUALITY_NONANTIALIASED |
1351 VG_IMAGE_QUALITY_FASTER | VG_IMAGE_QUALITY_BETTER);
1352 VGuint* palette = (VGuint*)malloc(256 * sizeof(VGuint));
1353 VGuint* in_palette = (VGuint*)command.data2;
1355 for (int i = 0; i < 256; i++)
1357 VGuint color = in_palette[i];
1358 palette[i] = color << 8 | (color & 0xff000000) >> 24;
1361 vgLookupSingle(handle, input, palette, VG_ALPHA, VG_FALSE, VG_FALSE);
1363 vgDestroyImage(input);
1368 case OVGcreateMonoBitmap: //imcount++;
1370 VGImage handle = vgCreateImage(VG_A_1, command.param1, command.param2,
1371 VG_IMAGE_QUALITY_FASTER);
1372 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create mono %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1373 unsigned int buffer_len = (command.param1 * command.param2) >> 3;
1374 unsigned char* buffer = (unsigned char*)malloc(buffer_len);
1375 unsigned char* r_buffer1 = buffer;
1376 const unsigned char* r_buffer2 = static_cast<const unsigned char*>(command.data);
1377 unsigned char* buffer_end = buffer + buffer_len;
1379 while (r_buffer1 != buffer_end)
1381 unsigned char byte = *r_buffer2;
1382 *r_buffer1 = ((byte * 0x0802LU & 0x22110LU) | (byte * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
1383 r_buffer1++; r_buffer2++;
1387 vgImageSubData(handle, buffer, command.param1 >> 3,
1388 VG_A_1, 0, 0, command.param1, command.param2);
1390 // Log::getInstance()->log("OSD", Log::DEBUG, "Draw create mono2 %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1394 /*case OVGcreateImageFile: {
1397 Image *imagefile=(Image*)command.data;
1399 imagefile->write(&imageblob,"RGBA");
1402 handle=vgCreateImage(VG_sXBGR_8888,imagefile->columns(),imagefile->rows(),
1403 VG_IMAGE_QUALITY_BETTER);
1404 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create image details %d %d %x mark1",imagefile->columns(),imagefile->rows(),(unsigned int*)imageblob.data());
1405 vgImageSubData(handle,imageblob.data(),imagefile->columns()*4,
1406 VG_sXBGR_8888,0,0,imagefile->columns(),imagefile->rows());
1407 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create image details %d %d %x mark2",imagefile->columns(),imagefile->rows(),(unsigned int*)imageblob.data());
1409 }catch( Exception &error_ )
1411 Log::getInstance()->log("OSD", Log::DEBUG, "Libmagick hT: %s",error_.what());
1416 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw create file %d %d %x %d",command.param1,command.param2,vgGetError(),handle);
1419 case OVGcreateImageMemory: //imcount++;
1421 PictureInfo* info = (PictureInfo*) command.data;
1423 //Log::getInstance()->log("OSD", Log::DEBUG, "TVMedia OVGcreateImageMemory %d",imcount);
1424 handle = vgCreateImage(VG_sABGR_8888, info->width, info->height, VG_IMAGE_QUALITY_BETTER);
1425 vgImageSubData(handle, info->image, info->width * 4,
1426 VG_sABGR_8888, 0, 0, info->width, info->height);
1427 info->decoder->freeReference(info->reference);
1429 bool static_image = true;
1431 if (info->lindex & 0xffffffff) static_image = false;
1433 Message* m = new Message();
1434 // We have a pictures! send a message to ourself, to switch to gui thread
1436 m->p_to = Message::CONTROL;
1437 m->data = reinterpret_cast<void*>(handle);
1441 m->message = Message::NEW_PICTURE;
1442 m->tag = info->lindex;
1446 m->message = Message::NEW_PICTURE_STATIC;
1447 m->tag = info->lindex >> 32LL;
1450 MessageQueue::getInstance()->postMessage(m); // inform command about new picture
1456 case OVGcreateEGLImage: //imcount++;
1458 PictureInfo* info = (PictureInfo*) command.data;
1461 handle = vgCreateImage(VG_sABGR_8888, info->width, info->height, VG_IMAGE_QUALITY_BETTER);
1462 // Log::getInstance()->log("OSD", Log::DEBUG, "TVMedia OVGcreateEGLImage %d %d %x %d",info->width,info->height, handle,imcount);
1464 info->handle = handle;
1465 info->reference = eglCreateImageKHR(egl_display, egl_context, EGL_VG_PARENT_IMAGE_KHR, (EGLClientBuffer)handle, NULL);
1467 if (info->reference) return true;
1470 Log::getInstance()->log("OSD", Log::DEBUG, "TVMedia OVGcreateEGLImage %d %d %x Fail!", info->width, info->height, handle);
1472 if (handle) vgDestroyImage(handle);
1479 case OVGreadyEGLImage:
1481 PictureInfo* info = (PictureInfo*) command.data;
1482 eglDestroyImageKHR(egl_display, info->reference);
1483 bool static_image = true;
1485 if (info->lindex & 0xffffffff) static_image = false;
1487 Message* m = new Message();
1489 m->p_to = Message::CONTROL;
1490 m->data = reinterpret_cast<void*>(info->handle);
1494 m->message = Message::NEW_PICTURE;
1495 m->tag = info->lindex;
1499 m->message = Message::NEW_PICTURE_STATIC;
1500 m->tag = info->lindex >> 32LL;
1503 MessageQueue::getInstance()->postMessage(m); // inform command about new picture
1510 case OVGcreateColorRef :
1513 handle = vgCreatePaint();
1514 DrawStyle* style = (DrawStyle*)command.data;
1518 case DrawStyle::Color:
1520 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
1521 vgSetColor(handle, command.param1);
1523 //rgba = vgGetColor((VGPaint)handle);
1524 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Paint %d %x %x",handle,command.param1,rgba);
1527 case DrawStyle::GradientLinear:
1529 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_LINEAR_GRADIENT);
1530 VGfloat params[] = {style->x1, style->y1, style->x2, style->y2, style->r};
1531 //Log::getInstance()->log("OSD", Log::DEBUG, "Draw Gradient %d %g %g %g %g",handle,params[0],params[1],params[2],params[3]);
1532 vgSetParameterfv(handle, VG_PAINT_LINEAR_GRADIENT, 4, params);
1538 case DrawStyle::GradientRadial:
1540 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_RADIAL_GRADIENT);
1541 VGfloat params[] = {style->x1, style->y1, style->x2, style->y2, style->r};
1542 vgSetParameterfv(handle, VG_PAINT_RADIAL_GRADIENT, 5, params);
1549 if (style->ft == DrawStyle::GradientLinear || style->ft == DrawStyle::GradientRadial)
1551 VGfloat colorramp[5 * 5];
1552 colorramp[0 + 0 * 5] = 0.f;
1553 colorramp[1 + 0 * 5] = ((float)style->red) / 255.f;
1554 colorramp[2 + 0 * 5] = ((float)style->green) / 255.f;
1555 colorramp[3 + 0 * 5] = ((float)style->blue) / 255.f;
1556 colorramp[4 + 0 * 5] = ((float)style->alpha) / 255.f;
1558 for (int i = 0; i < (style->num_colors - 1); i++)
1560 colorramp[0 + (i + 1) * 5] = style->grad_pos[i];
1561 colorramp[1 + (i + 1) * 5] = ((float)style->grad_col[i].red) / 255.f;
1562 colorramp[2 + (i + 1) * 5] = ((float)style->grad_col[i].green) / 255.f;
1563 colorramp[3 + (i + 1) * 5] = ((float)style->grad_col[i].blue) / 255.f;
1564 colorramp[4 + (i + 1) * 5] = ((float)style->grad_col[i].alpha) / 255.f;
1567 colorramp[0 + (style->num_colors) * 5] = 1.f;
1568 colorramp[1 + (style->num_colors) * 5] = ((float)style->grad_col[style->num_colors - 1].red) / 255.f;
1569 colorramp[2 + (style->num_colors) * 5] = ((float)style->grad_col[style->num_colors - 1].green) / 255.f;
1570 colorramp[3 + (style->num_colors) * 5] = ((float)style->grad_col[style->num_colors - 1].blue) / 255.f;
1571 colorramp[4 + (style->num_colors) * 5] = ((float)style->grad_col[style->num_colors - 1].alpha) / 255.f;
1572 vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_SPREAD_MODE, VG_COLOR_RAMP_SPREAD_REFLECT);
1573 vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_PREMULTIPLIED, VG_FALSE);
1574 vgSetParameterfv(handle, VG_PAINT_COLOR_RAMP_STOPS, 5 + (style->num_colors) * 5, colorramp);
1581 case OVGimageUploadLine:
1589 bool OsdOpenVG::processTasks()
1591 bool worked = false;
1595 while (vgcommands.size() > 0)
1597 OpenVGCommand comm = vgcommands.front();
1598 vgcommands.pop_front();
1601 OpenVGResponse resp;
1602 resp.result = handleTask(comm);
1608 vgresponses.push_back(resp);
1614 /* Getting rid of Signal class. As with VideoOMX, just replicate what Signal did here
1615 * and figure out if any of this can be simplified later. e.g. taskmutex sounds
1616 * like it should be the mutex being used. 3 mutexes here???
1619 vgTaskSignalMutex.lock();
1620 vgTaskSignal.notify_one();
1621 vgTaskSignalMutex.unlock();
1634 bool OsdOpenVG::haveOpenVGResponse(unsigned int id, unsigned int* resp)
1638 if (vgresponses.size() > 0)
1640 std::deque<OpenVGResponse>::iterator itty = vgresponses.begin();
1642 while (itty != vgresponses.end())
1644 if ((*itty).id == id)
1646 *resp = (*itty).result;
1660 unsigned int OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm, bool wait)
1671 comm.id = 0; // we are not waiting
1674 vgcommands.push_back(comm);
1682 if (!haveOpenVGResponse(comm.id, &resp))
1685 // Log::getInstance()->log("OsdOpenVG", Log::DEBUG, "putOpenVGCommand wait_for");
1686 std::unique_lock<std::mutex> ul(vgTaskSignalMutex);
1687 /*auto a = */ vgTaskSignal.wait_for(ul, std::chrono::milliseconds(100));
1690 if (a == std::cv_status::timeout)
1691 Log::getInstance()->log("OsdOpenVG", Log::DEBUG, "putOpenVGCommand timeout");
1693 Log::getInstance()->log("OsdOpenVG", Log::DEBUG, "putOpenVGCommand no timeout");
1696 struct timespec target_time;
1697 clock_gettime(CLOCK_REALTIME,&target_time);
1698 target_time.tv_nsec+=1000000LL*100;
1699 if (target_time.tv_nsec>999999999) {
1700 target_time.tv_nsec-=1000000000L;
1701 target_time.tv_sec+=1;
1703 vgtaskSignal.waitForSignalTimed(&target_time);
1719 void OsdOpenVG::destroyImageRef(ImageIndex index)
1721 struct OpenVGCommand comm;
1722 comm.task = OVGdestroyImageRef;
1723 comm.param1 = index;
1724 putOpenVGCommand(comm, false);
1728 bool OsdOpenVG::getStaticImageData(unsigned int static_id, UCHAR** userdata, ULONG* length)
1730 if (sa_MAX > static_id)
1732 *userdata = static_artwork_begin[static_id];
1733 *length = static_artwork_end[static_id] - static_artwork_begin[static_id];
1743 void OsdOpenVG::createPicture(struct PictureInfo& pict_inf)
1745 struct OpenVGCommand comm;
1746 Log::getInstance()->log("OsdOpenVG", Log::DEBUG, "TVMedia Create Picture %d", pict_inf.type);
1748 if (pict_inf.type == PictureInfo::RGBAMemBlock)
1750 comm.task = OVGcreateImageMemory;
1751 comm.data = new PictureInfo(pict_inf);
1752 putOpenVGCommand(comm, false);
1754 else if (pict_inf.type == PictureInfo::EGLImage)
1756 comm.task = OVGreadyEGLImage;
1757 comm.data = new PictureInfo(pict_inf);
1758 putOpenVGCommand(comm, false);
1763 pict_inf.decoder->freeReference(pict_inf.reference);
1769 bool OsdOpenVG::getEGLPicture(struct OsdVector::PictureInfo& info, EGLDisplay* display)
1771 struct OpenVGCommand comm;
1772 info.type = PictureInfo::EGLImage;
1773 comm.task = OVGcreateEGLImage;
1775 *display = egl_display;
1776 return putOpenVGCommand(comm, true);
1781 ImageIndex OsdOpenVG::createMonoBitmap(void* base, int width, int height)
1783 struct OpenVGCommand comm;
1784 comm.task = OVGcreateMonoBitmap;
1785 comm.param1 = width;
1786 comm.param2 = height;
1788 return putOpenVGCommand(comm, true);
1791 ImageIndex OsdOpenVG::createImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data)
1793 struct OpenVGCommand comm;
1794 comm.task = OVGcreateImagePalette;
1795 comm.param1 = width;
1796 comm.param2 = height;
1797 comm.data = image_data;
1798 comm.data2 = palette_data;
1799 return putOpenVGCommand(comm, true);
1802 void OsdOpenVG::destroyDrawStyleHandle(VectorHandle index)
1804 struct OpenVGCommand comm;
1805 comm.task = OVGdestroyPaint;
1806 comm.param1 = index;
1807 putOpenVGCommand(comm, false);
1810 VectorHandle OsdOpenVG::createDrawStyleHandle(const DrawStyle& c)
1812 unsigned int col = c.rgba();
1813 struct OpenVGCommand comm;
1814 comm.task = OVGcreateColorRef;
1815 comm.param1 = col << 8 | (col & 0xff000000) >> 24;
1817 return putOpenVGCommand(comm, true);