lib/gdi/font.cpp: call libfribidi for single lines on multi line texts to prevent...
[enigma2.git] / lib / gdi / grc.h
1 #ifndef __grc_h
2 #define __grc_h
3
4 /*
5         gPainter ist die high-level version. die highlevel daten werden zu low level opcodes ueber
6         die gRC-queue geschickt und landen beim gDC der hardwarespezifisch ist, meist aber auf einen
7         gPixmap aufsetzt (und damit unbeschleunigt ist).
8 */
9
10 // for debugging use:
11 //#define SYNC_PAINT
12 #undef SYNC_PAINT
13
14 #include <pthread.h>
15 #include <stack>
16 #include <list>
17
18 #include <string>
19 #include <lib/base/elock.h>
20 #include <lib/base/message.h>
21 #include <lib/gdi/erect.h>
22 #include <lib/gdi/gpixmap.h>
23 #include <lib/gdi/region.h>
24 #include <lib/gdi/gfont.h>
25 #include <lib/gdi/compositing.h>
26
27 class eTextPara;
28
29 class gDC;
30 struct gOpcode
31 {
32         enum Opcode
33         {
34                 renderText,
35                 renderPara,
36                 setFont,
37                 
38                 fill, fillRegion, clear,
39                 blit,
40
41                 setPalette,
42                 mergePalette,
43                 
44                 line,
45                 
46                 setBackgroundColor,
47                 setForegroundColor,
48                 
49                 setBackgroundColorRGB,
50                 setForegroundColorRGB,
51                 
52                 setOffset,
53                 
54                 setClip, addClip, popClip,
55                 
56                 flush,
57                 
58                 waitVSync,
59                 flip,
60                 notify,
61                 
62                 enableSpinner, disableSpinner, incrementSpinner,
63                 
64                 shutdown,
65                 
66                 setCompositing,
67         } opcode;
68
69         gDC *dc;
70         union para
71         {
72                 struct pfillRect
73                 {
74                         eRect area;
75                 } *fill;
76
77                 struct pfillRegion
78                 {
79                         gRegion region;
80                 } *fillRegion;
81
82                 struct prenderText
83                 {
84                         eRect area;
85                         char *text;
86                         int flags;
87                 } *renderText;
88
89                 struct prenderPara
90                 {
91                         ePoint offset;
92                         eTextPara *textpara;
93                 } *renderPara;
94                 
95                 struct psetFont
96                 {
97                         gFont *font;
98                 } *setFont;
99
100                 struct psetPalette
101                 {
102                         gPalette *palette;
103                 } *setPalette;
104                 
105                 struct pblit
106                 {
107                         gPixmap *pixmap;
108                         int flags;
109                         eRect position;
110                         eRect clip;
111                 } *blit;
112
113                 struct pmergePalette
114                 {
115                         gPixmap *target;
116                 } *mergePalette;
117                 
118                 struct pline
119                 {
120                         ePoint start, end;
121                 } *line;
122
123                 struct psetClip
124                 {
125                         gRegion region;
126                 } *clip;
127                 
128                 struct psetColor
129                 {
130                         gColor color;
131                 } *setColor;
132                 
133                 struct psetColorRGB
134                 {
135                         gRGB color;
136                 } *setColorRGB;
137                 
138                 struct psetOffset
139                 {
140                         ePoint value;
141                         int rel;
142                 } *setOffset;
143                 
144                 gCompositingData *setCompositing;
145         } parm;
146 };
147
148 #define MAXSIZE 2048
149
150                 /* gRC is the singleton which controls the fifo and dispatches commands */
151 class gRC: public iObject, public Object
152 {
153         DECLARE_REF(gRC);
154         friend class gPainter;
155         static gRC *instance;
156
157 #ifndef SYNC_PAINT
158         static void *thread_wrapper(void *ptr);
159         pthread_t the_thread;
160         pthread_mutex_t mutex;
161         pthread_cond_t cond;
162 #endif
163         void *thread();
164
165         gOpcode queue[MAXSIZE];
166         int rp, wp;
167
168         eFixedMessagePump<int> m_notify_pump;
169         void recv_notify(const int &i);
170
171         ePtr<gDC> m_spinner_dc;
172         int m_spinner_enabled;
173         
174         void enableSpinner();
175         void disableSpinner();
176         
177         ePtr<gCompositingData> m_compositing;
178
179         int m_prev_idle_count;
180 public:
181         gRC();
182         virtual ~gRC();
183
184         void submit(const gOpcode &o);
185
186         Signal0<void> notify;
187         
188         void setSpinnerDC(gDC *dc) { m_spinner_dc = dc; }
189         
190         static gRC *getInstance();
191 };
192
193         /* gPainter is the user frontend, which in turn sends commands through gRC */
194 class gPainter
195 {
196         ePtr<gDC> m_dc;
197         ePtr<gRC> m_rc;
198         friend class gRC;
199
200         gOpcode *beginptr;
201         void begin(const eRect &rect);
202         void end();
203 public:
204         gPainter(gDC *dc, eRect rect=eRect());
205         virtual ~gPainter();
206         
207         void setBackgroundColor(const gColor &color);
208         void setForegroundColor(const gColor &color);
209
210         void setBackgroundColor(const gRGB &color);
211         void setForegroundColor(const gRGB &color);
212
213         void setFont(gFont *font);
214                 /* flags only THESE: */
215         enum
216         {
217                         // todo, make mask. you cannot align both right AND center AND block ;)
218                 RT_HALIGN_LEFT = 0,  /* default */
219                 RT_HALIGN_RIGHT = 1,
220                 RT_HALIGN_CENTER = 2,
221                 RT_HALIGN_BLOCK = 4,
222                 
223                 RT_VALIGN_TOP = 0,  /* default */
224                 RT_VALIGN_CENTER = 8,
225                 RT_VALIGN_BOTTOM = 16,
226                 
227                 RT_WRAP = 32
228         };
229         void renderText(const eRect &position, const std::string &string, int flags=0);
230         
231         void renderPara(eTextPara *para, ePoint offset=ePoint(0, 0));
232
233         void fill(const eRect &area);
234         void fill(const gRegion &area);
235         
236         void clear();
237
238         enum
239         {
240                 BT_ALPHATEST = 1,
241                 BT_ALPHABLEND = 2,
242                 BT_SCALE = 4 /* will be automatically set by blitScale */
243         };
244
245         void blit(gPixmap *pixmap, ePoint pos, const eRect &clip=eRect(), int flags=0);
246         void blitScale(gPixmap *pixmap, const eRect &pos, const eRect &clip=eRect(), int flags=0, int aflags = BT_SCALE);
247
248         void setPalette(gRGB *colors, int start=0, int len=256);
249         void setPalette(gPixmap *source);
250         void mergePalette(gPixmap *target);
251         
252         void line(ePoint start, ePoint end);
253
254         void setOffset(ePoint abs);
255         void moveOffset(ePoint rel);
256         void resetOffset();
257         
258         void resetClip(const gRegion &clip);
259         void clip(const gRegion &clip);
260         void clippop();
261
262         void waitVSync();
263         void flip();
264         void notify();
265         void setCompositing(gCompositingData *comp);
266         
267         void flush();
268 };
269
270 class gDC: public iObject
271 {
272         DECLARE_REF(gDC);
273 protected:
274         ePtr<gPixmap> m_pixmap;
275
276         gColor m_foreground_color, m_background_color;
277         gRGB m_foreground_color_rgb, m_background_color_rgb;
278         ePtr<gFont> m_current_font;
279         ePoint m_current_offset;
280         
281         std::stack<gRegion> m_clip_stack;
282         gRegion m_current_clip;
283         
284         ePtr<gPixmap> m_spinner_saved, m_spinner_temp;
285         ePtr<gPixmap> *m_spinner_pic;
286         eRect m_spinner_pos;
287         int m_spinner_num, m_spinner_i;
288 public:
289         virtual void exec(gOpcode *opcode);
290         gDC(gPixmap *pixmap);
291         gDC();
292         virtual ~gDC();
293         gRegion &getClip() { return m_current_clip; }
294         int getPixmap(ePtr<gPixmap> &pm) { pm = m_pixmap; return 0; }
295         gRGB getRGB(gColor col);
296         virtual eSize size() { return m_pixmap->size(); }
297         virtual int islocked() { return 0; }
298         
299         void enableSpinner();
300         void disableSpinner();
301         void incrementSpinner();
302         void setSpinner(eRect pos, ePtr<gPixmap> *pic, int len);
303 };
304
305 #endif