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);
109 pthread_mutex_lock(&mutex);
112 gOpcode o(queue[rp]);
116 pthread_mutex_unlock(&mutex);
117 if (o.opcode==gOpcode::shutdown)
119 else if (o.opcode==gOpcode::notify)
129 m_notify_pump.send(1);
132 pthread_cond_wait(&cond, &mutex);
134 pthread_mutex_unlock(&mutex);
143 void gRC::recv_notify(const int &i)
148 gRC *gRC::getInstance()
153 static int gPainter_instances;
155 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
157 // ASSERT(!gPainter_instances);
158 gPainter_instances++;
162 gPainter::~gPainter()
165 gPainter_instances--;
168 void gPainter::setBackgroundColor(const gColor &color)
170 if ( m_dc->islocked() )
173 o.opcode = gOpcode::setBackgroundColor;
174 o.dc = m_dc.grabRef();
175 o.parm.setColor = new gOpcode::para::psetColor;
176 o.parm.setColor->color = color;
181 void gPainter::setForegroundColor(const gColor &color)
183 if ( m_dc->islocked() )
186 o.opcode = gOpcode::setForegroundColor;
187 o.dc = m_dc.grabRef();
188 o.parm.setColor = new gOpcode::para::psetColor;
189 o.parm.setColor->color = color;
194 void gPainter::setBackgroundColor(const gRGB &color)
196 if ( m_dc->islocked() )
199 o.opcode = gOpcode::setBackgroundColorRGB;
200 o.dc = m_dc.grabRef();
201 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
202 o.parm.setColorRGB->color = color;
207 void gPainter::setForegroundColor(const gRGB &color)
209 if ( m_dc->islocked() )
212 o.opcode = gOpcode::setForegroundColorRGB;
213 o.dc = m_dc.grabRef();
214 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
215 o.parm.setColorRGB->color = color;
220 void gPainter::setFont(gFont *font)
222 if ( m_dc->islocked() )
225 o.opcode = gOpcode::setFont;
226 o.dc = m_dc.grabRef();
228 o.parm.setFont = new gOpcode::para::psetFont;
229 o.parm.setFont->font = font;
234 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
236 if ( m_dc->islocked() )
239 o.opcode=gOpcode::renderText;
240 o.dc = m_dc.grabRef();
241 o.parm.renderText = new gOpcode::para::prenderText;
242 o.parm.renderText->area = pos;
243 o.parm.renderText->text = string;
244 o.parm.renderText->flags = flags;
248 void gPainter::renderPara(eTextPara *para, ePoint offset)
250 if ( m_dc->islocked() )
253 o.opcode=gOpcode::renderPara;
254 o.dc = m_dc.grabRef();
255 o.parm.renderPara = new gOpcode::para::prenderPara;
256 o.parm.renderPara->offset = offset;
259 o.parm.renderPara->textpara = para;
263 void gPainter::fill(const eRect &area)
265 if ( m_dc->islocked() )
268 o.opcode=gOpcode::fill;
270 o.dc = m_dc.grabRef();
271 o.parm.fill = new gOpcode::para::pfillRect;
272 o.parm.fill->area = area;
276 void gPainter::fill(const gRegion ®ion)
278 if ( m_dc->islocked() )
281 o.opcode=gOpcode::fillRegion;
283 o.dc = m_dc.grabRef();
284 o.parm.fillRegion = new gOpcode::para::pfillRegion;
285 o.parm.fillRegion->region = region;
289 void gPainter::clear()
291 if ( m_dc->islocked() )
294 o.opcode=gOpcode::clear;
295 o.dc = m_dc.grabRef();
296 o.parm.fill = new gOpcode::para::pfillRect;
297 o.parm.fill->area = eRect();
301 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
303 if ( m_dc->islocked() )
309 o.opcode=gOpcode::blit;
310 o.dc = m_dc.grabRef();
312 o.parm.blit = new gOpcode::para::pblit;
313 o.parm.blit->pixmap = pixmap;
314 o.parm.blit->position = pos;
315 o.parm.blit->clip = clip;
316 o.parm.blit->flags=flags;
321 void gPainter::setPalette(gRGB *colors, int start, int len)
323 if ( m_dc->islocked() )
326 o.opcode=gOpcode::setPalette;
327 o.dc = m_dc.grabRef();
328 gPalette *p=new gPalette;
330 o.parm.setPalette = new gOpcode::para::psetPalette;
331 p->data=new gRGB[len];
333 memcpy(p->data, colors, len*sizeof(gRGB));
336 o.parm.setPalette->palette = p;
340 void gPainter::setPalette(gPixmap *source)
343 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
346 void gPainter::mergePalette(gPixmap *target)
348 if ( m_dc->islocked() )
351 o.opcode = gOpcode::mergePalette;
352 o.dc = m_dc.grabRef();
354 o.parm.mergePalette = new gOpcode::para::pmergePalette;
355 o.parm.mergePalette->target = target;
359 void gPainter::line(ePoint start, ePoint end)
361 if ( m_dc->islocked() )
364 o.opcode=gOpcode::line;
365 o.dc = m_dc.grabRef();
366 o.parm.line = new gOpcode::para::pline;
367 o.parm.line->start = start;
368 o.parm.line->end = end;
372 void gPainter::setOffset(ePoint val)
374 if ( m_dc->islocked() )
377 o.opcode=gOpcode::setOffset;
378 o.dc = m_dc.grabRef();
379 o.parm.setOffset = new gOpcode::para::psetOffset;
380 o.parm.setOffset->rel = 0;
381 o.parm.setOffset->value = val;
385 void gPainter::moveOffset(ePoint rel)
387 if ( m_dc->islocked() )
390 o.opcode=gOpcode::setOffset;
391 o.dc = m_dc.grabRef();
392 o.parm.setOffset = new gOpcode::para::psetOffset;
393 o.parm.setOffset->rel = 1;
394 o.parm.setOffset->value = rel;
398 void gPainter::resetOffset()
400 if ( m_dc->islocked() )
403 o.opcode=gOpcode::setOffset;
404 o.dc = m_dc.grabRef();
405 o.parm.setOffset = new gOpcode::para::psetOffset;
406 o.parm.setOffset->rel = 0;
407 o.parm.setOffset->value = ePoint(0, 0);
411 void gPainter::resetClip(const gRegion ®ion)
413 if ( m_dc->islocked() )
416 o.opcode = gOpcode::setClip;
417 o.dc = m_dc.grabRef();
418 o.parm.clip = new gOpcode::para::psetClip;
419 o.parm.clip->region = region;
423 void gPainter::clip(const gRegion ®ion)
425 if ( m_dc->islocked() )
428 o.opcode = gOpcode::addClip;
429 o.dc = m_dc.grabRef();
430 o.parm.clip = new gOpcode::para::psetClip;
431 o.parm.clip->region = region;
435 void gPainter::clippop()
437 if ( m_dc->islocked() )
440 o.opcode = gOpcode::popClip;
441 o.dc = m_dc.grabRef();
445 void gPainter::flush()
447 if ( m_dc->islocked() )
450 o.opcode = gOpcode::flush;
451 o.dc = m_dc.grabRef();
455 void gPainter::waitVSync()
457 if ( m_dc->islocked() )
460 o.opcode = gOpcode::waitVSync;
461 o.dc = m_dc.grabRef();
465 void gPainter::flip()
467 if ( m_dc->islocked() )
470 o.opcode = gOpcode::flip;
471 o.dc = m_dc.grabRef();
475 void gPainter::notify()
477 if ( m_dc->islocked() )
480 o.opcode = gOpcode::notify;
481 o.dc = m_dc.grabRef();
487 if ( m_dc->islocked() )
490 o.opcode = gOpcode::flush;
491 o.dc = m_dc.grabRef();
499 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
507 void gDC::exec(gOpcode *o)
511 case gOpcode::setBackgroundColor:
512 m_background_color = o->parm.setColor->color;
513 delete o->parm.setColor;
515 case gOpcode::setForegroundColor:
516 m_foreground_color = o->parm.setColor->color;
517 delete o->parm.setColor;
519 case gOpcode::setBackgroundColorRGB:
520 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
521 delete o->parm.setColorRGB;
523 case gOpcode::setForegroundColorRGB:
524 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
525 delete o->parm.setColorRGB;
527 case gOpcode::setFont:
528 m_current_font = o->parm.setFont->font;
529 o->parm.setFont->font->Release();
530 delete o->parm.setFont;
532 case gOpcode::renderText:
534 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
535 int flags = o->parm.renderText->flags;
536 assert(m_current_font);
537 para->setFont(m_current_font);
538 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
540 if (flags & gPainter::RT_HALIGN_RIGHT)
541 para->realign(eTextPara::dirRight);
542 else if (flags & gPainter::RT_HALIGN_CENTER)
543 para->realign(eTextPara::dirCenter);
544 else if (flags & gPainter::RT_HALIGN_BLOCK)
545 para->realign(eTextPara::dirBlock);
547 ePoint offset = m_current_offset;
549 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
551 eRect bbox = para->getBoundBox();
552 int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
553 int correction = vcentered_top - bbox.top();
554 offset += ePoint(0, correction);
556 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
557 delete o->parm.renderText;
560 case gOpcode::renderPara:
562 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
563 o->parm.renderPara->textpara->Release();
564 delete o->parm.renderPara;
569 eRect area = o->parm.fill->area;
570 area.moveBy(m_current_offset);
571 gRegion clip = m_current_clip & area;
572 m_pixmap->fill(clip, m_foreground_color);
576 case gOpcode::fillRegion:
578 o->parm.fillRegion->region.moveBy(m_current_offset);
579 gRegion clip = m_current_clip & o->parm.fillRegion->region;
580 m_pixmap->fill(clip, m_foreground_color);
581 delete o->parm.fillRegion;
585 m_pixmap->fill(m_current_clip, m_background_color);
591 // this code should be checked again but i'm too tired now
593 o->parm.blit->position += m_current_offset;
595 if (o->parm.blit->clip.valid())
597 o->parm.blit->clip.moveBy(m_current_offset);
598 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
600 clip = m_current_clip;
602 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
603 o->parm.blit->pixmap->Release();
607 case gOpcode::setPalette:
608 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
609 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
610 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
611 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
612 if (o->parm.setPalette->palette->colors)
613 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
615 delete[] o->parm.setPalette->palette->data;
616 delete o->parm.setPalette->palette;
617 delete o->parm.setPalette;
619 case gOpcode::mergePalette:
620 m_pixmap->mergePalette(*o->parm.mergePalette->target);
621 o->parm.mergePalette->target->Release();
622 delete o->parm.mergePalette;
626 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
627 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
631 case gOpcode::addClip:
632 m_clip_stack.push(m_current_clip);
633 o->parm.clip->region.moveBy(m_current_offset);
634 m_current_clip &= o->parm.clip->region;
637 case gOpcode::setClip:
638 o->parm.clip->region.moveBy(m_current_offset);
639 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
642 case gOpcode::popClip:
643 if (!m_clip_stack.empty())
645 m_current_clip = m_clip_stack.top();
649 case gOpcode::setOffset:
650 if (o->parm.setOffset->rel)
651 m_current_offset += o->parm.setOffset->value;
653 m_current_offset = o->parm.setOffset->value;
654 delete o->parm.setOffset;
656 case gOpcode::waitVSync:
663 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
667 gRGB gDC::getRGB(gColor col)
669 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
670 return gRGB(col, col, col);
673 eFatal("bla transp");
674 return gRGB(0, 0, 0, 0xFF);
676 return m_pixmap->surface->clut.data[col];
681 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");