fix: e2 didn't start when no sat-frontend was detected
[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), m_notify_pump(eApp, 0), queuelock(MAXSIZE)
26 {
27         ASSERT(!instance);
28         instance=this;
29         queuelock.lock(MAXSIZE);
30         CONNECT(m_notify_pump.recv_msg, gRC::recv_notify);
31 #ifndef SYNC_PAINT
32         int res = pthread_create(&the_thread, 0, thread_wrapper, this);
33         if (res)
34                 eFatal("RC thread couldn't be created");
35         else
36                 eDebug("RC thread created successfully");
37 #endif
38 }
39
40 DEFINE_REF(gRC);
41
42 gRC::~gRC()
43 {
44         instance=0;
45
46         gOpcode o;
47         o.opcode=gOpcode::shutdown;
48         submit(o);
49 #ifndef SYNC_PAINT
50         eDebug("waiting for gRC thread shutdown");
51         pthread_join(the_thread, 0);
52         eDebug("gRC thread has finished");
53 #endif
54 }
55
56 void *gRC::thread()
57 {
58         int need_notify = 0;
59 #ifndef SYNC_PAINT
60         while (1)
61 #else
62         while (queue.size())
63 #endif
64         {
65                 queuelock.lock(1);
66                 gOpcode& o(queue.current());
67                 if (o.opcode==gOpcode::shutdown)
68                         break;
69                 if (o.opcode==gOpcode::notify)
70                         need_notify = 1;
71                 else
72                         o.dc->exec(&o);
73                 o.dc->Release();
74                 queue.dequeue();
75
76                 if ((!queue.size()) && need_notify)
77                 {
78                         need_notify = 0;
79                         m_notify_pump.send(1);
80                 }
81         }
82 #ifndef SYNC_PAINT
83         pthread_exit(0);
84 #endif
85         return 0;
86 }
87
88 void gRC::recv_notify(const int &i)
89 {
90         notify();
91 }
92
93 gRC *gRC::getInstance()
94 {
95         return instance;
96 }
97
98 static int gPainter_instances;
99
100 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
101 {
102 //      ASSERT(!gPainter_instances);
103         gPainter_instances++;
104 //      begin(rect);
105 }
106
107 gPainter::~gPainter()
108 {
109         end();
110         gPainter_instances--;
111 }
112
113 void gPainter::setBackgroundColor(const gColor &color)
114 {
115         gOpcode o;
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;
120         
121         m_rc->submit(o);
122 }
123
124 void gPainter::setForegroundColor(const gColor &color)
125 {
126         gOpcode o;
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;
131         
132         m_rc->submit(o);
133 }
134
135 void gPainter::setBackgroundColor(const gRGB &color)
136 {
137         gOpcode o;
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;
142         
143         m_rc->submit(o);
144 }
145
146 void gPainter::setForegroundColor(const gRGB &color)
147 {
148         gOpcode o;
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;
153         
154         m_rc->submit(o);
155 }
156
157 void gPainter::setFont(gFont *font)
158 {
159         gOpcode o;
160         o.opcode = gOpcode::setFont;
161         o.dc = m_dc.grabRef();
162         font->AddRef();
163         o.parm.setFont = new gOpcode::para::psetFont;
164         o.parm.setFont->font = font;
165         
166         m_rc->submit(o);
167 }
168
169 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
170 {
171         gOpcode o;
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;
178         m_rc->submit(o);
179 }
180
181 void gPainter::renderPara(eTextPara *para, ePoint offset)
182 {
183         gOpcode o;
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;
188
189         para->AddRef();
190         o.parm.renderPara->textpara = para;
191         m_rc->submit(o);
192 }
193
194 void gPainter::fill(const eRect &area)
195 {
196         gOpcode o;
197         o.opcode=gOpcode::fill;
198
199         o.dc = m_dc.grabRef();
200         o.parm.fill = new gOpcode::para::pfillRect;
201         o.parm.fill->area = area;
202         m_rc->submit(o);
203 }
204
205 void gPainter::fill(const gRegion &region)
206 {
207         gOpcode o;
208         o.opcode=gOpcode::fillRegion;
209
210         o.dc = m_dc.grabRef();
211         o.parm.fillRegion = new gOpcode::para::pfillRegion;
212         o.parm.fillRegion->region = region;
213         m_rc->submit(o);
214 }
215
216 void gPainter::clear()
217 {
218         gOpcode o;
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();
223         m_rc->submit(o);
224 }
225
226 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
227 {
228         gOpcode o;
229         
230         o.opcode=gOpcode::blit;
231         o.dc = m_dc.grabRef();
232         pixmap->AddRef();
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;
238         m_rc->submit(o);
239 }
240
241
242 void gPainter::setPalette(gRGB *colors, int start, int len)
243 {
244         gOpcode o;
245         o.opcode=gOpcode::setPalette;
246         o.dc = m_dc.grabRef();
247         gPalette *p=new gPalette;
248         
249         o.parm.setPalette = new gOpcode::para::psetPalette;
250         p->data=new gRGB[len];
251         
252         memcpy(p->data, colors, len*sizeof(gRGB));
253         p->start=start;
254         p->colors=len;
255         o.parm.setPalette->palette = p;
256         m_rc->submit(o);
257 }
258
259 void gPainter::setPalette(gPixmap *source)
260 {
261         ASSERT(source);
262         setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
263 }
264
265 void gPainter::mergePalette(gPixmap *target)
266 {
267         gOpcode o;
268         o.opcode = gOpcode::mergePalette;
269         o.dc = m_dc.grabRef();
270         target->AddRef();
271         o.parm.mergePalette = new gOpcode::para::pmergePalette;
272         o.parm.mergePalette->target = target;
273         m_rc->submit(o);
274 }
275
276 void gPainter::line(ePoint start, ePoint end)
277 {
278         gOpcode o;
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;
284         m_rc->submit(o);
285 }
286
287 void gPainter::setOffset(ePoint val)
288 {
289         gOpcode o;
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;
295         m_rc->submit(o);
296 }
297
298 void gPainter::moveOffset(ePoint rel)
299 {
300         gOpcode o;
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;
306         m_rc->submit(o);
307 }
308
309 void gPainter::resetOffset()
310 {
311         gOpcode o;
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);
317         m_rc->submit(o);
318 }
319
320 void gPainter::resetClip(const gRegion &region)
321 {
322         gOpcode o;
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;
327         m_rc->submit(o);
328 }
329
330 void gPainter::clip(const gRegion &region)
331 {
332         gOpcode o;
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;
337         m_rc->submit(o);
338 }
339
340 void gPainter::clippop()
341 {
342         gOpcode o;
343         o.opcode = gOpcode::popClip;
344         o.dc = m_dc.grabRef();
345         m_rc->submit(o);
346 }
347
348 void gPainter::flush()
349 {
350         gOpcode o;
351         o.opcode = gOpcode::flush;
352         o.dc = m_dc.grabRef();
353         m_rc->submit(o);
354 }
355
356 void gPainter::waitVSync()
357 {
358         gOpcode o;
359         o.opcode = gOpcode::waitVSync;
360         o.dc = m_dc.grabRef();
361         m_rc->submit(o);
362 }
363
364 void gPainter::flip()
365 {
366         gOpcode o;
367         o.opcode = gOpcode::flip;
368         o.dc = m_dc.grabRef();
369         m_rc->submit(o);
370 }
371
372 void gPainter::notify()
373 {
374         gOpcode o;
375         o.opcode = gOpcode::notify;
376         o.dc = m_dc.grabRef();
377         m_rc->submit(o);
378 }
379
380 void gPainter::end()
381 {
382         gOpcode o;
383         o.opcode = gOpcode::flush;
384         o.dc = m_dc.grabRef();
385         m_rc->submit(o);
386 }
387
388 gDC::gDC()
389 {
390 }
391
392 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
393 {
394 }
395
396 gDC::~gDC()
397 {
398 }
399
400 void gDC::exec(gOpcode *o)
401 {
402         switch (o->opcode)
403         {
404         case gOpcode::setBackgroundColor:
405                 m_background_color = o->parm.setColor->color;
406                 delete o->parm.setColor;
407                 break;
408         case gOpcode::setForegroundColor:
409                 m_foreground_color = o->parm.setColor->color;
410                 delete o->parm.setColor;
411                 break;
412         case gOpcode::setBackgroundColorRGB:
413                 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
414                 delete o->parm.setColorRGB;
415                 break;
416         case gOpcode::setForegroundColorRGB:
417                 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
418                 delete o->parm.setColorRGB;
419                 break;
420         case gOpcode::setFont:
421                 m_current_font = o->parm.setFont->font;
422                 o->parm.setFont->font->Release();
423                 delete o->parm.setFont;
424                 break;
425         case gOpcode::renderText:
426         {
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);
432                 
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);
439                 
440                 ePoint offset = m_current_offset;
441                 
442                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
443                 {
444                         eRect bbox = para->getBoundBox();
445                         offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
446                 }
447                 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
448                 delete o->parm.renderText;
449                 break;
450         }
451         case gOpcode::renderPara:
452         {
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;
456                 break;
457         }
458         case gOpcode::fill:
459         {
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);
464                 delete o->parm.fill;
465                 break;
466         }
467         case gOpcode::fillRegion:
468         {
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;
473                 break;
474         }
475         case gOpcode::clear:
476                 m_pixmap->fill(m_current_clip, m_background_color);
477                 delete o->parm.fill;
478                 break;
479         case gOpcode::blit:
480         {
481                 gRegion clip;
482                                 // this code should be checked again but i'm too tired now
483                 
484                 o->parm.blit->position += m_current_offset;
485                 
486                 if (o->parm.blit->clip.valid())
487                 {
488                         o->parm.blit->clip.moveBy(m_current_offset);
489                         clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
490                 } else
491                         clip = m_current_clip;
492                 
493                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
494                 o->parm.blit->pixmap->Release();
495                 delete o->parm.blit;
496                 break;
497         }
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));
505                 
506                 delete[] o->parm.setPalette->palette->data;
507                 delete o->parm.setPalette->palette;
508                 delete o->parm.setPalette;
509                 break;
510         case gOpcode::mergePalette:
511                 m_pixmap->mergePalette(*o->parm.mergePalette->target);
512                 o->parm.mergePalette->target->Release();
513                 delete o->parm.mergePalette;
514                 break; 
515         case gOpcode::line:
516         {
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);
519                 delete o->parm.line;
520                 break;
521         }
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;
526                 delete o->parm.clip;
527                 break;
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());
531                 delete o->parm.clip;
532                 break;
533         case gOpcode::popClip:
534                 if (!m_clip_stack.empty())
535                 {
536                         m_current_clip = m_clip_stack.top();
537                         m_clip_stack.pop();
538                 }
539                 break;
540         case gOpcode::setOffset:
541                 if (o->parm.setOffset->rel)
542                         m_current_offset += o->parm.setOffset->value;
543                 else
544                         m_current_offset  = o->parm.setOffset->value;
545                 delete o->parm.setOffset;
546                 break;
547         case gOpcode::waitVSync:
548                 break;
549         case gOpcode::flip:
550                 break;
551         case gOpcode::flush:
552                 break;
553         default:
554                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
555         }
556 }
557
558 gRGB gDC::getRGB(gColor col)
559 {
560         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
561                 return gRGB(col, col, col);
562         if (col<0)
563         {
564                 eFatal("bla transp");
565                 return gRGB(0, 0, 0, 0xFF);
566         }
567         return m_pixmap->surface->clut.data[col];
568 }
569
570 DEFINE_REF(gDC);
571
572 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");