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