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