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);
142 void gRC::recv_notify(const int &i)
147 gRC *gRC::getInstance()
152 static int gPainter_instances;
154 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
156 // ASSERT(!gPainter_instances);
157 gPainter_instances++;
161 gPainter::~gPainter()
164 gPainter_instances--;
167 void gPainter::setBackgroundColor(const gColor &color)
169 if ( m_dc->islocked() )
172 o.opcode = gOpcode::setBackgroundColor;
173 o.dc = m_dc.grabRef();
174 o.parm.setColor = new gOpcode::para::psetColor;
175 o.parm.setColor->color = color;
180 void gPainter::setForegroundColor(const gColor &color)
182 if ( m_dc->islocked() )
185 o.opcode = gOpcode::setForegroundColor;
186 o.dc = m_dc.grabRef();
187 o.parm.setColor = new gOpcode::para::psetColor;
188 o.parm.setColor->color = color;
193 void gPainter::setBackgroundColor(const gRGB &color)
195 if ( m_dc->islocked() )
198 o.opcode = gOpcode::setBackgroundColorRGB;
199 o.dc = m_dc.grabRef();
200 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
201 o.parm.setColorRGB->color = color;
206 void gPainter::setForegroundColor(const gRGB &color)
208 if ( m_dc->islocked() )
211 o.opcode = gOpcode::setForegroundColorRGB;
212 o.dc = m_dc.grabRef();
213 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
214 o.parm.setColorRGB->color = color;
219 void gPainter::setFont(gFont *font)
221 if ( m_dc->islocked() )
224 o.opcode = gOpcode::setFont;
225 o.dc = m_dc.grabRef();
227 o.parm.setFont = new gOpcode::para::psetFont;
228 o.parm.setFont->font = font;
233 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
235 if ( m_dc->islocked() )
238 o.opcode=gOpcode::renderText;
239 o.dc = m_dc.grabRef();
240 o.parm.renderText = new gOpcode::para::prenderText;
241 o.parm.renderText->area = pos;
242 o.parm.renderText->text = string;
243 o.parm.renderText->flags = flags;
247 void gPainter::renderPara(eTextPara *para, ePoint offset)
249 if ( m_dc->islocked() )
252 o.opcode=gOpcode::renderPara;
253 o.dc = m_dc.grabRef();
254 o.parm.renderPara = new gOpcode::para::prenderPara;
255 o.parm.renderPara->offset = offset;
258 o.parm.renderPara->textpara = para;
262 void gPainter::fill(const eRect &area)
264 if ( m_dc->islocked() )
267 o.opcode=gOpcode::fill;
269 o.dc = m_dc.grabRef();
270 o.parm.fill = new gOpcode::para::pfillRect;
271 o.parm.fill->area = area;
275 void gPainter::fill(const gRegion ®ion)
277 if ( m_dc->islocked() )
280 o.opcode=gOpcode::fillRegion;
282 o.dc = m_dc.grabRef();
283 o.parm.fillRegion = new gOpcode::para::pfillRegion;
284 o.parm.fillRegion->region = region;
288 void gPainter::clear()
290 if ( m_dc->islocked() )
293 o.opcode=gOpcode::clear;
294 o.dc = m_dc.grabRef();
295 o.parm.fill = new gOpcode::para::pfillRect;
296 o.parm.fill->area = eRect();
300 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
302 if ( m_dc->islocked() )
308 o.opcode=gOpcode::blit;
309 o.dc = m_dc.grabRef();
311 o.parm.blit = new gOpcode::para::pblit;
312 o.parm.blit->pixmap = pixmap;
313 o.parm.blit->position = pos;
314 o.parm.blit->clip = clip;
315 o.parm.blit->flags=flags;
320 void gPainter::setPalette(gRGB *colors, int start, int len)
322 if ( m_dc->islocked() )
325 o.opcode=gOpcode::setPalette;
326 o.dc = m_dc.grabRef();
327 gPalette *p=new gPalette;
329 o.parm.setPalette = new gOpcode::para::psetPalette;
330 p->data=new gRGB[len];
332 memcpy(p->data, colors, len*sizeof(gRGB));
335 o.parm.setPalette->palette = p;
339 void gPainter::setPalette(gPixmap *source)
342 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
345 void gPainter::mergePalette(gPixmap *target)
347 if ( m_dc->islocked() )
350 o.opcode = gOpcode::mergePalette;
351 o.dc = m_dc.grabRef();
353 o.parm.mergePalette = new gOpcode::para::pmergePalette;
354 o.parm.mergePalette->target = target;
358 void gPainter::line(ePoint start, ePoint end)
360 if ( m_dc->islocked() )
363 o.opcode=gOpcode::line;
364 o.dc = m_dc.grabRef();
365 o.parm.line = new gOpcode::para::pline;
366 o.parm.line->start = start;
367 o.parm.line->end = end;
371 void gPainter::setOffset(ePoint val)
373 if ( m_dc->islocked() )
376 o.opcode=gOpcode::setOffset;
377 o.dc = m_dc.grabRef();
378 o.parm.setOffset = new gOpcode::para::psetOffset;
379 o.parm.setOffset->rel = 0;
380 o.parm.setOffset->value = val;
384 void gPainter::moveOffset(ePoint rel)
386 if ( m_dc->islocked() )
389 o.opcode=gOpcode::setOffset;
390 o.dc = m_dc.grabRef();
391 o.parm.setOffset = new gOpcode::para::psetOffset;
392 o.parm.setOffset->rel = 1;
393 o.parm.setOffset->value = rel;
397 void gPainter::resetOffset()
399 if ( m_dc->islocked() )
402 o.opcode=gOpcode::setOffset;
403 o.dc = m_dc.grabRef();
404 o.parm.setOffset = new gOpcode::para::psetOffset;
405 o.parm.setOffset->rel = 0;
406 o.parm.setOffset->value = ePoint(0, 0);
410 void gPainter::resetClip(const gRegion ®ion)
412 if ( m_dc->islocked() )
415 o.opcode = gOpcode::setClip;
416 o.dc = m_dc.grabRef();
417 o.parm.clip = new gOpcode::para::psetClip;
418 o.parm.clip->region = region;
422 void gPainter::clip(const gRegion ®ion)
424 if ( m_dc->islocked() )
427 o.opcode = gOpcode::addClip;
428 o.dc = m_dc.grabRef();
429 o.parm.clip = new gOpcode::para::psetClip;
430 o.parm.clip->region = region;
434 void gPainter::clippop()
436 if ( m_dc->islocked() )
439 o.opcode = gOpcode::popClip;
440 o.dc = m_dc.grabRef();
444 void gPainter::flush()
446 if ( m_dc->islocked() )
449 o.opcode = gOpcode::flush;
450 o.dc = m_dc.grabRef();
454 void gPainter::waitVSync()
456 if ( m_dc->islocked() )
459 o.opcode = gOpcode::waitVSync;
460 o.dc = m_dc.grabRef();
464 void gPainter::flip()
466 if ( m_dc->islocked() )
469 o.opcode = gOpcode::flip;
470 o.dc = m_dc.grabRef();
474 void gPainter::notify()
476 if ( m_dc->islocked() )
479 o.opcode = gOpcode::notify;
480 o.dc = m_dc.grabRef();
486 if ( m_dc->islocked() )
489 o.opcode = gOpcode::flush;
490 o.dc = m_dc.grabRef();
498 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
506 void gDC::exec(gOpcode *o)
510 case gOpcode::setBackgroundColor:
511 m_background_color = o->parm.setColor->color;
512 delete o->parm.setColor;
514 case gOpcode::setForegroundColor:
515 m_foreground_color = o->parm.setColor->color;
516 delete o->parm.setColor;
518 case gOpcode::setBackgroundColorRGB:
519 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
520 delete o->parm.setColorRGB;
522 case gOpcode::setForegroundColorRGB:
523 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
524 delete o->parm.setColorRGB;
526 case gOpcode::setFont:
527 m_current_font = o->parm.setFont->font;
528 o->parm.setFont->font->Release();
529 delete o->parm.setFont;
531 case gOpcode::renderText:
533 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
534 int flags = o->parm.renderText->flags;
535 assert(m_current_font);
536 para->setFont(m_current_font);
537 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
539 if (flags & gPainter::RT_HALIGN_RIGHT)
540 para->realign(eTextPara::dirRight);
541 else if (flags & gPainter::RT_HALIGN_CENTER)
542 para->realign(eTextPara::dirCenter);
543 else if (flags & gPainter::RT_HALIGN_BLOCK)
544 para->realign(eTextPara::dirBlock);
546 ePoint offset = m_current_offset;
548 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
550 eRect bbox = para->getBoundBox();
551 int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
552 int correction = vcentered_top - bbox.top();
553 offset += ePoint(0, correction);
555 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
556 delete o->parm.renderText;
559 case gOpcode::renderPara:
561 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
562 o->parm.renderPara->textpara->Release();
563 delete o->parm.renderPara;
568 eRect area = o->parm.fill->area;
569 area.moveBy(m_current_offset);
570 gRegion clip = m_current_clip & area;
571 m_pixmap->fill(clip, m_foreground_color);
575 case gOpcode::fillRegion:
577 o->parm.fillRegion->region.moveBy(m_current_offset);
578 gRegion clip = m_current_clip & o->parm.fillRegion->region;
579 m_pixmap->fill(clip, m_foreground_color);
580 delete o->parm.fillRegion;
584 m_pixmap->fill(m_current_clip, m_background_color);
590 // this code should be checked again but i'm too tired now
592 o->parm.blit->position += m_current_offset;
594 if (o->parm.blit->clip.valid())
596 o->parm.blit->clip.moveBy(m_current_offset);
597 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
599 clip = m_current_clip;
601 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
602 o->parm.blit->pixmap->Release();
606 case gOpcode::setPalette:
607 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
608 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
609 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
610 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
611 if (o->parm.setPalette->palette->colors)
612 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
614 delete[] o->parm.setPalette->palette->data;
615 delete o->parm.setPalette->palette;
616 delete o->parm.setPalette;
618 case gOpcode::mergePalette:
619 m_pixmap->mergePalette(*o->parm.mergePalette->target);
620 o->parm.mergePalette->target->Release();
621 delete o->parm.mergePalette;
625 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
626 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
630 case gOpcode::addClip:
631 m_clip_stack.push(m_current_clip);
632 o->parm.clip->region.moveBy(m_current_offset);
633 m_current_clip &= o->parm.clip->region;
636 case gOpcode::setClip:
637 o->parm.clip->region.moveBy(m_current_offset);
638 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
641 case gOpcode::popClip:
642 if (!m_clip_stack.empty())
644 m_current_clip = m_clip_stack.top();
648 case gOpcode::setOffset:
649 if (o->parm.setOffset->rel)
650 m_current_offset += o->parm.setOffset->value;
652 m_current_offset = o->parm.setOffset->value;
653 delete o->parm.setOffset;
655 case gOpcode::waitVSync:
662 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
666 gRGB gDC::getRGB(gColor col)
668 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
669 return gRGB(col, col, col);
672 eFatal("bla transp");
673 return gRGB(0, 0, 0, 0xFF);
675 return m_pixmap->surface->clut.data[col];
680 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");