- fixed console input mode restore
[enigma2.git] / lib / gdi / grc.cpp
1 // for debugging use:
2 // #define SYNC_PAINT
3 #include <unistd.h>
4 #ifndef SYNC_PAINT
5 #include <pthread.h>
6 #endif
7
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>
12
13 #define MAXSIZE 1024
14
15 #ifndef SYNC_PAINT
16 void *gRC::thread_wrapper(void *ptr)
17 {
18         nice(3);
19         return ((gRC*)ptr)->thread();
20 }
21 #endif
22
23 gRC *gRC::instance=0;
24
25 gRC::gRC(): queue(2048), queuelock(MAXSIZE)
26 {
27         ASSERT(!instance);
28         instance=this;
29         queuelock.lock(MAXSIZE);
30 #ifndef SYNC_PAINT
31         int res = pthread_create(&the_thread, 0, thread_wrapper, this);
32         if (res)
33                 eFatal("RC thread couldn't be created");
34         else
35                 eDebug("RC thread createted successfully");
36 #endif
37 }
38
39 DEFINE_REF(gRC);
40
41 gRC::~gRC()
42 {
43         instance=0;
44
45         gOpcode o;
46         o.opcode=gOpcode::shutdown;
47         submit(o);
48 #ifndef SYNC_PAINT
49         eDebug("waiting for gRC thread shutdown");
50         pthread_join(the_thread, 0);
51         eDebug("gRC thread has finished");
52 #endif
53 }
54
55 void *gRC::thread()
56 {
57 #ifndef SYNC_PAINT
58         while (1)
59 #else
60         while (queue.size())
61 #endif
62         {
63                 queuelock.lock(1);
64                 gOpcode& o(queue.current());
65                 if (o.opcode==gOpcode::shutdown)
66                         break;
67                 o.dc->exec(&o);
68                 o.dc->Release();
69                 queue.dequeue();
70         }
71 #ifndef SYNC_PAINT
72         pthread_exit(0);
73 #endif
74         return 0;
75 }
76
77 gRC *gRC::getInstance()
78 {
79         return instance;
80 }
81
82 static int gPainter_instances;
83
84 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
85 {
86 //      ASSERT(!gPainter_instances);
87         gPainter_instances++;
88 //      begin(rect);
89 }
90
91 gPainter::~gPainter()
92 {
93         end();
94         gPainter_instances--;
95 }
96
97 void gPainter::setBackgroundColor(const gColor &color)
98 {
99         gOpcode o;
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;
104         
105         m_rc->submit(o);
106 }
107
108 void gPainter::setForegroundColor(const gColor &color)
109 {
110         gOpcode o;
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;
115         
116         m_rc->submit(o);
117 }
118
119 void gPainter::setFont(gFont *font)
120 {
121         gOpcode o;
122         o.opcode = gOpcode::setFont;
123         o.dc = m_dc.grabRef();
124         font->AddRef();
125         o.parm.setFont = new gOpcode::para::psetFont;
126         o.parm.setFont->font = font;
127         
128         m_rc->submit(o);
129 }
130
131 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
132 {
133         gOpcode o;
134         o.opcode=gOpcode::renderText;
135         o.dc = m_dc.grabRef();
136         o.parm.renderText = new gOpcode::para::prenderText;
137         o.parm.renderText->area = pos;
138         o.parm.renderText->text = string;
139         o.parm.renderText->flags = flags;
140         m_rc->submit(o);
141 }
142
143 void gPainter::renderPara(eTextPara *para, ePoint offset)
144 {
145         gOpcode o;
146         o.opcode=gOpcode::renderPara;
147         o.dc = m_dc.grabRef();
148         o.parm.renderPara = new gOpcode::para::prenderPara;
149         o.parm.renderPara->offset = offset;
150
151         para->AddRef();
152         o.parm.renderPara->textpara = para;
153         m_rc->submit(o);
154 }
155
156 void gPainter::fill(const eRect &area)
157 {
158         gOpcode o;
159         o.opcode=gOpcode::fill;
160
161         o.dc = m_dc.grabRef();
162         o.parm.fill = new gOpcode::para::pfillRect;
163         o.parm.fill->area = area;
164         m_rc->submit(o);
165 }
166
167 void gPainter::fill(const gRegion &region)
168 {
169         gOpcode o;
170         o.opcode=gOpcode::fillRegion;
171
172         o.dc = m_dc.grabRef();
173         o.parm.fillRegion = new gOpcode::para::pfillRegion;
174         o.parm.fillRegion->region = region;
175         m_rc->submit(o);
176 }
177
178 void gPainter::clear()
179 {
180         gOpcode o;
181         o.opcode=gOpcode::clear;
182         o.dc = m_dc.grabRef();
183         o.parm.fill = new gOpcode::para::pfillRect;
184         o.parm.fill->area = eRect();
185         m_rc->submit(o);
186 }
187
188 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
189 {
190         gOpcode o;
191
192         o.opcode=gOpcode::blit;
193         o.dc = m_dc.grabRef();
194         pixmap->AddRef();
195         o.parm.blit  = new gOpcode::para::pblit;
196         o.parm.blit->pixmap = pixmap;
197         o.parm.blit->position = pos;
198         o.parm.blit->clip = clip;
199         o.flags=flags;
200         m_rc->submit(o);
201 }
202
203
204 void gPainter::setPalette(gRGB *colors, int start, int len)
205 {
206         gOpcode o;
207         o.opcode=gOpcode::setPalette;
208         o.dc = m_dc.grabRef();
209         gPalette *p=new gPalette;
210         
211         o.parm.setPalette = new gOpcode::para::psetPalette;
212         p->data=new gRGB[len];
213         
214         memcpy(p->data, colors, len*sizeof(gRGB));
215         p->start=start;
216         p->colors=len;
217         o.parm.setPalette->palette = p;
218         m_rc->submit(o);
219 }
220
221 void gPainter::mergePalette(gPixmap *target)
222 {
223         gOpcode o;
224         o.opcode=gOpcode::mergePalette;
225         o.dc = m_dc.grabRef();
226         target->AddRef();
227         o.parm.mergePalette->target = target;
228         m_rc->submit(o);
229 }
230
231 void gPainter::line(ePoint start, ePoint end)
232 {
233         gOpcode o;
234         o.opcode=gOpcode::line;
235         o.dc = m_dc.grabRef();
236         o.parm.line = new gOpcode::para::pline;
237         o.parm.line->start = start;
238         o.parm.line->end = end;
239         m_rc->submit(o);
240 }
241
242 void gPainter::setOffset(ePoint val)
243 {
244         gOpcode o;
245         o.opcode=gOpcode::setOffset;
246         o.dc = m_dc.grabRef();
247         o.parm.setOffset = new gOpcode::para::psetOffset;
248         o.parm.setOffset->rel = 0;
249         o.parm.setOffset->value = val;
250         m_rc->submit(o);
251 }
252
253 void gPainter::moveOffset(ePoint rel)
254 {
255         gOpcode o;
256         o.opcode=gOpcode::setOffset;
257         o.dc = m_dc.grabRef();
258         o.parm.setOffset = new gOpcode::para::psetOffset;
259         o.parm.setOffset->rel = 1;
260         o.parm.setOffset->value = rel;
261         m_rc->submit(o);
262 }
263
264 void gPainter::resetOffset()
265 {
266         gOpcode o;
267         o.opcode=gOpcode::setOffset;
268         o.dc = m_dc.grabRef();
269         o.parm.setOffset = new gOpcode::para::psetOffset;
270         o.parm.setOffset->rel = 0;
271         o.parm.setOffset->value = ePoint(0, 0);
272         m_rc->submit(o);
273 }
274
275 void gPainter::resetClip(const gRegion &region)
276 {
277         gOpcode o;
278         o.opcode = gOpcode::setClip;
279         o.dc = m_dc.grabRef();
280         o.parm.clip = new gOpcode::para::psetClip;
281         o.parm.clip->region = region;
282         m_rc->submit(o);
283 }
284
285 void gPainter::clip(const gRegion &region)
286 {
287         gOpcode o;
288         o.opcode = gOpcode::addClip;
289         o.dc = m_dc.grabRef();
290         o.parm.clip = new gOpcode::para::psetClip;
291         o.parm.clip->region = region;
292         m_rc->submit(o);
293 }
294
295 void gPainter::clippop()
296 {
297         gOpcode o;
298         o.opcode = gOpcode::popClip;
299         o.dc = m_dc.grabRef();
300         m_rc->submit(o);
301 }
302
303 void gPainter::flush()
304 {
305 }
306
307 void gPainter::end()
308 {
309 }
310
311 gDC::gDC()
312 {
313 }
314
315 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
316 {
317 }
318
319 gDC::~gDC()
320 {
321 }
322
323 void gDC::exec(gOpcode *o)
324 {
325         switch (o->opcode)
326         {
327         case gOpcode::setBackgroundColor:
328                 m_background_color = o->parm.setColor->color;
329                 delete o->parm.setColor;
330                 break;
331         case gOpcode::setForegroundColor:
332                 m_foreground_color = o->parm.setColor->color;
333                 delete o->parm.setColor;
334                 break;
335         case gOpcode::setFont:
336                 m_current_font = o->parm.setFont->font;
337                 o->parm.setFont->font->Release();
338                 delete o->parm.setFont;
339                 break;
340         case gOpcode::renderText:
341         {
342                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
343                 assert(m_current_font);
344                 para->setFont(m_current_font);
345                 para->renderString(o->parm.renderText->text, 0);
346                 
347                 if (o->parm.renderText->flags & gPainter::RT_HALIGN_RIGHT)
348                         para->realign(eTextPara::dirRight);
349                 else if (o->parm.renderText->flags & gPainter::RT_HALIGN_CENTER)
350                         para->realign(eTextPara::dirCenter);
351                 else if (o->parm.renderText->flags & gPainter::RT_HALIGN_BLOCK)
352                         para->realign(eTextPara::dirBlock);
353                 
354                 ePoint offset = m_current_offset;
355                 
356                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
357                 {
358                         eRect bbox = para->getBoundBox();
359                         offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
360                 }
361                 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
362                 delete o->parm.renderText;
363                 break;
364         }
365         case gOpcode::renderPara:
366         {
367                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
368                 o->parm.renderPara->textpara->Release();
369                 delete o->parm.renderPara;
370                 break;
371         }
372         case gOpcode::fill:
373         {
374                 eRect area = o->parm.fill->area;
375                 area.moveBy(m_current_offset);
376                 gRegion clip = m_current_clip & area;
377                 m_pixmap->fill(clip, m_foreground_color);
378                 delete o->parm.fill;
379                 break;
380         }
381         case gOpcode::fillRegion:
382         {
383                 o->parm.fillRegion->region.moveBy(m_current_offset);
384                 gRegion clip = m_current_clip & o->parm.fillRegion->region;
385                 m_pixmap->fill(clip, m_foreground_color);
386                 delete o->parm.fillRegion;
387                 break;
388         }
389         case gOpcode::clear:
390                 m_pixmap->fill(m_current_clip, m_background_color);
391                 delete o->parm.fill;
392                 break;
393         case gOpcode::blit:
394         {
395                 gRegion clip;
396                 if (!o->parm.blit->clip.valid())
397                 {
398                         clip.intersect(gRegion(o->parm.blit->clip), clip);
399                 } else
400                         clip = m_current_clip;
401                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
402                 o->parm.blit->pixmap->Release();
403                 delete o->parm.blit;
404                 break;
405         }
406         case gOpcode::setPalette:
407                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
408                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
409                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
410                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
411                 if (o->parm.setPalette->palette->colors)
412                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
413                 
414                 delete[] o->parm.setPalette->palette->data;
415                 delete o->parm.setPalette->palette;
416                 delete o->parm.setPalette;
417                 break;
418         case gOpcode::mergePalette:
419 #if 0
420                 pixmap->mergePalette(*o->parm.blit->pixmap);
421                 o->parm.blit->pixmap->unlock();
422                 delete o->parm.blit;
423 #endif
424                 break;
425         case gOpcode::line:
426         {
427                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
428                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
429                 delete o->parm.line;
430                 break;
431         }
432         case gOpcode::addClip:
433                 m_clip_stack.push(m_current_clip);
434                 o->parm.clip->region.moveBy(m_current_offset);
435                 m_current_clip &= o->parm.clip->region;
436                 delete o->parm.clip;
437                 break;
438         case gOpcode::setClip:
439                 o->parm.clip->region.moveBy(m_current_offset);
440                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->getSize());
441                 delete o->parm.clip;
442                 break;
443         case gOpcode::popClip:
444                 if (!m_clip_stack.empty())
445                 {
446                         m_current_clip = m_clip_stack.top();
447                         m_clip_stack.pop();
448                 }
449                 break;
450         case gOpcode::setOffset:
451                 if (o->parm.setOffset->rel)
452                         m_current_offset += o->parm.setOffset->value;
453                 else
454                         m_current_offset  = o->parm.setOffset->value;
455                 delete o->parm.setOffset;
456                 break;
457         default:
458                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
459         }
460 }
461
462 gRGB gDC::getRGB(gColor col)
463 {
464         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
465                 return gRGB(col, col, col);
466         if (col<0)
467         {
468                 eFatal("bla transp");
469                 return gRGB(0, 0, 0, 0xFF);
470         }
471         return m_pixmap->surface->clut.data[col];
472 }
473
474 DEFINE_REF(gDC);
475
476 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");
477