-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.c
209 lines (175 loc) · 4.24 KB
/
draw.c
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
#include <u.h>
#include <libc.h>
#include <draw.h>
#include "dat.h"
#include "fns.h"
static Image* pad;
static Image* icache[3][600];
static void padresize(Rdp*);
static void drawupd1(Rdp*,Imgupd*);
extern /* load.c */
int loadbmp(Image*,Rectangle,uchar*,int,uchar*);
int loadrle(Image*,Rectangle,uchar*,int,uchar*);
static int
depth2chan(int depth)
{
int chan;
switch(depth){
default: sysfatal("Unsupported remote color depth: %uhd\n", depth);
case 8: chan = CMAP8; break;
case 15: chan = RGB15; break;
case 16: chan = RGB16; break;
case 24: chan = RGB24; break;
case 32: chan = XRGB32; break;
}
return chan;
}
void
initscreen(Rdp* c)
{
if(initdraw(drawerror, nil, c->label) < 0)
sysfatal("initdraw: %r");
display->locking = 1;
unlockdisplay(display);
c->ysz = Dy(screen->r);
c->xsz = (Dx(screen->r) +3) & ~3;
}
void
drawimgupdate(Rdp *c, Share* s)
{
int (*getupd)(Imgupd*, uchar*, uint);
uchar* p, *ep;
int n, nr;
Imgupd u;
switch(s->type){
default: sysfatal("drawimgupdate: bad s->type");
case ShUimg: getupd = getimgupd; break;
case ShUorders: getupd = getfupd; break;
}
p = s->data;
ep = s->data + s->ndata;
nr = s->nr;
if(display->locking)
lockdisplay(display);
while(p<ep && nr>0){
if((n = getupd(&u, p, ep-p)) < 0)
sysfatal("getimgupd: %r");
drawupd1(c, &u);
p += n;
nr--;
}
flushimage(display, 1);
if(display->locking)
unlockdisplay(display);
}
static void
padresize(Rdp* c)
{
Rectangle rs;
int chan;
rs = rectaddpt(Rpt(ZP, Pt(c->xsz+4, c->ysz+4)), screen->r.min);
if(pad==nil || eqrect(pad->r, rs) == 0){
chan = depth2chan(c->depth);
freeimage(pad);
pad = allocimage(display, rs, chan, 0, DNofill);
if(pad==nil)
sysfatal("drawimgupdate: %r");
}
}
static void
imgupd(Rdp* c, Imgupd* up)
{
Rectangle r;
int (*loadfn)(Image*,Rectangle,uchar*,int,uchar*);
padresize(c); // BUG call elsewhere - whenever c->xsz changes
if(up->depth != pad->depth)
sysfatal("bad image depth");
loadfn = loadbmp;
if(up->compressed)
loadfn = loadrle;
r = rectaddpt(Rect(up->x, up->y, up->x+up->xsz, up->y+up->ysz), screen->r.min);
if(loadfn(pad, r, up->bytes, up->nbytes, c->cmap) < 0)
sysfatal("drawimgupdate: %r");
r = rectaddpt(Rect(up->x, up->y, up->xm+1, up->ym+1), screen->r.min);
draw(screen, r, pad, nil, r.min);
}
static void
scrblt(Rdp* c, Imgupd* up)
{
Rectangle r;
Point p;
USED(c);
r = rectaddpt(Rect(up->x, up->y, up->x+up->xsz, up->y+up->ysz), screen->r.min);
p = addpt(Pt(up->sx, up->sy), screen->r.min);
draw(screen, r, screen, nil, p);
}
static void
memblt(Rdp* c, Imgupd* up)
{
Image* img;
Rectangle clipr, r;
Point pt;
USED(c);
if(up->cid >= nelem(icache) || up->coff >= nelem(*icache)){
fprint(2, "drawmemimg: bad cache spec [%d %d]\n", up->cid, up->coff);
return;
}
img = icache[up->cid][up->coff];
if(img == nil){
fprint(2, "drawmemimg: empty cache entry cid %d coff %d\n", up->cid, up->coff);
return;
}
if(up->clip){
clipr = Rect(up->cx, up->cy, up->cx+up->cxsz, up->cy+up->cysz);
replclipr(screen, screen->repl, rectaddpt(clipr, screen->r.min));
}
r = Rect(up->x, up->y, up->xm+1, up->ym+1);
r = rectaddpt(r, screen->r.min);
pt = Pt(up->sx, up->sy);
draw(screen, r, img, nil, pt);
if(up->clip)
replclipr(screen, screen->repl, screen->r);
}
static void
cacheimg(Rdp* c, Imgupd* u)
{
int (*loadfn)(Image*,Rectangle,uchar*,int,uchar*);
int chan;
Image* img;
Rectangle r;
loadfn = loadbmp;
if(u->compressed)
loadfn = loadrle;
r = Rect(0, 0, u->xsz, u->ysz);
if(u->cid >= nelem(icache) || u->coff >= nelem(*icache))
sysfatal("cacheimg: bad cache spec [%d %d]", u->cid, u->coff);
img = icache[u->cid][u->coff];
if(img==nil || eqrect(img->r, r)==0){
chan = depth2chan(c->depth);
freeimage(img);
img = allocimage(display, r, chan, 0, DNofill);
if(img == nil)
sysfatal("cacheimg: %r");
icache[u->cid][u->coff] = img;
}
if(loadfn(img, r, u->bytes, u->nbytes, c->cmap) < 0)
sysfatal("loadmemimg: %r");
}
static void
cachecmap(Rdp* c, Imgupd* up)
{
/* BUG: who cares? */
USED(c);
USED(up);
}
static void
drawupd1(Rdp* c, Imgupd* up)
{
switch(up->type){
case Ubitmap: imgupd(c, up); break;
case Uscrblt: scrblt(c, up); break;
case Umemblt: memblt(c, up); break;
case Ucacheimg: cacheimg(c, up); break;
case Umcache: cachecmap(c, up); break;
}
}