1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
#ifndef __grc_h
#define __grc_h
/*
gPainter ist die high-level version. die highlevel daten werden zu low level opcodes ueber
die gRC-queue geschickt und landen beim gDC der hardwarespezifisch ist, meist aber auf einen
gPixmap aufsetzt (und damit unbeschleunigt ist).
*/
#include <pthread.h>
#include <stack>
#include <list>
#include <lib/base/estring.h>
#include <lib/base/ringbuffer.h>
#include <lib/gdi/erect.h>
#include <lib/base/elock.h>
#include <lib/gdi/gpixmap.h>
class eTextPara;
class gDC;
struct gOpcode
{
enum Opcode
{
begin,
renderText,
renderPara,
fill,
blit,
setPalette,
mergePalette,
line,
clip,
flush,
end,
shutdown
} opcode;
union para
{
struct pbegin
{
eRect area;
pbegin(const eRect &area): area(area) { }
} *begin;
struct pfill
{
eRect area;
gColor color;
pfill(const eRect &area, gColor color): area(area), color(color) { }
} *fill;
struct prenderText
{
gFont font;
eRect area;
eString text;
gRGB foregroundColor, backgroundColor;
prenderText(const gFont &font, const eRect &area, const eString &text, const gRGB &foregroundColor, const gRGB &backgroundColor):
font(font), area(area), text(text), foregroundColor(foregroundColor), backgroundColor(backgroundColor) { }
} *renderText;
struct prenderPara
{
ePoint offset;
eTextPara *textpara;
gRGB foregroundColor, backgroundColor;
prenderPara(const ePoint &offset, eTextPara *textpara, const gRGB &foregroundColor, const gRGB &backgroundColor)
: offset(offset), textpara(textpara), foregroundColor(foregroundColor), backgroundColor(backgroundColor) { }
} *renderPara;
struct psetPalette
{
gPalette *palette;
psetPalette(gPalette *palette): palette(palette) { }
} *setPalette;
struct pblit
{
ePtr<gPixmap> pixmap;
ePoint position;
eRect clip;
pblit(gPixmap *pixmap, const ePoint &position, const eRect &clip)
: pixmap(pixmap), position(position), clip(clip) { }
} *blit;
struct pmergePalette
{
ePtr<gPixmap> target;
pmergePalette(gPixmap *target): target(target) { }
} *mergePalette;
struct pline
{
ePoint start, end;
gColor color;
pline(const ePoint &start, const ePoint &end, gColor color): start(start), end(end), color(color) { }
} *line;
struct pclip
{
eRect clip;
pclip(const eRect &clip): clip(clip) { }
} *clip;
} parm;
int flags;
gDC *dc;
};
class gRC
{
static gRC *instance;
static void *thread_wrapper(void *ptr);
pthread_t the_thread;
void *thread();
eLock queuelock;
queueRingBuffer<gOpcode> queue;
public:
gRC();
virtual ~gRC();
void submit(const gOpcode &o)
{
static int collected=0;
queue.enqueue(o);
collected++;
if (o.opcode==gOpcode::end)
{
queuelock.unlock(collected);
#ifdef SYNC_PAINT
thread();
#endif
collected=0;
}
}
static gRC &getInstance();
};
class gPainter
{
gDC &dc;
gRC &rc;
friend class gRC;
gOpcode *beginptr;
/* paint states */
// std::stack<eRect, std::list<eRect> > cliparea;
std::stack<eRect> cliparea;
gFont font;
gColor foregroundColor, backgroundColor;
ePoint logicalZero;
void begin(const eRect &rect);
void end();
public:
gPainter(gDC &dc, eRect rect=eRect());
virtual ~gPainter();
void setBackgroundColor(const gColor &color);
void setForegroundColor(const gColor &color);
void setFont(const gFont &font);
void renderText(const eRect &position, const std::string &string, int flags=0);
void renderPara(eTextPara ¶, ePoint offset=ePoint(0, 0));
void fill(const eRect &area);
void clear();
void gPainter::blit(gPixmap *pixmap, ePoint pos, eRect clip=eRect(), int flags=0)
{
gOpcode o;
o.dc=&dc;
o.opcode=gOpcode::blit;
pos+=logicalZero;
clip.moveBy(logicalZero.x(), logicalZero.y());
o.parm.blit=new gOpcode::para::pblit(pixmap, pos, clip);
o.flags=flags;
rc.submit(o);
}
void setPalette(gRGB *colors, int start=0, int len=256);
void mergePalette(gPixmap *target);
void line(ePoint start, ePoint end);
void setLogicalZero(ePoint abs);
void moveLogicalZero(ePoint rel);
void resetLogicalZero();
void clip(eRect clip);
void clippop();
void flush();
};
class gDC
{
protected:
eLock dclock;
public:
virtual void exec(gOpcode *opcode)=0;
virtual RESULT getPixmap(ePtr<gPixmap> &)=0;
virtual eSize getSize()=0;
virtual const eRect &getClip()=0;
virtual gRGB getRGB(gColor col)=0;
virtual ~gDC();
void lock() { dclock.lock(1); }
void unlock() { dclock.unlock(1); }
};
class gPixmapDC: public gDC
{
protected:
ePtr<gPixmap> pixmap;
eRect clip;
void exec(gOpcode *opcode);
gPixmapDC();
public:
gPixmapDC(gPixmap *pixmap);
virtual ~gPixmapDC();
RESULT getPixmap(ePtr<gPixmap> &ptr) { ptr = pixmap; return 0; }
gRGB getRGB(gColor col);
const eRect &getClip() { return clip; }
virtual eSize getSize() { return eSize(pixmap->x, pixmap->y); }
};
#endif
|