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)
125 // o.dc is a gDC* filled with grabref... so we must release it here
134 m_notify_pump.send(1);
140 /* when the main thread is non-idle for a too long time without any display output,
141 we want to display a spinner. */
144 struct timespec timeout;
145 gettimeofday(&time, NULL);
146 timeout.tv_sec = time.tv_sec;
147 timeout.tv_nsec = time.tv_usec * 1000;
149 if (m_spinner_enabled)
150 timeout.tv_nsec += 100*1000*1000;
154 /* yes, this is required. */
155 if (timeout.tv_nsec > 1000*1000*1000)
157 timeout.tv_nsec -= 1000*1000*1000;
163 if (pthread_cond_timedwait(&cond, &mutex, &timeout) == ETIMEDOUT)
165 if (eApp && !eApp->isIdle())
169 pthread_mutex_unlock(&mutex);
173 if (!m_spinner_enabled)
174 eDebug("main thread is non-idle! display spinner!");
188 void gRC::recv_notify(const int &i)
193 gRC *gRC::getInstance()
198 void gRC::enableSpinner()
202 eDebug("no spinner DC!");
207 o.opcode = m_spinner_enabled ? gOpcode::incrementSpinner : gOpcode::enableSpinner;
208 m_spinner_dc->exec(&o);
209 m_spinner_enabled = 1;
210 o.opcode = gOpcode::flush;
211 m_spinner_dc->exec(&o);
214 void gRC::disableSpinner()
216 if (!m_spinner_enabled)
221 eDebug("no spinner DC!");
225 m_spinner_enabled = 0;
228 o.opcode = gOpcode::disableSpinner;
229 m_spinner_dc->exec(&o);
230 o.opcode = gOpcode::flush;
231 m_spinner_dc->exec(&o);
234 static int gPainter_instances;
236 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
238 // ASSERT(!gPainter_instances);
239 gPainter_instances++;
243 gPainter::~gPainter()
246 gPainter_instances--;
249 void gPainter::setBackgroundColor(const gColor &color)
251 if ( m_dc->islocked() )
254 o.opcode = gOpcode::setBackgroundColor;
255 o.dc = m_dc.grabRef();
256 o.parm.setColor = new gOpcode::para::psetColor;
257 o.parm.setColor->color = color;
262 void gPainter::setForegroundColor(const gColor &color)
264 if ( m_dc->islocked() )
267 o.opcode = gOpcode::setForegroundColor;
268 o.dc = m_dc.grabRef();
269 o.parm.setColor = new gOpcode::para::psetColor;
270 o.parm.setColor->color = color;
275 void gPainter::setBackgroundColor(const gRGB &color)
277 if ( m_dc->islocked() )
280 o.opcode = gOpcode::setBackgroundColorRGB;
281 o.dc = m_dc.grabRef();
282 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
283 o.parm.setColorRGB->color = color;
288 void gPainter::setForegroundColor(const gRGB &color)
290 if ( m_dc->islocked() )
293 o.opcode = gOpcode::setForegroundColorRGB;
294 o.dc = m_dc.grabRef();
295 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
296 o.parm.setColorRGB->color = color;
301 void gPainter::setFont(gFont *font)
303 if ( m_dc->islocked() )
306 o.opcode = gOpcode::setFont;
307 o.dc = m_dc.grabRef();
309 o.parm.setFont = new gOpcode::para::psetFont;
310 o.parm.setFont->font = font;
315 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
317 if ( m_dc->islocked() )
320 o.opcode=gOpcode::renderText;
321 o.dc = m_dc.grabRef();
322 o.parm.renderText = new gOpcode::para::prenderText;
323 o.parm.renderText->area = pos;
324 o.parm.renderText->text = string.empty()?0:strdup(string.c_str());
325 o.parm.renderText->flags = flags;
329 void gPainter::renderPara(eTextPara *para, ePoint offset)
331 if ( m_dc->islocked() )
334 o.opcode=gOpcode::renderPara;
335 o.dc = m_dc.grabRef();
336 o.parm.renderPara = new gOpcode::para::prenderPara;
337 o.parm.renderPara->offset = offset;
340 o.parm.renderPara->textpara = para;
344 void gPainter::fill(const eRect &area)
346 if ( m_dc->islocked() )
349 o.opcode=gOpcode::fill;
351 o.dc = m_dc.grabRef();
352 o.parm.fill = new gOpcode::para::pfillRect;
353 o.parm.fill->area = area;
357 void gPainter::fill(const gRegion ®ion)
359 if ( m_dc->islocked() )
362 o.opcode=gOpcode::fillRegion;
364 o.dc = m_dc.grabRef();
365 o.parm.fillRegion = new gOpcode::para::pfillRegion;
366 o.parm.fillRegion->region = region;
370 void gPainter::clear()
372 if ( m_dc->islocked() )
375 o.opcode=gOpcode::clear;
376 o.dc = m_dc.grabRef();
377 o.parm.fill = new gOpcode::para::pfillRect;
378 o.parm.fill->area = eRect();
382 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
384 if ( m_dc->islocked() )
390 o.opcode=gOpcode::blit;
391 o.dc = m_dc.grabRef();
393 o.parm.blit = new gOpcode::para::pblit;
394 o.parm.blit->pixmap = pixmap;
395 o.parm.blit->position = pos;
396 o.parm.blit->clip = clip;
397 o.parm.blit->flags=flags;
402 void gPainter::setPalette(gRGB *colors, int start, int len)
404 if ( m_dc->islocked() )
407 o.opcode=gOpcode::setPalette;
408 o.dc = m_dc.grabRef();
409 gPalette *p=new gPalette;
411 o.parm.setPalette = new gOpcode::para::psetPalette;
412 p->data=new gRGB[len];
414 memcpy(p->data, colors, len*sizeof(gRGB));
417 o.parm.setPalette->palette = p;
421 void gPainter::setPalette(gPixmap *source)
424 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
427 void gPainter::mergePalette(gPixmap *target)
429 if ( m_dc->islocked() )
432 o.opcode = gOpcode::mergePalette;
433 o.dc = m_dc.grabRef();
435 o.parm.mergePalette = new gOpcode::para::pmergePalette;
436 o.parm.mergePalette->target = target;
440 void gPainter::line(ePoint start, ePoint end)
442 if ( m_dc->islocked() )
445 o.opcode=gOpcode::line;
446 o.dc = m_dc.grabRef();
447 o.parm.line = new gOpcode::para::pline;
448 o.parm.line->start = start;
449 o.parm.line->end = end;
453 void gPainter::setOffset(ePoint val)
455 if ( m_dc->islocked() )
458 o.opcode=gOpcode::setOffset;
459 o.dc = m_dc.grabRef();
460 o.parm.setOffset = new gOpcode::para::psetOffset;
461 o.parm.setOffset->rel = 0;
462 o.parm.setOffset->value = val;
466 void gPainter::moveOffset(ePoint rel)
468 if ( m_dc->islocked() )
471 o.opcode=gOpcode::setOffset;
472 o.dc = m_dc.grabRef();
473 o.parm.setOffset = new gOpcode::para::psetOffset;
474 o.parm.setOffset->rel = 1;
475 o.parm.setOffset->value = rel;
479 void gPainter::resetOffset()
481 if ( m_dc->islocked() )
484 o.opcode=gOpcode::setOffset;
485 o.dc = m_dc.grabRef();
486 o.parm.setOffset = new gOpcode::para::psetOffset;
487 o.parm.setOffset->rel = 0;
488 o.parm.setOffset->value = ePoint(0, 0);
492 void gPainter::resetClip(const gRegion ®ion)
494 if ( m_dc->islocked() )
497 o.opcode = gOpcode::setClip;
498 o.dc = m_dc.grabRef();
499 o.parm.clip = new gOpcode::para::psetClip;
500 o.parm.clip->region = region;
504 void gPainter::clip(const gRegion ®ion)
506 if ( m_dc->islocked() )
509 o.opcode = gOpcode::addClip;
510 o.dc = m_dc.grabRef();
511 o.parm.clip = new gOpcode::para::psetClip;
512 o.parm.clip->region = region;
516 void gPainter::clippop()
518 if ( m_dc->islocked() )
521 o.opcode = gOpcode::popClip;
522 o.dc = m_dc.grabRef();
526 void gPainter::waitVSync()
528 if ( m_dc->islocked() )
531 o.opcode = gOpcode::waitVSync;
532 o.dc = m_dc.grabRef();
536 void gPainter::flip()
538 if ( m_dc->islocked() )
541 o.opcode = gOpcode::flip;
542 o.dc = m_dc.grabRef();
546 void gPainter::notify()
548 if ( m_dc->islocked() )
551 o.opcode = gOpcode::notify;
552 o.dc = m_dc.grabRef();
558 if ( m_dc->islocked() )
561 o.opcode = gOpcode::flush;
562 o.dc = m_dc.grabRef();
571 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
578 delete[] m_spinner_pic;
581 void gDC::exec(gOpcode *o)
585 case gOpcode::setBackgroundColor:
586 m_background_color = o->parm.setColor->color;
587 m_background_color_rgb = getRGB(m_background_color);
588 delete o->parm.setColor;
590 case gOpcode::setForegroundColor:
591 m_foreground_color = o->parm.setColor->color;
592 m_background_color_rgb = getRGB(m_foreground_color);
593 delete o->parm.setColor;
595 case gOpcode::setBackgroundColorRGB:
596 if (m_pixmap->needClut())
597 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
598 m_background_color_rgb = o->parm.setColorRGB->color;
599 delete o->parm.setColorRGB;
601 case gOpcode::setForegroundColorRGB:
602 if (m_pixmap->needClut())
603 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
604 m_foreground_color_rgb = o->parm.setColorRGB->color;
605 delete o->parm.setColorRGB;
607 case gOpcode::setFont:
608 m_current_font = o->parm.setFont->font;
609 o->parm.setFont->font->Release();
610 delete o->parm.setFont;
612 case gOpcode::renderText:
614 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
615 int flags = o->parm.renderText->flags;
616 assert(m_current_font);
617 para->setFont(m_current_font);
618 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
619 if (o->parm.renderText->text)
620 free(o->parm.renderText->text);
621 if (flags & gPainter::RT_HALIGN_RIGHT)
622 para->realign(eTextPara::dirRight);
623 else if (flags & gPainter::RT_HALIGN_CENTER)
624 para->realign(eTextPara::dirCenter);
625 else if (flags & gPainter::RT_HALIGN_BLOCK)
626 para->realign(eTextPara::dirBlock);
628 ePoint offset = m_current_offset;
630 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
632 eRect bbox = para->getBoundBox();
633 int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
634 int correction = vcentered_top - bbox.top();
635 offset += ePoint(0, correction);
638 para->blit(*this, offset, m_background_color_rgb, m_foreground_color_rgb);
639 delete o->parm.renderText;
642 case gOpcode::renderPara:
644 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, m_background_color_rgb, m_foreground_color_rgb);
645 o->parm.renderPara->textpara->Release();
646 delete o->parm.renderPara;
651 eRect area = o->parm.fill->area;
652 area.moveBy(m_current_offset);
653 gRegion clip = m_current_clip & area;
654 if (m_pixmap->needClut())
655 m_pixmap->fill(clip, m_foreground_color);
657 m_pixmap->fill(clip, m_foreground_color_rgb);
661 case gOpcode::fillRegion:
663 o->parm.fillRegion->region.moveBy(m_current_offset);
664 gRegion clip = m_current_clip & o->parm.fillRegion->region;
665 if (m_pixmap->needClut())
666 m_pixmap->fill(clip, m_foreground_color);
668 m_pixmap->fill(clip, m_foreground_color_rgb);
669 delete o->parm.fillRegion;
673 if (m_pixmap->needClut())
674 m_pixmap->fill(m_current_clip, m_background_color);
676 m_pixmap->fill(m_current_clip, m_background_color_rgb);
682 // this code should be checked again but i'm too tired now
684 o->parm.blit->position += m_current_offset;
686 if (o->parm.blit->clip.valid())
688 o->parm.blit->clip.moveBy(m_current_offset);
689 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
691 clip = m_current_clip;
693 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
694 o->parm.blit->pixmap->Release();
698 case gOpcode::setPalette:
699 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
700 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
701 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
702 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
703 if (o->parm.setPalette->palette->colors)
704 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
706 delete[] o->parm.setPalette->palette->data;
707 delete o->parm.setPalette->palette;
708 delete o->parm.setPalette;
710 case gOpcode::mergePalette:
711 m_pixmap->mergePalette(*o->parm.mergePalette->target);
712 o->parm.mergePalette->target->Release();
713 delete o->parm.mergePalette;
717 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
718 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
722 case gOpcode::addClip:
723 m_clip_stack.push(m_current_clip);
724 o->parm.clip->region.moveBy(m_current_offset);
725 m_current_clip &= o->parm.clip->region;
728 case gOpcode::setClip:
729 o->parm.clip->region.moveBy(m_current_offset);
730 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
733 case gOpcode::popClip:
734 if (!m_clip_stack.empty())
736 m_current_clip = m_clip_stack.top();
740 case gOpcode::setOffset:
741 if (o->parm.setOffset->rel)
742 m_current_offset += o->parm.setOffset->value;
744 m_current_offset = o->parm.setOffset->value;
745 delete o->parm.setOffset;
747 case gOpcode::waitVSync:
753 case gOpcode::enableSpinner:
756 case gOpcode::disableSpinner:
759 case gOpcode::incrementSpinner:
763 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
767 gRGB gDC::getRGB(gColor col)
769 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
770 return gRGB(col, col, col);
773 eFatal("bla transp");
774 return gRGB(0, 0, 0, 0xFF);
776 return m_pixmap->surface->clut.data[col];
779 void gDC::enableSpinner()
781 ASSERT(m_spinner_saved);
783 /* save the background to restore it later. We need to negative position because we want to blit from the middle of the screen. */
784 m_spinner_saved->blit(*m_pixmap, -m_spinner_pos.topLeft(), gRegion(eRect(ePoint(0, 0), m_spinner_saved->size())), 0);
789 void gDC::disableSpinner()
791 ASSERT(m_spinner_saved);
793 /* restore background */
794 m_pixmap->blit(*m_spinner_saved, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
797 void gDC::incrementSpinner()
799 ASSERT(m_spinner_saved);
807 for (i = 0; i < 5; ++i)
809 int x = i * 20 + m_spinner_pos.left();
810 int y = m_spinner_pos.top();
812 int col = ((blub - i) * 30) % 256;
814 m_pixmap->fill(eRect(x, y, 10, 10), gRGB(col, col, col));
818 m_spinner_temp->blit(*m_spinner_saved, ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()));
820 if (m_spinner_pic[m_spinner_i])
821 m_spinner_temp->blit(*m_spinner_pic[m_spinner_i], ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()), gPixmap::blitAlphaTest);
823 m_pixmap->blit(*m_spinner_temp, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
825 m_spinner_i %= m_spinner_num;
828 void gDC::setSpinner(eRect pos, ePtr<gPixmap> *pic, int len)
831 ASSERT(m_pixmap->surface);
832 m_spinner_saved = new gPixmap(pos.size(), m_pixmap->surface->bpp);
833 m_spinner_temp = new gPixmap(pos.size(), m_pixmap->surface->bpp);
841 delete[] m_spinner_pic;
843 m_spinner_pic = new ePtr<gPixmap>[len];
845 for (i = 0; i < len; ++i)
846 m_spinner_pic[i] = pic[i];
851 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");