- hopefully fixed some python/refcount stuff (__deref__ is still evil!)
[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 #include <pthread.h>
11 #include <stack>
12 #include <list>
13
14 #include <string>
15 #include <lib/base/ringbuffer.h>
16 #include <lib/base/elock.h>
17 #include <lib/gdi/erect.h>
18 #include <lib/gdi/gpixmap.h>
19 #include <lib/gdi/region.h>
20
21 class eTextPara;
22
23 class gDC;
24 struct gOpcode
25 {
26         enum Opcode
27         {
28                 renderText,
29                 renderPara,
30                 setFont,
31                 
32                 fill, fillRegion, clear,
33                 blit,
34
35                 setPalette,
36                 mergePalette,
37                 
38                 line,
39                 
40                 setBackgroundColor,
41                 setForegroundColor,
42                 
43                 setOffset,
44                 
45                 setClip, addClip, popClip,
46                 
47                 end,shutdown
48         } opcode;
49
50         gDC *dc;
51         union para
52         {
53                 struct pfillRect
54                 {
55                         eRect area;
56                 } *fill;
57
58                 struct pfillRegion
59                 {
60                         gRegion region;
61                 } *fillRegion;
62
63                 struct prenderText
64                 {
65                         eRect area;
66                         std::string text;
67                         int flags;
68                 } *renderText;
69
70                 struct prenderPara
71                 {
72                         ePoint offset;
73                         eTextPara *textpara;
74                 } *renderPara;
75                 
76                 struct psetFont
77                 {
78                         gFont *font;
79                 } *setFont;
80
81                 struct psetPalette
82                 {
83                         gPalette *palette;
84                 } *setPalette;
85                 
86                 struct pblit
87                 {
88                         gPixmap *pixmap;
89                         ePoint position;
90                         int flags;
91                         eRect clip;
92                 } *blit;
93
94                 struct pmergePalette
95                 {
96                         gPixmap *target;
97                 } *mergePalette;
98                 
99                 struct pline
100                 {
101                         ePoint start, end;
102                 } *line;
103
104                 struct psetClip
105                 {
106                         gRegion region;
107                 } *clip;
108                 
109                 struct psetColor
110                 {
111                         gColor color;
112                 } *setColor;
113                 
114                 struct psetOffset
115                 {
116                         ePoint value;
117                         int rel;
118                 } *setOffset;
119         } parm;
120
121         int flags;
122 };
123
124                 /* gRC is the singleton which controls the fifo and dispatches commands */
125 class gRC: public iObject
126 {
127 DECLARE_REF(gRC);
128 private:
129         static gRC *instance;
130         
131         static void *thread_wrapper(void *ptr);
132         pthread_t the_thread;
133         void *thread();
134
135         queueRingBuffer<gOpcode> queue;
136 public:
137         eLock queuelock;
138         gRC();
139         virtual ~gRC();
140
141         void submit(const gOpcode &o)
142         {
143                 static int collected=0;
144                 queue.enqueue(o);
145                 collected++;
146 //              if (o.opcode==gOpcode::end||o.opcode==gOpcode::shutdown)
147                 {
148                         queuelock.unlock(collected);
149 #ifdef SYNC_PAINT
150                         thread();
151 #endif
152                         collected=0;
153                 }
154         }
155
156         static gRC *getInstance();
157 };
158
159         /* gPainter is the user frontend, which in turn sends commands through gRC */
160 class gPainter
161 {
162         ePtr<gDC> m_dc;
163         ePtr<gRC> m_rc;
164         friend class gRC;
165
166         gOpcode *beginptr;
167         void begin(const eRect &rect);
168         void end();
169 public:
170         gPainter(gDC *dc, eRect rect=eRect());
171         virtual ~gPainter();
172
173         void setBackgroundColor(const gColor &color);
174         void setForegroundColor(const gColor &color);
175
176         void setFont(gFont *font);
177                 /* flags only THESE: */
178         enum
179         {
180                         // todo, make mask. you cannot align both right AND center AND block ;)
181                 RT_HALIGN_RIGHT = 1,
182                 RT_HALIGN_CENTER = 2,
183                 RT_HALIGN_BLOCK = 4,
184                 RT_VALIGN_CENTER = 8
185         };
186         void renderText(const eRect &position, const std::string &string, int flags=0);
187         
188         void renderPara(eTextPara *para, ePoint offset=ePoint(0, 0));
189
190         void fill(const eRect &area);
191         void fill(const gRegion &area);
192         
193         void clear();
194         
195         void blit(gPixmap *pixmap, ePoint pos, const eRect &what=eRect(), int flags=0);
196
197         void setPalette(gRGB *colors, int start=0, int len=256);
198         void mergePalette(gPixmap *target);
199         
200         void line(ePoint start, ePoint end);
201
202         void setOffset(ePoint abs);
203         void moveOffset(ePoint rel);
204         void resetOffset();
205         
206         void resetClip(const gRegion &clip);
207         void clip(const gRegion &clip);
208         void clippop();
209
210         void flush();
211 };
212
213 class gDC: public iObject
214 {
215 DECLARE_REF(gDC);
216 protected:
217         ePtr<gPixmap> m_pixmap;
218
219         gColor m_foreground_color, m_background_color;
220         ePtr<gFont> m_current_font;
221         ePoint m_current_offset;
222         
223         std::stack<gRegion> m_clip_stack;
224         gRegion m_current_clip;
225         
226 public:
227         virtual void exec(gOpcode *opcode);
228         gDC(gPixmap *pixmap);
229         gDC();
230         virtual ~gDC();
231         gRegion &getClip() { return m_current_clip; }
232         int getPixmap(ePtr<gPixmap> &pm) { pm = m_pixmap; return 0; }
233         gRGB getRGB(gColor col);
234         virtual eSize getSize() { return m_pixmap->getSize(); }
235 };
236
237 #endif