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