- fixed blit in gRC
[openblackhole/openblackhole-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::setFont(gFont *font)
120 {
121         gOpcode o;
122         o.opcode = gOpcode::setFont;
123         o.dc = m_dc.grabRef();
124         font->AddRef();
125         o.parm.setFont = new gOpcode::para::psetFont;
126         o.parm.setFont->font = font;
127         
128         m_rc->submit(o);
129 }
130
131 void gPainter::renderText(const eRect &pos, const std::string &string, int flags)
132 {
133         gOpcode o;
134         o.opcode=gOpcode::renderText;
135         o.dc = m_dc.grabRef();
136         o.parm.renderText = new gOpcode::para::prenderText;
137         o.parm.renderText->area = pos;
138         o.parm.renderText->text = string;
139         o.parm.renderText->flags = flags;
140         m_rc->submit(o);
141 }
142
143 void gPainter::renderPara(eTextPara *para, ePoint offset)
144 {
145         gOpcode o;
146         o.opcode=gOpcode::renderPara;
147         o.dc = m_dc.grabRef();
148         o.parm.renderPara = new gOpcode::para::prenderPara;
149         o.parm.renderPara->offset = offset;
150
151         para->AddRef();
152         o.parm.renderPara->textpara = para;
153         m_rc->submit(o);
154 }
155
156 void gPainter::fill(const eRect &area)
157 {
158         gOpcode o;
159         o.opcode=gOpcode::fill;
160
161         o.dc = m_dc.grabRef();
162         o.parm.fill = new gOpcode::para::pfillRect;
163         o.parm.fill->area = area;
164         m_rc->submit(o);
165 }
166
167 void gPainter::fill(const gRegion &region)
168 {
169         gOpcode o;
170         o.opcode=gOpcode::fillRegion;
171
172         o.dc = m_dc.grabRef();
173         o.parm.fillRegion = new gOpcode::para::pfillRegion;
174         o.parm.fillRegion->region = region;
175         m_rc->submit(o);
176 }
177
178 void gPainter::clear()
179 {
180         gOpcode o;
181         o.opcode=gOpcode::clear;
182         o.dc = m_dc.grabRef();
183         o.parm.fill = new gOpcode::para::pfillRect;
184         o.parm.fill->area = eRect();
185         m_rc->submit(o);
186 }
187
188 void gPainter::blit(gPixmap *pixmap, ePoint pos, const eRect &clip, int flags)
189 {
190         gOpcode o;
191         
192         o.opcode=gOpcode::blit;
193         o.dc = m_dc.grabRef();
194         pixmap->AddRef();
195         o.parm.blit  = new gOpcode::para::pblit;
196         o.parm.blit->pixmap = pixmap;
197         o.parm.blit->position = pos;
198         o.parm.blit->clip = clip;
199         o.flags=flags;
200         m_rc->submit(o);
201 }
202
203
204 void gPainter::setPalette(gRGB *colors, int start, int len)
205 {
206         gOpcode o;
207         o.opcode=gOpcode::setPalette;
208         o.dc = m_dc.grabRef();
209         gPalette *p=new gPalette;
210         
211         o.parm.setPalette = new gOpcode::para::psetPalette;
212         p->data=new gRGB[len];
213         
214         memcpy(p->data, colors, len*sizeof(gRGB));
215         p->start=start;
216         p->colors=len;
217         o.parm.setPalette->palette = p;
218         m_rc->submit(o);
219 }
220
221 void gPainter::mergePalette(gPixmap *target)
222 {
223         gOpcode o;
224         o.opcode=gOpcode::mergePalette;
225         o.dc = m_dc.grabRef();
226         target->AddRef();
227         o.parm.mergePalette->target = target;
228         m_rc->submit(o);
229 }
230
231 void gPainter::line(ePoint start, ePoint end)
232 {
233         gOpcode o;
234         o.opcode=gOpcode::line;
235         o.dc = m_dc.grabRef();
236         o.parm.line = new gOpcode::para::pline;
237         o.parm.line->start = start;
238         o.parm.line->end = end;
239         m_rc->submit(o);
240 }
241
242 void gPainter::setOffset(ePoint val)
243 {
244         gOpcode o;
245         o.opcode=gOpcode::setOffset;
246         o.dc = m_dc.grabRef();
247         o.parm.setOffset = new gOpcode::para::psetOffset;
248         o.parm.setOffset->rel = 0;
249         o.parm.setOffset->value = val;
250         m_rc->submit(o);
251 }
252
253 void gPainter::moveOffset(ePoint rel)
254 {
255         gOpcode o;
256         o.opcode=gOpcode::setOffset;
257         o.dc = m_dc.grabRef();
258         o.parm.setOffset = new gOpcode::para::psetOffset;
259         o.parm.setOffset->rel = 1;
260         o.parm.setOffset->value = rel;
261         m_rc->submit(o);
262 }
263
264 void gPainter::resetOffset()
265 {
266         gOpcode o;
267         o.opcode=gOpcode::setOffset;
268         o.dc = m_dc.grabRef();
269         o.parm.setOffset = new gOpcode::para::psetOffset;
270         o.parm.setOffset->rel = 0;
271         o.parm.setOffset->value = ePoint(0, 0);
272         m_rc->submit(o);
273 }
274
275 void gPainter::resetClip(const gRegion &region)
276 {
277         gOpcode o;
278         o.opcode = gOpcode::setClip;
279         o.dc = m_dc.grabRef();
280         o.parm.clip = new gOpcode::para::psetClip;
281         o.parm.clip->region = region;
282         m_rc->submit(o);
283 }
284
285 void gPainter::clip(const gRegion &region)
286 {
287         gOpcode o;
288         o.opcode = gOpcode::addClip;
289         o.dc = m_dc.grabRef();
290         o.parm.clip = new gOpcode::para::psetClip;
291         o.parm.clip->region = region;
292         m_rc->submit(o);
293 }
294
295 void gPainter::clippop()
296 {
297         gOpcode o;
298         o.opcode = gOpcode::popClip;
299         o.dc = m_dc.grabRef();
300         m_rc->submit(o);
301 }
302
303 void gPainter::flush()
304 {
305         gOpcode o;
306         o.opcode = gOpcode::flush;
307         o.dc = m_dc.grabRef();
308         m_rc->submit(o);
309 }
310
311 void gPainter::end()
312 {
313 }
314
315 gDC::gDC()
316 {
317 }
318
319 gDC::gDC(gPixmap *pixmap): m_pixmap(pixmap)
320 {
321 }
322
323 gDC::~gDC()
324 {
325 }
326
327 void gDC::exec(gOpcode *o)
328 {
329         switch (o->opcode)
330         {
331         case gOpcode::setBackgroundColor:
332                 m_background_color = o->parm.setColor->color;
333                 delete o->parm.setColor;
334                 break;
335         case gOpcode::setForegroundColor:
336                 m_foreground_color = o->parm.setColor->color;
337                 delete o->parm.setColor;
338                 break;
339         case gOpcode::setFont:
340                 m_current_font = o->parm.setFont->font;
341                 o->parm.setFont->font->Release();
342                 delete o->parm.setFont;
343                 break;
344         case gOpcode::renderText:
345         {
346                 ePtr<eTextPara> para = new eTextPara(o->parm.renderText->area);
347                 int flags = o->parm.renderText->flags;
348                 assert(m_current_font);
349                 para->setFont(m_current_font);
350                 para->renderString(o->parm.renderText->text, (flags & gPainter::RT_WRAP) ? RS_WRAP : 0);
351                 
352                 if (flags & gPainter::RT_HALIGN_RIGHT)
353                         para->realign(eTextPara::dirRight);
354                 else if (flags & gPainter::RT_HALIGN_CENTER)
355                         para->realign(eTextPara::dirCenter);
356                 else if (flags & gPainter::RT_HALIGN_BLOCK)
357                         para->realign(eTextPara::dirBlock);
358                 
359                 ePoint offset = m_current_offset;
360                 
361                 if (o->parm.renderText->flags & gPainter::RT_VALIGN_CENTER)
362                 {
363                         eRect bbox = para->getBoundBox();
364                         offset += ePoint(0, (o->parm.renderText->area.height() - bbox.height()) / 2);
365                 }
366                 para->blit(*this, offset, getRGB(m_background_color), getRGB(m_foreground_color));
367                 delete o->parm.renderText;
368                 break;
369         }
370         case gOpcode::renderPara:
371         {
372                 o->parm.renderPara->textpara->blit(*this, o->parm.renderPara->offset + m_current_offset, getRGB(m_background_color), getRGB(m_foreground_color));
373                 o->parm.renderPara->textpara->Release();
374                 delete o->parm.renderPara;
375                 break;
376         }
377         case gOpcode::fill:
378         {
379                 eRect area = o->parm.fill->area;
380                 area.moveBy(m_current_offset);
381                 gRegion clip = m_current_clip & area;
382                 m_pixmap->fill(clip, m_foreground_color);
383                 delete o->parm.fill;
384                 break;
385         }
386         case gOpcode::fillRegion:
387         {
388                 o->parm.fillRegion->region.moveBy(m_current_offset);
389                 gRegion clip = m_current_clip & o->parm.fillRegion->region;
390                 m_pixmap->fill(clip, m_foreground_color);
391                 delete o->parm.fillRegion;
392                 break;
393         }
394         case gOpcode::clear:
395                 m_pixmap->fill(m_current_clip, m_background_color);
396                 delete o->parm.fill;
397                 break;
398         case gOpcode::blit:
399         {
400                 gRegion clip;
401                                 // this code should be checked again but i'm too tired now
402                 
403                 o->parm.blit->position += m_current_offset;
404                 
405                 if (o->parm.blit->clip.valid())
406                 {
407                         clip.intersect(gRegion(o->parm.blit->clip), clip);
408                 } else
409                         clip = m_current_clip;
410                 
411                 m_pixmap->blit(*o->parm.blit->pixmap, o->parm.blit->position, clip, o->parm.blit->flags);
412                 o->parm.blit->pixmap->Release();
413                 delete o->parm.blit;
414                 break;
415         }
416         case gOpcode::setPalette:
417                 if (o->parm.setPalette->palette->start > m_pixmap->surface->clut.colors)
418                         o->parm.setPalette->palette->start = m_pixmap->surface->clut.colors;
419                 if (o->parm.setPalette->palette->colors > (m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start))
420                         o->parm.setPalette->palette->colors = m_pixmap->surface->clut.colors-o->parm.setPalette->palette->start;
421                 if (o->parm.setPalette->palette->colors)
422                         memcpy(m_pixmap->surface->clut.data+o->parm.setPalette->palette->start, o->parm.setPalette->palette->data, o->parm.setPalette->palette->colors*sizeof(gRGB));
423                 
424                 delete[] o->parm.setPalette->palette->data;
425                 delete o->parm.setPalette->palette;
426                 delete o->parm.setPalette;
427                 break;
428         case gOpcode::mergePalette:
429 #if 0
430                 pixmap->mergePalette(*o->parm.blit->pixmap);
431                 o->parm.blit->pixmap->unlock();
432                 delete o->parm.blit;
433 #endif
434                 break;
435         case gOpcode::line:
436         {
437                 ePoint start = o->parm.line->start + m_current_offset, end = o->parm.line->end + m_current_offset;
438                 m_pixmap->line(m_current_clip, start, end, m_foreground_color);
439                 delete o->parm.line;
440                 break;
441         }
442         case gOpcode::addClip:
443                 m_clip_stack.push(m_current_clip);
444                 o->parm.clip->region.moveBy(m_current_offset);
445                 m_current_clip &= o->parm.clip->region;
446                 delete o->parm.clip;
447                 break;
448         case gOpcode::setClip:
449                 o->parm.clip->region.moveBy(m_current_offset);
450                 m_current_clip = o->parm.clip->region & eRect(ePoint(0, 0), m_pixmap->getSize());
451                 delete o->parm.clip;
452                 break;
453         case gOpcode::popClip:
454                 if (!m_clip_stack.empty())
455                 {
456                         m_current_clip = m_clip_stack.top();
457                         m_clip_stack.pop();
458                 }
459                 break;
460         case gOpcode::setOffset:
461                 if (o->parm.setOffset->rel)
462                         m_current_offset += o->parm.setOffset->value;
463                 else
464                         m_current_offset  = o->parm.setOffset->value;
465                 delete o->parm.setOffset;
466                 break;
467         case gOpcode::flush:
468                 break;
469         default:
470                 eFatal("illegal opcode %d. expect memory leak!", o->opcode);
471         }
472 }
473
474 gRGB gDC::getRGB(gColor col)
475 {
476         if ((!m_pixmap) || (!m_pixmap->surface->clut.data))
477                 return gRGB(col, col, col);
478         if (col<0)
479         {
480                 eFatal("bla transp");
481                 return gRGB(0, 0, 0, 0xFF);
482         }
483         return m_pixmap->surface->clut.data[col];
484 }
485
486 DEFINE_REF(gDC);
487
488 eAutoInitPtr<gRC> init_grc(eAutoInitNumbers::graphic, "gRC");
489