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