fix spinner handling
[enigma2.git] / lib / gdi / grc.cpp
index 99fe085916a5ca298332a586e81cc7763834368e..c8af2457e1d67a7911ff1cb6430de257e4b0dbae 100644 (file)
@@ -1,40 +1,38 @@
-// 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>
 #include <lib/base/init_num.h>
 
-#define MAXSIZE 1024
-
 #ifndef SYNC_PAINT
 void *gRC::thread_wrapper(void *ptr)
 {
-       nice(3);
        return ((gRC*)ptr)->thread();
 }
 #endif
 
 gRC *gRC::instance=0;
 
-gRC::gRC(): queue(2048), m_notify_pump(eApp, 0), queuelock(MAXSIZE)
+gRC::gRC(): rp(0), wp(0)
+#ifdef SYNC_PAINT
+,m_notify_pump(eApp, 0)
+#else
+,m_notify_pump(eApp, 1)
+#endif
 {
        ASSERT(!instance);
        instance=this;
-       queuelock.lock(MAXSIZE);
        CONNECT(m_notify_pump.recv_msg, gRC::recv_notify);
 #ifndef SYNC_PAINT
+       pthread_mutex_init(&mutex, 0);
+       pthread_cond_init(&cond, 0);
        int res = pthread_create(&the_thread, 0, thread_wrapper, this);
        if (res)
                eFatal("RC thread couldn't be created");
        else
                eDebug("RC thread created successfully");
 #endif
+       m_spinner_enabled = 0;
 }
 
 DEFINE_REF(gRC);
@@ -53,25 +51,129 @@ gRC::~gRC()
 #endif
 }
 
+void gRC::submit(const gOpcode &o)
+{
+       while(1)
+       {
+#ifndef SYNC_PAINT
+               pthread_mutex_lock(&mutex);
+#endif
+               int tmp=wp+1;
+               if ( tmp == MAXSIZE )
+                       tmp=0;
+               if ( tmp == rp )
+               {
+#ifndef SYNC_PAINT
+                       pthread_cond_signal(&cond);  // wakeup gdi thread
+                       pthread_mutex_unlock(&mutex);
+#else
+                       thread();
+#endif
+                       //printf("render buffer full...\n");
+                       //fflush(stdout);
+                       usleep(1000);  // wait 1 msec
+                       continue;
+               }
+               int free=rp-wp;
+               if ( free <= 0 )
+                       free+=MAXSIZE;
+               queue[wp++]=o;
+               if ( wp == MAXSIZE )
+                       wp = 0;
+               if (o.opcode==gOpcode::flush||o.opcode==gOpcode::shutdown||o.opcode==gOpcode::notify)
+#ifndef SYNC_PAINT
+                       pthread_cond_signal(&cond);  // wakeup gdi thread
+               pthread_mutex_unlock(&mutex);
+#else
+                       thread(); // paint
+#endif
+               break;
+       }
+}
+
 void *gRC::thread()
 {
+       int need_notify = 0;
 #ifndef SYNC_PAINT
        while (1)
+       {
 #else
-       while (queue.size())
-#endif
+       while (rp != wp)
        {
-               queuelock.lock(1);
-               gOpcode& o(queue.current());
-               if (o.opcode==gOpcode::shutdown)
-                       break;
-               if (o.opcode==gOpcode::notify)
+#endif
+#ifndef SYNC_PAINT
+               pthread_mutex_lock(&mutex);
+#endif
+               if ( rp != wp )
                {
-                       m_notify_pump.send(1);
-               } else
-                       o.dc->exec(&o);
-               o.dc->Release();
-               queue.dequeue();
+                               /* make sure the spinner is not displayed when we something is painted */
+                       disableSpinner();
+
+                       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 if(o.dc)
+                       {
+                               o.dc->exec(&o);
+                               // o.dc is a gDC* filled with grabref... so we must release it here
+                               o.dc->Release();
+                       }
+               }
+               else
+               {
+                       if (need_notify)
+                       {
+                               need_notify = 0;
+                               m_notify_pump.send(1);
+                       }
+#ifndef SYNC_PAINT
+                       while(rp == wp)
+                       {
+                       
+                                       /* when the main thread is non-idle for a too long time without any display output,
+                                          we want to display a spinner. */
+                               struct timespec timeout;
+                               clock_gettime(CLOCK_REALTIME, &timeout);
+
+                               if (m_spinner_enabled)
+                               {
+                                       timeout.tv_nsec += 100*1000*1000;
+                                       /* yes, this is required. */
+                                       if (timeout.tv_nsec > 1000*1000*1000)
+                                       {
+                                               timeout.tv_nsec -= 1000*1000*1000;
+                                               timeout.tv_sec++;
+                                       }
+                               }
+                               else
+                                       timeout.tv_sec += 2;
+
+                               int idle = 1;
+
+                               if (pthread_cond_timedwait(&cond, &mutex, &timeout) == ETIMEDOUT)
+                               {
+                                       if (eApp && !eApp->isIdle())
+                                               idle = 0;
+                               }
+
+                               if (!idle)
+                               {
+                                       if (!m_spinner_enabled)
+                                               eDebug("main thread is non-idle! display spinner!");
+                                       enableSpinner();
+                               } else
+                                       disableSpinner();
+                       }
+                       pthread_mutex_unlock(&mutex);
+#endif
+               }
        }
 #ifndef SYNC_PAINT
        pthread_exit(0);
@@ -89,6 +191,42 @@ gRC *gRC::getInstance()
        return instance;
 }
 
+void gRC::enableSpinner()
+{
+       if (!m_spinner_dc)
+       {
+               eDebug("no spinner DC!");
+               return;
+       }
+
+       gOpcode o;
+       o.opcode = m_spinner_enabled ? gOpcode::incrementSpinner : gOpcode::enableSpinner;
+       m_spinner_dc->exec(&o);
+       m_spinner_enabled = 1;
+       o.opcode = gOpcode::flush;
+       m_spinner_dc->exec(&o);
+}
+
+void gRC::disableSpinner()
+{
+       if (!m_spinner_enabled)
+               return;
+
+       if (!m_spinner_dc)
+       {
+               eDebug("no spinner DC!");
+               return;
+       }
+
+       m_spinner_enabled = 0;
+       
+       gOpcode o;
+       o.opcode = gOpcode::disableSpinner;
+       m_spinner_dc->exec(&o);
+       o.opcode = gOpcode::flush;
+       m_spinner_dc->exec(&o);
+}
+
 static int gPainter_instances;
 
 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
@@ -106,74 +244,88 @@ 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();
        o.parm.renderText = new gOpcode::para::prenderText;
        o.parm.renderText->area = pos;
-       o.parm.renderText->text = string;
+       o.parm.renderText->text = string.empty()?0:strdup(string.c_str());
        o.parm.renderText->flags = flags;
        m_rc->submit(o);
 }
 
 void gPainter::renderPara(eTextPara *para, ePoint offset)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::renderPara;
        o.dc = m_dc.grabRef();
@@ -187,6 +339,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;
 
@@ -198,6 +352,8 @@ void gPainter::fill(const eRect &area)
 
 void gPainter::fill(const gRegion &region)
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode=gOpcode::fillRegion;
 
@@ -209,6 +365,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();
@@ -219,8 +377,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();
@@ -235,14 +397,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;
@@ -258,6 +422,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();
@@ -269,6 +435,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();
@@ -280,6 +448,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();
@@ -291,6 +461,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();
@@ -302,6 +474,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();
@@ -313,6 +487,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();
@@ -323,6 +499,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();
@@ -333,22 +511,18 @@ 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();
        m_rc->submit(o);
 }
 
-void gPainter::flush()
-{
-       gOpcode o;
-       o.opcode = gOpcode::flush;
-       o.dc = m_dc.grabRef();
-       m_rc->submit(o);
-}
-
 void gPainter::waitVSync()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::waitVSync;
        o.dc = m_dc.grabRef();
@@ -357,6 +531,8 @@ void gPainter::waitVSync()
 
 void gPainter::flip()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::flip;
        o.dc = m_dc.grabRef();
@@ -365,6 +541,8 @@ void gPainter::flip()
 
 void gPainter::notify()
 {
+       if ( m_dc->islocked() )
+               return;
        gOpcode o;
        o.opcode = gOpcode::notify;
        o.dc = m_dc.grabRef();
@@ -373,18 +551,27 @@ void gPainter::notify()
 
 void gPainter::end()
 {
+       if ( m_dc->islocked() )
+               return;
+       gOpcode o;
+       o.opcode = gOpcode::flush;
+       o.dc = m_dc.grabRef();
+       m_rc->submit(o);
 }
 
 gDC::gDC()
 {
+       m_spinner_pic = 0;
 }
 
 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
 {
+       m_spinner_pic = 0;
 }
 
 gDC::~gDC()
 {
+       delete[] m_spinner_pic;
 }
 
 void gDC::exec(gOpcode *o)
@@ -393,18 +580,24 @@ void gDC::exec(gOpcode *o)
        {
        case gOpcode::setBackgroundColor:
                m_background_color = o->parm.setColor->color;
+               m_background_color_rgb = getRGB(m_background_color);
                delete o->parm.setColor;
                break;
        case gOpcode::setForegroundColor:
                m_foreground_color = o->parm.setColor->color;
+               m_background_color_rgb = getRGB(m_foreground_color);
                delete o->parm.setColor;
                break;
        case gOpcode::setBackgroundColorRGB:
-               m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
+               if (m_pixmap->needClut())
+                       m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
+               m_background_color_rgb = o->parm.setColorRGB->color;
                delete o->parm.setColorRGB;
                break;
        case gOpcode::setForegroundColorRGB:
-               m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
+               if (m_pixmap->needClut())
+                       m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
+               m_foreground_color_rgb = o->parm.setColorRGB->color;
                delete o->parm.setColorRGB;
                break;
        case gOpcode::setFont:
@@ -419,7 +612,8 @@ void gDC::exec(gOpcode *o)
                assert(m_current_font);
                para->setFont(m_current_font);
                para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
-               
+               if (o->parm.renderText->text)
+                       free(o->parm.renderText->text);
                if (flags & gPainter::RT_HALIGN_RIGHT)
                        para->realign(eTextPara::dirRight);
                else if (flags & gPainter::RT_HALIGN_CENTER)
@@ -432,15 +626,18 @@ void gDC::exec(gOpcode *o)
                if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
                {
                        eRect bbox = para->getBoundBox();
-                       offset += ePoint(0, (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);
                }
-               para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
+               
+               para->blit(*this, offset, m_background_color_rgb, m_foreground_color_rgb);
                delete o->parm.renderText;
                break;
        }
        case gOpcode::renderPara:
        {
-               o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
+               o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, m_background_color_rgb, m_foreground_color_rgb);
                o->parm.renderPara->textpara->Release();
                delete o->parm.renderPara;
                break;
@@ -450,7 +647,10 @@ void gDC::exec(gOpcode *o)
                eRect area = o->parm.fill->area;
                area.moveBy(m_current_offset);
                gRegion clip = m_current_clip & area;
-               m_pixmap->fill(clip, m_foreground_color);
+               if (m_pixmap->needClut())
+                       m_pixmap->fill(clip, m_foreground_color);
+               else
+                       m_pixmap->fill(clip, m_foreground_color_rgb);
                delete o->parm.fill;
                break;
        }
@@ -458,12 +658,18 @@ void gDC::exec(gOpcode *o)
        {
                o->parm.fillRegion->region.moveBy(m_current_offset);
                gRegion clip = m_current_clip & o->parm.fillRegion->region;
-               m_pixmap->fill(clip, m_foreground_color);
+               if (m_pixmap->needClut())
+                       m_pixmap->fill(clip, m_foreground_color);
+               else
+                       m_pixmap->fill(clip, m_foreground_color_rgb);
                delete o->parm.fillRegion;
                break;
        }
        case gOpcode::clear:
-               m_pixmap->fill(m_current_clip, m_background_color);
+               if (m_pixmap->needClut())
+                       m_pixmap->fill(m_current_clip, m_background_color);
+               else
+                       m_pixmap->fill(m_current_clip, m_background_color_rgb);
                delete o->parm.fill;
                break;
        case gOpcode::blit:
@@ -540,6 +746,15 @@ void gDC::exec(gOpcode *o)
                break;
        case gOpcode::flush:
                break;
+       case gOpcode::enableSpinner:
+               enableSpinner();
+               break;
+       case gOpcode::disableSpinner:
+               disableSpinner();
+               break;
+       case gOpcode::incrementSpinner:
+               incrementSpinner();
+               break;
        default:
                eFatal("illegal opcode %d. expect memory leak!", o->opcode);
        }
@@ -557,6 +772,76 @@ gRGB gDC::getRGB(gColor col)
        return m_pixmap->surface->clut.data[col];
 }
 
+void gDC::enableSpinner()
+{
+       ASSERT(m_spinner_saved);
+       
+               /* save the background to restore it later. We need to negative position because we want to blit from the middle of the screen. */
+       m_spinner_saved->blit(*m_pixmap, -m_spinner_pos.topLeft(), gRegion(eRect(ePoint(0, 0), m_spinner_saved->size())), 0);
+       
+       incrementSpinner();
+}
+
+void gDC::disableSpinner()
+{
+       ASSERT(m_spinner_saved);
+
+               /* restore background */
+       m_pixmap->blit(*m_spinner_saved, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
+}
+
+void gDC::incrementSpinner()
+{
+       ASSERT(m_spinner_saved);
+       
+       static int blub;
+       blub++;
+
+#if 0
+       int i;
+       
+       for (i = 0; i < 5; ++i)
+       {
+               int x = i * 20 + m_spinner_pos.left();
+               int y = m_spinner_pos.top();
+               
+               int col = ((blub - i) * 30) % 256;
+
+               m_pixmap->fill(eRect(x, y, 10, 10), gRGB(col, col, col));
+       }
+#endif
+
+       m_spinner_temp->blit(*m_spinner_saved, ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()));
+
+       if (m_spinner_pic[m_spinner_i])
+               m_spinner_temp->blit(*m_spinner_pic[m_spinner_i], ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()), gPixmap::blitAlphaTest);
+
+       m_pixmap->blit(*m_spinner_temp, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
+       m_spinner_i++;
+       m_spinner_i %= m_spinner_num;
+}
+
+void gDC::setSpinner(eRect pos, ePtr<gPixmap> *pic, int len)
+{
+       ASSERT(m_pixmap);
+       ASSERT(m_pixmap->surface);
+       m_spinner_saved = new gPixmap(pos.size(), m_pixmap->surface->bpp);
+       m_spinner_temp = new gPixmap(pos.size(), m_pixmap->surface->bpp);
+       m_spinner_pos = pos;
+       
+       m_spinner_i = 0;
+       m_spinner_num = len;
+       
+       int i;
+       if (m_spinner_pic)
+               delete[] m_spinner_pic;
+       
+       m_spinner_pic = new ePtr<gPixmap>[len];
+       
+       for (i = 0; i < len; ++i)
+               m_spinner_pic[i] = pic[i];
+}
+
 DEFINE_REF(gDC);
 
 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");