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 offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
447 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
448 delete o->parm.renderText;
451 case gOpcode::renderPara:
453 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
454 o->parm.renderPara->textpara->Release();
455 delete o->parm.renderPara;
460 eRect area = o->parm.fill->area;
461 area.moveBy(m_current_offset);
462 gRegion clip = m_current_clip & area;
463 m_pixmap->fill(clip, m_foreground_color);
467 case gOpcode::fillRegion:
469 o->parm.fillRegion->region.moveBy(m_current_offset);
470 gRegion clip = m_current_clip & o->parm.fillRegion->region;
471 m_pixmap->fill(clip, m_foreground_color);
472 delete o->parm.fillRegion;
476 m_pixmap->fill(m_current_clip, m_background_color);
482 // this code should be checked again but i'm too tired now
484 o->parm.blit->position += m_current_offset;
486 if (o->parm.blit->clip.valid())
488 o->parm.blit->clip.moveBy(m_current_offset);
489 clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
491 clip = m_current_clip;
493 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
494 o->parm.blit->pixmap->Release();
498 case gOpcode::setPalette:
499 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
500 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
501 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
502 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
503 if (o->parm.setPalette->palette->colors)
504 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
506 delete[] o->parm.setPalette->palette->data;
507 delete o->parm.setPalette->palette;
508 delete o->parm.setPalette;
510 case gOpcode::mergePalette:
511 m_pixmap->mergePalette(*o->parm.mergePalette->target);
512 o->parm.mergePalette->target->Release();
513 delete o->parm.mergePalette;
517 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
518 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
522 case gOpcode::addClip:
523 m_clip_stack.push(m_current_clip);
524 o->parm.clip->region.moveBy(m_current_offset);
525 m_current_clip &= o->parm.clip->region;
528 case gOpcode::setClip:
529 o->parm.clip->region.moveBy(m_current_offset);
530 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
533 case gOpcode::popClip:
534 if (!m_clip_stack.empty())
536 m_current_clip = m_clip_stack.top();
540 case gOpcode::setOffset:
541 if (o->parm.setOffset->rel)
542 m_current_offset += o->parm.setOffset->value;
544 m_current_offset = o->parm.setOffset->value;
545 delete o->parm.setOffset;
547 case gOpcode::waitVSync:
554 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
558 gRGB gDC::getRGB(gColor col)
560 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
561 return gRGB(col, col, col);
564 eFatal("bla transp");
565 return gRGB(0, 0, 0, 0xFF);
567 return m_pixmap->surface->clut.data[col];
572 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");