*** empty log message ***
[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 #ifndef SYNC_PAINT
59         while (1)
60 #else
61         while (queue.size())
62 #endif
63         {
64                 queuelock.lock(1);
65                 gOpcode& o(queue.current());
66                 if (o.opcode==gOpcode::shutdown)
67                         break;
68                 if (o.opcode==gOpcode::notify)
69                 {
70                         m_notify_pump.send(1);
71                 } else
72                         o.dc->exec(&o);
73                 o.dc->Release();
74                 queue.dequeue();
75         }
76 #ifndef SYNC_PAINT
77         pthread_exit(0);
78 #endif
79         return 0;
80 }
81
82 void gRC::recv_notify(const int &i)
83 {
84         notify();
85 }
86
87 gRC *gRC::getInstance()
88 {
89         return instance;
90 }
91
92 static int gPainter_instances;
93
94 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
95 {
96 //      ASSERT(!gPainter_instances);
97         gPainter_instances++;
98 //      begin(rect);
99 }
100
101 gPainter::~gPainter()
102 {
103         end();
104         gPainter_instances--;
105 }
106
107 void gPainter::setBackgroundColor(const gColor &color)
108 {
109         gOpcode o;
110         o.opcode = gOpcode::setBackgroundColor;
111         o.dc = m_dc.grabRef();
112         o.parm.setColor = new gOpcode::para::psetColor;
113         o.parm.setColor->color = color;
114         
115         m_rc->submit(o);
116 }
117
118 void gPainter::setForegroundColor(const gColor &color)
119 {
120         gOpcode o;
121         o.opcode = gOpcode::setForegroundColor;
122         o.dc = m_dc.grabRef();
123         o.parm.setColor = new gOpcode::para::psetColor;
124         o.parm.setColor->color = color;
125         
126         m_rc->submit(o);
127 }
128
129 void gPainter::setBackgroundColor(const gRGB &color)
130 {
131         gOpcode o;
132         o.opcode = gOpcode::setBackgroundColorRGB;
133         o.dc = m_dc.grabRef();
134         o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
135         o.parm.setColorRGB->color = color;
136         
137         m_rc->submit(o);
138 }
139
140 void gPainter::setForegroundColor(const gRGB &color)
141 {
142         gOpcode o;
143         o.opcode = gOpcode::setForegroundColorRGB;
144         o.dc = m_dc.grabRef();
145         o.parm.setColorRGB = new gOpcode::para::psetColorRGB;
146         o.parm.setColorRGB->color = color;
147         
148         m_rc->submit(o);
149 }
150
151 void gPainter::setFont(gFont *font)
152 {
153         gOpcode o;
154         o.opcode = gOpcode::setFont;
155         o.dc = m_dc.grabRef();
156         font->AddRef();
157         o.parm.setFont = new gOpcode::para::psetFont;
158         o.parm.setFont->font = font;
159         
160         m_rc->submit(o);
161 }
162
163 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
164 {
165         gOpcode o;
166         o.opcode=gOpcode::renderText;
167         o.dc = m_dc.grabRef();
168         o.parm.renderText = new gOpcode::para::prenderText;
169         o.parm.renderText->area = pos;
170         o.parm.renderText->text = string;
171         o.parm.renderText->flags = flags;
172         m_rc->submit(o);
173 }
174
175 void gPainter::renderPara(eTextPara *para, ePoint offset)
176 {
177         gOpcode o;
178         o.opcode=gOpcode::renderPara;
179         o.dc = m_dc.grabRef();
180         o.parm.renderPara = new gOpcode::para::prenderPara;
181         o.parm.renderPara->offset = offset;
182
183         para->AddRef();
184         o.parm.renderPara->textpara = para;
185         m_rc->submit(o);
186 }
187
188 void gPainter::fill(const eRect &area)
189 {
190         gOpcode o;
191         o.opcode=gOpcode::fill;
192
193         o.dc = m_dc.grabRef();
194         o.parm.fill = new gOpcode::para::pfillRect;
195         o.parm.fill->area = area;
196         m_rc->submit(o);
197 }
198
199 void gPainter::fill(const gRegion &region)
200 {
201         gOpcode o;
202         o.opcode=gOpcode::fillRegion;
203
204         o.dc = m_dc.grabRef();
205         o.parm.fillRegion = new gOpcode::para::pfillRegion;
206         o.parm.fillRegion->region = region;
207         m_rc->submit(o);
208 }
209
210 void gPainter::clear()
211 {
212         gOpcode o;
213         o.opcode=gOpcode::clear;
214         o.dc = m_dc.grabRef();
215         o.parm.fill = new gOpcode::para::pfillRect;
216         o.parm.fill->area = eRect();
217         m_rc->submit(o);
218 }
219
220 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
221 {
222         gOpcode o;
223         
224         o.opcode=gOpcode::blit;
225         o.dc = m_dc.grabRef();
226         pixmap->AddRef();
227         o.parm.blit  = new gOpcode::para::pblit;
228         o.parm.blit->pixmap = pixmap;
229         o.parm.blit->position = pos;
230         o.parm.blit->clip = clip;
231         o.parm.blit->flags=flags;
232         m_rc->submit(o);
233 }
234
235
236 void gPainter::setPalette(gRGB *colors, int start, int len)
237 {
238         gOpcode o;
239         o.opcode=gOpcode::setPalette;
240         o.dc = m_dc.grabRef();
241         gPalette *p=new gPalette;
242         
243         o.parm.setPalette = new gOpcode::para::psetPalette;
244         p->data=new gRGB[len];
245         
246         memcpy(p->data, colors, len*sizeof(gRGB));
247         p->start=start;
248         p->colors=len;
249         o.parm.setPalette->palette = p;
250         m_rc->submit(o);
251 }
252
253 void gPainter::setPalette(gPixmap *source)
254 {
255         ASSERT(source);
256         setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
257 }
258
259 void gPainter::mergePalette(gPixmap *target)
260 {
261         gOpcode o;
262         o.opcode = gOpcode::mergePalette;
263         o.dc = m_dc.grabRef();
264         target->AddRef();
265         o.parm.mergePalette = new gOpcode::para::pmergePalette;
266         o.parm.mergePalette->target = target;
267         m_rc->submit(o);
268 }
269
270 void gPainter::line(ePoint start, ePoint end)
271 {
272         gOpcode o;
273         o.opcode=gOpcode::line;
274         o.dc = m_dc.grabRef();
275         o.parm.line = new gOpcode::para::pline;
276         o.parm.line->start = start;
277         o.parm.line->end = end;
278         m_rc->submit(o);
279 }
280
281 void gPainter::setOffset(ePoint val)
282 {
283         gOpcode o;
284         o.opcode=gOpcode::setOffset;
285         o.dc = m_dc.grabRef();
286         o.parm.setOffset = new gOpcode::para::psetOffset;
287         o.parm.setOffset->rel = 0;
288         o.parm.setOffset->value = val;
289         m_rc->submit(o);
290 }
291
292 void gPainter::moveOffset(ePoint rel)
293 {
294         gOpcode o;
295         o.opcode=gOpcode::setOffset;
296         o.dc = m_dc.grabRef();
297         o.parm.setOffset = new gOpcode::para::psetOffset;
298         o.parm.setOffset->rel = 1;
299         o.parm.setOffset->value = rel;
300         m_rc->submit(o);
301 }
302
303 void gPainter::resetOffset()
304 {
305         gOpcode o;
306         o.opcode=gOpcode::setOffset;
307         o.dc = m_dc.grabRef();
308         o.parm.setOffset = new gOpcode::para::psetOffset;
309         o.parm.setOffset->rel = 0;
310         o.parm.setOffset->value = ePoint(0, 0);
311         m_rc->submit(o);
312 }
313
314 void gPainter::resetClip(const gRegion &region)
315 {
316         gOpcode o;
317         o.opcode = gOpcode::setClip;
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::clip(const gRegion &region)
325 {
326         gOpcode o;
327         o.opcode = gOpcode::addClip;
328         o.dc = m_dc.grabRef();
329         o.parm.clip = new gOpcode::para::psetClip;
330         o.parm.clip->region = region;
331         m_rc->submit(o);
332 }
333
334 void gPainter::clippop()
335 {
336         gOpcode o;
337         o.opcode = gOpcode::popClip;
338         o.dc = m_dc.grabRef();
339         m_rc->submit(o);
340 }
341
342 void gPainter::flush()
343 {
344         gOpcode o;
345         o.opcode = gOpcode::flush;
346         o.dc = m_dc.grabRef();
347         m_rc->submit(o);
348 }
349
350 void gPainter::waitVSync()
351 {
352         gOpcode o;
353         o.opcode = gOpcode::waitVSync;
354         o.dc = m_dc.grabRef();
355         m_rc->submit(o);
356 }
357
358 void gPainter::flip()
359 {
360         gOpcode o;
361         o.opcode = gOpcode::flip;
362         o.dc = m_dc.grabRef();
363         m_rc->submit(o);
364 }
365
366 void gPainter::notify()
367 {
368         gOpcode o;
369         o.opcode = gOpcode::notify;
370         o.dc = m_dc.grabRef();
371         m_rc->submit(o);
372 }
373
374 void gPainter::end()
375 {
376         gOpcode o;
377         o.opcode = gOpcode::flush;
378         o.dc = m_dc.grabRef();
379         m_rc->submit(o);
380 }
381
382 gDC::gDC()
383 {
384 }
385
386 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
387 {
388 }
389
390 gDC::~gDC()
391 {
392 }
393
394 void gDC::exec(gOpcode *o)
395 {
396         switch (o->opcode)
397         {
398         case gOpcode::setBackgroundColor:
399                 m_background_color = o->parm.setColor->color;
400                 delete o->parm.setColor;
401                 break;
402         case gOpcode::setForegroundColor:
403                 m_foreground_color = o->parm.setColor->color;
404                 delete o->parm.setColor;
405                 break;
406         case gOpcode::setBackgroundColorRGB:
407                 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
408                 delete o->parm.setColorRGB;
409                 break;
410         case gOpcode::setForegroundColorRGB:
411                 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
412                 delete o->parm.setColorRGB;
413                 break;
414         case gOpcode::setFont:
415                 m_current_font = o->parm.setFont->font;
416                 o->parm.setFont->font->Release();
417                 delete o->parm.setFont;
418                 break;
419         case gOpcode::renderText:
420         {
421                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
422                 int flags = o->parm.renderText->flags;
423                 assert(m_current_font);
424                 para->setFont(m_current_font);
425                 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
426                 
427                 if (flags & gPainter::RT_HALIGN_RIGHT)
428                         para->realign(eTextPara::dirRight);
429                 else if (flags & gPainter::RT_HALIGN_CENTER)
430                         para->realign(eTextPara::dirCenter);
431                 else if (flags & gPainter::RT_HALIGN_BLOCK)
432                         para->realign(eTextPara::dirBlock);
433                 
434                 ePoint offset = m_current_offset;
435                 
436                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
437                 {
438                         eRect bbox = para->getBoundBox();
439                         offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
440                 }
441                 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
442                 delete o->parm.renderText;
443                 break;
444         }
445         case gOpcode::renderPara:
446         {
447                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
448                 o->parm.renderPara->textpara->Release();
449                 delete o->parm.renderPara;
450                 break;
451         }
452         case gOpcode::fill:
453         {
454                 eRect area = o->parm.fill->area;
455                 area.moveBy(m_current_offset);
456                 gRegion clip = m_current_clip & area;
457                 m_pixmap->fill(clip, m_foreground_color);
458                 delete o->parm.fill;
459                 break;
460         }
461         case gOpcode::fillRegion:
462         {
463                 o->parm.fillRegion->region.moveBy(m_current_offset);
464                 gRegion clip = m_current_clip & o->parm.fillRegion->region;
465                 m_pixmap->fill(clip, m_foreground_color);
466                 delete o->parm.fillRegion;
467                 break;
468         }
469         case gOpcode::clear:
470                 m_pixmap->fill(m_current_clip, m_background_color);
471                 delete o->parm.fill;
472                 break;
473         case gOpcode::blit:
474         {
475                 gRegion clip;
476                                 // this code should be checked again but i'm too tired now
477                 
478                 o->parm.blit->position += m_current_offset;
479                 
480                 if (o->parm.blit->clip.valid())
481                 {
482                         o->parm.blit->clip.moveBy(m_current_offset);
483                         clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
484                 } else
485                         clip = m_current_clip;
486                 
487                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
488                 o->parm.blit->pixmap->Release();
489                 delete o->parm.blit;
490                 break;
491         }
492         case gOpcode::setPalette:
493                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
494                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
495                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
496                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
497                 if (o->parm.setPalette->palette->colors)
498                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
499                 
500                 delete[] o->parm.setPalette->palette->data;
501                 delete o->parm.setPalette->palette;
502                 delete o->parm.setPalette;
503                 break;
504         case gOpcode::mergePalette:
505                 m_pixmap->mergePalette(*o->parm.mergePalette->target);
506                 o->parm.mergePalette->target->Release();
507                 delete o->parm.mergePalette;
508                 break; 
509         case gOpcode::line:
510         {
511                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
512                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
513                 delete o->parm.line;
514                 break;
515         }
516         case gOpcode::addClip:
517                 m_clip_stack.push(m_current_clip);
518                 o->parm.clip->region.moveBy(m_current_offset);
519                 m_current_clip &= o->parm.clip->region;
520                 delete o->parm.clip;
521                 break;
522         case gOpcode::setClip:
523                 o->parm.clip->region.moveBy(m_current_offset);
524                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
525                 delete o->parm.clip;
526                 break;
527         case gOpcode::popClip:
528                 if (!m_clip_stack.empty())
529                 {
530                         m_current_clip = m_clip_stack.top();
531                         m_clip_stack.pop();
532                 }
533                 break;
534         case gOpcode::setOffset:
535                 if (o->parm.setOffset->rel)
536                         m_current_offset += o->parm.setOffset->value;
537                 else
538                         m_current_offset  = o->parm.setOffset->value;
539                 delete o->parm.setOffset;
540                 break;
541         case gOpcode::waitVSync:
542                 break;
543         case gOpcode::flip:
544                 break;
545         case gOpcode::flush:
546                 break;
547         default:
548                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
549         }
550 }
551
552 gRGB gDC::getRGB(gColor col)
553 {
554         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
555                 return gRGB(col, col, col);
556         if (col<0)
557         {
558                 eFatal("bla transp");
559                 return gRGB(0, 0, 0, 0xFF);
560         }
561         return m_pixmap->surface->clut.data[col];
562 }
563
564 DEFINE_REF(gDC);
565
566 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");