225fd9de4da054cad05c2346e19f8318d1b21d44
[openblackhole/openblackhole-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 #include <pthread.h>
11 #include <stack>
12 #include <list>
13
14 #include <lib/base/estring.h>
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, clear,
33                 blit,
34
35                 setPalette,
36                 mergePalette,
37                 
38                 line,
39                 
40                 setBackgroundColor,
41                 setForegroundColor,
42                 
43                 setOffset, moveOffset,
44                 
45                 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 prenderText
59                 {
60                         eRect area;
61                         eString text;
62                         int flags;
63                 } *renderText;
64
65                 struct prenderPara
66                 {
67                         ePoint offset;
68                         eTextPara *textpara;
69                 } *renderPara;
70                 
71                 struct psetFont
72                 {
73                         gFont *font;
74                 } *setFont;
75
76                 struct psetPalette
77                 {
78                         gPalette *palette;
79                 } *setPalette;
80                 
81                 struct pblit
82                 {
83                         gPixmap *pixmap;
84                         ePoint position;
85                         int flags;
86                         gRegion *clip;
87                 } *blit;
88
89                 struct pmergePalette
90                 {
91                         gPixmap *target;
92                 } *mergePalette;
93                 
94                 struct pline
95                 {
96                         ePoint start, end;
97                 } *line;
98
99                 struct psetClip
100                 {
101                         gRegion *region;
102                 } *clip;
103                 
104                 struct psetColor
105                 {
106                         gColor color;
107                 } *setColor;
108                 
109                 struct psetOffset
110                 {
111                         ePoint value;
112                         int rel;
113                 } *setOffset;
114         } parm;
115
116         int flags;
117 };
118
119                 /* gRC is the singleton which controls the fifo and dispatches commands */
120 class gRC: public virtual iObject
121 {
122 DECLARE_REF;
123 private:
124         static gRC *instance;
125         
126         static void *thread_wrapper(void *ptr);
127         pthread_t the_thread;
128         void *thread();
129
130         queueRingBuffer<gOpcode> queue;
131 public:
132         eLock queuelock;
133         gRC();
134         virtual ~gRC();
135
136         void submit(const gOpcode &o)
137         {
138                 static int collected=0;
139                 queue.enqueue(o);
140                 collected++;
141                 if (o.opcode==gOpcode::end||o.opcode==gOpcode::shutdown)
142                 {
143                         queuelock.unlock(collected);
144 #ifdef SYNC_PAINT
145                         thread();
146 #endif
147                         collected=0;
148                 }
149         }
150
151         static gRC *getInstance();
152 };
153
154         /* gPainter is the user frontend, which in turn sends commands through gRC */
155 class gPainter
156 {
157         ePtr<gDC> m_dc;
158         ePtr<gRC> m_rc;
159         friend class gRC;
160
161         gOpcode *beginptr;
162         void begin(const eRect &rect);
163         void end();
164 public:
165         gPainter(gDC *dc, eRect rect=eRect());
166         virtual ~gPainter();
167
168         void setBackgroundColor(const gColor &color);
169         void setForegroundColor(const gColor &color);
170
171         void setFont(gFont *font);
172         void renderText(const eRect &position, const std::string &string, int flags=0);
173         void renderPara(eTextPara *para, ePoint offset=ePoint(0, 0));
174
175         void fill(const eRect &area);
176         
177         void clear();
178         
179         void blit(gPixmap *pixmap, ePoint pos, gRegion *clip = 0, int flags=0);
180
181         void setPalette(gRGB *colors, int start=0, int len=256);
182         void mergePalette(gPixmap *target);
183         
184         void line(ePoint start, ePoint end);
185
186         void setLogicalZero(ePoint abs);
187         void moveLogicalZero(ePoint rel);
188         void resetLogicalZero();
189         
190         void clip(const gRegion &clip);
191         void clippop();
192
193         void flush();
194 };
195
196 class gDC: public iObject
197 {
198 DECLARE_REF;
199 protected:
200         ePtr<gPixmap> m_pixmap;
201
202         ePtr<gRegion> m_clip_region;
203         gColor m_foregroundColor, m_backgroundColor;
204         ePtr<gFont> m_current_font;
205         ePoint m_current_offset;
206         gRegion m_current_clip;
207         
208 public:
209         void exec(gOpcode *opcode);
210         gDC(gPixmap *pixmap);
211         gDC();
212         virtual ~gDC();
213         gRegion &getClip() { return *m_clip_region; }
214         int getPixmap(ePtr<gPixmap> &pm) { pm = m_pixmap; return 0; }
215         gRGB getRGB(gColor col);
216         virtual eSize getSize() { return m_pixmap->getSize(); }
217 };
218
219 #endif