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), queuelock(MAXSIZE)
29 queuelock.lock(MAXSIZE);
31 int res = pthread_create(&the_thread, 0, thread_wrapper, this);
33 eFatal("RC thread couldn't be created");
35 eDebug("RC thread createted successfully");
46 o.opcode=gOpcode::shutdown;
49 eDebug("waiting for gRC thread shutdown");
50 pthread_join(the_thread, 0);
51 eDebug("gRC thread has finished");
64 gOpcode& o(queue.current());
65 if (o.opcode==gOpcode::shutdown)
77 gRC *gRC::getInstance()
82 static int gPainter_instances;
84 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
86 // ASSERT(!gPainter_instances);
97 void gPainter::setBackgroundColor(const gColor &color)
100 o.opcode = gOpcode::setBackgroundColor;
101 o.dc = m_dc.grabRef();
102 o.parm.setColor = new gOpcode::para::psetColor;
103 o.parm.setColor->color = color;
108 void gPainter::setForegroundColor(const gColor &color)
111 o.opcode = gOpcode::setForegroundColor;
112 o.dc = m_dc.grabRef();
113 o.parm.setColor = new gOpcode::para::psetColor;
114 o.parm.setColor->color = color;
119 void gPainter::setBackgroundColor(const gRGB &color)
122 o.opcode = gOpcode::setBackgroundColorRGB;
123 o.dc = m_dc.grabRef();
124 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
125 o.parm.setColorRGB->color = color;
130 void gPainter::setForegroundColor(const gRGB &color)
133 o.opcode = gOpcode::setForegroundColorRGB;
134 o.dc = m_dc.grabRef();
135 o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
136 o.parm.setColorRGB->color = color;
141 void gPainter::setFont(gFont *font)
144 o.opcode = gOpcode::setFont;
145 o.dc = m_dc.grabRef();
147 o.parm.setFont = new gOpcode::para::psetFont;
148 o.parm.setFont->font = font;
153 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
156 o.opcode=gOpcode::renderText;
157 o.dc = m_dc.grabRef();
158 o.parm.renderText = new gOpcode::para::prenderText;
159 o.parm.renderText->area = pos;
160 o.parm.renderText->text = string;
161 o.parm.renderText->flags = flags;
165 void gPainter::renderPara(eTextPara *para, ePoint offset)
168 o.opcode=gOpcode::renderPara;
169 o.dc = m_dc.grabRef();
170 o.parm.renderPara = new gOpcode::para::prenderPara;
171 o.parm.renderPara->offset = offset;
174 o.parm.renderPara->textpara = para;
178 void gPainter::fill(const eRect &area)
181 o.opcode=gOpcode::fill;
183 o.dc = m_dc.grabRef();
184 o.parm.fill = new gOpcode::para::pfillRect;
185 o.parm.fill->area = area;
189 void gPainter::fill(const gRegion ®ion)
192 o.opcode=gOpcode::fillRegion;
194 o.dc = m_dc.grabRef();
195 o.parm.fillRegion = new gOpcode::para::pfillRegion;
196 o.parm.fillRegion->region = region;
200 void gPainter::clear()
203 o.opcode=gOpcode::clear;
204 o.dc = m_dc.grabRef();
205 o.parm.fill = new gOpcode::para::pfillRect;
206 o.parm.fill->area = eRect();
210 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
214 o.opcode=gOpcode::blit;
215 o.dc = m_dc.grabRef();
217 o.parm.blit = new gOpcode::para::pblit;
218 o.parm.blit->pixmap = pixmap;
219 o.parm.blit->position = pos;
220 o.parm.blit->clip = clip;
226 void gPainter::setPalette(gRGB *colors, int start, int len)
229 o.opcode=gOpcode::setPalette;
230 o.dc = m_dc.grabRef();
231 gPalette *p=new gPalette;
233 o.parm.setPalette = new gOpcode::para::psetPalette;
234 p->data=new gRGB[len];
236 memcpy(p->data, colors, len*sizeof(gRGB));
239 o.parm.setPalette->palette = p;
243 void gPainter::setPalette(gPixmap *source)
246 setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
249 void gPainter::mergePalette(gPixmap *target)
252 o.opcode = gOpcode::mergePalette;
253 o.dc = m_dc.grabRef();
255 o.parm.mergePalette = new gOpcode::para::pmergePalette;
256 o.parm.mergePalette->target = target;
260 void gPainter::line(ePoint start, ePoint end)
263 o.opcode=gOpcode::line;
264 o.dc = m_dc.grabRef();
265 o.parm.line = new gOpcode::para::pline;
266 o.parm.line->start = start;
267 o.parm.line->end = end;
271 void gPainter::setOffset(ePoint val)
274 o.opcode=gOpcode::setOffset;
275 o.dc = m_dc.grabRef();
276 o.parm.setOffset = new gOpcode::para::psetOffset;
277 o.parm.setOffset->rel = 0;
278 o.parm.setOffset->value = val;
282 void gPainter::moveOffset(ePoint rel)
285 o.opcode=gOpcode::setOffset;
286 o.dc = m_dc.grabRef();
287 o.parm.setOffset = new gOpcode::para::psetOffset;
288 o.parm.setOffset->rel = 1;
289 o.parm.setOffset->value = rel;
293 void gPainter::resetOffset()
296 o.opcode=gOpcode::setOffset;
297 o.dc = m_dc.grabRef();
298 o.parm.setOffset = new gOpcode::para::psetOffset;
299 o.parm.setOffset->rel = 0;
300 o.parm.setOffset->value = ePoint(0, 0);
304 void gPainter::resetClip(const gRegion ®ion)
307 o.opcode = gOpcode::setClip;
308 o.dc = m_dc.grabRef();
309 o.parm.clip = new gOpcode::para::psetClip;
310 o.parm.clip->region = region;
314 void gPainter::clip(const gRegion ®ion)
317 o.opcode = gOpcode::addClip;
318 o.dc = m_dc.grabRef();
319 o.parm.clip = new gOpcode::para::psetClip;
320 o.parm.clip->region = region;
324 void gPainter::clippop()
327 o.opcode = gOpcode::popClip;
328 o.dc = m_dc.grabRef();
332 void gPainter::flush()
335 o.opcode = gOpcode::flush;
336 o.dc = m_dc.grabRef();
348 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
356 void gDC::exec(gOpcode *o)
360 case gOpcode::setBackgroundColor:
361 m_background_color = o->parm.setColor->color;
362 delete o->parm.setColor;
364 case gOpcode::setForegroundColor:
365 m_foreground_color = o->parm.setColor->color;
366 delete o->parm.setColor;
368 case gOpcode::setBackgroundColorRGB:
369 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
370 delete o->parm.setColorRGB;
372 case gOpcode::setForegroundColorRGB:
373 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
374 delete o->parm.setColorRGB;
376 case gOpcode::setFont:
377 m_current_font = o->parm.setFont->font;
378 o->parm.setFont->font->Release();
379 delete o->parm.setFont;
381 case gOpcode::renderText:
383 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
384 int flags = o->parm.renderText->flags;
385 assert(m_current_font);
386 para->setFont(m_current_font);
387 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
389 if (flags & gPainter::RT_HALIGN_RIGHT)
390 para->realign(eTextPara::dirRight);
391 else if (flags & gPainter::RT_HALIGN_CENTER)
392 para->realign(eTextPara::dirCenter);
393 else if (flags & gPainter::RT_HALIGN_BLOCK)
394 para->realign(eTextPara::dirBlock);
396 ePoint offset = m_current_offset;
398 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
400 eRect bbox = para->getBoundBox();
401 offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
403 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
404 delete o->parm.renderText;
407 case gOpcode::renderPara:
409 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
410 o->parm.renderPara->textpara->Release();
411 delete o->parm.renderPara;
416 eRect area = o->parm.fill->area;
417 area.moveBy(m_current_offset);
418 gRegion clip = m_current_clip & area;
419 m_pixmap->fill(clip, m_foreground_color);
423 case gOpcode::fillRegion:
425 o->parm.fillRegion->region.moveBy(m_current_offset);
426 gRegion clip = m_current_clip & o->parm.fillRegion->region;
427 m_pixmap->fill(clip, m_foreground_color);
428 delete o->parm.fillRegion;
432 m_pixmap->fill(m_current_clip, m_background_color);
438 // this code should be checked again but i'm too tired now
440 o->parm.blit->position += m_current_offset;
442 if (o->parm.blit->clip.valid())
444 o->parm.blit->clip.moveBy(m_current_offset);
445 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
447 clip = m_current_clip;
449 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
450 o->parm.blit->pixmap->Release();
454 case gOpcode::setPalette:
455 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
456 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
457 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
458 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
459 if (o->parm.setPalette->palette->colors)
460 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
462 delete[] o->parm.setPalette->palette->data;
463 delete o->parm.setPalette->palette;
464 delete o->parm.setPalette;
466 case gOpcode::mergePalette:
467 m_pixmap->mergePalette(*o->parm.mergePalette->target);
468 o->parm.mergePalette->target->Release();
469 delete o->parm.mergePalette;
473 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
474 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
478 case gOpcode::addClip:
479 m_clip_stack.push(m_current_clip);
480 o->parm.clip->region.moveBy(m_current_offset);
481 m_current_clip &= o->parm.clip->region;
484 case gOpcode::setClip:
485 o->parm.clip->region.moveBy(m_current_offset);
486 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
489 case gOpcode::popClip:
490 if (!m_clip_stack.empty())
492 m_current_clip = m_clip_stack.top();
496 case gOpcode::setOffset:
497 if (o->parm.setOffset->rel)
498 m_current_offset += o->parm.setOffset->value;
500 m_current_offset = o->parm.setOffset->value;
501 delete o->parm.setOffset;
506 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
510 gRGB gDC::getRGB(gColor col)
512 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
513 return gRGB(col, col, col);
516 eFatal("bla transp");
517 return gRGB(0, 0, 0, 0xFF);
519 return m_pixmap->surface->clut.data[col];
524 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");