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;
48 eDebug("waiting for gRC thread shutdown");
49 pthread_join(the_thread, 0);
50 eDebug("gRC thread has finished");
62 gOpcode& o(queue.current());
63 if (o.opcode==gOpcode::shutdown)
75 gRC *gRC::getInstance()
80 static int gPainter_instances;
82 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
84 // ASSERT(!gPainter_instances);
95 void gPainter::setBackgroundColor(const gColor &color)
98 o.opcode = gOpcode::setBackgroundColor;
99 o.dc = m_dc.grabRef();
100 o.parm.setColor = new gOpcode::para::psetColor;
101 o.parm.setColor->color = color;
106 void gPainter::setForegroundColor(const gColor &color)
109 o.opcode = gOpcode::setForegroundColor;
110 o.dc = m_dc.grabRef();
111 o.parm.setColor = new gOpcode::para::psetColor;
112 o.parm.setColor->color = color;
117 void gPainter::setFont(gFont *font)
120 o.opcode = gOpcode::setFont;
121 o.dc = m_dc.grabRef();
123 o.parm.setFont = new gOpcode::para::psetFont;
124 o.parm.setFont->font = font;
129 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
132 o.opcode=gOpcode::renderText;
133 o.dc = m_dc.grabRef();
134 o.parm.renderText = new gOpcode::para::prenderText;
135 o.parm.renderText->area = pos;
136 o.parm.renderText->text = string;
137 o.parm.renderText->flags = flags;
141 void gPainter::renderPara(eTextPara *para, ePoint offset)
144 o.opcode=gOpcode::renderPara;
145 o.dc = m_dc.grabRef();
146 o.parm.renderPara = new gOpcode::para::prenderPara;
147 o.parm.renderPara->offset = offset;
150 o.parm.renderPara->textpara = para;
154 void gPainter::fill(const eRect &area)
157 o.opcode=gOpcode::fill;
159 o.dc = m_dc.grabRef();
160 o.parm.fill = new gOpcode::para::pfillRect;
161 o.parm.fill->area = area;
165 void gPainter::clear()
168 o.opcode=gOpcode::clear;
169 o.dc = m_dc.grabRef();
170 o.parm.fill = new gOpcode::para::pfillRect;
171 o.parm.fill->area = eRect();
175 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
179 o.opcode=gOpcode::blit;
180 o.dc = m_dc.grabRef();
182 o.parm.blit = new gOpcode::para::pblit;
183 o.parm.blit->pixmap = pixmap;
184 o.parm.blit->position = pos;
185 o.parm.blit->clip = clip;
191 void gPainter::setPalette(gRGB *colors, int start, int len)
194 o.opcode=gOpcode::setPalette;
195 o.dc = m_dc.grabRef();
196 gPalette *p=new gPalette;
198 o.parm.setPalette = new gOpcode::para::psetPalette;
199 p->data=new gRGB[len];
201 memcpy(p->data, colors, len*sizeof(gRGB));
204 o.parm.setPalette->palette = p;
208 void gPainter::mergePalette(gPixmap *target)
211 o.opcode=gOpcode::mergePalette;
212 o.dc = m_dc.grabRef();
214 o.parm.mergePalette->target = target;
218 void gPainter::line(ePoint start, ePoint end)
221 o.opcode=gOpcode::line;
222 o.dc = m_dc.grabRef();
223 o.parm.line = new gOpcode::para::pline;
224 o.parm.line->start = start;
225 o.parm.line->end = end;
229 void gPainter::setOffset(ePoint val)
232 o.opcode=gOpcode::setOffset;
233 o.dc = m_dc.grabRef();
234 o.parm.setOffset = new gOpcode::para::psetOffset;
235 o.parm.setOffset->rel = 0;
236 o.parm.setOffset->value = val;
240 void gPainter::moveOffset(ePoint rel)
243 o.opcode=gOpcode::setOffset;
244 o.dc = m_dc.grabRef();
245 o.parm.setOffset = new gOpcode::para::psetOffset;
246 o.parm.setOffset->rel = 1;
247 o.parm.setOffset->value = rel;
251 void gPainter::resetOffset()
254 o.opcode=gOpcode::setOffset;
255 o.dc = m_dc.grabRef();
256 o.parm.setOffset = new gOpcode::para::psetOffset;
257 o.parm.setOffset->value = ePoint(0, 0);
261 void gPainter::resetClip(const gRegion ®ion)
264 o.opcode = gOpcode::setClip;
265 o.dc = m_dc.grabRef();
266 o.parm.clip = new gOpcode::para::psetClip;
267 o.parm.clip->region = region;
271 void gPainter::clip(const gRegion ®ion)
274 o.opcode = gOpcode::addClip;
275 o.dc = m_dc.grabRef();
276 o.parm.clip = new gOpcode::para::psetClip;
277 o.parm.clip->region = region;
281 void gPainter::clippop()
284 o.opcode = gOpcode::popClip;
285 o.dc = m_dc.grabRef();
289 void gPainter::flush()
301 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
309 void gDC::exec(gOpcode *o)
313 case gOpcode::setBackgroundColor:
314 m_background_color = o->parm.setColor->color;
315 delete o->parm.setColor;
317 case gOpcode::setForegroundColor:
318 m_foreground_color = o->parm.setColor->color;
319 delete o->parm.setColor;
321 case gOpcode::setFont:
322 m_current_font = o->parm.setFont->font;
323 o->parm.setFont->font->Release();
324 delete o->parm.setFont;
326 case gOpcode::renderText:
328 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
329 para->setFont(m_current_font);
330 para->renderString(o->parm.renderText->text, o->parm.renderText->flags);
331 para->blit(*this, ePoint(0, 0), getRGB(m_foreground_color), getRGB(m_background_color));
332 delete o->parm.renderText;
335 case gOpcode::renderPara:
337 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset, getRGB(m_foreground_color), getRGB(m_background_color));
338 o->parm.renderPara->textpara->Release();
339 delete o->parm.renderPara;
344 eRect area = o->parm.fill->area;
345 area.moveBy(m_current_offset);
346 gRegion clip = m_current_clip & area;
347 m_pixmap->fill(clip, m_foreground_color);
352 m_pixmap->fill(m_current_clip, m_background_color);
358 if (!o->parm.blit->clip.isValid())
360 clip.intersect(gRegion(o->parm.blit->clip), clip);
362 clip = m_current_clip;
363 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
364 o->parm.blit->pixmap->Release();
368 case gOpcode::setPalette:
369 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
370 o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
371 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
372 o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
373 if (o->parm.setPalette->palette->colors)
374 memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
376 delete[] o->parm.setPalette->palette->data;
377 delete o->parm.setPalette->palette;
378 delete o->parm.setPalette;
380 case gOpcode::mergePalette:
382 pixmap->mergePalette(*o->parm.blit->pixmap);
383 o->parm.blit->pixmap->unlock();
389 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
390 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
394 case gOpcode::addClip:
395 m_clip_stack.push(m_current_clip);
396 o->parm.clip->region.moveBy(m_current_offset);
397 m_current_clip &= o->parm.clip->region;
400 case gOpcode::setClip:
401 o->parm.clip->region.moveBy(m_current_offset);
402 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->getSize());
405 case gOpcode::popClip:
406 if (!m_clip_stack.empty())
408 m_current_clip = m_clip_stack.top();
412 case gOpcode::setOffset:
413 if (o->parm.setOffset->rel)
414 m_current_offset += o->parm.setOffset->value;
416 m_current_offset = o->parm.setOffset->value;
417 delete o->parm.setOffset;
420 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
424 gRGB gDC::getRGB(gColor col)
426 if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
427 return gRGB(col, col, col);
430 eFatal("bla transp");
431 return gRGB(0, 0, 0, 0xFF);
433 return m_pixmap->surface->clut.data[col];
438 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");