8 #include <lib/gdi/grc.h>
9 #include <lib/gdi/font.h>
10 #include <lib/base/init.h>
11 #include <lib/base/init_num.h>
14 void *gRC::thread_wrapper(void *ptr)
16 return ((gRC*)ptr)->thread();
22 gRC::gRC(): rp(0), wp(0)
24 ,m_notify_pump(eApp, 0)
26 ,m_notify_pump(eApp, 1)
31 CONNECT(m_notify_pump.recv_msg, gRC::recv_notify);
33 pthread_mutex_init(&mutex, 0);
34 pthread_cond_init(&cond, 0);
35 int res = pthread_create(&the_thread, 0, thread_wrapper, this);
37 eFatal("RC thread couldn't be created");
39 eDebug("RC thread created successfully");
50 o.opcode=gOpcode::shutdown;
53 eDebug("waiting for gRC thread shutdown");
54 pthread_join(the_thread, 0);
55 eDebug("gRC thread has finished");
59 void gRC::submit(const gOpcode &o)
64 pthread_mutex_lock(&mutex);
73 pthread_mutex_unlock(&mutex);
77 //printf("render buffer full...\n");
79 usleep(1000); // wait 1 msec
88 if (o.opcode==gOpcode::flush||o.opcode==gOpcode::shutdown||o.opcode==gOpcode::notify)
90 pthread_cond_signal(&cond); // wakeup gdi thread
91 pthread_mutex_unlock(&mutex);
112 gOpcode& o(queue[rp]);
113 if (o.opcode==gOpcode::shutdown)
115 else if (o.opcode==gOpcode::notify)
128 m_notify_pump.send(1);
131 pthread_cond_wait(&cond, &mutex);
141 void gRC::recv_notify(const int &i)
146 gRC *gRC::getInstance()
151 static int gPainter_instances;
153 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
155 // ASSERT(!gPainter_instances);
156 gPainter_instances++;
160 gPainter::~gPainter()
163 gPainter_instances--;
166 void gPainter::setBackgroundColor(const gColor &color)
168 if ( m_dc->islocked() )
171 o.opcode = gOpcode::setBackgroundColor;
172 o.dc = m_dc.grabRef();
173 o.parm.setColor = new gOpcode::para::psetColor;
174 o.parm.setColor->color = color;
179 void gPainter::setForegroundColor(const gColor &color)
181 if ( m_dc->islocked() )
184 o.opcode = gOpcode::setForegroundColor;
185 o.dc = m_dc.grabRef();
186 o.parm.setColor = new gOpcode::para::psetColor;
187 o.parm.setColor->color = color;
192 void gPainter::setBackgroundColor(const gRGB &color)
194 if ( m_dc->islocked() )
197 o.opcode = gOpcode::setBackgroundColorRGB;
198 o.dc = m_dc.grabRef();
199 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
200 o.parm.setColorRGB->color = color;
205 void gPainter::setForegroundColor(const gRGB &color)
207 if ( m_dc->islocked() )
210 o.opcode = gOpcode::setForegroundColorRGB;
211 o.dc = m_dc.grabRef();
212 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
213 o.parm.setColorRGB->color = color;
218 void gPainter::setFont(gFont *font)
220 if ( m_dc->islocked() )
223 o.opcode = gOpcode::setFont;
224 o.dc = m_dc.grabRef();
226 o.parm.setFont = new gOpcode::para::psetFont;
227 o.parm.setFont->font = font;
232 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
234 if ( m_dc->islocked() )
237 o.opcode=gOpcode::renderText;
238 o.dc = m_dc.grabRef();
239 o.parm.renderText = new gOpcode::para::prenderText;
240 o.parm.renderText->area = pos;
241 o.parm.renderText->text = string;
242 o.parm.renderText->flags = flags;
246 void gPainter::renderPara(eTextPara *para, ePoint offset)
248 if ( m_dc->islocked() )
251 o.opcode=gOpcode::renderPara;
252 o.dc = m_dc.grabRef();
253 o.parm.renderPara = new gOpcode::para::prenderPara;
254 o.parm.renderPara->offset = offset;
257 o.parm.renderPara->textpara = para;
261 void gPainter::fill(const eRect &area)
263 if ( m_dc->islocked() )
266 o.opcode=gOpcode::fill;
268 o.dc = m_dc.grabRef();
269 o.parm.fill = new gOpcode::para::pfillRect;
270 o.parm.fill->area = area;
274 void gPainter::fill(const gRegion ®ion)
276 if ( m_dc->islocked() )
279 o.opcode=gOpcode::fillRegion;
281 o.dc = m_dc.grabRef();
282 o.parm.fillRegion = new gOpcode::para::pfillRegion;
283 o.parm.fillRegion->region = region;
287 void gPainter::clear()
289 if ( m_dc->islocked() )
292 o.opcode=gOpcode::clear;
293 o.dc = m_dc.grabRef();
294 o.parm.fill = new gOpcode::para::pfillRect;
295 o.parm.fill->area = eRect();
299 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
301 if ( m_dc->islocked() )
307 o.opcode=gOpcode::blit;
308 o.dc = m_dc.grabRef();
310 o.parm.blit = new gOpcode::para::pblit;
311 o.parm.blit->pixmap = pixmap;
312 o.parm.blit->position = pos;
313 o.parm.blit->clip = clip;
314 o.parm.blit->flags=flags;
319 void gPainter::setPalette(gRGB *colors, int start, int len)
321 if ( m_dc->islocked() )
324 o.opcode=gOpcode::setPalette;
325 o.dc = m_dc.grabRef();
326 gPalette *p=new gPalette;
328 o.parm.setPalette = new gOpcode::para::psetPalette;
329 p->data=new gRGB[len];
331 memcpy(p->data, colors, len*sizeof(gRGB));
334 o.parm.setPalette->palette = p;
338 void gPainter::setPalette(gPixmap *source)
341 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
344 void gPainter::mergePalette(gPixmap *target)
346 if ( m_dc->islocked() )
349 o.opcode = gOpcode::mergePalette;
350 o.dc = m_dc.grabRef();
352 o.parm.mergePalette = new gOpcode::para::pmergePalette;
353 o.parm.mergePalette->target = target;
357 void gPainter::line(ePoint start, ePoint end)
359 if ( m_dc->islocked() )
362 o.opcode=gOpcode::line;
363 o.dc = m_dc.grabRef();
364 o.parm.line = new gOpcode::para::pline;
365 o.parm.line->start = start;
366 o.parm.line->end = end;
370 void gPainter::setOffset(ePoint val)
372 if ( m_dc->islocked() )
375 o.opcode=gOpcode::setOffset;
376 o.dc = m_dc.grabRef();
377 o.parm.setOffset = new gOpcode::para::psetOffset;
378 o.parm.setOffset->rel = 0;
379 o.parm.setOffset->value = val;
383 void gPainter::moveOffset(ePoint rel)
385 if ( m_dc->islocked() )
388 o.opcode=gOpcode::setOffset;
389 o.dc = m_dc.grabRef();
390 o.parm.setOffset = new gOpcode::para::psetOffset;
391 o.parm.setOffset->rel = 1;
392 o.parm.setOffset->value = rel;
396 void gPainter::resetOffset()
398 if ( m_dc->islocked() )
401 o.opcode=gOpcode::setOffset;
402 o.dc = m_dc.grabRef();
403 o.parm.setOffset = new gOpcode::para::psetOffset;
404 o.parm.setOffset->rel = 0;
405 o.parm.setOffset->value = ePoint(0, 0);
409 void gPainter::resetClip(const gRegion ®ion)
411 if ( m_dc->islocked() )
414 o.opcode = gOpcode::setClip;
415 o.dc = m_dc.grabRef();
416 o.parm.clip = new gOpcode::para::psetClip;
417 o.parm.clip->region = region;
421 void gPainter::clip(const gRegion ®ion)
423 if ( m_dc->islocked() )
426 o.opcode = gOpcode::addClip;
427 o.dc = m_dc.grabRef();
428 o.parm.clip = new gOpcode::para::psetClip;
429 o.parm.clip->region = region;
433 void gPainter::clippop()
435 if ( m_dc->islocked() )
438 o.opcode = gOpcode::popClip;
439 o.dc = m_dc.grabRef();
443 void gPainter::flush()
445 if ( m_dc->islocked() )
448 o.opcode = gOpcode::flush;
449 o.dc = m_dc.grabRef();
453 void gPainter::waitVSync()
455 if ( m_dc->islocked() )
458 o.opcode = gOpcode::waitVSync;
459 o.dc = m_dc.grabRef();
463 void gPainter::flip()
465 if ( m_dc->islocked() )
468 o.opcode = gOpcode::flip;
469 o.dc = m_dc.grabRef();
473 void gPainter::notify()
475 if ( m_dc->islocked() )
478 o.opcode = gOpcode::notify;
479 o.dc = m_dc.grabRef();
485 if ( m_dc->islocked() )
488 o.opcode = gOpcode::flush;
489 o.dc = m_dc.grabRef();
497 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
505 void gDC::exec(gOpcode *o)
509 case gOpcode::setBackgroundColor:
510 m_background_color = o->parm.setColor->color;
511 delete o->parm.setColor;
513 case gOpcode::setForegroundColor:
514 m_foreground_color = o->parm.setColor->color;
515 delete o->parm.setColor;
517 case gOpcode::setBackgroundColorRGB:
518 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
519 delete o->parm.setColorRGB;
521 case gOpcode::setForegroundColorRGB:
522 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
523 delete o->parm.setColorRGB;
525 case gOpcode::setFont:
526 m_current_font = o->parm.setFont->font;
527 o->parm.setFont->font->Release();
528 delete o->parm.setFont;
530 case gOpcode::renderText:
532 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
533 int flags = o->parm.renderText->flags;
534 assert(m_current_font);
535 para->setFont(m_current_font);
536 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
538 if (flags & gPainter::RT_HALIGN_RIGHT)
539 para->realign(eTextPara::dirRight);
540 else if (flags & gPainter::RT_HALIGN_CENTER)
541 para->realign(eTextPara::dirCenter);
542 else if (flags & gPainter::RT_HALIGN_BLOCK)
543 para->realign(eTextPara::dirBlock);
545 ePoint offset = m_current_offset;
547 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
549 eRect bbox = para->getBoundBox();
550 int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
551 int correction = vcentered_top - bbox.top();
552 offset += ePoint(0, correction);
554 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
555 delete o->parm.renderText;
558 case gOpcode::renderPara:
560 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
561 o->parm.renderPara->textpara->Release();
562 delete o->parm.renderPara;
567 eRect area = o->parm.fill->area;
568 area.moveBy(m_current_offset);
569 gRegion clip = m_current_clip & area;
570 m_pixmap->fill(clip, m_foreground_color);
574 case gOpcode::fillRegion:
576 o->parm.fillRegion->region.moveBy(m_current_offset);
577 gRegion clip = m_current_clip & o->parm.fillRegion->region;
578 m_pixmap->fill(clip, m_foreground_color);
579 delete o->parm.fillRegion;
583 m_pixmap->fill(m_current_clip, m_background_color);
589 // this code should be checked again but i'm too tired now
591 o->parm.blit->position += m_current_offset;
593 if (o->parm.blit->clip.valid())
595 o->parm.blit->clip.moveBy(m_current_offset);
596 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
598 clip = m_current_clip;
600 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
601 o->parm.blit->pixmap->Release();
605 case gOpcode::setPalette:
606 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
607 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
608 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
609 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
610 if (o->parm.setPalette->palette->colors)
611 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
613 delete[] o->parm.setPalette->palette->data;
614 delete o->parm.setPalette->palette;
615 delete o->parm.setPalette;
617 case gOpcode::mergePalette:
618 m_pixmap->mergePalette(*o->parm.mergePalette->target);
619 o->parm.mergePalette->target->Release();
620 delete o->parm.mergePalette;
624 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
625 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
629 case gOpcode::addClip:
630 m_clip_stack.push(m_current_clip);
631 o->parm.clip->region.moveBy(m_current_offset);
632 m_current_clip &= o->parm.clip->region;
635 case gOpcode::setClip:
636 o->parm.clip->region.moveBy(m_current_offset);
637 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
640 case gOpcode::popClip:
641 if (!m_clip_stack.empty())
643 m_current_clip = m_clip_stack.top();
647 case gOpcode::setOffset:
648 if (o->parm.setOffset->rel)
649 m_current_offset += o->parm.setOffset->value;
651 m_current_offset = o->parm.setOffset->value;
652 delete o->parm.setOffset;
654 case gOpcode::waitVSync:
661 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
665 gRGB gDC::getRGB(gColor col)
667 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
668 return gRGB(col, col, col);
671 eFatal("bla transp");
672 return gRGB(0, 0, 0, 0xFF);
674 return m_pixmap->surface->clut.data[col];
679 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");