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");
44 o.opcode=gOpcode::shutdown;
47 eDebug("waiting for gRC thread shutdown");
48 pthread_join(the_thread, 0);
49 eDebug("gRC thread has finished");
53 void gRC::submit(const gOpcode &o)
58 pthread_mutex_lock(&mutex);
66 pthread_mutex_unlock(&mutex);
70 //printf("render buffer full...\n");
72 usleep(1000); // wait 1 msec
81 if (o.opcode==gOpcode::flush||o.opcode==gOpcode::shutdown||o.opcode==gOpcode::notify)
83 pthread_cond_signal(&cond); // wakeup gdi thread
84 pthread_mutex_unlock(&mutex);
103 pthread_mutex_lock(&mutex);
107 gOpcode o(queue[rp++]);
111 pthread_mutex_unlock(&mutex);
113 if (o.opcode==gOpcode::shutdown)
115 else if (o.opcode==gOpcode::notify)
120 // o.dc is a gDC* filled with grabref... so we must release it here
129 m_notify_pump.send(1);
133 pthread_cond_wait(&cond, &mutex);
134 pthread_mutex_unlock(&mutex);
144 void gRC::recv_notify(const int &i)
149 gRC *gRC::getInstance()
154 static int gPainter_instances;
156 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
158 // ASSERT(!gPainter_instances);
159 gPainter_instances++;
163 gPainter::~gPainter()
166 gPainter_instances--;
169 void gPainter::setBackgroundColor(const gColor &color)
171 if ( m_dc->islocked() )
174 o.opcode = gOpcode::setBackgroundColor;
175 o.dc = m_dc.grabRef();
176 o.parm.setColor = new gOpcode::para::psetColor;
177 o.parm.setColor->color = color;
182 void gPainter::setForegroundColor(const gColor &color)
184 if ( m_dc->islocked() )
187 o.opcode = gOpcode::setForegroundColor;
188 o.dc = m_dc.grabRef();
189 o.parm.setColor = new gOpcode::para::psetColor;
190 o.parm.setColor->color = color;
195 void gPainter::setBackgroundColor(const gRGB &color)
197 if ( m_dc->islocked() )
200 o.opcode = gOpcode::setBackgroundColorRGB;
201 o.dc = m_dc.grabRef();
202 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
203 o.parm.setColorRGB->color = color;
208 void gPainter::setForegroundColor(const gRGB &color)
210 if ( m_dc->islocked() )
213 o.opcode = gOpcode::setForegroundColorRGB;
214 o.dc = m_dc.grabRef();
215 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
216 o.parm.setColorRGB->color = color;
221 void gPainter::setFont(gFont *font)
223 if ( m_dc->islocked() )
226 o.opcode = gOpcode::setFont;
227 o.dc = m_dc.grabRef();
229 o.parm.setFont = new gOpcode::para::psetFont;
230 o.parm.setFont->font = font;
235 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
237 if ( m_dc->islocked() )
240 o.opcode=gOpcode::renderText;
241 o.dc = m_dc.grabRef();
242 o.parm.renderText = new gOpcode::para::prenderText;
243 o.parm.renderText->area = pos;
244 o.parm.renderText->text = string.empty()?0:strdup(string.c_str());
245 o.parm.renderText->flags = flags;
249 void gPainter::renderPara(eTextPara *para, ePoint offset)
251 if ( m_dc->islocked() )
254 o.opcode=gOpcode::renderPara;
255 o.dc = m_dc.grabRef();
256 o.parm.renderPara = new gOpcode::para::prenderPara;
257 o.parm.renderPara->offset = offset;
260 o.parm.renderPara->textpara = para;
264 void gPainter::fill(const eRect &area)
266 if ( m_dc->islocked() )
269 o.opcode=gOpcode::fill;
271 o.dc = m_dc.grabRef();
272 o.parm.fill = new gOpcode::para::pfillRect;
273 o.parm.fill->area = area;
277 void gPainter::fill(const gRegion ®ion)
279 if ( m_dc->islocked() )
282 o.opcode=gOpcode::fillRegion;
284 o.dc = m_dc.grabRef();
285 o.parm.fillRegion = new gOpcode::para::pfillRegion;
286 o.parm.fillRegion->region = region;
290 void gPainter::clear()
292 if ( m_dc->islocked() )
295 o.opcode=gOpcode::clear;
296 o.dc = m_dc.grabRef();
297 o.parm.fill = new gOpcode::para::pfillRect;
298 o.parm.fill->area = eRect();
302 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
304 if ( m_dc->islocked() )
310 o.opcode=gOpcode::blit;
311 o.dc = m_dc.grabRef();
313 o.parm.blit = new gOpcode::para::pblit;
314 o.parm.blit->pixmap = pixmap;
315 o.parm.blit->position = pos;
316 o.parm.blit->clip = clip;
317 o.parm.blit->flags=flags;
322 void gPainter::setPalette(gRGB *colors, int start, int len)
324 if ( m_dc->islocked() )
327 o.opcode=gOpcode::setPalette;
328 o.dc = m_dc.grabRef();
329 gPalette *p=new gPalette;
331 o.parm.setPalette = new gOpcode::para::psetPalette;
332 p->data=new gRGB[len];
334 memcpy(p->data, colors, len*sizeof(gRGB));
337 o.parm.setPalette->palette = p;
341 void gPainter::setPalette(gPixmap *source)
344 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
347 void gPainter::mergePalette(gPixmap *target)
349 if ( m_dc->islocked() )
352 o.opcode = gOpcode::mergePalette;
353 o.dc = m_dc.grabRef();
355 o.parm.mergePalette = new gOpcode::para::pmergePalette;
356 o.parm.mergePalette->target = target;
360 void gPainter::line(ePoint start, ePoint end)
362 if ( m_dc->islocked() )
365 o.opcode=gOpcode::line;
366 o.dc = m_dc.grabRef();
367 o.parm.line = new gOpcode::para::pline;
368 o.parm.line->start = start;
369 o.parm.line->end = end;
373 void gPainter::setOffset(ePoint val)
375 if ( m_dc->islocked() )
378 o.opcode=gOpcode::setOffset;
379 o.dc = m_dc.grabRef();
380 o.parm.setOffset = new gOpcode::para::psetOffset;
381 o.parm.setOffset->rel = 0;
382 o.parm.setOffset->value = val;
386 void gPainter::moveOffset(ePoint rel)
388 if ( m_dc->islocked() )
391 o.opcode=gOpcode::setOffset;
392 o.dc = m_dc.grabRef();
393 o.parm.setOffset = new gOpcode::para::psetOffset;
394 o.parm.setOffset->rel = 1;
395 o.parm.setOffset->value = rel;
399 void gPainter::resetOffset()
401 if ( m_dc->islocked() )
404 o.opcode=gOpcode::setOffset;
405 o.dc = m_dc.grabRef();
406 o.parm.setOffset = new gOpcode::para::psetOffset;
407 o.parm.setOffset->rel = 0;
408 o.parm.setOffset->value = ePoint(0, 0);
412 void gPainter::resetClip(const gRegion ®ion)
414 if ( m_dc->islocked() )
417 o.opcode = gOpcode::setClip;
418 o.dc = m_dc.grabRef();
419 o.parm.clip = new gOpcode::para::psetClip;
420 o.parm.clip->region = region;
424 void gPainter::clip(const gRegion ®ion)
426 if ( m_dc->islocked() )
429 o.opcode = gOpcode::addClip;
430 o.dc = m_dc.grabRef();
431 o.parm.clip = new gOpcode::para::psetClip;
432 o.parm.clip->region = region;
436 void gPainter::clippop()
438 if ( m_dc->islocked() )
441 o.opcode = gOpcode::popClip;
442 o.dc = m_dc.grabRef();
446 void gPainter::flush()
448 if ( m_dc->islocked() )
451 o.opcode = gOpcode::flush;
452 o.dc = m_dc.grabRef();
456 void gPainter::waitVSync()
458 if ( m_dc->islocked() )
461 o.opcode = gOpcode::waitVSync;
462 o.dc = m_dc.grabRef();
466 void gPainter::flip()
468 if ( m_dc->islocked() )
471 o.opcode = gOpcode::flip;
472 o.dc = m_dc.grabRef();
476 void gPainter::notify()
478 if ( m_dc->islocked() )
481 o.opcode = gOpcode::notify;
482 o.dc = m_dc.grabRef();
488 if ( m_dc->islocked() )
491 o.opcode = gOpcode::flush;
492 o.dc = m_dc.grabRef();
500 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
508 void gDC::exec(gOpcode *o)
512 case gOpcode::setBackgroundColor:
513 m_background_color = o->parm.setColor->color;
514 m_background_color_rgb = getRGB(m_background_color);
515 delete o->parm.setColor;
517 case gOpcode::setForegroundColor:
518 m_foreground_color = o->parm.setColor->color;
519 m_background_color_rgb = getRGB(m_foreground_color);
520 delete o->parm.setColor;
522 case gOpcode::setBackgroundColorRGB:
523 if (m_pixmap->needClut())
524 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
525 m_background_color_rgb = o->parm.setColorRGB->color;
526 delete o->parm.setColorRGB;
528 case gOpcode::setForegroundColorRGB:
529 if (m_pixmap->needClut())
530 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
531 m_foreground_color_rgb = o->parm.setColorRGB->color;
532 delete o->parm.setColorRGB;
534 case gOpcode::setFont:
535 m_current_font = o->parm.setFont->font;
536 o->parm.setFont->font->Release();
537 delete o->parm.setFont;
539 case gOpcode::renderText:
541 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
542 int flags = o->parm.renderText->flags;
543 assert(m_current_font);
544 para->setFont(m_current_font);
545 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
546 if (o->parm.renderText->text)
547 free(o->parm.renderText->text);
548 if (flags & gPainter::RT_HALIGN_RIGHT)
549 para->realign(eTextPara::dirRight);
550 else if (flags & gPainter::RT_HALIGN_CENTER)
551 para->realign(eTextPara::dirCenter);
552 else if (flags & gPainter::RT_HALIGN_BLOCK)
553 para->realign(eTextPara::dirBlock);
555 ePoint offset = m_current_offset;
557 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
559 eRect bbox = para->getBoundBox();
560 int vcentered_top = o->parm.renderText->area.top() + ((o->parm.renderText->area.height() - bbox.height()) / 2);
561 int correction = vcentered_top - bbox.top();
562 offset += ePoint(0, correction);
565 para->blit(*this, offset, m_background_color_rgb, m_foreground_color_rgb);
566 delete o->parm.renderText;
569 case gOpcode::renderPara:
571 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, m_background_color_rgb, m_foreground_color_rgb);
572 o->parm.renderPara->textpara->Release();
573 delete o->parm.renderPara;
578 eRect area = o->parm.fill->area;
579 area.moveBy(m_current_offset);
580 gRegion clip = m_current_clip & area;
581 if (m_pixmap->needClut())
582 m_pixmap->fill(clip, m_foreground_color);
584 m_pixmap->fill(clip, m_foreground_color_rgb);
588 case gOpcode::fillRegion:
590 o->parm.fillRegion->region.moveBy(m_current_offset);
591 gRegion clip = m_current_clip & o->parm.fillRegion->region;
592 if (m_pixmap->needClut())
593 m_pixmap->fill(clip, m_foreground_color);
595 m_pixmap->fill(clip, m_foreground_color_rgb);
596 delete o->parm.fillRegion;
600 if (m_pixmap->needClut())
601 m_pixmap->fill(m_current_clip, m_background_color);
603 m_pixmap->fill(m_current_clip, m_background_color_rgb);
609 // this code should be checked again but i'm too tired now
611 o->parm.blit->position += m_current_offset;
613 if (o->parm.blit->clip.valid())
615 o->parm.blit->clip.moveBy(m_current_offset);
616 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
618 clip = m_current_clip;
620 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
621 o->parm.blit->pixmap->Release();
625 case gOpcode::setPalette:
626 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
627 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
628 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
629 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
630 if (o->parm.setPalette->palette->colors)
631 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
633 delete[] o->parm.setPalette->palette->data;
634 delete o->parm.setPalette->palette;
635 delete o->parm.setPalette;
637 case gOpcode::mergePalette:
638 m_pixmap->mergePalette(*o->parm.mergePalette->target);
639 o->parm.mergePalette->target->Release();
640 delete o->parm.mergePalette;
644 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
645 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
649 case gOpcode::addClip:
650 m_clip_stack.push(m_current_clip);
651 o->parm.clip->region.moveBy(m_current_offset);
652 m_current_clip &= o->parm.clip->region;
655 case gOpcode::setClip:
656 o->parm.clip->region.moveBy(m_current_offset);
657 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
660 case gOpcode::popClip:
661 if (!m_clip_stack.empty())
663 m_current_clip = m_clip_stack.top();
667 case gOpcode::setOffset:
668 if (o->parm.setOffset->rel)
669 m_current_offset += o->parm.setOffset->value;
671 m_current_offset = o->parm.setOffset->value;
672 delete o->parm.setOffset;
674 case gOpcode::waitVSync:
681 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
685 gRGB gDC::getRGB(gColor col)
687 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
688 return gRGB(col, col, col);
691 eFatal("bla transp");
692 return gRGB(0, 0, 0, 0xFF);
694 return m_pixmap->surface->clut.data[col];
699 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");