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::mergePalette(gPixmap *target)
246 o.opcode = gOpcode::mergePalette;
247 o.dc = m_dc.grabRef();
249 o.parm.mergePalette = new gOpcode::para::pmergePalette;
250 o.parm.mergePalette->target = target;
254 void gPainter::line(ePoint start, ePoint end)
257 o.opcode=gOpcode::line;
258 o.dc = m_dc.grabRef();
259 o.parm.line = new gOpcode::para::pline;
260 o.parm.line->start = start;
261 o.parm.line->end = end;
265 void gPainter::setOffset(ePoint val)
268 o.opcode=gOpcode::setOffset;
269 o.dc = m_dc.grabRef();
270 o.parm.setOffset = new gOpcode::para::psetOffset;
271 o.parm.setOffset->rel = 0;
272 o.parm.setOffset->value = val;
276 void gPainter::moveOffset(ePoint rel)
279 o.opcode=gOpcode::setOffset;
280 o.dc = m_dc.grabRef();
281 o.parm.setOffset = new gOpcode::para::psetOffset;
282 o.parm.setOffset->rel = 1;
283 o.parm.setOffset->value = rel;
287 void gPainter::resetOffset()
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 = ePoint(0, 0);
298 void gPainter::resetClip(const gRegion ®ion)
301 o.opcode = gOpcode::setClip;
302 o.dc = m_dc.grabRef();
303 o.parm.clip = new gOpcode::para::psetClip;
304 o.parm.clip->region = region;
308 void gPainter::clip(const gRegion ®ion)
311 o.opcode = gOpcode::addClip;
312 o.dc = m_dc.grabRef();
313 o.parm.clip = new gOpcode::para::psetClip;
314 o.parm.clip->region = region;
318 void gPainter::clippop()
321 o.opcode = gOpcode::popClip;
322 o.dc = m_dc.grabRef();
326 void gPainter::flush()
329 o.opcode = gOpcode::flush;
330 o.dc = m_dc.grabRef();
342 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
350 void gDC::exec(gOpcode *o)
354 case gOpcode::setBackgroundColor:
355 m_background_color = o->parm.setColor->color;
356 delete o->parm.setColor;
358 case gOpcode::setForegroundColor:
359 m_foreground_color = o->parm.setColor->color;
360 delete o->parm.setColor;
362 case gOpcode::setBackgroundColorRGB:
363 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
364 delete o->parm.setColorRGB;
366 case gOpcode::setForegroundColorRGB:
367 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
368 delete o->parm.setColorRGB;
370 case gOpcode::setFont:
371 m_current_font = o->parm.setFont->font;
372 o->parm.setFont->font->Release();
373 delete o->parm.setFont;
375 case gOpcode::renderText:
377 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
378 int flags = o->parm.renderText->flags;
379 assert(m_current_font);
380 para->setFont(m_current_font);
381 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
383 if (flags & gPainter::RT_HALIGN_RIGHT)
384 para->realign(eTextPara::dirRight);
385 else if (flags & gPainter::RT_HALIGN_CENTER)
386 para->realign(eTextPara::dirCenter);
387 else if (flags & gPainter::RT_HALIGN_BLOCK)
388 para->realign(eTextPara::dirBlock);
390 ePoint offset = m_current_offset;
392 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
394 eRect bbox = para->getBoundBox();
395 offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
397 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
398 delete o->parm.renderText;
401 case gOpcode::renderPara:
403 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
404 o->parm.renderPara->textpara->Release();
405 delete o->parm.renderPara;
410 eRect area = o->parm.fill->area;
411 area.moveBy(m_current_offset);
412 gRegion clip = m_current_clip & area;
413 m_pixmap->fill(clip, m_foreground_color);
417 case gOpcode::fillRegion:
419 o->parm.fillRegion->region.moveBy(m_current_offset);
420 gRegion clip = m_current_clip & o->parm.fillRegion->region;
421 m_pixmap->fill(clip, m_foreground_color);
422 delete o->parm.fillRegion;
426 m_pixmap->fill(m_current_clip, m_background_color);
432 // this code should be checked again but i'm too tired now
434 o->parm.blit->position += m_current_offset;
436 if (o->parm.blit->clip.valid())
438 o->parm.blit->clip.moveBy(m_current_offset);
439 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
441 clip = m_current_clip;
443 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
444 o->parm.blit->pixmap->Release();
448 case gOpcode::setPalette:
449 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
450 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
451 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
452 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
453 if (o->parm.setPalette->palette->colors)
454 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
456 delete[] o->parm.setPalette->palette->data;
457 delete o->parm.setPalette->palette;
458 delete o->parm.setPalette;
460 case gOpcode::mergePalette:
461 m_pixmap->mergePalette(*o->parm.mergePalette->target);
462 o->parm.mergePalette->target->Release();
463 delete o->parm.mergePalette;
467 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
468 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
472 case gOpcode::addClip:
473 m_clip_stack.push(m_current_clip);
474 o->parm.clip->region.moveBy(m_current_offset);
475 m_current_clip &= o->parm.clip->region;
478 case gOpcode::setClip:
479 o->parm.clip->region.moveBy(m_current_offset);
480 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
483 case gOpcode::popClip:
484 if (!m_clip_stack.empty())
486 m_current_clip = m_clip_stack.top();
490 case gOpcode::setOffset:
491 if (o->parm.setOffset->rel)
492 m_current_offset += o->parm.setOffset->value;
494 m_current_offset = o->parm.setOffset->value;
495 delete o->parm.setOffset;
500 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
504 gRGB gDC::getRGB(gColor col)
506 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
507 return gRGB(col, col, col);
510 eFatal("bla transp");
511 return gRGB(0, 0, 0, 0xFF);
513 return m_pixmap->surface->clut.data[col];
518 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");