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