fix alphatest (HACK!!!)
[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         ASSERT(pixmap);
231         
232         o.opcode=gOpcode::blit;
233         o.dc = m_dc.grabRef();
234         pixmap->AddRef();
235         o.parm.blit  = new gOpcode::para::pblit;
236         o.parm.blit->pixmap = pixmap;
237         o.parm.blit->position = pos;
238         o.parm.blit->clip = clip;
239         o.parm.blit->flags=flags;
240         m_rc->submit(o);
241 }
242
243
244 void gPainter::setPalette(gRGB *colors, int start, int len)
245 {
246         gOpcode o;
247         o.opcode=gOpcode::setPalette;
248         o.dc = m_dc.grabRef();
249         gPalette *p=new gPalette;
250         
251         o.parm.setPalette = new gOpcode::para::psetPalette;
252         p->data=new gRGB[len];
253         
254         memcpy(p->data, colors, len*sizeof(gRGB));
255         p->start=start;
256         p->colors=len;
257         o.parm.setPalette->palette = p;
258         m_rc->submit(o);
259 }
260
261 void gPainter::setPalette(gPixmap *source)
262 {
263         ASSERT(source);
264         setPalette(source->surface->clut.data, source->surface->clut.start, source->surface->clut.colors);
265 }
266
267 void gPainter::mergePalette(gPixmap *target)
268 {
269         gOpcode o;
270         o.opcode = gOpcode::mergePalette;
271         o.dc = m_dc.grabRef();
272         target->AddRef();
273         o.parm.mergePalette = new gOpcode::para::pmergePalette;
274         o.parm.mergePalette->target = target;
275         m_rc->submit(o);
276 }
277
278 void gPainter::line(ePoint start, ePoint end)
279 {
280         gOpcode o;
281         o.opcode=gOpcode::line;
282         o.dc = m_dc.grabRef();
283         o.parm.line = new gOpcode::para::pline;
284         o.parm.line->start = start;
285         o.parm.line->end = end;
286         m_rc->submit(o);
287 }
288
289 void gPainter::setOffset(ePoint val)
290 {
291         gOpcode o;
292         o.opcode=gOpcode::setOffset;
293         o.dc = m_dc.grabRef();
294         o.parm.setOffset = new gOpcode::para::psetOffset;
295         o.parm.setOffset->rel = 0;
296         o.parm.setOffset->value = val;
297         m_rc->submit(o);
298 }
299
300 void gPainter::moveOffset(ePoint rel)
301 {
302         gOpcode o;
303         o.opcode=gOpcode::setOffset;
304         o.dc = m_dc.grabRef();
305         o.parm.setOffset = new gOpcode::para::psetOffset;
306         o.parm.setOffset->rel = 1;
307         o.parm.setOffset->value = rel;
308         m_rc->submit(o);
309 }
310
311 void gPainter::resetOffset()
312 {
313         gOpcode o;
314         o.opcode=gOpcode::setOffset;
315         o.dc = m_dc.grabRef();
316         o.parm.setOffset = new gOpcode::para::psetOffset;
317         o.parm.setOffset->rel = 0;
318         o.parm.setOffset->value = ePoint(0, 0);
319         m_rc->submit(o);
320 }
321
322 void gPainter::resetClip(const gRegion &region)
323 {
324         gOpcode o;
325         o.opcode = gOpcode::setClip;
326         o.dc = m_dc.grabRef();
327         o.parm.clip = new gOpcode::para::psetClip;
328         o.parm.clip->region = region;
329         m_rc->submit(o);
330 }
331
332 void gPainter::clip(const gRegion &region)
333 {
334         gOpcode o;
335         o.opcode = gOpcode::addClip;
336         o.dc = m_dc.grabRef();
337         o.parm.clip = new gOpcode::para::psetClip;
338         o.parm.clip->region = region;
339         m_rc->submit(o);
340 }
341
342 void gPainter::clippop()
343 {
344         gOpcode o;
345         o.opcode = gOpcode::popClip;
346         o.dc = m_dc.grabRef();
347         m_rc->submit(o);
348 }
349
350 void gPainter::flush()
351 {
352         gOpcode o;
353         o.opcode = gOpcode::flush;
354         o.dc = m_dc.grabRef();
355         m_rc->submit(o);
356 }
357
358 void gPainter::waitVSync()
359 {
360         gOpcode o;
361         o.opcode = gOpcode::waitVSync;
362         o.dc = m_dc.grabRef();
363         m_rc->submit(o);
364 }
365
366 void gPainter::flip()
367 {
368         gOpcode o;
369         o.opcode = gOpcode::flip;
370         o.dc = m_dc.grabRef();
371         m_rc->submit(o);
372 }
373
374 void gPainter::notify()
375 {
376         gOpcode o;
377         o.opcode = gOpcode::notify;
378         o.dc = m_dc.grabRef();
379         m_rc->submit(o);
380 }
381
382 void gPainter::end()
383 {
384         gOpcode o;
385         o.opcode = gOpcode::flush;
386         o.dc = m_dc.grabRef();
387         m_rc->submit(o);
388 }
389
390 gDC::gDC()
391 {
392 }
393
394 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
395 {
396 }
397
398 gDC::~gDC()
399 {
400 }
401
402 void gDC::exec(gOpcode *o)
403 {
404         switch (o->opcode)
405         {
406         case gOpcode::setBackgroundColor:
407                 m_background_color = o->parm.setColor->color;
408                 delete o->parm.setColor;
409                 break;
410         case gOpcode::setForegroundColor:
411                 m_foreground_color = o->parm.setColor->color;
412                 delete o->parm.setColor;
413                 break;
414         case gOpcode::setBackgroundColorRGB:
415                 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
416                 delete o->parm.setColorRGB;
417                 break;
418         case gOpcode::setForegroundColorRGB:
419                 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
420                 delete o->parm.setColorRGB;
421                 break;
422         case gOpcode::setFont:
423                 m_current_font = o->parm.setFont->font;
424                 o->parm.setFont->font->Release();
425                 delete o->parm.setFont;
426                 break;
427         case gOpcode::renderText:
428         {
429                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
430                 int flags = o->parm.renderText->flags;
431                 assert(m_current_font);
432                 para->setFont(m_current_font);
433                 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
434                 
435                 if (flags & gPainter::RT_HALIGN_RIGHT)
436                         para->realign(eTextPara::dirRight);
437                 else if (flags & gPainter::RT_HALIGN_CENTER)
438                         para->realign(eTextPara::dirCenter);
439                 else if (flags & gPainter::RT_HALIGN_BLOCK)
440                         para->realign(eTextPara::dirBlock);
441                 
442                 ePoint offset = m_current_offset;
443                 
444                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
445                 {
446                         eRect bbox = para->getBoundBox();
447                         int vcentered_top = (o->parm.renderText->area.height() - bbox.height()) / 2;
448                         int correction = vcentered_top - bbox.top();
449                         offset += ePoint(0, correction);
450                 }
451                 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
452                 delete o->parm.renderText;
453                 break;
454         }
455         case gOpcode::renderPara:
456         {
457                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
458                 o->parm.renderPara->textpara->Release();
459                 delete o->parm.renderPara;
460                 break;
461         }
462         case gOpcode::fill:
463         {
464                 eRect area = o->parm.fill->area;
465                 area.moveBy(m_current_offset);
466                 gRegion clip = m_current_clip & area;
467                 m_pixmap->fill(clip, m_foreground_color);
468                 delete o->parm.fill;
469                 break;
470         }
471         case gOpcode::fillRegion:
472         {
473                 o->parm.fillRegion->region.moveBy(m_current_offset);
474                 gRegion clip = m_current_clip & o->parm.fillRegion->region;
475                 m_pixmap->fill(clip, m_foreground_color);
476                 delete o->parm.fillRegion;
477                 break;
478         }
479         case gOpcode::clear:
480                 m_pixmap->fill(m_current_clip, m_background_color);
481                 delete o->parm.fill;
482                 break;
483         case gOpcode::blit:
484         {
485                 gRegion clip;
486                                 // this code should be checked again but i'm too tired now
487                 
488                 o->parm.blit->position += m_current_offset;
489                 
490                 if (o->parm.blit->clip.valid())
491                 {
492                         o->parm.blit->clip.moveBy(m_current_offset);
493                         clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
494                 } else
495                         clip = m_current_clip;
496                 
497                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
498                 o->parm.blit->pixmap->Release();
499                 delete o->parm.blit;
500                 break;
501         }
502         case gOpcode::setPalette:
503                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
504                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
505                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
506                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
507                 if (o->parm.setPalette->palette->colors)
508                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
509                 
510                 delete[] o->parm.setPalette->palette->data;
511                 delete o->parm.setPalette->palette;
512                 delete o->parm.setPalette;
513                 break;
514         case gOpcode::mergePalette:
515                 m_pixmap->mergePalette(*o->parm.mergePalette->target);
516                 o->parm.mergePalette->target->Release();
517                 delete o->parm.mergePalette;
518                 break; 
519         case gOpcode::line:
520         {
521                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
522                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
523                 delete o->parm.line;
524                 break;
525         }
526         case gOpcode::addClip:
527                 m_clip_stack.push(m_current_clip);
528                 o->parm.clip->region.moveBy(m_current_offset);
529                 m_current_clip &= o->parm.clip->region;
530                 delete o->parm.clip;
531                 break;
532         case gOpcode::setClip:
533                 o->parm.clip->region.moveBy(m_current_offset);
534                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
535                 delete o->parm.clip;
536                 break;
537         case gOpcode::popClip:
538                 if (!m_clip_stack.empty())
539                 {
540                         m_current_clip = m_clip_stack.top();
541                         m_clip_stack.pop();
542                 }
543                 break;
544         case gOpcode::setOffset:
545                 if (o->parm.setOffset->rel)
546                         m_current_offset += o->parm.setOffset->value;
547                 else
548                         m_current_offset  = o->parm.setOffset->value;
549                 delete o->parm.setOffset;
550                 break;
551         case gOpcode::waitVSync:
552                 break;
553         case gOpcode::flip:
554                 break;
555         case gOpcode::flush:
556                 break;
557         default:
558                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
559         }
560 }
561
562 gRGB gDC::getRGB(gColor col)
563 {
564         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
565                 return gRGB(col, col, col);
566         if (col<0)
567         {
568                 eFatal("bla transp");
569                 return gRGB(0, 0, 0, 0xFF);
570         }
571         return m_pixmap->surface->clut.data[col];
572 }
573
574 DEFINE_REF(gDC);
575
576 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");