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/>.
22 #include <sys/syscall.h>
23 #include <fontconfig/fontconfig.h>
30 #include "messagequeue.h"
31 #include "teletxt/txtfont.h"
34 #include "osdopenvg.h"
36 static const char* TAG = "OsdOpenVG";
38 #define EXTERNALPICTURE(name, fname, fileextension) extern uint8_t name ## _data[] asm("_binary_other_"#fname"_"#fileextension"_start"); \
39 extern uint8_t name ## _data_end[] asm("_binary_other_"#fname"_"#fileextension"_end");
43 #undef EXTERNALPICTURE
45 #define BACKBUFFER_WIDTH 1280
46 #define BACKBUFFER_HEIGHT 720
48 OsdOpenVG::OsdOpenVG()
52 lastrendertime = getTimeMS();
54 const char* fontname = "Droid Sans:style=Regular";
55 cur_fontname = static_cast<char*>(malloc(strlen(fontname) + 1));
56 strcpy(cur_fontname, fontname);
58 #define EXTERNALPICTURE(name, fname, fileextension) static_artwork_begin[sa_ ## name]=name ## _data;
62 #undef EXTERNALPICTURE
63 #define EXTERNALPICTURE(name, fname, fileextension) static_artwork_end[sa_ ## name]=name ## _data_end;
67 #undef EXTERNALPICTURE
69 MessageQueue::getInstance()->addReceiver(this);
72 OsdOpenVG::~OsdOpenVG()
74 MessageQueue::getInstance()->removeReceiver(this);
76 if (initted) shutdown();
78 if (cur_fontname) free(cur_fontname);
80 if (freetype_inited) FT_Done_Face(ft_face);
82 for (char* c : fontnames) free(c);
83 for (char* c : fontnames_keys) free(c);
91 if (initted) return 0;
95 //First get connection to egl
96 egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
97 if (egl_display == EGL_NO_DISPLAY)
99 LogNT::getInstance()->crit(TAG, "Could not get egl display! {:#x}", eglGetError());
104 if (eglInitialize(egl_display, NULL, NULL) == EGL_FALSE)
106 LogNT::getInstance()->crit(TAG, "Initialising display failed! {:#x}", eglGetError());
111 const char* query_str = eglQueryString(egl_display, EGL_CLIENT_APIS);
113 if (query_str) LogNT::getInstance()->debug(TAG, query_str);
114 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", eglGetError());
116 query_str = eglQueryString(egl_display, EGL_EXTENSIONS);
118 if (query_str) LogNT::getInstance()->info(TAG, query_str);
119 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", eglGetError());
121 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
123 LogNT::getInstance()->warn(TAG, "Binding openvg api failed! {:#x}", eglGetError());
128 const EGLint attributs[] =
130 EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
131 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
132 EGL_CONFORMANT, EGL_OPENVG_BIT,
134 }; // Here, we might have to select the resolution!
138 if (eglChooseConfig(egl_display, attributs, &egl_ourconfig, 1, &number) == EGL_FALSE)
140 LogNT::getInstance()->warn(TAG, "Choosing egl config failed! {:#x}", eglGetError());
145 egl_context = eglCreateContext(egl_display, egl_ourconfig, NULL, NULL);
147 if (egl_context == EGL_NO_CONTEXT)
149 LogNT::getInstance()->warn(TAG, "Creating egl context failed! {:#x}", eglGetError());
154 // warning broadcom specific, get display size!
155 display_width = display_height = 0;
157 if (graphics_get_display_size(0, reinterpret_cast<uint32_t*>(&display_width), reinterpret_cast<uint32_t*>(&display_height)) < 0)
159 LogNT::getInstance()->warn(TAG, "Getting display size failed! (BCM API)");
164 LogNT::getInstance()->info(TAG, "Displaysize is {} x {}", display_width, display_height);
165 VC_RECT_T dst_rect = {0, 0, display_width, display_height};
166 VC_RECT_T src_rect = {0, 0, BACKBUFFER_WIDTH << 16, BACKBUFFER_HEIGHT << 16};
167 VC_RECT_T src_rect_bg = {0, 0, 16 << 16, 16 << 16};
168 // VC_RECT_T src_rect_im={0,0,16,16};
170 uint32_t back_image_ptr;
171 bcm_backres = vc_dispmanx_resource_create(VC_IMAGE_RGBX32, 16, 16, &back_image_ptr);
173 updateBackgroundColor(DrawStyle::WALLPAPER);
175 DISPMANX_UPDATE_HANDLE_T bcm_update;
176 bcm_display = vc_dispmanx_display_open(0);
177 bcm_update = vc_dispmanx_update_start(0);
178 bcm_element = vc_dispmanx_element_add(bcm_update, bcm_display,
180 &src_rect, DISPMANX_PROTECTION_NONE, 0, 0, static_cast<DISPMANX_TRANSFORM_T>(0));
183 bcm_background = vc_dispmanx_element_add(bcm_update, bcm_display,
184 0, &dst_rect, bcm_backres,
185 &src_rect_bg, DISPMANX_PROTECTION_NONE, 0, 0, static_cast<DISPMANX_TRANSFORM_T>(0));
187 vc_dispmanx_update_submit_sync(bcm_update);
189 static EGL_DISPMANX_WINDOW_T nativewindow;
190 nativewindow.element = bcm_element;
191 nativewindow.height = BACKBUFFER_HEIGHT;
192 nativewindow.width = BACKBUFFER_WIDTH;
194 egl_surface = eglCreateWindowSurface(egl_display, egl_ourconfig, &nativewindow, NULL);
195 if (egl_surface == EGL_NO_SURFACE)
197 LogNT::getInstance()->warn(TAG, "Creating egl window surface failed!");
202 LogNT::getInstance()->debug(TAG, "Making egl current in SYS_gettid: {}", syscall(SYS_gettid));
204 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
206 LogNT::getInstance()->warn(TAG, "Making egl Current failed");
213 query_str = reinterpret_cast<const char*>(vgGetString(VG_VERSION));
215 if (query_str) LogNT::getInstance()->info(TAG, query_str);
216 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
218 query_str = reinterpret_cast<const char*>(vgGetString(VG_VENDOR));
220 if (query_str) LogNT::getInstance()->info(TAG, query_str);
221 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
223 query_str = reinterpret_cast<const char*>(vgGetString(VG_RENDERER));
225 if (query_str) LogNT::getInstance()->info(TAG, query_str);
226 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
228 query_str = reinterpret_cast<const char*>(vgGetString(VG_EXTENSIONS));
230 if (query_str) LogNT::getInstance()->info(TAG, query_str);
231 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
233 aspect_correction = BACKBUFFER_HEIGHT / 576.f / (BACKBUFFER_WIDTH / 720.f);
236 if (!fontnames.size())
238 //inspired by and copied from vdr's code
240 FcObjectSet* objset = FcObjectSetBuild(FC_FAMILY, FC_STYLE, NULL);
241 FcPattern* pattern = FcPatternCreate();
242 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
243 FcFontSet* fonts = FcFontList(NULL, pattern, objset);
245 for (int i = 0; i < fonts->nfont; i++)
247 char* s = reinterpret_cast<char*>(FcNameUnparse(fonts->fonts[i]));
251 char* c = strchr(s, ':');
255 char* s2 = strchr(c + 1, ',');
260 char* p = strchr(s, ',');
265 else memmove(p, c, strlen(c) + 1);
268 char* key = (char*)malloc(strlen(s) + 1);
270 fontnames_keys.push_back(key);
271 char* s2 = strstr(s, "style=");
275 memmove(s2, s2 + 6, strlen(s2 + 6) + 1);
278 fontnames.push_back(s);
282 FcFontSetDestroy(fonts);
283 FcPatternDestroy(pattern);
284 FcObjectSetDestroy(objset);
287 if (!loadFont(false))
292 vgttfont = vgCreateFont(0);
293 vgttpaint = vgCreatePaint();
294 vgSetParameteri( vgttpaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
295 vgSetColor(vgttpaint, 0xffffffff);
297 eglSwapInterval(egl_display, 1);
299 LogNT::getInstance()->debug(TAG, "Making egl current out SYS_gettid {}", syscall(SYS_gettid));
300 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
301 //Now we will create the Screen
302 initted = true; // must set this here or create surface won't work
304 /*if (((VideoOMX*)Video::getInstance())->initUsingOSDObjects()!=1) { //call Video for init stuff
309 renderThreadMutex.lock(); // start-protect
311 renderThread = std::thread( [this]
313 renderThreadMutex.lock();
314 renderThreadMutex.unlock();
317 renderThreadMutex.unlock();
324 #ifdef PICTURE_DECODER_OMX
325 imageomx2 = new ImageOMX2(/*egl_display*/);
326 ImageLoader::getInstance()->addDecoder(imageomx2);
332 void OsdOpenVG::renderThreadStop()
334 LogNT::getInstance()->warn(TAG, "renderThreadStop");
336 renderThreadMutex.lock();
337 renderThreadReq = 2; // req-stop
338 renderThreadCond.notify_one(); // in case renderLoop is in cond_wait
339 renderThreadMutex.unlock();
343 int OsdOpenVG::stopUpdate()
346 processOpenVGCommands();
350 int OsdOpenVG::shutdown()
352 #ifdef PICTURE_DECODER_OMX
353 if (imageomx2) ImageLoader::getInstance()->removeDecoder(imageomx2);
357 if (!initted) return 0;
360 LogNT::getInstance()->debug(TAG, "shutdown mark1");
362 LogNT::getInstance()->debug(TAG, "shutdown mark1a");
364 //(((VideoOMX*)Video::getInstance())->shutdownUsingOSDObjects());
365 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
367 LogNT::getInstance()->warn(TAG, "Making egl Current failed in shutdown {:#x}", eglGetError());
370 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
372 LogNT::getInstance()->warn(TAG, "Binding openvg api thread failed! {:#x}", eglGetError());
375 LogNT::getInstance()->debug(TAG, "shutdown mark2");
376 processOpenVGCommands();
377 LogNT::getInstance()->debug(TAG, "shutdown mark3");
382 //purgeAllReferences();
384 vgDestroyFont(vgfont);
385 vgDestroyFont(vgttfont);
386 vgDestroyPaint(vgttpaint);
388 float colclear[] = {0.8f, 0.8f, 0.8f, 1.f};
389 vgSetfv(VG_CLEAR_COLOR, 4, colclear);
390 vgClear(0, 0, BACKBUFFER_WIDTH, BACKBUFFER_HEIGHT);
391 eglSwapBuffers(egl_display, egl_surface);
392 LogNT::getInstance()->debug(TAG, "Making egl current out final");
393 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
395 if (eglDestroySurface(egl_display, egl_surface) == EGL_FALSE)
397 LogNT::getInstance()->error(TAG, "eglDestroySurface failed {:#x}", eglGetError());
400 if (eglDestroyContext(egl_display, egl_context) == EGL_FALSE)
402 LogNT::getInstance()->error(TAG, "eglDestroyContext failed {:#x}", eglGetError());
405 if (eglTerminate(egl_display) == EGL_FALSE)
407 LogNT::getInstance()->error(TAG, "eglTerminate failed {:#x}", eglGetError());
410 DISPMANX_UPDATE_HANDLE_T bcm_update;
411 bcm_update = vc_dispmanx_update_start(0);
413 vc_dispmanx_element_remove(bcm_update, bcm_element);
414 vc_dispmanx_element_remove(bcm_update, bcm_background);
415 vc_dispmanx_update_submit_sync(bcm_update);
416 vc_dispmanx_resource_delete(bcm_backres);
418 vc_dispmanx_display_close(bcm_display);
422 void OsdOpenVG::processMessage(Message* m)
424 if (m->message == Message::NEW_PICTURE)
426 logger->debug(TAG, "NEW_PICTURE");
428 OsdImageBase* newImageOVG = new OsdImageOpenVG(reinterpret_cast<VGImage>(m->data));
429 OsdImage oImage(newImageOVG);
433 ImageLoader::getInstance()->pictureRendered(reinterpret_cast<LoadingIndex>(m->tag), oImage);
434 // The above returns false if it does not accept the OsdImage
435 // We just drop out here and leave the only reference to it in Osd::images
436 // It will be deleted properly on the next garbage collection run
438 // OSDOVG-ROD-EXPERIMENT
439 logger->debug(TAG, "EXPERIMENT - call doRender");
444 void OsdOpenVG::garbageCollectOsdImages()
446 // Only use references into images
447 // Therefore if use_count == 1 the only copy of the OsdImage is in images and therefore is dead
450 for(auto& oi : images)
452 if (oi.use_count() == 1)
453 destroyImageRef(std::static_pointer_cast<OsdImageOpenVG>(oi)->getVGImage());
456 auto it = std::remove_if(images.begin(), images.end(), [](OsdImage& oi) { return oi.use_count() == 1; });
457 images.erase(it, images.end());
459 // C++20: std::erase_if(images, [](OsdImage oi) { return oi.use_count() == 1; });
464 // OSDOVG-ROD-EXPERIMENT - temp hack to allow OsdVector to signal the thread in OsdOpenVG
465 void OsdOpenVG::doRender()
467 LogNT::getInstance()->debug(TAG, "Nudge render thread");
469 // signal renderLoop to go-around (from putOpenVGCommand)
470 renderThreadMutex.lock();
471 if (renderThreadReq == 0) renderThreadReq = 1;
472 renderThreadCond.notify_one();
473 renderThreadMutex.unlock();
477 * OSDOVG-ROD-EXPERIMENT
479 * To switch OsdOpenVG to render-on-demand instead of rendering in a loop
482 * More responsive OSD
483 * Cuts 5% constant CPU usage
484 * Seems to have tickled the GFX corruption bug, might make it easier to find that - update: found and fixed!!
487 * There's a comment about video tearing if render is called too often.
488 * Will tearing show up in on-demand mode?
489 * Moves away from conventional FPS type render loop. VOMP doesn't use 3D GFX / animations
490 * currently, so this doesn't matter at the moment?
491 * Code needs updating to call doRender,
492 * e.g. backing out of the options screen doesn't redraw VWelcome, fixed now
494 * Maybe have a hybrid system. One that normally works on-demand but can
495 * also switch to looping on the fly if necessary.
499 void OsdOpenVG::renderLoop()
501 // We have to claim the egl context for this thread
502 LogNT::getInstance()->info(TAG, "Entering drawing thread");
504 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
506 LogNT::getInstance()->warn(TAG, "Making egl Current failed in thread {:#x}", eglGetError());
510 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
512 LogNT::getInstance()->warn(TAG, "Binding openvg api thread failed! {:#x}", eglGetError());
516 std::unique_lock<std::mutex> ul(renderThreadMutex, std::defer_lock);
523 long long time1 = getTimeMS();
525 // OSDOVG-ROD-EXPERIMENT - always render, but only when signalled to
526 if (1) // ((time1 - lastrendertime) > 200) //5 fps for OSD updates are enough, avoids tearing
531 LogNT::getInstance()->debug(TAG, "EXPERIMENT - render");
534 lastrendertime = getTimeMS();
540 ts = time1 - lastrendertime;
543 if (processOpenVGCommands()) ts = 0;
546 if (renderThreadReq == 2)
548 if (eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) == EGL_FALSE)
549 LogNT::getInstance()->warn(TAG, "Making egl Current out thread failed");
556 // OSDOVG-ROD-EXPERIMENT
559 renderThreadCond.wait(ul, [this]{ return renderThreadReq != 0; }); // experiment! always wait, no time limit
560 //renderThreadCond.wait_for(ul, std::chrono::milliseconds(ts), [this]{ return renderThreadReq != 0; });
562 if (renderThreadReq == 2)
564 if (eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) == EGL_FALSE)
565 LogNT::getInstance()->warn(TAG, "Making egl Current out thread failed");
570 if (renderThreadReq == 0) abort(); // OSDOVG-ROD-EXPERIMENT - check this never happens - confirmed
572 // Either timeout or renderThreadReq == 1 - go around
581 u4 OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm, bool wait)
583 if (wait) comm.response = new OpenVGResponse;
585 std::unique_lock<std::mutex> ul(taskmutex); // locks
586 vgcommands.push_back(comm);
589 // signal renderLoop to go-around
590 renderThreadMutex.lock();
591 if (renderThreadReq == 0) renderThreadReq = 1;
592 renderThreadCond.notify_one();
593 renderThreadMutex.unlock();
598 // Wait until done is true and we are signalled. If done is already true, this returns immediately
599 comm.response->doneCond.wait(ul, [&comm]{ return comm.response->done; });
601 // Now response->done = true, mutex is locked, we have been signalled
604 u4 toReturn = comm.response->result;
605 delete comm.response;
609 bool OsdOpenVG::processOpenVGCommands()
614 while (vgcommands.size() > 0)
618 OpenVGCommand comm = vgcommands.front();
619 vgcommands.pop_front();
623 u4 vgResult = handleOpenVGCommand(comm);
627 if (comm.response) // There is a response object, a thread is waiting
629 comm.response->result = vgResult;
630 comm.response->done = true;
631 comm.response->doneCond.notify_one();
639 void OsdOpenVG::InternalRendering()
642 Colour bg = DrawStyle::WALLPAPER;
643 float colclear[] = {1.f, 1.0f, 1.f, 1.f};
645 if (bg.alpha == 0) colclear[3] = 0.f;
647 vgSetfv(VG_CLEAR_COLOR, 4, colclear);
648 vgClear(0, 0, BACKBUFFER_WIDTH, BACKBUFFER_HEIGHT);
649 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
651 drawSurfaces(); //iterate through and draws all commands
653 //Show it to the user!
654 eglSwapBuffers(egl_display, egl_surface);
658 // --------------------------------------------------------------------------------------------------------------------------
661 void OsdOpenVG::purgeAllReferences()
664 drawstyleHandlesRefCounts.clear(); // remove all references
667 map<void *,VectorHandleImage>::iterator mitty=monobitmaps.begin();
668 while (mitty!=monobitmaps.end()) {
669 vgDestroyImage((VGImage)(*mitty).second);
674 / *map<string,VectorHandleImage>::iterator jitty=jpegs.begin();
675 while (jitty!=jpegs.end()) {
676 vgDestroyImage((VGImage)(*jitty).second);
681 map<pair<Colour*,unsigned int>,unsigned int>::iterator sitty=drawstyleHandles.begin();
682 while (sitty!=drawstyleHandles.end()) {
683 vgDestroyPaint((VGPaint)(*sitty).second);
686 drawstyleHandles.clear();
690 void OsdOpenVG::updateBackgroundColor(DrawStyle bg)
692 unsigned int color[16 * 16];
694 if (!bcm_backres) return;
696 VC_RECT_T src_rect_im = {0, 0, 16, 16};
698 if (bg.ft != DrawStyle::GradientLinear)
703 //memset(color,0xFF,sizeof(unsigned int)*4*8);
704 for (int j = 0; j < 16; j++)
706 int helpr = (((15 - j) * bg.red) + (j * bg.grad_col[0].red)) / 15;
707 int helpb = (((15 - j) * bg.blue) + (j * bg.grad_col[0].blue)) / 15;
708 int helpg = (((15 - j) * bg.green) + (j * bg.grad_col[0].green)) / 15;
709 //unsigned int cur_col=help | (help<< 8) | (help<< 16) | (0xFF<< 24);
710 unsigned int cur_col = helpr | (helpg << (8)) | (helpb << (16)) | (0xFF << (24));
712 for (int i = 0; i < 16; i++)
714 color[i + 16 * j] = cur_col;
718 vc_dispmanx_resource_write_data(bcm_backres, VC_IMAGE_RGBX32, 16 * 4, color, &src_rect_im);
721 void OsdOpenVG::getScreenSize(int& width, int& height)
723 width = BACKBUFFER_WIDTH;
724 height = BACKBUFFER_HEIGHT;
727 void OsdOpenVG::getRealScreenSize(int& width, int& height)
729 width = display_width;
730 height = display_height;
733 bool OsdOpenVG::screenShotInternal(void* buffer, int width, int height, bool includeOSD)
735 if (!initted) return false;
737 if (!buffer) return false;
739 DISPMANX_RESOURCE_HANDLE_T res;
740 DISPMANX_DISPLAY_HANDLE_T display;
744 res = vc_dispmanx_resource_create(VC_IMAGE_RGBA32, tou4(width), tou4(height), &image_ptr);
745 display = vc_dispmanx_display_open(0);
749 vc_dispmanx_snapshot(display, res,
750 static_cast<DISPMANX_TRANSFORM_T>(DISPMANX_SNAPSHOT_NO_RGB | DISPMANX_SNAPSHOT_FILL/*|DISPMANX_SNAPSHOT_PACK*/));
754 vc_dispmanx_snapshot(display, res,
755 static_cast<DISPMANX_TRANSFORM_T>(DISPMANX_SNAPSHOT_FILL));
758 vc_dispmanx_rect_set(&rect, 0, 0, tou4(width), tou4(height));
759 vc_dispmanx_resource_read_data(res, &rect, buffer, width * 4);
760 vc_dispmanx_resource_delete(res);
761 vc_dispmanx_display_close(display);
765 void OsdOpenVG::initPaths()
768 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
769 VG_PATH_DATATYPE_F, 1.f, 0.f,
770 0, 0, VG_PATH_CAPABILITY_ALL);
772 vguLine(current, 0.f, 0.f, 1.f, 0.f);
774 std_paths[PIHorzLine] = current;
776 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
777 VG_PATH_DATATYPE_F, 1.f, 0.f,
778 0, 0, VG_PATH_CAPABILITY_ALL);
779 vguLine(current, 0.f, 0.f, 0.f, 1.f);
781 std_paths[PIVertLine] = current;
783 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
784 VG_PATH_DATATYPE_F, 1.f, 0.f,
785 0, 0, VG_PATH_CAPABILITY_ALL);
786 //vguRect(current,0.f,0.f,1.f,1.f);
787 vguRect(current, 0.f, 0.f, 1.f, 1.f);
789 std_paths[PIRectangle] = current;
791 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
792 VG_PATH_DATATYPE_F, 1.f, 0.f,
794 vguEllipse(current, 0.f, 0.f, 1.f, 1.f);
796 std_paths[PIPoint] = current;
799 void OsdOpenVG::destroyPaths()
801 vgDestroyPath(std_paths[PIHorzLine]);
802 vgDestroyPath(std_paths[PIVertLine]);
803 vgDestroyPath(std_paths[PIRectangle]);
804 vgDestroyPath(std_paths[PIPoint]);
809 float OsdOpenVG::getFontHeight()
811 return font_height; //dummy
814 float OsdOpenVG::getCharWidth(wchar_t c)
816 if (c < 256) return byte_char_width[c];
818 unsigned int glyph_index = FT_Get_Char_Index(ft_face, c);
819 return font_exp_x[glyph_index];
822 unsigned int OsdOpenVG::loadTTchar(cTeletextChar c)
824 unsigned int glyph_index = c.getGlyphIndex();
826 if (tt_font_chars.find(glyph_index) != tt_font_chars.end())
831 unsigned int buffer[10];
832 const VGfloat glyphOrigin[] = { 0.f, 0.f };
833 const VGfloat escapement[] = { 12.f, 0.f };
834 unsigned int* charmap = GetFontChar(c, buffer);
836 if (!charmap) return 0; //invalid char
838 for (int i = 0; i < 10; i++)
840 buffer[i] = charmap[i] >> 4;
843 VGImage handle = vgCreateImage(
847 VG_IMAGE_QUALITY_NONANTIALIASED | VG_IMAGE_QUALITY_FASTER
848 | VG_IMAGE_QUALITY_BETTER);
849 vgImageSubData(handle, buffer, 4, VG_A_1, 0, 0, 12, 10);
853 handle, const_cast<VGfloat*>(glyphOrigin), const_cast<VGfloat*>(escapement));
854 vgDestroyImage(handle);
855 tt_font_chars[glyph_index] = 1;
860 int OsdOpenVG::getFontNames(const char*** names, const char*** names_keys)
862 *names = const_cast<const char**>(&(fontnames[0]));
863 *names_keys = const_cast<const char**>(&(fontnames_keys[0]));
864 return toi4(fontnames.size());
867 void OsdOpenVG::setFont(const char* fontname)
869 if (strcmp(fontname, cur_fontname))
872 if (cur_fontname) free(cur_fontname);
874 cur_fontname = static_cast<char*>(malloc(strlen(fontname) + 1));
875 strcpy(cur_fontname, fontname);
877 struct OpenVGCommand comm;
878 comm.task = OVGreplacefont;
879 putOpenVGCommand(comm, false);
883 int OsdOpenVG::loadFont(bool newfont)
886 float font_size = 16.f;
888 if (!freetype_inited)
890 error = FT_Init_FreeType(&ft_library);
894 LogNT::getInstance()->warn(TAG, "Could not load freetype {:#x}", error);
899 if (!freetype_inited || newfont)
901 //first get the filename algorith extracted from vdr by Klaus Schmidinger
903 FcPattern* pattern = FcNameParse(reinterpret_cast<FcChar8*>(cur_fontname));
904 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
905 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
906 FcDefaultSubstitute(pattern);
908 FcFontSet* fonts = FcFontSort(NULL, pattern, FcFalse, NULL, &fres);
909 FcChar8* filename = NULL;
913 for (int i = 0; i < fonts->nfont; i++)
916 FcPatternGetBool(fonts->fonts[i], FC_SCALABLE, 0, &canscale);
920 FcPatternGetString(fonts->fonts[i], FC_FILE, 0, &filename);
925 FcFontSetDestroy(fonts);
929 LogNT::getInstance()->crit(TAG, "Could not locate a font! Abort!");
933 FcPatternDestroy(pattern);
935 LogNT::getInstance()->info(TAG, "Load Font {}: {}", cur_fontname, filename);
936 // second load the font
938 error = FT_New_Face(ft_library, reinterpret_cast<const char*>(filename), 0, &new_ft_face);
942 LogNT::getInstance()->warn(TAG, "Could not load font face {:#x} {}", error, filename);
946 error = FT_Set_Char_Size(new_ft_face, 0, font_size * 256, 0, 0 /*dpi*/);
950 FT_Done_Face(new_ft_face);
951 LogNT::getInstance()->warn(TAG, "Could not set face size {:#x}", error);
955 FT_Face old_ft_face = ft_face; // do it thread safe
956 ft_face = new_ft_face;
958 if (freetype_inited) FT_Done_Face(old_ft_face);//
960 freetype_inited = true;
963 vgfont = vgCreateFont(0);
966 font_height = ft_face->size->metrics.height / 256.f;
967 cur_char = FT_Get_First_Char(ft_face, &glyph);
968 std::vector<VGubyte> segments;
969 std::vector<VGfloat> coord;
970 segments.reserve(256);
973 //LogNT::getInstance()->debug(TAG, "Create Glyph test {} {} {} {}",cur_char,font_data_end,font_data,glyph);
976 error = FT_Load_Glyph(ft_face, glyph, FT_LOAD_DEFAULT);
980 cur_char = FT_Get_Next_Char(ft_face, cur_char, &glyph);
981 LogNT::getInstance()->warn(TAG, "Could not load glyph {:#x} {}", error);
986 FT_Outline ot = ft_face->glyph->outline;
990 if (ot.n_contours == 0)
992 path = VG_INVALID_HANDLE;
996 path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F,
997 1.0f, 0.f, 0, 0, VG_PATH_CAPABILITY_ALL);
1000 FT_Vector* pt = ot.points;
1001 const char* tags = ot.tags;
1002 const short* cont = ot.contours;
1003 short n_cont = ot.n_contours;
1005 //short n_point=ot.n_points;
1006 //short last_cont=0;
1007 for (short point = 0; n_cont != 0; cont++, n_cont--)
1009 short next_cont = *cont + 1;
1012 short first_point = point;
1014 //LogNT::getInstance()->debug(TAG, "runs {}", *cont);
1015 for (; point < next_cont; point++)
1017 char tag = tags[point];
1018 FT_Vector fpoint = pt[point];
1020 // LogNT::getInstance()->debug(TAG, "tag %d point {} {}: {} {}",tag,fpoint.x,fpoint.y,point,n_point);
1023 segments.push_back(VG_MOVE_TO);
1026 else if (tag & 0x1) //on curve
1030 segments.push_back(VG_LINE_TO);
1032 else if (last_tag & 0x2)
1034 segments.push_back(VG_CUBIC_TO);
1038 segments.push_back(VG_QUAD_TO);
1046 if (!(last_tag & 0x1))
1048 segments.push_back(VG_QUAD_TO);
1049 u4 coord_size = coord.size();
1050 VGfloat x = (coord[coord_size - 2] + ((float)fpoint.x) / 256.f) * 0.5f * aspect_correction;
1051 VGfloat y = (coord[coord_size - 1] + (font_size - ((float)fpoint.y) / 256.f)) * 0.5f;
1059 coord.push_back(((float)fpoint.x)*aspect_correction / 256.);
1060 coord.push_back(font_size - ((float)fpoint.y) / 256.);
1061 //LogNT::getInstance()->debug(TAG, "Create APD Glyph coord {} {} {} {}",fpoint.x,fpoint.y,coord[coord.size()-2],coord[coord.size()-1]);
1064 if (!(last_tag & 0x1))
1068 segments.push_back(VG_CUBIC_TO);
1072 segments.push_back(VG_QUAD_TO);
1075 coord.push_back(((float)pt[first_point].x)*aspect_correction / 256.);
1076 coord.push_back(font_size - ((float)pt[first_point].y) / 256.);
1079 //segments.push_back(VG_CLOSE_PATH);
1082 vgAppendPathData(path, segments.size(), &segments[0], &coord[0]);
1083 /* for (int m=0;m<segments.size();m++) {
1085 switch (segments[m])
1088 LogNT::getInstance()->debug(TAG, "Move To {} {}",coord[n],coord[n+1]);n+=2; break;
1090 LogNT::getInstance()->debug(TAG, "Line To {} {}",coord[n],coord[n+1]);n+=2; break;
1092 LogNT::getInstance()->debug(TAG, "Cubic To {} {} {} {} {} {}",coord[n],coord[n+1],coord[n+2],coord[n+3],coord[n+4],coord[n+5]);n+=6; break;
1094 LogNT::getInstance()->debug(TAG, "Quad To {} {} {} {}",coord[n],coord[n+1],coord[n+2],coord[n+3]);n+=4; break;
1096 LogNT::getInstance()->debug(TAG, "Close Path"); break;
1100 //vguRect(path,0.f,0.f,1.f,1.f);
1101 //LogNT::getInstance()->debug(TAG, "Create APD Glyph {} {:#x}",segments.size(),vgGetError());
1104 VGfloat ori[] = {0.f, 0.f};
1105 VGfloat esp[] = {ft_face->glyph->advance.x / 256.f * aspect_correction, ft_face->glyph->advance.y / 256.f};
1106 font_exp_x[glyph] = ft_face->glyph->advance.x / 256.f * aspect_correction; //recalculate
1108 vgSetGlyphToPath(vgfont, glyph, path, VG_FALSE, ori, esp);
1110 //LogNT::getInstance()->debug(TAG, "Create Glyph {} {} {:#x}",path,glyph,vgGetError());
1111 if (path != VG_INVALID_HANDLE)
1113 vgDestroyPath(path);
1116 cur_char = FT_Get_Next_Char(ft_face, cur_char, &glyph);
1119 for (u4 i = 0; i < 256; i++)
1121 u4 glyph_index = FT_Get_Char_Index(ft_face, i);
1122 byte_char_width[i] = font_exp_x[glyph_index];
1128 void OsdOpenVG::drawSetTrans(SurfaceInfo& sc)
1130 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1132 vgScale(BACKBUFFER_WIDTH / 720.f, -BACKBUFFER_HEIGHT / 576.f);
1133 vgTranslate(0.f + sc.x, -576.f + sc.y);
1135 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1137 vgScale(BACKBUFFER_WIDTH / 720.f, -BACKBUFFER_HEIGHT / 576.f);
1138 vgTranslate(0.f + sc.x, -576.f + sc.y);
1140 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1142 vgScale(BACKBUFFER_WIDTH / 720.f, -BACKBUFFER_HEIGHT / 576.f);
1143 vgTranslate(0.f + sc.x, -576.f + sc.y);
1144 clip_shift_x = static_cast<i4>(sc.x);
1145 clip_shift_y = static_cast<i4>(sc.y);
1147 //vgTranslate(0.f+sc.x,576.f-sc.y);
1148 //LogNT::getInstance()->debug(TAG, "Draw Translate {} {}",sc.x,sc.y);
1151 void OsdOpenVG::executeDrawCommand(SVGCommand& command)
1153 VGfloat save_matrix[9];
1154 VGfloat save_matrix2[9];
1156 switch (command.instr)
1160 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1162 // rgba = vgGetColor((VGPaint) command.handle);
1163 //LogNT::getInstance()->debug(TAG, "Draw Path {} {:#x} {} {} {} {}",command.handle,command.target.path_index,command.x,command.y,command.w,command.h);
1164 //vgSeti(VG_FILL_RULE,);
1166 vgGetMatrix(save_matrix);
1167 vgSetPaint(static_cast<VGPaint>(command.handle), VG_FILL_PATH);
1168 vgSetPaint(static_cast<VGPaint>(command.handle), VG_STROKE_PATH);
1169 vgTranslate(command.x, command.y);
1170 vgScale(command.w, command.h);
1171 vgDrawPath(std_paths[command.target.path_index], VG_FILL_PATH);
1172 vgLoadMatrix(save_matrix);
1179 if (command.instr == DrawBitmap)
1181 vgimage = static_cast<VGImage>(command.target.bitmap);
1183 else // command.instr == DrawImage
1185 // FIXME use shared_ptr for OsdImageOpenVG objects?
1187 // Don't use shared_ptrs for all those CImage types, are they needed?
1188 // Preferring the second option at the moment
1190 OsdImage oi = command.image->getOsdImage();
1191 OsdImageOpenVG* oiovg = std::static_pointer_cast<OsdImageOpenVG>(oi).get();
1192 vgimage = oiovg->getVGImage();
1195 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1196 vgGetMatrix(save_matrix);
1197 VGfloat imagewidth = static_cast<float>(vgGetParameteri(vgimage, VG_IMAGE_WIDTH));
1198 VGfloat imageheight = static_cast<float>(vgGetParameteri(vgimage, VG_IMAGE_HEIGHT));
1200 //vgScale(command.w,command.h);
1202 if (command.handle) //special behaviour for bw images they act as a mask on the current paint
1204 vgTranslate(command.x, command.y);
1205 vgSetPaint(static_cast<VGPaint>(command.handle), VG_FILL_PATH);
1206 vgSetPaint(static_cast<VGPaint>(command.handle), VG_STROKE_PATH);
1207 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_STENCIL);
1208 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1209 vgScale(aspect_correction, 1.f);
1210 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1211 vgGetMatrix(save_matrix2);
1212 vgScale(imagewidth, imageheight);
1216 //vgScale(720.f/((float)BACKBUFFER_WIDTH), 576.f/((float)BACKBUFFER_HEIGHT));
1217 float scalex = command.w / imagewidth;
1218 float scaley = command.h / imageheight;
1219 float tx = command.x;
1220 float ty = command.y;
1222 if (command.corner == TopLeftLimited)
1224 if (scalex != 0.f && scaley != 0.f)
1226 float imageaspect = imagewidth / imageheight;
1227 float boxaspect = command.w / command.h / aspect_correction;
1229 if (imageaspect > boxaspect)
1232 ty += (command.h - imageheight * scalex / aspect_correction) * 0.5f;
1237 tx += (command.w - imagewidth * scaley * aspect_correction) * 0.5f;
1242 if (scalex == 0.f && scaley == 0.f)
1244 scalex = aspect_correction;
1247 else if (scalex == 0.f)
1249 scalex = scaley * aspect_correction;
1251 else if (scaley == 0.f)
1253 scaley = scalex / aspect_correction;
1256 if (command.corner == BottomRight || command.corner == BottomLeft || command.corner == BottomMiddle)
1258 ty -= imageheight * scaley;
1261 if (command.corner == BottomRight || command.corner == TopRight)
1263 tx -= imagewidth * scalex;
1266 if (command.corner == BottomMiddle || command.corner == TopMiddle)
1268 tx -= imagewidth * scalex * 0.5f;
1271 vgTranslate(tx, ty);
1272 //vgScale(command.w/imagewidth,command.h/imageheight);
1273 vgScale(scalex, scaley);
1274 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1275 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1276 //LogNT::getInstance()->debug(TAG, "TVMedia Draw Image Scale {} {} {} {} {} {}",command.w,imagewidth,command.h,imageheight,scalex,scaley);
1280 //vgTranslate(200.f,500.f);
1281 //vgScale(100.f,100.f);
1283 vgDrawImage(vgimage);
1285 //LogNT::getInstance()->debug(TAG, "Draw Image {} {:#x} {} {} {} {} {:#x}",command.handle,vgimage,command.x,command.y,command.w,command.h,
1289 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1290 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1291 vgLoadMatrix(save_matrix2);
1294 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
1296 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1297 vgLoadMatrix(save_matrix);
1302 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1303 vgGetMatrix(save_matrix);
1304 vgSetPaint(static_cast<VGPaint>(command.handle), VG_FILL_PATH);
1305 vgSetPaint(static_cast<VGPaint>(command.handle), VG_STROKE_PATH);
1306 vgTranslate(command.x, command.y);
1307 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1308 vgGetMatrix(save_matrix2);
1309 unsigned int glyph_index = FT_Get_Char_Index(ft_face, command.target.textchar);
1310 vgScale(font_exp_x[glyph_index], font_height);
1312 VGfloat gori[] = {0., 0.};
1313 vgSetfv(VG_GLYPH_ORIGIN, 2, gori);
1315 vgDrawGlyph(vgfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1316 //vgDrawPath(std_paths[Rectangle],VG_FILL_PATH);
1317 /* LogNT::getInstance()->debug(TAG, "Draw Glyph {} {} {} {} {} {:#x}",command.handle,command.target.textchar,glyph_index,command.x,command.y,
1319 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1320 vgLoadMatrix(save_matrix);
1321 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1322 vgLoadMatrix(save_matrix2);
1324 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1329 cTeletextChar tchar;
1330 tchar.setInternal(command.target.ttchar);
1331 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1332 vgGetMatrix(save_matrix);
1333 enumTeletextColor ttforegcolour = tchar.GetFGColor();
1334 enumTeletextColor ttbackgcolour = tchar.GetBGColor();
1336 if (tchar.GetBoxedOut())
1338 ttforegcolour = ttcTransparent;
1339 ttbackgcolour = ttcTransparent;
1342 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_STENCIL);
1343 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1345 vgTranslate(command.x + command.w * 11.85f * 1.4f, command.y + command.h * 19.75f);
1346 VGfloat gori[] = {0., 0.};
1347 vgSetfv(VG_GLYPH_ORIGIN, 2, gori);
1349 vgScale(-1.4f, 2.f);
1350 unsigned int color = Surface::enumTeletextColorToCoulour(ttbackgcolour).rgba();
1351 color = color << 8 | (color & 0xff000000) >> 24;
1352 vgSetColor(vgttpaint, color);
1353 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_FILL_PATH);
1354 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_STROKE_PATH);
1355 cTeletextChar filled;
1356 filled.setInternal(0x187f);
1357 unsigned int glyph_index = loadTTchar(filled);
1358 vgDrawGlyph(vgttfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1360 color = Surface::enumTeletextColorToCoulour(ttforegcolour).rgba();
1361 color = color << 8 | (color & 0xff000000) >> 24;
1362 vgSetColor(vgttpaint, color);
1363 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_FILL_PATH);
1364 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_STROKE_PATH);
1365 glyph_index = loadTTchar(tchar);
1366 vgDrawGlyph(vgttfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1368 /* LogNT::getInstance()->debug(TAG, "Draw TTchar {:#x} {:#x} {:#x} {:#x}",glyph_index,ttforegcolour,Surface::enumTeletextColorToCoulour(ttforegcolour).rgba(),
1369 vgGetColor(vgttpaint));*/
1371 vgLoadMatrix(save_matrix);
1372 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1373 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1374 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
1379 VGint coords[4] = {0, 0, 0, 0};
1380 coords[0] = toi4((command.x + static_cast<float>(clip_shift_x)) * BACKBUFFER_WIDTH / 720.f);
1381 coords[1] = toi4((576.f - command.y - static_cast<float>(clip_shift_y) - command.h) * BACKBUFFER_HEIGHT / 576.f);
1382 coords[2] = toi4((command.w - 1.f) * BACKBUFFER_WIDTH / 720.f);
1383 coords[3] = toi4((command.h - 1.f) * BACKBUFFER_HEIGHT / 576.f);
1384 vgSetiv(VG_SCISSOR_RECTS, 4, coords);
1386 if (command.w == 0.f && command.h == 0.f)
1387 vgSeti(VG_SCISSORING, VG_FALSE);
1389 vgSeti(VG_SCISSORING, VG_TRUE);
1393 case DrawImageLoading:
1399 //int imcount=0;// this is debug code and should not go into release
1400 unsigned int OsdOpenVG::handleOpenVGCommand(OpenVGCommand& command)
1402 switch (command.task)
1404 case OVGreplacefont:
1406 vgDestroyFont(vgfont);
1410 case OVGdestroyImageRef: //imcount--;
1412 //LogNT::getInstance()->debug(TAG, "OsdImage Destroy vgDestroyImage {:#x}",command.param1);
1413 vgDestroyImage(static_cast<VGImage>(command.param1));
1416 case OVGdestroyPaint:
1418 //LogNT::getInstance()->debug(TAG, "Draw Paint Destroy {}",command.param1);
1419 vgDestroyPaint(static_cast<VGPaint>(command.param1));
1422 case OVGcreateImagePalette: //imcount++;
1424 VGImage input = vgCreateImage(VG_A_8, toi4(command.param1), toi4(command.param2),
1425 VG_IMAGE_QUALITY_NONANTIALIASED |
1426 VG_IMAGE_QUALITY_FASTER | VG_IMAGE_QUALITY_BETTER);
1427 //LogNT::getInstance()->debug(TAG, "Draw create palette {} {} {:#x} {}",command.param1,command.param2,vgGetError(),input);
1428 vgImageSubData(input, command.data, toi4(command.param1),
1429 VG_A_8, 0, 0, toi4(command.param1), toi4(command.param2)); // upload palettized image data
1430 VGImage handle = vgCreateImage(VG_sRGBA_8888, toi4(command.param1), toi4(command.param2),
1431 VG_IMAGE_QUALITY_NONANTIALIASED |
1432 VG_IMAGE_QUALITY_FASTER | VG_IMAGE_QUALITY_BETTER);
1433 VGuint* palette = static_cast<VGuint*>(malloc(256 * sizeof(VGuint)));
1434 const VGuint* in_palette = static_cast<const VGuint*>(command.data2);
1436 for (int i = 0; i < 256; i++)
1438 VGuint color = in_palette[i];
1439 palette[i] = color << 8 | (color & 0xff000000) >> 24;
1442 vgLookupSingle(handle, input, palette, VG_ALPHA, VG_FALSE, VG_FALSE);
1444 vgDestroyImage(input);
1448 case OVGcreateMonoBitmap: //imcount++;
1450 VGImage handle = vgCreateImage(VG_A_1, toi4(command.param1), toi4(command.param2),
1451 VG_IMAGE_QUALITY_FASTER);
1452 //LogNT::getInstance()->debug(TAG, "Draw create mono {} {} {:#x} {}",command.param1,command.param2,vgGetError(),handle);
1453 unsigned int buffer_len = (command.param1 * command.param2) >> 3;
1454 unsigned char* buffer = static_cast<unsigned char*>(malloc(buffer_len));
1455 unsigned char* r_buffer1 = buffer;
1456 const unsigned char* r_buffer2 = static_cast<const unsigned char*>(command.data);
1457 unsigned char* buffer_end = buffer + buffer_len;
1459 while (r_buffer1 != buffer_end)
1461 unsigned char byte = *r_buffer2;
1462 *r_buffer1 = ((byte * 0x0802LU & 0x22110LU) | (byte * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
1463 r_buffer1++; r_buffer2++;
1466 vgImageSubData(handle, buffer, command.param1 >> 3, VG_A_1, 0, 0, toi4(command.param1), toi4(command.param2));
1468 // LogNT::getInstance()->debug(TAG, "Draw create mono2 {} {} {:#x} {}",command.param1,command.param2,vgGetError(),handle);
1471 /*case OVGcreateImageFile: {
1474 Image *imagefile=(Image*)command.data;
1476 imagefile->write(&imageblob,"RGBA");
1478 handle=vgCreateImage(VG_sXBGR_8888,imagefile->columns(),imagefile->rows(),
1479 VG_IMAGE_QUALITY_BETTER);
1480 //LogNT::getInstance()->debug(TAG, "Draw create image details {} {} {:#x} mark1",imagefile->columns(),imagefile->rows(),(unsigned int*)imageblob.data());
1481 vgImageSubData(handle,imageblob.data(),imagefile->columns()*4,
1482 VG_sXBGR_8888,0,0,imagefile->columns(),imagefile->rows());
1483 //LogNT::getInstance()->debug(TAG, "Draw create image details {} {} {:#x} mark2",imagefile->columns(),imagefile->rows(),(unsigned int*)imageblob.data());
1485 }catch( Exception &error_ )
1487 LogNT::getInstance()->debug(TAG, "Libmagick hT: {}",error_.what());
1492 //LogNT::getInstance()->debug(TAG, "Draw create file {} {} {:#x} {}",command.param1,command.param2,vgGetError(),handle);
1495 case OVGcreateImageMemory: //imcount++;
1497 const PictureInfo* info = static_cast<const PictureInfo*>(command.data);
1499 //LogNT::getInstance()->debug(TAG, "TVMedia OVGcreateImageMemory {}",imcount);
1500 handle = vgCreateImage(VG_sABGR_8888, toi4(info->width), toi4(info->height), VG_IMAGE_QUALITY_BETTER);
1501 vgImageSubData(handle, info->image, toi4(info->width) * 4,
1502 VG_sABGR_8888, 0, 0, toi4(info->width), toi4(info->height));
1503 info->decoder->freeReference(info->reference);
1505 // We have a pictures! send a message to ourself, to switch to gui thread
1506 Message* m = new Message();
1509 m->data = reinterpret_cast<void*>(handle);
1510 m->message = Message::NEW_PICTURE;
1511 m->tag = reinterpret_cast<u4>(info->lindex);
1512 MessageQueue::getInstance()->postMessage(m);
1517 case OVGcreateEGLImage: //imcount++;
1519 PictureInfo* info = const_cast<PictureInfo*>(static_cast<const PictureInfo*>(command.data));
1522 handle = vgCreateImage(VG_sABGR_8888, toi4(info->width), toi4(info->height), VG_IMAGE_QUALITY_BETTER);
1523 // LogNT::getInstance()->debug(TAG, "TVMedia OVGcreateEGLImage {} {} {:#x} {}",info->width,info->height, handle,imcount);
1525 info->handle = handle;
1526 info->reference = eglCreateImageKHR(egl_display, egl_context, EGL_VG_PARENT_IMAGE_KHR, reinterpret_cast<EGLClientBuffer>(handle), NULL);
1527 if (info->reference) return true;
1529 LogNT::getInstance()->debug(TAG, "TVMedia OVGcreateEGLImage {} {} {:#x} Fail!", info->width, info->height, handle);
1530 if (handle) vgDestroyImage(handle);
1533 case OVGreadyEGLImage:
1535 PictureInfo* info = const_cast<PictureInfo*>(static_cast<const PictureInfo*>(command.data));
1536 eglDestroyImageKHR(egl_display, info->reference);
1538 Message* m = new Message();
1541 m->data = reinterpret_cast<void*>(info->handle);
1542 m->message = Message::NEW_PICTURE;
1543 m->tag = reinterpret_cast<u4>(info->lindex);
1544 MessageQueue::getInstance()->postMessage(m); // inform command about new picture
1549 case OVGcreateColorRef:
1552 handle = vgCreatePaint();
1553 const DrawStyle* style = static_cast<const DrawStyle*>(command.data);
1557 case DrawStyle::Color:
1559 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
1560 vgSetColor(handle, command.param1);
1562 //rgba = vgGetColor((VGPaint)handle);
1563 //LogNT::getInstance()->debug(TAG, "Draw Paint {} {:#x} {:#x}",handle,command.param1,rgba);
1566 case DrawStyle::GradientLinear:
1568 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_LINEAR_GRADIENT);
1569 VGfloat params[] = {style->x1, style->y1, style->x2, style->y2, style->r};
1570 //LogNT::getInstance()->debug(TAG, "Draw Gradient {} {} {} {} {}",handle,params[0],params[1],params[2],params[3]);
1571 vgSetParameterfv(handle, VG_PAINT_LINEAR_GRADIENT, 4, params);
1574 case DrawStyle::GradientRadial:
1576 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_RADIAL_GRADIENT);
1577 VGfloat params[] = {style->x1, style->y1, style->x2, style->y2, style->r};
1578 vgSetParameterfv(handle, VG_PAINT_RADIAL_GRADIENT, 5, params);
1583 if (style->ft == DrawStyle::GradientLinear || style->ft == DrawStyle::GradientRadial)
1585 VGfloat colorramp[5 * 5];
1586 colorramp[0 + 0 * 5] = 0.f;
1587 colorramp[1 + 0 * 5] = static_cast<float>(style->red) / 255;
1588 colorramp[2 + 0 * 5] = static_cast<float>(style->green) / 255;
1589 colorramp[3 + 0 * 5] = static_cast<float>(style->blue) / 255;
1590 colorramp[4 + 0 * 5] = static_cast<float>(style->alpha) / 255;
1592 for (int i = 0; i < (style->num_colors - 1); i++)
1594 colorramp[0 + (i + 1) * 5] = style->grad_pos[i];
1595 colorramp[1 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].red) / 255.f;
1596 colorramp[2 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].green) / 255.f;
1597 colorramp[3 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].blue) / 255.f;
1598 colorramp[4 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].alpha) / 255.f;
1601 colorramp[0 + (style->num_colors) * 5] = 1.f;
1602 colorramp[1 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].red) / 255.f;
1603 colorramp[2 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].green) / 255.f;
1604 colorramp[3 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].blue) / 255.f;
1605 colorramp[4 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].alpha) / 255.f;
1606 vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_SPREAD_MODE, VG_COLOR_RAMP_SPREAD_REFLECT);
1607 vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_PREMULTIPLIED, VG_FALSE);
1608 vgSetParameterfv(handle, VG_PAINT_COLOR_RAMP_STOPS, 5 + (style->num_colors) * 5, colorramp);
1614 case OVGimageUploadLine:
1621 void OsdOpenVG::destroyImageRef(VectorHandleImage handle)
1623 struct OpenVGCommand comm;
1624 comm.task = OVGdestroyImageRef;
1625 comm.param1 = handle;
1626 putOpenVGCommand(comm, false);
1629 bool OsdOpenVG::getStaticImageData(unsigned int static_id, u1** userdata, u4* length)
1631 if (sa_MAX > static_id)
1633 *userdata = static_artwork_begin[static_id];
1634 *length = tou4(static_artwork_end[static_id] - static_artwork_begin[static_id]);
1643 void OsdOpenVG::createPicture(struct PictureInfo& pict_inf)
1645 struct OpenVGCommand comm;
1646 LogNT::getInstance()->debug(TAG, "TVMedia Create Picture {}", pict_inf.type);
1648 if (pict_inf.type == PictureInfo::RGBAMemBlock)
1650 comm.task = OVGcreateImageMemory;
1651 comm.data = new PictureInfo(pict_inf); // deleted in the commands processor
1652 putOpenVGCommand(comm, false);
1654 else if (pict_inf.type == PictureInfo::EGLImage)
1656 comm.task = OVGreadyEGLImage;
1657 comm.data = new PictureInfo(pict_inf); // deleted in the commands processor
1658 putOpenVGCommand(comm, false);
1663 pict_inf.decoder->freeReference(pict_inf.reference);
1667 bool OsdOpenVG::getEGLPicture(struct PictureInfo& info, EGLDisplay* display)
1669 struct OpenVGCommand comm;
1670 info.type = PictureInfo::EGLImage;
1671 comm.task = OVGcreateEGLImage;
1673 *display = egl_display;
1674 return putOpenVGCommand(comm, true);
1677 VectorHandleImage OsdOpenVG::createMonoBitmap(void* base, int width, int height)
1679 struct OpenVGCommand comm;
1680 comm.task = OVGcreateMonoBitmap;
1681 comm.param1 = tou4(width);
1682 comm.param2 = tou4(height);
1684 return putOpenVGCommand(comm, true);
1687 VectorHandleImage OsdOpenVG::createImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data)
1689 struct OpenVGCommand comm;
1690 comm.task = OVGcreateImagePalette;
1691 comm.param1 = tou4(width);
1692 comm.param2 = tou4(height);
1693 comm.data = image_data;
1694 comm.data2 = palette_data;
1695 return putOpenVGCommand(comm, true);
1698 void OsdOpenVG::destroyDrawStyleHandle(VectorHandle index)
1700 struct OpenVGCommand comm;
1701 comm.task = OVGdestroyPaint;
1702 comm.param1 = index;
1703 putOpenVGCommand(comm, false);
1706 VectorHandle OsdOpenVG::createDrawStyleHandle(const DrawStyle& c)
1708 unsigned int col = c.rgba();
1709 struct OpenVGCommand comm;
1710 comm.task = OVGcreateColorRef;
1711 comm.param1 = col << 8 | (col & 0xff000000) >> 24;
1713 return putOpenVGCommand(comm, true);