[RecordTimer] fix repeat timer issue.
[vuplus_dvbapp] / lib / gdi / grc.cpp
1 #include <unistd.h>
2 #include <lib/gdi/grc.h>
3 #include <lib/gdi/font.h>
4 #include <lib/base/init.h>
5 #include <lib/base/init_num.h>
6 #ifdef USE_LIBVUGLES2
7 #include <vuplus_gles.h>
8 #endif
9
10 #ifndef SYNC_PAINT
11 void *gRC::thread_wrapper(void *ptr)
12 {
13         return ((gRC*)ptr)->thread();
14 }
15 #endif
16
17 gRC *gRC::instance=0;
18
19 gRC::gRC(): rp(0), wp(0)
20 #ifdef SYNC_PAINT
21 ,m_notify_pump(eApp, 0)
22 #else
23 ,m_notify_pump(eApp, 1)
24 #endif
25 {
26         ASSERT(!instance);
27         instance=this;
28         CONNECT(m_notify_pump.recv_msg, gRC::recv_notify);
29 #ifndef SYNC_PAINT
30         pthread_mutex_init(&mutex, 0);
31         pthread_cond_init(&cond, 0);
32         int res = pthread_create(&the_thread, 0, thread_wrapper, this);
33         if (res)
34                 eFatal("RC thread couldn't be created");
35         else
36                 eDebug("RC thread created successfully");
37 #endif
38         m_spinner_enabled = 0;
39 }
40
41 DEFINE_REF(gRC);
42
43 gRC::~gRC()
44 {
45         instance=0;
46
47         gOpcode o;
48         o.opcode=gOpcode::shutdown;
49         submit(o);
50 #ifndef SYNC_PAINT
51         eDebug("waiting for gRC thread shutdown");
52         pthread_join(the_thread, 0);
53         eDebug("gRC thread has finished");
54 #endif
55 }
56
57 void gRC::submit(const gOpcode &o)
58 {
59         while(1)
60         {
61 #ifndef SYNC_PAINT
62                 pthread_mutex_lock(&mutex);
63 #endif
64                 int tmp=wp+1;
65                 if ( tmp == MAXSIZE )
66                         tmp=0;
67                 if ( tmp == rp )
68                 {
69 #ifndef SYNC_PAINT
70                         pthread_cond_signal(&cond);  // wakeup gdi thread
71                         pthread_mutex_unlock(&mutex);
72 #else
73                         thread();
74 #endif
75                         //printf("render buffer full...\n");
76                         //fflush(stdout);
77                         usleep(1000);  // wait 1 msec
78                         continue;
79                 }
80                 int free=rp-wp;
81                 if ( free <= 0 )
82                         free+=MAXSIZE;
83                 queue[wp++]=o;
84                 if ( wp == MAXSIZE )
85                         wp = 0;
86                 if (o.opcode==gOpcode::flush||o.opcode==gOpcode::shutdown||o.opcode==gOpcode::notify)
87 #ifndef SYNC_PAINT
88                         pthread_cond_signal(&cond);  // wakeup gdi thread
89                 pthread_mutex_unlock(&mutex);
90 #else
91                         thread(); // paint
92 #endif
93                 break;
94         }
95 }
96
97 void *gRC::thread()
98 {
99         int need_notify = 0;
100 #ifdef USE_LIBVUGLES2
101         if (gles_open()) {
102                 gles_state_open();
103                 gles_viewport(720, 576, 720 * 4);
104         }
105 #endif
106 #ifndef SYNC_PAINT
107         while (1)
108         {
109 #else
110         while (rp != wp)
111         {
112 #endif
113 #ifndef SYNC_PAINT
114                 pthread_mutex_lock(&mutex);
115 #endif
116                 if ( rp != wp )
117                 {
118                                 /* make sure the spinner is not displayed when we something is painted */
119                         disableSpinner();
120
121                         gOpcode o(queue[rp++]);
122                         if ( rp == MAXSIZE )
123                                 rp=0;
124 #ifndef SYNC_PAINT
125                         pthread_mutex_unlock(&mutex);
126 #endif
127                         if (o.opcode==gOpcode::shutdown)
128                                 break;
129                         else if (o.opcode==gOpcode::notify)
130                                 need_notify = 1;
131                         else if (o.opcode==gOpcode::setCompositing)
132                         {
133                                 m_compositing = o.parm.setCompositing;
134                                 m_compositing->Release();
135                         } else if(o.dc)
136                         {
137                                 o.dc->exec(&o);
138                                 // o.dc is a gDC* filled with grabref... so we must release it here
139                                 o.dc->Release();
140                         }
141                 }
142                 else
143                 {
144                         if (need_notify)
145                         {
146                                 need_notify = 0;
147                                 m_notify_pump.send(1);
148                         }
149 #ifndef SYNC_PAINT
150                         while(rp == wp)
151                         {
152                         
153                                         /* when the main thread is non-idle for a too long time without any display output,
154                                            we want to display a spinner. */
155                                 struct timespec timeout;
156                                 clock_gettime(CLOCK_REALTIME, &timeout);
157
158                                 if (m_spinner_enabled)
159                                 {
160                                         timeout.tv_nsec += 100*1000*1000;
161                                         /* yes, this is required. */
162                                         if (timeout.tv_nsec > 1000*1000*1000)
163                                         {
164                                                 timeout.tv_nsec -= 1000*1000*1000;
165                                                 timeout.tv_sec++;
166                                         }
167                                 }
168                                 else
169                                         timeout.tv_sec += 2;
170
171                                 int idle = 1;
172
173                                 if (pthread_cond_timedwait(&cond, &mutex, &timeout) == ETIMEDOUT)
174                                 {
175                                         if (eApp && !eApp->isIdle())
176                                         {
177                                                 int idle_count = eApp->idleCount();
178                                                 if (idle_count == m_prev_idle_count)
179                                                         idle = 0;
180                                                 else
181                                                         m_prev_idle_count = idle_count;
182                                         }
183                                 }
184
185                                 if (!idle)
186                                 {
187                                         if (!m_spinner_enabled)
188                                                 eDebug("main thread is non-idle! display spinner!");
189                                         enableSpinner();
190                                 } else
191                                         disableSpinner();
192                         }
193                         pthread_mutex_unlock(&mutex);
194 #endif
195                 }
196         }
197 #ifdef USE_LIBVUGLES2
198         gles_state_close();
199         gles_close();
200 #endif
201 #ifndef SYNC_PAINT
202         pthread_exit(0);
203 #endif
204         return 0;
205 }
206
207 void gRC::recv_notify(const int &i)
208 {
209         notify();
210 }
211
212 gRC *gRC::getInstance()
213 {
214         return instance;
215 }
216
217 void gRC::enableSpinner()
218 {
219         if (!m_spinner_dc)
220         {
221                 eDebug("no spinner DC!");
222                 return;
223         }
224
225         gOpcode o;
226         o.opcode = m_spinner_enabled ? gOpcode::incrementSpinner : gOpcode::enableSpinner;
227         m_spinner_dc->exec(&o);
228         m_spinner_enabled = 1;
229         o.opcode = gOpcode::flush;
230         m_spinner_dc->exec(&o);
231 }
232
233 void gRC::disableSpinner()
234 {
235         if (!m_spinner_enabled)
236                 return;
237
238         if (!m_spinner_dc)
239         {
240                 eDebug("no spinner DC!");
241                 return;
242         }
243
244         m_spinner_enabled = 0;
245         
246         gOpcode o;
247         o.opcode = gOpcode::disableSpinner;
248         m_spinner_dc->exec(&o);
249         o.opcode = gOpcode::flush;
250         m_spinner_dc->exec(&o);
251 }
252
253 static int gPainter_instances;
254
255 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
256 {
257 //      ASSERT(!gPainter_instances);
258         gPainter_instances++;
259 //      begin(rect);
260 }
261
262 gPainter::~gPainter()
263 {
264         end();
265         gPainter_instances--;
266 }
267
268 void gPainter::setBackgroundColor(const gColor &color)
269 {
270         if ( m_dc->islocked() )
271                 return;
272         gOpcode o;
273         o.opcode = gOpcode::setBackgroundColor;
274         o.dc = m_dc.grabRef();
275         o.parm.setColor = new gOpcode::para::psetColor;
276         o.parm.setColor->color = color;
277
278         m_rc->submit(o);
279 }
280
281 void gPainter::setForegroundColor(const gColor &color)
282 {
283         if ( m_dc->islocked() )
284                 return;
285         gOpcode o;
286         o.opcode = gOpcode::setForegroundColor;
287         o.dc = m_dc.grabRef();
288         o.parm.setColor = new gOpcode::para::psetColor;
289         o.parm.setColor->color = color;
290
291         m_rc->submit(o);
292 }
293
294 void gPainter::setBackgroundColor(const gRGB &color)
295 {
296         if ( m_dc->islocked() )
297                 return;
298         gOpcode o;
299         o.opcode = gOpcode::setBackgroundColorRGB;
300         o.dc = m_dc.grabRef();
301         o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
302         o.parm.setColorRGB->color = color;
303
304         m_rc->submit(o);
305 }
306
307 void gPainter::setForegroundColor(const gRGB &color)
308 {
309         if ( m_dc->islocked() )
310                 return;
311         gOpcode o;
312         o.opcode = gOpcode::setForegroundColorRGB;
313         o.dc = m_dc.grabRef();
314         o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
315         o.parm.setColorRGB->color = color;
316
317         m_rc->submit(o);
318 }
319
320 void gPainter::setFont(gFont *font)
321 {
322         if ( m_dc->islocked() )
323                 return;
324         gOpcode o;
325         o.opcode = gOpcode::setFont;
326         o.dc = m_dc.grabRef();
327         font->AddRef();
328         o.parm.setFont = new gOpcode::para::psetFont;
329         o.parm.setFont->font = font;
330
331         m_rc->submit(o);
332 }
333
334 void gPainter::renderText(const eRect &pos, const std::string &string, int flags, gRGB bordercolor, int border)
335 {
336         if ( m_dc->islocked() )
337                 return;
338         gOpcode o;
339         o.opcode=gOpcode::renderText;
340         o.dc = m_dc.grabRef();
341         o.parm.renderText = new gOpcode::para::prenderText;
342         o.parm.renderText->area = pos;
343         o.parm.renderText->text = string.empty()?0:strdup(string.c_str());
344         o.parm.renderText->flags = flags;
345         o.parm.renderText->border = border;
346         o.parm.renderText->bordercolor = bordercolor;
347         m_rc->submit(o);
348 }
349
350 void gPainter::renderPara(eTextPara *para, ePoint offset)
351 {
352         if ( m_dc->islocked() )
353                 return;
354         ASSERT(para);
355         gOpcode o;
356         o.opcode=gOpcode::renderPara;
357         o.dc = m_dc.grabRef();
358         o.parm.renderPara = new gOpcode::para::prenderPara;
359         o.parm.renderPara->offset = offset;
360
361         para->AddRef();
362         o.parm.renderPara->textpara = para;
363         m_rc->submit(o);
364 }
365
366 void gPainter::fill(const eRect &area)
367 {
368         if ( m_dc->islocked() )
369                 return;
370         gOpcode o;
371         o.opcode=gOpcode::fill;
372
373         o.dc = m_dc.grabRef();
374         o.parm.fill = new gOpcode::para::pfillRect;
375         o.parm.fill->area = area;
376         m_rc->submit(o);
377 }
378
379 void gPainter::fill(const gRegion &region)
380 {
381         if ( m_dc->islocked() )
382                 return;
383         gOpcode o;
384         o.opcode=gOpcode::fillRegion;
385
386         o.dc = m_dc.grabRef();
387         o.parm.fillRegion = new gOpcode::para::pfillRegion;
388         o.parm.fillRegion->region = region;
389         m_rc->submit(o);
390 }
391
392 void gPainter::clear()
393 {
394         if ( m_dc->islocked() )
395                 return;
396         gOpcode o;
397         o.opcode=gOpcode::clear;
398         o.dc = m_dc.grabRef();
399         o.parm.fill = new gOpcode::para::pfillRect;
400         o.parm.fill->area = eRect();
401         m_rc->submit(o);
402 }
403
404 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
405 {
406         blitScale(pixmap, eRect(pos, eSize()), clip, flags, 0);
407 }
408
409 void gPainter::blitScale(gPixmap *pixmap, const eRect &position, const eRect &clip, int flags, int aflags)
410 {
411         flags |= aflags;
412
413         if ( m_dc->islocked() )
414                 return;
415         gOpcode o;
416
417         ASSERT(pixmap);
418
419         o.opcode=gOpcode::blit;
420         o.dc = m_dc.grabRef();
421         pixmap->AddRef();
422         o.parm.blit  = new gOpcode::para::pblit;
423         o.parm.blit->pixmap = pixmap;
424         o.parm.blit->clip = clip;
425         o.parm.blit->flags = flags;
426         o.parm.blit->position = position;
427         m_rc->submit(o);
428 }
429
430 void gPainter::setPalette(gRGB *colors, int start, int len)
431 {
432         if ( m_dc->islocked() )
433                 return;
434         ASSERT(colors);
435         gOpcode o;
436         o.opcode=gOpcode::setPalette;
437         o.dc = m_dc.grabRef();
438         gPalette *p=new gPalette;
439
440         o.parm.setPalette = new gOpcode::para::psetPalette;
441         p->data=new gRGB[len];
442
443         memcpy(p->data, colors, len*sizeof(gRGB));
444         p->start=start;
445         p->colors=len;
446         o.parm.setPalette->palette = p;
447         m_rc->submit(o);
448 }
449
450 void gPainter::setPalette(gPixmap *source)
451 {
452         ASSERT(source);
453         setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
454 }
455
456 void gPainter::mergePalette(gPixmap *target)
457 {
458         if ( m_dc->islocked() )
459                 return;
460         ASSERT(target);
461         gOpcode o;
462         o.opcode = gOpcode::mergePalette;
463         o.dc = m_dc.grabRef();
464         target->AddRef();
465         o.parm.mergePalette = new gOpcode::para::pmergePalette;
466         o.parm.mergePalette->target = target;
467         m_rc->submit(o);
468 }
469
470 void gPainter::line(ePoint start, ePoint end)
471 {
472         if ( m_dc->islocked() )
473                 return;
474         gOpcode o;
475         o.opcode=gOpcode::line;
476         o.dc = m_dc.grabRef();
477         o.parm.line = new gOpcode::para::pline;
478         o.parm.line->start = start;
479         o.parm.line->end = end;
480         m_rc->submit(o);
481 }
482
483 void gPainter::setOffset(ePoint val)
484 {
485         if ( m_dc->islocked() )
486                 return;
487         gOpcode o;
488         o.opcode=gOpcode::setOffset;
489         o.dc = m_dc.grabRef();
490         o.parm.setOffset = new gOpcode::para::psetOffset;
491         o.parm.setOffset->rel = 0;
492         o.parm.setOffset->value = val;
493         m_rc->submit(o);
494 }
495
496 void gPainter::moveOffset(ePoint rel)
497 {
498         if ( m_dc->islocked() )
499                 return;
500         gOpcode o;
501         o.opcode=gOpcode::setOffset;
502         o.dc = m_dc.grabRef();
503         o.parm.setOffset = new gOpcode::para::psetOffset;
504         o.parm.setOffset->rel = 1;
505         o.parm.setOffset->value = rel;
506         m_rc->submit(o);
507 }
508
509 void gPainter::resetOffset()
510 {
511         if ( m_dc->islocked() )
512                 return;
513         gOpcode o;
514         o.opcode=gOpcode::setOffset;
515         o.dc = m_dc.grabRef();
516         o.parm.setOffset = new gOpcode::para::psetOffset;
517         o.parm.setOffset->rel = 0;
518         o.parm.setOffset->value = ePoint(0, 0);
519         m_rc->submit(o);
520 }
521
522 void gPainter::resetClip(const gRegion &region)
523 {
524         if ( m_dc->islocked() )
525                 return;
526         gOpcode o;
527         o.opcode = gOpcode::setClip;
528         o.dc = m_dc.grabRef();
529         o.parm.clip = new gOpcode::para::psetClip;
530         o.parm.clip->region = region;
531         m_rc->submit(o);
532 }
533
534 void gPainter::clip(const gRegion &region)
535 {
536         if ( m_dc->islocked() )
537                 return;
538         gOpcode o;
539         o.opcode = gOpcode::addClip;
540         o.dc = m_dc.grabRef();
541         o.parm.clip = new gOpcode::para::psetClip;
542         o.parm.clip->region = region;
543         m_rc->submit(o);
544 }
545
546 void gPainter::clippop()
547 {
548         if ( m_dc->islocked() )
549                 return;
550         gOpcode o;
551         o.opcode = gOpcode::popClip;
552         o.dc = m_dc.grabRef();
553         m_rc->submit(o);
554 }
555
556 void gPainter::waitVSync()
557 {
558         if ( m_dc->islocked() )
559                 return;
560         gOpcode o;
561         o.opcode = gOpcode::waitVSync;
562         o.dc = m_dc.grabRef();
563         m_rc->submit(o);
564 }
565
566 void gPainter::flip()
567 {
568         if ( m_dc->islocked() )
569                 return;
570         gOpcode o;
571         o.opcode = gOpcode::flip;
572         o.dc = m_dc.grabRef();
573         m_rc->submit(o);
574 }
575
576 void gPainter::notify()
577 {
578         if ( m_dc->islocked() )
579                 return;
580         gOpcode o;
581         o.opcode = gOpcode::notify;
582         o.dc = m_dc.grabRef();
583         m_rc->submit(o);
584 }
585
586 void gPainter::setCompositing(gCompositingData *comp)
587 {
588         gOpcode o;
589         o.opcode = gOpcode::setCompositing;
590         o.dc = 0;
591         o.parm.setCompositing = comp;
592         comp->AddRef(); /* will be freed in ::thread */
593         m_rc->submit(o);
594 }
595
596 void gPainter::flush()
597 {
598         if ( m_dc->islocked() )
599                 return;
600         gOpcode o;
601         o.opcode = gOpcode::flush;
602         o.dc = m_dc.grabRef();
603         m_rc->submit(o);
604 }
605
606 void gPainter::end()
607 {
608         if ( m_dc->islocked() )
609                 return;
610 }
611
612 void gPainter::sendShow(ePoint point, eSize size)
613 {
614         if ( m_dc->islocked() )
615                 return;
616         gOpcode o;
617         o.opcode=gOpcode::sendShow;
618         o.dc = m_dc.grabRef();
619         o.parm.setShowHideInfo = new gOpcode::para::psetShowHideInfo;
620         o.parm.setShowHideInfo->point = point;
621         o.parm.setShowHideInfo->size = size;
622         m_rc->submit(o);
623 }
624
625 void gPainter::sendHide(ePoint point, eSize size)
626 {
627         if ( m_dc->islocked() )
628                 return;
629         gOpcode o;
630         o.opcode=gOpcode::sendHide;
631         o.dc = m_dc.grabRef();
632         o.parm.setShowHideInfo = new gOpcode::para::psetShowHideInfo;
633         o.parm.setShowHideInfo->point = point;
634         o.parm.setShowHideInfo->size = size;
635         m_rc->submit(o);
636 }
637
638 #ifdef USE_LIBVUGLES2
639 void gPainter::sendShowItem(long dir, ePoint point, eSize size)
640 {
641         if ( m_dc->islocked() )
642                 return;
643         gOpcode o;
644         o.opcode=gOpcode::sendShowItem;
645         o.dc = m_dc.grabRef();
646         o.parm.setShowItemInfo = new gOpcode::para::psetShowItemInfo;
647         o.parm.setShowItemInfo->dir = dir;
648         o.parm.setShowItemInfo->point = point;
649         o.parm.setShowItemInfo->size = size;
650         m_rc->submit(o);
651 }
652
653 void gPainter::setFlush(bool val)
654 {
655         if ( m_dc->islocked() )
656                 return;
657         gOpcode o;
658         o.opcode=gOpcode::setFlush;
659         o.dc = m_dc.grabRef();
660         o.parm.setFlush = new gOpcode::para::psetFlush;
661         o.parm.setFlush->enable = val;
662         m_rc->submit(o);
663 }
664
665 void gPainter::setView(eSize size)
666 {
667         if ( m_dc->islocked() )
668                 return;
669         gOpcode o;
670         o.opcode=gOpcode::setView;
671         o.dc = m_dc.grabRef();
672         o.parm.setViewInfo = new gOpcode::para::psetViewInfo;
673         o.parm.setViewInfo->size = size;
674         m_rc->submit(o);
675 }
676 #endif
677
678 gDC::gDC()
679 {
680         m_spinner_pic = 0;
681 }
682
683 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
684 {
685         m_spinner_pic = 0;
686 }
687
688 gDC::~gDC()
689 {
690         delete[] m_spinner_pic;
691 }
692
693 void gDC::exec(const gOpcode *o)
694 {
695         switch (o->opcode)
696         {
697         case gOpcode::setBackgroundColor:
698                 m_background_color = o->parm.setColor->color;
699                 m_background_color_rgb = getRGB(m_background_color);
700                 delete o->parm.setColor;
701                 break;
702         case gOpcode::setForegroundColor:
703                 m_foreground_color = o->parm.setColor->color;
704                 m_background_color_rgb = getRGB(m_foreground_color);
705                 delete o->parm.setColor;
706                 break;
707         case gOpcode::setBackgroundColorRGB:
708                 if (m_pixmap->needClut())
709                         m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
710                 m_background_color_rgb = o->parm.setColorRGB->color;
711                 delete o->parm.setColorRGB;
712                 break;
713         case gOpcode::setForegroundColorRGB:
714                 if (m_pixmap->needClut())
715                         m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
716                 m_foreground_color_rgb = o->parm.setColorRGB->color;
717                 delete o->parm.setColorRGB;
718                 break;
719         case gOpcode::setFont:
720                 m_current_font = o->parm.setFont->font;
721                 o->parm.setFont->font->Release();
722                 delete o->parm.setFont;
723                 break;
724         case gOpcode::renderText:
725         {
726                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
727                 int flags = o->parm.renderText->flags;
728                 ASSERT(m_current_font);
729                 para->setFont(m_current_font);
730                 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0, o->parm.renderText->border);
731                 if (o->parm.renderText->text)
732                         free(o->parm.renderText->text);
733                 if (flags & gPainter::RT_HALIGN_RIGHT)
734                         para->realign(eTextPara::dirRight);
735                 else if (flags & gPainter::RT_HALIGN_CENTER)
736                         para->realign(eTextPara::dirCenter);
737                 else if (flags & gPainter::RT_HALIGN_BLOCK)
738                         para->realign(eTextPara::dirBlock);
739                 
740                 ePoint offset = m_current_offset;
741                 
742                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
743                 {
744                         eRect bbox = para->getBoundBox();
745                         int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
746                         int correction = vcentered_top - bbox.top();
747                         offset += ePoint(0, correction);
748                 }
749
750                 if (o->parm.renderText->border)
751                 {
752                         para->blit(*this, offset, m_background_color_rgb, o->parm.renderText->bordercolor, true);
753                         para->blit(*this, offset, o->parm.renderText->bordercolor, m_foreground_color_rgb);
754                 }
755                 else
756                         para->blit(*this, offset, m_background_color_rgb, m_foreground_color_rgb);
757                 delete o->parm.renderText;
758                 break;
759         }
760         case gOpcode::renderPara:
761         {
762                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, m_background_color_rgb, m_foreground_color_rgb);
763                 o->parm.renderPara->textpara->Release();
764                 delete o->parm.renderPara;
765                 break;
766         }
767         case gOpcode::fill:
768         {
769                 eRect area = o->parm.fill->area;
770                 area.moveBy(m_current_offset);
771                 gRegion clip = m_current_clip & area;
772                 if (m_pixmap->needClut())
773                         m_pixmap->fill(clip, m_foreground_color);
774                 else
775                         m_pixmap->fill(clip, m_foreground_color_rgb);
776                 delete o->parm.fill;
777                 break;
778         }
779         case gOpcode::fillRegion:
780         {
781                 o->parm.fillRegion->region.moveBy(m_current_offset);
782                 gRegion clip = m_current_clip & o->parm.fillRegion->region;
783                 if (m_pixmap->needClut())
784                         m_pixmap->fill(clip, m_foreground_color);
785                 else
786                         m_pixmap->fill(clip, m_foreground_color_rgb);
787                 delete o->parm.fillRegion;
788                 break;
789         }
790         case gOpcode::clear:
791                 if (m_pixmap->needClut())
792                         m_pixmap->fill(m_current_clip, m_background_color);
793                 else
794                         m_pixmap->fill(m_current_clip, m_background_color_rgb);
795                 delete o->parm.fill;
796                 break;
797         case gOpcode::blit:
798         {
799                 gRegion clip;
800                                 // this code should be checked again but i'm too tired now
801                 
802                 o->parm.blit->position.moveBy(m_current_offset);
803                 
804                 if (o->parm.blit->clip.valid())
805                 {
806                         o->parm.blit->clip.moveBy(m_current_offset);
807                         clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
808                 } else
809                         clip = m_current_clip;
810                 
811                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
812                 o->parm.blit->pixmap->Release();
813                 delete o->parm.blit;
814                 break;
815         }
816         case gOpcode::setPalette:
817                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
818                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
819                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
820                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
821                 if (o->parm.setPalette->palette->colors)
822                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
823                 
824                 delete[] o->parm.setPalette->palette->data;
825                 delete o->parm.setPalette->palette;
826                 delete o->parm.setPalette;
827                 break;
828         case gOpcode::mergePalette:
829                 m_pixmap->mergePalette(*o->parm.mergePalette->target);
830                 o->parm.mergePalette->target->Release();
831                 delete o->parm.mergePalette;
832                 break; 
833         case gOpcode::line:
834         {
835                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
836                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
837                 delete o->parm.line;
838                 break;
839         }
840         case gOpcode::addClip:
841                 m_clip_stack.push(m_current_clip);
842                 o->parm.clip->region.moveBy(m_current_offset);
843                 m_current_clip &= o->parm.clip->region;
844                 delete o->parm.clip;
845                 break;
846         case gOpcode::setClip:
847                 o->parm.clip->region.moveBy(m_current_offset);
848                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
849                 delete o->parm.clip;
850                 break;
851         case gOpcode::popClip:
852                 if (!m_clip_stack.empty())
853                 {
854                         m_current_clip = m_clip_stack.top();
855                         m_clip_stack.pop();
856                 }
857                 break;
858         case gOpcode::setOffset:
859                 if (o->parm.setOffset->rel)
860                         m_current_offset += o->parm.setOffset->value;
861                 else
862                         m_current_offset  = o->parm.setOffset->value;
863                 delete o->parm.setOffset;
864                 break;
865         case gOpcode::waitVSync:
866                 break;
867         case gOpcode::flip:
868                 break;
869         case gOpcode::flush:
870                 break;
871         case gOpcode::sendShow:
872                 break;
873         case gOpcode::sendHide:
874                 break;
875 #ifdef USE_LIBVUGLES2
876         case gOpcode::sendShowItem:
877                 break;
878         case gOpcode::setFlush:
879                 break;
880         case gOpcode::setView:
881                 break;
882 #endif
883         case gOpcode::enableSpinner:
884                 enableSpinner();
885                 break;
886         case gOpcode::disableSpinner:
887                 disableSpinner();
888                 break;
889         case gOpcode::incrementSpinner:
890                 incrementSpinner();
891                 break;
892         default:
893                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
894         }
895 }
896
897 gRGB gDC::getRGB(gColor col)
898 {
899         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
900                 return gRGB(col, col, col);
901         if (col<0)
902         {
903                 eFatal("bla transp");
904                 return gRGB(0, 0, 0, 0xFF);
905         }
906         return m_pixmap->surface->clut.data[col];
907 }
908
909 void gDC::enableSpinner()
910 {
911         ASSERT(m_spinner_saved);
912         
913                 /* save the background to restore it later. We need to negative position because we want to blit from the middle of the screen. */
914         m_spinner_saved->blit(*m_pixmap, eRect(-m_spinner_pos.topLeft(), eSize()), gRegion(eRect(ePoint(0, 0), m_spinner_saved->size())), 0);
915         
916         incrementSpinner();
917 }
918
919 void gDC::disableSpinner()
920 {
921         ASSERT(m_spinner_saved);
922
923                 /* restore background */
924         m_pixmap->blit(*m_spinner_saved, eRect(m_spinner_pos.topLeft(), eSize()), gRegion(m_spinner_pos), 0);
925 }
926
927 void gDC::incrementSpinner()
928 {
929         ASSERT(m_spinner_saved);
930         
931         static int blub;
932         blub++;
933
934 #if 0
935         int i;
936         
937         for (i = 0; i < 5; ++i)
938         {
939                 int x = i * 20 + m_spinner_pos.left();
940                 int y = m_spinner_pos.top();
941                 
942                 int col = ((blub - i) * 30) % 256;
943
944                 m_pixmap->fill(eRect(x, y, 10, 10), gRGB(col, col, col));
945         }
946 #endif
947
948         m_spinner_temp->blit(*m_spinner_saved, eRect(0, 0, 0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()));
949
950         if (m_spinner_pic[m_spinner_i])
951                 m_spinner_temp->blit(*m_spinner_pic[m_spinner_i], eRect(0, 0, 0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()), gPixmap::blitAlphaTest);
952
953         m_pixmap->blit(*m_spinner_temp, eRect(m_spinner_pos.topLeft(), eSize()), gRegion(m_spinner_pos), 0);
954         m_spinner_i++;
955         m_spinner_i %= m_spinner_num;
956 }
957
958 void gDC::setSpinner(eRect pos, ePtr<gPixmap> *pic, int len)
959 {
960         ASSERT(m_pixmap);
961         ASSERT(m_pixmap->surface);
962         m_spinner_saved = new gPixmap(pos.size(), m_pixmap->surface->bpp);
963         m_spinner_temp = new gPixmap(pos.size(), m_pixmap->surface->bpp);
964         m_spinner_pos = pos;
965         
966         m_spinner_i = 0;
967         m_spinner_num = len;
968         
969         int i;
970         if (m_spinner_pic)
971                 delete[] m_spinner_pic;
972         
973         m_spinner_pic = new ePtr<gPixmap>[len];
974         
975         for (i = 0; i < len; ++i)
976                 m_spinner_pic[i] = pic[i];
977 }
978
979 DEFINE_REF(gDC);
980
981 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");