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