]> git.vomp.tv Git - vompclient.git/commitdiff
OsdVector: Minor code readability mods
authorChris Tallon <chris@vomp.tv>
Mon, 11 May 2020 22:27:12 +0000 (23:27 +0100)
committerChris Tallon <chris@vomp.tv>
Mon, 11 May 2020 22:27:12 +0000 (23:27 +0100)
osdopenvg.cc
osdvector.cc
osdvector.h
surfacevector.cc

index cc1f7b62b92ce4acda9b35a01bcf2168ea2c28b6..d416b197f30aab3dbd489ade58e40959d8747095 100644 (file)
@@ -647,7 +647,6 @@ void OsdOpenVG::threadMethod()
 
     if (initted)
     {
-
       long long time1 = getTimeMS();
 
       if ((time1 - lastrendertime) > 200)  //5 fps for OSD updates are enough, avoids tearing
@@ -660,7 +659,6 @@ void OsdOpenVG::threadMethod()
       else
       {
         ts = time1 - lastrendertime;
-
       }
 
       if (processTasks()) ts = 0;
@@ -692,8 +690,6 @@ void OsdOpenVG::threadMethod()
       threadWaitForSignalTimed(&target_time);
       threadUnlock();
     }
-
-    //Sleep(1);
   }
 
   eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
@@ -1614,7 +1610,6 @@ bool OsdOpenVG::processTasks()
 
     taskmutex.unlock();
     vgmutex.unlock();
-    //threadCheckExit();
 
     /* Getting rid of Signal class. As with VideoOMX, just replicate what Signal did here
      * and figure out if any of this can be simplified later. e.g. taskmutex sounds
@@ -1662,7 +1657,7 @@ bool OsdOpenVG::haveOpenVGResponse(unsigned int id, unsigned int* resp)
 }
 
 
-unsigned int  OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm, bool wait)
+unsigned int OsdOpenVG::putOpenVGCommand(OpenVGCommand& comm, bool wait)
 {
   taskmutex.lock();
 
index d4b4ab15fb35ceea3ee70b9cff2ef12e469c1a42..e662afcaa0c902380380057fc92739295915ebd5 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright 2012 Marten Richter
+    Copyright 2012 Marten Richter, 2020 Chris Tallon
 
     This file is part of VOMP.
 
     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
 */
 
-#include "osdvector.h"
 #include "surfacevector.h"
 #include "vdr.h"
 #include "vdrresponsepacket.h"
 #include "control.h"
 #include "message.h"
 
+#include "osdvector.h"
+
 // The next section is activated, if the magick++ PictureReader is provided, it should be available for many POSIX platforms
 #ifdef PICTURE_DECODER_MAGICK
 #define MAGICKCORE_QUANTUM_DEPTH 16
@@ -121,8 +122,6 @@ OsdVector::OsdVector()
   {
     byte_char_width[i] = 0.f;
   }
-
-  styles_lastit_valid = styles_ref_lastit_valid = false;
 }
 
 void OsdVector::screenShot(const char* fileName)
@@ -163,7 +162,6 @@ Surface* OsdVector::createNewSurface()
   return new SurfaceVector(this);
 }
 
-
 void OsdVector::Blank()
 {
   // do nothing? remove this one?
@@ -174,14 +172,11 @@ int OsdVector::restore()
   // First clear the contents of all registered surfaces
   surfaces_mutex.lock();
 
-  //Now go through all surfaces and draw them
-  std::list<SurfaceInfo>::iterator curdraw = surfaces.begin();
-
-  while (curdraw != surfaces.end())
+  //Now go through all surfaces and clear them
+  for (auto& surface : surfaces)
   {
-    (*curdraw).commands.clear();
-    (*curdraw).commands.reserve(2048);
-    curdraw++;
+    surface.commands.clear();
+    surface.commands.reserve(2048);
   }
 
   //also clear all handles, they are now invalid, no need to release them
@@ -282,133 +277,102 @@ void OsdVector::drawSurfaces()
   surfaces_mutex.unlock();
 }
 
-void OsdVector::updateOrAddSurface(const SurfaceVector* surf, float x, float y, float height, float width,
-                                   std::vector<SVGCommand>& commands)
+void OsdVector::updateOrAddSurface(const SurfaceVector* surf, float x, float y, float height, float width, std::vector<SVGCommand>& commands)
 {
-  surfaces_mutex.lock();
-  //First determine it is already in our system
-  std::list<SurfaceInfo>::iterator itty = surfaces.begin();
+  std::lock_guard<std::mutex> lg(surfaces_mutex);
+  SurfacesIterator si;
 
-  while (itty != surfaces.end())
+  // First determine it is already in our system
+  for(si = surfaces.begin(); si != surfaces.end(); si++)
   {
-    if ((*itty).surf == surf)
+    if (si->surface == surf)
     {
-      //decrease the references
-      decrementAllRefCounts((*itty).commands);
+      // If the surface given (surf) is already in our surfaces vector, reset our SurfaceInfo->commands vector
+      decrementAllRefCounts(si->commands);
+      si->commands.clear();
       break;
     }
-
-    itty++;
   }
 
-  // if not insert it
-  if (itty == surfaces.end())
+  // if not found, make a new SurfaceInfo
+  if (si == surfaces.end())
   {
     SurfaceInfo new_sc;
-    new_sc.surf = surf;
+    new_sc.surface = surf;
     new_sc.x = x;
     new_sc.y = y;
     new_sc.w = width;
     new_sc.h = height;
-    itty = surfaces.insert(itty, new_sc);
+    si = surfaces.insert(si, new_sc);
   }
 
   // update any images loaded in the mean time
-  std::vector<SVGCommand>::iterator ilitty = commands.begin();
 
-  while (ilitty != commands.end())
+  for (SVGCommand& command : commands)
   {
-    if ((*ilitty).instr == DrawImageLoading)
+    if (command.instr == DrawImageLoading)
     {
-      LoadIndex loadindex = (*ilitty).target.loadindex;
+      LoadIndex loadindex = command.target.loadindex;
 
       if (tvmedias_loaded.find(loadindex) != tvmedias_loaded.end())
       {
-
-        (*ilitty).instr = DrawImage;
-        (*ilitty).target.image = tvmedias_loaded[loadindex];
-        incImageRef((*ilitty).target.image);
+        command.instr = DrawImage;
+        command.target.image = tvmedias_loaded[loadindex];
+        incImageRef(command.target.image);
         removeLoadIndexRef(loadindex);
       }
     }
-
-    ilitty++;
   }
 
+  si->commands = commands; // Copy surf->commands to our SurfaceInfo->commands
+  incrementAllRefCounts(si->commands);
 
-  // then clear and copy
-  (*itty).commands.clear();
-  (*itty).commands = commands;
-  //increase the references
-  incrementAllRefCounts((*itty).commands);
   cleanupOrphanedRefs();
-
-  surfaces_mutex.unlock();
 }
 
 void OsdVector::removeSurface(const SurfaceVector* surf)
 {
-  surfaces_mutex.lock();
-  //First determine it is already in our system
-  std::list<SurfaceInfo>::iterator itty = surfaces.begin();
-
-  while (itty != surfaces.end())
+  std::lock_guard<std::mutex> lg(surfaces_mutex);
+  for (auto i = surfaces.begin(); i != surfaces.end(); i++)
   {
-    if ((*itty).surf == surf)
+    if (i->surface == surf)
     {
-      decrementAllRefCounts((*itty).commands);
-      (*itty).commands.clear();
-      surfaces.erase(itty);
-      break;
+      decrementAllRefCounts(i->commands);
+      i->commands.clear();
+      surfaces.erase(i);
+      return;
     }
-
-    itty++;
   }
-
-  surfaces_mutex.unlock();
-
 }
 
-void OsdVector::decrementAllRefCounts(std::vector<SVGCommand>& commands )
+void OsdVector::decrementAllRefCounts(std::vector<SVGCommand>& commands)
 {
-
-  std::vector<SVGCommand>::iterator sitty = commands.begin();
-
-  while (sitty != commands.end())
+  for (SVGCommand& command : commands)
   {
-    removeStyleRef((*sitty).getRef());
-    ImageIndex ii = (*sitty).getImageIndex();
+    decrementStyleRefCount(command.getRef());
 
+    ImageIndex ii = command.getImageIndex();
     if (ii) removeImageRef(ii);
 
-    LoadIndex li = (*sitty).getLoadIndex();
-
+    LoadIndex li = command.getLoadIndex();
     if (li) removeLoadIndexRef(li);
-
-    sitty++;
   }
 }
 
-void OsdVector::incrementAllRefCounts(std::vector<SVGCommand>& commands )
+void OsdVector::incrementAllRefCounts(std::vector<SVGCommand>& commands)
 {
-  std::vector<SVGCommand>::iterator sitty = commands.begin();
-
-  while (sitty != commands.end())
+  for (SVGCommand& command : commands)
   {
-    incStyleRef((*sitty).getRef());
-    ImageIndex ii = (*sitty).getImageIndex();
+    incrementStyleRefCount(command.getRef());
 
+    ImageIndex ii = command.getImageIndex();
     if (ii) incImageRef(ii);
 
-    LoadIndex li = (*sitty).getLoadIndex();
-
+    LoadIndex li = command.getLoadIndex();
     if (li) incLoadIndexRef(li);
-
-    sitty++;
   }
 }
 
-
 void OsdVector::incImageRef(ImageIndex index)
 {
   if (images_ref.find(index) == images_ref.end())
@@ -474,13 +438,9 @@ void OsdVector::removeLoadIndexRef(const LoadIndex ref)
     tvmedias_load_inv.erase(ref);
 
     reader.invalidateLoadIndex(ref);
-
-
   }
 }
 
-
-
 void OsdVector::cleanupOrphanedRefs()
 {
   // Do some garbage collection
@@ -602,7 +562,6 @@ void OsdVector::cleanupOrphanedRefs()
   }
 }
 
-
 int OsdVector::getImageRef(ImageIndex index)
 {
   surfaces_mutex.lock();
@@ -619,29 +578,24 @@ int OsdVector::getImageRef(ImageIndex index)
   surfaces_mutex.unlock();
 }
 
-void OsdVector::incStyleRef(VectorHandle index)
+void OsdVector::incrementStyleRefCount(VectorHandle index)
 {
-  if (!styles_ref_lastit_valid || (*styles_ref_lastit).first != index)
+  if (!styles_ref_lastit_valid || (styles_ref_lastit->first != index))
   {
-    styles_ref_lastit_valid = false;
     styles_ref_lastit = styles_ref.find(index);
+    if (styles_ref_lastit == styles_ref.end())
+    {
+      styles_ref_lastit = styles_ref.insert(std::pair<VectorHandle, int>(index, 0)).first;
+    }
   }
 
-  if (styles_ref_lastit == styles_ref.end())
-  {
-    styles_ref_lastit = styles_ref.insert(std::pair<VectorHandle, int>(index, 1)).first;
-  }
-  else
-  {
-    (*styles_ref_lastit).second++;
-  }
-
+  styles_ref_lastit->second++;
   styles_ref_lastit_valid = true;
 }
 
-void OsdVector::removeStyleRef(VectorHandle index)
+void OsdVector::decrementStyleRefCount(VectorHandle index)
 {
-  if (!styles_ref_lastit_valid || (*styles_ref_lastit).first != index)
+  if (!styles_ref_lastit_valid || (styles_ref_lastit->first != index))
   {
     styles_ref_lastit_valid = false;
     styles_ref_lastit = styles_ref.find(index);
@@ -650,7 +604,7 @@ void OsdVector::removeStyleRef(VectorHandle index)
   if (styles_ref_lastit != styles_ref.end())
   {
     styles_ref_lastit_valid = true;
-    (*styles_ref_lastit).second--;
+    styles_ref_lastit->second--;
   }
 }
 
@@ -659,7 +613,7 @@ VectorHandle OsdVector::getStyleRef(const DrawStyle& c)
   surfaces_mutex.lock();
   VectorHandle style_handle = 0;
 
-  if (!styles_lastit_valid || (*styles_lastit).first != c)
+  if (!styles_lastit_valid || (styles_lastit->first != c))
   {
     styles_lastit_valid = false;
     styles_lastit = styles.find(c);
@@ -674,8 +628,7 @@ VectorHandle OsdVector::getStyleRef(const DrawStyle& c)
   }
   else
   {
-
-    style_handle = (*styles_lastit).second;
+    style_handle = styles_lastit->second;
 
     //Now check if the handle is valid
     if (!styles_ref_lastit_valid || (*styles_ref_lastit).first != style_handle)
@@ -690,13 +643,13 @@ VectorHandle OsdVector::getStyleRef(const DrawStyle& c)
       surfaces_mutex.unlock();
       style_handle = createStyleRef(c);
       surfaces_mutex.lock();
-      (*styles_lastit).second = style_handle;
+      styles_lastit->second = style_handle;
     }
     else styles_ref_lastit_valid = true;
   }
 
   styles_lastit_valid = true;
-  incStyleRef(style_handle);
+  incrementStyleRefCount(style_handle);
   surfaces_mutex.unlock();
   return style_handle;
 }
@@ -783,8 +736,6 @@ LoadIndex OsdVector::loadTVMedia(TVMediaInfo& tvmedia)
   return index;
 }
 
-
-
 void OsdVector::informPicture(LoadIndex index, ImageIndex imageIndex)
 {
   //Beware for thread safety
@@ -816,13 +767,8 @@ void OsdVector::informPicture(LoadIndex index, ImageIndex imageIndex)
   }
 
   surfaces_mutex.unlock();
-
-
 }
 
-
-
-
 /*
 ImageIndex OsdVector::getJpegRef(const char* fileName, int *width,int *height)
 {
@@ -875,7 +821,7 @@ ImageIndex OsdVector::getMonoBitmapRef(void* base, int width, int height)
   return image_handle;
 }
 
-ImageIndex  OsdVector::getImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data)
+ImageIndex OsdVector::getImagePalette(int width, int height, const unsigned char* image_data, const unsigned int* palette_data)
 {
   ImageIndex image_handle;
   image_handle = createImagePalette(width, height, image_data, palette_data);
@@ -890,7 +836,7 @@ OsdVector::PictureReader::~PictureReader()
 {
   decoders_lock.lock();
 
-  while ( decoders.size())
+  while (decoders.size())
   {
     PictureDecoder* dec = decoders.front();
     decoders.pop_front();
@@ -908,8 +854,6 @@ void OsdVector::PictureReader::init()
 void OsdVector::PictureReader::shutdown()
 {
   threadStop();
-
-
 }
 
 void OsdVector::PictureReader::addDecoder(PictureDecoder* decoder)
@@ -1066,8 +1010,8 @@ bool OsdVector::PictureReader::processReceivedPictures()
       return true;
     }
 
-    //          Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia Pictures arrived VDR %x %d %d",
-    //                  vresp->getStreamID(),vresp->getUserDataLength(),vresp->getFlag());
+    // Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia Pictures arrived VDR %x %d %d",
+    // vresp->getStreamID(),vresp->getUserDataLength(),vresp->getFlag());
     bool decode = false;
     bool freed = false;
     UCHAR* userdata;
@@ -1097,7 +1041,6 @@ bool OsdVector::PictureReader::processReceivedPictures()
         decode = true;
         freed = false;
       }
-
     }
 
     if (decode)
@@ -1172,7 +1115,6 @@ bool OsdVector::PictureReader::processReceivedPictures()
     pict_lock_incoming.unlock();
   }
 
-
   if (pict_incoming.size() || pict_incoming_static.size()) return true;
 
   return decoded;
index 73962d45908daa2c994c4b26477635391bedde61..6695e99bec481e06b9f452177a6b7bdd85bbb600 100644 (file)
@@ -206,7 +206,7 @@ class VDR_ResponsePacket;
 
 struct SurfaceInfo
 {
-  const SurfaceVector* surf;
+  const SurfaceVector* surface;
   std::vector<SVGCommand> commands;
   float x, y, w, h;
 };
@@ -246,7 +246,7 @@ class OsdVector : public Osd
     void removeImageRef(const ImageIndex ref);
     void removeLoadIndexRef(const LoadIndex ref);
     VectorHandle getStyleRef(const DrawStyle& c);
-    virtual void removeStyleRef(VectorHandle ref);
+    virtual void decrementStyleRefCount(VectorHandle ref);
     virtual void getScreenSize(int& width, int& height) = 0;
     virtual void getRealScreenSize(int& width, int& height) = 0;
 
@@ -385,21 +385,21 @@ class OsdVector : public Osd
 
 
 
-    void incStyleRef(VectorHandle index);
+    void incrementStyleRefCount(VectorHandle index);
     virtual void destroyStyleRef(VectorHandle index) = 0;
 
 
     std::map<DrawStyle, VectorHandle> styles;
     std::map<VectorHandle, int> styles_ref;
     std::map<DrawStyle, VectorHandle>::iterator styles_lastit;
-    bool styles_lastit_valid;
+    bool styles_lastit_valid{};
     std::map<VectorHandle, int>::iterator styles_ref_lastit;
-    bool styles_ref_lastit_valid;
+    bool styles_ref_lastit_valid{};
 
     virtual VectorHandle createStyleRef(const DrawStyle& c) = 0;
 
-    void decrementAllRefCounts(std::vector<SVGCommand>& commands );
-    void incrementAllRefCounts(std::vector<SVGCommand>& commands );
+    void decrementAllRefCounts(std::vector<SVGCommand>& commands);
+    void incrementAllRefCounts(std::vector<SVGCommand>& commands);
     void cleanupOrphanedRefs();
 
 
@@ -409,6 +409,7 @@ class OsdVector : public Osd
 
 
     std::list<SurfaceInfo> surfaces;
+    using SurfacesIterator = std::list<SurfaceInfo>::iterator;
 
     std::mutex surfaces_mutex;
 
index 124da80768504d13ca13253338a4bdd0a5e3fce0..397273af4ae776a55ca01eace0b39e6de90beb2c 100644 (file)
@@ -26,7 +26,6 @@
 
 SurfaceVector::SurfaceVector(OsdVector* vosd)
 {
-
   osd = vosd;
   commands.reserve(2048);
 }
@@ -34,27 +33,19 @@ SurfaceVector::SurfaceVector(OsdVector* vosd)
 SurfaceVector::~SurfaceVector()
 {
   osd->removeSurface(this);
-  std::vector<SVGCommand>::iterator itty = commands.begin();
 
-  while (itty != commands.end())
+  for (SVGCommand& command : commands)
   {
-    osd->removeStyleRef((*itty).getRef()); // We remove the Style reference, so that osd can free stuff
-    ImageIndex ii = (*itty).getImageIndex();
-
-    if (ii)
-    {
-      osd->removeImageRef(ii);
-    }
+    osd->decrementStyleRefCount(command.getRef()); // We remove the Style reference, so that osd can free stuff
 
-    LoadIndex li = (*itty).getLoadIndex();
+    ImageIndex ii = command.getImageIndex();
+    if (ii) osd->removeImageRef(ii);
 
+    LoadIndex li = command.getLoadIndex();
     if (li) osd->removeLoadIndexRef(li);
-
-    itty++;
   }
 }
 
-
 int SurfaceVector::getFontHeight()
 {
   return (int)osd->getFontHeight();
@@ -65,7 +56,6 @@ float SurfaceVector::getCharWidth(wchar_t c)
   return osd->getCharWidth(c);
 }
 
-
 int SurfaceVector::drawText(const char* text, int x, int y, const DrawStyle& c)
 {
   return drawText(text, x, y, 0, c);
@@ -389,7 +379,7 @@ int SurfaceVector::removeCommands(float x, float y, float width, float height)
     {
       //Log::getInstance()->log("OSD", Log::DEBUG, "Remove command %d %g %g %g %g %d %d",(*itty).instr,
       //(*itty).x,(*itty).y,(*itty).w,(*itty).h,(*itty).reference,(*itty).target.image);
-      osd->removeStyleRef((*itty).getRef()); // We remove the Style reference, so that osd can free stuff
+      osd->decrementStyleRefCount((*itty).getRef()); // We remove the Style reference, so that osd can free stuff
       ImageIndex ii = (*itty).getImageIndex();
 
       if (ii) osd->removeImageRef(ii);