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>
8 void *gRC::thread_wrapper(void *ptr)
10 return ((gRC*)ptr)->thread();
16 gRC::gRC(): rp(0), wp(0)
18 ,m_notify_pump(eApp, 0)
20 ,m_notify_pump(eApp, 1)
25 CONNECT(m_notify_pump.recv_msg, gRC::recv_notify);
27 pthread_mutex_init(&mutex, 0);
28 pthread_cond_init(&cond, 0);
29 int res = pthread_create(&the_thread, 0, thread_wrapper, this);
31 eFatal("RC thread couldn't be created");
33 eDebug("RC thread created successfully");
35 m_spinner_enabled = 0;
45 o.opcode=gOpcode::shutdown;
48 eDebug("waiting for gRC thread shutdown");
49 pthread_join(the_thread, 0);
50 eDebug("gRC thread has finished");
54 void gRC::submit(const gOpcode &o)
59 pthread_mutex_lock(&mutex);
67 pthread_cond_signal(&cond); // wakeup gdi thread
68 pthread_mutex_unlock(&mutex);
72 //printf("render buffer full...\n");
74 usleep(1000); // wait 1 msec
83 if (o.opcode==gOpcode::flush||o.opcode==gOpcode::shutdown||o.opcode==gOpcode::notify)
85 pthread_cond_signal(&cond); // wakeup gdi thread
86 pthread_mutex_unlock(&mutex);
105 pthread_mutex_lock(&mutex);
109 /* make sure the spinner is not displayed when we something is painted */
112 gOpcode o(queue[rp++]);
116 pthread_mutex_unlock(&mutex);
118 if (o.opcode==gOpcode::shutdown)
120 else if (o.opcode==gOpcode::notify)
122 else if (o.opcode==gOpcode::setCompositing)
124 m_compositing = o.parm.setCompositing;
125 m_compositing->Release();
129 // o.dc is a gDC* filled with grabref... so we must release it here
138 m_notify_pump.send(1);
144 /* when the main thread is non-idle for a too long time without any display output,
145 we want to display a spinner. */
146 struct timespec timeout;
147 clock_gettime(CLOCK_REALTIME, &timeout);
149 if (m_spinner_enabled)
151 timeout.tv_nsec += 100*1000*1000;
152 /* yes, this is required. */
153 if (timeout.tv_nsec > 1000*1000*1000)
155 timeout.tv_nsec -= 1000*1000*1000;
164 if (pthread_cond_timedwait(&cond, &mutex, &timeout) == ETIMEDOUT)
166 if (eApp && !eApp->isIdle())
168 int idle_count = eApp->idleCount();
169 if (idle_count == m_prev_idle_count)
172 m_prev_idle_count = idle_count;
178 if (!m_spinner_enabled)
179 eDebug("main thread is non-idle! display spinner!");
184 pthread_mutex_unlock(&mutex);
194 void gRC::recv_notify(const int &i)
199 gRC *gRC::getInstance()
204 void gRC::enableSpinner()
208 eDebug("no spinner DC!");
213 o.opcode = m_spinner_enabled ? gOpcode::incrementSpinner : gOpcode::enableSpinner;
214 m_spinner_dc->exec(&o);
215 m_spinner_enabled = 1;
216 o.opcode = gOpcode::flush;
217 m_spinner_dc->exec(&o);
220 void gRC::disableSpinner()
222 if (!m_spinner_enabled)
227 eDebug("no spinner DC!");
231 m_spinner_enabled = 0;
234 o.opcode = gOpcode::disableSpinner;
235 m_spinner_dc->exec(&o);
236 o.opcode = gOpcode::flush;
237 m_spinner_dc->exec(&o);
240 static int gPainter_instances;
242 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
244 // ASSERT(!gPainter_instances);
245 gPainter_instances++;
249 gPainter::~gPainter()
252 gPainter_instances--;
255 void gPainter::setBackgroundColor(const gColor &color)
257 if ( m_dc->islocked() )
260 o.opcode = gOpcode::setBackgroundColor;
261 o.dc = m_dc.grabRef();
262 o.parm.setColor = new gOpcode::para::psetColor;
263 o.parm.setColor->color = color;
268 void gPainter::setForegroundColor(const gColor &color)
270 if ( m_dc->islocked() )
273 o.opcode = gOpcode::setForegroundColor;
274 o.dc = m_dc.grabRef();
275 o.parm.setColor = new gOpcode::para::psetColor;
276 o.parm.setColor->color = color;
281 void gPainter::setBackgroundColor(const gRGB &color)
283 if ( m_dc->islocked() )
286 o.opcode = gOpcode::setBackgroundColorRGB;
287 o.dc = m_dc.grabRef();
288 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
289 o.parm.setColorRGB->color = color;
294 void gPainter::setForegroundColor(const gRGB &color)
296 if ( m_dc->islocked() )
299 o.opcode = gOpcode::setForegroundColorRGB;
300 o.dc = m_dc.grabRef();
301 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
302 o.parm.setColorRGB->color = color;
307 void gPainter::setFont(gFont *font)
309 if ( m_dc->islocked() )
312 o.opcode = gOpcode::setFont;
313 o.dc = m_dc.grabRef();
315 o.parm.setFont = new gOpcode::para::psetFont;
316 o.parm.setFont->font = font;
321 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
323 if ( m_dc->islocked() )
326 o.opcode=gOpcode::renderText;
327 o.dc = m_dc.grabRef();
328 o.parm.renderText = new gOpcode::para::prenderText;
329 o.parm.renderText->area = pos;
330 o.parm.renderText->text = string.empty()?0:strdup(string.c_str());
331 o.parm.renderText->flags = flags;
335 void gPainter::renderPara(eTextPara *para, ePoint offset)
337 if ( m_dc->islocked() )
341 o.opcode=gOpcode::renderPara;
342 o.dc = m_dc.grabRef();
343 o.parm.renderPara = new gOpcode::para::prenderPara;
344 o.parm.renderPara->offset = offset;
347 o.parm.renderPara->textpara = para;
351 void gPainter::fill(const eRect &area)
353 if ( m_dc->islocked() )
356 o.opcode=gOpcode::fill;
358 o.dc = m_dc.grabRef();
359 o.parm.fill = new gOpcode::para::pfillRect;
360 o.parm.fill->area = area;
364 void gPainter::fill(const gRegion ®ion)
366 if ( m_dc->islocked() )
369 o.opcode=gOpcode::fillRegion;
371 o.dc = m_dc.grabRef();
372 o.parm.fillRegion = new gOpcode::para::pfillRegion;
373 o.parm.fillRegion->region = region;
377 void gPainter::clear()
379 if ( m_dc->islocked() )
382 o.opcode=gOpcode::clear;
383 o.dc = m_dc.grabRef();
384 o.parm.fill = new gOpcode::para::pfillRect;
385 o.parm.fill->area = eRect();
389 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
391 blitScale(pixmap, eRect(pos, eSize()), clip, flags, 0);
394 void gPainter::blitScale(gPixmap *pixmap, const eRect &position, const eRect &clip, int flags, int aflags)
398 if ( m_dc->islocked() )
404 o.opcode=gOpcode::blit;
405 o.dc = m_dc.grabRef();
407 o.parm.blit = new gOpcode::para::pblit;
408 o.parm.blit->pixmap = pixmap;
409 o.parm.blit->clip = clip;
410 o.parm.blit->flags = flags;
411 o.parm.blit->position = position;
415 void gPainter::setPalette(gRGB *colors, int start, int len)
417 if ( m_dc->islocked() )
421 o.opcode=gOpcode::setPalette;
422 o.dc = m_dc.grabRef();
423 gPalette *p=new gPalette;
425 o.parm.setPalette = new gOpcode::para::psetPalette;
426 p->data=new gRGB[len];
428 memcpy(p->data, colors, len*sizeof(gRGB));
431 o.parm.setPalette->palette = p;
435 void gPainter::setPalette(gPixmap *source)
438 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
441 void gPainter::mergePalette(gPixmap *target)
443 if ( m_dc->islocked() )
447 o.opcode = gOpcode::mergePalette;
448 o.dc = m_dc.grabRef();
450 o.parm.mergePalette = new gOpcode::para::pmergePalette;
451 o.parm.mergePalette->target = target;
455 void gPainter::line(ePoint start, ePoint end)
457 if ( m_dc->islocked() )
460 o.opcode=gOpcode::line;
461 o.dc = m_dc.grabRef();
462 o.parm.line = new gOpcode::para::pline;
463 o.parm.line->start = start;
464 o.parm.line->end = end;
468 void gPainter::setOffset(ePoint val)
470 if ( m_dc->islocked() )
473 o.opcode=gOpcode::setOffset;
474 o.dc = m_dc.grabRef();
475 o.parm.setOffset = new gOpcode::para::psetOffset;
476 o.parm.setOffset->rel = 0;
477 o.parm.setOffset->value = val;
481 void gPainter::moveOffset(ePoint rel)
483 if ( m_dc->islocked() )
486 o.opcode=gOpcode::setOffset;
487 o.dc = m_dc.grabRef();
488 o.parm.setOffset = new gOpcode::para::psetOffset;
489 o.parm.setOffset->rel = 1;
490 o.parm.setOffset->value = rel;
494 void gPainter::resetOffset()
496 if ( m_dc->islocked() )
499 o.opcode=gOpcode::setOffset;
500 o.dc = m_dc.grabRef();
501 o.parm.setOffset = new gOpcode::para::psetOffset;
502 o.parm.setOffset->rel = 0;
503 o.parm.setOffset->value = ePoint(0, 0);
507 void gPainter::resetClip(const gRegion ®ion)
509 if ( m_dc->islocked() )
512 o.opcode = gOpcode::setClip;
513 o.dc = m_dc.grabRef();
514 o.parm.clip = new gOpcode::para::psetClip;
515 o.parm.clip->region = region;
519 void gPainter::clip(const gRegion ®ion)
521 if ( m_dc->islocked() )
524 o.opcode = gOpcode::addClip;
525 o.dc = m_dc.grabRef();
526 o.parm.clip = new gOpcode::para::psetClip;
527 o.parm.clip->region = region;
531 void gPainter::clippop()
533 if ( m_dc->islocked() )
536 o.opcode = gOpcode::popClip;
537 o.dc = m_dc.grabRef();
541 void gPainter::waitVSync()
543 if ( m_dc->islocked() )
546 o.opcode = gOpcode::waitVSync;
547 o.dc = m_dc.grabRef();
551 void gPainter::flip()
553 if ( m_dc->islocked() )
556 o.opcode = gOpcode::flip;
557 o.dc = m_dc.grabRef();
561 void gPainter::notify()
563 if ( m_dc->islocked() )
566 o.opcode = gOpcode::notify;
567 o.dc = m_dc.grabRef();
571 void gPainter::setCompositing(gCompositingData *comp)
574 o.opcode = gOpcode::setCompositing;
576 o.parm.setCompositing = comp;
577 comp->AddRef(); /* will be freed in ::thread */
581 void gPainter::flush()
583 if ( m_dc->islocked() )
586 o.opcode = gOpcode::flush;
587 o.dc = m_dc.grabRef();
593 if ( m_dc->islocked() )
602 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
609 delete[] m_spinner_pic;
612 void gDC::exec(const gOpcode *o)
616 case gOpcode::setBackgroundColor:
617 m_background_color = o->parm.setColor->color;
618 m_background_color_rgb = getRGB(m_background_color);
619 delete o->parm.setColor;
621 case gOpcode::setForegroundColor:
622 m_foreground_color = o->parm.setColor->color;
623 m_background_color_rgb = getRGB(m_foreground_color);
624 delete o->parm.setColor;
626 case gOpcode::setBackgroundColorRGB:
627 if (m_pixmap->needClut())
628 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
629 m_background_color_rgb = o->parm.setColorRGB->color;
630 delete o->parm.setColorRGB;
632 case gOpcode::setForegroundColorRGB:
633 if (m_pixmap->needClut())
634 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
635 m_foreground_color_rgb = o->parm.setColorRGB->color;
636 delete o->parm.setColorRGB;
638 case gOpcode::setFont:
639 m_current_font = o->parm.setFont->font;
640 o->parm.setFont->font->Release();
641 delete o->parm.setFont;
643 case gOpcode::renderText:
645 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
646 int flags = o->parm.renderText->flags;
647 ASSERT(m_current_font);
648 para->setFont(m_current_font);
649 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
650 if (o->parm.renderText->text)
651 free(o->parm.renderText->text);
652 if (flags & gPainter::RT_HALIGN_RIGHT)
653 para->realign(eTextPara::dirRight);
654 else if (flags & gPainter::RT_HALIGN_CENTER)
655 para->realign(eTextPara::dirCenter);
656 else if (flags & gPainter::RT_HALIGN_BLOCK)
657 para->realign(eTextPara::dirBlock);
659 ePoint offset = m_current_offset;
661 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
663 eRect bbox = para->getBoundBox();
664 int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
665 int correction = vcentered_top - bbox.top();
666 offset += ePoint(0, correction);
669 para->blit(*this, offset, m_background_color_rgb, m_foreground_color_rgb);
670 delete o->parm.renderText;
673 case gOpcode::renderPara:
675 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, m_background_color_rgb, m_foreground_color_rgb);
676 o->parm.renderPara->textpara->Release();
677 delete o->parm.renderPara;
682 eRect area = o->parm.fill->area;
683 area.moveBy(m_current_offset);
684 gRegion clip = m_current_clip & area;
685 if (m_pixmap->needClut())
686 m_pixmap->fill(clip, m_foreground_color);
688 m_pixmap->fill(clip, m_foreground_color_rgb);
692 case gOpcode::fillRegion:
694 o->parm.fillRegion->region.moveBy(m_current_offset);
695 gRegion clip = m_current_clip & o->parm.fillRegion->region;
696 if (m_pixmap->needClut())
697 m_pixmap->fill(clip, m_foreground_color);
699 m_pixmap->fill(clip, m_foreground_color_rgb);
700 delete o->parm.fillRegion;
704 if (m_pixmap->needClut())
705 m_pixmap->fill(m_current_clip, m_background_color);
707 m_pixmap->fill(m_current_clip, m_background_color_rgb);
713 // this code should be checked again but i'm too tired now
715 o->parm.blit->position.moveBy(m_current_offset);
717 if (o->parm.blit->clip.valid())
719 o->parm.blit->clip.moveBy(m_current_offset);
720 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
722 clip = m_current_clip;
724 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
725 o->parm.blit->pixmap->Release();
729 case gOpcode::setPalette:
730 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
731 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
732 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
733 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
734 if (o->parm.setPalette->palette->colors)
735 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
737 delete[] o->parm.setPalette->palette->data;
738 delete o->parm.setPalette->palette;
739 delete o->parm.setPalette;
741 case gOpcode::mergePalette:
742 m_pixmap->mergePalette(*o->parm.mergePalette->target);
743 o->parm.mergePalette->target->Release();
744 delete o->parm.mergePalette;
748 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
749 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
753 case gOpcode::addClip:
754 m_clip_stack.push(m_current_clip);
755 o->parm.clip->region.moveBy(m_current_offset);
756 m_current_clip &= o->parm.clip->region;
759 case gOpcode::setClip:
760 o->parm.clip->region.moveBy(m_current_offset);
761 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
764 case gOpcode::popClip:
765 if (!m_clip_stack.empty())
767 m_current_clip = m_clip_stack.top();
771 case gOpcode::setOffset:
772 if (o->parm.setOffset->rel)
773 m_current_offset += o->parm.setOffset->value;
775 m_current_offset = o->parm.setOffset->value;
776 delete o->parm.setOffset;
778 case gOpcode::waitVSync:
784 case gOpcode::enableSpinner:
787 case gOpcode::disableSpinner:
790 case gOpcode::incrementSpinner:
794 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
798 gRGB gDC::getRGB(gColor col)
800 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
801 return gRGB(col, col, col);
804 eFatal("bla transp");
805 return gRGB(0, 0, 0, 0xFF);
807 return m_pixmap->surface->clut.data[col];
810 void gDC::enableSpinner()
812 ASSERT(m_spinner_saved);
814 /* save the background to restore it later. We need to negative position because we want to blit from the middle of the screen. */
815 m_spinner_saved->blit(*m_pixmap, eRect(-m_spinner_pos.topLeft(), eSize()), gRegion(eRect(ePoint(0, 0), m_spinner_saved->size())), 0);
820 void gDC::disableSpinner()
822 ASSERT(m_spinner_saved);
824 /* restore background */
825 m_pixmap->blit(*m_spinner_saved, eRect(m_spinner_pos.topLeft(), eSize()), gRegion(m_spinner_pos), 0);
828 void gDC::incrementSpinner()
830 ASSERT(m_spinner_saved);
838 for (i = 0; i < 5; ++i)
840 int x = i * 20 + m_spinner_pos.left();
841 int y = m_spinner_pos.top();
843 int col = ((blub - i) * 30) % 256;
845 m_pixmap->fill(eRect(x, y, 10, 10), gRGB(col, col, col));
849 m_spinner_temp->blit(*m_spinner_saved, eRect(0, 0, 0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()));
851 if (m_spinner_pic[m_spinner_i])
852 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);
854 m_pixmap->blit(*m_spinner_temp, eRect(m_spinner_pos.topLeft(), eSize()), gRegion(m_spinner_pos), 0);
856 m_spinner_i %= m_spinner_num;
859 void gDC::setSpinner(eRect pos, ePtr<gPixmap> *pic, int len)
862 ASSERT(m_pixmap->surface);
863 m_spinner_saved = new gPixmap(pos.size(), m_pixmap->surface->bpp);
864 m_spinner_temp = new gPixmap(pos.size(), m_pixmap->surface->bpp);
872 delete[] m_spinner_pic;
874 m_spinner_pic = new ePtr<gPixmap>[len];
876 for (i = 0; i < len; ++i)
877 m_spinner_pic[i] = pic[i];
882 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");