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"
33 #include "osdopenvg.h"
35 static const char* TAG = "OsdOpenVG";
37 #define EXTERNALPICTURE(name, fname, fileextension) extern uint8_t name ## _data[] asm("_binary_other_"#fname"_"#fileextension"_start"); \
38 extern uint8_t name ## _data_end[] asm("_binary_other_"#fname"_"#fileextension"_end");
42 #undef EXTERNALPICTURE
44 #define BACKBUFFER_WIDTH 1280
45 #define BACKBUFFER_HEIGHT 720
47 OsdOpenVG::OsdOpenVG()
51 lastrendertime = getTimeMS();
53 const char* fontname = "Droid Sans:style=Regular";
54 cur_fontname = static_cast<char*>(malloc(strlen(fontname) + 1));
55 strcpy(cur_fontname, fontname);
57 #define EXTERNALPICTURE(name, fname, fileextension) static_artwork_begin[sa_ ## name]=name ## _data;
61 #undef EXTERNALPICTURE
62 #define EXTERNALPICTURE(name, fname, fileextension) static_artwork_end[sa_ ## name]=name ## _data_end;
66 #undef EXTERNALPICTURE
68 MessageQueue::getInstance()->addReceiver(this);
71 OsdOpenVG::~OsdOpenVG()
73 MessageQueue::getInstance()->removeReceiver(this);
75 if (initted) shutdown();
77 if (cur_fontname) free(cur_fontname);
79 if (freetype_inited) FT_Done_Face(ft_face);
81 for (char* c : fontnames) free(c);
82 for (char* c : fontnames_keys) free(c);
90 if (initted) return 0;
94 //First get connection to egl
95 egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
96 if (egl_display == EGL_NO_DISPLAY)
98 LogNT::getInstance()->crit(TAG, "Could not get egl display! {:#x}", eglGetError());
103 if (eglInitialize(egl_display, NULL, NULL) == EGL_FALSE)
105 LogNT::getInstance()->crit(TAG, "Initialising display failed! {:#x}", eglGetError());
110 const char* query_str = eglQueryString(egl_display, EGL_CLIENT_APIS);
112 if (query_str) LogNT::getInstance()->debug(TAG, query_str);
113 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", eglGetError());
115 query_str = eglQueryString(egl_display, EGL_EXTENSIONS);
117 if (query_str) LogNT::getInstance()->info(TAG, query_str);
118 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", eglGetError());
120 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
122 LogNT::getInstance()->warn(TAG, "Binding openvg api failed! {:#x}", eglGetError());
127 const EGLint attributs[] =
129 EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
130 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
131 EGL_CONFORMANT, EGL_OPENVG_BIT,
133 }; // Here, we might have to select the resolution!
137 if (eglChooseConfig(egl_display, attributs, &egl_ourconfig, 1, &number) == EGL_FALSE)
139 LogNT::getInstance()->warn(TAG, "Choosing egl config failed! {:#x}", eglGetError());
144 egl_context = eglCreateContext(egl_display, egl_ourconfig, NULL, NULL);
146 if (egl_context == EGL_NO_CONTEXT)
148 LogNT::getInstance()->warn(TAG, "Creating egl context failed! {:#x}", eglGetError());
153 // warning broadcom specific, get display size!
154 display_width = display_height = 0;
156 if (graphics_get_display_size(0, reinterpret_cast<uint32_t*>(&display_width), reinterpret_cast<uint32_t*>(&display_height)) < 0)
158 LogNT::getInstance()->warn(TAG, "Getting display size failed! (BCM API)");
163 LogNT::getInstance()->info(TAG, "Displaysize is {} x {}", display_width, display_height);
164 VC_RECT_T dst_rect = {0, 0, display_width, display_height};
165 VC_RECT_T src_rect = {0, 0, BACKBUFFER_WIDTH << 16, BACKBUFFER_HEIGHT << 16};
166 VC_RECT_T src_rect_bg = {0, 0, 16 << 16, 16 << 16};
167 // VC_RECT_T src_rect_im={0,0,16,16};
169 uint32_t back_image_ptr;
170 bcm_backres = vc_dispmanx_resource_create(VC_IMAGE_RGBX32, 16, 16, &back_image_ptr);
172 updateBackgroundColor(DrawStyle::WALLPAPER);
174 DISPMANX_UPDATE_HANDLE_T bcm_update;
175 bcm_display = vc_dispmanx_display_open(0);
176 bcm_update = vc_dispmanx_update_start(0);
177 bcm_element = vc_dispmanx_element_add(bcm_update, bcm_display,
179 &src_rect, DISPMANX_PROTECTION_NONE, 0, 0, static_cast<DISPMANX_TRANSFORM_T>(0));
182 bcm_background = vc_dispmanx_element_add(bcm_update, bcm_display,
183 0, &dst_rect, bcm_backres,
184 &src_rect_bg, DISPMANX_PROTECTION_NONE, 0, 0, static_cast<DISPMANX_TRANSFORM_T>(0));
186 vc_dispmanx_update_submit_sync(bcm_update);
188 static EGL_DISPMANX_WINDOW_T nativewindow;
189 nativewindow.element = bcm_element;
190 nativewindow.height = BACKBUFFER_HEIGHT;
191 nativewindow.width = BACKBUFFER_WIDTH;
193 egl_surface = eglCreateWindowSurface(egl_display, egl_ourconfig, &nativewindow, NULL);
194 if (egl_surface == EGL_NO_SURFACE)
196 LogNT::getInstance()->warn(TAG, "Creating egl window surface failed!");
201 LogNT::getInstance()->debug(TAG, "Making egl current in SYS_gettid: {}", syscall(SYS_gettid));
203 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
205 LogNT::getInstance()->warn(TAG, "Making egl Current failed");
212 query_str = reinterpret_cast<const char*>(vgGetString(VG_VERSION));
214 if (query_str) LogNT::getInstance()->info(TAG, query_str);
215 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
217 query_str = reinterpret_cast<const char*>(vgGetString(VG_VENDOR));
219 if (query_str) LogNT::getInstance()->info(TAG, query_str);
220 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
222 query_str = reinterpret_cast<const char*>(vgGetString(VG_RENDERER));
224 if (query_str) LogNT::getInstance()->info(TAG, query_str);
225 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
227 query_str = reinterpret_cast<const char*>(vgGetString(VG_EXTENSIONS));
229 if (query_str) LogNT::getInstance()->info(TAG, query_str);
230 else LogNT::getInstance()->warn(TAG, "Could not query display {:#x}", vgGetError());
232 aspect_correction = ((float)BACKBUFFER_HEIGHT) / 576.f / (((float)BACKBUFFER_WIDTH) / 720.f);
235 if (!fontnames.size())
237 //inspired by and copied from vdr's code
239 FcObjectSet* objset = FcObjectSetBuild(FC_FAMILY, FC_STYLE, NULL);
240 FcPattern* pattern = FcPatternCreate();
241 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
242 FcFontSet* fonts = FcFontList(NULL, pattern, objset);
244 for (int i = 0; i < fonts->nfont; i++)
246 char* s = reinterpret_cast<char*>(FcNameUnparse(fonts->fonts[i]));
250 char* c = strchr(s, ':');
254 char* s2 = strchr(c + 1, ',');
259 char* p = strchr(s, ',');
264 else memmove(p, c, strlen(c) + 1);
267 char* key = (char*)malloc(strlen(s) + 1);
269 fontnames_keys.push_back(key);
270 char* s2 = strstr(s, "style=");
274 memmove(s2, s2 + 6, strlen(s2 + 6) + 1);
277 fontnames.push_back(s);
281 FcFontSetDestroy(fonts);
282 FcPatternDestroy(pattern);
283 FcObjectSetDestroy(objset);
286 if (!loadFont(false))
291 vgttfont = vgCreateFont(0);
292 vgttpaint = vgCreatePaint();
293 vgSetParameteri( vgttpaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
294 vgSetColor(vgttpaint, 0xffffffff);
296 eglSwapInterval(egl_display, 1);
298 LogNT::getInstance()->debug(TAG, "Making egl current out SYS_gettid {}", syscall(SYS_gettid));
299 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
300 //Now we will create the Screen
301 initted = true; // must set this here or create surface won't work
303 /*if (((VideoOMX*)Video::getInstance())->initUsingOSDObjects()!=1) { //call Video for init stuff
308 renderThreadMutex.lock(); // start-protect
310 renderThread = std::thread( [this]
312 renderThreadMutex.lock();
313 renderThreadMutex.unlock();
316 renderThreadMutex.unlock();
323 #ifdef PICTURE_DECODER_OMX
324 imageomx2 = new ImageOMX2(/*egl_display*/);
325 ImageLoader::getInstance()->addDecoder(imageomx2);
331 void OsdOpenVG::renderThreadStop()
333 LogNT::getInstance()->warn(TAG, "renderThreadStop");
335 renderThreadMutex.lock();
336 renderThreadReq = 2; // req-stop
337 renderThreadCond.notify_one(); // in case renderLoop is in cond_wait
338 renderThreadMutex.unlock();
342 int OsdOpenVG::stopUpdate()
345 processOpenVGCommands();
349 int OsdOpenVG::shutdown()
351 #ifdef PICTURE_DECODER_OMX
352 if (imageomx2) ImageLoader::getInstance()->removeDecoder(imageomx2);
356 if (!initted) return 0;
359 LogNT::getInstance()->debug(TAG, "shutdown mark1");
361 LogNT::getInstance()->debug(TAG, "shutdown mark1a");
363 //(((VideoOMX*)Video::getInstance())->shutdownUsingOSDObjects());
364 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
366 LogNT::getInstance()->warn(TAG, "Making egl Current failed in shutdown {:#x}", eglGetError());
369 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
371 LogNT::getInstance()->warn(TAG, "Binding openvg api thread failed! {:#x}", eglGetError());
374 LogNT::getInstance()->debug(TAG, "shutdown mark2");
375 processOpenVGCommands();
376 LogNT::getInstance()->debug(TAG, "shutdown mark3");
381 //purgeAllReferences();
383 vgDestroyFont(vgfont);
384 vgDestroyFont(vgttfont);
385 vgDestroyPaint(vgttpaint);
387 float colclear[] = {0.8f, 0.8f, 0.8f, 1.f};
388 vgSetfv(VG_CLEAR_COLOR, 4, colclear);
389 vgClear(0, 0, BACKBUFFER_WIDTH, BACKBUFFER_HEIGHT);
390 eglSwapBuffers(egl_display, egl_surface);
391 LogNT::getInstance()->debug(TAG, "Making egl current out final");
392 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
394 if (eglDestroySurface(egl_display, egl_surface) == EGL_FALSE)
396 LogNT::getInstance()->error(TAG, "eglDestroySurface failed {:#x}", eglGetError());
399 if (eglDestroyContext(egl_display, egl_context) == EGL_FALSE)
401 LogNT::getInstance()->error(TAG, "eglDestroyContext failed {:#x}", eglGetError());
404 if (eglTerminate(egl_display) == EGL_FALSE)
406 LogNT::getInstance()->error(TAG, "eglTerminate failed {:#x}", eglGetError());
409 DISPMANX_UPDATE_HANDLE_T bcm_update;
410 bcm_update = vc_dispmanx_update_start(0);
412 vc_dispmanx_element_remove(bcm_update, bcm_element);
413 vc_dispmanx_element_remove(bcm_update, bcm_background);
414 vc_dispmanx_update_submit_sync(bcm_update);
415 vc_dispmanx_resource_delete(bcm_backres);
417 vc_dispmanx_display_close(bcm_display);
421 void OsdOpenVG::processMessage(Message* m)
423 if (m->message == Message::NEW_PICTURE)
425 logger->debug(TAG, "NEW_PICTURE");
427 OsdImageBase* newImageOVG = new OsdImageOpenVG(reinterpret_cast<VGImage>(m->data));
428 OsdImage oImage(newImageOVG);
432 ImageLoader::getInstance()->pictureRendered(reinterpret_cast<LoadingIndex>(m->tag), oImage);
433 // The above returns false if it does not accept the OsdImage
434 // We just drop out here and leave the only reference to it in Osd::images
435 // It will be deleted properly on the next garbage collection run
437 // OSDOVG-ROD-EXPERIMENT
438 logger->trace(TAG, "EXPERIMENT - call doRender");
443 void OsdOpenVG::garbageCollectOsdImages()
445 // Only use references into images
446 // Therefore if use_count == 1 the only copy of the OsdImage is in images and therefore is dead
449 for(auto& oi : images)
451 if (oi.use_count() == 1)
452 destroyImageRef(std::static_pointer_cast<OsdImageOpenVG>(oi)->getVGImage());
455 auto it = std::remove_if(images.begin(), images.end(), [](OsdImage& oi) { return oi.use_count() == 1; });
456 images.erase(it, images.end());
458 // C++20: std::erase_if(images, [](OsdImage oi) { return oi.use_count() == 1; });
463 // OSDOVG-ROD-EXPERIMENT - temp hack to allow OsdVector to signal the thread in OsdOpenVG
464 void OsdOpenVG::doRender()
466 LogNT::getInstance()->debug(TAG, "Nudge render thread");
468 // signal renderLoop to go-around (from putOpenVGCommand)
469 renderThreadMutex.lock();
470 if (renderThreadReq == 0) renderThreadReq = 1;
471 renderThreadCond.notify_one();
472 renderThreadMutex.unlock();
476 * OSDOVG-ROD-EXPERIMENT
478 * To switch OsdOpenVG to render-on-demand instead of rendering in a loop
481 * More responsive OSD
482 * Cuts 5% constant CPU usage
483 * Seems to have tickled the GFX corruption bug, might make it easier to find that - update: found and fixed!!
486 * There's a comment about video tearing if render is called too often.
487 * Will tearing show up in on-demand mode?
488 * Moves away from conventional FPS type render loop. VOMP doesn't use 3D GFX / animations
489 * currently, so this doesn't matter at the moment?
490 * Code needs updating to call doRender,
491 * e.g. backing out of the options screen doesn't redraw VWelcome, fixed now
493 * Maybe have a hybrid system. One that normally works on-demand but can
494 * also switch to looping on the fly if necessary.
498 void OsdOpenVG::renderLoop()
500 // We have to claim the egl context for this thread
501 LogNT::getInstance()->info(TAG, "Entering drawing thread");
503 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
505 LogNT::getInstance()->warn(TAG, "Making egl Current failed in thread {:#x}", eglGetError());
509 if (eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
511 LogNT::getInstance()->warn(TAG, "Binding openvg api thread failed! {:#x}", eglGetError());
515 std::unique_lock<std::mutex> ul(renderThreadMutex, std::defer_lock);
522 long long time1 = getTimeMS();
524 // OSDOVG-ROD-EXPERIMENT - always render, but only when signalled to
525 if (1) // ((time1 - lastrendertime) > 200) //5 fps for OSD updates are enough, avoids tearing
530 LogNT::getInstance()->trace(TAG, "EXPERIMENT - render");
533 lastrendertime = getTimeMS();
539 ts = time1 - lastrendertime;
542 if (processOpenVGCommands()) ts = 0;
545 if (renderThreadReq == 2)
547 if (eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) == EGL_FALSE)
548 LogNT::getInstance()->warn(TAG, "Making egl Current out thread failed");
555 // OSDOVG-ROD-EXPERIMENT
558 renderThreadCond.wait(ul, [this]{ return renderThreadReq != 0; }); // experiment! always wait, no time limit
559 //renderThreadCond.wait_for(ul, std::chrono::milliseconds(ts), [this]{ return renderThreadReq != 0; });
561 if (renderThreadReq == 2)
563 if (eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) == EGL_FALSE)
564 LogNT::getInstance()->warn(TAG, "Making egl Current out thread failed");
569 if (renderThreadReq == 0) abort(); // OSDOVG-ROD-EXPERIMENT - check this never happens - confirmed
571 // Either timeout or renderThreadReq == 1 - go around
580 UINT OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm, bool wait)
582 if (wait) comm.response = new OpenVGResponse;
584 std::unique_lock<std::mutex> ul(taskmutex); // locks
585 vgcommands.push_back(comm);
588 // signal renderLoop to go-around
589 renderThreadMutex.lock();
590 if (renderThreadReq == 0) renderThreadReq = 1;
591 renderThreadCond.notify_one();
592 renderThreadMutex.unlock();
597 // Wait until done is true and we are signalled. If done is already true, this returns immediately
598 comm.response->doneCond.wait(ul, [&comm]{ return comm.response->done; });
600 // Now response->done = true, mutex is locked, we have been signalled
603 UINT toReturn = comm.response->result;
604 delete comm.response;
608 bool OsdOpenVG::processOpenVGCommands()
613 while (vgcommands.size() > 0)
617 OpenVGCommand comm = vgcommands.front();
618 vgcommands.pop_front();
622 UINT vgResult = handleOpenVGCommand(comm);
626 if (comm.response) // There is a response object, a thread is waiting
628 comm.response->result = vgResult;
629 comm.response->done = true;
630 comm.response->doneCond.notify_one();
638 void OsdOpenVG::InternalRendering()
641 Colour bg = DrawStyle::WALLPAPER;
642 float colclear[] = {1.f, 1.0f, 1.f, 1.f};
644 if (bg.alpha == 0) colclear[3] = 0.f;
646 vgSetfv(VG_CLEAR_COLOR, 4, colclear);
647 vgClear(0, 0, BACKBUFFER_WIDTH, BACKBUFFER_HEIGHT);
648 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
650 drawSurfaces(); //iterate through and draws all commands
652 //Show it to the user!
653 eglSwapBuffers(egl_display, egl_surface);
657 // --------------------------------------------------------------------------------------------------------------------------
660 void OsdOpenVG::purgeAllReferences()
663 drawstyleHandlesRefCounts.clear(); // remove all references
666 map<void *,VectorHandleImage>::iterator mitty=monobitmaps.begin();
667 while (mitty!=monobitmaps.end()) {
668 vgDestroyImage((VGImage)(*mitty).second);
673 / *map<string,VectorHandleImage>::iterator jitty=jpegs.begin();
674 while (jitty!=jpegs.end()) {
675 vgDestroyImage((VGImage)(*jitty).second);
680 map<pair<Colour*,unsigned int>,unsigned int>::iterator sitty=drawstyleHandles.begin();
681 while (sitty!=drawstyleHandles.end()) {
682 vgDestroyPaint((VGPaint)(*sitty).second);
685 drawstyleHandles.clear();
689 void OsdOpenVG::updateBackgroundColor(DrawStyle bg)
691 unsigned int color[16 * 16];
693 if (!bcm_backres) return;
695 VC_RECT_T src_rect_im = {0, 0, 16, 16};
697 if (bg.ft != DrawStyle::GradientLinear)
702 //memset(color,0xFF,sizeof(unsigned int)*4*8);
703 for (int j = 0; j < 16; j++)
705 int helpr = (((15 - j) * bg.red) + (j * bg.grad_col[0].red)) / 15;
706 int helpb = (((15 - j) * bg.blue) + (j * bg.grad_col[0].blue)) / 15;
707 int helpg = (((15 - j) * bg.green) + (j * bg.grad_col[0].green)) / 15;
708 //unsigned int cur_col=help | (help<< 8) | (help<< 16) | (0xFF<< 24);
709 unsigned int cur_col = helpr | (helpg << (8)) | (helpb << (16)) | (0xFF << (24));
711 for (int i = 0; i < 16; i++)
713 color[i + 16 * j] = cur_col;
717 vc_dispmanx_resource_write_data(bcm_backres, VC_IMAGE_RGBX32, 16 * 4, color, &src_rect_im);
720 void OsdOpenVG::getScreenSize(int& width, int& height)
722 width = BACKBUFFER_WIDTH;
723 height = BACKBUFFER_HEIGHT;
726 void OsdOpenVG::getRealScreenSize(int& width, int& height)
728 width = display_width;
729 height = display_height;
732 bool OsdOpenVG::screenShotInternal(void* buffer, int width, int height, bool includeOSD)
734 if (!initted) return false;
736 if (!buffer) return false;
738 DISPMANX_RESOURCE_HANDLE_T res;
739 DISPMANX_DISPLAY_HANDLE_T display;
743 res = vc_dispmanx_resource_create(VC_IMAGE_RGBA32, width, height, &image_ptr);
744 display = vc_dispmanx_display_open(0);
748 vc_dispmanx_snapshot(display, res,
749 static_cast<DISPMANX_TRANSFORM_T>(DISPMANX_SNAPSHOT_NO_RGB | DISPMANX_SNAPSHOT_FILL/*|DISPMANX_SNAPSHOT_PACK*/));
753 vc_dispmanx_snapshot(display, res,
754 static_cast<DISPMANX_TRANSFORM_T>(DISPMANX_SNAPSHOT_FILL));
757 vc_dispmanx_rect_set(&rect, 0, 0, width, height);
758 vc_dispmanx_resource_read_data(res, &rect, buffer, width * 4);
759 vc_dispmanx_resource_delete(res);
760 vc_dispmanx_display_close(display);
764 void OsdOpenVG::initPaths()
767 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
768 VG_PATH_DATATYPE_F, 1.f, 0.f,
769 0, 0, VG_PATH_CAPABILITY_ALL);
771 vguLine(current, 0.f, 0.f, 1.f, 0.f);
773 std_paths[PIHorzLine] = current;
775 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
776 VG_PATH_DATATYPE_F, 1.f, 0.f,
777 0, 0, VG_PATH_CAPABILITY_ALL);
778 vguLine(current, 0.f, 0.f, 0.f, 1.f);
780 std_paths[PIVertLine] = current;
782 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
783 VG_PATH_DATATYPE_F, 1.f, 0.f,
784 0, 0, VG_PATH_CAPABILITY_ALL);
785 //vguRect(current,0.f,0.f,1.f,1.f);
786 vguRect(current, 0.f, 0.f, 1.f, 1.f);
788 std_paths[PIRectangle] = current;
790 current = vgCreatePath(VG_PATH_FORMAT_STANDARD,
791 VG_PATH_DATATYPE_F, 1.f, 0.f,
793 vguEllipse(current, 0.f, 0.f, 1.f, 1.f);
795 std_paths[PIPoint] = current;
798 void OsdOpenVG::destroyPaths()
800 vgDestroyPath(std_paths[PIHorzLine]);
801 vgDestroyPath(std_paths[PIVertLine]);
802 vgDestroyPath(std_paths[PIRectangle]);
803 vgDestroyPath(std_paths[PIPoint]);
808 float OsdOpenVG::getFontHeight()
810 return font_height; //dummy
813 float OsdOpenVG::getCharWidth(wchar_t c)
815 if (c < 256) return byte_char_width[c];
817 unsigned int glyph_index = FT_Get_Char_Index(ft_face, c);
818 return font_exp_x[glyph_index];
821 unsigned int OsdOpenVG::loadTTchar(cTeletextChar c)
823 unsigned int glyph_index = c.getGlyphIndex();
825 if (tt_font_chars.find(glyph_index) != tt_font_chars.end())
830 unsigned int buffer[10];
831 const VGfloat glyphOrigin[] = { 0.f, 0.f };
832 const VGfloat escapement[] = { 12.f, 0.f };
833 unsigned int* charmap = GetFontChar(c, buffer);
835 if (!charmap) return 0; //invalid char
837 for (int i = 0; i < 10; i++)
839 buffer[i] = charmap[i] >> 4;
842 VGImage handle = vgCreateImage(
846 VG_IMAGE_QUALITY_NONANTIALIASED | VG_IMAGE_QUALITY_FASTER
847 | VG_IMAGE_QUALITY_BETTER);
848 vgImageSubData(handle, buffer, 4, VG_A_1, 0, 0, 12, 10);
852 handle, const_cast<VGfloat*>(glyphOrigin), const_cast<VGfloat*>(escapement));
853 vgDestroyImage(handle);
854 tt_font_chars[glyph_index] = 1;
859 int OsdOpenVG::getFontNames(const char*** names, const char*** names_keys)
861 *names = const_cast<const char**>(&(fontnames[0]));
862 *names_keys = const_cast<const char**>(&(fontnames_keys[0]));
863 return fontnames.size();
866 void OsdOpenVG::setFont(const char* fontname)
868 if (strcmp(fontname, cur_fontname))
871 if (cur_fontname) free(cur_fontname);
873 cur_fontname = static_cast<char*>(malloc(strlen(fontname) + 1));
874 strcpy(cur_fontname, fontname);
876 struct OpenVGCommand comm;
877 comm.task = OVGreplacefont;
878 putOpenVGCommand(comm, false);
882 int OsdOpenVG::loadFont(bool newfont)
885 float font_size = 16.f;
887 if (!freetype_inited)
889 error = FT_Init_FreeType(&ft_library);
893 LogNT::getInstance()->warn(TAG, "Could not load freetype {:#x}", error);
898 if (!freetype_inited || newfont)
900 //first get the filename algorith extracted from vdr by Klaus Schmidinger
902 FcPattern* pattern = FcNameParse(reinterpret_cast<FcChar8*>(cur_fontname));
903 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
904 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
905 FcDefaultSubstitute(pattern);
907 FcFontSet* fonts = FcFontSort(NULL, pattern, FcFalse, NULL, &fres);
908 FcChar8* filename = NULL;
912 for (int i = 0; i < fonts->nfont; i++)
915 FcPatternGetBool(fonts->fonts[i], FC_SCALABLE, 0, &canscale);
919 FcPatternGetString(fonts->fonts[i], FC_FILE, 0, &filename);
924 FcFontSetDestroy(fonts);
928 LogNT::getInstance()->crit(TAG, "Could not locate a font! Abort!");
932 FcPatternDestroy(pattern);
934 LogNT::getInstance()->info(TAG, "Load Font {}: {}", cur_fontname, filename);
935 // second load the font
937 error = FT_New_Face(ft_library, reinterpret_cast<const char*>(filename), 0, &new_ft_face);
941 LogNT::getInstance()->warn(TAG, "Could not load font face {:#x} {}", error, filename);
945 error = FT_Set_Char_Size(new_ft_face, 0, font_size * 256, 0, 0 /*dpi*/);
949 FT_Done_Face(new_ft_face);
950 LogNT::getInstance()->warn(TAG, "Could not set face size {:#x}", error);
954 FT_Face old_ft_face = ft_face; // do it thread safe
955 ft_face = new_ft_face;
957 if (freetype_inited) FT_Done_Face(old_ft_face);//
959 freetype_inited = true;
962 vgfont = vgCreateFont(0);
965 font_height = ft_face->size->metrics.height / 256.f;
966 cur_char = FT_Get_First_Char(ft_face, &glyph);
967 std::vector<VGubyte> segments;
968 std::vector<VGfloat> coord;
969 segments.reserve(256);
972 //LogNT::getInstance()->debug(TAG, "Create Glyph test {} {} {} {}",cur_char,font_data_end,font_data,glyph);
975 error = FT_Load_Glyph(ft_face, glyph, FT_LOAD_DEFAULT);
979 cur_char = FT_Get_Next_Char(ft_face, cur_char, &glyph);
980 LogNT::getInstance()->warn(TAG, "Could not load glyph {:#x} {}", error);
985 FT_Outline ot = ft_face->glyph->outline;
989 if (ot.n_contours == 0)
991 path = VG_INVALID_HANDLE;
995 path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F,
996 1.0f, 0.f, 0, 0, VG_PATH_CAPABILITY_ALL);
999 FT_Vector* pt = ot.points;
1000 const char* tags = ot.tags;
1001 const short* cont = ot.contours;
1002 short n_cont = ot.n_contours;
1004 //short n_point=ot.n_points;
1005 //short last_cont=0;
1006 for (short point = 0; n_cont != 0; cont++, n_cont--)
1008 short next_cont = *cont + 1;
1011 short first_point = point;
1013 //LogNT::getInstance()->debug(TAG, "runs {}", *cont);
1014 for (; point < next_cont; point++)
1016 char tag = tags[point];
1017 FT_Vector fpoint = pt[point];
1019 // LogNT::getInstance()->debug(TAG, "tag %d point {} {}: {} {}",tag,fpoint.x,fpoint.y,point,n_point);
1022 segments.push_back(VG_MOVE_TO);
1025 else if (tag & 0x1) //on curve
1029 segments.push_back(VG_LINE_TO);
1031 else if (last_tag & 0x2)
1033 segments.push_back(VG_CUBIC_TO);
1037 segments.push_back(VG_QUAD_TO);
1045 if (!(last_tag & 0x1))
1047 segments.push_back(VG_QUAD_TO);
1048 int coord_size = coord.size();
1049 VGfloat x = (coord[coord_size - 2] + ((float)fpoint.x) / 256.f) * 0.5f * aspect_correction;
1050 VGfloat y = (coord[coord_size - 1] + (font_size - ((float)fpoint.y) / 256.f)) * 0.5f;
1058 coord.push_back(((float)fpoint.x)*aspect_correction / 256.);
1059 coord.push_back(font_size - ((float)fpoint.y) / 256.);
1060 //LogNT::getInstance()->debug(TAG, "Create APD Glyph coord {} {} {} {}",fpoint.x,fpoint.y,coord[coord.size()-2],coord[coord.size()-1]);
1063 if (!(last_tag & 0x1))
1067 segments.push_back(VG_CUBIC_TO);
1071 segments.push_back(VG_QUAD_TO);
1074 coord.push_back(((float)pt[first_point].x)*aspect_correction / 256.);
1075 coord.push_back(font_size - ((float)pt[first_point].y) / 256.);
1078 //segments.push_back(VG_CLOSE_PATH);
1081 vgAppendPathData(path, segments.size(), &segments[0], &coord[0]);
1082 /* for (int m=0;m<segments.size();m++) {
1084 switch (segments[m])
1087 LogNT::getInstance()->debug(TAG, "Move To {} {}",coord[n],coord[n+1]);n+=2; break;
1089 LogNT::getInstance()->debug(TAG, "Line To {} {}",coord[n],coord[n+1]);n+=2; break;
1091 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;
1093 LogNT::getInstance()->debug(TAG, "Quad To {} {} {} {}",coord[n],coord[n+1],coord[n+2],coord[n+3]);n+=4; break;
1095 LogNT::getInstance()->debug(TAG, "Close Path"); break;
1099 //vguRect(path,0.f,0.f,1.f,1.f);
1100 //LogNT::getInstance()->debug(TAG, "Create APD Glyph {} {:#x}",segments.size(),vgGetError());
1103 VGfloat ori[] = {0.f, 0.f};
1104 VGfloat esp[] = {ft_face->glyph->advance.x / 256.f * aspect_correction, ft_face->glyph->advance.y / 256.f};
1105 font_exp_x[glyph] = ft_face->glyph->advance.x / 256.f * aspect_correction; //recalculate
1107 vgSetGlyphToPath(vgfont, glyph, path, VG_FALSE, ori, esp);
1109 //LogNT::getInstance()->debug(TAG, "Create Glyph {} {} {:#x}",path,glyph,vgGetError());
1110 if (path != VG_INVALID_HANDLE)
1112 vgDestroyPath(path);
1115 cur_char = FT_Get_Next_Char(ft_face, cur_char, &glyph);
1118 for (int i = 0; i < 256; i++)
1120 unsigned int glyph_index = FT_Get_Char_Index(ft_face, i);
1121 byte_char_width[i] = font_exp_x[glyph_index];
1127 void OsdOpenVG::drawSetTrans(SurfaceInfo& sc)
1129 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1131 vgScale(((float)BACKBUFFER_WIDTH) / 720.f, -((float)BACKBUFFER_HEIGHT) / 576.f);
1132 vgTranslate(0.f + sc.x, -576.f + sc.y);
1134 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1136 vgScale(((float)BACKBUFFER_WIDTH) / 720.f, -((float)BACKBUFFER_HEIGHT) / 576.f);
1137 vgTranslate(0.f + sc.x, -576.f + sc.y);
1139 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1141 vgScale(((float)BACKBUFFER_WIDTH) / 720.f, -((float)BACKBUFFER_HEIGHT) / 576.f);
1142 vgTranslate(0.f + sc.x, -576.f + sc.y);
1143 clip_shift_x = sc.x;
1144 clip_shift_y = sc.y;
1146 //vgTranslate(0.f+sc.x,576.f-sc.y);
1147 //LogNT::getInstance()->debug(TAG, "Draw Translate {} {}",sc.x,sc.y);
1150 void OsdOpenVG::executeDrawCommand(SVGCommand& command)
1152 VGfloat save_matrix[9];
1153 VGfloat save_matrix2[9];
1155 switch (command.instr)
1159 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1161 // rgba = vgGetColor((VGPaint) command.handle);
1162 //LogNT::getInstance()->debug(TAG, "Draw Path {} {:#x} {} {} {} {}",command.handle,command.target.path_index,command.x,command.y,command.w,command.h);
1163 //vgSeti(VG_FILL_RULE,);
1165 vgGetMatrix(save_matrix);
1166 vgSetPaint(static_cast<VGPaint>(command.handle), VG_FILL_PATH);
1167 vgSetPaint(static_cast<VGPaint>(command.handle), VG_STROKE_PATH);
1168 vgTranslate(command.x, command.y);
1169 vgScale(command.w, command.h);
1170 vgDrawPath(std_paths[command.target.path_index], VG_FILL_PATH);
1171 vgLoadMatrix(save_matrix);
1178 if (command.instr == DrawBitmap) vgimage = static_cast<VGImage>(command.target.bitmap);
1179 else if (command.instr == DrawImage)
1181 // FIXME use shared_ptr for OsdImageOpenVG objects?
1183 // Don't use shared_ptrs for all those CImage types, are they needed?
1184 // Preferring the second option at the moment
1186 OsdImage oi = command.image->getOsdImage();
1187 OsdImageOpenVG* oiovg = std::static_pointer_cast<OsdImageOpenVG>(oi).get();
1188 vgimage = oiovg->getVGImage();
1191 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1192 vgGetMatrix(save_matrix);
1193 VGfloat imagewidth = vgGetParameteri(vgimage, VG_IMAGE_WIDTH);
1194 VGfloat imageheight = vgGetParameteri(vgimage, VG_IMAGE_HEIGHT);
1196 //vgScale(command.w,command.h);
1198 if (command.handle) //special behaviour for bw images they act as a mask on the current paint
1200 vgTranslate(command.x, command.y);
1201 vgSetPaint(static_cast<VGPaint>(command.handle), VG_FILL_PATH);
1202 vgSetPaint(static_cast<VGPaint>(command.handle), VG_STROKE_PATH);
1203 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_STENCIL);
1204 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1205 vgScale(aspect_correction, 1.f);
1206 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1207 vgGetMatrix(save_matrix2);
1208 vgScale(imagewidth, imageheight);
1212 //vgScale(720.f/((float)BACKBUFFER_WIDTH), 576.f/((float)BACKBUFFER_HEIGHT));
1213 float scalex = command.w / imagewidth;
1214 float scaley = command.h / imageheight;
1215 float tx = command.x;
1216 float ty = command.y;
1218 if (command.corner == TopLeftLimited)
1220 if (scalex != 0.f && scaley != 0.f)
1222 float imageaspect = imagewidth / imageheight;
1223 float boxaspect = command.w / command.h / aspect_correction;
1225 if (imageaspect > boxaspect)
1228 ty += (command.h - imageheight * scalex / aspect_correction) * 0.5f;
1233 tx += (command.w - imagewidth * scaley * aspect_correction) * 0.5f;
1238 if (scalex == 0.f && scaley == 0.f)
1240 scalex = aspect_correction;
1243 else if (scalex == 0.f)
1245 scalex = scaley * aspect_correction;
1247 else if (scaley == 0.f)
1249 scaley = scalex / aspect_correction;
1252 if (command.corner == BottomRight || command.corner == BottomLeft || command.corner == BottomMiddle)
1254 ty -= imageheight * scaley;
1257 if (command.corner == BottomRight || command.corner == TopRight)
1259 tx -= imagewidth * scalex;
1262 if (command.corner == BottomMiddle || command.corner == TopMiddle)
1264 tx -= imagewidth * scalex * 0.5f;
1267 vgTranslate(tx, ty);
1268 //vgScale(command.w/imagewidth,command.h/imageheight);
1269 vgScale(scalex, scaley);
1270 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1271 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1272 //LogNT::getInstance()->debug(TAG, "TVMedia Draw Image Scale {} {} {} {} {} {}",command.w,imagewidth,command.h,imageheight,scalex,scaley);
1276 //vgTranslate(200.f,500.f);
1277 //vgScale(100.f,100.f);
1279 vgDrawImage(vgimage);
1281 //LogNT::getInstance()->debug(TAG, "Draw Image {} {:#x} {} {} {} {} {:#x}",command.handle,vgimage,command.x,command.y,command.w,command.h,
1285 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1286 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1287 vgLoadMatrix(save_matrix2);
1290 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
1292 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
1293 vgLoadMatrix(save_matrix);
1298 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1299 vgGetMatrix(save_matrix);
1300 vgSetPaint(static_cast<VGPaint>(command.handle), VG_FILL_PATH);
1301 vgSetPaint(static_cast<VGPaint>(command.handle), VG_STROKE_PATH);
1302 vgTranslate(command.x, command.y);
1303 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1304 vgGetMatrix(save_matrix2);
1305 unsigned int glyph_index = FT_Get_Char_Index(ft_face, command.target.textchar);
1306 vgScale(font_exp_x[glyph_index], font_height);
1308 VGfloat gori[] = {0., 0.};
1309 vgSetfv(VG_GLYPH_ORIGIN, 2, gori);
1311 vgDrawGlyph(vgfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1312 //vgDrawPath(std_paths[Rectangle],VG_FILL_PATH);
1313 /* LogNT::getInstance()->debug(TAG, "Draw Glyph {} {} {} {} {} {:#x}",command.handle,command.target.textchar,glyph_index,command.x,command.y,
1315 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1316 vgLoadMatrix(save_matrix);
1317 vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
1318 vgLoadMatrix(save_matrix2);
1320 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1325 cTeletextChar tchar;
1326 tchar.setInternal(command.target.ttchar);
1327 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
1328 vgGetMatrix(save_matrix);
1329 enumTeletextColor ttforegcolour = tchar.GetFGColor();
1330 enumTeletextColor ttbackgcolour = tchar.GetBGColor();
1332 if (tchar.GetBoxedOut())
1334 ttforegcolour = ttcTransparent;
1335 ttbackgcolour = ttcTransparent;
1338 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_STENCIL);
1339 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
1341 vgTranslate(command.x + command.w * 11.85f * 1.4f, command.y + command.h * 19.75f);
1342 VGfloat gori[] = {0., 0.};
1343 vgSetfv(VG_GLYPH_ORIGIN, 2, gori);
1345 vgScale(-1.4f, 2.f);
1346 unsigned int color = Surface::enumTeletextColorToCoulour(ttbackgcolour).rgba();
1347 color = color << 8 | (color & 0xff000000) >> 24;
1348 vgSetColor(vgttpaint, color);
1349 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_FILL_PATH);
1350 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_STROKE_PATH);
1351 cTeletextChar filled;
1352 filled.setInternal(0x187f);
1353 unsigned int glyph_index = loadTTchar(filled);
1354 vgDrawGlyph(vgttfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1356 color = Surface::enumTeletextColorToCoulour(ttforegcolour).rgba();
1357 color = color << 8 | (color & 0xff000000) >> 24;
1358 vgSetColor(vgttpaint, color);
1359 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_FILL_PATH);
1360 vgSetPaint(static_cast<VGPaint>(vgttpaint), VG_STROKE_PATH);
1361 glyph_index = loadTTchar(tchar);
1362 vgDrawGlyph(vgttfont, glyph_index, VG_FILL_PATH, VG_FALSE);
1364 /* LogNT::getInstance()->debug(TAG, "Draw TTchar {:#x} {:#x} {:#x} {:#x}",glyph_index,ttforegcolour,Surface::enumTeletextColorToCoulour(ttforegcolour).rgba(),
1365 vgGetColor(vgttpaint));*/
1367 vgLoadMatrix(save_matrix);
1368 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
1369 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
1370 vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);
1375 VGint coords[4] = {0, 0, 0, 0};
1376 coords[0] = ((command.x + clip_shift_x) * ((float)BACKBUFFER_WIDTH) / 720.f);
1377 coords[1] = ((576.f - command.y - clip_shift_y - command.h) * ((float)BACKBUFFER_HEIGHT) / 576.f);
1378 coords[2] = ((command.w - 1.f) * ((float)BACKBUFFER_WIDTH) / 720.f);
1379 coords[3] = ((command.h - 1.f) * ((float)BACKBUFFER_HEIGHT) / 576.f);
1380 vgSetiv(VG_SCISSOR_RECTS, 4, coords);
1382 if (command.w == 0.f && command.h == 0.f)
1383 vgSeti(VG_SCISSORING, VG_FALSE);
1385 vgSeti(VG_SCISSORING, VG_TRUE);
1389 case DrawImageLoading:
1395 //int imcount=0;// this is debug code and should not go into release
1396 unsigned int OsdOpenVG::handleOpenVGCommand(OpenVGCommand& command)
1398 switch (command.task)
1400 case OVGreplacefont:
1402 vgDestroyFont(vgfont);
1406 case OVGdestroyImageRef: //imcount--;
1408 //LogNT::getInstance()->debug(TAG, "OsdImage Destroy vgDestroyImage {:#x}",command.param1);
1409 vgDestroyImage(static_cast<VGImage>(command.param1));
1412 case OVGdestroyPaint:
1414 //LogNT::getInstance()->debug(TAG, "Draw Paint Destroy {}",command.param1);
1415 vgDestroyPaint(static_cast<VGPaint>(command.param1));
1418 case OVGcreateImagePalette: //imcount++;
1420 VGImage input = vgCreateImage(VG_A_8, command.param1, command.param2,
1421 VG_IMAGE_QUALITY_NONANTIALIASED |
1422 VG_IMAGE_QUALITY_FASTER | VG_IMAGE_QUALITY_BETTER);
1423 //LogNT::getInstance()->debug(TAG, "Draw create palette {} {} {:#x} {}",command.param1,command.param2,vgGetError(),input);
1424 vgImageSubData(input, command.data, command.param1,
1425 VG_A_8, 0, 0, command.param1, command.param2); // upload palettized image data
1426 VGImage handle = vgCreateImage(VG_sRGBA_8888, command.param1, command.param2,
1427 VG_IMAGE_QUALITY_NONANTIALIASED |
1428 VG_IMAGE_QUALITY_FASTER | VG_IMAGE_QUALITY_BETTER);
1429 VGuint* palette = static_cast<VGuint*>(malloc(256 * sizeof(VGuint)));
1430 const VGuint* in_palette = static_cast<const VGuint*>(command.data2);
1432 for (int i = 0; i < 256; i++)
1434 VGuint color = in_palette[i];
1435 palette[i] = color << 8 | (color & 0xff000000) >> 24;
1438 vgLookupSingle(handle, input, palette, VG_ALPHA, VG_FALSE, VG_FALSE);
1440 vgDestroyImage(input);
1444 case OVGcreateMonoBitmap: //imcount++;
1446 VGImage handle = vgCreateImage(VG_A_1, command.param1, command.param2,
1447 VG_IMAGE_QUALITY_FASTER);
1448 //LogNT::getInstance()->debug(TAG, "Draw create mono {} {} {:#x} {}",command.param1,command.param2,vgGetError(),handle);
1449 unsigned int buffer_len = (command.param1 * command.param2) >> 3;
1450 unsigned char* buffer = static_cast<unsigned char*>(malloc(buffer_len));
1451 unsigned char* r_buffer1 = buffer;
1452 const unsigned char* r_buffer2 = static_cast<const unsigned char*>(command.data);
1453 unsigned char* buffer_end = buffer + buffer_len;
1455 while (r_buffer1 != buffer_end)
1457 unsigned char byte = *r_buffer2;
1458 *r_buffer1 = ((byte * 0x0802LU & 0x22110LU) | (byte * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
1459 r_buffer1++; r_buffer2++;
1462 vgImageSubData(handle, buffer, command.param1 >> 3, VG_A_1, 0, 0, command.param1, command.param2);
1464 // LogNT::getInstance()->debug(TAG, "Draw create mono2 {} {} {:#x} {}",command.param1,command.param2,vgGetError(),handle);
1467 /*case OVGcreateImageFile: {
1470 Image *imagefile=(Image*)command.data;
1472 imagefile->write(&imageblob,"RGBA");
1474 handle=vgCreateImage(VG_sXBGR_8888,imagefile->columns(),imagefile->rows(),
1475 VG_IMAGE_QUALITY_BETTER);
1476 //LogNT::getInstance()->debug(TAG, "Draw create image details {} {} {:#x} mark1",imagefile->columns(),imagefile->rows(),(unsigned int*)imageblob.data());
1477 vgImageSubData(handle,imageblob.data(),imagefile->columns()*4,
1478 VG_sXBGR_8888,0,0,imagefile->columns(),imagefile->rows());
1479 //LogNT::getInstance()->debug(TAG, "Draw create image details {} {} {:#x} mark2",imagefile->columns(),imagefile->rows(),(unsigned int*)imageblob.data());
1481 }catch( Exception &error_ )
1483 LogNT::getInstance()->debug(TAG, "Libmagick hT: {}",error_.what());
1488 //LogNT::getInstance()->debug(TAG, "Draw create file {} {} {:#x} {}",command.param1,command.param2,vgGetError(),handle);
1491 case OVGcreateImageMemory: //imcount++;
1493 const PictureInfo* info = static_cast<const PictureInfo*>(command.data);
1495 //LogNT::getInstance()->debug(TAG, "TVMedia OVGcreateImageMemory {}",imcount);
1496 handle = vgCreateImage(VG_sABGR_8888, info->width, info->height, VG_IMAGE_QUALITY_BETTER);
1497 vgImageSubData(handle, info->image, info->width * 4,
1498 VG_sABGR_8888, 0, 0, info->width, info->height);
1499 info->decoder->freeReference(info->reference);
1501 // We have a pictures! send a message to ourself, to switch to gui thread
1502 Message* m = new Message();
1505 m->data = reinterpret_cast<void*>(handle);
1506 m->message = Message::NEW_PICTURE;
1507 m->tag = reinterpret_cast<ULONG>(info->lindex);
1508 MessageQueue::getInstance()->postMessage(m);
1513 case OVGcreateEGLImage: //imcount++;
1515 PictureInfo* info = const_cast<PictureInfo*>(static_cast<const PictureInfo*>(command.data));
1518 handle = vgCreateImage(VG_sABGR_8888, info->width, info->height, VG_IMAGE_QUALITY_BETTER);
1519 // LogNT::getInstance()->debug(TAG, "TVMedia OVGcreateEGLImage {} {} {:#x} {}",info->width,info->height, handle,imcount);
1521 info->handle = handle;
1522 info->reference = eglCreateImageKHR(egl_display, egl_context, EGL_VG_PARENT_IMAGE_KHR, reinterpret_cast<EGLClientBuffer>(handle), NULL);
1523 if (info->reference) return true;
1525 LogNT::getInstance()->debug(TAG, "TVMedia OVGcreateEGLImage {} {} {:#x} Fail!", info->width, info->height, handle);
1526 if (handle) vgDestroyImage(handle);
1529 case OVGreadyEGLImage:
1531 PictureInfo* info = const_cast<PictureInfo*>(static_cast<const PictureInfo*>(command.data));
1532 eglDestroyImageKHR(egl_display, info->reference);
1534 Message* m = new Message();
1537 m->data = reinterpret_cast<void*>(info->handle);
1538 m->message = Message::NEW_PICTURE;
1539 m->tag = reinterpret_cast<ULONG>(info->lindex);
1540 MessageQueue::getInstance()->postMessage(m); // inform command about new picture
1545 case OVGcreateColorRef:
1548 handle = vgCreatePaint();
1549 const DrawStyle* style = static_cast<const DrawStyle*>(command.data);
1553 case DrawStyle::Color:
1555 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
1556 vgSetColor(handle, command.param1);
1558 //rgba = vgGetColor((VGPaint)handle);
1559 //LogNT::getInstance()->debug(TAG, "Draw Paint {} {:#x} {:#x}",handle,command.param1,rgba);
1562 case DrawStyle::GradientLinear:
1564 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_LINEAR_GRADIENT);
1565 VGfloat params[] = {style->x1, style->y1, style->x2, style->y2, style->r};
1566 //LogNT::getInstance()->debug(TAG, "Draw Gradient {} {} {} {} {}",handle,params[0],params[1],params[2],params[3]);
1567 vgSetParameterfv(handle, VG_PAINT_LINEAR_GRADIENT, 4, params);
1570 case DrawStyle::GradientRadial:
1572 vgSetParameteri(handle, VG_PAINT_TYPE, VG_PAINT_TYPE_RADIAL_GRADIENT);
1573 VGfloat params[] = {style->x1, style->y1, style->x2, style->y2, style->r};
1574 vgSetParameterfv(handle, VG_PAINT_RADIAL_GRADIENT, 5, params);
1579 if (style->ft == DrawStyle::GradientLinear || style->ft == DrawStyle::GradientRadial)
1581 VGfloat colorramp[5 * 5];
1582 colorramp[0 + 0 * 5] = 0.f;
1583 colorramp[1 + 0 * 5] = static_cast<float>(style->red) / 255;
1584 colorramp[2 + 0 * 5] = static_cast<float>(style->green) / 255;
1585 colorramp[3 + 0 * 5] = static_cast<float>(style->blue) / 255;
1586 colorramp[4 + 0 * 5] = static_cast<float>(style->alpha) / 255;
1588 for (int i = 0; i < (style->num_colors - 1); i++)
1590 colorramp[0 + (i + 1) * 5] = style->grad_pos[i];
1591 colorramp[1 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].red) / 255.f;
1592 colorramp[2 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].green) / 255.f;
1593 colorramp[3 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].blue) / 255.f;
1594 colorramp[4 + (i + 1) * 5] = static_cast<float>(style->grad_col[i].alpha) / 255.f;
1597 colorramp[0 + (style->num_colors) * 5] = 1.f;
1598 colorramp[1 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].red) / 255.f;
1599 colorramp[2 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].green) / 255.f;
1600 colorramp[3 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].blue) / 255.f;
1601 colorramp[4 + (style->num_colors) * 5] = static_cast<float>(style->grad_col[style->num_colors - 1].alpha) / 255.f;
1602 vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_SPREAD_MODE, VG_COLOR_RAMP_SPREAD_REFLECT);
1603 vgSetParameteri(handle, VG_PAINT_COLOR_RAMP_PREMULTIPLIED, VG_FALSE);
1604 vgSetParameterfv(handle, VG_PAINT_COLOR_RAMP_STOPS, 5 + (style->num_colors) * 5, colorramp);
1610 case OVGimageUploadLine:
1617 void OsdOpenVG::destroyImageRef(VectorHandleImage handle)
1619 struct OpenVGCommand comm;
1620 comm.task = OVGdestroyImageRef;
1621 comm.param1 = handle;
1622 putOpenVGCommand(comm, false);
1625 bool OsdOpenVG::getStaticImageData(unsigned int static_id, UCHAR** userdata, ULONG* length)
1627 if (sa_MAX > static_id)
1629 *userdata = static_artwork_begin[static_id];
1630 *length = static_artwork_end[static_id] - static_artwork_begin[static_id];
1639 void OsdOpenVG::createPicture(struct PictureInfo& pict_inf)
1641 struct OpenVGCommand comm;
1642 LogNT::getInstance()->debug(TAG, "TVMedia Create Picture {}", pict_inf.type);
1644 if (pict_inf.type == PictureInfo::RGBAMemBlock)
1646 comm.task = OVGcreateImageMemory;
1647 comm.data = new PictureInfo(pict_inf); // deleted in the commands processor
1648 putOpenVGCommand(comm, false);
1650 else if (pict_inf.type == PictureInfo::EGLImage)
1652 comm.task = OVGreadyEGLImage;
1653 comm.data = new PictureInfo(pict_inf); // deleted in the commands processor
1654 putOpenVGCommand(comm, false);
1659 pict_inf.decoder->freeReference(pict_inf.reference);
1663 bool OsdOpenVG::getEGLPicture(struct PictureInfo& info, EGLDisplay* display)
1665 struct OpenVGCommand comm;
1666 info.type = PictureInfo::EGLImage;
1667 comm.task = OVGcreateEGLImage;
1669 *display = egl_display;
1670 return putOpenVGCommand(comm, true);
1673 VectorHandleImage OsdOpenVG::createMonoBitmap(void* base, int width, int height)
1675 struct OpenVGCommand comm;
1676 comm.task = OVGcreateMonoBitmap;
1677 comm.param1 = width;
1678 comm.param2 = height;
1680 return putOpenVGCommand(comm, true);
1683 VectorHandleImage OsdOpenVG::createImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data)
1685 struct OpenVGCommand comm;
1686 comm.task = OVGcreateImagePalette;
1687 comm.param1 = width;
1688 comm.param2 = height;
1689 comm.data = image_data;
1690 comm.data2 = palette_data;
1691 return putOpenVGCommand(comm, true);
1694 void OsdOpenVG::destroyDrawStyleHandle(VectorHandle index)
1696 struct OpenVGCommand comm;
1697 comm.task = OVGdestroyPaint;
1698 comm.param1 = index;
1699 putOpenVGCommand(comm, false);
1702 VectorHandle OsdOpenVG::createDrawStyleHandle(const DrawStyle& c)
1704 unsigned int col = c.rgba();
1705 struct OpenVGCommand comm;
1706 comm.task = OVGcreateColorRef;
1707 comm.param1 = col << 8 | (col & 0xff000000) >> 24;
1709 return putOpenVGCommand(comm, true);