don't crash if time is *really* invalid
[enigma2.git] / lib / gdi / grc.cpp
index b09171f6e19acad53dba2951be6f4f6bdfe87fe8..19a32b62b5c379ea095089c4131ec90b6564c450 100644 (file)
@@ -1,10 +1,4 @@
-// for debugging use:
-// #define SYNC_PAINT
 #include <unistd.h>
-#ifndef SYNC_PAINT
-#include <pthread.h>
-#endif
-
 #include <lib/gdi/grc.h>
 #include <lib/gdi/font.h>
 #include <lib/base/init.h>
@@ -63,8 +57,7 @@ void gRC::submit(const gOpcode &o)
 #ifndef SYNC_PAINT
                pthread_mutex_lock(&mutex);
 #endif
-               int tmp=wp;
-               tmp+=1;
+               int tmp=wp+1;
                if ( tmp == MAXSIZE )
                        tmp=0;
                if ( tmp == rp )
@@ -102,23 +95,27 @@ void *gRC::thread()
 #ifndef SYNC_PAINT
        while (1)
        {
-               singleLock s(mutex);
 #else
        while (rp != wp)
        {
+#endif
+#ifndef SYNC_PAINT
+               pthread_mutex_lock(&mutex);
 #endif
                if ( rp != wp )
                {
-                       gOpcode& o(queue[rp]);
+                       gOpcode o(queue[rp++]);
+                       if ( rp == MAXSIZE )
+                               rp=0;
+#ifndef SYNC_PAINT
+                       pthread_mutex_unlock(&mutex);
+#endif
                        if (o.opcode==gOpcode::shutdown)
                                break;
                        else if (o.opcode==gOpcode::notify)
                                need_notify = 1;
                        else
                                o.dc->exec(&o);
-                       rp++;
-                       if ( rp == MAXSIZE )
-                               rp=0;
                }
                else
                {
@@ -129,6 +126,7 @@ void *gRC::thread()
                        }
 #ifndef SYNC_PAINT
                        pthread_cond_wait(&cond, &mutex);
+                       pthread_mutex_unlock(&mutex);
 #endif
                }
        }
@@ -165,62 +163,74 @@ gPainter::~gPainter()
 
 void gPainter::setBackgroundColor(const gColor &color)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::setBackgroundColor;
        o.dc = m_dc.grabRef();
        o.parm.setColor = new gOpcode::para::psetColor;
        o.parm.setColor->color = color;
-       
+
        m_rc->submit(o);
 }
 
 void gPainter::setForegroundColor(const gColor &color)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::setForegroundColor;
        o.dc = m_dc.grabRef();
        o.parm.setColor = new gOpcode::para::psetColor;
        o.parm.setColor->color = color;
-       
+
        m_rc->submit(o);
 }
 
 void gPainter::setBackgroundColor(const gRGB &color)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::setBackgroundColorRGB;
        o.dc = m_dc.grabRef();
        o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
        o.parm.setColorRGB->color = color;
-       
+
        m_rc->submit(o);
 }
 
 void gPainter::setForegroundColor(const gRGB &color)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::setForegroundColorRGB;
        o.dc = m_dc.grabRef();
        o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
        o.parm.setColorRGB->color = color;
-       
+
        m_rc->submit(o);
 }
 
 void gPainter::setFont(gFont *font)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::setFont;
        o.dc = m_dc.grabRef();
        font->AddRef();
        o.parm.setFont = new gOpcode::para::psetFont;
        o.parm.setFont->font = font;
-       
+
        m_rc->submit(o);
 }
 
 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::renderText;
        o.dc = m_dc.grabRef();
@@ -233,6 +243,8 @@ void gPainter::renderText(const eRect &pos, const std::string &string, int flags
 
 void gPainter::renderPara(eTextPara *para, ePoint offset)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::renderPara;
        o.dc = m_dc.grabRef();
@@ -246,6 +258,8 @@ void gPainter::renderPara(eTextPara *para, ePoint offset)
 
 void gPainter::fill(const eRect &area)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::fill;
 
@@ -257,6 +271,8 @@ void gPainter::fill(const eRect &area)
 
 void gPainter::fill(const gRegion &region)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::fillRegion;
 
@@ -268,6 +284,8 @@ void gPainter::fill(const gRegion &region)
 
 void gPainter::clear()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::clear;
        o.dc = m_dc.grabRef();
@@ -278,10 +296,12 @@ void gPainter::clear()
 
 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
-       
+
        ASSERT(pixmap);
-       
+
        o.opcode=gOpcode::blit;
        o.dc = m_dc.grabRef();
        pixmap->AddRef();
@@ -296,14 +316,16 @@ void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
 
 void gPainter::setPalette(gRGB *colors, int start, int len)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::setPalette;
        o.dc = m_dc.grabRef();
        gPalette *p=new gPalette;
-       
+
        o.parm.setPalette = new gOpcode::para::psetPalette;
        p->data=new gRGB[len];
-       
+
        memcpy(p->data, colors, len*sizeof(gRGB));
        p->start=start;
        p->colors=len;
@@ -319,6 +341,8 @@ void gPainter::setPalette(gPixmap *source)
 
 void gPainter::mergePalette(gPixmap *target)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::mergePalette;
        o.dc = m_dc.grabRef();
@@ -330,6 +354,8 @@ void gPainter::mergePalette(gPixmap *target)
 
 void gPainter::line(ePoint start, ePoint end)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::line;
        o.dc = m_dc.grabRef();
@@ -341,6 +367,8 @@ void gPainter::line(ePoint start, ePoint end)
 
 void gPainter::setOffset(ePoint val)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::setOffset;
        o.dc = m_dc.grabRef();
@@ -352,6 +380,8 @@ void gPainter::setOffset(ePoint val)
 
 void gPainter::moveOffset(ePoint rel)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::setOffset;
        o.dc = m_dc.grabRef();
@@ -363,6 +393,8 @@ void gPainter::moveOffset(ePoint rel)
 
 void gPainter::resetOffset()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::setOffset;
        o.dc = m_dc.grabRef();
@@ -374,6 +406,8 @@ void gPainter::resetOffset()
 
 void gPainter::resetClip(const gRegion &region)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::setClip;
        o.dc = m_dc.grabRef();
@@ -384,6 +418,8 @@ void gPainter::resetClip(const gRegion &region)
 
 void gPainter::clip(const gRegion &region)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::addClip;
        o.dc = m_dc.grabRef();
@@ -394,6 +430,8 @@ void gPainter::clip(const gRegion &region)
 
 void gPainter::clippop()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::popClip;
        o.dc = m_dc.grabRef();
@@ -402,6 +440,8 @@ void gPainter::clippop()
 
 void gPainter::flush()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::flush;
        o.dc = m_dc.grabRef();
@@ -410,6 +450,8 @@ void gPainter::flush()
 
 void gPainter::waitVSync()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::waitVSync;
        o.dc = m_dc.grabRef();
@@ -418,6 +460,8 @@ void gPainter::waitVSync()
 
 void gPainter::flip()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::flip;
        o.dc = m_dc.grabRef();
@@ -426,6 +470,8 @@ void gPainter::flip()
 
 void gPainter::notify()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::notify;
        o.dc = m_dc.grabRef();
@@ -434,6 +480,8 @@ void gPainter::notify()
 
 void gPainter::end()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::flush;
        o.dc = m_dc.grabRef();
@@ -497,7 +545,7 @@ void gDC::exec(gOpcode *o)
                if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
                {
                        eRect bbox = para->getBoundBox();
-                       int vcentered_top = (o->parm.renderText->area.height() - bbox.height()) / 2;
+                       int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
                        int correction = vcentered_top - bbox.top();
                        offset += ePoint(0, correction);
                }