99fe085916a5ca298332a586e81cc7763834368e
[vuplus_dvbapp] / 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 }
377
378 gDC::gDC()
379 {
380 }
381
382 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
383 {
384 }
385
386 gDC::~gDC()
387 {
388 }
389
390 void gDC::exec(gOpcode *o)
391 {
392         switch (o->opcode)
393         {
394         case gOpcode::setBackgroundColor:
395                 m_background_color = o->parm.setColor->color;
396                 delete o->parm.setColor;
397                 break;
398         case gOpcode::setForegroundColor:
399                 m_foreground_color = o->parm.setColor->color;
400                 delete o->parm.setColor;
401                 break;
402         case gOpcode::setBackgroundColorRGB:
403                 m_background_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
404                 delete o->parm.setColorRGB;
405                 break;
406         case gOpcode::setForegroundColorRGB:
407                 m_foreground_color = m_pixmap->surface->clut.findColor(o->parm.setColorRGB->color);
408                 delete o->parm.setColorRGB;
409                 break;
410         case gOpcode::setFont:
411                 m_current_font = o->parm.setFont->font;
412                 o->parm.setFont->font->Release();
413                 delete o->parm.setFont;
414                 break;
415         case gOpcode::renderText:
416         {
417                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
418                 int flags = o->parm.renderText->flags;
419                 assert(m_current_font);
420                 para->setFont(m_current_font);
421                 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
422                 
423                 if (flags & gPainter::RT_HALIGN_RIGHT)
424                         para->realign(eTextPara::dirRight);
425                 else if (flags & gPainter::RT_HALIGN_CENTER)
426                         para->realign(eTextPara::dirCenter);
427                 else if (flags & gPainter::RT_HALIGN_BLOCK)
428                         para->realign(eTextPara::dirBlock);
429                 
430                 ePoint offset = m_current_offset;
431                 
432                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
433                 {
434                         eRect bbox = para->getBoundBox();
435                         offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
436                 }
437                 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
438                 delete o->parm.renderText;
439                 break;
440         }
441         case gOpcode::renderPara:
442         {
443                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
444                 o->parm.renderPara->textpara->Release();
445                 delete o->parm.renderPara;
446                 break;
447         }
448         case gOpcode::fill:
449         {
450                 eRect area = o->parm.fill->area;
451                 area.moveBy(m_current_offset);
452                 gRegion clip = m_current_clip & area;
453                 m_pixmap->fill(clip, m_foreground_color);
454                 delete o->parm.fill;
455                 break;
456         }
457         case gOpcode::fillRegion:
458         {
459                 o->parm.fillRegion->region.moveBy(m_current_offset);
460                 gRegion clip = m_current_clip & o->parm.fillRegion->region;
461                 m_pixmap->fill(clip, m_foreground_color);
462                 delete o->parm.fillRegion;
463                 break;
464         }
465         case gOpcode::clear:
466                 m_pixmap->fill(m_current_clip, m_background_color);
467                 delete o->parm.fill;
468                 break;
469         case gOpcode::blit:
470         {
471                 gRegion clip;
472                                 // this code should be checked again but i'm too tired now
473                 
474                 o->parm.blit->position += m_current_offset;
475                 
476                 if (o->parm.blit->clip.valid())
477                 {
478                         o->parm.blit->clip.moveBy(m_current_offset);
479                         clip.intersect(gRegion(o->parm.blit->clip), m_current_clip);
480                 } else
481                         clip = m_current_clip;
482                 
483                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
484                 o->parm.blit->pixmap->Release();
485                 delete o->parm.blit;
486                 break;
487         }
488         case gOpcode::setPalette:
489                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
490                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
491                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
492                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
493                 if (o->parm.setPalette->palette->colors)
494                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
495                 
496                 delete[] o->parm.setPalette->palette->data;
497                 delete o->parm.setPalette->palette;
498                 delete o->parm.setPalette;
499                 break;
500         case gOpcode::mergePalette:
501                 m_pixmap->mergePalette(*o->parm.mergePalette->target);
502                 o->parm.mergePalette->target->Release();
503                 delete o->parm.mergePalette;
504                 break; 
505         case gOpcode::line:
506         {
507                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
508                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
509                 delete o->parm.line;
510                 break;
511         }
512         case gOpcode::addClip:
513                 m_clip_stack.push(m_current_clip);
514                 o->parm.clip->region.moveBy(m_current_offset);
515                 m_current_clip &= o->parm.clip->region;
516                 delete o->parm.clip;
517                 break;
518         case gOpcode::setClip:
519                 o->parm.clip->region.moveBy(m_current_offset);
520                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->size());
521                 delete o->parm.clip;
522                 break;
523         case gOpcode::popClip:
524                 if (!m_clip_stack.empty())
525                 {
526                         m_current_clip = m_clip_stack.top();
527                         m_clip_stack.pop();
528                 }
529                 break;
530         case gOpcode::setOffset:
531                 if (o->parm.setOffset->rel)
532                         m_current_offset += o->parm.setOffset->value;
533                 else
534                         m_current_offset  = o->parm.setOffset->value;
535                 delete o->parm.setOffset;
536                 break;
537         case gOpcode::waitVSync:
538                 break;
539         case gOpcode::flip:
540                 break;
541         case gOpcode::flush:
542                 break;
543         default:
544                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
545         }
546 }
547
548 gRGB gDC::getRGB(gColor col)
549 {
550         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
551                 return gRGB(col, col, col);
552         if (col<0)
553         {
554                 eFatal("bla transp");
555                 return gRGB(0, 0, 0, 0xFF);
556         }
557         return m_pixmap->surface->clut.data[col];
558 }
559
560 DEFINE_REF(gDC);
561
562 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");