the epgcache can now handle more then one channel(transponder),
[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::setBackgroundColor(const gRGB &color)
120 {
121         gOpcode o;
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;
126         
127         m_rc->submit(o);
128 }
129
130 void gPainter::setForegroundColor(const gRGB &color)
131 {
132         gOpcode o;
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;
137         
138         m_rc->submit(o);
139 }
140
141 void gPainter::setFont(gFont *font)
142 {
143         gOpcode o;
144         o.opcode = gOpcode::setFont;
145         o.dc = m_dc.grabRef();
146         font->AddRef();
147         o.parm.setFont = new gOpcode::para::psetFont;
148         o.parm.setFont->font = font;
149         
150         m_rc->submit(o);
151 }
152
153 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
154 {
155         gOpcode o;
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;
162         m_rc->submit(o);
163 }
164
165 void gPainter::renderPara(eTextPara *para, ePoint offset)
166 {
167         gOpcode o;
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;
172
173         para->AddRef();
174         o.parm.renderPara->textpara = para;
175         m_rc->submit(o);
176 }
177
178 void gPainter::fill(const eRect &area)
179 {
180         gOpcode o;
181         o.opcode=gOpcode::fill;
182
183         o.dc = m_dc.grabRef();
184         o.parm.fill = new gOpcode::para::pfillRect;
185         o.parm.fill->area = area;
186         m_rc->submit(o);
187 }
188
189 void gPainter::fill(const gRegion &region)
190 {
191         gOpcode o;
192         o.opcode=gOpcode::fillRegion;
193
194         o.dc = m_dc.grabRef();
195         o.parm.fillRegion = new gOpcode::para::pfillRegion;
196         o.parm.fillRegion->region = region;
197         m_rc->submit(o);
198 }
199
200 void gPainter::clear()
201 {
202         gOpcode o;
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();
207         m_rc->submit(o);
208 }
209
210 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
211 {
212         gOpcode o;
213         
214         o.opcode=gOpcode::blit;
215         o.dc = m_dc.grabRef();
216         pixmap->AddRef();
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;
221         o.flags=flags;
222         m_rc->submit(o);
223 }
224
225
226 void gPainter::setPalette(gRGB *colors, int start, int len)
227 {
228         gOpcode o;
229         o.opcode=gOpcode::setPalette;
230         o.dc = m_dc.grabRef();
231         gPalette *p=new gPalette;
232         
233         o.parm.setPalette = new gOpcode::para::psetPalette;
234         p->data=new gRGB[len];
235         
236         memcpy(p->data, colors, len*sizeof(gRGB));
237         p->start=start;
238         p->colors=len;
239         o.parm.setPalette->palette = p;
240         m_rc->submit(o);
241 }
242
243 void gPainter::setPalette(gPixmap *source)
244 {
245         ASSERT(source);
246         setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
247 }
248
249 void gPainter::mergePalette(gPixmap *target)
250 {
251         gOpcode o;
252         o.opcode = gOpcode::mergePalette;
253         o.dc = m_dc.grabRef();
254         target->AddRef();
255         o.parm.mergePalette = new gOpcode::para::pmergePalette;
256         o.parm.mergePalette->target = target;
257         m_rc->submit(o);
258 }
259
260 void gPainter::line(ePoint start, ePoint end)
261 {
262         gOpcode o;
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;
268         m_rc->submit(o);
269 }
270
271 void gPainter::setOffset(ePoint val)
272 {
273         gOpcode o;
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;
279         m_rc->submit(o);
280 }
281
282 void gPainter::moveOffset(ePoint rel)
283 {
284         gOpcode o;
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;
290         m_rc->submit(o);
291 }
292
293 void gPainter::resetOffset()
294 {
295         gOpcode o;
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);
301         m_rc->submit(o);
302 }
303
304 void gPainter::resetClip(const gRegion &region)
305 {
306         gOpcode o;
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;
311         m_rc->submit(o);
312 }
313
314 void gPainter::clip(const gRegion &region)
315 {
316         gOpcode o;
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;
321         m_rc->submit(o);
322 }
323
324 void gPainter::clippop()
325 {
326         gOpcode o;
327         o.opcode = gOpcode::popClip;
328         o.dc = m_dc.grabRef();
329         m_rc->submit(o);
330 }
331
332 void gPainter::flush()
333 {
334         gOpcode o;
335         o.opcode = gOpcode::flush;
336         o.dc = m_dc.grabRef();
337         m_rc->submit(o);
338 }
339
340 void gPainter::end()
341 {
342 }
343
344 gDC::gDC()
345 {
346 }
347
348 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
349 {
350 }
351
352 gDC::~gDC()
353 {
354 }
355
356 void gDC::exec(gOpcode *o)
357 {
358         switch (o->opcode)
359         {
360         case gOpcode::setBackgroundColor:
361                 m_background_color = o->parm.setColor->color;
362                 delete o->parm.setColor;
363                 break;
364         case gOpcode::setForegroundColor:
365                 m_foreground_color = o->parm.setColor->color;
366                 delete o->parm.setColor;
367                 break;
368         case gOpcode::setBackgroundColorRGB:
369                 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
370                 delete o->parm.setColorRGB;
371                 break;
372         case gOpcode::setForegroundColorRGB:
373                 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
374                 delete o->parm.setColorRGB;
375                 break;
376         case gOpcode::setFont:
377                 m_current_font = o->parm.setFont->font;
378                 o->parm.setFont->font->Release();
379                 delete o->parm.setFont;
380                 break;
381         case gOpcode::renderText:
382         {
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);
388                 
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);
395                 
396                 ePoint offset = m_current_offset;
397                 
398                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
399                 {
400                         eRect bbox = para->getBoundBox();
401                         offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
402                 }
403                 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
404                 delete o->parm.renderText;
405                 break;
406         }
407         case gOpcode::renderPara:
408         {
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;
412                 break;
413         }
414         case gOpcode::fill:
415         {
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);
420                 delete o->parm.fill;
421                 break;
422         }
423         case gOpcode::fillRegion:
424         {
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;
429                 break;
430         }
431         case gOpcode::clear:
432                 m_pixmap->fill(m_current_clip, m_background_color);
433                 delete o->parm.fill;
434                 break;
435         case gOpcode::blit:
436         {
437                 gRegion clip;
438                                 // this code should be checked again but i'm too tired now
439                 
440                 o->parm.blit->position += m_current_offset;
441                 
442                 if (o->parm.blit->clip.valid())
443                 {
444                         o->parm.blit->clip.moveBy(m_current_offset);
445                         clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
446                 } else
447                         clip = m_current_clip;
448                 
449                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
450                 o->parm.blit->pixmap->Release();
451                 delete o->parm.blit;
452                 break;
453         }
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));
461                 
462                 delete[] o->parm.setPalette->palette->data;
463                 delete o->parm.setPalette->palette;
464                 delete o->parm.setPalette;
465                 break;
466         case gOpcode::mergePalette:
467                 m_pixmap->mergePalette(*o->parm.mergePalette->target);
468                 o->parm.mergePalette->target->Release();
469                 delete o->parm.mergePalette;
470                 break; 
471         case gOpcode::line:
472         {
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);
475                 delete o->parm.line;
476                 break;
477         }
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;
482                 delete o->parm.clip;
483                 break;
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());
487                 delete o->parm.clip;
488                 break;
489         case gOpcode::popClip:
490                 if (!m_clip_stack.empty())
491                 {
492                         m_current_clip = m_clip_stack.top();
493                         m_clip_stack.pop();
494                 }
495                 break;
496         case gOpcode::setOffset:
497                 if (o->parm.setOffset->rel)
498                         m_current_offset += o->parm.setOffset->value;
499                 else
500                         m_current_offset  = o->parm.setOffset->value;
501                 delete o->parm.setOffset;
502                 break;
503         case gOpcode::flush:
504                 break;
505         default:
506                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
507         }
508 }
509
510 gRGB gDC::getRGB(gColor col)
511 {
512         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
513                 return gRGB(col, col, col);
514         if (col<0)
515         {
516                 eFatal("bla transp");
517                 return gRGB(0, 0, 0, 0xFF);
518         }
519         return m_pixmap->surface->clut.data[col];
520 }
521
522 DEFINE_REF(gDC);
523
524 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");
525