- disabled gui for a moment
[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         eDebug("waiting for gRC thread shutdown");
49         pthread_join(the_thread, 0);
50         eDebug("gRC thread has finished");
51 }
52
53 void *gRC::thread()
54 {
55 #ifndef SYNC_PAINT
56         while (1)
57 #else
58         while (queue.size())
59 #endif
60         {
61                 queuelock.lock(1);
62                 gOpcode& o(queue.current());
63                 if (o.opcode==gOpcode::shutdown)
64                         break;
65                 o.dc->exec(&o);
66                 o.dc->Release();
67                 queue.dequeue();
68         }
69 #ifndef SYNC_PAINT
70         pthread_exit(0);
71 #endif
72         return 0;
73 }
74
75 gRC *gRC::getInstance()
76 {
77         return instance;
78 }
79
80 static int gPainter_instances;
81
82 gPainter::gPainter(gDC *dc, eRect rect): m_dc(dc), m_rc(gRC::getInstance())
83 {
84 //      ASSERT(!gPainter_instances);
85         gPainter_instances++;
86 //      begin(rect);
87 }
88
89 gPainter::~gPainter()
90 {
91         end();
92         gPainter_instances--;
93 }
94
95 void gPainter::setBackgroundColor(const gColor &color)
96 {
97         gOpcode o;
98         o.opcode = gOpcode::setBackgroundColor;
99         o.dc = m_dc.grabRef();
100         o.parm.setColor = new gOpcode::para::psetColor;
101         o.parm.setColor->color = color;
102         
103         m_rc->submit(o);
104 }
105
106 void gPainter::setForegroundColor(const gColor &color)
107 {
108         gOpcode o;
109         o.opcode = gOpcode::setForegroundColor;
110         o.dc = m_dc.grabRef();
111         o.parm.setColor = new gOpcode::para::psetColor;
112         o.parm.setColor->color = color;
113         
114         m_rc->submit(o);
115 }
116
117 void gPainter::setFont(gFont *font)
118 {
119         gOpcode o;
120         o.opcode = gOpcode::setFont;
121         o.dc = m_dc.grabRef();
122         font->AddRef();
123         o.parm.setFont = new gOpcode::para::psetFont;
124         o.parm.setFont->font = font;
125         
126         m_rc->submit(o);
127 }
128
129 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
130 {
131         gOpcode o;
132         o.opcode=gOpcode::renderText;
133         o.dc = m_dc.grabRef();
134         o.parm.renderText = new gOpcode::para::prenderText;
135         o.parm.renderText->area = pos;
136         o.parm.renderText->text = string;
137         o.parm.renderText->flags = flags;
138         m_rc->submit(o);
139 }
140
141 void gPainter::renderPara(eTextPara *para, ePoint offset)
142 {
143         gOpcode o;
144         o.opcode=gOpcode::renderPara;
145         o.dc = m_dc.grabRef();
146         o.parm.renderPara = new gOpcode::para::prenderPara;
147         o.parm.renderPara->offset = offset;
148
149         para->AddRef();
150         o.parm.renderPara->textpara = para;
151         m_rc->submit(o);
152 }
153
154 void gPainter::fill(const eRect &area)
155 {
156         gOpcode o;
157         o.opcode=gOpcode::fill;
158
159         o.dc = m_dc.grabRef();
160         o.parm.fill = new gOpcode::para::pfillRect;
161         o.parm.fill->area = area;
162         m_rc->submit(o);
163 }
164
165 void gPainter::clear()
166 {
167         gOpcode o;
168         o.opcode=gOpcode::clear;
169         o.dc = m_dc.grabRef();
170         o.parm.fill = new gOpcode::para::pfillRect;
171         o.parm.fill->area = eRect();
172         m_rc->submit(o);
173 }
174
175 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
176 {
177         gOpcode o;
178
179         o.opcode=gOpcode::blit;
180         o.dc = m_dc.grabRef();
181         pixmap->AddRef();
182         o.parm.blit  = new gOpcode::para::pblit;
183         o.parm.blit->pixmap = pixmap;
184         o.parm.blit->position = pos;
185         o.parm.blit->clip = clip;
186         o.flags=flags;
187         m_rc->submit(o);
188 }
189
190
191 void gPainter::setPalette(gRGB *colors, int start, int len)
192 {
193         gOpcode o;
194         o.opcode=gOpcode::setPalette;
195         o.dc = m_dc.grabRef();
196         gPalette *p=new gPalette;
197         
198         o.parm.setPalette = new gOpcode::para::psetPalette;
199         p->data=new gRGB[len];
200         
201         memcpy(p->data, colors, len*sizeof(gRGB));
202         p->start=start;
203         p->colors=len;
204         o.parm.setPalette->palette = p;
205         m_rc->submit(o);
206 }
207
208 void gPainter::mergePalette(gPixmap *target)
209 {
210         gOpcode o;
211         o.opcode=gOpcode::mergePalette;
212         o.dc = m_dc.grabRef();
213         target->AddRef();
214         o.parm.mergePalette->target = target;
215         m_rc->submit(o);
216 }
217
218 void gPainter::line(ePoint start, ePoint end)
219 {
220         gOpcode o;
221         o.opcode=gOpcode::line;
222         o.dc = m_dc.grabRef();
223         o.parm.line = new gOpcode::para::pline;
224         o.parm.line->start = start;
225         o.parm.line->end = end;
226         m_rc->submit(o);
227 }
228
229 void gPainter::setOffset(ePoint val)
230 {
231         gOpcode o;
232         o.opcode=gOpcode::setOffset;
233         o.dc = m_dc.grabRef();
234         o.parm.setOffset = new gOpcode::para::psetOffset;
235         o.parm.setOffset->rel = 0;
236         o.parm.setOffset->value = val;
237         m_rc->submit(o);
238 }
239
240 void gPainter::moveOffset(ePoint rel)
241 {
242         gOpcode o;
243         o.opcode=gOpcode::setOffset;
244         o.dc = m_dc.grabRef();
245         o.parm.setOffset = new gOpcode::para::psetOffset;
246         o.parm.setOffset->rel = 1;
247         o.parm.setOffset->value = rel;
248         m_rc->submit(o);
249 }
250
251 void gPainter::resetOffset()
252 {
253         gOpcode o;
254         o.opcode=gOpcode::setOffset;
255         o.dc = m_dc.grabRef();
256         o.parm.setOffset = new gOpcode::para::psetOffset;
257         o.parm.setOffset->value = ePoint(0, 0);
258         m_rc->submit(o);
259 }
260
261 void gPainter::resetClip(const gRegion &region)
262 {
263         gOpcode o;
264         o.opcode = gOpcode::setClip;
265         o.dc = m_dc.grabRef();
266         o.parm.clip = new gOpcode::para::psetClip;
267         o.parm.clip->region = region;
268         m_rc->submit(o);
269 }
270
271 void gPainter::clip(const gRegion &region)
272 {
273         gOpcode o;
274         o.opcode = gOpcode::addClip;
275         o.dc = m_dc.grabRef();
276         o.parm.clip = new gOpcode::para::psetClip;
277         o.parm.clip->region = region;
278         m_rc->submit(o);
279 }
280
281 void gPainter::clippop()
282 {
283         gOpcode o;
284         o.opcode = gOpcode::popClip;
285         o.dc = m_dc.grabRef();
286         m_rc->submit(o);
287 }
288
289 void gPainter::flush()
290 {
291 }
292
293 void gPainter::end()
294 {
295 }
296
297 gDC::gDC()
298 {
299 }
300
301 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
302 {
303 }
304
305 gDC::~gDC()
306 {
307 }
308
309 void gDC::exec(gOpcode *o)
310 {
311         switch (o->opcode)
312         {
313         case gOpcode::setBackgroundColor:
314                 m_background_color = o->parm.setColor->color;
315                 delete o->parm.setColor;
316                 break;
317         case gOpcode::setForegroundColor:
318                 m_foreground_color = o->parm.setColor->color;
319                 delete o->parm.setColor;
320                 break;
321         case gOpcode::setFont:
322                 m_current_font = o->parm.setFont->font;
323                 o->parm.setFont->font->Release();
324                 delete o->parm.setFont;
325                 break;
326         case gOpcode::renderText:
327         {
328                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
329                 para->setFont(m_current_font);
330                 para->renderString(o->parm.renderText->text, o->parm.renderText->flags);
331                 para->blit(*this, ePoint(0, 0), getRGB(m_foreground_color), getRGB(m_background_color));
332                 delete o->parm.renderText;
333                 break;
334         }
335         case gOpcode::renderPara:
336         {
337                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset, getRGB(m_foreground_color), getRGB(m_background_color));
338                 o->parm.renderPara->textpara->Release();
339                 delete o->parm.renderPara;
340                 break;
341         }
342         case gOpcode::fill:
343         {
344                 eRect area = o->parm.fill->area;
345                 area.moveBy(m_current_offset);
346                 gRegion clip = m_current_clip & area;
347                 m_pixmap->fill(clip, m_foreground_color);
348                 delete o->parm.fill;
349                 break;
350         }
351         case gOpcode::clear:
352                 m_pixmap->fill(m_current_clip, m_background_color);
353                 delete o->parm.fill;
354                 break;
355         case gOpcode::blit:
356         {
357                 gRegion clip;
358                 if (!o->parm.blit->clip.isValid())
359                 {
360                         clip.intersect(gRegion(o->parm.blit->clip), clip);
361                 } else
362                         clip = m_current_clip;
363                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
364                 o->parm.blit->pixmap->Release();
365                 delete o->parm.blit;
366                 break;
367         }
368         case gOpcode::setPalette:
369                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
370                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
371                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
372                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
373                 if (o->parm.setPalette->palette->colors)
374                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
375                 
376                 delete[] o->parm.setPalette->palette->data;
377                 delete o->parm.setPalette->palette;
378                 delete o->parm.setPalette;
379                 break;
380         case gOpcode::mergePalette:
381 #if 0
382                 pixmap->mergePalette(*o->parm.blit->pixmap);
383                 o->parm.blit->pixmap->unlock();
384                 delete o->parm.blit;
385 #endif
386                 break;
387         case gOpcode::line:
388         {
389                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
390                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
391                 delete o->parm.line;
392                 break;
393         }
394         case gOpcode::addClip:
395                 m_clip_stack.push(m_current_clip);
396                 o->parm.clip->region.moveBy(m_current_offset);
397                 m_current_clip &= o->parm.clip->region;
398                 delete o->parm.clip;
399                 break;
400         case gOpcode::setClip:
401                 o->parm.clip->region.moveBy(m_current_offset);
402                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->getSize());
403                 delete o->parm.clip;
404                 break;
405         case gOpcode::popClip:
406                 if (!m_clip_stack.empty())
407                 {
408                         m_current_clip = m_clip_stack.top();
409                         m_clip_stack.pop();
410                 }
411                 break;
412         case gOpcode::setOffset:
413                 if (o->parm.setOffset->rel)
414                         m_current_offset += o->parm.setOffset->value;
415                 else
416                         m_current_offset  = o->parm.setOffset->value;
417                 delete o->parm.setOffset;
418                 break;
419         default:
420                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
421         }
422 }
423
424 gRGB gDC::getRGB(gColor col)
425 {
426         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
427                 return gRGB(col, col, col);
428         if (col<0)
429         {
430                 eFatal("bla transp");
431                 return gRGB(0, 0, 0, 0xFF);
432         }
433         return m_pixmap->surface->clut.data[col];
434 }
435
436 DEFINE_REF(gDC);
437
438 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");
439