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