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");
65 gOpcode& o(queue.current());
66 if (o.opcode==gOpcode::shutdown)
68 if (o.opcode==gOpcode::notify)
70 m_notify_pump.send(1);
82 void gRC::recv_notify(const int &i)
87 gRC *gRC::getInstance()
92 static int gPainter_instances;
94 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
96 // ASSERT(!gPainter_instances);
101 gPainter::~gPainter()
104 gPainter_instances--;
107 void gPainter::setBackgroundColor(const gColor &color)
110 o.opcode = gOpcode::setBackgroundColor;
111 o.dc = m_dc.grabRef();
112 o.parm.setColor = new gOpcode::para::psetColor;
113 o.parm.setColor->color = color;
118 void gPainter::setForegroundColor(const gColor &color)
121 o.opcode = gOpcode::setForegroundColor;
122 o.dc = m_dc.grabRef();
123 o.parm.setColor = new gOpcode::para::psetColor;
124 o.parm.setColor->color = color;
129 void gPainter::setBackgroundColor(const gRGB &color)
132 o.opcode = gOpcode::setBackgroundColorRGB;
133 o.dc = m_dc.grabRef();
134 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
135 o.parm.setColorRGB->color = color;
140 void gPainter::setForegroundColor(const gRGB &color)
143 o.opcode = gOpcode::setForegroundColorRGB;
144 o.dc = m_dc.grabRef();
145 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
146 o.parm.setColorRGB->color = color;
151 void gPainter::setFont(gFont *font)
154 o.opcode = gOpcode::setFont;
155 o.dc = m_dc.grabRef();
157 o.parm.setFont = new gOpcode::para::psetFont;
158 o.parm.setFont->font = font;
163 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
166 o.opcode=gOpcode::renderText;
167 o.dc = m_dc.grabRef();
168 o.parm.renderText = new gOpcode::para::prenderText;
169 o.parm.renderText->area = pos;
170 o.parm.renderText->text = string;
171 o.parm.renderText->flags = flags;
175 void gPainter::renderPara(eTextPara *para, ePoint offset)
178 o.opcode=gOpcode::renderPara;
179 o.dc = m_dc.grabRef();
180 o.parm.renderPara = new gOpcode::para::prenderPara;
181 o.parm.renderPara->offset = offset;
184 o.parm.renderPara->textpara = para;
188 void gPainter::fill(const eRect &area)
191 o.opcode=gOpcode::fill;
193 o.dc = m_dc.grabRef();
194 o.parm.fill = new gOpcode::para::pfillRect;
195 o.parm.fill->area = area;
199 void gPainter::fill(const gRegion ®ion)
202 o.opcode=gOpcode::fillRegion;
204 o.dc = m_dc.grabRef();
205 o.parm.fillRegion = new gOpcode::para::pfillRegion;
206 o.parm.fillRegion->region = region;
210 void gPainter::clear()
213 o.opcode=gOpcode::clear;
214 o.dc = m_dc.grabRef();
215 o.parm.fill = new gOpcode::para::pfillRect;
216 o.parm.fill->area = eRect();
220 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
224 o.opcode=gOpcode::blit;
225 o.dc = m_dc.grabRef();
227 o.parm.blit = new gOpcode::para::pblit;
228 o.parm.blit->pixmap = pixmap;
229 o.parm.blit->position = pos;
230 o.parm.blit->clip = clip;
231 o.parm.blit->flags=flags;
236 void gPainter::setPalette(gRGB *colors, int start, int len)
239 o.opcode=gOpcode::setPalette;
240 o.dc = m_dc.grabRef();
241 gPalette *p=new gPalette;
243 o.parm.setPalette = new gOpcode::para::psetPalette;
244 p->data=new gRGB[len];
246 memcpy(p->data, colors, len*sizeof(gRGB));
249 o.parm.setPalette->palette = p;
253 void gPainter::setPalette(gPixmap *source)
256 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
259 void gPainter::mergePalette(gPixmap *target)
262 o.opcode = gOpcode::mergePalette;
263 o.dc = m_dc.grabRef();
265 o.parm.mergePalette = new gOpcode::para::pmergePalette;
266 o.parm.mergePalette->target = target;
270 void gPainter::line(ePoint start, ePoint end)
273 o.opcode=gOpcode::line;
274 o.dc = m_dc.grabRef();
275 o.parm.line = new gOpcode::para::pline;
276 o.parm.line->start = start;
277 o.parm.line->end = end;
281 void gPainter::setOffset(ePoint val)
284 o.opcode=gOpcode::setOffset;
285 o.dc = m_dc.grabRef();
286 o.parm.setOffset = new gOpcode::para::psetOffset;
287 o.parm.setOffset->rel = 0;
288 o.parm.setOffset->value = val;
292 void gPainter::moveOffset(ePoint rel)
295 o.opcode=gOpcode::setOffset;
296 o.dc = m_dc.grabRef();
297 o.parm.setOffset = new gOpcode::para::psetOffset;
298 o.parm.setOffset->rel = 1;
299 o.parm.setOffset->value = rel;
303 void gPainter::resetOffset()
306 o.opcode=gOpcode::setOffset;
307 o.dc = m_dc.grabRef();
308 o.parm.setOffset = new gOpcode::para::psetOffset;
309 o.parm.setOffset->rel = 0;
310 o.parm.setOffset->value = ePoint(0, 0);
314 void gPainter::resetClip(const gRegion ®ion)
317 o.opcode = gOpcode::setClip;
318 o.dc = m_dc.grabRef();
319 o.parm.clip = new gOpcode::para::psetClip;
320 o.parm.clip->region = region;
324 void gPainter::clip(const gRegion ®ion)
327 o.opcode = gOpcode::addClip;
328 o.dc = m_dc.grabRef();
329 o.parm.clip = new gOpcode::para::psetClip;
330 o.parm.clip->region = region;
334 void gPainter::clippop()
337 o.opcode = gOpcode::popClip;
338 o.dc = m_dc.grabRef();
342 void gPainter::flush()
345 o.opcode = gOpcode::flush;
346 o.dc = m_dc.grabRef();
350 void gPainter::waitVSync()
353 o.opcode = gOpcode::waitVSync;
354 o.dc = m_dc.grabRef();
358 void gPainter::flip()
361 o.opcode = gOpcode::flip;
362 o.dc = m_dc.grabRef();
366 void gPainter::notify()
369 o.opcode = gOpcode::notify;
370 o.dc = m_dc.grabRef();
377 o.opcode = gOpcode::flush;
378 o.dc = m_dc.grabRef();
386 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
394 void gDC::exec(gOpcode *o)
398 case gOpcode::setBackgroundColor:
399 m_background_color = o->parm.setColor->color;
400 delete o->parm.setColor;
402 case gOpcode::setForegroundColor:
403 m_foreground_color = o->parm.setColor->color;
404 delete o->parm.setColor;
406 case gOpcode::setBackgroundColorRGB:
407 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
408 delete o->parm.setColorRGB;
410 case gOpcode::setForegroundColorRGB:
411 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
412 delete o->parm.setColorRGB;
414 case gOpcode::setFont:
415 m_current_font = o->parm.setFont->font;
416 o->parm.setFont->font->Release();
417 delete o->parm.setFont;
419 case gOpcode::renderText:
421 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
422 int flags = o->parm.renderText->flags;
423 assert(m_current_font);
424 para->setFont(m_current_font);
425 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
427 if (flags & gPainter::RT_HALIGN_RIGHT)
428 para->realign(eTextPara::dirRight);
429 else if (flags & gPainter::RT_HALIGN_CENTER)
430 para->realign(eTextPara::dirCenter);
431 else if (flags & gPainter::RT_HALIGN_BLOCK)
432 para->realign(eTextPara::dirBlock);
434 ePoint offset = m_current_offset;
436 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
438 eRect bbox = para->getBoundBox();
439 offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
441 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
442 delete o->parm.renderText;
445 case gOpcode::renderPara:
447 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
448 o->parm.renderPara->textpara->Release();
449 delete o->parm.renderPara;
454 eRect area = o->parm.fill->area;
455 area.moveBy(m_current_offset);
456 gRegion clip = m_current_clip & area;
457 m_pixmap->fill(clip, m_foreground_color);
461 case gOpcode::fillRegion:
463 o->parm.fillRegion->region.moveBy(m_current_offset);
464 gRegion clip = m_current_clip & o->parm.fillRegion->region;
465 m_pixmap->fill(clip, m_foreground_color);
466 delete o->parm.fillRegion;
470 m_pixmap->fill(m_current_clip, m_background_color);
476 // this code should be checked again but i'm too tired now
478 o->parm.blit->position += m_current_offset;
480 if (o->parm.blit->clip.valid())
482 o->parm.blit->clip.moveBy(m_current_offset);
483 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
485 clip = m_current_clip;
487 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
488 o->parm.blit->pixmap->Release();
492 case gOpcode::setPalette:
493 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
494 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
495 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
496 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
497 if (o->parm.setPalette->palette->colors)
498 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
500 delete[] o->parm.setPalette->palette->data;
501 delete o->parm.setPalette->palette;
502 delete o->parm.setPalette;
504 case gOpcode::mergePalette:
505 m_pixmap->mergePalette(*o->parm.mergePalette->target);
506 o->parm.mergePalette->target->Release();
507 delete o->parm.mergePalette;
511 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
512 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
516 case gOpcode::addClip:
517 m_clip_stack.push(m_current_clip);
518 o->parm.clip->region.moveBy(m_current_offset);
519 m_current_clip &= o->parm.clip->region;
522 case gOpcode::setClip:
523 o->parm.clip->region.moveBy(m_current_offset);
524 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
527 case gOpcode::popClip:
528 if (!m_clip_stack.empty())
530 m_current_clip = m_clip_stack.top();
534 case gOpcode::setOffset:
535 if (o->parm.setOffset->rel)
536 m_current_offset += o->parm.setOffset->value;
538 m_current_offset = o->parm.setOffset->value;
539 delete o->parm.setOffset;
541 case gOpcode::waitVSync:
548 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
552 gRGB gDC::getRGB(gColor col)
554 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
555 return gRGB(col, col, col);
558 eFatal("bla transp");
559 return gRGB(0, 0, 0, 0xFF);
561 return m_pixmap->surface->clut.data[col];
566 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");