2 Copyright 2012 Marten Richter, 2020 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
21 #include "surfacevector.h"
23 #include "vdrresponsepacket.h"
27 #include "osdvector.h"
29 // The next section is activated, if the magick++ PictureReader is provided, it should be available for many POSIX platforms
30 #ifdef PICTURE_DECODER_MAGICK
31 #define MAGICKCORE_QUANTUM_DEPTH 16
32 #define MAGICKCORE_HDRI_ENABLE 0
35 using namespace Magick;
37 static const char* TAG = "OsdVector";
39 class MagickDecoder: public OsdVector::PictureDecoder
42 MagickDecoder(OsdVector::PictureReader* treader): OsdVector::PictureDecoder(treader) {pictInfValid = false;};
44 unsigned char* decodePicture(LoadIndex index, unsigned char* buffer, unsigned int length, bool freemem);
46 bool getDecodedPicture( struct OsdVector::PictureInfo& pict_inf);
48 void freeReference(void* ref);
51 OsdVector::PictureInfo pictInf;
55 unsigned char* MagickDecoder::decodePicture(LoadIndex index, unsigned char* buffer, unsigned int length, bool freemem)
57 if (pictInfValid) return buffer; // does support only one image at a Time;
60 Blob* imageblob = new Blob();
65 LogNT::getInstance()->debug("MagickDecoder", "decodePicture");
67 if (freemem) myblob.updateNoCopy(buffer, length, Blob::MallocAllocator);
68 else myblob.update(buffer, length);
70 magicimage.read(myblob);
72 magicimage.write(imageblob, "RGBA");
75 catch ( Exception& error_ )
77 LogNT::getInstance()->debug("MagickDecoder", "Libmagick: {}", error_.what());
79 LogNT::getInstance()->debug("MagickDecoder", "Libmagick: error mark2");
80 unsigned char* newbuffer = (unsigned char*) malloc(length);
81 memcpy(newbuffer, myblob.data(), length);
85 pictInf.reference = (void*) imageblob;
86 pictInf.width = magicimage.columns();
87 pictInf.height = magicimage.rows();
88 pictInf.image = imageblob->data();
89 pictInf.decoder = this;
90 pictInf.type = OsdVector::PictureInfo::RGBAMemBlock;
91 pictInf.lindex = index;
96 // I can handle everything, so the return value is always true
99 void MagickDecoder::freeReference(void* ref)
101 Blob* todelete = (Blob*) ref;
105 bool MagickDecoder::getDecodedPicture(struct OsdVector::PictureInfo& pict_inf)
107 if (!pictInfValid) return false;
110 pictInfValid = false;
117 OsdVector::OsdVector()
119 logger = LogNT::getInstance();
121 setlocale(LC_CTYPE, "C.UTF-8");
122 #ifdef PICTURE_DECODER_MAGICK
123 reader.addDecoder(new MagickDecoder(&reader));
125 MessageQueue::getInstance()->addReceiver(this);
128 OsdVector::~OsdVector()
130 MessageQueue::getInstance()->removeReceiver(this);
133 bool OsdVector::screenShot(const char* fileName)
135 //Do nothing, if no libmagick is there
136 #ifdef PICTURE_DECODER_MAGICK
138 getRealScreenSize(width, height);
139 size_t length = width * height * 4;
140 void* mem = malloc(length);
146 if (!screenShotInternal(mem, width, height, true))
148 logger->debug(TAG, "Screenshot failed!");
153 myblob.updateNoCopy(mem, length, Blob::MallocAllocator);
154 Image image(myblob, Geometry(width, height), 8, "RGBA");
155 image.write(fileName);
157 catch ( Exception& error_ )
159 logger->debug("MagickEncoder", "Libmagick: {}", error_.what());
168 Surface* OsdVector::createNewSurface()
170 return new SurfaceVector(this);
173 void OsdVector::Blank()
175 // do nothing? remove this one?
178 int OsdVector::restore()
180 // First clear the contents of all registered surfaces
181 surfaces_mutex.lock();
183 //Now go through all surfaces and clear them
184 for (auto& surface : surfaces)
186 surface.commands.clear();
187 surface.commands.reserve(2048);
190 //also clear all handles, they are now invalid, no need to release them
194 drawstyleHandles.clear();
195 drawstyleHandlesRefCounts.clear();
196 drawstyleHandles_lastit_valid = drawstyleHandlesRefCounts_lastit_valid = false;
200 loadindex_ref.clear();
201 tvmedias_load.clear();
202 tvmedias_load_inv.clear();
203 tvmedias_loaded.clear();
205 surfaces_mutex.unlock();
209 void OsdVector::drawSurfaces()
211 surfaces_mutex.lock();
212 std::list<SurfaceInfo*> todraw; //First figure out if a surfaces is below another surface
213 std::list<SurfaceInfo>::iterator itty1 = surfaces.begin();
215 while (itty1 != surfaces.end())
217 std::list<SurfaceInfo>::iterator itty2 = itty1;
221 while (itty2 != surfaces.end())
223 SurfaceInfo& ref1 = *itty1;
224 SurfaceInfo& ref2 = *itty2;
226 if (ref1.x >= ref2.x && ref1.y >= ref2.y
227 && (ref1.x + ref1.w) <= (ref2.x + ref2.w)
228 && (ref1.y + ref1.h) <= (ref2.y + ref2.h) )
237 if (!hidden) // we are not hidden, perfect
239 todraw.push_back(&(*itty1));
246 getScreenSize(swidth, sheight);
247 //Now go through all surfaces and draw them
248 std::list<SurfaceInfo*>::iterator curdraw = todraw.begin();
250 while (curdraw != todraw.end())
252 drawSetTrans(*(*curdraw));
253 std::vector<SVGCommand>::iterator commands = (*(*curdraw)).commands.begin();
254 std::vector<SVGCommand>::iterator end = (*(*curdraw)).commands.end();
256 while (commands != end)
258 // update any images loaded in the mean time
259 if ((*commands).instr == DrawImageLoading)
261 LoadIndex loadindex = (*commands).target.loadindex;
263 if (tvmedias_loaded.find(loadindex) != tvmedias_loaded.end())
265 (*commands).instr = DrawImage;
266 (*commands).target.image = tvmedias_loaded[loadindex];;
267 incImageRef((*commands).target.image);
268 removeLoadIndexRef(loadindex);
273 // Now check if the command is on screen!
274 if (!(*commands).Outside(0, 0, swidth, sheight))
276 executeDrawCommand(*commands);
285 surfaces_mutex.unlock();
288 void OsdVector::updateOrAddSurface(const SurfaceVector* surf, float x, float y, float height, float width, std::vector<SVGCommand>& commands)
290 std::lock_guard<std::mutex> lg(surfaces_mutex);
294 logger->trace(TAG, "updateOrAddSurface, surfaces.length {}", surfaces.size());
298 // First determine it is already in our system
299 for(si = surfaces.begin(); si != surfaces.end(); si++)
301 if (si->surface == surf)
303 // If the surface given (surf) is already in our surfaces vector, reset our SurfaceInfo->commands vector
304 decrementAllRefCounts(si->commands);
305 si->commands.clear();
310 // if not found, make a new SurfaceInfo
311 if (si == surfaces.end())
314 new_sc.surface = surf;
319 si = surfaces.insert(si, new_sc);
322 // update any images loaded in the mean time
324 for (SVGCommand& command : commands)
326 if (command.instr == DrawImageLoading)
328 LoadIndex loadindex = command.target.loadindex;
330 if (tvmedias_loaded.find(loadindex) != tvmedias_loaded.end())
332 command.instr = DrawImage;
333 command.target.image = tvmedias_loaded[loadindex];
334 incImageRef(command.target.image);
335 removeLoadIndexRef(loadindex);
340 si->commands = commands; // Copy surf->commands to our SurfaceInfo->commands
341 incrementAllRefCounts(si->commands);
343 cleanupOrphanedRefs();
346 logger->trace(TAG, "After UOAS:");
350 // OSDOVG-ROD-EXPERIMENT
351 logger->trace(TAG, "EXPERIMENT - call doRender");
355 void OsdVector::removeSurface(const SurfaceVector* surf)
357 std::lock_guard<std::mutex> lg(surfaces_mutex); // FIXME - Can block here on shutdown if a timer is fired just as the wrong time
358 logger->trace("OsdVector-348", "EXPERIMENT - removeSurface");
359 for (auto i = surfaces.begin(); i != surfaces.end(); i++)
361 if (i->surface == surf)
363 decrementAllRefCounts(i->commands);
371 void OsdVector::decrementAllRefCounts(std::vector<SVGCommand>& commands)
373 for (SVGCommand& command : commands)
375 VectorHandle handle = command.getHandle();
376 if (handle != 0) // command might not have a handle
377 decrementDrawStyleHandleRefCount(handle);
379 VectorHandleImage imageHandle = command.getImageHandle();
380 if (imageHandle) removeImageRef(imageHandle);
382 LoadIndex li = command.getLoadIndex();
383 if (li) removeLoadIndexRef(li);
387 void OsdVector::incrementAllRefCounts(std::vector<SVGCommand>& commands)
389 for (SVGCommand& command : commands)
391 VectorHandle handle = command.getHandle();
392 if (handle != 0) // command might not have a handle
393 incrementDrawStyleHandleRefCount(handle);
395 VectorHandleImage imageHandle = command.getImageHandle();
396 if (imageHandle) incImageRef(imageHandle);
398 LoadIndex li = command.getLoadIndex();
399 if (li) incLoadIndexRef(li);
403 void OsdVector::incImageRef(VectorHandleImage handle)
405 if (images_ref.find(handle) == images_ref.end())
407 images_ref[handle] = 1;
411 images_ref[handle]++;
415 void OsdVector::removeImageRef(const VectorHandleImage handle)
417 images_ref[handle]--;
420 int OsdVector::getLoadIndexRef(LoadIndex index)
422 surfaces_mutex.lock();
424 if (loadindex_ref.find(index) == loadindex_ref.end())
430 return loadindex_ref[index];
433 surfaces_mutex.unlock();
436 void OsdVector::incLoadIndexRef(LoadIndex index)
438 if (loadindex_ref.find(index) == loadindex_ref.end())
440 loadindex_ref[index] = 1;
444 loadindex_ref[index]++;
448 void OsdVector::removeLoadIndexRef(const LoadIndex ref)
450 loadindex_ref[ref]--;
452 if (loadindex_ref[ref] == 0)
454 //now check, if it is already loaded
455 std::map<LoadIndex, VectorHandleImage>::iterator itty = tvmedias_loaded.find(ref);
457 if ( itty != tvmedias_loaded.end())
459 removeImageRef((*itty).second); // remove lock
462 tvmedias_loaded.erase(ref);
463 // logger->debug(TAG, "TVMedia removeLoadIndexRef {} {:#x}",tvmedias_load.size(),ref);
464 tvmedias_load.erase(tvmedias_load_inv[ref]);
465 tvmedias_load_inv.erase(ref);
467 reader.invalidateLoadIndex(ref);
471 void OsdVector::cleanupOrphanedRefs()
473 // Do some garbage collection
475 std::map<void*, VectorHandleImage>::iterator mitty = monobitmaps.begin();
477 while (mitty != monobitmaps.end())
479 std::map<VectorHandleImage, int>::iterator curitty = images_ref.find((*mitty).second);
480 int count = (*curitty).second;
484 VectorHandleImage handle = (*curitty).first;
485 monobitmaps.erase(mitty++);
486 images_ref.erase(curitty++);
487 destroyImageRef(handle);
492 /*map<string,VectorHandleImage>::iterator jitty=jpegs.begin();
493 while (jitty!=jpegs.end()) {
494 map<VectorHandleImage,int>::iterator curitty=images_ref.find((*jitty).second);
495 int count=(*curitty).second;
497 VectorHandleImage handle=(*curitty).first;
498 jpegs.erase(jitty++);
499 images_ref.erase(curitty++);
500 destroyImageRef(handle);
504 std::map<TVMediaInfo, VectorHandleImage>::iterator titty = tvmedias.begin();
506 while (titty != tvmedias.end())
508 std::map<VectorHandleImage, int>::iterator curitty = images_ref.find((*titty).second);
509 int count = (*curitty).second;
513 VectorHandleImage handle = (*curitty).first;
514 tvmedias.erase(titty++);
515 images_ref.erase(curitty);
516 destroyImageRef(handle);
522 std::map<TVMediaInfo, LoadIndex>::iterator litty = tvmedias_load.begin();
524 while (litty != tvmedias_load.end())
526 std::map<LoadIndex, int>::iterator curitty = loadindex_ref.find((*litty).second);
527 int count = (*curitty).second;
531 tvmedias_load_inv.erase((*litty).second);
532 tvmedias_loaded.erase((*litty).second);
533 tvmedias_load.erase(litty++);
538 std::list<VectorHandleImage>::iterator pitty = palettepics.begin();
540 while (pitty != palettepics.end())
542 std::map<VectorHandleImage, int>::iterator curitty = images_ref.find((*pitty));
543 int count = (*curitty).second;
547 VectorHandleImage handle = (*curitty).first;
548 palettepics.erase(pitty++);
549 images_ref.erase(curitty++);
550 destroyImageRef(handle);
555 std::map<VectorHandleImage, int>::iterator citty = images_ref.begin();
557 while (citty != images_ref.end())
559 int count = (*citty).second;
563 VectorHandleImage handle = (*citty).first;
564 images_ref.erase(citty++);
565 destroyImageRef(handle);
571 std::map<DrawStyle, VectorHandle>::iterator sitty = drawstyleHandles.begin();
573 while (sitty != drawstyleHandles.end())
575 std::map<VectorHandle, int>::iterator curitty = drawstyleHandlesRefCounts.find((*sitty).second);
576 int count = (*curitty).second;
580 VectorHandle ref = (*curitty).first;
581 drawstyleHandles.erase(sitty++);
582 drawstyleHandlesRefCounts.erase(curitty++);
583 drawstyleHandles_lastit_valid = drawstyleHandlesRefCounts_lastit_valid = false;
584 destroyDrawStyleHandle(ref);
592 int OsdVector::getImageRef(VectorHandleImage handle)
594 surfaces_mutex.lock();
596 if (images_ref.find(handle) == images_ref.end())
602 return images_ref[handle];
605 surfaces_mutex.unlock();
608 void OsdVector::incrementDrawStyleHandleRefCount(VectorHandle index)
610 if (!drawstyleHandlesRefCounts_lastit_valid || (drawstyleHandlesRefCounts_lastit->first != index))
612 drawstyleHandlesRefCounts_lastit = drawstyleHandlesRefCounts.find(index);
613 if (drawstyleHandlesRefCounts_lastit == drawstyleHandlesRefCounts.end())
615 drawstyleHandlesRefCounts_lastit = drawstyleHandlesRefCounts.insert(std::pair<VectorHandle, int>(index, 0)).first;
619 drawstyleHandlesRefCounts_lastit->second++;
620 drawstyleHandlesRefCounts_lastit_valid = true;
623 void OsdVector::decrementDrawStyleHandleRefCount(VectorHandle index)
625 if (!drawstyleHandlesRefCounts_lastit_valid || (drawstyleHandlesRefCounts_lastit->first != index))
627 drawstyleHandlesRefCounts_lastit_valid = false;
628 drawstyleHandlesRefCounts_lastit = drawstyleHandlesRefCounts.find(index);
631 if (drawstyleHandlesRefCounts_lastit != drawstyleHandlesRefCounts.end())
633 drawstyleHandlesRefCounts_lastit_valid = true;
634 drawstyleHandlesRefCounts_lastit->second--;
638 VectorHandle OsdVector::getDrawStyleHandle(const DrawStyle& c)
640 surfaces_mutex.lock();
641 VectorHandle style_handle = 0;
647 if (!drawstyleHandles_lastit_valid || (drawstyleHandles_lastit->first != c))
649 drawstyleHandles_lastit_valid = false;
650 drawstyleHandles_lastit = drawstyleHandles.find(c);
653 if (drawstyleHandles_lastit == drawstyleHandles.end())
655 surfaces_mutex.unlock();
656 style_handle = createDrawStyleHandle(c);
657 surfaces_mutex.lock();
658 drawstyleHandles_lastit = drawstyleHandles.insert(std::pair<DrawStyle, VectorHandle>(c, style_handle)).first;
662 style_handle = drawstyleHandles_lastit->second;
664 //Now check if the handle is valid
665 if (!drawstyleHandlesRefCounts_lastit_valid || (*drawstyleHandlesRefCounts_lastit).first != style_handle)
667 drawstyleHandlesRefCounts_lastit_valid = false;
668 drawstyleHandlesRefCounts_lastit = drawstyleHandlesRefCounts.find(style_handle);
671 if (drawstyleHandlesRefCounts_lastit == drawstyleHandlesRefCounts.end())
673 //invalid handle recreate
674 surfaces_mutex.unlock();
675 style_handle = createDrawStyleHandle(c);
676 surfaces_mutex.lock();
677 drawstyleHandles_lastit->second = style_handle;
679 else drawstyleHandlesRefCounts_lastit_valid = true;
682 drawstyleHandles_lastit_valid = true;
683 incrementDrawStyleHandleRefCount(style_handle);
684 surfaces_mutex.unlock();
689 void OsdVector::dumpStyles()
693 std::map<DrawStyle, VectorHandle>::iterator i;
694 for(i = drawstyleHandles.begin(); i != drawstyleHandles.end(); i++)
696 const DrawStyle* test = &(i->first);
697 logger->debug(TAG, "DumpStyles: {} {}", (void*)test , i->second);
700 std::map<VectorHandle, int>::iterator i2;
701 for (i2 = drawstyleHandlesRefCounts.begin(); i2 != drawstyleHandlesRefCounts.end(); i2++)
703 logger->debug(TAG, "DumpStylesRef: {} {}", i2->first, i2->second);
708 LoadIndex OsdVector::getTVMediaRef(TVMediaInfo& tvmedia, VectorHandleImage& handle)
710 VectorHandleImage localHandle = 0;
711 LoadIndex loadindex = 0;
712 surfaces_mutex.lock();
714 if (tvmedias.find(tvmedia) == tvmedias.end()) // if not found, return a loadIndex from loadTVMedia(tvmedia)
716 loadindex = loadTVMedia(tvmedia);
718 else // if found, return a real imageIndex. incImageRef(localHandle). EXCEPT: If it's not found in images_ref (???) in which case do same as above.
720 localHandle = tvmedias[tvmedia];
722 if (images_ref.find(localHandle) == images_ref.end())
724 //invalid handle recreate
725 loadindex = loadTVMedia(tvmedia);
730 incImageRef(localHandle);
734 /*tvmedias[tvmedia]=createTVMedia(tvmedia,width,height);
735 incImageRef(localHandle);*/
736 handle = localHandle;
737 surfaces_mutex.unlock();
741 LoadIndex OsdVector::loadTVMedia(TVMediaInfo& tvmedia) // insert a tvmedia for loading
745 if (tvmedias_load.find(tvmedia) == tvmedias_load.end()) // if not found in tvmedias_load
747 switch (tvmedia.getType())
750 index = VDR::getInstance()->loadTVMediaRecThumb(tvmedia);
755 index = ((long long) tvmedia.getPrimaryID()) << 32LL;
756 reader.addStaticImage(tvmedia.getPrimaryID());
760 index = VDR::getInstance()->loadTVMediaEventThumb(tvmedia);
764 index = VDR::getInstance()->loadChannelLogo(tvmedia);
768 index = VDR::getInstance()->loadTVMedia(tvmedia);
772 if (tvmedia.getType() != 4 && tvmedia.getStaticFallback() > -1)
774 reader.informFallback(index, tvmedia.getStaticFallback());
777 tvmedias_load[tvmedia] = index;
778 tvmedias_load_inv[index] = tvmedia;
782 index = tvmedias_load[tvmedia];
785 incLoadIndexRef(index);
790 void OsdVector::informPicture(LoadIndex index, VectorHandleImage handle)
792 //Beware for thread safety
793 VectorHandleImage localHandle = 0;
795 logger->debug(TAG, "TVMedia Picture for load-id {:#x} arrived index {:#x}", index, handle);
796 surfaces_mutex.lock();
797 TVMediaInfo tvmedia = tvmedias_load_inv[index];
801 std::map<LoadIndex, int>::iterator itty = loadindex_ref.find(index);
802 localHandle = tvmedias[tvmedia] = handle;
803 tvmedias_loaded[index] = localHandle;
805 if (itty == loadindex_ref.end() || (*itty).second == 0)
807 // we do not want the picture anymore . Really...
808 // fill images_ref in to not irritate the garbage collector
809 if (images_ref.find(localHandle) == images_ref.end())
811 images_ref[localHandle] = 0;
816 incImageRef(localHandle); // hold one index until all loadings refs are gone;
820 surfaces_mutex.unlock();
823 void OsdVector::processMessage(Message* m)
827 case Message::NEW_PICTURE:
829 //logger->debug(TAG, "TVMedia NEW_PICTURE");
830 informPicture(m->tag, reinterpret_cast<VectorHandleImage>(m->data));
833 case Message::NEW_PICTURE_STATIC:
835 //logger->debug(TAG, "TVMedia NEW_PICTURE {:#x} {:#x}", m->tag, m->parameter);
836 informPicture(static_cast<unsigned long long>(m->tag) << 32LL, reinterpret_cast<VectorHandleImage>(m->data));
843 VectorHandleImage OsdVector::getJpegRef(const char* fileName, int *width,int *height)
845 VectorHandleImage image_handle=0;
846 if (jpegs.find(fileName)==jpegs.end())
848 image_handle=jpegs[fileName]=createJpeg(fileName,width,height);
850 image_handle=jpegs[fileName];
853 if (images_ref.find(image_handle)==images_ref.end()) {
854 //invalid handle recreate
855 image_handle=jpegs[fileName]=createJpeg(fileName,width,height);
858 incImageRef(image_handle);
863 VectorHandleImage OsdVector::getMonoBitmapRef(void* base, int width, int height)
865 VectorHandleImage image_handle;
866 surfaces_mutex.lock();
868 if (monobitmaps.find(base) == monobitmaps.end())
870 surfaces_mutex.unlock();
871 image_handle = createMonoBitmap(base, width, height);
872 surfaces_mutex.lock();
873 monobitmaps[base] = image_handle;
877 image_handle = monobitmaps[base];
879 if (images_ref.find(image_handle) == images_ref.end())
881 //invalid handle recreate
882 surfaces_mutex.unlock();
883 image_handle = createMonoBitmap(base, width, height);
884 surfaces_mutex.lock();
885 monobitmaps[base] = image_handle;
889 incImageRef(image_handle);
890 surfaces_mutex.unlock();
894 VectorHandleImage OsdVector::getImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data)
896 VectorHandleImage image_handle;
897 image_handle = createImagePalette(width, height, image_data, palette_data);
898 surfaces_mutex.lock();
899 palettepics.push_back(image_handle);
900 incImageRef(image_handle);
901 surfaces_mutex.unlock();
905 /// PictureReader Class
907 OsdVector::PictureReader::~PictureReader()
910 if (readerThread.joinable())
912 threadReqQuit = true;
913 threadCond.notify_one();
914 threadMutex.unlock();
918 threadMutex.unlock();
921 decoders_lock.lock();
922 while (decoders.size())
924 PictureDecoder* dec = decoders.front();
925 decoders.pop_front();
928 decoders_lock.unlock();
931 void OsdVector::PictureReader::init()
934 readerThread = std::thread( [this]
937 threadMutex.unlock();
940 threadMutex.unlock();
943 void OsdVector::PictureReader::shutdown()
945 std::unique_lock<std::mutex> lg(threadMutex);
947 if (readerThread.joinable())
949 threadReqQuit = true;
950 threadCond.notify_one();
956 void OsdVector::PictureReader::addDecoder(PictureDecoder* decoder)
958 decoders_lock.lock();
960 decoders.push_front(decoder);
961 decoders_lock.unlock();
964 void OsdVector::PictureReader::removeDecoder(PictureDecoder* decoder)
966 decoders_lock.lock();
967 std::list<PictureDecoder*>::iterator itty = decoders.begin();
969 while (itty != decoders.end())
971 if ((*itty) == decoder)
973 decoders.erase(itty);
980 LogNT::getInstance()->debug(TAG, "removeDecoder");
983 decoders_lock.unlock();
986 void OsdVector::PictureReader::threadMethod()
988 OsdVector* osdvector = dynamic_cast<OsdVector*>(Osd::getInstance());
989 LogNT::getInstance()->debug("PictureReader", "TVMedia Start Picture Reader");
991 std::unique_lock<std::mutex> ul(threadMutex); // locked
995 threadCond.wait(ul, [this]{ return threadReqQuit || runLoop; });
997 if (threadReqQuit) return; // unlock
998 LogNT::getInstance()->debug("PictureReader", "Running loop");
1002 // unlocked while we work
1007 miniRunAgain = false;
1009 PictureInfo pictinf;
1010 decoders_lock.lock();
1011 std::list<PictureDecoder*>::iterator itty = decoders.begin();
1013 while (itty != decoders.end())
1015 if ((*itty)->getDecodedPicture(pictinf)) // FIXME somewhere around here?, or in getDecodedPicture, try SA optimisation
1017 miniRunAgain = true;
1018 osdvector->createPicture(pictinf);
1023 if (processReceivedPictures()) miniRunAgain = true;
1024 decoders_lock.unlock();
1026 LogNT::getInstance()->debug("PictureReader", "miniRunAgain = true...");
1028 } while(miniRunAgain);
1034 void OsdVector::PictureReader::invalidateLoadIndex(LoadIndex index)
1036 pict_lock_incoming.lock();
1037 invalid_loadindex.insert(index);
1038 pict_lock_incoming.unlock();
1040 // From conversion to std::thread
1041 // The thread used to spin every 10ms, now it only waits on the cond
1042 // This method does not signal the main thread, does it need to now?
1043 // Doesn't look like it, but if it does get the 4 lines below
1046 void OsdVector::PictureReader::informFallback(LoadIndex index, int fallback)
1048 pict_lock_incoming.lock();
1049 inform_fallback[index] = fallback;
1050 pict_lock_incoming.unlock();
1051 // Also here, does this need to signal the thread?
1054 void OsdVector::PictureReader::receivePicture(VDR_ResponsePacket* vresp)
1056 pict_lock_incoming.lock();
1057 pict_incoming.push(vresp);
1058 pict_lock_incoming.unlock();
1060 // These 4 lines to signal the thread
1063 threadCond.notify_one();
1064 threadMutex.unlock();
1067 void OsdVector::PictureReader::addStaticImage(unsigned int id)
1069 pict_lock_incoming.lock();
1070 pict_incoming_static.push(id);
1071 invalid_loadindex.erase(((long long) id) << 32LL);
1072 pict_lock_incoming.unlock();
1076 threadCond.notify_one();
1077 threadMutex.unlock();
1082 #pragma warning(disable : 4703)
1085 bool OsdVector::PictureReader::processReceivedPictures()
1087 bool decoded = false;
1089 pict_lock_incoming.lock();
1091 if (pict_incoming.size())
1093 VDR_ResponsePacket* vresp = pict_incoming.front();
1094 pict_incoming.pop();
1095 std::set<LoadIndex>::iterator setpos = invalid_loadindex.find(vresp->getStreamID());
1097 if (setpos != invalid_loadindex.end())
1100 invalid_loadindex.erase(setpos);
1103 pict_lock_incoming.unlock();
1105 if (!valid) // we do not want it anymore skip it;
1111 // LogNT::getInstance()->debug(TAG, "TVMedia Pictures arrived VDR {:#x} {} {}",
1112 // vresp->getStreamID(),vresp->getUserDataLength(),vresp->getFlag());
1113 bool decode = false;
1118 if (vresp->getFlag() != 2)
1120 userdata = vresp->getUserData();
1121 length = vresp->getUserDataLength();
1128 pict_lock_incoming.lock();
1130 if (inform_fallback.find(vresp->getStreamID()) != inform_fallback.end())
1132 fallback = inform_fallback[vresp->getStreamID()];
1135 pict_lock_incoming.unlock();
1137 if (fallback >= 0 && ((OsdVector*)Osd::getInstance())->getStaticImageData(fallback, &userdata, &length))
1146 std::list<PictureDecoder*>::iterator itty = decoders.begin();
1148 while (itty != decoders.end())
1150 userdata = (*itty)->decodePicture(vresp->getStreamID(), userdata, length, freed);
1161 if (!decoded && userdata && freed)
1167 pict_lock_incoming.lock();
1168 inform_fallback.erase(vresp->getStreamID());
1169 pict_lock_incoming.unlock();
1170 //else osd->informPicture(vresp->getStreamID(), 0);
1173 else if (pict_incoming_static.size())
1175 unsigned int static_id = pict_incoming_static.front();
1176 pict_incoming_static.pop();
1177 std::set<LoadIndex>::iterator setpos = invalid_loadindex.find(((long long) static_id) << 32LL);
1179 if (setpos != invalid_loadindex.end())
1182 invalid_loadindex.erase(setpos);
1185 pict_lock_incoming.unlock();
1187 if (!valid) // we do not want it anymore skip it;
1195 if (((OsdVector*)Osd::getInstance())->getStaticImageData(static_id, &userdata, &length))
1197 std::list<PictureDecoder*>::iterator itty = decoders.begin();
1199 while (itty != decoders.end())
1201 if (!(*itty)->decodePicture(((long long) static_id) << 32LL, userdata, length, false))
1213 pict_lock_incoming.unlock();
1216 if (pict_incoming.size() || pict_incoming_static.size()) return true;