Support turbo2.
[vuplus_dvbapp] / 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 // for debugging use:
11 //#define SYNC_PAINT
12 #undef SYNC_PAINT
13
14 #include <pthread.h>
15 #include <stack>
16 #include <list>
17
18 #include <string>
19 #include <lib/base/elock.h>
20 #include <lib/base/message.h>
21 #include <lib/gdi/erect.h>
22 #include <lib/gdi/gpixmap.h>
23 #include <lib/gdi/region.h>
24 #include <lib/gdi/gfont.h>
25 #include <lib/gdi/compositing.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                 enableSpinner, disableSpinner, incrementSpinner,
63                 
64                 shutdown,
65                 
66                 setCompositing,
67                 sendShow,
68                 sendHide,
69 #ifdef USE_LIBVUGLES2
70                 sendShowItem,
71                 setFlush,
72                 setView,
73 #endif
74         } opcode;
75
76         gDC *dc;
77         union para
78         {
79                 struct pfillRect
80                 {
81                         eRect area;
82                 } *fill;
83
84                 struct pfillRegion
85                 {
86                         gRegion region;
87                 } *fillRegion;
88
89                 struct prenderText
90                 {
91                         eRect area;
92                         char *text;
93                         int flags;
94                         int border;
95                         gRGB bordercolor;
96                 } *renderText;
97
98                 struct prenderPara
99                 {
100                         ePoint offset;
101                         eTextPara *textpara;
102                 } *renderPara;
103                 
104                 struct psetFont
105                 {
106                         gFont *font;
107                 } *setFont;
108
109                 struct psetPalette
110                 {
111                         gPalette *palette;
112                 } *setPalette;
113                 
114                 struct pblit
115                 {
116                         gPixmap *pixmap;
117                         int flags;
118                         eRect position;
119                         eRect clip;
120                 } *blit;
121
122                 struct pmergePalette
123                 {
124                         gPixmap *target;
125                 } *mergePalette;
126                 
127                 struct pline
128                 {
129                         ePoint start, end;
130                 } *line;
131
132                 struct psetClip
133                 {
134                         gRegion region;
135                 } *clip;
136                 
137                 struct psetColor
138                 {
139                         gColor color;
140                 } *setColor;
141                 
142                 struct psetColorRGB
143                 {
144                         gRGB color;
145                 } *setColorRGB;
146                 
147                 struct psetOffset
148                 {
149                         ePoint value;
150                         int rel;
151                 } *setOffset;
152                 
153                 gCompositingData *setCompositing;
154
155                 struct psetShowHideInfo
156                 {
157                         ePoint point;
158                         eSize size;
159                 } *setShowHideInfo;
160
161 #ifdef USE_LIBVUGLES2
162                 struct psetShowItemInfo
163                 {
164                         long dir;
165                         ePoint point;
166                         eSize size;
167                 } *setShowItemInfo;
168
169                 struct psetFlush
170                 {
171                         bool enable;
172                 } *setFlush;
173
174                 struct psetViewInfo
175                 {
176                         eSize size;
177                 } *setViewInfo;
178 #endif
179         } parm;
180 };
181
182 #define MAXSIZE 2048
183
184                 /* gRC is the singleton which controls the fifo and dispatches commands */
185 class gRC: public iObject, public Object
186 {
187         DECLARE_REF(gRC);
188         friend class gPainter;
189         static gRC *instance;
190
191 #ifndef SYNC_PAINT
192         static void *thread_wrapper(void *ptr);
193         pthread_t the_thread;
194         pthread_mutex_t mutex;
195         pthread_cond_t cond;
196 #endif
197         void *thread();
198
199         gOpcode queue[MAXSIZE];
200         int rp, wp;
201
202         eFixedMessagePump<int> m_notify_pump;
203         void recv_notify(const int &i);
204
205         ePtr<gDC> m_spinner_dc;
206         int m_spinner_enabled;
207         
208         void enableSpinner();
209         void disableSpinner();
210         
211         ePtr<gCompositingData> m_compositing;
212
213         int m_prev_idle_count;
214 public:
215         gRC();
216         virtual ~gRC();
217
218         void submit(const gOpcode &o);
219
220         Signal0<void> notify;
221         
222         void setSpinnerDC(gDC *dc) { m_spinner_dc = dc; }
223         
224         static gRC *getInstance();
225 };
226
227         /* gPainter is the user frontend, which in turn sends commands through gRC */
228 class gPainter
229 {
230         ePtr<gDC> m_dc;
231         ePtr<gRC> m_rc;
232         friend class gRC;
233
234         gOpcode *beginptr;
235         void begin(const eRect &rect);
236         void end();
237 public:
238         gPainter(gDC *dc, eRect rect=eRect());
239         virtual ~gPainter();
240         
241         void setBackgroundColor(const gColor &color);
242         void setForegroundColor(const gColor &color);
243
244         void setBackgroundColor(const gRGB &color);
245         void setForegroundColor(const gRGB &color);
246
247         void setFont(gFont *font);
248                 /* flags only THESE: */
249         enum
250         {
251                         // todo, make mask. you cannot align both right AND center AND block ;)
252                 RT_HALIGN_LEFT = 0,  /* default */
253                 RT_HALIGN_RIGHT = 1,
254                 RT_HALIGN_CENTER = 2,
255                 RT_HALIGN_BLOCK = 4,
256                 
257                 RT_VALIGN_TOP = 0,  /* default */
258                 RT_VALIGN_CENTER = 8,
259                 RT_VALIGN_BOTTOM = 16,
260                 
261                 RT_WRAP = 32
262         };
263         void renderText(const eRect &position, const std::string &string, int flags=0, gRGB bordercolor=gRGB(), int border=0);
264         
265         void renderPara(eTextPara *para, ePoint offset=ePoint(0, 0));
266
267         void fill(const eRect &area);
268         void fill(const gRegion &area);
269         
270         void clear();
271
272         enum
273         {
274                 BT_ALPHATEST = 1,
275                 BT_ALPHABLEND = 2,
276                 BT_SCALE = 4 /* will be automatically set by blitScale */
277         };
278
279         void blit(gPixmap *pixmap, ePoint pos, const eRect &clip=eRect(), int flags=0);
280         void blitScale(gPixmap *pixmap, const eRect &pos, const eRect &clip=eRect(), int flags=0, int aflags = BT_SCALE);
281
282         void setPalette(gRGB *colors, int start=0, int len=256);
283         void setPalette(gPixmap *source);
284         void mergePalette(gPixmap *target);
285         
286         void line(ePoint start, ePoint end);
287
288         void setOffset(ePoint abs);
289         void moveOffset(ePoint rel);
290         void resetOffset();
291         
292         void resetClip(const gRegion &clip);
293         void clip(const gRegion &clip);
294         void clippop();
295
296         void waitVSync();
297         void flip();
298         void notify();
299         void setCompositing(gCompositingData *comp);
300         
301         void flush();
302         void sendShow(ePoint point, eSize size);
303         void sendHide(ePoint point, eSize size);
304 #ifdef USE_LIBVUGLES2
305         void sendShowItem(long dir, ePoint point, eSize size);
306         void setFlush(bool val);
307         void setView(eSize size);
308 #endif
309 };
310
311 class gDC: public iObject
312 {
313         DECLARE_REF(gDC);
314 protected:
315         ePtr<gPixmap> m_pixmap;
316
317         gColor m_foreground_color, m_background_color;
318         gRGB m_foreground_color_rgb, m_background_color_rgb;
319         ePtr<gFont> m_current_font;
320         ePoint m_current_offset;
321         
322         std::stack<gRegion> m_clip_stack;
323         gRegion m_current_clip;
324         
325         ePtr<gPixmap> m_spinner_saved, m_spinner_temp;
326         ePtr<gPixmap> *m_spinner_pic;
327         eRect m_spinner_pos;
328         int m_spinner_num, m_spinner_i;
329 public:
330         virtual void exec(const gOpcode *opcode);
331         gDC(gPixmap *pixmap);
332         gDC();
333         virtual ~gDC();
334         gRegion &getClip() { return m_current_clip; }
335         int getPixmap(ePtr<gPixmap> &pm) { pm = m_pixmap; return 0; }
336         gRGB getRGB(gColor col);
337         virtual eSize size() { return m_pixmap->size(); }
338         virtual int islocked() { return 0; }
339         
340         virtual void enableSpinner();
341         virtual void disableSpinner();
342         virtual void incrementSpinner();
343         virtual void setSpinner(eRect pos, ePtr<gPixmap> *pic, int len);
344 };
345
346 #endif