generic language update
[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 private:
150         friend class gPainter;
151         static gRC *instance;
152
153 #ifndef SYNC_PAINT
154         static void *thread_wrapper(void *ptr);
155         pthread_t the_thread;
156         pthread_mutex_t mutex;
157         pthread_cond_t cond;
158 #endif
159         void *thread();
160
161         gOpcode queue[MAXSIZE];
162         int rp, wp;
163
164         eFixedMessagePump<int> m_notify_pump;
165         void recv_notify(const int &i);
166
167         ePtr<gDC> m_spinner_dc;
168         int m_spinner_enabled;
169         
170         void enableSpinner();
171         void disableSpinner();
172
173 public:
174         gRC();
175         virtual ~gRC();
176
177         void submit(const gOpcode &o);
178
179         Signal0<void> notify;
180         
181         void setSpinnerDC(gDC *dc) { m_spinner_dc = dc; }
182         
183         static gRC *getInstance();
184 };
185
186         /* gPainter is the user frontend, which in turn sends commands through gRC */
187 class gPainter
188 {
189         ePtr<gDC> m_dc;
190         ePtr<gRC> m_rc;
191         friend class gRC;
192
193         gOpcode *beginptr;
194         void begin(const eRect &rect);
195         void end();
196 public:
197         gPainter(gDC *dc, eRect rect=eRect());
198         virtual ~gPainter();
199         
200         void setBackgroundColor(const gColor &color);
201         void setForegroundColor(const gColor &color);
202
203         void setBackgroundColor(const gRGB &color);
204         void setForegroundColor(const gRGB &color);
205
206         void setFont(gFont *font);
207                 /* flags only THESE: */
208         enum
209         {
210                         // todo, make mask. you cannot align both right AND center AND block ;)
211                 RT_HALIGN_LEFT = 0,  /* default */
212                 RT_HALIGN_RIGHT = 1,
213                 RT_HALIGN_CENTER = 2,
214                 RT_HALIGN_BLOCK = 4,
215                 
216                 RT_VALIGN_TOP = 0,  /* default */
217                 RT_VALIGN_CENTER = 8,
218                 RT_VALIGN_BOTTOM = 16,
219                 
220                 RT_WRAP = 32
221         };
222         void renderText(const eRect &position, const std::string &string, int flags=0);
223         
224         void renderPara(eTextPara *para, ePoint offset=ePoint(0, 0));
225
226         void fill(const eRect &area);
227         void fill(const gRegion &area);
228         
229         void clear();
230
231         enum
232         {
233                 BT_ALPHATEST = 1,
234                 BT_ALPHABLEND = 2
235         };
236
237         void blit(gPixmap *pixmap, ePoint pos, const eRect &what=eRect(), int flags=0);
238
239         void setPalette(gRGB *colors, int start=0, int len=256);
240         void setPalette(gPixmap *source);
241         void mergePalette(gPixmap *target);
242         
243         void line(ePoint start, ePoint end);
244
245         void setOffset(ePoint abs);
246         void moveOffset(ePoint rel);
247         void resetOffset();
248         
249         void resetClip(const gRegion &clip);
250         void clip(const gRegion &clip);
251         void clippop();
252
253         void waitVSync();
254         void flip();
255         void notify();
256 };
257
258 class gDC: public iObject
259 {
260 DECLARE_REF(gDC);
261 protected:
262         ePtr<gPixmap> m_pixmap;
263
264         gColor m_foreground_color, m_background_color;
265         gRGB m_foreground_color_rgb, m_background_color_rgb;
266         ePtr<gFont> m_current_font;
267         ePoint m_current_offset;
268         
269         std::stack<gRegion> m_clip_stack;
270         gRegion m_current_clip;
271         
272         ePtr<gPixmap> m_spinner_saved, m_spinner_temp;
273         ePtr<gPixmap> *m_spinner_pic;
274         eRect m_spinner_pos;
275         int m_spinner_num, m_spinner_i;
276 public:
277         virtual void exec(gOpcode *opcode);
278         gDC(gPixmap *pixmap);
279         gDC();
280         virtual ~gDC();
281         gRegion &getClip() { return m_current_clip; }
282         int getPixmap(ePtr<gPixmap> &pm) { pm = m_pixmap; return 0; }
283         gRGB getRGB(gColor col);
284         virtual eSize size() { return m_pixmap->size(); }
285         virtual int islocked() { return 0; }
286         
287         void enableSpinner();
288         void disableSpinner();
289         void incrementSpinner();
290         void setSpinner(eRect pos, ePtr<gPixmap> *pic, int len);
291 };
292
293 #endif