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