1 |
matty |
9 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Cache routines |
4 |
matthewc |
207 |
Copyright (C) Matthew Chapman 1999-2002 |
5 |
matty |
9 |
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
matty |
10 |
#include "rdesktop.h" |
22 |
matty |
9 |
|
23 |
matty |
10 |
#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0])) |
24 |
jsorg71 |
725 |
#define TOUCH(id, idx) (g_bmpcache[id][idx].usage = ++g_stamp) |
25 |
|
|
#define IS_PERSISTENT(id) (g_pstcache_fd[id] > 0) |
26 |
matty |
10 |
|
27 |
jsorg71 |
725 |
extern int g_pstcache_fd[]; |
28 |
matty |
10 |
|
29 |
jsorg71 |
725 |
uint32 g_stamp; |
30 |
|
|
int g_num_bitmaps_in_memory[3]; |
31 |
|
|
|
32 |
|
|
|
33 |
matty |
10 |
/* BITMAP CACHE */ |
34 |
jsorg71 |
725 |
static BMPCACHEENTRY g_bmpcache[3][0xa00]; |
35 |
jsorg71 |
730 |
static HBITMAP g_volatile_bc[3]; |
36 |
matty |
10 |
|
37 |
jsorg71 |
725 |
/* Remove the least-recently used bitmap from the cache */ |
38 |
|
|
void |
39 |
|
|
cache_remove_lru_bitmap(uint8 cache_id) |
40 |
|
|
{ |
41 |
|
|
int i; |
42 |
|
|
uint16 cache_idx = 0; |
43 |
stargo |
726 |
uint32 m = (uint32)-1; |
44 |
jsorg71 |
725 |
BMPCACHEENTRY *pbce; |
45 |
|
|
|
46 |
|
|
for (i = 0; i < NUM_ELEMENTS(g_bmpcache[cache_id]); i++) |
47 |
|
|
{ |
48 |
|
|
if (g_bmpcache[cache_id][i].bitmap && g_bmpcache[cache_id][i].usage < m) |
49 |
|
|
{ |
50 |
|
|
cache_idx = i; |
51 |
|
|
m = g_bmpcache[cache_id][i].usage; |
52 |
|
|
} |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
pbce = &g_bmpcache[cache_id][cache_idx]; |
56 |
|
|
ui_destroy_bitmap(pbce->bitmap); |
57 |
|
|
--g_num_bitmaps_in_memory[cache_id]; |
58 |
|
|
pbce->bitmap = 0; |
59 |
|
|
pbce->usage = 0; |
60 |
|
|
} |
61 |
|
|
|
62 |
matty |
10 |
/* Retrieve a bitmap from the cache */ |
63 |
matty |
25 |
HBITMAP |
64 |
|
|
cache_get_bitmap(uint8 cache_id, uint16 cache_idx) |
65 |
matty |
9 |
{ |
66 |
jsorg71 |
725 |
HBITMAP *pbitmap; |
67 |
matty |
9 |
|
68 |
jsorg71 |
379 |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
69 |
matty |
9 |
{ |
70 |
jsorg71 |
725 |
pbitmap = &g_bmpcache[cache_id][cache_idx].bitmap; |
71 |
|
|
if ((*pbitmap != 0) || pstcache_load_bitmap(cache_id, cache_idx)) |
72 |
|
|
{ |
73 |
|
|
if (IS_PERSISTENT(cache_id)) |
74 |
|
|
TOUCH(cache_id, cache_idx); |
75 |
|
|
|
76 |
|
|
return *pbitmap; |
77 |
|
|
} |
78 |
matty |
9 |
} |
79 |
jsorg71 |
730 |
else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff)) |
80 |
|
|
{ |
81 |
|
|
return g_volatile_bc[cache_id]; |
82 |
|
|
} |
83 |
matty |
9 |
|
84 |
matty |
30 |
error("get bitmap %d:%d\n", cache_id, cache_idx); |
85 |
matty |
9 |
return NULL; |
86 |
|
|
} |
87 |
|
|
|
88 |
matty |
10 |
/* Store a bitmap in the cache */ |
89 |
matty |
25 |
void |
90 |
jsorg71 |
725 |
cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap, uint32 stamp) |
91 |
matty |
9 |
{ |
92 |
|
|
HBITMAP old; |
93 |
|
|
|
94 |
jsorg71 |
379 |
if ((cache_id < NUM_ELEMENTS(g_bmpcache)) && (cache_idx < NUM_ELEMENTS(g_bmpcache[0]))) |
95 |
matty |
9 |
{ |
96 |
jsorg71 |
725 |
old = g_bmpcache[cache_id][cache_idx].bitmap; |
97 |
matty |
9 |
if (old != NULL) |
98 |
jsorg71 |
725 |
{ |
99 |
matty |
10 |
ui_destroy_bitmap(old); |
100 |
jsorg71 |
725 |
} |
101 |
|
|
else |
102 |
|
|
{ |
103 |
|
|
if (++g_num_bitmaps_in_memory[cache_id] > BMPCACHE2_C2_CELLS) |
104 |
|
|
cache_remove_lru_bitmap(cache_id); |
105 |
|
|
} |
106 |
matty |
9 |
|
107 |
jsorg71 |
725 |
g_bmpcache[cache_id][cache_idx].bitmap = bitmap; |
108 |
|
|
g_bmpcache[cache_id][cache_idx].usage = stamp; |
109 |
matty |
9 |
} |
110 |
jsorg71 |
730 |
else if ((cache_id < NUM_ELEMENTS(g_volatile_bc)) && (cache_idx == 0x7fff)) |
111 |
|
|
{ |
112 |
|
|
old = g_volatile_bc[cache_id]; |
113 |
|
|
if (old != NULL) |
114 |
|
|
ui_destroy_bitmap(old); |
115 |
|
|
g_volatile_bc[cache_id] = bitmap; |
116 |
|
|
} |
117 |
matty |
9 |
else |
118 |
|
|
{ |
119 |
matty |
30 |
error("put bitmap %d:%d\n", cache_id, cache_idx); |
120 |
matty |
9 |
} |
121 |
|
|
} |
122 |
|
|
|
123 |
jsorg71 |
725 |
/* Updates the persistent bitmap cache MRU information on exit */ |
124 |
|
|
void |
125 |
|
|
cache_save_state(void) |
126 |
|
|
{ |
127 |
|
|
int id, idx; |
128 |
matty |
10 |
|
129 |
jsorg71 |
725 |
for (id = 0; id < NUM_ELEMENTS(g_bmpcache); id++) |
130 |
|
|
if (IS_PERSISTENT(id)) |
131 |
|
|
for (idx = 0; idx < NUM_ELEMENTS(g_bmpcache[id]); idx++) |
132 |
|
|
pstcache_touch_bitmap(id, idx, g_bmpcache[id][idx].usage); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
|
136 |
matty |
10 |
/* FONT CACHE */ |
137 |
jsorg71 |
379 |
static FONTGLYPH g_fontcache[12][256]; |
138 |
matty |
10 |
|
139 |
|
|
/* Retrieve a glyph from the font cache */ |
140 |
matty |
25 |
FONTGLYPH * |
141 |
|
|
cache_get_font(uint8 font, uint16 character) |
142 |
matty |
9 |
{ |
143 |
matty |
10 |
FONTGLYPH *glyph; |
144 |
matty |
9 |
|
145 |
jsorg71 |
379 |
if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0]))) |
146 |
matty |
9 |
{ |
147 |
jsorg71 |
379 |
glyph = &g_fontcache[font][character]; |
148 |
matty |
9 |
if (glyph->pixmap != NULL) |
149 |
|
|
return glyph; |
150 |
|
|
} |
151 |
|
|
|
152 |
matty |
30 |
error("get font %d:%d\n", font, character); |
153 |
matty |
9 |
return NULL; |
154 |
|
|
} |
155 |
|
|
|
156 |
matty |
10 |
/* Store a glyph in the font cache */ |
157 |
matty |
25 |
void |
158 |
|
|
cache_put_font(uint8 font, uint16 character, uint16 offset, |
159 |
|
|
uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap) |
160 |
matty |
9 |
{ |
161 |
matty |
10 |
FONTGLYPH *glyph; |
162 |
matty |
9 |
|
163 |
jsorg71 |
379 |
if ((font < NUM_ELEMENTS(g_fontcache)) && (character < NUM_ELEMENTS(g_fontcache[0]))) |
164 |
matty |
9 |
{ |
165 |
jsorg71 |
379 |
glyph = &g_fontcache[font][character]; |
166 |
matty |
9 |
if (glyph->pixmap != NULL) |
167 |
matty |
10 |
ui_destroy_glyph(glyph->pixmap); |
168 |
matty |
9 |
|
169 |
matty |
20 |
glyph->offset = offset; |
170 |
matty |
9 |
glyph->baseline = baseline; |
171 |
|
|
glyph->width = width; |
172 |
|
|
glyph->height = height; |
173 |
|
|
glyph->pixmap = pixmap; |
174 |
|
|
} |
175 |
|
|
else |
176 |
|
|
{ |
177 |
matty |
30 |
error("put font %d:%d\n", font, character); |
178 |
matty |
9 |
} |
179 |
|
|
} |
180 |
|
|
|
181 |
matty |
10 |
|
182 |
|
|
/* TEXT CACHE */ |
183 |
jsorg71 |
379 |
static DATABLOB g_textcache[256]; |
184 |
matty |
10 |
|
185 |
|
|
/* Retrieve a text item from the cache */ |
186 |
matty |
25 |
DATABLOB * |
187 |
|
|
cache_get_text(uint8 cache_id) |
188 |
matty |
9 |
{ |
189 |
matty |
10 |
DATABLOB *text; |
190 |
matty |
9 |
|
191 |
jsorg71 |
379 |
if (cache_id < NUM_ELEMENTS(g_textcache)) |
192 |
matty |
9 |
{ |
193 |
jsorg71 |
379 |
text = &g_textcache[cache_id]; |
194 |
matty |
9 |
if (text->data != NULL) |
195 |
|
|
return text; |
196 |
|
|
} |
197 |
|
|
|
198 |
matty |
30 |
error("get text %d\n", cache_id); |
199 |
matty |
9 |
return NULL; |
200 |
|
|
} |
201 |
|
|
|
202 |
matty |
10 |
/* Store a text item in the cache */ |
203 |
matty |
25 |
void |
204 |
|
|
cache_put_text(uint8 cache_id, void *data, int length) |
205 |
matty |
9 |
{ |
206 |
matty |
10 |
DATABLOB *text; |
207 |
matty |
9 |
|
208 |
jsorg71 |
379 |
if (cache_id < NUM_ELEMENTS(g_textcache)) |
209 |
matty |
9 |
{ |
210 |
jsorg71 |
379 |
text = &g_textcache[cache_id]; |
211 |
matty |
9 |
if (text->data != NULL) |
212 |
matty |
10 |
xfree(text->data); |
213 |
matty |
9 |
|
214 |
matty |
10 |
text->data = xmalloc(length); |
215 |
matty |
9 |
text->size = length; |
216 |
|
|
memcpy(text->data, data, length); |
217 |
|
|
} |
218 |
|
|
else |
219 |
|
|
{ |
220 |
matty |
30 |
error("put text %d\n", cache_id); |
221 |
matty |
9 |
} |
222 |
|
|
} |
223 |
matty |
10 |
|
224 |
|
|
|
225 |
|
|
/* DESKTOP CACHE */ |
226 |
jsorg71 |
379 |
static uint8 g_deskcache[0x38400 * 4]; |
227 |
matty |
10 |
|
228 |
|
|
/* Retrieve desktop data from the cache */ |
229 |
matty |
25 |
uint8 * |
230 |
matty |
28 |
cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel) |
231 |
matty |
10 |
{ |
232 |
matty |
28 |
int length = cx * cy * bytes_per_pixel; |
233 |
matty |
16 |
|
234 |
jsorg71 |
563 |
if (offset > sizeof(g_deskcache)) |
235 |
|
|
offset = 0; |
236 |
|
|
|
237 |
jsorg71 |
379 |
if ((offset + length) <= sizeof(g_deskcache)) |
238 |
matty |
10 |
{ |
239 |
jsorg71 |
379 |
return &g_deskcache[offset]; |
240 |
matty |
10 |
} |
241 |
|
|
|
242 |
matty |
30 |
error("get desktop %d:%d\n", offset, length); |
243 |
matty |
10 |
return NULL; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
/* Store desktop data in the cache */ |
247 |
matty |
25 |
void |
248 |
astrand |
82 |
cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 * data) |
249 |
matty |
10 |
{ |
250 |
matty |
28 |
int length = cx * cy * bytes_per_pixel; |
251 |
matty |
16 |
|
252 |
jsorg71 |
563 |
if (offset > sizeof(g_deskcache)) |
253 |
|
|
offset = 0; |
254 |
|
|
|
255 |
jsorg71 |
379 |
if ((offset + length) <= sizeof(g_deskcache)) |
256 |
matty |
10 |
{ |
257 |
matty |
28 |
cx *= bytes_per_pixel; |
258 |
matty |
16 |
while (cy--) |
259 |
|
|
{ |
260 |
jsorg71 |
379 |
memcpy(&g_deskcache[offset], data, cx); |
261 |
matty |
16 |
data += scanline; |
262 |
|
|
offset += cx; |
263 |
|
|
} |
264 |
matty |
10 |
} |
265 |
|
|
else |
266 |
|
|
{ |
267 |
matty |
30 |
error("put desktop %d:%d\n", offset, length); |
268 |
matty |
10 |
} |
269 |
|
|
} |
270 |
matty |
28 |
|
271 |
|
|
|
272 |
|
|
/* CURSOR CACHE */ |
273 |
jsorg71 |
379 |
static HCURSOR g_cursorcache[0x20]; |
274 |
matty |
28 |
|
275 |
|
|
/* Retrieve cursor from cache */ |
276 |
astrand |
64 |
HCURSOR |
277 |
|
|
cache_get_cursor(uint16 cache_idx) |
278 |
matty |
28 |
{ |
279 |
|
|
HCURSOR cursor; |
280 |
|
|
|
281 |
jsorg71 |
379 |
if (cache_idx < NUM_ELEMENTS(g_cursorcache)) |
282 |
matty |
28 |
{ |
283 |
jsorg71 |
379 |
cursor = g_cursorcache[cache_idx]; |
284 |
matty |
28 |
if (cursor != NULL) |
285 |
|
|
return cursor; |
286 |
|
|
} |
287 |
|
|
|
288 |
matty |
30 |
error("get cursor %d\n", cache_idx); |
289 |
matty |
28 |
return NULL; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
/* Store cursor in cache */ |
293 |
|
|
void |
294 |
|
|
cache_put_cursor(uint16 cache_idx, HCURSOR cursor) |
295 |
|
|
{ |
296 |
|
|
HCURSOR old; |
297 |
|
|
|
298 |
jsorg71 |
379 |
if (cache_idx < NUM_ELEMENTS(g_cursorcache)) |
299 |
matty |
28 |
{ |
300 |
jsorg71 |
379 |
old = g_cursorcache[cache_idx]; |
301 |
matty |
28 |
if (old != NULL) |
302 |
|
|
ui_destroy_cursor(old); |
303 |
|
|
|
304 |
jsorg71 |
379 |
g_cursorcache[cache_idx] = cursor; |
305 |
matty |
28 |
} |
306 |
|
|
else |
307 |
|
|
{ |
308 |
matty |
30 |
error("put cursor %d\n", cache_idx); |
309 |
matty |
28 |
} |
310 |
|
|
} |