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