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. */
142 struct timespec timeout;
143 clock_gettime(CLOCK_REALTIME, &timeout);
145 if (m_spinner_enabled)
147 timeout.tv_nsec += 100*1000*1000;
148 /* yes, this is required. */
149 if (timeout.tv_nsec > 1000*1000*1000)
151 timeout.tv_nsec -= 1000*1000*1000;
160 if (pthread_cond_timedwait(&cond, &mutex, &timeout) == ETIMEDOUT)
162 if (eApp && !eApp->isIdle())
168 if (!m_spinner_enabled)
169 eDebug("main thread is non-idle! display spinner!");
174 pthread_mutex_unlock(&mutex);
184 void gRC::recv_notify(const int &i)
189 gRC *gRC::getInstance()
194 void gRC::enableSpinner()
198 eDebug("no spinner DC!");
203 o.opcode = m_spinner_enabled ? gOpcode::incrementSpinner : gOpcode::enableSpinner;
204 m_spinner_dc->exec(&o);
205 m_spinner_enabled = 1;
206 o.opcode = gOpcode::flush;
207 m_spinner_dc->exec(&o);
210 void gRC::disableSpinner()
212 if (!m_spinner_enabled)
217 eDebug("no spinner DC!");
221 m_spinner_enabled = 0;
224 o.opcode = gOpcode::disableSpinner;
225 m_spinner_dc->exec(&o);
226 o.opcode = gOpcode::flush;
227 m_spinner_dc->exec(&o);
230 static int gPainter_instances;
232 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
234 // ASSERT(!gPainter_instances);
235 gPainter_instances++;
239 gPainter::~gPainter()
242 gPainter_instances--;
245 void gPainter::setBackgroundColor(const gColor &color)
247 if ( m_dc->islocked() )
250 o.opcode = gOpcode::setBackgroundColor;
251 o.dc = m_dc.grabRef();
252 o.parm.setColor = new gOpcode::para::psetColor;
253 o.parm.setColor->color = color;
258 void gPainter::setForegroundColor(const gColor &color)
260 if ( m_dc->islocked() )
263 o.opcode = gOpcode::setForegroundColor;
264 o.dc = m_dc.grabRef();
265 o.parm.setColor = new gOpcode::para::psetColor;
266 o.parm.setColor->color = color;
271 void gPainter::setBackgroundColor(const gRGB &color)
273 if ( m_dc->islocked() )
276 o.opcode = gOpcode::setBackgroundColorRGB;
277 o.dc = m_dc.grabRef();
278 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
279 o.parm.setColorRGB->color = color;
284 void gPainter::setForegroundColor(const gRGB &color)
286 if ( m_dc->islocked() )
289 o.opcode = gOpcode::setForegroundColorRGB;
290 o.dc = m_dc.grabRef();
291 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
292 o.parm.setColorRGB->color = color;
297 void gPainter::setFont(gFont *font)
299 if ( m_dc->islocked() )
302 o.opcode = gOpcode::setFont;
303 o.dc = m_dc.grabRef();
305 o.parm.setFont = new gOpcode::para::psetFont;
306 o.parm.setFont->font = font;
311 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
313 if ( m_dc->islocked() )
316 o.opcode=gOpcode::renderText;
317 o.dc = m_dc.grabRef();
318 o.parm.renderText = new gOpcode::para::prenderText;
319 o.parm.renderText->area = pos;
320 o.parm.renderText->text = string.empty()?0:strdup(string.c_str());
321 o.parm.renderText->flags = flags;
325 void gPainter::renderPara(eTextPara *para, ePoint offset)
327 if ( m_dc->islocked() )
330 o.opcode=gOpcode::renderPara;
331 o.dc = m_dc.grabRef();
332 o.parm.renderPara = new gOpcode::para::prenderPara;
333 o.parm.renderPara->offset = offset;
336 o.parm.renderPara->textpara = para;
340 void gPainter::fill(const eRect &area)
342 if ( m_dc->islocked() )
345 o.opcode=gOpcode::fill;
347 o.dc = m_dc.grabRef();
348 o.parm.fill = new gOpcode::para::pfillRect;
349 o.parm.fill->area = area;
353 void gPainter::fill(const gRegion ®ion)
355 if ( m_dc->islocked() )
358 o.opcode=gOpcode::fillRegion;
360 o.dc = m_dc.grabRef();
361 o.parm.fillRegion = new gOpcode::para::pfillRegion;
362 o.parm.fillRegion->region = region;
366 void gPainter::clear()
368 if ( m_dc->islocked() )
371 o.opcode=gOpcode::clear;
372 o.dc = m_dc.grabRef();
373 o.parm.fill = new gOpcode::para::pfillRect;
374 o.parm.fill->area = eRect();
378 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
380 if ( m_dc->islocked() )
386 o.opcode=gOpcode::blit;
387 o.dc = m_dc.grabRef();
389 o.parm.blit = new gOpcode::para::pblit;
390 o.parm.blit->pixmap = pixmap;
391 o.parm.blit->position = pos;
392 o.parm.blit->clip = clip;
393 o.parm.blit->flags=flags;
398 void gPainter::setPalette(gRGB *colors, int start, int len)
400 if ( m_dc->islocked() )
403 o.opcode=gOpcode::setPalette;
404 o.dc = m_dc.grabRef();
405 gPalette *p=new gPalette;
407 o.parm.setPalette = new gOpcode::para::psetPalette;
408 p->data=new gRGB[len];
410 memcpy(p->data, colors, len*sizeof(gRGB));
413 o.parm.setPalette->palette = p;
417 void gPainter::setPalette(gPixmap *source)
420 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
423 void gPainter::mergePalette(gPixmap *target)
425 if ( m_dc->islocked() )
428 o.opcode = gOpcode::mergePalette;
429 o.dc = m_dc.grabRef();
431 o.parm.mergePalette = new gOpcode::para::pmergePalette;
432 o.parm.mergePalette->target = target;
436 void gPainter::line(ePoint start, ePoint end)
438 if ( m_dc->islocked() )
441 o.opcode=gOpcode::line;
442 o.dc = m_dc.grabRef();
443 o.parm.line = new gOpcode::para::pline;
444 o.parm.line->start = start;
445 o.parm.line->end = end;
449 void gPainter::setOffset(ePoint val)
451 if ( m_dc->islocked() )
454 o.opcode=gOpcode::setOffset;
455 o.dc = m_dc.grabRef();
456 o.parm.setOffset = new gOpcode::para::psetOffset;
457 o.parm.setOffset->rel = 0;
458 o.parm.setOffset->value = val;
462 void gPainter::moveOffset(ePoint rel)
464 if ( m_dc->islocked() )
467 o.opcode=gOpcode::setOffset;
468 o.dc = m_dc.grabRef();
469 o.parm.setOffset = new gOpcode::para::psetOffset;
470 o.parm.setOffset->rel = 1;
471 o.parm.setOffset->value = rel;
475 void gPainter::resetOffset()
477 if ( m_dc->islocked() )
480 o.opcode=gOpcode::setOffset;
481 o.dc = m_dc.grabRef();
482 o.parm.setOffset = new gOpcode::para::psetOffset;
483 o.parm.setOffset->rel = 0;
484 o.parm.setOffset->value = ePoint(0, 0);
488 void gPainter::resetClip(const gRegion ®ion)
490 if ( m_dc->islocked() )
493 o.opcode = gOpcode::setClip;
494 o.dc = m_dc.grabRef();
495 o.parm.clip = new gOpcode::para::psetClip;
496 o.parm.clip->region = region;
500 void gPainter::clip(const gRegion ®ion)
502 if ( m_dc->islocked() )
505 o.opcode = gOpcode::addClip;
506 o.dc = m_dc.grabRef();
507 o.parm.clip = new gOpcode::para::psetClip;
508 o.parm.clip->region = region;
512 void gPainter::clippop()
514 if ( m_dc->islocked() )
517 o.opcode = gOpcode::popClip;
518 o.dc = m_dc.grabRef();
522 void gPainter::waitVSync()
524 if ( m_dc->islocked() )
527 o.opcode = gOpcode::waitVSync;
528 o.dc = m_dc.grabRef();
532 void gPainter::flip()
534 if ( m_dc->islocked() )
537 o.opcode = gOpcode::flip;
538 o.dc = m_dc.grabRef();
542 void gPainter::notify()
544 if ( m_dc->islocked() )
547 o.opcode = gOpcode::notify;
548 o.dc = m_dc.grabRef();
554 if ( m_dc->islocked() )
557 o.opcode = gOpcode::flush;
558 o.dc = m_dc.grabRef();
567 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
574 delete[] m_spinner_pic;
577 void gDC::exec(gOpcode *o)
581 case gOpcode::setBackgroundColor:
582 m_background_color = o->parm.setColor->color;
583 m_background_color_rgb = getRGB(m_background_color);
584 delete o->parm.setColor;
586 case gOpcode::setForegroundColor:
587 m_foreground_color = o->parm.setColor->color;
588 m_background_color_rgb = getRGB(m_foreground_color);
589 delete o->parm.setColor;
591 case gOpcode::setBackgroundColorRGB:
592 if (m_pixmap->needClut())
593 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
594 m_background_color_rgb = o->parm.setColorRGB->color;
595 delete o->parm.setColorRGB;
597 case gOpcode::setForegroundColorRGB:
598 if (m_pixmap->needClut())
599 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
600 m_foreground_color_rgb = o->parm.setColorRGB->color;
601 delete o->parm.setColorRGB;
603 case gOpcode::setFont:
604 m_current_font = o->parm.setFont->font;
605 o->parm.setFont->font->Release();
606 delete o->parm.setFont;
608 case gOpcode::renderText:
610 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
611 int flags = o->parm.renderText->flags;
612 ASSERT(m_current_font);
613 para->setFont(m_current_font);
614 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
615 if (o->parm.renderText->text)
616 free(o->parm.renderText->text);
617 if (flags & gPainter::RT_HALIGN_RIGHT)
618 para->realign(eTextPara::dirRight);
619 else if (flags & gPainter::RT_HALIGN_CENTER)
620 para->realign(eTextPara::dirCenter);
621 else if (flags & gPainter::RT_HALIGN_BLOCK)
622 para->realign(eTextPara::dirBlock);
624 ePoint offset = m_current_offset;
626 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
628 eRect bbox = para->getBoundBox();
629 int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
630 int correction = vcentered_top - bbox.top();
631 offset += ePoint(0, correction);
634 para->blit(*this, offset, m_background_color_rgb, m_foreground_color_rgb);
635 delete o->parm.renderText;
638 case gOpcode::renderPara:
640 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, m_background_color_rgb, m_foreground_color_rgb);
641 o->parm.renderPara->textpara->Release();
642 delete o->parm.renderPara;
647 eRect area = o->parm.fill->area;
648 area.moveBy(m_current_offset);
649 gRegion clip = m_current_clip & area;
650 if (m_pixmap->needClut())
651 m_pixmap->fill(clip, m_foreground_color);
653 m_pixmap->fill(clip, m_foreground_color_rgb);
657 case gOpcode::fillRegion:
659 o->parm.fillRegion->region.moveBy(m_current_offset);
660 gRegion clip = m_current_clip & o->parm.fillRegion->region;
661 if (m_pixmap->needClut())
662 m_pixmap->fill(clip, m_foreground_color);
664 m_pixmap->fill(clip, m_foreground_color_rgb);
665 delete o->parm.fillRegion;
669 if (m_pixmap->needClut())
670 m_pixmap->fill(m_current_clip, m_background_color);
672 m_pixmap->fill(m_current_clip, m_background_color_rgb);
678 // this code should be checked again but i'm too tired now
680 o->parm.blit->position += m_current_offset;
682 if (o->parm.blit->clip.valid())
684 o->parm.blit->clip.moveBy(m_current_offset);
685 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
687 clip = m_current_clip;
689 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
690 o->parm.blit->pixmap->Release();
694 case gOpcode::setPalette:
695 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
696 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
697 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
698 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
699 if (o->parm.setPalette->palette->colors)
700 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
702 delete[] o->parm.setPalette->palette->data;
703 delete o->parm.setPalette->palette;
704 delete o->parm.setPalette;
706 case gOpcode::mergePalette:
707 m_pixmap->mergePalette(*o->parm.mergePalette->target);
708 o->parm.mergePalette->target->Release();
709 delete o->parm.mergePalette;
713 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
714 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
718 case gOpcode::addClip:
719 m_clip_stack.push(m_current_clip);
720 o->parm.clip->region.moveBy(m_current_offset);
721 m_current_clip &= o->parm.clip->region;
724 case gOpcode::setClip:
725 o->parm.clip->region.moveBy(m_current_offset);
726 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
729 case gOpcode::popClip:
730 if (!m_clip_stack.empty())
732 m_current_clip = m_clip_stack.top();
736 case gOpcode::setOffset:
737 if (o->parm.setOffset->rel)
738 m_current_offset += o->parm.setOffset->value;
740 m_current_offset = o->parm.setOffset->value;
741 delete o->parm.setOffset;
743 case gOpcode::waitVSync:
749 case gOpcode::enableSpinner:
752 case gOpcode::disableSpinner:
755 case gOpcode::incrementSpinner:
759 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
763 gRGB gDC::getRGB(gColor col)
765 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
766 return gRGB(col, col, col);
769 eFatal("bla transp");
770 return gRGB(0, 0, 0, 0xFF);
772 return m_pixmap->surface->clut.data[col];
775 void gDC::enableSpinner()
777 ASSERT(m_spinner_saved);
779 /* save the background to restore it later. We need to negative position because we want to blit from the middle of the screen. */
780 m_spinner_saved->blit(*m_pixmap, -m_spinner_pos.topLeft(), gRegion(eRect(ePoint(0, 0), m_spinner_saved->size())), 0);
785 void gDC::disableSpinner()
787 ASSERT(m_spinner_saved);
789 /* restore background */
790 m_pixmap->blit(*m_spinner_saved, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
793 void gDC::incrementSpinner()
795 ASSERT(m_spinner_saved);
803 for (i = 0; i < 5; ++i)
805 int x = i * 20 + m_spinner_pos.left();
806 int y = m_spinner_pos.top();
808 int col = ((blub - i) * 30) % 256;
810 m_pixmap->fill(eRect(x, y, 10, 10), gRGB(col, col, col));
814 m_spinner_temp->blit(*m_spinner_saved, ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()));
816 if (m_spinner_pic[m_spinner_i])
817 m_spinner_temp->blit(*m_spinner_pic[m_spinner_i], ePoint(0, 0), eRect(ePoint(0, 0), m_spinner_pos.size()), gPixmap::blitAlphaTest);
819 m_pixmap->blit(*m_spinner_temp, m_spinner_pos.topLeft(), gRegion(m_spinner_pos), 0);
821 m_spinner_i %= m_spinner_num;
824 void gDC::setSpinner(eRect pos, ePtr<gPixmap> *pic, int len)
827 ASSERT(m_pixmap->surface);
828 m_spinner_saved = new gPixmap(pos.size(), m_pixmap->surface->bpp);
829 m_spinner_temp = new gPixmap(pos.size(), m_pixmap->surface->bpp);
837 delete[] m_spinner_pic;
839 m_spinner_pic = new ePtr<gPixmap>[len];
841 for (i = 0; i < len; ++i)
842 m_spinner_pic[i] = pic[i];
847 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");