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