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>
16 void *gRC::thread_wrapper(void *ptr)
19 return ((gRC*)ptr)->thread();
25 gRC::gRC(): queue(2048), m_notify_pump(eApp, 0), queuelock(MAXSIZE)
29 queuelock.lock(MAXSIZE);
30 CONNECT(m_notify_pump.recv_msg, gRC::recv_notify);
32 int res = pthread_create(&the_thread, 0, thread_wrapper, this);
34 eFatal("RC thread couldn't be created");
36 eDebug("RC thread created successfully");
47 o.opcode=gOpcode::shutdown;
50 eDebug("waiting for gRC thread shutdown");
51 pthread_join(the_thread, 0);
52 eDebug("gRC thread has finished");
66 gOpcode& o(queue.current());
67 if (o.opcode==gOpcode::shutdown)
69 if (o.opcode==gOpcode::notify)
76 if ((!queue.size()) && need_notify)
79 m_notify_pump.send(1);
88 void gRC::recv_notify(const int &i)
93 gRC *gRC::getInstance()
98 static int gPainter_instances;
100 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
102 // ASSERT(!gPainter_instances);
103 gPainter_instances++;
107 gPainter::~gPainter()
110 gPainter_instances--;
113 void gPainter::setBackgroundColor(const gColor &color)
116 o.opcode = gOpcode::setBackgroundColor;
117 o.dc = m_dc.grabRef();
118 o.parm.setColor = new gOpcode::para::psetColor;
119 o.parm.setColor->color = color;
124 void gPainter::setForegroundColor(const gColor &color)
127 o.opcode = gOpcode::setForegroundColor;
128 o.dc = m_dc.grabRef();
129 o.parm.setColor = new gOpcode::para::psetColor;
130 o.parm.setColor->color = color;
135 void gPainter::setBackgroundColor(const gRGB &color)
138 o.opcode = gOpcode::setBackgroundColorRGB;
139 o.dc = m_dc.grabRef();
140 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
141 o.parm.setColorRGB->color = color;
146 void gPainter::setForegroundColor(const gRGB &color)
149 o.opcode = gOpcode::setForegroundColorRGB;
150 o.dc = m_dc.grabRef();
151 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
152 o.parm.setColorRGB->color = color;
157 void gPainter::setFont(gFont *font)
160 o.opcode = gOpcode::setFont;
161 o.dc = m_dc.grabRef();
163 o.parm.setFont = new gOpcode::para::psetFont;
164 o.parm.setFont->font = font;
169 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
172 o.opcode=gOpcode::renderText;
173 o.dc = m_dc.grabRef();
174 o.parm.renderText = new gOpcode::para::prenderText;
175 o.parm.renderText->area = pos;
176 o.parm.renderText->text = string;
177 o.parm.renderText->flags = flags;
181 void gPainter::renderPara(eTextPara *para, ePoint offset)
184 o.opcode=gOpcode::renderPara;
185 o.dc = m_dc.grabRef();
186 o.parm.renderPara = new gOpcode::para::prenderPara;
187 o.parm.renderPara->offset = offset;
190 o.parm.renderPara->textpara = para;
194 void gPainter::fill(const eRect &area)
197 o.opcode=gOpcode::fill;
199 o.dc = m_dc.grabRef();
200 o.parm.fill = new gOpcode::para::pfillRect;
201 o.parm.fill->area = area;
205 void gPainter::fill(const gRegion ®ion)
208 o.opcode=gOpcode::fillRegion;
210 o.dc = m_dc.grabRef();
211 o.parm.fillRegion = new gOpcode::para::pfillRegion;
212 o.parm.fillRegion->region = region;
216 void gPainter::clear()
219 o.opcode=gOpcode::clear;
220 o.dc = m_dc.grabRef();
221 o.parm.fill = new gOpcode::para::pfillRect;
222 o.parm.fill->area = eRect();
226 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
230 o.opcode=gOpcode::blit;
231 o.dc = m_dc.grabRef();
233 o.parm.blit = new gOpcode::para::pblit;
234 o.parm.blit->pixmap = pixmap;
235 o.parm.blit->position = pos;
236 o.parm.blit->clip = clip;
237 o.parm.blit->flags=flags;
242 void gPainter::setPalette(gRGB *colors, int start, int len)
245 o.opcode=gOpcode::setPalette;
246 o.dc = m_dc.grabRef();
247 gPalette *p=new gPalette;
249 o.parm.setPalette = new gOpcode::para::psetPalette;
250 p->data=new gRGB[len];
252 memcpy(p->data, colors, len*sizeof(gRGB));
255 o.parm.setPalette->palette = p;
259 void gPainter::setPalette(gPixmap *source)
262 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
265 void gPainter::mergePalette(gPixmap *target)
268 o.opcode = gOpcode::mergePalette;
269 o.dc = m_dc.grabRef();
271 o.parm.mergePalette = new gOpcode::para::pmergePalette;
272 o.parm.mergePalette->target = target;
276 void gPainter::line(ePoint start, ePoint end)
279 o.opcode=gOpcode::line;
280 o.dc = m_dc.grabRef();
281 o.parm.line = new gOpcode::para::pline;
282 o.parm.line->start = start;
283 o.parm.line->end = end;
287 void gPainter::setOffset(ePoint val)
290 o.opcode=gOpcode::setOffset;
291 o.dc = m_dc.grabRef();
292 o.parm.setOffset = new gOpcode::para::psetOffset;
293 o.parm.setOffset->rel = 0;
294 o.parm.setOffset->value = val;
298 void gPainter::moveOffset(ePoint rel)
301 o.opcode=gOpcode::setOffset;
302 o.dc = m_dc.grabRef();
303 o.parm.setOffset = new gOpcode::para::psetOffset;
304 o.parm.setOffset->rel = 1;
305 o.parm.setOffset->value = rel;
309 void gPainter::resetOffset()
312 o.opcode=gOpcode::setOffset;
313 o.dc = m_dc.grabRef();
314 o.parm.setOffset = new gOpcode::para::psetOffset;
315 o.parm.setOffset->rel = 0;
316 o.parm.setOffset->value = ePoint(0, 0);
320 void gPainter::resetClip(const gRegion ®ion)
323 o.opcode = gOpcode::setClip;
324 o.dc = m_dc.grabRef();
325 o.parm.clip = new gOpcode::para::psetClip;
326 o.parm.clip->region = region;
330 void gPainter::clip(const gRegion ®ion)
333 o.opcode = gOpcode::addClip;
334 o.dc = m_dc.grabRef();
335 o.parm.clip = new gOpcode::para::psetClip;
336 o.parm.clip->region = region;
340 void gPainter::clippop()
343 o.opcode = gOpcode::popClip;
344 o.dc = m_dc.grabRef();
348 void gPainter::flush()
351 o.opcode = gOpcode::flush;
352 o.dc = m_dc.grabRef();
356 void gPainter::waitVSync()
359 o.opcode = gOpcode::waitVSync;
360 o.dc = m_dc.grabRef();
364 void gPainter::flip()
367 o.opcode = gOpcode::flip;
368 o.dc = m_dc.grabRef();
372 void gPainter::notify()
375 o.opcode = gOpcode::notify;
376 o.dc = m_dc.grabRef();
383 o.opcode = gOpcode::flush;
384 o.dc = m_dc.grabRef();
392 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
400 void gDC::exec(gOpcode *o)
404 case gOpcode::setBackgroundColor:
405 m_background_color = o->parm.setColor->color;
406 delete o->parm.setColor;
408 case gOpcode::setForegroundColor:
409 m_foreground_color = o->parm.setColor->color;
410 delete o->parm.setColor;
412 case gOpcode::setBackgroundColorRGB:
413 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
414 delete o->parm.setColorRGB;
416 case gOpcode::setForegroundColorRGB:
417 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
418 delete o->parm.setColorRGB;
420 case gOpcode::setFont:
421 m_current_font = o->parm.setFont->font;
422 o->parm.setFont->font->Release();
423 delete o->parm.setFont;
425 case gOpcode::renderText:
427 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
428 int flags = o->parm.renderText->flags;
429 assert(m_current_font);
430 para->setFont(m_current_font);
431 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
433 if (flags & gPainter::RT_HALIGN_RIGHT)
434 para->realign(eTextPara::dirRight);
435 else if (flags & gPainter::RT_HALIGN_CENTER)
436 para->realign(eTextPara::dirCenter);
437 else if (flags & gPainter::RT_HALIGN_BLOCK)
438 para->realign(eTextPara::dirBlock);
440 ePoint offset = m_current_offset;
442 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
444 eRect bbox = para->getBoundBox();
445 int vcentered_top = (o->parm.renderText->area.height() - bbox.height()) / 2;
446 int correction = vcentered_top - bbox.top();
447 offset += ePoint(0, correction);
449 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
450 delete o->parm.renderText;
453 case gOpcode::renderPara:
455 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
456 o->parm.renderPara->textpara->Release();
457 delete o->parm.renderPara;
462 eRect area = o->parm.fill->area;
463 area.moveBy(m_current_offset);
464 gRegion clip = m_current_clip & area;
465 m_pixmap->fill(clip, m_foreground_color);
469 case gOpcode::fillRegion:
471 o->parm.fillRegion->region.moveBy(m_current_offset);
472 gRegion clip = m_current_clip & o->parm.fillRegion->region;
473 m_pixmap->fill(clip, m_foreground_color);
474 delete o->parm.fillRegion;
478 m_pixmap->fill(m_current_clip, m_background_color);
484 // this code should be checked again but i'm too tired now
486 o->parm.blit->position += m_current_offset;
488 if (o->parm.blit->clip.valid())
490 o->parm.blit->clip.moveBy(m_current_offset);
491 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
493 clip = m_current_clip;
495 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
496 o->parm.blit->pixmap->Release();
500 case gOpcode::setPalette:
501 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
502 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
503 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
504 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
505 if (o->parm.setPalette->palette->colors)
506 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
508 delete[] o->parm.setPalette->palette->data;
509 delete o->parm.setPalette->palette;
510 delete o->parm.setPalette;
512 case gOpcode::mergePalette:
513 m_pixmap->mergePalette(*o->parm.mergePalette->target);
514 o->parm.mergePalette->target->Release();
515 delete o->parm.mergePalette;
519 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
520 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
524 case gOpcode::addClip:
525 m_clip_stack.push(m_current_clip);
526 o->parm.clip->region.moveBy(m_current_offset);
527 m_current_clip &= o->parm.clip->region;
530 case gOpcode::setClip:
531 o->parm.clip->region.moveBy(m_current_offset);
532 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
535 case gOpcode::popClip:
536 if (!m_clip_stack.empty())
538 m_current_clip = m_clip_stack.top();
542 case gOpcode::setOffset:
543 if (o->parm.setOffset->rel)
544 m_current_offset += o->parm.setOffset->value;
546 m_current_offset = o->parm.setOffset->value;
547 delete o->parm.setOffset;
549 case gOpcode::waitVSync:
556 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
560 gRGB gDC::getRGB(gColor col)
562 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
563 return gRGB(col, col, col);
566 eFatal("bla transp");
567 return gRGB(0, 0, 0, 0xFF);
569 return m_pixmap->surface->clut.data[col];
574 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");